1 /* 2 * Copyright 2018 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 GrMtlUniformHandler_DEFINED 9 #define GrMtlUniformHandler_DEFINED 10 11 #include "src/gpu/GrAllocator.h" 12 #include "src/gpu/GrShaderVar.h" 13 #include "src/gpu/glsl/GrGLSLUniformHandler.h" 14 15 // TODO: this class is basically copy and pasted from GrVkUniformHandler so that we can have 16 // some shaders working. The SkSL Metal code generator was written to work with GLSL generated for 17 // the Ganesh Vulkan backend, so it should all work. There might be better ways to do things in 18 // Metal and/or some Vulkan GLSLisms left in. 19 class GrMtlUniformHandler : public GrGLSLUniformHandler { 20 public: 21 static const int kUniformsPerBlock = 8; 22 23 enum { 24 kUniformBinding = 0, 25 kLastUniformBinding = kUniformBinding, 26 }; 27 28 // fUBOffset is only valid if the GrSLType of the fVariable is not a sampler 29 struct UniformInfo { 30 GrShaderVar fVariable; 31 uint32_t fVisibility; 32 uint32_t fUBOffset; 33 }; 34 typedef GrTAllocator<UniformInfo> UniformInfoArray; 35 getUniformVariable(UniformHandle u)36 const GrShaderVar& getUniformVariable(UniformHandle u) const override { 37 return fUniforms[u.toIndex()].fVariable; 38 } 39 getUniformCStr(UniformHandle u)40 const char* getUniformCStr(UniformHandle u) const override { 41 return this->getUniformVariable(u).c_str(); 42 } 43 44 private: GrMtlUniformHandler(GrGLSLProgramBuilder * program)45 explicit GrMtlUniformHandler(GrGLSLProgramBuilder* program) 46 : INHERITED(program) 47 , fUniforms(kUniformsPerBlock) 48 , fSamplers(kUniformsPerBlock) 49 , fCurrentUBOOffset(0) 50 , fCurrentUBOMaxAlignment(0x0) { 51 } 52 53 UniformHandle internalAddUniformArray(uint32_t visibility, 54 GrSLType type, 55 const char* name, 56 bool mangleName, 57 int arrayCount, 58 const char** outName) override; 59 60 SamplerHandle addSampler(const GrTexture*, 61 const GrSamplerState&, 62 const GrSwizzle&, 63 const char* name, 64 const GrShaderCaps*) override; 65 numSamplers()66 int numSamplers() const { return fSamplers.count(); } samplerVariable(SamplerHandle handle)67 const char* samplerVariable(SamplerHandle handle) const override { 68 return fSamplers[handle.toIndex()].fVariable.c_str(); 69 } samplerSwizzle(SamplerHandle handle)70 GrSwizzle samplerSwizzle(SamplerHandle handle) const override { 71 return fSamplerSwizzles[handle.toIndex()]; 72 } samplerVisibility(SamplerHandle handle)73 uint32_t samplerVisibility(SamplerHandle handle) const { 74 return fSamplers[handle.toIndex()].fVisibility; 75 } 76 77 void appendUniformDecls(GrShaderFlags, SkString*) const override; 78 getUniformInfo(UniformHandle u)79 const UniformInfo& getUniformInfo(UniformHandle u) const { 80 return fUniforms[u.toIndex()]; 81 } 82 83 UniformInfoArray fUniforms; 84 UniformInfoArray fSamplers; 85 SkTArray<GrSwizzle> fSamplerSwizzles; 86 87 uint32_t fCurrentUBOOffset; 88 uint32_t fCurrentUBOMaxAlignment; 89 90 friend class GrMtlPipelineStateBuilder; 91 92 typedef GrGLSLUniformHandler INHERITED; 93 }; 94 95 #endif 96