GrMtlUniformHandler.h 3.5 KB

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