GrVkPipelineState.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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 GrVkPipelineState_DEFINED
  8. #define GrVkPipelineState_DEFINED
  9. #include "include/gpu/vk/GrVkTypes.h"
  10. #include "src/gpu/glsl/GrGLSLProgramBuilder.h"
  11. #include "src/gpu/vk/GrVkDescriptorSetManager.h"
  12. #include "src/gpu/vk/GrVkPipelineStateDataManager.h"
  13. class GrPipeline;
  14. class GrStencilSettings;
  15. class GrVkBufferView;
  16. class GrVkCommandBuffer;
  17. class GrVkDescriptorPool;
  18. class GrVkDescriptorSet;
  19. class GrVkGpu;
  20. class GrVkImageView;
  21. class GrVkPipeline;
  22. class GrVkPipelineLayout;
  23. class GrVkSampler;
  24. class GrVkTexture;
  25. class GrVkUniformBuffer;
  26. /**
  27. * This class holds onto a GrVkPipeline object that we use for draws. Besides storing the acutal
  28. * GrVkPipeline object, this class is also responsible handling all uniforms, descriptors, samplers,
  29. * and other similar objects that are used along with the VkPipeline in the draw. This includes both
  30. * allocating and freeing these objects, as well as updating their values.
  31. */
  32. class GrVkPipelineState : public SkRefCnt {
  33. public:
  34. using UniformInfoArray = GrVkPipelineStateDataManager::UniformInfoArray;
  35. using UniformHandle = GrGLSLProgramDataManager::UniformHandle;
  36. GrVkPipelineState(
  37. GrVkGpu* gpu,
  38. GrVkPipeline* pipeline,
  39. VkPipelineLayout layout,
  40. const GrVkDescriptorSetManager::Handle& samplerDSHandle,
  41. const GrGLSLBuiltinUniformHandles& builtinUniformHandles,
  42. const UniformInfoArray& uniforms,
  43. uint32_t geometryUniformSize,
  44. uint32_t fragmentUniformSize,
  45. const UniformInfoArray& samplers,
  46. std::unique_ptr<GrGLSLPrimitiveProcessor> geometryProcessor,
  47. std::unique_ptr<GrGLSLXferProcessor> xferProcessor,
  48. std::unique_ptr<std::unique_ptr<GrGLSLFragmentProcessor>[]> fragmentProcessors,
  49. int fFragmentProcessorCnt);
  50. ~GrVkPipelineState();
  51. void setAndBindUniforms(GrVkGpu*, const GrRenderTarget*, GrSurfaceOrigin,
  52. const GrPrimitiveProcessor&, const GrPipeline&, GrVkCommandBuffer*);
  53. /**
  54. * This must be called after setAndBindUniforms() since that function invalidates texture
  55. * bindings.
  56. */
  57. void setAndBindTextures(GrVkGpu*, const GrPrimitiveProcessor&, const GrPipeline&,
  58. const GrTextureProxy* const primitiveProcessorTextures[],
  59. GrVkCommandBuffer*);
  60. void bindPipeline(const GrVkGpu* gpu, GrVkCommandBuffer* commandBuffer);
  61. void addUniformResources(GrVkCommandBuffer&, GrVkSampler*[], GrVkTexture*[], int numTextures);
  62. void freeGPUResources(GrVkGpu* gpu);
  63. void abandonGPUResources();
  64. private:
  65. void writeUniformBuffers(const GrVkGpu* gpu);
  66. /**
  67. * We use the RT's size and origin to adjust from Skia device space to vulkan normalized device
  68. * space and to make device space positions have the correct origin for processors that require
  69. * them.
  70. */
  71. struct RenderTargetState {
  72. SkISize fRenderTargetSize;
  73. GrSurfaceOrigin fRenderTargetOrigin;
  74. RenderTargetState() { this->invalidate(); }
  75. void invalidate() {
  76. fRenderTargetSize.fWidth = -1;
  77. fRenderTargetSize.fHeight = -1;
  78. fRenderTargetOrigin = (GrSurfaceOrigin)-1;
  79. }
  80. /**
  81. * Gets a float4 that adjusts the position from Skia device coords to Vulkans normalized device
  82. * coords. Assuming the transformed position, pos, is a homogeneous float3, the vec, v, is
  83. * applied as such:
  84. * pos.x = dot(v.xy, pos.xz)
  85. * pos.y = dot(v.zw, pos.yz)
  86. */
  87. void getRTAdjustmentVec(float* destVec) {
  88. destVec[0] = 2.f / fRenderTargetSize.fWidth;
  89. destVec[1] = -1.f;
  90. if (kBottomLeft_GrSurfaceOrigin == fRenderTargetOrigin) {
  91. destVec[2] = -2.f / fRenderTargetSize.fHeight;
  92. destVec[3] = 1.f;
  93. } else {
  94. destVec[2] = 2.f / fRenderTargetSize.fHeight;
  95. destVec[3] = -1.f;
  96. }
  97. }
  98. };
  99. // Helper for setData() that sets the view matrix and loads the render target height uniform
  100. void setRenderTargetState(const GrRenderTarget*, GrSurfaceOrigin);
  101. // GrVkResources
  102. GrVkPipeline* fPipeline;
  103. // Used for binding DescriptorSets to the command buffer but does not need to survive during
  104. // command buffer execution. Thus this is not need to be a GrVkResource.
  105. GrVkPipelineLayout* fPipelineLayout;
  106. // The DescriptorSets need to survive until the gpu has finished all draws that use them.
  107. // However, they will only be freed by the descriptor pool. Thus by simply keeping the
  108. // descriptor pool alive through the draw, the descritor sets will also stay alive. Thus we do
  109. // not need a GrVkResource versions of VkDescriptorSet. We hold on to these in the
  110. // GrVkPipelineState since we update the descriptor sets and bind them at separate times;
  111. VkDescriptorSet fDescriptorSets[3];
  112. const GrVkDescriptorSet* fUniformDescriptorSet;
  113. const GrVkDescriptorSet* fSamplerDescriptorSet;
  114. const GrVkDescriptorSetManager::Handle fSamplerDSHandle;
  115. SkSTArray<4, const GrVkSampler*> fImmutableSamplers;
  116. std::unique_ptr<GrVkUniformBuffer> fGeometryUniformBuffer;
  117. std::unique_ptr<GrVkUniformBuffer> fFragmentUniformBuffer;
  118. // Tracks the current render target uniforms stored in the vertex buffer.
  119. RenderTargetState fRenderTargetState;
  120. GrGLSLBuiltinUniformHandles fBuiltinUniformHandles;
  121. // Processors in the GrVkPipelineState
  122. std::unique_ptr<GrGLSLPrimitiveProcessor> fGeometryProcessor;
  123. std::unique_ptr<GrGLSLXferProcessor> fXferProcessor;
  124. std::unique_ptr<std::unique_ptr<GrGLSLFragmentProcessor>[]> fFragmentProcessors;
  125. int fFragmentProcessorCnt;
  126. GrVkPipelineStateDataManager fDataManager;
  127. int fNumSamplers;
  128. };
  129. #endif