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