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 | // |
| 6 | // DispatchUtilsVk.cpp: |
| 7 | // Implements the DispatchUtilsVk class. |
| 8 | // |
| 9 | |
| 10 | #include "libANGLE/renderer/vulkan/DispatchUtilsVk.h" |
| 11 | |
Shahbaz Youssefi | 8f1b7a6 | 2018-11-14 16:02:54 -0500 | [diff] [blame] | 12 | #include "libANGLE/renderer/vulkan/RendererVk.h" |
Shahbaz Youssefi | 8f1b7a6 | 2018-11-14 16:02:54 -0500 | [diff] [blame] | 13 | |
| 14 | namespace rx |
| 15 | { |
| 16 | |
Shahbaz Youssefi | 611bbaa | 2018-12-06 01:59:53 +0100 | [diff] [blame^] | 17 | namespace BufferUtils_comp = vk::InternalShader::BufferUtils_comp; |
| 18 | namespace ConvertVertex_comp = vk::InternalShader::ConvertVertex_comp; |
Shahbaz Youssefi | 8f1b7a6 | 2018-11-14 16:02:54 -0500 | [diff] [blame] | 19 | |
| 20 | namespace |
| 21 | { |
| 22 | // All internal shaders assume there is only one descriptor set, indexed at 0 |
| 23 | constexpr uint32_t kSetIndex = 0; |
| 24 | |
Shahbaz Youssefi | 611bbaa | 2018-12-06 01:59:53 +0100 | [diff] [blame^] | 25 | constexpr uint32_t kBufferClearOutputBinding = 0; |
| 26 | constexpr uint32_t kBufferCopyDestinationBinding = 0; |
| 27 | constexpr uint32_t kBufferCopySourceBinding = 1; |
| 28 | constexpr uint32_t kConvertVertexDestinationBinding = 0; |
| 29 | constexpr uint32_t kConvertVertexSourceBinding = 1; |
Shahbaz Youssefi | 8f1b7a6 | 2018-11-14 16:02:54 -0500 | [diff] [blame] | 30 | |
| 31 | uint32_t GetBufferUtilsFlags(size_t dispatchSize, const vk::Format &format) |
| 32 | { |
| 33 | uint32_t flags = dispatchSize % 64 == 0 ? BufferUtils_comp::kIsAligned : 0; |
| 34 | const angle::Format &bufferFormat = format.bufferFormat(); |
| 35 | |
| 36 | flags |= bufferFormat.componentType == GL_INT |
| 37 | ? BufferUtils_comp::kIsInt |
| 38 | : bufferFormat.componentType == GL_UNSIGNED_INT ? BufferUtils_comp::kIsUint |
| 39 | : BufferUtils_comp::kIsFloat; |
| 40 | |
| 41 | return flags; |
| 42 | } |
Shahbaz Youssefi | 611bbaa | 2018-12-06 01:59:53 +0100 | [diff] [blame^] | 43 | |
| 44 | uint32_t GetConvertVertexFlags(const DispatchUtilsVk::ConvertVertexParameters ¶ms) |
| 45 | { |
| 46 | bool srcIsInt = params.srcFormat->componentType == GL_INT; |
| 47 | bool srcIsUint = params.srcFormat->componentType == GL_UNSIGNED_INT; |
| 48 | bool srcIsSnorm = params.srcFormat->componentType == GL_SIGNED_NORMALIZED; |
| 49 | bool srcIsUnorm = params.srcFormat->componentType == GL_UNSIGNED_NORMALIZED; |
| 50 | bool srcIsFixed = params.srcFormat->isFixed; |
| 51 | bool srcIsFloat = params.srcFormat->componentType == GL_FLOAT; |
| 52 | |
| 53 | bool destIsInt = params.destFormat->componentType == GL_INT; |
| 54 | bool destIsUint = params.destFormat->componentType == GL_UNSIGNED_INT; |
| 55 | bool destIsFloat = params.destFormat->componentType == GL_FLOAT; |
| 56 | |
| 57 | // Assert on the types to make sure the shader supports its. These are based on |
| 58 | // ConvertVertex_comp::Conversion values. |
| 59 | ASSERT(!destIsInt || srcIsInt); // If destination is int, src must be int too |
| 60 | ASSERT(!destIsUint || srcIsUint); // If destination is uint, src must be uint too |
| 61 | ASSERT(!srcIsFixed || destIsFloat); // If source is fixed, dest must be float |
| 62 | // One of each bool set must be true |
| 63 | ASSERT(srcIsInt || srcIsUint || srcIsSnorm || srcIsUnorm || srcIsFixed || srcIsFloat); |
| 64 | ASSERT(destIsInt || destIsUint || destIsFloat); |
| 65 | |
| 66 | // We currently don't have any big-endian devices in the list of supported platforms. The |
| 67 | // shader is capable of supporting big-endian architectures, but the relevant flag (IsBigEndian) |
| 68 | // is not added to the build configuration file (to reduce binary size). If necessary, add |
| 69 | // IsBigEndian to ConvertVertex.comp.json and select the appropriate flag based on the |
| 70 | // endian-ness test here. |
| 71 | uint32_t endiannessTest = 0; |
| 72 | *reinterpret_cast<uint8_t *>(&endiannessTest) = 1; |
| 73 | ASSERT(endiannessTest == 1); |
| 74 | |
| 75 | uint32_t flags = 0; |
| 76 | |
| 77 | if (srcIsInt && destIsInt) |
| 78 | { |
| 79 | flags |= ConvertVertex_comp::kIntToInt; |
| 80 | } |
| 81 | else if (srcIsUint && destIsUint) |
| 82 | { |
| 83 | flags |= ConvertVertex_comp::kUintToUint; |
| 84 | } |
| 85 | else if (srcIsInt) |
| 86 | { |
| 87 | flags |= ConvertVertex_comp::kIntToFloat; |
| 88 | } |
| 89 | else if (srcIsUint) |
| 90 | { |
| 91 | flags |= ConvertVertex_comp::kUintToFloat; |
| 92 | } |
| 93 | else if (srcIsSnorm) |
| 94 | { |
| 95 | flags |= ConvertVertex_comp::kSnormToFloat; |
| 96 | } |
| 97 | else if (srcIsUnorm) |
| 98 | { |
| 99 | flags |= ConvertVertex_comp::kUnormToFloat; |
| 100 | } |
| 101 | else if (srcIsFixed) |
| 102 | { |
| 103 | flags |= ConvertVertex_comp::kFixedToFloat; |
| 104 | } |
| 105 | else if (srcIsFloat) |
| 106 | { |
| 107 | flags |= ConvertVertex_comp::kFloatToFloat; |
| 108 | } |
| 109 | else |
| 110 | { |
| 111 | UNREACHABLE(); |
| 112 | } |
| 113 | |
| 114 | return flags; |
| 115 | } |
Shahbaz Youssefi | 8f1b7a6 | 2018-11-14 16:02:54 -0500 | [diff] [blame] | 116 | } // namespace |
| 117 | |
| 118 | DispatchUtilsVk::DispatchUtilsVk() = default; |
| 119 | |
| 120 | DispatchUtilsVk::~DispatchUtilsVk() = default; |
| 121 | |
| 122 | void DispatchUtilsVk::destroy(VkDevice device) |
| 123 | { |
| 124 | for (Function f : angle::AllEnums<Function>()) |
| 125 | { |
| 126 | for (auto &descriptorSetLayout : mDescriptorSetLayouts[f]) |
| 127 | { |
| 128 | descriptorSetLayout.reset(); |
| 129 | } |
| 130 | mPipelineLayouts[f].reset(); |
| 131 | mDescriptorPools[f].destroy(device); |
| 132 | } |
| 133 | |
Shahbaz Youssefi | 611bbaa | 2018-12-06 01:59:53 +0100 | [diff] [blame^] | 134 | for (vk::ShaderProgramHelper &program : mBufferUtilsPrograms) |
| 135 | { |
| 136 | program.destroy(device); |
| 137 | } |
| 138 | for (vk::ShaderProgramHelper &program : mConvertVertexPrograms) |
Shahbaz Youssefi | 8f1b7a6 | 2018-11-14 16:02:54 -0500 | [diff] [blame] | 139 | { |
| 140 | program.destroy(device); |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | angle::Result DispatchUtilsVk::ensureResourcesInitialized(vk::Context *context, |
| 145 | Function function, |
| 146 | VkDescriptorPoolSize *setSizes, |
Shahbaz Youssefi | 611bbaa | 2018-12-06 01:59:53 +0100 | [diff] [blame^] | 147 | size_t setSizesCount, |
| 148 | size_t pushConstantsSize) |
Shahbaz Youssefi | 8f1b7a6 | 2018-11-14 16:02:54 -0500 | [diff] [blame] | 149 | { |
| 150 | RendererVk *renderer = context->getRenderer(); |
| 151 | |
| 152 | vk::DescriptorSetLayoutDesc descriptorSetDesc; |
| 153 | |
| 154 | uint32_t currentBinding = 0; |
| 155 | for (size_t i = 0; i < setSizesCount; ++i) |
| 156 | { |
| 157 | descriptorSetDesc.update(currentBinding, setSizes[i].type, setSizes[i].descriptorCount); |
| 158 | currentBinding += setSizes[i].descriptorCount; |
| 159 | } |
| 160 | |
| 161 | ANGLE_TRY(renderer->getDescriptorSetLayout(context, descriptorSetDesc, |
| 162 | &mDescriptorSetLayouts[function][kSetIndex])); |
| 163 | |
| 164 | // Corresponding pipeline layouts: |
| 165 | vk::PipelineLayoutDesc pipelineLayoutDesc; |
| 166 | |
| 167 | pipelineLayoutDesc.updateDescriptorSetLayout(kSetIndex, descriptorSetDesc); |
Shahbaz Youssefi | 611bbaa | 2018-12-06 01:59:53 +0100 | [diff] [blame^] | 168 | pipelineLayoutDesc.updatePushConstantRange(gl::ShaderType::Compute, 0, pushConstantsSize); |
Shahbaz Youssefi | 8f1b7a6 | 2018-11-14 16:02:54 -0500 | [diff] [blame] | 169 | |
| 170 | ANGLE_TRY(renderer->getPipelineLayout( |
| 171 | context, pipelineLayoutDesc, mDescriptorSetLayouts[function], &mPipelineLayouts[function])); |
| 172 | |
| 173 | ANGLE_TRY(mDescriptorPools[function].init(context, setSizes, setSizesCount)); |
| 174 | |
Jamie Madill | 7c985f5 | 2018-11-29 18:16:17 -0500 | [diff] [blame] | 175 | return angle::Result::Continue; |
Shahbaz Youssefi | 8f1b7a6 | 2018-11-14 16:02:54 -0500 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | angle::Result DispatchUtilsVk::ensureBufferClearInitialized(vk::Context *context) |
| 179 | { |
| 180 | if (mPipelineLayouts[Function::BufferClear].valid()) |
| 181 | { |
Jamie Madill | 7c985f5 | 2018-11-29 18:16:17 -0500 | [diff] [blame] | 182 | return angle::Result::Continue; |
Shahbaz Youssefi | 8f1b7a6 | 2018-11-14 16:02:54 -0500 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | VkDescriptorPoolSize setSizes[1] = { |
| 186 | {VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER, 1}, |
| 187 | }; |
| 188 | |
Shahbaz Youssefi | 611bbaa | 2018-12-06 01:59:53 +0100 | [diff] [blame^] | 189 | return ensureResourcesInitialized(context, Function::BufferClear, setSizes, ArraySize(setSizes), |
| 190 | sizeof(BufferUtilsShaderParams)); |
Shahbaz Youssefi | 8f1b7a6 | 2018-11-14 16:02:54 -0500 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | angle::Result DispatchUtilsVk::ensureBufferCopyInitialized(vk::Context *context) |
| 194 | { |
| 195 | if (mPipelineLayouts[Function::BufferCopy].valid()) |
| 196 | { |
Jamie Madill | 7c985f5 | 2018-11-29 18:16:17 -0500 | [diff] [blame] | 197 | return angle::Result::Continue; |
Shahbaz Youssefi | 8f1b7a6 | 2018-11-14 16:02:54 -0500 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | VkDescriptorPoolSize setSizes[2] = { |
| 201 | {VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER, 1}, |
| 202 | {VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER, 1}, |
| 203 | }; |
| 204 | |
Shahbaz Youssefi | 611bbaa | 2018-12-06 01:59:53 +0100 | [diff] [blame^] | 205 | return ensureResourcesInitialized(context, Function::BufferCopy, setSizes, ArraySize(setSizes), |
| 206 | sizeof(BufferUtilsShaderParams)); |
Shahbaz Youssefi | 8f1b7a6 | 2018-11-14 16:02:54 -0500 | [diff] [blame] | 207 | } |
| 208 | |
Shahbaz Youssefi | 611bbaa | 2018-12-06 01:59:53 +0100 | [diff] [blame^] | 209 | angle::Result DispatchUtilsVk::ensureConvertVertexInitialized(vk::Context *context) |
| 210 | { |
| 211 | if (mPipelineLayouts[Function::ConvertVertexBuffer].valid()) |
| 212 | { |
| 213 | return angle::Result::Continue; |
| 214 | } |
| 215 | |
| 216 | VkDescriptorPoolSize setSizes[2] = { |
| 217 | {VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, 1}, |
| 218 | {VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, 1}, |
| 219 | }; |
| 220 | |
| 221 | return ensureResourcesInitialized(context, Function::ConvertVertexBuffer, setSizes, |
| 222 | ArraySize(setSizes), sizeof(ConvertVertexShaderParams)); |
| 223 | } |
| 224 | |
| 225 | angle::Result DispatchUtilsVk::setupProgramCommon(vk::Context *context, |
| 226 | Function function, |
| 227 | vk::RefCounted<vk::ShaderAndSerial> *shader, |
| 228 | vk::ShaderProgramHelper *program, |
| 229 | const VkDescriptorSet descriptorSet, |
| 230 | const void *pushConstants, |
| 231 | size_t pushConstantsSize, |
| 232 | vk::CommandBuffer *commandBuffer) |
| 233 | { |
| 234 | RendererVk *renderer = context->getRenderer(); |
| 235 | |
| 236 | program->setShader(gl::ShaderType::Compute, shader); |
| 237 | |
| 238 | const vk::BindingPointer<vk::PipelineLayout> &pipelineLayout = mPipelineLayouts[function]; |
| 239 | |
| 240 | vk::PipelineAndSerial *pipelineAndSerial; |
| 241 | ANGLE_TRY(program->getComputePipeline(context, pipelineLayout.get(), &pipelineAndSerial)); |
| 242 | |
| 243 | commandBuffer->bindPipeline(VK_PIPELINE_BIND_POINT_COMPUTE, pipelineAndSerial->get()); |
| 244 | pipelineAndSerial->updateSerial(renderer->getCurrentQueueSerial()); |
| 245 | |
| 246 | commandBuffer->bindDescriptorSets(VK_PIPELINE_BIND_POINT_COMPUTE, pipelineLayout.get(), 0, 1, |
| 247 | &descriptorSet, 0, nullptr); |
| 248 | |
| 249 | commandBuffer->pushConstants(pipelineLayout.get(), VK_SHADER_STAGE_COMPUTE_BIT, 0, |
| 250 | pushConstantsSize, pushConstants); |
| 251 | |
| 252 | return angle::Result::Continue; |
| 253 | } |
| 254 | |
| 255 | template <angle::Result (vk::ShaderLibrary::*getShader)(vk::Context *, |
| 256 | uint32_t, |
| 257 | vk::RefCounted<vk::ShaderAndSerial> **), |
| 258 | DispatchUtilsVk::Function function, |
| 259 | typename ShaderParams> |
Shahbaz Youssefi | 8f1b7a6 | 2018-11-14 16:02:54 -0500 | [diff] [blame] | 260 | angle::Result DispatchUtilsVk::setupProgram(vk::Context *context, |
Shahbaz Youssefi | 611bbaa | 2018-12-06 01:59:53 +0100 | [diff] [blame^] | 261 | vk::ShaderProgramHelper *program, |
| 262 | uint32_t flags, |
Shahbaz Youssefi | 8f1b7a6 | 2018-11-14 16:02:54 -0500 | [diff] [blame] | 263 | const VkDescriptorSet &descriptorSet, |
| 264 | const ShaderParams ¶ms, |
| 265 | vk::CommandBuffer *commandBuffer) |
| 266 | { |
| 267 | RendererVk *renderer = context->getRenderer(); |
| 268 | vk::ShaderLibrary &shaderLibrary = renderer->getShaderLibrary(); |
| 269 | |
| 270 | vk::RefCounted<vk::ShaderAndSerial> *shader = nullptr; |
Shahbaz Youssefi | 611bbaa | 2018-12-06 01:59:53 +0100 | [diff] [blame^] | 271 | ANGLE_TRY((shaderLibrary.*getShader)(context, flags, &shader)); |
Shahbaz Youssefi | 8f1b7a6 | 2018-11-14 16:02:54 -0500 | [diff] [blame] | 272 | |
Shahbaz Youssefi | 611bbaa | 2018-12-06 01:59:53 +0100 | [diff] [blame^] | 273 | ANGLE_TRY(setupProgramCommon(context, function, shader, program, descriptorSet, ¶ms, |
| 274 | sizeof(params), commandBuffer)); |
Shahbaz Youssefi | 8f1b7a6 | 2018-11-14 16:02:54 -0500 | [diff] [blame] | 275 | |
Jamie Madill | 7c985f5 | 2018-11-29 18:16:17 -0500 | [diff] [blame] | 276 | return angle::Result::Continue; |
Shahbaz Youssefi | 8f1b7a6 | 2018-11-14 16:02:54 -0500 | [diff] [blame] | 277 | } |
| 278 | |
| 279 | angle::Result DispatchUtilsVk::clearBuffer(vk::Context *context, |
| 280 | vk::BufferHelper *dest, |
| 281 | const ClearParameters ¶ms) |
| 282 | { |
| 283 | ANGLE_TRY(ensureBufferClearInitialized(context)); |
| 284 | |
| 285 | vk::CommandBuffer *commandBuffer; |
| 286 | ANGLE_TRY(dest->recordCommands(context, &commandBuffer)); |
| 287 | |
| 288 | // Tell dest it's being written to. |
| 289 | dest->onWrite(VK_ACCESS_SHADER_WRITE_BIT); |
| 290 | |
| 291 | const vk::Format &destFormat = dest->getViewFormat(); |
| 292 | |
Shahbaz Youssefi | 611bbaa | 2018-12-06 01:59:53 +0100 | [diff] [blame^] | 293 | uint32_t flags = BufferUtils_comp::kIsClear | GetBufferUtilsFlags(params.size, destFormat); |
Shahbaz Youssefi | 8f1b7a6 | 2018-11-14 16:02:54 -0500 | [diff] [blame] | 294 | |
Shahbaz Youssefi | 611bbaa | 2018-12-06 01:59:53 +0100 | [diff] [blame^] | 295 | BufferUtilsShaderParams shaderParams; |
Shahbaz Youssefi | 8f1b7a6 | 2018-11-14 16:02:54 -0500 | [diff] [blame] | 296 | shaderParams.destOffset = params.offset; |
| 297 | shaderParams.size = params.size; |
| 298 | shaderParams.clearValue = params.clearValue; |
| 299 | |
| 300 | VkDescriptorSet descriptorSet; |
| 301 | vk::SharedDescriptorPoolBinding descriptorPoolBinding; |
| 302 | ANGLE_TRY(mDescriptorPools[Function::BufferClear].allocateSets( |
| 303 | context, mDescriptorSetLayouts[Function::BufferClear][kSetIndex].get().ptr(), 1, |
| 304 | &descriptorPoolBinding, &descriptorSet)); |
Shahbaz Youssefi | 611bbaa | 2018-12-06 01:59:53 +0100 | [diff] [blame^] | 305 | descriptorPoolBinding.get().updateSerial(context->getRenderer()->getCurrentQueueSerial()); |
Shahbaz Youssefi | 8f1b7a6 | 2018-11-14 16:02:54 -0500 | [diff] [blame] | 306 | |
| 307 | VkWriteDescriptorSet writeInfo = {}; |
| 308 | |
| 309 | writeInfo.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET; |
| 310 | writeInfo.dstSet = descriptorSet; |
Shahbaz Youssefi | 611bbaa | 2018-12-06 01:59:53 +0100 | [diff] [blame^] | 311 | writeInfo.dstBinding = kBufferClearOutputBinding; |
Shahbaz Youssefi | 8f1b7a6 | 2018-11-14 16:02:54 -0500 | [diff] [blame] | 312 | writeInfo.descriptorCount = 1; |
| 313 | writeInfo.descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER; |
| 314 | writeInfo.pTexelBufferView = dest->getBufferView().ptr(); |
| 315 | |
| 316 | vkUpdateDescriptorSets(context->getDevice(), 1, &writeInfo, 0, nullptr); |
| 317 | |
Shahbaz Youssefi | 611bbaa | 2018-12-06 01:59:53 +0100 | [diff] [blame^] | 318 | ANGLE_TRY((setupProgram<&vk::ShaderLibrary::getBufferUtils_comp, Function::BufferClear, |
| 319 | BufferUtilsShaderParams>(context, &mBufferUtilsPrograms[flags], flags, |
| 320 | descriptorSet, shaderParams, commandBuffer))); |
Shahbaz Youssefi | 8f1b7a6 | 2018-11-14 16:02:54 -0500 | [diff] [blame] | 321 | |
| 322 | commandBuffer->dispatch(UnsignedCeilDivide(params.size, 64), 1, 1); |
| 323 | |
| 324 | descriptorPoolBinding.reset(); |
| 325 | |
Jamie Madill | 7c985f5 | 2018-11-29 18:16:17 -0500 | [diff] [blame] | 326 | return angle::Result::Continue; |
Shahbaz Youssefi | 8f1b7a6 | 2018-11-14 16:02:54 -0500 | [diff] [blame] | 327 | } |
| 328 | |
| 329 | angle::Result DispatchUtilsVk::copyBuffer(vk::Context *context, |
| 330 | vk::BufferHelper *dest, |
| 331 | vk::BufferHelper *src, |
| 332 | const CopyParameters ¶ms) |
| 333 | { |
| 334 | ANGLE_TRY(ensureBufferCopyInitialized(context)); |
| 335 | |
| 336 | vk::CommandBuffer *commandBuffer; |
| 337 | ANGLE_TRY(dest->recordCommands(context, &commandBuffer)); |
| 338 | |
| 339 | // Tell src we are going to read from it. |
| 340 | src->onRead(dest, VK_ACCESS_SHADER_READ_BIT); |
| 341 | // Tell dest it's being written to. |
| 342 | dest->onWrite(VK_ACCESS_SHADER_WRITE_BIT); |
| 343 | |
| 344 | const vk::Format &destFormat = dest->getViewFormat(); |
| 345 | const vk::Format &srcFormat = src->getViewFormat(); |
| 346 | |
| 347 | ASSERT(destFormat.vkFormatIsInt == srcFormat.vkFormatIsInt); |
| 348 | ASSERT(destFormat.vkFormatIsUnsigned == srcFormat.vkFormatIsUnsigned); |
| 349 | |
Shahbaz Youssefi | 611bbaa | 2018-12-06 01:59:53 +0100 | [diff] [blame^] | 350 | uint32_t flags = BufferUtils_comp::kIsCopy | GetBufferUtilsFlags(params.size, destFormat); |
Shahbaz Youssefi | 8f1b7a6 | 2018-11-14 16:02:54 -0500 | [diff] [blame] | 351 | |
Shahbaz Youssefi | 611bbaa | 2018-12-06 01:59:53 +0100 | [diff] [blame^] | 352 | BufferUtilsShaderParams shaderParams; |
Shahbaz Youssefi | 8f1b7a6 | 2018-11-14 16:02:54 -0500 | [diff] [blame] | 353 | shaderParams.destOffset = params.destOffset; |
| 354 | shaderParams.size = params.size; |
| 355 | shaderParams.srcOffset = params.srcOffset; |
| 356 | |
| 357 | VkDescriptorSet descriptorSet; |
| 358 | vk::SharedDescriptorPoolBinding descriptorPoolBinding; |
| 359 | ANGLE_TRY(mDescriptorPools[Function::BufferCopy].allocateSets( |
| 360 | context, mDescriptorSetLayouts[Function::BufferCopy][kSetIndex].get().ptr(), 1, |
| 361 | &descriptorPoolBinding, &descriptorSet)); |
Shahbaz Youssefi | 611bbaa | 2018-12-06 01:59:53 +0100 | [diff] [blame^] | 362 | descriptorPoolBinding.get().updateSerial(context->getRenderer()->getCurrentQueueSerial()); |
Shahbaz Youssefi | 8f1b7a6 | 2018-11-14 16:02:54 -0500 | [diff] [blame] | 363 | |
| 364 | VkWriteDescriptorSet writeInfo[2] = {}; |
| 365 | |
| 366 | writeInfo[0].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET; |
| 367 | writeInfo[0].dstSet = descriptorSet; |
Shahbaz Youssefi | 611bbaa | 2018-12-06 01:59:53 +0100 | [diff] [blame^] | 368 | writeInfo[0].dstBinding = kBufferCopyDestinationBinding; |
Shahbaz Youssefi | 8f1b7a6 | 2018-11-14 16:02:54 -0500 | [diff] [blame] | 369 | writeInfo[0].descriptorCount = 1; |
| 370 | writeInfo[0].descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER; |
| 371 | writeInfo[0].pTexelBufferView = dest->getBufferView().ptr(); |
| 372 | |
| 373 | writeInfo[1].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET; |
| 374 | writeInfo[1].dstSet = descriptorSet; |
Shahbaz Youssefi | 611bbaa | 2018-12-06 01:59:53 +0100 | [diff] [blame^] | 375 | writeInfo[1].dstBinding = kBufferCopySourceBinding; |
Shahbaz Youssefi | 8f1b7a6 | 2018-11-14 16:02:54 -0500 | [diff] [blame] | 376 | writeInfo[1].descriptorCount = 1; |
| 377 | writeInfo[1].descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER; |
| 378 | writeInfo[1].pTexelBufferView = src->getBufferView().ptr(); |
| 379 | |
| 380 | vkUpdateDescriptorSets(context->getDevice(), 2, writeInfo, 0, nullptr); |
| 381 | |
Shahbaz Youssefi | 611bbaa | 2018-12-06 01:59:53 +0100 | [diff] [blame^] | 382 | ANGLE_TRY((setupProgram<&vk::ShaderLibrary::getBufferUtils_comp, Function::BufferCopy, |
| 383 | BufferUtilsShaderParams>(context, &mBufferUtilsPrograms[flags], flags, |
| 384 | descriptorSet, shaderParams, commandBuffer))); |
Shahbaz Youssefi | 8f1b7a6 | 2018-11-14 16:02:54 -0500 | [diff] [blame] | 385 | |
| 386 | commandBuffer->dispatch(UnsignedCeilDivide(params.size, 64), 1, 1); |
| 387 | |
| 388 | descriptorPoolBinding.reset(); |
| 389 | |
Jamie Madill | 7c985f5 | 2018-11-29 18:16:17 -0500 | [diff] [blame] | 390 | return angle::Result::Continue; |
Shahbaz Youssefi | 8f1b7a6 | 2018-11-14 16:02:54 -0500 | [diff] [blame] | 391 | } |
| 392 | |
Shahbaz Youssefi | 611bbaa | 2018-12-06 01:59:53 +0100 | [diff] [blame^] | 393 | angle::Result DispatchUtilsVk::convertVertexBuffer(vk::Context *context, |
| 394 | vk::BufferHelper *dest, |
| 395 | vk::BufferHelper *src, |
| 396 | const ConvertVertexParameters ¶ms) |
| 397 | { |
| 398 | ANGLE_TRY(ensureConvertVertexInitialized(context)); |
| 399 | |
| 400 | vk::CommandBuffer *commandBuffer; |
| 401 | ANGLE_TRY(dest->recordCommands(context, &commandBuffer)); |
| 402 | |
| 403 | // Tell src we are going to read from it. |
| 404 | src->onRead(dest, VK_ACCESS_SHADER_READ_BIT); |
| 405 | // Tell dest it's being written to. |
| 406 | dest->onWrite(VK_ACCESS_SHADER_WRITE_BIT); |
| 407 | |
| 408 | ConvertVertexShaderParams shaderParams; |
| 409 | shaderParams.Ns = params.srcFormat->channelCount(); |
| 410 | shaderParams.Bs = params.srcFormat->pixelBytes / params.srcFormat->channelCount(); |
| 411 | shaderParams.Ss = params.srcStride; |
| 412 | shaderParams.Nd = params.destFormat->channelCount(); |
| 413 | shaderParams.Bd = params.destFormat->pixelBytes / params.destFormat->channelCount(); |
| 414 | shaderParams.Sd = shaderParams.Nd * shaderParams.Bd; |
| 415 | // The component size is expected to either be 1, 2 or 4 bytes. |
| 416 | ASSERT(4 % shaderParams.Bs == 0); |
| 417 | ASSERT(4 % shaderParams.Bd == 0); |
| 418 | shaderParams.Es = 4 / shaderParams.Bs; |
| 419 | shaderParams.Ed = 4 / shaderParams.Bd; |
| 420 | // Total number of output components is simply the number of vertices by number of components in |
| 421 | // each. |
| 422 | shaderParams.componentCount = params.vertexCount * shaderParams.Nd; |
| 423 | // Total number of 4-byte outputs is the number of components divided by how many components can |
| 424 | // fit in a 4-byte value. Note that this value is also the invocation size of the shader. |
| 425 | shaderParams.outputCount = shaderParams.componentCount / shaderParams.Ed; |
| 426 | shaderParams.srcOffset = params.srcOffset; |
| 427 | shaderParams.destOffset = params.destOffset; |
| 428 | |
| 429 | uint32_t flags = GetConvertVertexFlags(params); |
| 430 | |
| 431 | bool isAligned = |
| 432 | shaderParams.outputCount % 64 == 0 && shaderParams.componentCount % shaderParams.Ed == 0; |
| 433 | flags |= isAligned ? ConvertVertex_comp::kIsAligned : 0; |
| 434 | |
| 435 | VkDescriptorSet descriptorSet; |
| 436 | vk::SharedDescriptorPoolBinding descriptorPoolBinding; |
| 437 | ANGLE_TRY(mDescriptorPools[Function::ConvertVertexBuffer].allocateSets( |
| 438 | context, mDescriptorSetLayouts[Function::ConvertVertexBuffer][kSetIndex].get().ptr(), 1, |
| 439 | &descriptorPoolBinding, &descriptorSet)); |
| 440 | descriptorPoolBinding.get().updateSerial(context->getRenderer()->getCurrentQueueSerial()); |
| 441 | |
| 442 | VkWriteDescriptorSet writeInfo = {}; |
| 443 | VkDescriptorBufferInfo buffers[2] = { |
| 444 | {dest->getBuffer().getHandle(), 0, VK_WHOLE_SIZE}, |
| 445 | {src->getBuffer().getHandle(), 0, VK_WHOLE_SIZE}, |
| 446 | }; |
| 447 | static_assert(kConvertVertexDestinationBinding + 1 == kConvertVertexSourceBinding, |
| 448 | "Update write info"); |
| 449 | |
| 450 | writeInfo.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET; |
| 451 | writeInfo.dstSet = descriptorSet; |
| 452 | writeInfo.dstBinding = kConvertVertexDestinationBinding; |
| 453 | writeInfo.descriptorCount = 2; |
| 454 | writeInfo.descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_BUFFER; |
| 455 | writeInfo.pBufferInfo = buffers; |
| 456 | |
| 457 | vkUpdateDescriptorSets(context->getDevice(), 1, &writeInfo, 0, nullptr); |
| 458 | |
| 459 | ANGLE_TRY( |
| 460 | (setupProgram<&vk::ShaderLibrary::getConvertVertex_comp, Function::ConvertVertexBuffer, |
| 461 | ConvertVertexShaderParams>(context, &mConvertVertexPrograms[flags], flags, |
| 462 | descriptorSet, shaderParams, commandBuffer))); |
| 463 | |
| 464 | commandBuffer->dispatch(UnsignedCeilDivide(shaderParams.outputCount, 64), 1, 1); |
| 465 | |
| 466 | descriptorPoolBinding.reset(); |
| 467 | |
| 468 | return angle::Result::Continue; |
| 469 | } |
| 470 | |
Shahbaz Youssefi | 8f1b7a6 | 2018-11-14 16:02:54 -0500 | [diff] [blame] | 471 | } // namespace rx |