blob: 512baa5633f13234caf5039a97089780d6185d03 [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
Jamie Madill4928b7c2017-06-20 12:57:39 -0400134gl::Error VertexArrayGL::syncElementArrayState(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 Qind9671222016-11-29 16:30:31 +0800137 ASSERT(elementArrayBuffer);
138 if (elementArrayBuffer != mAppliedElementArrayBuffer.get())
139 {
140 const BufferGL *bufferGL = GetImplAs<BufferGL>(elementArrayBuffer);
Corentin Wallez336129f2017-10-17 15:55:40 -0400141 mStateManager->bindBuffer(gl::BufferBinding::ElementArray, bufferGL->getBufferID());
Jamie Madill4928b7c2017-06-20 12:57:39 -0400142 mAppliedElementArrayBuffer.set(context, elementArrayBuffer);
Jiajia Qind9671222016-11-29 16:30:31 +0800143 }
144
145 return gl::NoError();
146}
147
Jamie Madill4928b7c2017-06-20 12:57:39 -0400148gl::Error VertexArrayGL::syncDrawState(const gl::Context *context,
149 const gl::AttributesMask &activeAttributesMask,
Jamie Madill0b9e9032015-08-17 11:51:52 +0000150 GLint first,
151 GLsizei count,
152 GLenum type,
Jamie Madill876429b2017-04-20 15:46:24 -0400153 const void *indices,
Geoff Lang3cf12ce2015-08-27 14:40:48 -0400154 GLsizei instanceCount,
Geoff Lang3edfe032015-09-04 16:38:24 -0400155 bool primitiveRestartEnabled,
Jamie Madill876429b2017-04-20 15:46:24 -0400156 const void **outIndices) const
Geoff Lang6ae6efc2015-03-09 14:42:35 -0400157{
Jamie Madill77a90c22015-08-11 16:33:17 -0400158 mStateManager->bindVertexArray(mVertexArrayID, getAppliedElementArrayBufferID());
Geoff Lang6ae6efc2015-03-09 14:42:35 -0400159
Shao80957d92017-02-20 21:25:59 +0800160 // Check if any attributes need to be streamed, determines if the index range needs to be
161 // computed
Jamie Madill0b9e9032015-08-17 11:51:52 +0000162 bool attributesNeedStreaming = mAttributesNeedStreaming.any();
Geoff Lang7c82bc42015-03-09 16:18:08 -0400163
Shao80957d92017-02-20 21:25:59 +0800164 // Determine if an index buffer needs to be streamed and the range of vertices that need to be
165 // copied
Geoff Lang3edfe032015-09-04 16:38:24 -0400166 IndexRange indexRange;
Geoff Lang7c82bc42015-03-09 16:18:08 -0400167 if (type != GL_NONE)
Geoff Langba4c4a82015-02-24 12:38:46 -0500168 {
Jamie Madill4928b7c2017-06-20 12:57:39 -0400169 ANGLE_TRY(syncIndexData(context, count, type, indices, primitiveRestartEnabled,
170 attributesNeedStreaming, &indexRange, outIndices));
Geoff Lang7c82bc42015-03-09 16:18:08 -0400171 }
172 else
173 {
Corentin Wallez2c34a4b2015-08-25 16:26:02 -0400174 // Not an indexed call, set the range to [first, first + count - 1]
Geoff Lang7c82bc42015-03-09 16:18:08 -0400175 indexRange.start = first;
Shao80957d92017-02-20 21:25:59 +0800176 indexRange.end = first + count - 1;
Geoff Langba4c4a82015-02-24 12:38:46 -0500177 }
178
Jamie Madill0b9e9032015-08-17 11:51:52 +0000179 if (attributesNeedStreaming)
Geoff Langba4c4a82015-02-24 12:38:46 -0500180 {
Jamie Madill4928b7c2017-06-20 12:57:39 -0400181 ANGLE_TRY(streamAttributes(activeAttributesMask, instanceCount, indexRange));
Geoff Lang7c82bc42015-03-09 16:18:08 -0400182 }
183
Yuly Novikovc4d18aa2017-03-09 18:45:02 -0500184 return gl::NoError();
Geoff Lang7c82bc42015-03-09 16:18:08 -0400185}
186
Jamie Madill4928b7c2017-06-20 12:57:39 -0400187gl::Error VertexArrayGL::syncIndexData(const gl::Context *context,
188 GLsizei count,
Jiajia Qind9671222016-11-29 16:30:31 +0800189 GLenum type,
Jamie Madill876429b2017-04-20 15:46:24 -0400190 const void *indices,
Jiajia Qind9671222016-11-29 16:30:31 +0800191 bool primitiveRestartEnabled,
192 bool attributesNeedStreaming,
193 IndexRange *outIndexRange,
Jamie Madill876429b2017-04-20 15:46:24 -0400194 const void **outIndices) const
Geoff Lang7c82bc42015-03-09 16:18:08 -0400195{
196 ASSERT(outIndices);
197
Jamie Madill492f58e2017-10-09 19:41:33 -0400198 gl::Buffer *elementArrayBuffer = mState.getElementArrayBuffer().get();
Jamie Madill8e344942015-07-09 14:22:07 -0400199
Geoff Lang7c82bc42015-03-09 16:18:08 -0400200 // Need to check the range of indices if attributes need to be streamed
Jamie Madill8e344942015-07-09 14:22:07 -0400201 if (elementArrayBuffer != nullptr)
Geoff Lang7c82bc42015-03-09 16:18:08 -0400202 {
Jamie Madill77a90c22015-08-11 16:33:17 -0400203 if (elementArrayBuffer != mAppliedElementArrayBuffer.get())
Geoff Lang7c82bc42015-03-09 16:18:08 -0400204 {
Jamie Madill77a90c22015-08-11 16:33:17 -0400205 const BufferGL *bufferGL = GetImplAs<BufferGL>(elementArrayBuffer);
Corentin Wallez336129f2017-10-17 15:55:40 -0400206 mStateManager->bindBuffer(gl::BufferBinding::ElementArray, bufferGL->getBufferID());
Jamie Madill4928b7c2017-06-20 12:57:39 -0400207 mAppliedElementArrayBuffer.set(context, elementArrayBuffer);
Geoff Lang7c82bc42015-03-09 16:18:08 -0400208 }
209
210 // Only compute the index range if the attributes also need to be streamed
211 if (attributesNeedStreaming)
212 {
213 ptrdiff_t elementArrayBufferOffset = reinterpret_cast<ptrdiff_t>(indices);
Jamie Madill492f58e2017-10-09 19:41:33 -0400214 Error error = mState.getElementArrayBuffer()->getIndexRange(
Jamie Madill33510102017-09-20 10:39:18 -0400215 context, type, elementArrayBufferOffset, count, primitiveRestartEnabled,
216 outIndexRange);
Geoff Lang520c4ae2015-05-05 13:12:36 -0400217 if (error.isError())
Geoff Lang7c82bc42015-03-09 16:18:08 -0400218 {
Geoff Lang520c4ae2015-05-05 13:12:36 -0400219 return error;
Geoff Lang7c82bc42015-03-09 16:18:08 -0400220 }
221 }
222
Shao80957d92017-02-20 21:25:59 +0800223 // Indices serves as an offset into the index buffer in this case, use the same value for
224 // the draw call
Geoff Lang7c82bc42015-03-09 16:18:08 -0400225 *outIndices = indices;
226 }
227 else
228 {
229 // Need to stream the index buffer
230 // TODO: if GLES, nothing needs to be streamed
231
232 // Only compute the index range if the attributes also need to be streamed
233 if (attributesNeedStreaming)
234 {
Geoff Lang3edfe032015-09-04 16:38:24 -0400235 *outIndexRange = ComputeIndexRange(type, indices, count, primitiveRestartEnabled);
Geoff Lang7c82bc42015-03-09 16:18:08 -0400236 }
237
238 // Allocate the streaming element array buffer
239 if (mStreamingElementArrayBuffer == 0)
240 {
241 mFunctions->genBuffers(1, &mStreamingElementArrayBuffer);
242 mStreamingElementArrayBufferSize = 0;
243 }
244
Corentin Wallez336129f2017-10-17 15:55:40 -0400245 mStateManager->bindBuffer(gl::BufferBinding::ElementArray, mStreamingElementArrayBuffer);
Jamie Madill4928b7c2017-06-20 12:57:39 -0400246 mAppliedElementArrayBuffer.set(context, nullptr);
Geoff Lang7c82bc42015-03-09 16:18:08 -0400247
248 // Make sure the element array buffer is large enough
Jamie Madill0b9e9032015-08-17 11:51:52 +0000249 const Type &indexTypeInfo = GetTypeInfo(type);
Geoff Lang7c82bc42015-03-09 16:18:08 -0400250 size_t requiredStreamingBufferSize = indexTypeInfo.bytes * count;
251 if (requiredStreamingBufferSize > mStreamingElementArrayBufferSize)
252 {
253 // Copy the indices in while resizing the buffer
Shao80957d92017-02-20 21:25:59 +0800254 mFunctions->bufferData(GL_ELEMENT_ARRAY_BUFFER, requiredStreamingBufferSize, indices,
255 GL_DYNAMIC_DRAW);
Geoff Lang7c82bc42015-03-09 16:18:08 -0400256 mStreamingElementArrayBufferSize = requiredStreamingBufferSize;
257 }
258 else
259 {
260 // Put the indices at the beginning of the buffer
Shao80957d92017-02-20 21:25:59 +0800261 mFunctions->bufferSubData(GL_ELEMENT_ARRAY_BUFFER, 0, requiredStreamingBufferSize,
262 indices);
Geoff Lang7c82bc42015-03-09 16:18:08 -0400263 }
264
Shao80957d92017-02-20 21:25:59 +0800265 // Set the index offset for the draw call to zero since the supplied index pointer is to
266 // client data
Geoff Lang7c82bc42015-03-09 16:18:08 -0400267 *outIndices = nullptr;
268 }
269
Yuly Novikovc4d18aa2017-03-09 18:45:02 -0500270 return gl::NoError();
Geoff Lang7c82bc42015-03-09 16:18:08 -0400271}
272
Jamie Madill0b9e9032015-08-17 11:51:52 +0000273void VertexArrayGL::computeStreamingAttributeSizes(const gl::AttributesMask &activeAttributesMask,
Geoff Lang3cf12ce2015-08-27 14:40:48 -0400274 GLsizei instanceCount,
Geoff Lang3edfe032015-09-04 16:38:24 -0400275 const gl::IndexRange &indexRange,
Jamie Madill0b9e9032015-08-17 11:51:52 +0000276 size_t *outStreamingDataSize,
277 size_t *outMaxAttributeDataSize) const
Geoff Lang7c82bc42015-03-09 16:18:08 -0400278{
Jamie Madill0b9e9032015-08-17 11:51:52 +0000279 *outStreamingDataSize = 0;
280 *outMaxAttributeDataSize = 0;
281
282 ASSERT(mAttributesNeedStreaming.any());
283
Jamie Madill492f58e2017-10-09 19:41:33 -0400284 const auto &attribs = mState.getVertexAttributes();
285 const auto &bindings = mState.getVertexBindings();
Jamie Madill6de51852017-04-12 09:53:01 -0400286
287 gl::AttributesMask attribsToStream = (mAttributesNeedStreaming & activeAttributesMask);
288
289 for (auto idx : attribsToStream)
Jamie Madill0b9e9032015-08-17 11:51:52 +0000290 {
Shao80957d92017-02-20 21:25:59 +0800291 const auto &attrib = attribs[idx];
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800292 const auto &binding = bindings[attrib.bindingIndex];
293 ASSERT(AttributeNeedsStreaming(attrib, binding));
Jamie Madill0b9e9032015-08-17 11:51:52 +0000294
Jamie Madill0b9e9032015-08-17 11:51:52 +0000295 // If streaming is going to be required, compute the size of the required buffer
296 // and how much slack space at the beginning of the buffer will be required by determining
297 // the attribute with the largest data size.
298 size_t typeSize = ComputeVertexAttributeTypeSize(attrib);
Martin Radev553590a2017-07-31 16:40:39 +0300299 GLuint adjustedDivisor = GetAdjustedDivisor(mAppliedNumViews, binding.getDivisor());
300 *outStreamingDataSize +=
301 typeSize * ComputeVertexBindingElementCount(adjustedDivisor, indexRange.vertexCount(),
302 instanceCount);
Jamie Madill0b9e9032015-08-17 11:51:52 +0000303 *outMaxAttributeDataSize = std::max(*outMaxAttributeDataSize, typeSize);
304 }
305}
306
307gl::Error VertexArrayGL::streamAttributes(const gl::AttributesMask &activeAttributesMask,
Geoff Lang3cf12ce2015-08-27 14:40:48 -0400308 GLsizei instanceCount,
Geoff Lang3edfe032015-09-04 16:38:24 -0400309 const gl::IndexRange &indexRange) const
Jamie Madill0b9e9032015-08-17 11:51:52 +0000310{
311 // Sync the vertex attribute state and track what data needs to be streamed
312 size_t streamingDataSize = 0;
313 size_t maxAttributeDataSize = 0;
314
Geoff Lang3cf12ce2015-08-27 14:40:48 -0400315 computeStreamingAttributeSizes(activeAttributesMask, instanceCount, indexRange,
316 &streamingDataSize, &maxAttributeDataSize);
Jamie Madill0b9e9032015-08-17 11:51:52 +0000317
318 if (streamingDataSize == 0)
319 {
He Yunchaoacd18982017-01-04 10:46:42 +0800320 return gl::NoError();
Jamie Madill0b9e9032015-08-17 11:51:52 +0000321 }
322
Geoff Lang7c82bc42015-03-09 16:18:08 -0400323 if (mStreamingArrayBuffer == 0)
324 {
325 mFunctions->genBuffers(1, &mStreamingArrayBuffer);
326 mStreamingArrayBufferSize = 0;
327 }
328
Shao80957d92017-02-20 21:25:59 +0800329 // If first is greater than zero, a slack space needs to be left at the beginning of the buffer
330 // so that the same 'first' argument can be passed into the draw call.
331 const size_t bufferEmptySpace = maxAttributeDataSize * indexRange.start;
Geoff Lang7c82bc42015-03-09 16:18:08 -0400332 const size_t requiredBufferSize = streamingDataSize + bufferEmptySpace;
333
Corentin Wallez336129f2017-10-17 15:55:40 -0400334 mStateManager->bindBuffer(gl::BufferBinding::Array, mStreamingArrayBuffer);
Geoff Lang7c82bc42015-03-09 16:18:08 -0400335 if (requiredBufferSize > mStreamingArrayBufferSize)
336 {
337 mFunctions->bufferData(GL_ARRAY_BUFFER, requiredBufferSize, nullptr, GL_DYNAMIC_DRAW);
338 mStreamingArrayBufferSize = requiredBufferSize;
339 }
340
341 // Unmapping a buffer can return GL_FALSE to indicate that the system has corrupted the data
Shao80957d92017-02-20 21:25:59 +0800342 // somehow (such as by a screen change), retry writing the data a few times and return
343 // OUT_OF_MEMORY if that fails.
344 GLboolean unmapResult = GL_FALSE;
Geoff Lang7c82bc42015-03-09 16:18:08 -0400345 size_t unmapRetryAttempts = 5;
346 while (unmapResult != GL_TRUE && --unmapRetryAttempts > 0)
347 {
Geoff Langb2ebb5e2016-06-08 18:17:55 +0000348 uint8_t *bufferPointer = MapBufferRangeWithFallback(mFunctions, GL_ARRAY_BUFFER, 0,
349 requiredBufferSize, GL_MAP_WRITE_BIT);
Geoff Lang7c82bc42015-03-09 16:18:08 -0400350 size_t curBufferOffset = bufferEmptySpace;
351
Jamie Madill492f58e2017-10-09 19:41:33 -0400352 const auto &attribs = mState.getVertexAttributes();
353 const auto &bindings = mState.getVertexBindings();
Jamie Madill6de51852017-04-12 09:53:01 -0400354
355 gl::AttributesMask attribsToStream = (mAttributesNeedStreaming & activeAttributesMask);
356
357 for (auto idx : attribsToStream)
Geoff Lang7c82bc42015-03-09 16:18:08 -0400358 {
Shao80957d92017-02-20 21:25:59 +0800359 const auto &attrib = attribs[idx];
Shaodde78e82017-05-22 14:13:27 +0800360 ASSERT(IsVertexAttribPointerSupported(idx, attrib));
361
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800362 const auto &binding = bindings[attrib.bindingIndex];
363 ASSERT(AttributeNeedsStreaming(attrib, binding));
Geoff Lang7c82bc42015-03-09 16:18:08 -0400364
Martin Radev553590a2017-07-31 16:40:39 +0300365 GLuint adjustedDivisor = GetAdjustedDivisor(mAppliedNumViews, binding.getDivisor());
366 const size_t streamedVertexCount = ComputeVertexBindingElementCount(
367 adjustedDivisor, indexRange.vertexCount(), instanceCount);
Geoff Lang3cf12ce2015-08-27 14:40:48 -0400368
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800369 const size_t sourceStride = ComputeVertexAttributeStride(attrib, binding);
Jamie Madill0b9e9032015-08-17 11:51:52 +0000370 const size_t destStride = ComputeVertexAttributeTypeSize(attrib);
371
Geoff Lang38a24a92017-02-15 13:53:06 -0500372 // Vertices do not apply the 'start' offset when the divisor is non-zero even when doing
373 // a non-instanced draw call
Martin Radev553590a2017-07-31 16:40:39 +0300374 const size_t firstIndex = adjustedDivisor == 0 ? indexRange.start : 0;
Geoff Lang38a24a92017-02-15 13:53:06 -0500375
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800376 // Attributes using client memory ignore the VERTEX_ATTRIB_BINDING state.
377 // https://www.opengl.org/registry/specs/ARB/vertex_attrib_binding.txt
Jamie Madill0b9e9032015-08-17 11:51:52 +0000378 const uint8_t *inputPointer = reinterpret_cast<const uint8_t *>(attrib.pointer);
379
380 // Pack the data when copying it, user could have supplied a very large stride that
381 // would cause the buffer to be much larger than needed.
382 if (destStride == sourceStride)
Jamie Madill8e344942015-07-09 14:22:07 -0400383 {
Jamie Madill0b9e9032015-08-17 11:51:52 +0000384 // Can copy in one go, the data is packed
Geoff Lang38a24a92017-02-15 13:53:06 -0500385 memcpy(bufferPointer + curBufferOffset, inputPointer + (sourceStride * firstIndex),
Jamie Madill0b9e9032015-08-17 11:51:52 +0000386 destStride * streamedVertexCount);
Jamie Madill6d51c702015-08-14 10:38:10 -0400387 }
Jamie Madill0b9e9032015-08-17 11:51:52 +0000388 else
389 {
390 // Copy each vertex individually
Geoff Lang6b10ddb2015-09-02 15:55:10 -0400391 for (size_t vertexIdx = 0; vertexIdx < streamedVertexCount; vertexIdx++)
Jamie Madill0b9e9032015-08-17 11:51:52 +0000392 {
Shao80957d92017-02-20 21:25:59 +0800393 uint8_t *out = bufferPointer + curBufferOffset + (destStride * vertexIdx);
Geoff Lang38a24a92017-02-15 13:53:06 -0500394 const uint8_t *in = inputPointer + sourceStride * (vertexIdx + firstIndex);
Corentin Wallez4d5362d2015-08-25 11:25:14 -0400395 memcpy(out, in, destStride);
Jamie Madill0b9e9032015-08-17 11:51:52 +0000396 }
397 }
398
399 // Compute where the 0-index vertex would be.
Geoff Lang38a24a92017-02-15 13:53:06 -0500400 const size_t vertexStartOffset = curBufferOffset - (firstIndex * destStride);
Jamie Madill0b9e9032015-08-17 11:51:52 +0000401
Shaodf682a82017-03-31 15:13:21 +0800402 callVertexAttribPointer(static_cast<GLuint>(idx), attrib,
403 static_cast<GLsizei>(destStride),
404 static_cast<GLintptr>(vertexStartOffset));
Jamie Madill0b9e9032015-08-17 11:51:52 +0000405
406 curBufferOffset += destStride * streamedVertexCount;
Geoff Lang7c82bc42015-03-09 16:18:08 -0400407 }
408
409 unmapResult = mFunctions->unmapBuffer(GL_ARRAY_BUFFER);
410 }
411
412 if (unmapResult != GL_TRUE)
413 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -0500414 return gl::OutOfMemory() << "Failed to unmap the client data streaming buffer.";
Geoff Lang7c82bc42015-03-09 16:18:08 -0400415 }
416
Yuly Novikovc4d18aa2017-03-09 18:45:02 -0500417 return gl::NoError();
Geoff Langba4c4a82015-02-24 12:38:46 -0500418}
419
420GLuint VertexArrayGL::getVertexArrayID() const
421{
422 return mVertexArrayID;
Geoff Langf9a6f082015-01-22 13:32:49 -0500423}
424
Geoff Lang294cad92015-05-26 15:11:23 -0400425GLuint VertexArrayGL::getAppliedElementArrayBufferID() const
426{
Jamie Madill77a90c22015-08-11 16:33:17 -0400427 if (mAppliedElementArrayBuffer.get() == nullptr)
428 {
429 return mStreamingElementArrayBuffer;
430 }
431
432 return GetImplAs<BufferGL>(mAppliedElementArrayBuffer.get())->getBufferID();
Geoff Lang294cad92015-05-26 15:11:23 -0400433}
434
Jamie Madill0b9e9032015-08-17 11:51:52 +0000435void VertexArrayGL::updateNeedsStreaming(size_t attribIndex)
436{
Jamie Madill492f58e2017-10-09 19:41:33 -0400437 const auto &attrib = mState.getVertexAttribute(attribIndex);
438 const auto &binding = mState.getBindingFromAttribIndex(attribIndex);
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800439 mAttributesNeedStreaming.set(attribIndex, AttributeNeedsStreaming(attrib, binding));
Geoff Langf9a6f082015-01-22 13:32:49 -0500440}
Jamie Madill0b9e9032015-08-17 11:51:52 +0000441
442void VertexArrayGL::updateAttribEnabled(size_t attribIndex)
443{
Jamie Madill492f58e2017-10-09 19:41:33 -0400444 const bool enabled = mState.getVertexAttribute(attribIndex).enabled;
Shaodf682a82017-03-31 15:13:21 +0800445 if (mAppliedAttributes[attribIndex].enabled == enabled)
Jamie Madill0b9e9032015-08-17 11:51:52 +0000446 {
447 return;
448 }
449
450 updateNeedsStreaming(attribIndex);
451
Shaodf682a82017-03-31 15:13:21 +0800452 if (enabled)
Jamie Madill0b9e9032015-08-17 11:51:52 +0000453 {
454 mFunctions->enableVertexAttribArray(static_cast<GLuint>(attribIndex));
455 }
456 else
457 {
458 mFunctions->disableVertexAttribArray(static_cast<GLuint>(attribIndex));
459 }
Shaodf682a82017-03-31 15:13:21 +0800460
461 mAppliedAttributes[attribIndex].enabled = enabled;
Jamie Madill0b9e9032015-08-17 11:51:52 +0000462}
463
Jamie Madill4928b7c2017-06-20 12:57:39 -0400464void VertexArrayGL::updateAttribPointer(const gl::Context *context, size_t attribIndex)
Jamie Madill0b9e9032015-08-17 11:51:52 +0000465{
Jamie Madill492f58e2017-10-09 19:41:33 -0400466 const VertexAttribute &attrib = mState.getVertexAttribute(attribIndex);
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800467
Shaodde78e82017-05-22 14:13:27 +0800468 // According to SPEC, VertexAttribPointer should update the binding indexed attribIndex instead
469 // of the binding indexed attrib.bindingIndex (unless attribIndex == attrib.bindingIndex).
Jamie Madill492f58e2017-10-09 19:41:33 -0400470 const VertexBinding &binding = mState.getVertexBinding(attribIndex);
Shaodf682a82017-03-31 15:13:21 +0800471
Shaodde78e82017-05-22 14:13:27 +0800472 // Since mAttributesNeedStreaming[attribIndex] keeps the value set in the last draw, here we
473 // only need to update it when the buffer has been changed. e.g. When we set an attribute to be
474 // streamed in the last draw, and only change its format in this draw without calling
475 // updateNeedsStreaming, it will still be streamed because the flag is already on.
476 const auto &bindingBuffer = binding.getBuffer();
477 if (bindingBuffer != mAppliedBindings[attribIndex].getBuffer())
478 {
479 updateNeedsStreaming(attribIndex);
480 }
481
482 // Early return when the vertex attribute isn't using a buffer object:
483 // - If we need to stream, defer the attribPointer to the draw call.
484 // - Skip the attribute that is disabled and uses a client memory pointer.
485 // - Skip the attribute whose buffer is detached by BindVertexBuffer. Since it cannot have a
486 // client memory pointer either, it must be disabled and shouldn't affect the draw.
487 const Buffer *arrayBuffer = bindingBuffer.get();
488 if (arrayBuffer == nullptr)
489 {
490 // Mark the applied binding isn't using a buffer by setting its buffer to nullptr so that if
491 // it starts to use a buffer later, there is no chance that the caching will skip it.
492 mAppliedBindings[attribIndex].setBuffer(context, nullptr);
493 return;
494 }
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800495
Shaodf682a82017-03-31 15:13:21 +0800496 // We do not need to compare attrib.pointer because when we use a different client memory
497 // pointer, we don't need to update mAttributesNeedStreaming by binding.buffer and we won't
498 // update attribPointer in this function.
Shaodde78e82017-05-22 14:13:27 +0800499 if ((SameVertexAttribFormat(mAppliedAttributes[attribIndex], attrib)) &&
500 (mAppliedAttributes[attribIndex].bindingIndex == attrib.bindingIndex) &&
501 (SameVertexBuffer(mAppliedBindings[attribIndex], binding)))
Jamie Madill0b9e9032015-08-17 11:51:52 +0000502 {
503 return;
504 }
505
Shao4181bc92017-03-17 14:55:25 +0800506 // Since ANGLE always uses a non-zero VAO, we cannot use a client memory pointer on it:
507 // [OpenGL ES 3.0.2] Section 2.8 page 24:
508 // An INVALID_OPERATION error is generated when a non-zero vertex array object is bound,
509 // zero is bound to the ARRAY_BUFFER buffer object binding point, and the pointer argument
510 // is not NULL.
Shaodf682a82017-03-31 15:13:21 +0800511
Shao4181bc92017-03-17 14:55:25 +0800512 const BufferGL *arrayBufferGL = GetImplAs<BufferGL>(arrayBuffer);
Corentin Wallez336129f2017-10-17 15:55:40 -0400513 mStateManager->bindBuffer(gl::BufferBinding::Array, arrayBufferGL->getBufferID());
Martin Radevdd5f27e2017-06-07 10:17:09 +0300514 callVertexAttribPointer(static_cast<GLuint>(attribIndex), attrib, binding.getStride(),
515 binding.getOffset());
Shaodf682a82017-03-31 15:13:21 +0800516
Shaodde78e82017-05-22 14:13:27 +0800517 mAppliedAttributes[attribIndex].size = attrib.size;
518 mAppliedAttributes[attribIndex].type = attrib.type;
519 mAppliedAttributes[attribIndex].normalized = attrib.normalized;
520 mAppliedAttributes[attribIndex].pureInteger = attrib.pureInteger;
Shaodf682a82017-03-31 15:13:21 +0800521
Shaodde78e82017-05-22 14:13:27 +0800522 // After VertexAttribPointer, attrib.relativeOffset is set to 0 and attrib.bindingIndex is set
523 // to attribIndex in driver. If attrib.relativeOffset != 0 or attrib.bindingIndex !=
524 // attribIndex, they should be set in updateAttribFormat and updateAttribBinding. The cache
525 // should be consistent with driver so that we won't miss anything.
526 mAppliedAttributes[attribIndex].relativeOffset = 0;
527 mAppliedAttributes[attribIndex].bindingIndex = static_cast<GLuint>(attribIndex);
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800528
Shaodde78e82017-05-22 14:13:27 +0800529 mAppliedBindings[attribIndex].setStride(binding.getStride());
530 mAppliedBindings[attribIndex].setOffset(binding.getOffset());
531 mAppliedBindings[attribIndex].setBuffer(context, binding.getBuffer().get());
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800532}
533
Shaodf682a82017-03-31 15:13:21 +0800534void VertexArrayGL::callVertexAttribPointer(GLuint attribIndex,
535 const VertexAttribute &attrib,
536 GLsizei stride,
537 GLintptr offset) const
538{
539 const GLvoid *pointer = reinterpret_cast<const GLvoid *>(offset);
540 if (attrib.pureInteger)
541 {
542 ASSERT(!attrib.normalized);
543 mFunctions->vertexAttribIPointer(attribIndex, attrib.size, attrib.type, stride, pointer);
544 }
545 else
546 {
547 mFunctions->vertexAttribPointer(attribIndex, attrib.size, attrib.type, attrib.normalized,
548 stride, pointer);
549 }
550}
551
Shaodde78e82017-05-22 14:13:27 +0800552bool VertexArrayGL::supportVertexAttribBinding() const
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800553{
Shaodde78e82017-05-22 14:13:27 +0800554 ASSERT(mFunctions);
555 return (mFunctions->vertexAttribBinding != nullptr);
556}
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800557
Shaodde78e82017-05-22 14:13:27 +0800558void VertexArrayGL::updateAttribFormat(size_t attribIndex)
559{
560 ASSERT(supportVertexAttribBinding());
561
Jamie Madill492f58e2017-10-09 19:41:33 -0400562 const VertexAttribute &attrib = mState.getVertexAttribute(attribIndex);
Shaodde78e82017-05-22 14:13:27 +0800563 if (SameVertexAttribFormat(mAppliedAttributes[attribIndex], attrib))
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800564 {
Shaodf682a82017-03-31 15:13:21 +0800565 return;
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800566 }
Shaodf682a82017-03-31 15:13:21 +0800567
Shaodde78e82017-05-22 14:13:27 +0800568 if (attrib.pureInteger)
569 {
570 ASSERT(!attrib.normalized);
571 mFunctions->vertexAttribIFormat(static_cast<GLuint>(attribIndex), attrib.size, attrib.type,
572 attrib.relativeOffset);
573 }
574 else
575 {
576 mFunctions->vertexAttribFormat(static_cast<GLuint>(attribIndex), attrib.size, attrib.type,
577 attrib.normalized, attrib.relativeOffset);
578 }
579
580 mAppliedAttributes[attribIndex].size = attrib.size;
581 mAppliedAttributes[attribIndex].type = attrib.type;
582 mAppliedAttributes[attribIndex].normalized = attrib.normalized;
583 mAppliedAttributes[attribIndex].pureInteger = attrib.pureInteger;
584 mAppliedAttributes[attribIndex].relativeOffset = attrib.relativeOffset;
585}
586
587void VertexArrayGL::updateAttribBinding(size_t attribIndex)
588{
589 ASSERT(supportVertexAttribBinding());
590
Jamie Madill492f58e2017-10-09 19:41:33 -0400591 GLuint bindingIndex = mState.getVertexAttribute(attribIndex).bindingIndex;
Shaodde78e82017-05-22 14:13:27 +0800592 if (mAppliedAttributes[attribIndex].bindingIndex == bindingIndex)
593 {
594 return;
595 }
596
597 mFunctions->vertexAttribBinding(static_cast<GLuint>(attribIndex), bindingIndex);
Shaodf682a82017-03-31 15:13:21 +0800598
599 mAppliedAttributes[attribIndex].bindingIndex = bindingIndex;
Shaodde78e82017-05-22 14:13:27 +0800600}
601
602void VertexArrayGL::updateBindingBuffer(const gl::Context *context, size_t bindingIndex)
603{
604 ASSERT(supportVertexAttribBinding());
605
Jamie Madill492f58e2017-10-09 19:41:33 -0400606 const VertexBinding &binding = mState.getVertexBinding(bindingIndex);
Shaodde78e82017-05-22 14:13:27 +0800607 if (SameVertexBuffer(mAppliedBindings[bindingIndex], binding))
608 {
609 return;
610 }
611
612 const Buffer *arrayBuffer = binding.getBuffer().get();
613 GLuint bufferId = 0;
614 if (arrayBuffer != nullptr)
615 {
616 bufferId = GetImplAs<BufferGL>(arrayBuffer)->getBufferID();
617 }
618
619 mFunctions->bindVertexBuffer(static_cast<GLuint>(bindingIndex), bufferId, binding.getOffset(),
620 binding.getStride());
621
622 mAppliedBindings[bindingIndex].setStride(binding.getStride());
623 mAppliedBindings[bindingIndex].setOffset(binding.getOffset());
624 mAppliedBindings[bindingIndex].setBuffer(context, binding.getBuffer().get());
625}
626
627void VertexArrayGL::updateBindingDivisor(size_t bindingIndex)
628{
Martin Radev553590a2017-07-31 16:40:39 +0300629 GLuint adjustedDivisor =
Jamie Madill492f58e2017-10-09 19:41:33 -0400630 GetAdjustedDivisor(mAppliedNumViews, mState.getVertexBinding(bindingIndex).getDivisor());
Martin Radev553590a2017-07-31 16:40:39 +0300631 if (mAppliedBindings[bindingIndex].getDivisor() == adjustedDivisor)
Shaodde78e82017-05-22 14:13:27 +0800632 {
633 return;
634 }
635
636 if (supportVertexAttribBinding())
637 {
Martin Radev553590a2017-07-31 16:40:39 +0300638 mFunctions->vertexBindingDivisor(static_cast<GLuint>(bindingIndex), adjustedDivisor);
Shaodde78e82017-05-22 14:13:27 +0800639 }
640 else
641 {
642 // We can only use VertexAttribDivisor on platforms that don't support Vertex Attrib
643 // Binding.
Martin Radev553590a2017-07-31 16:40:39 +0300644 mFunctions->vertexAttribDivisor(static_cast<GLuint>(bindingIndex), adjustedDivisor);
Shaodde78e82017-05-22 14:13:27 +0800645 }
646
Martin Radev553590a2017-07-31 16:40:39 +0300647 mAppliedBindings[bindingIndex].setDivisor(adjustedDivisor);
Jamie Madill0b9e9032015-08-17 11:51:52 +0000648}
649
Jamie Madillc564c072017-06-01 12:45:42 -0400650void VertexArrayGL::syncState(const gl::Context *context, const VertexArray::DirtyBits &dirtyBits)
Jamie Madill0b9e9032015-08-17 11:51:52 +0000651{
Shaodf682a82017-03-31 15:13:21 +0800652 mStateManager->bindVertexArray(mVertexArrayID, getAppliedElementArrayBufferID());
653
Jamie Madill6de51852017-04-12 09:53:01 -0400654 for (size_t dirtyBit : dirtyBits)
Jamie Madill0b9e9032015-08-17 11:51:52 +0000655 {
656 if (dirtyBit == VertexArray::DIRTY_BIT_ELEMENT_ARRAY_BUFFER)
657 {
658 // TODO(jmadill): Element array buffer bindings
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800659 continue;
Jamie Madill0b9e9032015-08-17 11:51:52 +0000660 }
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800661
Shaodde78e82017-05-22 14:13:27 +0800662 size_t index = VertexArray::GetVertexIndexFromDirtyBit(dirtyBit);
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800663 if (dirtyBit >= VertexArray::DIRTY_BIT_ATTRIB_0_ENABLED &&
664 dirtyBit < VertexArray::DIRTY_BIT_ATTRIB_MAX_ENABLED)
Jamie Madill0b9e9032015-08-17 11:51:52 +0000665 {
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800666 updateAttribEnabled(index);
Jamie Madill0b9e9032015-08-17 11:51:52 +0000667 }
668 else if (dirtyBit >= VertexArray::DIRTY_BIT_ATTRIB_0_POINTER &&
669 dirtyBit < VertexArray::DIRTY_BIT_ATTRIB_MAX_POINTER)
670 {
Jamie Madill4928b7c2017-06-20 12:57:39 -0400671 updateAttribPointer(context, index);
Jamie Madill0b9e9032015-08-17 11:51:52 +0000672 }
Shaodde78e82017-05-22 14:13:27 +0800673
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800674 else if (dirtyBit >= VertexArray::DIRTY_BIT_ATTRIB_0_FORMAT &&
Shaodde78e82017-05-22 14:13:27 +0800675 dirtyBit < VertexArray::DIRTY_BIT_ATTRIB_MAX_FORMAT)
676 {
677 ASSERT(supportVertexAttribBinding());
678 updateAttribFormat(index);
679 }
680 else if (dirtyBit >= VertexArray::DIRTY_BIT_ATTRIB_0_BINDING &&
681 dirtyBit < VertexArray::DIRTY_BIT_ATTRIB_MAX_BINDING)
682 {
683 ASSERT(supportVertexAttribBinding());
684 updateAttribBinding(index);
685 }
686 else if (dirtyBit >= VertexArray::DIRTY_BIT_BINDING_0_BUFFER &&
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800687 dirtyBit < VertexArray::DIRTY_BIT_BINDING_MAX_BUFFER)
Jamie Madill0b9e9032015-08-17 11:51:52 +0000688 {
Shaodde78e82017-05-22 14:13:27 +0800689 ASSERT(supportVertexAttribBinding());
690 updateBindingBuffer(context, index);
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800691 }
Shaodde78e82017-05-22 14:13:27 +0800692
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800693 else if (dirtyBit >= VertexArray::DIRTY_BIT_BINDING_0_DIVISOR &&
694 dirtyBit < VertexArray::DIRTY_BIT_BINDING_MAX_DIVISOR)
695 {
Shaodde78e82017-05-22 14:13:27 +0800696 updateBindingDivisor(index);
Jamie Madill0b9e9032015-08-17 11:51:52 +0000697 }
698 else
699 UNREACHABLE();
700 }
701}
702
Martin Radev553590a2017-07-31 16:40:39 +0300703void VertexArrayGL::applyNumViewsToDivisor(int numViews)
704{
705 if (numViews != mAppliedNumViews)
706 {
707 mStateManager->bindVertexArray(mVertexArrayID, getAppliedElementArrayBufferID());
708 mAppliedNumViews = numViews;
709 for (size_t index = 0u; index < mAppliedBindings.size(); ++index)
710 {
711 updateBindingDivisor(index);
712 }
713 }
714}
715
Jamie Madill0b9e9032015-08-17 11:51:52 +0000716} // rx