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 | 316c606 | 2018-05-29 10:49:45 -0400 | [diff] [blame] | 18 | #include "libANGLE/renderer/vulkan/FramebufferVk.h" |
Jamie Madill | c3755fc | 2018-04-05 08:39:13 -0400 | [diff] [blame] | 19 | #include "libANGLE/renderer/vulkan/RendererVk.h" |
Jamie Madill | 3c424b4 | 2018-01-19 12:35:09 -0500 | [diff] [blame] | 20 | #include "libANGLE/renderer/vulkan/vk_format_utils.h" |
Jamie Madill | dd43e6c | 2017-03-24 14:18:49 -0400 | [diff] [blame] | 21 | |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 22 | namespace rx |
| 23 | { |
Jamie Madill | c3755fc | 2018-04-05 08:39:13 -0400 | [diff] [blame] | 24 | namespace |
| 25 | { |
| 26 | constexpr size_t kDynamicVertexDataSize = 1024 * 1024; |
| 27 | constexpr size_t kDynamicIndexDataSize = 1024 * 8; |
| 28 | } // anonymous namespace |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 29 | |
Luc Ferron | a9ab0f3 | 2018-05-17 17:03:55 -0400 | [diff] [blame] | 30 | VertexArrayVk::VertexArrayVk(const gl::VertexArrayState &state, RendererVk *renderer) |
Jamie Madill | bd159f0 | 2017-10-09 19:39:06 -0400 | [diff] [blame] | 31 | : VertexArrayImpl(state), |
Jamie Madill | da854a2 | 2017-11-30 17:24:21 -0500 | [diff] [blame] | 32 | mCurrentArrayBufferHandles{}, |
Frank Henigman | 1744895 | 2017-01-05 15:48:26 -0500 | [diff] [blame] | 33 | mCurrentArrayBufferOffsets{}, |
Jamie Madill | da854a2 | 2017-11-30 17:24:21 -0500 | [diff] [blame] | 34 | mCurrentArrayBufferResources{}, |
Jamie Madill | c3755fc | 2018-04-05 08:39:13 -0400 | [diff] [blame] | 35 | mCurrentElementArrayBufferHandle(VK_NULL_HANDLE), |
| 36 | mCurrentElementArrayBufferOffset(0), |
| 37 | mCurrentElementArrayBufferResource(nullptr), |
| 38 | mDynamicVertexData(VK_BUFFER_USAGE_VERTEX_BUFFER_BIT, kDynamicVertexDataSize), |
| 39 | mDynamicIndexData(VK_BUFFER_USAGE_INDEX_BUFFER_BIT, kDynamicIndexDataSize), |
Luc Ferron | 6ed167a | 2018-06-13 13:45:55 -0400 | [diff] [blame] | 40 | mTranslatedByteIndexData(VK_BUFFER_USAGE_INDEX_BUFFER_BIT, kDynamicIndexDataSize), |
Luc Ferron | a9ab0f3 | 2018-05-17 17:03:55 -0400 | [diff] [blame] | 41 | mLineLoopHelper(renderer), |
Jamie Madill | b8e3966 | 2018-04-04 11:41:42 -0400 | [diff] [blame] | 42 | mDirtyLineLoopTranslation(true), |
Jamie Madill | c3755fc | 2018-04-05 08:39:13 -0400 | [diff] [blame] | 43 | mVertexBuffersDirty(false), |
Luc Ferron | 49aacad | 2018-06-12 08:33:59 -0400 | [diff] [blame] | 44 | mIndexBufferDirty(false), |
| 45 | mLastIndexBufferOffset(0) |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 46 | { |
Jamie Madill | da854a2 | 2017-11-30 17:24:21 -0500 | [diff] [blame] | 47 | mCurrentArrayBufferHandles.fill(VK_NULL_HANDLE); |
Frank Henigman | 1744895 | 2017-01-05 15:48:26 -0500 | [diff] [blame] | 48 | mCurrentArrayBufferOffsets.fill(0); |
Jamie Madill | da854a2 | 2017-11-30 17:24:21 -0500 | [diff] [blame] | 49 | mCurrentArrayBufferResources.fill(nullptr); |
Jamie Madill | 112a3a8 | 2018-01-23 13:04:06 -0500 | [diff] [blame] | 50 | |
| 51 | mPackedInputBindings.fill({0, 0}); |
| 52 | mPackedInputAttributes.fill({0, 0, 0}); |
Jamie Madill | c3755fc | 2018-04-05 08:39:13 -0400 | [diff] [blame] | 53 | |
Luc Ferron | a9ab0f3 | 2018-05-17 17:03:55 -0400 | [diff] [blame] | 54 | mDynamicVertexData.init(1, renderer); |
| 55 | mDynamicIndexData.init(1, renderer); |
Luc Ferron | 6ed167a | 2018-06-13 13:45:55 -0400 | [diff] [blame] | 56 | mTranslatedByteIndexData.init(1, renderer); |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 57 | } |
| 58 | |
Jamie Madill | acf2f3a | 2017-11-21 19:22:44 -0500 | [diff] [blame] | 59 | VertexArrayVk::~VertexArrayVk() |
| 60 | { |
| 61 | } |
| 62 | |
Jamie Madill | 4928b7c | 2017-06-20 12:57:39 -0400 | [diff] [blame] | 63 | void VertexArrayVk::destroy(const gl::Context *context) |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 64 | { |
Jamie Madill | c3755fc | 2018-04-05 08:39:13 -0400 | [diff] [blame] | 65 | VkDevice device = vk::GetImpl(context)->getRenderer()->getDevice(); |
Jamie Madill | c3755fc | 2018-04-05 08:39:13 -0400 | [diff] [blame] | 66 | mDynamicVertexData.destroy(device); |
| 67 | mDynamicIndexData.destroy(device); |
Luc Ferron | 6ed167a | 2018-06-13 13:45:55 -0400 | [diff] [blame] | 68 | mTranslatedByteIndexData.destroy(device); |
Jamie Madill | 22f12fe | 2018-04-08 14:23:40 -0400 | [diff] [blame] | 69 | mLineLoopHelper.destroy(device); |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 70 | } |
| 71 | |
Jamie Madill | c3755fc | 2018-04-05 08:39:13 -0400 | [diff] [blame] | 72 | gl::Error VertexArrayVk::streamVertexData(RendererVk *renderer, |
| 73 | const gl::AttributesMask &attribsToStream, |
Jamie Madill | 32fd63b | 2018-03-31 11:20:35 -0400 | [diff] [blame] | 74 | const gl::DrawCallParams &drawCallParams) |
Frank Henigman | 1744895 | 2017-01-05 15:48:26 -0500 | [diff] [blame] | 75 | { |
Jamie Madill | c3755fc | 2018-04-05 08:39:13 -0400 | [diff] [blame] | 76 | ASSERT(!attribsToStream.none()); |
| 77 | |
Jamie Madill | bcef322 | 2018-04-13 15:19:11 -0400 | [diff] [blame] | 78 | const auto &attribs = mState.getVertexAttributes(); |
| 79 | const auto &bindings = mState.getVertexBindings(); |
Frank Henigman | 1744895 | 2017-01-05 15:48:26 -0500 | [diff] [blame] | 80 | |
Jamie Madill | 32fd63b | 2018-03-31 11:20:35 -0400 | [diff] [blame] | 81 | const size_t lastVertex = drawCallParams.firstVertex() + drawCallParams.vertexCount(); |
| 82 | |
Frank Henigman | 1744895 | 2017-01-05 15:48:26 -0500 | [diff] [blame] | 83 | // TODO(fjhenigman): When we have a bunch of interleaved attributes, they end up |
| 84 | // un-interleaved, wasting space and copying time. Consider improving on that. |
Jamie Madill | b8e3966 | 2018-04-04 11:41:42 -0400 | [diff] [blame] | 85 | for (size_t attribIndex : attribsToStream) |
Frank Henigman | 1744895 | 2017-01-05 15:48:26 -0500 | [diff] [blame] | 86 | { |
Frank Henigman | 6dd4a92 | 2018-03-02 16:35:13 -0500 | [diff] [blame] | 87 | const gl::VertexAttribute &attrib = attribs[attribIndex]; |
| 88 | const gl::VertexBinding &binding = bindings[attrib.bindingIndex]; |
| 89 | ASSERT(attrib.enabled && binding.getBuffer().get() == nullptr); |
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 | // TODO(fjhenigman): Work with more formats than just GL_FLOAT. |
| 92 | if (attrib.type != GL_FLOAT) |
Frank Henigman | 1744895 | 2017-01-05 15:48:26 -0500 | [diff] [blame] | 93 | { |
Frank Henigman | 6dd4a92 | 2018-03-02 16:35:13 -0500 | [diff] [blame] | 94 | UNIMPLEMENTED(); |
| 95 | return gl::InternalError(); |
Frank Henigman | 1744895 | 2017-01-05 15:48:26 -0500 | [diff] [blame] | 96 | } |
Frank Henigman | 6dd4a92 | 2018-03-02 16:35:13 -0500 | [diff] [blame] | 97 | |
| 98 | // Only [firstVertex, lastVertex] is needed by the upcoming draw so that |
| 99 | // is all we copy, but we allocate space for [0, lastVertex] so indexing |
| 100 | // will work. If we don't start at zero all the indices will be off. |
| 101 | // TODO(fjhenigman): See if we can account for indices being off by adjusting |
| 102 | // the offset, thus avoiding wasted memory. |
Jamie Madill | 32fd63b | 2018-03-31 11:20:35 -0400 | [diff] [blame] | 103 | const size_t firstByte = drawCallParams.firstVertex() * binding.getStride(); |
Frank Henigman | 6dd4a92 | 2018-03-02 16:35:13 -0500 | [diff] [blame] | 104 | const size_t lastByte = |
| 105 | lastVertex * binding.getStride() + gl::ComputeVertexAttributeTypeSize(attrib); |
Jamie Madill | 493f957 | 2018-05-24 19:52:15 -0400 | [diff] [blame] | 106 | uint8_t *dst = nullptr; |
Luc Ferron | 7a06ac1 | 2018-03-15 10:17:04 -0400 | [diff] [blame] | 107 | uint32_t offset = 0; |
Jamie Madill | c3755fc | 2018-04-05 08:39:13 -0400 | [diff] [blame] | 108 | ANGLE_TRY(mDynamicVertexData.allocate( |
| 109 | renderer, lastByte, &dst, &mCurrentArrayBufferHandles[attribIndex], &offset, nullptr)); |
Luc Ferron | 7a06ac1 | 2018-03-15 10:17:04 -0400 | [diff] [blame] | 110 | mCurrentArrayBufferOffsets[attribIndex] = static_cast<VkDeviceSize>(offset); |
Frank Henigman | 6dd4a92 | 2018-03-02 16:35:13 -0500 | [diff] [blame] | 111 | memcpy(dst + firstByte, static_cast<const uint8_t *>(attrib.pointer) + firstByte, |
| 112 | lastByte - firstByte); |
Frank Henigman | 1744895 | 2017-01-05 15:48:26 -0500 | [diff] [blame] | 113 | } |
| 114 | |
Jamie Madill | c3755fc | 2018-04-05 08:39:13 -0400 | [diff] [blame] | 115 | ANGLE_TRY(mDynamicVertexData.flush(renderer->getDevice())); |
Luc Ferron | 6185981 | 2018-05-09 14:17:39 -0400 | [diff] [blame] | 116 | mDynamicVertexData.releaseRetainedBuffers(renderer); |
Jamie Madill | c3755fc | 2018-04-05 08:39:13 -0400 | [diff] [blame] | 117 | return gl::NoError(); |
| 118 | } |
| 119 | |
| 120 | gl::Error VertexArrayVk::streamIndexData(RendererVk *renderer, |
| 121 | const gl::DrawCallParams &drawCallParams) |
| 122 | { |
| 123 | ASSERT(!mState.getElementArrayBuffer().get()); |
| 124 | |
| 125 | uint32_t offset = 0; |
| 126 | |
| 127 | const GLsizei amount = sizeof(GLushort) * drawCallParams.indexCount(); |
| 128 | GLubyte *dst = nullptr; |
| 129 | |
| 130 | ANGLE_TRY(mDynamicIndexData.allocate(renderer, amount, &dst, &mCurrentElementArrayBufferHandle, |
| 131 | &offset, nullptr)); |
| 132 | if (drawCallParams.type() == GL_UNSIGNED_BYTE) |
| 133 | { |
| 134 | // Unsigned bytes don't have direct support in Vulkan so we have to expand the |
| 135 | // memory to a GLushort. |
| 136 | const GLubyte *in = static_cast<const GLubyte *>(drawCallParams.indices()); |
| 137 | GLushort *expandedDst = reinterpret_cast<GLushort *>(dst); |
| 138 | for (GLsizei index = 0; index < drawCallParams.indexCount(); index++) |
| 139 | { |
| 140 | expandedDst[index] = static_cast<GLushort>(in[index]); |
| 141 | } |
| 142 | } |
| 143 | else |
| 144 | { |
| 145 | memcpy(dst, drawCallParams.indices(), amount); |
| 146 | } |
| 147 | ANGLE_TRY(mDynamicIndexData.flush(renderer->getDevice())); |
Luc Ferron | 6185981 | 2018-05-09 14:17:39 -0400 | [diff] [blame] | 148 | mDynamicIndexData.releaseRetainedBuffers(renderer); |
Jamie Madill | c3755fc | 2018-04-05 08:39:13 -0400 | [diff] [blame] | 149 | mCurrentElementArrayBufferOffset = offset; |
Frank Henigman | 1744895 | 2017-01-05 15:48:26 -0500 | [diff] [blame] | 150 | return gl::NoError(); |
| 151 | } |
| 152 | |
Jamie Madill | a56467e | 2018-04-11 16:19:41 -0400 | [diff] [blame] | 153 | #define ANGLE_VERTEX_DIRTY_ATTRIB_FUNC(INDEX) \ |
| 154 | case gl::VertexArray::DIRTY_BIT_ATTRIB_0 + INDEX: \ |
| 155 | syncDirtyAttrib(attribs[INDEX], bindings[attribs[INDEX].bindingIndex], INDEX); \ |
| 156 | invalidatePipeline = true; \ |
| 157 | break; |
| 158 | |
| 159 | #define ANGLE_VERTEX_DIRTY_BINDING_FUNC(INDEX) \ |
| 160 | case gl::VertexArray::DIRTY_BIT_BINDING_0 + INDEX: \ |
| 161 | syncDirtyAttrib(attribs[INDEX], bindings[attribs[INDEX].bindingIndex], INDEX); \ |
| 162 | invalidatePipeline = true; \ |
| 163 | break; |
| 164 | |
| 165 | #define ANGLE_VERTEX_DIRTY_BUFFER_DATA_FUNC(INDEX) \ |
| 166 | case gl::VertexArray::DIRTY_BIT_BUFFER_DATA_0 + INDEX: \ |
| 167 | break; |
| 168 | |
Frank Henigman | 0af5b86 | 2018-03-27 20:19:33 -0400 | [diff] [blame] | 169 | gl::Error VertexArrayVk::syncState(const gl::Context *context, |
| 170 | const gl::VertexArray::DirtyBits &dirtyBits, |
| 171 | const gl::VertexArray::DirtyAttribBitsArray &attribBits, |
| 172 | const gl::VertexArray::DirtyBindingBitsArray &bindingBits) |
Jamie Madill | dd43e6c | 2017-03-24 14:18:49 -0400 | [diff] [blame] | 173 | { |
| 174 | ASSERT(dirtyBits.any()); |
Jamie Madill | 7210656 | 2017-03-24 14:18:50 -0400 | [diff] [blame] | 175 | |
Jamie Madill | c3755fc | 2018-04-05 08:39:13 -0400 | [diff] [blame] | 176 | bool invalidatePipeline = false; |
| 177 | |
Jamie Madill | bd159f0 | 2017-10-09 19:39:06 -0400 | [diff] [blame] | 178 | // Invalidate current pipeline. |
Jamie Madill | acf2f3a | 2017-11-21 19:22:44 -0500 | [diff] [blame] | 179 | ContextVk *contextVk = vk::GetImpl(context); |
Jamie Madill | bd159f0 | 2017-10-09 19:39:06 -0400 | [diff] [blame] | 180 | |
| 181 | // Rebuild current attribute buffers cache. This will fail horribly if the buffer changes. |
| 182 | // TODO(jmadill): Handle buffer storage changes. |
| 183 | const auto &attribs = mState.getVertexAttributes(); |
| 184 | const auto &bindings = mState.getVertexBindings(); |
| 185 | |
Jamie Madill | 0946393 | 2018-04-04 05:26:59 -0400 | [diff] [blame] | 186 | for (size_t dirtyBit : dirtyBits) |
Jamie Madill | bd159f0 | 2017-10-09 19:39:06 -0400 | [diff] [blame] | 187 | { |
Jamie Madill | 0946393 | 2018-04-04 05:26:59 -0400 | [diff] [blame] | 188 | switch (dirtyBit) |
Jamie Madill | da854a2 | 2017-11-30 17:24:21 -0500 | [diff] [blame] | 189 | { |
Jamie Madill | 0946393 | 2018-04-04 05:26:59 -0400 | [diff] [blame] | 190 | case gl::VertexArray::DIRTY_BIT_ELEMENT_ARRAY_BUFFER: |
Jamie Madill | da854a2 | 2017-11-30 17:24:21 -0500 | [diff] [blame] | 191 | { |
Jamie Madill | 0946393 | 2018-04-04 05:26:59 -0400 | [diff] [blame] | 192 | gl::Buffer *bufferGL = mState.getElementArrayBuffer().get(); |
| 193 | if (bufferGL) |
| 194 | { |
Jamie Madill | c3755fc | 2018-04-05 08:39:13 -0400 | [diff] [blame] | 195 | BufferVk *bufferVk = vk::GetImpl(bufferGL); |
| 196 | mCurrentElementArrayBufferResource = bufferVk; |
| 197 | mCurrentElementArrayBufferHandle = bufferVk->getVkBuffer().getHandle(); |
Jamie Madill | 0946393 | 2018-04-04 05:26:59 -0400 | [diff] [blame] | 198 | } |
| 199 | else |
| 200 | { |
| 201 | mCurrentElementArrayBufferResource = nullptr; |
Jamie Madill | c3755fc | 2018-04-05 08:39:13 -0400 | [diff] [blame] | 202 | mCurrentElementArrayBufferHandle = VK_NULL_HANDLE; |
Jamie Madill | 0946393 | 2018-04-04 05:26:59 -0400 | [diff] [blame] | 203 | } |
Luc Ferron | 387b3b3 | 2018-05-28 10:11:57 -0400 | [diff] [blame] | 204 | |
| 205 | mCurrentElementArrayBufferOffset = 0; |
| 206 | mLineLoopBufferFirstIndex.reset(); |
| 207 | mLineLoopBufferLastIndex.reset(); |
Jamie Madill | b8e3966 | 2018-04-04 11:41:42 -0400 | [diff] [blame] | 208 | mIndexBufferDirty = true; |
| 209 | mDirtyLineLoopTranslation = true; |
Jamie Madill | 0946393 | 2018-04-04 05:26:59 -0400 | [diff] [blame] | 210 | break; |
Jamie Madill | da854a2 | 2017-11-30 17:24:21 -0500 | [diff] [blame] | 211 | } |
Jamie Madill | 0946393 | 2018-04-04 05:26:59 -0400 | [diff] [blame] | 212 | |
| 213 | case gl::VertexArray::DIRTY_BIT_ELEMENT_ARRAY_BUFFER_DATA: |
Jamie Madill | c3755fc | 2018-04-05 08:39:13 -0400 | [diff] [blame] | 214 | mLineLoopBufferFirstIndex.reset(); |
| 215 | mLineLoopBufferLastIndex.reset(); |
Jamie Madill | b8e3966 | 2018-04-04 11:41:42 -0400 | [diff] [blame] | 216 | mDirtyLineLoopTranslation = true; |
Jamie Madill | 0946393 | 2018-04-04 05:26:59 -0400 | [diff] [blame] | 217 | break; |
| 218 | |
Jamie Madill | a56467e | 2018-04-11 16:19:41 -0400 | [diff] [blame] | 219 | ANGLE_VERTEX_INDEX_CASES(ANGLE_VERTEX_DIRTY_ATTRIB_FUNC); |
| 220 | ANGLE_VERTEX_INDEX_CASES(ANGLE_VERTEX_DIRTY_BINDING_FUNC); |
| 221 | ANGLE_VERTEX_INDEX_CASES(ANGLE_VERTEX_DIRTY_BUFFER_DATA_FUNC); |
| 222 | |
Jamie Madill | 0946393 | 2018-04-04 05:26:59 -0400 | [diff] [blame] | 223 | default: |
Jamie Madill | a56467e | 2018-04-11 16:19:41 -0400 | [diff] [blame] | 224 | UNREACHABLE(); |
Jamie Madill | 0946393 | 2018-04-04 05:26:59 -0400 | [diff] [blame] | 225 | break; |
Jamie Madill | bd159f0 | 2017-10-09 19:39:06 -0400 | [diff] [blame] | 226 | } |
| 227 | } |
Frank Henigman | 0af5b86 | 2018-03-27 20:19:33 -0400 | [diff] [blame] | 228 | |
Jamie Madill | c3755fc | 2018-04-05 08:39:13 -0400 | [diff] [blame] | 229 | if (invalidatePipeline) |
| 230 | { |
| 231 | mVertexBuffersDirty = true; |
| 232 | contextVk->invalidateCurrentPipeline(); |
| 233 | } |
| 234 | |
Frank Henigman | 0af5b86 | 2018-03-27 20:19:33 -0400 | [diff] [blame] | 235 | return gl::NoError(); |
Jamie Madill | bd159f0 | 2017-10-09 19:39:06 -0400 | [diff] [blame] | 236 | } |
| 237 | |
Jamie Madill | a56467e | 2018-04-11 16:19:41 -0400 | [diff] [blame] | 238 | void VertexArrayVk::syncDirtyAttrib(const gl::VertexAttribute &attrib, |
| 239 | const gl::VertexBinding &binding, |
| 240 | size_t attribIndex) |
| 241 | { |
| 242 | // Invalidate the input description for pipelines. |
| 243 | mDirtyPackedInputs.set(attribIndex); |
| 244 | |
| 245 | if (attrib.enabled) |
| 246 | { |
| 247 | gl::Buffer *bufferGL = binding.getBuffer().get(); |
| 248 | |
| 249 | if (bufferGL) |
| 250 | { |
| 251 | BufferVk *bufferVk = vk::GetImpl(bufferGL); |
| 252 | mCurrentArrayBufferResources[attribIndex] = bufferVk; |
| 253 | mCurrentArrayBufferHandles[attribIndex] = bufferVk->getVkBuffer().getHandle(); |
Jamie Madill | a56467e | 2018-04-11 16:19:41 -0400 | [diff] [blame] | 254 | } |
| 255 | else |
| 256 | { |
| 257 | mCurrentArrayBufferResources[attribIndex] = nullptr; |
| 258 | mCurrentArrayBufferHandles[attribIndex] = VK_NULL_HANDLE; |
Jamie Madill | a56467e | 2018-04-11 16:19:41 -0400 | [diff] [blame] | 259 | } |
| 260 | // TODO(jmadill): Offset handling. Assume zero for now. |
| 261 | mCurrentArrayBufferOffsets[attribIndex] = 0; |
| 262 | } |
| 263 | else |
| 264 | { |
Jamie Madill | a56467e | 2018-04-11 16:19:41 -0400 | [diff] [blame] | 265 | UNIMPLEMENTED(); |
| 266 | } |
| 267 | } |
| 268 | |
Jamie Madill | da854a2 | 2017-11-30 17:24:21 -0500 | [diff] [blame] | 269 | const gl::AttribArray<VkBuffer> &VertexArrayVk::getCurrentArrayBufferHandles() const |
Jamie Madill | bd159f0 | 2017-10-09 19:39:06 -0400 | [diff] [blame] | 270 | { |
Jamie Madill | da854a2 | 2017-11-30 17:24:21 -0500 | [diff] [blame] | 271 | return mCurrentArrayBufferHandles; |
Jamie Madill | bd159f0 | 2017-10-09 19:39:06 -0400 | [diff] [blame] | 272 | } |
| 273 | |
Frank Henigman | 1744895 | 2017-01-05 15:48:26 -0500 | [diff] [blame] | 274 | const gl::AttribArray<VkDeviceSize> &VertexArrayVk::getCurrentArrayBufferOffsets() const |
| 275 | { |
| 276 | return mCurrentArrayBufferOffsets; |
| 277 | } |
| 278 | |
Jamie Madill | 316c606 | 2018-05-29 10:49:45 -0400 | [diff] [blame] | 279 | void VertexArrayVk::updateArrayBufferReadDependencies(vk::CommandGraphResource *drawFramebuffer, |
Jamie Madill | c3755fc | 2018-04-05 08:39:13 -0400 | [diff] [blame] | 280 | const gl::AttributesMask &activeAttribsMask, |
| 281 | Serial serial) |
Jamie Madill | bd159f0 | 2017-10-09 19:39:06 -0400 | [diff] [blame] | 282 | { |
Jamie Madill | da854a2 | 2017-11-30 17:24:21 -0500 | [diff] [blame] | 283 | // Handle the bound array buffers. |
Jamie Madill | c3755fc | 2018-04-05 08:39:13 -0400 | [diff] [blame] | 284 | for (size_t attribIndex : activeAttribsMask) |
Jamie Madill | bd159f0 | 2017-10-09 19:39:06 -0400 | [diff] [blame] | 285 | { |
Frank Henigman | 1744895 | 2017-01-05 15:48:26 -0500 | [diff] [blame] | 286 | if (mCurrentArrayBufferResources[attribIndex]) |
Jamie Madill | 316c606 | 2018-05-29 10:49:45 -0400 | [diff] [blame] | 287 | mCurrentArrayBufferResources[attribIndex]->addReadDependency(drawFramebuffer); |
Jamie Madill | da854a2 | 2017-11-30 17:24:21 -0500 | [diff] [blame] | 288 | } |
Jamie Madill | c3755fc | 2018-04-05 08:39:13 -0400 | [diff] [blame] | 289 | } |
Jamie Madill | da854a2 | 2017-11-30 17:24:21 -0500 | [diff] [blame] | 290 | |
Jamie Madill | 316c606 | 2018-05-29 10:49:45 -0400 | [diff] [blame] | 291 | void VertexArrayVk::updateElementArrayBufferReadDependency( |
| 292 | vk::CommandGraphResource *drawFramebuffer, |
| 293 | Serial serial) |
Jamie Madill | c3755fc | 2018-04-05 08:39:13 -0400 | [diff] [blame] | 294 | { |
Jamie Madill | da854a2 | 2017-11-30 17:24:21 -0500 | [diff] [blame] | 295 | // Handle the bound element array buffer. |
Jamie Madill | c3755fc | 2018-04-05 08:39:13 -0400 | [diff] [blame] | 296 | if (mCurrentElementArrayBufferResource) |
Jamie Madill | da854a2 | 2017-11-30 17:24:21 -0500 | [diff] [blame] | 297 | { |
Jamie Madill | 316c606 | 2018-05-29 10:49:45 -0400 | [diff] [blame] | 298 | mCurrentElementArrayBufferResource->addReadDependency(drawFramebuffer); |
Jamie Madill | bd159f0 | 2017-10-09 19:39:06 -0400 | [diff] [blame] | 299 | } |
Jamie Madill | dd43e6c | 2017-03-24 14:18:49 -0400 | [diff] [blame] | 300 | } |
| 301 | |
Frank Henigman | 2ad498e | 2018-06-20 13:19:01 -0400 | [diff] [blame] | 302 | void VertexArrayVk::getPackedInputDescriptions(const RendererVk *rendererVk, |
| 303 | vk::PipelineDesc *pipelineDesc) |
Jamie Madill | ebf7299 | 2017-10-13 14:09:45 -0400 | [diff] [blame] | 304 | { |
Frank Henigman | 2ad498e | 2018-06-20 13:19:01 -0400 | [diff] [blame] | 305 | updatePackedInputDescriptions(rendererVk); |
Jamie Madill | 112a3a8 | 2018-01-23 13:04:06 -0500 | [diff] [blame] | 306 | pipelineDesc->updateVertexInputInfo(mPackedInputBindings, mPackedInputAttributes); |
Jamie Madill | ebf7299 | 2017-10-13 14:09:45 -0400 | [diff] [blame] | 307 | } |
| 308 | |
Frank Henigman | 2ad498e | 2018-06-20 13:19:01 -0400 | [diff] [blame] | 309 | void VertexArrayVk::updatePackedInputDescriptions(const RendererVk *rendererVk) |
Jamie Madill | ebf7299 | 2017-10-13 14:09:45 -0400 | [diff] [blame] | 310 | { |
Jamie Madill | 112a3a8 | 2018-01-23 13:04:06 -0500 | [diff] [blame] | 311 | if (!mDirtyPackedInputs.any()) |
Jamie Madill | ebf7299 | 2017-10-13 14:09:45 -0400 | [diff] [blame] | 312 | { |
| 313 | return; |
| 314 | } |
| 315 | |
| 316 | const auto &attribs = mState.getVertexAttributes(); |
| 317 | const auto &bindings = mState.getVertexBindings(); |
| 318 | |
Jamie Madill | 112a3a8 | 2018-01-23 13:04:06 -0500 | [diff] [blame] | 319 | for (auto attribIndex : mDirtyPackedInputs) |
Jamie Madill | ebf7299 | 2017-10-13 14:09:45 -0400 | [diff] [blame] | 320 | { |
| 321 | const auto &attrib = attribs[attribIndex]; |
| 322 | const auto &binding = bindings[attrib.bindingIndex]; |
| 323 | if (attrib.enabled) |
| 324 | { |
Frank Henigman | 2ad498e | 2018-06-20 13:19:01 -0400 | [diff] [blame] | 325 | updatePackedInputInfo(rendererVk, static_cast<uint32_t>(attribIndex), binding, attrib); |
Jamie Madill | ebf7299 | 2017-10-13 14:09:45 -0400 | [diff] [blame] | 326 | } |
| 327 | else |
| 328 | { |
| 329 | UNIMPLEMENTED(); |
| 330 | } |
| 331 | } |
| 332 | |
Jamie Madill | 112a3a8 | 2018-01-23 13:04:06 -0500 | [diff] [blame] | 333 | mDirtyPackedInputs.reset(); |
| 334 | } |
| 335 | |
Frank Henigman | 2ad498e | 2018-06-20 13:19:01 -0400 | [diff] [blame] | 336 | void VertexArrayVk::updatePackedInputInfo(const RendererVk *rendererVk, |
| 337 | uint32_t attribIndex, |
Jamie Madill | 112a3a8 | 2018-01-23 13:04:06 -0500 | [diff] [blame] | 338 | const gl::VertexBinding &binding, |
| 339 | const gl::VertexAttribute &attrib) |
| 340 | { |
| 341 | vk::PackedVertexInputBindingDesc &bindingDesc = mPackedInputBindings[attribIndex]; |
| 342 | |
| 343 | size_t attribSize = gl::ComputeVertexAttributeTypeSize(attrib); |
| 344 | ASSERT(attribSize <= std::numeric_limits<uint16_t>::max()); |
| 345 | |
Frank Henigman | a8e868f | 2018-01-28 23:32:25 -0500 | [diff] [blame] | 346 | bindingDesc.stride = static_cast<uint16_t>(binding.getStride()); |
Jamie Madill | 112a3a8 | 2018-01-23 13:04:06 -0500 | [diff] [blame] | 347 | bindingDesc.inputRate = static_cast<uint16_t>( |
| 348 | binding.getDivisor() > 0 ? VK_VERTEX_INPUT_RATE_INSTANCE : VK_VERTEX_INPUT_RATE_VERTEX); |
| 349 | |
Frank Henigman | 2ad498e | 2018-06-20 13:19:01 -0400 | [diff] [blame] | 350 | VkFormat vkFormat = rendererVk->getFormat(GetVertexFormatID(attrib)).vkBufferFormat; |
Jamie Madill | 112a3a8 | 2018-01-23 13:04:06 -0500 | [diff] [blame] | 351 | ASSERT(vkFormat <= std::numeric_limits<uint16_t>::max()); |
| 352 | |
| 353 | vk::PackedVertexInputAttributeDesc &attribDesc = mPackedInputAttributes[attribIndex]; |
| 354 | attribDesc.format = static_cast<uint16_t>(vkFormat); |
| 355 | attribDesc.location = static_cast<uint16_t>(attribIndex); |
| 356 | attribDesc.offset = static_cast<uint32_t>(ComputeVertexAttributeOffset(attrib, binding)); |
Jamie Madill | ebf7299 | 2017-10-13 14:09:45 -0400 | [diff] [blame] | 357 | } |
| 358 | |
Jamie Madill | c3755fc | 2018-04-05 08:39:13 -0400 | [diff] [blame] | 359 | gl::Error VertexArrayVk::drawArrays(const gl::Context *context, |
| 360 | RendererVk *renderer, |
| 361 | const gl::DrawCallParams &drawCallParams, |
Jamie Madill | 316c606 | 2018-05-29 10:49:45 -0400 | [diff] [blame] | 362 | vk::CommandBuffer *commandBuffer, |
Jamie Madill | c3755fc | 2018-04-05 08:39:13 -0400 | [diff] [blame] | 363 | bool newCommandBuffer) |
| 364 | { |
Jamie Madill | c3755fc | 2018-04-05 08:39:13 -0400 | [diff] [blame] | 365 | ASSERT(commandBuffer->valid()); |
| 366 | |
Jamie Madill | 316c606 | 2018-05-29 10:49:45 -0400 | [diff] [blame] | 367 | ANGLE_TRY(onDraw(context, renderer, drawCallParams, commandBuffer, newCommandBuffer)); |
Jamie Madill | c3755fc | 2018-04-05 08:39:13 -0400 | [diff] [blame] | 368 | |
Jamie Madill | 18e323a | 2018-05-11 16:54:17 -0400 | [diff] [blame] | 369 | // Note: Vertex indexes can be arbitrarily large. |
| 370 | uint32_t clampedVertexCount = drawCallParams.getClampedVertexCount<uint32_t>(); |
| 371 | |
Jamie Madill | 493f957 | 2018-05-24 19:52:15 -0400 | [diff] [blame] | 372 | if (drawCallParams.mode() != gl::PrimitiveMode::LineLoop) |
Jamie Madill | c3755fc | 2018-04-05 08:39:13 -0400 | [diff] [blame] | 373 | { |
Jamie Madill | 18e323a | 2018-05-11 16:54:17 -0400 | [diff] [blame] | 374 | commandBuffer->draw(clampedVertexCount, 1, drawCallParams.firstVertex(), 0); |
Jamie Madill | c3755fc | 2018-04-05 08:39:13 -0400 | [diff] [blame] | 375 | return gl::NoError(); |
| 376 | } |
| 377 | |
| 378 | // Handle GL_LINE_LOOP drawArrays. |
Jamie Madill | 18e323a | 2018-05-11 16:54:17 -0400 | [diff] [blame] | 379 | size_t lastVertex = static_cast<size_t>(drawCallParams.firstVertex() + clampedVertexCount); |
Jamie Madill | c3755fc | 2018-04-05 08:39:13 -0400 | [diff] [blame] | 380 | if (!mLineLoopBufferFirstIndex.valid() || !mLineLoopBufferLastIndex.valid() || |
| 381 | mLineLoopBufferFirstIndex != drawCallParams.firstVertex() || |
| 382 | mLineLoopBufferLastIndex != lastVertex) |
| 383 | { |
Jamie Madill | 22f12fe | 2018-04-08 14:23:40 -0400 | [diff] [blame] | 384 | ANGLE_TRY(mLineLoopHelper.getIndexBufferForDrawArrays(renderer, drawCallParams, |
| 385 | &mCurrentElementArrayBufferHandle, |
| 386 | &mCurrentElementArrayBufferOffset)); |
Jamie Madill | c3755fc | 2018-04-05 08:39:13 -0400 | [diff] [blame] | 387 | |
| 388 | mLineLoopBufferFirstIndex = drawCallParams.firstVertex(); |
| 389 | mLineLoopBufferLastIndex = lastVertex; |
| 390 | } |
| 391 | |
| 392 | commandBuffer->bindIndexBuffer(mCurrentElementArrayBufferHandle, |
| 393 | mCurrentElementArrayBufferOffset, VK_INDEX_TYPE_UINT32); |
| 394 | |
Jamie Madill | 18e323a | 2018-05-11 16:54:17 -0400 | [diff] [blame] | 395 | vk::LineLoopHelper::Draw(clampedVertexCount, commandBuffer); |
Jamie Madill | c3755fc | 2018-04-05 08:39:13 -0400 | [diff] [blame] | 396 | |
| 397 | return gl::NoError(); |
| 398 | } |
| 399 | |
| 400 | gl::Error VertexArrayVk::drawElements(const gl::Context *context, |
| 401 | RendererVk *renderer, |
| 402 | const gl::DrawCallParams &drawCallParams, |
Jamie Madill | 316c606 | 2018-05-29 10:49:45 -0400 | [diff] [blame] | 403 | vk::CommandBuffer *commandBuffer, |
Jamie Madill | c3755fc | 2018-04-05 08:39:13 -0400 | [diff] [blame] | 404 | bool newCommandBuffer) |
| 405 | { |
Jamie Madill | c3755fc | 2018-04-05 08:39:13 -0400 | [diff] [blame] | 406 | ASSERT(commandBuffer->valid()); |
| 407 | |
Jamie Madill | 493f957 | 2018-05-24 19:52:15 -0400 | [diff] [blame] | 408 | if (drawCallParams.mode() != gl::PrimitiveMode::LineLoop) |
Jamie Madill | c3755fc | 2018-04-05 08:39:13 -0400 | [diff] [blame] | 409 | { |
Jamie Madill | 316c606 | 2018-05-29 10:49:45 -0400 | [diff] [blame] | 410 | ANGLE_TRY( |
| 411 | onIndexedDraw(context, renderer, drawCallParams, commandBuffer, newCommandBuffer)); |
Jamie Madill | c3755fc | 2018-04-05 08:39:13 -0400 | [diff] [blame] | 412 | commandBuffer->drawIndexed(drawCallParams.indexCount(), 1, 0, 0, 0); |
| 413 | return gl::NoError(); |
| 414 | } |
| 415 | |
| 416 | // Handle GL_LINE_LOOP drawElements. |
Jamie Madill | b8e3966 | 2018-04-04 11:41:42 -0400 | [diff] [blame] | 417 | if (mDirtyLineLoopTranslation) |
| 418 | { |
Luc Ferron | a912046 | 2018-04-12 13:11:03 -0400 | [diff] [blame] | 419 | gl::Buffer *elementArrayBuffer = mState.getElementArrayBuffer().get(); |
| 420 | VkIndexType indexType = gl_vk::GetIndexType(drawCallParams.type()); |
| 421 | |
| 422 | if (!elementArrayBuffer) |
| 423 | { |
| 424 | ANGLE_TRY(mLineLoopHelper.getIndexBufferForClientElementArray( |
Luc Ferron | 3e31380 | 2018-06-11 14:44:33 -0400 | [diff] [blame] | 425 | renderer, drawCallParams, &mCurrentElementArrayBufferHandle, |
| 426 | &mCurrentElementArrayBufferOffset)); |
Luc Ferron | a912046 | 2018-04-12 13:11:03 -0400 | [diff] [blame] | 427 | } |
| 428 | else |
| 429 | { |
Luc Ferron | a4fa9c2 | 2018-04-13 07:00:56 -0400 | [diff] [blame] | 430 | // When using an element array buffer, 'indices' is an offset to the first element. |
| 431 | intptr_t offset = reinterpret_cast<intptr_t>(drawCallParams.indices()); |
Luc Ferron | a912046 | 2018-04-12 13:11:03 -0400 | [diff] [blame] | 432 | BufferVk *elementArrayBufferVk = vk::GetImpl(elementArrayBuffer); |
| 433 | ANGLE_TRY(mLineLoopHelper.getIndexBufferForElementArrayBuffer( |
Luc Ferron | a4fa9c2 | 2018-04-13 07:00:56 -0400 | [diff] [blame] | 434 | renderer, elementArrayBufferVk, indexType, drawCallParams.indexCount(), offset, |
Luc Ferron | a912046 | 2018-04-12 13:11:03 -0400 | [diff] [blame] | 435 | &mCurrentElementArrayBufferHandle, &mCurrentElementArrayBufferOffset)); |
| 436 | } |
Jamie Madill | b8e3966 | 2018-04-04 11:41:42 -0400 | [diff] [blame] | 437 | } |
Jamie Madill | c3755fc | 2018-04-05 08:39:13 -0400 | [diff] [blame] | 438 | |
Jamie Madill | 316c606 | 2018-05-29 10:49:45 -0400 | [diff] [blame] | 439 | ANGLE_TRY(onIndexedDraw(context, renderer, drawCallParams, commandBuffer, newCommandBuffer)); |
Jamie Madill | 22f12fe | 2018-04-08 14:23:40 -0400 | [diff] [blame] | 440 | vk::LineLoopHelper::Draw(drawCallParams.indexCount(), commandBuffer); |
Jamie Madill | c3755fc | 2018-04-05 08:39:13 -0400 | [diff] [blame] | 441 | |
| 442 | return gl::NoError(); |
| 443 | } |
| 444 | |
| 445 | gl::Error VertexArrayVk::onDraw(const gl::Context *context, |
| 446 | RendererVk *renderer, |
| 447 | const gl::DrawCallParams &drawCallParams, |
Jamie Madill | 316c606 | 2018-05-29 10:49:45 -0400 | [diff] [blame] | 448 | vk::CommandBuffer *commandBuffer, |
Jamie Madill | c3755fc | 2018-04-05 08:39:13 -0400 | [diff] [blame] | 449 | bool newCommandBuffer) |
| 450 | { |
| 451 | const gl::State &state = context->getGLState(); |
| 452 | const gl::Program *programGL = state.getProgram(); |
Jamie Madill | bcef322 | 2018-04-13 15:19:11 -0400 | [diff] [blame] | 453 | const gl::AttributesMask &clientAttribs = mState.getEnabledClientMemoryAttribsMask(); |
Jamie Madill | c3755fc | 2018-04-05 08:39:13 -0400 | [diff] [blame] | 454 | const gl::AttributesMask &activeAttribs = programGL->getActiveAttribLocationsMask(); |
| 455 | uint32_t maxAttrib = programGL->getState().getMaxActiveAttribLocation(); |
| 456 | |
Jamie Madill | bcef322 | 2018-04-13 15:19:11 -0400 | [diff] [blame] | 457 | if (clientAttribs.any()) |
Jamie Madill | c3755fc | 2018-04-05 08:39:13 -0400 | [diff] [blame] | 458 | { |
Jamie Madill | bcef322 | 2018-04-13 15:19:11 -0400 | [diff] [blame] | 459 | const gl::AttributesMask &attribsToStream = (clientAttribs & activeAttribs); |
Jamie Madill | c3755fc | 2018-04-05 08:39:13 -0400 | [diff] [blame] | 460 | if (attribsToStream.any()) |
| 461 | { |
| 462 | ANGLE_TRY(drawCallParams.ensureIndexRangeResolved(context)); |
| 463 | ANGLE_TRY(streamVertexData(renderer, attribsToStream, drawCallParams)); |
Jamie Madill | c3755fc | 2018-04-05 08:39:13 -0400 | [diff] [blame] | 464 | commandBuffer->bindVertexBuffers(0, maxAttrib, mCurrentArrayBufferHandles.data(), |
| 465 | mCurrentArrayBufferOffsets.data()); |
| 466 | } |
| 467 | } |
| 468 | else if (mVertexBuffersDirty || newCommandBuffer) |
| 469 | { |
Luc Ferron | 4416247 | 2018-04-06 13:20:45 -0400 | [diff] [blame] | 470 | if (maxAttrib > 0) |
| 471 | { |
Luc Ferron | 4416247 | 2018-04-06 13:20:45 -0400 | [diff] [blame] | 472 | commandBuffer->bindVertexBuffers(0, maxAttrib, mCurrentArrayBufferHandles.data(), |
| 473 | mCurrentArrayBufferOffsets.data()); |
Jamie Madill | 316c606 | 2018-05-29 10:49:45 -0400 | [diff] [blame] | 474 | |
| 475 | vk::CommandGraphResource *drawFramebuffer = vk::GetImpl(state.getDrawFramebuffer()); |
| 476 | updateArrayBufferReadDependencies(drawFramebuffer, activeAttribs, |
Luc Ferron | 4416247 | 2018-04-06 13:20:45 -0400 | [diff] [blame] | 477 | renderer->getCurrentQueueSerial()); |
| 478 | } |
Luc Ferron | c1e0268 | 2018-04-11 11:02:55 -0400 | [diff] [blame] | 479 | |
Jamie Madill | c3755fc | 2018-04-05 08:39:13 -0400 | [diff] [blame] | 480 | mVertexBuffersDirty = false; |
Luc Ferron | c1e0268 | 2018-04-11 11:02:55 -0400 | [diff] [blame] | 481 | |
| 482 | // This forces the binding to happen if we follow a drawElement call from a drawArrays call. |
| 483 | mIndexBufferDirty = true; |
Luc Ferron | 983c429 | 2018-04-10 13:05:45 -0400 | [diff] [blame] | 484 | |
| 485 | // If we've had a drawElements call with a line loop before, we want to make sure this is |
| 486 | // invalidated the next time drawElements is called since we use the same index buffer for |
| 487 | // both calls. |
| 488 | mDirtyLineLoopTranslation = true; |
Jamie Madill | c3755fc | 2018-04-05 08:39:13 -0400 | [diff] [blame] | 489 | } |
| 490 | |
| 491 | return gl::NoError(); |
| 492 | } |
| 493 | |
| 494 | gl::Error VertexArrayVk::onIndexedDraw(const gl::Context *context, |
| 495 | RendererVk *renderer, |
| 496 | const gl::DrawCallParams &drawCallParams, |
Jamie Madill | 316c606 | 2018-05-29 10:49:45 -0400 | [diff] [blame] | 497 | vk::CommandBuffer *commandBuffer, |
Jamie Madill | c3755fc | 2018-04-05 08:39:13 -0400 | [diff] [blame] | 498 | bool newCommandBuffer) |
| 499 | { |
Jamie Madill | 316c606 | 2018-05-29 10:49:45 -0400 | [diff] [blame] | 500 | ANGLE_TRY(onDraw(context, renderer, drawCallParams, commandBuffer, newCommandBuffer)); |
Luc Ferron | 49aacad | 2018-06-12 08:33:59 -0400 | [diff] [blame] | 501 | bool isLineLoop = drawCallParams.mode() == gl::PrimitiveMode::LineLoop; |
Luc Ferron | 6ed167a | 2018-06-13 13:45:55 -0400 | [diff] [blame] | 502 | gl::Buffer *glBuffer = mState.getElementArrayBuffer().get(); |
| 503 | uintptr_t offset = |
| 504 | glBuffer && !isLineLoop ? reinterpret_cast<uintptr_t>(drawCallParams.indices()) : 0; |
Jamie Madill | c3755fc | 2018-04-05 08:39:13 -0400 | [diff] [blame] | 505 | |
Luc Ferron | 6ed167a | 2018-06-13 13:45:55 -0400 | [diff] [blame] | 506 | if (!glBuffer && !isLineLoop) |
Jamie Madill | c3755fc | 2018-04-05 08:39:13 -0400 | [diff] [blame] | 507 | { |
| 508 | ANGLE_TRY(drawCallParams.ensureIndexRangeResolved(context)); |
| 509 | ANGLE_TRY(streamIndexData(renderer, drawCallParams)); |
Jamie Madill | c3755fc | 2018-04-05 08:39:13 -0400 | [diff] [blame] | 510 | commandBuffer->bindIndexBuffer(mCurrentElementArrayBufferHandle, |
| 511 | mCurrentElementArrayBufferOffset, |
| 512 | gl_vk::GetIndexType(drawCallParams.type())); |
| 513 | } |
Luc Ferron | 49aacad | 2018-06-12 08:33:59 -0400 | [diff] [blame] | 514 | else if (mIndexBufferDirty || newCommandBuffer || offset != mLastIndexBufferOffset) |
Jamie Madill | c3755fc | 2018-04-05 08:39:13 -0400 | [diff] [blame] | 515 | { |
Luc Ferron | 3e31380 | 2018-06-11 14:44:33 -0400 | [diff] [blame] | 516 | if (drawCallParams.type() == GL_UNSIGNED_BYTE && |
| 517 | drawCallParams.mode() != gl::PrimitiveMode::LineLoop) |
Jamie Madill | c3755fc | 2018-04-05 08:39:13 -0400 | [diff] [blame] | 518 | { |
Luc Ferron | 6ed167a | 2018-06-13 13:45:55 -0400 | [diff] [blame] | 519 | // Unsigned bytes don't have direct support in Vulkan so we have to expand the |
| 520 | // memory to a GLushort. |
| 521 | BufferVk *bufferVk = vk::GetImpl(glBuffer); |
| 522 | void *srcDataMapping = nullptr; |
| 523 | ASSERT(!glBuffer->isMapped()); |
| 524 | ANGLE_TRY(bufferVk->map(context, 0, &srcDataMapping)); |
Rafael Cintron | 05a449a | 2018-06-20 18:08:04 -0700 | [diff] [blame^] | 525 | uint8_t *srcData = static_cast<uint8_t *>(srcDataMapping); |
Luc Ferron | 6ed167a | 2018-06-13 13:45:55 -0400 | [diff] [blame] | 526 | intptr_t offsetIntoSrcData = reinterpret_cast<intptr_t>(drawCallParams.indices()); |
| 527 | srcData += offsetIntoSrcData; |
| 528 | |
| 529 | // Allocate a new buffer that's double the size of the buffer provided by the user to |
| 530 | // go from unsigned byte to unsigned short. |
| 531 | uint8_t *allocatedData = nullptr; |
| 532 | bool newBufferAllocated = false; |
| 533 | uint32_t expandedDataOffset = 0; |
| 534 | mTranslatedByteIndexData.allocate( |
| 535 | renderer, static_cast<size_t>(bufferVk->getSize()) * 2, &allocatedData, |
| 536 | &mCurrentElementArrayBufferHandle, &expandedDataOffset, &newBufferAllocated); |
| 537 | mCurrentElementArrayBufferOffset = static_cast<VkDeviceSize>(expandedDataOffset); |
| 538 | |
| 539 | // Expand the source into the destination |
| 540 | ASSERT(!context->getGLState().isPrimitiveRestartEnabled()); |
| 541 | uint16_t *expandedDst = reinterpret_cast<uint16_t *>(allocatedData); |
| 542 | for (GLsizei index = 0; index < bufferVk->getSize() - offsetIntoSrcData; index++) |
| 543 | { |
| 544 | expandedDst[index] = static_cast<GLushort>(srcData[index]); |
| 545 | } |
| 546 | |
| 547 | // Make sure our writes are available. |
| 548 | mTranslatedByteIndexData.flush(renderer->getDevice()); |
| 549 | GLboolean result = false; |
| 550 | ANGLE_TRY(bufferVk->unmap(context, &result)); |
| 551 | |
| 552 | // We do not add the offset from the drawCallParams here because we've already copied |
| 553 | // the source starting at the offset requested. |
| 554 | commandBuffer->bindIndexBuffer(mCurrentElementArrayBufferHandle, |
| 555 | mCurrentElementArrayBufferOffset, |
| 556 | gl_vk::GetIndexType(drawCallParams.type())); |
| 557 | } |
| 558 | else |
| 559 | { |
| 560 | commandBuffer->bindIndexBuffer(mCurrentElementArrayBufferHandle, |
| 561 | mCurrentElementArrayBufferOffset + offset, |
| 562 | gl_vk::GetIndexType(drawCallParams.type())); |
Jamie Madill | c3755fc | 2018-04-05 08:39:13 -0400 | [diff] [blame] | 563 | } |
| 564 | |
Luc Ferron | 49aacad | 2018-06-12 08:33:59 -0400 | [diff] [blame] | 565 | mLastIndexBufferOffset = offset; |
Jamie Madill | 316c606 | 2018-05-29 10:49:45 -0400 | [diff] [blame] | 566 | |
| 567 | const gl::State &glState = context->getGLState(); |
| 568 | vk::CommandGraphResource *drawFramebuffer = vk::GetImpl(glState.getDrawFramebuffer()); |
| 569 | updateElementArrayBufferReadDependency(drawFramebuffer, renderer->getCurrentQueueSerial()); |
Jamie Madill | c3755fc | 2018-04-05 08:39:13 -0400 | [diff] [blame] | 570 | mIndexBufferDirty = false; |
Luc Ferron | 983c429 | 2018-04-10 13:05:45 -0400 | [diff] [blame] | 571 | |
| 572 | // If we've had a drawArrays call with a line loop before, we want to make sure this is |
| 573 | // invalidated the next time drawArrays is called since we use the same index buffer for |
| 574 | // both calls. |
| 575 | mLineLoopBufferFirstIndex.reset(); |
| 576 | mLineLoopBufferLastIndex.reset(); |
Jamie Madill | c3755fc | 2018-04-05 08:39:13 -0400 | [diff] [blame] | 577 | } |
| 578 | |
| 579 | return gl::NoError(); |
| 580 | } |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 581 | } // namespace rx |