blob: 44475c5e1e94b3abcf3a3141f131cfc3a5633268 [file] [log] [blame]
Stephen White4da34bf2019-07-30 10:37:47 -04001/*
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
Greg Danield59a91d2020-04-23 13:22:47 -04008#ifndef GrSPIRVUniformHandler_DEFINED
9#define GrSPIRVUniformHandler_DEFINED
Stephen White4da34bf2019-07-30 10:37:47 -040010
Michael Ludwigc1ed11d2021-08-24 11:49:55 -040011#include "src/core/SkTBlockList.h"
Brian Salomona59925f2021-09-16 09:25:47 -040012#include "src/gpu/GrUniformDataManager.h"
Stephen White4da34bf2019-07-30 10:37:47 -040013#include "src/gpu/glsl/GrGLSLUniformHandler.h"
14
Greg Daniel690a4502020-04-21 09:19:51 -040015/*
Greg Danield59a91d2020-04-23 13:22:47 -040016 * This class can be used for basic SPIR-V uniform handling. It will make a single uniform buffer
Greg Daniel690a4502020-04-21 09:19:51 -040017 * for all the uniforms and will be placed in the first set and binding. Textures and samplers are
18 * placed in the second set and kept as separate objects. They are interleaved as sampler texture
19 * pairs with each object in the next binding slot.
20 */
Greg Danield59a91d2020-04-23 13:22:47 -040021class GrSPIRVUniformHandler : public GrGLSLUniformHandler {
Stephen White4da34bf2019-07-30 10:37:47 -040022public:
23 static const int kUniformsPerBlock = 8;
24
25 const GrShaderVar& getUniformVariable(UniformHandle u) const override;
26 const char* getUniformCStr(UniformHandle u) const override;
27
Greg Danield59a91d2020-04-23 13:22:47 -040028 struct SPIRVUniformInfo : public UniformInfo {
Ethan Nicholas16464c32020-04-06 13:53:05 -040029 int fUBOOffset;
Stephen White4da34bf2019-07-30 10:37:47 -040030 };
Michael Ludwigc1ed11d2021-08-24 11:49:55 -040031 typedef SkTBlockList<SPIRVUniformInfo> UniformInfoArray;
Stephen White4da34bf2019-07-30 10:37:47 -040032 enum {
Stephen Whitedd78efd2019-10-23 15:00:20 -040033 kUniformBinding = 0,
Greg Daniel690a4502020-04-21 09:19:51 -040034 kUniformDescriptorSet = 0,
35 kSamplerTextureDescriptorSet = 1,
Stephen White4da34bf2019-07-30 10:37:47 -040036 };
Brian Salomond8d85b92021-07-07 09:41:17 -040037 uint32_t getRTFlipOffset() const;
Stephen White4da34bf2019-07-30 10:37:47 -040038
Ethan Nicholas58430122020-04-14 09:54:02 -040039 int numUniforms() const override {
40 return fUniforms.count();
41 }
42
43 UniformInfo& uniform(int idx) override {
44 return fUniforms.item(idx);
45 }
Michael Ludwige88320b2020-06-24 09:04:56 -040046 const UniformInfo& uniform(int idx) const override {
47 return fUniforms.item(idx);
48 }
Ethan Nicholas58430122020-04-14 09:54:02 -040049
Brian Salomona59925f2021-09-16 09:25:47 -040050 /**
51 * Call after all legacy style uniforms have been added to assign offsets to new style uniforms
52 * and create the data structure needed to transfer new style uniforms to GrUniformDataManager.
53 * This must be called before appendUniformDecls() in order to ensure new style uniforms get
54 * declared. It must be called only once.
55 */
56 GrUniformDataManager::ProgramUniforms getNewProgramUniforms(const GrUniformAggregator&);
57
Stephen White4da34bf2019-07-30 10:37:47 -040058private:
Greg Danield59a91d2020-04-23 13:22:47 -040059 explicit GrSPIRVUniformHandler(GrGLSLProgramBuilder* program);
Stephen White4da34bf2019-07-30 10:37:47 -040060
Chris Dalton1b1b0d52020-03-03 12:00:59 -070061 SamplerHandle addSampler(const GrBackendFormat&, GrSamplerState, const GrSwizzle&,
Stephen White4da34bf2019-07-30 10:37:47 -040062 const char* name, const GrShaderCaps*) override;
63 const char* samplerVariable(SamplerHandle handle) const override;
64 GrSwizzle samplerSwizzle(SamplerHandle handle) const override;
Brian Salomona59925f2021-09-16 09:25:47 -040065 void appendUniformDecls(const GrUniformAggregator&,
66 GrShaderFlags visibility,
67 SkString*) const override;
Ethan Nicholas16464c32020-04-06 13:53:05 -040068 UniformHandle internalAddUniformArray(const GrFragmentProcessor* owner,
69 uint32_t visibility,
Stephen White4da34bf2019-07-30 10:37:47 -040070 GrSLType type,
71 const char* name,
72 bool mangleName,
73 int arrayCount,
74 const char** outName) override;
75
Ethan Nicholas16464c32020-04-06 13:53:05 -040076 UniformInfoArray fUniforms;
Brian Salomona59925f2021-09-16 09:25:47 -040077 UniformInfoArray fNewUniforms;
Ethan Nicholas16464c32020-04-06 13:53:05 -040078 UniformInfoArray fSamplers;
79 UniformInfoArray fTextures;
80 SkTArray<GrSwizzle> fSamplerSwizzles;
81 SkTArray<SkString> fSamplerReferences;
Stephen White170d9902019-08-15 16:48:24 -040082
Stephen Whitedd78efd2019-10-23 15:00:20 -040083 uint32_t fCurrentUBOOffset = 0;
Brian Salomond8d85b92021-07-07 09:41:17 -040084 uint32_t fRTFlipOffset = 0;
Stephen White4da34bf2019-07-30 10:37:47 -040085
Greg Daniel5fc5c812020-04-23 10:30:23 -040086 friend class GrD3DPipelineStateBuilder;
Stephen White4da34bf2019-07-30 10:37:47 -040087 friend class GrDawnProgramBuilder;
Greg Daniel5fc5c812020-04-23 10:30:23 -040088
John Stiles7571f9e2020-09-02 22:42:33 -040089 using INHERITED = GrGLSLUniformHandler;
Stephen White4da34bf2019-07-30 10:37:47 -040090};
91
92#endif