blob: e8a1b39928c8e7fa0d2e6c5d36545704e801ca05 [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
Greg Daniel54bfb182018-11-20 17:12:36 -050012#include "GrVkVulkan.h"
13
egdaniel707bbd62016-07-26 07:19:47 -070014#include "GrVkDescriptorSetManager.h"
egdaniel22281c12016-03-23 13:49:40 -070015#include "GrVkPipelineStateDataManager.h"
16#include "glsl/GrGLSLProgramBuilder.h"
egdaniel22281c12016-03-23 13:49:40 -070017
18class GrPipeline;
Brian Salomon1471df92018-06-08 10:49:00 -040019class GrStencilSettings;
Greg Daniel31ec1442017-05-08 10:30:59 -040020class GrVkBufferView;
egdaniel22281c12016-03-23 13:49:40 -070021class GrVkCommandBuffer;
22class GrVkDescriptorPool;
egdaniela95220d2016-07-21 11:50:37 -070023class GrVkDescriptorSet;
egdaniel22281c12016-03-23 13:49:40 -070024class GrVkGpu;
25class GrVkImageView;
26class GrVkPipeline;
Greg Daniel7d918fd2018-06-19 15:22:01 -040027class GrVkPipelineLayout;
egdaniel22281c12016-03-23 13:49:40 -070028class GrVkSampler;
Brian Salomone782f842018-07-31 13:53:11 -040029class GrVkTexture;
egdaniel22281c12016-03-23 13:49:40 -070030class 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 */
38class GrVkPipelineState : public SkRefCnt {
39public:
Brian Salomon1471df92018-06-08 10:49:00 -040040 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 Salomon1471df92018-06-08 10:49:00 -040048 const GrGLSLBuiltinUniformHandles& builtinUniformHandles,
49 const UniformInfoArray& uniforms,
50 uint32_t geometryUniformSize,
51 uint32_t fragmentUniformSize,
Greg Daniel7a82edf2018-12-04 10:54:34 -050052 const UniformInfoArray& samplers,
Brian Salomon1471df92018-06-08 10:49:00 -040053 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
Brian Salomonf7232642018-09-19 08:58:08 -040060 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*);
egdaniel22281c12016-03-23 13:49:40 -070069
Brian Salomonf7232642018-09-19 08:58:08 -040070 void bindPipeline(const GrVkGpu* gpu, GrVkCommandBuffer* commandBuffer);
egdaniel22281c12016-03-23 13:49:40 -070071
Brian Salomonf7232642018-09-19 08:58:08 -040072 void addUniformResources(GrVkCommandBuffer&, GrVkSampler*[], GrVkTexture*[], int numTextures);
egdaniel22281c12016-03-23 13:49:40 -070073
74 void freeGPUResources(const GrVkGpu* gpu);
75
egdaniel22281c12016-03-23 13:49:40 -070076 void abandonGPUResources();
77
egdaniel22281c12016-03-23 13:49:40 -070078private:
egdaniel22281c12016-03-23 13:49:40 -070079 void writeUniformBuffers(const GrVkGpu* gpu);
80
egdaniel22281c12016-03-23 13:49:40 -070081 /**
Brian Salomonf7232642018-09-19 08:58:08 -040082 * 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 */
egdaniel22281c12016-03-23 13:49:40 -070086 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 Nicholas5af9ea32017-07-28 15:19:46 -040098 * 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
egdaniel22281c12016-03-23 13:49:40 -0700100 * 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 Phillips2890fbf2017-07-26 15:48:41 -0400118 void setRenderTargetState(const GrRenderTargetProxy*);
egdaniel22281c12016-03-23 13:49:40 -0700119
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 Daniel7d918fd2018-06-19 15:22:01 -0400125 GrVkPipelineLayout* fPipelineLayout;
egdaniel22281c12016-03-23 13:49:40 -0700126
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 Daniel31ec1442017-05-08 10:30:59 -0400132 VkDescriptorSet fDescriptorSets[3];
egdaniel22281c12016-03-23 13:49:40 -0700133
egdaniela95220d2016-07-21 11:50:37 -0700134 const GrVkDescriptorSet* fUniformDescriptorSet;
egdaniel707bbd62016-07-26 07:19:47 -0700135 const GrVkDescriptorSet* fSamplerDescriptorSet;
136
137 const GrVkDescriptorSetManager::Handle fSamplerDSHandle;
egdaniel22281c12016-03-23 13:49:40 -0700138
Greg Daniel7a82edf2018-12-04 10:54:34 -0500139 SkSTArray<4, const GrVkSampler*> fImmutableSamplers;
140
Greg Daniel18f96022017-05-04 15:09:03 -0400141 std::unique_ptr<GrVkUniformBuffer> fGeometryUniformBuffer;
Ben Wagner145dbcd2016-11-03 14:40:50 -0400142 std::unique_ptr<GrVkUniformBuffer> fFragmentUniformBuffer;
egdaniel22281c12016-03-23 13:49:40 -0700143
egdaniel22281c12016-03-23 13:49:40 -0700144 // Tracks the current render target uniforms stored in the vertex buffer.
145 RenderTargetState fRenderTargetState;
Brian Salomon1471df92018-06-08 10:49:00 -0400146 GrGLSLBuiltinUniformHandles fBuiltinUniformHandles;
egdaniel22281c12016-03-23 13:49:40 -0700147
148 // Processors in the GrVkPipelineState
Ben Wagner145dbcd2016-11-03 14:40:50 -0400149 std::unique_ptr<GrGLSLPrimitiveProcessor> fGeometryProcessor;
150 std::unique_ptr<GrGLSLXferProcessor> fXferProcessor;
Brian Salomon4d3f5172018-06-07 14:42:52 -0400151 std::unique_ptr<std::unique_ptr<GrGLSLFragmentProcessor>[]> fFragmentProcessors;
152 int fFragmentProcessorCnt;
egdaniel22281c12016-03-23 13:49:40 -0700153
egdaniel22281c12016-03-23 13:49:40 -0700154 GrVkPipelineStateDataManager fDataManager;
155
egdaniel22281c12016-03-23 13:49:40 -0700156 int fNumSamplers;
egdaniel22281c12016-03-23 13:49:40 -0700157};
158
159#endif