blob: 25f542f927975e3184f1457fb4262c8fcc7c2bed [file] [log] [blame]
Geoff Langf9a6f082015-01-22 13:32:49 -05001//
2// Copyright 2015 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
7// VertexArrayGL.cpp: Implements the class methods for VertexArrayGL.
8
9#include "libANGLE/renderer/gl/VertexArrayGL.h"
10
Jamie Madill20e005b2017-04-07 14:19:22 -040011#include "common/bitset_utils.h"
Geoff Langf9a6f082015-01-22 13:32:49 -050012#include "common/debug.h"
Geoff Lang7c82bc42015-03-09 16:18:08 -040013#include "common/mathutil.h"
Geoff Lang831b1952015-05-05 11:02:27 -040014#include "common/utilities.h"
Geoff Lang6ae6efc2015-03-09 14:42:35 -040015#include "libANGLE/Buffer.h"
Geoff Langba4c4a82015-02-24 12:38:46 -050016#include "libANGLE/angletypes.h"
Geoff Lang7c82bc42015-03-09 16:18:08 -040017#include "libANGLE/formatutils.h"
Geoff Langba4c4a82015-02-24 12:38:46 -050018#include "libANGLE/renderer/gl/BufferGL.h"
19#include "libANGLE/renderer/gl/FunctionsGL.h"
20#include "libANGLE/renderer/gl/StateManagerGL.h"
Jamie Madill231c7f52017-04-26 13:45:37 -040021#include "libANGLE/renderer/gl/renderergl_utils.h"
Geoff Langf9a6f082015-01-22 13:32:49 -050022
Jamie Madill0b9e9032015-08-17 11:51:52 +000023using namespace gl;
24
Geoff Langf9a6f082015-01-22 13:32:49 -050025namespace rx
26{
Jamie Madill0b9e9032015-08-17 11:51:52 +000027namespace
28{
Jiawei-Shao2597fb62016-12-09 16:38:02 +080029// Warning: you should ensure binding really matches attrib.bindingIndex before using this function.
30bool AttributeNeedsStreaming(const VertexAttribute &attrib, const VertexBinding &binding)
Jamie Madill0b9e9032015-08-17 11:51:52 +000031{
Martin Radevdd5f27e2017-06-07 10:17:09 +030032 return (attrib.enabled && binding.getBuffer().get() == nullptr);
Jamie Madill0b9e9032015-08-17 11:51:52 +000033}
Shaodf682a82017-03-31 15:13:21 +080034
35bool SameVertexAttribFormat(const VertexAttribute &a, const VertexAttribute &b)
36{
37 return a.size == b.size && a.type == b.type && a.normalized == b.normalized &&
38 a.pureInteger == b.pureInteger && a.relativeOffset == b.relativeOffset;
39}
40
41bool SameVertexBuffer(const VertexBinding &a, const VertexBinding &b)
42{
Martin Radevdd5f27e2017-06-07 10:17:09 +030043 return a.getStride() == b.getStride() && a.getOffset() == b.getOffset() &&
44 a.getBuffer().get() == b.getBuffer().get();
Shaodf682a82017-03-31 15:13:21 +080045}
46
47bool IsVertexAttribPointerSupported(size_t attribIndex, const VertexAttribute &attrib)
48{
49 return (attribIndex == attrib.bindingIndex && attrib.relativeOffset == 0);
50}
Martin Radev553590a2017-07-31 16:40:39 +030051
52GLuint GetAdjustedDivisor(GLuint numViews, GLuint divisor)
53{
54 return numViews * divisor;
55}
56
Jamie Madill0b9e9032015-08-17 11:51:52 +000057} // anonymous namespace
58
Jamie Madill3f572682016-04-26 13:41:36 -040059VertexArrayGL::VertexArrayGL(const VertexArrayState &state,
Jamie Madill77a90c22015-08-11 16:33:17 -040060 const FunctionsGL *functions,
61 StateManagerGL *stateManager)
Jamie Madill3f572682016-04-26 13:41:36 -040062 : VertexArrayImpl(state),
Geoff Langba4c4a82015-02-24 12:38:46 -050063 mFunctions(functions),
64 mStateManager(stateManager),
65 mVertexArrayID(0),
Martin Radev553590a2017-07-31 16:40:39 +030066 mAppliedNumViews(1),
Jamie Madill77a90c22015-08-11 16:33:17 -040067 mAppliedElementArrayBuffer(),
Jiawei-Shao2597fb62016-12-09 16:38:02 +080068 mAppliedBindings(state.getMaxBindings()),
Geoff Lang7c82bc42015-03-09 16:18:08 -040069 mStreamingElementArrayBufferSize(0),
70 mStreamingElementArrayBuffer(0),
71 mStreamingArrayBufferSize(0),
72 mStreamingArrayBuffer(0)
Geoff Langba4c4a82015-02-24 12:38:46 -050073{
74 ASSERT(mFunctions);
75 ASSERT(mStateManager);
76 mFunctions->genVertexArrays(1, &mVertexArrayID);
77
Jiawei-Shao2597fb62016-12-09 16:38:02 +080078 // Set the cached vertex attribute array and vertex attribute binding array size
79 GLuint maxVertexAttribs = static_cast<GLuint>(state.getMaxAttribs());
80 for (GLuint i = 0; i < maxVertexAttribs; i++)
81 {
82 mAppliedAttributes.emplace_back(i);
83 }
Geoff Langba4c4a82015-02-24 12:38:46 -050084}
Geoff Langf9a6f082015-01-22 13:32:49 -050085
Jamie Madillacf2f3a2017-11-21 19:22:44 -050086VertexArrayGL::~VertexArrayGL()
87{
88}
89
Jamie Madill4928b7c2017-06-20 12:57:39 -040090void VertexArrayGL::destroy(const gl::Context *context)
Geoff Langba4c4a82015-02-24 12:38:46 -050091{
Geoff Lang1eb708e2015-05-04 14:58:23 -040092 mStateManager->deleteVertexArray(mVertexArrayID);
93 mVertexArrayID = 0;
Martin Radev553590a2017-07-31 16:40:39 +030094 mAppliedNumViews = 1;
Geoff Langba4c4a82015-02-24 12:38:46 -050095
Geoff Lang1eb708e2015-05-04 14:58:23 -040096 mStateManager->deleteBuffer(mStreamingElementArrayBuffer);
97 mStreamingElementArrayBufferSize = 0;
Shao80957d92017-02-20 21:25:59 +080098 mStreamingElementArrayBuffer = 0;
Geoff Lang7c82bc42015-03-09 16:18:08 -040099
Geoff Lang1eb708e2015-05-04 14:58:23 -0400100 mStateManager->deleteBuffer(mStreamingArrayBuffer);
101 mStreamingArrayBufferSize = 0;
Shao80957d92017-02-20 21:25:59 +0800102 mStreamingArrayBuffer = 0;
Geoff Lang7c82bc42015-03-09 16:18:08 -0400103
Jamie Madill4928b7c2017-06-20 12:57:39 -0400104 mAppliedElementArrayBuffer.set(context, nullptr);
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800105 for (auto &binding : mAppliedBindings)
Geoff Langba4c4a82015-02-24 12:38:46 -0500106 {
Jamie Madill4928b7c2017-06-20 12:57:39 -0400107 binding.setBuffer(context, nullptr);
Geoff Langba4c4a82015-02-24 12:38:46 -0500108 }
109}
Geoff Langf9a6f082015-01-22 13:32:49 -0500110
Jamie Madill4928b7c2017-06-20 12:57:39 -0400111gl::Error VertexArrayGL::syncDrawArraysState(const gl::Context *context,
112 const gl::AttributesMask &activeAttributesMask,
Jamie Madill0b9e9032015-08-17 11:51:52 +0000113 GLint first,
Geoff Lang3cf12ce2015-08-27 14:40:48 -0400114 GLsizei count,
115 GLsizei instanceCount) const
Geoff Lang7c82bc42015-03-09 16:18:08 -0400116{
Jamie Madill4928b7c2017-06-20 12:57:39 -0400117 return syncDrawState(context, activeAttributesMask, first, count, GL_NONE, nullptr,
118 instanceCount, false, nullptr);
Geoff Lang7c82bc42015-03-09 16:18:08 -0400119}
120
Jamie Madill4928b7c2017-06-20 12:57:39 -0400121gl::Error VertexArrayGL::syncDrawElementsState(const gl::Context *context,
122 const gl::AttributesMask &activeAttributesMask,
Jamie Madill0b9e9032015-08-17 11:51:52 +0000123 GLsizei count,
124 GLenum type,
Jamie Madill876429b2017-04-20 15:46:24 -0400125 const void *indices,
Geoff Lang3cf12ce2015-08-27 14:40:48 -0400126 GLsizei instanceCount,
Geoff Lang3edfe032015-09-04 16:38:24 -0400127 bool primitiveRestartEnabled,
Jamie Madill876429b2017-04-20 15:46:24 -0400128 const void **outIndices) const
Geoff Lang7c82bc42015-03-09 16:18:08 -0400129{
Jamie Madill4928b7c2017-06-20 12:57:39 -0400130 return syncDrawState(context, activeAttributesMask, 0, count, type, indices, instanceCount,
Geoff Lang3edfe032015-09-04 16:38:24 -0400131 primitiveRestartEnabled, outIndices);
Geoff Lang7c82bc42015-03-09 16:18:08 -0400132}
133
Jiajia Qin47474142017-12-29 13:41:00 +0800134void VertexArrayGL::updateElementArrayBufferBinding(const gl::Context *context) const
Jiajia Qind9671222016-11-29 16:30:31 +0800135{
Jamie Madill492f58e2017-10-09 19:41:33 -0400136 gl::Buffer *elementArrayBuffer = mState.getElementArrayBuffer().get();
Jiajia Qin47474142017-12-29 13:41:00 +0800137 if (elementArrayBuffer != nullptr && elementArrayBuffer != mAppliedElementArrayBuffer.get())
Jiajia Qind9671222016-11-29 16:30:31 +0800138 {
139 const BufferGL *bufferGL = GetImplAs<BufferGL>(elementArrayBuffer);
Corentin Wallez336129f2017-10-17 15:55:40 -0400140 mStateManager->bindBuffer(gl::BufferBinding::ElementArray, bufferGL->getBufferID());
Jamie Madill4928b7c2017-06-20 12:57:39 -0400141 mAppliedElementArrayBuffer.set(context, elementArrayBuffer);
Jiajia Qind9671222016-11-29 16:30:31 +0800142 }
Jiajia Qind9671222016-11-29 16:30:31 +0800143}
144
Jamie Madill4928b7c2017-06-20 12:57:39 -0400145gl::Error VertexArrayGL::syncDrawState(const gl::Context *context,
146 const gl::AttributesMask &activeAttributesMask,
Jamie Madill0b9e9032015-08-17 11:51:52 +0000147 GLint first,
148 GLsizei count,
149 GLenum type,
Jamie Madill876429b2017-04-20 15:46:24 -0400150 const void *indices,
Geoff Lang3cf12ce2015-08-27 14:40:48 -0400151 GLsizei instanceCount,
Geoff Lang3edfe032015-09-04 16:38:24 -0400152 bool primitiveRestartEnabled,
Jamie Madill876429b2017-04-20 15:46:24 -0400153 const void **outIndices) const
Geoff Lang6ae6efc2015-03-09 14:42:35 -0400154{
Jamie Madill77a90c22015-08-11 16:33:17 -0400155 mStateManager->bindVertexArray(mVertexArrayID, getAppliedElementArrayBufferID());
Geoff Lang6ae6efc2015-03-09 14:42:35 -0400156
Shao80957d92017-02-20 21:25:59 +0800157 // Check if any attributes need to be streamed, determines if the index range needs to be
158 // computed
Jamie Madill0b9e9032015-08-17 11:51:52 +0000159 bool attributesNeedStreaming = mAttributesNeedStreaming.any();
Geoff Lang7c82bc42015-03-09 16:18:08 -0400160
Shao80957d92017-02-20 21:25:59 +0800161 // Determine if an index buffer needs to be streamed and the range of vertices that need to be
162 // copied
Geoff Lang3edfe032015-09-04 16:38:24 -0400163 IndexRange indexRange;
Geoff Lang7c82bc42015-03-09 16:18:08 -0400164 if (type != GL_NONE)
Geoff Langba4c4a82015-02-24 12:38:46 -0500165 {
Jamie Madill4928b7c2017-06-20 12:57:39 -0400166 ANGLE_TRY(syncIndexData(context, count, type, indices, primitiveRestartEnabled,
167 attributesNeedStreaming, &indexRange, outIndices));
Geoff Lang7c82bc42015-03-09 16:18:08 -0400168 }
169 else
170 {
Corentin Wallez2c34a4b2015-08-25 16:26:02 -0400171 // Not an indexed call, set the range to [first, first + count - 1]
Geoff Lang7c82bc42015-03-09 16:18:08 -0400172 indexRange.start = first;
Shao80957d92017-02-20 21:25:59 +0800173 indexRange.end = first + count - 1;
Geoff Langba4c4a82015-02-24 12:38:46 -0500174 }
175
Jamie Madill0b9e9032015-08-17 11:51:52 +0000176 if (attributesNeedStreaming)
Geoff Langba4c4a82015-02-24 12:38:46 -0500177 {
Jamie Madill4928b7c2017-06-20 12:57:39 -0400178 ANGLE_TRY(streamAttributes(activeAttributesMask, instanceCount, indexRange));
Geoff Lang7c82bc42015-03-09 16:18:08 -0400179 }
180
Yuly Novikovc4d18aa2017-03-09 18:45:02 -0500181 return gl::NoError();
Geoff Lang7c82bc42015-03-09 16:18:08 -0400182}
183
Jamie Madill4928b7c2017-06-20 12:57:39 -0400184gl::Error VertexArrayGL::syncIndexData(const gl::Context *context,
185 GLsizei count,
Jiajia Qind9671222016-11-29 16:30:31 +0800186 GLenum type,
Jamie Madill876429b2017-04-20 15:46:24 -0400187 const void *indices,
Jiajia Qind9671222016-11-29 16:30:31 +0800188 bool primitiveRestartEnabled,
189 bool attributesNeedStreaming,
190 IndexRange *outIndexRange,
Jamie Madill876429b2017-04-20 15:46:24 -0400191 const void **outIndices) const
Geoff Lang7c82bc42015-03-09 16:18:08 -0400192{
193 ASSERT(outIndices);
194
Jamie Madill492f58e2017-10-09 19:41:33 -0400195 gl::Buffer *elementArrayBuffer = mState.getElementArrayBuffer().get();
Jamie Madill8e344942015-07-09 14:22:07 -0400196
Geoff Lang7c82bc42015-03-09 16:18:08 -0400197 // Need to check the range of indices if attributes need to be streamed
Jamie Madill8e344942015-07-09 14:22:07 -0400198 if (elementArrayBuffer != nullptr)
Geoff Lang7c82bc42015-03-09 16:18:08 -0400199 {
Jiajia Qin47474142017-12-29 13:41:00 +0800200 ASSERT(elementArrayBuffer == mAppliedElementArrayBuffer.get());
Geoff Lang7c82bc42015-03-09 16:18:08 -0400201 // Only compute the index range if the attributes also need to be streamed
202 if (attributesNeedStreaming)
203 {
204 ptrdiff_t elementArrayBufferOffset = reinterpret_cast<ptrdiff_t>(indices);
Jamie Madill492f58e2017-10-09 19:41:33 -0400205 Error error = mState.getElementArrayBuffer()->getIndexRange(
Jamie Madill33510102017-09-20 10:39:18 -0400206 context, type, elementArrayBufferOffset, count, primitiveRestartEnabled,
207 outIndexRange);
Geoff Lang520c4ae2015-05-05 13:12:36 -0400208 if (error.isError())
Geoff Lang7c82bc42015-03-09 16:18:08 -0400209 {
Geoff Lang520c4ae2015-05-05 13:12:36 -0400210 return error;
Geoff Lang7c82bc42015-03-09 16:18:08 -0400211 }
212 }
213
Shao80957d92017-02-20 21:25:59 +0800214 // Indices serves as an offset into the index buffer in this case, use the same value for
215 // the draw call
Geoff Lang7c82bc42015-03-09 16:18:08 -0400216 *outIndices = indices;
217 }
218 else
219 {
220 // Need to stream the index buffer
221 // TODO: if GLES, nothing needs to be streamed
222
223 // Only compute the index range if the attributes also need to be streamed
224 if (attributesNeedStreaming)
225 {
Geoff Lang3edfe032015-09-04 16:38:24 -0400226 *outIndexRange = ComputeIndexRange(type, indices, count, primitiveRestartEnabled);
Geoff Lang7c82bc42015-03-09 16:18:08 -0400227 }
228
229 // Allocate the streaming element array buffer
230 if (mStreamingElementArrayBuffer == 0)
231 {
232 mFunctions->genBuffers(1, &mStreamingElementArrayBuffer);
233 mStreamingElementArrayBufferSize = 0;
234 }
235
Corentin Wallez336129f2017-10-17 15:55:40 -0400236 mStateManager->bindBuffer(gl::BufferBinding::ElementArray, mStreamingElementArrayBuffer);
Jamie Madill4928b7c2017-06-20 12:57:39 -0400237 mAppliedElementArrayBuffer.set(context, nullptr);
Geoff Lang7c82bc42015-03-09 16:18:08 -0400238
239 // Make sure the element array buffer is large enough
Jamie Madill0b9e9032015-08-17 11:51:52 +0000240 const Type &indexTypeInfo = GetTypeInfo(type);
Geoff Lang7c82bc42015-03-09 16:18:08 -0400241 size_t requiredStreamingBufferSize = indexTypeInfo.bytes * count;
242 if (requiredStreamingBufferSize > mStreamingElementArrayBufferSize)
243 {
244 // Copy the indices in while resizing the buffer
Shao80957d92017-02-20 21:25:59 +0800245 mFunctions->bufferData(GL_ELEMENT_ARRAY_BUFFER, requiredStreamingBufferSize, indices,
246 GL_DYNAMIC_DRAW);
Geoff Lang7c82bc42015-03-09 16:18:08 -0400247 mStreamingElementArrayBufferSize = requiredStreamingBufferSize;
248 }
249 else
250 {
251 // Put the indices at the beginning of the buffer
Shao80957d92017-02-20 21:25:59 +0800252 mFunctions->bufferSubData(GL_ELEMENT_ARRAY_BUFFER, 0, requiredStreamingBufferSize,
253 indices);
Geoff Lang7c82bc42015-03-09 16:18:08 -0400254 }
255
Shao80957d92017-02-20 21:25:59 +0800256 // Set the index offset for the draw call to zero since the supplied index pointer is to
257 // client data
Geoff Lang7c82bc42015-03-09 16:18:08 -0400258 *outIndices = nullptr;
259 }
260
Yuly Novikovc4d18aa2017-03-09 18:45:02 -0500261 return gl::NoError();
Geoff Lang7c82bc42015-03-09 16:18:08 -0400262}
263
Jamie Madill0b9e9032015-08-17 11:51:52 +0000264void VertexArrayGL::computeStreamingAttributeSizes(const gl::AttributesMask &activeAttributesMask,
Geoff Lang3cf12ce2015-08-27 14:40:48 -0400265 GLsizei instanceCount,
Geoff Lang3edfe032015-09-04 16:38:24 -0400266 const gl::IndexRange &indexRange,
Jamie Madill0b9e9032015-08-17 11:51:52 +0000267 size_t *outStreamingDataSize,
268 size_t *outMaxAttributeDataSize) const
Geoff Lang7c82bc42015-03-09 16:18:08 -0400269{
Jamie Madill0b9e9032015-08-17 11:51:52 +0000270 *outStreamingDataSize = 0;
271 *outMaxAttributeDataSize = 0;
272
273 ASSERT(mAttributesNeedStreaming.any());
274
Jamie Madill492f58e2017-10-09 19:41:33 -0400275 const auto &attribs = mState.getVertexAttributes();
276 const auto &bindings = mState.getVertexBindings();
Jamie Madill6de51852017-04-12 09:53:01 -0400277
278 gl::AttributesMask attribsToStream = (mAttributesNeedStreaming & activeAttributesMask);
279
280 for (auto idx : attribsToStream)
Jamie Madill0b9e9032015-08-17 11:51:52 +0000281 {
Shao80957d92017-02-20 21:25:59 +0800282 const auto &attrib = attribs[idx];
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800283 const auto &binding = bindings[attrib.bindingIndex];
284 ASSERT(AttributeNeedsStreaming(attrib, binding));
Jamie Madill0b9e9032015-08-17 11:51:52 +0000285
Jamie Madill0b9e9032015-08-17 11:51:52 +0000286 // If streaming is going to be required, compute the size of the required buffer
287 // and how much slack space at the beginning of the buffer will be required by determining
288 // the attribute with the largest data size.
289 size_t typeSize = ComputeVertexAttributeTypeSize(attrib);
Martin Radev553590a2017-07-31 16:40:39 +0300290 GLuint adjustedDivisor = GetAdjustedDivisor(mAppliedNumViews, binding.getDivisor());
291 *outStreamingDataSize +=
292 typeSize * ComputeVertexBindingElementCount(adjustedDivisor, indexRange.vertexCount(),
293 instanceCount);
Jamie Madill0b9e9032015-08-17 11:51:52 +0000294 *outMaxAttributeDataSize = std::max(*outMaxAttributeDataSize, typeSize);
295 }
296}
297
298gl::Error VertexArrayGL::streamAttributes(const gl::AttributesMask &activeAttributesMask,
Geoff Lang3cf12ce2015-08-27 14:40:48 -0400299 GLsizei instanceCount,
Geoff Lang3edfe032015-09-04 16:38:24 -0400300 const gl::IndexRange &indexRange) const
Jamie Madill0b9e9032015-08-17 11:51:52 +0000301{
302 // Sync the vertex attribute state and track what data needs to be streamed
303 size_t streamingDataSize = 0;
304 size_t maxAttributeDataSize = 0;
305
Geoff Lang3cf12ce2015-08-27 14:40:48 -0400306 computeStreamingAttributeSizes(activeAttributesMask, instanceCount, indexRange,
307 &streamingDataSize, &maxAttributeDataSize);
Jamie Madill0b9e9032015-08-17 11:51:52 +0000308
309 if (streamingDataSize == 0)
310 {
He Yunchaoacd18982017-01-04 10:46:42 +0800311 return gl::NoError();
Jamie Madill0b9e9032015-08-17 11:51:52 +0000312 }
313
Geoff Lang7c82bc42015-03-09 16:18:08 -0400314 if (mStreamingArrayBuffer == 0)
315 {
316 mFunctions->genBuffers(1, &mStreamingArrayBuffer);
317 mStreamingArrayBufferSize = 0;
318 }
319
Shao80957d92017-02-20 21:25:59 +0800320 // If first is greater than zero, a slack space needs to be left at the beginning of the buffer
321 // so that the same 'first' argument can be passed into the draw call.
322 const size_t bufferEmptySpace = maxAttributeDataSize * indexRange.start;
Geoff Lang7c82bc42015-03-09 16:18:08 -0400323 const size_t requiredBufferSize = streamingDataSize + bufferEmptySpace;
324
Corentin Wallez336129f2017-10-17 15:55:40 -0400325 mStateManager->bindBuffer(gl::BufferBinding::Array, mStreamingArrayBuffer);
Geoff Lang7c82bc42015-03-09 16:18:08 -0400326 if (requiredBufferSize > mStreamingArrayBufferSize)
327 {
328 mFunctions->bufferData(GL_ARRAY_BUFFER, requiredBufferSize, nullptr, GL_DYNAMIC_DRAW);
329 mStreamingArrayBufferSize = requiredBufferSize;
330 }
331
332 // Unmapping a buffer can return GL_FALSE to indicate that the system has corrupted the data
Shao80957d92017-02-20 21:25:59 +0800333 // somehow (such as by a screen change), retry writing the data a few times and return
334 // OUT_OF_MEMORY if that fails.
335 GLboolean unmapResult = GL_FALSE;
Geoff Lang7c82bc42015-03-09 16:18:08 -0400336 size_t unmapRetryAttempts = 5;
337 while (unmapResult != GL_TRUE && --unmapRetryAttempts > 0)
338 {
Geoff Langb2ebb5e2016-06-08 18:17:55 +0000339 uint8_t *bufferPointer = MapBufferRangeWithFallback(mFunctions, GL_ARRAY_BUFFER, 0,
340 requiredBufferSize, GL_MAP_WRITE_BIT);
Geoff Lang7c82bc42015-03-09 16:18:08 -0400341 size_t curBufferOffset = bufferEmptySpace;
342
Jamie Madill492f58e2017-10-09 19:41:33 -0400343 const auto &attribs = mState.getVertexAttributes();
344 const auto &bindings = mState.getVertexBindings();
Jamie Madill6de51852017-04-12 09:53:01 -0400345
346 gl::AttributesMask attribsToStream = (mAttributesNeedStreaming & activeAttributesMask);
347
348 for (auto idx : attribsToStream)
Geoff Lang7c82bc42015-03-09 16:18:08 -0400349 {
Shao80957d92017-02-20 21:25:59 +0800350 const auto &attrib = attribs[idx];
Shaodde78e82017-05-22 14:13:27 +0800351 ASSERT(IsVertexAttribPointerSupported(idx, attrib));
352
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800353 const auto &binding = bindings[attrib.bindingIndex];
354 ASSERT(AttributeNeedsStreaming(attrib, binding));
Geoff Lang7c82bc42015-03-09 16:18:08 -0400355
Martin Radev553590a2017-07-31 16:40:39 +0300356 GLuint adjustedDivisor = GetAdjustedDivisor(mAppliedNumViews, binding.getDivisor());
357 const size_t streamedVertexCount = ComputeVertexBindingElementCount(
358 adjustedDivisor, indexRange.vertexCount(), instanceCount);
Geoff Lang3cf12ce2015-08-27 14:40:48 -0400359
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800360 const size_t sourceStride = ComputeVertexAttributeStride(attrib, binding);
Jamie Madill0b9e9032015-08-17 11:51:52 +0000361 const size_t destStride = ComputeVertexAttributeTypeSize(attrib);
362
Geoff Lang38a24a92017-02-15 13:53:06 -0500363 // Vertices do not apply the 'start' offset when the divisor is non-zero even when doing
364 // a non-instanced draw call
Martin Radev553590a2017-07-31 16:40:39 +0300365 const size_t firstIndex = adjustedDivisor == 0 ? indexRange.start : 0;
Geoff Lang38a24a92017-02-15 13:53:06 -0500366
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800367 // Attributes using client memory ignore the VERTEX_ATTRIB_BINDING state.
368 // https://www.opengl.org/registry/specs/ARB/vertex_attrib_binding.txt
Jamie Madill0b9e9032015-08-17 11:51:52 +0000369 const uint8_t *inputPointer = reinterpret_cast<const uint8_t *>(attrib.pointer);
370
371 // Pack the data when copying it, user could have supplied a very large stride that
372 // would cause the buffer to be much larger than needed.
373 if (destStride == sourceStride)
Jamie Madill8e344942015-07-09 14:22:07 -0400374 {
Jamie Madill0b9e9032015-08-17 11:51:52 +0000375 // Can copy in one go, the data is packed
Geoff Lang38a24a92017-02-15 13:53:06 -0500376 memcpy(bufferPointer + curBufferOffset, inputPointer + (sourceStride * firstIndex),
Jamie Madill0b9e9032015-08-17 11:51:52 +0000377 destStride * streamedVertexCount);
Jamie Madill6d51c702015-08-14 10:38:10 -0400378 }
Jamie Madill0b9e9032015-08-17 11:51:52 +0000379 else
380 {
381 // Copy each vertex individually
Geoff Lang6b10ddb2015-09-02 15:55:10 -0400382 for (size_t vertexIdx = 0; vertexIdx < streamedVertexCount; vertexIdx++)
Jamie Madill0b9e9032015-08-17 11:51:52 +0000383 {
Shao80957d92017-02-20 21:25:59 +0800384 uint8_t *out = bufferPointer + curBufferOffset + (destStride * vertexIdx);
Geoff Lang38a24a92017-02-15 13:53:06 -0500385 const uint8_t *in = inputPointer + sourceStride * (vertexIdx + firstIndex);
Corentin Wallez4d5362d2015-08-25 11:25:14 -0400386 memcpy(out, in, destStride);
Jamie Madill0b9e9032015-08-17 11:51:52 +0000387 }
388 }
389
390 // Compute where the 0-index vertex would be.
Geoff Lang38a24a92017-02-15 13:53:06 -0500391 const size_t vertexStartOffset = curBufferOffset - (firstIndex * destStride);
Jamie Madill0b9e9032015-08-17 11:51:52 +0000392
Shaodf682a82017-03-31 15:13:21 +0800393 callVertexAttribPointer(static_cast<GLuint>(idx), attrib,
394 static_cast<GLsizei>(destStride),
395 static_cast<GLintptr>(vertexStartOffset));
Jamie Madill0b9e9032015-08-17 11:51:52 +0000396
397 curBufferOffset += destStride * streamedVertexCount;
Geoff Lang7c82bc42015-03-09 16:18:08 -0400398 }
399
400 unmapResult = mFunctions->unmapBuffer(GL_ARRAY_BUFFER);
401 }
402
403 if (unmapResult != GL_TRUE)
404 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -0500405 return gl::OutOfMemory() << "Failed to unmap the client data streaming buffer.";
Geoff Lang7c82bc42015-03-09 16:18:08 -0400406 }
407
Yuly Novikovc4d18aa2017-03-09 18:45:02 -0500408 return gl::NoError();
Geoff Langba4c4a82015-02-24 12:38:46 -0500409}
410
411GLuint VertexArrayGL::getVertexArrayID() const
412{
413 return mVertexArrayID;
Geoff Langf9a6f082015-01-22 13:32:49 -0500414}
415
Geoff Lang294cad92015-05-26 15:11:23 -0400416GLuint VertexArrayGL::getAppliedElementArrayBufferID() const
417{
Jamie Madill77a90c22015-08-11 16:33:17 -0400418 if (mAppliedElementArrayBuffer.get() == nullptr)
419 {
420 return mStreamingElementArrayBuffer;
421 }
422
423 return GetImplAs<BufferGL>(mAppliedElementArrayBuffer.get())->getBufferID();
Geoff Lang294cad92015-05-26 15:11:23 -0400424}
425
Jamie Madill0b9e9032015-08-17 11:51:52 +0000426void VertexArrayGL::updateNeedsStreaming(size_t attribIndex)
427{
Jamie Madill492f58e2017-10-09 19:41:33 -0400428 const auto &attrib = mState.getVertexAttribute(attribIndex);
429 const auto &binding = mState.getBindingFromAttribIndex(attribIndex);
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800430 mAttributesNeedStreaming.set(attribIndex, AttributeNeedsStreaming(attrib, binding));
Geoff Langf9a6f082015-01-22 13:32:49 -0500431}
Jamie Madill0b9e9032015-08-17 11:51:52 +0000432
433void VertexArrayGL::updateAttribEnabled(size_t attribIndex)
434{
Jamie Madill492f58e2017-10-09 19:41:33 -0400435 const bool enabled = mState.getVertexAttribute(attribIndex).enabled;
Shaodf682a82017-03-31 15:13:21 +0800436 if (mAppliedAttributes[attribIndex].enabled == enabled)
Jamie Madill0b9e9032015-08-17 11:51:52 +0000437 {
438 return;
439 }
440
441 updateNeedsStreaming(attribIndex);
442
Shaodf682a82017-03-31 15:13:21 +0800443 if (enabled)
Jamie Madill0b9e9032015-08-17 11:51:52 +0000444 {
445 mFunctions->enableVertexAttribArray(static_cast<GLuint>(attribIndex));
446 }
447 else
448 {
449 mFunctions->disableVertexAttribArray(static_cast<GLuint>(attribIndex));
450 }
Shaodf682a82017-03-31 15:13:21 +0800451
452 mAppliedAttributes[attribIndex].enabled = enabled;
Jamie Madill0b9e9032015-08-17 11:51:52 +0000453}
454
Jamie Madill4928b7c2017-06-20 12:57:39 -0400455void VertexArrayGL::updateAttribPointer(const gl::Context *context, size_t attribIndex)
Jamie Madill0b9e9032015-08-17 11:51:52 +0000456{
Jamie Madill492f58e2017-10-09 19:41:33 -0400457 const VertexAttribute &attrib = mState.getVertexAttribute(attribIndex);
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800458
Shaodde78e82017-05-22 14:13:27 +0800459 // According to SPEC, VertexAttribPointer should update the binding indexed attribIndex instead
460 // of the binding indexed attrib.bindingIndex (unless attribIndex == attrib.bindingIndex).
Jamie Madill492f58e2017-10-09 19:41:33 -0400461 const VertexBinding &binding = mState.getVertexBinding(attribIndex);
Shaodf682a82017-03-31 15:13:21 +0800462
Shaodde78e82017-05-22 14:13:27 +0800463 // Since mAttributesNeedStreaming[attribIndex] keeps the value set in the last draw, here we
464 // only need to update it when the buffer has been changed. e.g. When we set an attribute to be
465 // streamed in the last draw, and only change its format in this draw without calling
466 // updateNeedsStreaming, it will still be streamed because the flag is already on.
467 const auto &bindingBuffer = binding.getBuffer();
468 if (bindingBuffer != mAppliedBindings[attribIndex].getBuffer())
469 {
470 updateNeedsStreaming(attribIndex);
471 }
472
473 // Early return when the vertex attribute isn't using a buffer object:
474 // - If we need to stream, defer the attribPointer to the draw call.
475 // - Skip the attribute that is disabled and uses a client memory pointer.
476 // - Skip the attribute whose buffer is detached by BindVertexBuffer. Since it cannot have a
477 // client memory pointer either, it must be disabled and shouldn't affect the draw.
478 const Buffer *arrayBuffer = bindingBuffer.get();
479 if (arrayBuffer == nullptr)
480 {
481 // Mark the applied binding isn't using a buffer by setting its buffer to nullptr so that if
482 // it starts to use a buffer later, there is no chance that the caching will skip it.
483 mAppliedBindings[attribIndex].setBuffer(context, nullptr);
484 return;
485 }
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800486
Shaodf682a82017-03-31 15:13:21 +0800487 // We do not need to compare attrib.pointer because when we use a different client memory
488 // pointer, we don't need to update mAttributesNeedStreaming by binding.buffer and we won't
489 // update attribPointer in this function.
Shaodde78e82017-05-22 14:13:27 +0800490 if ((SameVertexAttribFormat(mAppliedAttributes[attribIndex], attrib)) &&
491 (mAppliedAttributes[attribIndex].bindingIndex == attrib.bindingIndex) &&
492 (SameVertexBuffer(mAppliedBindings[attribIndex], binding)))
Jamie Madill0b9e9032015-08-17 11:51:52 +0000493 {
494 return;
495 }
496
Shao4181bc92017-03-17 14:55:25 +0800497 // Since ANGLE always uses a non-zero VAO, we cannot use a client memory pointer on it:
498 // [OpenGL ES 3.0.2] Section 2.8 page 24:
499 // An INVALID_OPERATION error is generated when a non-zero vertex array object is bound,
500 // zero is bound to the ARRAY_BUFFER buffer object binding point, and the pointer argument
501 // is not NULL.
Shaodf682a82017-03-31 15:13:21 +0800502
Shao4181bc92017-03-17 14:55:25 +0800503 const BufferGL *arrayBufferGL = GetImplAs<BufferGL>(arrayBuffer);
Corentin Wallez336129f2017-10-17 15:55:40 -0400504 mStateManager->bindBuffer(gl::BufferBinding::Array, arrayBufferGL->getBufferID());
Martin Radevdd5f27e2017-06-07 10:17:09 +0300505 callVertexAttribPointer(static_cast<GLuint>(attribIndex), attrib, binding.getStride(),
506 binding.getOffset());
Shaodf682a82017-03-31 15:13:21 +0800507
Shaodde78e82017-05-22 14:13:27 +0800508 mAppliedAttributes[attribIndex].size = attrib.size;
509 mAppliedAttributes[attribIndex].type = attrib.type;
510 mAppliedAttributes[attribIndex].normalized = attrib.normalized;
511 mAppliedAttributes[attribIndex].pureInteger = attrib.pureInteger;
Shaodf682a82017-03-31 15:13:21 +0800512
Shaodde78e82017-05-22 14:13:27 +0800513 // After VertexAttribPointer, attrib.relativeOffset is set to 0 and attrib.bindingIndex is set
514 // to attribIndex in driver. If attrib.relativeOffset != 0 or attrib.bindingIndex !=
515 // attribIndex, they should be set in updateAttribFormat and updateAttribBinding. The cache
516 // should be consistent with driver so that we won't miss anything.
517 mAppliedAttributes[attribIndex].relativeOffset = 0;
518 mAppliedAttributes[attribIndex].bindingIndex = static_cast<GLuint>(attribIndex);
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800519
Shaodde78e82017-05-22 14:13:27 +0800520 mAppliedBindings[attribIndex].setStride(binding.getStride());
521 mAppliedBindings[attribIndex].setOffset(binding.getOffset());
522 mAppliedBindings[attribIndex].setBuffer(context, binding.getBuffer().get());
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800523}
524
Shaodf682a82017-03-31 15:13:21 +0800525void VertexArrayGL::callVertexAttribPointer(GLuint attribIndex,
526 const VertexAttribute &attrib,
527 GLsizei stride,
528 GLintptr offset) const
529{
530 const GLvoid *pointer = reinterpret_cast<const GLvoid *>(offset);
531 if (attrib.pureInteger)
532 {
533 ASSERT(!attrib.normalized);
534 mFunctions->vertexAttribIPointer(attribIndex, attrib.size, attrib.type, stride, pointer);
535 }
536 else
537 {
538 mFunctions->vertexAttribPointer(attribIndex, attrib.size, attrib.type, attrib.normalized,
539 stride, pointer);
540 }
541}
542
Shaodde78e82017-05-22 14:13:27 +0800543bool VertexArrayGL::supportVertexAttribBinding() const
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800544{
Shaodde78e82017-05-22 14:13:27 +0800545 ASSERT(mFunctions);
546 return (mFunctions->vertexAttribBinding != nullptr);
547}
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800548
Shaodde78e82017-05-22 14:13:27 +0800549void VertexArrayGL::updateAttribFormat(size_t attribIndex)
550{
551 ASSERT(supportVertexAttribBinding());
552
Jamie Madill492f58e2017-10-09 19:41:33 -0400553 const VertexAttribute &attrib = mState.getVertexAttribute(attribIndex);
Shaodde78e82017-05-22 14:13:27 +0800554 if (SameVertexAttribFormat(mAppliedAttributes[attribIndex], attrib))
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800555 {
Shaodf682a82017-03-31 15:13:21 +0800556 return;
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800557 }
Shaodf682a82017-03-31 15:13:21 +0800558
Shaodde78e82017-05-22 14:13:27 +0800559 if (attrib.pureInteger)
560 {
561 ASSERT(!attrib.normalized);
562 mFunctions->vertexAttribIFormat(static_cast<GLuint>(attribIndex), attrib.size, attrib.type,
563 attrib.relativeOffset);
564 }
565 else
566 {
567 mFunctions->vertexAttribFormat(static_cast<GLuint>(attribIndex), attrib.size, attrib.type,
568 attrib.normalized, attrib.relativeOffset);
569 }
570
571 mAppliedAttributes[attribIndex].size = attrib.size;
572 mAppliedAttributes[attribIndex].type = attrib.type;
573 mAppliedAttributes[attribIndex].normalized = attrib.normalized;
574 mAppliedAttributes[attribIndex].pureInteger = attrib.pureInteger;
575 mAppliedAttributes[attribIndex].relativeOffset = attrib.relativeOffset;
576}
577
578void VertexArrayGL::updateAttribBinding(size_t attribIndex)
579{
580 ASSERT(supportVertexAttribBinding());
581
Jamie Madill492f58e2017-10-09 19:41:33 -0400582 GLuint bindingIndex = mState.getVertexAttribute(attribIndex).bindingIndex;
Shaodde78e82017-05-22 14:13:27 +0800583 if (mAppliedAttributes[attribIndex].bindingIndex == bindingIndex)
584 {
585 return;
586 }
587
588 mFunctions->vertexAttribBinding(static_cast<GLuint>(attribIndex), bindingIndex);
Shaodf682a82017-03-31 15:13:21 +0800589
590 mAppliedAttributes[attribIndex].bindingIndex = bindingIndex;
Shaodde78e82017-05-22 14:13:27 +0800591}
592
593void VertexArrayGL::updateBindingBuffer(const gl::Context *context, size_t bindingIndex)
594{
595 ASSERT(supportVertexAttribBinding());
596
Jamie Madill492f58e2017-10-09 19:41:33 -0400597 const VertexBinding &binding = mState.getVertexBinding(bindingIndex);
Shaodde78e82017-05-22 14:13:27 +0800598 if (SameVertexBuffer(mAppliedBindings[bindingIndex], binding))
599 {
600 return;
601 }
602
603 const Buffer *arrayBuffer = binding.getBuffer().get();
604 GLuint bufferId = 0;
605 if (arrayBuffer != nullptr)
606 {
607 bufferId = GetImplAs<BufferGL>(arrayBuffer)->getBufferID();
608 }
609
610 mFunctions->bindVertexBuffer(static_cast<GLuint>(bindingIndex), bufferId, binding.getOffset(),
611 binding.getStride());
612
613 mAppliedBindings[bindingIndex].setStride(binding.getStride());
614 mAppliedBindings[bindingIndex].setOffset(binding.getOffset());
615 mAppliedBindings[bindingIndex].setBuffer(context, binding.getBuffer().get());
616}
617
618void VertexArrayGL::updateBindingDivisor(size_t bindingIndex)
619{
Martin Radev553590a2017-07-31 16:40:39 +0300620 GLuint adjustedDivisor =
Jamie Madill492f58e2017-10-09 19:41:33 -0400621 GetAdjustedDivisor(mAppliedNumViews, mState.getVertexBinding(bindingIndex).getDivisor());
Martin Radev553590a2017-07-31 16:40:39 +0300622 if (mAppliedBindings[bindingIndex].getDivisor() == adjustedDivisor)
Shaodde78e82017-05-22 14:13:27 +0800623 {
624 return;
625 }
626
627 if (supportVertexAttribBinding())
628 {
Martin Radev553590a2017-07-31 16:40:39 +0300629 mFunctions->vertexBindingDivisor(static_cast<GLuint>(bindingIndex), adjustedDivisor);
Shaodde78e82017-05-22 14:13:27 +0800630 }
631 else
632 {
633 // We can only use VertexAttribDivisor on platforms that don't support Vertex Attrib
634 // Binding.
Martin Radev553590a2017-07-31 16:40:39 +0300635 mFunctions->vertexAttribDivisor(static_cast<GLuint>(bindingIndex), adjustedDivisor);
Shaodde78e82017-05-22 14:13:27 +0800636 }
637
Martin Radev553590a2017-07-31 16:40:39 +0300638 mAppliedBindings[bindingIndex].setDivisor(adjustedDivisor);
Jamie Madill0b9e9032015-08-17 11:51:52 +0000639}
640
Jamie Madillc564c072017-06-01 12:45:42 -0400641void VertexArrayGL::syncState(const gl::Context *context, const VertexArray::DirtyBits &dirtyBits)
Jamie Madill0b9e9032015-08-17 11:51:52 +0000642{
Shaodf682a82017-03-31 15:13:21 +0800643 mStateManager->bindVertexArray(mVertexArrayID, getAppliedElementArrayBufferID());
644
Jamie Madill6de51852017-04-12 09:53:01 -0400645 for (size_t dirtyBit : dirtyBits)
Jamie Madill0b9e9032015-08-17 11:51:52 +0000646 {
647 if (dirtyBit == VertexArray::DIRTY_BIT_ELEMENT_ARRAY_BUFFER)
648 {
Jiajia Qin47474142017-12-29 13:41:00 +0800649 updateElementArrayBufferBinding(context);
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800650 continue;
Jamie Madill0b9e9032015-08-17 11:51:52 +0000651 }
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800652
Shaodde78e82017-05-22 14:13:27 +0800653 size_t index = VertexArray::GetVertexIndexFromDirtyBit(dirtyBit);
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800654 if (dirtyBit >= VertexArray::DIRTY_BIT_ATTRIB_0_ENABLED &&
655 dirtyBit < VertexArray::DIRTY_BIT_ATTRIB_MAX_ENABLED)
Jamie Madill0b9e9032015-08-17 11:51:52 +0000656 {
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800657 updateAttribEnabled(index);
Jamie Madill0b9e9032015-08-17 11:51:52 +0000658 }
659 else if (dirtyBit >= VertexArray::DIRTY_BIT_ATTRIB_0_POINTER &&
660 dirtyBit < VertexArray::DIRTY_BIT_ATTRIB_MAX_POINTER)
661 {
Jamie Madill4928b7c2017-06-20 12:57:39 -0400662 updateAttribPointer(context, index);
Jamie Madill0b9e9032015-08-17 11:51:52 +0000663 }
Shaodde78e82017-05-22 14:13:27 +0800664
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800665 else if (dirtyBit >= VertexArray::DIRTY_BIT_ATTRIB_0_FORMAT &&
Shaodde78e82017-05-22 14:13:27 +0800666 dirtyBit < VertexArray::DIRTY_BIT_ATTRIB_MAX_FORMAT)
667 {
668 ASSERT(supportVertexAttribBinding());
669 updateAttribFormat(index);
670 }
671 else if (dirtyBit >= VertexArray::DIRTY_BIT_ATTRIB_0_BINDING &&
672 dirtyBit < VertexArray::DIRTY_BIT_ATTRIB_MAX_BINDING)
673 {
674 ASSERT(supportVertexAttribBinding());
675 updateAttribBinding(index);
676 }
677 else if (dirtyBit >= VertexArray::DIRTY_BIT_BINDING_0_BUFFER &&
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800678 dirtyBit < VertexArray::DIRTY_BIT_BINDING_MAX_BUFFER)
Jamie Madill0b9e9032015-08-17 11:51:52 +0000679 {
Shaodde78e82017-05-22 14:13:27 +0800680 ASSERT(supportVertexAttribBinding());
681 updateBindingBuffer(context, index);
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800682 }
Shaodde78e82017-05-22 14:13:27 +0800683
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800684 else if (dirtyBit >= VertexArray::DIRTY_BIT_BINDING_0_DIVISOR &&
685 dirtyBit < VertexArray::DIRTY_BIT_BINDING_MAX_DIVISOR)
686 {
Shaodde78e82017-05-22 14:13:27 +0800687 updateBindingDivisor(index);
Jamie Madill0b9e9032015-08-17 11:51:52 +0000688 }
689 else
690 UNREACHABLE();
691 }
692}
693
Martin Radev553590a2017-07-31 16:40:39 +0300694void VertexArrayGL::applyNumViewsToDivisor(int numViews)
695{
696 if (numViews != mAppliedNumViews)
697 {
698 mStateManager->bindVertexArray(mVertexArrayID, getAppliedElementArrayBufferID());
699 mAppliedNumViews = numViews;
700 for (size_t index = 0u; index < mAppliedBindings.size(); ++index)
701 {
702 updateBindingDivisor(index);
703 }
704 }
705}
706
Jamie Madill0b9e9032015-08-17 11:51:52 +0000707} // rx