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/BindGroup.h"
|
2017-04-20 18:38:20 +00:00
|
|
|
|
2017-07-10 17:46:05 +00:00
|
|
|
#include "common/Assert.h"
|
2017-08-17 18:03:14 +00:00
|
|
|
#include "common/Math.h"
|
2018-07-24 11:53:51 +00:00
|
|
|
#include "dawn_native/BindGroupLayout.h"
|
|
|
|
#include "dawn_native/Buffer.h"
|
|
|
|
#include "dawn_native/Device.h"
|
2018-12-05 07:18:30 +00:00
|
|
|
#include "dawn_native/Sampler.h"
|
2018-07-24 11:53:51 +00:00
|
|
|
#include "dawn_native/Texture.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
|
|
|
|
2018-12-05 07:18:30 +00:00
|
|
|
namespace {
|
2017-04-20 18:38:20 +00:00
|
|
|
|
2018-12-05 07:18:30 +00:00
|
|
|
// Helper functions to perform binding-type specific validation
|
2017-04-20 18:38:20 +00:00
|
|
|
|
2018-12-05 07:18:30 +00:00
|
|
|
MaybeError ValidateBufferBinding(const BindGroupBinding& binding,
|
|
|
|
dawn::BufferUsageBit requiredUsage) {
|
2018-12-07 12:31:53 +00:00
|
|
|
if (binding.buffer == nullptr || binding.sampler != nullptr ||
|
2018-12-05 07:18:30 +00:00
|
|
|
binding.textureView != nullptr) {
|
|
|
|
return DAWN_VALIDATION_ERROR("expected buffer binding");
|
|
|
|
}
|
2017-04-20 18:38:20 +00:00
|
|
|
|
2018-12-07 12:31:53 +00:00
|
|
|
uint32_t bufferSize = binding.buffer->GetSize();
|
|
|
|
if (binding.size > bufferSize) {
|
|
|
|
return DAWN_VALIDATION_ERROR("Buffer binding size larger than the buffer");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Note that no overflow can happen because we already checked that
|
|
|
|
// bufferSize >= binding.size
|
|
|
|
if (binding.offset > bufferSize - binding.size) {
|
|
|
|
return DAWN_VALIDATION_ERROR("Buffer binding doesn't fit in the buffer");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!IsAligned(binding.offset, 256)) {
|
2018-12-05 07:18:30 +00:00
|
|
|
return DAWN_VALIDATION_ERROR(
|
2018-12-07 12:31:53 +00:00
|
|
|
"Buffer offset for bind group needs to be 256-byte aligned");
|
2018-12-05 07:18:30 +00:00
|
|
|
}
|
2017-04-20 18:38:20 +00:00
|
|
|
|
2018-12-07 12:31:53 +00:00
|
|
|
if (!(binding.buffer->GetUsage() & requiredUsage)) {
|
2018-12-05 07:18:30 +00:00
|
|
|
return DAWN_VALIDATION_ERROR("buffer binding usage mismatch");
|
|
|
|
}
|
2017-04-20 18:38:20 +00:00
|
|
|
|
2018-12-05 07:18:30 +00:00
|
|
|
return {};
|
|
|
|
}
|
2017-04-20 18:38:20 +00:00
|
|
|
|
2018-12-05 07:18:30 +00:00
|
|
|
MaybeError ValidateTextureBinding(const BindGroupBinding& binding,
|
|
|
|
dawn::TextureUsageBit requiredUsage) {
|
|
|
|
if (binding.textureView == nullptr || binding.sampler != nullptr ||
|
2018-12-07 12:31:53 +00:00
|
|
|
binding.buffer != nullptr) {
|
2018-12-05 07:18:30 +00:00
|
|
|
return DAWN_VALIDATION_ERROR("expected texture binding");
|
|
|
|
}
|
2017-04-20 18:38:20 +00:00
|
|
|
|
2018-12-05 07:18:30 +00:00
|
|
|
if (!(binding.textureView->GetTexture()->GetUsage() & requiredUsage)) {
|
|
|
|
return DAWN_VALIDATION_ERROR("texture binding usage mismatch");
|
|
|
|
}
|
2017-04-20 18:38:20 +00:00
|
|
|
|
2018-12-05 07:18:30 +00:00
|
|
|
return {};
|
2017-04-20 18:38:20 +00:00
|
|
|
}
|
|
|
|
|
2018-12-05 07:18:30 +00:00
|
|
|
MaybeError ValidateSamplerBinding(const BindGroupBinding& binding) {
|
|
|
|
if (binding.sampler == nullptr || binding.textureView != nullptr ||
|
2018-12-07 12:31:53 +00:00
|
|
|
binding.buffer != nullptr) {
|
2018-12-05 07:18:30 +00:00
|
|
|
return DAWN_VALIDATION_ERROR("expected sampler binding");
|
|
|
|
}
|
|
|
|
return {};
|
2017-04-20 18:38:20 +00:00
|
|
|
}
|
|
|
|
|
2018-12-05 07:18:30 +00:00
|
|
|
} // anonymous namespace
|
2017-04-20 18:38:20 +00:00
|
|
|
|
2018-12-05 07:18:30 +00:00
|
|
|
MaybeError ValidateBindGroupDescriptor(DeviceBase*, const BindGroupDescriptor* descriptor) {
|
|
|
|
if (descriptor->nextInChain != nullptr) {
|
|
|
|
return DAWN_VALIDATION_ERROR("nextInChain must be nullptr");
|
2017-04-20 18:38:20 +00:00
|
|
|
}
|
|
|
|
|
2018-12-10 10:03:08 +00:00
|
|
|
if (descriptor->layout == nullptr) {
|
|
|
|
return DAWN_VALIDATION_ERROR("layout cannot be null");
|
|
|
|
}
|
|
|
|
|
2018-12-05 07:18:30 +00:00
|
|
|
const BindGroupLayoutBase::LayoutBindingInfo& layoutInfo =
|
|
|
|
descriptor->layout->GetBindingInfo();
|
2017-04-20 18:38:20 +00:00
|
|
|
|
2018-12-05 07:18:30 +00:00
|
|
|
if (descriptor->numBindings != layoutInfo.mask.count()) {
|
|
|
|
return DAWN_VALIDATION_ERROR("numBindings mismatch");
|
2017-04-20 18:38:20 +00:00
|
|
|
}
|
|
|
|
|
2018-12-05 07:18:30 +00:00
|
|
|
std::bitset<kMaxBindingsPerGroup> bindingsSet;
|
|
|
|
for (uint32_t i = 0; i < descriptor->numBindings; ++i) {
|
|
|
|
const BindGroupBinding& binding = descriptor->bindings[i];
|
|
|
|
uint32_t bindingIndex = binding.binding;
|
2017-04-20 18:38:20 +00:00
|
|
|
|
2018-12-05 07:18:30 +00:00
|
|
|
// Check that we can set this binding.
|
|
|
|
if (bindingIndex >= kMaxBindingsPerGroup) {
|
|
|
|
return DAWN_VALIDATION_ERROR("binding index too high");
|
|
|
|
}
|
2017-04-20 18:38:20 +00:00
|
|
|
|
2018-12-05 07:18:30 +00:00
|
|
|
if (!layoutInfo.mask[bindingIndex]) {
|
|
|
|
return DAWN_VALIDATION_ERROR("setting non-existent binding");
|
2017-04-20 18:38:20 +00:00
|
|
|
}
|
|
|
|
|
2018-12-05 07:18:30 +00:00
|
|
|
if (bindingsSet[bindingIndex]) {
|
|
|
|
return DAWN_VALIDATION_ERROR("binding set twice");
|
2017-04-20 18:38:20 +00:00
|
|
|
}
|
2018-12-05 07:18:30 +00:00
|
|
|
bindingsSet.set(bindingIndex);
|
2017-08-17 18:03:14 +00:00
|
|
|
|
2018-12-05 07:18:30 +00:00
|
|
|
// Perform binding-type specific validation.
|
|
|
|
switch (layoutInfo.types[bindingIndex]) {
|
|
|
|
case dawn::BindingType::UniformBuffer:
|
|
|
|
DAWN_TRY(ValidateBufferBinding(binding, dawn::BufferUsageBit::Uniform));
|
|
|
|
break;
|
|
|
|
case dawn::BindingType::StorageBuffer:
|
|
|
|
DAWN_TRY(ValidateBufferBinding(binding, dawn::BufferUsageBit::Storage));
|
|
|
|
break;
|
|
|
|
case dawn::BindingType::SampledTexture:
|
|
|
|
DAWN_TRY(ValidateTextureBinding(binding, dawn::TextureUsageBit::Sampled));
|
|
|
|
break;
|
|
|
|
case dawn::BindingType::Sampler:
|
|
|
|
DAWN_TRY(ValidateSamplerBinding(binding));
|
|
|
|
break;
|
2017-08-17 18:03:14 +00:00
|
|
|
}
|
2017-04-20 18:38:20 +00:00
|
|
|
}
|
|
|
|
|
2018-12-05 07:18:30 +00:00
|
|
|
// This should always be true because
|
|
|
|
// - numBindings has to match between the bind group and its layout.
|
|
|
|
// - Each binding must be set at most once
|
|
|
|
//
|
|
|
|
// We don't validate the equality because it wouldn't be possible to cover it with a test.
|
|
|
|
ASSERT(bindingsSet == layoutInfo.mask);
|
|
|
|
|
|
|
|
return {};
|
2017-04-20 18:38:20 +00:00
|
|
|
}
|
|
|
|
|
2018-12-05 07:18:30 +00:00
|
|
|
// BindGroup
|
2017-04-20 18:38:20 +00:00
|
|
|
|
2018-12-05 07:18:30 +00:00
|
|
|
BindGroupBase::BindGroupBase(DeviceBase* device, const BindGroupDescriptor* descriptor)
|
|
|
|
: ObjectBase(device), mLayout(descriptor->layout) {
|
|
|
|
for (uint32_t i = 0; i < descriptor->numBindings; ++i) {
|
|
|
|
const BindGroupBinding& binding = descriptor->bindings[i];
|
2017-04-20 18:38:20 +00:00
|
|
|
|
2018-12-05 07:18:30 +00:00
|
|
|
uint32_t bindingIndex = binding.binding;
|
|
|
|
ASSERT(bindingIndex < kMaxBindingsPerGroup);
|
2017-04-20 18:38:20 +00:00
|
|
|
|
2018-12-05 07:18:30 +00:00
|
|
|
// Only a single binding type should be set, so once we found it we can skip to the
|
|
|
|
// next loop iteration.
|
2017-04-20 18:38:20 +00:00
|
|
|
|
2018-12-07 12:31:53 +00:00
|
|
|
if (binding.buffer != nullptr) {
|
2018-12-05 07:18:30 +00:00
|
|
|
ASSERT(mBindings[bindingIndex].Get() == nullptr);
|
2018-12-07 12:31:53 +00:00
|
|
|
mBindings[bindingIndex] = binding.buffer;
|
|
|
|
mOffsets[bindingIndex] = binding.offset;
|
|
|
|
mSizes[bindingIndex] = binding.size;
|
2018-12-05 07:18:30 +00:00
|
|
|
continue;
|
2017-04-20 18:38:20 +00:00
|
|
|
}
|
|
|
|
|
2018-12-05 07:18:30 +00:00
|
|
|
if (binding.textureView != nullptr) {
|
|
|
|
ASSERT(mBindings[bindingIndex].Get() == nullptr);
|
|
|
|
mBindings[bindingIndex] = binding.textureView;
|
|
|
|
continue;
|
2017-04-20 18:38:20 +00:00
|
|
|
}
|
|
|
|
|
2018-12-05 07:18:30 +00:00
|
|
|
if (binding.sampler != nullptr) {
|
|
|
|
ASSERT(mBindings[bindingIndex].Get() == nullptr);
|
|
|
|
mBindings[bindingIndex] = binding.sampler;
|
|
|
|
continue;
|
|
|
|
}
|
2017-04-20 18:38:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-05 07:18:30 +00:00
|
|
|
const BindGroupLayoutBase* BindGroupBase::GetLayout() const {
|
|
|
|
return mLayout.Get();
|
|
|
|
}
|
2017-04-20 18:38:20 +00:00
|
|
|
|
2018-12-07 12:31:53 +00:00
|
|
|
BufferBinding BindGroupBase::GetBindingAsBufferBinding(size_t binding) {
|
2018-12-05 07:18:30 +00:00
|
|
|
ASSERT(binding < kMaxBindingsPerGroup);
|
|
|
|
ASSERT(mLayout->GetBindingInfo().mask[binding]);
|
|
|
|
ASSERT(mLayout->GetBindingInfo().types[binding] == dawn::BindingType::UniformBuffer ||
|
|
|
|
mLayout->GetBindingInfo().types[binding] == dawn::BindingType::StorageBuffer);
|
2018-12-07 12:31:53 +00:00
|
|
|
BufferBase* buffer = reinterpret_cast<BufferBase*>(mBindings[binding].Get());
|
|
|
|
return {buffer, mOffsets[binding], mSizes[binding]};
|
2018-12-05 07:18:30 +00:00
|
|
|
}
|
2017-04-20 18:38:20 +00:00
|
|
|
|
2018-12-05 07:18:30 +00:00
|
|
|
SamplerBase* BindGroupBase::GetBindingAsSampler(size_t binding) {
|
|
|
|
ASSERT(binding < kMaxBindingsPerGroup);
|
|
|
|
ASSERT(mLayout->GetBindingInfo().mask[binding]);
|
|
|
|
ASSERT(mLayout->GetBindingInfo().types[binding] == dawn::BindingType::Sampler);
|
|
|
|
return reinterpret_cast<SamplerBase*>(mBindings[binding].Get());
|
|
|
|
}
|
2017-04-20 18:38:20 +00:00
|
|
|
|
2018-12-05 07:18:30 +00:00
|
|
|
TextureViewBase* BindGroupBase::GetBindingAsTextureView(size_t binding) {
|
|
|
|
ASSERT(binding < kMaxBindingsPerGroup);
|
|
|
|
ASSERT(mLayout->GetBindingInfo().mask[binding]);
|
|
|
|
ASSERT(mLayout->GetBindingInfo().types[binding] == dawn::BindingType::SampledTexture);
|
|
|
|
return reinterpret_cast<TextureViewBase*>(mBindings[binding].Get());
|
2017-04-20 18:38:20 +00:00
|
|
|
}
|
2018-07-24 14:45:45 +00:00
|
|
|
} // namespace dawn_native
|