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 GrD3DPipelineState_DEFINED
9 #define GrD3DPipelineState_DEFINED
10 
11 #include "include/core/SkRefCnt.h"
12 #include "include/gpu/GrTypes.h"
13 #include "include/gpu/d3d/GrD3DTypes.h"
14 #include "src/gpu/GrManagedResource.h"
15 #include "src/gpu/d3d/GrD3DPipelineStateDataManager.h"
16 #include "src/gpu/glsl/GrGLSLProgramBuilder.h"
17 
18 #include <vector>
19 
20 class GrD3DDirectCommandList;
21 class GrD3DGpu;
22 class GrD3DPipeline;
23 class GrD3DRootSignature;
24 class GrProgramInfo;
25 
26 class GrD3DPipelineState {
27 public:
28     using UniformInfoArray = GrD3DPipelineStateDataManager::UniformInfoArray;
29 
30     GrD3DPipelineState(sk_sp<GrD3DPipeline> pipeline,
31                        sk_sp<GrD3DRootSignature> rootSignature,
32                        const GrGLSLBuiltinUniformHandles& builtinUniformHandles,
33                        const UniformInfoArray& uniforms,
34                        uint32_t uniformSize,
35                        uint32_t numSamplers,
36                        std::unique_ptr<GrGLSLGeometryProcessor> geometryProcessor,
37                        std::unique_ptr<GrGLSLXferProcessor> xferProcessor,
38                        std::vector<std::unique_ptr<GrGLSLFragmentProcessor>> fpImpls,
39                        size_t vertexStride,
40                        size_t instanceStride);
41 
pipeline()42     const sk_sp<GrD3DPipeline>& pipeline() const { return fPipeline; }
rootSignature()43     const sk_sp<GrD3DRootSignature>& rootSignature() const { return fRootSignature; }
44 
45     void setAndBindConstants(GrD3DGpu*, const GrRenderTarget*, const GrProgramInfo&);
46 
47     void setAndBindTextures(GrD3DGpu*,
48                             const GrGeometryProcessor&,
49                             const GrSurfaceProxy* const geomProcTextures[],
50                             const GrPipeline&);
51 
52     void bindBuffers(GrD3DGpu*, sk_sp<const GrBuffer> indexBuffer,
53                      sk_sp<const GrBuffer> instanceBuffer, sk_sp<const GrBuffer> vertexBuffer,
54                      GrD3DDirectCommandList* commandList);
55 
56     // We can only cache non dirty uniform values until we submit a command list. After that, the
57     // next frame will get a completely different uniform buffer and/or offset into the buffer. Thus
58     // we need a way to mark them all as dirty during submit.
markUniformsDirty()59     void markUniformsDirty() { fDataManager.markDirty(); }
60 
61 private:
62     /**
63      * We use the RT's size and origin to adjust from Skia device space to d3d normalized device
64      * space and to make device space positions have the correct origin for processors that require
65      * them.
66      */
67     struct RenderTargetState {
68         SkISize         fRenderTargetSize;
69         GrSurfaceOrigin fRenderTargetOrigin;
70 
RenderTargetStateRenderTargetState71         RenderTargetState() { this->invalidate(); }
invalidateRenderTargetState72         void invalidate() {
73             fRenderTargetSize.fWidth = -1;
74             fRenderTargetSize.fHeight = -1;
75             fRenderTargetOrigin = (GrSurfaceOrigin)-1;
76         }
77 
78         /**
79         * Gets a float4 that adjusts the position from Skia device coords to D3D's normalized device
80         * coords. Assuming the transformed position, pos, is a homogeneous float3, the vec, v, is
81         * applied as such:
82         * pos.x = dot(v.xy, pos.xz)
83         * pos.y = dot(v.zw, pos.yz)
84         */
getRTAdjustmentVecRenderTargetState85         void getRTAdjustmentVec(float* destVec) {
86             destVec[0] = 2.f / fRenderTargetSize.fWidth;
87             destVec[1] = -1.f;
88             // D3D's NDC space is flipped from Vulkan and Metal
89             if (kTopLeft_GrSurfaceOrigin == fRenderTargetOrigin) {
90                 destVec[2] = -2.f / fRenderTargetSize.fHeight;
91                 destVec[3] = 1.f;
92             } else {
93                 destVec[2] = 2.f / fRenderTargetSize.fHeight;
94                 destVec[3] = -1.f;
95             }
96         }
97     };
98 
99     // Helper for setData() that sets the view matrix and loads the render target height uniform
100     void setRenderTargetState(const GrRenderTarget*, GrSurfaceOrigin);
101 
102     sk_sp<GrD3DPipeline> fPipeline;
103     sk_sp<GrD3DRootSignature> fRootSignature;
104 
105     // Tracks the current render target uniforms stored in the vertex buffer.
106     RenderTargetState fRenderTargetState;
107     GrGLSLBuiltinUniformHandles fBuiltinUniformHandles;
108 
109     // Processors in the GrD3DPipelineState
110     std::unique_ptr<GrGLSLGeometryProcessor> fGeometryProcessor;
111     std::unique_ptr<GrGLSLXferProcessor> fXferProcessor;
112     std::vector<std::unique_ptr<GrGLSLFragmentProcessor>> fFPImpls;
113 
114     GrD3DPipelineStateDataManager fDataManager;
115 
116     unsigned int fNumSamplers;
117     size_t fVertexStride;
118     size_t fInstanceStride;
119 };
120 
121 #endif
122