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 "GrAllocator.h" 12 #include "GrShaderVar.h" 13 #include "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 kGeometryBinding = 0, 25 kFragBinding = 1, 26 kLastUniformBinding = kFragBinding, 27 }; 28 29 // fUBOffset is only valid if the GrSLType of the fVariable is not a sampler 30 struct UniformInfo { 31 GrShaderVar fVariable; 32 uint32_t fVisibility; 33 uint32_t fUBOffset; 34 }; 35 typedef GrTAllocator<UniformInfo> UniformInfoArray; 36 getUniformVariable(UniformHandle u)37 const GrShaderVar& getUniformVariable(UniformHandle u) const override { 38 return fUniforms[u.toIndex()].fVariable; 39 } 40 getUniformCStr(UniformHandle u)41 const char* getUniformCStr(UniformHandle u) const override { 42 return this->getUniformVariable(u).c_str(); 43 } 44 45 private: GrMtlUniformHandler(GrGLSLProgramBuilder * program)46 explicit GrMtlUniformHandler(GrGLSLProgramBuilder* program) 47 : INHERITED(program) 48 , fUniforms(kUniformsPerBlock) 49 , fSamplers(kUniformsPerBlock) 50 , fCurrentGeometryUBOOffset(0) 51 , fCurrentFragmentUBOOffset(0) { 52 } 53 54 UniformHandle internalAddUniformArray(uint32_t visibility, 55 GrSLType type, 56 GrSLPrecision precision, 57 const char* name, 58 bool mangleName, 59 int arrayCount, 60 const char** outName) override; 61 62 SamplerHandle addSampler(const GrTexture*, 63 const GrSamplerState&, 64 const char* name, 65 const GrShaderCaps*) override; 66 numSamplers()67 int numSamplers() const { return fSamplers.count(); } samplerVariable(SamplerHandle handle)68 const GrShaderVar& samplerVariable(SamplerHandle handle) const override { 69 return fSamplers[handle.toIndex()].fVariable; 70 } samplerSwizzle(SamplerHandle handle)71 GrSwizzle samplerSwizzle(SamplerHandle handle) const override { 72 return fSamplerSwizzles[handle.toIndex()]; 73 } samplerVisibility(SamplerHandle handle)74 uint32_t samplerVisibility(SamplerHandle handle) const { 75 return fSamplers[handle.toIndex()].fVisibility; 76 } 77 78 void appendUniformDecls(GrShaderFlags, SkString*) const override; 79 hasGeometryUniforms()80 bool hasGeometryUniforms() const { return fCurrentGeometryUBOOffset > 0; } hasFragmentUniforms()81 bool hasFragmentUniforms() const { return fCurrentFragmentUBOOffset > 0; } 82 83 getUniformInfo(UniformHandle u)84 const UniformInfo& getUniformInfo(UniformHandle u) const { 85 return fUniforms[u.toIndex()]; 86 } 87 88 89 UniformInfoArray fUniforms; 90 UniformInfoArray fSamplers; 91 SkTArray<GrSwizzle> fSamplerSwizzles; 92 93 uint32_t fCurrentGeometryUBOOffset; 94 uint32_t fCurrentFragmentUBOOffset; 95 96 friend class GrMtlPipelineStateBuilder; 97 98 typedef GrGLSLUniformHandler INHERITED; 99 }; 100 101 #endif 102