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 | dd43e6c | 2017-03-24 14:18:49 -0400 | [diff] [blame] | 16 | #include "libANGLE/renderer/vulkan/ContextVk.h" |
Jamie Madill | ebf7299 | 2017-10-13 14:09:45 -0400 | [diff] [blame] | 17 | #include "libANGLE/renderer/vulkan/formatutilsvk.h" |
Jamie Madill | dd43e6c | 2017-03-24 14:18:49 -0400 | [diff] [blame] | 18 | |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 19 | namespace rx |
| 20 | { |
| 21 | |
Jamie Madill | bd159f0 | 2017-10-09 19:39:06 -0400 | [diff] [blame] | 22 | VertexArrayVk::VertexArrayVk(const gl::VertexArrayState &state) |
| 23 | : VertexArrayImpl(state), |
Jamie Madill | da854a2 | 2017-11-30 17:24:21 -0500 | [diff] [blame] | 24 | mCurrentArrayBufferHandles{}, |
| 25 | mCurrentArrayBufferResources{}, |
| 26 | mCurrentElementArrayBufferResource(nullptr), |
Jamie Madill | ebf7299 | 2017-10-13 14:09:45 -0400 | [diff] [blame] | 27 | mCurrentVertexDescsValid(false) |
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 | ebf7299 | 2017-10-13 14:09:45 -0400 | [diff] [blame] | 31 | mCurrentVertexBindingDescs.reserve(state.getMaxAttribs()); |
| 32 | mCurrentVertexAttribDescs.reserve(state.getMaxAttribs()); |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 33 | } |
| 34 | |
Jamie Madill | acf2f3a | 2017-11-21 19:22:44 -0500 | [diff] [blame] | 35 | VertexArrayVk::~VertexArrayVk() |
| 36 | { |
| 37 | } |
| 38 | |
Jamie Madill | 4928b7c | 2017-06-20 12:57:39 -0400 | [diff] [blame] | 39 | void VertexArrayVk::destroy(const gl::Context *context) |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 40 | { |
| 41 | } |
| 42 | |
Jamie Madill | c564c07 | 2017-06-01 12:45:42 -0400 | [diff] [blame] | 43 | void VertexArrayVk::syncState(const gl::Context *context, |
| 44 | const gl::VertexArray::DirtyBits &dirtyBits) |
Jamie Madill | dd43e6c | 2017-03-24 14:18:49 -0400 | [diff] [blame] | 45 | { |
| 46 | ASSERT(dirtyBits.any()); |
Jamie Madill | 7210656 | 2017-03-24 14:18:50 -0400 | [diff] [blame] | 47 | |
Jamie Madill | bd159f0 | 2017-10-09 19:39:06 -0400 | [diff] [blame] | 48 | // Invalidate current pipeline. |
Jamie Madill | 7210656 | 2017-03-24 14:18:50 -0400 | [diff] [blame] | 49 | // TODO(jmadill): Use pipeline cache. |
Jamie Madill | acf2f3a | 2017-11-21 19:22:44 -0500 | [diff] [blame] | 50 | ContextVk *contextVk = vk::GetImpl(context); |
Jamie Madill | 7210656 | 2017-03-24 14:18:50 -0400 | [diff] [blame] | 51 | contextVk->invalidateCurrentPipeline(); |
Jamie Madill | bd159f0 | 2017-10-09 19:39:06 -0400 | [diff] [blame] | 52 | |
Jamie Madill | ebf7299 | 2017-10-13 14:09:45 -0400 | [diff] [blame] | 53 | // Invalidate the vertex descriptions. |
| 54 | invalidateVertexDescriptions(); |
| 55 | |
Jamie Madill | bd159f0 | 2017-10-09 19:39:06 -0400 | [diff] [blame] | 56 | // Rebuild current attribute buffers cache. This will fail horribly if the buffer changes. |
| 57 | // TODO(jmadill): Handle buffer storage changes. |
| 58 | const auto &attribs = mState.getVertexAttributes(); |
| 59 | const auto &bindings = mState.getVertexBindings(); |
| 60 | |
| 61 | for (auto dirtyBit : dirtyBits) |
| 62 | { |
| 63 | if (dirtyBit == gl::VertexArray::DIRTY_BIT_ELEMENT_ARRAY_BUFFER) |
Jamie Madill | da854a2 | 2017-11-30 17:24:21 -0500 | [diff] [blame] | 64 | { |
| 65 | gl::Buffer *bufferGL = mState.getElementArrayBuffer().get(); |
| 66 | if (bufferGL) |
| 67 | { |
| 68 | mCurrentElementArrayBufferResource = vk::GetImpl(bufferGL); |
| 69 | } |
| 70 | else |
| 71 | { |
| 72 | mCurrentElementArrayBufferResource = nullptr; |
| 73 | } |
Jamie Madill | bd159f0 | 2017-10-09 19:39:06 -0400 | [diff] [blame] | 74 | continue; |
Jamie Madill | da854a2 | 2017-11-30 17:24:21 -0500 | [diff] [blame] | 75 | } |
Jamie Madill | bd159f0 | 2017-10-09 19:39:06 -0400 | [diff] [blame] | 76 | |
| 77 | size_t attribIndex = gl::VertexArray::GetVertexIndexFromDirtyBit(dirtyBit); |
| 78 | |
| 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 | |
| 110 | void VertexArrayVk::updateCurrentBufferSerials(const gl::AttributesMask &activeAttribsMask, |
Jamie Madill | da854a2 | 2017-11-30 17:24:21 -0500 | [diff] [blame] | 111 | Serial serial, |
| 112 | DrawType drawType) |
Jamie Madill | bd159f0 | 2017-10-09 19:39:06 -0400 | [diff] [blame] | 113 | { |
Jamie Madill | da854a2 | 2017-11-30 17:24:21 -0500 | [diff] [blame] | 114 | // Handle the bound array buffers. |
Jamie Madill | bd159f0 | 2017-10-09 19:39:06 -0400 | [diff] [blame] | 115 | for (auto attribIndex : activeAttribsMask) |
| 116 | { |
Jamie Madill | da854a2 | 2017-11-30 17:24:21 -0500 | [diff] [blame] | 117 | ASSERT(mCurrentArrayBufferResources[attribIndex]); |
| 118 | mCurrentArrayBufferResources[attribIndex]->setQueueSerial(serial); |
| 119 | } |
| 120 | |
| 121 | // Handle the bound element array buffer. |
| 122 | if (drawType == DrawType::Elements) |
| 123 | { |
| 124 | ASSERT(mCurrentElementArrayBufferResource); |
| 125 | mCurrentElementArrayBufferResource->setQueueSerial(serial); |
Jamie Madill | bd159f0 | 2017-10-09 19:39:06 -0400 | [diff] [blame] | 126 | } |
Jamie Madill | dd43e6c | 2017-03-24 14:18:49 -0400 | [diff] [blame] | 127 | } |
| 128 | |
Jamie Madill | ebf7299 | 2017-10-13 14:09:45 -0400 | [diff] [blame] | 129 | void VertexArrayVk::invalidateVertexDescriptions() |
| 130 | { |
| 131 | mCurrentVertexDescsValid = false; |
| 132 | mCurrentVertexBindingDescs.clear(); |
| 133 | mCurrentVertexAttribDescs.clear(); |
| 134 | } |
| 135 | |
| 136 | void VertexArrayVk::updateVertexDescriptions(const gl::Context *context) |
| 137 | { |
| 138 | if (mCurrentVertexDescsValid) |
| 139 | { |
| 140 | return; |
| 141 | } |
| 142 | |
| 143 | const auto &attribs = mState.getVertexAttributes(); |
| 144 | const auto &bindings = mState.getVertexBindings(); |
| 145 | |
| 146 | const gl::Program *programGL = context->getGLState().getProgram(); |
| 147 | |
| 148 | for (auto attribIndex : programGL->getActiveAttribLocationsMask()) |
| 149 | { |
| 150 | const auto &attrib = attribs[attribIndex]; |
| 151 | const auto &binding = bindings[attrib.bindingIndex]; |
| 152 | if (attrib.enabled) |
| 153 | { |
| 154 | VkVertexInputBindingDescription bindingDesc; |
| 155 | bindingDesc.binding = static_cast<uint32_t>(mCurrentVertexBindingDescs.size()); |
| 156 | bindingDesc.stride = static_cast<uint32_t>(gl::ComputeVertexAttributeTypeSize(attrib)); |
| 157 | bindingDesc.inputRate = (binding.getDivisor() > 0 ? VK_VERTEX_INPUT_RATE_INSTANCE |
| 158 | : VK_VERTEX_INPUT_RATE_VERTEX); |
| 159 | |
| 160 | gl::VertexFormatType vertexFormatType = gl::GetVertexFormatType(attrib); |
| 161 | |
| 162 | VkVertexInputAttributeDescription attribDesc; |
| 163 | attribDesc.binding = bindingDesc.binding; |
| 164 | attribDesc.format = vk::GetNativeVertexFormat(vertexFormatType); |
| 165 | attribDesc.location = static_cast<uint32_t>(attribIndex); |
| 166 | attribDesc.offset = |
| 167 | static_cast<uint32_t>(ComputeVertexAttributeOffset(attrib, binding)); |
| 168 | |
| 169 | mCurrentVertexBindingDescs.push_back(bindingDesc); |
| 170 | mCurrentVertexAttribDescs.push_back(attribDesc); |
| 171 | } |
| 172 | else |
| 173 | { |
| 174 | UNIMPLEMENTED(); |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | mCurrentVertexDescsValid = true; |
| 179 | } |
| 180 | |
| 181 | const std::vector<VkVertexInputBindingDescription> &VertexArrayVk::getVertexBindingDescs() const |
| 182 | { |
| 183 | return mCurrentVertexBindingDescs; |
| 184 | } |
| 185 | |
| 186 | const std::vector<VkVertexInputAttributeDescription> &VertexArrayVk::getVertexAttribDescs() const |
| 187 | { |
| 188 | return mCurrentVertexAttribDescs; |
| 189 | } |
| 190 | |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 191 | } // namespace rx |