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