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" |
Jamie Madill | bfe31c4 | 2018-10-25 17:03:47 -0400 | [diff] [blame] | 13 | #include "common/utilities.h" |
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 | { |
Shahbaz Youssefi | 8f1b7a6 | 2018-11-14 16:02:54 -0500 | [diff] [blame] | 26 | constexpr size_t kDynamicVertexDataSize = 1024 * 1024; |
| 27 | constexpr size_t kDynamicIndexDataSize = 1024 * 8; |
| 28 | constexpr size_t kMaxVertexFormatAlignment = 4; |
| 29 | constexpr VkBufferUsageFlags kVertexBufferUsageFlags = VK_BUFFER_USAGE_VERTEX_BUFFER_BIT | |
| 30 | VK_BUFFER_USAGE_UNIFORM_TEXEL_BUFFER_BIT | |
| 31 | VK_BUFFER_USAGE_STORAGE_TEXEL_BUFFER_BIT; |
| 32 | constexpr VkBufferUsageFlags kIndexBufferUsageFlags = VK_BUFFER_USAGE_INDEX_BUFFER_BIT | |
| 33 | VK_BUFFER_USAGE_UNIFORM_TEXEL_BUFFER_BIT | |
| 34 | VK_BUFFER_USAGE_STORAGE_TEXEL_BUFFER_BIT; |
Frank Henigman | e452382 | 2018-07-19 16:10:53 -0400 | [diff] [blame] | 35 | |
| 36 | bool BindingIsAligned(const gl::VertexBinding &binding, unsigned componentSize) |
| 37 | { |
| 38 | return (binding.getOffset() % componentSize == 0) && (binding.getStride() % componentSize == 0); |
| 39 | } |
Jamie Madill | a064c27 | 2018-08-30 16:18:34 -0400 | [diff] [blame] | 40 | |
| 41 | angle::Result StreamVertexData(ContextVk *contextVk, |
| 42 | vk::DynamicBuffer *dynamicBuffer, |
| 43 | const uint8_t *sourceData, |
| 44 | size_t bytesToAllocate, |
| 45 | size_t destOffset, |
| 46 | size_t vertexCount, |
| 47 | size_t stride, |
| 48 | VertexCopyFunction vertexLoadFunction, |
| 49 | VkBuffer *bufferHandleOut, |
| 50 | VkDeviceSize *bufferOffsetOut) |
| 51 | { |
| 52 | uint8_t *dst = nullptr; |
| 53 | ANGLE_TRY(dynamicBuffer->allocate(contextVk, bytesToAllocate, &dst, bufferHandleOut, |
| 54 | bufferOffsetOut, nullptr)); |
| 55 | dst += destOffset; |
| 56 | vertexLoadFunction(sourceData, stride, vertexCount, dst); |
| 57 | |
| 58 | ANGLE_TRY(dynamicBuffer->flush(contextVk)); |
| 59 | return angle::Result::Continue(); |
| 60 | } |
jchen10 | 5dc0a6f | 2018-09-20 14:30:45 +0800 | [diff] [blame] | 61 | |
Jamie Madill | c3755fc | 2018-04-05 08:39:13 -0400 | [diff] [blame] | 62 | } // anonymous namespace |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 63 | |
Shahbaz Youssefi | 254b32c | 2018-11-26 11:58:03 -0500 | [diff] [blame] | 64 | #define INIT \ |
| 65 | { \ |
| 66 | kVertexBufferUsageFlags, 1024 * 8, true \ |
Frank Henigman | e452382 | 2018-07-19 16:10:53 -0400 | [diff] [blame] | 67 | } |
| 68 | |
Luc Ferron | a9ab0f3 | 2018-05-17 17:03:55 -0400 | [diff] [blame] | 69 | VertexArrayVk::VertexArrayVk(const gl::VertexArrayState &state, RendererVk *renderer) |
Jamie Madill | bd159f0 | 2017-10-09 19:39:06 -0400 | [diff] [blame] | 70 | : VertexArrayImpl(state), |
Jamie Madill | da854a2 | 2017-11-30 17:24:21 -0500 | [diff] [blame] | 71 | mCurrentArrayBufferHandles{}, |
Frank Henigman | 1744895 | 2017-01-05 15:48:26 -0500 | [diff] [blame] | 72 | mCurrentArrayBufferOffsets{}, |
Jamie Madill | 03d1a5e | 2018-11-12 11:34:24 -0500 | [diff] [blame] | 73 | mCurrentArrayBuffers{}, |
Frank Henigman | 419acc8 | 2018-06-24 19:57:31 -0400 | [diff] [blame] | 74 | mCurrentArrayBufferFormats{}, |
| 75 | mCurrentArrayBufferStrides{}, |
Frank Henigman | e452382 | 2018-07-19 16:10:53 -0400 | [diff] [blame] | 76 | mCurrentArrayBufferConversion{{ |
Jamie Madill | b980c56 | 2018-11-27 11:34:27 -0500 | [diff] [blame] | 77 | INIT, |
| 78 | INIT, |
| 79 | INIT, |
| 80 | INIT, |
| 81 | INIT, |
| 82 | INIT, |
| 83 | INIT, |
| 84 | INIT, |
| 85 | INIT, |
| 86 | INIT, |
| 87 | INIT, |
| 88 | INIT, |
| 89 | INIT, |
| 90 | INIT, |
| 91 | INIT, |
Frank Henigman | e452382 | 2018-07-19 16:10:53 -0400 | [diff] [blame] | 92 | INIT, |
| 93 | }}, |
| 94 | mCurrentArrayBufferConversionCanRelease{}, |
Jamie Madill | c3755fc | 2018-04-05 08:39:13 -0400 | [diff] [blame] | 95 | mCurrentElementArrayBufferOffset(0), |
Jamie Madill | 03d1a5e | 2018-11-12 11:34:24 -0500 | [diff] [blame] | 96 | mCurrentElementArrayBuffer(nullptr), |
Jamie Madill | 77b2436 | 2018-11-05 22:39:29 -0500 | [diff] [blame] | 97 | mPackedInputBindings{}, |
| 98 | mPackedInputAttributes{}, |
Shahbaz Youssefi | 254b32c | 2018-11-26 11:58:03 -0500 | [diff] [blame] | 99 | mDynamicVertexData(kVertexBufferUsageFlags, kDynamicVertexDataSize, true), |
| 100 | mDynamicIndexData(kIndexBufferUsageFlags, kDynamicIndexDataSize, true), |
| 101 | mTranslatedByteIndexData(kIndexBufferUsageFlags, kDynamicIndexDataSize, true), |
Luc Ferron | a9ab0f3 | 2018-05-17 17:03:55 -0400 | [diff] [blame] | 102 | mLineLoopHelper(renderer), |
Jamie Madill | 88fc6da | 2018-08-30 16:18:36 -0400 | [diff] [blame] | 103 | mDirtyLineLoopTranslation(true) |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 104 | { |
Jamie Madill | da854a2 | 2017-11-30 17:24:21 -0500 | [diff] [blame] | 105 | mCurrentArrayBufferHandles.fill(VK_NULL_HANDLE); |
Frank Henigman | 1744895 | 2017-01-05 15:48:26 -0500 | [diff] [blame] | 106 | mCurrentArrayBufferOffsets.fill(0); |
Jamie Madill | 03d1a5e | 2018-11-12 11:34:24 -0500 | [diff] [blame] | 107 | mCurrentArrayBuffers.fill(nullptr); |
Jamie Madill | 112a3a8 | 2018-01-23 13:04:06 -0500 | [diff] [blame] | 108 | |
Frank Henigman | e452382 | 2018-07-19 16:10:53 -0400 | [diff] [blame] | 109 | for (vk::DynamicBuffer &buffer : mCurrentArrayBufferConversion) |
| 110 | { |
jchen10 | 5dc0a6f | 2018-09-20 14:30:45 +0800 | [diff] [blame] | 111 | buffer.init(kMaxVertexFormatAlignment, renderer); |
Frank Henigman | e452382 | 2018-07-19 16:10:53 -0400 | [diff] [blame] | 112 | } |
jchen10 | 5dc0a6f | 2018-09-20 14:30:45 +0800 | [diff] [blame] | 113 | mDynamicVertexData.init(kMaxVertexFormatAlignment, renderer); |
Luc Ferron | a9ab0f3 | 2018-05-17 17:03:55 -0400 | [diff] [blame] | 114 | mDynamicIndexData.init(1, renderer); |
Luc Ferron | 6ed167a | 2018-06-13 13:45:55 -0400 | [diff] [blame] | 115 | mTranslatedByteIndexData.init(1, renderer); |
Jamie Madill | 0a17e48 | 2018-08-31 17:19:11 -0400 | [diff] [blame] | 116 | |
| 117 | // Initially consider all inputs dirty. |
| 118 | mDirtyPackedInputs.set(); |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 119 | } |
| 120 | |
Jamie Madill | b980c56 | 2018-11-27 11:34:27 -0500 | [diff] [blame] | 121 | VertexArrayVk::~VertexArrayVk() {} |
Jamie Madill | acf2f3a | 2017-11-21 19:22:44 -0500 | [diff] [blame] | 122 | |
Jamie Madill | 4928b7c | 2017-06-20 12:57:39 -0400 | [diff] [blame] | 123 | void VertexArrayVk::destroy(const gl::Context *context) |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 124 | { |
Shahbaz Youssefi | fca8fd6 | 2018-11-13 13:55:48 -0500 | [diff] [blame] | 125 | RendererVk *renderer = vk::GetImpl(context)->getRenderer(); |
| 126 | |
Frank Henigman | e452382 | 2018-07-19 16:10:53 -0400 | [diff] [blame] | 127 | for (vk::DynamicBuffer &buffer : mCurrentArrayBufferConversion) |
| 128 | { |
Shahbaz Youssefi | fca8fd6 | 2018-11-13 13:55:48 -0500 | [diff] [blame] | 129 | buffer.release(renderer); |
Frank Henigman | e452382 | 2018-07-19 16:10:53 -0400 | [diff] [blame] | 130 | } |
Shahbaz Youssefi | fca8fd6 | 2018-11-13 13:55:48 -0500 | [diff] [blame] | 131 | mDynamicVertexData.release(renderer); |
| 132 | mDynamicIndexData.release(renderer); |
| 133 | mTranslatedByteIndexData.release(renderer); |
| 134 | mLineLoopHelper.release(renderer); |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 135 | } |
| 136 | |
Jamie Madill | 2106102 | 2018-07-12 23:56:30 -0400 | [diff] [blame] | 137 | angle::Result VertexArrayVk::streamIndexData(ContextVk *contextVk, |
Jamie Madill | 8dc27f9 | 2018-11-29 11:45:44 -0500 | [diff] [blame] | 138 | gl::DrawElementsType indexType, |
Jamie Madill | 253038d | 2018-08-30 16:18:35 -0400 | [diff] [blame] | 139 | size_t indexCount, |
| 140 | const void *sourcePointer, |
| 141 | vk::DynamicBuffer *dynamicBuffer) |
Jamie Madill | c3755fc | 2018-04-05 08:39:13 -0400 | [diff] [blame] | 142 | { |
Jamie Madill | 8dc27f9 | 2018-11-29 11:45:44 -0500 | [diff] [blame] | 143 | ASSERT(!mState.getElementArrayBuffer() || indexType == gl::DrawElementsType::UnsignedByte); |
Jamie Madill | c3755fc | 2018-04-05 08:39:13 -0400 | [diff] [blame] | 144 | |
Jamie Madill | 253038d | 2018-08-30 16:18:35 -0400 | [diff] [blame] | 145 | dynamicBuffer->releaseRetainedBuffers(contextVk->getRenderer()); |
Jamie Madill | a064c27 | 2018-08-30 16:18:34 -0400 | [diff] [blame] | 146 | |
Jamie Madill | 253038d | 2018-08-30 16:18:35 -0400 | [diff] [blame] | 147 | const size_t amount = sizeof(GLushort) * indexCount; |
| 148 | GLubyte *dst = nullptr; |
Jamie Madill | c3755fc | 2018-04-05 08:39:13 -0400 | [diff] [blame] | 149 | |
Shahbaz Youssefi | 4abdf74 | 2018-11-28 14:41:10 -0500 | [diff] [blame] | 150 | ANGLE_TRY(dynamicBuffer->allocate(contextVk, amount, &dst, nullptr, |
Jamie Madill | 253038d | 2018-08-30 16:18:35 -0400 | [diff] [blame] | 151 | &mCurrentElementArrayBufferOffset, nullptr)); |
Shahbaz Youssefi | 4abdf74 | 2018-11-28 14:41:10 -0500 | [diff] [blame] | 152 | mCurrentElementArrayBuffer = dynamicBuffer->getCurrentBuffer(); |
Jamie Madill | 8dc27f9 | 2018-11-29 11:45:44 -0500 | [diff] [blame] | 153 | if (indexType == gl::DrawElementsType::UnsignedByte) |
Jamie Madill | c3755fc | 2018-04-05 08:39:13 -0400 | [diff] [blame] | 154 | { |
| 155 | // Unsigned bytes don't have direct support in Vulkan so we have to expand the |
| 156 | // memory to a GLushort. |
Jamie Madill | 253038d | 2018-08-30 16:18:35 -0400 | [diff] [blame] | 157 | const GLubyte *in = static_cast<const GLubyte *>(sourcePointer); |
Jamie Madill | c3755fc | 2018-04-05 08:39:13 -0400 | [diff] [blame] | 158 | GLushort *expandedDst = reinterpret_cast<GLushort *>(dst); |
Jamie Madill | 253038d | 2018-08-30 16:18:35 -0400 | [diff] [blame] | 159 | for (size_t index = 0; index < indexCount; index++) |
Jamie Madill | c3755fc | 2018-04-05 08:39:13 -0400 | [diff] [blame] | 160 | { |
| 161 | expandedDst[index] = static_cast<GLushort>(in[index]); |
| 162 | } |
| 163 | } |
| 164 | else |
| 165 | { |
Jamie Madill | 253038d | 2018-08-30 16:18:35 -0400 | [diff] [blame] | 166 | memcpy(dst, sourcePointer, amount); |
Jamie Madill | c3755fc | 2018-04-05 08:39:13 -0400 | [diff] [blame] | 167 | } |
Jamie Madill | 253038d | 2018-08-30 16:18:35 -0400 | [diff] [blame] | 168 | ANGLE_TRY(dynamicBuffer->flush(contextVk)); |
Jamie Madill | 2106102 | 2018-07-12 23:56:30 -0400 | [diff] [blame] | 169 | return angle::Result::Continue(); |
Frank Henigman | 1744895 | 2017-01-05 15:48:26 -0500 | [diff] [blame] | 170 | } |
| 171 | |
Frank Henigman | e452382 | 2018-07-19 16:10:53 -0400 | [diff] [blame] | 172 | // We assume the buffer is completely full of the same kind of data and convert |
| 173 | // and/or align it as we copy it to a DynamicBuffer. The assumption could be wrong |
| 174 | // but the alternative of copying it piecemeal on each draw would have a lot more |
| 175 | // overhead. |
Jamie Madill | a064c27 | 2018-08-30 16:18:34 -0400 | [diff] [blame] | 176 | angle::Result VertexArrayVk::convertVertexBuffer(ContextVk *contextVk, |
Frank Henigman | e452382 | 2018-07-19 16:10:53 -0400 | [diff] [blame] | 177 | BufferVk *srcBuffer, |
| 178 | const gl::VertexBinding &binding, |
| 179 | size_t attribIndex) |
| 180 | { |
Frank Henigman | 9d84ccb | 2018-09-12 18:09:02 -0400 | [diff] [blame] | 181 | // Needed before reading buffer or we could get stale data. |
Jamie Madill | a064c27 | 2018-08-30 16:18:34 -0400 | [diff] [blame] | 182 | ANGLE_TRY(contextVk->getRenderer()->finish(contextVk)); |
Frank Henigman | e452382 | 2018-07-19 16:10:53 -0400 | [diff] [blame] | 183 | |
| 184 | unsigned srcFormatSize = mCurrentArrayBufferFormats[attribIndex]->angleFormat().pixelBytes; |
Jamie Madill | a064c27 | 2018-08-30 16:18:34 -0400 | [diff] [blame] | 185 | unsigned dstFormatSize = mCurrentArrayBufferStrides[attribIndex]; |
| 186 | |
| 187 | mCurrentArrayBufferConversion[attribIndex].releaseRetainedBuffers(contextVk->getRenderer()); |
Frank Henigman | e452382 | 2018-07-19 16:10:53 -0400 | [diff] [blame] | 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 | |
Jamie Madill | 77abad8 | 2018-10-25 17:03:48 -0400 | [diff] [blame] | 202 | void *src = nullptr; |
Jamie Madill | a064c27 | 2018-08-30 16:18:34 -0400 | [diff] [blame] | 203 | ANGLE_TRY(srcBuffer->mapImpl(contextVk, &src)); |
| 204 | const uint8_t *srcBytes = reinterpret_cast<const uint8_t *>(src); |
| 205 | srcBytes += binding.getOffset(); |
jchen10 | 5dc0a6f | 2018-09-20 14:30:45 +0800 | [diff] [blame] | 206 | ASSERT(GetVertexInputAlignment(*mCurrentArrayBufferFormats[attribIndex]) <= |
| 207 | kMaxVertexFormatAlignment); |
Jamie Madill | a064c27 | 2018-08-30 16:18:34 -0400 | [diff] [blame] | 208 | ANGLE_TRY(StreamVertexData(contextVk, &mCurrentArrayBufferConversion[attribIndex], srcBytes, |
| 209 | numVertices * dstFormatSize, 0, numVertices, binding.getStride(), |
| 210 | mCurrentArrayBufferFormats[attribIndex]->vertexLoadFunction, |
| 211 | &mCurrentArrayBufferHandles[attribIndex], |
| 212 | &mCurrentArrayBufferOffsets[attribIndex])); |
| 213 | ANGLE_TRY(srcBuffer->unmapImpl(contextVk)); |
Frank Henigman | e452382 | 2018-07-19 16:10:53 -0400 | [diff] [blame] | 214 | |
| 215 | mCurrentArrayBufferConversionCanRelease[attribIndex] = true; |
Frank Henigman | e452382 | 2018-07-19 16:10:53 -0400 | [diff] [blame] | 216 | |
| 217 | return angle::Result::Continue(); |
| 218 | } |
| 219 | |
| 220 | void VertexArrayVk::ensureConversionReleased(RendererVk *renderer, size_t attribIndex) |
| 221 | { |
| 222 | if (mCurrentArrayBufferConversionCanRelease[attribIndex]) |
| 223 | { |
| 224 | mCurrentArrayBufferConversion[attribIndex].release(renderer); |
| 225 | mCurrentArrayBufferConversionCanRelease[attribIndex] = false; |
| 226 | } |
| 227 | } |
| 228 | |
| 229 | #define ANGLE_VERTEX_DIRTY_ATTRIB_FUNC(INDEX) \ |
| 230 | case gl::VertexArray::DIRTY_BIT_ATTRIB_0 + INDEX: \ |
| 231 | ANGLE_TRY(syncDirtyAttrib(contextVk, attribs[INDEX], \ |
| 232 | bindings[attribs[INDEX].bindingIndex], INDEX)); \ |
Jamie Madill | bc5834c | 2018-11-06 11:13:50 -0500 | [diff] [blame] | 233 | invalidateContext = true; \ |
Jamie Madill | a56467e | 2018-04-11 16:19:41 -0400 | [diff] [blame] | 234 | break; |
| 235 | |
Frank Henigman | e452382 | 2018-07-19 16:10:53 -0400 | [diff] [blame] | 236 | #define ANGLE_VERTEX_DIRTY_BINDING_FUNC(INDEX) \ |
| 237 | case gl::VertexArray::DIRTY_BIT_BINDING_0 + INDEX: \ |
| 238 | ANGLE_TRY(syncDirtyAttrib(contextVk, attribs[INDEX], \ |
| 239 | bindings[attribs[INDEX].bindingIndex], INDEX)); \ |
Jamie Madill | bc5834c | 2018-11-06 11:13:50 -0500 | [diff] [blame] | 240 | invalidateContext = true; \ |
Jamie Madill | a56467e | 2018-04-11 16:19:41 -0400 | [diff] [blame] | 241 | break; |
| 242 | |
Frank Henigman | e452382 | 2018-07-19 16:10:53 -0400 | [diff] [blame] | 243 | #define ANGLE_VERTEX_DIRTY_BUFFER_DATA_FUNC(INDEX) \ |
| 244 | case gl::VertexArray::DIRTY_BIT_BUFFER_DATA_0 + INDEX: \ |
| 245 | ANGLE_TRY(syncDirtyAttrib(contextVk, attribs[INDEX], \ |
| 246 | bindings[attribs[INDEX].bindingIndex], INDEX)); \ |
Jamie Madill | a56467e | 2018-04-11 16:19:41 -0400 | [diff] [blame] | 247 | break; |
| 248 | |
Jamie Madill | 6f755b2 | 2018-10-09 12:48:54 -0400 | [diff] [blame] | 249 | angle::Result VertexArrayVk::syncState(const gl::Context *context, |
| 250 | const gl::VertexArray::DirtyBits &dirtyBits, |
| 251 | const gl::VertexArray::DirtyAttribBitsArray &attribBits, |
| 252 | const gl::VertexArray::DirtyBindingBitsArray &bindingBits) |
Jamie Madill | dd43e6c | 2017-03-24 14:18:49 -0400 | [diff] [blame] | 253 | { |
| 254 | ASSERT(dirtyBits.any()); |
Jamie Madill | 7210656 | 2017-03-24 14:18:50 -0400 | [diff] [blame] | 255 | |
Jamie Madill | bc5834c | 2018-11-06 11:13:50 -0500 | [diff] [blame] | 256 | bool invalidateContext = false; |
Jamie Madill | c3755fc | 2018-04-05 08:39:13 -0400 | [diff] [blame] | 257 | |
Jamie Madill | 5a4c932 | 2018-07-16 11:01:58 -0400 | [diff] [blame] | 258 | ContextVk *contextVk = vk::GetImpl(context); |
Jamie Madill | bd159f0 | 2017-10-09 19:39:06 -0400 | [diff] [blame] | 259 | |
| 260 | // Rebuild current attribute buffers cache. This will fail horribly if the buffer changes. |
| 261 | // TODO(jmadill): Handle buffer storage changes. |
| 262 | const auto &attribs = mState.getVertexAttributes(); |
| 263 | const auto &bindings = mState.getVertexBindings(); |
| 264 | |
Jamie Madill | 0946393 | 2018-04-04 05:26:59 -0400 | [diff] [blame] | 265 | for (size_t dirtyBit : dirtyBits) |
Jamie Madill | bd159f0 | 2017-10-09 19:39:06 -0400 | [diff] [blame] | 266 | { |
Jamie Madill | 0946393 | 2018-04-04 05:26:59 -0400 | [diff] [blame] | 267 | switch (dirtyBit) |
Jamie Madill | da854a2 | 2017-11-30 17:24:21 -0500 | [diff] [blame] | 268 | { |
Jamie Madill | 0946393 | 2018-04-04 05:26:59 -0400 | [diff] [blame] | 269 | case gl::VertexArray::DIRTY_BIT_ELEMENT_ARRAY_BUFFER: |
Jamie Madill | da854a2 | 2017-11-30 17:24:21 -0500 | [diff] [blame] | 270 | { |
Jamie Madill | cd0a0a3 | 2018-10-18 18:41:57 -0400 | [diff] [blame] | 271 | gl::Buffer *bufferGL = mState.getElementArrayBuffer(); |
Jamie Madill | 0946393 | 2018-04-04 05:26:59 -0400 | [diff] [blame] | 272 | if (bufferGL) |
| 273 | { |
Jamie Madill | 03d1a5e | 2018-11-12 11:34:24 -0500 | [diff] [blame] | 274 | BufferVk *bufferVk = vk::GetImpl(bufferGL); |
| 275 | mCurrentElementArrayBuffer = &bufferVk->getBuffer(); |
Jamie Madill | 0946393 | 2018-04-04 05:26:59 -0400 | [diff] [blame] | 276 | } |
| 277 | else |
| 278 | { |
Shahbaz Youssefi | 4abdf74 | 2018-11-28 14:41:10 -0500 | [diff] [blame] | 279 | mCurrentElementArrayBuffer = nullptr; |
Jamie Madill | 0946393 | 2018-04-04 05:26:59 -0400 | [diff] [blame] | 280 | } |
Luc Ferron | 387b3b3 | 2018-05-28 10:11:57 -0400 | [diff] [blame] | 281 | |
| 282 | mCurrentElementArrayBufferOffset = 0; |
| 283 | mLineLoopBufferFirstIndex.reset(); |
| 284 | mLineLoopBufferLastIndex.reset(); |
Jamie Madill | 88fc6da | 2018-08-30 16:18:36 -0400 | [diff] [blame] | 285 | contextVk->setIndexBufferDirty(); |
Jamie Madill | b8e3966 | 2018-04-04 11:41:42 -0400 | [diff] [blame] | 286 | mDirtyLineLoopTranslation = true; |
Jamie Madill | 0946393 | 2018-04-04 05:26:59 -0400 | [diff] [blame] | 287 | break; |
Jamie Madill | da854a2 | 2017-11-30 17:24:21 -0500 | [diff] [blame] | 288 | } |
Jamie Madill | 0946393 | 2018-04-04 05:26:59 -0400 | [diff] [blame] | 289 | |
| 290 | case gl::VertexArray::DIRTY_BIT_ELEMENT_ARRAY_BUFFER_DATA: |
Jamie Madill | c3755fc | 2018-04-05 08:39:13 -0400 | [diff] [blame] | 291 | mLineLoopBufferFirstIndex.reset(); |
| 292 | mLineLoopBufferLastIndex.reset(); |
Jamie Madill | b8e3966 | 2018-04-04 11:41:42 -0400 | [diff] [blame] | 293 | mDirtyLineLoopTranslation = true; |
Jamie Madill | 0946393 | 2018-04-04 05:26:59 -0400 | [diff] [blame] | 294 | break; |
| 295 | |
Jamie Madill | a56467e | 2018-04-11 16:19:41 -0400 | [diff] [blame] | 296 | ANGLE_VERTEX_INDEX_CASES(ANGLE_VERTEX_DIRTY_ATTRIB_FUNC); |
| 297 | ANGLE_VERTEX_INDEX_CASES(ANGLE_VERTEX_DIRTY_BINDING_FUNC); |
| 298 | ANGLE_VERTEX_INDEX_CASES(ANGLE_VERTEX_DIRTY_BUFFER_DATA_FUNC); |
| 299 | |
Jamie Madill | 0946393 | 2018-04-04 05:26:59 -0400 | [diff] [blame] | 300 | default: |
Jamie Madill | a56467e | 2018-04-11 16:19:41 -0400 | [diff] [blame] | 301 | UNREACHABLE(); |
Jamie Madill | 0946393 | 2018-04-04 05:26:59 -0400 | [diff] [blame] | 302 | break; |
Jamie Madill | bd159f0 | 2017-10-09 19:39:06 -0400 | [diff] [blame] | 303 | } |
| 304 | } |
Frank Henigman | 0af5b86 | 2018-03-27 20:19:33 -0400 | [diff] [blame] | 305 | |
Jamie Madill | bc5834c | 2018-11-06 11:13:50 -0500 | [diff] [blame] | 306 | if (invalidateContext) |
Jamie Madill | c3755fc | 2018-04-05 08:39:13 -0400 | [diff] [blame] | 307 | { |
Jamie Madill | bc5834c | 2018-11-06 11:13:50 -0500 | [diff] [blame] | 308 | contextVk->invalidateVertexAndIndexBuffers(); |
Jamie Madill | c3755fc | 2018-04-05 08:39:13 -0400 | [diff] [blame] | 309 | } |
| 310 | |
Jamie Madill | 6f755b2 | 2018-10-09 12:48:54 -0400 | [diff] [blame] | 311 | return angle::Result::Continue(); |
Jamie Madill | bd159f0 | 2017-10-09 19:39:06 -0400 | [diff] [blame] | 312 | } |
| 313 | |
Frank Henigman | e452382 | 2018-07-19 16:10:53 -0400 | [diff] [blame] | 314 | angle::Result VertexArrayVk::syncDirtyAttrib(ContextVk *contextVk, |
| 315 | const gl::VertexAttribute &attrib, |
| 316 | const gl::VertexBinding &binding, |
| 317 | size_t attribIndex) |
Jamie Madill | a56467e | 2018-04-11 16:19:41 -0400 | [diff] [blame] | 318 | { |
| 319 | // Invalidate the input description for pipelines. |
| 320 | mDirtyPackedInputs.set(attribIndex); |
| 321 | |
Frank Henigman | 67c388e | 2018-07-19 19:16:11 -0400 | [diff] [blame] | 322 | RendererVk *renderer = contextVk->getRenderer(); |
Frank Henigman | e452382 | 2018-07-19 16:10:53 -0400 | [diff] [blame] | 323 | bool releaseConversion = true; |
Jamie Madill | 5a4c932 | 2018-07-16 11:01:58 -0400 | [diff] [blame] | 324 | |
Jamie Madill | a56467e | 2018-04-11 16:19:41 -0400 | [diff] [blame] | 325 | if (attrib.enabled) |
| 326 | { |
Frank Henigman | 67c388e | 2018-07-19 19:16:11 -0400 | [diff] [blame] | 327 | gl::Buffer *bufferGL = binding.getBuffer().get(); |
Frank Henigman | 419acc8 | 2018-06-24 19:57:31 -0400 | [diff] [blame] | 328 | mCurrentArrayBufferFormats[attribIndex] = &renderer->getFormat(GetVertexFormatID(attrib)); |
Frank Henigman | e452382 | 2018-07-19 16:10:53 -0400 | [diff] [blame] | 329 | |
Jamie Madill | a56467e | 2018-04-11 16:19:41 -0400 | [diff] [blame] | 330 | if (bufferGL) |
| 331 | { |
Frank Henigman | e452382 | 2018-07-19 16:10:53 -0400 | [diff] [blame] | 332 | BufferVk *bufferVk = vk::GetImpl(bufferGL); |
| 333 | unsigned componentSize = |
| 334 | mCurrentArrayBufferFormats[attribIndex]->angleFormat().pixelBytes / attrib.size; |
Frank Henigman | 419acc8 | 2018-06-24 19:57:31 -0400 | [diff] [blame] | 335 | |
Frank Henigman | e452382 | 2018-07-19 16:10:53 -0400 | [diff] [blame] | 336 | if (mCurrentArrayBufferFormats[attribIndex]->vertexLoadRequiresConversion || |
| 337 | !BindingIsAligned(binding, componentSize)) |
| 338 | { |
Jamie Madill | a064c27 | 2018-08-30 16:18:34 -0400 | [diff] [blame] | 339 | mCurrentArrayBufferStrides[attribIndex] = |
| 340 | mCurrentArrayBufferFormats[attribIndex]->bufferFormat().pixelBytes; |
| 341 | |
Frank Henigman | e452382 | 2018-07-19 16:10:53 -0400 | [diff] [blame] | 342 | ANGLE_TRY(convertVertexBuffer(contextVk, bufferVk, binding, attribIndex)); |
| 343 | |
Jamie Madill | 03d1a5e | 2018-11-12 11:34:24 -0500 | [diff] [blame] | 344 | mCurrentArrayBuffers[attribIndex] = nullptr; |
| 345 | releaseConversion = false; |
Frank Henigman | e452382 | 2018-07-19 16:10:53 -0400 | [diff] [blame] | 346 | } |
| 347 | else |
| 348 | { |
Jamie Madill | 03d1a5e | 2018-11-12 11:34:24 -0500 | [diff] [blame] | 349 | mCurrentArrayBuffers[attribIndex] = &bufferVk->getBuffer(); |
Jamie Madill | 2d03ff4 | 2018-09-27 15:04:26 -0400 | [diff] [blame] | 350 | mCurrentArrayBufferHandles[attribIndex] = |
| 351 | bufferVk->getBuffer().getBuffer().getHandle(); |
Jamie Madill | 77abad8 | 2018-10-25 17:03:48 -0400 | [diff] [blame] | 352 | mCurrentArrayBufferOffsets[attribIndex] = binding.getOffset(); |
| 353 | mCurrentArrayBufferStrides[attribIndex] = binding.getStride(); |
Frank Henigman | e452382 | 2018-07-19 16:10:53 -0400 | [diff] [blame] | 354 | } |
Jamie Madill | a56467e | 2018-04-11 16:19:41 -0400 | [diff] [blame] | 355 | } |
| 356 | else |
| 357 | { |
Jamie Madill | 03d1a5e | 2018-11-12 11:34:24 -0500 | [diff] [blame] | 358 | mCurrentArrayBuffers[attribIndex] = nullptr; |
| 359 | mCurrentArrayBufferHandles[attribIndex] = VK_NULL_HANDLE; |
| 360 | mCurrentArrayBufferOffsets[attribIndex] = 0; |
Frank Henigman | 419acc8 | 2018-06-24 19:57:31 -0400 | [diff] [blame] | 361 | mCurrentArrayBufferStrides[attribIndex] = |
Frank Henigman | e452382 | 2018-07-19 16:10:53 -0400 | [diff] [blame] | 362 | mCurrentArrayBufferFormats[attribIndex]->bufferFormat().pixelBytes; |
Jamie Madill | a56467e | 2018-04-11 16:19:41 -0400 | [diff] [blame] | 363 | } |
Jamie Madill | a56467e | 2018-04-11 16:19:41 -0400 | [diff] [blame] | 364 | } |
| 365 | else |
| 366 | { |
Jamie Madill | 5a4c932 | 2018-07-16 11:01:58 -0400 | [diff] [blame] | 367 | contextVk->invalidateDefaultAttribute(attribIndex); |
| 368 | |
| 369 | // These will be filled out by the ContextVk. |
Jamie Madill | 03d1a5e | 2018-11-12 11:34:24 -0500 | [diff] [blame] | 370 | mCurrentArrayBuffers[attribIndex] = nullptr; |
| 371 | mCurrentArrayBufferHandles[attribIndex] = VK_NULL_HANDLE; |
| 372 | mCurrentArrayBufferOffsets[attribIndex] = 0; |
| 373 | mCurrentArrayBufferStrides[attribIndex] = 0; |
Jamie Madill | 5a4c932 | 2018-07-16 11:01:58 -0400 | [diff] [blame] | 374 | mCurrentArrayBufferFormats[attribIndex] = |
Jamie Madill | ba36593 | 2018-07-18 17:23:46 -0400 | [diff] [blame] | 375 | &renderer->getFormat(angle::FormatID::R32G32B32A32_FLOAT); |
Jamie Madill | a56467e | 2018-04-11 16:19:41 -0400 | [diff] [blame] | 376 | } |
Frank Henigman | e452382 | 2018-07-19 16:10:53 -0400 | [diff] [blame] | 377 | |
| 378 | if (releaseConversion) |
| 379 | ensureConversionReleased(renderer, attribIndex); |
| 380 | |
| 381 | return angle::Result::Continue(); |
Jamie Madill | a56467e | 2018-04-11 16:19:41 -0400 | [diff] [blame] | 382 | } |
| 383 | |
Shahbaz Youssefi | c30f45d | 2018-11-12 16:37:59 -0500 | [diff] [blame] | 384 | void VertexArrayVk::getPackedInputDescriptions(vk::GraphicsPipelineDesc *pipelineDesc) |
Jamie Madill | ebf7299 | 2017-10-13 14:09:45 -0400 | [diff] [blame] | 385 | { |
Frank Henigman | 419acc8 | 2018-06-24 19:57:31 -0400 | [diff] [blame] | 386 | updatePackedInputDescriptions(); |
Jamie Madill | 112a3a8 | 2018-01-23 13:04:06 -0500 | [diff] [blame] | 387 | pipelineDesc->updateVertexInputInfo(mPackedInputBindings, mPackedInputAttributes); |
Jamie Madill | ebf7299 | 2017-10-13 14:09:45 -0400 | [diff] [blame] | 388 | } |
| 389 | |
Frank Henigman | 419acc8 | 2018-06-24 19:57:31 -0400 | [diff] [blame] | 390 | void VertexArrayVk::updatePackedInputDescriptions() |
Jamie Madill | ebf7299 | 2017-10-13 14:09:45 -0400 | [diff] [blame] | 391 | { |
Jamie Madill | 112a3a8 | 2018-01-23 13:04:06 -0500 | [diff] [blame] | 392 | if (!mDirtyPackedInputs.any()) |
Jamie Madill | ebf7299 | 2017-10-13 14:09:45 -0400 | [diff] [blame] | 393 | { |
| 394 | return; |
| 395 | } |
| 396 | |
| 397 | const auto &attribs = mState.getVertexAttributes(); |
| 398 | const auto &bindings = mState.getVertexBindings(); |
| 399 | |
Jamie Madill | 112a3a8 | 2018-01-23 13:04:06 -0500 | [diff] [blame] | 400 | for (auto attribIndex : mDirtyPackedInputs) |
Jamie Madill | ebf7299 | 2017-10-13 14:09:45 -0400 | [diff] [blame] | 401 | { |
| 402 | const auto &attrib = attribs[attribIndex]; |
| 403 | const auto &binding = bindings[attrib.bindingIndex]; |
| 404 | if (attrib.enabled) |
| 405 | { |
Frank Henigman | 419acc8 | 2018-06-24 19:57:31 -0400 | [diff] [blame] | 406 | updatePackedInputInfo(static_cast<uint32_t>(attribIndex), binding, attrib); |
Jamie Madill | ebf7299 | 2017-10-13 14:09:45 -0400 | [diff] [blame] | 407 | } |
| 408 | else |
| 409 | { |
Jamie Madill | 5a4c932 | 2018-07-16 11:01:58 -0400 | [diff] [blame] | 410 | vk::PackedVertexInputBindingDesc &bindingDesc = mPackedInputBindings[attribIndex]; |
| 411 | bindingDesc.stride = 0; |
| 412 | bindingDesc.inputRate = VK_VERTEX_INPUT_RATE_VERTEX; |
| 413 | |
Jamie Madill | 77b2436 | 2018-11-05 22:39:29 -0500 | [diff] [blame] | 414 | mPackedInputAttributes.formats[attribIndex] = |
| 415 | static_cast<uint16_t>(VK_FORMAT_R32G32B32A32_SFLOAT); |
| 416 | mPackedInputAttributes.offsets[attribIndex] = 0; |
Jamie Madill | ebf7299 | 2017-10-13 14:09:45 -0400 | [diff] [blame] | 417 | } |
| 418 | } |
| 419 | |
Jamie Madill | 112a3a8 | 2018-01-23 13:04:06 -0500 | [diff] [blame] | 420 | mDirtyPackedInputs.reset(); |
| 421 | } |
| 422 | |
Frank Henigman | 419acc8 | 2018-06-24 19:57:31 -0400 | [diff] [blame] | 423 | void VertexArrayVk::updatePackedInputInfo(uint32_t attribIndex, |
Jamie Madill | 112a3a8 | 2018-01-23 13:04:06 -0500 | [diff] [blame] | 424 | const gl::VertexBinding &binding, |
| 425 | const gl::VertexAttribute &attrib) |
| 426 | { |
| 427 | vk::PackedVertexInputBindingDesc &bindingDesc = mPackedInputBindings[attribIndex]; |
| 428 | |
Frank Henigman | 419acc8 | 2018-06-24 19:57:31 -0400 | [diff] [blame] | 429 | bindingDesc.stride = static_cast<uint16_t>(mCurrentArrayBufferStrides[attribIndex]); |
Jamie Madill | 112a3a8 | 2018-01-23 13:04:06 -0500 | [diff] [blame] | 430 | bindingDesc.inputRate = static_cast<uint16_t>( |
| 431 | binding.getDivisor() > 0 ? VK_VERTEX_INPUT_RATE_INSTANCE : VK_VERTEX_INPUT_RATE_VERTEX); |
| 432 | |
Frank Henigman | 419acc8 | 2018-06-24 19:57:31 -0400 | [diff] [blame] | 433 | VkFormat vkFormat = mCurrentArrayBufferFormats[attribIndex]->vkBufferFormat; |
Jamie Madill | 112a3a8 | 2018-01-23 13:04:06 -0500 | [diff] [blame] | 434 | ASSERT(vkFormat <= std::numeric_limits<uint16_t>::max()); |
Frank Henigman | 8c379f3 | 2018-06-21 19:55:05 -0400 | [diff] [blame] | 435 | if (vkFormat == VK_FORMAT_UNDEFINED) |
| 436 | { |
Frank Henigman | 8c379f3 | 2018-06-21 19:55:05 -0400 | [diff] [blame] | 437 | UNIMPLEMENTED(); |
| 438 | } |
Jamie Madill | 112a3a8 | 2018-01-23 13:04:06 -0500 | [diff] [blame] | 439 | |
Jamie Madill | 77b2436 | 2018-11-05 22:39:29 -0500 | [diff] [blame] | 440 | mPackedInputAttributes.formats[attribIndex] = static_cast<uint8_t>(vkFormat); |
| 441 | mPackedInputAttributes.offsets[attribIndex] = static_cast<uint16_t>(attrib.relativeOffset); |
Jamie Madill | ebf7299 | 2017-10-13 14:09:45 -0400 | [diff] [blame] | 442 | } |
| 443 | |
Jamie Madill | 88fc6da | 2018-08-30 16:18:36 -0400 | [diff] [blame] | 444 | angle::Result VertexArrayVk::updateClientAttribs(const gl::Context *context, |
Jamie Madill | bfe31c4 | 2018-10-25 17:03:47 -0400 | [diff] [blame] | 445 | GLint firstVertex, |
| 446 | GLsizei vertexOrIndexCount, |
Jamie Madill | 8dc27f9 | 2018-11-29 11:45:44 -0500 | [diff] [blame] | 447 | gl::DrawElementsType indexTypeOrInvalid, |
Jamie Madill | bfe31c4 | 2018-10-25 17:03:47 -0400 | [diff] [blame] | 448 | const void *indices) |
Jamie Madill | c3755fc | 2018-04-05 08:39:13 -0400 | [diff] [blame] | 449 | { |
Jamie Madill | 77abad8 | 2018-10-25 17:03:48 -0400 | [diff] [blame] | 450 | ContextVk *contextVk = vk::GetImpl(context); |
Jamie Madill | 88fc6da | 2018-08-30 16:18:36 -0400 | [diff] [blame] | 451 | const gl::AttributesMask &clientAttribs = context->getStateCache().getActiveClientAttribsMask(); |
Jamie Madill | 2106102 | 2018-07-12 23:56:30 -0400 | [diff] [blame] | 452 | |
Jamie Madill | 88fc6da | 2018-08-30 16:18:36 -0400 | [diff] [blame] | 453 | ASSERT(clientAttribs.any()); |
Jamie Madill | c1fd737 | 2018-10-26 22:48:39 -0400 | [diff] [blame] | 454 | |
| 455 | GLint startVertex; |
| 456 | size_t vertexCount; |
Jamie Madill | 8dc27f9 | 2018-11-29 11:45:44 -0500 | [diff] [blame] | 457 | ANGLE_TRY(GetVertexRangeInfo(context, firstVertex, vertexOrIndexCount, indexTypeOrInvalid, |
| 458 | indices, 0, &startVertex, &vertexCount)); |
Jamie Madill | 88fc6da | 2018-08-30 16:18:36 -0400 | [diff] [blame] | 459 | |
| 460 | mDynamicVertexData.releaseRetainedBuffers(contextVk->getRenderer()); |
| 461 | |
| 462 | const auto &attribs = mState.getVertexAttributes(); |
| 463 | const auto &bindings = mState.getVertexBindings(); |
| 464 | |
| 465 | // TODO(fjhenigman): When we have a bunch of interleaved attributes, they end up |
| 466 | // un-interleaved, wasting space and copying time. Consider improving on that. |
| 467 | for (size_t attribIndex : clientAttribs) |
| 468 | { |
| 469 | const gl::VertexAttribute &attrib = attribs[attribIndex]; |
| 470 | const gl::VertexBinding &binding = bindings[attrib.bindingIndex]; |
| 471 | ASSERT(attrib.enabled && binding.getBuffer().get() == nullptr); |
| 472 | |
| 473 | const size_t bytesToAllocate = |
Jamie Madill | c1fd737 | 2018-10-26 22:48:39 -0400 | [diff] [blame] | 474 | (startVertex + vertexCount) * mCurrentArrayBufferStrides[attribIndex]; |
| 475 | const uint8_t *src = |
| 476 | static_cast<const uint8_t *>(attrib.pointer) + startVertex * binding.getStride(); |
Jamie Madill | 88fc6da | 2018-08-30 16:18:36 -0400 | [diff] [blame] | 477 | |
Jamie Madill | c1fd737 | 2018-10-26 22:48:39 -0400 | [diff] [blame] | 478 | size_t destOffset = startVertex * mCurrentArrayBufferStrides[attribIndex]; |
jchen10 | 5dc0a6f | 2018-09-20 14:30:45 +0800 | [diff] [blame] | 479 | ASSERT(GetVertexInputAlignment(*mCurrentArrayBufferFormats[attribIndex]) <= |
| 480 | kMaxVertexFormatAlignment); |
Jamie Madill | 88fc6da | 2018-08-30 16:18:36 -0400 | [diff] [blame] | 481 | |
| 482 | // Only vertexCount() vertices will be used by the upcoming draw. so that is all we copy. |
Jamie Madill | bfe31c4 | 2018-10-25 17:03:47 -0400 | [diff] [blame] | 483 | // We allocate space for startVertex + vertexCount so indexing will work. If we |
Jamie Madill | 88fc6da | 2018-08-30 16:18:36 -0400 | [diff] [blame] | 484 | // don't start at zero all the indices will be off. |
| 485 | // TODO(fjhenigman): See if we can account for indices being off by adjusting the |
| 486 | // offset, thus avoiding wasted memory. |
Jamie Madill | c1fd737 | 2018-10-26 22:48:39 -0400 | [diff] [blame] | 487 | ANGLE_TRY(StreamVertexData( |
| 488 | contextVk, &mDynamicVertexData, src, bytesToAllocate, destOffset, vertexCount, |
| 489 | binding.getStride(), mCurrentArrayBufferFormats[attribIndex]->vertexLoadFunction, |
| 490 | &mCurrentArrayBufferHandles[attribIndex], &mCurrentArrayBufferOffsets[attribIndex])); |
Jamie Madill | 88fc6da | 2018-08-30 16:18:36 -0400 | [diff] [blame] | 491 | } |
| 492 | |
| 493 | return angle::Result::Continue(); |
| 494 | } |
| 495 | |
| 496 | angle::Result VertexArrayVk::handleLineLoop(ContextVk *contextVk, |
Jamie Madill | bfe31c4 | 2018-10-25 17:03:47 -0400 | [diff] [blame] | 497 | GLint firstVertex, |
| 498 | GLsizei vertexOrIndexCount, |
Jamie Madill | 8dc27f9 | 2018-11-29 11:45:44 -0500 | [diff] [blame] | 499 | gl::DrawElementsType indexTypeOrInvalid, |
Jamie Madill | bfe31c4 | 2018-10-25 17:03:47 -0400 | [diff] [blame] | 500 | const void *indices) |
Jamie Madill | 88fc6da | 2018-08-30 16:18:36 -0400 | [diff] [blame] | 501 | { |
Jamie Madill | 8dc27f9 | 2018-11-29 11:45:44 -0500 | [diff] [blame] | 502 | if (indexTypeOrInvalid != gl::DrawElementsType::InvalidEnum) |
Jamie Madill | 88fc6da | 2018-08-30 16:18:36 -0400 | [diff] [blame] | 503 | { |
| 504 | // Handle GL_LINE_LOOP drawElements. |
| 505 | if (mDirtyLineLoopTranslation) |
| 506 | { |
Jamie Madill | cd0a0a3 | 2018-10-18 18:41:57 -0400 | [diff] [blame] | 507 | gl::Buffer *elementArrayBuffer = mState.getElementArrayBuffer(); |
Jamie Madill | 88fc6da | 2018-08-30 16:18:36 -0400 | [diff] [blame] | 508 | |
| 509 | if (!elementArrayBuffer) |
| 510 | { |
Frank Henigman | 85c4b43 | 2018-09-19 23:35:00 -0400 | [diff] [blame] | 511 | ANGLE_TRY(mLineLoopHelper.streamIndices( |
Jamie Madill | 8dc27f9 | 2018-11-29 11:45:44 -0500 | [diff] [blame] | 512 | contextVk, indexTypeOrInvalid, vertexOrIndexCount, |
Shahbaz Youssefi | 4abdf74 | 2018-11-28 14:41:10 -0500 | [diff] [blame] | 513 | reinterpret_cast<const uint8_t *>(indices), &mCurrentElementArrayBuffer, |
Jamie Madill | bfe31c4 | 2018-10-25 17:03:47 -0400 | [diff] [blame] | 514 | &mCurrentElementArrayBufferOffset)); |
Jamie Madill | 88fc6da | 2018-08-30 16:18:36 -0400 | [diff] [blame] | 515 | } |
| 516 | else |
| 517 | { |
| 518 | // When using an element array buffer, 'indices' is an offset to the first element. |
Jamie Madill | bfe31c4 | 2018-10-25 17:03:47 -0400 | [diff] [blame] | 519 | intptr_t offset = reinterpret_cast<intptr_t>(indices); |
Jamie Madill | 88fc6da | 2018-08-30 16:18:36 -0400 | [diff] [blame] | 520 | BufferVk *elementArrayBufferVk = vk::GetImpl(elementArrayBuffer); |
| 521 | ANGLE_TRY(mLineLoopHelper.getIndexBufferForElementArrayBuffer( |
Jamie Madill | 8dc27f9 | 2018-11-29 11:45:44 -0500 | [diff] [blame] | 522 | contextVk, elementArrayBufferVk, indexTypeOrInvalid, vertexOrIndexCount, offset, |
Shahbaz Youssefi | 4abdf74 | 2018-11-28 14:41:10 -0500 | [diff] [blame] | 523 | &mCurrentElementArrayBuffer, &mCurrentElementArrayBufferOffset)); |
Jamie Madill | 88fc6da | 2018-08-30 16:18:36 -0400 | [diff] [blame] | 524 | } |
| 525 | } |
| 526 | |
| 527 | // If we've had a drawArrays call with a line loop before, we want to make sure this is |
| 528 | // invalidated the next time drawArrays is called since we use the same index buffer for |
| 529 | // both calls. |
| 530 | mLineLoopBufferFirstIndex.reset(); |
| 531 | mLineLoopBufferLastIndex.reset(); |
| 532 | return angle::Result::Continue(); |
| 533 | } |
Jamie Madill | c3755fc | 2018-04-05 08:39:13 -0400 | [diff] [blame] | 534 | |
Jamie Madill | 18e323a | 2018-05-11 16:54:17 -0400 | [diff] [blame] | 535 | // Note: Vertex indexes can be arbitrarily large. |
Jamie Madill | bfe31c4 | 2018-10-25 17:03:47 -0400 | [diff] [blame] | 536 | uint32_t clampedVertexCount = gl::clampCast<uint32_t>(vertexOrIndexCount); |
Jamie Madill | 18e323a | 2018-05-11 16:54:17 -0400 | [diff] [blame] | 537 | |
Jamie Madill | c3755fc | 2018-04-05 08:39:13 -0400 | [diff] [blame] | 538 | // Handle GL_LINE_LOOP drawArrays. |
Jamie Madill | bfe31c4 | 2018-10-25 17:03:47 -0400 | [diff] [blame] | 539 | size_t lastVertex = static_cast<size_t>(firstVertex + clampedVertexCount); |
Jamie Madill | c3755fc | 2018-04-05 08:39:13 -0400 | [diff] [blame] | 540 | if (!mLineLoopBufferFirstIndex.valid() || !mLineLoopBufferLastIndex.valid() || |
Jamie Madill | bfe31c4 | 2018-10-25 17:03:47 -0400 | [diff] [blame] | 541 | mLineLoopBufferFirstIndex != firstVertex || mLineLoopBufferLastIndex != lastVertex) |
Jamie Madill | c3755fc | 2018-04-05 08:39:13 -0400 | [diff] [blame] | 542 | { |
Jamie Madill | bfe31c4 | 2018-10-25 17:03:47 -0400 | [diff] [blame] | 543 | ANGLE_TRY(mLineLoopHelper.getIndexBufferForDrawArrays( |
Shahbaz Youssefi | 4abdf74 | 2018-11-28 14:41:10 -0500 | [diff] [blame] | 544 | contextVk, clampedVertexCount, firstVertex, &mCurrentElementArrayBuffer, |
Jamie Madill | bfe31c4 | 2018-10-25 17:03:47 -0400 | [diff] [blame] | 545 | &mCurrentElementArrayBufferOffset)); |
Jamie Madill | c3755fc | 2018-04-05 08:39:13 -0400 | [diff] [blame] | 546 | |
Jamie Madill | bfe31c4 | 2018-10-25 17:03:47 -0400 | [diff] [blame] | 547 | mLineLoopBufferFirstIndex = firstVertex; |
Jamie Madill | c3755fc | 2018-04-05 08:39:13 -0400 | [diff] [blame] | 548 | mLineLoopBufferLastIndex = lastVertex; |
| 549 | } |
| 550 | |
Jamie Madill | 88fc6da | 2018-08-30 16:18:36 -0400 | [diff] [blame] | 551 | return angle::Result::Continue(); |
Jamie Madill | c3755fc | 2018-04-05 08:39:13 -0400 | [diff] [blame] | 552 | } |
| 553 | |
Jamie Madill | 88fc6da | 2018-08-30 16:18:36 -0400 | [diff] [blame] | 554 | angle::Result VertexArrayVk::updateIndexTranslation(ContextVk *contextVk, |
Jamie Madill | bfe31c4 | 2018-10-25 17:03:47 -0400 | [diff] [blame] | 555 | GLsizei indexCount, |
Jamie Madill | 8dc27f9 | 2018-11-29 11:45:44 -0500 | [diff] [blame] | 556 | gl::DrawElementsType type, |
Jamie Madill | bfe31c4 | 2018-10-25 17:03:47 -0400 | [diff] [blame] | 557 | const void *indices) |
Jamie Madill | c3755fc | 2018-04-05 08:39:13 -0400 | [diff] [blame] | 558 | { |
Jamie Madill | 8dc27f9 | 2018-11-29 11:45:44 -0500 | [diff] [blame] | 559 | ASSERT(type != gl::DrawElementsType::InvalidEnum); |
Jamie Madill | c3755fc | 2018-04-05 08:39:13 -0400 | [diff] [blame] | 560 | |
Shahbaz Youssefi | 8f1b7a6 | 2018-11-14 16:02:54 -0500 | [diff] [blame] | 561 | RendererVk *renderer = contextVk->getRenderer(); |
Jamie Madill | cd0a0a3 | 2018-10-18 18:41:57 -0400 | [diff] [blame] | 562 | gl::Buffer *glBuffer = mState.getElementArrayBuffer(); |
Jamie Madill | c3755fc | 2018-04-05 08:39:13 -0400 | [diff] [blame] | 563 | |
Jamie Madill | 88fc6da | 2018-08-30 16:18:36 -0400 | [diff] [blame] | 564 | if (!glBuffer) |
Jamie Madill | c3755fc | 2018-04-05 08:39:13 -0400 | [diff] [blame] | 565 | { |
Jamie Madill | bfe31c4 | 2018-10-25 17:03:47 -0400 | [diff] [blame] | 566 | ANGLE_TRY(streamIndexData(contextVk, type, indexCount, indices, &mDynamicIndexData)); |
Jamie Madill | c3755fc | 2018-04-05 08:39:13 -0400 | [diff] [blame] | 567 | } |
Shahbaz Youssefi | 8f1b7a6 | 2018-11-14 16:02:54 -0500 | [diff] [blame] | 568 | else if (renderer->getFormat(angle::FormatID::R16_UINT).vkSupportsStorageBuffer) |
| 569 | { |
| 570 | BufferVk *bufferVk = vk::GetImpl(glBuffer); |
| 571 | |
| 572 | ASSERT(type == gl::DrawElementsType::UnsignedByte); |
| 573 | |
| 574 | intptr_t offsetIntoSrcData = reinterpret_cast<intptr_t>(indices); |
| 575 | size_t srcDataSize = static_cast<size_t>(bufferVk->getSize()) - offsetIntoSrcData; |
| 576 | |
| 577 | mTranslatedByteIndexData.releaseRetainedBuffers(renderer); |
| 578 | |
| 579 | ANGLE_TRY(mTranslatedByteIndexData.allocate(contextVk, sizeof(GLushort) * srcDataSize, |
| 580 | nullptr, nullptr, |
| 581 | &mCurrentElementArrayBufferOffset, nullptr)); |
| 582 | mCurrentElementArrayBuffer = mTranslatedByteIndexData.getCurrentBuffer(); |
| 583 | |
| 584 | vk::BufferHelper *dest = mTranslatedByteIndexData.getCurrentBuffer(); |
| 585 | vk::BufferHelper *src = &bufferVk->getBuffer(); |
| 586 | |
| 587 | ANGLE_TRY(src->initBufferView(contextVk, renderer->getFormat(angle::FormatID::R8_UINT))); |
| 588 | ANGLE_TRY(dest->initBufferView(contextVk, renderer->getFormat(angle::FormatID::R16_UINT))); |
| 589 | |
| 590 | // Copy relevant section of the source into destination at allocated offset. Note that the |
| 591 | // offset returned by allocate() above is in bytes, while our allocated array is of |
| 592 | // GLushorts. |
| 593 | DispatchUtilsVk::CopyParameters params = {}; |
| 594 | params.destOffset = |
| 595 | static_cast<size_t>(mCurrentElementArrayBufferOffset) / sizeof(GLushort); |
| 596 | params.srcOffset = offsetIntoSrcData; |
| 597 | params.size = srcDataSize; |
| 598 | |
| 599 | // Note: this is a copy, which implicitly converts between formats. Once support for |
| 600 | // primitive restart is added, a specialized shader is likely needed to special case 0xFF -> |
| 601 | // 0xFFFF. |
| 602 | ANGLE_TRY(renderer->getDispatchUtils()->copyBuffer(contextVk, dest, src, params)); |
| 603 | } |
Jamie Madill | 88fc6da | 2018-08-30 16:18:36 -0400 | [diff] [blame] | 604 | else |
Jamie Madill | c3755fc | 2018-04-05 08:39:13 -0400 | [diff] [blame] | 605 | { |
Shahbaz Youssefi | 8f1b7a6 | 2018-11-14 16:02:54 -0500 | [diff] [blame] | 606 | // If it's not possible to convert the buffer with compute, opt for a CPU read back for now. |
| 607 | // TODO(syoussefi): R8G8B8A8_UINT is required to have storage texel buffer support, so a |
| 608 | // specialized shader code can be made to read two ubyte indices and output them in R and B |
| 609 | // (or A and G based on endianness?) with 0 on the other channels. If specialized, we might |
| 610 | // as well support the ubyte to ushort case with correct handling of primitive restart. |
| 611 | // http://anglebug.com/3003 |
| 612 | |
Frank Henigman | 9d84ccb | 2018-09-12 18:09:02 -0400 | [diff] [blame] | 613 | // Needed before reading buffer or we could get stale data. |
Shahbaz Youssefi | 8f1b7a6 | 2018-11-14 16:02:54 -0500 | [diff] [blame] | 614 | ANGLE_TRY(renderer->finish(contextVk)); |
Frank Henigman | 9d84ccb | 2018-09-12 18:09:02 -0400 | [diff] [blame] | 615 | |
Jamie Madill | 8dc27f9 | 2018-11-29 11:45:44 -0500 | [diff] [blame] | 616 | ASSERT(type == gl::DrawElementsType::UnsignedByte); |
Jamie Madill | 88fc6da | 2018-08-30 16:18:36 -0400 | [diff] [blame] | 617 | // Unsigned bytes don't have direct support in Vulkan so we have to expand the |
| 618 | // memory to a GLushort. |
| 619 | BufferVk *bufferVk = vk::GetImpl(glBuffer); |
| 620 | void *srcDataMapping = nullptr; |
| 621 | ASSERT(!glBuffer->isMapped()); |
| 622 | ANGLE_TRY(bufferVk->mapImpl(contextVk, &srcDataMapping)); |
| 623 | uint8_t *srcData = static_cast<uint8_t *>(srcDataMapping); |
Jamie Madill | bfe31c4 | 2018-10-25 17:03:47 -0400 | [diff] [blame] | 624 | intptr_t offsetIntoSrcData = reinterpret_cast<intptr_t>(indices); |
Jamie Madill | 88fc6da | 2018-08-30 16:18:36 -0400 | [diff] [blame] | 625 | srcData += offsetIntoSrcData; |
Jamie Madill | 253038d | 2018-08-30 16:18:35 -0400 | [diff] [blame] | 626 | |
Jamie Madill | bfe31c4 | 2018-10-25 17:03:47 -0400 | [diff] [blame] | 627 | ANGLE_TRY(streamIndexData(contextVk, type, |
Jamie Madill | 88fc6da | 2018-08-30 16:18:36 -0400 | [diff] [blame] | 628 | static_cast<size_t>(bufferVk->getSize()) - offsetIntoSrcData, |
| 629 | srcData, &mTranslatedByteIndexData)); |
Luc Ferron | 6ed167a | 2018-06-13 13:45:55 -0400 | [diff] [blame] | 630 | |
Jamie Madill | 88fc6da | 2018-08-30 16:18:36 -0400 | [diff] [blame] | 631 | ANGLE_TRY(bufferVk->unmapImpl(contextVk)); |
Jamie Madill | c3755fc | 2018-04-05 08:39:13 -0400 | [diff] [blame] | 632 | } |
| 633 | |
Jamie Madill | 88fc6da | 2018-08-30 16:18:36 -0400 | [diff] [blame] | 634 | return angle::Result::Continue(); |
Jamie Madill | c3755fc | 2018-04-05 08:39:13 -0400 | [diff] [blame] | 635 | } |
Jamie Madill | 5a4c932 | 2018-07-16 11:01:58 -0400 | [diff] [blame] | 636 | |
| 637 | void VertexArrayVk::updateDefaultAttrib(RendererVk *renderer, |
| 638 | size_t attribIndex, |
| 639 | VkBuffer bufferHandle, |
| 640 | uint32_t offset) |
| 641 | { |
| 642 | if (!mState.getEnabledAttributesMask().test(attribIndex)) |
| 643 | { |
Jamie Madill | 03d1a5e | 2018-11-12 11:34:24 -0500 | [diff] [blame] | 644 | mCurrentArrayBufferHandles[attribIndex] = bufferHandle; |
| 645 | mCurrentArrayBufferOffsets[attribIndex] = offset; |
| 646 | mCurrentArrayBuffers[attribIndex] = nullptr; |
| 647 | mCurrentArrayBufferStrides[attribIndex] = 0; |
Jamie Madill | 5a4c932 | 2018-07-16 11:01:58 -0400 | [diff] [blame] | 648 | mCurrentArrayBufferFormats[attribIndex] = |
Jamie Madill | ba36593 | 2018-07-18 17:23:46 -0400 | [diff] [blame] | 649 | &renderer->getFormat(angle::FormatID::R32G32B32A32_FIXED); |
Jamie Madill | 5a4c932 | 2018-07-16 11:01:58 -0400 | [diff] [blame] | 650 | mDirtyPackedInputs.set(attribIndex); |
| 651 | } |
| 652 | } |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 653 | } // namespace rx |