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 | 76e471e | 2017-10-21 09:56:01 -0400 | [diff] [blame] | 13 | #include "common/utilities.h" |
Jamie Madill | c564c07 | 2017-06-01 12:45:42 -0400 | [diff] [blame] | 14 | #include "libANGLE/Context.h" |
Jamie Madill | 8ecf7f9 | 2017-01-13 17:29:52 -0500 | [diff] [blame] | 15 | #include "libANGLE/renderer/vulkan/ContextVk.h" |
| 16 | #include "libANGLE/renderer/vulkan/GlslangWrapper.h" |
| 17 | #include "libANGLE/renderer/vulkan/RendererVk.h" |
Jamie Madill | 5547b38 | 2017-10-23 18:16:01 -0400 | [diff] [blame] | 18 | #include "libANGLE/renderer/vulkan/TextureVk.h" |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 19 | |
| 20 | namespace rx |
| 21 | { |
| 22 | |
Jamie Madill | 76e471e | 2017-10-21 09:56:01 -0400 | [diff] [blame] | 23 | namespace |
| 24 | { |
| 25 | |
| 26 | gl::Error InitDefaultUniformBlock(const gl::Context *context, |
| 27 | VkDevice device, |
| 28 | gl::Shader *shader, |
| 29 | vk::BufferAndMemory *storageOut, |
| 30 | sh::BlockLayoutMap *blockLayoutMapOut, |
| 31 | size_t *requiredSizeOut) |
| 32 | { |
| 33 | const auto &uniforms = shader->getUniforms(context); |
| 34 | |
| 35 | if (uniforms.empty()) |
| 36 | { |
| 37 | *requiredSizeOut = 0; |
| 38 | return gl::NoError(); |
| 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 | |
| 44 | size_t blockSize = blockEncoder.getBlockSize(); |
| 45 | |
| 46 | // TODO(jmadill): I think we still need a valid block for the pipeline even if zero sized. |
| 47 | if (blockSize == 0) |
| 48 | { |
| 49 | *requiredSizeOut = 0; |
| 50 | return gl::NoError(); |
| 51 | } |
| 52 | |
| 53 | VkBufferCreateInfo uniformBufferInfo; |
| 54 | uniformBufferInfo.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO; |
| 55 | uniformBufferInfo.pNext = nullptr; |
| 56 | uniformBufferInfo.flags = 0; |
| 57 | uniformBufferInfo.size = blockSize; |
| 58 | uniformBufferInfo.usage = VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT; |
| 59 | uniformBufferInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE; |
| 60 | uniformBufferInfo.queueFamilyIndexCount = 0; |
| 61 | uniformBufferInfo.pQueueFamilyIndices = nullptr; |
| 62 | |
| 63 | ANGLE_TRY(storageOut->buffer.init(device, uniformBufferInfo)); |
| 64 | |
Jamie Madill | 57dd97a | 2018-02-06 17:10:49 -0500 | [diff] [blame] | 65 | // Assume host vislble/coherent memory available. |
| 66 | VkMemoryPropertyFlags flags = |
| 67 | (VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT); |
Jamie Madill | 57fbfd8 | 2018-02-14 12:45:34 -0500 | [diff] [blame] | 68 | |
| 69 | ContextVk *contextVk = vk::GetImpl(context); |
| 70 | |
| 71 | ANGLE_TRY(AllocateBufferMemory(contextVk->getRenderer(), flags, &storageOut->buffer, |
Jamie Madill | 76e471e | 2017-10-21 09:56:01 -0400 | [diff] [blame] | 72 | &storageOut->memory, requiredSizeOut)); |
| 73 | |
| 74 | return gl::NoError(); |
| 75 | } |
| 76 | |
| 77 | template <typename T> |
| 78 | void UpdateDefaultUniformBlock(GLsizei count, |
| 79 | int componentCount, |
| 80 | const T *v, |
| 81 | const sh::BlockMemberInfo &layoutInfo, |
| 82 | angle::MemoryBuffer *uniformData) |
| 83 | { |
| 84 | // Assume an offset of -1 means the block is unused. |
| 85 | if (layoutInfo.offset == -1) |
| 86 | { |
| 87 | return; |
| 88 | } |
| 89 | |
| 90 | int elementSize = sizeof(T) * componentCount; |
| 91 | if (layoutInfo.arrayStride == 0 || layoutInfo.arrayStride == elementSize) |
| 92 | { |
| 93 | uint8_t *writePtr = uniformData->data() + layoutInfo.offset; |
| 94 | memcpy(writePtr, v, elementSize * count); |
| 95 | } |
| 96 | else |
| 97 | { |
| 98 | UNIMPLEMENTED(); |
| 99 | } |
| 100 | } |
| 101 | |
Luc Ferron | 7cec335 | 2018-03-13 13:29:34 -0400 | [diff] [blame^] | 102 | template <typename T> |
| 103 | void ReadFromDefaultUniformBlock(int componentCount, |
| 104 | T *dst, |
| 105 | const sh::BlockMemberInfo &layoutInfo, |
| 106 | const angle::MemoryBuffer *uniformData) |
| 107 | { |
| 108 | ASSERT(layoutInfo.offset != -1); |
| 109 | |
| 110 | int elementSize = sizeof(T) * componentCount; |
| 111 | if (layoutInfo.arrayStride == 0 || layoutInfo.arrayStride == elementSize) |
| 112 | { |
| 113 | const uint8_t *readPtr = uniformData->data() + layoutInfo.offset; |
| 114 | memcpy(dst, readPtr, elementSize); |
| 115 | } |
| 116 | else |
| 117 | { |
| 118 | UNIMPLEMENTED(); |
| 119 | } |
| 120 | } |
| 121 | |
Jamie Madill | 76e471e | 2017-10-21 09:56:01 -0400 | [diff] [blame] | 122 | vk::Error SyncDefaultUniformBlock(VkDevice device, |
| 123 | vk::DeviceMemory *bufferMemory, |
| 124 | const angle::MemoryBuffer &bufferData) |
| 125 | { |
| 126 | ASSERT(bufferMemory->valid() && !bufferData.empty()); |
| 127 | uint8_t *mapPointer = nullptr; |
| 128 | ANGLE_TRY(bufferMemory->map(device, 0, bufferData.size(), 0, &mapPointer)); |
| 129 | memcpy(mapPointer, bufferData.data(), bufferData.size()); |
| 130 | bufferMemory->unmap(device); |
| 131 | return vk::NoError(); |
| 132 | } |
| 133 | |
| 134 | enum ShaderIndex : uint32_t |
| 135 | { |
| 136 | MinShaderIndex = 0, |
| 137 | VertexShader = MinShaderIndex, |
| 138 | FragmentShader = 1, |
| 139 | MaxShaderIndex = 2, |
| 140 | }; |
| 141 | |
| 142 | gl::Shader *GetShader(const gl::ProgramState &programState, uint32_t shaderIndex) |
| 143 | { |
| 144 | switch (shaderIndex) |
| 145 | { |
| 146 | case VertexShader: |
| 147 | return programState.getAttachedVertexShader(); |
| 148 | case FragmentShader: |
| 149 | return programState.getAttachedFragmentShader(); |
| 150 | default: |
| 151 | UNREACHABLE(); |
| 152 | return nullptr; |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | } // anonymous namespace |
| 157 | |
| 158 | ProgramVk::DefaultUniformBlock::DefaultUniformBlock() |
| 159 | : storage(), uniformData(), uniformsDirty(false), uniformLayout() |
| 160 | { |
| 161 | } |
| 162 | |
Jamie Madill | acf2f3a | 2017-11-21 19:22:44 -0500 | [diff] [blame] | 163 | ProgramVk::DefaultUniformBlock::~DefaultUniformBlock() |
| 164 | { |
| 165 | } |
| 166 | |
Jamie Madill | 76e471e | 2017-10-21 09:56:01 -0400 | [diff] [blame] | 167 | ProgramVk::ProgramVk(const gl::ProgramState &state) |
Jamie Madill | 8c3988c | 2017-12-21 14:44:56 -0500 | [diff] [blame] | 168 | : ProgramImpl(state), mDefaultUniformBlocks(), mUsedDescriptorSetRange(), mDirtyTextures(true) |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 169 | { |
Jamie Madill | 8c3988c | 2017-12-21 14:44:56 -0500 | [diff] [blame] | 170 | mUsedDescriptorSetRange.invalidate(); |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | ProgramVk::~ProgramVk() |
| 174 | { |
| 175 | } |
| 176 | |
Jamie Madill | b7d924a | 2018-03-10 11:16:54 -0500 | [diff] [blame] | 177 | gl::Error ProgramVk::destroy(const gl::Context *contextImpl) |
Jamie Madill | 5deea72 | 2017-02-16 10:44:46 -0500 | [diff] [blame] | 178 | { |
Jamie Madill | 67ae6c5 | 2018-03-09 11:49:01 -0500 | [diff] [blame] | 179 | ContextVk *contextVk = vk::GetImpl(contextImpl); |
Jamie Madill | b7d924a | 2018-03-10 11:16:54 -0500 | [diff] [blame] | 180 | return reset(contextVk); |
Jamie Madill | c514348 | 2017-10-15 20:20:06 -0400 | [diff] [blame] | 181 | } |
Jamie Madill | 5deea72 | 2017-02-16 10:44:46 -0500 | [diff] [blame] | 182 | |
Jamie Madill | b7d924a | 2018-03-10 11:16:54 -0500 | [diff] [blame] | 183 | vk::Error ProgramVk::reset(ContextVk *contextVk) |
Jamie Madill | c514348 | 2017-10-15 20:20:06 -0400 | [diff] [blame] | 184 | { |
Jamie Madill | 67ae6c5 | 2018-03-09 11:49:01 -0500 | [diff] [blame] | 185 | // TODO(jmadill): Handle re-linking a program that is in-use. http://anglebug.com/2397 |
| 186 | |
| 187 | VkDevice device = contextVk->getDevice(); |
| 188 | |
Jamie Madill | 76e471e | 2017-10-21 09:56:01 -0400 | [diff] [blame] | 189 | for (auto &uniformBlock : mDefaultUniformBlocks) |
| 190 | { |
| 191 | uniformBlock.storage.memory.destroy(device); |
| 192 | uniformBlock.storage.buffer.destroy(device); |
| 193 | } |
| 194 | |
| 195 | mEmptyUniformBlockStorage.memory.destroy(device); |
| 196 | mEmptyUniformBlockStorage.buffer.destroy(device); |
| 197 | |
Jamie Madill | 5deea72 | 2017-02-16 10:44:46 -0500 | [diff] [blame] | 198 | mLinkedFragmentModule.destroy(device); |
| 199 | mLinkedVertexModule.destroy(device); |
Jamie Madill | f2f6d37 | 2018-01-10 21:37:23 -0500 | [diff] [blame] | 200 | mVertexModuleSerial = Serial(); |
| 201 | mFragmentModuleSerial = Serial(); |
Jamie Madill | 76e471e | 2017-10-21 09:56:01 -0400 | [diff] [blame] | 202 | |
Jamie Madill | 67ae6c5 | 2018-03-09 11:49:01 -0500 | [diff] [blame] | 203 | // Free our descriptor set handles. |
| 204 | if (!mDescriptorSets.empty()) |
| 205 | { |
| 206 | vk::DescriptorPool *descriptorPool = contextVk->getDescriptorPool(); |
Jamie Madill | b7d924a | 2018-03-10 11:16:54 -0500 | [diff] [blame] | 207 | ANGLE_TRY(descriptorPool->freeDescriptorSets( |
| 208 | device, static_cast<uint32_t>(mDescriptorSets.size()), mDescriptorSets.data())); |
Jamie Madill | 67ae6c5 | 2018-03-09 11:49:01 -0500 | [diff] [blame] | 209 | } |
Jamie Madill | 5547b38 | 2017-10-23 18:16:01 -0400 | [diff] [blame] | 210 | mDescriptorSets.clear(); |
Jamie Madill | 8c3988c | 2017-12-21 14:44:56 -0500 | [diff] [blame] | 211 | mUsedDescriptorSetRange.invalidate(); |
Jamie Madill | 5547b38 | 2017-10-23 18:16:01 -0400 | [diff] [blame] | 212 | mDirtyTextures = false; |
Jamie Madill | b7d924a | 2018-03-10 11:16:54 -0500 | [diff] [blame] | 213 | |
| 214 | return vk::NoError(); |
Jamie Madill | 5deea72 | 2017-02-16 10:44:46 -0500 | [diff] [blame] | 215 | } |
| 216 | |
Jamie Madill | 9cf9e87 | 2017-06-05 12:59:25 -0400 | [diff] [blame] | 217 | gl::LinkResult ProgramVk::load(const gl::Context *contextImpl, |
| 218 | gl::InfoLog &infoLog, |
| 219 | gl::BinaryInputStream *stream) |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 220 | { |
| 221 | UNIMPLEMENTED(); |
Yuly Novikov | c4d18aa | 2017-03-09 18:45:02 -0500 | [diff] [blame] | 222 | return gl::InternalError(); |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 223 | } |
| 224 | |
Jamie Madill | 27a6063 | 2017-06-30 15:12:01 -0400 | [diff] [blame] | 225 | void ProgramVk::save(const gl::Context *context, gl::BinaryOutputStream *stream) |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 226 | { |
| 227 | UNIMPLEMENTED(); |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 228 | } |
| 229 | |
| 230 | void ProgramVk::setBinaryRetrievableHint(bool retrievable) |
| 231 | { |
| 232 | UNIMPLEMENTED(); |
| 233 | } |
| 234 | |
Yunchao He | 61afff1 | 2017-03-14 15:34:03 +0800 | [diff] [blame] | 235 | void ProgramVk::setSeparable(bool separable) |
| 236 | { |
| 237 | UNIMPLEMENTED(); |
| 238 | } |
| 239 | |
Jamie Madill | 9cf9e87 | 2017-06-05 12:59:25 -0400 | [diff] [blame] | 240 | gl::LinkResult ProgramVk::link(const gl::Context *glContext, |
Jamie Madill | c9727f3 | 2017-11-07 12:37:07 -0500 | [diff] [blame] | 241 | const gl::ProgramLinkedResources &resources, |
Jamie Madill | 9cf9e87 | 2017-06-05 12:59:25 -0400 | [diff] [blame] | 242 | gl::InfoLog &infoLog) |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 243 | { |
Jamie Madill | e1f3ad4 | 2017-10-28 23:00:42 -0400 | [diff] [blame] | 244 | ContextVk *contextVk = vk::GetImpl(glContext); |
Jamie Madill | c514348 | 2017-10-15 20:20:06 -0400 | [diff] [blame] | 245 | RendererVk *renderer = contextVk->getRenderer(); |
Jamie Madill | 8ecf7f9 | 2017-01-13 17:29:52 -0500 | [diff] [blame] | 246 | GlslangWrapper *glslangWrapper = renderer->getGlslangWrapper(); |
Jamie Madill | c514348 | 2017-10-15 20:20:06 -0400 | [diff] [blame] | 247 | VkDevice device = renderer->getDevice(); |
| 248 | |
Jamie Madill | b7d924a | 2018-03-10 11:16:54 -0500 | [diff] [blame] | 249 | ANGLE_TRY(reset(contextVk)); |
Jamie Madill | 8ecf7f9 | 2017-01-13 17:29:52 -0500 | [diff] [blame] | 250 | |
Jamie Madill | 8ecf7f9 | 2017-01-13 17:29:52 -0500 | [diff] [blame] | 251 | std::vector<uint32_t> vertexCode; |
| 252 | std::vector<uint32_t> fragmentCode; |
| 253 | bool linkSuccess = false; |
Jamie Madill | 4dd167f | 2017-11-09 13:08:31 -0500 | [diff] [blame] | 254 | ANGLE_TRY_RESULT( |
| 255 | glslangWrapper->linkProgram(glContext, mState, resources, &vertexCode, &fragmentCode), |
| 256 | linkSuccess); |
Jamie Madill | 8ecf7f9 | 2017-01-13 17:29:52 -0500 | [diff] [blame] | 257 | if (!linkSuccess) |
| 258 | { |
| 259 | return false; |
| 260 | } |
| 261 | |
Jamie Madill | 8ecf7f9 | 2017-01-13 17:29:52 -0500 | [diff] [blame] | 262 | { |
| 263 | VkShaderModuleCreateInfo vertexShaderInfo; |
| 264 | vertexShaderInfo.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO; |
| 265 | vertexShaderInfo.pNext = nullptr; |
| 266 | vertexShaderInfo.flags = 0; |
| 267 | vertexShaderInfo.codeSize = vertexCode.size() * sizeof(uint32_t); |
| 268 | vertexShaderInfo.pCode = vertexCode.data(); |
Jamie Madill | c514348 | 2017-10-15 20:20:06 -0400 | [diff] [blame] | 269 | |
| 270 | ANGLE_TRY(mLinkedVertexModule.init(device, vertexShaderInfo)); |
Jamie Madill | f2f6d37 | 2018-01-10 21:37:23 -0500 | [diff] [blame] | 271 | mVertexModuleSerial = renderer->issueProgramSerial(); |
Jamie Madill | 8ecf7f9 | 2017-01-13 17:29:52 -0500 | [diff] [blame] | 272 | } |
| 273 | |
| 274 | { |
| 275 | VkShaderModuleCreateInfo fragmentShaderInfo; |
| 276 | fragmentShaderInfo.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO; |
| 277 | fragmentShaderInfo.pNext = nullptr; |
| 278 | fragmentShaderInfo.flags = 0; |
| 279 | fragmentShaderInfo.codeSize = fragmentCode.size() * sizeof(uint32_t); |
| 280 | fragmentShaderInfo.pCode = fragmentCode.data(); |
| 281 | |
Jamie Madill | c514348 | 2017-10-15 20:20:06 -0400 | [diff] [blame] | 282 | ANGLE_TRY(mLinkedFragmentModule.init(device, fragmentShaderInfo)); |
Jamie Madill | f2f6d37 | 2018-01-10 21:37:23 -0500 | [diff] [blame] | 283 | mFragmentModuleSerial = renderer->issueProgramSerial(); |
Jamie Madill | 8ecf7f9 | 2017-01-13 17:29:52 -0500 | [diff] [blame] | 284 | } |
| 285 | |
Jamie Madill | 76e471e | 2017-10-21 09:56:01 -0400 | [diff] [blame] | 286 | ANGLE_TRY(initDescriptorSets(contextVk)); |
| 287 | ANGLE_TRY(initDefaultUniformBlocks(glContext)); |
Jamie Madill | 8ecf7f9 | 2017-01-13 17:29:52 -0500 | [diff] [blame] | 288 | |
Jamie Madill | 8c3988c | 2017-12-21 14:44:56 -0500 | [diff] [blame] | 289 | if (!mState.getSamplerUniformRange().empty()) |
| 290 | { |
| 291 | // Ensure the descriptor set range includes the textures at position 1. |
| 292 | mUsedDescriptorSetRange.extend(1); |
| 293 | mDirtyTextures = true; |
| 294 | } |
| 295 | |
Jamie Madill | 8ecf7f9 | 2017-01-13 17:29:52 -0500 | [diff] [blame] | 296 | return true; |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 297 | } |
| 298 | |
Jamie Madill | 76e471e | 2017-10-21 09:56:01 -0400 | [diff] [blame] | 299 | gl::Error ProgramVk::initDefaultUniformBlocks(const gl::Context *glContext) |
| 300 | { |
Jamie Madill | e1f3ad4 | 2017-10-28 23:00:42 -0400 | [diff] [blame] | 301 | ContextVk *contextVk = vk::GetImpl(glContext); |
Jamie Madill | 57fbfd8 | 2018-02-14 12:45:34 -0500 | [diff] [blame] | 302 | RendererVk *renderer = contextVk->getRenderer(); |
Jamie Madill | 76e471e | 2017-10-21 09:56:01 -0400 | [diff] [blame] | 303 | VkDevice device = contextVk->getDevice(); |
| 304 | |
| 305 | // Process vertex and fragment uniforms into std140 packing. |
| 306 | std::array<sh::BlockLayoutMap, 2> layoutMap; |
| 307 | std::array<size_t, 2> requiredBufferSize = {{0, 0}}; |
| 308 | |
| 309 | for (uint32_t shaderIndex = MinShaderIndex; shaderIndex < MaxShaderIndex; ++shaderIndex) |
| 310 | { |
| 311 | ANGLE_TRY(InitDefaultUniformBlock(glContext, device, GetShader(mState, shaderIndex), |
| 312 | &mDefaultUniformBlocks[shaderIndex].storage, |
| 313 | &layoutMap[shaderIndex], |
| 314 | &requiredBufferSize[shaderIndex])); |
| 315 | } |
| 316 | |
| 317 | // Init the default block layout info. |
| 318 | const auto &locations = mState.getUniformLocations(); |
| 319 | const auto &uniforms = mState.getUniforms(); |
| 320 | for (size_t locationIndex = 0; locationIndex < locations.size(); ++locationIndex) |
| 321 | { |
| 322 | std::array<sh::BlockMemberInfo, 2> layoutInfo; |
| 323 | |
| 324 | const auto &location = locations[locationIndex]; |
| 325 | if (location.used() && !location.ignored) |
| 326 | { |
Jamie Madill | de03e00 | 2017-10-21 14:04:20 -0400 | [diff] [blame] | 327 | const auto &uniform = uniforms[location.index]; |
| 328 | |
| 329 | if (uniform.isSampler()) |
| 330 | continue; |
| 331 | |
Jamie Madill | 76e471e | 2017-10-21 09:56:01 -0400 | [diff] [blame] | 332 | std::string uniformName = uniform.name; |
| 333 | if (uniform.isArray()) |
| 334 | { |
Olli Etuaho | 1734e17 | 2017-10-27 15:30:27 +0300 | [diff] [blame] | 335 | uniformName += ArrayString(location.arrayIndex); |
Jamie Madill | 76e471e | 2017-10-21 09:56:01 -0400 | [diff] [blame] | 336 | } |
| 337 | |
| 338 | bool found = false; |
| 339 | |
| 340 | for (uint32_t shaderIndex = MinShaderIndex; shaderIndex < MaxShaderIndex; ++shaderIndex) |
| 341 | { |
| 342 | auto it = layoutMap[shaderIndex].find(uniformName); |
| 343 | if (it != layoutMap[shaderIndex].end()) |
| 344 | { |
| 345 | found = true; |
| 346 | layoutInfo[shaderIndex] = it->second; |
| 347 | } |
| 348 | } |
| 349 | |
| 350 | ASSERT(found); |
| 351 | } |
| 352 | |
| 353 | for (uint32_t shaderIndex = MinShaderIndex; shaderIndex < MaxShaderIndex; ++shaderIndex) |
| 354 | { |
| 355 | mDefaultUniformBlocks[shaderIndex].uniformLayout.push_back(layoutInfo[shaderIndex]); |
| 356 | } |
| 357 | } |
| 358 | |
| 359 | bool anyDirty = false; |
| 360 | bool allDirty = true; |
| 361 | |
| 362 | for (uint32_t shaderIndex = MinShaderIndex; shaderIndex < MaxShaderIndex; ++shaderIndex) |
| 363 | { |
| 364 | if (requiredBufferSize[shaderIndex] > 0) |
| 365 | { |
| 366 | if (!mDefaultUniformBlocks[shaderIndex].uniformData.resize( |
| 367 | requiredBufferSize[shaderIndex])) |
| 368 | { |
| 369 | return gl::OutOfMemory() << "Memory allocation failure."; |
| 370 | } |
| 371 | mDefaultUniformBlocks[shaderIndex].uniformData.fill(0); |
| 372 | mDefaultUniformBlocks[shaderIndex].uniformsDirty = true; |
| 373 | |
| 374 | anyDirty = true; |
| 375 | } |
| 376 | else |
| 377 | { |
| 378 | allDirty = false; |
| 379 | } |
| 380 | } |
| 381 | |
| 382 | if (anyDirty) |
| 383 | { |
| 384 | // Initialize the "empty" uniform block if necessary. |
| 385 | if (!allDirty) |
| 386 | { |
| 387 | VkBufferCreateInfo uniformBufferInfo; |
| 388 | uniformBufferInfo.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO; |
| 389 | uniformBufferInfo.pNext = nullptr; |
| 390 | uniformBufferInfo.flags = 0; |
| 391 | uniformBufferInfo.size = 1; |
| 392 | uniformBufferInfo.usage = VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT; |
| 393 | uniformBufferInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE; |
| 394 | uniformBufferInfo.queueFamilyIndexCount = 0; |
| 395 | uniformBufferInfo.pQueueFamilyIndices = nullptr; |
| 396 | |
| 397 | ANGLE_TRY(mEmptyUniformBlockStorage.buffer.init(device, uniformBufferInfo)); |
| 398 | |
Jamie Madill | 57dd97a | 2018-02-06 17:10:49 -0500 | [diff] [blame] | 399 | // Assume host vislble/coherent memory available. |
| 400 | VkMemoryPropertyFlags flags = |
| 401 | (VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT); |
Jamie Madill | 76e471e | 2017-10-21 09:56:01 -0400 | [diff] [blame] | 402 | size_t requiredSize = 0; |
Jamie Madill | 57fbfd8 | 2018-02-14 12:45:34 -0500 | [diff] [blame] | 403 | ANGLE_TRY(AllocateBufferMemory(renderer, flags, &mEmptyUniformBlockStorage.buffer, |
Jamie Madill | 76e471e | 2017-10-21 09:56:01 -0400 | [diff] [blame] | 404 | &mEmptyUniformBlockStorage.memory, &requiredSize)); |
| 405 | } |
| 406 | |
| 407 | ANGLE_TRY(updateDefaultUniformsDescriptorSet(contextVk)); |
Jamie Madill | 8c3988c | 2017-12-21 14:44:56 -0500 | [diff] [blame] | 408 | |
| 409 | // Ensure the descriptor set range includes the uniform buffers at position 0. |
| 410 | mUsedDescriptorSetRange.extend(0); |
Jamie Madill | 5547b38 | 2017-10-23 18:16:01 -0400 | [diff] [blame] | 411 | } |
Jamie Madill | 76e471e | 2017-10-21 09:56:01 -0400 | [diff] [blame] | 412 | |
| 413 | return gl::NoError(); |
| 414 | } |
| 415 | |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 416 | GLboolean ProgramVk::validate(const gl::Caps &caps, gl::InfoLog *infoLog) |
| 417 | { |
| 418 | UNIMPLEMENTED(); |
| 419 | return GLboolean(); |
| 420 | } |
| 421 | |
Jamie Madill | 76e471e | 2017-10-21 09:56:01 -0400 | [diff] [blame] | 422 | template <typename T> |
| 423 | void ProgramVk::setUniformImpl(GLint location, GLsizei count, const T *v, GLenum entryPointType) |
| 424 | { |
| 425 | const gl::VariableLocation &locationInfo = mState.getUniformLocations()[location]; |
| 426 | const gl::LinkedUniform &linkedUniform = mState.getUniforms()[locationInfo.index]; |
| 427 | |
Luc Ferron | 7cec335 | 2018-03-13 13:29:34 -0400 | [diff] [blame^] | 428 | if (linkedUniform.isSampler()) |
| 429 | { |
| 430 | UNIMPLEMENTED(); |
| 431 | return; |
| 432 | } |
| 433 | |
Jamie Madill | 76e471e | 2017-10-21 09:56:01 -0400 | [diff] [blame] | 434 | if (linkedUniform.type == entryPointType) |
| 435 | { |
| 436 | for (auto &uniformBlock : mDefaultUniformBlocks) |
| 437 | { |
| 438 | const sh::BlockMemberInfo &layoutInfo = uniformBlock.uniformLayout[location]; |
| 439 | UpdateDefaultUniformBlock(count, linkedUniform.typeInfo->componentCount, v, layoutInfo, |
| 440 | &uniformBlock.uniformData); |
| 441 | } |
| 442 | } |
| 443 | else |
| 444 | { |
| 445 | ASSERT(linkedUniform.type == gl::VariableBoolVectorType(entryPointType)); |
| 446 | UNIMPLEMENTED(); |
| 447 | } |
| 448 | } |
| 449 | |
Luc Ferron | 7cec335 | 2018-03-13 13:29:34 -0400 | [diff] [blame^] | 450 | template <typename T> |
| 451 | void ProgramVk::getUniformImpl(GLint location, T *v, GLenum entryPointType) const |
| 452 | { |
| 453 | const gl::VariableLocation &locationInfo = mState.getUniformLocations()[location]; |
| 454 | const gl::LinkedUniform &linkedUniform = mState.getUniforms()[locationInfo.index]; |
| 455 | |
| 456 | if (linkedUniform.isSampler()) |
| 457 | { |
| 458 | UNIMPLEMENTED(); |
| 459 | return; |
| 460 | } |
| 461 | |
| 462 | ASSERT(linkedUniform.typeInfo->componentType == entryPointType); |
| 463 | const gl::ShaderType shaderType = linkedUniform.getFirstStaticUseShaderType(); |
| 464 | ASSERT(shaderType != gl::ShaderType::SHADER_TYPE_INVALID); |
| 465 | |
| 466 | const DefaultUniformBlock &uniformBlock = mDefaultUniformBlocks[shaderType]; |
| 467 | const sh::BlockMemberInfo &layoutInfo = uniformBlock.uniformLayout[location]; |
| 468 | ReadFromDefaultUniformBlock(linkedUniform.typeInfo->componentCount, v, layoutInfo, |
| 469 | &uniformBlock.uniformData); |
| 470 | } |
| 471 | |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 472 | void ProgramVk::setUniform1fv(GLint location, GLsizei count, const GLfloat *v) |
| 473 | { |
Jamie Madill | 76e471e | 2017-10-21 09:56:01 -0400 | [diff] [blame] | 474 | setUniformImpl(location, count, v, GL_FLOAT); |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 475 | } |
| 476 | |
| 477 | void ProgramVk::setUniform2fv(GLint location, GLsizei count, const GLfloat *v) |
| 478 | { |
Jamie Madill | 76e471e | 2017-10-21 09:56:01 -0400 | [diff] [blame] | 479 | setUniformImpl(location, count, v, GL_FLOAT_VEC2); |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 480 | } |
| 481 | |
| 482 | void ProgramVk::setUniform3fv(GLint location, GLsizei count, const GLfloat *v) |
| 483 | { |
Jamie Madill | 76e471e | 2017-10-21 09:56:01 -0400 | [diff] [blame] | 484 | setUniformImpl(location, count, v, GL_FLOAT_VEC3); |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 485 | } |
| 486 | |
| 487 | void ProgramVk::setUniform4fv(GLint location, GLsizei count, const GLfloat *v) |
| 488 | { |
Jamie Madill | 76e471e | 2017-10-21 09:56:01 -0400 | [diff] [blame] | 489 | setUniformImpl(location, count, v, GL_FLOAT_VEC4); |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 490 | } |
| 491 | |
| 492 | void ProgramVk::setUniform1iv(GLint location, GLsizei count, const GLint *v) |
| 493 | { |
Luc Ferron | 7cec335 | 2018-03-13 13:29:34 -0400 | [diff] [blame^] | 494 | setUniformImpl(location, count, v, GL_INT); |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 495 | } |
| 496 | |
| 497 | void ProgramVk::setUniform2iv(GLint location, GLsizei count, const GLint *v) |
| 498 | { |
| 499 | UNIMPLEMENTED(); |
| 500 | } |
| 501 | |
| 502 | void ProgramVk::setUniform3iv(GLint location, GLsizei count, const GLint *v) |
| 503 | { |
| 504 | UNIMPLEMENTED(); |
| 505 | } |
| 506 | |
| 507 | void ProgramVk::setUniform4iv(GLint location, GLsizei count, const GLint *v) |
| 508 | { |
| 509 | UNIMPLEMENTED(); |
| 510 | } |
| 511 | |
| 512 | void ProgramVk::setUniform1uiv(GLint location, GLsizei count, const GLuint *v) |
| 513 | { |
| 514 | UNIMPLEMENTED(); |
| 515 | } |
| 516 | |
| 517 | void ProgramVk::setUniform2uiv(GLint location, GLsizei count, const GLuint *v) |
| 518 | { |
| 519 | UNIMPLEMENTED(); |
| 520 | } |
| 521 | |
| 522 | void ProgramVk::setUniform3uiv(GLint location, GLsizei count, const GLuint *v) |
| 523 | { |
| 524 | UNIMPLEMENTED(); |
| 525 | } |
| 526 | |
| 527 | void ProgramVk::setUniform4uiv(GLint location, GLsizei count, const GLuint *v) |
| 528 | { |
| 529 | UNIMPLEMENTED(); |
| 530 | } |
| 531 | |
| 532 | void ProgramVk::setUniformMatrix2fv(GLint location, |
| 533 | GLsizei count, |
| 534 | GLboolean transpose, |
| 535 | const GLfloat *value) |
| 536 | { |
| 537 | UNIMPLEMENTED(); |
| 538 | } |
| 539 | |
| 540 | void ProgramVk::setUniformMatrix3fv(GLint location, |
| 541 | GLsizei count, |
| 542 | GLboolean transpose, |
| 543 | const GLfloat *value) |
| 544 | { |
| 545 | UNIMPLEMENTED(); |
| 546 | } |
| 547 | |
| 548 | void ProgramVk::setUniformMatrix4fv(GLint location, |
| 549 | GLsizei count, |
| 550 | GLboolean transpose, |
| 551 | const GLfloat *value) |
| 552 | { |
| 553 | UNIMPLEMENTED(); |
| 554 | } |
| 555 | |
| 556 | void ProgramVk::setUniformMatrix2x3fv(GLint location, |
| 557 | GLsizei count, |
| 558 | GLboolean transpose, |
| 559 | const GLfloat *value) |
| 560 | { |
| 561 | UNIMPLEMENTED(); |
| 562 | } |
| 563 | |
| 564 | void ProgramVk::setUniformMatrix3x2fv(GLint location, |
| 565 | GLsizei count, |
| 566 | GLboolean transpose, |
| 567 | const GLfloat *value) |
| 568 | { |
| 569 | UNIMPLEMENTED(); |
| 570 | } |
| 571 | |
| 572 | void ProgramVk::setUniformMatrix2x4fv(GLint location, |
| 573 | GLsizei count, |
| 574 | GLboolean transpose, |
| 575 | const GLfloat *value) |
| 576 | { |
| 577 | UNIMPLEMENTED(); |
| 578 | } |
| 579 | |
| 580 | void ProgramVk::setUniformMatrix4x2fv(GLint location, |
| 581 | GLsizei count, |
| 582 | GLboolean transpose, |
| 583 | const GLfloat *value) |
| 584 | { |
| 585 | UNIMPLEMENTED(); |
| 586 | } |
| 587 | |
| 588 | void ProgramVk::setUniformMatrix3x4fv(GLint location, |
| 589 | GLsizei count, |
| 590 | GLboolean transpose, |
| 591 | const GLfloat *value) |
| 592 | { |
| 593 | UNIMPLEMENTED(); |
| 594 | } |
| 595 | |
| 596 | void ProgramVk::setUniformMatrix4x3fv(GLint location, |
| 597 | GLsizei count, |
| 598 | GLboolean transpose, |
| 599 | const GLfloat *value) |
| 600 | { |
| 601 | UNIMPLEMENTED(); |
| 602 | } |
| 603 | |
| 604 | void ProgramVk::setUniformBlockBinding(GLuint uniformBlockIndex, GLuint uniformBlockBinding) |
| 605 | { |
| 606 | UNIMPLEMENTED(); |
| 607 | } |
| 608 | |
Sami Väisänen | 46eaa94 | 2016-06-29 10:26:37 +0300 | [diff] [blame] | 609 | void ProgramVk::setPathFragmentInputGen(const std::string &inputName, |
| 610 | GLenum genMode, |
| 611 | GLint components, |
| 612 | const GLfloat *coeffs) |
| 613 | { |
| 614 | UNIMPLEMENTED(); |
| 615 | } |
| 616 | |
Jamie Madill | 8ecf7f9 | 2017-01-13 17:29:52 -0500 | [diff] [blame] | 617 | const vk::ShaderModule &ProgramVk::getLinkedVertexModule() const |
| 618 | { |
| 619 | ASSERT(mLinkedVertexModule.getHandle() != VK_NULL_HANDLE); |
| 620 | return mLinkedVertexModule; |
| 621 | } |
| 622 | |
Jamie Madill | f2f6d37 | 2018-01-10 21:37:23 -0500 | [diff] [blame] | 623 | Serial ProgramVk::getVertexModuleSerial() const |
| 624 | { |
| 625 | return mVertexModuleSerial; |
| 626 | } |
| 627 | |
Jamie Madill | 8ecf7f9 | 2017-01-13 17:29:52 -0500 | [diff] [blame] | 628 | const vk::ShaderModule &ProgramVk::getLinkedFragmentModule() const |
| 629 | { |
| 630 | ASSERT(mLinkedFragmentModule.getHandle() != VK_NULL_HANDLE); |
| 631 | return mLinkedFragmentModule; |
| 632 | } |
| 633 | |
Jamie Madill | f2f6d37 | 2018-01-10 21:37:23 -0500 | [diff] [blame] | 634 | Serial ProgramVk::getFragmentModuleSerial() const |
| 635 | { |
| 636 | return mFragmentModuleSerial; |
| 637 | } |
| 638 | |
Jamie Madill | 76e471e | 2017-10-21 09:56:01 -0400 | [diff] [blame] | 639 | vk::Error ProgramVk::initDescriptorSets(ContextVk *contextVk) |
| 640 | { |
Jamie Madill | 5547b38 | 2017-10-23 18:16:01 -0400 | [diff] [blame] | 641 | ASSERT(mDescriptorSets.empty()); |
Jamie Madill | 76e471e | 2017-10-21 09:56:01 -0400 | [diff] [blame] | 642 | |
Jamie Madill | 8c3988c | 2017-12-21 14:44:56 -0500 | [diff] [blame] | 643 | RendererVk *renderer = contextVk->getRenderer(); |
Jamie Madill | 76e471e | 2017-10-21 09:56:01 -0400 | [diff] [blame] | 644 | VkDevice device = contextVk->getDevice(); |
| 645 | |
| 646 | // Write out to a new a descriptor set. |
| 647 | // TODO(jmadill): Handle descriptor set lifetime. |
| 648 | vk::DescriptorPool *descriptorPool = contextVk->getDescriptorPool(); |
| 649 | |
Jamie Madill | 8c3988c | 2017-12-21 14:44:56 -0500 | [diff] [blame] | 650 | const auto &descriptorSetLayouts = renderer->getGraphicsDescriptorSetLayouts(); |
| 651 | |
| 652 | uint32_t descriptorSetCount = static_cast<uint32_t>(descriptorSetLayouts.size()); |
Jamie Madill | 76e471e | 2017-10-21 09:56:01 -0400 | [diff] [blame] | 653 | |
Jamie Madill | 5547b38 | 2017-10-23 18:16:01 -0400 | [diff] [blame] | 654 | VkDescriptorSetAllocateInfo allocInfo; |
| 655 | allocInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO; |
| 656 | allocInfo.pNext = nullptr; |
| 657 | allocInfo.descriptorPool = descriptorPool->getHandle(); |
| 658 | allocInfo.descriptorSetCount = descriptorSetCount; |
Jamie Madill | 8c3988c | 2017-12-21 14:44:56 -0500 | [diff] [blame] | 659 | allocInfo.pSetLayouts = descriptorSetLayouts[0].ptr(); |
Jamie Madill | 76e471e | 2017-10-21 09:56:01 -0400 | [diff] [blame] | 660 | |
Jamie Madill | 5547b38 | 2017-10-23 18:16:01 -0400 | [diff] [blame] | 661 | mDescriptorSets.resize(descriptorSetCount, VK_NULL_HANDLE); |
| 662 | ANGLE_TRY(descriptorPool->allocateDescriptorSets(device, allocInfo, &mDescriptorSets[0])); |
Jamie Madill | 76e471e | 2017-10-21 09:56:01 -0400 | [diff] [blame] | 663 | return vk::NoError(); |
| 664 | } |
| 665 | |
Jamie Madill | 54164b0 | 2017-08-28 15:17:37 -0400 | [diff] [blame] | 666 | void ProgramVk::getUniformfv(const gl::Context *context, GLint location, GLfloat *params) const |
| 667 | { |
Luc Ferron | 7cec335 | 2018-03-13 13:29:34 -0400 | [diff] [blame^] | 668 | getUniformImpl(location, params, GL_FLOAT); |
Jamie Madill | 54164b0 | 2017-08-28 15:17:37 -0400 | [diff] [blame] | 669 | } |
| 670 | |
| 671 | void ProgramVk::getUniformiv(const gl::Context *context, GLint location, GLint *params) const |
| 672 | { |
Luc Ferron | 7cec335 | 2018-03-13 13:29:34 -0400 | [diff] [blame^] | 673 | getUniformImpl(location, params, GL_INT); |
Jamie Madill | 54164b0 | 2017-08-28 15:17:37 -0400 | [diff] [blame] | 674 | } |
| 675 | |
| 676 | void ProgramVk::getUniformuiv(const gl::Context *context, GLint location, GLuint *params) const |
| 677 | { |
| 678 | UNIMPLEMENTED(); |
| 679 | } |
| 680 | |
Jamie Madill | 76e471e | 2017-10-21 09:56:01 -0400 | [diff] [blame] | 681 | vk::Error ProgramVk::updateUniforms(ContextVk *contextVk) |
| 682 | { |
| 683 | if (!mDefaultUniformBlocks[VertexShader].uniformsDirty && |
| 684 | !mDefaultUniformBlocks[FragmentShader].uniformsDirty) |
| 685 | { |
| 686 | return vk::NoError(); |
| 687 | } |
| 688 | |
Jamie Madill | 8c3988c | 2017-12-21 14:44:56 -0500 | [diff] [blame] | 689 | ASSERT(mUsedDescriptorSetRange.contains(0)); |
Jamie Madill | 5547b38 | 2017-10-23 18:16:01 -0400 | [diff] [blame] | 690 | |
Jamie Madill | 76e471e | 2017-10-21 09:56:01 -0400 | [diff] [blame] | 691 | VkDevice device = contextVk->getDevice(); |
| 692 | |
| 693 | // Update buffer memory by immediate mapping. This immediate update only works once. |
| 694 | // TODO(jmadill): Handle inserting updates into the command stream, or use dynamic buffers. |
| 695 | for (auto &uniformBlock : mDefaultUniformBlocks) |
| 696 | { |
| 697 | if (uniformBlock.uniformsDirty) |
| 698 | { |
| 699 | ANGLE_TRY(SyncDefaultUniformBlock(device, &uniformBlock.storage.memory, |
| 700 | uniformBlock.uniformData)); |
| 701 | uniformBlock.uniformsDirty = false; |
| 702 | } |
| 703 | } |
| 704 | |
| 705 | return vk::NoError(); |
| 706 | } |
| 707 | |
| 708 | vk::Error ProgramVk::updateDefaultUniformsDescriptorSet(ContextVk *contextVk) |
| 709 | { |
| 710 | std::array<VkDescriptorBufferInfo, 2> descriptorBufferInfo; |
| 711 | std::array<VkWriteDescriptorSet, 2> writeDescriptorInfo; |
| 712 | uint32_t bufferCount = 0; |
| 713 | |
| 714 | for (auto &uniformBlock : mDefaultUniformBlocks) |
| 715 | { |
| 716 | auto &bufferInfo = descriptorBufferInfo[bufferCount]; |
| 717 | |
| 718 | if (!uniformBlock.uniformData.empty()) |
| 719 | { |
| 720 | bufferInfo.buffer = uniformBlock.storage.buffer.getHandle(); |
| 721 | } |
| 722 | else |
| 723 | { |
| 724 | bufferInfo.buffer = mEmptyUniformBlockStorage.buffer.getHandle(); |
| 725 | } |
| 726 | |
| 727 | bufferInfo.offset = 0; |
| 728 | bufferInfo.range = VK_WHOLE_SIZE; |
| 729 | |
| 730 | auto &writeInfo = writeDescriptorInfo[bufferCount]; |
| 731 | |
| 732 | writeInfo.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET; |
| 733 | writeInfo.pNext = nullptr; |
Jamie Madill | 5547b38 | 2017-10-23 18:16:01 -0400 | [diff] [blame] | 734 | writeInfo.dstSet = mDescriptorSets[0]; |
Jamie Madill | 76e471e | 2017-10-21 09:56:01 -0400 | [diff] [blame] | 735 | writeInfo.dstBinding = bufferCount; |
| 736 | writeInfo.dstArrayElement = 0; |
| 737 | writeInfo.descriptorCount = 1; |
| 738 | writeInfo.descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER; |
| 739 | writeInfo.pImageInfo = nullptr; |
| 740 | writeInfo.pBufferInfo = &bufferInfo; |
| 741 | writeInfo.pTexelBufferView = nullptr; |
| 742 | |
| 743 | bufferCount++; |
| 744 | } |
| 745 | |
| 746 | VkDevice device = contextVk->getDevice(); |
| 747 | |
| 748 | vkUpdateDescriptorSets(device, bufferCount, writeDescriptorInfo.data(), 0, nullptr); |
| 749 | |
| 750 | return vk::NoError(); |
| 751 | } |
| 752 | |
Jamie Madill | 5547b38 | 2017-10-23 18:16:01 -0400 | [diff] [blame] | 753 | const std::vector<VkDescriptorSet> &ProgramVk::getDescriptorSets() const |
Jamie Madill | 76e471e | 2017-10-21 09:56:01 -0400 | [diff] [blame] | 754 | { |
Jamie Madill | 5547b38 | 2017-10-23 18:16:01 -0400 | [diff] [blame] | 755 | return mDescriptorSets; |
| 756 | } |
| 757 | |
Jamie Madill | 8c3988c | 2017-12-21 14:44:56 -0500 | [diff] [blame] | 758 | const gl::RangeUI &ProgramVk::getUsedDescriptorSetRange() const |
Jamie Madill | 5547b38 | 2017-10-23 18:16:01 -0400 | [diff] [blame] | 759 | { |
Jamie Madill | 8c3988c | 2017-12-21 14:44:56 -0500 | [diff] [blame] | 760 | return mUsedDescriptorSetRange; |
Jamie Madill | 5547b38 | 2017-10-23 18:16:01 -0400 | [diff] [blame] | 761 | } |
| 762 | |
| 763 | void ProgramVk::updateTexturesDescriptorSet(ContextVk *contextVk) |
| 764 | { |
| 765 | if (mState.getSamplerBindings().empty() || !mDirtyTextures) |
| 766 | { |
| 767 | return; |
| 768 | } |
| 769 | |
Jamie Madill | 8c3988c | 2017-12-21 14:44:56 -0500 | [diff] [blame] | 770 | ASSERT(mUsedDescriptorSetRange.contains(1)); |
| 771 | VkDescriptorSet descriptorSet = mDescriptorSets[1]; |
Jamie Madill | 5547b38 | 2017-10-23 18:16:01 -0400 | [diff] [blame] | 772 | |
| 773 | // TODO(jmadill): Don't hard-code the texture limit. |
| 774 | ShaderTextureArray<VkDescriptorImageInfo> descriptorImageInfo; |
| 775 | ShaderTextureArray<VkWriteDescriptorSet> writeDescriptorInfo; |
| 776 | uint32_t imageCount = 0; |
| 777 | |
| 778 | const gl::State &glState = contextVk->getGLState(); |
| 779 | const auto &completeTextures = glState.getCompleteTextureCache(); |
| 780 | |
| 781 | for (const auto &samplerBinding : mState.getSamplerBindings()) |
| 782 | { |
| 783 | ASSERT(!samplerBinding.unreferenced); |
| 784 | |
| 785 | // TODO(jmadill): Sampler arrays |
| 786 | ASSERT(samplerBinding.boundTextureUnits.size() == 1); |
| 787 | |
| 788 | GLuint textureUnit = samplerBinding.boundTextureUnits[0]; |
| 789 | const gl::Texture *texture = completeTextures[textureUnit]; |
| 790 | |
| 791 | // TODO(jmadill): Incomplete textures handling. |
| 792 | ASSERT(texture); |
| 793 | |
Jamie Madill | e1f3ad4 | 2017-10-28 23:00:42 -0400 | [diff] [blame] | 794 | TextureVk *textureVk = vk::GetImpl(texture); |
Jamie Madill | 5547b38 | 2017-10-23 18:16:01 -0400 | [diff] [blame] | 795 | const vk::Image &image = textureVk->getImage(); |
| 796 | |
| 797 | VkDescriptorImageInfo &imageInfo = descriptorImageInfo[imageCount]; |
| 798 | |
| 799 | imageInfo.sampler = textureVk->getSampler().getHandle(); |
| 800 | imageInfo.imageView = textureVk->getImageView().getHandle(); |
| 801 | imageInfo.imageLayout = image.getCurrentLayout(); |
| 802 | |
| 803 | auto &writeInfo = writeDescriptorInfo[imageCount]; |
| 804 | |
| 805 | writeInfo.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET; |
| 806 | writeInfo.pNext = nullptr; |
| 807 | writeInfo.dstSet = descriptorSet; |
| 808 | writeInfo.dstBinding = imageCount; |
| 809 | writeInfo.dstArrayElement = 0; |
| 810 | writeInfo.descriptorCount = 1; |
| 811 | writeInfo.descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER; |
| 812 | writeInfo.pImageInfo = &imageInfo; |
| 813 | writeInfo.pBufferInfo = nullptr; |
| 814 | writeInfo.pTexelBufferView = nullptr; |
| 815 | |
| 816 | imageCount++; |
| 817 | } |
| 818 | |
| 819 | VkDevice device = contextVk->getDevice(); |
| 820 | |
| 821 | ASSERT(imageCount > 0); |
| 822 | vkUpdateDescriptorSets(device, imageCount, writeDescriptorInfo.data(), 0, nullptr); |
| 823 | |
| 824 | mDirtyTextures = false; |
| 825 | } |
| 826 | |
| 827 | void ProgramVk::invalidateTextures() |
| 828 | { |
| 829 | mDirtyTextures = true; |
Jamie Madill | 76e471e | 2017-10-21 09:56:01 -0400 | [diff] [blame] | 830 | } |
| 831 | |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 832 | } // namespace rx |