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