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 | // VertexArrayVk.cpp: |
| 7 | // Implements the class methods for VertexArrayVk. |
| 8 | // |
| 9 | |
| 10 | #include "libANGLE/renderer/vulkan/VertexArrayVk.h" |
| 11 | |
| 12 | #include "common/debug.h" |
| 13 | |
Jamie Madill | c564c07 | 2017-06-01 12:45:42 -0400 | [diff] [blame] | 14 | #include "libANGLE/Context.h" |
Jamie Madill | bd159f0 | 2017-10-09 19:39:06 -0400 | [diff] [blame] | 15 | #include "libANGLE/renderer/vulkan/BufferVk.h" |
Jamie Madill | 1f46bc1 | 2018-02-20 16:09:43 -0500 | [diff] [blame] | 16 | #include "libANGLE/renderer/vulkan/CommandGraph.h" |
Jamie Madill | dd43e6c | 2017-03-24 14:18:49 -0400 | [diff] [blame] | 17 | #include "libANGLE/renderer/vulkan/ContextVk.h" |
Jamie Madill | c3755fc | 2018-04-05 08:39:13 -0400 | [diff] [blame^] | 18 | #include "libANGLE/renderer/vulkan/RendererVk.h" |
Jamie Madill | 3c424b4 | 2018-01-19 12:35:09 -0500 | [diff] [blame] | 19 | #include "libANGLE/renderer/vulkan/vk_format_utils.h" |
Jamie Madill | dd43e6c | 2017-03-24 14:18:49 -0400 | [diff] [blame] | 20 | |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 21 | namespace rx |
| 22 | { |
Jamie Madill | c3755fc | 2018-04-05 08:39:13 -0400 | [diff] [blame^] | 23 | namespace |
| 24 | { |
| 25 | constexpr size_t kDynamicVertexDataSize = 1024 * 1024; |
| 26 | constexpr size_t kDynamicIndexDataSize = 1024 * 8; |
| 27 | } // anonymous namespace |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 28 | |
Jamie Madill | bd159f0 | 2017-10-09 19:39:06 -0400 | [diff] [blame] | 29 | VertexArrayVk::VertexArrayVk(const gl::VertexArrayState &state) |
| 30 | : VertexArrayImpl(state), |
Jamie Madill | da854a2 | 2017-11-30 17:24:21 -0500 | [diff] [blame] | 31 | mCurrentArrayBufferHandles{}, |
Frank Henigman | 1744895 | 2017-01-05 15:48:26 -0500 | [diff] [blame] | 32 | mCurrentArrayBufferOffsets{}, |
Jamie Madill | da854a2 | 2017-11-30 17:24:21 -0500 | [diff] [blame] | 33 | mCurrentArrayBufferResources{}, |
Jamie Madill | c3755fc | 2018-04-05 08:39:13 -0400 | [diff] [blame^] | 34 | mCurrentElementArrayBufferHandle(VK_NULL_HANDLE), |
| 35 | mCurrentElementArrayBufferOffset(0), |
| 36 | mCurrentElementArrayBufferResource(nullptr), |
| 37 | mDynamicVertexData(VK_BUFFER_USAGE_VERTEX_BUFFER_BIT, kDynamicVertexDataSize), |
| 38 | mDynamicIndexData(VK_BUFFER_USAGE_INDEX_BUFFER_BIT, kDynamicIndexDataSize), |
| 39 | mVertexBuffersDirty(false), |
| 40 | mIndexBufferDirty(false) |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 41 | { |
Jamie Madill | da854a2 | 2017-11-30 17:24:21 -0500 | [diff] [blame] | 42 | mCurrentArrayBufferHandles.fill(VK_NULL_HANDLE); |
Frank Henigman | 1744895 | 2017-01-05 15:48:26 -0500 | [diff] [blame] | 43 | mCurrentArrayBufferOffsets.fill(0); |
Jamie Madill | da854a2 | 2017-11-30 17:24:21 -0500 | [diff] [blame] | 44 | mCurrentArrayBufferResources.fill(nullptr); |
Jamie Madill | 112a3a8 | 2018-01-23 13:04:06 -0500 | [diff] [blame] | 45 | |
| 46 | mPackedInputBindings.fill({0, 0}); |
| 47 | mPackedInputAttributes.fill({0, 0, 0}); |
Jamie Madill | c3755fc | 2018-04-05 08:39:13 -0400 | [diff] [blame^] | 48 | |
| 49 | mDynamicVertexData.init(1); |
| 50 | mDynamicIndexData.init(1); |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 51 | } |
| 52 | |
Jamie Madill | acf2f3a | 2017-11-21 19:22:44 -0500 | [diff] [blame] | 53 | VertexArrayVk::~VertexArrayVk() |
| 54 | { |
| 55 | } |
| 56 | |
Jamie Madill | 4928b7c | 2017-06-20 12:57:39 -0400 | [diff] [blame] | 57 | void VertexArrayVk::destroy(const gl::Context *context) |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 58 | { |
Jamie Madill | c3755fc | 2018-04-05 08:39:13 -0400 | [diff] [blame^] | 59 | VkDevice device = vk::GetImpl(context)->getRenderer()->getDevice(); |
| 60 | |
| 61 | mDynamicVertexData.destroy(device); |
| 62 | mDynamicIndexData.destroy(device); |
| 63 | mLineLoopHandler.destroy(device); |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 64 | } |
| 65 | |
Jamie Madill | c3755fc | 2018-04-05 08:39:13 -0400 | [diff] [blame^] | 66 | gl::Error VertexArrayVk::streamVertexData(RendererVk *renderer, |
| 67 | const gl::AttributesMask &attribsToStream, |
Jamie Madill | 32fd63b | 2018-03-31 11:20:35 -0400 | [diff] [blame] | 68 | const gl::DrawCallParams &drawCallParams) |
Frank Henigman | 1744895 | 2017-01-05 15:48:26 -0500 | [diff] [blame] | 69 | { |
Jamie Madill | c3755fc | 2018-04-05 08:39:13 -0400 | [diff] [blame^] | 70 | ASSERT(!attribsToStream.none()); |
| 71 | |
Frank Henigman | 1744895 | 2017-01-05 15:48:26 -0500 | [diff] [blame] | 72 | const auto &attribs = mState.getVertexAttributes(); |
| 73 | const auto &bindings = mState.getVertexBindings(); |
Frank Henigman | 1744895 | 2017-01-05 15:48:26 -0500 | [diff] [blame] | 74 | |
Jamie Madill | 32fd63b | 2018-03-31 11:20:35 -0400 | [diff] [blame] | 75 | const size_t lastVertex = drawCallParams.firstVertex() + drawCallParams.vertexCount(); |
| 76 | |
Frank Henigman | 1744895 | 2017-01-05 15:48:26 -0500 | [diff] [blame] | 77 | // TODO(fjhenigman): When we have a bunch of interleaved attributes, they end up |
| 78 | // un-interleaved, wasting space and copying time. Consider improving on that. |
Jamie Madill | c3755fc | 2018-04-05 08:39:13 -0400 | [diff] [blame^] | 79 | for (auto attribIndex : attribsToStream) |
Frank Henigman | 1744895 | 2017-01-05 15:48:26 -0500 | [diff] [blame] | 80 | { |
Frank Henigman | 6dd4a92 | 2018-03-02 16:35:13 -0500 | [diff] [blame] | 81 | const gl::VertexAttribute &attrib = attribs[attribIndex]; |
| 82 | const gl::VertexBinding &binding = bindings[attrib.bindingIndex]; |
| 83 | ASSERT(attrib.enabled && binding.getBuffer().get() == nullptr); |
Frank Henigman | 1744895 | 2017-01-05 15:48:26 -0500 | [diff] [blame] | 84 | |
Frank Henigman | 6dd4a92 | 2018-03-02 16:35:13 -0500 | [diff] [blame] | 85 | // TODO(fjhenigman): Work with more formats than just GL_FLOAT. |
| 86 | if (attrib.type != GL_FLOAT) |
Frank Henigman | 1744895 | 2017-01-05 15:48:26 -0500 | [diff] [blame] | 87 | { |
Frank Henigman | 6dd4a92 | 2018-03-02 16:35:13 -0500 | [diff] [blame] | 88 | UNIMPLEMENTED(); |
| 89 | return gl::InternalError(); |
Frank Henigman | 1744895 | 2017-01-05 15:48:26 -0500 | [diff] [blame] | 90 | } |
Frank Henigman | 6dd4a92 | 2018-03-02 16:35:13 -0500 | [diff] [blame] | 91 | |
| 92 | // Only [firstVertex, lastVertex] is needed by the upcoming draw so that |
| 93 | // is all we copy, but we allocate space for [0, lastVertex] so indexing |
| 94 | // will work. If we don't start at zero all the indices will be off. |
| 95 | // TODO(fjhenigman): See if we can account for indices being off by adjusting |
| 96 | // the offset, thus avoiding wasted memory. |
Jamie Madill | 32fd63b | 2018-03-31 11:20:35 -0400 | [diff] [blame] | 97 | const size_t firstByte = drawCallParams.firstVertex() * binding.getStride(); |
Frank Henigman | 6dd4a92 | 2018-03-02 16:35:13 -0500 | [diff] [blame] | 98 | const size_t lastByte = |
| 99 | lastVertex * binding.getStride() + gl::ComputeVertexAttributeTypeSize(attrib); |
| 100 | uint8_t *dst = nullptr; |
Luc Ferron | 7a06ac1 | 2018-03-15 10:17:04 -0400 | [diff] [blame] | 101 | uint32_t offset = 0; |
Jamie Madill | c3755fc | 2018-04-05 08:39:13 -0400 | [diff] [blame^] | 102 | ANGLE_TRY(mDynamicVertexData.allocate( |
| 103 | renderer, lastByte, &dst, &mCurrentArrayBufferHandles[attribIndex], &offset, nullptr)); |
Luc Ferron | 7a06ac1 | 2018-03-15 10:17:04 -0400 | [diff] [blame] | 104 | mCurrentArrayBufferOffsets[attribIndex] = static_cast<VkDeviceSize>(offset); |
Frank Henigman | 6dd4a92 | 2018-03-02 16:35:13 -0500 | [diff] [blame] | 105 | memcpy(dst + firstByte, static_cast<const uint8_t *>(attrib.pointer) + firstByte, |
| 106 | lastByte - firstByte); |
Frank Henigman | 1744895 | 2017-01-05 15:48:26 -0500 | [diff] [blame] | 107 | } |
| 108 | |
Jamie Madill | c3755fc | 2018-04-05 08:39:13 -0400 | [diff] [blame^] | 109 | ANGLE_TRY(mDynamicVertexData.flush(renderer->getDevice())); |
| 110 | return gl::NoError(); |
| 111 | } |
| 112 | |
| 113 | gl::Error VertexArrayVk::streamIndexData(RendererVk *renderer, |
| 114 | const gl::DrawCallParams &drawCallParams) |
| 115 | { |
| 116 | ASSERT(!mState.getElementArrayBuffer().get()); |
| 117 | |
| 118 | uint32_t offset = 0; |
| 119 | |
| 120 | const GLsizei amount = sizeof(GLushort) * drawCallParams.indexCount(); |
| 121 | GLubyte *dst = nullptr; |
| 122 | |
| 123 | ANGLE_TRY(mDynamicIndexData.allocate(renderer, amount, &dst, &mCurrentElementArrayBufferHandle, |
| 124 | &offset, nullptr)); |
| 125 | if (drawCallParams.type() == GL_UNSIGNED_BYTE) |
| 126 | { |
| 127 | // Unsigned bytes don't have direct support in Vulkan so we have to expand the |
| 128 | // memory to a GLushort. |
| 129 | const GLubyte *in = static_cast<const GLubyte *>(drawCallParams.indices()); |
| 130 | GLushort *expandedDst = reinterpret_cast<GLushort *>(dst); |
| 131 | for (GLsizei index = 0; index < drawCallParams.indexCount(); index++) |
| 132 | { |
| 133 | expandedDst[index] = static_cast<GLushort>(in[index]); |
| 134 | } |
| 135 | } |
| 136 | else |
| 137 | { |
| 138 | memcpy(dst, drawCallParams.indices(), amount); |
| 139 | } |
| 140 | ANGLE_TRY(mDynamicIndexData.flush(renderer->getDevice())); |
| 141 | |
| 142 | mCurrentElementArrayBufferOffset = offset; |
| 143 | |
Frank Henigman | 1744895 | 2017-01-05 15:48:26 -0500 | [diff] [blame] | 144 | return gl::NoError(); |
| 145 | } |
| 146 | |
Frank Henigman | 0af5b86 | 2018-03-27 20:19:33 -0400 | [diff] [blame] | 147 | gl::Error VertexArrayVk::syncState(const gl::Context *context, |
| 148 | const gl::VertexArray::DirtyBits &dirtyBits, |
| 149 | const gl::VertexArray::DirtyAttribBitsArray &attribBits, |
| 150 | const gl::VertexArray::DirtyBindingBitsArray &bindingBits) |
Jamie Madill | dd43e6c | 2017-03-24 14:18:49 -0400 | [diff] [blame] | 151 | { |
| 152 | ASSERT(dirtyBits.any()); |
Jamie Madill | 7210656 | 2017-03-24 14:18:50 -0400 | [diff] [blame] | 153 | |
Jamie Madill | c3755fc | 2018-04-05 08:39:13 -0400 | [diff] [blame^] | 154 | bool invalidatePipeline = false; |
| 155 | |
Jamie Madill | bd159f0 | 2017-10-09 19:39:06 -0400 | [diff] [blame] | 156 | // Invalidate current pipeline. |
Jamie Madill | acf2f3a | 2017-11-21 19:22:44 -0500 | [diff] [blame] | 157 | ContextVk *contextVk = vk::GetImpl(context); |
Jamie Madill | bd159f0 | 2017-10-09 19:39:06 -0400 | [diff] [blame] | 158 | |
| 159 | // Rebuild current attribute buffers cache. This will fail horribly if the buffer changes. |
| 160 | // TODO(jmadill): Handle buffer storage changes. |
| 161 | const auto &attribs = mState.getVertexAttributes(); |
| 162 | const auto &bindings = mState.getVertexBindings(); |
| 163 | |
Jamie Madill | 0946393 | 2018-04-04 05:26:59 -0400 | [diff] [blame] | 164 | for (size_t dirtyBit : dirtyBits) |
Jamie Madill | bd159f0 | 2017-10-09 19:39:06 -0400 | [diff] [blame] | 165 | { |
Jamie Madill | 0946393 | 2018-04-04 05:26:59 -0400 | [diff] [blame] | 166 | switch (dirtyBit) |
Jamie Madill | da854a2 | 2017-11-30 17:24:21 -0500 | [diff] [blame] | 167 | { |
Jamie Madill | 0946393 | 2018-04-04 05:26:59 -0400 | [diff] [blame] | 168 | case gl::VertexArray::DIRTY_BIT_ELEMENT_ARRAY_BUFFER: |
Jamie Madill | da854a2 | 2017-11-30 17:24:21 -0500 | [diff] [blame] | 169 | { |
Jamie Madill | 0946393 | 2018-04-04 05:26:59 -0400 | [diff] [blame] | 170 | gl::Buffer *bufferGL = mState.getElementArrayBuffer().get(); |
| 171 | if (bufferGL) |
| 172 | { |
Jamie Madill | c3755fc | 2018-04-05 08:39:13 -0400 | [diff] [blame^] | 173 | BufferVk *bufferVk = vk::GetImpl(bufferGL); |
| 174 | mCurrentElementArrayBufferResource = bufferVk; |
| 175 | mCurrentElementArrayBufferHandle = bufferVk->getVkBuffer().getHandle(); |
| 176 | mCurrentElementArrayBufferOffset = 0; |
Jamie Madill | 0946393 | 2018-04-04 05:26:59 -0400 | [diff] [blame] | 177 | } |
| 178 | else |
| 179 | { |
| 180 | mCurrentElementArrayBufferResource = nullptr; |
Jamie Madill | c3755fc | 2018-04-05 08:39:13 -0400 | [diff] [blame^] | 181 | mCurrentElementArrayBufferHandle = VK_NULL_HANDLE; |
| 182 | mCurrentElementArrayBufferOffset = 0; |
Jamie Madill | 0946393 | 2018-04-04 05:26:59 -0400 | [diff] [blame] | 183 | } |
Jamie Madill | c3755fc | 2018-04-05 08:39:13 -0400 | [diff] [blame^] | 184 | mIndexBufferDirty = true; |
Jamie Madill | 0946393 | 2018-04-04 05:26:59 -0400 | [diff] [blame] | 185 | break; |
Jamie Madill | da854a2 | 2017-11-30 17:24:21 -0500 | [diff] [blame] | 186 | } |
Jamie Madill | 0946393 | 2018-04-04 05:26:59 -0400 | [diff] [blame] | 187 | |
| 188 | case gl::VertexArray::DIRTY_BIT_ELEMENT_ARRAY_BUFFER_DATA: |
Jamie Madill | c3755fc | 2018-04-05 08:39:13 -0400 | [diff] [blame^] | 189 | mLineLoopBufferFirstIndex.reset(); |
| 190 | mLineLoopBufferLastIndex.reset(); |
Jamie Madill | 0946393 | 2018-04-04 05:26:59 -0400 | [diff] [blame] | 191 | break; |
| 192 | |
| 193 | default: |
Jamie Madill | da854a2 | 2017-11-30 17:24:21 -0500 | [diff] [blame] | 194 | { |
Jamie Madill | 0946393 | 2018-04-04 05:26:59 -0400 | [diff] [blame] | 195 | size_t attribIndex = gl::VertexArray::GetVertexIndexFromDirtyBit(dirtyBit); |
| 196 | |
| 197 | // Invalidate the input description for pipelines. |
| 198 | mDirtyPackedInputs.set(attribIndex); |
| 199 | |
| 200 | const auto &attrib = attribs[attribIndex]; |
| 201 | const auto &binding = bindings[attrib.bindingIndex]; |
| 202 | |
| 203 | if (attrib.enabled) |
| 204 | { |
| 205 | gl::Buffer *bufferGL = binding.getBuffer().get(); |
| 206 | |
| 207 | if (bufferGL) |
| 208 | { |
| 209 | BufferVk *bufferVk = vk::GetImpl(bufferGL); |
| 210 | mCurrentArrayBufferResources[attribIndex] = bufferVk; |
| 211 | mCurrentArrayBufferHandles[attribIndex] = |
| 212 | bufferVk->getVkBuffer().getHandle(); |
| 213 | mClientMemoryAttribs.reset(attribIndex); |
| 214 | } |
| 215 | else |
| 216 | { |
| 217 | mCurrentArrayBufferResources[attribIndex] = nullptr; |
| 218 | mCurrentArrayBufferHandles[attribIndex] = VK_NULL_HANDLE; |
| 219 | mClientMemoryAttribs.set(attribIndex); |
| 220 | } |
| 221 | // TODO(jmadill): Offset handling. Assume zero for now. |
| 222 | mCurrentArrayBufferOffsets[attribIndex] = 0; |
| 223 | } |
| 224 | else |
| 225 | { |
| 226 | mClientMemoryAttribs.reset(attribIndex); |
| 227 | UNIMPLEMENTED(); |
| 228 | } |
Jamie Madill | c3755fc | 2018-04-05 08:39:13 -0400 | [diff] [blame^] | 229 | |
| 230 | invalidatePipeline = true; |
Jamie Madill | 0946393 | 2018-04-04 05:26:59 -0400 | [diff] [blame] | 231 | break; |
Jamie Madill | da854a2 | 2017-11-30 17:24:21 -0500 | [diff] [blame] | 232 | } |
Jamie Madill | bd159f0 | 2017-10-09 19:39:06 -0400 | [diff] [blame] | 233 | } |
| 234 | } |
Frank Henigman | 0af5b86 | 2018-03-27 20:19:33 -0400 | [diff] [blame] | 235 | |
Jamie Madill | c3755fc | 2018-04-05 08:39:13 -0400 | [diff] [blame^] | 236 | if (invalidatePipeline) |
| 237 | { |
| 238 | mVertexBuffersDirty = true; |
| 239 | contextVk->invalidateCurrentPipeline(); |
| 240 | } |
| 241 | |
Frank Henigman | 0af5b86 | 2018-03-27 20:19:33 -0400 | [diff] [blame] | 242 | return gl::NoError(); |
Jamie Madill | bd159f0 | 2017-10-09 19:39:06 -0400 | [diff] [blame] | 243 | } |
| 244 | |
Jamie Madill | da854a2 | 2017-11-30 17:24:21 -0500 | [diff] [blame] | 245 | const gl::AttribArray<VkBuffer> &VertexArrayVk::getCurrentArrayBufferHandles() const |
Jamie Madill | bd159f0 | 2017-10-09 19:39:06 -0400 | [diff] [blame] | 246 | { |
Jamie Madill | da854a2 | 2017-11-30 17:24:21 -0500 | [diff] [blame] | 247 | return mCurrentArrayBufferHandles; |
Jamie Madill | bd159f0 | 2017-10-09 19:39:06 -0400 | [diff] [blame] | 248 | } |
| 249 | |
Frank Henigman | 1744895 | 2017-01-05 15:48:26 -0500 | [diff] [blame] | 250 | const gl::AttribArray<VkDeviceSize> &VertexArrayVk::getCurrentArrayBufferOffsets() const |
| 251 | { |
| 252 | return mCurrentArrayBufferOffsets; |
| 253 | } |
| 254 | |
Jamie Madill | c3755fc | 2018-04-05 08:39:13 -0400 | [diff] [blame^] | 255 | void VertexArrayVk::updateArrayBufferReadDependencies(vk::CommandGraphNode *drawNode, |
| 256 | const gl::AttributesMask &activeAttribsMask, |
| 257 | Serial serial) |
Jamie Madill | bd159f0 | 2017-10-09 19:39:06 -0400 | [diff] [blame] | 258 | { |
Jamie Madill | da854a2 | 2017-11-30 17:24:21 -0500 | [diff] [blame] | 259 | // Handle the bound array buffers. |
Jamie Madill | c3755fc | 2018-04-05 08:39:13 -0400 | [diff] [blame^] | 260 | for (size_t attribIndex : activeAttribsMask) |
Jamie Madill | bd159f0 | 2017-10-09 19:39:06 -0400 | [diff] [blame] | 261 | { |
Frank Henigman | 1744895 | 2017-01-05 15:48:26 -0500 | [diff] [blame] | 262 | if (mCurrentArrayBufferResources[attribIndex]) |
Jamie Madill | c3755fc | 2018-04-05 08:39:13 -0400 | [diff] [blame^] | 263 | mCurrentArrayBufferResources[attribIndex]->onReadResource(drawNode, serial); |
Jamie Madill | da854a2 | 2017-11-30 17:24:21 -0500 | [diff] [blame] | 264 | } |
Jamie Madill | c3755fc | 2018-04-05 08:39:13 -0400 | [diff] [blame^] | 265 | } |
Jamie Madill | da854a2 | 2017-11-30 17:24:21 -0500 | [diff] [blame] | 266 | |
Jamie Madill | c3755fc | 2018-04-05 08:39:13 -0400 | [diff] [blame^] | 267 | void VertexArrayVk::updateElementArrayBufferReadDependency(vk::CommandGraphNode *drawNode, |
| 268 | Serial serial) |
| 269 | { |
Jamie Madill | da854a2 | 2017-11-30 17:24:21 -0500 | [diff] [blame] | 270 | // Handle the bound element array buffer. |
Jamie Madill | c3755fc | 2018-04-05 08:39:13 -0400 | [diff] [blame^] | 271 | if (mCurrentElementArrayBufferResource) |
Jamie Madill | da854a2 | 2017-11-30 17:24:21 -0500 | [diff] [blame] | 272 | { |
Jamie Madill | c3755fc | 2018-04-05 08:39:13 -0400 | [diff] [blame^] | 273 | mCurrentElementArrayBufferResource->onReadResource(drawNode, serial); |
Jamie Madill | bd159f0 | 2017-10-09 19:39:06 -0400 | [diff] [blame] | 274 | } |
Jamie Madill | dd43e6c | 2017-03-24 14:18:49 -0400 | [diff] [blame] | 275 | } |
| 276 | |
Jamie Madill | 112a3a8 | 2018-01-23 13:04:06 -0500 | [diff] [blame] | 277 | void VertexArrayVk::getPackedInputDescriptions(vk::PipelineDesc *pipelineDesc) |
Jamie Madill | ebf7299 | 2017-10-13 14:09:45 -0400 | [diff] [blame] | 278 | { |
Jamie Madill | 112a3a8 | 2018-01-23 13:04:06 -0500 | [diff] [blame] | 279 | updatePackedInputDescriptions(); |
| 280 | pipelineDesc->updateVertexInputInfo(mPackedInputBindings, mPackedInputAttributes); |
Jamie Madill | ebf7299 | 2017-10-13 14:09:45 -0400 | [diff] [blame] | 281 | } |
| 282 | |
Jamie Madill | 112a3a8 | 2018-01-23 13:04:06 -0500 | [diff] [blame] | 283 | void VertexArrayVk::updatePackedInputDescriptions() |
Jamie Madill | ebf7299 | 2017-10-13 14:09:45 -0400 | [diff] [blame] | 284 | { |
Jamie Madill | 112a3a8 | 2018-01-23 13:04:06 -0500 | [diff] [blame] | 285 | if (!mDirtyPackedInputs.any()) |
Jamie Madill | ebf7299 | 2017-10-13 14:09:45 -0400 | [diff] [blame] | 286 | { |
| 287 | return; |
| 288 | } |
| 289 | |
| 290 | const auto &attribs = mState.getVertexAttributes(); |
| 291 | const auto &bindings = mState.getVertexBindings(); |
| 292 | |
Jamie Madill | 112a3a8 | 2018-01-23 13:04:06 -0500 | [diff] [blame] | 293 | for (auto attribIndex : mDirtyPackedInputs) |
Jamie Madill | ebf7299 | 2017-10-13 14:09:45 -0400 | [diff] [blame] | 294 | { |
| 295 | const auto &attrib = attribs[attribIndex]; |
| 296 | const auto &binding = bindings[attrib.bindingIndex]; |
| 297 | if (attrib.enabled) |
| 298 | { |
Jamie Madill | 112a3a8 | 2018-01-23 13:04:06 -0500 | [diff] [blame] | 299 | updatePackedInputInfo(static_cast<uint32_t>(attribIndex), binding, attrib); |
Jamie Madill | ebf7299 | 2017-10-13 14:09:45 -0400 | [diff] [blame] | 300 | } |
| 301 | else |
| 302 | { |
| 303 | UNIMPLEMENTED(); |
| 304 | } |
| 305 | } |
| 306 | |
Jamie Madill | 112a3a8 | 2018-01-23 13:04:06 -0500 | [diff] [blame] | 307 | mDirtyPackedInputs.reset(); |
| 308 | } |
| 309 | |
| 310 | void VertexArrayVk::updatePackedInputInfo(uint32_t attribIndex, |
| 311 | const gl::VertexBinding &binding, |
| 312 | const gl::VertexAttribute &attrib) |
| 313 | { |
| 314 | vk::PackedVertexInputBindingDesc &bindingDesc = mPackedInputBindings[attribIndex]; |
| 315 | |
| 316 | size_t attribSize = gl::ComputeVertexAttributeTypeSize(attrib); |
| 317 | ASSERT(attribSize <= std::numeric_limits<uint16_t>::max()); |
| 318 | |
Frank Henigman | a8e868f | 2018-01-28 23:32:25 -0500 | [diff] [blame] | 319 | bindingDesc.stride = static_cast<uint16_t>(binding.getStride()); |
Jamie Madill | 112a3a8 | 2018-01-23 13:04:06 -0500 | [diff] [blame] | 320 | bindingDesc.inputRate = static_cast<uint16_t>( |
| 321 | binding.getDivisor() > 0 ? VK_VERTEX_INPUT_RATE_INSTANCE : VK_VERTEX_INPUT_RATE_VERTEX); |
| 322 | |
| 323 | gl::VertexFormatType vertexFormatType = gl::GetVertexFormatType(attrib); |
| 324 | VkFormat vkFormat = vk::GetNativeVertexFormat(vertexFormatType); |
| 325 | ASSERT(vkFormat <= std::numeric_limits<uint16_t>::max()); |
| 326 | |
| 327 | vk::PackedVertexInputAttributeDesc &attribDesc = mPackedInputAttributes[attribIndex]; |
| 328 | attribDesc.format = static_cast<uint16_t>(vkFormat); |
| 329 | attribDesc.location = static_cast<uint16_t>(attribIndex); |
| 330 | attribDesc.offset = static_cast<uint32_t>(ComputeVertexAttributeOffset(attrib, binding)); |
Jamie Madill | ebf7299 | 2017-10-13 14:09:45 -0400 | [diff] [blame] | 331 | } |
| 332 | |
Jamie Madill | c3755fc | 2018-04-05 08:39:13 -0400 | [diff] [blame^] | 333 | gl::Error VertexArrayVk::drawArrays(const gl::Context *context, |
| 334 | RendererVk *renderer, |
| 335 | const gl::DrawCallParams &drawCallParams, |
| 336 | vk::CommandGraphNode *drawNode, |
| 337 | bool newCommandBuffer) |
| 338 | { |
| 339 | vk::CommandBuffer *commandBuffer = drawNode->getInsideRenderPassCommands(); |
| 340 | ASSERT(commandBuffer->valid()); |
| 341 | |
| 342 | ANGLE_TRY(onDraw(context, renderer, drawCallParams, drawNode, newCommandBuffer)); |
| 343 | |
| 344 | if (drawCallParams.mode() != GL_LINE_LOOP) |
| 345 | { |
| 346 | commandBuffer->draw(drawCallParams.vertexCount(), 1, drawCallParams.firstVertex(), 0); |
| 347 | return gl::NoError(); |
| 348 | } |
| 349 | |
| 350 | // Handle GL_LINE_LOOP drawArrays. |
| 351 | int lastVertex = drawCallParams.firstVertex() + drawCallParams.vertexCount(); |
| 352 | if (!mLineLoopBufferFirstIndex.valid() || !mLineLoopBufferLastIndex.valid() || |
| 353 | mLineLoopBufferFirstIndex != drawCallParams.firstVertex() || |
| 354 | mLineLoopBufferLastIndex != lastVertex) |
| 355 | { |
| 356 | ANGLE_TRY(mLineLoopHandler.createIndexBuffer(renderer, drawCallParams, |
| 357 | &mCurrentElementArrayBufferHandle, |
| 358 | &mCurrentElementArrayBufferOffset)); |
| 359 | |
| 360 | mLineLoopBufferFirstIndex = drawCallParams.firstVertex(); |
| 361 | mLineLoopBufferLastIndex = lastVertex; |
| 362 | } |
| 363 | |
| 364 | commandBuffer->bindIndexBuffer(mCurrentElementArrayBufferHandle, |
| 365 | mCurrentElementArrayBufferOffset, VK_INDEX_TYPE_UINT32); |
| 366 | |
| 367 | vk::LineLoopHandler::Draw(drawCallParams.vertexCount(), commandBuffer); |
| 368 | |
| 369 | return gl::NoError(); |
| 370 | } |
| 371 | |
| 372 | gl::Error VertexArrayVk::drawElements(const gl::Context *context, |
| 373 | RendererVk *renderer, |
| 374 | const gl::DrawCallParams &drawCallParams, |
| 375 | vk::CommandGraphNode *drawNode, |
| 376 | bool newCommandBuffer) |
| 377 | { |
| 378 | vk::CommandBuffer *commandBuffer = drawNode->getInsideRenderPassCommands(); |
| 379 | ASSERT(commandBuffer->valid()); |
| 380 | |
| 381 | if (drawCallParams.mode() != GL_LINE_LOOP) |
| 382 | { |
| 383 | ANGLE_TRY(onIndexedDraw(context, renderer, drawCallParams, drawNode, newCommandBuffer)); |
| 384 | commandBuffer->drawIndexed(drawCallParams.indexCount(), 1, 0, 0, 0); |
| 385 | return gl::NoError(); |
| 386 | } |
| 387 | |
| 388 | // Handle GL_LINE_LOOP drawElements. |
| 389 | gl::Buffer *elementArrayBuffer = mState.getElementArrayBuffer().get(); |
| 390 | if (!elementArrayBuffer) |
| 391 | { |
| 392 | UNIMPLEMENTED(); |
| 393 | return gl::InternalError() << "Line loop indices in client memory not supported"; |
| 394 | } |
| 395 | |
| 396 | BufferVk *elementArrayBufferVk = vk::GetImpl(elementArrayBuffer); |
| 397 | |
| 398 | VkIndexType indexType = gl_vk::GetIndexType(drawCallParams.type()); |
| 399 | |
| 400 | ANGLE_TRY(mLineLoopHandler.createIndexBufferFromElementArrayBuffer( |
| 401 | renderer, elementArrayBufferVk, indexType, drawCallParams.indexCount(), |
| 402 | &mCurrentElementArrayBufferHandle, &mCurrentElementArrayBufferOffset)); |
| 403 | |
| 404 | ANGLE_TRY(onIndexedDraw(context, renderer, drawCallParams, drawNode, newCommandBuffer)); |
| 405 | vk::LineLoopHandler::Draw(drawCallParams.indexCount(), commandBuffer); |
| 406 | |
| 407 | return gl::NoError(); |
| 408 | } |
| 409 | |
| 410 | gl::Error VertexArrayVk::onDraw(const gl::Context *context, |
| 411 | RendererVk *renderer, |
| 412 | const gl::DrawCallParams &drawCallParams, |
| 413 | vk::CommandGraphNode *drawNode, |
| 414 | bool newCommandBuffer) |
| 415 | { |
| 416 | const gl::State &state = context->getGLState(); |
| 417 | const gl::Program *programGL = state.getProgram(); |
| 418 | const gl::AttributesMask &activeAttribs = programGL->getActiveAttribLocationsMask(); |
| 419 | uint32_t maxAttrib = programGL->getState().getMaxActiveAttribLocation(); |
| 420 | |
| 421 | if (mClientMemoryAttribs.any()) |
| 422 | { |
| 423 | const gl::AttributesMask &attribsToStream = (mClientMemoryAttribs & activeAttribs); |
| 424 | if (attribsToStream.any()) |
| 425 | { |
| 426 | ANGLE_TRY(drawCallParams.ensureIndexRangeResolved(context)); |
| 427 | ANGLE_TRY(streamVertexData(renderer, attribsToStream, drawCallParams)); |
| 428 | vk::CommandBuffer *commandBuffer = drawNode->getInsideRenderPassCommands(); |
| 429 | commandBuffer->bindVertexBuffers(0, maxAttrib, mCurrentArrayBufferHandles.data(), |
| 430 | mCurrentArrayBufferOffsets.data()); |
| 431 | } |
| 432 | } |
| 433 | else if (mVertexBuffersDirty || newCommandBuffer) |
| 434 | { |
| 435 | vk::CommandBuffer *commandBuffer = drawNode->getInsideRenderPassCommands(); |
| 436 | commandBuffer->bindVertexBuffers(0, maxAttrib, mCurrentArrayBufferHandles.data(), |
| 437 | mCurrentArrayBufferOffsets.data()); |
| 438 | updateArrayBufferReadDependencies(drawNode, activeAttribs, |
| 439 | renderer->getCurrentQueueSerial()); |
| 440 | mVertexBuffersDirty = false; |
| 441 | } |
| 442 | |
| 443 | return gl::NoError(); |
| 444 | } |
| 445 | |
| 446 | gl::Error VertexArrayVk::onIndexedDraw(const gl::Context *context, |
| 447 | RendererVk *renderer, |
| 448 | const gl::DrawCallParams &drawCallParams, |
| 449 | vk::CommandGraphNode *drawNode, |
| 450 | bool newCommandBuffer) |
| 451 | { |
| 452 | ANGLE_TRY(onDraw(context, renderer, drawCallParams, drawNode, newCommandBuffer)); |
| 453 | |
| 454 | if (!mState.getElementArrayBuffer().get()) |
| 455 | { |
| 456 | ANGLE_TRY(drawCallParams.ensureIndexRangeResolved(context)); |
| 457 | ANGLE_TRY(streamIndexData(renderer, drawCallParams)); |
| 458 | vk::CommandBuffer *commandBuffer = drawNode->getInsideRenderPassCommands(); |
| 459 | commandBuffer->bindIndexBuffer(mCurrentElementArrayBufferHandle, |
| 460 | mCurrentElementArrayBufferOffset, |
| 461 | gl_vk::GetIndexType(drawCallParams.type())); |
| 462 | } |
| 463 | else if (mIndexBufferDirty || newCommandBuffer) |
| 464 | { |
| 465 | if (drawCallParams.type() == GL_UNSIGNED_BYTE) |
| 466 | { |
| 467 | // TODO(fjhenigman): Index format translation. |
| 468 | UNIMPLEMENTED(); |
| 469 | return gl::InternalError() |
| 470 | << "Unsigned byte translation is not implemented for indices in a buffer object"; |
| 471 | } |
| 472 | |
| 473 | vk::CommandBuffer *commandBuffer = drawNode->getInsideRenderPassCommands(); |
| 474 | commandBuffer->bindIndexBuffer(mCurrentElementArrayBufferHandle, |
| 475 | mCurrentElementArrayBufferOffset, |
| 476 | gl_vk::GetIndexType(drawCallParams.type())); |
| 477 | updateElementArrayBufferReadDependency(drawNode, renderer->getCurrentQueueSerial()); |
| 478 | mIndexBufferDirty = false; |
| 479 | } |
| 480 | |
| 481 | return gl::NoError(); |
| 482 | } |
| 483 | |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 484 | } // namespace rx |