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