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