blob: 40b6d7241eac942b9dbc6a4ebc52a84eaaf6ff25 [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
11#include "common/debug.h"
Geoff Lang7c82bc42015-03-09 16:18:08 -040012#include "common/mathutil.h"
Geoff Lang831b1952015-05-05 11:02:27 -040013#include "common/utilities.h"
Geoff Lang6ae6efc2015-03-09 14:42:35 -040014#include "libANGLE/Buffer.h"
Geoff Langba4c4a82015-02-24 12:38:46 -050015#include "libANGLE/angletypes.h"
Geoff Lang7c82bc42015-03-09 16:18:08 -040016#include "libANGLE/formatutils.h"
Geoff Langba4c4a82015-02-24 12:38:46 -050017#include "libANGLE/renderer/gl/BufferGL.h"
18#include "libANGLE/renderer/gl/FunctionsGL.h"
19#include "libANGLE/renderer/gl/StateManagerGL.h"
Geoff Langf9a6f082015-01-22 13:32:49 -050020
21namespace rx
22{
23
Geoff Langba4c4a82015-02-24 12:38:46 -050024VertexArrayGL::VertexArrayGL(const FunctionsGL *functions, StateManagerGL *stateManager)
25 : VertexArrayImpl(),
26 mFunctions(functions),
27 mStateManager(stateManager),
28 mVertexArrayID(0),
Geoff Lang6ae6efc2015-03-09 14:42:35 -040029 mElementArrayBuffer(),
30 mAttributes(),
Geoff Langba4c4a82015-02-24 12:38:46 -050031 mAppliedElementArrayBuffer(0),
Geoff Lang7c82bc42015-03-09 16:18:08 -040032 mAppliedAttributes(),
33 mStreamingElementArrayBufferSize(0),
34 mStreamingElementArrayBuffer(0),
35 mStreamingArrayBufferSize(0),
36 mStreamingArrayBuffer(0)
Geoff Langba4c4a82015-02-24 12:38:46 -050037{
38 ASSERT(mFunctions);
39 ASSERT(mStateManager);
40 mFunctions->genVertexArrays(1, &mVertexArrayID);
41
42 // Set the cached vertex attribute array size
43 GLint maxVertexAttribs;
44 mFunctions->getIntegerv(GL_MAX_VERTEX_ATTRIBS, &maxVertexAttribs);
Geoff Lang6ae6efc2015-03-09 14:42:35 -040045 mAttributes.resize(maxVertexAttribs);
Geoff Langba4c4a82015-02-24 12:38:46 -050046 mAppliedAttributes.resize(maxVertexAttribs);
47}
Geoff Langf9a6f082015-01-22 13:32:49 -050048
49VertexArrayGL::~VertexArrayGL()
Geoff Langba4c4a82015-02-24 12:38:46 -050050{
Geoff Lang1eb708e2015-05-04 14:58:23 -040051 mStateManager->deleteVertexArray(mVertexArrayID);
52 mVertexArrayID = 0;
Geoff Langba4c4a82015-02-24 12:38:46 -050053
Geoff Lang1eb708e2015-05-04 14:58:23 -040054 mStateManager->deleteBuffer(mStreamingElementArrayBuffer);
55 mStreamingElementArrayBufferSize = 0;
56 mStreamingElementArrayBuffer = 0;
Geoff Lang7c82bc42015-03-09 16:18:08 -040057
Geoff Lang1eb708e2015-05-04 14:58:23 -040058 mStateManager->deleteBuffer(mStreamingArrayBuffer);
59 mStreamingArrayBufferSize = 0;
60 mStreamingArrayBuffer = 0;
Geoff Lang7c82bc42015-03-09 16:18:08 -040061
Geoff Lang6ae6efc2015-03-09 14:42:35 -040062 mElementArrayBuffer.set(nullptr);
Geoff Lang7c82bc42015-03-09 16:18:08 -040063 for (size_t idx = 0; idx < mAttributes.size(); idx++)
64 {
65 mAttributes[idx].buffer.set(NULL);
66 }
67
Geoff Langba4c4a82015-02-24 12:38:46 -050068 for (size_t idx = 0; idx < mAppliedAttributes.size(); idx++)
69 {
70 mAppliedAttributes[idx].buffer.set(NULL);
71 }
72}
Geoff Langf9a6f082015-01-22 13:32:49 -050073
74void VertexArrayGL::setElementArrayBuffer(const gl::Buffer *buffer)
75{
Geoff Lang6ae6efc2015-03-09 14:42:35 -040076 mElementArrayBuffer.set(buffer);
77}
78
79void VertexArrayGL::setAttribute(size_t idx, const gl::VertexAttribute &attr)
80{
81 mAttributes[idx] = attr;
82}
83
84void VertexArrayGL::setAttributeDivisor(size_t idx, GLuint divisor)
85{
86 mAttributes[idx].divisor = divisor;
87}
88
89void VertexArrayGL::enableAttribute(size_t idx, bool enabledState)
90{
91 mAttributes[idx].enabled = enabledState;
92}
93
Geoff Lang7c82bc42015-03-09 16:18:08 -040094gl::Error VertexArrayGL::syncDrawArraysState(GLint first, GLsizei count) const
95{
96 return syncDrawState(first, count, GL_NONE, nullptr, nullptr);
97}
98
99gl::Error VertexArrayGL::syncDrawElementsState(GLsizei count, GLenum type, const GLvoid *indices, const GLvoid **outIndices) const
100{
101 return syncDrawState(0, count, type, indices, outIndices);
102}
103
104gl::Error VertexArrayGL::syncDrawState(GLint first, GLsizei count, GLenum type, const GLvoid *indices, const GLvoid **outIndices) const
Geoff Lang6ae6efc2015-03-09 14:42:35 -0400105{
106 mStateManager->bindVertexArray(mVertexArrayID);
107
Geoff Lang7c82bc42015-03-09 16:18:08 -0400108 // Check if any attributes need to be streamed, determines if the index range needs to be computed
109 bool attributesNeedStreaming = doAttributesNeedStreaming();
110
111 // Determine if an index buffer needs to be streamed and the range of vertices that need to be copied
Geoff Lang831b1952015-05-05 11:02:27 -0400112 gl::RangeUI indexRange(0, 0);
Geoff Lang7c82bc42015-03-09 16:18:08 -0400113 if (type != GL_NONE)
Geoff Langba4c4a82015-02-24 12:38:46 -0500114 {
Geoff Lang7c82bc42015-03-09 16:18:08 -0400115 gl::Error error = syncIndexData(count, type, indices, attributesNeedStreaming, &indexRange, outIndices);
116 if (error.isError())
117 {
118 return error;
119 }
120 }
121 else
122 {
123 // Not an indexed call, set the range to [first, first + count)
124 indexRange.start = first;
125 indexRange.end = first + count;
Geoff Langba4c4a82015-02-24 12:38:46 -0500126 }
127
Geoff Lang7c82bc42015-03-09 16:18:08 -0400128 // Sync the vertex attribute state and track what data needs to be streamed
129 size_t streamingDataSize = 0;
130 size_t maxAttributeDataSize = 0;
131 gl::Error error = syncAttributeState(attributesNeedStreaming, indexRange, &streamingDataSize, &maxAttributeDataSize);
132 if (error.isError())
Geoff Langba4c4a82015-02-24 12:38:46 -0500133 {
Geoff Lang7c82bc42015-03-09 16:18:08 -0400134 return error;
Geoff Langba4c4a82015-02-24 12:38:46 -0500135 }
Geoff Langf9a6f082015-01-22 13:32:49 -0500136
Geoff Lang7c82bc42015-03-09 16:18:08 -0400137 if (streamingDataSize > 0)
138 {
139 ASSERT(attributesNeedStreaming);
140
141 gl::Error error = streamAttributes(streamingDataSize, maxAttributeDataSize, indexRange);
142 if (error.isError())
143 {
144 return error;
145 }
146 }
147
148 return gl::Error(GL_NO_ERROR);
149}
150
151bool VertexArrayGL::doAttributesNeedStreaming() const
152{
153 // TODO: if GLES, nothing needs to be streamed
154 for (size_t idx = 0; idx < mAttributes.size(); idx++)
155 {
156 if (mAttributes[idx].enabled && mAttributes[idx].buffer.get() == nullptr)
157 {
158 return true;
159 }
160 }
161
162 return false;
163}
164
Geoff Lang831b1952015-05-05 11:02:27 -0400165gl::Error VertexArrayGL::syncAttributeState(bool attributesNeedStreaming, const gl::RangeUI &indexRange,
Geoff Lang7c82bc42015-03-09 16:18:08 -0400166 size_t *outStreamingDataSize, size_t *outMaxAttributeDataSize) const
167{
168 *outStreamingDataSize = 0;
169 *outMaxAttributeDataSize = 0;
170
Geoff Lang6ae6efc2015-03-09 14:42:35 -0400171 for (size_t idx = 0; idx < mAttributes.size(); idx++)
Geoff Langba4c4a82015-02-24 12:38:46 -0500172 {
Geoff Lang7c82bc42015-03-09 16:18:08 -0400173 // Always sync the enabled and divisor state, they are required for both streaming and buffered
174 // attributes
175 if (mAppliedAttributes[idx].enabled != mAttributes[idx].enabled)
Geoff Langba4c4a82015-02-24 12:38:46 -0500176 {
Geoff Lang6ae6efc2015-03-09 14:42:35 -0400177 if (mAttributes[idx].enabled)
178 {
179 mFunctions->enableVertexAttribArray(idx);
180 }
181 else
182 {
183 mFunctions->disableVertexAttribArray(idx);
184 }
Geoff Lang7c82bc42015-03-09 16:18:08 -0400185 mAppliedAttributes[idx].enabled = mAttributes[idx].enabled;
186 }
187 if (mAppliedAttributes[idx].divisor != mAttributes[idx].divisor)
188 {
189 mFunctions->vertexAttribDivisor(idx, mAttributes[idx].divisor);
190 mAppliedAttributes[idx].divisor = mAttributes[idx].divisor;
191 }
Geoff Lang6ae6efc2015-03-09 14:42:35 -0400192
Geoff Lang7c82bc42015-03-09 16:18:08 -0400193 if (mAttributes[idx].enabled && mAttributes[idx].buffer.get() == nullptr)
194 {
195 ASSERT(attributesNeedStreaming);
196
197 const size_t streamedVertexCount = indexRange.end - indexRange.start + 1;
198
199 // If streaming is going to be required, compute the size of the required buffer
200 // and how much slack space at the beginning of the buffer will be required by determining
201 // the attribute with the largest data size.
202 size_t typeSize = ComputeVertexAttributeTypeSize(mAttributes[idx]);
203 *outStreamingDataSize += typeSize * streamedVertexCount;
204 *outMaxAttributeDataSize = std::max(*outMaxAttributeDataSize, typeSize);
205 }
206 else
207 {
208 // Sync the attribute with no translation
209 if (mAppliedAttributes[idx] != mAttributes[idx])
Geoff Lang6ae6efc2015-03-09 14:42:35 -0400210 {
Geoff Lang7c82bc42015-03-09 16:18:08 -0400211 const gl::Buffer *arrayBuffer = mAttributes[idx].buffer.get();
Geoff Lang6ae6efc2015-03-09 14:42:35 -0400212 const BufferGL *arrayBufferGL = GetImplAs<BufferGL>(arrayBuffer);
213 mStateManager->bindBuffer(GL_ARRAY_BUFFER, arrayBufferGL->getBufferID());
Geoff Lang6ae6efc2015-03-09 14:42:35 -0400214
Geoff Lang7c82bc42015-03-09 16:18:08 -0400215 if (mAttributes[idx].pureInteger)
216 {
217 mFunctions->vertexAttribIPointer(idx, mAttributes[idx].size, mAttributes[idx].type,
218 mAttributes[idx].stride, mAttributes[idx].pointer);
219 }
220 else
221 {
222 mFunctions->vertexAttribPointer(idx, mAttributes[idx].size, mAttributes[idx].type,
223 mAttributes[idx].normalized, mAttributes[idx].stride,
224 mAttributes[idx].pointer);
225 }
Geoff Lang6ae6efc2015-03-09 14:42:35 -0400226
Geoff Lang7c82bc42015-03-09 16:18:08 -0400227 mAppliedAttributes[idx] = mAttributes[idx];
228 }
Geoff Langba4c4a82015-02-24 12:38:46 -0500229 }
Geoff Langba4c4a82015-02-24 12:38:46 -0500230 }
Geoff Lang7c82bc42015-03-09 16:18:08 -0400231
232 return gl::Error(GL_NO_ERROR);
233}
234
235gl::Error VertexArrayGL::syncIndexData(GLsizei count, GLenum type, const GLvoid *indices, bool attributesNeedStreaming,
Geoff Lang831b1952015-05-05 11:02:27 -0400236 gl::RangeUI *outIndexRange, const GLvoid **outIndices) const
Geoff Lang7c82bc42015-03-09 16:18:08 -0400237{
238 ASSERT(outIndices);
239
240 // Need to check the range of indices if attributes need to be streamed
241 if (mElementArrayBuffer.get() != nullptr)
242 {
243 const BufferGL *bufferGL = GetImplAs<BufferGL>(mElementArrayBuffer.get());
244 GLuint elementArrayBufferID = bufferGL->getBufferID();
245 if (elementArrayBufferID != mAppliedElementArrayBuffer)
246 {
247 mStateManager->bindBuffer(GL_ELEMENT_ARRAY_BUFFER, elementArrayBufferID);
248 mAppliedElementArrayBuffer = elementArrayBufferID;
249 }
250
251 // Only compute the index range if the attributes also need to be streamed
252 if (attributesNeedStreaming)
253 {
254 ptrdiff_t elementArrayBufferOffset = reinterpret_cast<ptrdiff_t>(indices);
Geoff Lang520c4ae2015-05-05 13:12:36 -0400255 gl::Error error = mElementArrayBuffer->getIndexRange(type, static_cast<size_t>(elementArrayBufferOffset), count, outIndexRange);
256 if (error.isError())
Geoff Lang7c82bc42015-03-09 16:18:08 -0400257 {
Geoff Lang520c4ae2015-05-05 13:12:36 -0400258 return error;
Geoff Lang7c82bc42015-03-09 16:18:08 -0400259 }
260 }
261
262 // Indices serves as an offset into the index buffer in this case, use the same value for the draw call
263 *outIndices = indices;
264 }
265 else
266 {
267 // Need to stream the index buffer
268 // TODO: if GLES, nothing needs to be streamed
269
270 // Only compute the index range if the attributes also need to be streamed
271 if (attributesNeedStreaming)
272 {
Geoff Lang831b1952015-05-05 11:02:27 -0400273 *outIndexRange = gl::ComputeIndexRange(type, indices, count);
Geoff Lang7c82bc42015-03-09 16:18:08 -0400274 }
275
276 // Allocate the streaming element array buffer
277 if (mStreamingElementArrayBuffer == 0)
278 {
279 mFunctions->genBuffers(1, &mStreamingElementArrayBuffer);
280 mStreamingElementArrayBufferSize = 0;
281 }
282
283 mStateManager->bindBuffer(GL_ELEMENT_ARRAY_BUFFER, mStreamingElementArrayBuffer);
284 mAppliedElementArrayBuffer = mStreamingElementArrayBuffer;
285
286 // Make sure the element array buffer is large enough
287 const gl::Type &indexTypeInfo = gl::GetTypeInfo(type);
288 size_t requiredStreamingBufferSize = indexTypeInfo.bytes * count;
289 if (requiredStreamingBufferSize > mStreamingElementArrayBufferSize)
290 {
291 // Copy the indices in while resizing the buffer
292 mFunctions->bufferData(GL_ELEMENT_ARRAY_BUFFER, requiredStreamingBufferSize, indices, GL_DYNAMIC_DRAW);
293 mStreamingElementArrayBufferSize = requiredStreamingBufferSize;
294 }
295 else
296 {
297 // Put the indices at the beginning of the buffer
298 mFunctions->bufferSubData(GL_ELEMENT_ARRAY_BUFFER, 0, requiredStreamingBufferSize, indices);
299 }
300
301 // Set the index offset for the draw call to zero since the supplied index pointer is to client data
302 *outIndices = nullptr;
303 }
304
305 return gl::Error(GL_NO_ERROR);
306}
307
Geoff Lang831b1952015-05-05 11:02:27 -0400308gl::Error VertexArrayGL::streamAttributes(size_t streamingDataSize, size_t maxAttributeDataSize, const gl::RangeUI &indexRange) const
Geoff Lang7c82bc42015-03-09 16:18:08 -0400309{
310 if (mStreamingArrayBuffer == 0)
311 {
312 mFunctions->genBuffers(1, &mStreamingArrayBuffer);
313 mStreamingArrayBufferSize = 0;
314 }
315
316 // If first is greater than zero, a slack space needs to be left at the beginning of the buffer so that
317 // the same 'first' argument can be passed into the draw call.
318 const size_t bufferEmptySpace = maxAttributeDataSize * indexRange.start;
319 const size_t requiredBufferSize = streamingDataSize + bufferEmptySpace;
320
321 mStateManager->bindBuffer(GL_ARRAY_BUFFER, mStreamingArrayBuffer);
322 if (requiredBufferSize > mStreamingArrayBufferSize)
323 {
324 mFunctions->bufferData(GL_ARRAY_BUFFER, requiredBufferSize, nullptr, GL_DYNAMIC_DRAW);
325 mStreamingArrayBufferSize = requiredBufferSize;
326 }
327
328 // Unmapping a buffer can return GL_FALSE to indicate that the system has corrupted the data
329 // somehow (such as by a screen change), retry writing the data a few times and return OUT_OF_MEMORY
330 // if that fails.
331 GLboolean unmapResult = GL_FALSE;
332 size_t unmapRetryAttempts = 5;
333 while (unmapResult != GL_TRUE && --unmapRetryAttempts > 0)
334 {
335 uint8_t *bufferPointer = reinterpret_cast<uint8_t*>(mFunctions->mapBuffer(GL_ARRAY_BUFFER, GL_WRITE_ONLY));
336 size_t curBufferOffset = bufferEmptySpace;
337
338 const size_t streamedVertexCount = indexRange.end - indexRange.start + 1;
339
340 for (size_t idx = 0; idx < mAttributes.size(); idx++)
341 {
342 if (mAttributes[idx].enabled && mAttributes[idx].buffer.get() == nullptr)
343 {
344 const size_t sourceStride = ComputeVertexAttributeStride(mAttributes[idx]);
345 const size_t destStride = ComputeVertexAttributeTypeSize(mAttributes[idx]);
346
347 const uint8_t *inputPointer = reinterpret_cast<const uint8_t*>(mAttributes[idx].pointer);
348
349 // Pack the data when copying it, user could have supplied a very large stride that would
350 // cause the buffer to be much larger than needed.
351 if (destStride == sourceStride)
352 {
353 // Can copy in one go, the data is packed
354 memcpy(bufferPointer + curBufferOffset,
355 inputPointer + (sourceStride * indexRange.start),
356 destStride * streamedVertexCount);
357 }
358 else
359 {
360 // Copy each vertex individually
361 for (size_t vertexIdx = indexRange.start; vertexIdx <= indexRange.end; vertexIdx++)
362 {
363 memcpy(bufferPointer + curBufferOffset + (destStride * vertexIdx),
364 inputPointer + (sourceStride * vertexIdx),
365 destStride);
366 }
367 }
368
369 // Compute where the 0-index vertex would be.
370 const size_t vertexStartOffset = curBufferOffset - (indexRange.start * destStride);
371
372 mFunctions->vertexAttribPointer(idx, mAttributes[idx].size, mAttributes[idx].type,
373 mAttributes[idx].normalized, destStride,
374 reinterpret_cast<const GLvoid*>(vertexStartOffset));
375
376 curBufferOffset += destStride * streamedVertexCount;
377
378 // Mark the applied attribute as dirty by setting an invalid size so that if it doesn't
379 // need to be streamed later, there is no chance that the caching will skip it.
380 mAppliedAttributes[idx].size = static_cast<GLuint>(-1);
381 }
382 }
383
384 unmapResult = mFunctions->unmapBuffer(GL_ARRAY_BUFFER);
385 }
386
387 if (unmapResult != GL_TRUE)
388 {
389 return gl::Error(GL_OUT_OF_MEMORY, "Failed to unmap the client data streaming buffer.");
390 }
391
392 return gl::Error(GL_NO_ERROR);
Geoff Langba4c4a82015-02-24 12:38:46 -0500393}
394
395GLuint VertexArrayGL::getVertexArrayID() const
396{
397 return mVertexArrayID;
Geoff Langf9a6f082015-01-22 13:32:49 -0500398}
399
400}