GrVkUniformHandler.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /*
  2. * Copyright 2016 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 GrVkUniformHandler_DEFINED
  8. #define GrVkUniformHandler_DEFINED
  9. #include "include/gpu/GrSamplerState.h"
  10. #include "include/gpu/vk/GrVkTypes.h"
  11. #include "src/gpu/GrAllocator.h"
  12. #include "src/gpu/GrShaderVar.h"
  13. #include "src/gpu/glsl/GrGLSLUniformHandler.h"
  14. #include "src/gpu/vk/GrVkSampler.h"
  15. class GrVkUniformHandler : public GrGLSLUniformHandler {
  16. public:
  17. static const int kUniformsPerBlock = 8;
  18. enum {
  19. /**
  20. * Binding a descriptor set invalidates all higher index descriptor sets. We must bind
  21. * in the order of this enumeration. Samplers are after Uniforms because GrOps can specify
  22. * GP textures as dynamic state, meaning they get rebound for each GrMesh in a draw while
  23. * uniforms are bound once before all the draws.
  24. */
  25. kUniformBufferDescSet = 0,
  26. kSamplerDescSet = 1,
  27. };
  28. enum {
  29. kGeometryBinding = 0,
  30. kFragBinding = 1,
  31. };
  32. struct UniformInfo {
  33. GrShaderVar fVariable;
  34. uint32_t fVisibility;
  35. // fUBOffset is only valid if the GrSLType of the fVariable is not a sampler
  36. uint32_t fUBOffset;
  37. // The SamplerState, maxMipLevel, and ycbcrInfo are only valid if the GrSLType is a sampler
  38. // and that sampler is used for sampling an external image with a ycbcr conversion.
  39. const GrVkSampler* fImmutableSampler = nullptr;
  40. };
  41. typedef GrTAllocator<UniformInfo> UniformInfoArray;
  42. const GrShaderVar& getUniformVariable(UniformHandle u) const override {
  43. return fUniforms[u.toIndex()].fVariable;
  44. }
  45. const char* getUniformCStr(UniformHandle u) const override {
  46. return this->getUniformVariable(u).c_str();
  47. }
  48. private:
  49. explicit GrVkUniformHandler(GrGLSLProgramBuilder* program)
  50. : INHERITED(program)
  51. , fUniforms(kUniformsPerBlock)
  52. , fSamplers(kUniformsPerBlock)
  53. , fCurrentGeometryUBOOffset(0)
  54. , fCurrentFragmentUBOOffset(0) {
  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. SamplerHandle addSampler(const GrTexture* texture,
  63. const GrSamplerState&,
  64. const GrSwizzle&,
  65. const char* name,
  66. const GrShaderCaps*) override;
  67. int numSamplers() const { return fSamplers.count(); }
  68. const char* samplerVariable(SamplerHandle handle) const override {
  69. return fSamplers[handle.toIndex()].fVariable.c_str();
  70. }
  71. GrSwizzle samplerSwizzle(SamplerHandle handle) const override {
  72. return fSamplerSwizzles[handle.toIndex()];
  73. }
  74. uint32_t samplerVisibility(SamplerHandle handle) const {
  75. return fSamplers[handle.toIndex()].fVisibility;
  76. }
  77. const GrVkSampler* immutableSampler(UniformHandle u) const {
  78. return fSamplers[u.toIndex()].fImmutableSampler;
  79. }
  80. void appendUniformDecls(GrShaderFlags, SkString*) const override;
  81. bool hasGeometryUniforms() const { return fCurrentGeometryUBOOffset > 0; }
  82. bool hasFragmentUniforms() const { return fCurrentFragmentUBOOffset > 0; }
  83. const UniformInfo& getUniformInfo(UniformHandle u) const {
  84. return fUniforms[u.toIndex()];
  85. }
  86. UniformInfoArray fUniforms;
  87. UniformInfoArray fSamplers;
  88. SkTArray<GrSwizzle> fSamplerSwizzles;
  89. uint32_t fCurrentGeometryUBOOffset;
  90. uint32_t fCurrentFragmentUBOOffset;
  91. friend class GrVkPipelineStateBuilder;
  92. friend class GrVkDescriptorSetManager;
  93. typedef GrGLSLUniformHandler INHERITED;
  94. };
  95. #endif