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 updateUniformVisibility(UniformHandle u,uint32_t visibility)60 void updateUniformVisibility(UniformHandle u, uint32_t visibility) override { 61 fUniforms[u.toIndex()].fVisibility |= visibility; 62 } 63 64 SamplerHandle addSampler(const GrSurfaceProxy*, 65 GrSamplerState, 66 const GrSwizzle&, 67 const char* name, 68 const GrShaderCaps*) override; 69 numSamplers()70 int numSamplers() const { return fSamplers.count(); } samplerVariable(SamplerHandle handle)71 const char* samplerVariable(SamplerHandle handle) const override { 72 return fSamplers[handle.toIndex()].fVariable.c_str(); 73 } samplerSwizzle(SamplerHandle handle)74 GrSwizzle samplerSwizzle(SamplerHandle handle) const override { 75 return fSamplerSwizzles[handle.toIndex()]; 76 } samplerVisibility(SamplerHandle handle)77 uint32_t samplerVisibility(SamplerHandle handle) const { 78 return fSamplers[handle.toIndex()].fVisibility; 79 } 80 81 void appendUniformDecls(GrShaderFlags, SkString*) const override; 82 getUniformInfo(UniformHandle u)83 const UniformInfo& getUniformInfo(UniformHandle u) const { 84 return fUniforms[u.toIndex()]; 85 } 86 87 UniformInfoArray fUniforms; 88 UniformInfoArray fSamplers; 89 SkTArray<GrSwizzle> fSamplerSwizzles; 90 91 uint32_t fCurrentUBOOffset; 92 uint32_t fCurrentUBOMaxAlignment; 93 94 friend class GrMtlPipelineStateBuilder; 95 96 typedef GrGLSLUniformHandler INHERITED; 97 }; 98 99 #endif 100