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