blob: ee948a8519ae3531e05384b64af6e08893f0cbb0 [file] [log] [blame]
egdaniel22281c12016-03-23 13:49:40 -07001/*
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
egdaniel707bbd62016-07-26 07:19:47 -070012#include "GrVkDescriptorSetManager.h"
egdaniel22281c12016-03-23 13:49:40 -070013#include "GrVkPipelineStateDataManager.h"
14#include "glsl/GrGLSLProgramBuilder.h"
jvanverthe50f3e72016-03-28 07:03:06 -070015#include "vk/GrVkDefines.h"
egdaniel22281c12016-03-23 13:49:40 -070016
17class GrPipeline;
Brian Salomon1471df92018-06-08 10:49:00 -040018class GrStencilSettings;
Greg Daniel31ec1442017-05-08 10:30:59 -040019class GrVkBufferView;
egdaniel22281c12016-03-23 13:49:40 -070020class GrVkCommandBuffer;
21class GrVkDescriptorPool;
egdaniela95220d2016-07-21 11:50:37 -070022class GrVkDescriptorSet;
egdaniel22281c12016-03-23 13:49:40 -070023class GrVkGpu;
24class GrVkImageView;
25class GrVkPipeline;
Greg Daniel7d918fd2018-06-19 15:22:01 -040026class GrVkPipelineLayout;
egdaniel22281c12016-03-23 13:49:40 -070027class GrVkSampler;
28class 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 */
36class GrVkPipelineState : public SkRefCnt {
37public:
Brian Salomon1471df92018-06-08 10:49:00 -040038 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);
egdaniel22281c12016-03-23 13:49:40 -070057
58 ~GrVkPipelineState();
59
egdaniel22281c12016-03-23 13:49:40 -070060 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
egdaniel22281c12016-03-23 13:49:40 -070074private:
egdaniel22281c12016-03-23 13:49:40 -070075 void writeUniformBuffers(const GrVkGpu* gpu);
76
Brian Salomonab015ef2017-04-04 10:15:51 -040077 void writeSamplers(
78 GrVkGpu* gpu,
Brian Osman2b23c4b2018-06-01 12:25:08 -040079 const SkTArray<const GrResourceIOProcessor::TextureSampler*>& textureBindings);
egdaniel22281c12016-03-23 13:49:40 -070080
Greg Daniel31ec1442017-05-08 10:30:59 -040081 void writeTexelBuffers(
82 GrVkGpu* gpu,
83 const SkTArray<const GrResourceIOProcessor::BufferAccess*>& bufferAccesses);
84
egdaniel22281c12016-03-23 13:49:40 -070085 /**
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 Nicholas5af9ea32017-07-28 15:19:46 -0400102 * 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
egdaniel22281c12016-03-23 13:49:40 -0700104 * 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 Phillips2890fbf2017-07-26 15:48:41 -0400122 void setRenderTargetState(const GrRenderTargetProxy*);
egdaniel22281c12016-03-23 13:49:40 -0700123
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 Daniel7d918fd2018-06-19 15:22:01 -0400129 GrVkPipelineLayout* fPipelineLayout;
egdaniel22281c12016-03-23 13:49:40 -0700130
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 Daniel31ec1442017-05-08 10:30:59 -0400136 VkDescriptorSet fDescriptorSets[3];
egdaniel22281c12016-03-23 13:49:40 -0700137
egdaniela95220d2016-07-21 11:50:37 -0700138 const GrVkDescriptorSet* fUniformDescriptorSet;
egdaniel707bbd62016-07-26 07:19:47 -0700139 const GrVkDescriptorSet* fSamplerDescriptorSet;
Greg Daniel31ec1442017-05-08 10:30:59 -0400140 const GrVkDescriptorSet* fTexelBufferDescriptorSet;
egdaniel707bbd62016-07-26 07:19:47 -0700141
142 const GrVkDescriptorSetManager::Handle fSamplerDSHandle;
Greg Daniel31ec1442017-05-08 10:30:59 -0400143 const GrVkDescriptorSetManager::Handle fTexelBufferDSHandle;
egdaniel22281c12016-03-23 13:49:40 -0700144
Greg Daniel18f96022017-05-04 15:09:03 -0400145 std::unique_ptr<GrVkUniformBuffer> fGeometryUniformBuffer;
Ben Wagner145dbcd2016-11-03 14:40:50 -0400146 std::unique_ptr<GrVkUniformBuffer> fFragmentUniformBuffer;
egdaniel22281c12016-03-23 13:49:40 -0700147
148 // GrVkResources used for sampling textures
149 SkTDArray<GrVkSampler*> fSamplers;
150 SkTDArray<const GrVkImageView*> fTextureViews;
egdanielb2df0c22016-05-13 11:30:37 -0700151 SkTDArray<const GrVkResource*> fTextures;
egdaniel22281c12016-03-23 13:49:40 -0700152
Greg Daniel31ec1442017-05-08 10:30:59 -0400153 // GrVkResource used for TexelBuffers
154 SkTDArray<const GrVkBufferView*> fBufferViews;
155 SkTDArray<const GrVkResource*> fTexelBuffers;
156
egdaniel22281c12016-03-23 13:49:40 -0700157 // Tracks the current render target uniforms stored in the vertex buffer.
158 RenderTargetState fRenderTargetState;
Brian Salomon1471df92018-06-08 10:49:00 -0400159 GrGLSLBuiltinUniformHandles fBuiltinUniformHandles;
egdaniel22281c12016-03-23 13:49:40 -0700160
161 // Processors in the GrVkPipelineState
Ben Wagner145dbcd2016-11-03 14:40:50 -0400162 std::unique_ptr<GrGLSLPrimitiveProcessor> fGeometryProcessor;
163 std::unique_ptr<GrGLSLXferProcessor> fXferProcessor;
Brian Salomon4d3f5172018-06-07 14:42:52 -0400164 std::unique_ptr<std::unique_ptr<GrGLSLFragmentProcessor>[]> fFragmentProcessors;
165 int fFragmentProcessorCnt;
egdaniel22281c12016-03-23 13:49:40 -0700166
egdaniel22281c12016-03-23 13:49:40 -0700167 GrVkPipelineStateDataManager fDataManager;
168
egdaniel22281c12016-03-23 13:49:40 -0700169 int fNumSamplers;
Greg Daniel31ec1442017-05-08 10:30:59 -0400170 int fNumTexelBuffers;
egdaniel22281c12016-03-23 13:49:40 -0700171};
172
173#endif