D3D12: Add firstVertex/Instance to vertex/instance_index

Added root constants to emulate the behavior of other APIs under D3D12.
This patch only fixed Draw and DrawIndexed.

Bug: dawn:548
Change-Id: Ic759c22e0db1092f890d45c5db489697b1583827
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/37620
Commit-Queue: Enrico Galli <enrico.galli@intel.com>
Reviewed-by: Austin Eng <enga@chromium.org>
This commit is contained in:
Enrico Galli
2021-01-19 20:29:22 +00:00
committed by Commit Bot service account
parent 8bcde8e394
commit 311a17a8fe
9 changed files with 371 additions and 9 deletions

View File

@@ -137,6 +137,29 @@ namespace dawn_native { namespace d3d12 {
commandList->EndQuery(querySet->GetQueryHeap(), D3D12_QUERY_TYPE_TIMESTAMP,
cmd->queryIndex);
}
void RecordFirstIndexOffset(ID3D12GraphicsCommandList* commandList,
RenderPipeline* pipeline,
uint32_t firstVertex,
uint32_t firstInstance) {
const FirstOffsetInfo& firstOffsetInfo = pipeline->GetFirstOffsetInfo();
if (!firstOffsetInfo.usesVertexIndex && !firstOffsetInfo.usesInstanceIndex) {
return;
}
std::array<uint32_t, 2> offsets{};
uint32_t count = 0;
if (firstOffsetInfo.usesVertexIndex) {
offsets[firstOffsetInfo.vertexIndexOffset / sizeof(uint32_t)] = firstVertex;
++count;
}
if (firstOffsetInfo.usesInstanceIndex) {
offsets[firstOffsetInfo.instanceIndexOffset / sizeof(uint32_t)] = firstInstance;
++count;
}
PipelineLayout* layout = ToBackend(pipeline->GetLayout());
commandList->SetGraphicsRoot32BitConstants(layout->GetFirstIndexOffsetParameterIndex(),
count, offsets.data(), 0);
}
} // anonymous namespace
class BindGroupStateTracker : public BindGroupTrackerBase<false, uint64_t> {
@@ -1227,6 +1250,8 @@ namespace dawn_native { namespace d3d12 {
DAWN_TRY(bindingTracker->Apply(commandContext));
vertexBufferTracker.Apply(commandList, lastPipeline);
RecordFirstIndexOffset(commandList, lastPipeline, draw->firstVertex,
draw->firstInstance);
commandList->DrawInstanced(draw->vertexCount, draw->instanceCount,
draw->firstVertex, draw->firstInstance);
break;
@@ -1237,6 +1262,8 @@ namespace dawn_native { namespace d3d12 {
DAWN_TRY(bindingTracker->Apply(commandContext));
vertexBufferTracker.Apply(commandList, lastPipeline);
RecordFirstIndexOffset(commandList, lastPipeline, draw->baseVertex,
draw->firstInstance);
commandList->DrawIndexedInstanced(draw->indexCount, draw->instanceCount,
draw->firstIndex, draw->baseVertex,
draw->firstInstance);

View File

@@ -151,6 +151,39 @@ namespace dawn_native { namespace d3d12 {
}
}
// Since Tint's HLSL writer doesn't currently map sets to spaces, we use the default space
// (0).
mFirstIndexOffsetRegisterSpace = 0;
BindGroupIndex firstOffsetGroup{mFirstIndexOffsetRegisterSpace};
if (GetBindGroupLayoutsMask()[firstOffsetGroup]) {
// Find the last register used on firstOffsetGroup.
uint32_t maxRegister = 0;
for (uint32_t shaderRegister :
ToBackend(GetBindGroupLayout(firstOffsetGroup))->GetBindingOffsets()) {
if (shaderRegister > maxRegister) {
maxRegister = shaderRegister;
}
}
mFirstIndexOffsetShaderRegister = maxRegister + 1;
} else {
// firstOffsetGroup is not in use, we can use the first register.
mFirstIndexOffsetShaderRegister = 0;
}
D3D12_ROOT_PARAMETER indexOffsetConstants{};
indexOffsetConstants.ShaderVisibility = D3D12_SHADER_VISIBILITY_VERTEX;
indexOffsetConstants.ParameterType = D3D12_ROOT_PARAMETER_TYPE_32BIT_CONSTANTS;
// Always allocate 2 constants for vertex_index and instance_index
// NOTE: We should consider delaying root signature creation until we know how many values
// we need
indexOffsetConstants.Constants.Num32BitValues = 2;
indexOffsetConstants.Constants.RegisterSpace = mFirstIndexOffsetRegisterSpace;
indexOffsetConstants.Constants.ShaderRegister = mFirstIndexOffsetShaderRegister;
mFirstIndexOffsetParameterIndex = rootParameters.size();
// NOTE: We should consider moving this entry to earlier in the root signature since offsets
// would need to be updated often
rootParameters.emplace_back(indexOffsetConstants);
D3D12_ROOT_SIGNATURE_DESC rootSignatureDescriptor;
rootSignatureDescriptor.NumParameters = rootParameters.size();
rootSignatureDescriptor.pParameters = rootParameters.data();
@@ -195,4 +228,16 @@ namespace dawn_native { namespace d3d12 {
wgpu::ShaderStage::None);
return mDynamicRootParameterIndices[group][bindingIndex];
}
uint32_t PipelineLayout::GetFirstIndexOffsetRegisterSpace() const {
return mFirstIndexOffsetRegisterSpace;
}
uint32_t PipelineLayout::GetFirstIndexOffsetShaderRegister() const {
return mFirstIndexOffsetShaderRegister;
}
uint32_t PipelineLayout::GetFirstIndexOffsetParameterIndex() const {
return mFirstIndexOffsetParameterIndex;
}
}} // namespace dawn_native::d3d12

View File

@@ -36,6 +36,10 @@ namespace dawn_native { namespace d3d12 {
uint32_t GetDynamicRootParameterIndex(BindGroupIndex group,
BindingIndex bindingIndex) const;
uint32_t GetFirstIndexOffsetRegisterSpace() const;
uint32_t GetFirstIndexOffsetShaderRegister() const;
uint32_t GetFirstIndexOffsetParameterIndex() const;
ID3D12RootSignature* GetRootSignature() const;
private:
@@ -48,6 +52,9 @@ namespace dawn_native { namespace d3d12 {
ityp::array<BindingIndex, uint32_t, kMaxDynamicBuffersPerPipelineLayout>,
kMaxBindGroups>
mDynamicRootParameterIndices;
uint32_t mFirstIndexOffsetRegisterSpace;
uint32_t mFirstIndexOffsetShaderRegister;
uint32_t mFirstIndexOffsetParameterIndex;
ComPtr<ID3D12RootSignature> mRootSignature;
};

View File

@@ -333,6 +333,8 @@ namespace dawn_native { namespace d3d12 {
*shaders[stage] = compiledShader[stage].GetD3D12ShaderBytecode();
}
mFirstOffsetInfo = compiledShader[SingleShaderStage::Vertex].firstOffsetInfo;
PipelineLayout* layout = ToBackend(GetLayout());
descriptorD3D12.pRootSignature = layout->GetRootSignature();
@@ -403,6 +405,10 @@ namespace dawn_native { namespace d3d12 {
return mPipelineState.Get();
}
const FirstOffsetInfo& RenderPipeline::GetFirstOffsetInfo() const {
return mFirstOffsetInfo;
}
D3D12_INPUT_LAYOUT_DESC RenderPipeline::ComputeInputLayout(
std::array<D3D12_INPUT_ELEMENT_DESC, kMaxVertexAttributes>* inputElementDescriptors) {
unsigned int count = 0;

View File

@@ -17,6 +17,7 @@
#include "dawn_native/RenderPipeline.h"
#include "dawn_native/d3d12/ShaderModuleD3D12.h"
#include "dawn_native/d3d12/d3d12_platform.h"
namespace dawn_native { namespace d3d12 {
@@ -32,6 +33,8 @@ namespace dawn_native { namespace d3d12 {
D3D12_PRIMITIVE_TOPOLOGY GetD3D12PrimitiveTopology() const;
ID3D12PipelineState* GetPipelineState() const;
const FirstOffsetInfo& GetFirstOffsetInfo() const;
private:
~RenderPipeline() override;
using RenderPipelineBase::RenderPipelineBase;
@@ -41,6 +44,7 @@ namespace dawn_native { namespace d3d12 {
D3D12_PRIMITIVE_TOPOLOGY mD3d12PrimitiveTopology;
ComPtr<ID3D12PipelineState> mPipelineState;
FirstOffsetInfo mFirstOffsetInfo;
};
}} // namespace dawn_native::d3d12

View File

@@ -197,7 +197,8 @@ namespace dawn_native { namespace d3d12 {
const char* entryPointName,
SingleShaderStage stage,
PipelineLayout* layout,
std::string* remappedEntryPointName) const {
std::string* remappedEntryPointName,
FirstOffsetInfo* firstOffsetInfo) const {
ASSERT(!IsError());
#ifdef DAWN_ENABLE_WGSL
@@ -207,9 +208,32 @@ namespace dawn_native { namespace d3d12 {
tint::transform::Manager transformManager;
transformManager.append(std::make_unique<tint::transform::BoundArrayAccessors>());
tint::transform::FirstIndexOffset* firstOffsetTransform = nullptr;
if (stage == SingleShaderStage::Vertex) {
auto transformer = std::make_unique<tint::transform::FirstIndexOffset>(
layout->GetFirstIndexOffsetShaderRegister(),
layout->GetFirstIndexOffsetRegisterSpace());
firstOffsetTransform = transformer.get();
transformManager.append(std::move(transformer));
}
tint::ast::Module module;
DAWN_TRY_ASSIGN(module, RunTransforms(&transformManager, mTintModule.get()));
if (firstOffsetTransform != nullptr) {
// Functions are only available after transform has been performed
firstOffsetInfo->usesVertexIndex = firstOffsetTransform->HasVertexIndex();
if (firstOffsetInfo->usesVertexIndex) {
firstOffsetInfo->vertexIndexOffset = firstOffsetTransform->GetFirstVertexOffset();
}
firstOffsetInfo->usesInstanceIndex = firstOffsetTransform->HasInstanceIndex();
if (firstOffsetInfo->usesInstanceIndex) {
firstOffsetInfo->instanceIndexOffset =
firstOffsetTransform->GetFirstInstanceOffset();
}
}
ASSERT(remappedEntryPointName != nullptr);
tint::inspector::Inspector inspector(module);
*remappedEntryPointName = inspector.GetRemappedNameForEntryPoint(entryPointName);
@@ -302,9 +326,11 @@ namespace dawn_native { namespace d3d12 {
// Compile the source shader to HLSL.
std::string hlslSource;
std::string remappedEntryPoint;
CompiledShader compiledShader = {};
if (device->IsToggleEnabled(Toggle::UseTintGenerator)) {
DAWN_TRY_ASSIGN(hlslSource, TranslateToHLSLWithTint(entryPointName, stage, layout,
&remappedEntryPoint));
&remappedEntryPoint,
&compiledShader.firstOffsetInfo));
entryPointName = remappedEntryPoint.c_str();
} else {
DAWN_TRY_ASSIGN(hlslSource,
@@ -326,7 +352,6 @@ namespace dawn_native { namespace d3d12 {
DAWN_TRY_ASSIGN(shaderCacheKey,
CreateHLSLKey(entryPointName, stage, hlslSource, compileFlags));
CompiledShader compiledShader = {};
DAWN_TRY_ASSIGN(compiledShader.cachedShader,
device->GetPersistentCache()->GetOrCreate(
shaderCacheKey, [&](auto doCache) -> MaybeError {

View File

@@ -25,12 +25,22 @@ namespace dawn_native { namespace d3d12 {
class Device;
class PipelineLayout;
// Manages a ref to one of the various representations of shader blobs.
struct FirstOffsetInfo {
bool usesVertexIndex;
uint32_t vertexIndexOffset;
bool usesInstanceIndex;
uint32_t instanceIndexOffset;
};
// Manages a ref to one of the various representations of shader blobs and information used to
// emulate vertex/instance index starts
struct CompiledShader {
ScopedCachedBlob cachedShader;
ComPtr<ID3DBlob> compiledFXCShader;
ComPtr<IDxcBlob> compiledDXCShader;
D3D12_SHADER_BYTECODE GetD3D12ShaderBytecode() const;
FirstOffsetInfo firstOffsetInfo;
};
class ShaderModule final : public ShaderModuleBase {
@@ -49,11 +59,11 @@ namespace dawn_native { namespace d3d12 {
~ShaderModule() override = default;
MaybeError Initialize(ShaderModuleParseResult* parseResult);
ResultOrError<std::string> TranslateToHLSLWithTint(
const char* entryPointName,
SingleShaderStage stage,
PipelineLayout* layout,
std::string* remappedEntryPointName) const;
ResultOrError<std::string> TranslateToHLSLWithTint(const char* entryPointName,
SingleShaderStage stage,
PipelineLayout* layout,
std::string* remappedEntryPointName,
FirstOffsetInfo* firstOffsetInfo) const;
ResultOrError<std::string> TranslateToHLSLWithSPIRVCross(const char* entryPointName,
SingleShaderStage stage,