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 | |
| 12 | #include "GrVkImage.h" |
| 13 | #include "GrVkProgramDesc.h" |
| 14 | #include "GrVkPipelineStateDataManager.h" |
| 15 | #include "glsl/GrGLSLProgramBuilder.h" |
| 16 | |
jvanverth | e50f3e7 | 2016-03-28 07:03:06 -0700 | [diff] [blame] | 17 | #include "vk/GrVkDefines.h" |
egdaniel | 22281c1 | 2016-03-23 13:49:40 -0700 | [diff] [blame] | 18 | |
| 19 | class GrPipeline; |
| 20 | class GrVkCommandBuffer; |
| 21 | class GrVkDescriptorPool; |
| 22 | class GrVkGpu; |
| 23 | class GrVkImageView; |
| 24 | class GrVkPipeline; |
| 25 | class GrVkSampler; |
| 26 | class GrVkUniformBuffer; |
| 27 | |
| 28 | /** |
| 29 | * This class holds onto a GrVkPipeline object that we use for draws. Besides storing the acutal |
| 30 | * GrVkPipeline object, this class is also responsible handling all uniforms, descriptors, samplers, |
| 31 | * and other similar objects that are used along with the VkPipeline in the draw. This includes both |
| 32 | * allocating and freeing these objects, as well as updating their values. |
| 33 | */ |
| 34 | class GrVkPipelineState : public SkRefCnt { |
| 35 | public: |
| 36 | typedef GrGLSLProgramBuilder::BuiltinUniformHandles BuiltinUniformHandles; |
| 37 | |
| 38 | ~GrVkPipelineState(); |
| 39 | |
| 40 | GrVkPipeline* vkPipeline() const { return fPipeline; } |
| 41 | |
| 42 | void setData(GrVkGpu*, const GrPrimitiveProcessor&, const GrPipeline&); |
| 43 | |
| 44 | void bind(const GrVkGpu* gpu, GrVkCommandBuffer* commandBuffer); |
| 45 | |
| 46 | void addUniformResources(GrVkCommandBuffer&); |
| 47 | |
| 48 | void freeGPUResources(const GrVkGpu* gpu); |
| 49 | |
| 50 | // This releases resources that only a given instance of a GrVkPipelineState needs to hold onto |
| 51 | // and don't need to survive across new uses of the GrVkPipelineState. |
| 52 | void freeTempResources(const GrVkGpu* gpu); |
| 53 | |
| 54 | void abandonGPUResources(); |
| 55 | |
| 56 | // The key is composed of two parts: |
| 57 | // 1. uint32_t for total key length |
| 58 | // 2. Pipeline state data |
| 59 | enum StateKeyOffsets { |
| 60 | // Part 1. |
| 61 | kLength_StateKeyOffset = 0, |
| 62 | // Part 2. |
| 63 | kData_StateKeyOffset = kLength_StateKeyOffset + sizeof(uint32_t), |
| 64 | }; |
| 65 | static void BuildStateKey(const GrPipeline&, GrPrimitiveType primitiveType, |
| 66 | SkTArray<unsigned char, true>* key); |
| 67 | |
| 68 | /** |
| 69 | * For Vulkan we want to cache the entire VkPipeline for reuse of draws. The Desc here holds all |
| 70 | * the information needed to differentiate one pipeline from another. |
| 71 | * |
| 72 | * The GrVkProgramDesc contains all the information need to create the actual shaders for the |
| 73 | * pipeline. |
| 74 | * |
| 75 | * The fStateKey is used to store all the inputs for the rest of the state stored on the |
| 76 | * pipeline. This includes stencil settings, blending information, render pass format, draw face |
| 77 | * information, and primitive type. Note that some state is set dynamically on the pipeline for |
| 78 | * each draw and thus is not included in this descriptor. This includes the viewport, scissor, |
| 79 | * and blend constant. |
| 80 | * |
| 81 | * A checksum which includes the fProgramDesc and fStateKey is included at the top of the Desc |
| 82 | * for caching purposes and faster equality checks. |
| 83 | */ |
| 84 | struct Desc { |
| 85 | uint32_t fChecksum; |
| 86 | GrVkProgramDesc fProgramDesc; |
| 87 | |
| 88 | enum { |
| 89 | kRenderPassKeyAlloc = 12, // This is typical color attachment with no stencil or msaa |
| 90 | kStencilKeyAlloc = sizeof(GrStencilSettings), |
| 91 | kDrawFaceKeyAlloc = 4, |
| 92 | kBlendingKeyAlloc = 4, |
| 93 | kPrimitiveTypeKeyAlloc = 4, |
| 94 | kPreAllocSize = kData_StateKeyOffset + kRenderPassKeyAlloc + kStencilKeyAlloc + |
| 95 | kDrawFaceKeyAlloc + kBlendingKeyAlloc + kPrimitiveTypeKeyAlloc, |
| 96 | }; |
| 97 | SkSTArray<kPreAllocSize, uint8_t, true> fStateKey; |
| 98 | |
| 99 | bool operator== (const Desc& that) const { |
| 100 | if (fChecksum != that.fChecksum || fProgramDesc != that.fProgramDesc) { |
| 101 | return false; |
| 102 | } |
| 103 | // We store the keyLength at the start of fVkKey. Thus we don't have to worry about |
| 104 | // different length keys since we will fail on the comparison immediately. Therefore we |
| 105 | // just use this PipelineDesc to get the length to iterate over. |
| 106 | int keyLength = fStateKey.count(); |
| 107 | SkASSERT(SkIsAlign4(keyLength)); |
| 108 | int l = keyLength >> 2; |
| 109 | const uint32_t* aKey = reinterpret_cast<const uint32_t*>(fStateKey.begin()); |
| 110 | const uint32_t* bKey = reinterpret_cast<const uint32_t*>(that.fStateKey.begin()); |
| 111 | for (int i = 0; i < l; ++i) { |
| 112 | if (aKey[i] != bKey[i]) { |
| 113 | return false; |
| 114 | } |
| 115 | } |
| 116 | return true; |
| 117 | } |
| 118 | |
| 119 | static bool Less(const Desc& a, const Desc& b) { |
| 120 | if (a.fChecksum != b.fChecksum) { |
| 121 | return a.fChecksum < b.fChecksum ? true : false; |
| 122 | } |
| 123 | bool progDescLess = GrProgramDesc::Less(a.fProgramDesc, b.fProgramDesc); |
| 124 | if (progDescLess || a.fProgramDesc != b.fProgramDesc) { |
| 125 | return progDescLess; |
| 126 | } |
| 127 | |
| 128 | int keyLength = a.fStateKey.count(); |
| 129 | SkASSERT(SkIsAlign4(keyLength)); |
| 130 | int l = keyLength >> 2; |
| 131 | const uint32_t* aKey = reinterpret_cast<const uint32_t*>(a.fStateKey.begin()); |
| 132 | const uint32_t* bKey = reinterpret_cast<const uint32_t*>(b.fStateKey.begin()); |
| 133 | for (int i = 0; i < l; ++i) { |
| 134 | if (aKey[i] != bKey[i]) { |
| 135 | return aKey[i] < bKey[i] ? true : false; |
| 136 | } |
| 137 | } |
| 138 | return false; |
| 139 | } |
| 140 | }; |
| 141 | |
| 142 | const Desc& getDesc() { return fDesc; } |
| 143 | |
| 144 | private: |
| 145 | typedef GrVkPipelineStateDataManager::UniformInfoArray UniformInfoArray; |
| 146 | typedef GrGLSLProgramDataManager::UniformHandle UniformHandle; |
| 147 | |
| 148 | GrVkPipelineState(GrVkGpu* gpu, |
| 149 | const GrVkPipelineState::Desc&, |
| 150 | GrVkPipeline* pipeline, |
| 151 | VkPipelineLayout layout, |
| 152 | VkDescriptorSetLayout dsLayout[2], |
| 153 | const BuiltinUniformHandles& builtinUniformHandles, |
| 154 | const UniformInfoArray& uniforms, |
| 155 | uint32_t vertexUniformSize, |
| 156 | uint32_t fragmentUniformSize, |
| 157 | uint32_t numSamplers, |
| 158 | GrGLSLPrimitiveProcessor* geometryProcessor, |
| 159 | GrGLSLXferProcessor* xferProcessor, |
| 160 | const GrGLSLFragProcs& fragmentProcessors); |
| 161 | |
| 162 | // Each pool will manage one type of descriptor. Thus each descriptor set we use will all be of |
| 163 | // one VkDescriptorType. |
| 164 | struct DescriptorPoolManager { |
| 165 | DescriptorPoolManager(VkDescriptorSetLayout layout, VkDescriptorType type, |
| 166 | uint32_t descCount, GrVkGpu* gpu) |
| 167 | : fDescLayout(layout) |
| 168 | , fDescType(type) |
| 169 | , fCurrentDescriptorSet(0) |
| 170 | , fPool(nullptr) { |
egdaniel | 5f3b0e0 | 2016-04-07 13:49:01 -0700 | [diff] [blame] | 171 | SkASSERT(descCount < (kMaxDescSetLimit >> 2)); |
egdaniel | 22281c1 | 2016-03-23 13:49:40 -0700 | [diff] [blame] | 172 | fMaxDescriptorSets = descCount << 2; |
| 173 | this->getNewPool(gpu); |
| 174 | } |
| 175 | |
| 176 | ~DescriptorPoolManager() { |
| 177 | SkASSERT(!fDescLayout); |
| 178 | SkASSERT(!fPool); |
| 179 | } |
| 180 | |
| 181 | void getNewDescriptorSet(GrVkGpu* gpu, VkDescriptorSet* ds); |
| 182 | |
| 183 | void freeGPUResources(const GrVkGpu* gpu); |
| 184 | void abandonGPUResources(); |
| 185 | |
| 186 | VkDescriptorSetLayout fDescLayout; |
| 187 | VkDescriptorType fDescType; |
| 188 | uint32_t fMaxDescriptorSets; |
| 189 | uint32_t fCurrentDescriptorSet; |
| 190 | GrVkDescriptorPool* fPool; |
| 191 | |
| 192 | private: |
egdaniel | 5f3b0e0 | 2016-04-07 13:49:01 -0700 | [diff] [blame] | 193 | static const uint32_t kMaxDescSetLimit = 1 << 10; |
| 194 | |
egdaniel | 22281c1 | 2016-03-23 13:49:40 -0700 | [diff] [blame] | 195 | void getNewPool(GrVkGpu* gpu); |
| 196 | }; |
| 197 | |
| 198 | void writeUniformBuffers(const GrVkGpu* gpu); |
| 199 | |
| 200 | void writeSamplers(GrVkGpu* gpu, const SkTArray<const GrTextureAccess*>& textureBindings); |
| 201 | |
| 202 | /** |
| 203 | * We use the RT's size and origin to adjust from Skia device space to vulkan normalized device |
| 204 | * space and to make device space positions have the correct origin for processors that require |
| 205 | * them. |
| 206 | */ |
| 207 | struct RenderTargetState { |
| 208 | SkISize fRenderTargetSize; |
| 209 | GrSurfaceOrigin fRenderTargetOrigin; |
| 210 | |
| 211 | RenderTargetState() { this->invalidate(); } |
| 212 | void invalidate() { |
| 213 | fRenderTargetSize.fWidth = -1; |
| 214 | fRenderTargetSize.fHeight = -1; |
| 215 | fRenderTargetOrigin = (GrSurfaceOrigin)-1; |
| 216 | } |
| 217 | |
| 218 | /** |
| 219 | * Gets a vec4 that adjusts the position from Skia device coords to Vulkans normalized device |
| 220 | * coords. Assuming the transformed position, pos, is a homogeneous vec3, the vec, v, is |
| 221 | * applied as such: |
| 222 | * pos.x = dot(v.xy, pos.xz) |
| 223 | * pos.y = dot(v.zw, pos.yz) |
| 224 | */ |
| 225 | void getRTAdjustmentVec(float* destVec) { |
| 226 | destVec[0] = 2.f / fRenderTargetSize.fWidth; |
| 227 | destVec[1] = -1.f; |
| 228 | if (kBottomLeft_GrSurfaceOrigin == fRenderTargetOrigin) { |
| 229 | destVec[2] = -2.f / fRenderTargetSize.fHeight; |
| 230 | destVec[3] = 1.f; |
| 231 | } else { |
| 232 | destVec[2] = 2.f / fRenderTargetSize.fHeight; |
| 233 | destVec[3] = -1.f; |
| 234 | } |
| 235 | } |
| 236 | }; |
| 237 | |
| 238 | // Helper for setData() that sets the view matrix and loads the render target height uniform |
| 239 | void setRenderTargetState(const GrPipeline&); |
| 240 | |
| 241 | // GrVkResources |
| 242 | GrVkPipeline* fPipeline; |
| 243 | |
| 244 | // Used for binding DescriptorSets to the command buffer but does not need to survive during |
| 245 | // command buffer execution. Thus this is not need to be a GrVkResource. |
| 246 | VkPipelineLayout fPipelineLayout; |
| 247 | |
| 248 | // The DescriptorSets need to survive until the gpu has finished all draws that use them. |
| 249 | // However, they will only be freed by the descriptor pool. Thus by simply keeping the |
| 250 | // descriptor pool alive through the draw, the descritor sets will also stay alive. Thus we do |
| 251 | // not need a GrVkResource versions of VkDescriptorSet. We hold on to these in the |
| 252 | // GrVkPipelineState since we update the descriptor sets and bind them at separate times; |
| 253 | VkDescriptorSet fDescriptorSets[2]; |
| 254 | |
| 255 | // Meta data so we know which descriptor sets we are using and need to bind. |
| 256 | int fStartDS; |
| 257 | int fDSCount; |
| 258 | |
| 259 | SkAutoTDelete<GrVkUniformBuffer> fVertexUniformBuffer; |
| 260 | SkAutoTDelete<GrVkUniformBuffer> fFragmentUniformBuffer; |
| 261 | |
| 262 | // GrVkResources used for sampling textures |
| 263 | SkTDArray<GrVkSampler*> fSamplers; |
| 264 | SkTDArray<const GrVkImageView*> fTextureViews; |
| 265 | SkTDArray<const GrVkImage::Resource*> fTextures; |
| 266 | |
| 267 | // Tracks the current render target uniforms stored in the vertex buffer. |
| 268 | RenderTargetState fRenderTargetState; |
| 269 | BuiltinUniformHandles fBuiltinUniformHandles; |
| 270 | |
| 271 | // Processors in the GrVkPipelineState |
| 272 | SkAutoTDelete<GrGLSLPrimitiveProcessor> fGeometryProcessor; |
| 273 | SkAutoTDelete<GrGLSLXferProcessor> fXferProcessor; |
| 274 | GrGLSLFragProcs fFragmentProcessors; |
| 275 | |
| 276 | Desc fDesc; |
| 277 | |
| 278 | GrVkPipelineStateDataManager fDataManager; |
| 279 | |
| 280 | DescriptorPoolManager fSamplerPoolManager; |
| 281 | DescriptorPoolManager fUniformPoolManager; |
| 282 | |
| 283 | int fNumSamplers; |
| 284 | |
| 285 | friend class GrVkPipelineStateBuilder; |
| 286 | }; |
| 287 | |
| 288 | #endif |