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