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 "GrAllocator.h" 12 #include "GrSamplerState.h" 13 #include "GrShaderVar.h" 14 #include "GrVkSampler.h" 15 #include "glsl/GrGLSLUniformHandler.h" 16 #include "vk/GrVkTypes.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 kGeometryBinding = 0, 34 kFragBinding = 1, 35 }; 36 37 struct UniformInfo { 38 GrShaderVar fVariable; 39 uint32_t fVisibility; 40 // fUBOffset is only valid if the GrSLType of the fVariable is not a sampler 41 uint32_t fUBOffset; 42 // The SamplerState, maxMipLevel, and ycbcrInfo are only valid if the GrSLType is a sampler 43 // and that sampler is used for sampling an external image with a ycbcr conversion. 44 const GrVkSampler* fImmutableSampler = nullptr; 45 }; 46 typedef GrTAllocator<UniformInfo> UniformInfoArray; 47 48 const GrShaderVar& getUniformVariable(UniformHandle u) const override { 49 return fUniforms[u.toIndex()].fVariable; 50 } 51 52 const char* getUniformCStr(UniformHandle u) const override { 53 return this->getUniformVariable(u).c_str(); 54 } 55 56 private: 57 explicit GrVkUniformHandler(GrGLSLProgramBuilder* program) 58 : INHERITED(program) 59 , fUniforms(kUniformsPerBlock) 60 , fSamplers(kUniformsPerBlock) 61 , fCurrentGeometryUBOOffset(0) 62 , fCurrentFragmentUBOOffset(0) { 63 } 64 65 UniformHandle internalAddUniformArray(uint32_t visibility, 66 GrSLType type, 67 const char* name, 68 bool mangleName, 69 int arrayCount, 70 const char** outName) override; 71 72 SamplerHandle addSampler(const GrTexture* texture, 73 const GrSamplerState&, 74 const char* name, 75 const GrShaderCaps*) override; 76 77 int numSamplers() const { return fSamplers.count(); } 78 const GrShaderVar& samplerVariable(SamplerHandle handle) const override { 79 return fSamplers[handle.toIndex()].fVariable; 80 } 81 GrSwizzle samplerSwizzle(SamplerHandle handle) const override { 82 return fSamplerSwizzles[handle.toIndex()]; 83 } 84 uint32_t samplerVisibility(SamplerHandle handle) const { 85 return fSamplers[handle.toIndex()].fVisibility; 86 } 87 88 const GrVkSampler* immutableSampler(UniformHandle u) const { 89 return fSamplers[u.toIndex()].fImmutableSampler; 90 } 91 92 void appendUniformDecls(GrShaderFlags, SkString*) const override; 93 94 bool hasGeometryUniforms() const { return fCurrentGeometryUBOOffset > 0; } 95 bool hasFragmentUniforms() const { return fCurrentFragmentUBOOffset > 0; } 96 97 98 const UniformInfo& getUniformInfo(UniformHandle u) const { 99 return fUniforms[u.toIndex()]; 100 } 101 102 103 UniformInfoArray fUniforms; 104 UniformInfoArray fSamplers; 105 SkTArray<GrSwizzle> fSamplerSwizzles; 106 107 uint32_t fCurrentGeometryUBOOffset; 108 uint32_t fCurrentFragmentUBOOffset; 109 110 friend class GrVkPipelineStateBuilder; 111 friend class GrVkDescriptorSetManager; 112 113 typedef GrGLSLUniformHandler INHERITED; 114 }; 115 116 #endif 117