blob: 48e0ddcb256e0c649dc900695338cc2ff58a8b72 [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 Lang851cd582015-05-26 16:47:23 -0400212 if (arrayBuffer != nullptr)
213 {
214 const BufferGL *arrayBufferGL = GetImplAs<BufferGL>(arrayBuffer);
215 mStateManager->bindBuffer(GL_ARRAY_BUFFER, arrayBufferGL->getBufferID());
216 }
217 else
218 {
219 mStateManager->bindBuffer(GL_ARRAY_BUFFER, 0);
220 }
Geoff Lang6ae6efc2015-03-09 14:42:35 -0400221
Geoff Lang7c82bc42015-03-09 16:18:08 -0400222 if (mAttributes[idx].pureInteger)
223 {
224 mFunctions->vertexAttribIPointer(idx, mAttributes[idx].size, mAttributes[idx].type,
225 mAttributes[idx].stride, mAttributes[idx].pointer);
226 }
227 else
228 {
229 mFunctions->vertexAttribPointer(idx, mAttributes[idx].size, mAttributes[idx].type,
230 mAttributes[idx].normalized, mAttributes[idx].stride,
231 mAttributes[idx].pointer);
232 }
Geoff Lang6ae6efc2015-03-09 14:42:35 -0400233
Geoff Lang7c82bc42015-03-09 16:18:08 -0400234 mAppliedAttributes[idx] = mAttributes[idx];
235 }
Geoff Langba4c4a82015-02-24 12:38:46 -0500236 }
Geoff Langba4c4a82015-02-24 12:38:46 -0500237 }
Geoff Lang7c82bc42015-03-09 16:18:08 -0400238
239 return gl::Error(GL_NO_ERROR);
240}
241
242gl::Error VertexArrayGL::syncIndexData(GLsizei count, GLenum type, const GLvoid *indices, bool attributesNeedStreaming,
Geoff Lang831b1952015-05-05 11:02:27 -0400243 gl::RangeUI *outIndexRange, const GLvoid **outIndices) const
Geoff Lang7c82bc42015-03-09 16:18:08 -0400244{
245 ASSERT(outIndices);
246
247 // Need to check the range of indices if attributes need to be streamed
248 if (mElementArrayBuffer.get() != nullptr)
249 {
250 const BufferGL *bufferGL = GetImplAs<BufferGL>(mElementArrayBuffer.get());
251 GLuint elementArrayBufferID = bufferGL->getBufferID();
252 if (elementArrayBufferID != mAppliedElementArrayBuffer)
253 {
254 mStateManager->bindBuffer(GL_ELEMENT_ARRAY_BUFFER, elementArrayBufferID);
255 mAppliedElementArrayBuffer = elementArrayBufferID;
256 }
257
258 // Only compute the index range if the attributes also need to be streamed
259 if (attributesNeedStreaming)
260 {
261 ptrdiff_t elementArrayBufferOffset = reinterpret_cast<ptrdiff_t>(indices);
Geoff Lang520c4ae2015-05-05 13:12:36 -0400262 gl::Error error = mElementArrayBuffer->getIndexRange(type, static_cast<size_t>(elementArrayBufferOffset), count, outIndexRange);
263 if (error.isError())
Geoff Lang7c82bc42015-03-09 16:18:08 -0400264 {
Geoff Lang520c4ae2015-05-05 13:12:36 -0400265 return error;
Geoff Lang7c82bc42015-03-09 16:18:08 -0400266 }
267 }
268
269 // Indices serves as an offset into the index buffer in this case, use the same value for the draw call
270 *outIndices = indices;
271 }
272 else
273 {
274 // Need to stream the index buffer
275 // TODO: if GLES, nothing needs to be streamed
276
277 // Only compute the index range if the attributes also need to be streamed
278 if (attributesNeedStreaming)
279 {
Geoff Lang831b1952015-05-05 11:02:27 -0400280 *outIndexRange = gl::ComputeIndexRange(type, indices, count);
Geoff Lang7c82bc42015-03-09 16:18:08 -0400281 }
282
283 // Allocate the streaming element array buffer
284 if (mStreamingElementArrayBuffer == 0)
285 {
286 mFunctions->genBuffers(1, &mStreamingElementArrayBuffer);
287 mStreamingElementArrayBufferSize = 0;
288 }
289
290 mStateManager->bindBuffer(GL_ELEMENT_ARRAY_BUFFER, mStreamingElementArrayBuffer);
291 mAppliedElementArrayBuffer = mStreamingElementArrayBuffer;
292
293 // Make sure the element array buffer is large enough
294 const gl::Type &indexTypeInfo = gl::GetTypeInfo(type);
295 size_t requiredStreamingBufferSize = indexTypeInfo.bytes * count;
296 if (requiredStreamingBufferSize > mStreamingElementArrayBufferSize)
297 {
298 // Copy the indices in while resizing the buffer
299 mFunctions->bufferData(GL_ELEMENT_ARRAY_BUFFER, requiredStreamingBufferSize, indices, GL_DYNAMIC_DRAW);
300 mStreamingElementArrayBufferSize = requiredStreamingBufferSize;
301 }
302 else
303 {
304 // Put the indices at the beginning of the buffer
305 mFunctions->bufferSubData(GL_ELEMENT_ARRAY_BUFFER, 0, requiredStreamingBufferSize, indices);
306 }
307
308 // Set the index offset for the draw call to zero since the supplied index pointer is to client data
309 *outIndices = nullptr;
310 }
311
312 return gl::Error(GL_NO_ERROR);
313}
314
Geoff Lang831b1952015-05-05 11:02:27 -0400315gl::Error VertexArrayGL::streamAttributes(size_t streamingDataSize, size_t maxAttributeDataSize, const gl::RangeUI &indexRange) const
Geoff Lang7c82bc42015-03-09 16:18:08 -0400316{
317 if (mStreamingArrayBuffer == 0)
318 {
319 mFunctions->genBuffers(1, &mStreamingArrayBuffer);
320 mStreamingArrayBufferSize = 0;
321 }
322
323 // If first is greater than zero, a slack space needs to be left at the beginning of the buffer so that
324 // the same 'first' argument can be passed into the draw call.
325 const size_t bufferEmptySpace = maxAttributeDataSize * indexRange.start;
326 const size_t requiredBufferSize = streamingDataSize + bufferEmptySpace;
327
328 mStateManager->bindBuffer(GL_ARRAY_BUFFER, mStreamingArrayBuffer);
329 if (requiredBufferSize > mStreamingArrayBufferSize)
330 {
331 mFunctions->bufferData(GL_ARRAY_BUFFER, requiredBufferSize, nullptr, GL_DYNAMIC_DRAW);
332 mStreamingArrayBufferSize = requiredBufferSize;
333 }
334
335 // Unmapping a buffer can return GL_FALSE to indicate that the system has corrupted the data
336 // somehow (such as by a screen change), retry writing the data a few times and return OUT_OF_MEMORY
337 // if that fails.
338 GLboolean unmapResult = GL_FALSE;
339 size_t unmapRetryAttempts = 5;
340 while (unmapResult != GL_TRUE && --unmapRetryAttempts > 0)
341 {
342 uint8_t *bufferPointer = reinterpret_cast<uint8_t*>(mFunctions->mapBuffer(GL_ARRAY_BUFFER, GL_WRITE_ONLY));
343 size_t curBufferOffset = bufferEmptySpace;
344
345 const size_t streamedVertexCount = indexRange.end - indexRange.start + 1;
346
347 for (size_t idx = 0; idx < mAttributes.size(); idx++)
348 {
349 if (mAttributes[idx].enabled && mAttributes[idx].buffer.get() == nullptr)
350 {
351 const size_t sourceStride = ComputeVertexAttributeStride(mAttributes[idx]);
352 const size_t destStride = ComputeVertexAttributeTypeSize(mAttributes[idx]);
353
354 const uint8_t *inputPointer = reinterpret_cast<const uint8_t*>(mAttributes[idx].pointer);
355
356 // Pack the data when copying it, user could have supplied a very large stride that would
357 // cause the buffer to be much larger than needed.
358 if (destStride == sourceStride)
359 {
360 // Can copy in one go, the data is packed
361 memcpy(bufferPointer + curBufferOffset,
362 inputPointer + (sourceStride * indexRange.start),
363 destStride * streamedVertexCount);
364 }
365 else
366 {
367 // Copy each vertex individually
368 for (size_t vertexIdx = indexRange.start; vertexIdx <= indexRange.end; vertexIdx++)
369 {
370 memcpy(bufferPointer + curBufferOffset + (destStride * vertexIdx),
371 inputPointer + (sourceStride * vertexIdx),
372 destStride);
373 }
374 }
375
376 // Compute where the 0-index vertex would be.
377 const size_t vertexStartOffset = curBufferOffset - (indexRange.start * destStride);
378
379 mFunctions->vertexAttribPointer(idx, mAttributes[idx].size, mAttributes[idx].type,
380 mAttributes[idx].normalized, destStride,
381 reinterpret_cast<const GLvoid*>(vertexStartOffset));
382
383 curBufferOffset += destStride * streamedVertexCount;
384
385 // Mark the applied attribute as dirty by setting an invalid size so that if it doesn't
386 // need to be streamed later, there is no chance that the caching will skip it.
387 mAppliedAttributes[idx].size = static_cast<GLuint>(-1);
388 }
389 }
390
391 unmapResult = mFunctions->unmapBuffer(GL_ARRAY_BUFFER);
392 }
393
394 if (unmapResult != GL_TRUE)
395 {
396 return gl::Error(GL_OUT_OF_MEMORY, "Failed to unmap the client data streaming buffer.");
397 }
398
399 return gl::Error(GL_NO_ERROR);
Geoff Langba4c4a82015-02-24 12:38:46 -0500400}
401
402GLuint VertexArrayGL::getVertexArrayID() const
403{
404 return mVertexArrayID;
Geoff Langf9a6f082015-01-22 13:32:49 -0500405}
406
407}