Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 1 | // |
| 2 | // Copyright 2016 The ANGLE Project Authors. All rights reserved. |
| 3 | // Use of this source code is governed by a BSD-style license that can be |
| 4 | // found in the LICENSE file. |
| 5 | // |
| 6 | // VertexArrayVk.cpp: |
| 7 | // Implements the class methods for VertexArrayVk. |
| 8 | // |
| 9 | |
| 10 | #include "libANGLE/renderer/vulkan/VertexArrayVk.h" |
| 11 | |
| 12 | #include "common/debug.h" |
| 13 | |
Jamie Madill | c564c07 | 2017-06-01 12:45:42 -0400 | [diff] [blame] | 14 | #include "libANGLE/Context.h" |
Jamie Madill | bd159f0 | 2017-10-09 19:39:06 -0400 | [diff] [blame] | 15 | #include "libANGLE/renderer/vulkan/BufferVk.h" |
Jamie Madill | 1f46bc1 | 2018-02-20 16:09:43 -0500 | [diff] [blame] | 16 | #include "libANGLE/renderer/vulkan/CommandGraph.h" |
Jamie Madill | dd43e6c | 2017-03-24 14:18:49 -0400 | [diff] [blame] | 17 | #include "libANGLE/renderer/vulkan/ContextVk.h" |
Jamie Madill | 316c606 | 2018-05-29 10:49:45 -0400 | [diff] [blame] | 18 | #include "libANGLE/renderer/vulkan/FramebufferVk.h" |
Jamie Madill | c3755fc | 2018-04-05 08:39:13 -0400 | [diff] [blame] | 19 | #include "libANGLE/renderer/vulkan/RendererVk.h" |
Jamie Madill | 3c424b4 | 2018-01-19 12:35:09 -0500 | [diff] [blame] | 20 | #include "libANGLE/renderer/vulkan/vk_format_utils.h" |
Jamie Madill | dd43e6c | 2017-03-24 14:18:49 -0400 | [diff] [blame] | 21 | |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 22 | namespace rx |
| 23 | { |
Jamie Madill | c3755fc | 2018-04-05 08:39:13 -0400 | [diff] [blame] | 24 | namespace |
| 25 | { |
| 26 | constexpr size_t kDynamicVertexDataSize = 1024 * 1024; |
| 27 | constexpr size_t kDynamicIndexDataSize = 1024 * 8; |
Frank Henigman | e452382 | 2018-07-19 16:10:53 -0400 | [diff] [blame^] | 28 | |
| 29 | bool BindingIsAligned(const gl::VertexBinding &binding, unsigned componentSize) |
| 30 | { |
| 31 | return (binding.getOffset() % componentSize == 0) && (binding.getStride() % componentSize == 0); |
| 32 | } |
Jamie Madill | c3755fc | 2018-04-05 08:39:13 -0400 | [diff] [blame] | 33 | } // anonymous namespace |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 34 | |
Frank Henigman | e452382 | 2018-07-19 16:10:53 -0400 | [diff] [blame^] | 35 | #define INIT \ |
| 36 | { \ |
| 37 | VK_BUFFER_USAGE_VERTEX_BUFFER_BIT, 1024 * 8 \ |
| 38 | } |
| 39 | |
Luc Ferron | a9ab0f3 | 2018-05-17 17:03:55 -0400 | [diff] [blame] | 40 | VertexArrayVk::VertexArrayVk(const gl::VertexArrayState &state, RendererVk *renderer) |
Jamie Madill | bd159f0 | 2017-10-09 19:39:06 -0400 | [diff] [blame] | 41 | : VertexArrayImpl(state), |
Jamie Madill | da854a2 | 2017-11-30 17:24:21 -0500 | [diff] [blame] | 42 | mCurrentArrayBufferHandles{}, |
Frank Henigman | 1744895 | 2017-01-05 15:48:26 -0500 | [diff] [blame] | 43 | mCurrentArrayBufferOffsets{}, |
Jamie Madill | da854a2 | 2017-11-30 17:24:21 -0500 | [diff] [blame] | 44 | mCurrentArrayBufferResources{}, |
Frank Henigman | 419acc8 | 2018-06-24 19:57:31 -0400 | [diff] [blame] | 45 | mCurrentArrayBufferFormats{}, |
| 46 | mCurrentArrayBufferStrides{}, |
Frank Henigman | e452382 | 2018-07-19 16:10:53 -0400 | [diff] [blame^] | 47 | mCurrentArrayBufferConversion{{ |
| 48 | INIT, INIT, INIT, INIT, INIT, INIT, INIT, INIT, INIT, INIT, INIT, INIT, INIT, INIT, INIT, |
| 49 | INIT, |
| 50 | }}, |
| 51 | mCurrentArrayBufferConversionCanRelease{}, |
Jamie Madill | c3755fc | 2018-04-05 08:39:13 -0400 | [diff] [blame] | 52 | mCurrentElementArrayBufferHandle(VK_NULL_HANDLE), |
| 53 | mCurrentElementArrayBufferOffset(0), |
| 54 | mCurrentElementArrayBufferResource(nullptr), |
| 55 | mDynamicVertexData(VK_BUFFER_USAGE_VERTEX_BUFFER_BIT, kDynamicVertexDataSize), |
| 56 | mDynamicIndexData(VK_BUFFER_USAGE_INDEX_BUFFER_BIT, kDynamicIndexDataSize), |
Luc Ferron | 6ed167a | 2018-06-13 13:45:55 -0400 | [diff] [blame] | 57 | mTranslatedByteIndexData(VK_BUFFER_USAGE_INDEX_BUFFER_BIT, kDynamicIndexDataSize), |
Luc Ferron | a9ab0f3 | 2018-05-17 17:03:55 -0400 | [diff] [blame] | 58 | mLineLoopHelper(renderer), |
Jamie Madill | b8e3966 | 2018-04-04 11:41:42 -0400 | [diff] [blame] | 59 | mDirtyLineLoopTranslation(true), |
Jamie Madill | c3755fc | 2018-04-05 08:39:13 -0400 | [diff] [blame] | 60 | mVertexBuffersDirty(false), |
Luc Ferron | 49aacad | 2018-06-12 08:33:59 -0400 | [diff] [blame] | 61 | mIndexBufferDirty(false), |
| 62 | mLastIndexBufferOffset(0) |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 63 | { |
Jamie Madill | da854a2 | 2017-11-30 17:24:21 -0500 | [diff] [blame] | 64 | mCurrentArrayBufferHandles.fill(VK_NULL_HANDLE); |
Frank Henigman | 1744895 | 2017-01-05 15:48:26 -0500 | [diff] [blame] | 65 | mCurrentArrayBufferOffsets.fill(0); |
Jamie Madill | da854a2 | 2017-11-30 17:24:21 -0500 | [diff] [blame] | 66 | mCurrentArrayBufferResources.fill(nullptr); |
Jamie Madill | 112a3a8 | 2018-01-23 13:04:06 -0500 | [diff] [blame] | 67 | |
| 68 | mPackedInputBindings.fill({0, 0}); |
| 69 | mPackedInputAttributes.fill({0, 0, 0}); |
Jamie Madill | c3755fc | 2018-04-05 08:39:13 -0400 | [diff] [blame] | 70 | |
Frank Henigman | e452382 | 2018-07-19 16:10:53 -0400 | [diff] [blame^] | 71 | for (vk::DynamicBuffer &buffer : mCurrentArrayBufferConversion) |
| 72 | { |
| 73 | buffer.init(1, renderer); |
| 74 | } |
Luc Ferron | a9ab0f3 | 2018-05-17 17:03:55 -0400 | [diff] [blame] | 75 | mDynamicVertexData.init(1, renderer); |
| 76 | mDynamicIndexData.init(1, renderer); |
Luc Ferron | 6ed167a | 2018-06-13 13:45:55 -0400 | [diff] [blame] | 77 | mTranslatedByteIndexData.init(1, renderer); |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 78 | } |
| 79 | |
Jamie Madill | acf2f3a | 2017-11-21 19:22:44 -0500 | [diff] [blame] | 80 | VertexArrayVk::~VertexArrayVk() |
| 81 | { |
| 82 | } |
| 83 | |
Jamie Madill | 4928b7c | 2017-06-20 12:57:39 -0400 | [diff] [blame] | 84 | void VertexArrayVk::destroy(const gl::Context *context) |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 85 | { |
Jamie Madill | c3755fc | 2018-04-05 08:39:13 -0400 | [diff] [blame] | 86 | VkDevice device = vk::GetImpl(context)->getRenderer()->getDevice(); |
Frank Henigman | e452382 | 2018-07-19 16:10:53 -0400 | [diff] [blame^] | 87 | for (vk::DynamicBuffer &buffer : mCurrentArrayBufferConversion) |
| 88 | { |
| 89 | buffer.destroy(device); |
| 90 | } |
Jamie Madill | c3755fc | 2018-04-05 08:39:13 -0400 | [diff] [blame] | 91 | mDynamicVertexData.destroy(device); |
| 92 | mDynamicIndexData.destroy(device); |
Luc Ferron | 6ed167a | 2018-06-13 13:45:55 -0400 | [diff] [blame] | 93 | mTranslatedByteIndexData.destroy(device); |
Jamie Madill | 22f12fe | 2018-04-08 14:23:40 -0400 | [diff] [blame] | 94 | mLineLoopHelper.destroy(device); |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 95 | } |
| 96 | |
Jamie Madill | 2106102 | 2018-07-12 23:56:30 -0400 | [diff] [blame] | 97 | angle::Result VertexArrayVk::streamVertexData(ContextVk *contextVk, |
| 98 | const gl::AttributesMask &attribsToStream, |
| 99 | const gl::DrawCallParams &drawCallParams) |
Frank Henigman | 1744895 | 2017-01-05 15:48:26 -0500 | [diff] [blame] | 100 | { |
Jamie Madill | c3755fc | 2018-04-05 08:39:13 -0400 | [diff] [blame] | 101 | ASSERT(!attribsToStream.none()); |
| 102 | |
Jamie Madill | bcef322 | 2018-04-13 15:19:11 -0400 | [diff] [blame] | 103 | const auto &attribs = mState.getVertexAttributes(); |
| 104 | const auto &bindings = mState.getVertexBindings(); |
Frank Henigman | 1744895 | 2017-01-05 15:48:26 -0500 | [diff] [blame] | 105 | |
| 106 | // TODO(fjhenigman): When we have a bunch of interleaved attributes, they end up |
| 107 | // un-interleaved, wasting space and copying time. Consider improving on that. |
Jamie Madill | b8e3966 | 2018-04-04 11:41:42 -0400 | [diff] [blame] | 108 | for (size_t attribIndex : attribsToStream) |
Frank Henigman | 1744895 | 2017-01-05 15:48:26 -0500 | [diff] [blame] | 109 | { |
Frank Henigman | 6dd4a92 | 2018-03-02 16:35:13 -0500 | [diff] [blame] | 110 | const gl::VertexAttribute &attrib = attribs[attribIndex]; |
| 111 | const gl::VertexBinding &binding = bindings[attrib.bindingIndex]; |
| 112 | ASSERT(attrib.enabled && binding.getBuffer().get() == nullptr); |
Frank Henigman | 1744895 | 2017-01-05 15:48:26 -0500 | [diff] [blame] | 113 | |
Frank Henigman | 419acc8 | 2018-06-24 19:57:31 -0400 | [diff] [blame] | 114 | // Only vertexCount() vertices will be used by the upcoming draw so that is |
| 115 | // all we copy, but we allocate space firstVertex() + vertexCount() so indexing |
Frank Henigman | 6dd4a92 | 2018-03-02 16:35:13 -0500 | [diff] [blame] | 116 | // will work. If we don't start at zero all the indices will be off. |
| 117 | // TODO(fjhenigman): See if we can account for indices being off by adjusting |
| 118 | // the offset, thus avoiding wasted memory. |
Frank Henigman | 419acc8 | 2018-06-24 19:57:31 -0400 | [diff] [blame] | 119 | const size_t bytesAllocated = |
| 120 | (drawCallParams.firstVertex() + drawCallParams.vertexCount()) * |
| 121 | mCurrentArrayBufferStrides[attribIndex]; |
| 122 | const uint8_t *src = static_cast<const uint8_t *>(attrib.pointer) + |
| 123 | drawCallParams.firstVertex() * binding.getStride(); |
Jamie Madill | 493f957 | 2018-05-24 19:52:15 -0400 | [diff] [blame] | 124 | uint8_t *dst = nullptr; |
Luc Ferron | 7a06ac1 | 2018-03-15 10:17:04 -0400 | [diff] [blame] | 125 | uint32_t offset = 0; |
Frank Henigman | 419acc8 | 2018-06-24 19:57:31 -0400 | [diff] [blame] | 126 | ANGLE_TRY(mDynamicVertexData.allocate(contextVk, bytesAllocated, &dst, |
| 127 | &mCurrentArrayBufferHandles[attribIndex], &offset, |
| 128 | nullptr)); |
| 129 | dst += drawCallParams.firstVertex() * mCurrentArrayBufferStrides[attribIndex]; |
Luc Ferron | 7a06ac1 | 2018-03-15 10:17:04 -0400 | [diff] [blame] | 130 | mCurrentArrayBufferOffsets[attribIndex] = static_cast<VkDeviceSize>(offset); |
Frank Henigman | 419acc8 | 2018-06-24 19:57:31 -0400 | [diff] [blame] | 131 | mCurrentArrayBufferFormats[attribIndex]->vertexLoadFunction( |
| 132 | src, binding.getStride(), drawCallParams.vertexCount(), dst); |
Frank Henigman | 1744895 | 2017-01-05 15:48:26 -0500 | [diff] [blame] | 133 | } |
| 134 | |
Jamie Madill | 2106102 | 2018-07-12 23:56:30 -0400 | [diff] [blame] | 135 | ANGLE_TRY(mDynamicVertexData.flush(contextVk)); |
| 136 | mDynamicVertexData.releaseRetainedBuffers(contextVk->getRenderer()); |
| 137 | return angle::Result::Continue(); |
Jamie Madill | c3755fc | 2018-04-05 08:39:13 -0400 | [diff] [blame] | 138 | } |
| 139 | |
Jamie Madill | 2106102 | 2018-07-12 23:56:30 -0400 | [diff] [blame] | 140 | angle::Result VertexArrayVk::streamIndexData(ContextVk *contextVk, |
| 141 | const gl::DrawCallParams &drawCallParams) |
Jamie Madill | c3755fc | 2018-04-05 08:39:13 -0400 | [diff] [blame] | 142 | { |
| 143 | ASSERT(!mState.getElementArrayBuffer().get()); |
| 144 | |
| 145 | uint32_t offset = 0; |
| 146 | |
| 147 | const GLsizei amount = sizeof(GLushort) * drawCallParams.indexCount(); |
| 148 | GLubyte *dst = nullptr; |
| 149 | |
Jamie Madill | 2106102 | 2018-07-12 23:56:30 -0400 | [diff] [blame] | 150 | ANGLE_TRY(mDynamicIndexData.allocate(contextVk, amount, &dst, &mCurrentElementArrayBufferHandle, |
Jamie Madill | c3755fc | 2018-04-05 08:39:13 -0400 | [diff] [blame] | 151 | &offset, nullptr)); |
| 152 | if (drawCallParams.type() == GL_UNSIGNED_BYTE) |
| 153 | { |
| 154 | // Unsigned bytes don't have direct support in Vulkan so we have to expand the |
| 155 | // memory to a GLushort. |
| 156 | const GLubyte *in = static_cast<const GLubyte *>(drawCallParams.indices()); |
| 157 | GLushort *expandedDst = reinterpret_cast<GLushort *>(dst); |
| 158 | for (GLsizei index = 0; index < drawCallParams.indexCount(); index++) |
| 159 | { |
| 160 | expandedDst[index] = static_cast<GLushort>(in[index]); |
| 161 | } |
| 162 | } |
| 163 | else |
| 164 | { |
| 165 | memcpy(dst, drawCallParams.indices(), amount); |
| 166 | } |
Jamie Madill | 2106102 | 2018-07-12 23:56:30 -0400 | [diff] [blame] | 167 | ANGLE_TRY(mDynamicIndexData.flush(contextVk)); |
| 168 | mDynamicIndexData.releaseRetainedBuffers(contextVk->getRenderer()); |
Jamie Madill | c3755fc | 2018-04-05 08:39:13 -0400 | [diff] [blame] | 169 | mCurrentElementArrayBufferOffset = offset; |
Jamie Madill | 2106102 | 2018-07-12 23:56:30 -0400 | [diff] [blame] | 170 | return angle::Result::Continue(); |
Frank Henigman | 1744895 | 2017-01-05 15:48:26 -0500 | [diff] [blame] | 171 | } |
| 172 | |
Frank Henigman | e452382 | 2018-07-19 16:10:53 -0400 | [diff] [blame^] | 173 | // We assume the buffer is completely full of the same kind of data and convert |
| 174 | // and/or align it as we copy it to a DynamicBuffer. The assumption could be wrong |
| 175 | // but the alternative of copying it piecemeal on each draw would have a lot more |
| 176 | // overhead. |
| 177 | angle::Result VertexArrayVk::convertVertexBuffer(ContextVk *context, |
| 178 | BufferVk *srcBuffer, |
| 179 | const gl::VertexBinding &binding, |
| 180 | size_t attribIndex) |
| 181 | { |
| 182 | |
| 183 | // Preparation for mapping source buffer. |
| 184 | ANGLE_TRY(context->getRenderer()->finish(context)); |
| 185 | |
| 186 | unsigned srcFormatSize = mCurrentArrayBufferFormats[attribIndex]->angleFormat().pixelBytes; |
| 187 | unsigned dstFormatSize = mCurrentArrayBufferFormats[attribIndex]->bufferFormat().pixelBytes; |
| 188 | |
| 189 | // Bytes usable for vertex data. |
| 190 | GLint64 bytes = srcBuffer->getSize() - binding.getOffset(); |
| 191 | if (bytes < srcFormatSize) |
| 192 | return angle::Result::Continue(); |
| 193 | |
| 194 | // Count the last vertex. It may occupy less than a full stride. |
| 195 | size_t numVertices = 1; |
| 196 | bytes -= srcFormatSize; |
| 197 | |
| 198 | // Count how many strides fit remaining space. |
| 199 | if (bytes > 0) |
| 200 | numVertices += static_cast<size_t>(bytes) / binding.getStride(); |
| 201 | |
| 202 | void *src = nullptr; |
| 203 | uint8_t *dst = nullptr; |
| 204 | uint32_t offset = 0; |
| 205 | |
| 206 | ANGLE_TRY(mCurrentArrayBufferConversion[attribIndex].allocate( |
| 207 | context, numVertices * dstFormatSize, &dst, &mCurrentArrayBufferHandles[attribIndex], |
| 208 | &offset, nullptr)); |
| 209 | ANGLE_TRY(srcBuffer->mapImpl(context, &src)); |
| 210 | mCurrentArrayBufferFormats[attribIndex]->vertexLoadFunction( |
| 211 | static_cast<const uint8_t *>(src) + binding.getOffset(), binding.getStride(), numVertices, |
| 212 | dst); |
| 213 | ANGLE_TRY(srcBuffer->unmapImpl(context)); |
| 214 | ANGLE_TRY(mCurrentArrayBufferConversion[attribIndex].flush(context)); |
| 215 | |
| 216 | mCurrentArrayBufferConversionCanRelease[attribIndex] = true; |
| 217 | mCurrentArrayBufferOffsets[attribIndex] = offset; |
| 218 | mCurrentArrayBufferStrides[attribIndex] = dstFormatSize; |
| 219 | |
| 220 | return angle::Result::Continue(); |
| 221 | } |
| 222 | |
| 223 | void VertexArrayVk::ensureConversionReleased(RendererVk *renderer, size_t attribIndex) |
| 224 | { |
| 225 | if (mCurrentArrayBufferConversionCanRelease[attribIndex]) |
| 226 | { |
| 227 | mCurrentArrayBufferConversion[attribIndex].release(renderer); |
| 228 | mCurrentArrayBufferConversionCanRelease[attribIndex] = false; |
| 229 | } |
| 230 | } |
| 231 | |
| 232 | #define ANGLE_VERTEX_DIRTY_ATTRIB_FUNC(INDEX) \ |
| 233 | case gl::VertexArray::DIRTY_BIT_ATTRIB_0 + INDEX: \ |
| 234 | ANGLE_TRY(syncDirtyAttrib(contextVk, attribs[INDEX], \ |
| 235 | bindings[attribs[INDEX].bindingIndex], INDEX)); \ |
| 236 | invalidatePipeline = true; \ |
Jamie Madill | a56467e | 2018-04-11 16:19:41 -0400 | [diff] [blame] | 237 | break; |
| 238 | |
Frank Henigman | e452382 | 2018-07-19 16:10:53 -0400 | [diff] [blame^] | 239 | #define ANGLE_VERTEX_DIRTY_BINDING_FUNC(INDEX) \ |
| 240 | case gl::VertexArray::DIRTY_BIT_BINDING_0 + INDEX: \ |
| 241 | ANGLE_TRY(syncDirtyAttrib(contextVk, attribs[INDEX], \ |
| 242 | bindings[attribs[INDEX].bindingIndex], INDEX)); \ |
| 243 | invalidatePipeline = true; \ |
Jamie Madill | a56467e | 2018-04-11 16:19:41 -0400 | [diff] [blame] | 244 | break; |
| 245 | |
Frank Henigman | e452382 | 2018-07-19 16:10:53 -0400 | [diff] [blame^] | 246 | #define ANGLE_VERTEX_DIRTY_BUFFER_DATA_FUNC(INDEX) \ |
| 247 | case gl::VertexArray::DIRTY_BIT_BUFFER_DATA_0 + INDEX: \ |
| 248 | ANGLE_TRY(syncDirtyAttrib(contextVk, attribs[INDEX], \ |
| 249 | bindings[attribs[INDEX].bindingIndex], INDEX)); \ |
Jamie Madill | a56467e | 2018-04-11 16:19:41 -0400 | [diff] [blame] | 250 | break; |
| 251 | |
Frank Henigman | 0af5b86 | 2018-03-27 20:19:33 -0400 | [diff] [blame] | 252 | gl::Error VertexArrayVk::syncState(const gl::Context *context, |
| 253 | const gl::VertexArray::DirtyBits &dirtyBits, |
| 254 | const gl::VertexArray::DirtyAttribBitsArray &attribBits, |
| 255 | const gl::VertexArray::DirtyBindingBitsArray &bindingBits) |
Jamie Madill | dd43e6c | 2017-03-24 14:18:49 -0400 | [diff] [blame] | 256 | { |
| 257 | ASSERT(dirtyBits.any()); |
Jamie Madill | 7210656 | 2017-03-24 14:18:50 -0400 | [diff] [blame] | 258 | |
Jamie Madill | c3755fc | 2018-04-05 08:39:13 -0400 | [diff] [blame] | 259 | bool invalidatePipeline = false; |
| 260 | |
Jamie Madill | 5a4c932 | 2018-07-16 11:01:58 -0400 | [diff] [blame] | 261 | ContextVk *contextVk = vk::GetImpl(context); |
Jamie Madill | bd159f0 | 2017-10-09 19:39:06 -0400 | [diff] [blame] | 262 | |
| 263 | // Rebuild current attribute buffers cache. This will fail horribly if the buffer changes. |
| 264 | // TODO(jmadill): Handle buffer storage changes. |
| 265 | const auto &attribs = mState.getVertexAttributes(); |
| 266 | const auto &bindings = mState.getVertexBindings(); |
| 267 | |
Jamie Madill | 0946393 | 2018-04-04 05:26:59 -0400 | [diff] [blame] | 268 | for (size_t dirtyBit : dirtyBits) |
Jamie Madill | bd159f0 | 2017-10-09 19:39:06 -0400 | [diff] [blame] | 269 | { |
Jamie Madill | 0946393 | 2018-04-04 05:26:59 -0400 | [diff] [blame] | 270 | switch (dirtyBit) |
Jamie Madill | da854a2 | 2017-11-30 17:24:21 -0500 | [diff] [blame] | 271 | { |
Jamie Madill | 0946393 | 2018-04-04 05:26:59 -0400 | [diff] [blame] | 272 | case gl::VertexArray::DIRTY_BIT_ELEMENT_ARRAY_BUFFER: |
Jamie Madill | da854a2 | 2017-11-30 17:24:21 -0500 | [diff] [blame] | 273 | { |
Jamie Madill | 0946393 | 2018-04-04 05:26:59 -0400 | [diff] [blame] | 274 | gl::Buffer *bufferGL = mState.getElementArrayBuffer().get(); |
| 275 | if (bufferGL) |
| 276 | { |
Jamie Madill | c3755fc | 2018-04-05 08:39:13 -0400 | [diff] [blame] | 277 | BufferVk *bufferVk = vk::GetImpl(bufferGL); |
| 278 | mCurrentElementArrayBufferResource = bufferVk; |
| 279 | mCurrentElementArrayBufferHandle = bufferVk->getVkBuffer().getHandle(); |
Jamie Madill | 0946393 | 2018-04-04 05:26:59 -0400 | [diff] [blame] | 280 | } |
| 281 | else |
| 282 | { |
| 283 | mCurrentElementArrayBufferResource = nullptr; |
Jamie Madill | c3755fc | 2018-04-05 08:39:13 -0400 | [diff] [blame] | 284 | mCurrentElementArrayBufferHandle = VK_NULL_HANDLE; |
Jamie Madill | 0946393 | 2018-04-04 05:26:59 -0400 | [diff] [blame] | 285 | } |
Luc Ferron | 387b3b3 | 2018-05-28 10:11:57 -0400 | [diff] [blame] | 286 | |
| 287 | mCurrentElementArrayBufferOffset = 0; |
| 288 | mLineLoopBufferFirstIndex.reset(); |
| 289 | mLineLoopBufferLastIndex.reset(); |
Jamie Madill | b8e3966 | 2018-04-04 11:41:42 -0400 | [diff] [blame] | 290 | mIndexBufferDirty = true; |
| 291 | mDirtyLineLoopTranslation = true; |
Jamie Madill | 0946393 | 2018-04-04 05:26:59 -0400 | [diff] [blame] | 292 | break; |
Jamie Madill | da854a2 | 2017-11-30 17:24:21 -0500 | [diff] [blame] | 293 | } |
Jamie Madill | 0946393 | 2018-04-04 05:26:59 -0400 | [diff] [blame] | 294 | |
| 295 | case gl::VertexArray::DIRTY_BIT_ELEMENT_ARRAY_BUFFER_DATA: |
Jamie Madill | c3755fc | 2018-04-05 08:39:13 -0400 | [diff] [blame] | 296 | mLineLoopBufferFirstIndex.reset(); |
| 297 | mLineLoopBufferLastIndex.reset(); |
Jamie Madill | b8e3966 | 2018-04-04 11:41:42 -0400 | [diff] [blame] | 298 | mDirtyLineLoopTranslation = true; |
Jamie Madill | 0946393 | 2018-04-04 05:26:59 -0400 | [diff] [blame] | 299 | break; |
| 300 | |
Jamie Madill | a56467e | 2018-04-11 16:19:41 -0400 | [diff] [blame] | 301 | ANGLE_VERTEX_INDEX_CASES(ANGLE_VERTEX_DIRTY_ATTRIB_FUNC); |
| 302 | ANGLE_VERTEX_INDEX_CASES(ANGLE_VERTEX_DIRTY_BINDING_FUNC); |
| 303 | ANGLE_VERTEX_INDEX_CASES(ANGLE_VERTEX_DIRTY_BUFFER_DATA_FUNC); |
| 304 | |
Jamie Madill | 0946393 | 2018-04-04 05:26:59 -0400 | [diff] [blame] | 305 | default: |
Jamie Madill | a56467e | 2018-04-11 16:19:41 -0400 | [diff] [blame] | 306 | UNREACHABLE(); |
Jamie Madill | 0946393 | 2018-04-04 05:26:59 -0400 | [diff] [blame] | 307 | break; |
Jamie Madill | bd159f0 | 2017-10-09 19:39:06 -0400 | [diff] [blame] | 308 | } |
| 309 | } |
Frank Henigman | 0af5b86 | 2018-03-27 20:19:33 -0400 | [diff] [blame] | 310 | |
Jamie Madill | c3755fc | 2018-04-05 08:39:13 -0400 | [diff] [blame] | 311 | if (invalidatePipeline) |
| 312 | { |
| 313 | mVertexBuffersDirty = true; |
| 314 | contextVk->invalidateCurrentPipeline(); |
| 315 | } |
| 316 | |
Frank Henigman | 0af5b86 | 2018-03-27 20:19:33 -0400 | [diff] [blame] | 317 | return gl::NoError(); |
Jamie Madill | bd159f0 | 2017-10-09 19:39:06 -0400 | [diff] [blame] | 318 | } |
| 319 | |
Frank Henigman | e452382 | 2018-07-19 16:10:53 -0400 | [diff] [blame^] | 320 | angle::Result VertexArrayVk::syncDirtyAttrib(ContextVk *contextVk, |
| 321 | const gl::VertexAttribute &attrib, |
| 322 | const gl::VertexBinding &binding, |
| 323 | size_t attribIndex) |
Jamie Madill | a56467e | 2018-04-11 16:19:41 -0400 | [diff] [blame] | 324 | { |
| 325 | // Invalidate the input description for pipelines. |
| 326 | mDirtyPackedInputs.set(attribIndex); |
| 327 | |
Jamie Madill | 5a4c932 | 2018-07-16 11:01:58 -0400 | [diff] [blame] | 328 | RendererVk *renderer = contextVk->getRenderer(); |
Frank Henigman | e452382 | 2018-07-19 16:10:53 -0400 | [diff] [blame^] | 329 | bool releaseConversion = true; |
Jamie Madill | 5a4c932 | 2018-07-16 11:01:58 -0400 | [diff] [blame] | 330 | |
Jamie Madill | a56467e | 2018-04-11 16:19:41 -0400 | [diff] [blame] | 331 | if (attrib.enabled) |
| 332 | { |
| 333 | gl::Buffer *bufferGL = binding.getBuffer().get(); |
Frank Henigman | 419acc8 | 2018-06-24 19:57:31 -0400 | [diff] [blame] | 334 | mCurrentArrayBufferFormats[attribIndex] = &renderer->getFormat(GetVertexFormatID(attrib)); |
Frank Henigman | e452382 | 2018-07-19 16:10:53 -0400 | [diff] [blame^] | 335 | |
Jamie Madill | a56467e | 2018-04-11 16:19:41 -0400 | [diff] [blame] | 336 | if (bufferGL) |
| 337 | { |
Frank Henigman | e452382 | 2018-07-19 16:10:53 -0400 | [diff] [blame^] | 338 | BufferVk *bufferVk = vk::GetImpl(bufferGL); |
| 339 | unsigned componentSize = |
| 340 | mCurrentArrayBufferFormats[attribIndex]->angleFormat().pixelBytes / attrib.size; |
Frank Henigman | 419acc8 | 2018-06-24 19:57:31 -0400 | [diff] [blame] | 341 | |
Frank Henigman | e452382 | 2018-07-19 16:10:53 -0400 | [diff] [blame^] | 342 | if (mCurrentArrayBufferFormats[attribIndex]->vertexLoadRequiresConversion || |
| 343 | !BindingIsAligned(binding, componentSize)) |
| 344 | { |
| 345 | ANGLE_TRY(convertVertexBuffer(contextVk, bufferVk, binding, attribIndex)); |
| 346 | |
| 347 | mCurrentArrayBufferConversion[attribIndex].releaseRetainedBuffers(renderer); |
| 348 | mCurrentArrayBufferResources[attribIndex] = nullptr; |
| 349 | releaseConversion = false; |
| 350 | } |
| 351 | else |
| 352 | { |
| 353 | mCurrentArrayBufferResources[attribIndex] = bufferVk; |
| 354 | mCurrentArrayBufferHandles[attribIndex] = bufferVk->getVkBuffer().getHandle(); |
| 355 | mCurrentArrayBufferOffsets[attribIndex] = binding.getOffset(); |
| 356 | mCurrentArrayBufferStrides[attribIndex] = binding.getStride(); |
| 357 | } |
Jamie Madill | a56467e | 2018-04-11 16:19:41 -0400 | [diff] [blame] | 358 | } |
| 359 | else |
| 360 | { |
| 361 | mCurrentArrayBufferResources[attribIndex] = nullptr; |
| 362 | mCurrentArrayBufferHandles[attribIndex] = VK_NULL_HANDLE; |
Luc Ferron | fa5d84b | 2018-06-28 10:40:04 -0400 | [diff] [blame] | 363 | mCurrentArrayBufferOffsets[attribIndex] = 0; |
Frank Henigman | 419acc8 | 2018-06-24 19:57:31 -0400 | [diff] [blame] | 364 | mCurrentArrayBufferStrides[attribIndex] = |
Frank Henigman | e452382 | 2018-07-19 16:10:53 -0400 | [diff] [blame^] | 365 | mCurrentArrayBufferFormats[attribIndex]->bufferFormat().pixelBytes; |
Jamie Madill | a56467e | 2018-04-11 16:19:41 -0400 | [diff] [blame] | 366 | } |
Jamie Madill | a56467e | 2018-04-11 16:19:41 -0400 | [diff] [blame] | 367 | } |
| 368 | else |
| 369 | { |
Jamie Madill | 5a4c932 | 2018-07-16 11:01:58 -0400 | [diff] [blame] | 370 | contextVk->invalidateDefaultAttribute(attribIndex); |
| 371 | |
| 372 | // These will be filled out by the ContextVk. |
| 373 | mCurrentArrayBufferResources[attribIndex] = nullptr; |
| 374 | mCurrentArrayBufferHandles[attribIndex] = VK_NULL_HANDLE; |
| 375 | mCurrentArrayBufferOffsets[attribIndex] = 0; |
| 376 | mCurrentArrayBufferStrides[attribIndex] = 0; |
| 377 | mCurrentArrayBufferFormats[attribIndex] = |
Jamie Madill | ba36593 | 2018-07-18 17:23:46 -0400 | [diff] [blame] | 378 | &renderer->getFormat(angle::FormatID::R32G32B32A32_FLOAT); |
Jamie Madill | a56467e | 2018-04-11 16:19:41 -0400 | [diff] [blame] | 379 | } |
Frank Henigman | e452382 | 2018-07-19 16:10:53 -0400 | [diff] [blame^] | 380 | |
| 381 | if (releaseConversion) |
| 382 | ensureConversionReleased(renderer, attribIndex); |
| 383 | |
| 384 | return angle::Result::Continue(); |
Jamie Madill | a56467e | 2018-04-11 16:19:41 -0400 | [diff] [blame] | 385 | } |
| 386 | |
Jamie Madill | 316c606 | 2018-05-29 10:49:45 -0400 | [diff] [blame] | 387 | void VertexArrayVk::updateArrayBufferReadDependencies(vk::CommandGraphResource *drawFramebuffer, |
Jamie Madill | c3755fc | 2018-04-05 08:39:13 -0400 | [diff] [blame] | 388 | const gl::AttributesMask &activeAttribsMask, |
| 389 | Serial serial) |
Jamie Madill | bd159f0 | 2017-10-09 19:39:06 -0400 | [diff] [blame] | 390 | { |
Jamie Madill | da854a2 | 2017-11-30 17:24:21 -0500 | [diff] [blame] | 391 | // Handle the bound array buffers. |
Jamie Madill | c3755fc | 2018-04-05 08:39:13 -0400 | [diff] [blame] | 392 | for (size_t attribIndex : activeAttribsMask) |
Jamie Madill | bd159f0 | 2017-10-09 19:39:06 -0400 | [diff] [blame] | 393 | { |
Frank Henigman | 1744895 | 2017-01-05 15:48:26 -0500 | [diff] [blame] | 394 | if (mCurrentArrayBufferResources[attribIndex]) |
Jamie Madill | 316c606 | 2018-05-29 10:49:45 -0400 | [diff] [blame] | 395 | mCurrentArrayBufferResources[attribIndex]->addReadDependency(drawFramebuffer); |
Jamie Madill | da854a2 | 2017-11-30 17:24:21 -0500 | [diff] [blame] | 396 | } |
Jamie Madill | c3755fc | 2018-04-05 08:39:13 -0400 | [diff] [blame] | 397 | } |
Jamie Madill | da854a2 | 2017-11-30 17:24:21 -0500 | [diff] [blame] | 398 | |
Jamie Madill | 316c606 | 2018-05-29 10:49:45 -0400 | [diff] [blame] | 399 | void VertexArrayVk::updateElementArrayBufferReadDependency( |
| 400 | vk::CommandGraphResource *drawFramebuffer, |
| 401 | Serial serial) |
Jamie Madill | c3755fc | 2018-04-05 08:39:13 -0400 | [diff] [blame] | 402 | { |
Jamie Madill | da854a2 | 2017-11-30 17:24:21 -0500 | [diff] [blame] | 403 | // Handle the bound element array buffer. |
Jamie Madill | c3755fc | 2018-04-05 08:39:13 -0400 | [diff] [blame] | 404 | if (mCurrentElementArrayBufferResource) |
Jamie Madill | da854a2 | 2017-11-30 17:24:21 -0500 | [diff] [blame] | 405 | { |
Jamie Madill | 316c606 | 2018-05-29 10:49:45 -0400 | [diff] [blame] | 406 | mCurrentElementArrayBufferResource->addReadDependency(drawFramebuffer); |
Jamie Madill | bd159f0 | 2017-10-09 19:39:06 -0400 | [diff] [blame] | 407 | } |
Jamie Madill | dd43e6c | 2017-03-24 14:18:49 -0400 | [diff] [blame] | 408 | } |
| 409 | |
Frank Henigman | 419acc8 | 2018-06-24 19:57:31 -0400 | [diff] [blame] | 410 | void VertexArrayVk::getPackedInputDescriptions(vk::PipelineDesc *pipelineDesc) |
Jamie Madill | ebf7299 | 2017-10-13 14:09:45 -0400 | [diff] [blame] | 411 | { |
Frank Henigman | 419acc8 | 2018-06-24 19:57:31 -0400 | [diff] [blame] | 412 | updatePackedInputDescriptions(); |
Jamie Madill | 112a3a8 | 2018-01-23 13:04:06 -0500 | [diff] [blame] | 413 | pipelineDesc->updateVertexInputInfo(mPackedInputBindings, mPackedInputAttributes); |
Jamie Madill | ebf7299 | 2017-10-13 14:09:45 -0400 | [diff] [blame] | 414 | } |
| 415 | |
Frank Henigman | 419acc8 | 2018-06-24 19:57:31 -0400 | [diff] [blame] | 416 | void VertexArrayVk::updatePackedInputDescriptions() |
Jamie Madill | ebf7299 | 2017-10-13 14:09:45 -0400 | [diff] [blame] | 417 | { |
Jamie Madill | 112a3a8 | 2018-01-23 13:04:06 -0500 | [diff] [blame] | 418 | if (!mDirtyPackedInputs.any()) |
Jamie Madill | ebf7299 | 2017-10-13 14:09:45 -0400 | [diff] [blame] | 419 | { |
| 420 | return; |
| 421 | } |
| 422 | |
| 423 | const auto &attribs = mState.getVertexAttributes(); |
| 424 | const auto &bindings = mState.getVertexBindings(); |
| 425 | |
Jamie Madill | 112a3a8 | 2018-01-23 13:04:06 -0500 | [diff] [blame] | 426 | for (auto attribIndex : mDirtyPackedInputs) |
Jamie Madill | ebf7299 | 2017-10-13 14:09:45 -0400 | [diff] [blame] | 427 | { |
| 428 | const auto &attrib = attribs[attribIndex]; |
| 429 | const auto &binding = bindings[attrib.bindingIndex]; |
| 430 | if (attrib.enabled) |
| 431 | { |
Frank Henigman | 419acc8 | 2018-06-24 19:57:31 -0400 | [diff] [blame] | 432 | updatePackedInputInfo(static_cast<uint32_t>(attribIndex), binding, attrib); |
Jamie Madill | ebf7299 | 2017-10-13 14:09:45 -0400 | [diff] [blame] | 433 | } |
| 434 | else |
| 435 | { |
Jamie Madill | 5a4c932 | 2018-07-16 11:01:58 -0400 | [diff] [blame] | 436 | vk::PackedVertexInputBindingDesc &bindingDesc = mPackedInputBindings[attribIndex]; |
| 437 | bindingDesc.stride = 0; |
| 438 | bindingDesc.inputRate = VK_VERTEX_INPUT_RATE_VERTEX; |
| 439 | |
| 440 | vk::PackedVertexInputAttributeDesc &attribDesc = mPackedInputAttributes[attribIndex]; |
| 441 | attribDesc.format = static_cast<uint16_t>(VK_FORMAT_R32G32B32A32_SFLOAT); |
| 442 | attribDesc.location = static_cast<uint16_t>(attribIndex); |
| 443 | attribDesc.offset = 0; |
Jamie Madill | ebf7299 | 2017-10-13 14:09:45 -0400 | [diff] [blame] | 444 | } |
| 445 | } |
| 446 | |
Jamie Madill | 112a3a8 | 2018-01-23 13:04:06 -0500 | [diff] [blame] | 447 | mDirtyPackedInputs.reset(); |
| 448 | } |
| 449 | |
Frank Henigman | 419acc8 | 2018-06-24 19:57:31 -0400 | [diff] [blame] | 450 | void VertexArrayVk::updatePackedInputInfo(uint32_t attribIndex, |
Jamie Madill | 112a3a8 | 2018-01-23 13:04:06 -0500 | [diff] [blame] | 451 | const gl::VertexBinding &binding, |
| 452 | const gl::VertexAttribute &attrib) |
| 453 | { |
| 454 | vk::PackedVertexInputBindingDesc &bindingDesc = mPackedInputBindings[attribIndex]; |
| 455 | |
Frank Henigman | 419acc8 | 2018-06-24 19:57:31 -0400 | [diff] [blame] | 456 | bindingDesc.stride = static_cast<uint16_t>(mCurrentArrayBufferStrides[attribIndex]); |
Jamie Madill | 112a3a8 | 2018-01-23 13:04:06 -0500 | [diff] [blame] | 457 | bindingDesc.inputRate = static_cast<uint16_t>( |
| 458 | binding.getDivisor() > 0 ? VK_VERTEX_INPUT_RATE_INSTANCE : VK_VERTEX_INPUT_RATE_VERTEX); |
| 459 | |
Frank Henigman | 419acc8 | 2018-06-24 19:57:31 -0400 | [diff] [blame] | 460 | VkFormat vkFormat = mCurrentArrayBufferFormats[attribIndex]->vkBufferFormat; |
Jamie Madill | 112a3a8 | 2018-01-23 13:04:06 -0500 | [diff] [blame] | 461 | ASSERT(vkFormat <= std::numeric_limits<uint16_t>::max()); |
Frank Henigman | 8c379f3 | 2018-06-21 19:55:05 -0400 | [diff] [blame] | 462 | if (vkFormat == VK_FORMAT_UNDEFINED) |
| 463 | { |
| 464 | // TODO(fjhenigman): Add support for vertex data format. anglebug.com/2405 |
| 465 | UNIMPLEMENTED(); |
| 466 | } |
Jamie Madill | 112a3a8 | 2018-01-23 13:04:06 -0500 | [diff] [blame] | 467 | |
| 468 | vk::PackedVertexInputAttributeDesc &attribDesc = mPackedInputAttributes[attribIndex]; |
| 469 | attribDesc.format = static_cast<uint16_t>(vkFormat); |
| 470 | attribDesc.location = static_cast<uint16_t>(attribIndex); |
Luc Ferron | fa5d84b | 2018-06-28 10:40:04 -0400 | [diff] [blame] | 471 | attribDesc.offset = static_cast<uint32_t>(attrib.relativeOffset); |
Jamie Madill | ebf7299 | 2017-10-13 14:09:45 -0400 | [diff] [blame] | 472 | } |
| 473 | |
Jamie Madill | c3755fc | 2018-04-05 08:39:13 -0400 | [diff] [blame] | 474 | gl::Error VertexArrayVk::drawArrays(const gl::Context *context, |
Jamie Madill | c3755fc | 2018-04-05 08:39:13 -0400 | [diff] [blame] | 475 | const gl::DrawCallParams &drawCallParams, |
Jamie Madill | 316c606 | 2018-05-29 10:49:45 -0400 | [diff] [blame] | 476 | vk::CommandBuffer *commandBuffer, |
Jamie Madill | c3755fc | 2018-04-05 08:39:13 -0400 | [diff] [blame] | 477 | bool newCommandBuffer) |
| 478 | { |
Jamie Madill | c3755fc | 2018-04-05 08:39:13 -0400 | [diff] [blame] | 479 | ASSERT(commandBuffer->valid()); |
| 480 | |
Jamie Madill | 2106102 | 2018-07-12 23:56:30 -0400 | [diff] [blame] | 481 | ContextVk *contextVk = vk::GetImpl(context); |
| 482 | |
| 483 | ANGLE_TRY(onDraw(context, drawCallParams, commandBuffer, newCommandBuffer)); |
Jamie Madill | c3755fc | 2018-04-05 08:39:13 -0400 | [diff] [blame] | 484 | |
Jamie Madill | 18e323a | 2018-05-11 16:54:17 -0400 | [diff] [blame] | 485 | // Note: Vertex indexes can be arbitrarily large. |
| 486 | uint32_t clampedVertexCount = drawCallParams.getClampedVertexCount<uint32_t>(); |
| 487 | |
Jamie Madill | 493f957 | 2018-05-24 19:52:15 -0400 | [diff] [blame] | 488 | if (drawCallParams.mode() != gl::PrimitiveMode::LineLoop) |
Jamie Madill | c3755fc | 2018-04-05 08:39:13 -0400 | [diff] [blame] | 489 | { |
Jamie Madill | 18e323a | 2018-05-11 16:54:17 -0400 | [diff] [blame] | 490 | commandBuffer->draw(clampedVertexCount, 1, drawCallParams.firstVertex(), 0); |
Jamie Madill | c3755fc | 2018-04-05 08:39:13 -0400 | [diff] [blame] | 491 | return gl::NoError(); |
| 492 | } |
| 493 | |
| 494 | // Handle GL_LINE_LOOP drawArrays. |
Jamie Madill | 18e323a | 2018-05-11 16:54:17 -0400 | [diff] [blame] | 495 | size_t lastVertex = static_cast<size_t>(drawCallParams.firstVertex() + clampedVertexCount); |
Jamie Madill | c3755fc | 2018-04-05 08:39:13 -0400 | [diff] [blame] | 496 | if (!mLineLoopBufferFirstIndex.valid() || !mLineLoopBufferLastIndex.valid() || |
| 497 | mLineLoopBufferFirstIndex != drawCallParams.firstVertex() || |
| 498 | mLineLoopBufferLastIndex != lastVertex) |
| 499 | { |
Jamie Madill | 2106102 | 2018-07-12 23:56:30 -0400 | [diff] [blame] | 500 | ANGLE_TRY(mLineLoopHelper.getIndexBufferForDrawArrays(contextVk, drawCallParams, |
Jamie Madill | 22f12fe | 2018-04-08 14:23:40 -0400 | [diff] [blame] | 501 | &mCurrentElementArrayBufferHandle, |
| 502 | &mCurrentElementArrayBufferOffset)); |
Jamie Madill | c3755fc | 2018-04-05 08:39:13 -0400 | [diff] [blame] | 503 | |
| 504 | mLineLoopBufferFirstIndex = drawCallParams.firstVertex(); |
| 505 | mLineLoopBufferLastIndex = lastVertex; |
| 506 | } |
| 507 | |
| 508 | commandBuffer->bindIndexBuffer(mCurrentElementArrayBufferHandle, |
| 509 | mCurrentElementArrayBufferOffset, VK_INDEX_TYPE_UINT32); |
| 510 | |
Jamie Madill | 18e323a | 2018-05-11 16:54:17 -0400 | [diff] [blame] | 511 | vk::LineLoopHelper::Draw(clampedVertexCount, commandBuffer); |
Jamie Madill | c3755fc | 2018-04-05 08:39:13 -0400 | [diff] [blame] | 512 | |
| 513 | return gl::NoError(); |
| 514 | } |
| 515 | |
| 516 | gl::Error VertexArrayVk::drawElements(const gl::Context *context, |
Jamie Madill | c3755fc | 2018-04-05 08:39:13 -0400 | [diff] [blame] | 517 | const gl::DrawCallParams &drawCallParams, |
Jamie Madill | 316c606 | 2018-05-29 10:49:45 -0400 | [diff] [blame] | 518 | vk::CommandBuffer *commandBuffer, |
Jamie Madill | c3755fc | 2018-04-05 08:39:13 -0400 | [diff] [blame] | 519 | bool newCommandBuffer) |
| 520 | { |
Jamie Madill | c3755fc | 2018-04-05 08:39:13 -0400 | [diff] [blame] | 521 | ASSERT(commandBuffer->valid()); |
| 522 | |
Jamie Madill | 2106102 | 2018-07-12 23:56:30 -0400 | [diff] [blame] | 523 | ContextVk *contextVk = vk::GetImpl(context); |
| 524 | |
Jamie Madill | 493f957 | 2018-05-24 19:52:15 -0400 | [diff] [blame] | 525 | if (drawCallParams.mode() != gl::PrimitiveMode::LineLoop) |
Jamie Madill | c3755fc | 2018-04-05 08:39:13 -0400 | [diff] [blame] | 526 | { |
Jamie Madill | 2106102 | 2018-07-12 23:56:30 -0400 | [diff] [blame] | 527 | ANGLE_TRY(onIndexedDraw(context, drawCallParams, commandBuffer, newCommandBuffer)); |
Jamie Madill | c3755fc | 2018-04-05 08:39:13 -0400 | [diff] [blame] | 528 | commandBuffer->drawIndexed(drawCallParams.indexCount(), 1, 0, 0, 0); |
| 529 | return gl::NoError(); |
| 530 | } |
| 531 | |
| 532 | // Handle GL_LINE_LOOP drawElements. |
Jamie Madill | b8e3966 | 2018-04-04 11:41:42 -0400 | [diff] [blame] | 533 | if (mDirtyLineLoopTranslation) |
| 534 | { |
Luc Ferron | a912046 | 2018-04-12 13:11:03 -0400 | [diff] [blame] | 535 | gl::Buffer *elementArrayBuffer = mState.getElementArrayBuffer().get(); |
| 536 | VkIndexType indexType = gl_vk::GetIndexType(drawCallParams.type()); |
| 537 | |
| 538 | if (!elementArrayBuffer) |
| 539 | { |
| 540 | ANGLE_TRY(mLineLoopHelper.getIndexBufferForClientElementArray( |
Jamie Madill | 2106102 | 2018-07-12 23:56:30 -0400 | [diff] [blame] | 541 | contextVk, drawCallParams, &mCurrentElementArrayBufferHandle, |
Luc Ferron | 3e31380 | 2018-06-11 14:44:33 -0400 | [diff] [blame] | 542 | &mCurrentElementArrayBufferOffset)); |
Luc Ferron | a912046 | 2018-04-12 13:11:03 -0400 | [diff] [blame] | 543 | } |
| 544 | else |
| 545 | { |
Luc Ferron | a4fa9c2 | 2018-04-13 07:00:56 -0400 | [diff] [blame] | 546 | // When using an element array buffer, 'indices' is an offset to the first element. |
| 547 | intptr_t offset = reinterpret_cast<intptr_t>(drawCallParams.indices()); |
Luc Ferron | a912046 | 2018-04-12 13:11:03 -0400 | [diff] [blame] | 548 | BufferVk *elementArrayBufferVk = vk::GetImpl(elementArrayBuffer); |
| 549 | ANGLE_TRY(mLineLoopHelper.getIndexBufferForElementArrayBuffer( |
Jamie Madill | 2106102 | 2018-07-12 23:56:30 -0400 | [diff] [blame] | 550 | contextVk, elementArrayBufferVk, indexType, drawCallParams.indexCount(), offset, |
Luc Ferron | a912046 | 2018-04-12 13:11:03 -0400 | [diff] [blame] | 551 | &mCurrentElementArrayBufferHandle, &mCurrentElementArrayBufferOffset)); |
| 552 | } |
Jamie Madill | b8e3966 | 2018-04-04 11:41:42 -0400 | [diff] [blame] | 553 | } |
Jamie Madill | c3755fc | 2018-04-05 08:39:13 -0400 | [diff] [blame] | 554 | |
Jamie Madill | 2106102 | 2018-07-12 23:56:30 -0400 | [diff] [blame] | 555 | ANGLE_TRY(onIndexedDraw(context, drawCallParams, commandBuffer, newCommandBuffer)); |
Jamie Madill | 22f12fe | 2018-04-08 14:23:40 -0400 | [diff] [blame] | 556 | vk::LineLoopHelper::Draw(drawCallParams.indexCount(), commandBuffer); |
Jamie Madill | c3755fc | 2018-04-05 08:39:13 -0400 | [diff] [blame] | 557 | |
| 558 | return gl::NoError(); |
| 559 | } |
| 560 | |
| 561 | gl::Error VertexArrayVk::onDraw(const gl::Context *context, |
Jamie Madill | c3755fc | 2018-04-05 08:39:13 -0400 | [diff] [blame] | 562 | const gl::DrawCallParams &drawCallParams, |
Jamie Madill | 316c606 | 2018-05-29 10:49:45 -0400 | [diff] [blame] | 563 | vk::CommandBuffer *commandBuffer, |
Jamie Madill | c3755fc | 2018-04-05 08:39:13 -0400 | [diff] [blame] | 564 | bool newCommandBuffer) |
| 565 | { |
Jamie Madill | 2106102 | 2018-07-12 23:56:30 -0400 | [diff] [blame] | 566 | ContextVk *contextVk = vk::GetImpl(context); |
Jamie Madill | c3755fc | 2018-04-05 08:39:13 -0400 | [diff] [blame] | 567 | const gl::State &state = context->getGLState(); |
| 568 | const gl::Program *programGL = state.getProgram(); |
Jamie Madill | bcef322 | 2018-04-13 15:19:11 -0400 | [diff] [blame] | 569 | const gl::AttributesMask &clientAttribs = mState.getEnabledClientMemoryAttribsMask(); |
Jamie Madill | c3755fc | 2018-04-05 08:39:13 -0400 | [diff] [blame] | 570 | const gl::AttributesMask &activeAttribs = programGL->getActiveAttribLocationsMask(); |
| 571 | uint32_t maxAttrib = programGL->getState().getMaxActiveAttribLocation(); |
| 572 | |
Jamie Madill | bcef322 | 2018-04-13 15:19:11 -0400 | [diff] [blame] | 573 | if (clientAttribs.any()) |
Jamie Madill | c3755fc | 2018-04-05 08:39:13 -0400 | [diff] [blame] | 574 | { |
Jamie Madill | bcef322 | 2018-04-13 15:19:11 -0400 | [diff] [blame] | 575 | const gl::AttributesMask &attribsToStream = (clientAttribs & activeAttribs); |
Jamie Madill | c3755fc | 2018-04-05 08:39:13 -0400 | [diff] [blame] | 576 | if (attribsToStream.any()) |
| 577 | { |
| 578 | ANGLE_TRY(drawCallParams.ensureIndexRangeResolved(context)); |
Jamie Madill | 2106102 | 2018-07-12 23:56:30 -0400 | [diff] [blame] | 579 | ANGLE_TRY(streamVertexData(contextVk, attribsToStream, drawCallParams)); |
Jamie Madill | c3755fc | 2018-04-05 08:39:13 -0400 | [diff] [blame] | 580 | commandBuffer->bindVertexBuffers(0, maxAttrib, mCurrentArrayBufferHandles.data(), |
| 581 | mCurrentArrayBufferOffsets.data()); |
| 582 | } |
| 583 | } |
| 584 | else if (mVertexBuffersDirty || newCommandBuffer) |
| 585 | { |
Luc Ferron | 4416247 | 2018-04-06 13:20:45 -0400 | [diff] [blame] | 586 | if (maxAttrib > 0) |
| 587 | { |
Luc Ferron | 4416247 | 2018-04-06 13:20:45 -0400 | [diff] [blame] | 588 | commandBuffer->bindVertexBuffers(0, maxAttrib, mCurrentArrayBufferHandles.data(), |
| 589 | mCurrentArrayBufferOffsets.data()); |
Jamie Madill | 316c606 | 2018-05-29 10:49:45 -0400 | [diff] [blame] | 590 | |
| 591 | vk::CommandGraphResource *drawFramebuffer = vk::GetImpl(state.getDrawFramebuffer()); |
| 592 | updateArrayBufferReadDependencies(drawFramebuffer, activeAttribs, |
Jamie Madill | 2106102 | 2018-07-12 23:56:30 -0400 | [diff] [blame] | 593 | contextVk->getRenderer()->getCurrentQueueSerial()); |
Luc Ferron | 4416247 | 2018-04-06 13:20:45 -0400 | [diff] [blame] | 594 | } |
Luc Ferron | c1e0268 | 2018-04-11 11:02:55 -0400 | [diff] [blame] | 595 | |
Jamie Madill | c3755fc | 2018-04-05 08:39:13 -0400 | [diff] [blame] | 596 | mVertexBuffersDirty = false; |
Luc Ferron | c1e0268 | 2018-04-11 11:02:55 -0400 | [diff] [blame] | 597 | |
| 598 | // This forces the binding to happen if we follow a drawElement call from a drawArrays call. |
| 599 | mIndexBufferDirty = true; |
Luc Ferron | 983c429 | 2018-04-10 13:05:45 -0400 | [diff] [blame] | 600 | |
| 601 | // If we've had a drawElements call with a line loop before, we want to make sure this is |
| 602 | // invalidated the next time drawElements is called since we use the same index buffer for |
| 603 | // both calls. |
| 604 | mDirtyLineLoopTranslation = true; |
Jamie Madill | c3755fc | 2018-04-05 08:39:13 -0400 | [diff] [blame] | 605 | } |
| 606 | |
| 607 | return gl::NoError(); |
| 608 | } |
| 609 | |
| 610 | gl::Error VertexArrayVk::onIndexedDraw(const gl::Context *context, |
Jamie Madill | c3755fc | 2018-04-05 08:39:13 -0400 | [diff] [blame] | 611 | const gl::DrawCallParams &drawCallParams, |
Jamie Madill | 316c606 | 2018-05-29 10:49:45 -0400 | [diff] [blame] | 612 | vk::CommandBuffer *commandBuffer, |
Jamie Madill | c3755fc | 2018-04-05 08:39:13 -0400 | [diff] [blame] | 613 | bool newCommandBuffer) |
| 614 | { |
Jamie Madill | 2106102 | 2018-07-12 23:56:30 -0400 | [diff] [blame] | 615 | ContextVk *contextVk = vk::GetImpl(context); |
| 616 | ANGLE_TRY(onDraw(context, drawCallParams, commandBuffer, newCommandBuffer)); |
Frank Henigman | 419acc8 | 2018-06-24 19:57:31 -0400 | [diff] [blame] | 617 | bool isLineLoop = drawCallParams.mode() == gl::PrimitiveMode::LineLoop; |
Luc Ferron | 6ed167a | 2018-06-13 13:45:55 -0400 | [diff] [blame] | 618 | gl::Buffer *glBuffer = mState.getElementArrayBuffer().get(); |
| 619 | uintptr_t offset = |
| 620 | glBuffer && !isLineLoop ? reinterpret_cast<uintptr_t>(drawCallParams.indices()) : 0; |
Jamie Madill | c3755fc | 2018-04-05 08:39:13 -0400 | [diff] [blame] | 621 | |
Luc Ferron | 6ed167a | 2018-06-13 13:45:55 -0400 | [diff] [blame] | 622 | if (!glBuffer && !isLineLoop) |
Jamie Madill | c3755fc | 2018-04-05 08:39:13 -0400 | [diff] [blame] | 623 | { |
| 624 | ANGLE_TRY(drawCallParams.ensureIndexRangeResolved(context)); |
Jamie Madill | 2106102 | 2018-07-12 23:56:30 -0400 | [diff] [blame] | 625 | ANGLE_TRY(streamIndexData(contextVk, drawCallParams)); |
Jamie Madill | c3755fc | 2018-04-05 08:39:13 -0400 | [diff] [blame] | 626 | commandBuffer->bindIndexBuffer(mCurrentElementArrayBufferHandle, |
| 627 | mCurrentElementArrayBufferOffset, |
| 628 | gl_vk::GetIndexType(drawCallParams.type())); |
| 629 | } |
Luc Ferron | 49aacad | 2018-06-12 08:33:59 -0400 | [diff] [blame] | 630 | else if (mIndexBufferDirty || newCommandBuffer || offset != mLastIndexBufferOffset) |
Jamie Madill | c3755fc | 2018-04-05 08:39:13 -0400 | [diff] [blame] | 631 | { |
Luc Ferron | 3e31380 | 2018-06-11 14:44:33 -0400 | [diff] [blame] | 632 | if (drawCallParams.type() == GL_UNSIGNED_BYTE && |
| 633 | drawCallParams.mode() != gl::PrimitiveMode::LineLoop) |
Jamie Madill | c3755fc | 2018-04-05 08:39:13 -0400 | [diff] [blame] | 634 | { |
Luc Ferron | 6ed167a | 2018-06-13 13:45:55 -0400 | [diff] [blame] | 635 | // Unsigned bytes don't have direct support in Vulkan so we have to expand the |
| 636 | // memory to a GLushort. |
| 637 | BufferVk *bufferVk = vk::GetImpl(glBuffer); |
| 638 | void *srcDataMapping = nullptr; |
| 639 | ASSERT(!glBuffer->isMapped()); |
Jamie Madill | 2106102 | 2018-07-12 23:56:30 -0400 | [diff] [blame] | 640 | ANGLE_TRY(bufferVk->mapImpl(contextVk, &srcDataMapping)); |
Rafael Cintron | 05a449a | 2018-06-20 18:08:04 -0700 | [diff] [blame] | 641 | uint8_t *srcData = static_cast<uint8_t *>(srcDataMapping); |
Luc Ferron | 6ed167a | 2018-06-13 13:45:55 -0400 | [diff] [blame] | 642 | intptr_t offsetIntoSrcData = reinterpret_cast<intptr_t>(drawCallParams.indices()); |
| 643 | srcData += offsetIntoSrcData; |
| 644 | |
| 645 | // Allocate a new buffer that's double the size of the buffer provided by the user to |
| 646 | // go from unsigned byte to unsigned short. |
| 647 | uint8_t *allocatedData = nullptr; |
| 648 | bool newBufferAllocated = false; |
| 649 | uint32_t expandedDataOffset = 0; |
Jamie Madill | eebe219 | 2018-07-11 09:01:18 -0400 | [diff] [blame] | 650 | ANGLE_TRY(mTranslatedByteIndexData.allocate( |
Jamie Madill | 2106102 | 2018-07-12 23:56:30 -0400 | [diff] [blame] | 651 | contextVk, static_cast<size_t>(bufferVk->getSize()) * 2, &allocatedData, |
Jamie Madill | eebe219 | 2018-07-11 09:01:18 -0400 | [diff] [blame] | 652 | &mCurrentElementArrayBufferHandle, &expandedDataOffset, &newBufferAllocated)); |
Luc Ferron | 6ed167a | 2018-06-13 13:45:55 -0400 | [diff] [blame] | 653 | mCurrentElementArrayBufferOffset = static_cast<VkDeviceSize>(expandedDataOffset); |
| 654 | |
| 655 | // Expand the source into the destination |
| 656 | ASSERT(!context->getGLState().isPrimitiveRestartEnabled()); |
| 657 | uint16_t *expandedDst = reinterpret_cast<uint16_t *>(allocatedData); |
| 658 | for (GLsizei index = 0; index < bufferVk->getSize() - offsetIntoSrcData; index++) |
| 659 | { |
| 660 | expandedDst[index] = static_cast<GLushort>(srcData[index]); |
| 661 | } |
| 662 | |
| 663 | // Make sure our writes are available. |
Jamie Madill | 2106102 | 2018-07-12 23:56:30 -0400 | [diff] [blame] | 664 | ANGLE_TRY(mTranslatedByteIndexData.flush(contextVk)); |
Luc Ferron | 6ed167a | 2018-06-13 13:45:55 -0400 | [diff] [blame] | 665 | GLboolean result = false; |
| 666 | ANGLE_TRY(bufferVk->unmap(context, &result)); |
| 667 | |
| 668 | // We do not add the offset from the drawCallParams here because we've already copied |
| 669 | // the source starting at the offset requested. |
| 670 | commandBuffer->bindIndexBuffer(mCurrentElementArrayBufferHandle, |
| 671 | mCurrentElementArrayBufferOffset, |
| 672 | gl_vk::GetIndexType(drawCallParams.type())); |
| 673 | } |
| 674 | else |
| 675 | { |
| 676 | commandBuffer->bindIndexBuffer(mCurrentElementArrayBufferHandle, |
| 677 | mCurrentElementArrayBufferOffset + offset, |
| 678 | gl_vk::GetIndexType(drawCallParams.type())); |
Jamie Madill | c3755fc | 2018-04-05 08:39:13 -0400 | [diff] [blame] | 679 | } |
| 680 | |
Luc Ferron | 49aacad | 2018-06-12 08:33:59 -0400 | [diff] [blame] | 681 | mLastIndexBufferOffset = offset; |
Jamie Madill | 316c606 | 2018-05-29 10:49:45 -0400 | [diff] [blame] | 682 | |
| 683 | const gl::State &glState = context->getGLState(); |
| 684 | vk::CommandGraphResource *drawFramebuffer = vk::GetImpl(glState.getDrawFramebuffer()); |
Jamie Madill | 2106102 | 2018-07-12 23:56:30 -0400 | [diff] [blame] | 685 | updateElementArrayBufferReadDependency(drawFramebuffer, |
| 686 | contextVk->getRenderer()->getCurrentQueueSerial()); |
Jamie Madill | c3755fc | 2018-04-05 08:39:13 -0400 | [diff] [blame] | 687 | mIndexBufferDirty = false; |
Luc Ferron | 983c429 | 2018-04-10 13:05:45 -0400 | [diff] [blame] | 688 | |
| 689 | // If we've had a drawArrays call with a line loop before, we want to make sure this is |
| 690 | // invalidated the next time drawArrays is called since we use the same index buffer for |
| 691 | // both calls. |
| 692 | mLineLoopBufferFirstIndex.reset(); |
| 693 | mLineLoopBufferLastIndex.reset(); |
Jamie Madill | c3755fc | 2018-04-05 08:39:13 -0400 | [diff] [blame] | 694 | } |
| 695 | |
| 696 | return gl::NoError(); |
| 697 | } |
Jamie Madill | 5a4c932 | 2018-07-16 11:01:58 -0400 | [diff] [blame] | 698 | |
| 699 | void VertexArrayVk::updateDefaultAttrib(RendererVk *renderer, |
| 700 | size_t attribIndex, |
| 701 | VkBuffer bufferHandle, |
| 702 | uint32_t offset) |
| 703 | { |
| 704 | if (!mState.getEnabledAttributesMask().test(attribIndex)) |
| 705 | { |
| 706 | mCurrentArrayBufferHandles[attribIndex] = bufferHandle; |
| 707 | mCurrentArrayBufferOffsets[attribIndex] = offset; |
| 708 | mCurrentArrayBufferResources[attribIndex] = nullptr; |
| 709 | mCurrentArrayBufferStrides[attribIndex] = 0; |
| 710 | mCurrentArrayBufferFormats[attribIndex] = |
Jamie Madill | ba36593 | 2018-07-18 17:23:46 -0400 | [diff] [blame] | 711 | &renderer->getFormat(angle::FormatID::R32G32B32A32_FIXED); |
Jamie Madill | 5a4c932 | 2018-07-16 11:01:58 -0400 | [diff] [blame] | 712 | mDirtyPackedInputs.set(attribIndex); |
| 713 | } |
| 714 | } |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 715 | } // namespace rx |