1/*
2 * Copyright 2019 Google Inc.
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 GrSPIRVUniformHandler_DEFINED
9#define GrSPIRVUniformHandler_DEFINED
10
11#include "src/core/SkTBlockList.h"
12#include "src/gpu/glsl/GrGLSLUniformHandler.h"
13
14/*
15 * This class can be used for basic SPIR-V uniform handling. It will make a single uniform buffer
16 * for all the uniforms and will be placed in the first set and binding. Textures and samplers are
17 * placed in the second set and kept as separate objects. They are interleaved as sampler texture
18 * pairs with each object in the next binding slot.
19 */
20class GrSPIRVUniformHandler : public GrGLSLUniformHandler {
21public:
22    static const int kUniformsPerBlock = 8;
23
24    const GrShaderVar& getUniformVariable(UniformHandle u) const override;
25    const char* getUniformCStr(UniformHandle u) const override;
26
27    struct SPIRVUniformInfo : public UniformInfo {
28        int fUBOOffset;
29    };
30    typedef SkTBlockList<SPIRVUniformInfo> UniformInfoArray;
31    enum {
32        kUniformBinding = 0,
33        kUniformDescriptorSet = 0,
34        kSamplerTextureDescriptorSet = 1,
35    };
36    uint32_t getRTFlipOffset() const;
37
38    int numUniforms() const override {
39        return fUniforms.count();
40    }
41
42    UniformInfo& uniform(int idx) override {
43        return fUniforms.item(idx);
44    }
45    const UniformInfo& uniform(int idx) const override {
46        return fUniforms.item(idx);
47    }
48
49private:
50    explicit GrSPIRVUniformHandler(GrGLSLProgramBuilder* program);
51
52    SamplerHandle addSampler(const GrBackendFormat&, GrSamplerState, const GrSwizzle&,
53                             const char* name, const GrShaderCaps*) override;
54    const char* samplerVariable(SamplerHandle handle) const override;
55    GrSwizzle samplerSwizzle(SamplerHandle handle) const override;
56    void appendUniformDecls(GrShaderFlags visibility, SkString*) const override;
57    UniformHandle internalAddUniformArray(const GrFragmentProcessor* owner,
58                                          uint32_t visibility,
59                                          GrSLType type,
60                                          const char* name,
61                                          bool mangleName,
62                                          int arrayCount,
63                                          const char** outName) override;
64
65    UniformInfoArray    fUniforms;
66    UniformInfoArray    fSamplers;
67    UniformInfoArray    fTextures;
68    SkTArray<GrSwizzle> fSamplerSwizzles;
69    SkTArray<SkString>  fSamplerReferences;
70
71    uint32_t fCurrentUBOOffset = 0;
72    uint32_t fRTFlipOffset = 0;
73
74    friend class GrD3DPipelineStateBuilder;
75    friend class GrDawnProgramBuilder;
76
77    using INHERITED = GrGLSLUniformHandler;
78};
79
80#endif
81