Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 1 | // |
| 2 | // Copyright 2016 The ANGLE Project Authors. All rights reserved. |
| 3 | // Use of this source code is governed by a BSD-style license that can be |
| 4 | // found in the LICENSE file. |
| 5 | // |
| 6 | // VertexArrayVk.cpp: |
| 7 | // Implements the class methods for VertexArrayVk. |
| 8 | // |
| 9 | |
| 10 | #include "libANGLE/renderer/vulkan/VertexArrayVk.h" |
| 11 | |
| 12 | #include "common/debug.h" |
| 13 | |
Jamie Madill | c564c07 | 2017-06-01 12:45:42 -0400 | [diff] [blame] | 14 | #include "libANGLE/Context.h" |
Jamie Madill | bd159f0 | 2017-10-09 19:39:06 -0400 | [diff] [blame] | 15 | #include "libANGLE/renderer/vulkan/BufferVk.h" |
Jamie Madill | 1f46bc1 | 2018-02-20 16:09:43 -0500 | [diff] [blame] | 16 | #include "libANGLE/renderer/vulkan/CommandGraph.h" |
Jamie Madill | dd43e6c | 2017-03-24 14:18:49 -0400 | [diff] [blame] | 17 | #include "libANGLE/renderer/vulkan/ContextVk.h" |
Jamie Madill | c3755fc | 2018-04-05 08:39:13 -0400 | [diff] [blame] | 18 | #include "libANGLE/renderer/vulkan/RendererVk.h" |
Jamie Madill | 3c424b4 | 2018-01-19 12:35:09 -0500 | [diff] [blame] | 19 | #include "libANGLE/renderer/vulkan/vk_format_utils.h" |
Jamie Madill | dd43e6c | 2017-03-24 14:18:49 -0400 | [diff] [blame] | 20 | |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 21 | namespace rx |
| 22 | { |
Jamie Madill | c3755fc | 2018-04-05 08:39:13 -0400 | [diff] [blame] | 23 | namespace |
| 24 | { |
| 25 | constexpr size_t kDynamicVertexDataSize = 1024 * 1024; |
| 26 | constexpr size_t kDynamicIndexDataSize = 1024 * 8; |
| 27 | } // anonymous namespace |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 28 | |
Luc Ferron | a9ab0f3 | 2018-05-17 17:03:55 -0400 | [diff] [blame] | 29 | VertexArrayVk::VertexArrayVk(const gl::VertexArrayState &state, RendererVk *renderer) |
Jamie Madill | bd159f0 | 2017-10-09 19:39:06 -0400 | [diff] [blame] | 30 | : VertexArrayImpl(state), |
Jamie Madill | da854a2 | 2017-11-30 17:24:21 -0500 | [diff] [blame] | 31 | mCurrentArrayBufferHandles{}, |
Frank Henigman | 1744895 | 2017-01-05 15:48:26 -0500 | [diff] [blame] | 32 | mCurrentArrayBufferOffsets{}, |
Jamie Madill | da854a2 | 2017-11-30 17:24:21 -0500 | [diff] [blame] | 33 | mCurrentArrayBufferResources{}, |
Jamie Madill | c3755fc | 2018-04-05 08:39:13 -0400 | [diff] [blame] | 34 | mCurrentElementArrayBufferHandle(VK_NULL_HANDLE), |
| 35 | mCurrentElementArrayBufferOffset(0), |
| 36 | mCurrentElementArrayBufferResource(nullptr), |
| 37 | mDynamicVertexData(VK_BUFFER_USAGE_VERTEX_BUFFER_BIT, kDynamicVertexDataSize), |
| 38 | mDynamicIndexData(VK_BUFFER_USAGE_INDEX_BUFFER_BIT, kDynamicIndexDataSize), |
Luc Ferron | a9ab0f3 | 2018-05-17 17:03:55 -0400 | [diff] [blame] | 39 | mLineLoopHelper(renderer), |
Jamie Madill | b8e3966 | 2018-04-04 11:41:42 -0400 | [diff] [blame] | 40 | mDirtyLineLoopTranslation(true), |
Jamie Madill | c3755fc | 2018-04-05 08:39:13 -0400 | [diff] [blame] | 41 | mVertexBuffersDirty(false), |
| 42 | mIndexBufferDirty(false) |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 43 | { |
Jamie Madill | da854a2 | 2017-11-30 17:24:21 -0500 | [diff] [blame] | 44 | mCurrentArrayBufferHandles.fill(VK_NULL_HANDLE); |
Frank Henigman | 1744895 | 2017-01-05 15:48:26 -0500 | [diff] [blame] | 45 | mCurrentArrayBufferOffsets.fill(0); |
Jamie Madill | da854a2 | 2017-11-30 17:24:21 -0500 | [diff] [blame] | 46 | mCurrentArrayBufferResources.fill(nullptr); |
Jamie Madill | 112a3a8 | 2018-01-23 13:04:06 -0500 | [diff] [blame] | 47 | |
| 48 | mPackedInputBindings.fill({0, 0}); |
| 49 | mPackedInputAttributes.fill({0, 0, 0}); |
Jamie Madill | c3755fc | 2018-04-05 08:39:13 -0400 | [diff] [blame] | 50 | |
Luc Ferron | a9ab0f3 | 2018-05-17 17:03:55 -0400 | [diff] [blame] | 51 | mDynamicVertexData.init(1, renderer); |
| 52 | mDynamicIndexData.init(1, renderer); |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 53 | } |
| 54 | |
Jamie Madill | acf2f3a | 2017-11-21 19:22:44 -0500 | [diff] [blame] | 55 | VertexArrayVk::~VertexArrayVk() |
| 56 | { |
| 57 | } |
| 58 | |
Jamie Madill | 4928b7c | 2017-06-20 12:57:39 -0400 | [diff] [blame] | 59 | void VertexArrayVk::destroy(const gl::Context *context) |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 60 | { |
Jamie Madill | c3755fc | 2018-04-05 08:39:13 -0400 | [diff] [blame] | 61 | VkDevice device = vk::GetImpl(context)->getRenderer()->getDevice(); |
Jamie Madill | c3755fc | 2018-04-05 08:39:13 -0400 | [diff] [blame] | 62 | mDynamicVertexData.destroy(device); |
| 63 | mDynamicIndexData.destroy(device); |
Jamie Madill | 22f12fe | 2018-04-08 14:23:40 -0400 | [diff] [blame] | 64 | mLineLoopHelper.destroy(device); |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 65 | } |
| 66 | |
Jamie Madill | c3755fc | 2018-04-05 08:39:13 -0400 | [diff] [blame] | 67 | gl::Error VertexArrayVk::streamVertexData(RendererVk *renderer, |
| 68 | const gl::AttributesMask &attribsToStream, |
Jamie Madill | 32fd63b | 2018-03-31 11:20:35 -0400 | [diff] [blame] | 69 | const gl::DrawCallParams &drawCallParams) |
Frank Henigman | 1744895 | 2017-01-05 15:48:26 -0500 | [diff] [blame] | 70 | { |
Jamie Madill | c3755fc | 2018-04-05 08:39:13 -0400 | [diff] [blame] | 71 | ASSERT(!attribsToStream.none()); |
| 72 | |
Jamie Madill | bcef322 | 2018-04-13 15:19:11 -0400 | [diff] [blame] | 73 | const auto &attribs = mState.getVertexAttributes(); |
| 74 | const auto &bindings = mState.getVertexBindings(); |
Frank Henigman | 1744895 | 2017-01-05 15:48:26 -0500 | [diff] [blame] | 75 | |
Jamie Madill | 32fd63b | 2018-03-31 11:20:35 -0400 | [diff] [blame] | 76 | const size_t lastVertex = drawCallParams.firstVertex() + drawCallParams.vertexCount(); |
| 77 | |
Frank Henigman | 1744895 | 2017-01-05 15:48:26 -0500 | [diff] [blame] | 78 | // TODO(fjhenigman): When we have a bunch of interleaved attributes, they end up |
| 79 | // un-interleaved, wasting space and copying time. Consider improving on that. |
Jamie Madill | b8e3966 | 2018-04-04 11:41:42 -0400 | [diff] [blame] | 80 | for (size_t attribIndex : attribsToStream) |
Frank Henigman | 1744895 | 2017-01-05 15:48:26 -0500 | [diff] [blame] | 81 | { |
Frank Henigman | 6dd4a92 | 2018-03-02 16:35:13 -0500 | [diff] [blame] | 82 | const gl::VertexAttribute &attrib = attribs[attribIndex]; |
| 83 | const gl::VertexBinding &binding = bindings[attrib.bindingIndex]; |
| 84 | ASSERT(attrib.enabled && binding.getBuffer().get() == nullptr); |
Frank Henigman | 1744895 | 2017-01-05 15:48:26 -0500 | [diff] [blame] | 85 | |
Frank Henigman | 6dd4a92 | 2018-03-02 16:35:13 -0500 | [diff] [blame] | 86 | // TODO(fjhenigman): Work with more formats than just GL_FLOAT. |
| 87 | if (attrib.type != GL_FLOAT) |
Frank Henigman | 1744895 | 2017-01-05 15:48:26 -0500 | [diff] [blame] | 88 | { |
Frank Henigman | 6dd4a92 | 2018-03-02 16:35:13 -0500 | [diff] [blame] | 89 | UNIMPLEMENTED(); |
| 90 | return gl::InternalError(); |
Frank Henigman | 1744895 | 2017-01-05 15:48:26 -0500 | [diff] [blame] | 91 | } |
Frank Henigman | 6dd4a92 | 2018-03-02 16:35:13 -0500 | [diff] [blame] | 92 | |
| 93 | // Only [firstVertex, lastVertex] is needed by the upcoming draw so that |
| 94 | // is all we copy, but we allocate space for [0, lastVertex] so indexing |
| 95 | // will work. If we don't start at zero all the indices will be off. |
| 96 | // TODO(fjhenigman): See if we can account for indices being off by adjusting |
| 97 | // the offset, thus avoiding wasted memory. |
Jamie Madill | 32fd63b | 2018-03-31 11:20:35 -0400 | [diff] [blame] | 98 | const size_t firstByte = drawCallParams.firstVertex() * binding.getStride(); |
Frank Henigman | 6dd4a92 | 2018-03-02 16:35:13 -0500 | [diff] [blame] | 99 | const size_t lastByte = |
| 100 | lastVertex * binding.getStride() + gl::ComputeVertexAttributeTypeSize(attrib); |
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; |
Jamie Madill | c3755fc | 2018-04-05 08:39:13 -0400 | [diff] [blame] | 103 | ANGLE_TRY(mDynamicVertexData.allocate( |
| 104 | renderer, lastByte, &dst, &mCurrentArrayBufferHandles[attribIndex], &offset, nullptr)); |
Luc Ferron | 7a06ac1 | 2018-03-15 10:17:04 -0400 | [diff] [blame] | 105 | mCurrentArrayBufferOffsets[attribIndex] = static_cast<VkDeviceSize>(offset); |
Frank Henigman | 6dd4a92 | 2018-03-02 16:35:13 -0500 | [diff] [blame] | 106 | memcpy(dst + firstByte, static_cast<const uint8_t *>(attrib.pointer) + firstByte, |
| 107 | lastByte - firstByte); |
Frank Henigman | 1744895 | 2017-01-05 15:48:26 -0500 | [diff] [blame] | 108 | } |
| 109 | |
Jamie Madill | c3755fc | 2018-04-05 08:39:13 -0400 | [diff] [blame] | 110 | ANGLE_TRY(mDynamicVertexData.flush(renderer->getDevice())); |
Luc Ferron | 6185981 | 2018-05-09 14:17:39 -0400 | [diff] [blame] | 111 | mDynamicVertexData.releaseRetainedBuffers(renderer); |
Jamie Madill | c3755fc | 2018-04-05 08:39:13 -0400 | [diff] [blame] | 112 | return gl::NoError(); |
| 113 | } |
| 114 | |
| 115 | gl::Error VertexArrayVk::streamIndexData(RendererVk *renderer, |
| 116 | const gl::DrawCallParams &drawCallParams) |
| 117 | { |
| 118 | ASSERT(!mState.getElementArrayBuffer().get()); |
| 119 | |
| 120 | uint32_t offset = 0; |
| 121 | |
| 122 | const GLsizei amount = sizeof(GLushort) * drawCallParams.indexCount(); |
| 123 | GLubyte *dst = nullptr; |
| 124 | |
| 125 | ANGLE_TRY(mDynamicIndexData.allocate(renderer, amount, &dst, &mCurrentElementArrayBufferHandle, |
| 126 | &offset, nullptr)); |
| 127 | if (drawCallParams.type() == GL_UNSIGNED_BYTE) |
| 128 | { |
| 129 | // Unsigned bytes don't have direct support in Vulkan so we have to expand the |
| 130 | // memory to a GLushort. |
| 131 | const GLubyte *in = static_cast<const GLubyte *>(drawCallParams.indices()); |
| 132 | GLushort *expandedDst = reinterpret_cast<GLushort *>(dst); |
| 133 | for (GLsizei index = 0; index < drawCallParams.indexCount(); index++) |
| 134 | { |
| 135 | expandedDst[index] = static_cast<GLushort>(in[index]); |
| 136 | } |
| 137 | } |
| 138 | else |
| 139 | { |
| 140 | memcpy(dst, drawCallParams.indices(), amount); |
| 141 | } |
| 142 | ANGLE_TRY(mDynamicIndexData.flush(renderer->getDevice())); |
Luc Ferron | 6185981 | 2018-05-09 14:17:39 -0400 | [diff] [blame] | 143 | mDynamicIndexData.releaseRetainedBuffers(renderer); |
Jamie Madill | c3755fc | 2018-04-05 08:39:13 -0400 | [diff] [blame] | 144 | mCurrentElementArrayBufferOffset = offset; |
Frank Henigman | 1744895 | 2017-01-05 15:48:26 -0500 | [diff] [blame] | 145 | return gl::NoError(); |
| 146 | } |
| 147 | |
Jamie Madill | a56467e | 2018-04-11 16:19:41 -0400 | [diff] [blame] | 148 | #define ANGLE_VERTEX_DIRTY_ATTRIB_FUNC(INDEX) \ |
| 149 | case gl::VertexArray::DIRTY_BIT_ATTRIB_0 + INDEX: \ |
| 150 | syncDirtyAttrib(attribs[INDEX], bindings[attribs[INDEX].bindingIndex], INDEX); \ |
| 151 | invalidatePipeline = true; \ |
| 152 | break; |
| 153 | |
| 154 | #define ANGLE_VERTEX_DIRTY_BINDING_FUNC(INDEX) \ |
| 155 | case gl::VertexArray::DIRTY_BIT_BINDING_0 + INDEX: \ |
| 156 | syncDirtyAttrib(attribs[INDEX], bindings[attribs[INDEX].bindingIndex], INDEX); \ |
| 157 | invalidatePipeline = true; \ |
| 158 | break; |
| 159 | |
| 160 | #define ANGLE_VERTEX_DIRTY_BUFFER_DATA_FUNC(INDEX) \ |
| 161 | case gl::VertexArray::DIRTY_BIT_BUFFER_DATA_0 + INDEX: \ |
| 162 | break; |
| 163 | |
Frank Henigman | 0af5b86 | 2018-03-27 20:19:33 -0400 | [diff] [blame] | 164 | gl::Error VertexArrayVk::syncState(const gl::Context *context, |
| 165 | const gl::VertexArray::DirtyBits &dirtyBits, |
| 166 | const gl::VertexArray::DirtyAttribBitsArray &attribBits, |
| 167 | const gl::VertexArray::DirtyBindingBitsArray &bindingBits) |
Jamie Madill | dd43e6c | 2017-03-24 14:18:49 -0400 | [diff] [blame] | 168 | { |
| 169 | ASSERT(dirtyBits.any()); |
Jamie Madill | 7210656 | 2017-03-24 14:18:50 -0400 | [diff] [blame] | 170 | |
Jamie Madill | c3755fc | 2018-04-05 08:39:13 -0400 | [diff] [blame] | 171 | bool invalidatePipeline = false; |
| 172 | |
Jamie Madill | bd159f0 | 2017-10-09 19:39:06 -0400 | [diff] [blame] | 173 | // Invalidate current pipeline. |
Jamie Madill | acf2f3a | 2017-11-21 19:22:44 -0500 | [diff] [blame] | 174 | ContextVk *contextVk = vk::GetImpl(context); |
Jamie Madill | bd159f0 | 2017-10-09 19:39:06 -0400 | [diff] [blame] | 175 | |
| 176 | // Rebuild current attribute buffers cache. This will fail horribly if the buffer changes. |
| 177 | // TODO(jmadill): Handle buffer storage changes. |
| 178 | const auto &attribs = mState.getVertexAttributes(); |
| 179 | const auto &bindings = mState.getVertexBindings(); |
| 180 | |
Jamie Madill | 0946393 | 2018-04-04 05:26:59 -0400 | [diff] [blame] | 181 | for (size_t dirtyBit : dirtyBits) |
Jamie Madill | bd159f0 | 2017-10-09 19:39:06 -0400 | [diff] [blame] | 182 | { |
Jamie Madill | 0946393 | 2018-04-04 05:26:59 -0400 | [diff] [blame] | 183 | switch (dirtyBit) |
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 | case gl::VertexArray::DIRTY_BIT_ELEMENT_ARRAY_BUFFER: |
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 | gl::Buffer *bufferGL = mState.getElementArrayBuffer().get(); |
| 188 | if (bufferGL) |
| 189 | { |
Jamie Madill | c3755fc | 2018-04-05 08:39:13 -0400 | [diff] [blame] | 190 | BufferVk *bufferVk = vk::GetImpl(bufferGL); |
| 191 | mCurrentElementArrayBufferResource = bufferVk; |
| 192 | mCurrentElementArrayBufferHandle = bufferVk->getVkBuffer().getHandle(); |
| 193 | mCurrentElementArrayBufferOffset = 0; |
Jamie Madill | 0946393 | 2018-04-04 05:26:59 -0400 | [diff] [blame] | 194 | } |
| 195 | else |
| 196 | { |
| 197 | mCurrentElementArrayBufferResource = nullptr; |
Jamie Madill | c3755fc | 2018-04-05 08:39:13 -0400 | [diff] [blame] | 198 | mCurrentElementArrayBufferHandle = VK_NULL_HANDLE; |
| 199 | mCurrentElementArrayBufferOffset = 0; |
Jamie Madill | 0946393 | 2018-04-04 05:26:59 -0400 | [diff] [blame] | 200 | } |
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(); |
Jamie Madill | a56467e | 2018-04-11 16:19:41 -0400 | [diff] [blame] | 247 | } |
| 248 | else |
| 249 | { |
| 250 | mCurrentArrayBufferResources[attribIndex] = nullptr; |
| 251 | mCurrentArrayBufferHandles[attribIndex] = VK_NULL_HANDLE; |
Jamie Madill | a56467e | 2018-04-11 16:19:41 -0400 | [diff] [blame] | 252 | } |
| 253 | // TODO(jmadill): Offset handling. Assume zero for now. |
| 254 | mCurrentArrayBufferOffsets[attribIndex] = 0; |
| 255 | } |
| 256 | else |
| 257 | { |
Jamie Madill | a56467e | 2018-04-11 16:19:41 -0400 | [diff] [blame] | 258 | UNIMPLEMENTED(); |
| 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 | 9cceac4 | 2018-03-31 14:19:16 -0400 | [diff] [blame] | 272 | void VertexArrayVk::updateArrayBufferReadDependencies(vk::CommandGraphNode *readingNode, |
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 | 9cceac4 | 2018-03-31 14:19:16 -0400 | [diff] [blame] | 280 | mCurrentArrayBufferResources[attribIndex]->onReadResource(readingNode, serial); |
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 | 9cceac4 | 2018-03-31 14:19:16 -0400 | [diff] [blame] | 284 | void VertexArrayVk::updateElementArrayBufferReadDependency(vk::CommandGraphNode *readingNode, |
Jamie Madill | c3755fc | 2018-04-05 08:39:13 -0400 | [diff] [blame] | 285 | Serial serial) |
| 286 | { |
Jamie Madill | da854a2 | 2017-11-30 17:24:21 -0500 | [diff] [blame] | 287 | // Handle the bound element array buffer. |
Jamie Madill | c3755fc | 2018-04-05 08:39:13 -0400 | [diff] [blame] | 288 | if (mCurrentElementArrayBufferResource) |
Jamie Madill | da854a2 | 2017-11-30 17:24:21 -0500 | [diff] [blame] | 289 | { |
Jamie Madill | 9cceac4 | 2018-03-31 14:19:16 -0400 | [diff] [blame] | 290 | mCurrentElementArrayBufferResource->onReadResource(readingNode, serial); |
Jamie Madill | bd159f0 | 2017-10-09 19:39:06 -0400 | [diff] [blame] | 291 | } |
Jamie Madill | dd43e6c | 2017-03-24 14:18:49 -0400 | [diff] [blame] | 292 | } |
| 293 | |
Jamie Madill | 112a3a8 | 2018-01-23 13:04:06 -0500 | [diff] [blame] | 294 | void VertexArrayVk::getPackedInputDescriptions(vk::PipelineDesc *pipelineDesc) |
Jamie Madill | ebf7299 | 2017-10-13 14:09:45 -0400 | [diff] [blame] | 295 | { |
Jamie Madill | 112a3a8 | 2018-01-23 13:04:06 -0500 | [diff] [blame] | 296 | updatePackedInputDescriptions(); |
| 297 | pipelineDesc->updateVertexInputInfo(mPackedInputBindings, mPackedInputAttributes); |
Jamie Madill | ebf7299 | 2017-10-13 14:09:45 -0400 | [diff] [blame] | 298 | } |
| 299 | |
Jamie Madill | 112a3a8 | 2018-01-23 13:04:06 -0500 | [diff] [blame] | 300 | void VertexArrayVk::updatePackedInputDescriptions() |
Jamie Madill | ebf7299 | 2017-10-13 14:09:45 -0400 | [diff] [blame] | 301 | { |
Jamie Madill | 112a3a8 | 2018-01-23 13:04:06 -0500 | [diff] [blame] | 302 | if (!mDirtyPackedInputs.any()) |
Jamie Madill | ebf7299 | 2017-10-13 14:09:45 -0400 | [diff] [blame] | 303 | { |
| 304 | return; |
| 305 | } |
| 306 | |
| 307 | const auto &attribs = mState.getVertexAttributes(); |
| 308 | const auto &bindings = mState.getVertexBindings(); |
| 309 | |
Jamie Madill | 112a3a8 | 2018-01-23 13:04:06 -0500 | [diff] [blame] | 310 | for (auto attribIndex : mDirtyPackedInputs) |
Jamie Madill | ebf7299 | 2017-10-13 14:09:45 -0400 | [diff] [blame] | 311 | { |
| 312 | const auto &attrib = attribs[attribIndex]; |
| 313 | const auto &binding = bindings[attrib.bindingIndex]; |
| 314 | if (attrib.enabled) |
| 315 | { |
Jamie Madill | 112a3a8 | 2018-01-23 13:04:06 -0500 | [diff] [blame] | 316 | updatePackedInputInfo(static_cast<uint32_t>(attribIndex), binding, attrib); |
Jamie Madill | ebf7299 | 2017-10-13 14:09:45 -0400 | [diff] [blame] | 317 | } |
| 318 | else |
| 319 | { |
| 320 | UNIMPLEMENTED(); |
| 321 | } |
| 322 | } |
| 323 | |
Jamie Madill | 112a3a8 | 2018-01-23 13:04:06 -0500 | [diff] [blame] | 324 | mDirtyPackedInputs.reset(); |
| 325 | } |
| 326 | |
| 327 | void VertexArrayVk::updatePackedInputInfo(uint32_t attribIndex, |
| 328 | const gl::VertexBinding &binding, |
| 329 | const gl::VertexAttribute &attrib) |
| 330 | { |
| 331 | vk::PackedVertexInputBindingDesc &bindingDesc = mPackedInputBindings[attribIndex]; |
| 332 | |
| 333 | size_t attribSize = gl::ComputeVertexAttributeTypeSize(attrib); |
| 334 | ASSERT(attribSize <= std::numeric_limits<uint16_t>::max()); |
| 335 | |
Frank Henigman | a8e868f | 2018-01-28 23:32:25 -0500 | [diff] [blame] | 336 | bindingDesc.stride = static_cast<uint16_t>(binding.getStride()); |
Jamie Madill | 112a3a8 | 2018-01-23 13:04:06 -0500 | [diff] [blame] | 337 | bindingDesc.inputRate = static_cast<uint16_t>( |
| 338 | binding.getDivisor() > 0 ? VK_VERTEX_INPUT_RATE_INSTANCE : VK_VERTEX_INPUT_RATE_VERTEX); |
| 339 | |
| 340 | gl::VertexFormatType vertexFormatType = gl::GetVertexFormatType(attrib); |
| 341 | VkFormat vkFormat = vk::GetNativeVertexFormat(vertexFormatType); |
| 342 | ASSERT(vkFormat <= std::numeric_limits<uint16_t>::max()); |
| 343 | |
| 344 | vk::PackedVertexInputAttributeDesc &attribDesc = mPackedInputAttributes[attribIndex]; |
| 345 | attribDesc.format = static_cast<uint16_t>(vkFormat); |
| 346 | attribDesc.location = static_cast<uint16_t>(attribIndex); |
| 347 | attribDesc.offset = static_cast<uint32_t>(ComputeVertexAttributeOffset(attrib, binding)); |
Jamie Madill | ebf7299 | 2017-10-13 14:09:45 -0400 | [diff] [blame] | 348 | } |
| 349 | |
Jamie Madill | c3755fc | 2018-04-05 08:39:13 -0400 | [diff] [blame] | 350 | gl::Error VertexArrayVk::drawArrays(const gl::Context *context, |
| 351 | RendererVk *renderer, |
| 352 | const gl::DrawCallParams &drawCallParams, |
| 353 | vk::CommandGraphNode *drawNode, |
| 354 | bool newCommandBuffer) |
| 355 | { |
| 356 | vk::CommandBuffer *commandBuffer = drawNode->getInsideRenderPassCommands(); |
| 357 | ASSERT(commandBuffer->valid()); |
| 358 | |
| 359 | ANGLE_TRY(onDraw(context, renderer, drawCallParams, drawNode, newCommandBuffer)); |
| 360 | |
Jamie Madill | 18e323a | 2018-05-11 16:54:17 -0400 | [diff] [blame] | 361 | // Note: Vertex indexes can be arbitrarily large. |
| 362 | uint32_t clampedVertexCount = drawCallParams.getClampedVertexCount<uint32_t>(); |
| 363 | |
Jamie Madill | 493f957 | 2018-05-24 19:52:15 -0400 | [diff] [blame^] | 364 | if (drawCallParams.mode() != gl::PrimitiveMode::LineLoop) |
Jamie Madill | c3755fc | 2018-04-05 08:39:13 -0400 | [diff] [blame] | 365 | { |
Jamie Madill | 18e323a | 2018-05-11 16:54:17 -0400 | [diff] [blame] | 366 | commandBuffer->draw(clampedVertexCount, 1, drawCallParams.firstVertex(), 0); |
Jamie Madill | c3755fc | 2018-04-05 08:39:13 -0400 | [diff] [blame] | 367 | return gl::NoError(); |
| 368 | } |
| 369 | |
| 370 | // Handle GL_LINE_LOOP drawArrays. |
Jamie Madill | 18e323a | 2018-05-11 16:54:17 -0400 | [diff] [blame] | 371 | size_t lastVertex = static_cast<size_t>(drawCallParams.firstVertex() + clampedVertexCount); |
Jamie Madill | c3755fc | 2018-04-05 08:39:13 -0400 | [diff] [blame] | 372 | if (!mLineLoopBufferFirstIndex.valid() || !mLineLoopBufferLastIndex.valid() || |
| 373 | mLineLoopBufferFirstIndex != drawCallParams.firstVertex() || |
| 374 | mLineLoopBufferLastIndex != lastVertex) |
| 375 | { |
Jamie Madill | 22f12fe | 2018-04-08 14:23:40 -0400 | [diff] [blame] | 376 | ANGLE_TRY(mLineLoopHelper.getIndexBufferForDrawArrays(renderer, drawCallParams, |
| 377 | &mCurrentElementArrayBufferHandle, |
| 378 | &mCurrentElementArrayBufferOffset)); |
Jamie Madill | c3755fc | 2018-04-05 08:39:13 -0400 | [diff] [blame] | 379 | |
| 380 | mLineLoopBufferFirstIndex = drawCallParams.firstVertex(); |
| 381 | mLineLoopBufferLastIndex = lastVertex; |
| 382 | } |
| 383 | |
| 384 | commandBuffer->bindIndexBuffer(mCurrentElementArrayBufferHandle, |
| 385 | mCurrentElementArrayBufferOffset, VK_INDEX_TYPE_UINT32); |
| 386 | |
Jamie Madill | 18e323a | 2018-05-11 16:54:17 -0400 | [diff] [blame] | 387 | vk::LineLoopHelper::Draw(clampedVertexCount, commandBuffer); |
Jamie Madill | c3755fc | 2018-04-05 08:39:13 -0400 | [diff] [blame] | 388 | |
| 389 | return gl::NoError(); |
| 390 | } |
| 391 | |
| 392 | gl::Error VertexArrayVk::drawElements(const gl::Context *context, |
| 393 | RendererVk *renderer, |
| 394 | const gl::DrawCallParams &drawCallParams, |
| 395 | vk::CommandGraphNode *drawNode, |
| 396 | bool newCommandBuffer) |
| 397 | { |
| 398 | vk::CommandBuffer *commandBuffer = drawNode->getInsideRenderPassCommands(); |
| 399 | ASSERT(commandBuffer->valid()); |
| 400 | |
Jamie Madill | 493f957 | 2018-05-24 19:52:15 -0400 | [diff] [blame^] | 401 | if (drawCallParams.mode() != gl::PrimitiveMode::LineLoop) |
Jamie Madill | c3755fc | 2018-04-05 08:39:13 -0400 | [diff] [blame] | 402 | { |
| 403 | ANGLE_TRY(onIndexedDraw(context, renderer, drawCallParams, drawNode, newCommandBuffer)); |
| 404 | commandBuffer->drawIndexed(drawCallParams.indexCount(), 1, 0, 0, 0); |
| 405 | return gl::NoError(); |
| 406 | } |
| 407 | |
| 408 | // Handle GL_LINE_LOOP drawElements. |
Jamie Madill | b8e3966 | 2018-04-04 11:41:42 -0400 | [diff] [blame] | 409 | if (mDirtyLineLoopTranslation) |
| 410 | { |
Luc Ferron | a912046 | 2018-04-12 13:11:03 -0400 | [diff] [blame] | 411 | gl::Buffer *elementArrayBuffer = mState.getElementArrayBuffer().get(); |
| 412 | VkIndexType indexType = gl_vk::GetIndexType(drawCallParams.type()); |
| 413 | |
| 414 | if (!elementArrayBuffer) |
| 415 | { |
| 416 | ANGLE_TRY(mLineLoopHelper.getIndexBufferForClientElementArray( |
| 417 | renderer, drawCallParams.indices(), indexType, drawCallParams.indexCount(), |
| 418 | &mCurrentElementArrayBufferHandle, &mCurrentElementArrayBufferOffset)); |
| 419 | } |
| 420 | else |
| 421 | { |
Luc Ferron | a4fa9c2 | 2018-04-13 07:00:56 -0400 | [diff] [blame] | 422 | // When using an element array buffer, 'indices' is an offset to the first element. |
| 423 | intptr_t offset = reinterpret_cast<intptr_t>(drawCallParams.indices()); |
Luc Ferron | a912046 | 2018-04-12 13:11:03 -0400 | [diff] [blame] | 424 | BufferVk *elementArrayBufferVk = vk::GetImpl(elementArrayBuffer); |
| 425 | ANGLE_TRY(mLineLoopHelper.getIndexBufferForElementArrayBuffer( |
Luc Ferron | a4fa9c2 | 2018-04-13 07:00:56 -0400 | [diff] [blame] | 426 | renderer, elementArrayBufferVk, indexType, drawCallParams.indexCount(), offset, |
Luc Ferron | a912046 | 2018-04-12 13:11:03 -0400 | [diff] [blame] | 427 | &mCurrentElementArrayBufferHandle, &mCurrentElementArrayBufferOffset)); |
| 428 | } |
Jamie Madill | b8e3966 | 2018-04-04 11:41:42 -0400 | [diff] [blame] | 429 | } |
Jamie Madill | c3755fc | 2018-04-05 08:39:13 -0400 | [diff] [blame] | 430 | |
| 431 | ANGLE_TRY(onIndexedDraw(context, renderer, drawCallParams, drawNode, newCommandBuffer)); |
Jamie Madill | 22f12fe | 2018-04-08 14:23:40 -0400 | [diff] [blame] | 432 | vk::LineLoopHelper::Draw(drawCallParams.indexCount(), commandBuffer); |
Jamie Madill | c3755fc | 2018-04-05 08:39:13 -0400 | [diff] [blame] | 433 | |
| 434 | return gl::NoError(); |
| 435 | } |
| 436 | |
| 437 | gl::Error VertexArrayVk::onDraw(const gl::Context *context, |
| 438 | RendererVk *renderer, |
| 439 | const gl::DrawCallParams &drawCallParams, |
| 440 | vk::CommandGraphNode *drawNode, |
| 441 | bool newCommandBuffer) |
| 442 | { |
| 443 | const gl::State &state = context->getGLState(); |
| 444 | const gl::Program *programGL = state.getProgram(); |
Jamie Madill | bcef322 | 2018-04-13 15:19:11 -0400 | [diff] [blame] | 445 | const gl::AttributesMask &clientAttribs = mState.getEnabledClientMemoryAttribsMask(); |
Jamie Madill | c3755fc | 2018-04-05 08:39:13 -0400 | [diff] [blame] | 446 | const gl::AttributesMask &activeAttribs = programGL->getActiveAttribLocationsMask(); |
| 447 | uint32_t maxAttrib = programGL->getState().getMaxActiveAttribLocation(); |
| 448 | |
Jamie Madill | bcef322 | 2018-04-13 15:19:11 -0400 | [diff] [blame] | 449 | if (clientAttribs.any()) |
Jamie Madill | c3755fc | 2018-04-05 08:39:13 -0400 | [diff] [blame] | 450 | { |
Jamie Madill | bcef322 | 2018-04-13 15:19:11 -0400 | [diff] [blame] | 451 | const gl::AttributesMask &attribsToStream = (clientAttribs & activeAttribs); |
Jamie Madill | c3755fc | 2018-04-05 08:39:13 -0400 | [diff] [blame] | 452 | if (attribsToStream.any()) |
| 453 | { |
| 454 | ANGLE_TRY(drawCallParams.ensureIndexRangeResolved(context)); |
| 455 | ANGLE_TRY(streamVertexData(renderer, attribsToStream, drawCallParams)); |
| 456 | vk::CommandBuffer *commandBuffer = drawNode->getInsideRenderPassCommands(); |
| 457 | commandBuffer->bindVertexBuffers(0, maxAttrib, mCurrentArrayBufferHandles.data(), |
| 458 | mCurrentArrayBufferOffsets.data()); |
| 459 | } |
| 460 | } |
| 461 | else if (mVertexBuffersDirty || newCommandBuffer) |
| 462 | { |
Luc Ferron | 4416247 | 2018-04-06 13:20:45 -0400 | [diff] [blame] | 463 | if (maxAttrib > 0) |
| 464 | { |
| 465 | vk::CommandBuffer *commandBuffer = drawNode->getInsideRenderPassCommands(); |
| 466 | commandBuffer->bindVertexBuffers(0, maxAttrib, mCurrentArrayBufferHandles.data(), |
| 467 | mCurrentArrayBufferOffsets.data()); |
| 468 | updateArrayBufferReadDependencies(drawNode, activeAttribs, |
| 469 | renderer->getCurrentQueueSerial()); |
| 470 | } |
Luc Ferron | c1e0268 | 2018-04-11 11:02:55 -0400 | [diff] [blame] | 471 | |
Jamie Madill | c3755fc | 2018-04-05 08:39:13 -0400 | [diff] [blame] | 472 | mVertexBuffersDirty = false; |
Luc Ferron | c1e0268 | 2018-04-11 11:02:55 -0400 | [diff] [blame] | 473 | |
| 474 | // This forces the binding to happen if we follow a drawElement call from a drawArrays call. |
| 475 | mIndexBufferDirty = true; |
Luc Ferron | 983c429 | 2018-04-10 13:05:45 -0400 | [diff] [blame] | 476 | |
| 477 | // If we've had a drawElements call with a line loop before, we want to make sure this is |
| 478 | // invalidated the next time drawElements is called since we use the same index buffer for |
| 479 | // both calls. |
| 480 | mDirtyLineLoopTranslation = true; |
Jamie Madill | c3755fc | 2018-04-05 08:39:13 -0400 | [diff] [blame] | 481 | } |
| 482 | |
| 483 | return gl::NoError(); |
| 484 | } |
| 485 | |
| 486 | gl::Error VertexArrayVk::onIndexedDraw(const gl::Context *context, |
| 487 | RendererVk *renderer, |
| 488 | const gl::DrawCallParams &drawCallParams, |
| 489 | vk::CommandGraphNode *drawNode, |
| 490 | bool newCommandBuffer) |
| 491 | { |
| 492 | ANGLE_TRY(onDraw(context, renderer, drawCallParams, drawNode, newCommandBuffer)); |
| 493 | |
Jamie Madill | 493f957 | 2018-05-24 19:52:15 -0400 | [diff] [blame^] | 494 | if (!mState.getElementArrayBuffer().get() && |
| 495 | drawCallParams.mode() != gl::PrimitiveMode::LineLoop) |
Jamie Madill | c3755fc | 2018-04-05 08:39:13 -0400 | [diff] [blame] | 496 | { |
| 497 | ANGLE_TRY(drawCallParams.ensureIndexRangeResolved(context)); |
| 498 | ANGLE_TRY(streamIndexData(renderer, drawCallParams)); |
| 499 | vk::CommandBuffer *commandBuffer = drawNode->getInsideRenderPassCommands(); |
| 500 | commandBuffer->bindIndexBuffer(mCurrentElementArrayBufferHandle, |
| 501 | mCurrentElementArrayBufferOffset, |
| 502 | gl_vk::GetIndexType(drawCallParams.type())); |
| 503 | } |
| 504 | else if (mIndexBufferDirty || newCommandBuffer) |
| 505 | { |
| 506 | if (drawCallParams.type() == GL_UNSIGNED_BYTE) |
| 507 | { |
| 508 | // TODO(fjhenigman): Index format translation. |
| 509 | UNIMPLEMENTED(); |
| 510 | return gl::InternalError() |
| 511 | << "Unsigned byte translation is not implemented for indices in a buffer object"; |
| 512 | } |
| 513 | |
| 514 | vk::CommandBuffer *commandBuffer = drawNode->getInsideRenderPassCommands(); |
| 515 | commandBuffer->bindIndexBuffer(mCurrentElementArrayBufferHandle, |
| 516 | mCurrentElementArrayBufferOffset, |
| 517 | gl_vk::GetIndexType(drawCallParams.type())); |
| 518 | updateElementArrayBufferReadDependency(drawNode, renderer->getCurrentQueueSerial()); |
| 519 | mIndexBufferDirty = false; |
Luc Ferron | 983c429 | 2018-04-10 13:05:45 -0400 | [diff] [blame] | 520 | |
| 521 | // If we've had a drawArrays call with a line loop before, we want to make sure this is |
| 522 | // invalidated the next time drawArrays is called since we use the same index buffer for |
| 523 | // both calls. |
| 524 | mLineLoopBufferFirstIndex.reset(); |
| 525 | mLineLoopBufferLastIndex.reset(); |
Jamie Madill | c3755fc | 2018-04-05 08:39:13 -0400 | [diff] [blame] | 526 | } |
| 527 | |
| 528 | return gl::NoError(); |
| 529 | } |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 530 | } // namespace rx |