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 | |
egdaniel | 707bbd6 | 2016-07-26 07:19:47 -0700 | [diff] [blame] | 12 | #include "GrVkDescriptorSetManager.h" |
egdaniel | 22281c1 | 2016-03-23 13:49:40 -0700 | [diff] [blame] | 13 | #include "GrVkPipelineStateDataManager.h" |
| 14 | #include "glsl/GrGLSLProgramBuilder.h" |
jvanverth | e50f3e7 | 2016-03-28 07:03:06 -0700 | [diff] [blame] | 15 | #include "vk/GrVkDefines.h" |
egdaniel | 22281c1 | 2016-03-23 13:49:40 -0700 | [diff] [blame] | 16 | |
| 17 | class GrPipeline; |
Brian Salomon | 1471df9 | 2018-06-08 10:49:00 -0400 | [diff] [blame] | 18 | class GrStencilSettings; |
Greg Daniel | 31ec144 | 2017-05-08 10:30:59 -0400 | [diff] [blame] | 19 | class GrVkBufferView; |
egdaniel | 22281c1 | 2016-03-23 13:49:40 -0700 | [diff] [blame] | 20 | class GrVkCommandBuffer; |
| 21 | class GrVkDescriptorPool; |
egdaniel | a95220d | 2016-07-21 11:50:37 -0700 | [diff] [blame] | 22 | class GrVkDescriptorSet; |
egdaniel | 22281c1 | 2016-03-23 13:49:40 -0700 | [diff] [blame] | 23 | class GrVkGpu; |
| 24 | class GrVkImageView; |
| 25 | class GrVkPipeline; |
Greg Daniel | 7d918fd | 2018-06-19 15:22:01 -0400 | [diff] [blame^] | 26 | class GrVkPipelineLayout; |
egdaniel | 22281c1 | 2016-03-23 13:49:40 -0700 | [diff] [blame] | 27 | class GrVkSampler; |
| 28 | class GrVkUniformBuffer; |
| 29 | |
| 30 | /** |
| 31 | * This class holds onto a GrVkPipeline object that we use for draws. Besides storing the acutal |
| 32 | * GrVkPipeline object, this class is also responsible handling all uniforms, descriptors, samplers, |
| 33 | * and other similar objects that are used along with the VkPipeline in the draw. This includes both |
| 34 | * allocating and freeing these objects, as well as updating their values. |
| 35 | */ |
| 36 | class GrVkPipelineState : public SkRefCnt { |
| 37 | public: |
Brian Salomon | 1471df9 | 2018-06-08 10:49:00 -0400 | [diff] [blame] | 38 | using UniformInfoArray = GrVkPipelineStateDataManager::UniformInfoArray; |
| 39 | using UniformHandle = GrGLSLProgramDataManager::UniformHandle; |
| 40 | |
| 41 | GrVkPipelineState( |
| 42 | GrVkGpu* gpu, |
| 43 | GrVkPipeline* pipeline, |
| 44 | VkPipelineLayout layout, |
| 45 | const GrVkDescriptorSetManager::Handle& samplerDSHandle, |
| 46 | const GrVkDescriptorSetManager::Handle& texelBufferDSHandle, |
| 47 | const GrGLSLBuiltinUniformHandles& builtinUniformHandles, |
| 48 | const UniformInfoArray& uniforms, |
| 49 | uint32_t geometryUniformSize, |
| 50 | uint32_t fragmentUniformSize, |
| 51 | uint32_t numSamplers, |
| 52 | uint32_t numTexelBuffers, |
| 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 | |
egdaniel | 22281c1 | 2016-03-23 13:49:40 -0700 | [diff] [blame] | 60 | void setData(GrVkGpu*, const GrPrimitiveProcessor&, const GrPipeline&); |
| 61 | |
| 62 | void bind(const GrVkGpu* gpu, GrVkCommandBuffer* commandBuffer); |
| 63 | |
| 64 | void addUniformResources(GrVkCommandBuffer&); |
| 65 | |
| 66 | void freeGPUResources(const GrVkGpu* gpu); |
| 67 | |
| 68 | // This releases resources that only a given instance of a GrVkPipelineState needs to hold onto |
| 69 | // and don't need to survive across new uses of the GrVkPipelineState. |
| 70 | void freeTempResources(const GrVkGpu* gpu); |
| 71 | |
| 72 | void abandonGPUResources(); |
| 73 | |
egdaniel | 22281c1 | 2016-03-23 13:49:40 -0700 | [diff] [blame] | 74 | private: |
egdaniel | 22281c1 | 2016-03-23 13:49:40 -0700 | [diff] [blame] | 75 | void writeUniformBuffers(const GrVkGpu* gpu); |
| 76 | |
Brian Salomon | ab015ef | 2017-04-04 10:15:51 -0400 | [diff] [blame] | 77 | void writeSamplers( |
| 78 | GrVkGpu* gpu, |
Brian Osman | 2b23c4b | 2018-06-01 12:25:08 -0400 | [diff] [blame] | 79 | const SkTArray<const GrResourceIOProcessor::TextureSampler*>& textureBindings); |
egdaniel | 22281c1 | 2016-03-23 13:49:40 -0700 | [diff] [blame] | 80 | |
Greg Daniel | 31ec144 | 2017-05-08 10:30:59 -0400 | [diff] [blame] | 81 | void writeTexelBuffers( |
| 82 | GrVkGpu* gpu, |
| 83 | const SkTArray<const GrResourceIOProcessor::BufferAccess*>& bufferAccesses); |
| 84 | |
egdaniel | 22281c1 | 2016-03-23 13:49:40 -0700 | [diff] [blame] | 85 | /** |
| 86 | * We use the RT's size and origin to adjust from Skia device space to vulkan normalized device |
| 87 | * space and to make device space positions have the correct origin for processors that require |
| 88 | * them. |
| 89 | */ |
| 90 | struct RenderTargetState { |
| 91 | SkISize fRenderTargetSize; |
| 92 | GrSurfaceOrigin fRenderTargetOrigin; |
| 93 | |
| 94 | RenderTargetState() { this->invalidate(); } |
| 95 | void invalidate() { |
| 96 | fRenderTargetSize.fWidth = -1; |
| 97 | fRenderTargetSize.fHeight = -1; |
| 98 | fRenderTargetOrigin = (GrSurfaceOrigin)-1; |
| 99 | } |
| 100 | |
| 101 | /** |
Ethan Nicholas | 5af9ea3 | 2017-07-28 15:19:46 -0400 | [diff] [blame] | 102 | * Gets a float4 that adjusts the position from Skia device coords to Vulkans normalized device |
| 103 | * 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] | 104 | * applied as such: |
| 105 | * pos.x = dot(v.xy, pos.xz) |
| 106 | * pos.y = dot(v.zw, pos.yz) |
| 107 | */ |
| 108 | void getRTAdjustmentVec(float* destVec) { |
| 109 | destVec[0] = 2.f / fRenderTargetSize.fWidth; |
| 110 | destVec[1] = -1.f; |
| 111 | if (kBottomLeft_GrSurfaceOrigin == fRenderTargetOrigin) { |
| 112 | destVec[2] = -2.f / fRenderTargetSize.fHeight; |
| 113 | destVec[3] = 1.f; |
| 114 | } else { |
| 115 | destVec[2] = 2.f / fRenderTargetSize.fHeight; |
| 116 | destVec[3] = -1.f; |
| 117 | } |
| 118 | } |
| 119 | }; |
| 120 | |
| 121 | // 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] | 122 | void setRenderTargetState(const GrRenderTargetProxy*); |
egdaniel | 22281c1 | 2016-03-23 13:49:40 -0700 | [diff] [blame] | 123 | |
| 124 | // GrVkResources |
| 125 | GrVkPipeline* fPipeline; |
| 126 | |
| 127 | // Used for binding DescriptorSets to the command buffer but does not need to survive during |
| 128 | // command buffer execution. Thus this is not need to be a GrVkResource. |
Greg Daniel | 7d918fd | 2018-06-19 15:22:01 -0400 | [diff] [blame^] | 129 | GrVkPipelineLayout* fPipelineLayout; |
egdaniel | 22281c1 | 2016-03-23 13:49:40 -0700 | [diff] [blame] | 130 | |
| 131 | // The DescriptorSets need to survive until the gpu has finished all draws that use them. |
| 132 | // However, they will only be freed by the descriptor pool. Thus by simply keeping the |
| 133 | // descriptor pool alive through the draw, the descritor sets will also stay alive. Thus we do |
| 134 | // not need a GrVkResource versions of VkDescriptorSet. We hold on to these in the |
| 135 | // 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] | 136 | VkDescriptorSet fDescriptorSets[3]; |
egdaniel | 22281c1 | 2016-03-23 13:49:40 -0700 | [diff] [blame] | 137 | |
egdaniel | a95220d | 2016-07-21 11:50:37 -0700 | [diff] [blame] | 138 | const GrVkDescriptorSet* fUniformDescriptorSet; |
egdaniel | 707bbd6 | 2016-07-26 07:19:47 -0700 | [diff] [blame] | 139 | const GrVkDescriptorSet* fSamplerDescriptorSet; |
Greg Daniel | 31ec144 | 2017-05-08 10:30:59 -0400 | [diff] [blame] | 140 | const GrVkDescriptorSet* fTexelBufferDescriptorSet; |
egdaniel | 707bbd6 | 2016-07-26 07:19:47 -0700 | [diff] [blame] | 141 | |
| 142 | const GrVkDescriptorSetManager::Handle fSamplerDSHandle; |
Greg Daniel | 31ec144 | 2017-05-08 10:30:59 -0400 | [diff] [blame] | 143 | const GrVkDescriptorSetManager::Handle fTexelBufferDSHandle; |
egdaniel | 22281c1 | 2016-03-23 13:49:40 -0700 | [diff] [blame] | 144 | |
Greg Daniel | 18f9602 | 2017-05-04 15:09:03 -0400 | [diff] [blame] | 145 | std::unique_ptr<GrVkUniformBuffer> fGeometryUniformBuffer; |
Ben Wagner | 145dbcd | 2016-11-03 14:40:50 -0400 | [diff] [blame] | 146 | std::unique_ptr<GrVkUniformBuffer> fFragmentUniformBuffer; |
egdaniel | 22281c1 | 2016-03-23 13:49:40 -0700 | [diff] [blame] | 147 | |
| 148 | // GrVkResources used for sampling textures |
| 149 | SkTDArray<GrVkSampler*> fSamplers; |
| 150 | SkTDArray<const GrVkImageView*> fTextureViews; |
egdaniel | b2df0c2 | 2016-05-13 11:30:37 -0700 | [diff] [blame] | 151 | SkTDArray<const GrVkResource*> fTextures; |
egdaniel | 22281c1 | 2016-03-23 13:49:40 -0700 | [diff] [blame] | 152 | |
Greg Daniel | 31ec144 | 2017-05-08 10:30:59 -0400 | [diff] [blame] | 153 | // GrVkResource used for TexelBuffers |
| 154 | SkTDArray<const GrVkBufferView*> fBufferViews; |
| 155 | SkTDArray<const GrVkResource*> fTexelBuffers; |
| 156 | |
egdaniel | 22281c1 | 2016-03-23 13:49:40 -0700 | [diff] [blame] | 157 | // Tracks the current render target uniforms stored in the vertex buffer. |
| 158 | RenderTargetState fRenderTargetState; |
Brian Salomon | 1471df9 | 2018-06-08 10:49:00 -0400 | [diff] [blame] | 159 | GrGLSLBuiltinUniformHandles fBuiltinUniformHandles; |
egdaniel | 22281c1 | 2016-03-23 13:49:40 -0700 | [diff] [blame] | 160 | |
| 161 | // Processors in the GrVkPipelineState |
Ben Wagner | 145dbcd | 2016-11-03 14:40:50 -0400 | [diff] [blame] | 162 | std::unique_ptr<GrGLSLPrimitiveProcessor> fGeometryProcessor; |
| 163 | std::unique_ptr<GrGLSLXferProcessor> fXferProcessor; |
Brian Salomon | 4d3f517 | 2018-06-07 14:42:52 -0400 | [diff] [blame] | 164 | std::unique_ptr<std::unique_ptr<GrGLSLFragmentProcessor>[]> fFragmentProcessors; |
| 165 | int fFragmentProcessorCnt; |
egdaniel | 22281c1 | 2016-03-23 13:49:40 -0700 | [diff] [blame] | 166 | |
egdaniel | 22281c1 | 2016-03-23 13:49:40 -0700 | [diff] [blame] | 167 | GrVkPipelineStateDataManager fDataManager; |
| 168 | |
egdaniel | 22281c1 | 2016-03-23 13:49:40 -0700 | [diff] [blame] | 169 | int fNumSamplers; |
Greg Daniel | 31ec144 | 2017-05-08 10:30:59 -0400 | [diff] [blame] | 170 | int fNumTexelBuffers; |
egdaniel | 22281c1 | 2016-03-23 13:49:40 -0700 | [diff] [blame] | 171 | }; |
| 172 | |
| 173 | #endif |