2017-04-20 18:38:20 +00:00
|
|
|
// Copyright 2017 The NXT Authors
|
|
|
|
//
|
|
|
|
// 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/BindGroupLayout.h"
|
2017-04-20 18:38:20 +00:00
|
|
|
|
2017-07-06 18:41:13 +00:00
|
|
|
#include "backend/Device.h"
|
2018-05-09 18:08:17 +00:00
|
|
|
#include "common/BitSetIterator.h"
|
2018-05-07 21:33:48 +00:00
|
|
|
#include "common/HashUtils.h"
|
2017-04-20 18:38:20 +00:00
|
|
|
|
|
|
|
#include <functional>
|
|
|
|
|
|
|
|
namespace backend {
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
size_t HashBindingInfo(const BindGroupLayoutBase::LayoutBindingInfo& info) {
|
|
|
|
size_t hash = Hash(info.mask);
|
|
|
|
|
2018-05-09 18:08:17 +00:00
|
|
|
for (uint32_t binding : IterateBitSet(info.mask)) {
|
|
|
|
HashCombine(&hash, info.visibilities[binding], info.types[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) {
|
2017-04-20 18:38:20 +00:00
|
|
|
if (a.mask != b.mask) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-05-09 18:08:17 +00:00
|
|
|
for (uint32_t binding : IterateBitSet(a.mask)) {
|
|
|
|
if ((a.visibilities[binding] != b.visibilities[binding]) || (a.types[binding] != b.types[binding])) {
|
|
|
|
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
|
|
|
|
|
|
|
|
BindGroupLayoutBase::BindGroupLayoutBase(BindGroupLayoutBuilder* builder, bool blueprint)
|
2017-11-23 18:32:51 +00:00
|
|
|
: mDevice(builder->mDevice), mBindingInfo(builder->mBindingInfo), mIsBlueprint(blueprint) {
|
2017-04-20 18:38:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
BindGroupLayoutBase::~BindGroupLayoutBase() {
|
|
|
|
// Do not register the actual cached object if we are a blueprint
|
2017-11-23 18:32:51 +00:00
|
|
|
if (!mIsBlueprint) {
|
|
|
|
mDevice->UncacheBindGroupLayout(this);
|
2017-04-20 18:38:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const BindGroupLayoutBase::LayoutBindingInfo& BindGroupLayoutBase::GetBindingInfo() const {
|
2017-11-23 18:32:51 +00:00
|
|
|
return mBindingInfo;
|
2017-04-20 18:38:20 +00:00
|
|
|
}
|
|
|
|
|
2018-02-02 18:06:58 +00:00
|
|
|
DeviceBase* BindGroupLayoutBase::GetDevice() const {
|
|
|
|
return mDevice;
|
|
|
|
}
|
|
|
|
|
2017-04-20 18:38:20 +00:00
|
|
|
// BindGroupLayoutBuilder
|
|
|
|
|
2017-05-08 08:52:11 +00:00
|
|
|
BindGroupLayoutBuilder::BindGroupLayoutBuilder(DeviceBase* device) : Builder(device) {
|
2017-04-20 18:38:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const BindGroupLayoutBase::LayoutBindingInfo& BindGroupLayoutBuilder::GetBindingInfo() const {
|
2017-11-23 18:32:51 +00:00
|
|
|
return mBindingInfo;
|
2017-04-20 18:38:20 +00:00
|
|
|
}
|
|
|
|
|
2017-05-08 13:17:44 +00:00
|
|
|
BindGroupLayoutBase* BindGroupLayoutBuilder::GetResultImpl() {
|
2017-04-20 18:38:20 +00:00
|
|
|
BindGroupLayoutBase blueprint(this, true);
|
|
|
|
|
2017-11-23 18:32:51 +00:00
|
|
|
auto* result = mDevice->GetOrCreateBindGroupLayout(&blueprint, this);
|
2017-04-20 18:38:20 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2017-11-24 18:59:42 +00:00
|
|
|
void BindGroupLayoutBuilder::SetBindingsType(nxt::ShaderStageBit visibility,
|
|
|
|
nxt::BindingType bindingType,
|
|
|
|
uint32_t start,
|
|
|
|
uint32_t count) {
|
2017-04-20 18:38:20 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
for (size_t i = start; i < start + count; i++) {
|
2017-11-23 18:32:51 +00:00
|
|
|
if (mBindingInfo.mask[i]) {
|
2017-05-08 08:52:11 +00:00
|
|
|
HandleError("Setting already set binding type");
|
2017-04-20 18:38:20 +00:00
|
|
|
return;
|
|
|
|
}
|
2017-05-16 21:04:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for (size_t i = start; i < start + count; i++) {
|
2017-11-23 18:32:51 +00:00
|
|
|
mBindingInfo.mask.set(i);
|
|
|
|
mBindingInfo.visibilities[i] = visibility;
|
|
|
|
mBindingInfo.types[i] = bindingType;
|
2017-04-20 18:38:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// BindGroupLayoutCacheFuncs
|
|
|
|
|
2017-11-24 18:59:42 +00:00
|
|
|
size_t BindGroupLayoutCacheFuncs::operator()(const BindGroupLayoutBase* bgl) const {
|
2017-04-20 18:38:20 +00:00
|
|
|
return HashBindingInfo(bgl->GetBindingInfo());
|
|
|
|
}
|
|
|
|
|
2017-11-24 18:59:42 +00:00
|
|
|
bool BindGroupLayoutCacheFuncs::operator()(const BindGroupLayoutBase* a,
|
|
|
|
const BindGroupLayoutBase* b) const {
|
2017-04-20 18:38:20 +00:00
|
|
|
return a->GetBindingInfo() == b->GetBindingInfo();
|
|
|
|
}
|
|
|
|
|
2017-11-24 18:59:42 +00:00
|
|
|
} // namespace backend
|