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