blob: eab97496ea425ae25436cc7ee1c4ec62800833d5 [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 Madilldd43e6c2017-03-24 14:18:49 -040016#include "libANGLE/renderer/vulkan/ContextVk.h"
Jamie Madillebf72992017-10-13 14:09:45 -040017#include "libANGLE/renderer/vulkan/formatutilsvk.h"
Jamie Madilldd43e6c2017-03-24 14:18:49 -040018
Jamie Madill9e54b5a2016-05-25 12:57:39 -040019namespace rx
20{
21
Jamie Madillbd159f02017-10-09 19:39:06 -040022VertexArrayVk::VertexArrayVk(const gl::VertexArrayState &state)
23 : VertexArrayImpl(state),
24 mCurrentVertexBufferHandlesCache(state.getMaxAttribs(), VK_NULL_HANDLE),
Jamie Madillebf72992017-10-13 14:09:45 -040025 mCurrentVkBuffersCache(state.getMaxAttribs(), nullptr),
26 mCurrentVertexDescsValid(false)
Jamie Madill9e54b5a2016-05-25 12:57:39 -040027{
Jamie Madillebf72992017-10-13 14:09:45 -040028 mCurrentVertexBindingDescs.reserve(state.getMaxAttribs());
29 mCurrentVertexAttribDescs.reserve(state.getMaxAttribs());
Jamie Madill9e54b5a2016-05-25 12:57:39 -040030}
31
Jamie Madillacf2f3a2017-11-21 19:22:44 -050032VertexArrayVk::~VertexArrayVk()
33{
34}
35
Jamie Madill4928b7c2017-06-20 12:57:39 -040036void VertexArrayVk::destroy(const gl::Context *context)
Jamie Madill9e54b5a2016-05-25 12:57:39 -040037{
38}
39
Jamie Madillc564c072017-06-01 12:45:42 -040040void VertexArrayVk::syncState(const gl::Context *context,
41 const gl::VertexArray::DirtyBits &dirtyBits)
Jamie Madilldd43e6c2017-03-24 14:18:49 -040042{
43 ASSERT(dirtyBits.any());
Jamie Madill72106562017-03-24 14:18:50 -040044
Jamie Madillbd159f02017-10-09 19:39:06 -040045 // Invalidate current pipeline.
Jamie Madill72106562017-03-24 14:18:50 -040046 // TODO(jmadill): Use pipeline cache.
Jamie Madillacf2f3a2017-11-21 19:22:44 -050047 ContextVk *contextVk = vk::GetImpl(context);
Jamie Madill72106562017-03-24 14:18:50 -040048 contextVk->invalidateCurrentPipeline();
Jamie Madillbd159f02017-10-09 19:39:06 -040049
Jamie Madillebf72992017-10-13 14:09:45 -040050 // Invalidate the vertex descriptions.
51 invalidateVertexDescriptions();
52
Jamie Madillbd159f02017-10-09 19:39:06 -040053 // 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 Madillebf72992017-10-13 14:09:45 -040067
Jamie Madillbd159f02017-10-09 19:39:06 -040068 if (attrib.enabled)
69 {
70 gl::Buffer *bufferGL = binding.getBuffer().get();
71
72 if (bufferGL)
73 {
Jamie Madille1f3ad42017-10-28 23:00:42 -040074 BufferVk *bufferVk = vk::GetImpl(bufferGL);
Jamie Madillbd159f02017-10-09 19:39:06 -040075 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
91const std::vector<VkBuffer> &VertexArrayVk::getCurrentVertexBufferHandlesCache() const
92{
93 return mCurrentVertexBufferHandlesCache;
94}
95
96void VertexArrayVk::updateCurrentBufferSerials(const gl::AttributesMask &activeAttribsMask,
97 Serial serial)
98{
99 for (auto attribIndex : activeAttribsMask)
100 {
101 mCurrentVkBuffersCache[attribIndex]->setQueueSerial(serial);
102 }
Jamie Madilldd43e6c2017-03-24 14:18:49 -0400103}
104
Jamie Madillebf72992017-10-13 14:09:45 -0400105void VertexArrayVk::invalidateVertexDescriptions()
106{
107 mCurrentVertexDescsValid = false;
108 mCurrentVertexBindingDescs.clear();
109 mCurrentVertexAttribDescs.clear();
110}
111
112void 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
157const std::vector<VkVertexInputBindingDescription> &VertexArrayVk::getVertexBindingDescs() const
158{
159 return mCurrentVertexBindingDescs;
160}
161
162const std::vector<VkVertexInputAttributeDescription> &VertexArrayVk::getVertexAttribDescs() const
163{
164 return mCurrentVertexAttribDescs;
165}
166
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400167} // namespace rx