aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/gpu/vk/GrVkPipelineState.h
blob: d0b0f564de5b22a4b27edc34b3e5cd16b3851d7a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
/*
 * Copyright 2016 Google Inc.
 *
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */


#ifndef GrVkPipelineState_DEFINED
#define GrVkPipelineState_DEFINED

#include "GrStencilSettings.h"
#include "GrVkImage.h"
#include "GrVkProgramDesc.h"
#include "GrVkPipelineStateDataManager.h"
#include "glsl/GrGLSLProgramBuilder.h"

#include "vk/GrVkDefines.h"

class GrPipeline;
class GrVkCommandBuffer;
class GrVkDescriptorPool;
class GrVkGpu;
class GrVkImageView;
class GrVkPipeline;
class GrVkSampler;
class GrVkUniformBuffer;

/**
 * This class holds onto a GrVkPipeline object that we use for draws. Besides storing the acutal
 * GrVkPipeline object, this class is also responsible handling all uniforms, descriptors, samplers,
 * and other similar objects that are used along with the VkPipeline in the draw. This includes both
 * allocating and freeing these objects, as well as updating their values.
 */
class GrVkPipelineState : public SkRefCnt {
public:
    typedef GrGLSLProgramBuilder::BuiltinUniformHandles BuiltinUniformHandles;

    ~GrVkPipelineState();

    GrVkPipeline* vkPipeline() const { return fPipeline; }

    void setData(GrVkGpu*, const GrPrimitiveProcessor&, const GrPipeline&);

    void bind(const GrVkGpu* gpu, GrVkCommandBuffer* commandBuffer);

    void addUniformResources(GrVkCommandBuffer&);

    void freeGPUResources(const GrVkGpu* gpu);

    // This releases resources that only a given instance of a GrVkPipelineState needs to hold onto
    // and don't need to survive across new uses of the GrVkPipelineState.
    void freeTempResources(const GrVkGpu* gpu);

    void abandonGPUResources();

    // The key is composed of two parts:
    // 1. uint32_t for total key length
    // 2. Pipeline state data
    enum StateKeyOffsets {
        // Part 1.
        kLength_StateKeyOffset = 0,
        // Part 2.
        kData_StateKeyOffset = kLength_StateKeyOffset + sizeof(uint32_t),
    };
    static void BuildStateKey(const GrPipeline&, GrPrimitiveType primitiveType,
                               SkTArray<unsigned char, true>* key);

    /**
     * For Vulkan we want to cache the entire VkPipeline for reuse of draws. The Desc here holds all
     * the information needed to differentiate one pipeline from another.
     *
     * The GrVkProgramDesc contains all the information need to create the actual shaders for the
     * pipeline.
     *
     * The fStateKey is used to store all the inputs for the rest of the state stored on the
     * pipeline. This includes stencil settings, blending information, render pass format, draw face
     * information, and primitive type. Note that some state is set dynamically on the pipeline for
     * each draw  and thus is not included in this descriptor. This includes the viewport, scissor,
     * and blend constant.
     *
     * A checksum which includes the fProgramDesc and fStateKey is included at the top of the Desc
     * for caching purposes and faster equality checks.
     */
    struct Desc {
        uint32_t                fChecksum;
        GrVkProgramDesc         fProgramDesc;

        enum {
            kRenderPassKeyAlloc = 12, // This is typical color attachment with no stencil or msaa
            kStencilKeyAlloc = sizeof(GrStencilSettings),
            kDrawFaceKeyAlloc = 4,
            kBlendingKeyAlloc = 4,
            kPrimitiveTypeKeyAlloc = 4,
            kPreAllocSize = kData_StateKeyOffset + kRenderPassKeyAlloc + kStencilKeyAlloc +
                            kDrawFaceKeyAlloc + kBlendingKeyAlloc + kPrimitiveTypeKeyAlloc,
        };
        SkSTArray<kPreAllocSize, uint8_t, true> fStateKey;

        bool operator== (const Desc& that) const {
            if (fChecksum != that.fChecksum || fProgramDesc != that.fProgramDesc) {
                return false;
            }
            // We store the keyLength at the start of fVkKey. Thus we don't have to worry about
            // different length keys since we will fail on the comparison immediately. Therefore we
            // just use this PipelineDesc to get the length to iterate over.
            int keyLength = fStateKey.count();
            SkASSERT(SkIsAlign4(keyLength));
            int l = keyLength >> 2;
            const uint32_t* aKey = reinterpret_cast<const uint32_t*>(fStateKey.begin());
            const uint32_t* bKey = reinterpret_cast<const uint32_t*>(that.fStateKey.begin());
            for (int i = 0; i < l; ++i) {
                if (aKey[i] != bKey[i]) {
                    return false;
                }
            }
            return true;
        }

        static bool Less(const Desc& a, const Desc& b) {
            if (a.fChecksum != b.fChecksum) {
                return a.fChecksum < b.fChecksum ? true : false;
            }
            bool progDescLess = GrProgramDesc::Less(a.fProgramDesc, b.fProgramDesc);
            if (progDescLess || a.fProgramDesc != b.fProgramDesc) {
                return progDescLess;
            }

            int keyLength = a.fStateKey.count();
            SkASSERT(SkIsAlign4(keyLength));
            int l = keyLength >> 2;
            const uint32_t* aKey = reinterpret_cast<const uint32_t*>(a.fStateKey.begin());
            const uint32_t* bKey = reinterpret_cast<const uint32_t*>(b.fStateKey.begin());
            for (int i = 0; i < l; ++i) {
                if (aKey[i] != bKey[i]) {
                    return aKey[i] < bKey[i] ? true : false;
                }
            }
            return false;
        }
    };

    const Desc& getDesc() { return fDesc; }

private:
    typedef GrVkPipelineStateDataManager::UniformInfoArray UniformInfoArray;
    typedef GrGLSLProgramDataManager::UniformHandle UniformHandle;

    GrVkPipelineState(GrVkGpu* gpu,
                      const GrVkPipelineState::Desc&,
                      GrVkPipeline* pipeline,
                      VkPipelineLayout layout,
                      VkDescriptorSetLayout dsSamplerLayout,
                      const BuiltinUniformHandles& builtinUniformHandles,
                      const UniformInfoArray& uniforms,
                      uint32_t vertexUniformSize,
                      uint32_t fragmentUniformSize,
                      uint32_t numSamplers,
                      GrGLSLPrimitiveProcessor* geometryProcessor,
                      GrGLSLXferProcessor* xferProcessor,
                      const GrGLSLFragProcs& fragmentProcessors);

    // Each pool will manage one type of descriptor. Thus each descriptor set we use will all be of
    // one VkDescriptorType.
    struct DescriptorPoolManager {
        DescriptorPoolManager(VkDescriptorSetLayout layout, VkDescriptorType type,
                              uint32_t descCount, GrVkGpu* gpu)
            : fDescLayout(layout)
            , fDescType(type)
            , fDescCountPerSet(descCount)
            , fCurrentDescriptorCount(0)
            , fPool(nullptr) {
            SkASSERT(descCount < kMaxDescLimit >> 2);
            fMaxDescriptors = fDescCountPerSet << 2;
            this->getNewPool(gpu);
        }

        ~DescriptorPoolManager() {
            SkASSERT(!fDescLayout);
            SkASSERT(!fPool);
        }

        void getNewDescriptorSet(GrVkGpu* gpu, VkDescriptorSet* ds);

        void freeGPUResources(const GrVkGpu* gpu);
        void abandonGPUResources();

        VkDescriptorSetLayout  fDescLayout;
        VkDescriptorType       fDescType;
        uint32_t               fDescCountPerSet;
        uint32_t               fMaxDescriptors;
        uint32_t               fCurrentDescriptorCount;
        GrVkDescriptorPool*    fPool;

    private:
        static const uint32_t kMaxDescLimit = 1 << 10;

        void getNewPool(GrVkGpu* gpu);
    };

    void writeUniformBuffers(const GrVkGpu* gpu);

    void writeSamplers(GrVkGpu* gpu, const SkTArray<const GrTextureAccess*>& textureBindings);

    /**
    * We use the RT's size and origin to adjust from Skia device space to vulkan normalized device
    * space and to make device space positions have the correct origin for processors that require
    * them.
    */
    struct RenderTargetState {
        SkISize         fRenderTargetSize;
        GrSurfaceOrigin fRenderTargetOrigin;

        RenderTargetState() { this->invalidate(); }
        void invalidate() {
            fRenderTargetSize.fWidth = -1;
            fRenderTargetSize.fHeight = -1;
            fRenderTargetOrigin = (GrSurfaceOrigin)-1;
        }

        /**
        * Gets a vec4 that adjusts the position from Skia device coords to Vulkans normalized device
        * coords. Assuming the transformed position, pos, is a homogeneous vec3, the vec, v, is
        * applied as such:
        * pos.x = dot(v.xy, pos.xz)
        * pos.y = dot(v.zw, pos.yz)
        */
        void getRTAdjustmentVec(float* destVec) {
            destVec[0] = 2.f / fRenderTargetSize.fWidth;
            destVec[1] = -1.f;
            if (kBottomLeft_GrSurfaceOrigin == fRenderTargetOrigin) {
                destVec[2] = -2.f / fRenderTargetSize.fHeight;
                destVec[3] = 1.f;
            } else {
                destVec[2] = 2.f / fRenderTargetSize.fHeight;
                destVec[3] = -1.f;
            }
        }
    };

    // Helper for setData() that sets the view matrix and loads the render target height uniform
    void setRenderTargetState(const GrPipeline&);

    // GrVkResources
    GrVkPipeline* fPipeline;

    // Used for binding DescriptorSets to the command buffer but does not need to survive during
    // command buffer execution. Thus this is not need to be a GrVkResource.
    VkPipelineLayout fPipelineLayout;

    // The DescriptorSets need to survive until the gpu has finished all draws that use them.
    // However, they will only be freed by the descriptor pool. Thus by simply keeping the
    // descriptor pool alive through the draw, the descritor sets will also stay alive. Thus we do
    // not need a GrVkResource versions of VkDescriptorSet. We hold on to these in the
    // GrVkPipelineState since we update the descriptor sets and bind them at separate times;
    VkDescriptorSet fDescriptorSets[2];

    // Meta data so we know which descriptor sets we are using and need to bind.
    int fStartDS;
    int fDSCount;

    SkAutoTDelete<GrVkUniformBuffer> fVertexUniformBuffer;
    SkAutoTDelete<GrVkUniformBuffer> fFragmentUniformBuffer;

    // GrVkResources used for sampling textures
    SkTDArray<GrVkSampler*> fSamplers;
    SkTDArray<const GrVkImageView*> fTextureViews;
    SkTDArray<const GrVkImage::Resource*> fTextures;

    // Tracks the current render target uniforms stored in the vertex buffer.
    RenderTargetState fRenderTargetState;
    BuiltinUniformHandles fBuiltinUniformHandles;

    // Processors in the GrVkPipelineState
    SkAutoTDelete<GrGLSLPrimitiveProcessor> fGeometryProcessor;
    SkAutoTDelete<GrGLSLXferProcessor> fXferProcessor;
    GrGLSLFragProcs fFragmentProcessors;

    Desc fDesc;

    GrVkPipelineStateDataManager fDataManager;

    DescriptorPoolManager fSamplerPoolManager;
    const GrVkDescriptorPool*   fCurrentUniformDescPool;

    int fNumSamplers;

    friend class GrVkPipelineStateBuilder;
};

#endif