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