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