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
20class GrD3DDirectCommandList;
21class GrD3DGpu;
22class GrD3DPipeline;
23class GrD3DRootSignature;
24class GrProgramInfo;
25
26class GrD3DPipelineState {
27public:
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<GrGeometryProcessor::ProgramImpl> gpImpl,
37                       std::unique_ptr<GrXferProcessor::ProgramImpl> xpImpl,
38                       std::vector<std::unique_ptr<GrFragmentProcessor::ProgramImpl>> fpImpls,
39                       size_t vertexStride,
40                       size_t instanceStride);
41
42    const sk_sp<GrD3DPipeline>& pipeline() const { return fPipeline; }
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.
59    void markUniformsDirty() { fDataManager.markDirty(); }
60
61private:
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
71        RenderTargetState() { this->invalidate(); }
72        void invalidate() {
73            fRenderTargetSize.fWidth = -1;
74            fRenderTargetSize.fHeight = -1;
75            fRenderTargetOrigin = (GrSurfaceOrigin)-1;
76        }
77    };
78
79    // Helper for setData() that sets the view matrix and loads the render target height uniform
80    void setRenderTargetState(const GrRenderTarget*, GrSurfaceOrigin);
81
82    sk_sp<GrD3DPipeline> fPipeline;
83    sk_sp<GrD3DRootSignature> fRootSignature;
84
85    // Tracks the current render target uniforms stored in the vertex buffer.
86    RenderTargetState fRenderTargetState;
87    GrGLSLBuiltinUniformHandles fBuiltinUniformHandles;
88
89    // Processors in the GrD3DPipelineState
90    std::unique_ptr<GrGeometryProcessor::ProgramImpl>              fGPImpl;
91    std::unique_ptr<GrXferProcessor::ProgramImpl>                  fXPImpl;
92    std::vector<std::unique_ptr<GrFragmentProcessor::ProgramImpl>> fFPImpls;
93
94    GrD3DPipelineStateDataManager fDataManager;
95
96    unsigned int fNumSamplers;
97    size_t fVertexStride;
98    size_t fInstanceStride;
99};
100
101#endif
102