blob: 6ce71d2a14cbb9cdf5a4a90b7130ba8dc81d55a3 [file] [log] [blame]
Jamie Madill9e54b5a2016-05-25 12:57:39 -04001//
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 Madillc564c072017-06-01 12:45:42 -040014#include "libANGLE/Context.h"
Jamie Madillbd159f02017-10-09 19:39:06 -040015#include "libANGLE/renderer/vulkan/BufferVk.h"
Jamie Madill49ac74b2017-12-21 14:42:33 -050016#include "libANGLE/renderer/vulkan/CommandBufferNode.h"
Jamie Madilldd43e6c2017-03-24 14:18:49 -040017#include "libANGLE/renderer/vulkan/ContextVk.h"
Jamie Madillebf72992017-10-13 14:09:45 -040018#include "libANGLE/renderer/vulkan/formatutilsvk.h"
Jamie Madilldd43e6c2017-03-24 14:18:49 -040019
Jamie Madill9e54b5a2016-05-25 12:57:39 -040020namespace rx
21{
22
Jamie Madillbd159f02017-10-09 19:39:06 -040023VertexArrayVk::VertexArrayVk(const gl::VertexArrayState &state)
24 : VertexArrayImpl(state),
Jamie Madillda854a22017-11-30 17:24:21 -050025 mCurrentArrayBufferHandles{},
26 mCurrentArrayBufferResources{},
27 mCurrentElementArrayBufferResource(nullptr),
Jamie Madillebf72992017-10-13 14:09:45 -040028 mCurrentVertexDescsValid(false)
Jamie Madill9e54b5a2016-05-25 12:57:39 -040029{
Jamie Madillda854a22017-11-30 17:24:21 -050030 mCurrentArrayBufferHandles.fill(VK_NULL_HANDLE);
31 mCurrentArrayBufferResources.fill(nullptr);
Jamie Madillebf72992017-10-13 14:09:45 -040032 mCurrentVertexBindingDescs.reserve(state.getMaxAttribs());
33 mCurrentVertexAttribDescs.reserve(state.getMaxAttribs());
Jamie Madill9e54b5a2016-05-25 12:57:39 -040034}
35
Jamie Madillacf2f3a2017-11-21 19:22:44 -050036VertexArrayVk::~VertexArrayVk()
37{
38}
39
Jamie Madill4928b7c2017-06-20 12:57:39 -040040void VertexArrayVk::destroy(const gl::Context *context)
Jamie Madill9e54b5a2016-05-25 12:57:39 -040041{
42}
43
Jamie Madillc564c072017-06-01 12:45:42 -040044void VertexArrayVk::syncState(const gl::Context *context,
45 const gl::VertexArray::DirtyBits &dirtyBits)
Jamie Madilldd43e6c2017-03-24 14:18:49 -040046{
47 ASSERT(dirtyBits.any());
Jamie Madill72106562017-03-24 14:18:50 -040048
Jamie Madillbd159f02017-10-09 19:39:06 -040049 // Invalidate current pipeline.
Jamie Madill72106562017-03-24 14:18:50 -040050 // TODO(jmadill): Use pipeline cache.
Jamie Madillacf2f3a2017-11-21 19:22:44 -050051 ContextVk *contextVk = vk::GetImpl(context);
Jamie Madill49ac74b2017-12-21 14:42:33 -050052 contextVk->onVertexArrayChange();
Jamie Madillbd159f02017-10-09 19:39:06 -040053
Jamie Madillebf72992017-10-13 14:09:45 -040054 // Invalidate the vertex descriptions.
55 invalidateVertexDescriptions();
56
Jamie Madillbd159f02017-10-09 19:39:06 -040057 // Rebuild current attribute buffers cache. This will fail horribly if the buffer changes.
58 // TODO(jmadill): Handle buffer storage changes.
59 const auto &attribs = mState.getVertexAttributes();
60 const auto &bindings = mState.getVertexBindings();
61
62 for (auto dirtyBit : dirtyBits)
63 {
64 if (dirtyBit == gl::VertexArray::DIRTY_BIT_ELEMENT_ARRAY_BUFFER)
Jamie Madillda854a22017-11-30 17:24:21 -050065 {
66 gl::Buffer *bufferGL = mState.getElementArrayBuffer().get();
67 if (bufferGL)
68 {
69 mCurrentElementArrayBufferResource = vk::GetImpl(bufferGL);
70 }
71 else
72 {
73 mCurrentElementArrayBufferResource = nullptr;
74 }
Jamie Madillbd159f02017-10-09 19:39:06 -040075 continue;
Jamie Madillda854a22017-11-30 17:24:21 -050076 }
Jamie Madillbd159f02017-10-09 19:39:06 -040077
78 size_t attribIndex = gl::VertexArray::GetVertexIndexFromDirtyBit(dirtyBit);
79
80 const auto &attrib = attribs[attribIndex];
81 const auto &binding = bindings[attrib.bindingIndex];
Jamie Madillebf72992017-10-13 14:09:45 -040082
Jamie Madillbd159f02017-10-09 19:39:06 -040083 if (attrib.enabled)
84 {
85 gl::Buffer *bufferGL = binding.getBuffer().get();
86
87 if (bufferGL)
88 {
Jamie Madillda854a22017-11-30 17:24:21 -050089 BufferVk *bufferVk = vk::GetImpl(bufferGL);
90 mCurrentArrayBufferResources[attribIndex] = bufferVk;
91 mCurrentArrayBufferHandles[attribIndex] = bufferVk->getVkBuffer().getHandle();
Jamie Madillbd159f02017-10-09 19:39:06 -040092 }
93 else
94 {
Jamie Madillda854a22017-11-30 17:24:21 -050095 mCurrentArrayBufferResources[attribIndex] = nullptr;
96 mCurrentArrayBufferHandles[attribIndex] = VK_NULL_HANDLE;
Jamie Madillbd159f02017-10-09 19:39:06 -040097 }
98 }
99 else
100 {
101 UNIMPLEMENTED();
102 }
103 }
104}
105
Jamie Madillda854a22017-11-30 17:24:21 -0500106const gl::AttribArray<VkBuffer> &VertexArrayVk::getCurrentArrayBufferHandles() const
Jamie Madillbd159f02017-10-09 19:39:06 -0400107{
Jamie Madillda854a22017-11-30 17:24:21 -0500108 return mCurrentArrayBufferHandles;
Jamie Madillbd159f02017-10-09 19:39:06 -0400109}
110
Jamie Madill49ac74b2017-12-21 14:42:33 -0500111void VertexArrayVk::updateDrawDependencies(vk::CommandBufferNode *readNode,
112 const gl::AttributesMask &activeAttribsMask,
113 Serial serial,
114 DrawType drawType)
Jamie Madillbd159f02017-10-09 19:39:06 -0400115{
Jamie Madillda854a22017-11-30 17:24:21 -0500116 // Handle the bound array buffers.
Jamie Madillbd159f02017-10-09 19:39:06 -0400117 for (auto attribIndex : activeAttribsMask)
118 {
Jamie Madillda854a22017-11-30 17:24:21 -0500119 ASSERT(mCurrentArrayBufferResources[attribIndex]);
Jamie Madill49ac74b2017-12-21 14:42:33 -0500120 mCurrentArrayBufferResources[attribIndex]->updateDependencies(readNode, serial);
Jamie Madillda854a22017-11-30 17:24:21 -0500121 }
122
123 // Handle the bound element array buffer.
124 if (drawType == DrawType::Elements)
125 {
126 ASSERT(mCurrentElementArrayBufferResource);
Jamie Madill49ac74b2017-12-21 14:42:33 -0500127 mCurrentElementArrayBufferResource->updateDependencies(readNode, serial);
Jamie Madillbd159f02017-10-09 19:39:06 -0400128 }
Jamie Madilldd43e6c2017-03-24 14:18:49 -0400129}
130
Jamie Madillebf72992017-10-13 14:09:45 -0400131void VertexArrayVk::invalidateVertexDescriptions()
132{
133 mCurrentVertexDescsValid = false;
134 mCurrentVertexBindingDescs.clear();
135 mCurrentVertexAttribDescs.clear();
136}
137
138void VertexArrayVk::updateVertexDescriptions(const gl::Context *context)
139{
140 if (mCurrentVertexDescsValid)
141 {
142 return;
143 }
144
145 const auto &attribs = mState.getVertexAttributes();
146 const auto &bindings = mState.getVertexBindings();
147
148 const gl::Program *programGL = context->getGLState().getProgram();
149
150 for (auto attribIndex : programGL->getActiveAttribLocationsMask())
151 {
152 const auto &attrib = attribs[attribIndex];
153 const auto &binding = bindings[attrib.bindingIndex];
154 if (attrib.enabled)
155 {
156 VkVertexInputBindingDescription bindingDesc;
157 bindingDesc.binding = static_cast<uint32_t>(mCurrentVertexBindingDescs.size());
158 bindingDesc.stride = static_cast<uint32_t>(gl::ComputeVertexAttributeTypeSize(attrib));
159 bindingDesc.inputRate = (binding.getDivisor() > 0 ? VK_VERTEX_INPUT_RATE_INSTANCE
160 : VK_VERTEX_INPUT_RATE_VERTEX);
161
162 gl::VertexFormatType vertexFormatType = gl::GetVertexFormatType(attrib);
163
164 VkVertexInputAttributeDescription attribDesc;
165 attribDesc.binding = bindingDesc.binding;
166 attribDesc.format = vk::GetNativeVertexFormat(vertexFormatType);
167 attribDesc.location = static_cast<uint32_t>(attribIndex);
168 attribDesc.offset =
169 static_cast<uint32_t>(ComputeVertexAttributeOffset(attrib, binding));
170
171 mCurrentVertexBindingDescs.push_back(bindingDesc);
172 mCurrentVertexAttribDescs.push_back(attribDesc);
173 }
174 else
175 {
176 UNIMPLEMENTED();
177 }
178 }
179
180 mCurrentVertexDescsValid = true;
181}
182
183const std::vector<VkVertexInputBindingDescription> &VertexArrayVk::getVertexBindingDescs() const
184{
185 return mCurrentVertexBindingDescs;
186}
187
188const std::vector<VkVertexInputAttributeDescription> &VertexArrayVk::getVertexAttribDescs() const
189{
190 return mCurrentVertexAttribDescs;
191}
192
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400193} // namespace rx