blob: de16276ed82569bb0cde04164465d80026a4e126 [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 Madill1f46bc12018-02-20 16:09:43 -050016#include "libANGLE/renderer/vulkan/CommandGraph.h"
Jamie Madilldd43e6c2017-03-24 14:18:49 -040017#include "libANGLE/renderer/vulkan/ContextVk.h"
Jamie Madill316c6062018-05-29 10:49:45 -040018#include "libANGLE/renderer/vulkan/FramebufferVk.h"
Jamie Madillc3755fc2018-04-05 08:39:13 -040019#include "libANGLE/renderer/vulkan/RendererVk.h"
Jamie Madill3c424b42018-01-19 12:35:09 -050020#include "libANGLE/renderer/vulkan/vk_format_utils.h"
Jamie Madilldd43e6c2017-03-24 14:18:49 -040021
Jamie Madill9e54b5a2016-05-25 12:57:39 -040022namespace rx
23{
Jamie Madillc3755fc2018-04-05 08:39:13 -040024namespace
25{
26constexpr size_t kDynamicVertexDataSize = 1024 * 1024;
27constexpr size_t kDynamicIndexDataSize = 1024 * 8;
28} // anonymous namespace
Jamie Madill9e54b5a2016-05-25 12:57:39 -040029
Luc Ferrona9ab0f32018-05-17 17:03:55 -040030VertexArrayVk::VertexArrayVk(const gl::VertexArrayState &state, RendererVk *renderer)
Jamie Madillbd159f02017-10-09 19:39:06 -040031 : VertexArrayImpl(state),
Jamie Madillda854a22017-11-30 17:24:21 -050032 mCurrentArrayBufferHandles{},
Frank Henigman17448952017-01-05 15:48:26 -050033 mCurrentArrayBufferOffsets{},
Jamie Madillda854a22017-11-30 17:24:21 -050034 mCurrentArrayBufferResources{},
Jamie Madillc3755fc2018-04-05 08:39:13 -040035 mCurrentElementArrayBufferHandle(VK_NULL_HANDLE),
36 mCurrentElementArrayBufferOffset(0),
37 mCurrentElementArrayBufferResource(nullptr),
38 mDynamicVertexData(VK_BUFFER_USAGE_VERTEX_BUFFER_BIT, kDynamicVertexDataSize),
39 mDynamicIndexData(VK_BUFFER_USAGE_INDEX_BUFFER_BIT, kDynamicIndexDataSize),
Luc Ferron6ed167a2018-06-13 13:45:55 -040040 mTranslatedByteIndexData(VK_BUFFER_USAGE_INDEX_BUFFER_BIT, kDynamicIndexDataSize),
Luc Ferrona9ab0f32018-05-17 17:03:55 -040041 mLineLoopHelper(renderer),
Jamie Madillb8e39662018-04-04 11:41:42 -040042 mDirtyLineLoopTranslation(true),
Jamie Madillc3755fc2018-04-05 08:39:13 -040043 mVertexBuffersDirty(false),
Luc Ferron49aacad2018-06-12 08:33:59 -040044 mIndexBufferDirty(false),
45 mLastIndexBufferOffset(0)
Jamie Madill9e54b5a2016-05-25 12:57:39 -040046{
Jamie Madillda854a22017-11-30 17:24:21 -050047 mCurrentArrayBufferHandles.fill(VK_NULL_HANDLE);
Frank Henigman17448952017-01-05 15:48:26 -050048 mCurrentArrayBufferOffsets.fill(0);
Jamie Madillda854a22017-11-30 17:24:21 -050049 mCurrentArrayBufferResources.fill(nullptr);
Jamie Madill112a3a82018-01-23 13:04:06 -050050
51 mPackedInputBindings.fill({0, 0});
52 mPackedInputAttributes.fill({0, 0, 0});
Jamie Madillc3755fc2018-04-05 08:39:13 -040053
Luc Ferrona9ab0f32018-05-17 17:03:55 -040054 mDynamicVertexData.init(1, renderer);
55 mDynamicIndexData.init(1, renderer);
Luc Ferron6ed167a2018-06-13 13:45:55 -040056 mTranslatedByteIndexData.init(1, renderer);
Jamie Madill9e54b5a2016-05-25 12:57:39 -040057}
58
Jamie Madillacf2f3a2017-11-21 19:22:44 -050059VertexArrayVk::~VertexArrayVk()
60{
61}
62
Jamie Madill4928b7c2017-06-20 12:57:39 -040063void VertexArrayVk::destroy(const gl::Context *context)
Jamie Madill9e54b5a2016-05-25 12:57:39 -040064{
Jamie Madillc3755fc2018-04-05 08:39:13 -040065 VkDevice device = vk::GetImpl(context)->getRenderer()->getDevice();
Jamie Madillc3755fc2018-04-05 08:39:13 -040066 mDynamicVertexData.destroy(device);
67 mDynamicIndexData.destroy(device);
Luc Ferron6ed167a2018-06-13 13:45:55 -040068 mTranslatedByteIndexData.destroy(device);
Jamie Madill22f12fe2018-04-08 14:23:40 -040069 mLineLoopHelper.destroy(device);
Jamie Madill9e54b5a2016-05-25 12:57:39 -040070}
71
Jamie Madill21061022018-07-12 23:56:30 -040072angle::Result VertexArrayVk::streamVertexData(ContextVk *contextVk,
73 const gl::AttributesMask &attribsToStream,
74 const gl::DrawCallParams &drawCallParams)
Frank Henigman17448952017-01-05 15:48:26 -050075{
Jamie Madillc3755fc2018-04-05 08:39:13 -040076 ASSERT(!attribsToStream.none());
77
Jamie Madillbcef3222018-04-13 15:19:11 -040078 const auto &attribs = mState.getVertexAttributes();
79 const auto &bindings = mState.getVertexBindings();
Frank Henigman17448952017-01-05 15:48:26 -050080
Jamie Madill32fd63b2018-03-31 11:20:35 -040081 const size_t lastVertex = drawCallParams.firstVertex() + drawCallParams.vertexCount();
82
Frank Henigman17448952017-01-05 15:48:26 -050083 // TODO(fjhenigman): When we have a bunch of interleaved attributes, they end up
84 // un-interleaved, wasting space and copying time. Consider improving on that.
Jamie Madillb8e39662018-04-04 11:41:42 -040085 for (size_t attribIndex : attribsToStream)
Frank Henigman17448952017-01-05 15:48:26 -050086 {
Frank Henigman6dd4a922018-03-02 16:35:13 -050087 const gl::VertexAttribute &attrib = attribs[attribIndex];
88 const gl::VertexBinding &binding = bindings[attrib.bindingIndex];
89 ASSERT(attrib.enabled && binding.getBuffer().get() == nullptr);
Frank Henigman17448952017-01-05 15:48:26 -050090
Frank Henigman6dd4a922018-03-02 16:35:13 -050091 // Only [firstVertex, lastVertex] is needed by the upcoming draw so that
92 // is all we copy, but we allocate space for [0, lastVertex] so indexing
93 // will work. If we don't start at zero all the indices will be off.
94 // TODO(fjhenigman): See if we can account for indices being off by adjusting
95 // the offset, thus avoiding wasted memory.
Jamie Madill32fd63b2018-03-31 11:20:35 -040096 const size_t firstByte = drawCallParams.firstVertex() * binding.getStride();
Frank Henigman6dd4a922018-03-02 16:35:13 -050097 const size_t lastByte =
98 lastVertex * binding.getStride() + gl::ComputeVertexAttributeTypeSize(attrib);
Jamie Madill493f9572018-05-24 19:52:15 -040099 uint8_t *dst = nullptr;
Luc Ferron7a06ac12018-03-15 10:17:04 -0400100 uint32_t offset = 0;
Jamie Madillc3755fc2018-04-05 08:39:13 -0400101 ANGLE_TRY(mDynamicVertexData.allocate(
Jamie Madill21061022018-07-12 23:56:30 -0400102 contextVk, lastByte, &dst, &mCurrentArrayBufferHandles[attribIndex], &offset, nullptr));
Luc Ferron7a06ac12018-03-15 10:17:04 -0400103 mCurrentArrayBufferOffsets[attribIndex] = static_cast<VkDeviceSize>(offset);
Frank Henigman6dd4a922018-03-02 16:35:13 -0500104 memcpy(dst + firstByte, static_cast<const uint8_t *>(attrib.pointer) + firstByte,
105 lastByte - firstByte);
Frank Henigman17448952017-01-05 15:48:26 -0500106 }
107
Jamie Madill21061022018-07-12 23:56:30 -0400108 ANGLE_TRY(mDynamicVertexData.flush(contextVk));
109 mDynamicVertexData.releaseRetainedBuffers(contextVk->getRenderer());
110 return angle::Result::Continue();
Jamie Madillc3755fc2018-04-05 08:39:13 -0400111}
112
Jamie Madill21061022018-07-12 23:56:30 -0400113angle::Result VertexArrayVk::streamIndexData(ContextVk *contextVk,
114 const gl::DrawCallParams &drawCallParams)
Jamie Madillc3755fc2018-04-05 08:39:13 -0400115{
116 ASSERT(!mState.getElementArrayBuffer().get());
117
118 uint32_t offset = 0;
119
120 const GLsizei amount = sizeof(GLushort) * drawCallParams.indexCount();
121 GLubyte *dst = nullptr;
122
Jamie Madill21061022018-07-12 23:56:30 -0400123 ANGLE_TRY(mDynamicIndexData.allocate(contextVk, amount, &dst, &mCurrentElementArrayBufferHandle,
Jamie Madillc3755fc2018-04-05 08:39:13 -0400124 &offset, nullptr));
125 if (drawCallParams.type() == GL_UNSIGNED_BYTE)
126 {
127 // Unsigned bytes don't have direct support in Vulkan so we have to expand the
128 // memory to a GLushort.
129 const GLubyte *in = static_cast<const GLubyte *>(drawCallParams.indices());
130 GLushort *expandedDst = reinterpret_cast<GLushort *>(dst);
131 for (GLsizei index = 0; index < drawCallParams.indexCount(); index++)
132 {
133 expandedDst[index] = static_cast<GLushort>(in[index]);
134 }
135 }
136 else
137 {
138 memcpy(dst, drawCallParams.indices(), amount);
139 }
Jamie Madill21061022018-07-12 23:56:30 -0400140 ANGLE_TRY(mDynamicIndexData.flush(contextVk));
141 mDynamicIndexData.releaseRetainedBuffers(contextVk->getRenderer());
Jamie Madillc3755fc2018-04-05 08:39:13 -0400142 mCurrentElementArrayBufferOffset = offset;
Jamie Madill21061022018-07-12 23:56:30 -0400143 return angle::Result::Continue();
Frank Henigman17448952017-01-05 15:48:26 -0500144}
145
Jamie Madilla56467e2018-04-11 16:19:41 -0400146#define ANGLE_VERTEX_DIRTY_ATTRIB_FUNC(INDEX) \
147 case gl::VertexArray::DIRTY_BIT_ATTRIB_0 + INDEX: \
148 syncDirtyAttrib(attribs[INDEX], bindings[attribs[INDEX].bindingIndex], INDEX); \
149 invalidatePipeline = true; \
150 break;
151
152#define ANGLE_VERTEX_DIRTY_BINDING_FUNC(INDEX) \
153 case gl::VertexArray::DIRTY_BIT_BINDING_0 + INDEX: \
154 syncDirtyAttrib(attribs[INDEX], bindings[attribs[INDEX].bindingIndex], INDEX); \
155 invalidatePipeline = true; \
156 break;
157
158#define ANGLE_VERTEX_DIRTY_BUFFER_DATA_FUNC(INDEX) \
159 case gl::VertexArray::DIRTY_BIT_BUFFER_DATA_0 + INDEX: \
160 break;
161
Frank Henigman0af5b862018-03-27 20:19:33 -0400162gl::Error VertexArrayVk::syncState(const gl::Context *context,
163 const gl::VertexArray::DirtyBits &dirtyBits,
164 const gl::VertexArray::DirtyAttribBitsArray &attribBits,
165 const gl::VertexArray::DirtyBindingBitsArray &bindingBits)
Jamie Madilldd43e6c2017-03-24 14:18:49 -0400166{
167 ASSERT(dirtyBits.any());
Jamie Madill72106562017-03-24 14:18:50 -0400168
Jamie Madillc3755fc2018-04-05 08:39:13 -0400169 bool invalidatePipeline = false;
170
Jamie Madillbd159f02017-10-09 19:39:06 -0400171 // Invalidate current pipeline.
Jamie Madillacf2f3a2017-11-21 19:22:44 -0500172 ContextVk *contextVk = vk::GetImpl(context);
Jamie Madillbd159f02017-10-09 19:39:06 -0400173
174 // Rebuild current attribute buffers cache. This will fail horribly if the buffer changes.
175 // TODO(jmadill): Handle buffer storage changes.
176 const auto &attribs = mState.getVertexAttributes();
177 const auto &bindings = mState.getVertexBindings();
178
Jamie Madill09463932018-04-04 05:26:59 -0400179 for (size_t dirtyBit : dirtyBits)
Jamie Madillbd159f02017-10-09 19:39:06 -0400180 {
Jamie Madill09463932018-04-04 05:26:59 -0400181 switch (dirtyBit)
Jamie Madillda854a22017-11-30 17:24:21 -0500182 {
Jamie Madill09463932018-04-04 05:26:59 -0400183 case gl::VertexArray::DIRTY_BIT_ELEMENT_ARRAY_BUFFER:
Jamie Madillda854a22017-11-30 17:24:21 -0500184 {
Jamie Madill09463932018-04-04 05:26:59 -0400185 gl::Buffer *bufferGL = mState.getElementArrayBuffer().get();
186 if (bufferGL)
187 {
Jamie Madillc3755fc2018-04-05 08:39:13 -0400188 BufferVk *bufferVk = vk::GetImpl(bufferGL);
189 mCurrentElementArrayBufferResource = bufferVk;
190 mCurrentElementArrayBufferHandle = bufferVk->getVkBuffer().getHandle();
Jamie Madill09463932018-04-04 05:26:59 -0400191 }
192 else
193 {
194 mCurrentElementArrayBufferResource = nullptr;
Jamie Madillc3755fc2018-04-05 08:39:13 -0400195 mCurrentElementArrayBufferHandle = VK_NULL_HANDLE;
Jamie Madill09463932018-04-04 05:26:59 -0400196 }
Luc Ferron387b3b32018-05-28 10:11:57 -0400197
198 mCurrentElementArrayBufferOffset = 0;
199 mLineLoopBufferFirstIndex.reset();
200 mLineLoopBufferLastIndex.reset();
Jamie Madillb8e39662018-04-04 11:41:42 -0400201 mIndexBufferDirty = true;
202 mDirtyLineLoopTranslation = true;
Jamie Madill09463932018-04-04 05:26:59 -0400203 break;
Jamie Madillda854a22017-11-30 17:24:21 -0500204 }
Jamie Madill09463932018-04-04 05:26:59 -0400205
206 case gl::VertexArray::DIRTY_BIT_ELEMENT_ARRAY_BUFFER_DATA:
Jamie Madillc3755fc2018-04-05 08:39:13 -0400207 mLineLoopBufferFirstIndex.reset();
208 mLineLoopBufferLastIndex.reset();
Jamie Madillb8e39662018-04-04 11:41:42 -0400209 mDirtyLineLoopTranslation = true;
Jamie Madill09463932018-04-04 05:26:59 -0400210 break;
211
Jamie Madilla56467e2018-04-11 16:19:41 -0400212 ANGLE_VERTEX_INDEX_CASES(ANGLE_VERTEX_DIRTY_ATTRIB_FUNC);
213 ANGLE_VERTEX_INDEX_CASES(ANGLE_VERTEX_DIRTY_BINDING_FUNC);
214 ANGLE_VERTEX_INDEX_CASES(ANGLE_VERTEX_DIRTY_BUFFER_DATA_FUNC);
215
Jamie Madill09463932018-04-04 05:26:59 -0400216 default:
Jamie Madilla56467e2018-04-11 16:19:41 -0400217 UNREACHABLE();
Jamie Madill09463932018-04-04 05:26:59 -0400218 break;
Jamie Madillbd159f02017-10-09 19:39:06 -0400219 }
220 }
Frank Henigman0af5b862018-03-27 20:19:33 -0400221
Jamie Madillc3755fc2018-04-05 08:39:13 -0400222 if (invalidatePipeline)
223 {
224 mVertexBuffersDirty = true;
225 contextVk->invalidateCurrentPipeline();
226 }
227
Frank Henigman0af5b862018-03-27 20:19:33 -0400228 return gl::NoError();
Jamie Madillbd159f02017-10-09 19:39:06 -0400229}
230
Jamie Madilla56467e2018-04-11 16:19:41 -0400231void VertexArrayVk::syncDirtyAttrib(const gl::VertexAttribute &attrib,
232 const gl::VertexBinding &binding,
233 size_t attribIndex)
234{
235 // Invalidate the input description for pipelines.
236 mDirtyPackedInputs.set(attribIndex);
237
238 if (attrib.enabled)
239 {
240 gl::Buffer *bufferGL = binding.getBuffer().get();
241
242 if (bufferGL)
243 {
244 BufferVk *bufferVk = vk::GetImpl(bufferGL);
245 mCurrentArrayBufferResources[attribIndex] = bufferVk;
246 mCurrentArrayBufferHandles[attribIndex] = bufferVk->getVkBuffer().getHandle();
Luc Ferronfa5d84b2018-06-28 10:40:04 -0400247 mCurrentArrayBufferOffsets[attribIndex] = binding.getOffset();
Jamie Madilla56467e2018-04-11 16:19:41 -0400248 }
249 else
250 {
251 mCurrentArrayBufferResources[attribIndex] = nullptr;
252 mCurrentArrayBufferHandles[attribIndex] = VK_NULL_HANDLE;
Luc Ferronfa5d84b2018-06-28 10:40:04 -0400253 mCurrentArrayBufferOffsets[attribIndex] = 0;
Jamie Madilla56467e2018-04-11 16:19:41 -0400254 }
Jamie Madilla56467e2018-04-11 16:19:41 -0400255 }
256 else
257 {
Jamie Madilla2f043d2018-07-10 17:21:20 -0400258 WARN() << "Default vertex attributes unimplemented. http://anglebug.com/2444";
Jamie Madilla56467e2018-04-11 16:19:41 -0400259 }
260}
261
Jamie Madillda854a22017-11-30 17:24:21 -0500262const gl::AttribArray<VkBuffer> &VertexArrayVk::getCurrentArrayBufferHandles() const
Jamie Madillbd159f02017-10-09 19:39:06 -0400263{
Jamie Madillda854a22017-11-30 17:24:21 -0500264 return mCurrentArrayBufferHandles;
Jamie Madillbd159f02017-10-09 19:39:06 -0400265}
266
Frank Henigman17448952017-01-05 15:48:26 -0500267const gl::AttribArray<VkDeviceSize> &VertexArrayVk::getCurrentArrayBufferOffsets() const
268{
269 return mCurrentArrayBufferOffsets;
270}
271
Jamie Madill316c6062018-05-29 10:49:45 -0400272void VertexArrayVk::updateArrayBufferReadDependencies(vk::CommandGraphResource *drawFramebuffer,
Jamie Madillc3755fc2018-04-05 08:39:13 -0400273 const gl::AttributesMask &activeAttribsMask,
274 Serial serial)
Jamie Madillbd159f02017-10-09 19:39:06 -0400275{
Jamie Madillda854a22017-11-30 17:24:21 -0500276 // Handle the bound array buffers.
Jamie Madillc3755fc2018-04-05 08:39:13 -0400277 for (size_t attribIndex : activeAttribsMask)
Jamie Madillbd159f02017-10-09 19:39:06 -0400278 {
Frank Henigman17448952017-01-05 15:48:26 -0500279 if (mCurrentArrayBufferResources[attribIndex])
Jamie Madill316c6062018-05-29 10:49:45 -0400280 mCurrentArrayBufferResources[attribIndex]->addReadDependency(drawFramebuffer);
Jamie Madillda854a22017-11-30 17:24:21 -0500281 }
Jamie Madillc3755fc2018-04-05 08:39:13 -0400282}
Jamie Madillda854a22017-11-30 17:24:21 -0500283
Jamie Madill316c6062018-05-29 10:49:45 -0400284void VertexArrayVk::updateElementArrayBufferReadDependency(
285 vk::CommandGraphResource *drawFramebuffer,
286 Serial serial)
Jamie Madillc3755fc2018-04-05 08:39:13 -0400287{
Jamie Madillda854a22017-11-30 17:24:21 -0500288 // Handle the bound element array buffer.
Jamie Madillc3755fc2018-04-05 08:39:13 -0400289 if (mCurrentElementArrayBufferResource)
Jamie Madillda854a22017-11-30 17:24:21 -0500290 {
Jamie Madill316c6062018-05-29 10:49:45 -0400291 mCurrentElementArrayBufferResource->addReadDependency(drawFramebuffer);
Jamie Madillbd159f02017-10-09 19:39:06 -0400292 }
Jamie Madilldd43e6c2017-03-24 14:18:49 -0400293}
294
Frank Henigman2ad498e2018-06-20 13:19:01 -0400295void VertexArrayVk::getPackedInputDescriptions(const RendererVk *rendererVk,
296 vk::PipelineDesc *pipelineDesc)
Jamie Madillebf72992017-10-13 14:09:45 -0400297{
Frank Henigman2ad498e2018-06-20 13:19:01 -0400298 updatePackedInputDescriptions(rendererVk);
Jamie Madill112a3a82018-01-23 13:04:06 -0500299 pipelineDesc->updateVertexInputInfo(mPackedInputBindings, mPackedInputAttributes);
Jamie Madillebf72992017-10-13 14:09:45 -0400300}
301
Frank Henigman2ad498e2018-06-20 13:19:01 -0400302void VertexArrayVk::updatePackedInputDescriptions(const RendererVk *rendererVk)
Jamie Madillebf72992017-10-13 14:09:45 -0400303{
Jamie Madill112a3a82018-01-23 13:04:06 -0500304 if (!mDirtyPackedInputs.any())
Jamie Madillebf72992017-10-13 14:09:45 -0400305 {
306 return;
307 }
308
309 const auto &attribs = mState.getVertexAttributes();
310 const auto &bindings = mState.getVertexBindings();
311
Jamie Madill112a3a82018-01-23 13:04:06 -0500312 for (auto attribIndex : mDirtyPackedInputs)
Jamie Madillebf72992017-10-13 14:09:45 -0400313 {
314 const auto &attrib = attribs[attribIndex];
315 const auto &binding = bindings[attrib.bindingIndex];
316 if (attrib.enabled)
317 {
Frank Henigman2ad498e2018-06-20 13:19:01 -0400318 updatePackedInputInfo(rendererVk, static_cast<uint32_t>(attribIndex), binding, attrib);
Jamie Madillebf72992017-10-13 14:09:45 -0400319 }
320 else
321 {
Jamie Madilla2f043d2018-07-10 17:21:20 -0400322 WARN() << "Default vertex attributes unimplemented. http://anglebug.com/2444";
Jamie Madillebf72992017-10-13 14:09:45 -0400323 }
324 }
325
Jamie Madill112a3a82018-01-23 13:04:06 -0500326 mDirtyPackedInputs.reset();
327}
328
Frank Henigman2ad498e2018-06-20 13:19:01 -0400329void VertexArrayVk::updatePackedInputInfo(const RendererVk *rendererVk,
330 uint32_t attribIndex,
Jamie Madill112a3a82018-01-23 13:04:06 -0500331 const gl::VertexBinding &binding,
332 const gl::VertexAttribute &attrib)
333{
334 vk::PackedVertexInputBindingDesc &bindingDesc = mPackedInputBindings[attribIndex];
335
336 size_t attribSize = gl::ComputeVertexAttributeTypeSize(attrib);
337 ASSERT(attribSize <= std::numeric_limits<uint16_t>::max());
338
Frank Henigmana8e868f2018-01-28 23:32:25 -0500339 bindingDesc.stride = static_cast<uint16_t>(binding.getStride());
Jamie Madill112a3a82018-01-23 13:04:06 -0500340 bindingDesc.inputRate = static_cast<uint16_t>(
341 binding.getDivisor() > 0 ? VK_VERTEX_INPUT_RATE_INSTANCE : VK_VERTEX_INPUT_RATE_VERTEX);
342
Frank Henigman2ad498e2018-06-20 13:19:01 -0400343 VkFormat vkFormat = rendererVk->getFormat(GetVertexFormatID(attrib)).vkBufferFormat;
Jamie Madill112a3a82018-01-23 13:04:06 -0500344 ASSERT(vkFormat <= std::numeric_limits<uint16_t>::max());
Frank Henigman8c379f32018-06-21 19:55:05 -0400345 if (vkFormat == VK_FORMAT_UNDEFINED)
346 {
347 // TODO(fjhenigman): Add support for vertex data format. anglebug.com/2405
348 UNIMPLEMENTED();
349 }
Jamie Madill112a3a82018-01-23 13:04:06 -0500350
351 vk::PackedVertexInputAttributeDesc &attribDesc = mPackedInputAttributes[attribIndex];
352 attribDesc.format = static_cast<uint16_t>(vkFormat);
353 attribDesc.location = static_cast<uint16_t>(attribIndex);
Luc Ferronfa5d84b2018-06-28 10:40:04 -0400354 attribDesc.offset = static_cast<uint32_t>(attrib.relativeOffset);
Jamie Madillebf72992017-10-13 14:09:45 -0400355}
356
Jamie Madillc3755fc2018-04-05 08:39:13 -0400357gl::Error VertexArrayVk::drawArrays(const gl::Context *context,
Jamie Madillc3755fc2018-04-05 08:39:13 -0400358 const gl::DrawCallParams &drawCallParams,
Jamie Madill316c6062018-05-29 10:49:45 -0400359 vk::CommandBuffer *commandBuffer,
Jamie Madillc3755fc2018-04-05 08:39:13 -0400360 bool newCommandBuffer)
361{
Jamie Madillc3755fc2018-04-05 08:39:13 -0400362 ASSERT(commandBuffer->valid());
363
Jamie Madill21061022018-07-12 23:56:30 -0400364 ContextVk *contextVk = vk::GetImpl(context);
365
366 ANGLE_TRY(onDraw(context, drawCallParams, commandBuffer, newCommandBuffer));
Jamie Madillc3755fc2018-04-05 08:39:13 -0400367
Jamie Madill18e323a2018-05-11 16:54:17 -0400368 // Note: Vertex indexes can be arbitrarily large.
369 uint32_t clampedVertexCount = drawCallParams.getClampedVertexCount<uint32_t>();
370
Jamie Madill493f9572018-05-24 19:52:15 -0400371 if (drawCallParams.mode() != gl::PrimitiveMode::LineLoop)
Jamie Madillc3755fc2018-04-05 08:39:13 -0400372 {
Jamie Madill18e323a2018-05-11 16:54:17 -0400373 commandBuffer->draw(clampedVertexCount, 1, drawCallParams.firstVertex(), 0);
Jamie Madillc3755fc2018-04-05 08:39:13 -0400374 return gl::NoError();
375 }
376
377 // Handle GL_LINE_LOOP drawArrays.
Jamie Madill18e323a2018-05-11 16:54:17 -0400378 size_t lastVertex = static_cast<size_t>(drawCallParams.firstVertex() + clampedVertexCount);
Jamie Madillc3755fc2018-04-05 08:39:13 -0400379 if (!mLineLoopBufferFirstIndex.valid() || !mLineLoopBufferLastIndex.valid() ||
380 mLineLoopBufferFirstIndex != drawCallParams.firstVertex() ||
381 mLineLoopBufferLastIndex != lastVertex)
382 {
Jamie Madill21061022018-07-12 23:56:30 -0400383 ANGLE_TRY(mLineLoopHelper.getIndexBufferForDrawArrays(contextVk, drawCallParams,
Jamie Madill22f12fe2018-04-08 14:23:40 -0400384 &mCurrentElementArrayBufferHandle,
385 &mCurrentElementArrayBufferOffset));
Jamie Madillc3755fc2018-04-05 08:39:13 -0400386
387 mLineLoopBufferFirstIndex = drawCallParams.firstVertex();
388 mLineLoopBufferLastIndex = lastVertex;
389 }
390
391 commandBuffer->bindIndexBuffer(mCurrentElementArrayBufferHandle,
392 mCurrentElementArrayBufferOffset, VK_INDEX_TYPE_UINT32);
393
Jamie Madill18e323a2018-05-11 16:54:17 -0400394 vk::LineLoopHelper::Draw(clampedVertexCount, commandBuffer);
Jamie Madillc3755fc2018-04-05 08:39:13 -0400395
396 return gl::NoError();
397}
398
399gl::Error VertexArrayVk::drawElements(const gl::Context *context,
Jamie Madillc3755fc2018-04-05 08:39:13 -0400400 const gl::DrawCallParams &drawCallParams,
Jamie Madill316c6062018-05-29 10:49:45 -0400401 vk::CommandBuffer *commandBuffer,
Jamie Madillc3755fc2018-04-05 08:39:13 -0400402 bool newCommandBuffer)
403{
Jamie Madillc3755fc2018-04-05 08:39:13 -0400404 ASSERT(commandBuffer->valid());
405
Jamie Madill21061022018-07-12 23:56:30 -0400406 ContextVk *contextVk = vk::GetImpl(context);
407
Jamie Madill493f9572018-05-24 19:52:15 -0400408 if (drawCallParams.mode() != gl::PrimitiveMode::LineLoop)
Jamie Madillc3755fc2018-04-05 08:39:13 -0400409 {
Jamie Madill21061022018-07-12 23:56:30 -0400410 ANGLE_TRY(onIndexedDraw(context, drawCallParams, commandBuffer, newCommandBuffer));
Jamie Madillc3755fc2018-04-05 08:39:13 -0400411 commandBuffer->drawIndexed(drawCallParams.indexCount(), 1, 0, 0, 0);
412 return gl::NoError();
413 }
414
415 // Handle GL_LINE_LOOP drawElements.
Jamie Madillb8e39662018-04-04 11:41:42 -0400416 if (mDirtyLineLoopTranslation)
417 {
Luc Ferrona9120462018-04-12 13:11:03 -0400418 gl::Buffer *elementArrayBuffer = mState.getElementArrayBuffer().get();
419 VkIndexType indexType = gl_vk::GetIndexType(drawCallParams.type());
420
421 if (!elementArrayBuffer)
422 {
423 ANGLE_TRY(mLineLoopHelper.getIndexBufferForClientElementArray(
Jamie Madill21061022018-07-12 23:56:30 -0400424 contextVk, drawCallParams, &mCurrentElementArrayBufferHandle,
Luc Ferron3e313802018-06-11 14:44:33 -0400425 &mCurrentElementArrayBufferOffset));
Luc Ferrona9120462018-04-12 13:11:03 -0400426 }
427 else
428 {
Luc Ferrona4fa9c22018-04-13 07:00:56 -0400429 // When using an element array buffer, 'indices' is an offset to the first element.
430 intptr_t offset = reinterpret_cast<intptr_t>(drawCallParams.indices());
Luc Ferrona9120462018-04-12 13:11:03 -0400431 BufferVk *elementArrayBufferVk = vk::GetImpl(elementArrayBuffer);
432 ANGLE_TRY(mLineLoopHelper.getIndexBufferForElementArrayBuffer(
Jamie Madill21061022018-07-12 23:56:30 -0400433 contextVk, elementArrayBufferVk, indexType, drawCallParams.indexCount(), offset,
Luc Ferrona9120462018-04-12 13:11:03 -0400434 &mCurrentElementArrayBufferHandle, &mCurrentElementArrayBufferOffset));
435 }
Jamie Madillb8e39662018-04-04 11:41:42 -0400436 }
Jamie Madillc3755fc2018-04-05 08:39:13 -0400437
Jamie Madill21061022018-07-12 23:56:30 -0400438 ANGLE_TRY(onIndexedDraw(context, drawCallParams, commandBuffer, newCommandBuffer));
Jamie Madill22f12fe2018-04-08 14:23:40 -0400439 vk::LineLoopHelper::Draw(drawCallParams.indexCount(), commandBuffer);
Jamie Madillc3755fc2018-04-05 08:39:13 -0400440
441 return gl::NoError();
442}
443
444gl::Error VertexArrayVk::onDraw(const gl::Context *context,
Jamie Madillc3755fc2018-04-05 08:39:13 -0400445 const gl::DrawCallParams &drawCallParams,
Jamie Madill316c6062018-05-29 10:49:45 -0400446 vk::CommandBuffer *commandBuffer,
Jamie Madillc3755fc2018-04-05 08:39:13 -0400447 bool newCommandBuffer)
448{
Jamie Madill21061022018-07-12 23:56:30 -0400449 ContextVk *contextVk = vk::GetImpl(context);
Jamie Madillc3755fc2018-04-05 08:39:13 -0400450 const gl::State &state = context->getGLState();
451 const gl::Program *programGL = state.getProgram();
Jamie Madillbcef3222018-04-13 15:19:11 -0400452 const gl::AttributesMask &clientAttribs = mState.getEnabledClientMemoryAttribsMask();
Jamie Madillc3755fc2018-04-05 08:39:13 -0400453 const gl::AttributesMask &activeAttribs = programGL->getActiveAttribLocationsMask();
454 uint32_t maxAttrib = programGL->getState().getMaxActiveAttribLocation();
455
Jamie Madillbcef3222018-04-13 15:19:11 -0400456 if (clientAttribs.any())
Jamie Madillc3755fc2018-04-05 08:39:13 -0400457 {
Jamie Madillbcef3222018-04-13 15:19:11 -0400458 const gl::AttributesMask &attribsToStream = (clientAttribs & activeAttribs);
Jamie Madillc3755fc2018-04-05 08:39:13 -0400459 if (attribsToStream.any())
460 {
461 ANGLE_TRY(drawCallParams.ensureIndexRangeResolved(context));
Jamie Madill21061022018-07-12 23:56:30 -0400462 ANGLE_TRY(streamVertexData(contextVk, attribsToStream, drawCallParams));
Jamie Madillc3755fc2018-04-05 08:39:13 -0400463 commandBuffer->bindVertexBuffers(0, maxAttrib, mCurrentArrayBufferHandles.data(),
464 mCurrentArrayBufferOffsets.data());
465 }
466 }
467 else if (mVertexBuffersDirty || newCommandBuffer)
468 {
Luc Ferron44162472018-04-06 13:20:45 -0400469 if (maxAttrib > 0)
470 {
Luc Ferron44162472018-04-06 13:20:45 -0400471 commandBuffer->bindVertexBuffers(0, maxAttrib, mCurrentArrayBufferHandles.data(),
472 mCurrentArrayBufferOffsets.data());
Jamie Madill316c6062018-05-29 10:49:45 -0400473
474 vk::CommandGraphResource *drawFramebuffer = vk::GetImpl(state.getDrawFramebuffer());
475 updateArrayBufferReadDependencies(drawFramebuffer, activeAttribs,
Jamie Madill21061022018-07-12 23:56:30 -0400476 contextVk->getRenderer()->getCurrentQueueSerial());
Luc Ferron44162472018-04-06 13:20:45 -0400477 }
Luc Ferronc1e02682018-04-11 11:02:55 -0400478
Jamie Madillc3755fc2018-04-05 08:39:13 -0400479 mVertexBuffersDirty = false;
Luc Ferronc1e02682018-04-11 11:02:55 -0400480
481 // This forces the binding to happen if we follow a drawElement call from a drawArrays call.
482 mIndexBufferDirty = true;
Luc Ferron983c4292018-04-10 13:05:45 -0400483
484 // If we've had a drawElements call with a line loop before, we want to make sure this is
485 // invalidated the next time drawElements is called since we use the same index buffer for
486 // both calls.
487 mDirtyLineLoopTranslation = true;
Jamie Madillc3755fc2018-04-05 08:39:13 -0400488 }
489
490 return gl::NoError();
491}
492
493gl::Error VertexArrayVk::onIndexedDraw(const gl::Context *context,
Jamie Madillc3755fc2018-04-05 08:39:13 -0400494 const gl::DrawCallParams &drawCallParams,
Jamie Madill316c6062018-05-29 10:49:45 -0400495 vk::CommandBuffer *commandBuffer,
Jamie Madillc3755fc2018-04-05 08:39:13 -0400496 bool newCommandBuffer)
497{
Jamie Madill21061022018-07-12 23:56:30 -0400498 ContextVk *contextVk = vk::GetImpl(context);
499 ANGLE_TRY(onDraw(context, drawCallParams, commandBuffer, newCommandBuffer));
Luc Ferron49aacad2018-06-12 08:33:59 -0400500 bool isLineLoop = drawCallParams.mode() == gl::PrimitiveMode::LineLoop;
Luc Ferron6ed167a2018-06-13 13:45:55 -0400501 gl::Buffer *glBuffer = mState.getElementArrayBuffer().get();
502 uintptr_t offset =
503 glBuffer && !isLineLoop ? reinterpret_cast<uintptr_t>(drawCallParams.indices()) : 0;
Jamie Madillc3755fc2018-04-05 08:39:13 -0400504
Luc Ferron6ed167a2018-06-13 13:45:55 -0400505 if (!glBuffer && !isLineLoop)
Jamie Madillc3755fc2018-04-05 08:39:13 -0400506 {
507 ANGLE_TRY(drawCallParams.ensureIndexRangeResolved(context));
Jamie Madill21061022018-07-12 23:56:30 -0400508 ANGLE_TRY(streamIndexData(contextVk, drawCallParams));
Jamie Madillc3755fc2018-04-05 08:39:13 -0400509 commandBuffer->bindIndexBuffer(mCurrentElementArrayBufferHandle,
510 mCurrentElementArrayBufferOffset,
511 gl_vk::GetIndexType(drawCallParams.type()));
512 }
Luc Ferron49aacad2018-06-12 08:33:59 -0400513 else if (mIndexBufferDirty || newCommandBuffer || offset != mLastIndexBufferOffset)
Jamie Madillc3755fc2018-04-05 08:39:13 -0400514 {
Luc Ferron3e313802018-06-11 14:44:33 -0400515 if (drawCallParams.type() == GL_UNSIGNED_BYTE &&
516 drawCallParams.mode() != gl::PrimitiveMode::LineLoop)
Jamie Madillc3755fc2018-04-05 08:39:13 -0400517 {
Luc Ferron6ed167a2018-06-13 13:45:55 -0400518 // Unsigned bytes don't have direct support in Vulkan so we have to expand the
519 // memory to a GLushort.
520 BufferVk *bufferVk = vk::GetImpl(glBuffer);
521 void *srcDataMapping = nullptr;
522 ASSERT(!glBuffer->isMapped());
Jamie Madill21061022018-07-12 23:56:30 -0400523 ANGLE_TRY(bufferVk->mapImpl(contextVk, &srcDataMapping));
Rafael Cintron05a449a2018-06-20 18:08:04 -0700524 uint8_t *srcData = static_cast<uint8_t *>(srcDataMapping);
Luc Ferron6ed167a2018-06-13 13:45:55 -0400525 intptr_t offsetIntoSrcData = reinterpret_cast<intptr_t>(drawCallParams.indices());
526 srcData += offsetIntoSrcData;
527
528 // Allocate a new buffer that's double the size of the buffer provided by the user to
529 // go from unsigned byte to unsigned short.
530 uint8_t *allocatedData = nullptr;
531 bool newBufferAllocated = false;
532 uint32_t expandedDataOffset = 0;
Jamie Madilleebe2192018-07-11 09:01:18 -0400533 ANGLE_TRY(mTranslatedByteIndexData.allocate(
Jamie Madill21061022018-07-12 23:56:30 -0400534 contextVk, static_cast<size_t>(bufferVk->getSize()) * 2, &allocatedData,
Jamie Madilleebe2192018-07-11 09:01:18 -0400535 &mCurrentElementArrayBufferHandle, &expandedDataOffset, &newBufferAllocated));
Luc Ferron6ed167a2018-06-13 13:45:55 -0400536 mCurrentElementArrayBufferOffset = static_cast<VkDeviceSize>(expandedDataOffset);
537
538 // Expand the source into the destination
539 ASSERT(!context->getGLState().isPrimitiveRestartEnabled());
540 uint16_t *expandedDst = reinterpret_cast<uint16_t *>(allocatedData);
541 for (GLsizei index = 0; index < bufferVk->getSize() - offsetIntoSrcData; index++)
542 {
543 expandedDst[index] = static_cast<GLushort>(srcData[index]);
544 }
545
546 // Make sure our writes are available.
Jamie Madill21061022018-07-12 23:56:30 -0400547 ANGLE_TRY(mTranslatedByteIndexData.flush(contextVk));
Luc Ferron6ed167a2018-06-13 13:45:55 -0400548 GLboolean result = false;
549 ANGLE_TRY(bufferVk->unmap(context, &result));
550
551 // We do not add the offset from the drawCallParams here because we've already copied
552 // the source starting at the offset requested.
553 commandBuffer->bindIndexBuffer(mCurrentElementArrayBufferHandle,
554 mCurrentElementArrayBufferOffset,
555 gl_vk::GetIndexType(drawCallParams.type()));
556 }
557 else
558 {
559 commandBuffer->bindIndexBuffer(mCurrentElementArrayBufferHandle,
560 mCurrentElementArrayBufferOffset + offset,
561 gl_vk::GetIndexType(drawCallParams.type()));
Jamie Madillc3755fc2018-04-05 08:39:13 -0400562 }
563
Luc Ferron49aacad2018-06-12 08:33:59 -0400564 mLastIndexBufferOffset = offset;
Jamie Madill316c6062018-05-29 10:49:45 -0400565
566 const gl::State &glState = context->getGLState();
567 vk::CommandGraphResource *drawFramebuffer = vk::GetImpl(glState.getDrawFramebuffer());
Jamie Madill21061022018-07-12 23:56:30 -0400568 updateElementArrayBufferReadDependency(drawFramebuffer,
569 contextVk->getRenderer()->getCurrentQueueSerial());
Jamie Madillc3755fc2018-04-05 08:39:13 -0400570 mIndexBufferDirty = false;
Luc Ferron983c4292018-04-10 13:05:45 -0400571
572 // If we've had a drawArrays call with a line loop before, we want to make sure this is
573 // invalidated the next time drawArrays is called since we use the same index buffer for
574 // both calls.
575 mLineLoopBufferFirstIndex.reset();
576 mLineLoopBufferLastIndex.reset();
Jamie Madillc3755fc2018-04-05 08:39:13 -0400577 }
578
579 return gl::NoError();
580}
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400581} // namespace rx