Debian Codegraph @

src/gpu/ganesh/vk/GrVkMSAALoadManager.cpp

raw
1 /*
2 * Copyright 2020 Google LLC
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 #include "src/gpu/ganesh/vk/GrVkMSAALoadManager.h"
9
10 #include "include/core/SkRect.h"
11 #include "include/core/SkRefCnt.h"
12 #include "include/core/SkScalar.h"
13 #include "include/gpu/ganesh/GrDirectContext.h"
14 #include "include/private/base/SkAssert.h"
15 #include "include/private/base/SkDebug.h"
16 #include "include/private/gpu/ganesh/GrTypesPriv.h"
17 #include "src/core/SkTraceEvent.h"
18 #include "src/gpu/GpuRefCnt.h"
19 #include "src/gpu/ganesh/GrAttachment.h"
20 #include "src/gpu/ganesh/GrBuffer.h"
21 #include "src/gpu/ganesh/GrDirectContextPriv.h"
22 #include "src/gpu/ganesh/GrManagedResource.h"
23 #include "src/gpu/ganesh/GrResourceProvider.h"
24 #include "src/gpu/ganesh/vk/GrVkBuffer.h"
25 #include "src/gpu/ganesh/vk/GrVkCommandBuffer.h"
26 #include "src/gpu/ganesh/vk/GrVkDescriptorSet.h"
27 #include "src/gpu/ganesh/vk/GrVkDescriptorSetManager.h"
28 #include "src/gpu/ganesh/vk/GrVkGpu.h"
29 #include "src/gpu/ganesh/vk/GrVkImage.h"
30 #include "src/gpu/ganesh/vk/GrVkPipeline.h"
31 #include "src/gpu/ganesh/vk/GrVkResourceProvider.h"
32 #include "src/gpu/ganesh/vk/GrVkUniformHandler.h"
33 #include "src/gpu/ganesh/vk/GrVkUtil.h"
34 #include "src/sksl/SkSLProgramSettings.h"
35 #include "src/sksl/codegen/SkSLNativeShader.h"
36 #include "src/sksl/ir/SkSLProgram.h"
37
38 #include <string.h>
39 #include <cstdint>
40 #include <string>
41 #include <utility>
42
43 class GrGpuBuffer;
44
45 GrVkMSAALoadManager::GrVkMSAALoadManager()
46 : fVertShaderModule(VK_NULL_HANDLE)
47 , fFragShaderModule(VK_NULL_HANDLE)
48 , fPipelineLayout(VK_NULL_HANDLE) {}
49
50 GrVkMSAALoadManager::~GrVkMSAALoadManager() {}
51
52 bool GrVkMSAALoadManager::createMSAALoadProgram(GrVkGpu* gpu) {
53 TRACE_EVENT0("skia", TRACE_FUNC);
54
55 std::string vertShaderText;
56 vertShaderText.append(
57 "layout(vulkan, set=0, binding=0) uniform vertexUniformBuffer {"
58 "half4 uPosXform;"
59 "};"
60
61 // MSAA Load Program VS
62 "void main() {"
63 "float2 position = float2(sk_VertexID >> 1, sk_VertexID & 1);"
64 "sk_Position.xy = position * uPosXform.xy + uPosXform.zw;"
65 "sk_Position.zw = half2(0, 1);"
66 "}");
67
68 std::string fragShaderText;
69 fragShaderText.append(
70 "layout(vulkan, input_attachment_index=0, set=2, binding=0) subpassInput uInput;"
71
72 // MSAA Load Program FS
73 "void main() {"
74 "sk_FragColor = subpassLoad(uInput);"
75 "}");
76
77 SkSL::ProgramSettings settings;
78 SkSL::NativeShader spirv;
79 SkSL::Program::Interface interface;
80 if (!GrCompileVkShaderModule(gpu, vertShaderText, VK_SHADER_STAGE_VERTEX_BIT,
81 &fVertShaderModule, &fShaderStageInfo[0], settings, &spirv,
82 &interface)) {
83 this->destroyResources(gpu);
84 return false;
85 }
86 SkASSERT(interface == SkSL::Program::Interface());
87
88 if (!GrCompileVkShaderModule(gpu, fragShaderText, VK_SHADER_STAGE_FRAGMENT_BIT,
89 &fFragShaderModule, &fShaderStageInfo[1], settings, &spirv,
90 &interface)) {
91 this->destroyResources(gpu);
92 return false;
93 }
94 SkASSERT(interface == SkSL::Program::Interface());
95
96 VkDescriptorSetLayout dsLayout[GrVkUniformHandler::kDescSetCount];
97
98 GrVkResourceProvider& resourceProvider = gpu->resourceProvider();
99
100 dsLayout[GrVkUniformHandler::kUniformBufferDescSet] = resourceProvider.getUniformDSLayout();
101
102 // Even though we don't have a sampler we need to put a valid handle here (of zero samplers)
103 // since we set up our descriptor layout to be uniform, sampler, input.
104 //
105 // TODO: We should have a more general way for different pipelines to describe their descriptor
106 // layouts so that we don't have to use the compile time constants for the sets.
107 GrVkDescriptorSetManager::Handle samplerHandle;
108 resourceProvider.getZeroSamplerDescriptorSetHandle(&samplerHandle);
109
110 dsLayout[GrVkUniformHandler::kSamplerDescSet] =
111 resourceProvider.getSamplerDSLayout(samplerHandle);
112
113 dsLayout[GrVkUniformHandler::kInputDescSet] = resourceProvider.getInputDSLayout();
114
115 // Create the VkPipelineLayout
116 VkPipelineLayoutCreateInfo layoutCreateInfo;
117 memset(&layoutCreateInfo, 0, sizeof(VkPipelineLayoutCreateFlags));
118 layoutCreateInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO;
119 layoutCreateInfo.pNext = nullptr;
120 layoutCreateInfo.flags = 0;
121 layoutCreateInfo.setLayoutCount = GrVkUniformHandler::kDescSetCount;
122 layoutCreateInfo.pSetLayouts = dsLayout;
123 layoutCreateInfo.pushConstantRangeCount = 0;
124 layoutCreateInfo.pPushConstantRanges = nullptr;
125
126 VkResult err;
127 GR_VK_CALL_RESULT(gpu, err, CreatePipelineLayout(
128 gpu->device(), &layoutCreateInfo, nullptr, &fPipelineLayout));
129 if (err) {
130 this->destroyResources(gpu);
131 return false;
132 }
133
134 return true;
135 }
136
137 bool GrVkMSAALoadManager::loadMSAAFromResolve(GrVkGpu* gpu,
138 GrVkCommandBuffer* commandBuffer,
139 const GrVkRenderPass& renderPass,
140 GrAttachment* dst,
141 GrVkImage* src,
142 const SkIRect& rect) {
143 if (!dst) {
144 return false;
145 }
146 if (!src || !src->supportsInputAttachmentUsage()) {
147 return false;
148 }
149
150 if (VK_NULL_HANDLE == fVertShaderModule) {
151 SkASSERT(fFragShaderModule == VK_NULL_HANDLE && fPipelineLayout == VK_NULL_HANDLE);
152 if (!this->createMSAALoadProgram(gpu)) {
153 SkDebugf("Failed to create copy program.\n");
154 return false;
155 }
156 }
157 SkASSERT(fPipelineLayout != VK_NULL_HANDLE);
158
159 GrVkResourceProvider& resourceProv = gpu->resourceProvider();
160
161 sk_sp<const GrVkPipeline> pipeline =
162 resourceProv.findOrCreateMSAALoadPipeline(renderPass, dst->numSamples(),
163 fShaderStageInfo, fPipelineLayout);
164 if (!pipeline) {
165 return false;
166 }
167 commandBuffer->bindPipeline(gpu, std::move(pipeline));
168
169 // Set Dynamic viewport and stencil
170 // We always use one viewport the size of the RT
171 VkViewport viewport;
172 viewport.x = 0.0f;
173 viewport.y = 0.0f;
174 viewport.width = SkIntToScalar(dst->width());
175 viewport.height = SkIntToScalar(dst->height());
176 viewport.minDepth = 0.0f;
177 viewport.maxDepth = 1.0f;
178 commandBuffer->setViewport(gpu, 0, 1, &viewport);
179
180 // We assume the scissor is not enabled so just set it to the whole RT
181 VkRect2D scissor;
182 scissor.extent.width = dst->width();
183 scissor.extent.height = dst->height();
184 scissor.offset.x = 0;
185 scissor.offset.y = 0;
186 commandBuffer->setScissor(gpu, 0, 1, &scissor);
187
188 // Update and bind uniform descriptor set
189 int w = rect.width();
190 int h = rect.height();
191
192 // dst rect edges in NDC (-1 to 1)
193 int dw = dst->width();
194 int dh = dst->height();
195 float dx0 = 2.f * rect.fLeft / dw - 1.f;
196 float dx1 = 2.f * (rect.fLeft + w) / dw - 1.f;
197 float dy0 = 2.f * rect.fTop / dh - 1.f;
198 float dy1 = 2.f * (rect.fTop + h) / dh - 1.f;
199
200 float uniData[] = {dx1 - dx0, dy1 - dy0, dx0, dy0}; // posXform
201
202 GrResourceProvider* resourceProvider = gpu->getContext()->priv().resourceProvider();
203 // TODO: Is it worth holding onto the last used uniform buffer and tracking the width, height,
204 // dst width, and dst height so that we can use the buffer again without having to update the
205 // data?
206 sk_sp<GrGpuBuffer> uniformBuffer = resourceProvider->createBuffer(uniData,
207 sizeof(uniData),
208 GrGpuBufferType::kUniform,
209 kDynamic_GrAccessPattern);
210 if (!uniformBuffer) {
211 return false;
212 }
213 GrVkBuffer* vkUniformBuffer = static_cast<GrVkBuffer*>(uniformBuffer.get());
214 static_assert(GrVkUniformHandler::kUniformBufferDescSet < GrVkUniformHandler::kInputDescSet);
215 commandBuffer->bindDescriptorSets(gpu, fPipelineLayout,
216 GrVkUniformHandler::kUniformBufferDescSet,
217 /*setCount=*/1, vkUniformBuffer->uniformDescriptorSet(),
218 /*dynamicOffsetCount=*/0, /*dynamicOffsets=*/nullptr);
219 commandBuffer->addGrBuffer(std::move(uniformBuffer));
220
221 // Update the input descriptor set
222 gr_rp<const GrVkDescriptorSet> inputDS = src->inputDescSetForMSAALoad(gpu);
223 if (!inputDS) {
224 return false;
225 }
226 commandBuffer->bindDescriptorSets(gpu, fPipelineLayout,
227 GrVkUniformHandler::kInputDescSet, /*setCount=*/1,
228 inputDS->descriptorSet(),
229 /*dynamicOffsetCount=*/0, /*dynamicOffsets=*/nullptr);
230
231 // We don't need to add the src and dst resources here since those are all tracked by the main
232 // render pass code out in GrVkOpsRenderPass and GrVkRenderTarget::adResources.
233 commandBuffer->addRecycledResource(std::move(inputDS));
234
235 commandBuffer->draw(gpu, 4, 1, 0, 0);
236
237 return true;
238 }
239
240 void GrVkMSAALoadManager::destroyResources(GrVkGpu* gpu) {
241 if (fVertShaderModule != VK_NULL_HANDLE) {
242 GR_VK_CALL(gpu->vkInterface(),
243 DestroyShaderModule(gpu->device(), fVertShaderModule, nullptr));
244 fVertShaderModule = VK_NULL_HANDLE;
245 }
246
247 if (fFragShaderModule != VK_NULL_HANDLE) {
248 GR_VK_CALL(gpu->vkInterface(),
249 DestroyShaderModule(gpu->device(), fFragShaderModule, nullptr));
250 fFragShaderModule = VK_NULL_HANDLE;
251 }
252
253 if (fPipelineLayout != VK_NULL_HANDLE) {
254 GR_VK_CALL(gpu->vkInterface(),
255 DestroyPipelineLayout(gpu->device(), fPipelineLayout, nullptr));
256 fPipelineLayout = VK_NULL_HANDLE;
257 }
258 }