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 37 const GrShaderVar& getUniformVariable(UniformHandle u) const override { 38 return fUniforms[u.toIndex()].fVariable; 39 } 40 41 const char* getUniformCStr(UniformHandle u) const override { 42 return this->getUniformVariable(u).c_str(); 43 } 44 45 private: 46 explicit GrMtlUniformHandler(GrGLSLProgramBuilder* program) 47 : INHERITED(program) 48 , fUniforms(kUniformsPerBlock) 49 , fSamplers(kUniformsPerBlock) 50 , fCurrentGeometryUBOOffset(0) 51 , fCurrentGeometryUBOMaxAlignment(0x0) 52 , fCurrentFragmentUBOOffset(0) 53 , fCurrentFragmentUBOMaxAlignment(0x0) { 54 } 55 56 UniformHandle internalAddUniformArray(uint32_t visibility, 57 GrSLType type, 58 const char* name, 59 bool mangleName, 60 int arrayCount, 61 const char** outName) override; 62 63 SamplerHandle addSampler(const GrTexture*, 64 const GrSamplerState&, 65 const char* name, 66 const GrShaderCaps*) override; 67 68 int numSamplers() const { return fSamplers.count(); } 69 const GrShaderVar& samplerVariable(SamplerHandle handle) const override { 70 return fSamplers[handle.toIndex()].fVariable; 71 } 72 GrSwizzle samplerSwizzle(SamplerHandle handle) const override { 73 return fSamplerSwizzles[handle.toIndex()]; 74 } 75 uint32_t samplerVisibility(SamplerHandle handle) const { 76 return fSamplers[handle.toIndex()].fVisibility; 77 } 78 79 void appendUniformDecls(GrShaderFlags, SkString*) const override; 80 81 bool hasGeometryUniforms() const { return fCurrentGeometryUBOOffset > 0; } 82 bool hasFragmentUniforms() const { return fCurrentFragmentUBOOffset > 0; } 83 84 const UniformInfo& getUniformInfo(UniformHandle u) const { 85 return fUniforms[u.toIndex()]; 86 } 87 88 UniformInfoArray fUniforms; 89 UniformInfoArray fSamplers; 90 SkTArray<GrSwizzle> fSamplerSwizzles; 91 92 uint32_t fCurrentGeometryUBOOffset; 93 uint32_t fCurrentGeometryUBOMaxAlignment; 94 uint32_t fCurrentFragmentUBOOffset; 95 uint32_t fCurrentFragmentUBOMaxAlignment; 96 97 friend class GrMtlPipelineStateBuilder; 98 99 typedef GrGLSLUniformHandler INHERITED; 100 }; 101 102 #endif 103