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