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), |
| 24 | mCurrentVertexBufferHandlesCache(state.getMaxAttribs(), VK_NULL_HANDLE), |
Jamie Madill | ebf7299 | 2017-10-13 14:09:45 -0400 | [diff] [blame] | 25 | mCurrentVkBuffersCache(state.getMaxAttribs(), nullptr), |
| 26 | mCurrentVertexDescsValid(false) |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 27 | { |
Jamie Madill | ebf7299 | 2017-10-13 14:09:45 -0400 | [diff] [blame] | 28 | mCurrentVertexBindingDescs.reserve(state.getMaxAttribs()); |
| 29 | mCurrentVertexAttribDescs.reserve(state.getMaxAttribs()); |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 30 | } |
| 31 | |
Jamie Madill | 4928b7c | 2017-06-20 12:57:39 -0400 | [diff] [blame] | 32 | void VertexArrayVk::destroy(const gl::Context *context) |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 33 | { |
| 34 | } |
| 35 | |
Jamie Madill | c564c07 | 2017-06-01 12:45:42 -0400 | [diff] [blame] | 36 | void VertexArrayVk::syncState(const gl::Context *context, |
| 37 | const gl::VertexArray::DirtyBits &dirtyBits) |
Jamie Madill | dd43e6c | 2017-03-24 14:18:49 -0400 | [diff] [blame] | 38 | { |
| 39 | ASSERT(dirtyBits.any()); |
Jamie Madill | 7210656 | 2017-03-24 14:18:50 -0400 | [diff] [blame] | 40 | |
Jamie Madill | bd159f0 | 2017-10-09 19:39:06 -0400 | [diff] [blame] | 41 | // Invalidate current pipeline. |
Jamie Madill | 7210656 | 2017-03-24 14:18:50 -0400 | [diff] [blame] | 42 | // TODO(jmadill): Use pipeline cache. |
Jamie Madill | e1f3ad4 | 2017-10-28 23:00:42 -0400 | [diff] [blame] | 43 | auto contextVk = vk::GetImpl(context); |
Jamie Madill | 7210656 | 2017-03-24 14:18:50 -0400 | [diff] [blame] | 44 | contextVk->invalidateCurrentPipeline(); |
Jamie Madill | bd159f0 | 2017-10-09 19:39:06 -0400 | [diff] [blame] | 45 | |
Jamie Madill | ebf7299 | 2017-10-13 14:09:45 -0400 | [diff] [blame] | 46 | // Invalidate the vertex descriptions. |
| 47 | invalidateVertexDescriptions(); |
| 48 | |
Jamie Madill | bd159f0 | 2017-10-09 19:39:06 -0400 | [diff] [blame] | 49 | // Rebuild current attribute buffers cache. This will fail horribly if the buffer changes. |
| 50 | // TODO(jmadill): Handle buffer storage changes. |
| 51 | const auto &attribs = mState.getVertexAttributes(); |
| 52 | const auto &bindings = mState.getVertexBindings(); |
| 53 | |
| 54 | for (auto dirtyBit : dirtyBits) |
| 55 | { |
| 56 | if (dirtyBit == gl::VertexArray::DIRTY_BIT_ELEMENT_ARRAY_BUFFER) |
| 57 | continue; |
| 58 | |
| 59 | size_t attribIndex = gl::VertexArray::GetVertexIndexFromDirtyBit(dirtyBit); |
| 60 | |
| 61 | const auto &attrib = attribs[attribIndex]; |
| 62 | const auto &binding = bindings[attrib.bindingIndex]; |
Jamie Madill | ebf7299 | 2017-10-13 14:09:45 -0400 | [diff] [blame] | 63 | |
Jamie Madill | bd159f0 | 2017-10-09 19:39:06 -0400 | [diff] [blame] | 64 | if (attrib.enabled) |
| 65 | { |
| 66 | gl::Buffer *bufferGL = binding.getBuffer().get(); |
| 67 | |
| 68 | if (bufferGL) |
| 69 | { |
Jamie Madill | e1f3ad4 | 2017-10-28 23:00:42 -0400 | [diff] [blame] | 70 | BufferVk *bufferVk = vk::GetImpl(bufferGL); |
Jamie Madill | bd159f0 | 2017-10-09 19:39:06 -0400 | [diff] [blame] | 71 | mCurrentVkBuffersCache[attribIndex] = bufferVk; |
| 72 | mCurrentVertexBufferHandlesCache[attribIndex] = bufferVk->getVkBuffer().getHandle(); |
| 73 | } |
| 74 | else |
| 75 | { |
| 76 | mCurrentVkBuffersCache[attribIndex] = nullptr; |
| 77 | mCurrentVertexBufferHandlesCache[attribIndex] = VK_NULL_HANDLE; |
| 78 | } |
| 79 | } |
| 80 | else |
| 81 | { |
| 82 | UNIMPLEMENTED(); |
| 83 | } |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | const std::vector<VkBuffer> &VertexArrayVk::getCurrentVertexBufferHandlesCache() const |
| 88 | { |
| 89 | return mCurrentVertexBufferHandlesCache; |
| 90 | } |
| 91 | |
| 92 | void VertexArrayVk::updateCurrentBufferSerials(const gl::AttributesMask &activeAttribsMask, |
| 93 | Serial serial) |
| 94 | { |
| 95 | for (auto attribIndex : activeAttribsMask) |
| 96 | { |
| 97 | mCurrentVkBuffersCache[attribIndex]->setQueueSerial(serial); |
| 98 | } |
Jamie Madill | dd43e6c | 2017-03-24 14:18:49 -0400 | [diff] [blame] | 99 | } |
| 100 | |
Jamie Madill | ebf7299 | 2017-10-13 14:09:45 -0400 | [diff] [blame] | 101 | void VertexArrayVk::invalidateVertexDescriptions() |
| 102 | { |
| 103 | mCurrentVertexDescsValid = false; |
| 104 | mCurrentVertexBindingDescs.clear(); |
| 105 | mCurrentVertexAttribDescs.clear(); |
| 106 | } |
| 107 | |
| 108 | void VertexArrayVk::updateVertexDescriptions(const gl::Context *context) |
| 109 | { |
| 110 | if (mCurrentVertexDescsValid) |
| 111 | { |
| 112 | return; |
| 113 | } |
| 114 | |
| 115 | const auto &attribs = mState.getVertexAttributes(); |
| 116 | const auto &bindings = mState.getVertexBindings(); |
| 117 | |
| 118 | const gl::Program *programGL = context->getGLState().getProgram(); |
| 119 | |
| 120 | for (auto attribIndex : programGL->getActiveAttribLocationsMask()) |
| 121 | { |
| 122 | const auto &attrib = attribs[attribIndex]; |
| 123 | const auto &binding = bindings[attrib.bindingIndex]; |
| 124 | if (attrib.enabled) |
| 125 | { |
| 126 | VkVertexInputBindingDescription bindingDesc; |
| 127 | bindingDesc.binding = static_cast<uint32_t>(mCurrentVertexBindingDescs.size()); |
| 128 | bindingDesc.stride = static_cast<uint32_t>(gl::ComputeVertexAttributeTypeSize(attrib)); |
| 129 | bindingDesc.inputRate = (binding.getDivisor() > 0 ? VK_VERTEX_INPUT_RATE_INSTANCE |
| 130 | : VK_VERTEX_INPUT_RATE_VERTEX); |
| 131 | |
| 132 | gl::VertexFormatType vertexFormatType = gl::GetVertexFormatType(attrib); |
| 133 | |
| 134 | VkVertexInputAttributeDescription attribDesc; |
| 135 | attribDesc.binding = bindingDesc.binding; |
| 136 | attribDesc.format = vk::GetNativeVertexFormat(vertexFormatType); |
| 137 | attribDesc.location = static_cast<uint32_t>(attribIndex); |
| 138 | attribDesc.offset = |
| 139 | static_cast<uint32_t>(ComputeVertexAttributeOffset(attrib, binding)); |
| 140 | |
| 141 | mCurrentVertexBindingDescs.push_back(bindingDesc); |
| 142 | mCurrentVertexAttribDescs.push_back(attribDesc); |
| 143 | } |
| 144 | else |
| 145 | { |
| 146 | UNIMPLEMENTED(); |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | mCurrentVertexDescsValid = true; |
| 151 | } |
| 152 | |
| 153 | const std::vector<VkVertexInputBindingDescription> &VertexArrayVk::getVertexBindingDescs() const |
| 154 | { |
| 155 | return mCurrentVertexBindingDescs; |
| 156 | } |
| 157 | |
| 158 | const std::vector<VkVertexInputAttributeDescription> &VertexArrayVk::getVertexAttribDescs() const |
| 159 | { |
| 160 | return mCurrentVertexAttribDescs; |
| 161 | } |
| 162 | |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 163 | } // namespace rx |