blob: d8401fcca39433361fd7f4d6e88ed920041d2a94 [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;
Greg Daniel8eb119a2021-02-04 09:41:19 -050019class GrVkBuffer2;
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 Daniel37fd6582020-09-14 12:36:09 -040026class GrVkRenderTarget;
egdaniel22281c12016-03-23 13:49:40 -070027class GrVkSampler;
Brian Salomone782f842018-07-31 13:53:11 -040028class GrVkTexture;
egdaniel22281c12016-03-23 13:49:40 -070029
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 */
Stephen Whiteb1857852020-02-07 15:33:23 +000036class GrVkPipelineState {
egdaniel22281c12016-03-23 13:49:40 -070037public:
Brian Salomon1471df92018-06-08 10:49:00 -040038 using UniformInfoArray = GrVkPipelineStateDataManager::UniformInfoArray;
39 using UniformHandle = GrGLSLProgramDataManager::UniformHandle;
40
41 GrVkPipelineState(
42 GrVkGpu* gpu,
Greg Daniel3ef052c2021-01-05 12:20:27 -050043 sk_sp<const GrVkPipeline> pipeline,
Brian Salomon1471df92018-06-08 10:49:00 -040044 const GrVkDescriptorSetManager::Handle& samplerDSHandle,
Brian Salomon1471df92018-06-08 10:49:00 -040045 const GrGLSLBuiltinUniformHandles& builtinUniformHandles,
46 const UniformInfoArray& uniforms,
Ethan Nicholas0be34802019-08-15 12:36:58 -040047 uint32_t uniformSize,
Jim Van Verth205e99a2021-02-08 18:02:41 -050048 bool usePushConstants,
Greg Daniel7a82edf2018-12-04 10:54:34 -050049 const UniformInfoArray& samplers,
Brian Salomon1471df92018-06-08 10:49:00 -040050 std::unique_ptr<GrGLSLPrimitiveProcessor> geometryProcessor,
51 std::unique_ptr<GrGLSLXferProcessor> xferProcessor,
Brian Salomon61a70fb2020-07-08 19:02:54 -040052 std::unique_ptr<std::unique_ptr<GrGLSLFragmentProcessor>[]> fragmentProcessors);
egdaniel22281c12016-03-23 13:49:40 -070053
54 ~GrVkPipelineState();
55
Greg Daniel95f0b162019-11-11 13:42:30 -050056 bool setAndBindUniforms(GrVkGpu*, const GrRenderTarget*, const GrProgramInfo&,
Robert Phillips835cbda2019-10-09 15:41:50 -040057 GrVkCommandBuffer*);
Brian Salomonf7232642018-09-19 08:58:08 -040058 /**
59 * This must be called after setAndBindUniforms() since that function invalidates texture
60 * bindings.
61 */
Greg Daniel95f0b162019-11-11 13:42:30 -050062 bool setAndBindTextures(GrVkGpu*, const GrPrimitiveProcessor&, const GrPipeline&,
Michael Ludwigfcdd0612019-11-25 08:34:31 -050063 const GrSurfaceProxy* const primitiveProcessorTextures[],
Brian Salomonf7232642018-09-19 08:58:08 -040064 GrVkCommandBuffer*);
egdaniel22281c12016-03-23 13:49:40 -070065
Greg Daniel8cc5f192020-10-02 10:45:43 -040066 bool setAndBindInputAttachment(GrVkGpu*, GrVkRenderTarget* renderTarget, GrVkCommandBuffer*);
egdaniel22281c12016-03-23 13:49:40 -070067
Greg Daniel37fd6582020-09-14 12:36:09 -040068 void bindPipeline(const GrVkGpu* gpu, GrVkCommandBuffer* commandBuffer);
egdaniel22281c12016-03-23 13:49:40 -070069
Greg Daniel9d02a4c2020-07-15 14:26:08 -040070 void freeGPUResources(GrVkGpu* gpu);
egdaniel22281c12016-03-23 13:49:40 -070071
egdaniel22281c12016-03-23 13:49:40 -070072private:
egdaniel22281c12016-03-23 13:49:40 -070073 /**
Brian Salomonf7232642018-09-19 08:58:08 -040074 * We use the RT's size and origin to adjust from Skia device space to vulkan normalized device
75 * space and to make device space positions have the correct origin for processors that require
76 * them.
77 */
egdaniel22281c12016-03-23 13:49:40 -070078 struct RenderTargetState {
79 SkISize fRenderTargetSize;
80 GrSurfaceOrigin fRenderTargetOrigin;
81
82 RenderTargetState() { this->invalidate(); }
83 void invalidate() {
84 fRenderTargetSize.fWidth = -1;
85 fRenderTargetSize.fHeight = -1;
86 fRenderTargetOrigin = (GrSurfaceOrigin)-1;
87 }
88
89 /**
Ethan Nicholas5af9ea32017-07-28 15:19:46 -040090 * Gets a float4 that adjusts the position from Skia device coords to Vulkans normalized device
91 * coords. Assuming the transformed position, pos, is a homogeneous float3, the vec, v, is
egdaniel22281c12016-03-23 13:49:40 -070092 * applied as such:
93 * pos.x = dot(v.xy, pos.xz)
94 * pos.y = dot(v.zw, pos.yz)
95 */
96 void getRTAdjustmentVec(float* destVec) {
97 destVec[0] = 2.f / fRenderTargetSize.fWidth;
98 destVec[1] = -1.f;
99 if (kBottomLeft_GrSurfaceOrigin == fRenderTargetOrigin) {
100 destVec[2] = -2.f / fRenderTargetSize.fHeight;
101 destVec[3] = 1.f;
102 } else {
103 destVec[2] = 2.f / fRenderTargetSize.fHeight;
104 destVec[3] = -1.f;
105 }
106 }
107 };
108
109 // Helper for setData() that sets the view matrix and loads the render target height uniform
Robert Phillipsd0fe8752019-01-31 14:13:59 -0500110 void setRenderTargetState(const GrRenderTarget*, GrSurfaceOrigin);
egdaniel22281c12016-03-23 13:49:40 -0700111
Jim Van Verth3e192162020-03-10 16:23:16 -0400112 // GrManagedResources
Greg Daniel3ef052c2021-01-05 12:20:27 -0500113 sk_sp<const GrVkPipeline> fPipeline;
egdaniel22281c12016-03-23 13:49:40 -0700114
egdaniel707bbd62016-07-26 07:19:47 -0700115 const GrVkDescriptorSetManager::Handle fSamplerDSHandle;
egdaniel22281c12016-03-23 13:49:40 -0700116
Greg Danielc10bb6a2019-10-29 09:50:32 -0400117 SkSTArray<4, const GrVkSampler*> fImmutableSamplers;
Greg Daniel7a82edf2018-12-04 10:54:34 -0500118
egdaniel22281c12016-03-23 13:49:40 -0700119 // Tracks the current render target uniforms stored in the vertex buffer.
120 RenderTargetState fRenderTargetState;
Brian Salomon1471df92018-06-08 10:49:00 -0400121 GrGLSLBuiltinUniformHandles fBuiltinUniformHandles;
egdaniel22281c12016-03-23 13:49:40 -0700122
123 // Processors in the GrVkPipelineState
Ben Wagner145dbcd2016-11-03 14:40:50 -0400124 std::unique_ptr<GrGLSLPrimitiveProcessor> fGeometryProcessor;
125 std::unique_ptr<GrGLSLXferProcessor> fXferProcessor;
Brian Salomon4d3f5172018-06-07 14:42:52 -0400126 std::unique_ptr<std::unique_ptr<GrGLSLFragmentProcessor>[]> fFragmentProcessors;
egdaniel22281c12016-03-23 13:49:40 -0700127
egdaniel22281c12016-03-23 13:49:40 -0700128 GrVkPipelineStateDataManager fDataManager;
129
egdaniel22281c12016-03-23 13:49:40 -0700130 int fNumSamplers;
egdaniel22281c12016-03-23 13:49:40 -0700131};
132
133#endif