2018-07-18 09:40:26 +00:00
|
|
|
// Copyright 2017 The Dawn Authors
|
2017-04-20 18:38:20 +00:00
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2018-07-24 11:53:51 +00:00
|
|
|
#include "dawn_native/BindGroupLayout.h"
|
2017-04-20 18:38:20 +00:00
|
|
|
|
2020-03-11 01:28:48 +00:00
|
|
|
#include <functional>
|
|
|
|
|
2018-05-09 18:08:17 +00:00
|
|
|
#include "common/BitSetIterator.h"
|
2018-05-07 21:33:48 +00:00
|
|
|
#include "common/HashUtils.h"
|
2018-07-24 11:53:51 +00:00
|
|
|
#include "dawn_native/Device.h"
|
|
|
|
#include "dawn_native/ValidationUtils_autogen.h"
|
2017-04-20 18:38:20 +00:00
|
|
|
|
2018-07-24 14:45:45 +00:00
|
|
|
namespace dawn_native {
|
2017-04-20 18:38:20 +00:00
|
|
|
|
2020-03-09 00:45:18 +00:00
|
|
|
MaybeError ValidateBindingTypeWithShaderStageVisibility(
|
|
|
|
wgpu::BindingType bindingType,
|
|
|
|
wgpu::ShaderStage shaderStageVisibility) {
|
2020-03-11 01:28:48 +00:00
|
|
|
// TODO(jiawei.shao@intel.com): support read-write storage textures.
|
|
|
|
switch (bindingType) {
|
|
|
|
case wgpu::BindingType::StorageBuffer: {
|
|
|
|
if ((shaderStageVisibility & wgpu::ShaderStage::Vertex) != 0) {
|
|
|
|
return DAWN_VALIDATION_ERROR(
|
|
|
|
"storage buffer binding is not supported in vertex shader");
|
|
|
|
}
|
|
|
|
} break;
|
|
|
|
|
|
|
|
case wgpu::BindingType::WriteonlyStorageTexture: {
|
|
|
|
if ((shaderStageVisibility &
|
|
|
|
(wgpu::ShaderStage::Vertex | wgpu::ShaderStage::Fragment)) != 0) {
|
|
|
|
return DAWN_VALIDATION_ERROR(
|
|
|
|
"write-only storage texture binding is only supported in compute shader");
|
|
|
|
}
|
|
|
|
} break;
|
|
|
|
|
|
|
|
case wgpu::BindingType::StorageTexture: {
|
|
|
|
return DAWN_VALIDATION_ERROR("Read-write storage texture binding is not supported");
|
|
|
|
} break;
|
|
|
|
|
|
|
|
case wgpu::BindingType::UniformBuffer:
|
|
|
|
case wgpu::BindingType::ReadonlyStorageBuffer:
|
|
|
|
case wgpu::BindingType::Sampler:
|
|
|
|
case wgpu::BindingType::SampledTexture:
|
|
|
|
case wgpu::BindingType::ReadonlyStorageTexture:
|
|
|
|
break;
|
2020-03-09 00:45:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2018-07-25 15:03:23 +00:00
|
|
|
MaybeError ValidateBindGroupLayoutDescriptor(DeviceBase*,
|
|
|
|
const BindGroupLayoutDescriptor* descriptor) {
|
2018-09-10 14:17:24 +00:00
|
|
|
if (descriptor->nextInChain != nullptr) {
|
|
|
|
return DAWN_VALIDATION_ERROR("nextInChain must be nullptr");
|
|
|
|
}
|
2018-07-10 19:23:50 +00:00
|
|
|
|
|
|
|
std::bitset<kMaxBindingsPerGroup> bindingsSet;
|
2019-07-31 01:29:42 +00:00
|
|
|
uint32_t dynamicUniformBufferCount = 0;
|
|
|
|
uint32_t dynamicStorageBufferCount = 0;
|
2019-02-21 00:45:19 +00:00
|
|
|
for (uint32_t i = 0; i < descriptor->bindingCount; ++i) {
|
2019-07-15 10:29:08 +00:00
|
|
|
const BindGroupLayoutBinding& binding = descriptor->bindings[i];
|
2019-08-27 08:42:29 +00:00
|
|
|
DAWN_TRY(ValidateShaderStage(binding.visibility));
|
2018-07-18 09:45:17 +00:00
|
|
|
DAWN_TRY(ValidateBindingType(binding.type));
|
2019-08-21 12:16:33 +00:00
|
|
|
DAWN_TRY(ValidateTextureComponentType(binding.textureComponentType));
|
2018-07-10 19:23:50 +00:00
|
|
|
|
2019-10-23 11:57:41 +00:00
|
|
|
if (binding.textureDimension != wgpu::TextureViewDimension::Undefined) {
|
2019-09-10 08:58:28 +00:00
|
|
|
DAWN_TRY(ValidateTextureViewDimension(binding.textureDimension));
|
|
|
|
}
|
|
|
|
|
2018-11-21 09:47:19 +00:00
|
|
|
if (binding.binding >= kMaxBindingsPerGroup) {
|
2018-09-10 14:17:24 +00:00
|
|
|
return DAWN_VALIDATION_ERROR("some binding index exceeds the maximum value");
|
|
|
|
}
|
|
|
|
if (bindingsSet[binding.binding]) {
|
|
|
|
return DAWN_VALIDATION_ERROR("some binding index was specified more than once");
|
|
|
|
}
|
2019-07-15 10:29:08 +00:00
|
|
|
|
2020-03-09 00:45:18 +00:00
|
|
|
DAWN_TRY(
|
|
|
|
ValidateBindingTypeWithShaderStageVisibility(binding.type, binding.visibility));
|
2019-12-19 18:50:18 +00:00
|
|
|
|
2019-07-15 10:29:08 +00:00
|
|
|
switch (binding.type) {
|
2019-10-23 11:57:41 +00:00
|
|
|
case wgpu::BindingType::UniformBuffer:
|
2019-09-24 11:08:17 +00:00
|
|
|
if (binding.hasDynamicOffset) {
|
2019-07-31 01:29:42 +00:00
|
|
|
++dynamicUniformBufferCount;
|
|
|
|
}
|
|
|
|
break;
|
2019-10-23 11:57:41 +00:00
|
|
|
case wgpu::BindingType::StorageBuffer:
|
2019-11-07 07:09:07 +00:00
|
|
|
case wgpu::BindingType::ReadonlyStorageBuffer:
|
2019-09-24 11:08:17 +00:00
|
|
|
if (binding.hasDynamicOffset) {
|
2019-07-31 01:29:42 +00:00
|
|
|
++dynamicStorageBufferCount;
|
|
|
|
}
|
2019-07-15 10:29:08 +00:00
|
|
|
break;
|
2019-10-23 11:57:41 +00:00
|
|
|
case wgpu::BindingType::SampledTexture:
|
|
|
|
case wgpu::BindingType::Sampler:
|
2020-03-11 01:28:48 +00:00
|
|
|
case wgpu::BindingType::ReadonlyStorageTexture:
|
|
|
|
case wgpu::BindingType::WriteonlyStorageTexture:
|
2019-09-24 11:08:17 +00:00
|
|
|
if (binding.hasDynamicOffset) {
|
2019-07-15 10:29:08 +00:00
|
|
|
return DAWN_VALIDATION_ERROR("Samplers and textures cannot be dynamic");
|
|
|
|
}
|
|
|
|
break;
|
2019-10-23 11:57:41 +00:00
|
|
|
case wgpu::BindingType::StorageTexture:
|
2019-07-15 20:47:56 +00:00
|
|
|
return DAWN_VALIDATION_ERROR("storage textures aren't supported (yet)");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (binding.multisampled) {
|
|
|
|
return DAWN_VALIDATION_ERROR(
|
|
|
|
"BindGroupLayoutBinding::multisampled must be false (for now)");
|
2019-07-15 10:29:08 +00:00
|
|
|
}
|
|
|
|
|
2018-07-10 19:23:50 +00:00
|
|
|
bindingsSet.set(binding.binding);
|
|
|
|
}
|
2019-07-31 01:29:42 +00:00
|
|
|
|
|
|
|
if (dynamicUniformBufferCount > kMaxDynamicUniformBufferCount) {
|
|
|
|
return DAWN_VALIDATION_ERROR(
|
|
|
|
"The number of dynamic uniform buffer exceeds the maximum value");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (dynamicStorageBufferCount > kMaxDynamicStorageBufferCount) {
|
|
|
|
return DAWN_VALIDATION_ERROR(
|
|
|
|
"The number of dynamic storage buffer exceeds the maximum value");
|
|
|
|
}
|
|
|
|
|
2018-07-10 19:23:50 +00:00
|
|
|
return {};
|
2020-03-11 01:28:48 +00:00
|
|
|
} // namespace dawn_native
|
2018-07-10 19:23:50 +00:00
|
|
|
|
2017-04-20 18:38:20 +00:00
|
|
|
namespace {
|
|
|
|
size_t HashBindingInfo(const BindGroupLayoutBase::LayoutBindingInfo& info) {
|
|
|
|
size_t hash = Hash(info.mask);
|
2019-09-24 11:08:17 +00:00
|
|
|
HashCombine(&hash, info.hasDynamicOffset, info.multisampled);
|
2017-04-20 18:38:20 +00:00
|
|
|
|
2018-05-09 18:08:17 +00:00
|
|
|
for (uint32_t binding : IterateBitSet(info.mask)) {
|
2019-08-21 12:16:33 +00:00
|
|
|
HashCombine(&hash, info.visibilities[binding], info.types[binding],
|
2019-09-10 08:58:28 +00:00
|
|
|
info.textureComponentTypes[binding], info.textureDimensions[binding]);
|
2017-04-20 18:38:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return hash;
|
|
|
|
}
|
|
|
|
|
2017-11-24 18:59:42 +00:00
|
|
|
bool operator==(const BindGroupLayoutBase::LayoutBindingInfo& a,
|
|
|
|
const BindGroupLayoutBase::LayoutBindingInfo& b) {
|
2019-09-24 11:08:17 +00:00
|
|
|
if (a.mask != b.mask || a.hasDynamicOffset != b.hasDynamicOffset ||
|
|
|
|
a.multisampled != b.multisampled) {
|
2017-04-20 18:38:20 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-05-09 18:08:17 +00:00
|
|
|
for (uint32_t binding : IterateBitSet(a.mask)) {
|
2018-05-02 22:10:13 +00:00
|
|
|
if ((a.visibilities[binding] != b.visibilities[binding]) ||
|
2019-08-21 12:16:33 +00:00
|
|
|
(a.types[binding] != b.types[binding]) ||
|
2019-09-10 08:58:28 +00:00
|
|
|
(a.textureComponentTypes[binding] != b.textureComponentTypes[binding]) ||
|
|
|
|
(a.textureDimensions[binding] != b.textureDimensions[binding])) {
|
2018-05-09 18:08:17 +00:00
|
|
|
return false;
|
2017-04-20 18:38:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2017-11-24 18:59:42 +00:00
|
|
|
} // namespace
|
2017-04-20 18:38:20 +00:00
|
|
|
|
|
|
|
// BindGroupLayoutBase
|
|
|
|
|
2018-07-10 19:23:50 +00:00
|
|
|
BindGroupLayoutBase::BindGroupLayoutBase(DeviceBase* device,
|
2019-10-30 00:20:03 +00:00
|
|
|
const BindGroupLayoutDescriptor* descriptor)
|
|
|
|
: CachedObject(device) {
|
2019-02-21 00:45:19 +00:00
|
|
|
for (uint32_t i = 0; i < descriptor->bindingCount; ++i) {
|
2018-07-10 19:23:50 +00:00
|
|
|
auto& binding = descriptor->bindings[i];
|
|
|
|
|
|
|
|
uint32_t index = binding.binding;
|
|
|
|
mBindingInfo.visibilities[index] = binding.visibility;
|
|
|
|
mBindingInfo.types[index] = binding.type;
|
2019-08-21 12:16:33 +00:00
|
|
|
mBindingInfo.textureComponentTypes[index] = binding.textureComponentType;
|
2018-07-10 19:23:50 +00:00
|
|
|
|
2019-10-23 11:57:41 +00:00
|
|
|
if (binding.textureDimension == wgpu::TextureViewDimension::Undefined) {
|
|
|
|
mBindingInfo.textureDimensions[index] = wgpu::TextureViewDimension::e2D;
|
2019-09-10 08:58:28 +00:00
|
|
|
} else {
|
|
|
|
mBindingInfo.textureDimensions[index] = binding.textureDimension;
|
|
|
|
}
|
2019-09-24 11:08:17 +00:00
|
|
|
if (binding.hasDynamicOffset) {
|
|
|
|
mBindingInfo.hasDynamicOffset.set(index);
|
2019-07-31 01:29:42 +00:00
|
|
|
switch (binding.type) {
|
2019-10-23 11:57:41 +00:00
|
|
|
case wgpu::BindingType::UniformBuffer:
|
2019-07-31 01:29:42 +00:00
|
|
|
++mDynamicUniformBufferCount;
|
|
|
|
break;
|
2019-10-23 11:57:41 +00:00
|
|
|
case wgpu::BindingType::StorageBuffer:
|
2019-11-07 07:09:07 +00:00
|
|
|
case wgpu::BindingType::ReadonlyStorageBuffer:
|
2019-07-31 01:29:42 +00:00
|
|
|
++mDynamicStorageBufferCount;
|
|
|
|
break;
|
2019-10-23 11:57:41 +00:00
|
|
|
case wgpu::BindingType::SampledTexture:
|
|
|
|
case wgpu::BindingType::Sampler:
|
|
|
|
case wgpu::BindingType::StorageTexture:
|
2020-03-11 01:28:48 +00:00
|
|
|
case wgpu::BindingType::ReadonlyStorageTexture:
|
|
|
|
case wgpu::BindingType::WriteonlyStorageTexture:
|
2019-07-31 01:29:42 +00:00
|
|
|
UNREACHABLE();
|
|
|
|
break;
|
|
|
|
}
|
2019-05-17 02:05:37 +00:00
|
|
|
}
|
|
|
|
|
2019-07-15 20:47:56 +00:00
|
|
|
mBindingInfo.multisampled.set(index, binding.multisampled);
|
|
|
|
|
2018-07-10 19:23:50 +00:00
|
|
|
ASSERT(!mBindingInfo.mask[index]);
|
|
|
|
mBindingInfo.mask.set(index);
|
|
|
|
}
|
2017-04-20 18:38:20 +00:00
|
|
|
}
|
|
|
|
|
2019-02-13 13:09:18 +00:00
|
|
|
BindGroupLayoutBase::BindGroupLayoutBase(DeviceBase* device, ObjectBase::ErrorTag tag)
|
2019-10-30 00:20:03 +00:00
|
|
|
: CachedObject(device, tag) {
|
2019-02-13 13:09:18 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 18:38:20 +00:00
|
|
|
BindGroupLayoutBase::~BindGroupLayoutBase() {
|
2018-07-16 15:40:08 +00:00
|
|
|
// Do not uncache the actual cached object if we are a blueprint
|
2019-10-30 00:20:03 +00:00
|
|
|
if (IsCachedReference()) {
|
2018-10-15 12:54:30 +00:00
|
|
|
GetDevice()->UncacheBindGroupLayout(this);
|
2017-04-20 18:38:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-13 13:09:18 +00:00
|
|
|
// static
|
|
|
|
BindGroupLayoutBase* BindGroupLayoutBase::MakeError(DeviceBase* device) {
|
|
|
|
return new BindGroupLayoutBase(device, ObjectBase::kError);
|
|
|
|
}
|
|
|
|
|
2017-04-20 18:38:20 +00:00
|
|
|
const BindGroupLayoutBase::LayoutBindingInfo& BindGroupLayoutBase::GetBindingInfo() const {
|
2019-02-13 13:09:18 +00:00
|
|
|
ASSERT(!IsError());
|
2017-11-23 18:32:51 +00:00
|
|
|
return mBindingInfo;
|
2017-04-20 18:38:20 +00:00
|
|
|
}
|
|
|
|
|
2019-05-01 12:57:27 +00:00
|
|
|
size_t BindGroupLayoutBase::HashFunc::operator()(const BindGroupLayoutBase* bgl) const {
|
|
|
|
return HashBindingInfo(bgl->mBindingInfo);
|
2017-04-20 18:38:20 +00:00
|
|
|
}
|
|
|
|
|
2019-05-01 12:57:27 +00:00
|
|
|
bool BindGroupLayoutBase::EqualityFunc::operator()(const BindGroupLayoutBase* a,
|
|
|
|
const BindGroupLayoutBase* b) const {
|
|
|
|
return a->mBindingInfo == b->mBindingInfo;
|
2017-04-20 18:38:20 +00:00
|
|
|
}
|
|
|
|
|
2019-05-17 02:05:37 +00:00
|
|
|
uint32_t BindGroupLayoutBase::GetDynamicBufferCount() const {
|
2019-07-31 01:29:42 +00:00
|
|
|
return mDynamicStorageBufferCount + mDynamicUniformBufferCount;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t BindGroupLayoutBase::GetDynamicUniformBufferCount() const {
|
|
|
|
return mDynamicUniformBufferCount;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t BindGroupLayoutBase::GetDynamicStorageBufferCount() const {
|
|
|
|
return mDynamicStorageBufferCount;
|
2019-05-17 02:05:37 +00:00
|
|
|
}
|
|
|
|
|
2018-07-24 14:45:45 +00:00
|
|
|
} // namespace dawn_native
|