xref: /aosp_15_r20/external/skia/src/gpu/ganesh/d3d/GrD3DPipelineStateBuilder.h (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
1 /*
2  * Copyright 2020 Google LLC
3  *
4  * Use of this source code is governed by a BSD-style license that can be
5  * found in the LICENSE file.
6  */
7 
8 #ifndef GrD3DPipelineStateBuilder_DEFINED
9 #define GrD3DPipelineStateBuilder_DEFINED
10 
11 #include "src/gpu/SkSLToBackend.h"
12 #include "src/gpu/ganesh/GrPipeline.h"
13 #include "src/gpu/ganesh/GrSPIRVUniformHandler.h"
14 #include "src/gpu/ganesh/GrSPIRVVaryingHandler.h"
15 #include "src/gpu/ganesh/d3d/GrD3DPipelineState.h"
16 #include "src/gpu/ganesh/glsl/GrGLSLProgramBuilder.h"
17 #include "src/sksl/codegen/SkSLHLSLCodeGenerator.h"
18 #include "src/sksl/ir/SkSLProgram.h"
19 
20 namespace SkSL {
21 
22 enum class ProgramKind : int8_t;
23 struct ProgramInterface;
24 struct ProgramSettings;
25 struct ShaderCaps;
26 
27 }  // namespace SkSL
28 
29 class GrProgramDesc;
30 class GrD3DGpu;
31 class GrVkRenderPass;
32 
33 class GrD3DPipelineStateBuilder : public GrGLSLProgramBuilder {
34 public:
35     /** Generates a pipeline state.
36      *
37      * The returned GrD3DPipelineState implements the supplied GrProgramInfo.
38      *
39      * @return the created pipeline if generation was successful; nullptr otherwise
40      */
41     static std::unique_ptr<GrD3DPipelineState> MakePipelineState(GrD3DGpu*,
42                                                                  GrD3DRenderTarget*,
43                                                                  const GrProgramDesc&,
44                                                                  const GrProgramInfo&);
45 
46     static sk_sp<GrD3DPipeline> MakeComputePipeline(GrD3DGpu*, GrD3DRootSignature*,
47                                                     const char* shader);
48 
49     const GrCaps* caps() const override;
50 
gpu()51     GrD3DGpu* gpu() const { return fGpu; }
52 
53     void finalizeFragmentSecondaryColor(GrShaderVar& outputColor) override;
54 
55 private:
56     GrD3DPipelineStateBuilder(GrD3DGpu*, GrD3DRenderTarget*, const GrProgramDesc&,
57                               const GrProgramInfo&);
58 
59     std::unique_ptr<GrD3DPipelineState> finalize();
60 
61     bool loadHLSLFromCache(SkReadBuffer* reader, gr_cp<ID3DBlob> shaders[]);
62 
63     gr_cp<ID3DBlob> compileD3DProgram(SkSL::ProgramKind kind,
64                                       const std::string& sksl,
65                                       const SkSL::ProgramSettings& settings,
66                                       SkSL::Program::Interface* outInterface,
67                                       std::string* outHLSL);
68 
uniformHandler()69     GrGLSLUniformHandler* uniformHandler() override { return &fUniformHandler; }
uniformHandler()70     const GrGLSLUniformHandler* uniformHandler() const override { return &fUniformHandler; }
varyingHandler()71     GrGLSLVaryingHandler* varyingHandler() override { return &fVaryingHandler; }
72 
73     GrD3DGpu* fGpu;
74     GrSPIRVVaryingHandler fVaryingHandler;
75     GrSPIRVUniformHandler fUniformHandler;
76     GrD3DRenderTarget* fRenderTarget;
77 
78     using INHERITED = GrGLSLProgramBuilder;
79 };
80 
81 namespace skgpu {
82 
83 class ShaderErrorHandler;
84 
SkSLToHLSL(const SkSL::ShaderCaps * caps,const std::string & sksl,SkSL::ProgramKind programKind,const SkSL::ProgramSettings & settings,std::string * hlsl,SkSL::ProgramInterface * outInterface,ShaderErrorHandler * errorHandler)85 inline bool SkSLToHLSL(const SkSL::ShaderCaps* caps,
86                        const std::string& sksl,
87                        SkSL::ProgramKind programKind,
88                        const SkSL::ProgramSettings& settings,
89                        std::string* hlsl,
90                        SkSL::ProgramInterface* outInterface,
91                        ShaderErrorHandler* errorHandler) {
92     return SkSLToBackend(caps, &SkSL::ToHLSL, "HLSL",
93                          sksl, programKind, settings, hlsl, outInterface, errorHandler);
94 }
95 
96 }  // namespace skgpu
97 
98 #endif
99