Shahbaz Youssefi | 8f1b7a6 | 2018-11-14 16:02:54 -0500 | [diff] [blame] | 1 | // |
| 2 | // Copyright 2018 The ANGLE Project Authors. All rights reserved. |
| 3 | // Use of this source code is governed by a BSD-style license that can be |
| 4 | // found in the LICENSE file. |
| 5 | // |
Shahbaz Youssefi | e321940 | 2018-12-08 16:54:14 +0100 | [diff] [blame] | 6 | // UtilsVk.cpp: |
| 7 | // Implements the UtilsVk class. |
Shahbaz Youssefi | 8f1b7a6 | 2018-11-14 16:02:54 -0500 | [diff] [blame] | 8 | // |
| 9 | |
Shahbaz Youssefi | e321940 | 2018-12-08 16:54:14 +0100 | [diff] [blame] | 10 | #include "libANGLE/renderer/vulkan/UtilsVk.h" |
Shahbaz Youssefi | 8f1b7a6 | 2018-11-14 16:02:54 -0500 | [diff] [blame] | 11 | |
Shahbaz Youssefi | e321940 | 2018-12-08 16:54:14 +0100 | [diff] [blame] | 12 | #include "libANGLE/renderer/vulkan/ContextVk.h" |
| 13 | #include "libANGLE/renderer/vulkan/FramebufferVk.h" |
Shahbaz Youssefi | 8f1b7a6 | 2018-11-14 16:02:54 -0500 | [diff] [blame] | 14 | #include "libANGLE/renderer/vulkan/RendererVk.h" |
Shahbaz Youssefi | 8f1b7a6 | 2018-11-14 16:02:54 -0500 | [diff] [blame] | 15 | |
| 16 | namespace rx |
| 17 | { |
| 18 | |
Shahbaz Youssefi | 611bbaa | 2018-12-06 01:59:53 +0100 | [diff] [blame] | 19 | namespace BufferUtils_comp = vk::InternalShader::BufferUtils_comp; |
| 20 | namespace ConvertVertex_comp = vk::InternalShader::ConvertVertex_comp; |
Shahbaz Youssefi | f83a28a | 2018-12-09 03:48:34 +0100 | [diff] [blame] | 21 | namespace ImageCopy_frag = vk::InternalShader::ImageCopy_frag; |
Shahbaz Youssefi | 8f1b7a6 | 2018-11-14 16:02:54 -0500 | [diff] [blame] | 22 | |
| 23 | namespace |
| 24 | { |
| 25 | // All internal shaders assume there is only one descriptor set, indexed at 0 |
| 26 | constexpr uint32_t kSetIndex = 0; |
| 27 | |
Shahbaz Youssefi | 611bbaa | 2018-12-06 01:59:53 +0100 | [diff] [blame] | 28 | constexpr uint32_t kBufferClearOutputBinding = 0; |
| 29 | constexpr uint32_t kBufferCopyDestinationBinding = 0; |
| 30 | constexpr uint32_t kBufferCopySourceBinding = 1; |
| 31 | constexpr uint32_t kConvertVertexDestinationBinding = 0; |
| 32 | constexpr uint32_t kConvertVertexSourceBinding = 1; |
Shahbaz Youssefi | f83a28a | 2018-12-09 03:48:34 +0100 | [diff] [blame] | 33 | constexpr uint32_t kImageCopySourceBinding = 0; |
Shahbaz Youssefi | 8f1b7a6 | 2018-11-14 16:02:54 -0500 | [diff] [blame] | 34 | |
| 35 | uint32_t GetBufferUtilsFlags(size_t dispatchSize, const vk::Format &format) |
| 36 | { |
| 37 | uint32_t flags = dispatchSize % 64 == 0 ? BufferUtils_comp::kIsAligned : 0; |
| 38 | const angle::Format &bufferFormat = format.bufferFormat(); |
| 39 | |
Shahbaz Youssefi | f83a28a | 2018-12-09 03:48:34 +0100 | [diff] [blame] | 40 | flags |= bufferFormat.isInt() |
Shahbaz Youssefi | 8f1b7a6 | 2018-11-14 16:02:54 -0500 | [diff] [blame] | 41 | ? BufferUtils_comp::kIsInt |
Shahbaz Youssefi | f83a28a | 2018-12-09 03:48:34 +0100 | [diff] [blame] | 42 | : bufferFormat.isUint() ? BufferUtils_comp::kIsUint : BufferUtils_comp::kIsFloat; |
Shahbaz Youssefi | 8f1b7a6 | 2018-11-14 16:02:54 -0500 | [diff] [blame] | 43 | |
| 44 | return flags; |
| 45 | } |
Shahbaz Youssefi | 611bbaa | 2018-12-06 01:59:53 +0100 | [diff] [blame] | 46 | |
Shahbaz Youssefi | e321940 | 2018-12-08 16:54:14 +0100 | [diff] [blame] | 47 | uint32_t GetConvertVertexFlags(const UtilsVk::ConvertVertexParameters ¶ms) |
Shahbaz Youssefi | 611bbaa | 2018-12-06 01:59:53 +0100 | [diff] [blame] | 48 | { |
Shahbaz Youssefi | f83a28a | 2018-12-09 03:48:34 +0100 | [diff] [blame] | 49 | bool srcIsInt = params.srcFormat->isInt(); |
| 50 | bool srcIsUint = params.srcFormat->isUint(); |
| 51 | bool srcIsSnorm = params.srcFormat->isSnorm(); |
| 52 | bool srcIsUnorm = params.srcFormat->isUnorm(); |
Shahbaz Youssefi | 611bbaa | 2018-12-06 01:59:53 +0100 | [diff] [blame] | 53 | bool srcIsFixed = params.srcFormat->isFixed; |
Shahbaz Youssefi | f83a28a | 2018-12-09 03:48:34 +0100 | [diff] [blame] | 54 | bool srcIsFloat = params.srcFormat->isFloat(); |
Shahbaz Youssefi | 611bbaa | 2018-12-06 01:59:53 +0100 | [diff] [blame] | 55 | |
Shahbaz Youssefi | f83a28a | 2018-12-09 03:48:34 +0100 | [diff] [blame] | 56 | bool destIsInt = params.destFormat->isInt(); |
| 57 | bool destIsUint = params.destFormat->isUint(); |
| 58 | bool destIsFloat = params.destFormat->isFloat(); |
Shahbaz Youssefi | 611bbaa | 2018-12-06 01:59:53 +0100 | [diff] [blame] | 59 | |
| 60 | // Assert on the types to make sure the shader supports its. These are based on |
| 61 | // ConvertVertex_comp::Conversion values. |
| 62 | ASSERT(!destIsInt || srcIsInt); // If destination is int, src must be int too |
| 63 | ASSERT(!destIsUint || srcIsUint); // If destination is uint, src must be uint too |
| 64 | ASSERT(!srcIsFixed || destIsFloat); // If source is fixed, dest must be float |
| 65 | // One of each bool set must be true |
| 66 | ASSERT(srcIsInt || srcIsUint || srcIsSnorm || srcIsUnorm || srcIsFixed || srcIsFloat); |
| 67 | ASSERT(destIsInt || destIsUint || destIsFloat); |
| 68 | |
| 69 | // We currently don't have any big-endian devices in the list of supported platforms. The |
| 70 | // shader is capable of supporting big-endian architectures, but the relevant flag (IsBigEndian) |
| 71 | // is not added to the build configuration file (to reduce binary size). If necessary, add |
| 72 | // IsBigEndian to ConvertVertex.comp.json and select the appropriate flag based on the |
| 73 | // endian-ness test here. |
| 74 | uint32_t endiannessTest = 0; |
| 75 | *reinterpret_cast<uint8_t *>(&endiannessTest) = 1; |
| 76 | ASSERT(endiannessTest == 1); |
| 77 | |
| 78 | uint32_t flags = 0; |
| 79 | |
| 80 | if (srcIsInt && destIsInt) |
| 81 | { |
| 82 | flags |= ConvertVertex_comp::kIntToInt; |
| 83 | } |
| 84 | else if (srcIsUint && destIsUint) |
| 85 | { |
| 86 | flags |= ConvertVertex_comp::kUintToUint; |
| 87 | } |
| 88 | else if (srcIsInt) |
| 89 | { |
| 90 | flags |= ConvertVertex_comp::kIntToFloat; |
| 91 | } |
| 92 | else if (srcIsUint) |
| 93 | { |
| 94 | flags |= ConvertVertex_comp::kUintToFloat; |
| 95 | } |
| 96 | else if (srcIsSnorm) |
| 97 | { |
| 98 | flags |= ConvertVertex_comp::kSnormToFloat; |
| 99 | } |
| 100 | else if (srcIsUnorm) |
| 101 | { |
| 102 | flags |= ConvertVertex_comp::kUnormToFloat; |
| 103 | } |
| 104 | else if (srcIsFixed) |
| 105 | { |
| 106 | flags |= ConvertVertex_comp::kFixedToFloat; |
| 107 | } |
| 108 | else if (srcIsFloat) |
| 109 | { |
| 110 | flags |= ConvertVertex_comp::kFloatToFloat; |
| 111 | } |
| 112 | else |
| 113 | { |
| 114 | UNREACHABLE(); |
| 115 | } |
| 116 | |
| 117 | return flags; |
| 118 | } |
Shahbaz Youssefi | f83a28a | 2018-12-09 03:48:34 +0100 | [diff] [blame] | 119 | |
| 120 | uint32_t GetImageCopyFlags(const vk::Format &srcFormat, const vk::Format &destFormat) |
| 121 | { |
| 122 | const angle::Format &srcAngleFormat = srcFormat.angleFormat(); |
| 123 | const angle::Format &destAngleFormat = destFormat.angleFormat(); |
| 124 | |
| 125 | uint32_t flags = 0; |
| 126 | |
| 127 | flags |= srcAngleFormat.isInt() ? ImageCopy_frag::kSrcIsInt |
| 128 | : srcAngleFormat.isUint() ? ImageCopy_frag::kSrcIsUint |
| 129 | : ImageCopy_frag::kSrcIsFloat; |
| 130 | flags |= destAngleFormat.isInt() ? ImageCopy_frag::kDestIsInt |
| 131 | : destAngleFormat.isUint() ? ImageCopy_frag::kDestIsUint |
| 132 | : ImageCopy_frag::kDestIsFloat; |
| 133 | |
| 134 | return flags; |
| 135 | } |
Shahbaz Youssefi | 4f3b207 | 2019-01-01 14:48:25 -0500 | [diff] [blame] | 136 | |
| 137 | uint32_t GetFormatDefaultChannelMask(const vk::Format &format) |
| 138 | { |
| 139 | uint32_t mask = 0; |
| 140 | |
| 141 | const angle::Format &angleFormat = format.angleFormat(); |
| 142 | const angle::Format &textureFormat = format.textureFormat(); |
| 143 | |
| 144 | // Red can never be introduced due to format emulation (except for luma which is handled |
| 145 | // especially) |
| 146 | ASSERT(((angleFormat.redBits > 0) == (textureFormat.redBits > 0)) || angleFormat.isLUMA()); |
| 147 | mask |= angleFormat.greenBits == 0 && textureFormat.greenBits > 0 ? 2 : 0; |
| 148 | mask |= angleFormat.blueBits == 0 && textureFormat.blueBits > 0 ? 4 : 0; |
| 149 | mask |= angleFormat.alphaBits == 0 && textureFormat.alphaBits > 0 ? 8 : 0; |
| 150 | |
| 151 | return mask; |
| 152 | } |
Shahbaz Youssefi | 8f1b7a6 | 2018-11-14 16:02:54 -0500 | [diff] [blame] | 153 | } // namespace |
| 154 | |
Jamie Madill | ab2bfa8 | 2019-01-15 19:06:47 -0500 | [diff] [blame] | 155 | UtilsVk::ConvertVertexShaderParams::ConvertVertexShaderParams() = default; |
| 156 | |
| 157 | UtilsVk::ImageCopyShaderParams::ImageCopyShaderParams() = default; |
| 158 | |
Shahbaz Youssefi | e321940 | 2018-12-08 16:54:14 +0100 | [diff] [blame] | 159 | UtilsVk::UtilsVk() = default; |
Shahbaz Youssefi | 8f1b7a6 | 2018-11-14 16:02:54 -0500 | [diff] [blame] | 160 | |
Shahbaz Youssefi | e321940 | 2018-12-08 16:54:14 +0100 | [diff] [blame] | 161 | UtilsVk::~UtilsVk() = default; |
Shahbaz Youssefi | 8f1b7a6 | 2018-11-14 16:02:54 -0500 | [diff] [blame] | 162 | |
Shahbaz Youssefi | e321940 | 2018-12-08 16:54:14 +0100 | [diff] [blame] | 163 | void UtilsVk::destroy(VkDevice device) |
Shahbaz Youssefi | 8f1b7a6 | 2018-11-14 16:02:54 -0500 | [diff] [blame] | 164 | { |
| 165 | for (Function f : angle::AllEnums<Function>()) |
| 166 | { |
| 167 | for (auto &descriptorSetLayout : mDescriptorSetLayouts[f]) |
| 168 | { |
| 169 | descriptorSetLayout.reset(); |
| 170 | } |
| 171 | mPipelineLayouts[f].reset(); |
| 172 | mDescriptorPools[f].destroy(device); |
| 173 | } |
| 174 | |
Shahbaz Youssefi | 611bbaa | 2018-12-06 01:59:53 +0100 | [diff] [blame] | 175 | for (vk::ShaderProgramHelper &program : mBufferUtilsPrograms) |
| 176 | { |
| 177 | program.destroy(device); |
| 178 | } |
| 179 | for (vk::ShaderProgramHelper &program : mConvertVertexPrograms) |
Shahbaz Youssefi | 8f1b7a6 | 2018-11-14 16:02:54 -0500 | [diff] [blame] | 180 | { |
| 181 | program.destroy(device); |
| 182 | } |
Shahbaz Youssefi | e321940 | 2018-12-08 16:54:14 +0100 | [diff] [blame] | 183 | mImageClearProgram.destroy(device); |
Shahbaz Youssefi | f83a28a | 2018-12-09 03:48:34 +0100 | [diff] [blame] | 184 | for (vk::ShaderProgramHelper &program : mImageCopyPrograms) |
| 185 | { |
| 186 | program.destroy(device); |
| 187 | } |
Shahbaz Youssefi | 8f1b7a6 | 2018-11-14 16:02:54 -0500 | [diff] [blame] | 188 | } |
| 189 | |
Shahbaz Youssefi | e321940 | 2018-12-08 16:54:14 +0100 | [diff] [blame] | 190 | angle::Result UtilsVk::ensureResourcesInitialized(vk::Context *context, |
| 191 | Function function, |
| 192 | VkDescriptorPoolSize *setSizes, |
| 193 | size_t setSizesCount, |
| 194 | size_t pushConstantsSize) |
Shahbaz Youssefi | 8f1b7a6 | 2018-11-14 16:02:54 -0500 | [diff] [blame] | 195 | { |
| 196 | RendererVk *renderer = context->getRenderer(); |
| 197 | |
| 198 | vk::DescriptorSetLayoutDesc descriptorSetDesc; |
| 199 | |
| 200 | uint32_t currentBinding = 0; |
| 201 | for (size_t i = 0; i < setSizesCount; ++i) |
| 202 | { |
| 203 | descriptorSetDesc.update(currentBinding, setSizes[i].type, setSizes[i].descriptorCount); |
| 204 | currentBinding += setSizes[i].descriptorCount; |
| 205 | } |
| 206 | |
| 207 | ANGLE_TRY(renderer->getDescriptorSetLayout(context, descriptorSetDesc, |
| 208 | &mDescriptorSetLayouts[function][kSetIndex])); |
| 209 | |
Shahbaz Youssefi | e321940 | 2018-12-08 16:54:14 +0100 | [diff] [blame] | 210 | gl::ShaderType pushConstantsShaderStage = function >= Function::ComputeStartIndex |
| 211 | ? gl::ShaderType::Compute |
| 212 | : gl::ShaderType::Fragment; |
| 213 | |
Shahbaz Youssefi | 8f1b7a6 | 2018-11-14 16:02:54 -0500 | [diff] [blame] | 214 | // Corresponding pipeline layouts: |
| 215 | vk::PipelineLayoutDesc pipelineLayoutDesc; |
| 216 | |
| 217 | pipelineLayoutDesc.updateDescriptorSetLayout(kSetIndex, descriptorSetDesc); |
Shahbaz Youssefi | e321940 | 2018-12-08 16:54:14 +0100 | [diff] [blame] | 218 | pipelineLayoutDesc.updatePushConstantRange(pushConstantsShaderStage, 0, pushConstantsSize); |
Shahbaz Youssefi | 8f1b7a6 | 2018-11-14 16:02:54 -0500 | [diff] [blame] | 219 | |
| 220 | ANGLE_TRY(renderer->getPipelineLayout( |
| 221 | context, pipelineLayoutDesc, mDescriptorSetLayouts[function], &mPipelineLayouts[function])); |
| 222 | |
Shahbaz Youssefi | e321940 | 2018-12-08 16:54:14 +0100 | [diff] [blame] | 223 | if (setSizesCount > 0) |
| 224 | { |
| 225 | ANGLE_TRY(mDescriptorPools[function].init(context, setSizes, setSizesCount)); |
| 226 | } |
Shahbaz Youssefi | 8f1b7a6 | 2018-11-14 16:02:54 -0500 | [diff] [blame] | 227 | |
Jamie Madill | 7c985f5 | 2018-11-29 18:16:17 -0500 | [diff] [blame] | 228 | return angle::Result::Continue; |
Shahbaz Youssefi | 8f1b7a6 | 2018-11-14 16:02:54 -0500 | [diff] [blame] | 229 | } |
| 230 | |
Shahbaz Youssefi | e321940 | 2018-12-08 16:54:14 +0100 | [diff] [blame] | 231 | angle::Result UtilsVk::ensureBufferClearResourcesInitialized(vk::Context *context) |
Shahbaz Youssefi | 8f1b7a6 | 2018-11-14 16:02:54 -0500 | [diff] [blame] | 232 | { |
| 233 | if (mPipelineLayouts[Function::BufferClear].valid()) |
| 234 | { |
Jamie Madill | 7c985f5 | 2018-11-29 18:16:17 -0500 | [diff] [blame] | 235 | return angle::Result::Continue; |
Shahbaz Youssefi | 8f1b7a6 | 2018-11-14 16:02:54 -0500 | [diff] [blame] | 236 | } |
| 237 | |
| 238 | VkDescriptorPoolSize setSizes[1] = { |
| 239 | {VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER, 1}, |
| 240 | }; |
| 241 | |
Shahbaz Youssefi | 611bbaa | 2018-12-06 01:59:53 +0100 | [diff] [blame] | 242 | return ensureResourcesInitialized(context, Function::BufferClear, setSizes, ArraySize(setSizes), |
| 243 | sizeof(BufferUtilsShaderParams)); |
Shahbaz Youssefi | 8f1b7a6 | 2018-11-14 16:02:54 -0500 | [diff] [blame] | 244 | } |
| 245 | |
Shahbaz Youssefi | e321940 | 2018-12-08 16:54:14 +0100 | [diff] [blame] | 246 | angle::Result UtilsVk::ensureBufferCopyResourcesInitialized(vk::Context *context) |
Shahbaz Youssefi | 8f1b7a6 | 2018-11-14 16:02:54 -0500 | [diff] [blame] | 247 | { |
| 248 | if (mPipelineLayouts[Function::BufferCopy].valid()) |
| 249 | { |
Jamie Madill | 7c985f5 | 2018-11-29 18:16:17 -0500 | [diff] [blame] | 250 | return angle::Result::Continue; |
Shahbaz Youssefi | 8f1b7a6 | 2018-11-14 16:02:54 -0500 | [diff] [blame] | 251 | } |
| 252 | |
| 253 | VkDescriptorPoolSize setSizes[2] = { |
| 254 | {VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER, 1}, |
| 255 | {VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER, 1}, |
| 256 | }; |
| 257 | |
Shahbaz Youssefi | 611bbaa | 2018-12-06 01:59:53 +0100 | [diff] [blame] | 258 | return ensureResourcesInitialized(context, Function::BufferCopy, setSizes, ArraySize(setSizes), |
| 259 | sizeof(BufferUtilsShaderParams)); |
Shahbaz Youssefi | 8f1b7a6 | 2018-11-14 16:02:54 -0500 | [diff] [blame] | 260 | } |
| 261 | |
Shahbaz Youssefi | e321940 | 2018-12-08 16:54:14 +0100 | [diff] [blame] | 262 | angle::Result UtilsVk::ensureConvertVertexResourcesInitialized(vk::Context *context) |
Shahbaz Youssefi | 611bbaa | 2018-12-06 01:59:53 +0100 | [diff] [blame] | 263 | { |
| 264 | if (mPipelineLayouts[Function::ConvertVertexBuffer].valid()) |
| 265 | { |
| 266 | return angle::Result::Continue; |
| 267 | } |
| 268 | |
| 269 | VkDescriptorPoolSize setSizes[2] = { |
| 270 | {VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, 1}, |
| 271 | {VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, 1}, |
| 272 | }; |
| 273 | |
| 274 | return ensureResourcesInitialized(context, Function::ConvertVertexBuffer, setSizes, |
| 275 | ArraySize(setSizes), sizeof(ConvertVertexShaderParams)); |
| 276 | } |
| 277 | |
Shahbaz Youssefi | e321940 | 2018-12-08 16:54:14 +0100 | [diff] [blame] | 278 | angle::Result UtilsVk::ensureImageClearResourcesInitialized(vk::Context *context) |
| 279 | { |
| 280 | if (mPipelineLayouts[Function::ImageClear].valid()) |
| 281 | { |
| 282 | return angle::Result::Continue; |
| 283 | } |
| 284 | |
| 285 | // The shader does not use any descriptor sets. |
| 286 | return ensureResourcesInitialized(context, Function::ImageClear, nullptr, 0, |
| 287 | sizeof(ImageClearShaderParams)); |
| 288 | } |
| 289 | |
Shahbaz Youssefi | f83a28a | 2018-12-09 03:48:34 +0100 | [diff] [blame] | 290 | angle::Result UtilsVk::ensureImageCopyResourcesInitialized(vk::Context *context) |
| 291 | { |
| 292 | if (mPipelineLayouts[Function::ImageCopy].valid()) |
| 293 | { |
| 294 | return angle::Result::Continue; |
| 295 | } |
| 296 | |
| 297 | VkDescriptorPoolSize setSizes[1] = { |
| 298 | {VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE, 1}, |
| 299 | }; |
| 300 | |
| 301 | return ensureResourcesInitialized(context, Function::ImageCopy, setSizes, ArraySize(setSizes), |
| 302 | sizeof(ImageCopyShaderParams)); |
| 303 | } |
| 304 | |
Shahbaz Youssefi | e321940 | 2018-12-08 16:54:14 +0100 | [diff] [blame] | 305 | angle::Result UtilsVk::setupProgram(vk::Context *context, |
| 306 | Function function, |
| 307 | vk::RefCounted<vk::ShaderAndSerial> *fsCsShader, |
| 308 | vk::RefCounted<vk::ShaderAndSerial> *vsShader, |
| 309 | vk::ShaderProgramHelper *program, |
| 310 | const vk::GraphicsPipelineDesc *pipelineDesc, |
| 311 | const VkDescriptorSet descriptorSet, |
| 312 | const void *pushConstants, |
| 313 | size_t pushConstantsSize, |
| 314 | vk::CommandBuffer *commandBuffer) |
Shahbaz Youssefi | 611bbaa | 2018-12-06 01:59:53 +0100 | [diff] [blame] | 315 | { |
| 316 | RendererVk *renderer = context->getRenderer(); |
| 317 | |
Shahbaz Youssefi | e321940 | 2018-12-08 16:54:14 +0100 | [diff] [blame] | 318 | bool isCompute = function >= Function::ComputeStartIndex; |
| 319 | VkPipelineBindPoint bindPoint = |
| 320 | isCompute ? VK_PIPELINE_BIND_POINT_COMPUTE : VK_PIPELINE_BIND_POINT_GRAPHICS; |
| 321 | VkShaderStageFlags pushConstantsShaderStage = |
| 322 | isCompute ? VK_SHADER_STAGE_COMPUTE_BIT : VK_SHADER_STAGE_FRAGMENT_BIT; |
| 323 | |
| 324 | // If compute, vsShader and pipelineDesc should be nullptr, and if not compute they shouldn't |
| 325 | // be. |
| 326 | ASSERT(isCompute != (vsShader && pipelineDesc)); |
Shahbaz Youssefi | 611bbaa | 2018-12-06 01:59:53 +0100 | [diff] [blame] | 327 | |
| 328 | const vk::BindingPointer<vk::PipelineLayout> &pipelineLayout = mPipelineLayouts[function]; |
| 329 | |
Jamie Madill | dbc605c | 2019-01-04 16:39:14 -0500 | [diff] [blame] | 330 | Serial serial = renderer->getCurrentQueueSerial(); |
| 331 | |
Shahbaz Youssefi | e321940 | 2018-12-08 16:54:14 +0100 | [diff] [blame] | 332 | if (isCompute) |
| 333 | { |
Jamie Madill | 3f0c4a5 | 2019-01-10 10:20:35 -0500 | [diff] [blame] | 334 | vk::PipelineAndSerial *pipelineAndSerial; |
Shahbaz Youssefi | e321940 | 2018-12-08 16:54:14 +0100 | [diff] [blame] | 335 | program->setShader(gl::ShaderType::Compute, fsCsShader); |
| 336 | ANGLE_TRY(program->getComputePipeline(context, pipelineLayout.get(), &pipelineAndSerial)); |
Jamie Madill | 3f0c4a5 | 2019-01-10 10:20:35 -0500 | [diff] [blame] | 337 | pipelineAndSerial->updateSerial(serial); |
| 338 | commandBuffer->bindPipeline(bindPoint, pipelineAndSerial->get()); |
Shahbaz Youssefi | e321940 | 2018-12-08 16:54:14 +0100 | [diff] [blame] | 339 | } |
| 340 | else |
| 341 | { |
| 342 | program->setShader(gl::ShaderType::Vertex, vsShader); |
| 343 | program->setShader(gl::ShaderType::Fragment, fsCsShader); |
Jamie Madill | dbc605c | 2019-01-04 16:39:14 -0500 | [diff] [blame] | 344 | |
Jamie Madill | 3f0c4a5 | 2019-01-10 10:20:35 -0500 | [diff] [blame] | 345 | // This value is not used but is passed to getGraphicsPipeline to avoid a nullptr check. |
| 346 | const vk::GraphicsPipelineDesc *descPtr; |
| 347 | vk::PipelineHelper *helper; |
| 348 | |
Jamie Madill | dbc605c | 2019-01-04 16:39:14 -0500 | [diff] [blame] | 349 | ANGLE_TRY(program->getGraphicsPipeline( |
| 350 | context, &renderer->getRenderPassCache(), renderer->getPipelineCache(), serial, |
Jamie Madill | 3f0c4a5 | 2019-01-10 10:20:35 -0500 | [diff] [blame] | 351 | pipelineLayout.get(), *pipelineDesc, gl::AttributesMask(), &descPtr, &helper)); |
| 352 | helper->updateSerial(serial); |
| 353 | commandBuffer->bindPipeline(bindPoint, helper->getPipeline()); |
Shahbaz Youssefi | e321940 | 2018-12-08 16:54:14 +0100 | [diff] [blame] | 354 | } |
Shahbaz Youssefi | 611bbaa | 2018-12-06 01:59:53 +0100 | [diff] [blame] | 355 | |
Shahbaz Youssefi | e321940 | 2018-12-08 16:54:14 +0100 | [diff] [blame] | 356 | if (descriptorSet != VK_NULL_HANDLE) |
| 357 | { |
| 358 | commandBuffer->bindDescriptorSets(bindPoint, pipelineLayout.get(), 0, 1, &descriptorSet, 0, |
| 359 | nullptr); |
| 360 | } |
Shahbaz Youssefi | 611bbaa | 2018-12-06 01:59:53 +0100 | [diff] [blame] | 361 | |
Shahbaz Youssefi | e321940 | 2018-12-08 16:54:14 +0100 | [diff] [blame] | 362 | commandBuffer->pushConstants(pipelineLayout.get(), pushConstantsShaderStage, 0, |
Shahbaz Youssefi | 611bbaa | 2018-12-06 01:59:53 +0100 | [diff] [blame] | 363 | pushConstantsSize, pushConstants); |
| 364 | |
| 365 | return angle::Result::Continue; |
| 366 | } |
| 367 | |
Shahbaz Youssefi | e321940 | 2018-12-08 16:54:14 +0100 | [diff] [blame] | 368 | angle::Result UtilsVk::clearBuffer(vk::Context *context, |
| 369 | vk::BufferHelper *dest, |
| 370 | const ClearParameters ¶ms) |
Shahbaz Youssefi | 8f1b7a6 | 2018-11-14 16:02:54 -0500 | [diff] [blame] | 371 | { |
Shahbaz Youssefi | e321940 | 2018-12-08 16:54:14 +0100 | [diff] [blame] | 372 | RendererVk *renderer = context->getRenderer(); |
Shahbaz Youssefi | 8f1b7a6 | 2018-11-14 16:02:54 -0500 | [diff] [blame] | 373 | |
Shahbaz Youssefi | e321940 | 2018-12-08 16:54:14 +0100 | [diff] [blame] | 374 | ANGLE_TRY(ensureBufferClearResourcesInitialized(context)); |
Shahbaz Youssefi | 8f1b7a6 | 2018-11-14 16:02:54 -0500 | [diff] [blame] | 375 | |
| 376 | vk::CommandBuffer *commandBuffer; |
| 377 | ANGLE_TRY(dest->recordCommands(context, &commandBuffer)); |
| 378 | |
| 379 | // Tell dest it's being written to. |
| 380 | dest->onWrite(VK_ACCESS_SHADER_WRITE_BIT); |
| 381 | |
| 382 | const vk::Format &destFormat = dest->getViewFormat(); |
| 383 | |
Shahbaz Youssefi | 611bbaa | 2018-12-06 01:59:53 +0100 | [diff] [blame] | 384 | uint32_t flags = BufferUtils_comp::kIsClear | GetBufferUtilsFlags(params.size, destFormat); |
Shahbaz Youssefi | 8f1b7a6 | 2018-11-14 16:02:54 -0500 | [diff] [blame] | 385 | |
Shahbaz Youssefi | 611bbaa | 2018-12-06 01:59:53 +0100 | [diff] [blame] | 386 | BufferUtilsShaderParams shaderParams; |
Shahbaz Youssefi | 8f1b7a6 | 2018-11-14 16:02:54 -0500 | [diff] [blame] | 387 | shaderParams.destOffset = params.offset; |
| 388 | shaderParams.size = params.size; |
| 389 | shaderParams.clearValue = params.clearValue; |
| 390 | |
| 391 | VkDescriptorSet descriptorSet; |
| 392 | vk::SharedDescriptorPoolBinding descriptorPoolBinding; |
| 393 | ANGLE_TRY(mDescriptorPools[Function::BufferClear].allocateSets( |
| 394 | context, mDescriptorSetLayouts[Function::BufferClear][kSetIndex].get().ptr(), 1, |
| 395 | &descriptorPoolBinding, &descriptorSet)); |
Shahbaz Youssefi | 611bbaa | 2018-12-06 01:59:53 +0100 | [diff] [blame] | 396 | descriptorPoolBinding.get().updateSerial(context->getRenderer()->getCurrentQueueSerial()); |
Shahbaz Youssefi | 8f1b7a6 | 2018-11-14 16:02:54 -0500 | [diff] [blame] | 397 | |
| 398 | VkWriteDescriptorSet writeInfo = {}; |
| 399 | |
| 400 | writeInfo.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET; |
| 401 | writeInfo.dstSet = descriptorSet; |
Shahbaz Youssefi | 611bbaa | 2018-12-06 01:59:53 +0100 | [diff] [blame] | 402 | writeInfo.dstBinding = kBufferClearOutputBinding; |
Shahbaz Youssefi | 8f1b7a6 | 2018-11-14 16:02:54 -0500 | [diff] [blame] | 403 | writeInfo.descriptorCount = 1; |
| 404 | writeInfo.descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER; |
| 405 | writeInfo.pTexelBufferView = dest->getBufferView().ptr(); |
| 406 | |
| 407 | vkUpdateDescriptorSets(context->getDevice(), 1, &writeInfo, 0, nullptr); |
| 408 | |
Shahbaz Youssefi | e321940 | 2018-12-08 16:54:14 +0100 | [diff] [blame] | 409 | vk::RefCounted<vk::ShaderAndSerial> *shader = nullptr; |
| 410 | ANGLE_TRY(renderer->getShaderLibrary().getBufferUtils_comp(context, flags, &shader)); |
| 411 | |
| 412 | ANGLE_TRY(setupProgram(context, Function::BufferClear, shader, nullptr, |
| 413 | &mBufferUtilsPrograms[flags], nullptr, descriptorSet, &shaderParams, |
| 414 | sizeof(shaderParams), commandBuffer)); |
Shahbaz Youssefi | 8f1b7a6 | 2018-11-14 16:02:54 -0500 | [diff] [blame] | 415 | |
| 416 | commandBuffer->dispatch(UnsignedCeilDivide(params.size, 64), 1, 1); |
| 417 | |
| 418 | descriptorPoolBinding.reset(); |
| 419 | |
Jamie Madill | 7c985f5 | 2018-11-29 18:16:17 -0500 | [diff] [blame] | 420 | return angle::Result::Continue; |
Shahbaz Youssefi | 8f1b7a6 | 2018-11-14 16:02:54 -0500 | [diff] [blame] | 421 | } |
| 422 | |
Shahbaz Youssefi | e321940 | 2018-12-08 16:54:14 +0100 | [diff] [blame] | 423 | angle::Result UtilsVk::copyBuffer(vk::Context *context, |
| 424 | vk::BufferHelper *dest, |
| 425 | vk::BufferHelper *src, |
| 426 | const CopyParameters ¶ms) |
Shahbaz Youssefi | 8f1b7a6 | 2018-11-14 16:02:54 -0500 | [diff] [blame] | 427 | { |
Shahbaz Youssefi | e321940 | 2018-12-08 16:54:14 +0100 | [diff] [blame] | 428 | RendererVk *renderer = context->getRenderer(); |
| 429 | |
| 430 | ANGLE_TRY(ensureBufferCopyResourcesInitialized(context)); |
Shahbaz Youssefi | 8f1b7a6 | 2018-11-14 16:02:54 -0500 | [diff] [blame] | 431 | |
| 432 | vk::CommandBuffer *commandBuffer; |
| 433 | ANGLE_TRY(dest->recordCommands(context, &commandBuffer)); |
| 434 | |
| 435 | // Tell src we are going to read from it. |
| 436 | src->onRead(dest, VK_ACCESS_SHADER_READ_BIT); |
| 437 | // Tell dest it's being written to. |
| 438 | dest->onWrite(VK_ACCESS_SHADER_WRITE_BIT); |
| 439 | |
| 440 | const vk::Format &destFormat = dest->getViewFormat(); |
| 441 | const vk::Format &srcFormat = src->getViewFormat(); |
| 442 | |
| 443 | ASSERT(destFormat.vkFormatIsInt == srcFormat.vkFormatIsInt); |
| 444 | ASSERT(destFormat.vkFormatIsUnsigned == srcFormat.vkFormatIsUnsigned); |
| 445 | |
Shahbaz Youssefi | 611bbaa | 2018-12-06 01:59:53 +0100 | [diff] [blame] | 446 | uint32_t flags = BufferUtils_comp::kIsCopy | GetBufferUtilsFlags(params.size, destFormat); |
Shahbaz Youssefi | 8f1b7a6 | 2018-11-14 16:02:54 -0500 | [diff] [blame] | 447 | |
Shahbaz Youssefi | 611bbaa | 2018-12-06 01:59:53 +0100 | [diff] [blame] | 448 | BufferUtilsShaderParams shaderParams; |
Shahbaz Youssefi | 8f1b7a6 | 2018-11-14 16:02:54 -0500 | [diff] [blame] | 449 | shaderParams.destOffset = params.destOffset; |
| 450 | shaderParams.size = params.size; |
| 451 | shaderParams.srcOffset = params.srcOffset; |
| 452 | |
| 453 | VkDescriptorSet descriptorSet; |
| 454 | vk::SharedDescriptorPoolBinding descriptorPoolBinding; |
| 455 | ANGLE_TRY(mDescriptorPools[Function::BufferCopy].allocateSets( |
| 456 | context, mDescriptorSetLayouts[Function::BufferCopy][kSetIndex].get().ptr(), 1, |
| 457 | &descriptorPoolBinding, &descriptorSet)); |
Shahbaz Youssefi | 611bbaa | 2018-12-06 01:59:53 +0100 | [diff] [blame] | 458 | descriptorPoolBinding.get().updateSerial(context->getRenderer()->getCurrentQueueSerial()); |
Shahbaz Youssefi | 8f1b7a6 | 2018-11-14 16:02:54 -0500 | [diff] [blame] | 459 | |
| 460 | VkWriteDescriptorSet writeInfo[2] = {}; |
| 461 | |
| 462 | writeInfo[0].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET; |
| 463 | writeInfo[0].dstSet = descriptorSet; |
Shahbaz Youssefi | 611bbaa | 2018-12-06 01:59:53 +0100 | [diff] [blame] | 464 | writeInfo[0].dstBinding = kBufferCopyDestinationBinding; |
Shahbaz Youssefi | 8f1b7a6 | 2018-11-14 16:02:54 -0500 | [diff] [blame] | 465 | writeInfo[0].descriptorCount = 1; |
| 466 | writeInfo[0].descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER; |
| 467 | writeInfo[0].pTexelBufferView = dest->getBufferView().ptr(); |
| 468 | |
| 469 | writeInfo[1].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET; |
| 470 | writeInfo[1].dstSet = descriptorSet; |
Shahbaz Youssefi | 611bbaa | 2018-12-06 01:59:53 +0100 | [diff] [blame] | 471 | writeInfo[1].dstBinding = kBufferCopySourceBinding; |
Shahbaz Youssefi | 8f1b7a6 | 2018-11-14 16:02:54 -0500 | [diff] [blame] | 472 | writeInfo[1].descriptorCount = 1; |
| 473 | writeInfo[1].descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER; |
| 474 | writeInfo[1].pTexelBufferView = src->getBufferView().ptr(); |
| 475 | |
| 476 | vkUpdateDescriptorSets(context->getDevice(), 2, writeInfo, 0, nullptr); |
| 477 | |
Shahbaz Youssefi | e321940 | 2018-12-08 16:54:14 +0100 | [diff] [blame] | 478 | vk::RefCounted<vk::ShaderAndSerial> *shader = nullptr; |
| 479 | ANGLE_TRY(renderer->getShaderLibrary().getBufferUtils_comp(context, flags, &shader)); |
| 480 | |
| 481 | ANGLE_TRY(setupProgram(context, Function::BufferCopy, shader, nullptr, |
| 482 | &mBufferUtilsPrograms[flags], nullptr, descriptorSet, &shaderParams, |
| 483 | sizeof(shaderParams), commandBuffer)); |
Shahbaz Youssefi | 8f1b7a6 | 2018-11-14 16:02:54 -0500 | [diff] [blame] | 484 | |
| 485 | commandBuffer->dispatch(UnsignedCeilDivide(params.size, 64), 1, 1); |
| 486 | |
| 487 | descriptorPoolBinding.reset(); |
| 488 | |
Jamie Madill | 7c985f5 | 2018-11-29 18:16:17 -0500 | [diff] [blame] | 489 | return angle::Result::Continue; |
Shahbaz Youssefi | 8f1b7a6 | 2018-11-14 16:02:54 -0500 | [diff] [blame] | 490 | } |
| 491 | |
Shahbaz Youssefi | e321940 | 2018-12-08 16:54:14 +0100 | [diff] [blame] | 492 | angle::Result UtilsVk::convertVertexBuffer(vk::Context *context, |
| 493 | vk::BufferHelper *dest, |
| 494 | vk::BufferHelper *src, |
| 495 | const ConvertVertexParameters ¶ms) |
Shahbaz Youssefi | 611bbaa | 2018-12-06 01:59:53 +0100 | [diff] [blame] | 496 | { |
Shahbaz Youssefi | e321940 | 2018-12-08 16:54:14 +0100 | [diff] [blame] | 497 | RendererVk *renderer = context->getRenderer(); |
| 498 | |
| 499 | ANGLE_TRY(ensureConvertVertexResourcesInitialized(context)); |
Shahbaz Youssefi | 611bbaa | 2018-12-06 01:59:53 +0100 | [diff] [blame] | 500 | |
| 501 | vk::CommandBuffer *commandBuffer; |
| 502 | ANGLE_TRY(dest->recordCommands(context, &commandBuffer)); |
| 503 | |
| 504 | // Tell src we are going to read from it. |
| 505 | src->onRead(dest, VK_ACCESS_SHADER_READ_BIT); |
| 506 | // Tell dest it's being written to. |
| 507 | dest->onWrite(VK_ACCESS_SHADER_WRITE_BIT); |
| 508 | |
| 509 | ConvertVertexShaderParams shaderParams; |
| 510 | shaderParams.Ns = params.srcFormat->channelCount(); |
| 511 | shaderParams.Bs = params.srcFormat->pixelBytes / params.srcFormat->channelCount(); |
| 512 | shaderParams.Ss = params.srcStride; |
| 513 | shaderParams.Nd = params.destFormat->channelCount(); |
| 514 | shaderParams.Bd = params.destFormat->pixelBytes / params.destFormat->channelCount(); |
| 515 | shaderParams.Sd = shaderParams.Nd * shaderParams.Bd; |
| 516 | // The component size is expected to either be 1, 2 or 4 bytes. |
| 517 | ASSERT(4 % shaderParams.Bs == 0); |
| 518 | ASSERT(4 % shaderParams.Bd == 0); |
| 519 | shaderParams.Es = 4 / shaderParams.Bs; |
| 520 | shaderParams.Ed = 4 / shaderParams.Bd; |
| 521 | // Total number of output components is simply the number of vertices by number of components in |
| 522 | // each. |
| 523 | shaderParams.componentCount = params.vertexCount * shaderParams.Nd; |
| 524 | // Total number of 4-byte outputs is the number of components divided by how many components can |
| 525 | // fit in a 4-byte value. Note that this value is also the invocation size of the shader. |
| 526 | shaderParams.outputCount = shaderParams.componentCount / shaderParams.Ed; |
| 527 | shaderParams.srcOffset = params.srcOffset; |
| 528 | shaderParams.destOffset = params.destOffset; |
| 529 | |
| 530 | uint32_t flags = GetConvertVertexFlags(params); |
| 531 | |
| 532 | bool isAligned = |
| 533 | shaderParams.outputCount % 64 == 0 && shaderParams.componentCount % shaderParams.Ed == 0; |
| 534 | flags |= isAligned ? ConvertVertex_comp::kIsAligned : 0; |
| 535 | |
| 536 | VkDescriptorSet descriptorSet; |
| 537 | vk::SharedDescriptorPoolBinding descriptorPoolBinding; |
| 538 | ANGLE_TRY(mDescriptorPools[Function::ConvertVertexBuffer].allocateSets( |
| 539 | context, mDescriptorSetLayouts[Function::ConvertVertexBuffer][kSetIndex].get().ptr(), 1, |
| 540 | &descriptorPoolBinding, &descriptorSet)); |
| 541 | descriptorPoolBinding.get().updateSerial(context->getRenderer()->getCurrentQueueSerial()); |
| 542 | |
| 543 | VkWriteDescriptorSet writeInfo = {}; |
| 544 | VkDescriptorBufferInfo buffers[2] = { |
| 545 | {dest->getBuffer().getHandle(), 0, VK_WHOLE_SIZE}, |
| 546 | {src->getBuffer().getHandle(), 0, VK_WHOLE_SIZE}, |
| 547 | }; |
| 548 | static_assert(kConvertVertexDestinationBinding + 1 == kConvertVertexSourceBinding, |
| 549 | "Update write info"); |
| 550 | |
| 551 | writeInfo.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET; |
| 552 | writeInfo.dstSet = descriptorSet; |
| 553 | writeInfo.dstBinding = kConvertVertexDestinationBinding; |
| 554 | writeInfo.descriptorCount = 2; |
| 555 | writeInfo.descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_BUFFER; |
| 556 | writeInfo.pBufferInfo = buffers; |
| 557 | |
| 558 | vkUpdateDescriptorSets(context->getDevice(), 1, &writeInfo, 0, nullptr); |
| 559 | |
Shahbaz Youssefi | e321940 | 2018-12-08 16:54:14 +0100 | [diff] [blame] | 560 | vk::RefCounted<vk::ShaderAndSerial> *shader = nullptr; |
| 561 | ANGLE_TRY(renderer->getShaderLibrary().getConvertVertex_comp(context, flags, &shader)); |
| 562 | |
| 563 | ANGLE_TRY(setupProgram(context, Function::ConvertVertexBuffer, shader, nullptr, |
| 564 | &mConvertVertexPrograms[flags], nullptr, descriptorSet, &shaderParams, |
| 565 | sizeof(shaderParams), commandBuffer)); |
Shahbaz Youssefi | 611bbaa | 2018-12-06 01:59:53 +0100 | [diff] [blame] | 566 | |
| 567 | commandBuffer->dispatch(UnsignedCeilDivide(shaderParams.outputCount, 64), 1, 1); |
| 568 | |
| 569 | descriptorPoolBinding.reset(); |
| 570 | |
| 571 | return angle::Result::Continue; |
| 572 | } |
| 573 | |
Jamie Madill | 85ca189 | 2019-01-16 13:27:15 -0500 | [diff] [blame] | 574 | angle::Result UtilsVk::startRenderPass(ContextVk *contextVk, |
Shahbaz Youssefi | f83a28a | 2018-12-09 03:48:34 +0100 | [diff] [blame] | 575 | vk::ImageHelper *image, |
Shahbaz Youssefi | 4f3b207 | 2019-01-01 14:48:25 -0500 | [diff] [blame] | 576 | const vk::ImageView *imageView, |
Shahbaz Youssefi | f83a28a | 2018-12-09 03:48:34 +0100 | [diff] [blame] | 577 | const vk::RenderPassDesc &renderPassDesc, |
| 578 | const gl::Rectangle &renderArea, |
| 579 | vk::CommandBuffer **commandBufferOut) |
| 580 | { |
Jamie Madill | 85ca189 | 2019-01-16 13:27:15 -0500 | [diff] [blame] | 581 | RendererVk *renderer = contextVk->getRenderer(); |
Shahbaz Youssefi | f83a28a | 2018-12-09 03:48:34 +0100 | [diff] [blame] | 582 | |
| 583 | vk::RenderPass *renderPass = nullptr; |
Jamie Madill | 85ca189 | 2019-01-16 13:27:15 -0500 | [diff] [blame] | 584 | ANGLE_TRY(renderer->getCompatibleRenderPass(contextVk, renderPassDesc, &renderPass)); |
Shahbaz Youssefi | f83a28a | 2018-12-09 03:48:34 +0100 | [diff] [blame] | 585 | |
| 586 | VkFramebufferCreateInfo framebufferInfo = {}; |
| 587 | |
| 588 | framebufferInfo.sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO; |
| 589 | framebufferInfo.flags = 0; |
| 590 | framebufferInfo.renderPass = renderPass->getHandle(); |
| 591 | framebufferInfo.attachmentCount = 1; |
| 592 | framebufferInfo.pAttachments = imageView->ptr(); |
| 593 | framebufferInfo.width = renderArea.x + renderArea.width; |
| 594 | framebufferInfo.height = renderArea.y + renderArea.height; |
| 595 | framebufferInfo.layers = 1; |
| 596 | |
| 597 | vk::Framebuffer framebuffer; |
Jamie Madill | 85ca189 | 2019-01-16 13:27:15 -0500 | [diff] [blame] | 598 | ANGLE_VK_TRY(contextVk, framebuffer.init(contextVk->getDevice(), framebufferInfo)); |
Shahbaz Youssefi | f83a28a | 2018-12-09 03:48:34 +0100 | [diff] [blame] | 599 | |
| 600 | // TODO(jmadill): Proper clear value implementation. http://anglebug.com/2361 |
| 601 | std::vector<VkClearValue> clearValues = {{}}; |
| 602 | ASSERT(clearValues.size() == 1); |
| 603 | |
Jamie Madill | 85ca189 | 2019-01-16 13:27:15 -0500 | [diff] [blame] | 604 | ANGLE_TRY(image->beginRenderPass(contextVk, framebuffer, renderArea, renderPassDesc, |
| 605 | clearValues, commandBufferOut)); |
Shahbaz Youssefi | f83a28a | 2018-12-09 03:48:34 +0100 | [diff] [blame] | 606 | |
| 607 | renderer->releaseObject(renderer->getCurrentQueueSerial(), &framebuffer); |
| 608 | |
| 609 | return angle::Result::Continue; |
| 610 | } |
| 611 | |
Shahbaz Youssefi | e321940 | 2018-12-08 16:54:14 +0100 | [diff] [blame] | 612 | angle::Result UtilsVk::clearImage(ContextVk *contextVk, |
| 613 | FramebufferVk *framebuffer, |
| 614 | const ClearImageParameters ¶ms) |
| 615 | { |
| 616 | RendererVk *renderer = contextVk->getRenderer(); |
| 617 | |
| 618 | ANGLE_TRY(ensureImageClearResourcesInitialized(contextVk)); |
| 619 | |
| 620 | vk::CommandBuffer *commandBuffer; |
Jamie Madill | c759b8b | 2019-01-03 15:16:50 -0500 | [diff] [blame] | 621 | if (!framebuffer->appendToStartedRenderPass(renderer->getCurrentQueueSerial(), &commandBuffer)) |
Shahbaz Youssefi | e321940 | 2018-12-08 16:54:14 +0100 | [diff] [blame] | 622 | { |
Jamie Madill | c09ae15 | 2019-02-01 14:16:32 -0500 | [diff] [blame^] | 623 | ANGLE_TRY(framebuffer->startNewRenderPass(contextVk, &commandBuffer)); |
Shahbaz Youssefi | e321940 | 2018-12-08 16:54:14 +0100 | [diff] [blame] | 624 | } |
| 625 | |
| 626 | ImageClearShaderParams shaderParams; |
| 627 | shaderParams.clearValue = params.clearValue; |
| 628 | |
| 629 | vk::GraphicsPipelineDesc pipelineDesc; |
| 630 | pipelineDesc.initDefaults(); |
Jamie Madill | 3f0c4a5 | 2019-01-10 10:20:35 -0500 | [diff] [blame] | 631 | pipelineDesc.setColorWriteMask(params.colorMaskFlags, *params.alphaMask); |
| 632 | pipelineDesc.setRenderPassDesc(*params.renderPassDesc); |
Shahbaz Youssefi | e321940 | 2018-12-08 16:54:14 +0100 | [diff] [blame] | 633 | |
Jamie Madill | 633d5e6 | 2018-12-23 19:58:01 -0500 | [diff] [blame] | 634 | const gl::Rectangle &renderArea = framebuffer->getFramebuffer()->getRenderPassRenderArea(); |
| 635 | bool invertViewport = contextVk->isViewportFlipEnabledForDrawFBO(); |
| 636 | |
| 637 | VkViewport viewport; |
| 638 | gl_vk::GetViewport(renderArea, 0.0f, 1.0f, invertViewport, params.renderAreaHeight, &viewport); |
| 639 | pipelineDesc.setViewport(viewport); |
| 640 | |
| 641 | VkRect2D scissor; |
| 642 | const gl::State &glState = contextVk->getState(); |
| 643 | gl_vk::GetScissor(glState, invertViewport, renderArea, &scissor); |
| 644 | pipelineDesc.setScissor(scissor); |
| 645 | |
Shahbaz Youssefi | e321940 | 2018-12-08 16:54:14 +0100 | [diff] [blame] | 646 | vk::ShaderLibrary &shaderLibrary = renderer->getShaderLibrary(); |
| 647 | vk::RefCounted<vk::ShaderAndSerial> *vertexShader = nullptr; |
| 648 | vk::RefCounted<vk::ShaderAndSerial> *fragmentShader = nullptr; |
| 649 | ANGLE_TRY(shaderLibrary.getFullScreenQuad_vert(contextVk, 0, &vertexShader)); |
| 650 | ANGLE_TRY(shaderLibrary.getImageClear_frag(contextVk, 0, &fragmentShader)); |
| 651 | |
| 652 | ANGLE_TRY(setupProgram(contextVk, Function::ImageClear, fragmentShader, vertexShader, |
| 653 | &mImageClearProgram, &pipelineDesc, VK_NULL_HANDLE, &shaderParams, |
| 654 | sizeof(shaderParams), commandBuffer)); |
| 655 | |
Shahbaz Youssefi | e321940 | 2018-12-08 16:54:14 +0100 | [diff] [blame] | 656 | commandBuffer->draw(6, 1, 0, 0); |
| 657 | |
| 658 | return angle::Result::Continue; |
| 659 | } |
| 660 | |
Jamie Madill | 85ca189 | 2019-01-16 13:27:15 -0500 | [diff] [blame] | 661 | angle::Result UtilsVk::copyImage(ContextVk *contextVk, |
Shahbaz Youssefi | f83a28a | 2018-12-09 03:48:34 +0100 | [diff] [blame] | 662 | vk::ImageHelper *dest, |
Shahbaz Youssefi | 4f3b207 | 2019-01-01 14:48:25 -0500 | [diff] [blame] | 663 | const vk::ImageView *destView, |
Shahbaz Youssefi | f83a28a | 2018-12-09 03:48:34 +0100 | [diff] [blame] | 664 | vk::ImageHelper *src, |
Shahbaz Youssefi | 4f3b207 | 2019-01-01 14:48:25 -0500 | [diff] [blame] | 665 | const vk::ImageView *srcView, |
Shahbaz Youssefi | f83a28a | 2018-12-09 03:48:34 +0100 | [diff] [blame] | 666 | const CopyImageParameters ¶ms) |
| 667 | { |
Jamie Madill | 85ca189 | 2019-01-16 13:27:15 -0500 | [diff] [blame] | 668 | RendererVk *renderer = contextVk->getRenderer(); |
Shahbaz Youssefi | f83a28a | 2018-12-09 03:48:34 +0100 | [diff] [blame] | 669 | |
Jamie Madill | 85ca189 | 2019-01-16 13:27:15 -0500 | [diff] [blame] | 670 | ANGLE_TRY(ensureImageCopyResourcesInitialized(contextVk)); |
Shahbaz Youssefi | f83a28a | 2018-12-09 03:48:34 +0100 | [diff] [blame] | 671 | |
| 672 | const vk::Format &srcFormat = src->getFormat(); |
| 673 | const vk::Format &destFormat = dest->getFormat(); |
| 674 | |
| 675 | ImageCopyShaderParams shaderParams; |
Shahbaz Youssefi | 4f3b207 | 2019-01-01 14:48:25 -0500 | [diff] [blame] | 676 | shaderParams.flipY = params.srcFlipY || params.destFlipY; |
| 677 | shaderParams.premultiplyAlpha = params.srcPremultiplyAlpha; |
| 678 | shaderParams.unmultiplyAlpha = params.srcUnmultiplyAlpha; |
Shahbaz Youssefi | f83a28a | 2018-12-09 03:48:34 +0100 | [diff] [blame] | 679 | shaderParams.destHasLuminance = destFormat.angleFormat().luminanceBits > 0; |
| 680 | shaderParams.destIsAlpha = |
| 681 | destFormat.angleFormat().isLUMA() && destFormat.angleFormat().alphaBits > 0; |
Shahbaz Youssefi | 4f3b207 | 2019-01-01 14:48:25 -0500 | [diff] [blame] | 682 | shaderParams.destDefaultChannelsMask = GetFormatDefaultChannelMask(destFormat); |
| 683 | shaderParams.srcMip = params.srcMip; |
| 684 | shaderParams.srcLayer = params.srcLayer; |
| 685 | shaderParams.srcOffset[0] = params.srcOffset[0]; |
| 686 | shaderParams.srcOffset[1] = params.srcOffset[1]; |
| 687 | shaderParams.destOffset[0] = params.destOffset[0]; |
| 688 | shaderParams.destOffset[1] = params.destOffset[1]; |
Shahbaz Youssefi | f83a28a | 2018-12-09 03:48:34 +0100 | [diff] [blame] | 689 | |
Shahbaz Youssefi | 4f3b207 | 2019-01-01 14:48:25 -0500 | [diff] [blame] | 690 | ASSERT(!(params.srcFlipY && params.destFlipY)); |
| 691 | if (params.srcFlipY) |
Shahbaz Youssefi | f83a28a | 2018-12-09 03:48:34 +0100 | [diff] [blame] | 692 | { |
| 693 | // If viewport is flipped, the shader expects srcOffset[1] to have the |
| 694 | // last row's index instead of the first's. |
Shahbaz Youssefi | 4f3b207 | 2019-01-01 14:48:25 -0500 | [diff] [blame] | 695 | shaderParams.srcOffset[1] = params.srcHeight - params.srcOffset[1] - 1; |
| 696 | } |
| 697 | else if (params.destFlipY) |
| 698 | { |
| 699 | // If image is flipped during copy, the shader uses the same code path as above, |
| 700 | // with srcOffset being set to the last row's index instead of the first's. |
| 701 | shaderParams.srcOffset[1] = params.srcOffset[1] + params.srcExtents[1] - 1; |
Shahbaz Youssefi | f83a28a | 2018-12-09 03:48:34 +0100 | [diff] [blame] | 702 | } |
| 703 | |
| 704 | uint32_t flags = GetImageCopyFlags(srcFormat, destFormat); |
Shahbaz Youssefi | 4f3b207 | 2019-01-01 14:48:25 -0500 | [diff] [blame] | 705 | flags |= src->getLayerCount() > 1 ? ImageCopy_frag::kSrcIsArray : 0; |
Shahbaz Youssefi | f83a28a | 2018-12-09 03:48:34 +0100 | [diff] [blame] | 706 | |
| 707 | VkDescriptorSet descriptorSet; |
| 708 | vk::SharedDescriptorPoolBinding descriptorPoolBinding; |
| 709 | ANGLE_TRY(mDescriptorPools[Function::ImageCopy].allocateSets( |
Jamie Madill | 85ca189 | 2019-01-16 13:27:15 -0500 | [diff] [blame] | 710 | contextVk, mDescriptorSetLayouts[Function::ImageCopy][kSetIndex].get().ptr(), 1, |
Shahbaz Youssefi | f83a28a | 2018-12-09 03:48:34 +0100 | [diff] [blame] | 711 | &descriptorPoolBinding, &descriptorSet)); |
Jamie Madill | 85ca189 | 2019-01-16 13:27:15 -0500 | [diff] [blame] | 712 | descriptorPoolBinding.get().updateSerial(contextVk->getRenderer()->getCurrentQueueSerial()); |
Shahbaz Youssefi | f83a28a | 2018-12-09 03:48:34 +0100 | [diff] [blame] | 713 | |
| 714 | vk::RenderPassDesc renderPassDesc; |
| 715 | renderPassDesc.setSamples(dest->getSamples()); |
| 716 | renderPassDesc.packAttachment(destFormat); |
| 717 | |
| 718 | vk::GraphicsPipelineDesc pipelineDesc; |
| 719 | pipelineDesc.initDefaults(); |
Jamie Madill | 3f0c4a5 | 2019-01-10 10:20:35 -0500 | [diff] [blame] | 720 | pipelineDesc.setRenderPassDesc(renderPassDesc); |
Shahbaz Youssefi | f83a28a | 2018-12-09 03:48:34 +0100 | [diff] [blame] | 721 | |
| 722 | gl::Rectangle renderArea; |
| 723 | renderArea.x = params.destOffset[0]; |
| 724 | renderArea.y = params.destOffset[1]; |
| 725 | renderArea.width = params.srcExtents[0]; |
| 726 | renderArea.height = params.srcExtents[1]; |
| 727 | |
Jamie Madill | 633d5e6 | 2018-12-23 19:58:01 -0500 | [diff] [blame] | 728 | VkViewport viewport; |
| 729 | gl_vk::GetViewport(renderArea, 0.0f, 1.0f, false, dest->getExtents().height, &viewport); |
| 730 | pipelineDesc.setViewport(viewport); |
| 731 | |
| 732 | VkRect2D scissor = gl_vk::GetRect(renderArea); |
| 733 | pipelineDesc.setScissor(scissor); |
| 734 | |
Shahbaz Youssefi | f83a28a | 2018-12-09 03:48:34 +0100 | [diff] [blame] | 735 | // Change source layout outside render pass |
Shahbaz Youssefi | b5ba549 | 2019-01-02 15:19:22 -0500 | [diff] [blame] | 736 | if (src->getCurrentLayout() != VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL) |
| 737 | { |
| 738 | vk::CommandBuffer *srcLayoutChange; |
Jamie Madill | 85ca189 | 2019-01-16 13:27:15 -0500 | [diff] [blame] | 739 | ANGLE_TRY(src->recordCommands(contextVk, &srcLayoutChange)); |
Shahbaz Youssefi | b5ba549 | 2019-01-02 15:19:22 -0500 | [diff] [blame] | 740 | src->changeLayoutWithStages(VK_IMAGE_ASPECT_COLOR_BIT, |
| 741 | VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL, |
| 742 | VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, |
| 743 | VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT, srcLayoutChange); |
| 744 | } |
Shahbaz Youssefi | f83a28a | 2018-12-09 03:48:34 +0100 | [diff] [blame] | 745 | |
| 746 | // Change destination layout outside render pass as well |
| 747 | vk::CommandBuffer *destLayoutChange; |
Jamie Madill | 85ca189 | 2019-01-16 13:27:15 -0500 | [diff] [blame] | 748 | ANGLE_TRY(dest->recordCommands(contextVk, &destLayoutChange)); |
Shahbaz Youssefi | f83a28a | 2018-12-09 03:48:34 +0100 | [diff] [blame] | 749 | |
| 750 | dest->changeLayoutWithStages(VK_IMAGE_ASPECT_COLOR_BIT, |
| 751 | VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL, |
| 752 | VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, |
| 753 | VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT, destLayoutChange); |
| 754 | |
| 755 | vk::CommandBuffer *commandBuffer; |
Jamie Madill | 85ca189 | 2019-01-16 13:27:15 -0500 | [diff] [blame] | 756 | ANGLE_TRY( |
| 757 | startRenderPass(contextVk, dest, destView, renderPassDesc, renderArea, &commandBuffer)); |
Shahbaz Youssefi | f83a28a | 2018-12-09 03:48:34 +0100 | [diff] [blame] | 758 | |
| 759 | // Source's layout change should happen before rendering |
| 760 | src->addReadDependency(dest); |
| 761 | |
| 762 | VkDescriptorImageInfo imageInfo = {}; |
| 763 | imageInfo.imageView = srcView->getHandle(); |
| 764 | imageInfo.imageLayout = src->getCurrentLayout(); |
| 765 | |
| 766 | VkWriteDescriptorSet writeInfo = {}; |
| 767 | writeInfo.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET; |
| 768 | writeInfo.dstSet = descriptorSet; |
| 769 | writeInfo.dstBinding = kImageCopySourceBinding; |
| 770 | writeInfo.descriptorCount = 1; |
| 771 | writeInfo.descriptorType = VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE; |
| 772 | writeInfo.pImageInfo = &imageInfo; |
| 773 | |
Jamie Madill | 85ca189 | 2019-01-16 13:27:15 -0500 | [diff] [blame] | 774 | vkUpdateDescriptorSets(contextVk->getDevice(), 1, &writeInfo, 0, nullptr); |
Shahbaz Youssefi | f83a28a | 2018-12-09 03:48:34 +0100 | [diff] [blame] | 775 | |
| 776 | vk::ShaderLibrary &shaderLibrary = renderer->getShaderLibrary(); |
| 777 | vk::RefCounted<vk::ShaderAndSerial> *vertexShader = nullptr; |
| 778 | vk::RefCounted<vk::ShaderAndSerial> *fragmentShader = nullptr; |
Jamie Madill | 85ca189 | 2019-01-16 13:27:15 -0500 | [diff] [blame] | 779 | ANGLE_TRY(shaderLibrary.getFullScreenQuad_vert(contextVk, 0, &vertexShader)); |
| 780 | ANGLE_TRY(shaderLibrary.getImageCopy_frag(contextVk, flags, &fragmentShader)); |
Shahbaz Youssefi | f83a28a | 2018-12-09 03:48:34 +0100 | [diff] [blame] | 781 | |
Jamie Madill | 85ca189 | 2019-01-16 13:27:15 -0500 | [diff] [blame] | 782 | ANGLE_TRY(setupProgram(contextVk, Function::ImageCopy, fragmentShader, vertexShader, |
Shahbaz Youssefi | f83a28a | 2018-12-09 03:48:34 +0100 | [diff] [blame] | 783 | &mImageCopyPrograms[flags], &pipelineDesc, descriptorSet, &shaderParams, |
| 784 | sizeof(shaderParams), commandBuffer)); |
| 785 | |
Shahbaz Youssefi | f83a28a | 2018-12-09 03:48:34 +0100 | [diff] [blame] | 786 | commandBuffer->draw(6, 1, 0, 0); |
| 787 | |
| 788 | descriptorPoolBinding.reset(); |
| 789 | |
| 790 | return angle::Result::Continue; |
| 791 | } |
| 792 | |
Shahbaz Youssefi | 8f1b7a6 | 2018-11-14 16:02:54 -0500 | [diff] [blame] | 793 | } // namespace rx |