blob: 9e1decfcbb673bc0cc4e2a2377405dddf1d989b4 [file] [log] [blame]
egdanielbc9b2962016-09-27 08:00:53 -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#include "GrVkCopyManager.h"
9
Brian Salomon514baff2016-11-17 15:17:07 -050010#include "GrSamplerParams.h"
Brian Salomon94efbf52016-11-29 13:43:05 -050011#include "GrShaderCaps.h"
12#include "GrSurface.h"
egdanielbc9b2962016-09-27 08:00:53 -070013#include "GrTexturePriv.h"
14#include "GrVkCommandBuffer.h"
15#include "GrVkCopyPipeline.h"
16#include "GrVkDescriptorSet.h"
17#include "GrVkGpu.h"
18#include "GrVkImageView.h"
19#include "GrVkRenderTarget.h"
20#include "GrVkResourceProvider.h"
21#include "GrVkSampler.h"
22#include "GrVkTexture.h"
23#include "GrVkUniformBuffer.h"
24#include "GrVkVertexBuffer.h"
25#include "SkPoint.h"
26#include "SkRect.h"
Ryan Macnak38a10ad2017-07-10 10:36:34 -070027#include "SkTraceEvent.h"
egdanielbc9b2962016-09-27 08:00:53 -070028
Greg Danielf9f27232017-01-06 14:40:08 -050029GrVkCopyManager::GrVkCopyManager()
30 : fVertShaderModule(VK_NULL_HANDLE)
31 , fFragShaderModule(VK_NULL_HANDLE)
32 , fPipelineLayout(VK_NULL_HANDLE) {}
33
34GrVkCopyManager::~GrVkCopyManager() {}
35
egdanielbc9b2962016-09-27 08:00:53 -070036bool GrVkCopyManager::createCopyProgram(GrVkGpu* gpu) {
Brian Osman39c08ac2017-07-26 09:36:09 -040037 TRACE_EVENT0("skia", TRACE_FUNC);
Ryan Macnak38a10ad2017-07-10 10:36:34 -070038
Brian Salomon1edc5b92016-11-29 13:43:46 -050039 const GrShaderCaps* shaderCaps = gpu->caps()->shaderCaps();
40 const char* version = shaderCaps->versionDeclString();
egdanielbc9b2962016-09-27 08:00:53 -070041 SkString vertShaderText(version);
42 vertShaderText.append(
43 "#extension GL_ARB_separate_shader_objects : enable\n"
44 "#extension GL_ARB_shading_language_420pack : enable\n"
45
46 "layout(set = 0, binding = 0) uniform vertexUniformBuffer {"
Brian Salomon1d816b92017-08-17 11:07:59 -040047 "mediump float4 uPosXform;"
48 "mediump float4 uTexCoordXform;"
egdanielbc9b2962016-09-27 08:00:53 -070049 "};"
Brian Salomon1d816b92017-08-17 11:07:59 -040050 "layout(location = 0) in highp float2 inPosition;"
51 "layout(location = 1) out mediump float2 vTexCoord;"
egdanielbc9b2962016-09-27 08:00:53 -070052
53 "// Copy Program VS\n"
54 "void main() {"
55 "vTexCoord = inPosition * uTexCoordXform.xy + uTexCoordXform.zw;"
56 "gl_Position.xy = inPosition * uPosXform.xy + uPosXform.zw;"
Brian Salomon1d816b92017-08-17 11:07:59 -040057 "gl_Position.zw = float2(0, 1);"
egdanielbc9b2962016-09-27 08:00:53 -070058 "}"
59 );
60
61 SkString fragShaderText(version);
62 fragShaderText.append(
63 "#extension GL_ARB_separate_shader_objects : enable\n"
64 "#extension GL_ARB_shading_language_420pack : enable\n"
65
Brian Salomon1d816b92017-08-17 11:07:59 -040066 "precision mediump float;"
67
68 "layout(set = 1, binding = 0) uniform mediump sampler2D uTextureSampler;"
69 "layout(location = 1) in mediump float2 vTexCoord;"
70 "layout(location = 0, index = 0) out mediump float4 fsColorOut;"
egdanielbc9b2962016-09-27 08:00:53 -070071
72 "// Copy Program FS\n"
73 "void main() {"
74 "fsColorOut = texture(uTextureSampler, vTexCoord);"
75 "}"
76 );
77
Ethan Nicholas941e7e22016-12-12 15:33:30 -050078 SkSL::Program::Settings settings;
79 SkSL::Program::Inputs inputs;
80 if (!GrCompileVkShaderModule(gpu, vertShaderText.c_str(), VK_SHADER_STAGE_VERTEX_BIT,
81 &fVertShaderModule, &fShaderStageInfo[0], settings, &inputs)) {
egdanielbc9b2962016-09-27 08:00:53 -070082 this->destroyResources(gpu);
83 return false;
84 }
Ethan Nicholas941e7e22016-12-12 15:33:30 -050085 SkASSERT(inputs.isEmpty());
egdanielbc9b2962016-09-27 08:00:53 -070086
Ethan Nicholas941e7e22016-12-12 15:33:30 -050087 if (!GrCompileVkShaderModule(gpu, fragShaderText.c_str(), VK_SHADER_STAGE_FRAGMENT_BIT,
88 &fFragShaderModule, &fShaderStageInfo[1], settings, &inputs)) {
egdanielbc9b2962016-09-27 08:00:53 -070089 this->destroyResources(gpu);
90 return false;
91 }
Ethan Nicholas941e7e22016-12-12 15:33:30 -050092 SkASSERT(inputs.isEmpty());
egdanielbc9b2962016-09-27 08:00:53 -070093
94 VkDescriptorSetLayout dsLayout[2];
95
96 GrVkResourceProvider& resourceProvider = gpu->resourceProvider();
97
98 dsLayout[GrVkUniformHandler::kUniformBufferDescSet] = resourceProvider.getUniformDSLayout();
99
100 uint32_t samplerVisibility = kFragment_GrShaderFlag;
101 SkTArray<uint32_t> visibilityArray(&samplerVisibility, 1);
102
Greg Daniela7543782017-05-02 14:01:43 -0400103 resourceProvider.getSamplerDescriptorSetHandle(VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
104 visibilityArray, &fSamplerDSHandle);
egdanielbc9b2962016-09-27 08:00:53 -0700105 dsLayout[GrVkUniformHandler::kSamplerDescSet] =
106 resourceProvider.getSamplerDSLayout(fSamplerDSHandle);
107
108 // Create the VkPipelineLayout
109 VkPipelineLayoutCreateInfo layoutCreateInfo;
110 memset(&layoutCreateInfo, 0, sizeof(VkPipelineLayoutCreateFlags));
111 layoutCreateInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO;
112 layoutCreateInfo.pNext = 0;
113 layoutCreateInfo.flags = 0;
114 layoutCreateInfo.setLayoutCount = 2;
115 layoutCreateInfo.pSetLayouts = dsLayout;
116 layoutCreateInfo.pushConstantRangeCount = 0;
117 layoutCreateInfo.pPushConstantRanges = nullptr;
118
119 VkResult err = GR_VK_CALL(gpu->vkInterface(), CreatePipelineLayout(gpu->device(),
120 &layoutCreateInfo,
121 nullptr,
122 &fPipelineLayout));
123 if (err) {
124 this->destroyResources(gpu);
125 return false;
126 }
127
128 static const float vdata[] = {
129 0, 0,
130 0, 1,
131 1, 0,
132 1, 1
133 };
134 fVertexBuffer.reset(GrVkVertexBuffer::Create(gpu, sizeof(vdata), false));
135 SkASSERT(fVertexBuffer.get());
136 fVertexBuffer->updateData(vdata, sizeof(vdata));
137
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400138 // We use 2 float4's for uniforms
Greg Danielf9f27232017-01-06 14:40:08 -0500139 fUniformBuffer.reset(GrVkUniformBuffer::Create(gpu, 8 * sizeof(float)));
140 SkASSERT(fUniformBuffer.get());
egdanielbc9b2962016-09-27 08:00:53 -0700141
142 return true;
143}
144
145bool GrVkCopyManager::copySurfaceAsDraw(GrVkGpu* gpu,
Robert Phillipsb0e93a22017-08-29 08:26:54 -0400146 GrSurface* dst, GrSurfaceOrigin dstOrigin,
147 GrSurface* src, GrSurfaceOrigin srcOrigin,
148 const SkIRect& srcRect, const SkIPoint& dstPoint) {
Greg Daniel6c9f1012017-05-11 09:20:59 -0400149 // None of our copy methods can handle a swizzle. TODO: Make copySurfaceAsDraw handle the
150 // swizzle.
151 if (gpu->caps()->shaderCaps()->configOutputSwizzle(src->config()) !=
152 gpu->caps()->shaderCaps()->configOutputSwizzle(dst->config())) {
153 return false;
154 }
155
egdanielbc9b2962016-09-27 08:00:53 -0700156 if (!gpu->vkCaps().supportsCopiesAsDraws()) {
157 return false;
158 }
159
Greg Daniele3cd6912017-05-17 11:15:55 -0400160 if (gpu->vkCaps().newCBOnPipelineChange()) {
161 // We bind a new pipeline here for the copy so we must start a new command buffer.
Greg Daniel51316782017-08-02 15:10:09 +0000162 gpu->finishFlush(0, nullptr);
Greg Daniele3cd6912017-05-17 11:15:55 -0400163 }
164
egdanielbc9b2962016-09-27 08:00:53 -0700165 GrVkRenderTarget* rt = static_cast<GrVkRenderTarget*>(dst->asRenderTarget());
166 if (!rt) {
167 return false;
168 }
169
170 GrVkTexture* srcTex = static_cast<GrVkTexture*>(src->asTexture());
171 if (!srcTex) {
172 return false;
173 }
174
175 if (VK_NULL_HANDLE == fVertShaderModule) {
176 SkASSERT(VK_NULL_HANDLE == fFragShaderModule &&
177 VK_NULL_HANDLE == fPipelineLayout &&
178 nullptr == fVertexBuffer.get() &&
Greg Danielf9f27232017-01-06 14:40:08 -0500179 nullptr == fUniformBuffer.get());
egdanielbc9b2962016-09-27 08:00:53 -0700180 if (!this->createCopyProgram(gpu)) {
181 SkDebugf("Failed to create copy program.\n");
182 return false;
183 }
184 }
185
186 GrVkResourceProvider& resourceProv = gpu->resourceProvider();
187
188 GrVkCopyPipeline* pipeline = resourceProv.findOrCreateCopyPipeline(rt,
189 fShaderStageInfo,
190 fPipelineLayout);
191 if (!pipeline) {
192 return false;
193 }
194
195 // UPDATE UNIFORM DESCRIPTOR SET
196 int w = srcRect.width();
197 int h = srcRect.height();
198
199 // dst rect edges in NDC (-1 to 1)
200 int dw = dst->width();
201 int dh = dst->height();
202 float dx0 = 2.f * dstPoint.fX / dw - 1.f;
203 float dx1 = 2.f * (dstPoint.fX + w) / dw - 1.f;
204 float dy0 = 2.f * dstPoint.fY / dh - 1.f;
205 float dy1 = 2.f * (dstPoint.fY + h) / dh - 1.f;
Robert Phillipsb0e93a22017-08-29 08:26:54 -0400206 if (kBottomLeft_GrSurfaceOrigin == dstOrigin) {
egdanielbc9b2962016-09-27 08:00:53 -0700207 dy0 = -dy0;
208 dy1 = -dy1;
209 }
210
211
212 float sx0 = (float)srcRect.fLeft;
213 float sx1 = (float)(srcRect.fLeft + w);
214 float sy0 = (float)srcRect.fTop;
215 float sy1 = (float)(srcRect.fTop + h);
216 int sh = src->height();
Robert Phillipsb0e93a22017-08-29 08:26:54 -0400217 if (kBottomLeft_GrSurfaceOrigin == srcOrigin) {
egdanielbc9b2962016-09-27 08:00:53 -0700218 sy0 = sh - sy0;
219 sy1 = sh - sy1;
220 }
221 // src rect edges in normalized texture space (0 to 1).
222 int sw = src->width();
223 sx0 /= sw;
224 sx1 /= sw;
225 sy0 /= sh;
226 sy1 /= sh;
227
228 float uniData[] = { dx1 - dx0, dy1 - dy0, dx0, dy0, // posXform
229 sx1 - sx0, sy1 - sy0, sx0, sy0 }; // texCoordXform
230
231 fUniformBuffer->updateData(gpu, uniData, sizeof(uniData), nullptr);
232
233 const GrVkDescriptorSet* uniformDS = resourceProv.getUniformDescriptorSet();
234 SkASSERT(uniformDS);
235
236 VkDescriptorBufferInfo uniBufferInfo;
237 uniBufferInfo.buffer = fUniformBuffer->buffer();
238 uniBufferInfo.offset = fUniformBuffer->offset();
239 uniBufferInfo.range = fUniformBuffer->size();
240
241 VkWriteDescriptorSet descriptorWrites;
242 descriptorWrites.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
243 descriptorWrites.pNext = nullptr;
244 descriptorWrites.dstSet = uniformDS->descriptorSet();
Greg Daniel18f96022017-05-04 15:09:03 -0400245 descriptorWrites.dstBinding = GrVkUniformHandler::kGeometryBinding;
egdanielbc9b2962016-09-27 08:00:53 -0700246 descriptorWrites.dstArrayElement = 0;
247 descriptorWrites.descriptorCount = 1;
248 descriptorWrites.descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
249 descriptorWrites.pImageInfo = nullptr;
250 descriptorWrites.pBufferInfo = &uniBufferInfo;
251 descriptorWrites.pTexelBufferView = nullptr;
252
253 GR_VK_CALL(gpu->vkInterface(), UpdateDescriptorSets(gpu->device(),
254 1,
255 &descriptorWrites,
256 0, nullptr));
257
258 // UPDATE SAMPLER DESCRIPTOR SET
259 const GrVkDescriptorSet* samplerDS =
260 gpu->resourceProvider().getSamplerDescriptorSet(fSamplerDSHandle);
261
Brian Salomon514baff2016-11-17 15:17:07 -0500262 GrSamplerParams params(SkShader::kClamp_TileMode, GrSamplerParams::kNone_FilterMode);
egdanielbc9b2962016-09-27 08:00:53 -0700263
264 GrVkSampler* sampler =
265 resourceProv.findOrCreateCompatibleSampler(params, srcTex->texturePriv().maxMipMapLevel());
266
267 VkDescriptorImageInfo imageInfo;
268 memset(&imageInfo, 0, sizeof(VkDescriptorImageInfo));
269 imageInfo.sampler = sampler->sampler();
270 imageInfo.imageView = srcTex->textureView(true)->imageView();
271 imageInfo.imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
272
273 VkWriteDescriptorSet writeInfo;
274 memset(&writeInfo, 0, sizeof(VkWriteDescriptorSet));
275 writeInfo.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
276 writeInfo.pNext = nullptr;
277 writeInfo.dstSet = samplerDS->descriptorSet();
278 writeInfo.dstBinding = 0;
279 writeInfo.dstArrayElement = 0;
280 writeInfo.descriptorCount = 1;
281 writeInfo.descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
282 writeInfo.pImageInfo = &imageInfo;
283 writeInfo.pBufferInfo = nullptr;
284 writeInfo.pTexelBufferView = nullptr;
285
286 GR_VK_CALL(gpu->vkInterface(), UpdateDescriptorSets(gpu->device(),
287 1,
288 &writeInfo,
289 0, nullptr));
290
291 VkDescriptorSet vkDescSets[] = { uniformDS->descriptorSet(), samplerDS->descriptorSet() };
292
293 GrVkRenderTarget* texRT = static_cast<GrVkRenderTarget*>(srcTex->asRenderTarget());
294 if (texRT) {
Robert Phillipsb0e93a22017-08-29 08:26:54 -0400295 gpu->onResolveRenderTarget(texRT, srcOrigin);
egdanielbc9b2962016-09-27 08:00:53 -0700296 }
297
298 GrVkPrimaryCommandBuffer* cmdBuffer = gpu->currentCommandBuffer();
299
300 // TODO: Make tighter bounds and then adjust bounds for origin and granularity if we see
301 // any perf issues with using the whole bounds
302 SkIRect bounds = SkIRect::MakeWH(rt->width(), rt->height());
303
304 // Change layouts of rt and texture
305 GrVkImage* targetImage = rt->msaaImage() ? rt->msaaImage() : rt;
306 targetImage->setImageLayout(gpu,
307 VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
308 VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT,
309 VK_PIPELINE_STAGE_ALL_GRAPHICS_BIT,
310 false);
311
312 srcTex->setImageLayout(gpu,
313 VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL,
314 VK_ACCESS_SHADER_READ_BIT,
315 VK_PIPELINE_STAGE_ALL_GRAPHICS_BIT,
316 false);
317
318 GrVkRenderPass::LoadStoreOps vkColorOps(VK_ATTACHMENT_LOAD_OP_DONT_CARE,
319 VK_ATTACHMENT_STORE_OP_STORE);
Greg Danielac95c5f2017-01-30 16:33:57 -0500320 GrVkRenderPass::LoadStoreOps vkStencilOps(VK_ATTACHMENT_LOAD_OP_LOAD,
egdanielbc9b2962016-09-27 08:00:53 -0700321 VK_ATTACHMENT_STORE_OP_STORE);
egdanielbc9b2962016-09-27 08:00:53 -0700322 const GrVkRenderPass* renderPass;
323 const GrVkResourceProvider::CompatibleRPHandle& rpHandle =
324 rt->compatibleRenderPassHandle();
325 if (rpHandle.isValid()) {
326 renderPass = gpu->resourceProvider().findRenderPass(rpHandle,
327 vkColorOps,
egdanielbc9b2962016-09-27 08:00:53 -0700328 vkStencilOps);
329 } else {
330 renderPass = gpu->resourceProvider().findRenderPass(*rt,
331 vkColorOps,
egdanielbc9b2962016-09-27 08:00:53 -0700332 vkStencilOps);
333 }
334
335 SkASSERT(renderPass->isCompatible(*rt->simpleRenderPass()));
336
337
Greg Daniel77a86f82017-01-23 11:04:45 -0500338 cmdBuffer->beginRenderPass(gpu, renderPass, nullptr, *rt, bounds, false);
egdanielbc9b2962016-09-27 08:00:53 -0700339 cmdBuffer->bindPipeline(gpu, pipeline);
340
341 // Uniform DescriptorSet, Sampler DescriptorSet, and vertex shader uniformBuffer
342 SkSTArray<3, const GrVkRecycledResource*> descriptorRecycledResources;
343 descriptorRecycledResources.push_back(uniformDS);
344 descriptorRecycledResources.push_back(samplerDS);
345 descriptorRecycledResources.push_back(fUniformBuffer->resource());
346
347 // One sampler, texture view, and texture
348 SkSTArray<3, const GrVkResource*> descriptorResources;
349 descriptorResources.push_back(sampler);
350 descriptorResources.push_back(srcTex->textureView(true));
351 descriptorResources.push_back(srcTex->resource());
352
353 cmdBuffer->bindDescriptorSets(gpu,
354 descriptorRecycledResources,
355 descriptorResources,
356 fPipelineLayout,
357 0,
358 2,
359 vkDescSets,
360 0,
361 nullptr);
362
363 // Set Dynamic viewport and stencil
364 // We always use one viewport the size of the RT
365 VkViewport viewport;
366 viewport.x = 0.0f;
367 viewport.y = 0.0f;
368 viewport.width = SkIntToScalar(rt->width());
369 viewport.height = SkIntToScalar(rt->height());
370 viewport.minDepth = 0.0f;
371 viewport.maxDepth = 1.0f;
372 cmdBuffer->setViewport(gpu, 0, 1, &viewport);
373
374 // We assume the scissor is not enabled so just set it to the whole RT
375 VkRect2D scissor;
376 scissor.extent.width = rt->width();
377 scissor.extent.height = rt->height();
378 scissor.offset.x = 0;
379 scissor.offset.y = 0;
380 cmdBuffer->setScissor(gpu, 0, 1, &scissor);
381
Chris Dalton1d616352017-05-31 12:51:23 -0600382 cmdBuffer->bindInputBuffer(gpu, 0, fVertexBuffer.get());
egdanielbc9b2962016-09-27 08:00:53 -0700383 cmdBuffer->draw(gpu, 4, 1, 0, 0);
384 cmdBuffer->endRenderPass(gpu);
385
386 // Release all temp resources which should now be reffed by the cmd buffer
387 pipeline->unref(gpu);
388 uniformDS->unref(gpu);
389 samplerDS->unref(gpu);
390 sampler->unref(gpu);
391 renderPass->unref(gpu);
392
393 return true;
394}
395
396void GrVkCopyManager::destroyResources(GrVkGpu* gpu) {
397 if (VK_NULL_HANDLE != fVertShaderModule) {
398 GR_VK_CALL(gpu->vkInterface(), DestroyShaderModule(gpu->device(), fVertShaderModule,
399 nullptr));
400 fVertShaderModule = VK_NULL_HANDLE;
401 }
402
403 if (VK_NULL_HANDLE != fFragShaderModule) {
404 GR_VK_CALL(gpu->vkInterface(), DestroyShaderModule(gpu->device(), fFragShaderModule,
405 nullptr));
406 fFragShaderModule = VK_NULL_HANDLE;
407 }
408
409 if (VK_NULL_HANDLE != fPipelineLayout) {
410 GR_VK_CALL(gpu->vkInterface(), DestroyPipelineLayout(gpu->device(), fPipelineLayout,
411 nullptr));
412 fPipelineLayout = VK_NULL_HANDLE;
413 }
414
415 if (fUniformBuffer) {
416 fUniformBuffer->release(gpu);
Greg Danielf9f27232017-01-06 14:40:08 -0500417 fUniformBuffer.reset();
egdanielbc9b2962016-09-27 08:00:53 -0700418 }
419}
420
421void GrVkCopyManager::abandonResources() {
422 fVertShaderModule = VK_NULL_HANDLE;
423 fFragShaderModule = VK_NULL_HANDLE;
424 fPipelineLayout = VK_NULL_HANDLE;
425
426 if (fUniformBuffer) {
427 fUniformBuffer->abandon();
Greg Danielf9f27232017-01-06 14:40:08 -0500428 fUniformBuffer.reset();
egdanielbc9b2962016-09-27 08:00:53 -0700429 }
430}