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{}, |
| 26 | mCurrentArrayBufferResources{}, |
Jamie Madill | 112a3a8 | 2018-01-23 13:04:06 -0500 | [diff] [blame] | 27 | mCurrentElementArrayBufferResource(nullptr) |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 28 | { |
Jamie Madill | da854a2 | 2017-11-30 17:24:21 -0500 | [diff] [blame] | 29 | mCurrentArrayBufferHandles.fill(VK_NULL_HANDLE); |
| 30 | mCurrentArrayBufferResources.fill(nullptr); |
Jamie Madill | 112a3a8 | 2018-01-23 13:04:06 -0500 | [diff] [blame] | 31 | |
| 32 | mPackedInputBindings.fill({0, 0}); |
| 33 | mPackedInputAttributes.fill({0, 0, 0}); |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 34 | } |
| 35 | |
Jamie Madill | acf2f3a | 2017-11-21 19:22:44 -0500 | [diff] [blame] | 36 | VertexArrayVk::~VertexArrayVk() |
| 37 | { |
| 38 | } |
| 39 | |
Jamie Madill | 4928b7c | 2017-06-20 12:57:39 -0400 | [diff] [blame] | 40 | void VertexArrayVk::destroy(const gl::Context *context) |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 41 | { |
| 42 | } |
| 43 | |
Jamie Madill | c564c07 | 2017-06-01 12:45:42 -0400 | [diff] [blame] | 44 | void VertexArrayVk::syncState(const gl::Context *context, |
| 45 | const gl::VertexArray::DirtyBits &dirtyBits) |
Jamie Madill | dd43e6c | 2017-03-24 14:18:49 -0400 | [diff] [blame] | 46 | { |
| 47 | ASSERT(dirtyBits.any()); |
Jamie Madill | 7210656 | 2017-03-24 14:18:50 -0400 | [diff] [blame] | 48 | |
Jamie Madill | bd159f0 | 2017-10-09 19:39:06 -0400 | [diff] [blame] | 49 | // Invalidate current pipeline. |
Jamie Madill | acf2f3a | 2017-11-21 19:22:44 -0500 | [diff] [blame] | 50 | ContextVk *contextVk = vk::GetImpl(context); |
Jamie Madill | 49ac74b | 2017-12-21 14:42:33 -0500 | [diff] [blame] | 51 | contextVk->onVertexArrayChange(); |
Jamie Madill | bd159f0 | 2017-10-09 19:39:06 -0400 | [diff] [blame] | 52 | |
| 53 | // Rebuild current attribute buffers cache. This will fail horribly if the buffer changes. |
| 54 | // TODO(jmadill): Handle buffer storage changes. |
| 55 | const auto &attribs = mState.getVertexAttributes(); |
| 56 | const auto &bindings = mState.getVertexBindings(); |
| 57 | |
| 58 | for (auto dirtyBit : dirtyBits) |
| 59 | { |
| 60 | if (dirtyBit == gl::VertexArray::DIRTY_BIT_ELEMENT_ARRAY_BUFFER) |
Jamie Madill | da854a2 | 2017-11-30 17:24:21 -0500 | [diff] [blame] | 61 | { |
| 62 | gl::Buffer *bufferGL = mState.getElementArrayBuffer().get(); |
| 63 | if (bufferGL) |
| 64 | { |
| 65 | mCurrentElementArrayBufferResource = vk::GetImpl(bufferGL); |
| 66 | } |
| 67 | else |
| 68 | { |
| 69 | mCurrentElementArrayBufferResource = nullptr; |
| 70 | } |
Jamie Madill | bd159f0 | 2017-10-09 19:39:06 -0400 | [diff] [blame] | 71 | continue; |
Jamie Madill | da854a2 | 2017-11-30 17:24:21 -0500 | [diff] [blame] | 72 | } |
Jamie Madill | bd159f0 | 2017-10-09 19:39:06 -0400 | [diff] [blame] | 73 | |
| 74 | size_t attribIndex = gl::VertexArray::GetVertexIndexFromDirtyBit(dirtyBit); |
| 75 | |
Jamie Madill | 112a3a8 | 2018-01-23 13:04:06 -0500 | [diff] [blame] | 76 | // Invalidate the input description for pipelines. |
| 77 | mDirtyPackedInputs.set(attribIndex); |
| 78 | |
Jamie Madill | bd159f0 | 2017-10-09 19:39:06 -0400 | [diff] [blame] | 79 | const auto &attrib = attribs[attribIndex]; |
| 80 | const auto &binding = bindings[attrib.bindingIndex]; |
Jamie Madill | ebf7299 | 2017-10-13 14:09:45 -0400 | [diff] [blame] | 81 | |
Jamie Madill | bd159f0 | 2017-10-09 19:39:06 -0400 | [diff] [blame] | 82 | if (attrib.enabled) |
| 83 | { |
| 84 | gl::Buffer *bufferGL = binding.getBuffer().get(); |
| 85 | |
| 86 | if (bufferGL) |
| 87 | { |
Jamie Madill | da854a2 | 2017-11-30 17:24:21 -0500 | [diff] [blame] | 88 | BufferVk *bufferVk = vk::GetImpl(bufferGL); |
| 89 | mCurrentArrayBufferResources[attribIndex] = bufferVk; |
| 90 | mCurrentArrayBufferHandles[attribIndex] = bufferVk->getVkBuffer().getHandle(); |
Jamie Madill | bd159f0 | 2017-10-09 19:39:06 -0400 | [diff] [blame] | 91 | } |
| 92 | else |
| 93 | { |
Jamie Madill | da854a2 | 2017-11-30 17:24:21 -0500 | [diff] [blame] | 94 | mCurrentArrayBufferResources[attribIndex] = nullptr; |
| 95 | mCurrentArrayBufferHandles[attribIndex] = VK_NULL_HANDLE; |
Jamie Madill | bd159f0 | 2017-10-09 19:39:06 -0400 | [diff] [blame] | 96 | } |
| 97 | } |
| 98 | else |
| 99 | { |
| 100 | UNIMPLEMENTED(); |
| 101 | } |
| 102 | } |
| 103 | } |
| 104 | |
Jamie Madill | da854a2 | 2017-11-30 17:24:21 -0500 | [diff] [blame] | 105 | const gl::AttribArray<VkBuffer> &VertexArrayVk::getCurrentArrayBufferHandles() const |
Jamie Madill | bd159f0 | 2017-10-09 19:39:06 -0400 | [diff] [blame] | 106 | { |
Jamie Madill | da854a2 | 2017-11-30 17:24:21 -0500 | [diff] [blame] | 107 | return mCurrentArrayBufferHandles; |
Jamie Madill | bd159f0 | 2017-10-09 19:39:06 -0400 | [diff] [blame] | 108 | } |
| 109 | |
Jamie Madill | 49ac74b | 2017-12-21 14:42:33 -0500 | [diff] [blame] | 110 | void VertexArrayVk::updateDrawDependencies(vk::CommandBufferNode *readNode, |
| 111 | const gl::AttributesMask &activeAttribsMask, |
| 112 | Serial serial, |
| 113 | DrawType drawType) |
Jamie Madill | bd159f0 | 2017-10-09 19:39:06 -0400 | [diff] [blame] | 114 | { |
Jamie Madill | da854a2 | 2017-11-30 17:24:21 -0500 | [diff] [blame] | 115 | // Handle the bound array buffers. |
Jamie Madill | bd159f0 | 2017-10-09 19:39:06 -0400 | [diff] [blame] | 116 | for (auto attribIndex : activeAttribsMask) |
| 117 | { |
Jamie Madill | da854a2 | 2017-11-30 17:24:21 -0500 | [diff] [blame] | 118 | ASSERT(mCurrentArrayBufferResources[attribIndex]); |
Jamie Madill | 0e65454 | 2018-02-07 14:50:06 -0500 | [diff] [blame] | 119 | mCurrentArrayBufferResources[attribIndex]->onReadResource(readNode, serial); |
Jamie Madill | da854a2 | 2017-11-30 17:24:21 -0500 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | // Handle the bound element array buffer. |
| 123 | if (drawType == DrawType::Elements) |
| 124 | { |
| 125 | ASSERT(mCurrentElementArrayBufferResource); |
Jamie Madill | 0e65454 | 2018-02-07 14:50:06 -0500 | [diff] [blame] | 126 | mCurrentElementArrayBufferResource->onReadResource(readNode, serial); |
Jamie Madill | bd159f0 | 2017-10-09 19:39:06 -0400 | [diff] [blame] | 127 | } |
Jamie Madill | dd43e6c | 2017-03-24 14:18:49 -0400 | [diff] [blame] | 128 | } |
| 129 | |
Jamie Madill | 112a3a8 | 2018-01-23 13:04:06 -0500 | [diff] [blame] | 130 | void VertexArrayVk::getPackedInputDescriptions(vk::PipelineDesc *pipelineDesc) |
Jamie Madill | ebf7299 | 2017-10-13 14:09:45 -0400 | [diff] [blame] | 131 | { |
Jamie Madill | 112a3a8 | 2018-01-23 13:04:06 -0500 | [diff] [blame] | 132 | updatePackedInputDescriptions(); |
| 133 | pipelineDesc->updateVertexInputInfo(mPackedInputBindings, mPackedInputAttributes); |
Jamie Madill | ebf7299 | 2017-10-13 14:09:45 -0400 | [diff] [blame] | 134 | } |
| 135 | |
Jamie Madill | 112a3a8 | 2018-01-23 13:04:06 -0500 | [diff] [blame] | 136 | void VertexArrayVk::updatePackedInputDescriptions() |
Jamie Madill | ebf7299 | 2017-10-13 14:09:45 -0400 | [diff] [blame] | 137 | { |
Jamie Madill | 112a3a8 | 2018-01-23 13:04:06 -0500 | [diff] [blame] | 138 | if (!mDirtyPackedInputs.any()) |
Jamie Madill | ebf7299 | 2017-10-13 14:09:45 -0400 | [diff] [blame] | 139 | { |
| 140 | return; |
| 141 | } |
| 142 | |
| 143 | const auto &attribs = mState.getVertexAttributes(); |
| 144 | const auto &bindings = mState.getVertexBindings(); |
| 145 | |
Jamie Madill | 112a3a8 | 2018-01-23 13:04:06 -0500 | [diff] [blame] | 146 | for (auto attribIndex : mDirtyPackedInputs) |
Jamie Madill | ebf7299 | 2017-10-13 14:09:45 -0400 | [diff] [blame] | 147 | { |
| 148 | const auto &attrib = attribs[attribIndex]; |
| 149 | const auto &binding = bindings[attrib.bindingIndex]; |
| 150 | if (attrib.enabled) |
| 151 | { |
Jamie Madill | 112a3a8 | 2018-01-23 13:04:06 -0500 | [diff] [blame] | 152 | updatePackedInputInfo(static_cast<uint32_t>(attribIndex), binding, attrib); |
Jamie Madill | ebf7299 | 2017-10-13 14:09:45 -0400 | [diff] [blame] | 153 | } |
| 154 | else |
| 155 | { |
| 156 | UNIMPLEMENTED(); |
| 157 | } |
| 158 | } |
| 159 | |
Jamie Madill | 112a3a8 | 2018-01-23 13:04:06 -0500 | [diff] [blame] | 160 | mDirtyPackedInputs.reset(); |
| 161 | } |
| 162 | |
| 163 | void VertexArrayVk::updatePackedInputInfo(uint32_t attribIndex, |
| 164 | const gl::VertexBinding &binding, |
| 165 | const gl::VertexAttribute &attrib) |
| 166 | { |
| 167 | vk::PackedVertexInputBindingDesc &bindingDesc = mPackedInputBindings[attribIndex]; |
| 168 | |
| 169 | size_t attribSize = gl::ComputeVertexAttributeTypeSize(attrib); |
| 170 | ASSERT(attribSize <= std::numeric_limits<uint16_t>::max()); |
| 171 | |
| 172 | bindingDesc.stride = static_cast<uint16_t>(attribSize); |
| 173 | bindingDesc.inputRate = static_cast<uint16_t>( |
| 174 | binding.getDivisor() > 0 ? VK_VERTEX_INPUT_RATE_INSTANCE : VK_VERTEX_INPUT_RATE_VERTEX); |
| 175 | |
| 176 | gl::VertexFormatType vertexFormatType = gl::GetVertexFormatType(attrib); |
| 177 | VkFormat vkFormat = vk::GetNativeVertexFormat(vertexFormatType); |
| 178 | ASSERT(vkFormat <= std::numeric_limits<uint16_t>::max()); |
| 179 | |
| 180 | vk::PackedVertexInputAttributeDesc &attribDesc = mPackedInputAttributes[attribIndex]; |
| 181 | attribDesc.format = static_cast<uint16_t>(vkFormat); |
| 182 | attribDesc.location = static_cast<uint16_t>(attribIndex); |
| 183 | attribDesc.offset = static_cast<uint32_t>(ComputeVertexAttributeOffset(attrib, binding)); |
Jamie Madill | ebf7299 | 2017-10-13 14:09:45 -0400 | [diff] [blame] | 184 | } |
| 185 | |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 186 | } // namespace rx |