d3d12: Update PSO creation to use CreatePipelineState
authorGiancarlo Devich <gdevich@microsoft.com>
Tue, 11 Apr 2023 23:41:17 +0000 (16:41 -0700)
committerMarge Bot <emma+marge@anholt.net>
Fri, 14 Apr 2023 03:19:22 +0000 (03:19 +0000)
This uses CD3DX12_PIPELINE_STATE_STREAM3 from
d3dx12_pipeline_state_stream.h and gives us access to newer D3D12
features.

Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/22454>

src/gallium/drivers/d3d12/d3d12_context.cpp
src/gallium/drivers/d3d12/d3d12_pipeline_state.cpp
src/gallium/drivers/d3d12/d3d12_pipeline_state.h

index bfe321a..08de834 100644 (file)
@@ -427,10 +427,10 @@ stencil_op(enum pipe_stencil_op op)
    unreachable("unexpected op");
 }
 
-static D3D12_DEPTH_STENCILOP_DESC
+static D3D12_DEPTH_STENCILOP_DESC1
 stencil_op_state(const struct pipe_stencil_state *src)
 {
-   D3D12_DEPTH_STENCILOP_DESC ret;
+   D3D12_DEPTH_STENCILOP_DESC1 ret;
    ret.StencilFailOp = stencil_op((pipe_stencil_op) src->fail_op);
    ret.StencilPassOp = stencil_op((pipe_stencil_op) src->zpass_op);
    ret.StencilDepthFailOp = stencil_op((pipe_stencil_op) src->zfail_op);
@@ -470,8 +470,8 @@ d3d12_create_depth_stencil_alpha_state(struct pipe_context *pctx,
    else
       dsa->desc.BackFace = dsa->desc.FrontFace;
 
-   dsa->desc.StencilReadMask = depth_stencil_alpha->stencil[0].valuemask; /* FIXME Back face mask */
-   dsa->desc.StencilWriteMask = depth_stencil_alpha->stencil[0].writemask; /* FIXME Back face mask */
+   dsa->desc.FrontFace.StencilReadMask = dsa->desc.BackFace.StencilReadMask = depth_stencil_alpha->stencil[0].valuemask; /* FIXME Back face mask */
+   dsa->desc.FrontFace.StencilWriteMask = dsa->desc.BackFace.StencilWriteMask = depth_stencil_alpha->stencil[0].writemask; /* FIXME Back face mask */
    dsa->desc.DepthWriteMask = (D3D12_DEPTH_WRITE_MASK) depth_stencil_alpha->depth_writemask;
 
    return dsa;
index 8d7359f..030ed47 100644 (file)
  * IN THE SOFTWARE.
  */
 
+
 #include "d3d12_pipeline_state.h"
 #include "d3d12_compiler.h"
 #include "d3d12_context.h"
 #include "d3d12_screen.h"
+#include <directx/d3dx12_pipeline_state_stream.h>
 
 #include "util/hash_table.h"
 #include "util/set.h"
@@ -224,36 +226,32 @@ create_gfx_pipeline_state(struct d3d12_context *ctx)
    UINT strides[PIPE_MAX_SO_OUTPUTS] = { 0 };
    UINT num_entries = 0, num_strides = 0;
 
-   D3D12_GRAPHICS_PIPELINE_STATE_DESC pso_desc = { 0 };
+   CD3DX12_PIPELINE_STATE_STREAM3 pso_desc;
    pso_desc.pRootSignature = state->root_signature;
 
    nir_shader *last_vertex_stage_nir = NULL;
 
    if (state->stages[PIPE_SHADER_VERTEX]) {
       auto shader = state->stages[PIPE_SHADER_VERTEX];
-      pso_desc.VS.BytecodeLength = shader->bytecode_length;
-      pso_desc.VS.pShaderBytecode = shader->bytecode;
+      pso_desc.VS = D3D12_SHADER_BYTECODE { shader->bytecode, shader->bytecode_length };
       last_vertex_stage_nir = shader->nir;
    }
 
    if (state->stages[PIPE_SHADER_TESS_CTRL]) {
       auto shader = state->stages[PIPE_SHADER_TESS_CTRL];
-      pso_desc.HS.BytecodeLength = shader->bytecode_length;
-      pso_desc.HS.pShaderBytecode = shader->bytecode;
+      pso_desc.HS = D3D12_SHADER_BYTECODE{ shader->bytecode, shader->bytecode_length };
       last_vertex_stage_nir = shader->nir;
    }
 
    if (state->stages[PIPE_SHADER_TESS_EVAL]) {
       auto shader = state->stages[PIPE_SHADER_TESS_EVAL];
-      pso_desc.DS.BytecodeLength = shader->bytecode_length;
-      pso_desc.DS.pShaderBytecode = shader->bytecode;
+      pso_desc.DS = D3D12_SHADER_BYTECODE{ shader->bytecode, shader->bytecode_length };
       last_vertex_stage_nir = shader->nir;
    }
 
    if (state->stages[PIPE_SHADER_GEOMETRY]) {
       auto shader = state->stages[PIPE_SHADER_GEOMETRY];
-      pso_desc.GS.BytecodeLength = shader->bytecode_length;
-      pso_desc.GS.pShaderBytecode = shader->bytecode;
+      pso_desc.GS = D3D12_SHADER_BYTECODE{ shader->bytecode, shader->bytecode_length };
       last_vertex_stage_nir = shader->nir;
    }
 
@@ -261,54 +259,61 @@ create_gfx_pipeline_state(struct d3d12_context *ctx)
    if (last_vertex_stage_writes_pos && state->stages[PIPE_SHADER_FRAGMENT] &&
        !state->rast->base.rasterizer_discard) {
       auto shader = state->stages[PIPE_SHADER_FRAGMENT];
-      pso_desc.PS.BytecodeLength = shader->bytecode_length;
-      pso_desc.PS.pShaderBytecode = shader->bytecode;
+      pso_desc.PS = D3D12_SHADER_BYTECODE{ shader->bytecode, shader->bytecode_length };
    }
 
    if (state->num_so_targets)
       fill_so_declaration(&state->so_info, last_vertex_stage_nir, entries, &num_entries, strides, &num_strides);
-   pso_desc.StreamOutput.NumEntries = num_entries;
-   pso_desc.StreamOutput.pSODeclaration = entries;
-   pso_desc.StreamOutput.RasterizedStream = state->rast->base.rasterizer_discard ? D3D12_SO_NO_RASTERIZED_STREAM : 0;
-   pso_desc.StreamOutput.NumStrides = num_strides;
-   pso_desc.StreamOutput.pBufferStrides = strides;
 
-   pso_desc.BlendState = state->blend->desc;
+   D3D12_STREAM_OUTPUT_DESC& stream_output_desc = (D3D12_STREAM_OUTPUT_DESC&)pso_desc.StreamOutput;
+   stream_output_desc.NumEntries = num_entries;
+   stream_output_desc.pSODeclaration = entries;
+   stream_output_desc.RasterizedStream = state->rast->base.rasterizer_discard ? D3D12_SO_NO_RASTERIZED_STREAM : 0;
+   stream_output_desc.NumStrides = num_strides;
+   stream_output_desc.pBufferStrides = strides;
+   pso_desc.StreamOutput = stream_output_desc;
+
+   D3D12_BLEND_DESC& blend_state = (D3D12_BLEND_DESC&)pso_desc.BlendState;
+   blend_state = state->blend->desc;
    if (state->has_float_rtv)
-      pso_desc.BlendState.RenderTarget[0].LogicOpEnable = FALSE;
+      blend_state.RenderTarget[0].LogicOpEnable = FALSE;
 
-   pso_desc.DepthStencilState = state->zsa->desc;
+   (D3D12_DEPTH_STENCIL_DESC2&)pso_desc.DepthStencilState = state->zsa->desc;
    pso_desc.SampleMask = state->sample_mask;
-   pso_desc.RasterizerState = state->rast->desc;
+
+   D3D12_RASTERIZER_DESC& rast = (D3D12_RASTERIZER_DESC&)pso_desc.RasterizerState;
+   rast = state->rast->desc;
 
    if (reduced_prim != PIPE_PRIM_TRIANGLES)
-      pso_desc.RasterizerState.CullMode = D3D12_CULL_MODE_NONE;
+      rast.CullMode = D3D12_CULL_MODE_NONE;
 
    if (depth_bias(state->rast, reduced_prim)) {
-      pso_desc.RasterizerState.DepthBias = state->rast->base.offset_units * 2;
-      pso_desc.RasterizerState.DepthBiasClamp = state->rast->base.offset_clamp;
-      pso_desc.RasterizerState.SlopeScaledDepthBias = state->rast->base.offset_scale;
+      rast.DepthBias = state->rast->base.offset_units * 2;
+      rast.DepthBiasClamp = state->rast->base.offset_clamp;
+      rast.SlopeScaledDepthBias = state->rast->base.offset_scale;
    }
-
-   pso_desc.InputLayout.pInputElementDescs = state->ves->elements;
-   pso_desc.InputLayout.NumElements = state->ves->num_elements;
+   D3D12_INPUT_LAYOUT_DESC& input_layout = (D3D12_INPUT_LAYOUT_DESC&)pso_desc.InputLayout;
+   input_layout.pInputElementDescs = state->ves->elements;
+   input_layout.NumElements = state->ves->num_elements;
 
    pso_desc.IBStripCutValue = state->ib_strip_cut_value;
 
    pso_desc.PrimitiveTopologyType = topology_type(reduced_prim);
 
-   pso_desc.NumRenderTargets = state->num_cbufs;
+   D3D12_RT_FORMAT_ARRAY& render_targets = (D3D12_RT_FORMAT_ARRAY&)pso_desc.RTVFormats;
+   render_targets.NumRenderTargets = state->num_cbufs;
    for (unsigned i = 0; i < state->num_cbufs; ++i)
-      pso_desc.RTVFormats[i] = d3d12_rtv_format(ctx, i);
+      render_targets.RTFormats[i] = d3d12_rtv_format(ctx, i);
    pso_desc.DSVFormat = state->dsv_format;
 
-   pso_desc.SampleDesc.Count = state->samples;
+   DXGI_SAMPLE_DESC& samples = (DXGI_SAMPLE_DESC&)pso_desc.SampleDesc;
+   samples.Count = state->samples;
    if (state->num_cbufs || state->dsv_format != DXGI_FORMAT_UNKNOWN) {
       if (!state->zsa->desc.DepthEnable &&
           !state->zsa->desc.StencilEnable &&
           !state->rast->desc.MultisampleEnable &&
           state->samples > 1) {
-         pso_desc.RasterizerState.ForcedSampleCount = 1;
+         rast.ForcedSampleCount = 1;
          pso_desc.DSVFormat = DXGI_FORMAT_UNKNOWN;
       }
    } else if (state->samples > 1) {
@@ -316,24 +321,41 @@ create_gfx_pipeline_state(struct d3d12_context *ctx)
       if (!(screen->opts19.SupportedSampleCountsWithNoOutputs & (1 << state->samples)))
 #endif
       {
-         pso_desc.SampleDesc.Count = 1;
-         pso_desc.RasterizerState.ForcedSampleCount = state->samples;
+         samples.Count = 1;
+         rast.ForcedSampleCount = state->samples;
       }
    }
-   pso_desc.SampleDesc.Quality = 0;
+   samples.Quality = 0;
 
    pso_desc.NodeMask = 0;
 
-   pso_desc.CachedPSO.pCachedBlob = NULL;
-   pso_desc.CachedPSO.CachedBlobSizeInBytes = 0;
+   D3D12_CACHED_PIPELINE_STATE& cached_pso = (D3D12_CACHED_PIPELINE_STATE&)pso_desc.CachedPSO;
+   cached_pso.pCachedBlob = NULL;
+   cached_pso.CachedBlobSizeInBytes = 0;
 
    pso_desc.Flags = D3D12_PIPELINE_STATE_FLAG_NONE;
 
    ID3D12PipelineState *ret;
-   if (FAILED(screen->dev->CreateGraphicsPipelineState(&pso_desc,
+
+   if (screen->opts14.IndependentFrontAndBackStencilRefMaskSupported) {
+      D3D12_PIPELINE_STATE_STREAM_DESC pso_stream_desc{
+          sizeof(pso_desc),
+          &pso_desc
+      };
+
+      if (FAILED(screen->dev->CreatePipelineState(&pso_stream_desc,
+                                                  IID_PPV_ARGS(&ret)))) {
+         debug_printf("D3D12: CreateGraphicsPipelineState failed!\n");
+         return NULL;
+      }
+   } 
+   else {
+      D3D12_GRAPHICS_PIPELINE_STATE_DESC v0desc = pso_desc.GraphicsDescV0();
+      if (FAILED(screen->dev->CreateGraphicsPipelineState(&v0desc,
                                                        IID_PPV_ARGS(&ret)))) {
-      debug_printf("D3D12: CreateGraphicsPipelineState failed!\n");
-      return NULL;
+         debug_printf("D3D12: CreateGraphicsPipelineState failed!\n");
+         return NULL;
+      }
    }
 
    return ret;
index 42d7023..400df34 100644 (file)
@@ -52,7 +52,7 @@ struct d3d12_blend_state {
 };
 
 struct d3d12_depth_stencil_alpha_state {
-   D3D12_DEPTH_STENCIL_DESC desc;
+   D3D12_DEPTH_STENCIL_DESC2 desc;
 };
 
 struct d3d12_gfx_pipeline_state {