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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 12 | #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" |
egdaniel | 22281c1 | 2016-03-23 13:49:40 -0700 | [diff] [blame] | 16 | |
| 17 | class GrPipeline; |
Brian Salomon | 1471df9 | 2018-06-08 10:49:00 -0400 | [diff] [blame] | 18 | class GrStencilSettings; |
Greg Daniel | 8eb119a | 2021-02-04 09:41:19 -0500 | [diff] [blame] | 19 | class GrVkBuffer2; |
egdaniel | 22281c1 | 2016-03-23 13:49:40 -0700 | [diff] [blame] | 20 | class GrVkCommandBuffer; |
| 21 | class GrVkDescriptorPool; |
egdaniel | a95220d | 2016-07-21 11:50:37 -0700 | [diff] [blame] | 22 | class GrVkDescriptorSet; |
egdaniel | 22281c1 | 2016-03-23 13:49:40 -0700 | [diff] [blame] | 23 | class GrVkGpu; |
| 24 | class GrVkImageView; |
| 25 | class GrVkPipeline; |
Greg Daniel | 37fd658 | 2020-09-14 12:36:09 -0400 | [diff] [blame] | 26 | class GrVkRenderTarget; |
egdaniel | 22281c1 | 2016-03-23 13:49:40 -0700 | [diff] [blame] | 27 | class GrVkSampler; |
Brian Salomon | e782f84 | 2018-07-31 13:53:11 -0400 | [diff] [blame] | 28 | class GrVkTexture; |
egdaniel | 22281c1 | 2016-03-23 13:49:40 -0700 | [diff] [blame] | 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 | */ |
Stephen White | b185785 | 2020-02-07 15:33:23 +0000 | [diff] [blame] | 36 | class GrVkPipelineState { |
egdaniel | 22281c1 | 2016-03-23 13:49:40 -0700 | [diff] [blame] | 37 | public: |
Brian Salomon | 1471df9 | 2018-06-08 10:49:00 -0400 | [diff] [blame] | 38 | using UniformInfoArray = GrVkPipelineStateDataManager::UniformInfoArray; |
| 39 | using UniformHandle = GrGLSLProgramDataManager::UniformHandle; |
| 40 | |
| 41 | GrVkPipelineState( |
| 42 | GrVkGpu* gpu, |
Greg Daniel | 3ef052c | 2021-01-05 12:20:27 -0500 | [diff] [blame] | 43 | sk_sp<const GrVkPipeline> pipeline, |
Brian Salomon | 1471df9 | 2018-06-08 10:49:00 -0400 | [diff] [blame] | 44 | const GrVkDescriptorSetManager::Handle& samplerDSHandle, |
Brian Salomon | 1471df9 | 2018-06-08 10:49:00 -0400 | [diff] [blame] | 45 | const GrGLSLBuiltinUniformHandles& builtinUniformHandles, |
| 46 | const UniformInfoArray& uniforms, |
Ethan Nicholas | 0be3480 | 2019-08-15 12:36:58 -0400 | [diff] [blame] | 47 | uint32_t uniformSize, |
Jim Van Verth | 205e99a | 2021-02-08 18:02:41 -0500 | [diff] [blame^] | 48 | bool usePushConstants, |
Greg Daniel | 7a82edf | 2018-12-04 10:54:34 -0500 | [diff] [blame] | 49 | const UniformInfoArray& samplers, |
Brian Salomon | 1471df9 | 2018-06-08 10:49:00 -0400 | [diff] [blame] | 50 | std::unique_ptr<GrGLSLPrimitiveProcessor> geometryProcessor, |
| 51 | std::unique_ptr<GrGLSLXferProcessor> xferProcessor, |
Brian Salomon | 61a70fb | 2020-07-08 19:02:54 -0400 | [diff] [blame] | 52 | std::unique_ptr<std::unique_ptr<GrGLSLFragmentProcessor>[]> fragmentProcessors); |
egdaniel | 22281c1 | 2016-03-23 13:49:40 -0700 | [diff] [blame] | 53 | |
| 54 | ~GrVkPipelineState(); |
| 55 | |
Greg Daniel | 95f0b16 | 2019-11-11 13:42:30 -0500 | [diff] [blame] | 56 | bool setAndBindUniforms(GrVkGpu*, const GrRenderTarget*, const GrProgramInfo&, |
Robert Phillips | 835cbda | 2019-10-09 15:41:50 -0400 | [diff] [blame] | 57 | GrVkCommandBuffer*); |
Brian Salomon | f723264 | 2018-09-19 08:58:08 -0400 | [diff] [blame] | 58 | /** |
| 59 | * This must be called after setAndBindUniforms() since that function invalidates texture |
| 60 | * bindings. |
| 61 | */ |
Greg Daniel | 95f0b16 | 2019-11-11 13:42:30 -0500 | [diff] [blame] | 62 | bool setAndBindTextures(GrVkGpu*, const GrPrimitiveProcessor&, const GrPipeline&, |
Michael Ludwig | fcdd061 | 2019-11-25 08:34:31 -0500 | [diff] [blame] | 63 | const GrSurfaceProxy* const primitiveProcessorTextures[], |
Brian Salomon | f723264 | 2018-09-19 08:58:08 -0400 | [diff] [blame] | 64 | GrVkCommandBuffer*); |
egdaniel | 22281c1 | 2016-03-23 13:49:40 -0700 | [diff] [blame] | 65 | |
Greg Daniel | 8cc5f19 | 2020-10-02 10:45:43 -0400 | [diff] [blame] | 66 | bool setAndBindInputAttachment(GrVkGpu*, GrVkRenderTarget* renderTarget, GrVkCommandBuffer*); |
egdaniel | 22281c1 | 2016-03-23 13:49:40 -0700 | [diff] [blame] | 67 | |
Greg Daniel | 37fd658 | 2020-09-14 12:36:09 -0400 | [diff] [blame] | 68 | void bindPipeline(const GrVkGpu* gpu, GrVkCommandBuffer* commandBuffer); |
egdaniel | 22281c1 | 2016-03-23 13:49:40 -0700 | [diff] [blame] | 69 | |
Greg Daniel | 9d02a4c | 2020-07-15 14:26:08 -0400 | [diff] [blame] | 70 | void freeGPUResources(GrVkGpu* gpu); |
egdaniel | 22281c1 | 2016-03-23 13:49:40 -0700 | [diff] [blame] | 71 | |
egdaniel | 22281c1 | 2016-03-23 13:49:40 -0700 | [diff] [blame] | 72 | private: |
egdaniel | 22281c1 | 2016-03-23 13:49:40 -0700 | [diff] [blame] | 73 | /** |
Brian Salomon | f723264 | 2018-09-19 08:58:08 -0400 | [diff] [blame] | 74 | * 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 | */ |
egdaniel | 22281c1 | 2016-03-23 13:49:40 -0700 | [diff] [blame] | 78 | 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 Nicholas | 5af9ea3 | 2017-07-28 15:19:46 -0400 | [diff] [blame] | 90 | * 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 |
egdaniel | 22281c1 | 2016-03-23 13:49:40 -0700 | [diff] [blame] | 92 | * 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 Phillips | d0fe875 | 2019-01-31 14:13:59 -0500 | [diff] [blame] | 110 | void setRenderTargetState(const GrRenderTarget*, GrSurfaceOrigin); |
egdaniel | 22281c1 | 2016-03-23 13:49:40 -0700 | [diff] [blame] | 111 | |
Jim Van Verth | 3e19216 | 2020-03-10 16:23:16 -0400 | [diff] [blame] | 112 | // GrManagedResources |
Greg Daniel | 3ef052c | 2021-01-05 12:20:27 -0500 | [diff] [blame] | 113 | sk_sp<const GrVkPipeline> fPipeline; |
egdaniel | 22281c1 | 2016-03-23 13:49:40 -0700 | [diff] [blame] | 114 | |
egdaniel | 707bbd6 | 2016-07-26 07:19:47 -0700 | [diff] [blame] | 115 | const GrVkDescriptorSetManager::Handle fSamplerDSHandle; |
egdaniel | 22281c1 | 2016-03-23 13:49:40 -0700 | [diff] [blame] | 116 | |
Greg Daniel | c10bb6a | 2019-10-29 09:50:32 -0400 | [diff] [blame] | 117 | SkSTArray<4, const GrVkSampler*> fImmutableSamplers; |
Greg Daniel | 7a82edf | 2018-12-04 10:54:34 -0500 | [diff] [blame] | 118 | |
egdaniel | 22281c1 | 2016-03-23 13:49:40 -0700 | [diff] [blame] | 119 | // Tracks the current render target uniforms stored in the vertex buffer. |
| 120 | RenderTargetState fRenderTargetState; |
Brian Salomon | 1471df9 | 2018-06-08 10:49:00 -0400 | [diff] [blame] | 121 | GrGLSLBuiltinUniformHandles fBuiltinUniformHandles; |
egdaniel | 22281c1 | 2016-03-23 13:49:40 -0700 | [diff] [blame] | 122 | |
| 123 | // Processors in the GrVkPipelineState |
Ben Wagner | 145dbcd | 2016-11-03 14:40:50 -0400 | [diff] [blame] | 124 | std::unique_ptr<GrGLSLPrimitiveProcessor> fGeometryProcessor; |
| 125 | std::unique_ptr<GrGLSLXferProcessor> fXferProcessor; |
Brian Salomon | 4d3f517 | 2018-06-07 14:42:52 -0400 | [diff] [blame] | 126 | std::unique_ptr<std::unique_ptr<GrGLSLFragmentProcessor>[]> fFragmentProcessors; |
egdaniel | 22281c1 | 2016-03-23 13:49:40 -0700 | [diff] [blame] | 127 | |
egdaniel | 22281c1 | 2016-03-23 13:49:40 -0700 | [diff] [blame] | 128 | GrVkPipelineStateDataManager fDataManager; |
| 129 | |
egdaniel | 22281c1 | 2016-03-23 13:49:40 -0700 | [diff] [blame] | 130 | int fNumSamplers; |
egdaniel | 22281c1 | 2016-03-23 13:49:40 -0700 | [diff] [blame] | 131 | }; |
| 132 | |
| 133 | #endif |