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
|
|
|
|
2019-02-13 13:09:18 +00:00
|
|
|
MaybeError ValidateBufferBinding(const DeviceBase* device,
|
|
|
|
const BindGroupBinding& binding,
|
2018-12-05 07:18:30 +00:00
|
|
|
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");
|
|
|
|
}
|
2019-02-13 13:09:18 +00:00
|
|
|
DAWN_TRY(device->ValidateObject(binding.buffer));
|
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
|
|
|
|
2019-02-13 13:09:18 +00:00
|
|
|
MaybeError ValidateTextureBinding(const DeviceBase* device,
|
|
|
|
const BindGroupBinding& binding,
|
2018-12-05 07:18:30 +00:00
|
|
|
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");
|
|
|
|
}
|
2019-02-13 13:09:18 +00:00
|
|
|
DAWN_TRY(device->ValidateObject(binding.textureView));
|
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
|
|
|
}
|
|
|
|
|
2019-02-13 13:09:18 +00:00
|
|
|
MaybeError ValidateSamplerBinding(const DeviceBase* device,
|
|
|
|
const BindGroupBinding& binding) {
|
2018-12-05 07:18:30 +00:00
|
|
|
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");
|
|
|
|
}
|
2019-02-13 13:09:18 +00:00
|
|
|
DAWN_TRY(device->ValidateObject(binding.sampler));
|
|
|
|
|
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
|
|
|
} // anonymous namespace
|
2017-04-20 18:38:20 +00:00
|
|
|
|
2019-02-13 13:09:18 +00:00
|
|
|
MaybeError ValidateBindGroupDescriptor(DeviceBase* device,
|
|
|
|
const BindGroupDescriptor* descriptor) {
|
2018-12-05 07:18:30 +00:00
|
|
|
if (descriptor->nextInChain != nullptr) {
|
|
|
|
return DAWN_VALIDATION_ERROR("nextInChain must be nullptr");
|
2017-04-20 18:38:20 +00:00
|
|
|
}
|
|
|
|
|
2019-02-13 13:09:18 +00:00
|
|
|
DAWN_TRY(device->ValidateObject(descriptor->layout));
|
2018-12-10 10:03:08 +00:00
|
|
|
|
2018-12-05 07:18:30 +00:00
|
|
|
const BindGroupLayoutBase::LayoutBindingInfo& layoutInfo =
|
|
|
|
descriptor->layout->GetBindingInfo();
|
2017-04-20 18:38:20 +00:00
|
|
|
|
2019-02-21 00:45:19 +00:00
|
|
|
if (descriptor->bindingCount != layoutInfo.mask.count()) {
|
2018-12-05 07:18:30 +00:00
|
|
|
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;
|
2019-02-21 00:45:19 +00:00
|
|
|
for (uint32_t i = 0; i < descriptor->bindingCount; ++i) {
|
2018-12-05 07:18:30 +00:00
|
|
|
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:
|
2019-02-13 13:09:18 +00:00
|
|
|
DAWN_TRY(ValidateBufferBinding(device, binding, dawn::BufferUsageBit::Uniform));
|
2018-12-05 07:18:30 +00:00
|
|
|
break;
|
|
|
|
case dawn::BindingType::StorageBuffer:
|
2019-02-13 13:09:18 +00:00
|
|
|
DAWN_TRY(ValidateBufferBinding(device, binding, dawn::BufferUsageBit::Storage));
|
2018-12-05 07:18:30 +00:00
|
|
|
break;
|
|
|
|
case dawn::BindingType::SampledTexture:
|
2019-02-13 13:09:18 +00:00
|
|
|
DAWN_TRY(
|
|
|
|
ValidateTextureBinding(device, binding, dawn::TextureUsageBit::Sampled));
|
2018-12-05 07:18:30 +00:00
|
|
|
break;
|
|
|
|
case dawn::BindingType::Sampler:
|
2019-02-13 13:09:18 +00:00
|
|
|
DAWN_TRY(ValidateSamplerBinding(device, binding));
|
2018-12-05 07:18:30 +00:00
|
|
|
break;
|
2019-03-18 06:01:37 +00:00
|
|
|
// TODO(shaobo.yan@intel.com): Implement dynamic buffer offset.
|
|
|
|
case dawn::BindingType::DynamicUniformBuffer:
|
|
|
|
case dawn::BindingType::DynamicStorageBuffer:
|
|
|
|
return DAWN_VALIDATION_ERROR("Dawn doesn't support dynamic buffer yet");
|
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) {
|
2019-02-21 00:45:19 +00:00
|
|
|
for (uint32_t i = 0; i < descriptor->bindingCount; ++i) {
|
2018-12-05 07:18:30 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-13 13:09:18 +00:00
|
|
|
BindGroupBase::BindGroupBase(DeviceBase* device, ObjectBase::ErrorTag tag)
|
|
|
|
: ObjectBase(device, tag) {
|
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
|
|
|
BindGroupBase* BindGroupBase::MakeError(DeviceBase* device) {
|
|
|
|
return new BindGroupBase(device, ObjectBase::kError);
|
|
|
|
}
|
|
|
|
|
2018-12-05 07:18:30 +00:00
|
|
|
const BindGroupLayoutBase* BindGroupBase::GetLayout() const {
|
2019-02-13 13:09:18 +00:00
|
|
|
ASSERT(!IsError());
|
2018-12-05 07:18:30 +00:00
|
|
|
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) {
|
2019-02-13 13:09:18 +00:00
|
|
|
ASSERT(!IsError());
|
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) {
|
2019-02-13 13:09:18 +00:00
|
|
|
ASSERT(!IsError());
|
2018-12-05 07:18:30 +00:00
|
|
|
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) {
|
2019-02-13 13:09:18 +00:00
|
|
|
ASSERT(!IsError());
|
2018-12-05 07:18:30 +00:00
|
|
|
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
|
|
|
}
|
2019-02-13 13:09:18 +00:00
|
|
|
|
2018-07-24 14:45:45 +00:00
|
|
|
} // namespace dawn_native
|