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 | 5d8f69f | 2016-09-07 07:24:12 -0700 | [diff] [blame] | 12 | #include "GrProgramDesc.h" |
cdalton | 93a379b | 2016-05-11 13:58:08 -0700 | [diff] [blame] | 13 | #include "GrStencilSettings.h" |
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 "GrVkImage.h" |
egdaniel | 22281c1 | 2016-03-23 13:49:40 -0700 | [diff] [blame] | 16 | #include "GrVkPipelineStateDataManager.h" |
| 17 | #include "glsl/GrGLSLProgramBuilder.h" |
| 18 | |
jvanverth | e50f3e7 | 2016-03-28 07:03:06 -0700 | [diff] [blame] | 19 | #include "vk/GrVkDefines.h" |
egdaniel | 22281c1 | 2016-03-23 13:49:40 -0700 | [diff] [blame] | 20 | |
| 21 | class GrPipeline; |
Greg Daniel | 31ec144 | 2017-05-08 10:30:59 -0400 | [diff] [blame] | 22 | class GrVkBufferView; |
egdaniel | 22281c1 | 2016-03-23 13:49:40 -0700 | [diff] [blame] | 23 | class GrVkCommandBuffer; |
| 24 | class GrVkDescriptorPool; |
egdaniel | a95220d | 2016-07-21 11:50:37 -0700 | [diff] [blame] | 25 | class GrVkDescriptorSet; |
egdaniel | 22281c1 | 2016-03-23 13:49:40 -0700 | [diff] [blame] | 26 | class GrVkGpu; |
| 27 | class GrVkImageView; |
| 28 | class GrVkPipeline; |
| 29 | class GrVkSampler; |
| 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: |
| 40 | typedef GrGLSLProgramBuilder::BuiltinUniformHandles BuiltinUniformHandles; |
| 41 | |
| 42 | ~GrVkPipelineState(); |
| 43 | |
| 44 | GrVkPipeline* vkPipeline() const { return fPipeline; } |
| 45 | |
| 46 | void setData(GrVkGpu*, const GrPrimitiveProcessor&, const GrPipeline&); |
| 47 | |
| 48 | void bind(const GrVkGpu* gpu, GrVkCommandBuffer* commandBuffer); |
| 49 | |
| 50 | void addUniformResources(GrVkCommandBuffer&); |
| 51 | |
| 52 | void freeGPUResources(const GrVkGpu* gpu); |
| 53 | |
| 54 | // This releases resources that only a given instance of a GrVkPipelineState needs to hold onto |
| 55 | // and don't need to survive across new uses of the GrVkPipelineState. |
| 56 | void freeTempResources(const GrVkGpu* gpu); |
| 57 | |
| 58 | void abandonGPUResources(); |
| 59 | |
egdaniel | 22281c1 | 2016-03-23 13:49:40 -0700 | [diff] [blame] | 60 | /** |
| 61 | * For Vulkan we want to cache the entire VkPipeline for reuse of draws. The Desc here holds all |
| 62 | * the information needed to differentiate one pipeline from another. |
| 63 | * |
egdaniel | 720dc71 | 2016-09-07 11:56:59 -0700 | [diff] [blame] | 64 | * The GrProgramDesc contains all the information need to create the actual shaders for the |
egdaniel | 22281c1 | 2016-03-23 13:49:40 -0700 | [diff] [blame] | 65 | * pipeline. |
| 66 | * |
egdaniel | 720dc71 | 2016-09-07 11:56:59 -0700 | [diff] [blame] | 67 | * For Vulkan we need to add to the GrProgramDesc to include the rest of the state on the |
| 68 | * pipline. This includes stencil settings, blending information, render pass format, draw face |
egdaniel | 22281c1 | 2016-03-23 13:49:40 -0700 | [diff] [blame] | 69 | * information, and primitive type. Note that some state is set dynamically on the pipeline for |
| 70 | * each draw and thus is not included in this descriptor. This includes the viewport, scissor, |
| 71 | * and blend constant. |
egdaniel | 22281c1 | 2016-03-23 13:49:40 -0700 | [diff] [blame] | 72 | */ |
egdaniel | 720dc71 | 2016-09-07 11:56:59 -0700 | [diff] [blame] | 73 | class Desc : public GrProgramDesc { |
| 74 | public: |
| 75 | static bool Build(Desc*, |
| 76 | const GrPrimitiveProcessor&, |
| 77 | const GrPipeline&, |
csmartdalton | c633abb | 2016-11-01 08:55:55 -0700 | [diff] [blame] | 78 | const GrStencilSettings&, |
egdaniel | 720dc71 | 2016-09-07 11:56:59 -0700 | [diff] [blame] | 79 | GrPrimitiveType primitiveType, |
Jim Van Verth | 6a40abc | 2017-11-02 16:56:09 +0000 | [diff] [blame] | 80 | const GrShaderCaps&); |
egdaniel | 720dc71 | 2016-09-07 11:56:59 -0700 | [diff] [blame] | 81 | private: |
| 82 | typedef GrProgramDesc INHERITED; |
egdaniel | 22281c1 | 2016-03-23 13:49:40 -0700 | [diff] [blame] | 83 | }; |
| 84 | |
| 85 | const Desc& getDesc() { return fDesc; } |
| 86 | |
| 87 | private: |
| 88 | typedef GrVkPipelineStateDataManager::UniformInfoArray UniformInfoArray; |
| 89 | typedef GrGLSLProgramDataManager::UniformHandle UniformHandle; |
| 90 | |
| 91 | GrVkPipelineState(GrVkGpu* gpu, |
| 92 | const GrVkPipelineState::Desc&, |
| 93 | GrVkPipeline* pipeline, |
| 94 | VkPipelineLayout layout, |
egdaniel | 707bbd6 | 2016-07-26 07:19:47 -0700 | [diff] [blame] | 95 | const GrVkDescriptorSetManager::Handle& samplerDSHandle, |
Greg Daniel | 31ec144 | 2017-05-08 10:30:59 -0400 | [diff] [blame] | 96 | const GrVkDescriptorSetManager::Handle& texelBufferDSHandle, |
egdaniel | 22281c1 | 2016-03-23 13:49:40 -0700 | [diff] [blame] | 97 | const BuiltinUniformHandles& builtinUniformHandles, |
| 98 | const UniformInfoArray& uniforms, |
Greg Daniel | 18f9602 | 2017-05-04 15:09:03 -0400 | [diff] [blame] | 99 | uint32_t geometryUniformSize, |
egdaniel | 22281c1 | 2016-03-23 13:49:40 -0700 | [diff] [blame] | 100 | uint32_t fragmentUniformSize, |
| 101 | uint32_t numSamplers, |
Greg Daniel | 31ec144 | 2017-05-08 10:30:59 -0400 | [diff] [blame] | 102 | uint32_t numTexelBuffers, |
Robert Phillips | 369e8b7 | 2017-08-01 16:13:04 -0400 | [diff] [blame] | 103 | std::unique_ptr<GrGLSLPrimitiveProcessor> geometryProcessor, |
| 104 | std::unique_ptr<GrGLSLXferProcessor> xferProcessor, |
egdaniel | 22281c1 | 2016-03-23 13:49:40 -0700 | [diff] [blame] | 105 | const GrGLSLFragProcs& fragmentProcessors); |
| 106 | |
egdaniel | 22281c1 | 2016-03-23 13:49:40 -0700 | [diff] [blame] | 107 | void writeUniformBuffers(const GrVkGpu* gpu); |
| 108 | |
Brian Salomon | ab015ef | 2017-04-04 10:15:51 -0400 | [diff] [blame] | 109 | void writeSamplers( |
| 110 | GrVkGpu* gpu, |
| 111 | const SkTArray<const GrResourceIOProcessor::TextureSampler*>& textureBindings, |
| 112 | bool allowSRGBInputs); |
egdaniel | 22281c1 | 2016-03-23 13:49:40 -0700 | [diff] [blame] | 113 | |
Greg Daniel | 31ec144 | 2017-05-08 10:30:59 -0400 | [diff] [blame] | 114 | void writeTexelBuffers( |
| 115 | GrVkGpu* gpu, |
| 116 | const SkTArray<const GrResourceIOProcessor::BufferAccess*>& bufferAccesses); |
| 117 | |
egdaniel | 22281c1 | 2016-03-23 13:49:40 -0700 | [diff] [blame] | 118 | /** |
| 119 | * We use the RT's size and origin to adjust from Skia device space to vulkan normalized device |
| 120 | * space and to make device space positions have the correct origin for processors that require |
| 121 | * them. |
| 122 | */ |
| 123 | struct RenderTargetState { |
| 124 | SkISize fRenderTargetSize; |
| 125 | GrSurfaceOrigin fRenderTargetOrigin; |
| 126 | |
| 127 | RenderTargetState() { this->invalidate(); } |
| 128 | void invalidate() { |
| 129 | fRenderTargetSize.fWidth = -1; |
| 130 | fRenderTargetSize.fHeight = -1; |
| 131 | fRenderTargetOrigin = (GrSurfaceOrigin)-1; |
| 132 | } |
| 133 | |
| 134 | /** |
Ethan Nicholas | 5af9ea3 | 2017-07-28 15:19:46 -0400 | [diff] [blame] | 135 | * Gets a float4 that adjusts the position from Skia device coords to Vulkans normalized device |
| 136 | * 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] | 137 | * applied as such: |
| 138 | * pos.x = dot(v.xy, pos.xz) |
| 139 | * pos.y = dot(v.zw, pos.yz) |
| 140 | */ |
| 141 | void getRTAdjustmentVec(float* destVec) { |
| 142 | destVec[0] = 2.f / fRenderTargetSize.fWidth; |
| 143 | destVec[1] = -1.f; |
| 144 | if (kBottomLeft_GrSurfaceOrigin == fRenderTargetOrigin) { |
| 145 | destVec[2] = -2.f / fRenderTargetSize.fHeight; |
| 146 | destVec[3] = 1.f; |
| 147 | } else { |
| 148 | destVec[2] = 2.f / fRenderTargetSize.fHeight; |
| 149 | destVec[3] = -1.f; |
| 150 | } |
| 151 | } |
| 152 | }; |
| 153 | |
| 154 | // 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] | 155 | void setRenderTargetState(const GrRenderTargetProxy*); |
egdaniel | 22281c1 | 2016-03-23 13:49:40 -0700 | [diff] [blame] | 156 | |
| 157 | // GrVkResources |
| 158 | GrVkPipeline* fPipeline; |
| 159 | |
| 160 | // Used for binding DescriptorSets to the command buffer but does not need to survive during |
| 161 | // command buffer execution. Thus this is not need to be a GrVkResource. |
| 162 | VkPipelineLayout fPipelineLayout; |
| 163 | |
| 164 | // The DescriptorSets need to survive until the gpu has finished all draws that use them. |
| 165 | // However, they will only be freed by the descriptor pool. Thus by simply keeping the |
| 166 | // descriptor pool alive through the draw, the descritor sets will also stay alive. Thus we do |
| 167 | // not need a GrVkResource versions of VkDescriptorSet. We hold on to these in the |
| 168 | // 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] | 169 | VkDescriptorSet fDescriptorSets[3]; |
egdaniel | 22281c1 | 2016-03-23 13:49:40 -0700 | [diff] [blame] | 170 | |
egdaniel | a95220d | 2016-07-21 11:50:37 -0700 | [diff] [blame] | 171 | const GrVkDescriptorSet* fUniformDescriptorSet; |
egdaniel | 707bbd6 | 2016-07-26 07:19:47 -0700 | [diff] [blame] | 172 | const GrVkDescriptorSet* fSamplerDescriptorSet; |
Greg Daniel | 31ec144 | 2017-05-08 10:30:59 -0400 | [diff] [blame] | 173 | const GrVkDescriptorSet* fTexelBufferDescriptorSet; |
egdaniel | 707bbd6 | 2016-07-26 07:19:47 -0700 | [diff] [blame] | 174 | |
| 175 | const GrVkDescriptorSetManager::Handle fSamplerDSHandle; |
Greg Daniel | 31ec144 | 2017-05-08 10:30:59 -0400 | [diff] [blame] | 176 | const GrVkDescriptorSetManager::Handle fTexelBufferDSHandle; |
egdaniel | 22281c1 | 2016-03-23 13:49:40 -0700 | [diff] [blame] | 177 | |
Greg Daniel | 18f9602 | 2017-05-04 15:09:03 -0400 | [diff] [blame] | 178 | std::unique_ptr<GrVkUniformBuffer> fGeometryUniformBuffer; |
Ben Wagner | 145dbcd | 2016-11-03 14:40:50 -0400 | [diff] [blame] | 179 | std::unique_ptr<GrVkUniformBuffer> fFragmentUniformBuffer; |
egdaniel | 22281c1 | 2016-03-23 13:49:40 -0700 | [diff] [blame] | 180 | |
| 181 | // GrVkResources used for sampling textures |
| 182 | SkTDArray<GrVkSampler*> fSamplers; |
| 183 | SkTDArray<const GrVkImageView*> fTextureViews; |
egdaniel | b2df0c2 | 2016-05-13 11:30:37 -0700 | [diff] [blame] | 184 | SkTDArray<const GrVkResource*> fTextures; |
egdaniel | 22281c1 | 2016-03-23 13:49:40 -0700 | [diff] [blame] | 185 | |
Greg Daniel | 31ec144 | 2017-05-08 10:30:59 -0400 | [diff] [blame] | 186 | // GrVkResource used for TexelBuffers |
| 187 | SkTDArray<const GrVkBufferView*> fBufferViews; |
| 188 | SkTDArray<const GrVkResource*> fTexelBuffers; |
| 189 | |
egdaniel | 22281c1 | 2016-03-23 13:49:40 -0700 | [diff] [blame] | 190 | // Tracks the current render target uniforms stored in the vertex buffer. |
| 191 | RenderTargetState fRenderTargetState; |
| 192 | BuiltinUniformHandles fBuiltinUniformHandles; |
| 193 | |
| 194 | // Processors in the GrVkPipelineState |
Ben Wagner | 145dbcd | 2016-11-03 14:40:50 -0400 | [diff] [blame] | 195 | std::unique_ptr<GrGLSLPrimitiveProcessor> fGeometryProcessor; |
| 196 | std::unique_ptr<GrGLSLXferProcessor> fXferProcessor; |
egdaniel | 22281c1 | 2016-03-23 13:49:40 -0700 | [diff] [blame] | 197 | GrGLSLFragProcs fFragmentProcessors; |
| 198 | |
| 199 | Desc fDesc; |
| 200 | |
| 201 | GrVkPipelineStateDataManager fDataManager; |
| 202 | |
egdaniel | 22281c1 | 2016-03-23 13:49:40 -0700 | [diff] [blame] | 203 | int fNumSamplers; |
Greg Daniel | 31ec144 | 2017-05-08 10:30:59 -0400 | [diff] [blame] | 204 | int fNumTexelBuffers; |
egdaniel | 22281c1 | 2016-03-23 13:49:40 -0700 | [diff] [blame] | 205 | |
| 206 | friend class GrVkPipelineStateBuilder; |
| 207 | }; |
| 208 | |
| 209 | #endif |