blob: 02c24ad1c4662c23527d37e6deeedfbe51715685 [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 Madill4928b7c2017-06-20 12:57:39 -040032void VertexArrayVk::destroy(const gl::Context *context)
Jamie Madill9e54b5a2016-05-25 12:57:39 -040033{
34}
35
Jamie Madillc564c072017-06-01 12:45:42 -040036void VertexArrayVk::syncState(const gl::Context *context,
37 const gl::VertexArray::DirtyBits &dirtyBits)
Jamie Madilldd43e6c2017-03-24 14:18:49 -040038{
39 ASSERT(dirtyBits.any());
Jamie Madill72106562017-03-24 14:18:50 -040040
Jamie Madillbd159f02017-10-09 19:39:06 -040041 // Invalidate current pipeline.
Jamie Madill72106562017-03-24 14:18:50 -040042 // TODO(jmadill): Use pipeline cache.
Jamie Madille1f3ad42017-10-28 23:00:42 -040043 auto contextVk = vk::GetImpl(context);
Jamie Madill72106562017-03-24 14:18:50 -040044 contextVk->invalidateCurrentPipeline();
Jamie Madillbd159f02017-10-09 19:39:06 -040045
Jamie Madillebf72992017-10-13 14:09:45 -040046 // Invalidate the vertex descriptions.
47 invalidateVertexDescriptions();
48
Jamie Madillbd159f02017-10-09 19:39:06 -040049 // 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 Madillebf72992017-10-13 14:09:45 -040063
Jamie Madillbd159f02017-10-09 19:39:06 -040064 if (attrib.enabled)
65 {
66 gl::Buffer *bufferGL = binding.getBuffer().get();
67
68 if (bufferGL)
69 {
Jamie Madille1f3ad42017-10-28 23:00:42 -040070 BufferVk *bufferVk = vk::GetImpl(bufferGL);
Jamie Madillbd159f02017-10-09 19:39:06 -040071 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
87const std::vector<VkBuffer> &VertexArrayVk::getCurrentVertexBufferHandlesCache() const
88{
89 return mCurrentVertexBufferHandlesCache;
90}
91
92void VertexArrayVk::updateCurrentBufferSerials(const gl::AttributesMask &activeAttribsMask,
93 Serial serial)
94{
95 for (auto attribIndex : activeAttribsMask)
96 {
97 mCurrentVkBuffersCache[attribIndex]->setQueueSerial(serial);
98 }
Jamie Madilldd43e6c2017-03-24 14:18:49 -040099}
100
Jamie Madillebf72992017-10-13 14:09:45 -0400101void VertexArrayVk::invalidateVertexDescriptions()
102{
103 mCurrentVertexDescsValid = false;
104 mCurrentVertexBindingDescs.clear();
105 mCurrentVertexAttribDescs.clear();
106}
107
108void 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
153const std::vector<VkVertexInputBindingDescription> &VertexArrayVk::getVertexBindingDescs() const
154{
155 return mCurrentVertexBindingDescs;
156}
157
158const std::vector<VkVertexInputAttributeDescription> &VertexArrayVk::getVertexAttribDescs() const
159{
160 return mCurrentVertexAttribDescs;
161}
162
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400163} // namespace rx