1 /* 2 * Copyright 2016 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 GrVkUniformHandler_DEFINED 9 #define GrVkUniformHandler_DEFINED 10 11 #include "include/gpu/vk/GrVkTypes.h" 12 #include "src/gpu/GrAllocator.h" 13 #include "src/gpu/GrSamplerState.h" 14 #include "src/gpu/GrShaderVar.h" 15 #include "src/gpu/glsl/GrGLSLUniformHandler.h" 16 #include "src/gpu/vk/GrVkSampler.h" 17 18 class GrVkUniformHandler : public GrGLSLUniformHandler { 19 public: 20 static const int kUniformsPerBlock = 8; 21 22 enum { 23 /** 24 * Binding a descriptor set invalidates all higher index descriptor sets. We must bind 25 * in the order of this enumeration. Samplers are after Uniforms because GrOps can specify 26 * GP textures as dynamic state, meaning they get rebound for each GrMesh in a draw while 27 * uniforms are bound once before all the draws. 28 */ 29 kUniformBufferDescSet = 0, 30 kSamplerDescSet = 1, 31 }; 32 enum { 33 kUniformBinding = 0 34 }; 35 36 struct UniformInfo { 37 GrShaderVar fVariable; 38 uint32_t fVisibility; 39 // fUBOffset is only valid if the GrSLType of the fVariable is not a sampler 40 uint32_t fUBOffset; 41 // fImmutableSampler is used for sampling an image with a ycbcr conversion. 42 const GrVkSampler* fImmutableSampler = nullptr; 43 }; 44 typedef GrTAllocator<UniformInfo> UniformInfoArray; 45 46 ~GrVkUniformHandler() override; 47 getUniformVariable(UniformHandle u)48 const GrShaderVar& getUniformVariable(UniformHandle u) const override { 49 return fUniforms[u.toIndex()].fVariable; 50 } 51 getUniformCStr(UniformHandle u)52 const char* getUniformCStr(UniformHandle u) const override { 53 return this->getUniformVariable(u).c_str(); 54 } 55 56 /** 57 * Returns the offset that the RTHeight synthetic uniform should use if it needs to be created. 58 */ 59 uint32_t getRTHeightOffset() const; 60 61 private: GrVkUniformHandler(GrGLSLProgramBuilder * program)62 explicit GrVkUniformHandler(GrGLSLProgramBuilder* program) 63 : INHERITED(program) 64 , fUniforms(kUniformsPerBlock) 65 , fSamplers(kUniformsPerBlock) 66 , fCurrentUBOOffset(0) { 67 } 68 69 UniformHandle internalAddUniformArray(uint32_t visibility, 70 GrSLType type, 71 const char* name, 72 bool mangleName, 73 int arrayCount, 74 const char** outName) override; 75 76 SamplerHandle addSampler(const GrTexture* texture, 77 const GrSamplerState&, 78 const GrSwizzle&, 79 const char* name, 80 const GrShaderCaps*) override; 81 numSamplers()82 int numSamplers() const { return fSamplers.count(); } samplerVariable(SamplerHandle handle)83 const char* samplerVariable(SamplerHandle handle) const override { 84 return fSamplers[handle.toIndex()].fVariable.c_str(); 85 } samplerSwizzle(SamplerHandle handle)86 GrSwizzle samplerSwizzle(SamplerHandle handle) const override { 87 return fSamplerSwizzles[handle.toIndex()]; 88 } samplerVisibility(SamplerHandle handle)89 uint32_t samplerVisibility(SamplerHandle handle) const { 90 return fSamplers[handle.toIndex()].fVisibility; 91 } 92 immutableSampler(UniformHandle u)93 const GrVkSampler* immutableSampler(UniformHandle u) const { 94 return fSamplers[u.toIndex()].fImmutableSampler; 95 } 96 97 void appendUniformDecls(GrShaderFlags, SkString*) const override; 98 getUniformInfo(UniformHandle u)99 const UniformInfo& getUniformInfo(UniformHandle u) const { 100 return fUniforms[u.toIndex()]; 101 } 102 103 104 UniformInfoArray fUniforms; 105 UniformInfoArray fSamplers; 106 SkTArray<GrSwizzle> fSamplerSwizzles; 107 108 uint32_t fCurrentUBOOffset; 109 110 friend class GrVkPipelineStateBuilder; 111 friend class GrVkDescriptorSetManager; 112 113 typedef GrGLSLUniformHandler INHERITED; 114 }; 115 116 #endif 117