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(); |
| 61 | |
| 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 | |
Frank Henigman | 1744895 | 2017-01-05 15:48:26 -0500 | [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); |
| 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())); |
| 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())); |
| 142 | |
| 143 | mCurrentElementArrayBufferOffset = offset; |
Frank Henigman | 1744895 | 2017-01-05 15:48:26 -0500 | [diff] [blame] | 144 | return gl::NoError(); |
| 145 | } |
| 146 | |
Frank Henigman | 0af5b86 | 2018-03-27 20:19:33 -0400 | [diff] [blame] | 147 | gl::Error VertexArrayVk::syncState(const gl::Context *context, |
| 148 | const gl::VertexArray::DirtyBits &dirtyBits, |
| 149 | const gl::VertexArray::DirtyAttribBitsArray &attribBits, |
| 150 | const gl::VertexArray::DirtyBindingBitsArray &bindingBits) |
Jamie Madill | dd43e6c | 2017-03-24 14:18:49 -0400 | [diff] [blame] | 151 | { |
| 152 | ASSERT(dirtyBits.any()); |
Jamie Madill | 7210656 | 2017-03-24 14:18:50 -0400 | [diff] [blame] | 153 | |
Jamie Madill | c3755fc | 2018-04-05 08:39:13 -0400 | [diff] [blame] | 154 | bool invalidatePipeline = false; |
| 155 | |
Jamie Madill | bd159f0 | 2017-10-09 19:39:06 -0400 | [diff] [blame] | 156 | // Invalidate current pipeline. |
Jamie Madill | acf2f3a | 2017-11-21 19:22:44 -0500 | [diff] [blame] | 157 | ContextVk *contextVk = vk::GetImpl(context); |
Jamie Madill | bd159f0 | 2017-10-09 19:39:06 -0400 | [diff] [blame] | 158 | |
| 159 | // Rebuild current attribute buffers cache. This will fail horribly if the buffer changes. |
| 160 | // TODO(jmadill): Handle buffer storage changes. |
| 161 | const auto &attribs = mState.getVertexAttributes(); |
| 162 | const auto &bindings = mState.getVertexBindings(); |
| 163 | |
Jamie Madill | 0946393 | 2018-04-04 05:26:59 -0400 | [diff] [blame] | 164 | for (size_t dirtyBit : dirtyBits) |
Jamie Madill | bd159f0 | 2017-10-09 19:39:06 -0400 | [diff] [blame] | 165 | { |
Jamie Madill | 0946393 | 2018-04-04 05:26:59 -0400 | [diff] [blame] | 166 | switch (dirtyBit) |
Jamie Madill | da854a2 | 2017-11-30 17:24:21 -0500 | [diff] [blame] | 167 | { |
Jamie Madill | 0946393 | 2018-04-04 05:26:59 -0400 | [diff] [blame] | 168 | case gl::VertexArray::DIRTY_BIT_ELEMENT_ARRAY_BUFFER: |
Jamie Madill | da854a2 | 2017-11-30 17:24:21 -0500 | [diff] [blame] | 169 | { |
Jamie Madill | 0946393 | 2018-04-04 05:26:59 -0400 | [diff] [blame] | 170 | gl::Buffer *bufferGL = mState.getElementArrayBuffer().get(); |
| 171 | if (bufferGL) |
| 172 | { |
Jamie Madill | c3755fc | 2018-04-05 08:39:13 -0400 | [diff] [blame] | 173 | BufferVk *bufferVk = vk::GetImpl(bufferGL); |
| 174 | mCurrentElementArrayBufferResource = bufferVk; |
| 175 | mCurrentElementArrayBufferHandle = bufferVk->getVkBuffer().getHandle(); |
| 176 | mCurrentElementArrayBufferOffset = 0; |
Jamie Madill | 0946393 | 2018-04-04 05:26:59 -0400 | [diff] [blame] | 177 | } |
| 178 | else |
| 179 | { |
| 180 | mCurrentElementArrayBufferResource = nullptr; |
Jamie Madill | c3755fc | 2018-04-05 08:39:13 -0400 | [diff] [blame] | 181 | mCurrentElementArrayBufferHandle = VK_NULL_HANDLE; |
| 182 | mCurrentElementArrayBufferOffset = 0; |
Jamie Madill | 0946393 | 2018-04-04 05:26:59 -0400 | [diff] [blame] | 183 | } |
Jamie Madill | b8e3966 | 2018-04-04 11:41:42 -0400 | [diff] [blame] | 184 | mIndexBufferDirty = true; |
| 185 | mDirtyLineLoopTranslation = true; |
Jamie Madill | 0946393 | 2018-04-04 05:26:59 -0400 | [diff] [blame] | 186 | break; |
Jamie Madill | da854a2 | 2017-11-30 17:24:21 -0500 | [diff] [blame] | 187 | } |
Jamie Madill | 0946393 | 2018-04-04 05:26:59 -0400 | [diff] [blame] | 188 | |
| 189 | case gl::VertexArray::DIRTY_BIT_ELEMENT_ARRAY_BUFFER_DATA: |
Jamie Madill | c3755fc | 2018-04-05 08:39:13 -0400 | [diff] [blame] | 190 | mLineLoopBufferFirstIndex.reset(); |
| 191 | mLineLoopBufferLastIndex.reset(); |
Jamie Madill | b8e3966 | 2018-04-04 11:41:42 -0400 | [diff] [blame] | 192 | mDirtyLineLoopTranslation = true; |
Jamie Madill | 0946393 | 2018-04-04 05:26:59 -0400 | [diff] [blame] | 193 | break; |
| 194 | |
| 195 | default: |
Jamie Madill | da854a2 | 2017-11-30 17:24:21 -0500 | [diff] [blame] | 196 | { |
Jamie Madill | 0946393 | 2018-04-04 05:26:59 -0400 | [diff] [blame] | 197 | size_t attribIndex = gl::VertexArray::GetVertexIndexFromDirtyBit(dirtyBit); |
| 198 | |
| 199 | // Invalidate the input description for pipelines. |
| 200 | mDirtyPackedInputs.set(attribIndex); |
| 201 | |
| 202 | const auto &attrib = attribs[attribIndex]; |
| 203 | const auto &binding = bindings[attrib.bindingIndex]; |
| 204 | |
| 205 | if (attrib.enabled) |
| 206 | { |
| 207 | gl::Buffer *bufferGL = binding.getBuffer().get(); |
| 208 | |
| 209 | if (bufferGL) |
| 210 | { |
| 211 | BufferVk *bufferVk = vk::GetImpl(bufferGL); |
| 212 | mCurrentArrayBufferResources[attribIndex] = bufferVk; |
| 213 | mCurrentArrayBufferHandles[attribIndex] = |
| 214 | bufferVk->getVkBuffer().getHandle(); |
| 215 | mClientMemoryAttribs.reset(attribIndex); |
| 216 | } |
| 217 | else |
| 218 | { |
| 219 | mCurrentArrayBufferResources[attribIndex] = nullptr; |
| 220 | mCurrentArrayBufferHandles[attribIndex] = VK_NULL_HANDLE; |
| 221 | mClientMemoryAttribs.set(attribIndex); |
| 222 | } |
| 223 | // TODO(jmadill): Offset handling. Assume zero for now. |
| 224 | mCurrentArrayBufferOffsets[attribIndex] = 0; |
| 225 | } |
| 226 | else |
| 227 | { |
| 228 | mClientMemoryAttribs.reset(attribIndex); |
| 229 | UNIMPLEMENTED(); |
| 230 | } |
Jamie Madill | c3755fc | 2018-04-05 08:39:13 -0400 | [diff] [blame] | 231 | |
| 232 | invalidatePipeline = true; |
Jamie Madill | 0946393 | 2018-04-04 05:26:59 -0400 | [diff] [blame] | 233 | break; |
Jamie Madill | da854a2 | 2017-11-30 17:24:21 -0500 | [diff] [blame] | 234 | } |
Jamie Madill | bd159f0 | 2017-10-09 19:39:06 -0400 | [diff] [blame] | 235 | } |
| 236 | } |
Frank Henigman | 0af5b86 | 2018-03-27 20:19:33 -0400 | [diff] [blame] | 237 | |
Jamie Madill | c3755fc | 2018-04-05 08:39:13 -0400 | [diff] [blame] | 238 | if (invalidatePipeline) |
| 239 | { |
| 240 | mVertexBuffersDirty = true; |
| 241 | contextVk->invalidateCurrentPipeline(); |
| 242 | } |
| 243 | |
Frank Henigman | 0af5b86 | 2018-03-27 20:19:33 -0400 | [diff] [blame] | 244 | return gl::NoError(); |
Jamie Madill | bd159f0 | 2017-10-09 19:39:06 -0400 | [diff] [blame] | 245 | } |
| 246 | |
Jamie Madill | da854a2 | 2017-11-30 17:24:21 -0500 | [diff] [blame] | 247 | const gl::AttribArray<VkBuffer> &VertexArrayVk::getCurrentArrayBufferHandles() const |
Jamie Madill | bd159f0 | 2017-10-09 19:39:06 -0400 | [diff] [blame] | 248 | { |
Jamie Madill | da854a2 | 2017-11-30 17:24:21 -0500 | [diff] [blame] | 249 | return mCurrentArrayBufferHandles; |
Jamie Madill | bd159f0 | 2017-10-09 19:39:06 -0400 | [diff] [blame] | 250 | } |
| 251 | |
Frank Henigman | 1744895 | 2017-01-05 15:48:26 -0500 | [diff] [blame] | 252 | const gl::AttribArray<VkDeviceSize> &VertexArrayVk::getCurrentArrayBufferOffsets() const |
| 253 | { |
| 254 | return mCurrentArrayBufferOffsets; |
| 255 | } |
| 256 | |
Jamie Madill | 9cceac4 | 2018-03-31 14:19:16 -0400 | [diff] [blame] | 257 | void VertexArrayVk::updateArrayBufferReadDependencies(vk::CommandGraphNode *readingNode, |
Jamie Madill | c3755fc | 2018-04-05 08:39:13 -0400 | [diff] [blame] | 258 | const gl::AttributesMask &activeAttribsMask, |
| 259 | Serial serial) |
Jamie Madill | bd159f0 | 2017-10-09 19:39:06 -0400 | [diff] [blame] | 260 | { |
Jamie Madill | da854a2 | 2017-11-30 17:24:21 -0500 | [diff] [blame] | 261 | // Handle the bound array buffers. |
Jamie Madill | c3755fc | 2018-04-05 08:39:13 -0400 | [diff] [blame] | 262 | for (size_t attribIndex : activeAttribsMask) |
Jamie Madill | bd159f0 | 2017-10-09 19:39:06 -0400 | [diff] [blame] | 263 | { |
Frank Henigman | 1744895 | 2017-01-05 15:48:26 -0500 | [diff] [blame] | 264 | if (mCurrentArrayBufferResources[attribIndex]) |
Jamie Madill | 9cceac4 | 2018-03-31 14:19:16 -0400 | [diff] [blame] | 265 | mCurrentArrayBufferResources[attribIndex]->onReadResource(readingNode, serial); |
Jamie Madill | da854a2 | 2017-11-30 17:24:21 -0500 | [diff] [blame] | 266 | } |
Jamie Madill | c3755fc | 2018-04-05 08:39:13 -0400 | [diff] [blame] | 267 | } |
Jamie Madill | da854a2 | 2017-11-30 17:24:21 -0500 | [diff] [blame] | 268 | |
Jamie Madill | 9cceac4 | 2018-03-31 14:19:16 -0400 | [diff] [blame] | 269 | void VertexArrayVk::updateElementArrayBufferReadDependency(vk::CommandGraphNode *readingNode, |
Jamie Madill | c3755fc | 2018-04-05 08:39:13 -0400 | [diff] [blame] | 270 | Serial serial) |
| 271 | { |
Jamie Madill | da854a2 | 2017-11-30 17:24:21 -0500 | [diff] [blame] | 272 | // Handle the bound element array buffer. |
Jamie Madill | c3755fc | 2018-04-05 08:39:13 -0400 | [diff] [blame] | 273 | if (mCurrentElementArrayBufferResource) |
Jamie Madill | da854a2 | 2017-11-30 17:24:21 -0500 | [diff] [blame] | 274 | { |
Jamie Madill | 9cceac4 | 2018-03-31 14:19:16 -0400 | [diff] [blame] | 275 | mCurrentElementArrayBufferResource->onReadResource(readingNode, serial); |
Jamie Madill | bd159f0 | 2017-10-09 19:39:06 -0400 | [diff] [blame] | 276 | } |
Jamie Madill | dd43e6c | 2017-03-24 14:18:49 -0400 | [diff] [blame] | 277 | } |
| 278 | |
Jamie Madill | 112a3a8 | 2018-01-23 13:04:06 -0500 | [diff] [blame] | 279 | void VertexArrayVk::getPackedInputDescriptions(vk::PipelineDesc *pipelineDesc) |
Jamie Madill | ebf7299 | 2017-10-13 14:09:45 -0400 | [diff] [blame] | 280 | { |
Jamie Madill | 112a3a8 | 2018-01-23 13:04:06 -0500 | [diff] [blame] | 281 | updatePackedInputDescriptions(); |
| 282 | pipelineDesc->updateVertexInputInfo(mPackedInputBindings, mPackedInputAttributes); |
Jamie Madill | ebf7299 | 2017-10-13 14:09:45 -0400 | [diff] [blame] | 283 | } |
| 284 | |
Jamie Madill | 112a3a8 | 2018-01-23 13:04:06 -0500 | [diff] [blame] | 285 | void VertexArrayVk::updatePackedInputDescriptions() |
Jamie Madill | ebf7299 | 2017-10-13 14:09:45 -0400 | [diff] [blame] | 286 | { |
Jamie Madill | 112a3a8 | 2018-01-23 13:04:06 -0500 | [diff] [blame] | 287 | if (!mDirtyPackedInputs.any()) |
Jamie Madill | ebf7299 | 2017-10-13 14:09:45 -0400 | [diff] [blame] | 288 | { |
| 289 | return; |
| 290 | } |
| 291 | |
| 292 | const auto &attribs = mState.getVertexAttributes(); |
| 293 | const auto &bindings = mState.getVertexBindings(); |
| 294 | |
Jamie Madill | 112a3a8 | 2018-01-23 13:04:06 -0500 | [diff] [blame] | 295 | for (auto attribIndex : mDirtyPackedInputs) |
Jamie Madill | ebf7299 | 2017-10-13 14:09:45 -0400 | [diff] [blame] | 296 | { |
| 297 | const auto &attrib = attribs[attribIndex]; |
| 298 | const auto &binding = bindings[attrib.bindingIndex]; |
| 299 | if (attrib.enabled) |
| 300 | { |
Jamie Madill | 112a3a8 | 2018-01-23 13:04:06 -0500 | [diff] [blame] | 301 | updatePackedInputInfo(static_cast<uint32_t>(attribIndex), binding, attrib); |
Jamie Madill | ebf7299 | 2017-10-13 14:09:45 -0400 | [diff] [blame] | 302 | } |
| 303 | else |
| 304 | { |
| 305 | UNIMPLEMENTED(); |
| 306 | } |
| 307 | } |
| 308 | |
Jamie Madill | 112a3a8 | 2018-01-23 13:04:06 -0500 | [diff] [blame] | 309 | mDirtyPackedInputs.reset(); |
| 310 | } |
| 311 | |
| 312 | void VertexArrayVk::updatePackedInputInfo(uint32_t attribIndex, |
| 313 | const gl::VertexBinding &binding, |
| 314 | const gl::VertexAttribute &attrib) |
| 315 | { |
| 316 | vk::PackedVertexInputBindingDesc &bindingDesc = mPackedInputBindings[attribIndex]; |
| 317 | |
| 318 | size_t attribSize = gl::ComputeVertexAttributeTypeSize(attrib); |
| 319 | ASSERT(attribSize <= std::numeric_limits<uint16_t>::max()); |
| 320 | |
Frank Henigman | a8e868f | 2018-01-28 23:32:25 -0500 | [diff] [blame] | 321 | bindingDesc.stride = static_cast<uint16_t>(binding.getStride()); |
Jamie Madill | 112a3a8 | 2018-01-23 13:04:06 -0500 | [diff] [blame] | 322 | bindingDesc.inputRate = static_cast<uint16_t>( |
| 323 | binding.getDivisor() > 0 ? VK_VERTEX_INPUT_RATE_INSTANCE : VK_VERTEX_INPUT_RATE_VERTEX); |
| 324 | |
| 325 | gl::VertexFormatType vertexFormatType = gl::GetVertexFormatType(attrib); |
| 326 | VkFormat vkFormat = vk::GetNativeVertexFormat(vertexFormatType); |
| 327 | ASSERT(vkFormat <= std::numeric_limits<uint16_t>::max()); |
| 328 | |
| 329 | vk::PackedVertexInputAttributeDesc &attribDesc = mPackedInputAttributes[attribIndex]; |
| 330 | attribDesc.format = static_cast<uint16_t>(vkFormat); |
| 331 | attribDesc.location = static_cast<uint16_t>(attribIndex); |
| 332 | attribDesc.offset = static_cast<uint32_t>(ComputeVertexAttributeOffset(attrib, binding)); |
Jamie Madill | ebf7299 | 2017-10-13 14:09:45 -0400 | [diff] [blame] | 333 | } |
| 334 | |
Jamie Madill | c3755fc | 2018-04-05 08:39:13 -0400 | [diff] [blame] | 335 | gl::Error VertexArrayVk::drawArrays(const gl::Context *context, |
| 336 | RendererVk *renderer, |
| 337 | const gl::DrawCallParams &drawCallParams, |
| 338 | vk::CommandGraphNode *drawNode, |
| 339 | bool newCommandBuffer) |
| 340 | { |
| 341 | vk::CommandBuffer *commandBuffer = drawNode->getInsideRenderPassCommands(); |
| 342 | ASSERT(commandBuffer->valid()); |
| 343 | |
| 344 | ANGLE_TRY(onDraw(context, renderer, drawCallParams, drawNode, newCommandBuffer)); |
| 345 | |
| 346 | if (drawCallParams.mode() != GL_LINE_LOOP) |
| 347 | { |
| 348 | commandBuffer->draw(drawCallParams.vertexCount(), 1, drawCallParams.firstVertex(), 0); |
| 349 | return gl::NoError(); |
| 350 | } |
| 351 | |
| 352 | // Handle GL_LINE_LOOP drawArrays. |
Jamie Madill | b8e3966 | 2018-04-04 11:41:42 -0400 | [diff] [blame] | 353 | // This test may be incorrect if the draw call switches from DrawArrays/DrawElements. |
Jamie Madill | c3755fc | 2018-04-05 08:39:13 -0400 | [diff] [blame] | 354 | int lastVertex = drawCallParams.firstVertex() + drawCallParams.vertexCount(); |
| 355 | if (!mLineLoopBufferFirstIndex.valid() || !mLineLoopBufferLastIndex.valid() || |
| 356 | mLineLoopBufferFirstIndex != drawCallParams.firstVertex() || |
| 357 | mLineLoopBufferLastIndex != lastVertex) |
| 358 | { |
Jamie Madill | 22f12fe | 2018-04-08 14:23:40 -0400 | [diff] [blame^] | 359 | ANGLE_TRY(mLineLoopHelper.getIndexBufferForDrawArrays(renderer, drawCallParams, |
| 360 | &mCurrentElementArrayBufferHandle, |
| 361 | &mCurrentElementArrayBufferOffset)); |
Jamie Madill | c3755fc | 2018-04-05 08:39:13 -0400 | [diff] [blame] | 362 | |
| 363 | mLineLoopBufferFirstIndex = drawCallParams.firstVertex(); |
| 364 | mLineLoopBufferLastIndex = lastVertex; |
| 365 | } |
| 366 | |
| 367 | commandBuffer->bindIndexBuffer(mCurrentElementArrayBufferHandle, |
| 368 | mCurrentElementArrayBufferOffset, VK_INDEX_TYPE_UINT32); |
| 369 | |
Jamie Madill | 22f12fe | 2018-04-08 14:23:40 -0400 | [diff] [blame^] | 370 | vk::LineLoopHelper::Draw(drawCallParams.vertexCount(), commandBuffer); |
Jamie Madill | c3755fc | 2018-04-05 08:39:13 -0400 | [diff] [blame] | 371 | |
| 372 | return gl::NoError(); |
| 373 | } |
| 374 | |
| 375 | gl::Error VertexArrayVk::drawElements(const gl::Context *context, |
| 376 | RendererVk *renderer, |
| 377 | const gl::DrawCallParams &drawCallParams, |
| 378 | vk::CommandGraphNode *drawNode, |
| 379 | bool newCommandBuffer) |
| 380 | { |
| 381 | vk::CommandBuffer *commandBuffer = drawNode->getInsideRenderPassCommands(); |
| 382 | ASSERT(commandBuffer->valid()); |
| 383 | |
| 384 | if (drawCallParams.mode() != GL_LINE_LOOP) |
| 385 | { |
| 386 | ANGLE_TRY(onIndexedDraw(context, renderer, drawCallParams, drawNode, newCommandBuffer)); |
| 387 | commandBuffer->drawIndexed(drawCallParams.indexCount(), 1, 0, 0, 0); |
| 388 | return gl::NoError(); |
| 389 | } |
| 390 | |
| 391 | // Handle GL_LINE_LOOP drawElements. |
| 392 | gl::Buffer *elementArrayBuffer = mState.getElementArrayBuffer().get(); |
| 393 | if (!elementArrayBuffer) |
| 394 | { |
| 395 | UNIMPLEMENTED(); |
| 396 | return gl::InternalError() << "Line loop indices in client memory not supported"; |
| 397 | } |
| 398 | |
| 399 | BufferVk *elementArrayBufferVk = vk::GetImpl(elementArrayBuffer); |
| 400 | |
| 401 | VkIndexType indexType = gl_vk::GetIndexType(drawCallParams.type()); |
| 402 | |
Jamie Madill | b8e3966 | 2018-04-04 11:41:42 -0400 | [diff] [blame] | 403 | // This also doesn't check if the element type changed, which should trigger translation. |
| 404 | if (mDirtyLineLoopTranslation) |
| 405 | { |
Jamie Madill | 22f12fe | 2018-04-08 14:23:40 -0400 | [diff] [blame^] | 406 | ANGLE_TRY(mLineLoopHelper.getIndexBufferForElementArrayBuffer( |
Jamie Madill | b8e3966 | 2018-04-04 11:41:42 -0400 | [diff] [blame] | 407 | renderer, elementArrayBufferVk, indexType, drawCallParams.indexCount(), |
| 408 | &mCurrentElementArrayBufferHandle, &mCurrentElementArrayBufferOffset)); |
| 409 | } |
Jamie Madill | c3755fc | 2018-04-05 08:39:13 -0400 | [diff] [blame] | 410 | |
| 411 | ANGLE_TRY(onIndexedDraw(context, renderer, drawCallParams, drawNode, newCommandBuffer)); |
Jamie Madill | 22f12fe | 2018-04-08 14:23:40 -0400 | [diff] [blame^] | 412 | vk::LineLoopHelper::Draw(drawCallParams.indexCount(), commandBuffer); |
Jamie Madill | c3755fc | 2018-04-05 08:39:13 -0400 | [diff] [blame] | 413 | |
| 414 | return gl::NoError(); |
| 415 | } |
| 416 | |
| 417 | gl::Error VertexArrayVk::onDraw(const gl::Context *context, |
| 418 | RendererVk *renderer, |
| 419 | const gl::DrawCallParams &drawCallParams, |
| 420 | vk::CommandGraphNode *drawNode, |
| 421 | bool newCommandBuffer) |
| 422 | { |
| 423 | const gl::State &state = context->getGLState(); |
| 424 | const gl::Program *programGL = state.getProgram(); |
| 425 | const gl::AttributesMask &activeAttribs = programGL->getActiveAttribLocationsMask(); |
| 426 | uint32_t maxAttrib = programGL->getState().getMaxActiveAttribLocation(); |
| 427 | |
| 428 | if (mClientMemoryAttribs.any()) |
| 429 | { |
| 430 | const gl::AttributesMask &attribsToStream = (mClientMemoryAttribs & activeAttribs); |
| 431 | if (attribsToStream.any()) |
| 432 | { |
| 433 | ANGLE_TRY(drawCallParams.ensureIndexRangeResolved(context)); |
| 434 | ANGLE_TRY(streamVertexData(renderer, attribsToStream, drawCallParams)); |
| 435 | vk::CommandBuffer *commandBuffer = drawNode->getInsideRenderPassCommands(); |
| 436 | commandBuffer->bindVertexBuffers(0, maxAttrib, mCurrentArrayBufferHandles.data(), |
| 437 | mCurrentArrayBufferOffsets.data()); |
| 438 | } |
| 439 | } |
| 440 | else if (mVertexBuffersDirty || newCommandBuffer) |
| 441 | { |
Luc Ferron | 4416247 | 2018-04-06 13:20:45 -0400 | [diff] [blame] | 442 | if (maxAttrib > 0) |
| 443 | { |
| 444 | vk::CommandBuffer *commandBuffer = drawNode->getInsideRenderPassCommands(); |
| 445 | commandBuffer->bindVertexBuffers(0, maxAttrib, mCurrentArrayBufferHandles.data(), |
| 446 | mCurrentArrayBufferOffsets.data()); |
| 447 | updateArrayBufferReadDependencies(drawNode, activeAttribs, |
| 448 | renderer->getCurrentQueueSerial()); |
| 449 | } |
Jamie Madill | c3755fc | 2018-04-05 08:39:13 -0400 | [diff] [blame] | 450 | mVertexBuffersDirty = false; |
| 451 | } |
| 452 | |
| 453 | return gl::NoError(); |
| 454 | } |
| 455 | |
| 456 | gl::Error VertexArrayVk::onIndexedDraw(const gl::Context *context, |
| 457 | RendererVk *renderer, |
| 458 | const gl::DrawCallParams &drawCallParams, |
| 459 | vk::CommandGraphNode *drawNode, |
| 460 | bool newCommandBuffer) |
| 461 | { |
| 462 | ANGLE_TRY(onDraw(context, renderer, drawCallParams, drawNode, newCommandBuffer)); |
| 463 | |
| 464 | if (!mState.getElementArrayBuffer().get()) |
| 465 | { |
| 466 | ANGLE_TRY(drawCallParams.ensureIndexRangeResolved(context)); |
| 467 | ANGLE_TRY(streamIndexData(renderer, drawCallParams)); |
| 468 | vk::CommandBuffer *commandBuffer = drawNode->getInsideRenderPassCommands(); |
| 469 | commandBuffer->bindIndexBuffer(mCurrentElementArrayBufferHandle, |
| 470 | mCurrentElementArrayBufferOffset, |
| 471 | gl_vk::GetIndexType(drawCallParams.type())); |
| 472 | } |
| 473 | else if (mIndexBufferDirty || newCommandBuffer) |
| 474 | { |
| 475 | if (drawCallParams.type() == GL_UNSIGNED_BYTE) |
| 476 | { |
| 477 | // TODO(fjhenigman): Index format translation. |
| 478 | UNIMPLEMENTED(); |
| 479 | return gl::InternalError() |
| 480 | << "Unsigned byte translation is not implemented for indices in a buffer object"; |
| 481 | } |
| 482 | |
| 483 | vk::CommandBuffer *commandBuffer = drawNode->getInsideRenderPassCommands(); |
| 484 | commandBuffer->bindIndexBuffer(mCurrentElementArrayBufferHandle, |
| 485 | mCurrentElementArrayBufferOffset, |
| 486 | gl_vk::GetIndexType(drawCallParams.type())); |
| 487 | updateElementArrayBufferReadDependency(drawNode, renderer->getCurrentQueueSerial()); |
| 488 | mIndexBufferDirty = false; |
| 489 | } |
| 490 | |
| 491 | return gl::NoError(); |
| 492 | } |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 493 | } // namespace rx |