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 | 3c424b4 | 2018-01-19 12:35:09 -0500 | [diff] [blame] | 18 | #include "libANGLE/renderer/vulkan/vk_format_utils.h" |
Jamie Madill | dd43e6c | 2017-03-24 14:18:49 -0400 | [diff] [blame] | 19 | |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 20 | namespace rx |
| 21 | { |
| 22 | |
Jamie Madill | bd159f0 | 2017-10-09 19:39:06 -0400 | [diff] [blame] | 23 | VertexArrayVk::VertexArrayVk(const gl::VertexArrayState &state) |
| 24 | : VertexArrayImpl(state), |
Jamie Madill | da854a2 | 2017-11-30 17:24:21 -0500 | [diff] [blame] | 25 | mCurrentArrayBufferHandles{}, |
Frank Henigman | 1744895 | 2017-01-05 15:48:26 -0500 | [diff] [blame] | 26 | mCurrentArrayBufferOffsets{}, |
Jamie Madill | da854a2 | 2017-11-30 17:24:21 -0500 | [diff] [blame] | 27 | mCurrentArrayBufferResources{}, |
Jamie Madill | 112a3a8 | 2018-01-23 13:04:06 -0500 | [diff] [blame] | 28 | mCurrentElementArrayBufferResource(nullptr) |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 29 | { |
Jamie Madill | da854a2 | 2017-11-30 17:24:21 -0500 | [diff] [blame] | 30 | mCurrentArrayBufferHandles.fill(VK_NULL_HANDLE); |
Frank Henigman | 1744895 | 2017-01-05 15:48:26 -0500 | [diff] [blame] | 31 | mCurrentArrayBufferOffsets.fill(0); |
Jamie Madill | da854a2 | 2017-11-30 17:24:21 -0500 | [diff] [blame] | 32 | mCurrentArrayBufferResources.fill(nullptr); |
Jamie Madill | 112a3a8 | 2018-01-23 13:04:06 -0500 | [diff] [blame] | 33 | |
| 34 | mPackedInputBindings.fill({0, 0}); |
| 35 | mPackedInputAttributes.fill({0, 0, 0}); |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 36 | } |
| 37 | |
Jamie Madill | acf2f3a | 2017-11-21 19:22:44 -0500 | [diff] [blame] | 38 | VertexArrayVk::~VertexArrayVk() |
| 39 | { |
| 40 | } |
| 41 | |
Jamie Madill | 4928b7c | 2017-06-20 12:57:39 -0400 | [diff] [blame] | 42 | void VertexArrayVk::destroy(const gl::Context *context) |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 43 | { |
| 44 | } |
| 45 | |
Frank Henigman | a53d0e1 | 2018-02-13 00:06:06 -0500 | [diff] [blame^] | 46 | gl::AttributesMask VertexArrayVk::attribsToStream(ContextVk *context) const |
| 47 | { |
| 48 | const gl::Program *programGL = context->getGLState().getProgram(); |
| 49 | return mClientMemoryAttribs & programGL->getActiveAttribLocationsMask(); |
| 50 | } |
| 51 | |
Frank Henigman | 1744895 | 2017-01-05 15:48:26 -0500 | [diff] [blame] | 52 | gl::Error VertexArrayVk::streamVertexData(ContextVk *context, |
| 53 | StreamingBuffer *stream, |
Frank Henigman | 6dd4a92 | 2018-03-02 16:35:13 -0500 | [diff] [blame] | 54 | size_t firstVertex, |
| 55 | size_t lastVertex) |
Frank Henigman | 1744895 | 2017-01-05 15:48:26 -0500 | [diff] [blame] | 56 | { |
| 57 | const auto &attribs = mState.getVertexAttributes(); |
| 58 | const auto &bindings = mState.getVertexBindings(); |
Frank Henigman | 1744895 | 2017-01-05 15:48:26 -0500 | [diff] [blame] | 59 | |
| 60 | // TODO(fjhenigman): When we have a bunch of interleaved attributes, they end up |
| 61 | // un-interleaved, wasting space and copying time. Consider improving on that. |
Frank Henigman | a53d0e1 | 2018-02-13 00:06:06 -0500 | [diff] [blame^] | 62 | for (auto attribIndex : attribsToStream(context)) |
Frank Henigman | 1744895 | 2017-01-05 15:48:26 -0500 | [diff] [blame] | 63 | { |
Frank Henigman | 6dd4a92 | 2018-03-02 16:35:13 -0500 | [diff] [blame] | 64 | const gl::VertexAttribute &attrib = attribs[attribIndex]; |
| 65 | const gl::VertexBinding &binding = bindings[attrib.bindingIndex]; |
| 66 | ASSERT(attrib.enabled && binding.getBuffer().get() == nullptr); |
Frank Henigman | 1744895 | 2017-01-05 15:48:26 -0500 | [diff] [blame] | 67 | |
Frank Henigman | 6dd4a92 | 2018-03-02 16:35:13 -0500 | [diff] [blame] | 68 | // TODO(fjhenigman): Work with more formats than just GL_FLOAT. |
| 69 | if (attrib.type != GL_FLOAT) |
Frank Henigman | 1744895 | 2017-01-05 15:48:26 -0500 | [diff] [blame] | 70 | { |
Frank Henigman | 6dd4a92 | 2018-03-02 16:35:13 -0500 | [diff] [blame] | 71 | UNIMPLEMENTED(); |
| 72 | return gl::InternalError(); |
Frank Henigman | 1744895 | 2017-01-05 15:48:26 -0500 | [diff] [blame] | 73 | } |
Frank Henigman | 6dd4a92 | 2018-03-02 16:35:13 -0500 | [diff] [blame] | 74 | |
| 75 | // Only [firstVertex, lastVertex] is needed by the upcoming draw so that |
| 76 | // is all we copy, but we allocate space for [0, lastVertex] so indexing |
| 77 | // will work. If we don't start at zero all the indices will be off. |
| 78 | // TODO(fjhenigman): See if we can account for indices being off by adjusting |
| 79 | // the offset, thus avoiding wasted memory. |
| 80 | const size_t firstByte = firstVertex * binding.getStride(); |
| 81 | const size_t lastByte = |
| 82 | lastVertex * binding.getStride() + gl::ComputeVertexAttributeTypeSize(attrib); |
| 83 | uint8_t *dst = nullptr; |
| 84 | ANGLE_TRY(stream->allocate(context, lastByte, &dst, |
| 85 | &mCurrentArrayBufferHandles[attribIndex], |
| 86 | &mCurrentArrayBufferOffsets[attribIndex])); |
| 87 | memcpy(dst + firstByte, static_cast<const uint8_t *>(attrib.pointer) + firstByte, |
| 88 | lastByte - firstByte); |
Frank Henigman | 1744895 | 2017-01-05 15:48:26 -0500 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | ANGLE_TRY(stream->flush(context)); |
| 92 | return gl::NoError(); |
| 93 | } |
| 94 | |
Jamie Madill | c564c07 | 2017-06-01 12:45:42 -0400 | [diff] [blame] | 95 | void VertexArrayVk::syncState(const gl::Context *context, |
| 96 | const gl::VertexArray::DirtyBits &dirtyBits) |
Jamie Madill | dd43e6c | 2017-03-24 14:18:49 -0400 | [diff] [blame] | 97 | { |
| 98 | ASSERT(dirtyBits.any()); |
Jamie Madill | 7210656 | 2017-03-24 14:18:50 -0400 | [diff] [blame] | 99 | |
Jamie Madill | bd159f0 | 2017-10-09 19:39:06 -0400 | [diff] [blame] | 100 | // Invalidate current pipeline. |
Jamie Madill | acf2f3a | 2017-11-21 19:22:44 -0500 | [diff] [blame] | 101 | ContextVk *contextVk = vk::GetImpl(context); |
Jamie Madill | 49ac74b | 2017-12-21 14:42:33 -0500 | [diff] [blame] | 102 | contextVk->onVertexArrayChange(); |
Jamie Madill | bd159f0 | 2017-10-09 19:39:06 -0400 | [diff] [blame] | 103 | |
| 104 | // Rebuild current attribute buffers cache. This will fail horribly if the buffer changes. |
| 105 | // TODO(jmadill): Handle buffer storage changes. |
| 106 | const auto &attribs = mState.getVertexAttributes(); |
| 107 | const auto &bindings = mState.getVertexBindings(); |
| 108 | |
| 109 | for (auto dirtyBit : dirtyBits) |
| 110 | { |
| 111 | if (dirtyBit == gl::VertexArray::DIRTY_BIT_ELEMENT_ARRAY_BUFFER) |
Jamie Madill | da854a2 | 2017-11-30 17:24:21 -0500 | [diff] [blame] | 112 | { |
| 113 | gl::Buffer *bufferGL = mState.getElementArrayBuffer().get(); |
| 114 | if (bufferGL) |
| 115 | { |
| 116 | mCurrentElementArrayBufferResource = vk::GetImpl(bufferGL); |
| 117 | } |
| 118 | else |
| 119 | { |
| 120 | mCurrentElementArrayBufferResource = nullptr; |
| 121 | } |
Jamie Madill | bd159f0 | 2017-10-09 19:39:06 -0400 | [diff] [blame] | 122 | continue; |
Jamie Madill | da854a2 | 2017-11-30 17:24:21 -0500 | [diff] [blame] | 123 | } |
Jamie Madill | bd159f0 | 2017-10-09 19:39:06 -0400 | [diff] [blame] | 124 | |
| 125 | size_t attribIndex = gl::VertexArray::GetVertexIndexFromDirtyBit(dirtyBit); |
| 126 | |
Jamie Madill | 112a3a8 | 2018-01-23 13:04:06 -0500 | [diff] [blame] | 127 | // Invalidate the input description for pipelines. |
| 128 | mDirtyPackedInputs.set(attribIndex); |
| 129 | |
Jamie Madill | bd159f0 | 2017-10-09 19:39:06 -0400 | [diff] [blame] | 130 | const auto &attrib = attribs[attribIndex]; |
| 131 | const auto &binding = bindings[attrib.bindingIndex]; |
Jamie Madill | ebf7299 | 2017-10-13 14:09:45 -0400 | [diff] [blame] | 132 | |
Jamie Madill | bd159f0 | 2017-10-09 19:39:06 -0400 | [diff] [blame] | 133 | if (attrib.enabled) |
| 134 | { |
| 135 | gl::Buffer *bufferGL = binding.getBuffer().get(); |
| 136 | |
| 137 | if (bufferGL) |
| 138 | { |
Jamie Madill | da854a2 | 2017-11-30 17:24:21 -0500 | [diff] [blame] | 139 | BufferVk *bufferVk = vk::GetImpl(bufferGL); |
| 140 | mCurrentArrayBufferResources[attribIndex] = bufferVk; |
| 141 | mCurrentArrayBufferHandles[attribIndex] = bufferVk->getVkBuffer().getHandle(); |
Frank Henigman | 6dd4a92 | 2018-03-02 16:35:13 -0500 | [diff] [blame] | 142 | mClientMemoryAttribs.reset(attribIndex); |
Jamie Madill | bd159f0 | 2017-10-09 19:39:06 -0400 | [diff] [blame] | 143 | } |
| 144 | else |
| 145 | { |
Jamie Madill | da854a2 | 2017-11-30 17:24:21 -0500 | [diff] [blame] | 146 | mCurrentArrayBufferResources[attribIndex] = nullptr; |
| 147 | mCurrentArrayBufferHandles[attribIndex] = VK_NULL_HANDLE; |
Frank Henigman | 6dd4a92 | 2018-03-02 16:35:13 -0500 | [diff] [blame] | 148 | mClientMemoryAttribs.set(attribIndex); |
Jamie Madill | bd159f0 | 2017-10-09 19:39:06 -0400 | [diff] [blame] | 149 | } |
Frank Henigman | 1744895 | 2017-01-05 15:48:26 -0500 | [diff] [blame] | 150 | // TODO(jmadill): Offset handling. Assume zero for now. |
| 151 | mCurrentArrayBufferOffsets[attribIndex] = 0; |
Jamie Madill | bd159f0 | 2017-10-09 19:39:06 -0400 | [diff] [blame] | 152 | } |
| 153 | else |
| 154 | { |
Frank Henigman | 6dd4a92 | 2018-03-02 16:35:13 -0500 | [diff] [blame] | 155 | mClientMemoryAttribs.reset(attribIndex); |
Jamie Madill | bd159f0 | 2017-10-09 19:39:06 -0400 | [diff] [blame] | 156 | UNIMPLEMENTED(); |
| 157 | } |
| 158 | } |
| 159 | } |
| 160 | |
Jamie Madill | da854a2 | 2017-11-30 17:24:21 -0500 | [diff] [blame] | 161 | const gl::AttribArray<VkBuffer> &VertexArrayVk::getCurrentArrayBufferHandles() const |
Jamie Madill | bd159f0 | 2017-10-09 19:39:06 -0400 | [diff] [blame] | 162 | { |
Jamie Madill | da854a2 | 2017-11-30 17:24:21 -0500 | [diff] [blame] | 163 | return mCurrentArrayBufferHandles; |
Jamie Madill | bd159f0 | 2017-10-09 19:39:06 -0400 | [diff] [blame] | 164 | } |
| 165 | |
Frank Henigman | 1744895 | 2017-01-05 15:48:26 -0500 | [diff] [blame] | 166 | const gl::AttribArray<VkDeviceSize> &VertexArrayVk::getCurrentArrayBufferOffsets() const |
| 167 | { |
| 168 | return mCurrentArrayBufferOffsets; |
| 169 | } |
| 170 | |
Jamie Madill | 1f46bc1 | 2018-02-20 16:09:43 -0500 | [diff] [blame] | 171 | void VertexArrayVk::updateDrawDependencies(vk::CommandGraphNode *readNode, |
Jamie Madill | 49ac74b | 2017-12-21 14:42:33 -0500 | [diff] [blame] | 172 | const gl::AttributesMask &activeAttribsMask, |
Luc Ferron | 78e39b3 | 2018-02-26 07:42:44 -0500 | [diff] [blame] | 173 | ResourceVk *elementArrayBufferOverride, |
Jamie Madill | 49ac74b | 2017-12-21 14:42:33 -0500 | [diff] [blame] | 174 | Serial serial, |
| 175 | DrawType drawType) |
Jamie Madill | bd159f0 | 2017-10-09 19:39:06 -0400 | [diff] [blame] | 176 | { |
Jamie Madill | da854a2 | 2017-11-30 17:24:21 -0500 | [diff] [blame] | 177 | // Handle the bound array buffers. |
Jamie Madill | bd159f0 | 2017-10-09 19:39:06 -0400 | [diff] [blame] | 178 | for (auto attribIndex : activeAttribsMask) |
| 179 | { |
Frank Henigman | 1744895 | 2017-01-05 15:48:26 -0500 | [diff] [blame] | 180 | if (mCurrentArrayBufferResources[attribIndex]) |
| 181 | mCurrentArrayBufferResources[attribIndex]->onReadResource(readNode, serial); |
Jamie Madill | da854a2 | 2017-11-30 17:24:21 -0500 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | // Handle the bound element array buffer. |
Frank Henigman | a53d0e1 | 2018-02-13 00:06:06 -0500 | [diff] [blame^] | 185 | if (drawType == DrawType::Elements && mCurrentElementArrayBufferResource) |
Jamie Madill | da854a2 | 2017-11-30 17:24:21 -0500 | [diff] [blame] | 186 | { |
Luc Ferron | 78e39b3 | 2018-02-26 07:42:44 -0500 | [diff] [blame] | 187 | if (elementArrayBufferOverride != nullptr) |
| 188 | { |
| 189 | elementArrayBufferOverride->onReadResource(readNode, serial); |
| 190 | } |
| 191 | else |
| 192 | { |
| 193 | ASSERT(mCurrentElementArrayBufferResource); |
| 194 | mCurrentElementArrayBufferResource->onReadResource(readNode, serial); |
| 195 | } |
Jamie Madill | bd159f0 | 2017-10-09 19:39:06 -0400 | [diff] [blame] | 196 | } |
Jamie Madill | dd43e6c | 2017-03-24 14:18:49 -0400 | [diff] [blame] | 197 | } |
| 198 | |
Jamie Madill | 112a3a8 | 2018-01-23 13:04:06 -0500 | [diff] [blame] | 199 | void VertexArrayVk::getPackedInputDescriptions(vk::PipelineDesc *pipelineDesc) |
Jamie Madill | ebf7299 | 2017-10-13 14:09:45 -0400 | [diff] [blame] | 200 | { |
Jamie Madill | 112a3a8 | 2018-01-23 13:04:06 -0500 | [diff] [blame] | 201 | updatePackedInputDescriptions(); |
| 202 | pipelineDesc->updateVertexInputInfo(mPackedInputBindings, mPackedInputAttributes); |
Jamie Madill | ebf7299 | 2017-10-13 14:09:45 -0400 | [diff] [blame] | 203 | } |
| 204 | |
Jamie Madill | 112a3a8 | 2018-01-23 13:04:06 -0500 | [diff] [blame] | 205 | void VertexArrayVk::updatePackedInputDescriptions() |
Jamie Madill | ebf7299 | 2017-10-13 14:09:45 -0400 | [diff] [blame] | 206 | { |
Jamie Madill | 112a3a8 | 2018-01-23 13:04:06 -0500 | [diff] [blame] | 207 | if (!mDirtyPackedInputs.any()) |
Jamie Madill | ebf7299 | 2017-10-13 14:09:45 -0400 | [diff] [blame] | 208 | { |
| 209 | return; |
| 210 | } |
| 211 | |
| 212 | const auto &attribs = mState.getVertexAttributes(); |
| 213 | const auto &bindings = mState.getVertexBindings(); |
| 214 | |
Jamie Madill | 112a3a8 | 2018-01-23 13:04:06 -0500 | [diff] [blame] | 215 | for (auto attribIndex : mDirtyPackedInputs) |
Jamie Madill | ebf7299 | 2017-10-13 14:09:45 -0400 | [diff] [blame] | 216 | { |
| 217 | const auto &attrib = attribs[attribIndex]; |
| 218 | const auto &binding = bindings[attrib.bindingIndex]; |
| 219 | if (attrib.enabled) |
| 220 | { |
Jamie Madill | 112a3a8 | 2018-01-23 13:04:06 -0500 | [diff] [blame] | 221 | updatePackedInputInfo(static_cast<uint32_t>(attribIndex), binding, attrib); |
Jamie Madill | ebf7299 | 2017-10-13 14:09:45 -0400 | [diff] [blame] | 222 | } |
| 223 | else |
| 224 | { |
| 225 | UNIMPLEMENTED(); |
| 226 | } |
| 227 | } |
| 228 | |
Jamie Madill | 112a3a8 | 2018-01-23 13:04:06 -0500 | [diff] [blame] | 229 | mDirtyPackedInputs.reset(); |
| 230 | } |
| 231 | |
| 232 | void VertexArrayVk::updatePackedInputInfo(uint32_t attribIndex, |
| 233 | const gl::VertexBinding &binding, |
| 234 | const gl::VertexAttribute &attrib) |
| 235 | { |
| 236 | vk::PackedVertexInputBindingDesc &bindingDesc = mPackedInputBindings[attribIndex]; |
| 237 | |
| 238 | size_t attribSize = gl::ComputeVertexAttributeTypeSize(attrib); |
| 239 | ASSERT(attribSize <= std::numeric_limits<uint16_t>::max()); |
| 240 | |
Frank Henigman | a8e868f | 2018-01-28 23:32:25 -0500 | [diff] [blame] | 241 | bindingDesc.stride = static_cast<uint16_t>(binding.getStride()); |
Jamie Madill | 112a3a8 | 2018-01-23 13:04:06 -0500 | [diff] [blame] | 242 | bindingDesc.inputRate = static_cast<uint16_t>( |
| 243 | binding.getDivisor() > 0 ? VK_VERTEX_INPUT_RATE_INSTANCE : VK_VERTEX_INPUT_RATE_VERTEX); |
| 244 | |
| 245 | gl::VertexFormatType vertexFormatType = gl::GetVertexFormatType(attrib); |
| 246 | VkFormat vkFormat = vk::GetNativeVertexFormat(vertexFormatType); |
| 247 | ASSERT(vkFormat <= std::numeric_limits<uint16_t>::max()); |
| 248 | |
| 249 | vk::PackedVertexInputAttributeDesc &attribDesc = mPackedInputAttributes[attribIndex]; |
| 250 | attribDesc.format = static_cast<uint16_t>(vkFormat); |
| 251 | attribDesc.location = static_cast<uint16_t>(attribIndex); |
| 252 | attribDesc.offset = static_cast<uint32_t>(ComputeVertexAttributeOffset(attrib, binding)); |
Jamie Madill | ebf7299 | 2017-10-13 14:09:45 -0400 | [diff] [blame] | 253 | } |
| 254 | |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 255 | } // namespace rx |