dawn-cmake/src/dawn_native/d3d12/BindGroupLayoutD3D12.cpp
Stephen White 4408b7f20c D3D12 fix for register binding offsets.
When both the vert and frag shaders have a UBO binding, the D3D12
backend was using register offset 0 for both, causing a collision,
and the wrong constant value used in one of the shaders.

The fix is to use the binding offsets computed by the BindGroupLayout,
since they know about all of the bindings, not just the ones computed
for each shader. This made it necessary to defer shader compilation
until pipeline layout creation time (as is done in the Metal backend
for similar reasons).

Finally, those bindings offsets computed by the BGL include an offset
for the CBV, UAV and SRV subgroups, so we must add the same register
offset when assigning the BaseShaderRegister to the descriptor ranges
in the PipelineLayout constructor so that they match.

Bug: dawn:20

Change-Id: I18287bf1c06f06dd61288e12da64752f54634466
Reviewed-on: https://dawn-review.googlesource.com/c/1960
Reviewed-by: Stephen White <senorblanco@chromium.org>
Reviewed-by: Corentin Wallez <cwallez@chromium.org>
Commit-Queue: Stephen White <senorblanco@chromium.org>
2018-11-01 13:59:19 +00:00

133 lines
5.5 KiB
C++

// Copyright 2017 The Dawn 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.
#include "dawn_native/d3d12/BindGroupLayoutD3D12.h"
#include "common/BitSetIterator.h"
#include "dawn_native/d3d12/DeviceD3D12.h"
namespace dawn_native { namespace d3d12 {
BindGroupLayout::BindGroupLayout(Device* device, const BindGroupLayoutDescriptor* descriptor)
: BindGroupLayoutBase(device, descriptor), mDescriptorCounts{} {
const auto& groupInfo = GetBindingInfo();
for (uint32_t binding : IterateBitSet(groupInfo.mask)) {
switch (groupInfo.types[binding]) {
case dawn::BindingType::UniformBuffer:
mBindingOffsets[binding] = mDescriptorCounts[CBV]++;
break;
case dawn::BindingType::StorageBuffer:
mBindingOffsets[binding] = mDescriptorCounts[UAV]++;
break;
case dawn::BindingType::SampledTexture:
mBindingOffsets[binding] = mDescriptorCounts[SRV]++;
break;
case dawn::BindingType::Sampler:
mBindingOffsets[binding] = mDescriptorCounts[Sampler]++;
break;
}
}
auto SetDescriptorRange = [&](uint32_t index, uint32_t count, uint32_t* baseRegister,
D3D12_DESCRIPTOR_RANGE_TYPE type) -> bool {
if (count == 0) {
return false;
}
auto& range = mRanges[index];
range.RangeType = type;
range.NumDescriptors = count;
range.RegisterSpace = 0;
range.OffsetInDescriptorsFromTableStart = D3D12_DESCRIPTOR_RANGE_OFFSET_APPEND;
range.BaseShaderRegister = *baseRegister;
*baseRegister += count;
// These ranges will be copied and range.BaseShaderRegister will be set in
// d3d12::PipelineLayout to account for bind group register offsets
return true;
};
uint32_t rangeIndex = 0;
uint32_t baseRegister = 0;
std::array<uint32_t, DescriptorType::Count> descriptorOffsets;
// Ranges 0-2 contain the CBV, UAV, and SRV ranges, if they exist, tightly packed
// Range 3 contains the Sampler range, if there is one
if (SetDescriptorRange(rangeIndex, mDescriptorCounts[CBV], &baseRegister,
D3D12_DESCRIPTOR_RANGE_TYPE_CBV)) {
descriptorOffsets[CBV] = mRanges[rangeIndex++].BaseShaderRegister;
}
if (SetDescriptorRange(rangeIndex, mDescriptorCounts[UAV], &baseRegister,
D3D12_DESCRIPTOR_RANGE_TYPE_UAV)) {
descriptorOffsets[UAV] = mRanges[rangeIndex++].BaseShaderRegister;
}
if (SetDescriptorRange(rangeIndex, mDescriptorCounts[SRV], &baseRegister,
D3D12_DESCRIPTOR_RANGE_TYPE_SRV)) {
descriptorOffsets[SRV] = mRanges[rangeIndex++].BaseShaderRegister;
}
uint32_t zero = 0;
SetDescriptorRange(Sampler, mDescriptorCounts[Sampler], &zero,
D3D12_DESCRIPTOR_RANGE_TYPE_SAMPLER);
descriptorOffsets[Sampler] = 0;
for (uint32_t binding : IterateBitSet(groupInfo.mask)) {
switch (groupInfo.types[binding]) {
case dawn::BindingType::UniformBuffer:
mBindingOffsets[binding] += descriptorOffsets[CBV];
break;
case dawn::BindingType::StorageBuffer:
mBindingOffsets[binding] += descriptorOffsets[UAV];
break;
case dawn::BindingType::SampledTexture:
mBindingOffsets[binding] += descriptorOffsets[SRV];
break;
case dawn::BindingType::Sampler:
mBindingOffsets[binding] += descriptorOffsets[Sampler];
break;
}
}
}
const std::array<uint32_t, kMaxBindingsPerGroup>& BindGroupLayout::GetBindingOffsets() const {
return mBindingOffsets;
}
uint32_t BindGroupLayout::GetCbvUavSrvDescriptorTableSize() const {
return (static_cast<uint32_t>(mDescriptorCounts[CBV] > 0) +
static_cast<uint32_t>(mDescriptorCounts[UAV] > 0) +
static_cast<uint32_t>(mDescriptorCounts[SRV] > 0));
}
uint32_t BindGroupLayout::GetSamplerDescriptorTableSize() const {
return mDescriptorCounts[Sampler] > 0;
}
uint32_t BindGroupLayout::GetCbvUavSrvDescriptorCount() const {
return mDescriptorCounts[CBV] + mDescriptorCounts[UAV] + mDescriptorCounts[SRV];
}
uint32_t BindGroupLayout::GetSamplerDescriptorCount() const {
return mDescriptorCounts[Sampler];
}
const D3D12_DESCRIPTOR_RANGE* BindGroupLayout::GetCbvUavSrvDescriptorRanges() const {
return mRanges;
}
const D3D12_DESCRIPTOR_RANGE* BindGroupLayout::GetSamplerDescriptorRanges() const {
return &mRanges[Sampler];
}
}} // namespace dawn_native::d3d12