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{}, |
| 27 | mCurrentElementArrayBufferResource(nullptr), |
Jamie Madill | ebf7299 | 2017-10-13 14:09:45 -0400 | [diff] [blame] | 28 | mCurrentVertexDescsValid(false) |
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); |
| 31 | mCurrentArrayBufferResources.fill(nullptr); |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 32 | } |
| 33 | |
Jamie Madill | acf2f3a | 2017-11-21 19:22:44 -0500 | [diff] [blame] | 34 | VertexArrayVk::~VertexArrayVk() |
| 35 | { |
| 36 | } |
| 37 | |
Jamie Madill | 4928b7c | 2017-06-20 12:57:39 -0400 | [diff] [blame] | 38 | void VertexArrayVk::destroy(const gl::Context *context) |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 39 | { |
| 40 | } |
| 41 | |
Jamie Madill | c564c07 | 2017-06-01 12:45:42 -0400 | [diff] [blame] | 42 | void VertexArrayVk::syncState(const gl::Context *context, |
| 43 | const gl::VertexArray::DirtyBits &dirtyBits) |
Jamie Madill | dd43e6c | 2017-03-24 14:18:49 -0400 | [diff] [blame] | 44 | { |
| 45 | ASSERT(dirtyBits.any()); |
Jamie Madill | 7210656 | 2017-03-24 14:18:50 -0400 | [diff] [blame] | 46 | |
Jamie Madill | bd159f0 | 2017-10-09 19:39:06 -0400 | [diff] [blame] | 47 | // Invalidate current pipeline. |
Jamie Madill | 7210656 | 2017-03-24 14:18:50 -0400 | [diff] [blame] | 48 | // TODO(jmadill): Use pipeline cache. |
Jamie Madill | acf2f3a | 2017-11-21 19:22:44 -0500 | [diff] [blame] | 49 | ContextVk *contextVk = vk::GetImpl(context); |
Jamie Madill | 49ac74b | 2017-12-21 14:42:33 -0500 | [diff] [blame] | 50 | contextVk->onVertexArrayChange(); |
Jamie Madill | bd159f0 | 2017-10-09 19:39:06 -0400 | [diff] [blame] | 51 | |
Jamie Madill | ebf7299 | 2017-10-13 14:09:45 -0400 | [diff] [blame] | 52 | // Invalidate the vertex descriptions. |
| 53 | invalidateVertexDescriptions(); |
| 54 | |
Jamie Madill | bd159f0 | 2017-10-09 19:39:06 -0400 | [diff] [blame] | 55 | // Rebuild current attribute buffers cache. This will fail horribly if the buffer changes. |
| 56 | // TODO(jmadill): Handle buffer storage changes. |
| 57 | const auto &attribs = mState.getVertexAttributes(); |
| 58 | const auto &bindings = mState.getVertexBindings(); |
| 59 | |
| 60 | for (auto dirtyBit : dirtyBits) |
| 61 | { |
| 62 | if (dirtyBit == gl::VertexArray::DIRTY_BIT_ELEMENT_ARRAY_BUFFER) |
Jamie Madill | da854a2 | 2017-11-30 17:24:21 -0500 | [diff] [blame] | 63 | { |
| 64 | gl::Buffer *bufferGL = mState.getElementArrayBuffer().get(); |
| 65 | if (bufferGL) |
| 66 | { |
| 67 | mCurrentElementArrayBufferResource = vk::GetImpl(bufferGL); |
| 68 | } |
| 69 | else |
| 70 | { |
| 71 | mCurrentElementArrayBufferResource = nullptr; |
| 72 | } |
Jamie Madill | bd159f0 | 2017-10-09 19:39:06 -0400 | [diff] [blame] | 73 | continue; |
Jamie Madill | da854a2 | 2017-11-30 17:24:21 -0500 | [diff] [blame] | 74 | } |
Jamie Madill | bd159f0 | 2017-10-09 19:39:06 -0400 | [diff] [blame] | 75 | |
| 76 | size_t attribIndex = gl::VertexArray::GetVertexIndexFromDirtyBit(dirtyBit); |
| 77 | |
| 78 | const auto &attrib = attribs[attribIndex]; |
| 79 | const auto &binding = bindings[attrib.bindingIndex]; |
Jamie Madill | ebf7299 | 2017-10-13 14:09:45 -0400 | [diff] [blame] | 80 | |
Jamie Madill | bd159f0 | 2017-10-09 19:39:06 -0400 | [diff] [blame] | 81 | if (attrib.enabled) |
| 82 | { |
| 83 | gl::Buffer *bufferGL = binding.getBuffer().get(); |
| 84 | |
| 85 | if (bufferGL) |
| 86 | { |
Jamie Madill | da854a2 | 2017-11-30 17:24:21 -0500 | [diff] [blame] | 87 | BufferVk *bufferVk = vk::GetImpl(bufferGL); |
| 88 | mCurrentArrayBufferResources[attribIndex] = bufferVk; |
| 89 | mCurrentArrayBufferHandles[attribIndex] = bufferVk->getVkBuffer().getHandle(); |
Jamie Madill | bd159f0 | 2017-10-09 19:39:06 -0400 | [diff] [blame] | 90 | } |
| 91 | else |
| 92 | { |
Jamie Madill | da854a2 | 2017-11-30 17:24:21 -0500 | [diff] [blame] | 93 | mCurrentArrayBufferResources[attribIndex] = nullptr; |
| 94 | mCurrentArrayBufferHandles[attribIndex] = VK_NULL_HANDLE; |
Jamie Madill | bd159f0 | 2017-10-09 19:39:06 -0400 | [diff] [blame] | 95 | } |
| 96 | } |
| 97 | else |
| 98 | { |
| 99 | UNIMPLEMENTED(); |
| 100 | } |
| 101 | } |
| 102 | } |
| 103 | |
Jamie Madill | da854a2 | 2017-11-30 17:24:21 -0500 | [diff] [blame] | 104 | const gl::AttribArray<VkBuffer> &VertexArrayVk::getCurrentArrayBufferHandles() const |
Jamie Madill | bd159f0 | 2017-10-09 19:39:06 -0400 | [diff] [blame] | 105 | { |
Jamie Madill | da854a2 | 2017-11-30 17:24:21 -0500 | [diff] [blame] | 106 | return mCurrentArrayBufferHandles; |
Jamie Madill | bd159f0 | 2017-10-09 19:39:06 -0400 | [diff] [blame] | 107 | } |
| 108 | |
Jamie Madill | 49ac74b | 2017-12-21 14:42:33 -0500 | [diff] [blame] | 109 | void VertexArrayVk::updateDrawDependencies(vk::CommandBufferNode *readNode, |
| 110 | const gl::AttributesMask &activeAttribsMask, |
| 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]); |
Jamie Madill | 49ac74b | 2017-12-21 14:42:33 -0500 | [diff] [blame] | 118 | mCurrentArrayBufferResources[attribIndex]->updateDependencies(readNode, serial); |
Jamie Madill | da854a2 | 2017-11-30 17:24:21 -0500 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | // Handle the bound element array buffer. |
| 122 | if (drawType == DrawType::Elements) |
| 123 | { |
| 124 | ASSERT(mCurrentElementArrayBufferResource); |
Jamie Madill | 49ac74b | 2017-12-21 14:42:33 -0500 | [diff] [blame] | 125 | mCurrentElementArrayBufferResource->updateDependencies(readNode, 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; |
Jamie Madill | ebf7299 | 2017-10-13 14:09:45 -0400 | [diff] [blame] | 132 | } |
| 133 | |
Jamie Madill | f2f6d37 | 2018-01-10 21:37:23 -0500 | [diff] [blame] | 134 | void VertexArrayVk::updateVertexDescriptions(const gl::Context *context, |
| 135 | vk::PipelineDesc *pipelineDesc) |
Jamie Madill | ebf7299 | 2017-10-13 14:09:45 -0400 | [diff] [blame] | 136 | { |
| 137 | if (mCurrentVertexDescsValid) |
| 138 | { |
| 139 | return; |
| 140 | } |
| 141 | |
| 142 | const auto &attribs = mState.getVertexAttributes(); |
| 143 | const auto &bindings = mState.getVertexBindings(); |
| 144 | |
| 145 | const gl::Program *programGL = context->getGLState().getProgram(); |
| 146 | |
Jamie Madill | f2f6d37 | 2018-01-10 21:37:23 -0500 | [diff] [blame] | 147 | pipelineDesc->resetVertexInputState(); |
| 148 | |
Jamie Madill | ebf7299 | 2017-10-13 14:09:45 -0400 | [diff] [blame] | 149 | for (auto attribIndex : programGL->getActiveAttribLocationsMask()) |
| 150 | { |
| 151 | const auto &attrib = attribs[attribIndex]; |
| 152 | const auto &binding = bindings[attrib.bindingIndex]; |
| 153 | if (attrib.enabled) |
| 154 | { |
Jamie Madill | f2f6d37 | 2018-01-10 21:37:23 -0500 | [diff] [blame] | 155 | pipelineDesc->updateVertexInputInfo(static_cast<uint32_t>(attribIndex), binding, |
| 156 | attrib); |
Jamie Madill | ebf7299 | 2017-10-13 14:09:45 -0400 | [diff] [blame] | 157 | } |
| 158 | else |
| 159 | { |
| 160 | UNIMPLEMENTED(); |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | mCurrentVertexDescsValid = true; |
| 165 | } |
| 166 | |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 167 | } // namespace rx |