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