blob: 1b1cdaf24920292288effab099615cc38a6bfbad [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -050012#include "include/gpu/vk/GrVkTypes.h"
13#include "src/gpu/glsl/GrGLSLProgramBuilder.h"
14#include "src/gpu/vk/GrVkDescriptorSetManager.h"
15#include "src/gpu/vk/GrVkPipelineStateDataManager.h"
egdaniel22281c12016-03-23 13:49:40 -070016
17class GrPipeline;
Brian Salomon1471df92018-06-08 10:49:00 -040018class GrStencilSettings;
egdaniel22281c12016-03-23 13:49:40 -070019class GrVkCommandBuffer;
20class GrVkDescriptorPool;
egdaniela95220d2016-07-21 11:50:37 -070021class GrVkDescriptorSet;
egdaniel22281c12016-03-23 13:49:40 -070022class GrVkGpu;
23class GrVkImageView;
24class GrVkPipeline;
25class GrVkSampler;
Brian Salomone782f842018-07-31 13:53:11 -040026class GrVkTexture;
egdaniel22281c12016-03-23 13:49:40 -070027class GrVkUniformBuffer;
28
29/**
30 * This class holds onto a GrVkPipeline object that we use for draws. Besides storing the acutal
31 * GrVkPipeline object, this class is also responsible handling all uniforms, descriptors, samplers,
32 * and other similar objects that are used along with the VkPipeline in the draw. This includes both
33 * allocating and freeing these objects, as well as updating their values.
34 */
35class GrVkPipelineState : public SkRefCnt {
36public:
Brian Salomon1471df92018-06-08 10:49:00 -040037 using UniformInfoArray = GrVkPipelineStateDataManager::UniformInfoArray;
38 using UniformHandle = GrGLSLProgramDataManager::UniformHandle;
39
40 GrVkPipelineState(
41 GrVkGpu* gpu,
42 GrVkPipeline* pipeline,
Brian Salomon1471df92018-06-08 10:49:00 -040043 const GrVkDescriptorSetManager::Handle& samplerDSHandle,
Brian Salomon1471df92018-06-08 10:49:00 -040044 const GrGLSLBuiltinUniformHandles& builtinUniformHandles,
45 const UniformInfoArray& uniforms,
Ethan Nicholas0be34802019-08-15 12:36:58 -040046 uint32_t uniformSize,
Greg Daniel7a82edf2018-12-04 10:54:34 -050047 const UniformInfoArray& samplers,
Brian Salomon1471df92018-06-08 10:49:00 -040048 std::unique_ptr<GrGLSLPrimitiveProcessor> geometryProcessor,
49 std::unique_ptr<GrGLSLXferProcessor> xferProcessor,
50 std::unique_ptr<std::unique_ptr<GrGLSLFragmentProcessor>[]> fragmentProcessors,
51 int fFragmentProcessorCnt);
egdaniel22281c12016-03-23 13:49:40 -070052
53 ~GrVkPipelineState();
54
Greg Daniel95f0b162019-11-11 13:42:30 -050055 bool setAndBindUniforms(GrVkGpu*, const GrRenderTarget*, const GrProgramInfo&,
Robert Phillips835cbda2019-10-09 15:41:50 -040056 GrVkCommandBuffer*);
Brian Salomonf7232642018-09-19 08:58:08 -040057 /**
58 * This must be called after setAndBindUniforms() since that function invalidates texture
59 * bindings.
60 */
Greg Daniel95f0b162019-11-11 13:42:30 -050061 bool setAndBindTextures(GrVkGpu*, const GrPrimitiveProcessor&, const GrPipeline&,
Brian Salomonf7232642018-09-19 08:58:08 -040062 const GrTextureProxy* const primitiveProcessorTextures[],
63 GrVkCommandBuffer*);
egdaniel22281c12016-03-23 13:49:40 -070064
Brian Salomonf7232642018-09-19 08:58:08 -040065 void bindPipeline(const GrVkGpu* gpu, GrVkCommandBuffer* commandBuffer);
egdaniel22281c12016-03-23 13:49:40 -070066
Brian Salomonf7232642018-09-19 08:58:08 -040067 void addUniformResources(GrVkCommandBuffer&, GrVkSampler*[], GrVkTexture*[], int numTextures);
egdaniel22281c12016-03-23 13:49:40 -070068
Ethan Nicholas8e265a72018-12-12 16:22:40 -050069 void freeGPUResources(GrVkGpu* gpu);
egdaniel22281c12016-03-23 13:49:40 -070070
egdaniel22281c12016-03-23 13:49:40 -070071 void abandonGPUResources();
72
egdaniel22281c12016-03-23 13:49:40 -070073private:
egdaniel22281c12016-03-23 13:49:40 -070074 void writeUniformBuffers(const GrVkGpu* gpu);
75
egdaniel22281c12016-03-23 13:49:40 -070076 /**
Brian Salomonf7232642018-09-19 08:58:08 -040077 * We use the RT's size and origin to adjust from Skia device space to vulkan normalized device
78 * space and to make device space positions have the correct origin for processors that require
79 * them.
80 */
egdaniel22281c12016-03-23 13:49:40 -070081 struct RenderTargetState {
82 SkISize fRenderTargetSize;
83 GrSurfaceOrigin fRenderTargetOrigin;
84
85 RenderTargetState() { this->invalidate(); }
86 void invalidate() {
87 fRenderTargetSize.fWidth = -1;
88 fRenderTargetSize.fHeight = -1;
89 fRenderTargetOrigin = (GrSurfaceOrigin)-1;
90 }
91
92 /**
Ethan Nicholas5af9ea32017-07-28 15:19:46 -040093 * Gets a float4 that adjusts the position from Skia device coords to Vulkans normalized device
94 * coords. Assuming the transformed position, pos, is a homogeneous float3, the vec, v, is
egdaniel22281c12016-03-23 13:49:40 -070095 * applied as such:
96 * pos.x = dot(v.xy, pos.xz)
97 * pos.y = dot(v.zw, pos.yz)
98 */
99 void getRTAdjustmentVec(float* destVec) {
100 destVec[0] = 2.f / fRenderTargetSize.fWidth;
101 destVec[1] = -1.f;
102 if (kBottomLeft_GrSurfaceOrigin == fRenderTargetOrigin) {
103 destVec[2] = -2.f / fRenderTargetSize.fHeight;
104 destVec[3] = 1.f;
105 } else {
106 destVec[2] = 2.f / fRenderTargetSize.fHeight;
107 destVec[3] = -1.f;
108 }
109 }
110 };
111
112 // Helper for setData() that sets the view matrix and loads the render target height uniform
Robert Phillipsd0fe8752019-01-31 14:13:59 -0500113 void setRenderTargetState(const GrRenderTarget*, GrSurfaceOrigin);
egdaniel22281c12016-03-23 13:49:40 -0700114
115 // GrVkResources
116 GrVkPipeline* fPipeline;
117
egdaniela95220d2016-07-21 11:50:37 -0700118 const GrVkDescriptorSet* fUniformDescriptorSet;
egdaniel707bbd62016-07-26 07:19:47 -0700119
120 const GrVkDescriptorSetManager::Handle fSamplerDSHandle;
egdaniel22281c12016-03-23 13:49:40 -0700121
Greg Danielc10bb6a2019-10-29 09:50:32 -0400122 SkSTArray<4, const GrVkSampler*> fImmutableSamplers;
Greg Daniel7a82edf2018-12-04 10:54:34 -0500123
Ethan Nicholas0be34802019-08-15 12:36:58 -0400124 std::unique_ptr<GrVkUniformBuffer> fUniformBuffer;
egdaniel22281c12016-03-23 13:49:40 -0700125
egdaniel22281c12016-03-23 13:49:40 -0700126 // Tracks the current render target uniforms stored in the vertex buffer.
127 RenderTargetState fRenderTargetState;
Brian Salomon1471df92018-06-08 10:49:00 -0400128 GrGLSLBuiltinUniformHandles fBuiltinUniformHandles;
egdaniel22281c12016-03-23 13:49:40 -0700129
130 // Processors in the GrVkPipelineState
Ben Wagner145dbcd2016-11-03 14:40:50 -0400131 std::unique_ptr<GrGLSLPrimitiveProcessor> fGeometryProcessor;
132 std::unique_ptr<GrGLSLXferProcessor> fXferProcessor;
Brian Salomon4d3f5172018-06-07 14:42:52 -0400133 std::unique_ptr<std::unique_ptr<GrGLSLFragmentProcessor>[]> fFragmentProcessors;
134 int fFragmentProcessorCnt;
egdaniel22281c12016-03-23 13:49:40 -0700135
egdaniel22281c12016-03-23 13:49:40 -0700136 GrVkPipelineStateDataManager fDataManager;
137
egdaniel22281c12016-03-23 13:49:40 -0700138 int fNumSamplers;
egdaniel22281c12016-03-23 13:49:40 -0700139};
140
141#endif