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