Bind D3D constants.

Final step to get shader constants working. Changes the root
signature to store the constant buffer view directly inside it, and
sets it via the command list.

Change-Id: Ib82a5cc60dd54347f2c5b6885f3ef553e005e760
Bug: skia:9935
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/293348
Reviewed-by: Greg Daniel <egdaniel@google.com>
Commit-Queue: Jim Van Verth <jvanverth@google.com>
diff --git a/src/gpu/d3d/GrD3DCommandList.cpp b/src/gpu/d3d/GrD3DCommandList.cpp
index 4703bbb..363a3f7 100644
--- a/src/gpu/d3d/GrD3DCommandList.cpp
+++ b/src/gpu/d3d/GrD3DCommandList.cpp
@@ -347,6 +347,11 @@
     fCommandList->OMSetRenderTargets(1, &rtvDescriptor, false, nullptr);
 }
 
+void GrD3DDirectCommandList::setGraphicsRootConstantBufferView(
+        unsigned int rootParameterIndex, D3D12_GPU_VIRTUAL_ADDRESS bufferLocation) {
+    fCommandList->SetGraphicsRootConstantBufferView(rootParameterIndex, bufferLocation);
+}
+
 ////////////////////////////////////////////////////////////////////////////////////////////////////
 
 std::unique_ptr<GrD3DCopyCommandList> GrD3DCopyCommandList::Make(ID3D12Device* device) {
diff --git a/src/gpu/d3d/GrD3DCommandList.h b/src/gpu/d3d/GrD3DCommandList.h
index 86d51da..9652234 100644
--- a/src/gpu/d3d/GrD3DCommandList.h
+++ b/src/gpu/d3d/GrD3DCommandList.h
@@ -146,6 +146,9 @@
                                const GrScissorState& scissor);
     void setRenderTarget(GrD3DRenderTarget* renderTarget);
 
+    void setGraphicsRootConstantBufferView(unsigned int rootParameterIndex,
+                                           D3D12_GPU_VIRTUAL_ADDRESS bufferLocation);
+
 private:
     GrD3DDirectCommandList(gr_cp<ID3D12CommandAllocator> allocator,
                            gr_cp<ID3D12GraphicsCommandList> commandList);
diff --git a/src/gpu/d3d/GrD3DOpsRenderPass.cpp b/src/gpu/d3d/GrD3DOpsRenderPass.cpp
index 7280d58..1a35c23 100644
--- a/src/gpu/d3d/GrD3DOpsRenderPass.cpp
+++ b/src/gpu/d3d/GrD3DOpsRenderPass.cpp
@@ -160,11 +160,10 @@
         return false;
     }
 
-    fCurrentPipelineState->setData(fGpu, fRenderTarget, info);
     fGpu->currentCommandList()->setGraphicsRootSignature(fCurrentPipelineState->rootSignature());
     fGpu->currentCommandList()->setPipelineState(fCurrentPipelineState);
 
-    // TODO: bind uniforms (either a new method or in pipelineState->setData())
+    fCurrentPipelineState->setAndBindConstants(fGpu, fRenderTarget, info);
 
     set_stencil_ref(fGpu, info);
     set_blend_factor(fGpu, info);
diff --git a/src/gpu/d3d/GrD3DPipelineState.cpp b/src/gpu/d3d/GrD3DPipelineState.cpp
index f025fed..c9db9a4 100644
--- a/src/gpu/d3d/GrD3DPipelineState.cpp
+++ b/src/gpu/d3d/GrD3DPipelineState.cpp
@@ -42,8 +42,9 @@
     , fVertexStride(vertexStride)
     , fInstanceStride(instanceStride) {}
 
-void GrD3DPipelineState::setData(GrD3DGpu* gpu, const GrRenderTarget* renderTarget,
-                                 const GrProgramInfo& programInfo) {
+void GrD3DPipelineState::setAndBindConstants(GrD3DGpu* gpu,
+                                             const GrRenderTarget* renderTarget,
+                                             const GrProgramInfo& programInfo) {
     this->setRenderTargetState(renderTarget, programInfo.origin());
 
     GrFragmentProcessor::PipelineCoordTransformRange transformRange(programInfo.pipeline());
@@ -63,8 +64,10 @@
                                 dstTexture, offset);
     }
 
-    // TODO: use returned virtual address to create a CBV and set in command list
-    (void) fDataManager.uploadConstants(gpu);
+    D3D12_GPU_VIRTUAL_ADDRESS constantsAddress = fDataManager.uploadConstants(gpu);
+    gpu->currentCommandList()->setGraphicsRootConstantBufferView(
+        (unsigned int)(GrD3DRootSignature::ParamIndex::kConstantBufferView),
+        constantsAddress);
 }
 
 void GrD3DPipelineState::setRenderTargetState(const GrRenderTarget* rt, GrSurfaceOrigin origin) {
diff --git a/src/gpu/d3d/GrD3DPipelineState.h b/src/gpu/d3d/GrD3DPipelineState.h
index 63c5482..e6aa71b 100644
--- a/src/gpu/d3d/GrD3DPipelineState.h
+++ b/src/gpu/d3d/GrD3DPipelineState.h
@@ -52,7 +52,7 @@
     ID3D12PipelineState* pipelineState() const { return fPipelineState.get(); }
     const sk_sp<GrD3DRootSignature>& rootSignature() const { return fRootSignature; }
 
-    void setData(GrD3DGpu*, const GrRenderTarget* renderTarget, const GrProgramInfo& programInfo);
+    void setAndBindConstants(GrD3DGpu*, const GrRenderTarget*, const GrProgramInfo&);
 
     void setAndBindTextures(const GrPrimitiveProcessor& primProc,
                             const GrSurfaceProxy* const primProcTextures[],
diff --git a/src/gpu/d3d/GrD3DRootSignature.cpp b/src/gpu/d3d/GrD3DRootSignature.cpp
index b4f2d8d..e64faa2 100644
--- a/src/gpu/d3d/GrD3DRootSignature.cpp
+++ b/src/gpu/d3d/GrD3DRootSignature.cpp
@@ -15,17 +15,9 @@
     D3D12_ROOT_PARAMETER parameters[3];
 
     // The first will always be our uniforms
-    D3D12_DESCRIPTOR_RANGE uniformRange{};
-    uniformRange.RangeType = D3D12_DESCRIPTOR_RANGE_TYPE_CBV;
-    uniformRange.NumDescriptors = 1;
-    uniformRange.BaseShaderRegister = 0;
-    // Spirv-Cross uses the descriptor set as the space in HSLS
-    uniformRange.RegisterSpace = GrSPIRVUniformHandler::kUniformDescriptorSet;
-    uniformRange.OffsetInDescriptorsFromTableStart = D3D12_DESCRIPTOR_RANGE_OFFSET_APPEND;
-
-    parameters[0].ParameterType = D3D12_ROOT_PARAMETER_TYPE_DESCRIPTOR_TABLE;
-    parameters[0].DescriptorTable.NumDescriptorRanges = 1;
-    parameters[0].DescriptorTable.pDescriptorRanges = &uniformRange;
+    parameters[0].ParameterType = D3D12_ROOT_PARAMETER_TYPE_CBV;
+    parameters[0].Descriptor.ShaderRegister = 0;
+    parameters[0].Descriptor.RegisterSpace = GrSPIRVUniformHandler::kUniformDescriptorSet;
     parameters[0].ShaderVisibility = D3D12_SHADER_VISIBILITY_ALL;
 
     SkAutoTArray<D3D12_DESCRIPTOR_RANGE> samplerRanges(numTextureSamplers);
diff --git a/src/gpu/d3d/GrD3DRootSignature.h b/src/gpu/d3d/GrD3DRootSignature.h
index c133e2e..025ac4d 100644
--- a/src/gpu/d3d/GrD3DRootSignature.h
+++ b/src/gpu/d3d/GrD3DRootSignature.h
@@ -17,6 +17,12 @@
 public:
     static sk_sp<GrD3DRootSignature> Make(GrD3DGpu* gpu, int numTextureSamplers);
 
+    enum class ParamIndex {
+        kConstantBufferView = 0,
+        kSamplerDescriptorTable = 1,
+        kTextureDescriptorTable = 2
+    };
+
     bool isCompatible(int numTextureSamplers) const;
 
     ID3D12RootSignature* rootSignature() const { return fRootSignature.get(); }