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