Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 1 | // |
| 2 | // Copyright 2016 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 | // ProgramVk.cpp: |
| 7 | // Implements the class methods for ProgramVk. |
| 8 | // |
| 9 | |
| 10 | #include "libANGLE/renderer/vulkan/ProgramVk.h" |
| 11 | |
| 12 | #include "common/debug.h" |
Jamie Madill | c564c07 | 2017-06-01 12:45:42 -0400 | [diff] [blame] | 13 | #include "libANGLE/Context.h" |
Shahbaz Youssefi | 216f73d | 2019-04-12 13:32:30 -0400 | [diff] [blame] | 14 | #include "libANGLE/ProgramLinkedResources.h" |
Luc Ferron | 48cdc2e | 2018-05-31 09:58:34 -0400 | [diff] [blame] | 15 | #include "libANGLE/renderer/renderer_utils.h" |
Shahbaz Youssefi | 216f73d | 2019-04-12 13:32:30 -0400 | [diff] [blame] | 16 | #include "libANGLE/renderer/vulkan/BufferVk.h" |
Jamie Madill | 8ecf7f9 | 2017-01-13 17:29:52 -0500 | [diff] [blame] | 17 | #include "libANGLE/renderer/vulkan/ContextVk.h" |
| 18 | #include "libANGLE/renderer/vulkan/GlslangWrapper.h" |
| 19 | #include "libANGLE/renderer/vulkan/RendererVk.h" |
Jamie Madill | 5547b38 | 2017-10-23 18:16:01 -0400 | [diff] [blame] | 20 | #include "libANGLE/renderer/vulkan/TextureVk.h" |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 21 | |
| 22 | namespace rx |
| 23 | { |
| 24 | |
Jamie Madill | 76e471e | 2017-10-21 09:56:01 -0400 | [diff] [blame] | 25 | namespace |
| 26 | { |
| 27 | |
Jamie Madill | f361437 | 2018-03-31 14:19:14 -0400 | [diff] [blame] | 28 | constexpr size_t kUniformBlockDynamicBufferMinSize = 256 * 128; |
Luc Ferron | 7a06ac1 | 2018-03-15 10:17:04 -0400 | [diff] [blame] | 29 | |
Jamie Madill | 242c4fe | 2018-07-12 15:56:56 -0400 | [diff] [blame] | 30 | void InitDefaultUniformBlock(const std::vector<sh::Uniform> &uniforms, |
Jamie Madill | 2106102 | 2018-07-12 23:56:30 -0400 | [diff] [blame] | 31 | gl::Shader *shader, |
| 32 | sh::BlockLayoutMap *blockLayoutMapOut, |
| 33 | size_t *blockSizeOut) |
Jamie Madill | 76e471e | 2017-10-21 09:56:01 -0400 | [diff] [blame] | 34 | { |
Jamie Madill | 76e471e | 2017-10-21 09:56:01 -0400 | [diff] [blame] | 35 | if (uniforms.empty()) |
| 36 | { |
Luc Ferron | 7a06ac1 | 2018-03-15 10:17:04 -0400 | [diff] [blame] | 37 | *blockSizeOut = 0; |
Jamie Madill | 2106102 | 2018-07-12 23:56:30 -0400 | [diff] [blame] | 38 | return; |
Jamie Madill | 76e471e | 2017-10-21 09:56:01 -0400 | [diff] [blame] | 39 | } |
| 40 | |
| 41 | sh::Std140BlockEncoder blockEncoder; |
Olli Etuaho | 3de2703 | 2017-11-30 12:16:47 +0200 | [diff] [blame] | 42 | sh::GetUniformBlockInfo(uniforms, "", &blockEncoder, blockLayoutMapOut); |
Jamie Madill | 76e471e | 2017-10-21 09:56:01 -0400 | [diff] [blame] | 43 | |
Jamie Madill | 4e712be | 2019-01-03 13:53:59 -0500 | [diff] [blame] | 44 | size_t blockSize = blockEncoder.getCurrentOffset(); |
Jamie Madill | 76e471e | 2017-10-21 09:56:01 -0400 | [diff] [blame] | 45 | |
| 46 | // TODO(jmadill): I think we still need a valid block for the pipeline even if zero sized. |
| 47 | if (blockSize == 0) |
| 48 | { |
Luc Ferron | 7a06ac1 | 2018-03-15 10:17:04 -0400 | [diff] [blame] | 49 | *blockSizeOut = 0; |
Jamie Madill | 2106102 | 2018-07-12 23:56:30 -0400 | [diff] [blame] | 50 | return; |
Jamie Madill | 76e471e | 2017-10-21 09:56:01 -0400 | [diff] [blame] | 51 | } |
| 52 | |
Luc Ferron | 7a06ac1 | 2018-03-15 10:17:04 -0400 | [diff] [blame] | 53 | *blockSizeOut = blockSize; |
Jamie Madill | 2106102 | 2018-07-12 23:56:30 -0400 | [diff] [blame] | 54 | return; |
Jamie Madill | 76e471e | 2017-10-21 09:56:01 -0400 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | template <typename T> |
| 58 | void UpdateDefaultUniformBlock(GLsizei count, |
Luc Ferron | 2371aca | 2018-03-27 16:03:03 -0400 | [diff] [blame] | 59 | uint32_t arrayIndex, |
Jamie Madill | 76e471e | 2017-10-21 09:56:01 -0400 | [diff] [blame] | 60 | int componentCount, |
| 61 | const T *v, |
| 62 | const sh::BlockMemberInfo &layoutInfo, |
| 63 | angle::MemoryBuffer *uniformData) |
| 64 | { |
Luc Ferron | 2371aca | 2018-03-27 16:03:03 -0400 | [diff] [blame] | 65 | const int elementSize = sizeof(T) * componentCount; |
| 66 | |
| 67 | uint8_t *dst = uniformData->data() + layoutInfo.offset; |
Jamie Madill | 76e471e | 2017-10-21 09:56:01 -0400 | [diff] [blame] | 68 | if (layoutInfo.arrayStride == 0 || layoutInfo.arrayStride == elementSize) |
| 69 | { |
Luc Ferron | 2371aca | 2018-03-27 16:03:03 -0400 | [diff] [blame] | 70 | uint32_t arrayOffset = arrayIndex * layoutInfo.arrayStride; |
| 71 | uint8_t *writePtr = dst + arrayOffset; |
Geoff Lang | beb0c94 | 2018-09-27 11:22:23 -0400 | [diff] [blame] | 72 | ASSERT(writePtr + (elementSize * count) <= uniformData->data() + uniformData->size()); |
Jamie Madill | 76e471e | 2017-10-21 09:56:01 -0400 | [diff] [blame] | 73 | memcpy(writePtr, v, elementSize * count); |
| 74 | } |
| 75 | else |
| 76 | { |
Luc Ferron | 2371aca | 2018-03-27 16:03:03 -0400 | [diff] [blame] | 77 | // Have to respect the arrayStride between each element of the array. |
| 78 | int maxIndex = arrayIndex + count; |
| 79 | for (int writeIndex = arrayIndex, readIndex = 0; writeIndex < maxIndex; |
| 80 | writeIndex++, readIndex++) |
| 81 | { |
| 82 | const int arrayOffset = writeIndex * layoutInfo.arrayStride; |
| 83 | uint8_t *writePtr = dst + arrayOffset; |
Luc Ferron | e9465a6 | 2018-06-04 10:41:52 -0400 | [diff] [blame] | 84 | const T *readPtr = v + (readIndex * componentCount); |
Geoff Lang | beb0c94 | 2018-09-27 11:22:23 -0400 | [diff] [blame] | 85 | ASSERT(writePtr + elementSize <= uniformData->data() + uniformData->size()); |
Luc Ferron | 2371aca | 2018-03-27 16:03:03 -0400 | [diff] [blame] | 86 | memcpy(writePtr, readPtr, elementSize); |
| 87 | } |
Jamie Madill | 76e471e | 2017-10-21 09:56:01 -0400 | [diff] [blame] | 88 | } |
| 89 | } |
| 90 | |
Luc Ferron | 7cec335 | 2018-03-13 13:29:34 -0400 | [diff] [blame] | 91 | template <typename T> |
| 92 | void ReadFromDefaultUniformBlock(int componentCount, |
Luc Ferron | 2371aca | 2018-03-27 16:03:03 -0400 | [diff] [blame] | 93 | uint32_t arrayIndex, |
Luc Ferron | 7cec335 | 2018-03-13 13:29:34 -0400 | [diff] [blame] | 94 | T *dst, |
| 95 | const sh::BlockMemberInfo &layoutInfo, |
| 96 | const angle::MemoryBuffer *uniformData) |
| 97 | { |
| 98 | ASSERT(layoutInfo.offset != -1); |
| 99 | |
Luc Ferron | 2371aca | 2018-03-27 16:03:03 -0400 | [diff] [blame] | 100 | const int elementSize = sizeof(T) * componentCount; |
| 101 | const uint8_t *source = uniformData->data() + layoutInfo.offset; |
| 102 | |
Luc Ferron | 7cec335 | 2018-03-13 13:29:34 -0400 | [diff] [blame] | 103 | if (layoutInfo.arrayStride == 0 || layoutInfo.arrayStride == elementSize) |
| 104 | { |
Luc Ferron | 2371aca | 2018-03-27 16:03:03 -0400 | [diff] [blame] | 105 | const uint8_t *readPtr = source + arrayIndex * layoutInfo.arrayStride; |
Luc Ferron | 7cec335 | 2018-03-13 13:29:34 -0400 | [diff] [blame] | 106 | memcpy(dst, readPtr, elementSize); |
| 107 | } |
| 108 | else |
| 109 | { |
Luc Ferron | 2371aca | 2018-03-27 16:03:03 -0400 | [diff] [blame] | 110 | // Have to respect the arrayStride between each element of the array. |
| 111 | const int arrayOffset = arrayIndex * layoutInfo.arrayStride; |
| 112 | const uint8_t *readPtr = source + arrayOffset; |
| 113 | memcpy(dst, readPtr, elementSize); |
Luc Ferron | 7cec335 | 2018-03-13 13:29:34 -0400 | [diff] [blame] | 114 | } |
| 115 | } |
| 116 | |
Jamie Madill | 2106102 | 2018-07-12 23:56:30 -0400 | [diff] [blame] | 117 | angle::Result SyncDefaultUniformBlock(ContextVk *contextVk, |
| 118 | vk::DynamicBuffer *dynamicBuffer, |
| 119 | const angle::MemoryBuffer &bufferData, |
| 120 | uint32_t *outOffset, |
| 121 | bool *outBufferModified) |
Jamie Madill | 76e471e | 2017-10-21 09:56:01 -0400 | [diff] [blame] | 122 | { |
Shahbaz Youssefi | 5829c03 | 2018-11-28 10:39:41 -0500 | [diff] [blame] | 123 | dynamicBuffer->releaseRetainedBuffers(contextVk->getRenderer()); |
| 124 | |
Luc Ferron | 7a06ac1 | 2018-03-15 10:17:04 -0400 | [diff] [blame] | 125 | ASSERT(!bufferData.empty()); |
| 126 | uint8_t *data = nullptr; |
| 127 | VkBuffer *outBuffer = nullptr; |
Jamie Madill | 4c31083 | 2018-08-29 13:43:17 -0400 | [diff] [blame] | 128 | VkDeviceSize offset = 0; |
Jamie Madill | 2106102 | 2018-07-12 23:56:30 -0400 | [diff] [blame] | 129 | ANGLE_TRY(dynamicBuffer->allocate(contextVk, bufferData.size(), &data, outBuffer, &offset, |
Jamie Madill | c3755fc | 2018-04-05 08:39:13 -0400 | [diff] [blame] | 130 | outBufferModified)); |
Jamie Madill | 4c31083 | 2018-08-29 13:43:17 -0400 | [diff] [blame] | 131 | *outOffset = static_cast<uint32_t>(offset); |
Luc Ferron | 7a06ac1 | 2018-03-15 10:17:04 -0400 | [diff] [blame] | 132 | memcpy(data, bufferData.data(), bufferData.size()); |
Jamie Madill | 2106102 | 2018-07-12 23:56:30 -0400 | [diff] [blame] | 133 | ANGLE_TRY(dynamicBuffer->flush(contextVk)); |
Jamie Madill | 7c985f5 | 2018-11-29 18:16:17 -0500 | [diff] [blame] | 134 | return angle::Result::Continue; |
Jamie Madill | 76e471e | 2017-10-21 09:56:01 -0400 | [diff] [blame] | 135 | } |
Shahbaz Youssefi | 216f73d | 2019-04-12 13:32:30 -0400 | [diff] [blame] | 136 | |
| 137 | uint32_t GetUniformBlockArraySize(const std::vector<gl::InterfaceBlock> &uniformBlocks, |
| 138 | uint32_t bufferIndex) |
| 139 | { |
| 140 | const gl::InterfaceBlock &uniformBlock = uniformBlocks[bufferIndex]; |
| 141 | |
| 142 | if (!uniformBlock.isArray) |
| 143 | { |
| 144 | return 1; |
| 145 | } |
| 146 | |
| 147 | ASSERT(uniformBlock.arrayElement == 0); |
| 148 | |
| 149 | // Search consecutively until all array indices of this block are visited. |
| 150 | uint32_t arraySize; |
| 151 | for (arraySize = 1; bufferIndex + arraySize < uniformBlocks.size(); ++arraySize) |
| 152 | { |
| 153 | const gl::InterfaceBlock &nextBlock = uniformBlocks[bufferIndex + arraySize]; |
| 154 | |
| 155 | if (nextBlock.arrayElement != arraySize) |
| 156 | { |
| 157 | break; |
| 158 | } |
| 159 | |
| 160 | // It's unexpected for an array to start at a non-zero array size, so we can always rely on |
| 161 | // the sequential `arrayElement`s to belong to the same block. |
| 162 | ASSERT(nextBlock.name == uniformBlock.name); |
| 163 | ASSERT(nextBlock.isArray); |
| 164 | } |
| 165 | |
| 166 | return arraySize; |
| 167 | } |
Jamie Madill | 76e471e | 2017-10-21 09:56:01 -0400 | [diff] [blame] | 168 | } // anonymous namespace |
| 169 | |
Jamie Madill | 242c4fe | 2018-07-12 15:56:56 -0400 | [diff] [blame] | 170 | // ProgramVk::ShaderInfo implementation. |
Jamie Madill | b980c56 | 2018-11-27 11:34:27 -0500 | [diff] [blame] | 171 | ProgramVk::ShaderInfo::ShaderInfo() {} |
Jamie Madill | 242c4fe | 2018-07-12 15:56:56 -0400 | [diff] [blame] | 172 | |
| 173 | ProgramVk::ShaderInfo::~ShaderInfo() = default; |
| 174 | |
Jamie Madill | dc65c5b | 2018-11-21 11:07:26 -0500 | [diff] [blame] | 175 | angle::Result ProgramVk::ShaderInfo::initShaders(ContextVk *contextVk, |
| 176 | const std::string &vertexSource, |
| 177 | const std::string &fragmentSource, |
| 178 | bool enableLineRasterEmulation) |
Jamie Madill | 242c4fe | 2018-07-12 15:56:56 -0400 | [diff] [blame] | 179 | { |
Jamie Madill | dbc605c | 2019-01-04 16:39:14 -0500 | [diff] [blame] | 180 | ASSERT(!valid()); |
Jamie Madill | 242c4fe | 2018-07-12 15:56:56 -0400 | [diff] [blame] | 181 | |
Jamie Madill | dbc605c | 2019-01-04 16:39:14 -0500 | [diff] [blame] | 182 | std::vector<uint32_t> vertexCode; |
| 183 | std::vector<uint32_t> fragmentCode; |
| 184 | ANGLE_TRY(GlslangWrapper::GetShaderCode(contextVk, contextVk->getCaps(), |
| 185 | enableLineRasterEmulation, vertexSource, fragmentSource, |
| 186 | &vertexCode, &fragmentCode)); |
Jamie Madill | dc65c5b | 2018-11-21 11:07:26 -0500 | [diff] [blame] | 187 | |
Jamie Madill | dbc605c | 2019-01-04 16:39:14 -0500 | [diff] [blame] | 188 | ANGLE_TRY(vk::InitShaderAndSerial(contextVk, &mShaders[gl::ShaderType::Vertex].get(), |
| 189 | vertexCode.data(), vertexCode.size() * sizeof(uint32_t))); |
| 190 | ANGLE_TRY(vk::InitShaderAndSerial(contextVk, &mShaders[gl::ShaderType::Fragment].get(), |
| 191 | fragmentCode.data(), fragmentCode.size() * sizeof(uint32_t))); |
| 192 | |
| 193 | mProgramHelper.setShader(gl::ShaderType::Vertex, &mShaders[gl::ShaderType::Vertex]); |
| 194 | mProgramHelper.setShader(gl::ShaderType::Fragment, &mShaders[gl::ShaderType::Fragment]); |
Jamie Madill | 242c4fe | 2018-07-12 15:56:56 -0400 | [diff] [blame] | 195 | |
Jamie Madill | 7c985f5 | 2018-11-29 18:16:17 -0500 | [diff] [blame] | 196 | return angle::Result::Continue; |
Jamie Madill | 242c4fe | 2018-07-12 15:56:56 -0400 | [diff] [blame] | 197 | } |
| 198 | |
Jamie Madill | dc65c5b | 2018-11-21 11:07:26 -0500 | [diff] [blame] | 199 | void ProgramVk::ShaderInfo::release(RendererVk *renderer) |
Jamie Madill | 242c4fe | 2018-07-12 15:56:56 -0400 | [diff] [blame] | 200 | { |
Jamie Madill | dc65c5b | 2018-11-21 11:07:26 -0500 | [diff] [blame] | 201 | mProgramHelper.release(renderer); |
| 202 | |
| 203 | for (vk::RefCounted<vk::ShaderAndSerial> &shader : mShaders) |
| 204 | { |
| 205 | shader.get().destroy(renderer->getDevice()); |
| 206 | } |
Jamie Madill | 242c4fe | 2018-07-12 15:56:56 -0400 | [diff] [blame] | 207 | } |
| 208 | |
Jamie Madill | 242c4fe | 2018-07-12 15:56:56 -0400 | [diff] [blame] | 209 | // ProgramVk implementation. |
Jamie Madill | 76e471e | 2017-10-21 09:56:01 -0400 | [diff] [blame] | 210 | ProgramVk::DefaultUniformBlock::DefaultUniformBlock() |
Luc Ferron | 7a06ac1 | 2018-03-15 10:17:04 -0400 | [diff] [blame] | 211 | : storage(VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT | VK_BUFFER_USAGE_TRANSFER_SRC_BIT, |
Shahbaz Youssefi | 254b32c | 2018-11-26 11:58:03 -0500 | [diff] [blame] | 212 | kUniformBlockDynamicBufferMinSize, |
| 213 | true) |
Jamie Madill | b980c56 | 2018-11-27 11:34:27 -0500 | [diff] [blame] | 214 | {} |
Jamie Madill | 76e471e | 2017-10-21 09:56:01 -0400 | [diff] [blame] | 215 | |
Jamie Madill | 242c4fe | 2018-07-12 15:56:56 -0400 | [diff] [blame] | 216 | ProgramVk::DefaultUniformBlock::~DefaultUniformBlock() = default; |
Jamie Madill | acf2f3a | 2017-11-21 19:22:44 -0500 | [diff] [blame] | 217 | |
Jamie Madill | 88fc6da | 2018-08-30 16:18:36 -0400 | [diff] [blame] | 218 | ProgramVk::ProgramVk(const gl::ProgramState &state) : ProgramImpl(state), mUniformBlocksOffsets{} |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 219 | { |
| 220 | } |
| 221 | |
Jamie Madill | 242c4fe | 2018-07-12 15:56:56 -0400 | [diff] [blame] | 222 | ProgramVk::~ProgramVk() = default; |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 223 | |
Jamie Madill | f4a789f | 2018-10-18 16:56:20 -0400 | [diff] [blame] | 224 | void ProgramVk::destroy(const gl::Context *context) |
Jamie Madill | 5deea72 | 2017-02-16 10:44:46 -0500 | [diff] [blame] | 225 | { |
Jamie Madill | 242c4fe | 2018-07-12 15:56:56 -0400 | [diff] [blame] | 226 | ContextVk *contextVk = vk::GetImpl(context); |
Jamie Madill | dc65c5b | 2018-11-21 11:07:26 -0500 | [diff] [blame] | 227 | reset(contextVk->getRenderer()); |
Jamie Madill | c514348 | 2017-10-15 20:20:06 -0400 | [diff] [blame] | 228 | } |
Jamie Madill | 5deea72 | 2017-02-16 10:44:46 -0500 | [diff] [blame] | 229 | |
Jamie Madill | dc65c5b | 2018-11-21 11:07:26 -0500 | [diff] [blame] | 230 | void ProgramVk::reset(RendererVk *renderer) |
Jamie Madill | c514348 | 2017-10-15 20:20:06 -0400 | [diff] [blame] | 231 | { |
Jamie Madill | 9b168d0 | 2018-06-13 13:25:32 -0400 | [diff] [blame] | 232 | for (auto &descriptorSetLayout : mDescriptorSetLayouts) |
| 233 | { |
| 234 | descriptorSetLayout.reset(); |
| 235 | } |
| 236 | mPipelineLayout.reset(); |
| 237 | |
Jamie Madill | 76e471e | 2017-10-21 09:56:01 -0400 | [diff] [blame] | 238 | for (auto &uniformBlock : mDefaultUniformBlocks) |
| 239 | { |
Jamie Madill | caaff16 | 2018-06-22 08:55:37 -0400 | [diff] [blame] | 240 | uniformBlock.storage.release(renderer); |
Jamie Madill | 76e471e | 2017-10-21 09:56:01 -0400 | [diff] [blame] | 241 | } |
| 242 | |
Jamie Madill | dc65c5b | 2018-11-21 11:07:26 -0500 | [diff] [blame] | 243 | mDefaultShaderInfo.release(renderer); |
| 244 | mLineRasterShaderInfo.release(renderer); |
Jamie Madill | 242c4fe | 2018-07-12 15:56:56 -0400 | [diff] [blame] | 245 | |
Shahbaz Youssefi | 254b32c | 2018-11-26 11:58:03 -0500 | [diff] [blame] | 246 | mEmptyUniformBlockStorage.release(renderer); |
Jamie Madill | 76e471e | 2017-10-21 09:56:01 -0400 | [diff] [blame] | 247 | |
Jamie Madill | 5547b38 | 2017-10-23 18:16:01 -0400 | [diff] [blame] | 248 | mDescriptorSets.clear(); |
Shahbaz Youssefi | 216f73d | 2019-04-12 13:32:30 -0400 | [diff] [blame] | 249 | mEmptyDescriptorSets.fill(VK_NULL_HANDLE); |
Jamie Madill | b7d924a | 2018-03-10 11:16:54 -0500 | [diff] [blame] | 250 | |
Shahbaz Youssefi | d838178 | 2019-03-04 11:07:47 -0500 | [diff] [blame] | 251 | for (vk::RefCountedDescriptorPoolBinding &binding : mDescriptorPoolBindings) |
Jamie Madill | 78bcd2b | 2018-10-16 15:05:20 -0400 | [diff] [blame] | 252 | { |
| 253 | binding.reset(); |
| 254 | } |
Jamie Madill | 5deea72 | 2017-02-16 10:44:46 -0500 | [diff] [blame] | 255 | } |
| 256 | |
Geoff Lang | e332e62 | 2019-02-14 12:53:04 -0500 | [diff] [blame] | 257 | std::unique_ptr<rx::LinkEvent> ProgramVk::load(const gl::Context *context, |
| 258 | gl::BinaryInputStream *stream, |
| 259 | gl::InfoLog &infoLog) |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 260 | { |
| 261 | UNIMPLEMENTED(); |
Geoff Lang | e332e62 | 2019-02-14 12:53:04 -0500 | [diff] [blame] | 262 | return std::make_unique<LinkEventDone>(angle::Result::Stop); |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 263 | } |
| 264 | |
Jamie Madill | 27a6063 | 2017-06-30 15:12:01 -0400 | [diff] [blame] | 265 | void ProgramVk::save(const gl::Context *context, gl::BinaryOutputStream *stream) |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 266 | { |
| 267 | UNIMPLEMENTED(); |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 268 | } |
| 269 | |
| 270 | void ProgramVk::setBinaryRetrievableHint(bool retrievable) |
| 271 | { |
| 272 | UNIMPLEMENTED(); |
| 273 | } |
| 274 | |
Yunchao He | 61afff1 | 2017-03-14 15:34:03 +0800 | [diff] [blame] | 275 | void ProgramVk::setSeparable(bool separable) |
| 276 | { |
| 277 | UNIMPLEMENTED(); |
| 278 | } |
| 279 | |
Jamie Madill | 785e8a0 | 2018-10-04 17:42:00 -0400 | [diff] [blame] | 280 | std::unique_ptr<LinkEvent> ProgramVk::link(const gl::Context *context, |
jchen10 | 7ae70d8 | 2018-07-06 13:47:01 +0800 | [diff] [blame] | 281 | const gl::ProgramLinkedResources &resources, |
| 282 | gl::InfoLog &infoLog) |
| 283 | { |
| 284 | // TODO(jie.a.chen@intel.com): Parallelize linking. |
| 285 | // http://crbug.com/849576 |
Jamie Madill | 785e8a0 | 2018-10-04 17:42:00 -0400 | [diff] [blame] | 286 | return std::make_unique<LinkEventDone>(linkImpl(context, resources, infoLog)); |
jchen10 | 7ae70d8 | 2018-07-06 13:47:01 +0800 | [diff] [blame] | 287 | } |
| 288 | |
Jamie Madill | 785e8a0 | 2018-10-04 17:42:00 -0400 | [diff] [blame] | 289 | angle::Result ProgramVk::linkImpl(const gl::Context *glContext, |
| 290 | const gl::ProgramLinkedResources &resources, |
| 291 | gl::InfoLog &infoLog) |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 292 | { |
Jamie Madill | 06ca634 | 2018-07-12 15:56:53 -0400 | [diff] [blame] | 293 | ContextVk *contextVk = vk::GetImpl(glContext); |
| 294 | RendererVk *renderer = contextVk->getRenderer(); |
Jamie Madill | c514348 | 2017-10-15 20:20:06 -0400 | [diff] [blame] | 295 | |
Jamie Madill | dc65c5b | 2018-11-21 11:07:26 -0500 | [diff] [blame] | 296 | reset(renderer); |
Jamie Madill | 8ecf7f9 | 2017-01-13 17:29:52 -0500 | [diff] [blame] | 297 | |
Shahbaz Youssefi | 216f73d | 2019-04-12 13:32:30 -0400 | [diff] [blame] | 298 | // Link resources before calling GetShaderSource to make sure they are ready for the set/binding |
| 299 | // assignment done in that function. |
| 300 | linkResources(resources); |
| 301 | |
jchen10 | 3fd614d | 2018-08-13 12:21:58 +0800 | [diff] [blame] | 302 | GlslangWrapper::GetShaderSource(mState, resources, &mVertexSource, &mFragmentSource); |
Jamie Madill | 8ecf7f9 | 2017-01-13 17:29:52 -0500 | [diff] [blame] | 303 | |
Jamie Madill | 76e471e | 2017-10-21 09:56:01 -0400 | [diff] [blame] | 304 | ANGLE_TRY(initDefaultUniformBlocks(glContext)); |
Jamie Madill | 8ecf7f9 | 2017-01-13 17:29:52 -0500 | [diff] [blame] | 305 | |
Jamie Madill | 9b168d0 | 2018-06-13 13:25:32 -0400 | [diff] [blame] | 306 | // Store a reference to the pipeline and descriptor set layouts. This will create them if they |
| 307 | // don't already exist in the cache. |
| 308 | vk::DescriptorSetLayoutDesc uniformsSetDesc; |
| 309 | uniformsSetDesc.update(kVertexUniformsBindingIndex, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC, |
| 310 | 1); |
| 311 | uniformsSetDesc.update(kFragmentUniformsBindingIndex, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC, |
| 312 | 1); |
| 313 | |
| 314 | ANGLE_TRY(renderer->getDescriptorSetLayout( |
Jamie Madill | 2106102 | 2018-07-12 23:56:30 -0400 | [diff] [blame] | 315 | contextVk, uniformsSetDesc, &mDescriptorSetLayouts[kUniformsDescriptorSetIndex])); |
Jamie Madill | 9b168d0 | 2018-06-13 13:25:32 -0400 | [diff] [blame] | 316 | |
Shahbaz Youssefi | 216f73d | 2019-04-12 13:32:30 -0400 | [diff] [blame] | 317 | vk::DescriptorSetLayoutDesc uniformBlocksSetDesc; |
| 318 | |
| 319 | const std::vector<gl::InterfaceBlock> &uniformBlocks = mState.getUniformBlocks(); |
| 320 | for (uint32_t bufferIndex = 0; bufferIndex < uniformBlocks.size();) |
| 321 | { |
| 322 | const uint32_t arraySize = GetUniformBlockArraySize(uniformBlocks, bufferIndex); |
| 323 | |
| 324 | uniformBlocksSetDesc.update(bufferIndex, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, arraySize); |
| 325 | |
| 326 | bufferIndex += arraySize; |
| 327 | } |
| 328 | |
| 329 | ANGLE_TRY(renderer->getDescriptorSetLayout( |
| 330 | contextVk, uniformBlocksSetDesc, &mDescriptorSetLayouts[kUniformBlockDescriptorSetIndex])); |
| 331 | |
Jamie Madill | 9b168d0 | 2018-06-13 13:25:32 -0400 | [diff] [blame] | 332 | vk::DescriptorSetLayoutDesc texturesSetDesc; |
Jamie Madill | 4cc753e | 2018-06-13 13:25:33 -0400 | [diff] [blame] | 333 | |
| 334 | for (uint32_t textureIndex = 0; textureIndex < mState.getSamplerBindings().size(); |
| 335 | ++textureIndex) |
Jamie Madill | 9b168d0 | 2018-06-13 13:25:32 -0400 | [diff] [blame] | 336 | { |
Jamie Madill | 4cc753e | 2018-06-13 13:25:33 -0400 | [diff] [blame] | 337 | const gl::SamplerBinding &samplerBinding = mState.getSamplerBindings()[textureIndex]; |
| 338 | |
| 339 | // The front-end always binds array sampler units sequentially. |
| 340 | const uint32_t count = static_cast<uint32_t>(samplerBinding.boundTextureUnits.size()); |
| 341 | texturesSetDesc.update(textureIndex, VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, count); |
Jamie Madill | 9b168d0 | 2018-06-13 13:25:32 -0400 | [diff] [blame] | 342 | } |
| 343 | |
Jamie Madill | 2106102 | 2018-07-12 23:56:30 -0400 | [diff] [blame] | 344 | ANGLE_TRY(renderer->getDescriptorSetLayout(contextVk, texturesSetDesc, |
Jamie Madill | 9b168d0 | 2018-06-13 13:25:32 -0400 | [diff] [blame] | 345 | &mDescriptorSetLayouts[kTextureDescriptorSetIndex])); |
| 346 | |
Jamie Madill | b01b480 | 2018-07-10 12:43:57 -0400 | [diff] [blame] | 347 | vk::DescriptorSetLayoutDesc driverUniformsSetDesc; |
| 348 | driverUniformsSetDesc.update(0, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, 1); |
| 349 | ANGLE_TRY(renderer->getDescriptorSetLayout( |
Jamie Madill | 2106102 | 2018-07-12 23:56:30 -0400 | [diff] [blame] | 350 | contextVk, driverUniformsSetDesc, |
| 351 | &mDescriptorSetLayouts[kDriverUniformsDescriptorSetIndex])); |
Jamie Madill | b01b480 | 2018-07-10 12:43:57 -0400 | [diff] [blame] | 352 | |
Jamie Madill | 9b168d0 | 2018-06-13 13:25:32 -0400 | [diff] [blame] | 353 | vk::PipelineLayoutDesc pipelineLayoutDesc; |
| 354 | pipelineLayoutDesc.updateDescriptorSetLayout(kUniformsDescriptorSetIndex, uniformsSetDesc); |
Shahbaz Youssefi | 216f73d | 2019-04-12 13:32:30 -0400 | [diff] [blame] | 355 | pipelineLayoutDesc.updateDescriptorSetLayout(kUniformBlockDescriptorSetIndex, |
| 356 | uniformBlocksSetDesc); |
Jamie Madill | 9b168d0 | 2018-06-13 13:25:32 -0400 | [diff] [blame] | 357 | pipelineLayoutDesc.updateDescriptorSetLayout(kTextureDescriptorSetIndex, texturesSetDesc); |
Jamie Madill | b01b480 | 2018-07-10 12:43:57 -0400 | [diff] [blame] | 358 | pipelineLayoutDesc.updateDescriptorSetLayout(kDriverUniformsDescriptorSetIndex, |
| 359 | driverUniformsSetDesc); |
Jamie Madill | 9b168d0 | 2018-06-13 13:25:32 -0400 | [diff] [blame] | 360 | |
Jamie Madill | 2106102 | 2018-07-12 23:56:30 -0400 | [diff] [blame] | 361 | ANGLE_TRY(renderer->getPipelineLayout(contextVk, pipelineLayoutDesc, mDescriptorSetLayouts, |
| 362 | &mPipelineLayout)); |
Jamie Madill | 9b168d0 | 2018-06-13 13:25:32 -0400 | [diff] [blame] | 363 | |
Jamie Madill | 7c985f5 | 2018-11-29 18:16:17 -0500 | [diff] [blame] | 364 | return angle::Result::Continue; |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 365 | } |
| 366 | |
Shahbaz Youssefi | 216f73d | 2019-04-12 13:32:30 -0400 | [diff] [blame] | 367 | void ProgramVk::linkResources(const gl::ProgramLinkedResources &resources) |
| 368 | { |
| 369 | gl::ProgramLinkedResourcesLinker linker(nullptr); |
| 370 | |
| 371 | linker.linkResources(mState, resources); |
| 372 | } |
| 373 | |
Jamie Madill | 242c4fe | 2018-07-12 15:56:56 -0400 | [diff] [blame] | 374 | angle::Result ProgramVk::initDefaultUniformBlocks(const gl::Context *glContext) |
Jamie Madill | 76e471e | 2017-10-21 09:56:01 -0400 | [diff] [blame] | 375 | { |
Jamie Madill | e1f3ad4 | 2017-10-28 23:00:42 -0400 | [diff] [blame] | 376 | ContextVk *contextVk = vk::GetImpl(glContext); |
Jamie Madill | 57fbfd8 | 2018-02-14 12:45:34 -0500 | [diff] [blame] | 377 | RendererVk *renderer = contextVk->getRenderer(); |
Jamie Madill | 76e471e | 2017-10-21 09:56:01 -0400 | [diff] [blame] | 378 | |
| 379 | // Process vertex and fragment uniforms into std140 packing. |
Shahbaz Youssefi | df066e9 | 2018-11-13 15:39:47 -0500 | [diff] [blame] | 380 | gl::ShaderMap<sh::BlockLayoutMap> layoutMap; |
| 381 | gl::ShaderMap<size_t> requiredBufferSize; |
Jamie Madill | 33318de | 2018-05-01 11:22:54 -0400 | [diff] [blame] | 382 | requiredBufferSize.fill(0); |
Jamie Madill | 76e471e | 2017-10-21 09:56:01 -0400 | [diff] [blame] | 383 | |
Shahbaz Youssefi | df066e9 | 2018-11-13 15:39:47 -0500 | [diff] [blame] | 384 | for (gl::ShaderType shaderType : gl::AllGLES2ShaderTypes()) |
Jamie Madill | 76e471e | 2017-10-21 09:56:01 -0400 | [diff] [blame] | 385 | { |
Shahbaz Youssefi | df066e9 | 2018-11-13 15:39:47 -0500 | [diff] [blame] | 386 | gl::Shader *shader = mState.getAttachedShader(shaderType); |
jchen10 | 3fd614d | 2018-08-13 12:21:58 +0800 | [diff] [blame] | 387 | const std::vector<sh::Uniform> &uniforms = shader->getUniforms(); |
Jamie Madill | 242c4fe | 2018-07-12 15:56:56 -0400 | [diff] [blame] | 388 | InitDefaultUniformBlock(uniforms, shader, &layoutMap[shaderType], |
| 389 | &requiredBufferSize[shaderType]); |
Jamie Madill | 76e471e | 2017-10-21 09:56:01 -0400 | [diff] [blame] | 390 | } |
| 391 | |
| 392 | // Init the default block layout info. |
Jamie Madill | 77abad8 | 2018-10-25 17:03:48 -0400 | [diff] [blame] | 393 | const auto &uniforms = mState.getUniforms(); |
Jamie Madill | 242c4fe | 2018-07-12 15:56:56 -0400 | [diff] [blame] | 394 | for (const gl::VariableLocation &location : mState.getUniformLocations()) |
Jamie Madill | 76e471e | 2017-10-21 09:56:01 -0400 | [diff] [blame] | 395 | { |
Shahbaz Youssefi | df066e9 | 2018-11-13 15:39:47 -0500 | [diff] [blame] | 396 | gl::ShaderMap<sh::BlockMemberInfo> layoutInfo; |
Jamie Madill | 76e471e | 2017-10-21 09:56:01 -0400 | [diff] [blame] | 397 | |
Jamie Madill | 76e471e | 2017-10-21 09:56:01 -0400 | [diff] [blame] | 398 | if (location.used() && !location.ignored) |
| 399 | { |
Jamie Madill | de03e00 | 2017-10-21 14:04:20 -0400 | [diff] [blame] | 400 | const auto &uniform = uniforms[location.index]; |
Shahbaz Youssefi | 216f73d | 2019-04-12 13:32:30 -0400 | [diff] [blame] | 401 | if (uniform.isInDefaultBlock() && !uniform.isSampler()) |
Jamie Madill | 76e471e | 2017-10-21 09:56:01 -0400 | [diff] [blame] | 402 | { |
Geoff Lang | beb0c94 | 2018-09-27 11:22:23 -0400 | [diff] [blame] | 403 | std::string uniformName = uniform.name; |
| 404 | if (uniform.isArray()) |
Jamie Madill | 76e471e | 2017-10-21 09:56:01 -0400 | [diff] [blame] | 405 | { |
Geoff Lang | beb0c94 | 2018-09-27 11:22:23 -0400 | [diff] [blame] | 406 | // Gets the uniform name without the [0] at the end. |
| 407 | uniformName = gl::ParseResourceName(uniformName, nullptr); |
Jamie Madill | 76e471e | 2017-10-21 09:56:01 -0400 | [diff] [blame] | 408 | } |
Jamie Madill | 76e471e | 2017-10-21 09:56:01 -0400 | [diff] [blame] | 409 | |
Geoff Lang | beb0c94 | 2018-09-27 11:22:23 -0400 | [diff] [blame] | 410 | bool found = false; |
| 411 | |
Shahbaz Youssefi | df066e9 | 2018-11-13 15:39:47 -0500 | [diff] [blame] | 412 | for (gl::ShaderType shaderType : gl::AllGLES2ShaderTypes()) |
Geoff Lang | beb0c94 | 2018-09-27 11:22:23 -0400 | [diff] [blame] | 413 | { |
| 414 | auto it = layoutMap[shaderType].find(uniformName); |
| 415 | if (it != layoutMap[shaderType].end()) |
| 416 | { |
| 417 | found = true; |
| 418 | layoutInfo[shaderType] = it->second; |
| 419 | } |
| 420 | } |
| 421 | |
| 422 | ASSERT(found); |
| 423 | } |
Jamie Madill | 76e471e | 2017-10-21 09:56:01 -0400 | [diff] [blame] | 424 | } |
| 425 | |
Shahbaz Youssefi | df066e9 | 2018-11-13 15:39:47 -0500 | [diff] [blame] | 426 | for (gl::ShaderType shaderType : gl::AllGLES2ShaderTypes()) |
Jamie Madill | 76e471e | 2017-10-21 09:56:01 -0400 | [diff] [blame] | 427 | { |
Jamie Madill | 33318de | 2018-05-01 11:22:54 -0400 | [diff] [blame] | 428 | mDefaultUniformBlocks[shaderType].uniformLayout.push_back(layoutInfo[shaderType]); |
Jamie Madill | 76e471e | 2017-10-21 09:56:01 -0400 | [diff] [blame] | 429 | } |
| 430 | } |
| 431 | |
Shahbaz Youssefi | df066e9 | 2018-11-13 15:39:47 -0500 | [diff] [blame] | 432 | for (gl::ShaderType shaderType : gl::AllGLES2ShaderTypes()) |
Jamie Madill | 76e471e | 2017-10-21 09:56:01 -0400 | [diff] [blame] | 433 | { |
Jamie Madill | 33318de | 2018-05-01 11:22:54 -0400 | [diff] [blame] | 434 | if (requiredBufferSize[shaderType] > 0) |
Jamie Madill | 76e471e | 2017-10-21 09:56:01 -0400 | [diff] [blame] | 435 | { |
Jamie Madill | 33318de | 2018-05-01 11:22:54 -0400 | [diff] [blame] | 436 | if (!mDefaultUniformBlocks[shaderType].uniformData.resize( |
| 437 | requiredBufferSize[shaderType])) |
Jamie Madill | 76e471e | 2017-10-21 09:56:01 -0400 | [diff] [blame] | 438 | { |
Jamie Madill | 242c4fe | 2018-07-12 15:56:56 -0400 | [diff] [blame] | 439 | ANGLE_VK_CHECK(contextVk, false, VK_ERROR_OUT_OF_HOST_MEMORY); |
Jamie Madill | 76e471e | 2017-10-21 09:56:01 -0400 | [diff] [blame] | 440 | } |
Luc Ferron | 7a06ac1 | 2018-03-15 10:17:04 -0400 | [diff] [blame] | 441 | size_t minAlignment = static_cast<size_t>( |
| 442 | renderer->getPhysicalDeviceProperties().limits.minUniformBufferOffsetAlignment); |
| 443 | |
Luc Ferron | a9ab0f3 | 2018-05-17 17:03:55 -0400 | [diff] [blame] | 444 | mDefaultUniformBlocks[shaderType].storage.init(minAlignment, renderer); |
Luc Ferron | 7a06ac1 | 2018-03-15 10:17:04 -0400 | [diff] [blame] | 445 | |
| 446 | // Initialize uniform buffer memory to zero by default. |
Jamie Madill | 33318de | 2018-05-01 11:22:54 -0400 | [diff] [blame] | 447 | mDefaultUniformBlocks[shaderType].uniformData.fill(0); |
Jamie Madill | 2b858c2 | 2018-09-03 13:58:14 -0400 | [diff] [blame] | 448 | mDefaultUniformBlocksDirty.set(shaderType); |
Jamie Madill | 76e471e | 2017-10-21 09:56:01 -0400 | [diff] [blame] | 449 | } |
| 450 | } |
| 451 | |
Jamie Madill | 2b858c2 | 2018-09-03 13:58:14 -0400 | [diff] [blame] | 452 | if (mDefaultUniformBlocksDirty.any()) |
Jamie Madill | 76e471e | 2017-10-21 09:56:01 -0400 | [diff] [blame] | 453 | { |
| 454 | // Initialize the "empty" uniform block if necessary. |
Jamie Madill | 2b858c2 | 2018-09-03 13:58:14 -0400 | [diff] [blame] | 455 | if (!mDefaultUniformBlocksDirty.all()) |
Jamie Madill | 76e471e | 2017-10-21 09:56:01 -0400 | [diff] [blame] | 456 | { |
Shahbaz Youssefi | 06270c9 | 2018-10-03 17:00:25 -0400 | [diff] [blame] | 457 | VkBufferCreateInfo uniformBufferInfo = {}; |
Jamie Madill | 76e471e | 2017-10-21 09:56:01 -0400 | [diff] [blame] | 458 | uniformBufferInfo.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO; |
Jamie Madill | 76e471e | 2017-10-21 09:56:01 -0400 | [diff] [blame] | 459 | uniformBufferInfo.flags = 0; |
| 460 | uniformBufferInfo.size = 1; |
| 461 | uniformBufferInfo.usage = VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT; |
| 462 | uniformBufferInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE; |
| 463 | uniformBufferInfo.queueFamilyIndexCount = 0; |
| 464 | uniformBufferInfo.pQueueFamilyIndices = nullptr; |
| 465 | |
Shahbaz Youssefi | 216f73d | 2019-04-12 13:32:30 -0400 | [diff] [blame] | 466 | constexpr VkMemoryPropertyFlags kMemoryType = VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT; |
| 467 | ANGLE_TRY(mEmptyUniformBlockStorage.init(contextVk, uniformBufferInfo, kMemoryType)); |
Jamie Madill | 76e471e | 2017-10-21 09:56:01 -0400 | [diff] [blame] | 468 | } |
Jamie Madill | 5547b38 | 2017-10-23 18:16:01 -0400 | [diff] [blame] | 469 | } |
Jamie Madill | 76e471e | 2017-10-21 09:56:01 -0400 | [diff] [blame] | 470 | |
Jamie Madill | 7c985f5 | 2018-11-29 18:16:17 -0500 | [diff] [blame] | 471 | return angle::Result::Continue; |
Jamie Madill | 76e471e | 2017-10-21 09:56:01 -0400 | [diff] [blame] | 472 | } |
| 473 | |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 474 | GLboolean ProgramVk::validate(const gl::Caps &caps, gl::InfoLog *infoLog) |
| 475 | { |
Luc Ferron | fba1f61 | 2018-06-04 14:37:17 -0400 | [diff] [blame] | 476 | // No-op. The spec is very vague about the behavior of validation. |
| 477 | return GL_TRUE; |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 478 | } |
| 479 | |
Jamie Madill | 76e471e | 2017-10-21 09:56:01 -0400 | [diff] [blame] | 480 | template <typename T> |
| 481 | void ProgramVk::setUniformImpl(GLint location, GLsizei count, const T *v, GLenum entryPointType) |
| 482 | { |
| 483 | const gl::VariableLocation &locationInfo = mState.getUniformLocations()[location]; |
| 484 | const gl::LinkedUniform &linkedUniform = mState.getUniforms()[locationInfo.index]; |
| 485 | |
Luc Ferron | 7cec335 | 2018-03-13 13:29:34 -0400 | [diff] [blame] | 486 | if (linkedUniform.isSampler()) |
| 487 | { |
Jamie Madill | 0cb6dc4 | 2018-04-16 10:36:39 -0400 | [diff] [blame] | 488 | // We could potentially cache some indexing here. For now this is a no-op since the mapping |
| 489 | // is handled entirely in ContextVk. |
Luc Ferron | 7cec335 | 2018-03-13 13:29:34 -0400 | [diff] [blame] | 490 | return; |
| 491 | } |
| 492 | |
Luc Ferron | 24a3137 | 2018-04-04 11:49:14 -0400 | [diff] [blame] | 493 | if (linkedUniform.typeInfo->type == entryPointType) |
Jamie Madill | 76e471e | 2017-10-21 09:56:01 -0400 | [diff] [blame] | 494 | { |
Shahbaz Youssefi | df066e9 | 2018-11-13 15:39:47 -0500 | [diff] [blame] | 495 | for (gl::ShaderType shaderType : gl::AllGLES2ShaderTypes()) |
Jamie Madill | 76e471e | 2017-10-21 09:56:01 -0400 | [diff] [blame] | 496 | { |
Jamie Madill | 2b858c2 | 2018-09-03 13:58:14 -0400 | [diff] [blame] | 497 | DefaultUniformBlock &uniformBlock = mDefaultUniformBlocks[shaderType]; |
Luc Ferron | 24a3137 | 2018-04-04 11:49:14 -0400 | [diff] [blame] | 498 | const sh::BlockMemberInfo &layoutInfo = uniformBlock.uniformLayout[location]; |
Luc Ferron | 62059a5 | 2018-03-29 07:01:35 -0400 | [diff] [blame] | 499 | |
Luc Ferron | 24a3137 | 2018-04-04 11:49:14 -0400 | [diff] [blame] | 500 | // Assume an offset of -1 means the block is unused. |
| 501 | if (layoutInfo.offset == -1) |
| 502 | { |
| 503 | continue; |
| 504 | } |
| 505 | |
| 506 | const GLint componentCount = linkedUniform.typeInfo->componentCount; |
Luc Ferron | 62059a5 | 2018-03-29 07:01:35 -0400 | [diff] [blame] | 507 | UpdateDefaultUniformBlock(count, locationInfo.arrayIndex, componentCount, v, layoutInfo, |
| 508 | &uniformBlock.uniformData); |
Jamie Madill | 2b858c2 | 2018-09-03 13:58:14 -0400 | [diff] [blame] | 509 | mDefaultUniformBlocksDirty.set(shaderType); |
Luc Ferron | 62059a5 | 2018-03-29 07:01:35 -0400 | [diff] [blame] | 510 | } |
Luc Ferron | 24a3137 | 2018-04-04 11:49:14 -0400 | [diff] [blame] | 511 | } |
| 512 | else |
| 513 | { |
Shahbaz Youssefi | df066e9 | 2018-11-13 15:39:47 -0500 | [diff] [blame] | 514 | for (gl::ShaderType shaderType : gl::AllGLES2ShaderTypes()) |
Luc Ferron | 62059a5 | 2018-03-29 07:01:35 -0400 | [diff] [blame] | 515 | { |
Jamie Madill | 2b858c2 | 2018-09-03 13:58:14 -0400 | [diff] [blame] | 516 | DefaultUniformBlock &uniformBlock = mDefaultUniformBlocks[shaderType]; |
Luc Ferron | 24a3137 | 2018-04-04 11:49:14 -0400 | [diff] [blame] | 517 | const sh::BlockMemberInfo &layoutInfo = uniformBlock.uniformLayout[location]; |
| 518 | |
| 519 | // Assume an offset of -1 means the block is unused. |
| 520 | if (layoutInfo.offset == -1) |
| 521 | { |
| 522 | continue; |
| 523 | } |
| 524 | |
| 525 | const GLint componentCount = linkedUniform.typeInfo->componentCount; |
| 526 | |
Luc Ferron | 62059a5 | 2018-03-29 07:01:35 -0400 | [diff] [blame] | 527 | ASSERT(linkedUniform.typeInfo->type == gl::VariableBoolVectorType(entryPointType)); |
| 528 | |
Luc Ferron | d91c379 | 2018-04-06 09:36:36 -0400 | [diff] [blame] | 529 | GLint initialArrayOffset = |
| 530 | locationInfo.arrayIndex * layoutInfo.arrayStride + layoutInfo.offset; |
Luc Ferron | 62059a5 | 2018-03-29 07:01:35 -0400 | [diff] [blame] | 531 | for (GLint i = 0; i < count; i++) |
| 532 | { |
| 533 | GLint elementOffset = i * layoutInfo.arrayStride + initialArrayOffset; |
| 534 | GLint *dest = |
| 535 | reinterpret_cast<GLint *>(uniformBlock.uniformData.data() + elementOffset); |
| 536 | const T *source = v + i * componentCount; |
| 537 | |
| 538 | for (int c = 0; c < componentCount; c++) |
| 539 | { |
| 540 | dest[c] = (source[c] == static_cast<T>(0)) ? GL_FALSE : GL_TRUE; |
| 541 | } |
| 542 | } |
Jamie Madill | 2b858c2 | 2018-09-03 13:58:14 -0400 | [diff] [blame] | 543 | |
| 544 | mDefaultUniformBlocksDirty.set(shaderType); |
Luc Ferron | 62059a5 | 2018-03-29 07:01:35 -0400 | [diff] [blame] | 545 | } |
Jamie Madill | 76e471e | 2017-10-21 09:56:01 -0400 | [diff] [blame] | 546 | } |
| 547 | } |
| 548 | |
Luc Ferron | 7cec335 | 2018-03-13 13:29:34 -0400 | [diff] [blame] | 549 | template <typename T> |
| 550 | void ProgramVk::getUniformImpl(GLint location, T *v, GLenum entryPointType) const |
| 551 | { |
| 552 | const gl::VariableLocation &locationInfo = mState.getUniformLocations()[location]; |
| 553 | const gl::LinkedUniform &linkedUniform = mState.getUniforms()[locationInfo.index]; |
| 554 | |
Jamie Madill | 3bb2bbe | 2018-06-15 09:47:03 -0400 | [diff] [blame] | 555 | ASSERT(!linkedUniform.isSampler()); |
Luc Ferron | 7cec335 | 2018-03-13 13:29:34 -0400 | [diff] [blame] | 556 | |
Olli Etuaho | 107c724 | 2018-03-20 15:45:35 +0200 | [diff] [blame] | 557 | const gl::ShaderType shaderType = linkedUniform.getFirstShaderTypeWhereActive(); |
Jiawei Shao | 385b3e0 | 2018-03-21 09:43:28 +0800 | [diff] [blame] | 558 | ASSERT(shaderType != gl::ShaderType::InvalidEnum); |
Luc Ferron | 7cec335 | 2018-03-13 13:29:34 -0400 | [diff] [blame] | 559 | |
Shahbaz Youssefi | df066e9 | 2018-11-13 15:39:47 -0500 | [diff] [blame] | 560 | const DefaultUniformBlock &uniformBlock = mDefaultUniformBlocks[shaderType]; |
Jamie Madill | b980c56 | 2018-11-27 11:34:27 -0500 | [diff] [blame] | 561 | const sh::BlockMemberInfo &layoutInfo = uniformBlock.uniformLayout[location]; |
Luc Ferron | 62059a5 | 2018-03-29 07:01:35 -0400 | [diff] [blame] | 562 | |
| 563 | ASSERT(linkedUniform.typeInfo->componentType == entryPointType || |
| 564 | linkedUniform.typeInfo->componentType == gl::VariableBoolVectorType(entryPointType)); |
Luc Ferron | 48cdc2e | 2018-05-31 09:58:34 -0400 | [diff] [blame] | 565 | |
| 566 | if (gl::IsMatrixType(linkedUniform.type)) |
| 567 | { |
| 568 | const uint8_t *ptrToElement = uniformBlock.uniformData.data() + layoutInfo.offset + |
Luc Ferron | f6fd48f | 2018-06-18 08:11:27 -0400 | [diff] [blame] | 569 | (locationInfo.arrayIndex * layoutInfo.arrayStride); |
Luc Ferron | 48cdc2e | 2018-05-31 09:58:34 -0400 | [diff] [blame] | 570 | GetMatrixUniform(linkedUniform.type, v, reinterpret_cast<const T *>(ptrToElement), false); |
| 571 | } |
| 572 | else |
| 573 | { |
| 574 | ReadFromDefaultUniformBlock(linkedUniform.typeInfo->componentCount, locationInfo.arrayIndex, |
| 575 | v, layoutInfo, &uniformBlock.uniformData); |
| 576 | } |
Luc Ferron | 7cec335 | 2018-03-13 13:29:34 -0400 | [diff] [blame] | 577 | } |
| 578 | |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 579 | void ProgramVk::setUniform1fv(GLint location, GLsizei count, const GLfloat *v) |
| 580 | { |
Jamie Madill | 76e471e | 2017-10-21 09:56:01 -0400 | [diff] [blame] | 581 | setUniformImpl(location, count, v, GL_FLOAT); |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 582 | } |
| 583 | |
| 584 | void ProgramVk::setUniform2fv(GLint location, GLsizei count, const GLfloat *v) |
| 585 | { |
Jamie Madill | 76e471e | 2017-10-21 09:56:01 -0400 | [diff] [blame] | 586 | setUniformImpl(location, count, v, GL_FLOAT_VEC2); |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 587 | } |
| 588 | |
| 589 | void ProgramVk::setUniform3fv(GLint location, GLsizei count, const GLfloat *v) |
| 590 | { |
Jamie Madill | 76e471e | 2017-10-21 09:56:01 -0400 | [diff] [blame] | 591 | setUniformImpl(location, count, v, GL_FLOAT_VEC3); |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 592 | } |
| 593 | |
| 594 | void ProgramVk::setUniform4fv(GLint location, GLsizei count, const GLfloat *v) |
| 595 | { |
Jamie Madill | 76e471e | 2017-10-21 09:56:01 -0400 | [diff] [blame] | 596 | setUniformImpl(location, count, v, GL_FLOAT_VEC4); |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 597 | } |
| 598 | |
| 599 | void ProgramVk::setUniform1iv(GLint location, GLsizei count, const GLint *v) |
| 600 | { |
Luc Ferron | 7cec335 | 2018-03-13 13:29:34 -0400 | [diff] [blame] | 601 | setUniformImpl(location, count, v, GL_INT); |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 602 | } |
| 603 | |
| 604 | void ProgramVk::setUniform2iv(GLint location, GLsizei count, const GLint *v) |
| 605 | { |
Luc Ferron | 489243f | 2018-03-28 16:55:28 -0400 | [diff] [blame] | 606 | setUniformImpl(location, count, v, GL_INT_VEC2); |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 607 | } |
| 608 | |
| 609 | void ProgramVk::setUniform3iv(GLint location, GLsizei count, const GLint *v) |
| 610 | { |
Luc Ferron | 489243f | 2018-03-28 16:55:28 -0400 | [diff] [blame] | 611 | setUniformImpl(location, count, v, GL_INT_VEC3); |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 612 | } |
| 613 | |
| 614 | void ProgramVk::setUniform4iv(GLint location, GLsizei count, const GLint *v) |
| 615 | { |
Luc Ferron | 489243f | 2018-03-28 16:55:28 -0400 | [diff] [blame] | 616 | setUniformImpl(location, count, v, GL_INT_VEC4); |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 617 | } |
| 618 | |
| 619 | void ProgramVk::setUniform1uiv(GLint location, GLsizei count, const GLuint *v) |
| 620 | { |
| 621 | UNIMPLEMENTED(); |
| 622 | } |
| 623 | |
| 624 | void ProgramVk::setUniform2uiv(GLint location, GLsizei count, const GLuint *v) |
| 625 | { |
| 626 | UNIMPLEMENTED(); |
| 627 | } |
| 628 | |
| 629 | void ProgramVk::setUniform3uiv(GLint location, GLsizei count, const GLuint *v) |
| 630 | { |
| 631 | UNIMPLEMENTED(); |
| 632 | } |
| 633 | |
| 634 | void ProgramVk::setUniform4uiv(GLint location, GLsizei count, const GLuint *v) |
| 635 | { |
| 636 | UNIMPLEMENTED(); |
| 637 | } |
| 638 | |
Luc Ferron | 48cdc2e | 2018-05-31 09:58:34 -0400 | [diff] [blame] | 639 | template <int cols, int rows> |
| 640 | void ProgramVk::setUniformMatrixfv(GLint location, |
| 641 | GLsizei count, |
| 642 | GLboolean transpose, |
| 643 | const GLfloat *value) |
| 644 | { |
| 645 | const gl::VariableLocation &locationInfo = mState.getUniformLocations()[location]; |
| 646 | const gl::LinkedUniform &linkedUniform = mState.getUniforms()[locationInfo.index]; |
| 647 | |
Shahbaz Youssefi | df066e9 | 2018-11-13 15:39:47 -0500 | [diff] [blame] | 648 | for (gl::ShaderType shaderType : gl::AllGLES2ShaderTypes()) |
Luc Ferron | 48cdc2e | 2018-05-31 09:58:34 -0400 | [diff] [blame] | 649 | { |
Jamie Madill | 2b858c2 | 2018-09-03 13:58:14 -0400 | [diff] [blame] | 650 | DefaultUniformBlock &uniformBlock = mDefaultUniformBlocks[shaderType]; |
Luc Ferron | 48cdc2e | 2018-05-31 09:58:34 -0400 | [diff] [blame] | 651 | const sh::BlockMemberInfo &layoutInfo = uniformBlock.uniformLayout[location]; |
| 652 | |
| 653 | // Assume an offset of -1 means the block is unused. |
| 654 | if (layoutInfo.offset == -1) |
| 655 | { |
| 656 | continue; |
| 657 | } |
| 658 | |
Luc Ferron | c8fbff3 | 2018-06-04 10:30:48 -0400 | [diff] [blame] | 659 | bool updated = SetFloatUniformMatrix<cols, rows>( |
Luc Ferron | 48cdc2e | 2018-05-31 09:58:34 -0400 | [diff] [blame] | 660 | locationInfo.arrayIndex, linkedUniform.getArraySizeProduct(), count, transpose, value, |
| 661 | uniformBlock.uniformData.data() + layoutInfo.offset); |
Luc Ferron | c8fbff3 | 2018-06-04 10:30:48 -0400 | [diff] [blame] | 662 | |
| 663 | // If the uniformsDirty flag was true, we don't want to flip it to false here if the |
| 664 | // setter did not update any data. We still want the uniform to be included when we'll |
| 665 | // update the descriptor sets. |
Jamie Madill | 2b858c2 | 2018-09-03 13:58:14 -0400 | [diff] [blame] | 666 | if (updated) |
| 667 | { |
| 668 | mDefaultUniformBlocksDirty.set(shaderType); |
| 669 | } |
Luc Ferron | 48cdc2e | 2018-05-31 09:58:34 -0400 | [diff] [blame] | 670 | } |
| 671 | } |
| 672 | |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 673 | void ProgramVk::setUniformMatrix2fv(GLint location, |
| 674 | GLsizei count, |
| 675 | GLboolean transpose, |
| 676 | const GLfloat *value) |
| 677 | { |
Luc Ferron | 48cdc2e | 2018-05-31 09:58:34 -0400 | [diff] [blame] | 678 | setUniformMatrixfv<2, 2>(location, count, transpose, value); |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 679 | } |
| 680 | |
| 681 | void ProgramVk::setUniformMatrix3fv(GLint location, |
| 682 | GLsizei count, |
| 683 | GLboolean transpose, |
| 684 | const GLfloat *value) |
| 685 | { |
Luc Ferron | 48cdc2e | 2018-05-31 09:58:34 -0400 | [diff] [blame] | 686 | setUniformMatrixfv<3, 3>(location, count, transpose, value); |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 687 | } |
| 688 | |
| 689 | void ProgramVk::setUniformMatrix4fv(GLint location, |
| 690 | GLsizei count, |
| 691 | GLboolean transpose, |
| 692 | const GLfloat *value) |
| 693 | { |
Luc Ferron | 48cdc2e | 2018-05-31 09:58:34 -0400 | [diff] [blame] | 694 | setUniformMatrixfv<4, 4>(location, count, transpose, value); |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 695 | } |
| 696 | |
| 697 | void ProgramVk::setUniformMatrix2x3fv(GLint location, |
| 698 | GLsizei count, |
| 699 | GLboolean transpose, |
| 700 | const GLfloat *value) |
| 701 | { |
Luc Ferron | 48cdc2e | 2018-05-31 09:58:34 -0400 | [diff] [blame] | 702 | setUniformMatrixfv<2, 3>(location, count, transpose, value); |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 703 | } |
| 704 | |
| 705 | void ProgramVk::setUniformMatrix3x2fv(GLint location, |
| 706 | GLsizei count, |
| 707 | GLboolean transpose, |
| 708 | const GLfloat *value) |
| 709 | { |
Luc Ferron | 48cdc2e | 2018-05-31 09:58:34 -0400 | [diff] [blame] | 710 | setUniformMatrixfv<3, 2>(location, count, transpose, value); |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 711 | } |
| 712 | |
| 713 | void ProgramVk::setUniformMatrix2x4fv(GLint location, |
| 714 | GLsizei count, |
| 715 | GLboolean transpose, |
| 716 | const GLfloat *value) |
| 717 | { |
Luc Ferron | 48cdc2e | 2018-05-31 09:58:34 -0400 | [diff] [blame] | 718 | setUniformMatrixfv<2, 4>(location, count, transpose, value); |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 719 | } |
| 720 | |
| 721 | void ProgramVk::setUniformMatrix4x2fv(GLint location, |
| 722 | GLsizei count, |
| 723 | GLboolean transpose, |
| 724 | const GLfloat *value) |
| 725 | { |
Luc Ferron | 48cdc2e | 2018-05-31 09:58:34 -0400 | [diff] [blame] | 726 | setUniformMatrixfv<4, 2>(location, count, transpose, value); |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 727 | } |
| 728 | |
| 729 | void ProgramVk::setUniformMatrix3x4fv(GLint location, |
| 730 | GLsizei count, |
| 731 | GLboolean transpose, |
| 732 | const GLfloat *value) |
| 733 | { |
Luc Ferron | 48cdc2e | 2018-05-31 09:58:34 -0400 | [diff] [blame] | 734 | setUniformMatrixfv<3, 4>(location, count, transpose, value); |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 735 | } |
| 736 | |
| 737 | void ProgramVk::setUniformMatrix4x3fv(GLint location, |
| 738 | GLsizei count, |
| 739 | GLboolean transpose, |
| 740 | const GLfloat *value) |
| 741 | { |
Luc Ferron | 48cdc2e | 2018-05-31 09:58:34 -0400 | [diff] [blame] | 742 | setUniformMatrixfv<4, 3>(location, count, transpose, value); |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 743 | } |
| 744 | |
Sami Väisänen | 46eaa94 | 2016-06-29 10:26:37 +0300 | [diff] [blame] | 745 | void ProgramVk::setPathFragmentInputGen(const std::string &inputName, |
| 746 | GLenum genMode, |
| 747 | GLint components, |
| 748 | const GLfloat *coeffs) |
| 749 | { |
| 750 | UNIMPLEMENTED(); |
| 751 | } |
| 752 | |
Jamie Madill | 2106102 | 2018-07-12 23:56:30 -0400 | [diff] [blame] | 753 | angle::Result ProgramVk::allocateDescriptorSet(ContextVk *contextVk, uint32_t descriptorSetIndex) |
Jamie Madill | 76e471e | 2017-10-21 09:56:01 -0400 | [diff] [blame] | 754 | { |
Jamie Madill | 76e471e | 2017-10-21 09:56:01 -0400 | [diff] [blame] | 755 | // Write out to a new a descriptor set. |
Jamie Madill | edeaa83 | 2018-06-22 09:18:41 -0400 | [diff] [blame] | 756 | vk::DynamicDescriptorPool *dynamicDescriptorPool = |
| 757 | contextVk->getDynamicDescriptorPool(descriptorSetIndex); |
Jamie Madill | 76e471e | 2017-10-21 09:56:01 -0400 | [diff] [blame] | 758 | |
Luc Ferron | 6ea1b41 | 2018-03-21 16:13:01 -0400 | [diff] [blame] | 759 | uint32_t potentialNewCount = descriptorSetIndex + 1; |
| 760 | if (potentialNewCount > mDescriptorSets.size()) |
| 761 | { |
| 762 | mDescriptorSets.resize(potentialNewCount, VK_NULL_HANDLE); |
| 763 | } |
Luc Ferron | 7a06ac1 | 2018-03-15 10:17:04 -0400 | [diff] [blame] | 764 | |
Jamie Madill | c7918ce | 2018-06-13 13:25:31 -0400 | [diff] [blame] | 765 | const vk::DescriptorSetLayout &descriptorSetLayout = |
Jamie Madill | 9b168d0 | 2018-06-13 13:25:32 -0400 | [diff] [blame] | 766 | mDescriptorSetLayouts[descriptorSetIndex].get(); |
Jamie Madill | 8a4c49f | 2018-06-21 15:43:06 -0400 | [diff] [blame] | 767 | ANGLE_TRY(dynamicDescriptorPool->allocateSets(contextVk, descriptorSetLayout.ptr(), 1, |
Jamie Madill | 78bcd2b | 2018-10-16 15:05:20 -0400 | [diff] [blame] | 768 | &mDescriptorPoolBindings[descriptorSetIndex], |
Jamie Madill | 8a4c49f | 2018-06-21 15:43:06 -0400 | [diff] [blame] | 769 | &mDescriptorSets[descriptorSetIndex])); |
Shahbaz Youssefi | 216f73d | 2019-04-12 13:32:30 -0400 | [diff] [blame] | 770 | mEmptyDescriptorSets[descriptorSetIndex] = VK_NULL_HANDLE; |
| 771 | |
Jamie Madill | 7c985f5 | 2018-11-29 18:16:17 -0500 | [diff] [blame] | 772 | return angle::Result::Continue; |
Jamie Madill | 76e471e | 2017-10-21 09:56:01 -0400 | [diff] [blame] | 773 | } |
| 774 | |
Jamie Madill | 54164b0 | 2017-08-28 15:17:37 -0400 | [diff] [blame] | 775 | void ProgramVk::getUniformfv(const gl::Context *context, GLint location, GLfloat *params) const |
| 776 | { |
Luc Ferron | 7cec335 | 2018-03-13 13:29:34 -0400 | [diff] [blame] | 777 | getUniformImpl(location, params, GL_FLOAT); |
Jamie Madill | 54164b0 | 2017-08-28 15:17:37 -0400 | [diff] [blame] | 778 | } |
| 779 | |
| 780 | void ProgramVk::getUniformiv(const gl::Context *context, GLint location, GLint *params) const |
| 781 | { |
Luc Ferron | 7cec335 | 2018-03-13 13:29:34 -0400 | [diff] [blame] | 782 | getUniformImpl(location, params, GL_INT); |
Jamie Madill | 54164b0 | 2017-08-28 15:17:37 -0400 | [diff] [blame] | 783 | } |
| 784 | |
| 785 | void ProgramVk::getUniformuiv(const gl::Context *context, GLint location, GLuint *params) const |
| 786 | { |
| 787 | UNIMPLEMENTED(); |
| 788 | } |
| 789 | |
Jamie Madill | 2106102 | 2018-07-12 23:56:30 -0400 | [diff] [blame] | 790 | angle::Result ProgramVk::updateUniforms(ContextVk *contextVk) |
Jamie Madill | 76e471e | 2017-10-21 09:56:01 -0400 | [diff] [blame] | 791 | { |
Jamie Madill | 88fc6da | 2018-08-30 16:18:36 -0400 | [diff] [blame] | 792 | ASSERT(dirtyUniforms()); |
Jamie Madill | 76e471e | 2017-10-21 09:56:01 -0400 | [diff] [blame] | 793 | |
Jamie Madill | 76e471e | 2017-10-21 09:56:01 -0400 | [diff] [blame] | 794 | // Update buffer memory by immediate mapping. This immediate update only works once. |
Luc Ferron | 7a06ac1 | 2018-03-15 10:17:04 -0400 | [diff] [blame] | 795 | bool anyNewBufferAllocated = false; |
Shahbaz Youssefi | df066e9 | 2018-11-13 15:39:47 -0500 | [diff] [blame] | 796 | for (gl::ShaderType shaderType : gl::AllGLES2ShaderTypes()) |
Jamie Madill | 76e471e | 2017-10-21 09:56:01 -0400 | [diff] [blame] | 797 | { |
Jamie Madill | 33318de | 2018-05-01 11:22:54 -0400 | [diff] [blame] | 798 | DefaultUniformBlock &uniformBlock = mDefaultUniformBlocks[shaderType]; |
Luc Ferron | 7a06ac1 | 2018-03-15 10:17:04 -0400 | [diff] [blame] | 799 | |
Jamie Madill | 2b858c2 | 2018-09-03 13:58:14 -0400 | [diff] [blame] | 800 | if (mDefaultUniformBlocksDirty[shaderType]) |
Jamie Madill | 76e471e | 2017-10-21 09:56:01 -0400 | [diff] [blame] | 801 | { |
Luc Ferron | 7a06ac1 | 2018-03-15 10:17:04 -0400 | [diff] [blame] | 802 | bool bufferModified = false; |
Jamie Madill | 2106102 | 2018-07-12 23:56:30 -0400 | [diff] [blame] | 803 | ANGLE_TRY(SyncDefaultUniformBlock(contextVk, &uniformBlock.storage, |
Luc Ferron | 7a06ac1 | 2018-03-15 10:17:04 -0400 | [diff] [blame] | 804 | uniformBlock.uniformData, |
Jamie Madill | 33318de | 2018-05-01 11:22:54 -0400 | [diff] [blame] | 805 | &mUniformBlocksOffsets[shaderType], &bufferModified)); |
Jamie Madill | 2b858c2 | 2018-09-03 13:58:14 -0400 | [diff] [blame] | 806 | mDefaultUniformBlocksDirty.reset(shaderType); |
Luc Ferron | 7a06ac1 | 2018-03-15 10:17:04 -0400 | [diff] [blame] | 807 | |
| 808 | if (bufferModified) |
| 809 | { |
| 810 | anyNewBufferAllocated = true; |
| 811 | } |
Jamie Madill | 76e471e | 2017-10-21 09:56:01 -0400 | [diff] [blame] | 812 | } |
| 813 | } |
| 814 | |
Luc Ferron | 7a06ac1 | 2018-03-15 10:17:04 -0400 | [diff] [blame] | 815 | if (anyNewBufferAllocated) |
| 816 | { |
| 817 | // We need to reinitialize the descriptor sets if we newly allocated buffers since we can't |
| 818 | // modify the descriptor sets once initialized. |
Jamie Madill | c7918ce | 2018-06-13 13:25:31 -0400 | [diff] [blame] | 819 | ANGLE_TRY(allocateDescriptorSet(contextVk, kUniformsDescriptorSetIndex)); |
Luc Ferron | 7a06ac1 | 2018-03-15 10:17:04 -0400 | [diff] [blame] | 820 | ANGLE_TRY(updateDefaultUniformsDescriptorSet(contextVk)); |
| 821 | } |
| 822 | |
Jamie Madill | 7c985f5 | 2018-11-29 18:16:17 -0500 | [diff] [blame] | 823 | return angle::Result::Continue; |
Jamie Madill | 76e471e | 2017-10-21 09:56:01 -0400 | [diff] [blame] | 824 | } |
| 825 | |
Jamie Madill | 2106102 | 2018-07-12 23:56:30 -0400 | [diff] [blame] | 826 | angle::Result ProgramVk::updateDefaultUniformsDescriptorSet(ContextVk *contextVk) |
Jamie Madill | 76e471e | 2017-10-21 09:56:01 -0400 | [diff] [blame] | 827 | { |
Shahbaz Youssefi | df066e9 | 2018-11-13 15:39:47 -0500 | [diff] [blame] | 828 | gl::ShaderMap<VkDescriptorBufferInfo> descriptorBufferInfo; |
| 829 | gl::ShaderMap<VkWriteDescriptorSet> writeDescriptorInfo; |
Jamie Madill | 76e471e | 2017-10-21 09:56:01 -0400 | [diff] [blame] | 830 | |
Shahbaz Youssefi | 216f73d | 2019-04-12 13:32:30 -0400 | [diff] [blame] | 831 | // Write default uniforms for each shader type. |
Shahbaz Youssefi | df066e9 | 2018-11-13 15:39:47 -0500 | [diff] [blame] | 832 | for (gl::ShaderType shaderType : gl::AllGLES2ShaderTypes()) |
Jamie Madill | 76e471e | 2017-10-21 09:56:01 -0400 | [diff] [blame] | 833 | { |
Jamie Madill | 242c4fe | 2018-07-12 15:56:56 -0400 | [diff] [blame] | 834 | DefaultUniformBlock &uniformBlock = mDefaultUniformBlocks[shaderType]; |
| 835 | VkDescriptorBufferInfo &bufferInfo = descriptorBufferInfo[shaderType]; |
| 836 | VkWriteDescriptorSet &writeInfo = writeDescriptorInfo[shaderType]; |
Jamie Madill | 76e471e | 2017-10-21 09:56:01 -0400 | [diff] [blame] | 837 | |
| 838 | if (!uniformBlock.uniformData.empty()) |
| 839 | { |
Shahbaz Youssefi | 216f73d | 2019-04-12 13:32:30 -0400 | [diff] [blame] | 840 | const vk::BufferHelper *bufferHelper = uniformBlock.storage.getCurrentBuffer(); |
| 841 | bufferInfo.buffer = bufferHelper->getBuffer().getHandle(); |
Jamie Madill | 76e471e | 2017-10-21 09:56:01 -0400 | [diff] [blame] | 842 | } |
| 843 | else |
| 844 | { |
Shahbaz Youssefi | 254b32c | 2018-11-26 11:58:03 -0500 | [diff] [blame] | 845 | bufferInfo.buffer = mEmptyUniformBlockStorage.getBuffer().getHandle(); |
Jamie Madill | 76e471e | 2017-10-21 09:56:01 -0400 | [diff] [blame] | 846 | } |
| 847 | |
| 848 | bufferInfo.offset = 0; |
| 849 | bufferInfo.range = VK_WHOLE_SIZE; |
| 850 | |
Jamie Madill | 76e471e | 2017-10-21 09:56:01 -0400 | [diff] [blame] | 851 | writeInfo.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET; |
| 852 | writeInfo.pNext = nullptr; |
Shahbaz Youssefi | 216f73d | 2019-04-12 13:32:30 -0400 | [diff] [blame] | 853 | writeInfo.dstSet = mDescriptorSets[kUniformsDescriptorSetIndex]; |
Jamie Madill | 33318de | 2018-05-01 11:22:54 -0400 | [diff] [blame] | 854 | writeInfo.dstBinding = static_cast<uint32_t>(shaderType); |
Jamie Madill | 76e471e | 2017-10-21 09:56:01 -0400 | [diff] [blame] | 855 | writeInfo.dstArrayElement = 0; |
| 856 | writeInfo.descriptorCount = 1; |
Luc Ferron | 7a06ac1 | 2018-03-15 10:17:04 -0400 | [diff] [blame] | 857 | writeInfo.descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC; |
Jamie Madill | 76e471e | 2017-10-21 09:56:01 -0400 | [diff] [blame] | 858 | writeInfo.pImageInfo = nullptr; |
| 859 | writeInfo.pBufferInfo = &bufferInfo; |
| 860 | writeInfo.pTexelBufferView = nullptr; |
Jamie Madill | 76e471e | 2017-10-21 09:56:01 -0400 | [diff] [blame] | 861 | } |
| 862 | |
| 863 | VkDevice device = contextVk->getDevice(); |
| 864 | |
Shahbaz Youssefi | 216f73d | 2019-04-12 13:32:30 -0400 | [diff] [blame] | 865 | constexpr uint32_t kShaderTypeMin = static_cast<uint32_t>(gl::kGLES2ShaderTypeMin); |
| 866 | constexpr uint32_t kShaderTypeMax = static_cast<uint32_t>(gl::kGLES2ShaderTypeMax); |
| 867 | constexpr uint32_t kGLES2ShaderCount = kShaderTypeMax - kShaderTypeMin + 1; |
| 868 | vkUpdateDescriptorSets(device, kGLES2ShaderCount, writeDescriptorInfo.data(), 0, nullptr); |
| 869 | |
| 870 | return angle::Result::Continue; |
| 871 | } |
| 872 | |
| 873 | angle::Result ProgramVk::updateUniformBuffersDescriptorSet(ContextVk *contextVk, |
| 874 | vk::FramebufferHelper *framebuffer) |
| 875 | { |
| 876 | ASSERT(hasUniformBuffers()); |
| 877 | ANGLE_TRY(allocateDescriptorSet(contextVk, kUniformBlockDescriptorSetIndex)); |
| 878 | |
| 879 | VkDescriptorSet descriptorSet = mDescriptorSets[kUniformBlockDescriptorSetIndex]; |
| 880 | |
| 881 | gl::UniformBuffersArray<VkDescriptorBufferInfo> descriptorBufferInfo; |
| 882 | gl::UniformBuffersArray<VkWriteDescriptorSet> writeDescriptorInfo; |
| 883 | uint32_t writeCount = 0; |
| 884 | uint32_t currentBinding = 0; |
| 885 | |
| 886 | // Write uniform buffers. |
| 887 | const gl::State &glState = contextVk->getState(); |
| 888 | const std::vector<gl::InterfaceBlock> &uniformBlocks = mState.getUniformBlocks(); |
| 889 | for (uint32_t bufferIndex = 0; bufferIndex < uniformBlocks.size(); ++bufferIndex) |
| 890 | { |
| 891 | if (glState.getIndexedUniformBuffer(uniformBlocks[bufferIndex].binding).get() == nullptr) |
| 892 | { |
| 893 | continue; |
| 894 | } |
| 895 | |
| 896 | VkWriteDescriptorSet &writeInfo = writeDescriptorInfo[writeCount]; |
| 897 | VkDescriptorBufferInfo &bufferInfo = descriptorBufferInfo[writeCount]; |
| 898 | |
| 899 | const gl::InterfaceBlock &uniformBlock = uniformBlocks[bufferIndex]; |
| 900 | const gl::OffsetBindingPointer<gl::Buffer> &bufferBinding = |
| 901 | glState.getIndexedUniformBuffer(uniformBlock.binding); |
| 902 | gl::Buffer *buffer = bufferBinding.get(); |
| 903 | ASSERT(buffer != nullptr); |
| 904 | |
| 905 | // Make sure there's no possible under/overflow with binding size. |
| 906 | static_assert(sizeof(VkDeviceSize) >= sizeof(bufferBinding.getSize()), |
| 907 | "VkDeviceSize too small"); |
| 908 | ASSERT(bufferBinding.getSize() >= 0); |
| 909 | |
| 910 | BufferVk *bufferVk = vk::GetImpl(buffer); |
| 911 | GLintptr offset = bufferBinding.getOffset(); |
| 912 | VkDeviceSize size = bufferBinding.getSize(); |
| 913 | VkDeviceSize blockSize = uniformBlock.dataSize; |
| 914 | vk::BufferHelper &bufferHelper = bufferVk->getBuffer(); |
| 915 | |
| 916 | ANGLE_TRY(bufferVk->onRead(contextVk, framebuffer, VK_ACCESS_UNIFORM_READ_BIT)); |
| 917 | |
| 918 | // If size is 0, we can't always use VK_WHOLE_SIZE (or bufferHelper.getSize()), as the |
| 919 | // backing buffer may be larger than maxUniformBufferRange. In that case, we use the |
| 920 | // minimum of the backing buffer size (what's left after offset) and the uniform buffer |
| 921 | // size as defined by the shader. |
| 922 | size = std::min(size > 0 ? size : (bufferHelper.getSize() - offset), blockSize); |
| 923 | |
| 924 | bufferInfo.buffer = bufferHelper.getBuffer().getHandle(); |
| 925 | bufferInfo.offset = offset; |
| 926 | bufferInfo.range = size; |
| 927 | |
| 928 | if (!uniformBlock.isArray || uniformBlock.arrayElement == 0) |
| 929 | { |
| 930 | // Array indices of the same buffer binding are placed sequentially in `uniformBlocks`. |
| 931 | // Thus, the uniform block binding is updated only when array index 0 is encountered. |
| 932 | currentBinding = bufferIndex; |
| 933 | } |
| 934 | |
| 935 | writeInfo.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET; |
| 936 | writeInfo.pNext = nullptr; |
| 937 | writeInfo.dstSet = descriptorSet; |
| 938 | writeInfo.dstBinding = currentBinding; |
| 939 | writeInfo.dstArrayElement = uniformBlock.isArray ? uniformBlock.arrayElement : 0; |
| 940 | writeInfo.descriptorCount = 1; |
| 941 | writeInfo.descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER; |
| 942 | writeInfo.pImageInfo = nullptr; |
| 943 | writeInfo.pBufferInfo = &bufferInfo; |
| 944 | writeInfo.pTexelBufferView = nullptr; |
| 945 | ASSERT(writeInfo.pBufferInfo[0].buffer != VK_NULL_HANDLE); |
| 946 | |
| 947 | ++writeCount; |
| 948 | } |
| 949 | |
| 950 | VkDevice device = contextVk->getDevice(); |
| 951 | |
| 952 | vkUpdateDescriptorSets(device, writeCount, writeDescriptorInfo.data(), 0, nullptr); |
Jamie Madill | 76e471e | 2017-10-21 09:56:01 -0400 | [diff] [blame] | 953 | |
Jamie Madill | 7c985f5 | 2018-11-29 18:16:17 -0500 | [diff] [blame] | 954 | return angle::Result::Continue; |
Jamie Madill | 76e471e | 2017-10-21 09:56:01 -0400 | [diff] [blame] | 955 | } |
| 956 | |
Shahbaz Youssefi | f83a28a | 2018-12-09 03:48:34 +0100 | [diff] [blame] | 957 | angle::Result ProgramVk::updateTexturesDescriptorSet(ContextVk *contextVk, |
| 958 | vk::FramebufferHelper *framebuffer) |
Jamie Madill | 5547b38 | 2017-10-23 18:16:01 -0400 | [diff] [blame] | 959 | { |
Jamie Madill | 88fc6da | 2018-08-30 16:18:36 -0400 | [diff] [blame] | 960 | ASSERT(hasTextures()); |
Jamie Madill | c7918ce | 2018-06-13 13:25:31 -0400 | [diff] [blame] | 961 | ANGLE_TRY(allocateDescriptorSet(contextVk, kTextureDescriptorSetIndex)); |
Luc Ferron | 6ea1b41 | 2018-03-21 16:13:01 -0400 | [diff] [blame] | 962 | |
Jamie Madill | c7918ce | 2018-06-13 13:25:31 -0400 | [diff] [blame] | 963 | VkDescriptorSet descriptorSet = mDescriptorSets[kTextureDescriptorSetIndex]; |
Jamie Madill | 5547b38 | 2017-10-23 18:16:01 -0400 | [diff] [blame] | 964 | |
Jamie Madill | 84c662b | 2018-07-12 15:56:55 -0400 | [diff] [blame] | 965 | gl::ActiveTextureArray<VkDescriptorImageInfo> descriptorImageInfo; |
| 966 | gl::ActiveTextureArray<VkWriteDescriptorSet> writeDescriptorInfo; |
Jamie Madill | 4cc753e | 2018-06-13 13:25:33 -0400 | [diff] [blame] | 967 | uint32_t writeCount = 0; |
Jamie Madill | 5547b38 | 2017-10-23 18:16:01 -0400 | [diff] [blame] | 968 | |
Jamie Madill | 84c662b | 2018-07-12 15:56:55 -0400 | [diff] [blame] | 969 | const gl::ActiveTextureArray<TextureVk *> &activeTextures = contextVk->getActiveTextures(); |
Jamie Madill | 5547b38 | 2017-10-23 18:16:01 -0400 | [diff] [blame] | 970 | |
Jamie Madill | 4cc753e | 2018-06-13 13:25:33 -0400 | [diff] [blame] | 971 | for (uint32_t textureIndex = 0; textureIndex < mState.getSamplerBindings().size(); |
| 972 | ++textureIndex) |
Jamie Madill | 5547b38 | 2017-10-23 18:16:01 -0400 | [diff] [blame] | 973 | { |
Jamie Madill | 4cc753e | 2018-06-13 13:25:33 -0400 | [diff] [blame] | 974 | const gl::SamplerBinding &samplerBinding = mState.getSamplerBindings()[textureIndex]; |
| 975 | |
Jamie Madill | 5547b38 | 2017-10-23 18:16:01 -0400 | [diff] [blame] | 976 | ASSERT(!samplerBinding.unreferenced); |
| 977 | |
Jamie Madill | 4cc753e | 2018-06-13 13:25:33 -0400 | [diff] [blame] | 978 | for (uint32_t arrayElement = 0; arrayElement < samplerBinding.boundTextureUnits.size(); |
| 979 | ++arrayElement) |
Luc Ferron | 9096836 | 2018-05-04 08:47:22 -0400 | [diff] [blame] | 980 | { |
Jamie Madill | dbc605c | 2019-01-04 16:39:14 -0500 | [diff] [blame] | 981 | GLuint textureUnit = samplerBinding.boundTextureUnits[arrayElement]; |
| 982 | TextureVk *textureVk = activeTextures[textureUnit]; |
Shahbaz Youssefi | f83a28a | 2018-12-09 03:48:34 +0100 | [diff] [blame] | 983 | |
| 984 | // Ensure any writes to the textures are flushed before we read from them. |
| 985 | ANGLE_TRY(textureVk->ensureImageInitialized(contextVk)); |
| 986 | vk::ImageHelper &image = textureVk->getImage(); |
| 987 | |
| 988 | // Ensure the image is in read-only layout |
Shahbaz Youssefi | 7dafe3e | 2019-01-28 11:39:15 -0500 | [diff] [blame] | 989 | if (image.isLayoutChangeNecessary(vk::ImageLayout::FragmentShaderReadOnly)) |
Shahbaz Youssefi | f83a28a | 2018-12-09 03:48:34 +0100 | [diff] [blame] | 990 | { |
Shahbaz Youssefi | 2660b50 | 2019-03-21 12:08:40 -0400 | [diff] [blame] | 991 | vk::CommandBuffer *srcLayoutChange; |
Shahbaz Youssefi | f83a28a | 2018-12-09 03:48:34 +0100 | [diff] [blame] | 992 | ANGLE_TRY(image.recordCommands(contextVk, &srcLayoutChange)); |
| 993 | |
Shahbaz Youssefi | 7dafe3e | 2019-01-28 11:39:15 -0500 | [diff] [blame] | 994 | image.changeLayout(VK_IMAGE_ASPECT_COLOR_BIT, |
| 995 | vk::ImageLayout::FragmentShaderReadOnly, srcLayoutChange); |
Shahbaz Youssefi | f83a28a | 2018-12-09 03:48:34 +0100 | [diff] [blame] | 996 | } |
| 997 | |
| 998 | image.addReadDependency(framebuffer); |
Jamie Madill | 4cc753e | 2018-06-13 13:25:33 -0400 | [diff] [blame] | 999 | |
| 1000 | VkDescriptorImageInfo &imageInfo = descriptorImageInfo[writeCount]; |
| 1001 | |
| 1002 | imageInfo.sampler = textureVk->getSampler().getHandle(); |
Shahbaz Youssefi | f83a28a | 2018-12-09 03:48:34 +0100 | [diff] [blame] | 1003 | imageInfo.imageView = textureVk->getReadImageView().getHandle(); |
Jamie Madill | 4cc753e | 2018-06-13 13:25:33 -0400 | [diff] [blame] | 1004 | imageInfo.imageLayout = image.getCurrentLayout(); |
| 1005 | |
| 1006 | VkWriteDescriptorSet &writeInfo = writeDescriptorInfo[writeCount]; |
| 1007 | |
| 1008 | writeInfo.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET; |
| 1009 | writeInfo.pNext = nullptr; |
| 1010 | writeInfo.dstSet = descriptorSet; |
| 1011 | writeInfo.dstBinding = textureIndex; |
| 1012 | writeInfo.dstArrayElement = arrayElement; |
| 1013 | writeInfo.descriptorCount = 1; |
| 1014 | writeInfo.descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER; |
| 1015 | writeInfo.pImageInfo = &imageInfo; |
| 1016 | writeInfo.pBufferInfo = nullptr; |
| 1017 | writeInfo.pTexelBufferView = nullptr; |
| 1018 | |
| 1019 | writeCount++; |
Luc Ferron | 9096836 | 2018-05-04 08:47:22 -0400 | [diff] [blame] | 1020 | } |
Jamie Madill | 5547b38 | 2017-10-23 18:16:01 -0400 | [diff] [blame] | 1021 | } |
| 1022 | |
| 1023 | VkDevice device = contextVk->getDevice(); |
| 1024 | |
Jamie Madill | 4cc753e | 2018-06-13 13:25:33 -0400 | [diff] [blame] | 1025 | ASSERT(writeCount > 0); |
| 1026 | vkUpdateDescriptorSets(device, writeCount, writeDescriptorInfo.data(), 0, nullptr); |
Jamie Madill | 5547b38 | 2017-10-23 18:16:01 -0400 | [diff] [blame] | 1027 | |
Jamie Madill | 7c985f5 | 2018-11-29 18:16:17 -0500 | [diff] [blame] | 1028 | return angle::Result::Continue; |
Jamie Madill | 5547b38 | 2017-10-23 18:16:01 -0400 | [diff] [blame] | 1029 | } |
| 1030 | |
Luc Ferron | 7a06ac1 | 2018-03-15 10:17:04 -0400 | [diff] [blame] | 1031 | void ProgramVk::setDefaultUniformBlocksMinSizeForTesting(size_t minSize) |
| 1032 | { |
| 1033 | for (DefaultUniformBlock &block : mDefaultUniformBlocks) |
| 1034 | { |
Jamie Madill | 6c7ab7f | 2018-03-31 14:19:15 -0400 | [diff] [blame] | 1035 | block.storage.setMinimumSizeForTesting(minSize); |
Luc Ferron | 7a06ac1 | 2018-03-15 10:17:04 -0400 | [diff] [blame] | 1036 | } |
| 1037 | } |
Jamie Madill | 242c4fe | 2018-07-12 15:56:56 -0400 | [diff] [blame] | 1038 | |
Shahbaz Youssefi | 2660b50 | 2019-03-21 12:08:40 -0400 | [diff] [blame] | 1039 | angle::Result ProgramVk::updateDescriptorSets(ContextVk *contextVk, |
| 1040 | vk::CommandBuffer *commandBuffer) |
Jamie Madill | 242c4fe | 2018-07-12 15:56:56 -0400 | [diff] [blame] | 1041 | { |
Jamie Madill | 242c4fe | 2018-07-12 15:56:56 -0400 | [diff] [blame] | 1042 | // Can probably use better dirty bits here. |
Jamie Madill | 242c4fe | 2018-07-12 15:56:56 -0400 | [diff] [blame] | 1043 | |
Shahbaz Youssefi | 216f73d | 2019-04-12 13:32:30 -0400 | [diff] [blame] | 1044 | if (mDescriptorSets.empty()) |
Jamie Madill | 7c985f5 | 2018-11-29 18:16:17 -0500 | [diff] [blame] | 1045 | return angle::Result::Continue; |
Jamie Madill | 242c4fe | 2018-07-12 15:56:56 -0400 | [diff] [blame] | 1046 | |
Shahbaz Youssefi | 216f73d | 2019-04-12 13:32:30 -0400 | [diff] [blame] | 1047 | // Find the maximum non-null descriptor set. This is used in conjunction with a driver |
| 1048 | // workaround to bind empty descriptor sets only for gaps in between 0 and max and avoid |
| 1049 | // binding unnecessary empty descriptor sets for the sets beyond max. |
| 1050 | size_t descriptorSetRange = 0; |
| 1051 | for (size_t descriptorSetIndex = 0; descriptorSetIndex < mDescriptorSets.size(); |
| 1052 | ++descriptorSetIndex) |
Jamie Madill | 242c4fe | 2018-07-12 15:56:56 -0400 | [diff] [blame] | 1053 | { |
Shahbaz Youssefi | 216f73d | 2019-04-12 13:32:30 -0400 | [diff] [blame] | 1054 | if (mDescriptorSets[descriptorSetIndex] != VK_NULL_HANDLE) |
| 1055 | { |
| 1056 | descriptorSetRange = descriptorSetIndex + 1; |
| 1057 | } |
| 1058 | } |
| 1059 | |
| 1060 | for (size_t descriptorSetIndex = 0; descriptorSetIndex < descriptorSetRange; |
| 1061 | ++descriptorSetIndex) |
| 1062 | { |
| 1063 | VkDescriptorSet descSet = mDescriptorSets[descriptorSetIndex]; |
| 1064 | if (descSet == VK_NULL_HANDLE) |
| 1065 | { |
Jonah Ryan-Davis | 776694c | 2019-05-08 10:28:55 -0400 | [diff] [blame^] | 1066 | if (!contextVk->getRenderer()->getFeatures().bindEmptyForUnusedDescriptorSets.enabled) |
Shahbaz Youssefi | 216f73d | 2019-04-12 13:32:30 -0400 | [diff] [blame] | 1067 | { |
| 1068 | continue; |
| 1069 | } |
| 1070 | |
| 1071 | // Workaround a driver bug where missing (though unused) descriptor sets indices cause |
| 1072 | // later sets to misbehave. |
| 1073 | if (mEmptyDescriptorSets[descriptorSetIndex] == VK_NULL_HANDLE) |
| 1074 | { |
| 1075 | vk::DynamicDescriptorPool *dynamicDescriptorPool = |
| 1076 | contextVk->getDynamicDescriptorPool(descriptorSetIndex); |
| 1077 | const vk::DescriptorSetLayout &descriptorSetLayout = |
| 1078 | mDescriptorSetLayouts[descriptorSetIndex].get(); |
| 1079 | |
| 1080 | ANGLE_TRY(dynamicDescriptorPool->allocateSets( |
| 1081 | contextVk, descriptorSetLayout.ptr(), 1, |
| 1082 | &mDescriptorPoolBindings[descriptorSetIndex], |
| 1083 | &mEmptyDescriptorSets[descriptorSetIndex])); |
| 1084 | } |
| 1085 | descSet = mEmptyDescriptorSets[descriptorSetIndex]; |
| 1086 | } |
| 1087 | |
Shahbaz Youssefi | df066e9 | 2018-11-13 15:39:47 -0500 | [diff] [blame] | 1088 | constexpr uint32_t kShaderTypeMin = static_cast<uint32_t>(gl::kGLES2ShaderTypeMin); |
| 1089 | constexpr uint32_t kShaderTypeMax = static_cast<uint32_t>(gl::kGLES2ShaderTypeMax); |
Shahbaz Youssefi | 216f73d | 2019-04-12 13:32:30 -0400 | [diff] [blame] | 1090 | constexpr uint32_t kShaderTypeCount = kShaderTypeMax - kShaderTypeMin + 1; |
| 1091 | |
| 1092 | // Default uniforms are encompassed in a block per shader stage, and they are assigned |
| 1093 | // through dynamic uniform buffers (requiring dynamic offsets). No other descriptor |
| 1094 | // requires a dynamic offset. |
| 1095 | const uint32_t uniformBlockOffsetCount = |
| 1096 | descriptorSetIndex == kUniformsDescriptorSetIndex ? kShaderTypeCount : 0; |
| 1097 | |
| 1098 | commandBuffer->bindGraphicsDescriptorSets(mPipelineLayout.get(), descriptorSetIndex, 1, |
| 1099 | &descSet, uniformBlockOffsetCount, |
| 1100 | mUniformBlocksOffsets.data() + kShaderTypeMin); |
Jamie Madill | 242c4fe | 2018-07-12 15:56:56 -0400 | [diff] [blame] | 1101 | } |
| 1102 | |
Jamie Madill | 7c985f5 | 2018-11-29 18:16:17 -0500 | [diff] [blame] | 1103 | return angle::Result::Continue; |
Jamie Madill | 242c4fe | 2018-07-12 15:56:56 -0400 | [diff] [blame] | 1104 | } |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 1105 | } // namespace rx |