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.
|
|
|
|
|
2017-07-06 18:41:13 +00:00
|
|
|
#include "backend/BindGroup.h"
|
2017-04-20 18:38:20 +00:00
|
|
|
|
2017-07-06 18:41:13 +00:00
|
|
|
#include "backend/BindGroupLayout.h"
|
|
|
|
#include "backend/Buffer.h"
|
|
|
|
#include "backend/Device.h"
|
|
|
|
#include "backend/Texture.h"
|
2017-07-10 17:46:05 +00:00
|
|
|
#include "common/Assert.h"
|
2017-08-17 18:03:14 +00:00
|
|
|
#include "common/Math.h"
|
2017-04-20 18:38:20 +00:00
|
|
|
|
|
|
|
namespace backend {
|
|
|
|
|
|
|
|
// BindGroup
|
|
|
|
|
|
|
|
BindGroupBase::BindGroupBase(BindGroupBuilder* builder)
|
2017-11-24 18:59:42 +00:00
|
|
|
: mLayout(std::move(builder->mLayout)),
|
|
|
|
mUsage(builder->mUsage),
|
|
|
|
mBindings(std::move(builder->mBindings)) {
|
2017-04-20 18:38:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const BindGroupLayoutBase* BindGroupBase::GetLayout() const {
|
2017-11-23 18:32:51 +00:00
|
|
|
return mLayout.Get();
|
2017-04-20 18:38:20 +00:00
|
|
|
}
|
|
|
|
|
2018-07-18 09:38:11 +00:00
|
|
|
dawn::BindGroupUsage BindGroupBase::GetUsage() const {
|
2017-11-23 18:32:51 +00:00
|
|
|
return mUsage;
|
2017-04-20 18:38:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
BufferViewBase* BindGroupBase::GetBindingAsBufferView(size_t binding) {
|
|
|
|
ASSERT(binding < kMaxBindingsPerGroup);
|
2017-11-23 18:32:51 +00:00
|
|
|
ASSERT(mLayout->GetBindingInfo().mask[binding]);
|
2018-07-18 09:38:11 +00:00
|
|
|
ASSERT(mLayout->GetBindingInfo().types[binding] == dawn::BindingType::UniformBuffer ||
|
|
|
|
mLayout->GetBindingInfo().types[binding] == dawn::BindingType::StorageBuffer);
|
2017-11-23 18:32:51 +00:00
|
|
|
return reinterpret_cast<BufferViewBase*>(mBindings[binding].Get());
|
2017-04-20 18:38:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
SamplerBase* BindGroupBase::GetBindingAsSampler(size_t binding) {
|
|
|
|
ASSERT(binding < kMaxBindingsPerGroup);
|
2017-11-23 18:32:51 +00:00
|
|
|
ASSERT(mLayout->GetBindingInfo().mask[binding]);
|
2018-07-18 09:38:11 +00:00
|
|
|
ASSERT(mLayout->GetBindingInfo().types[binding] == dawn::BindingType::Sampler);
|
2017-11-23 18:32:51 +00:00
|
|
|
return reinterpret_cast<SamplerBase*>(mBindings[binding].Get());
|
2017-04-20 18:38:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TextureViewBase* BindGroupBase::GetBindingAsTextureView(size_t binding) {
|
|
|
|
ASSERT(binding < kMaxBindingsPerGroup);
|
2017-11-23 18:32:51 +00:00
|
|
|
ASSERT(mLayout->GetBindingInfo().mask[binding]);
|
2018-07-18 09:38:11 +00:00
|
|
|
ASSERT(mLayout->GetBindingInfo().types[binding] == dawn::BindingType::SampledTexture);
|
2017-11-23 18:32:51 +00:00
|
|
|
return reinterpret_cast<TextureViewBase*>(mBindings[binding].Get());
|
2017-04-20 18:38:20 +00:00
|
|
|
}
|
|
|
|
|
2018-02-02 21:49:51 +00:00
|
|
|
DeviceBase* BindGroupBase::GetDevice() const {
|
|
|
|
return mLayout->GetDevice();
|
|
|
|
}
|
|
|
|
|
2017-04-20 18:38:20 +00:00
|
|
|
// BindGroupBuilder
|
|
|
|
|
|
|
|
enum BindGroupSetProperties {
|
|
|
|
BINDGROUP_PROPERTY_USAGE = 0x1,
|
|
|
|
BINDGROUP_PROPERTY_LAYOUT = 0x2,
|
|
|
|
};
|
|
|
|
|
2017-05-08 13:17:44 +00:00
|
|
|
BindGroupBuilder::BindGroupBuilder(DeviceBase* device) : Builder(device) {
|
2017-04-20 18:38:20 +00:00
|
|
|
}
|
|
|
|
|
2017-05-08 13:17:44 +00:00
|
|
|
BindGroupBase* BindGroupBuilder::GetResultImpl() {
|
2017-04-20 18:38:20 +00:00
|
|
|
constexpr int allProperties = BINDGROUP_PROPERTY_USAGE | BINDGROUP_PROPERTY_LAYOUT;
|
2017-11-23 18:32:51 +00:00
|
|
|
if ((mPropertiesSet & allProperties) != allProperties) {
|
2017-05-08 08:52:11 +00:00
|
|
|
HandleError("Bindgroup missing properties");
|
2017-04-20 18:38:20 +00:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2017-11-23 18:32:51 +00:00
|
|
|
if (mSetMask != mLayout->GetBindingInfo().mask) {
|
2017-05-08 08:52:11 +00:00
|
|
|
HandleError("Bindgroup missing bindings");
|
2017-04-20 18:38:20 +00:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2017-11-23 18:32:51 +00:00
|
|
|
return mDevice->CreateBindGroup(this);
|
2017-04-20 18:38:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void BindGroupBuilder::SetLayout(BindGroupLayoutBase* layout) {
|
2017-11-23 18:32:51 +00:00
|
|
|
if ((mPropertiesSet & BINDGROUP_PROPERTY_LAYOUT) != 0) {
|
2017-05-08 08:52:11 +00:00
|
|
|
HandleError("Bindgroup layout property set multiple times");
|
2017-04-20 18:38:20 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-11-23 18:32:51 +00:00
|
|
|
mLayout = layout;
|
|
|
|
mPropertiesSet |= BINDGROUP_PROPERTY_LAYOUT;
|
2017-04-20 18:38:20 +00:00
|
|
|
}
|
|
|
|
|
2018-07-18 09:38:11 +00:00
|
|
|
void BindGroupBuilder::SetUsage(dawn::BindGroupUsage usage) {
|
2017-11-23 18:32:51 +00:00
|
|
|
if ((mPropertiesSet & BINDGROUP_PROPERTY_USAGE) != 0) {
|
2017-05-08 08:52:11 +00:00
|
|
|
HandleError("Bindgroup usage property set multiple times");
|
2017-04-20 18:38:20 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-11-23 18:32:51 +00:00
|
|
|
mUsage = usage;
|
|
|
|
mPropertiesSet |= BINDGROUP_PROPERTY_USAGE;
|
2017-04-20 18:38:20 +00:00
|
|
|
}
|
|
|
|
|
2017-11-24 18:59:42 +00:00
|
|
|
void BindGroupBuilder::SetBufferViews(uint32_t start,
|
|
|
|
uint32_t count,
|
|
|
|
BufferViewBase* const* bufferViews) {
|
2017-04-20 18:38:20 +00:00
|
|
|
if (!SetBindingsValidationBase(start, count)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-11-23 18:32:51 +00:00
|
|
|
const auto& layoutInfo = mLayout->GetBindingInfo();
|
2017-04-20 18:38:20 +00:00
|
|
|
for (size_t i = start, j = 0; i < start + count; ++i, ++j) {
|
2018-07-18 09:38:11 +00:00
|
|
|
dawn::BufferUsageBit requiredBit = dawn::BufferUsageBit::None;
|
2017-04-20 18:38:20 +00:00
|
|
|
switch (layoutInfo.types[i]) {
|
2018-07-18 09:38:11 +00:00
|
|
|
case dawn::BindingType::UniformBuffer:
|
|
|
|
requiredBit = dawn::BufferUsageBit::Uniform;
|
2017-04-20 18:38:20 +00:00
|
|
|
break;
|
|
|
|
|
2018-07-18 09:38:11 +00:00
|
|
|
case dawn::BindingType::StorageBuffer:
|
|
|
|
requiredBit = dawn::BufferUsageBit::Storage;
|
2017-04-20 18:38:20 +00:00
|
|
|
break;
|
|
|
|
|
2018-07-18 09:38:11 +00:00
|
|
|
case dawn::BindingType::Sampler:
|
|
|
|
case dawn::BindingType::SampledTexture:
|
2017-05-08 08:52:11 +00:00
|
|
|
HandleError("Setting buffer for a wrong binding type");
|
2017-04-20 18:38:20 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!(bufferViews[j]->GetBuffer()->GetAllowedUsage() & requiredBit)) {
|
2017-05-08 08:52:11 +00:00
|
|
|
HandleError("Buffer needs to allow the correct usage bit");
|
2017-04-20 18:38:20 +00:00
|
|
|
return;
|
|
|
|
}
|
2017-08-17 18:03:14 +00:00
|
|
|
|
2017-08-18 15:26:58 +00:00
|
|
|
if (!IsAligned(bufferViews[j]->GetOffset(), 256)) {
|
2017-08-17 18:03:14 +00:00
|
|
|
HandleError("Buffer view offset for bind group needs to be 256-byte aligned");
|
|
|
|
return;
|
|
|
|
}
|
2017-04-20 18:38:20 +00:00
|
|
|
}
|
|
|
|
|
2017-11-24 18:59:42 +00:00
|
|
|
SetBindingsBase(start, count, reinterpret_cast<RefCounted* const*>(bufferViews));
|
2017-04-20 18:38:20 +00:00
|
|
|
}
|
|
|
|
|
2017-11-24 18:59:42 +00:00
|
|
|
void BindGroupBuilder::SetSamplers(uint32_t start,
|
|
|
|
uint32_t count,
|
|
|
|
SamplerBase* const* samplers) {
|
2017-04-20 18:38:20 +00:00
|
|
|
if (!SetBindingsValidationBase(start, count)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-11-23 18:32:51 +00:00
|
|
|
const auto& layoutInfo = mLayout->GetBindingInfo();
|
2017-04-20 18:38:20 +00:00
|
|
|
for (size_t i = start, j = 0; i < start + count; ++i, ++j) {
|
2018-07-18 09:38:11 +00:00
|
|
|
if (layoutInfo.types[i] != dawn::BindingType::Sampler) {
|
2017-05-08 08:52:11 +00:00
|
|
|
HandleError("Setting binding for a wrong layout binding type");
|
2017-04-20 18:38:20 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-24 18:59:42 +00:00
|
|
|
SetBindingsBase(start, count, reinterpret_cast<RefCounted* const*>(samplers));
|
2017-04-20 18:38:20 +00:00
|
|
|
}
|
|
|
|
|
2017-11-24 18:59:42 +00:00
|
|
|
void BindGroupBuilder::SetTextureViews(uint32_t start,
|
|
|
|
uint32_t count,
|
|
|
|
TextureViewBase* const* textureViews) {
|
2017-04-20 18:38:20 +00:00
|
|
|
if (!SetBindingsValidationBase(start, count)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-11-23 18:32:51 +00:00
|
|
|
const auto& layoutInfo = mLayout->GetBindingInfo();
|
2017-04-20 18:38:20 +00:00
|
|
|
for (size_t i = start, j = 0; i < start + count; ++i, ++j) {
|
2018-07-18 09:38:11 +00:00
|
|
|
if (layoutInfo.types[i] != dawn::BindingType::SampledTexture) {
|
2017-05-08 08:52:11 +00:00
|
|
|
HandleError("Setting binding for a wrong layout binding type");
|
2017-04-20 18:38:20 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-11-24 18:59:42 +00:00
|
|
|
if (!(textureViews[j]->GetTexture()->GetAllowedUsage() &
|
2018-07-18 09:38:11 +00:00
|
|
|
dawn::TextureUsageBit::Sampled)) {
|
2017-05-08 08:52:11 +00:00
|
|
|
HandleError("Texture needs to allow the sampled usage bit");
|
2017-04-20 18:38:20 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-24 18:59:42 +00:00
|
|
|
SetBindingsBase(start, count, reinterpret_cast<RefCounted* const*>(textureViews));
|
2017-04-20 18:38:20 +00:00
|
|
|
}
|
|
|
|
|
2017-11-24 18:59:42 +00:00
|
|
|
void BindGroupBuilder::SetBindingsBase(uint32_t start,
|
|
|
|
uint32_t count,
|
|
|
|
RefCounted* const* objects) {
|
2017-04-20 18:38:20 +00:00
|
|
|
for (size_t i = start, j = 0; i < start + count; ++i, ++j) {
|
2017-11-23 18:32:51 +00:00
|
|
|
mSetMask.set(i);
|
|
|
|
mBindings[i] = objects[j];
|
2017-04-20 18:38:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool BindGroupBuilder::SetBindingsValidationBase(uint32_t start, uint32_t count) {
|
|
|
|
if (start + count > kMaxBindingsPerGroup) {
|
2017-05-08 08:52:11 +00:00
|
|
|
HandleError("Setting bindings type over maximum number of bindings");
|
2017-04-20 18:38:20 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-11-23 18:32:51 +00:00
|
|
|
if ((mPropertiesSet & BINDGROUP_PROPERTY_LAYOUT) == 0) {
|
2017-05-08 08:52:11 +00:00
|
|
|
HandleError("Bindgroup layout must be set before views");
|
2017-04-20 18:38:20 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-11-23 18:32:51 +00:00
|
|
|
const auto& layoutInfo = mLayout->GetBindingInfo();
|
2017-04-20 18:38:20 +00:00
|
|
|
for (size_t i = start, j = 0; i < start + count; ++i, ++j) {
|
2017-11-23 18:32:51 +00:00
|
|
|
if (mSetMask[i]) {
|
2017-05-08 08:52:11 +00:00
|
|
|
HandleError("Setting already set binding");
|
2017-04-20 18:38:20 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!layoutInfo.mask[i]) {
|
2017-05-08 08:52:11 +00:00
|
|
|
HandleError("Setting binding that isn't present in the layout");
|
2017-04-20 18:38:20 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2017-11-24 18:59:42 +00:00
|
|
|
} // namespace backend
|