egdaniel | bc9b296 | 2016-09-27 08:00:53 -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 | #include "GrVkCopyManager.h" |
| 9 | |
Greg Daniel | 2b41784 | 2018-03-19 11:17:13 -0400 | [diff] [blame] | 10 | #include "GrRenderTargetPriv.h" |
Brian Salomon | 2bbdcc4 | 2017-09-07 12:36:34 -0400 | [diff] [blame] | 11 | #include "GrSamplerState.h" |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 12 | #include "GrShaderCaps.h" |
| 13 | #include "GrSurface.h" |
egdaniel | bc9b296 | 2016-09-27 08:00:53 -0700 | [diff] [blame] | 14 | #include "GrTexturePriv.h" |
| 15 | #include "GrVkCommandBuffer.h" |
| 16 | #include "GrVkCopyPipeline.h" |
| 17 | #include "GrVkDescriptorSet.h" |
| 18 | #include "GrVkGpu.h" |
| 19 | #include "GrVkImageView.h" |
Greg Daniel | 7d918fd | 2018-06-19 15:22:01 -0400 | [diff] [blame] | 20 | #include "GrVkPipelineLayout.h" |
egdaniel | bc9b296 | 2016-09-27 08:00:53 -0700 | [diff] [blame] | 21 | #include "GrVkRenderTarget.h" |
| 22 | #include "GrVkResourceProvider.h" |
| 23 | #include "GrVkSampler.h" |
| 24 | #include "GrVkTexture.h" |
| 25 | #include "GrVkUniformBuffer.h" |
| 26 | #include "GrVkVertexBuffer.h" |
| 27 | #include "SkPoint.h" |
| 28 | #include "SkRect.h" |
Ryan Macnak | 38a10ad | 2017-07-10 10:36:34 -0700 | [diff] [blame] | 29 | #include "SkTraceEvent.h" |
egdaniel | bc9b296 | 2016-09-27 08:00:53 -0700 | [diff] [blame] | 30 | |
Greg Daniel | f9f2723 | 2017-01-06 14:40:08 -0500 | [diff] [blame] | 31 | GrVkCopyManager::GrVkCopyManager() |
| 32 | : fVertShaderModule(VK_NULL_HANDLE) |
| 33 | , fFragShaderModule(VK_NULL_HANDLE) |
Greg Daniel | 7d918fd | 2018-06-19 15:22:01 -0400 | [diff] [blame] | 34 | , fPipelineLayout(nullptr) {} |
Greg Daniel | f9f2723 | 2017-01-06 14:40:08 -0500 | [diff] [blame] | 35 | |
| 36 | GrVkCopyManager::~GrVkCopyManager() {} |
| 37 | |
egdaniel | bc9b296 | 2016-09-27 08:00:53 -0700 | [diff] [blame] | 38 | bool GrVkCopyManager::createCopyProgram(GrVkGpu* gpu) { |
Brian Osman | 39c08ac | 2017-07-26 09:36:09 -0400 | [diff] [blame] | 39 | TRACE_EVENT0("skia", TRACE_FUNC); |
Ryan Macnak | 38a10ad | 2017-07-10 10:36:34 -0700 | [diff] [blame] | 40 | |
Brian Salomon | 1edc5b9 | 2016-11-29 13:43:46 -0500 | [diff] [blame] | 41 | const GrShaderCaps* shaderCaps = gpu->caps()->shaderCaps(); |
| 42 | const char* version = shaderCaps->versionDeclString(); |
egdaniel | bc9b296 | 2016-09-27 08:00:53 -0700 | [diff] [blame] | 43 | SkString vertShaderText(version); |
| 44 | vertShaderText.append( |
| 45 | "#extension GL_ARB_separate_shader_objects : enable\n" |
| 46 | "#extension GL_ARB_shading_language_420pack : enable\n" |
| 47 | |
| 48 | "layout(set = 0, binding = 0) uniform vertexUniformBuffer {" |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 49 | "half4 uPosXform;" |
| 50 | "half4 uTexCoordXform;" |
egdaniel | bc9b296 | 2016-09-27 08:00:53 -0700 | [diff] [blame] | 51 | "};" |
Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 52 | "layout(location = 0) in float2 inPosition;" |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 53 | "layout(location = 1) out half2 vTexCoord;" |
egdaniel | bc9b296 | 2016-09-27 08:00:53 -0700 | [diff] [blame] | 54 | |
| 55 | "// Copy Program VS\n" |
| 56 | "void main() {" |
| 57 | "vTexCoord = inPosition * uTexCoordXform.xy + uTexCoordXform.zw;" |
Ethan Nicholas | bed683a | 2017-09-26 14:23:59 -0400 | [diff] [blame] | 58 | "sk_Position.xy = inPosition * uPosXform.xy + uPosXform.zw;" |
| 59 | "sk_Position.zw = half2(0, 1);" |
egdaniel | bc9b296 | 2016-09-27 08:00:53 -0700 | [diff] [blame] | 60 | "}" |
| 61 | ); |
| 62 | |
| 63 | SkString fragShaderText(version); |
| 64 | fragShaderText.append( |
| 65 | "#extension GL_ARB_separate_shader_objects : enable\n" |
| 66 | "#extension GL_ARB_shading_language_420pack : enable\n" |
| 67 | |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 68 | "layout(set = 1, binding = 0) uniform sampler2D uTextureSampler;" |
| 69 | "layout(location = 1) in half2 vTexCoord;" |
egdaniel | bc9b296 | 2016-09-27 08:00:53 -0700 | [diff] [blame] | 70 | |
| 71 | "// Copy Program FS\n" |
| 72 | "void main() {" |
Greg Daniel | e464160 | 2018-03-16 08:52:32 -0400 | [diff] [blame] | 73 | "sk_FragColor = texture(uTextureSampler, vTexCoord);" |
egdaniel | bc9b296 | 2016-09-27 08:00:53 -0700 | [diff] [blame] | 74 | "}" |
| 75 | ); |
| 76 | |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 77 | SkSL::Program::Settings settings; |
| 78 | SkSL::Program::Inputs inputs; |
| 79 | if (!GrCompileVkShaderModule(gpu, vertShaderText.c_str(), VK_SHADER_STAGE_VERTEX_BIT, |
| 80 | &fVertShaderModule, &fShaderStageInfo[0], settings, &inputs)) { |
egdaniel | bc9b296 | 2016-09-27 08:00:53 -0700 | [diff] [blame] | 81 | this->destroyResources(gpu); |
| 82 | return false; |
| 83 | } |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 84 | SkASSERT(inputs.isEmpty()); |
egdaniel | bc9b296 | 2016-09-27 08:00:53 -0700 | [diff] [blame] | 85 | |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 86 | if (!GrCompileVkShaderModule(gpu, fragShaderText.c_str(), VK_SHADER_STAGE_FRAGMENT_BIT, |
| 87 | &fFragShaderModule, &fShaderStageInfo[1], settings, &inputs)) { |
egdaniel | bc9b296 | 2016-09-27 08:00:53 -0700 | [diff] [blame] | 88 | this->destroyResources(gpu); |
| 89 | return false; |
| 90 | } |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 91 | SkASSERT(inputs.isEmpty()); |
egdaniel | bc9b296 | 2016-09-27 08:00:53 -0700 | [diff] [blame] | 92 | |
| 93 | VkDescriptorSetLayout dsLayout[2]; |
| 94 | |
| 95 | GrVkResourceProvider& resourceProvider = gpu->resourceProvider(); |
| 96 | |
| 97 | dsLayout[GrVkUniformHandler::kUniformBufferDescSet] = resourceProvider.getUniformDSLayout(); |
| 98 | |
| 99 | uint32_t samplerVisibility = kFragment_GrShaderFlag; |
| 100 | SkTArray<uint32_t> visibilityArray(&samplerVisibility, 1); |
| 101 | |
Greg Daniel | a754378 | 2017-05-02 14:01:43 -0400 | [diff] [blame] | 102 | resourceProvider.getSamplerDescriptorSetHandle(VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, |
| 103 | visibilityArray, &fSamplerDSHandle); |
egdaniel | bc9b296 | 2016-09-27 08:00:53 -0700 | [diff] [blame] | 104 | dsLayout[GrVkUniformHandler::kSamplerDescSet] = |
| 105 | resourceProvider.getSamplerDSLayout(fSamplerDSHandle); |
| 106 | |
| 107 | // Create the VkPipelineLayout |
| 108 | VkPipelineLayoutCreateInfo layoutCreateInfo; |
| 109 | memset(&layoutCreateInfo, 0, sizeof(VkPipelineLayoutCreateFlags)); |
| 110 | layoutCreateInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO; |
| 111 | layoutCreateInfo.pNext = 0; |
| 112 | layoutCreateInfo.flags = 0; |
| 113 | layoutCreateInfo.setLayoutCount = 2; |
| 114 | layoutCreateInfo.pSetLayouts = dsLayout; |
| 115 | layoutCreateInfo.pushConstantRangeCount = 0; |
| 116 | layoutCreateInfo.pPushConstantRanges = nullptr; |
| 117 | |
Greg Daniel | 7d918fd | 2018-06-19 15:22:01 -0400 | [diff] [blame] | 118 | VkPipelineLayout pipelineLayout; |
egdaniel | bc9b296 | 2016-09-27 08:00:53 -0700 | [diff] [blame] | 119 | VkResult err = GR_VK_CALL(gpu->vkInterface(), CreatePipelineLayout(gpu->device(), |
| 120 | &layoutCreateInfo, |
| 121 | nullptr, |
Greg Daniel | 7d918fd | 2018-06-19 15:22:01 -0400 | [diff] [blame] | 122 | &pipelineLayout)); |
egdaniel | bc9b296 | 2016-09-27 08:00:53 -0700 | [diff] [blame] | 123 | if (err) { |
| 124 | this->destroyResources(gpu); |
| 125 | return false; |
| 126 | } |
| 127 | |
Greg Daniel | 7d918fd | 2018-06-19 15:22:01 -0400 | [diff] [blame] | 128 | fPipelineLayout = new GrVkPipelineLayout(pipelineLayout); |
| 129 | |
egdaniel | bc9b296 | 2016-09-27 08:00:53 -0700 | [diff] [blame] | 130 | static const float vdata[] = { |
| 131 | 0, 0, |
| 132 | 0, 1, |
| 133 | 1, 0, |
| 134 | 1, 1 |
| 135 | }; |
| 136 | fVertexBuffer.reset(GrVkVertexBuffer::Create(gpu, sizeof(vdata), false)); |
| 137 | SkASSERT(fVertexBuffer.get()); |
| 138 | fVertexBuffer->updateData(vdata, sizeof(vdata)); |
| 139 | |
Ethan Nicholas | 5af9ea3 | 2017-07-28 15:19:46 -0400 | [diff] [blame] | 140 | // We use 2 float4's for uniforms |
Greg Daniel | f9f2723 | 2017-01-06 14:40:08 -0500 | [diff] [blame] | 141 | fUniformBuffer.reset(GrVkUniformBuffer::Create(gpu, 8 * sizeof(float))); |
| 142 | SkASSERT(fUniformBuffer.get()); |
egdaniel | bc9b296 | 2016-09-27 08:00:53 -0700 | [diff] [blame] | 143 | |
| 144 | return true; |
| 145 | } |
| 146 | |
| 147 | bool GrVkCopyManager::copySurfaceAsDraw(GrVkGpu* gpu, |
Robert Phillips | b0e93a2 | 2017-08-29 08:26:54 -0400 | [diff] [blame] | 148 | GrSurface* dst, GrSurfaceOrigin dstOrigin, |
| 149 | GrSurface* src, GrSurfaceOrigin srcOrigin, |
Greg Daniel | 55fa647 | 2018-03-16 16:13:10 -0400 | [diff] [blame] | 150 | const SkIRect& srcRect, const SkIPoint& dstPoint, |
| 151 | bool canDiscardOutsideDstRect) { |
Greg Daniel | 6c9f101 | 2017-05-11 09:20:59 -0400 | [diff] [blame] | 152 | // None of our copy methods can handle a swizzle. TODO: Make copySurfaceAsDraw handle the |
| 153 | // swizzle. |
| 154 | if (gpu->caps()->shaderCaps()->configOutputSwizzle(src->config()) != |
| 155 | gpu->caps()->shaderCaps()->configOutputSwizzle(dst->config())) { |
| 156 | return false; |
| 157 | } |
| 158 | |
Greg Daniel | e3cd691 | 2017-05-17 11:15:55 -0400 | [diff] [blame] | 159 | if (gpu->vkCaps().newCBOnPipelineChange()) { |
| 160 | // We bind a new pipeline here for the copy so we must start a new command buffer. |
Greg Daniel | 5131678 | 2017-08-02 15:10:09 +0000 | [diff] [blame] | 161 | gpu->finishFlush(0, nullptr); |
Greg Daniel | e3cd691 | 2017-05-17 11:15:55 -0400 | [diff] [blame] | 162 | } |
| 163 | |
egdaniel | bc9b296 | 2016-09-27 08:00:53 -0700 | [diff] [blame] | 164 | GrVkRenderTarget* rt = static_cast<GrVkRenderTarget*>(dst->asRenderTarget()); |
| 165 | if (!rt) { |
| 166 | return false; |
| 167 | } |
| 168 | |
| 169 | GrVkTexture* srcTex = static_cast<GrVkTexture*>(src->asTexture()); |
| 170 | if (!srcTex) { |
| 171 | return false; |
| 172 | } |
| 173 | |
| 174 | if (VK_NULL_HANDLE == fVertShaderModule) { |
| 175 | SkASSERT(VK_NULL_HANDLE == fFragShaderModule && |
Greg Daniel | 7d918fd | 2018-06-19 15:22:01 -0400 | [diff] [blame] | 176 | nullptr == fPipelineLayout && |
egdaniel | bc9b296 | 2016-09-27 08:00:53 -0700 | [diff] [blame] | 177 | nullptr == fVertexBuffer.get() && |
Greg Daniel | f9f2723 | 2017-01-06 14:40:08 -0500 | [diff] [blame] | 178 | nullptr == fUniformBuffer.get()); |
egdaniel | bc9b296 | 2016-09-27 08:00:53 -0700 | [diff] [blame] | 179 | if (!this->createCopyProgram(gpu)) { |
| 180 | SkDebugf("Failed to create copy program.\n"); |
| 181 | return false; |
| 182 | } |
| 183 | } |
Greg Daniel | 7d918fd | 2018-06-19 15:22:01 -0400 | [diff] [blame] | 184 | SkASSERT(fPipelineLayout); |
egdaniel | bc9b296 | 2016-09-27 08:00:53 -0700 | [diff] [blame] | 185 | |
| 186 | GrVkResourceProvider& resourceProv = gpu->resourceProvider(); |
| 187 | |
| 188 | GrVkCopyPipeline* pipeline = resourceProv.findOrCreateCopyPipeline(rt, |
| 189 | fShaderStageInfo, |
Greg Daniel | 7d918fd | 2018-06-19 15:22:01 -0400 | [diff] [blame] | 190 | fPipelineLayout->layout()); |
egdaniel | bc9b296 | 2016-09-27 08:00:53 -0700 | [diff] [blame] | 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 Phillips | b0e93a2 | 2017-08-29 08:26:54 -0400 | [diff] [blame] | 206 | if (kBottomLeft_GrSurfaceOrigin == dstOrigin) { |
egdaniel | bc9b296 | 2016-09-27 08:00:53 -0700 | [diff] [blame] | 207 | 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 Phillips | b0e93a2 | 2017-08-29 08:26:54 -0400 | [diff] [blame] | 217 | if (kBottomLeft_GrSurfaceOrigin == srcOrigin) { |
egdaniel | bc9b296 | 2016-09-27 08:00:53 -0700 | [diff] [blame] | 218 | 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 Daniel | 18f9602 | 2017-05-04 15:09:03 -0400 | [diff] [blame] | 245 | descriptorWrites.dstBinding = GrVkUniformHandler::kGeometryBinding; |
egdaniel | bc9b296 | 2016-09-27 08:00:53 -0700 | [diff] [blame] | 246 | 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 Salomon | 2bbdcc4 | 2017-09-07 12:36:34 -0400 | [diff] [blame] | 262 | GrSamplerState samplerState = GrSamplerState::ClampNearest(); |
egdaniel | bc9b296 | 2016-09-27 08:00:53 -0700 | [diff] [blame] | 263 | |
Brian Salomon | 2bbdcc4 | 2017-09-07 12:36:34 -0400 | [diff] [blame] | 264 | GrVkSampler* sampler = resourceProv.findOrCreateCompatibleSampler( |
| 265 | samplerState, srcTex->texturePriv().maxMipMapLevel()); |
egdaniel | bc9b296 | 2016-09-27 08:00:53 -0700 | [diff] [blame] | 266 | |
| 267 | VkDescriptorImageInfo imageInfo; |
| 268 | memset(&imageInfo, 0, sizeof(VkDescriptorImageInfo)); |
| 269 | imageInfo.sampler = sampler->sampler(); |
Brian Osman | 2b23c4b | 2018-06-01 12:25:08 -0400 | [diff] [blame] | 270 | imageInfo.imageView = srcTex->textureView()->imageView(); |
egdaniel | bc9b296 | 2016-09-27 08:00:53 -0700 | [diff] [blame] | 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) { |
Brian Salomon | 1fabd51 | 2018-02-09 09:54:25 -0500 | [diff] [blame] | 295 | gpu->onResolveRenderTarget(texRT); |
egdaniel | bc9b296 | 2016-09-27 08:00:53 -0700 | [diff] [blame] | 296 | } |
| 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 | |
Greg Daniel | f7828d0 | 2018-10-09 12:01:32 -0400 | [diff] [blame^] | 304 | // Change layouts of rt and texture. We aren't blending so we don't need color attachment read |
| 305 | // access for blending. |
egdaniel | bc9b296 | 2016-09-27 08:00:53 -0700 | [diff] [blame] | 306 | GrVkImage* targetImage = rt->msaaImage() ? rt->msaaImage() : rt; |
Greg Daniel | f7828d0 | 2018-10-09 12:01:32 -0400 | [diff] [blame^] | 307 | VkAccessFlags dstAccessFlags = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT; |
| 308 | if (!canDiscardOutsideDstRect) { |
| 309 | // We need to load the color attachment so need to be able to read it. |
| 310 | dstAccessFlags |= VK_ACCESS_COLOR_ATTACHMENT_READ_BIT; |
| 311 | } |
egdaniel | bc9b296 | 2016-09-27 08:00:53 -0700 | [diff] [blame] | 312 | targetImage->setImageLayout(gpu, |
| 313 | VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL, |
Greg Daniel | f7828d0 | 2018-10-09 12:01:32 -0400 | [diff] [blame^] | 314 | dstAccessFlags, |
| 315 | VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT, |
egdaniel | bc9b296 | 2016-09-27 08:00:53 -0700 | [diff] [blame] | 316 | false); |
| 317 | |
| 318 | srcTex->setImageLayout(gpu, |
| 319 | VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL, |
| 320 | VK_ACCESS_SHADER_READ_BIT, |
Greg Daniel | f7828d0 | 2018-10-09 12:01:32 -0400 | [diff] [blame^] | 321 | VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT, |
egdaniel | bc9b296 | 2016-09-27 08:00:53 -0700 | [diff] [blame] | 322 | false); |
| 323 | |
Greg Daniel | 2b41784 | 2018-03-19 11:17:13 -0400 | [diff] [blame] | 324 | GrStencilAttachment* stencil = rt->renderTargetPriv().getStencilAttachment(); |
| 325 | if (stencil) { |
| 326 | GrVkStencilAttachment* vkStencil = (GrVkStencilAttachment*)stencil; |
Greg Daniel | f7828d0 | 2018-10-09 12:01:32 -0400 | [diff] [blame^] | 327 | // We aren't actually using the stencil but we still load and store it so we need |
| 328 | // appropriate barriers. |
| 329 | // TODO: Once we refactor surface and how we conntect stencil to RTs, we should not even |
| 330 | // have the stencil on this render pass if possible. |
Greg Daniel | 2b41784 | 2018-03-19 11:17:13 -0400 | [diff] [blame] | 331 | vkStencil->setImageLayout(gpu, |
| 332 | VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL, |
Greg Daniel | f7828d0 | 2018-10-09 12:01:32 -0400 | [diff] [blame^] | 333 | VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_READ_BIT | |
| 334 | VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT, |
| 335 | VK_PIPELINE_STAGE_EARLY_FRAGMENT_TESTS_BIT, |
Greg Daniel | 2b41784 | 2018-03-19 11:17:13 -0400 | [diff] [blame] | 336 | false); |
| 337 | } |
| 338 | |
Greg Daniel | 55fa647 | 2018-03-16 16:13:10 -0400 | [diff] [blame] | 339 | VkAttachmentLoadOp loadOp = canDiscardOutsideDstRect ? VK_ATTACHMENT_LOAD_OP_DONT_CARE |
| 340 | : VK_ATTACHMENT_LOAD_OP_LOAD; |
| 341 | GrVkRenderPass::LoadStoreOps vkColorOps(loadOp, VK_ATTACHMENT_STORE_OP_STORE); |
Greg Daniel | ac95c5f | 2017-01-30 16:33:57 -0500 | [diff] [blame] | 342 | GrVkRenderPass::LoadStoreOps vkStencilOps(VK_ATTACHMENT_LOAD_OP_LOAD, |
egdaniel | bc9b296 | 2016-09-27 08:00:53 -0700 | [diff] [blame] | 343 | VK_ATTACHMENT_STORE_OP_STORE); |
egdaniel | bc9b296 | 2016-09-27 08:00:53 -0700 | [diff] [blame] | 344 | const GrVkRenderPass* renderPass; |
Greg Daniel | 55fa647 | 2018-03-16 16:13:10 -0400 | [diff] [blame] | 345 | const GrVkResourceProvider::CompatibleRPHandle& rpHandle = rt->compatibleRenderPassHandle(); |
egdaniel | bc9b296 | 2016-09-27 08:00:53 -0700 | [diff] [blame] | 346 | if (rpHandle.isValid()) { |
| 347 | renderPass = gpu->resourceProvider().findRenderPass(rpHandle, |
| 348 | vkColorOps, |
egdaniel | bc9b296 | 2016-09-27 08:00:53 -0700 | [diff] [blame] | 349 | vkStencilOps); |
| 350 | } else { |
| 351 | renderPass = gpu->resourceProvider().findRenderPass(*rt, |
| 352 | vkColorOps, |
egdaniel | bc9b296 | 2016-09-27 08:00:53 -0700 | [diff] [blame] | 353 | vkStencilOps); |
| 354 | } |
| 355 | |
| 356 | SkASSERT(renderPass->isCompatible(*rt->simpleRenderPass())); |
| 357 | |
| 358 | |
Greg Daniel | 77a86f8 | 2017-01-23 11:04:45 -0500 | [diff] [blame] | 359 | cmdBuffer->beginRenderPass(gpu, renderPass, nullptr, *rt, bounds, false); |
egdaniel | bc9b296 | 2016-09-27 08:00:53 -0700 | [diff] [blame] | 360 | cmdBuffer->bindPipeline(gpu, pipeline); |
| 361 | |
| 362 | // Uniform DescriptorSet, Sampler DescriptorSet, and vertex shader uniformBuffer |
| 363 | SkSTArray<3, const GrVkRecycledResource*> descriptorRecycledResources; |
| 364 | descriptorRecycledResources.push_back(uniformDS); |
| 365 | descriptorRecycledResources.push_back(samplerDS); |
| 366 | descriptorRecycledResources.push_back(fUniformBuffer->resource()); |
| 367 | |
| 368 | // One sampler, texture view, and texture |
| 369 | SkSTArray<3, const GrVkResource*> descriptorResources; |
| 370 | descriptorResources.push_back(sampler); |
Brian Osman | 2b23c4b | 2018-06-01 12:25:08 -0400 | [diff] [blame] | 371 | descriptorResources.push_back(srcTex->textureView()); |
egdaniel | bc9b296 | 2016-09-27 08:00:53 -0700 | [diff] [blame] | 372 | descriptorResources.push_back(srcTex->resource()); |
| 373 | |
| 374 | cmdBuffer->bindDescriptorSets(gpu, |
| 375 | descriptorRecycledResources, |
| 376 | descriptorResources, |
| 377 | fPipelineLayout, |
| 378 | 0, |
| 379 | 2, |
| 380 | vkDescSets, |
| 381 | 0, |
| 382 | nullptr); |
| 383 | |
| 384 | // Set Dynamic viewport and stencil |
| 385 | // We always use one viewport the size of the RT |
| 386 | VkViewport viewport; |
| 387 | viewport.x = 0.0f; |
| 388 | viewport.y = 0.0f; |
| 389 | viewport.width = SkIntToScalar(rt->width()); |
| 390 | viewport.height = SkIntToScalar(rt->height()); |
| 391 | viewport.minDepth = 0.0f; |
| 392 | viewport.maxDepth = 1.0f; |
| 393 | cmdBuffer->setViewport(gpu, 0, 1, &viewport); |
| 394 | |
| 395 | // We assume the scissor is not enabled so just set it to the whole RT |
| 396 | VkRect2D scissor; |
| 397 | scissor.extent.width = rt->width(); |
| 398 | scissor.extent.height = rt->height(); |
| 399 | scissor.offset.x = 0; |
| 400 | scissor.offset.y = 0; |
| 401 | cmdBuffer->setScissor(gpu, 0, 1, &scissor); |
| 402 | |
Chris Dalton | 1d61635 | 2017-05-31 12:51:23 -0600 | [diff] [blame] | 403 | cmdBuffer->bindInputBuffer(gpu, 0, fVertexBuffer.get()); |
egdaniel | bc9b296 | 2016-09-27 08:00:53 -0700 | [diff] [blame] | 404 | cmdBuffer->draw(gpu, 4, 1, 0, 0); |
| 405 | cmdBuffer->endRenderPass(gpu); |
| 406 | |
| 407 | // Release all temp resources which should now be reffed by the cmd buffer |
| 408 | pipeline->unref(gpu); |
| 409 | uniformDS->unref(gpu); |
| 410 | samplerDS->unref(gpu); |
| 411 | sampler->unref(gpu); |
| 412 | renderPass->unref(gpu); |
| 413 | |
| 414 | return true; |
| 415 | } |
| 416 | |
| 417 | void GrVkCopyManager::destroyResources(GrVkGpu* gpu) { |
| 418 | if (VK_NULL_HANDLE != fVertShaderModule) { |
| 419 | GR_VK_CALL(gpu->vkInterface(), DestroyShaderModule(gpu->device(), fVertShaderModule, |
| 420 | nullptr)); |
| 421 | fVertShaderModule = VK_NULL_HANDLE; |
| 422 | } |
| 423 | |
| 424 | if (VK_NULL_HANDLE != fFragShaderModule) { |
| 425 | GR_VK_CALL(gpu->vkInterface(), DestroyShaderModule(gpu->device(), fFragShaderModule, |
| 426 | nullptr)); |
| 427 | fFragShaderModule = VK_NULL_HANDLE; |
| 428 | } |
| 429 | |
Greg Daniel | 7d918fd | 2018-06-19 15:22:01 -0400 | [diff] [blame] | 430 | if (fPipelineLayout) { |
| 431 | fPipelineLayout->unref(gpu); |
| 432 | fPipelineLayout = nullptr; |
egdaniel | bc9b296 | 2016-09-27 08:00:53 -0700 | [diff] [blame] | 433 | } |
| 434 | |
| 435 | if (fUniformBuffer) { |
| 436 | fUniformBuffer->release(gpu); |
Greg Daniel | f9f2723 | 2017-01-06 14:40:08 -0500 | [diff] [blame] | 437 | fUniformBuffer.reset(); |
egdaniel | bc9b296 | 2016-09-27 08:00:53 -0700 | [diff] [blame] | 438 | } |
| 439 | } |
| 440 | |
| 441 | void GrVkCopyManager::abandonResources() { |
| 442 | fVertShaderModule = VK_NULL_HANDLE; |
| 443 | fFragShaderModule = VK_NULL_HANDLE; |
Greg Daniel | 7d918fd | 2018-06-19 15:22:01 -0400 | [diff] [blame] | 444 | if (fPipelineLayout) { |
| 445 | fPipelineLayout->unrefAndAbandon(); |
| 446 | fPipelineLayout = nullptr; |
| 447 | } |
egdaniel | bc9b296 | 2016-09-27 08:00:53 -0700 | [diff] [blame] | 448 | |
| 449 | if (fUniformBuffer) { |
| 450 | fUniformBuffer->abandon(); |
Greg Daniel | f9f2723 | 2017-01-06 14:40:08 -0500 | [diff] [blame] | 451 | fUniformBuffer.reset(); |
egdaniel | bc9b296 | 2016-09-27 08:00:53 -0700 | [diff] [blame] | 452 | } |
| 453 | } |