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