blob: c03647f05931dcd52247f32f2f2b982aa0b867a2 [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 Madill0b9e9032015-08-17 11:51:52 +000011#include "common/BitSetIterator.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"
Geoff Langb2ebb5e2016-06-08 18:17:55 +000020#include "libANGLE/renderer/gl/renderergl_utils.h"
Geoff Langba4c4a82015-02-24 12:38:46 -050021#include "libANGLE/renderer/gl/StateManagerGL.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{
Jiawei-Shao2597fb62016-12-09 16:38:02 +080032 return (attrib.enabled && binding.buffer.get() == nullptr);
Jamie Madill0b9e9032015-08-17 11:51:52 +000033}
Jamie Madill0b9e9032015-08-17 11:51:52 +000034} // anonymous namespace
35
Jamie Madill3f572682016-04-26 13:41:36 -040036VertexArrayGL::VertexArrayGL(const VertexArrayState &state,
Jamie Madill77a90c22015-08-11 16:33:17 -040037 const FunctionsGL *functions,
38 StateManagerGL *stateManager)
Jamie Madill3f572682016-04-26 13:41:36 -040039 : VertexArrayImpl(state),
Geoff Langba4c4a82015-02-24 12:38:46 -050040 mFunctions(functions),
41 mStateManager(stateManager),
42 mVertexArrayID(0),
Jamie Madill77a90c22015-08-11 16:33:17 -040043 mAppliedElementArrayBuffer(),
Jiawei-Shao2597fb62016-12-09 16:38:02 +080044 mAppliedBindings(state.getMaxBindings()),
Geoff Lang7c82bc42015-03-09 16:18:08 -040045 mStreamingElementArrayBufferSize(0),
46 mStreamingElementArrayBuffer(0),
47 mStreamingArrayBufferSize(0),
48 mStreamingArrayBuffer(0)
Geoff Langba4c4a82015-02-24 12:38:46 -050049{
50 ASSERT(mFunctions);
51 ASSERT(mStateManager);
52 mFunctions->genVertexArrays(1, &mVertexArrayID);
53
Jiawei-Shao2597fb62016-12-09 16:38:02 +080054 // Set the cached vertex attribute array and vertex attribute binding array size
55 GLuint maxVertexAttribs = static_cast<GLuint>(state.getMaxAttribs());
56 for (GLuint i = 0; i < maxVertexAttribs; i++)
57 {
58 mAppliedAttributes.emplace_back(i);
59 }
Geoff Langba4c4a82015-02-24 12:38:46 -050060}
Geoff Langf9a6f082015-01-22 13:32:49 -050061
62VertexArrayGL::~VertexArrayGL()
Geoff Langba4c4a82015-02-24 12:38:46 -050063{
Geoff Lang1eb708e2015-05-04 14:58:23 -040064 mStateManager->deleteVertexArray(mVertexArrayID);
65 mVertexArrayID = 0;
Geoff Langba4c4a82015-02-24 12:38:46 -050066
Geoff Lang1eb708e2015-05-04 14:58:23 -040067 mStateManager->deleteBuffer(mStreamingElementArrayBuffer);
68 mStreamingElementArrayBufferSize = 0;
69 mStreamingElementArrayBuffer = 0;
Geoff Lang7c82bc42015-03-09 16:18:08 -040070
Geoff Lang1eb708e2015-05-04 14:58:23 -040071 mStateManager->deleteBuffer(mStreamingArrayBuffer);
72 mStreamingArrayBufferSize = 0;
73 mStreamingArrayBuffer = 0;
Geoff Lang7c82bc42015-03-09 16:18:08 -040074
Jamie Madill77a90c22015-08-11 16:33:17 -040075 mAppliedElementArrayBuffer.set(nullptr);
Jiawei-Shao2597fb62016-12-09 16:38:02 +080076 for (auto &binding : mAppliedBindings)
Geoff Langba4c4a82015-02-24 12:38:46 -050077 {
Jiawei-Shao2597fb62016-12-09 16:38:02 +080078 binding.buffer.set(nullptr);
Geoff Langba4c4a82015-02-24 12:38:46 -050079 }
80}
Geoff Langf9a6f082015-01-22 13:32:49 -050081
Jamie Madill0b9e9032015-08-17 11:51:52 +000082gl::Error VertexArrayGL::syncDrawArraysState(const gl::AttributesMask &activeAttributesMask,
83 GLint first,
Geoff Lang3cf12ce2015-08-27 14:40:48 -040084 GLsizei count,
85 GLsizei instanceCount) const
Geoff Lang7c82bc42015-03-09 16:18:08 -040086{
Geoff Lang3edfe032015-09-04 16:38:24 -040087 return syncDrawState(activeAttributesMask, first, count, GL_NONE, nullptr, instanceCount, false,
Geoff Lang3cf12ce2015-08-27 14:40:48 -040088 nullptr);
Geoff Lang7c82bc42015-03-09 16:18:08 -040089}
90
Jamie Madill0b9e9032015-08-17 11:51:52 +000091gl::Error VertexArrayGL::syncDrawElementsState(const gl::AttributesMask &activeAttributesMask,
92 GLsizei count,
93 GLenum type,
94 const GLvoid *indices,
Geoff Lang3cf12ce2015-08-27 14:40:48 -040095 GLsizei instanceCount,
Geoff Lang3edfe032015-09-04 16:38:24 -040096 bool primitiveRestartEnabled,
Jamie Madill0b9e9032015-08-17 11:51:52 +000097 const GLvoid **outIndices) const
Geoff Lang7c82bc42015-03-09 16:18:08 -040098{
Geoff Lang3edfe032015-09-04 16:38:24 -040099 return syncDrawState(activeAttributesMask, 0, count, type, indices, instanceCount,
100 primitiveRestartEnabled, outIndices);
Geoff Lang7c82bc42015-03-09 16:18:08 -0400101}
102
Jiajia Qind9671222016-11-29 16:30:31 +0800103gl::Error VertexArrayGL::syncElementArrayState() const
104{
105 gl::Buffer *elementArrayBuffer = mData.getElementArrayBuffer().get();
106 ASSERT(elementArrayBuffer);
107 if (elementArrayBuffer != mAppliedElementArrayBuffer.get())
108 {
109 const BufferGL *bufferGL = GetImplAs<BufferGL>(elementArrayBuffer);
110 mStateManager->bindBuffer(GL_ELEMENT_ARRAY_BUFFER, bufferGL->getBufferID());
111 mAppliedElementArrayBuffer.set(elementArrayBuffer);
112 }
113
114 return gl::NoError();
115}
116
Jamie Madill0b9e9032015-08-17 11:51:52 +0000117gl::Error VertexArrayGL::syncDrawState(const gl::AttributesMask &activeAttributesMask,
118 GLint first,
119 GLsizei count,
120 GLenum type,
121 const GLvoid *indices,
Geoff Lang3cf12ce2015-08-27 14:40:48 -0400122 GLsizei instanceCount,
Geoff Lang3edfe032015-09-04 16:38:24 -0400123 bool primitiveRestartEnabled,
Jamie Madill0b9e9032015-08-17 11:51:52 +0000124 const GLvoid **outIndices) const
Geoff Lang6ae6efc2015-03-09 14:42:35 -0400125{
Jamie Madill77a90c22015-08-11 16:33:17 -0400126 mStateManager->bindVertexArray(mVertexArrayID, getAppliedElementArrayBufferID());
Geoff Lang6ae6efc2015-03-09 14:42:35 -0400127
Geoff Lang7c82bc42015-03-09 16:18:08 -0400128 // Check if any attributes need to be streamed, determines if the index range needs to be computed
Jamie Madill0b9e9032015-08-17 11:51:52 +0000129 bool attributesNeedStreaming = mAttributesNeedStreaming.any();
Geoff Lang7c82bc42015-03-09 16:18:08 -0400130
131 // Determine if an index buffer needs to be streamed and the range of vertices that need to be copied
Geoff Lang3edfe032015-09-04 16:38:24 -0400132 IndexRange indexRange;
Geoff Lang7c82bc42015-03-09 16:18:08 -0400133 if (type != GL_NONE)
Geoff Langba4c4a82015-02-24 12:38:46 -0500134 {
Geoff Lang3edfe032015-09-04 16:38:24 -0400135 Error error = syncIndexData(count, type, indices, primitiveRestartEnabled,
136 attributesNeedStreaming, &indexRange, outIndices);
Geoff Lang7c82bc42015-03-09 16:18:08 -0400137 if (error.isError())
138 {
139 return error;
140 }
141 }
142 else
143 {
Corentin Wallez2c34a4b2015-08-25 16:26:02 -0400144 // Not an indexed call, set the range to [first, first + count - 1]
Geoff Lang7c82bc42015-03-09 16:18:08 -0400145 indexRange.start = first;
Corentin Wallez2c34a4b2015-08-25 16:26:02 -0400146 indexRange.end = first + count - 1;
Geoff Langba4c4a82015-02-24 12:38:46 -0500147 }
148
Jamie Madill0b9e9032015-08-17 11:51:52 +0000149 if (attributesNeedStreaming)
Geoff Langba4c4a82015-02-24 12:38:46 -0500150 {
Geoff Lang3cf12ce2015-08-27 14:40:48 -0400151 Error error = streamAttributes(activeAttributesMask, instanceCount, indexRange);
Geoff Lang7c82bc42015-03-09 16:18:08 -0400152 if (error.isError())
153 {
154 return error;
155 }
156 }
157
He Yunchaoacd18982017-01-04 10:46:42 +0800158 return NoError();
Geoff Lang7c82bc42015-03-09 16:18:08 -0400159}
160
Jiajia Qind9671222016-11-29 16:30:31 +0800161gl::Error VertexArrayGL::syncIndexData(GLsizei count,
162 GLenum type,
163 const GLvoid *indices,
164 bool primitiveRestartEnabled,
165 bool attributesNeedStreaming,
166 IndexRange *outIndexRange,
167 const GLvoid **outIndices) const
Geoff Lang7c82bc42015-03-09 16:18:08 -0400168{
169 ASSERT(outIndices);
170
Jamie Madill8e344942015-07-09 14:22:07 -0400171 gl::Buffer *elementArrayBuffer = mData.getElementArrayBuffer().get();
172
Geoff Lang7c82bc42015-03-09 16:18:08 -0400173 // Need to check the range of indices if attributes need to be streamed
Jamie Madill8e344942015-07-09 14:22:07 -0400174 if (elementArrayBuffer != nullptr)
Geoff Lang7c82bc42015-03-09 16:18:08 -0400175 {
Jamie Madill77a90c22015-08-11 16:33:17 -0400176 if (elementArrayBuffer != mAppliedElementArrayBuffer.get())
Geoff Lang7c82bc42015-03-09 16:18:08 -0400177 {
Jamie Madill77a90c22015-08-11 16:33:17 -0400178 const BufferGL *bufferGL = GetImplAs<BufferGL>(elementArrayBuffer);
179 mStateManager->bindBuffer(GL_ELEMENT_ARRAY_BUFFER, bufferGL->getBufferID());
180 mAppliedElementArrayBuffer.set(elementArrayBuffer);
Geoff Lang7c82bc42015-03-09 16:18:08 -0400181 }
182
183 // Only compute the index range if the attributes also need to be streamed
184 if (attributesNeedStreaming)
185 {
186 ptrdiff_t elementArrayBufferOffset = reinterpret_cast<ptrdiff_t>(indices);
Jamie Madill0b9e9032015-08-17 11:51:52 +0000187 Error error = mData.getElementArrayBuffer()->getIndexRange(
Geoff Lang3edfe032015-09-04 16:38:24 -0400188 type, elementArrayBufferOffset, count, primitiveRestartEnabled, outIndexRange);
Geoff Lang520c4ae2015-05-05 13:12:36 -0400189 if (error.isError())
Geoff Lang7c82bc42015-03-09 16:18:08 -0400190 {
Geoff Lang520c4ae2015-05-05 13:12:36 -0400191 return error;
Geoff Lang7c82bc42015-03-09 16:18:08 -0400192 }
193 }
194
195 // Indices serves as an offset into the index buffer in this case, use the same value for the draw call
196 *outIndices = indices;
197 }
198 else
199 {
200 // Need to stream the index buffer
201 // TODO: if GLES, nothing needs to be streamed
202
203 // Only compute the index range if the attributes also need to be streamed
204 if (attributesNeedStreaming)
205 {
Geoff Lang3edfe032015-09-04 16:38:24 -0400206 *outIndexRange = ComputeIndexRange(type, indices, count, primitiveRestartEnabled);
Geoff Lang7c82bc42015-03-09 16:18:08 -0400207 }
208
209 // Allocate the streaming element array buffer
210 if (mStreamingElementArrayBuffer == 0)
211 {
212 mFunctions->genBuffers(1, &mStreamingElementArrayBuffer);
213 mStreamingElementArrayBufferSize = 0;
214 }
215
216 mStateManager->bindBuffer(GL_ELEMENT_ARRAY_BUFFER, mStreamingElementArrayBuffer);
Jamie Madill77a90c22015-08-11 16:33:17 -0400217 mAppliedElementArrayBuffer.set(nullptr);
Geoff Lang7c82bc42015-03-09 16:18:08 -0400218
219 // Make sure the element array buffer is large enough
Jamie Madill0b9e9032015-08-17 11:51:52 +0000220 const Type &indexTypeInfo = GetTypeInfo(type);
Geoff Lang7c82bc42015-03-09 16:18:08 -0400221 size_t requiredStreamingBufferSize = indexTypeInfo.bytes * count;
222 if (requiredStreamingBufferSize > mStreamingElementArrayBufferSize)
223 {
224 // Copy the indices in while resizing the buffer
225 mFunctions->bufferData(GL_ELEMENT_ARRAY_BUFFER, requiredStreamingBufferSize, indices, GL_DYNAMIC_DRAW);
226 mStreamingElementArrayBufferSize = requiredStreamingBufferSize;
227 }
228 else
229 {
230 // Put the indices at the beginning of the buffer
231 mFunctions->bufferSubData(GL_ELEMENT_ARRAY_BUFFER, 0, requiredStreamingBufferSize, indices);
232 }
233
234 // Set the index offset for the draw call to zero since the supplied index pointer is to client data
235 *outIndices = nullptr;
236 }
237
He Yunchaoacd18982017-01-04 10:46:42 +0800238 return NoError();
Geoff Lang7c82bc42015-03-09 16:18:08 -0400239}
240
Jamie Madill0b9e9032015-08-17 11:51:52 +0000241void VertexArrayGL::computeStreamingAttributeSizes(const gl::AttributesMask &activeAttributesMask,
Geoff Lang3cf12ce2015-08-27 14:40:48 -0400242 GLsizei instanceCount,
Geoff Lang3edfe032015-09-04 16:38:24 -0400243 const gl::IndexRange &indexRange,
Jamie Madill0b9e9032015-08-17 11:51:52 +0000244 size_t *outStreamingDataSize,
245 size_t *outMaxAttributeDataSize) const
Geoff Lang7c82bc42015-03-09 16:18:08 -0400246{
Jamie Madill0b9e9032015-08-17 11:51:52 +0000247 *outStreamingDataSize = 0;
248 *outMaxAttributeDataSize = 0;
249
250 ASSERT(mAttributesNeedStreaming.any());
251
252 const auto &attribs = mData.getVertexAttributes();
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800253 const auto &bindings = mData.getVertexBindings();
Corentin Wallezca311dd2015-12-07 15:07:48 -0500254 for (auto idx : angle::IterateBitSet(mAttributesNeedStreaming & activeAttributesMask))
Jamie Madill0b9e9032015-08-17 11:51:52 +0000255 {
256 const auto &attrib = attribs[idx];
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800257 const auto &binding = bindings[attrib.bindingIndex];
258 ASSERT(AttributeNeedsStreaming(attrib, binding));
Jamie Madill0b9e9032015-08-17 11:51:52 +0000259
Jamie Madill0b9e9032015-08-17 11:51:52 +0000260 // If streaming is going to be required, compute the size of the required buffer
261 // and how much slack space at the beginning of the buffer will be required by determining
262 // the attribute with the largest data size.
263 size_t typeSize = ComputeVertexAttributeTypeSize(attrib);
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800264 *outStreamingDataSize += typeSize * ComputeVertexBindingElementCount(
265 binding, indexRange.vertexCount(), instanceCount);
Jamie Madill0b9e9032015-08-17 11:51:52 +0000266 *outMaxAttributeDataSize = std::max(*outMaxAttributeDataSize, typeSize);
267 }
268}
269
270gl::Error VertexArrayGL::streamAttributes(const gl::AttributesMask &activeAttributesMask,
Geoff Lang3cf12ce2015-08-27 14:40:48 -0400271 GLsizei instanceCount,
Geoff Lang3edfe032015-09-04 16:38:24 -0400272 const gl::IndexRange &indexRange) const
Jamie Madill0b9e9032015-08-17 11:51:52 +0000273{
274 // Sync the vertex attribute state and track what data needs to be streamed
275 size_t streamingDataSize = 0;
276 size_t maxAttributeDataSize = 0;
277
Geoff Lang3cf12ce2015-08-27 14:40:48 -0400278 computeStreamingAttributeSizes(activeAttributesMask, instanceCount, indexRange,
279 &streamingDataSize, &maxAttributeDataSize);
Jamie Madill0b9e9032015-08-17 11:51:52 +0000280
281 if (streamingDataSize == 0)
282 {
He Yunchaoacd18982017-01-04 10:46:42 +0800283 return gl::NoError();
Jamie Madill0b9e9032015-08-17 11:51:52 +0000284 }
285
Geoff Lang7c82bc42015-03-09 16:18:08 -0400286 if (mStreamingArrayBuffer == 0)
287 {
288 mFunctions->genBuffers(1, &mStreamingArrayBuffer);
289 mStreamingArrayBufferSize = 0;
290 }
291
292 // If first is greater than zero, a slack space needs to be left at the beginning of the buffer so that
293 // the same 'first' argument can be passed into the draw call.
294 const size_t bufferEmptySpace = maxAttributeDataSize * indexRange.start;
295 const size_t requiredBufferSize = streamingDataSize + bufferEmptySpace;
296
297 mStateManager->bindBuffer(GL_ARRAY_BUFFER, mStreamingArrayBuffer);
298 if (requiredBufferSize > mStreamingArrayBufferSize)
299 {
300 mFunctions->bufferData(GL_ARRAY_BUFFER, requiredBufferSize, nullptr, GL_DYNAMIC_DRAW);
301 mStreamingArrayBufferSize = requiredBufferSize;
302 }
303
304 // Unmapping a buffer can return GL_FALSE to indicate that the system has corrupted the data
305 // somehow (such as by a screen change), retry writing the data a few times and return OUT_OF_MEMORY
306 // if that fails.
307 GLboolean unmapResult = GL_FALSE;
308 size_t unmapRetryAttempts = 5;
309 while (unmapResult != GL_TRUE && --unmapRetryAttempts > 0)
310 {
Geoff Langb2ebb5e2016-06-08 18:17:55 +0000311 uint8_t *bufferPointer = MapBufferRangeWithFallback(mFunctions, GL_ARRAY_BUFFER, 0,
312 requiredBufferSize, GL_MAP_WRITE_BIT);
Geoff Lang7c82bc42015-03-09 16:18:08 -0400313 size_t curBufferOffset = bufferEmptySpace;
314
Jamie Madill8e344942015-07-09 14:22:07 -0400315 const auto &attribs = mData.getVertexAttributes();
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800316 const auto &bindings = mData.getVertexBindings();
Corentin Wallezca311dd2015-12-07 15:07:48 -0500317 for (auto idx : angle::IterateBitSet(mAttributesNeedStreaming & activeAttributesMask))
Geoff Lang7c82bc42015-03-09 16:18:08 -0400318 {
Jamie Madill8e344942015-07-09 14:22:07 -0400319 const auto &attrib = attribs[idx];
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800320 const auto &binding = bindings[attrib.bindingIndex];
321 ASSERT(AttributeNeedsStreaming(attrib, binding));
Geoff Lang7c82bc42015-03-09 16:18:08 -0400322
Geoff Lang3cf12ce2015-08-27 14:40:48 -0400323 const size_t streamedVertexCount =
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800324 ComputeVertexBindingElementCount(binding, indexRange.vertexCount(), instanceCount);
Geoff Lang3cf12ce2015-08-27 14:40:48 -0400325
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800326 const size_t sourceStride = ComputeVertexAttributeStride(attrib, binding);
Jamie Madill0b9e9032015-08-17 11:51:52 +0000327 const size_t destStride = ComputeVertexAttributeTypeSize(attrib);
328
Geoff Lang38a24a92017-02-15 13:53:06 -0500329 // Vertices do not apply the 'start' offset when the divisor is non-zero even when doing
330 // a non-instanced draw call
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800331 const size_t firstIndex = binding.divisor == 0 ? indexRange.start : 0;
Geoff Lang38a24a92017-02-15 13:53:06 -0500332
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800333 // Attributes using client memory ignore the VERTEX_ATTRIB_BINDING state.
334 // https://www.opengl.org/registry/specs/ARB/vertex_attrib_binding.txt
Jamie Madill0b9e9032015-08-17 11:51:52 +0000335 const uint8_t *inputPointer = reinterpret_cast<const uint8_t *>(attrib.pointer);
336
337 // Pack the data when copying it, user could have supplied a very large stride that
338 // would cause the buffer to be much larger than needed.
339 if (destStride == sourceStride)
Jamie Madill8e344942015-07-09 14:22:07 -0400340 {
Jamie Madill0b9e9032015-08-17 11:51:52 +0000341 // Can copy in one go, the data is packed
Geoff Lang38a24a92017-02-15 13:53:06 -0500342 memcpy(bufferPointer + curBufferOffset, inputPointer + (sourceStride * firstIndex),
Jamie Madill0b9e9032015-08-17 11:51:52 +0000343 destStride * streamedVertexCount);
Jamie Madill6d51c702015-08-14 10:38:10 -0400344 }
Jamie Madill0b9e9032015-08-17 11:51:52 +0000345 else
346 {
347 // Copy each vertex individually
Geoff Lang6b10ddb2015-09-02 15:55:10 -0400348 for (size_t vertexIdx = 0; vertexIdx < streamedVertexCount; vertexIdx++)
Jamie Madill0b9e9032015-08-17 11:51:52 +0000349 {
Corentin Wallez4d5362d2015-08-25 11:25:14 -0400350 uint8_t *out = bufferPointer + curBufferOffset + (destStride * vertexIdx);
Geoff Lang38a24a92017-02-15 13:53:06 -0500351 const uint8_t *in = inputPointer + sourceStride * (vertexIdx + firstIndex);
Corentin Wallez4d5362d2015-08-25 11:25:14 -0400352 memcpy(out, in, destStride);
Jamie Madill0b9e9032015-08-17 11:51:52 +0000353 }
354 }
355
356 // Compute where the 0-index vertex would be.
Geoff Lang38a24a92017-02-15 13:53:06 -0500357 const size_t vertexStartOffset = curBufferOffset - (firstIndex * destStride);
Jamie Madill0b9e9032015-08-17 11:51:52 +0000358
Geoff Lang2b835a62015-09-10 15:58:44 -0400359 if (attrib.pureInteger)
360 {
361 ASSERT(!attrib.normalized);
362 mFunctions->vertexAttribIPointer(
Corentin Wallezca311dd2015-12-07 15:07:48 -0500363 static_cast<GLuint>(idx), attrib.size, attrib.type,
364 static_cast<GLsizei>(destStride),
Geoff Lang2b835a62015-09-10 15:58:44 -0400365 reinterpret_cast<const GLvoid *>(vertexStartOffset));
366 }
367 else
368 {
369 mFunctions->vertexAttribPointer(
Corentin Wallezca311dd2015-12-07 15:07:48 -0500370 static_cast<GLuint>(idx), attrib.size, attrib.type, attrib.normalized,
Geoff Lang2b835a62015-09-10 15:58:44 -0400371 static_cast<GLsizei>(destStride),
372 reinterpret_cast<const GLvoid *>(vertexStartOffset));
373 }
Jamie Madill0b9e9032015-08-17 11:51:52 +0000374
375 curBufferOffset += destStride * streamedVertexCount;
376
377 // Mark the applied attribute as dirty by setting an invalid size so that if it doesn't
378 // need to be streamed later, there is no chance that the caching will skip it.
379 mAppliedAttributes[idx].size = static_cast<GLuint>(-1);
Geoff Lang7c82bc42015-03-09 16:18:08 -0400380 }
381
382 unmapResult = mFunctions->unmapBuffer(GL_ARRAY_BUFFER);
383 }
384
385 if (unmapResult != GL_TRUE)
386 {
Jamie Madill0b9e9032015-08-17 11:51:52 +0000387 return Error(GL_OUT_OF_MEMORY, "Failed to unmap the client data streaming buffer.");
Geoff Lang7c82bc42015-03-09 16:18:08 -0400388 }
389
He Yunchaoacd18982017-01-04 10:46:42 +0800390 return NoError();
Geoff Langba4c4a82015-02-24 12:38:46 -0500391}
392
393GLuint VertexArrayGL::getVertexArrayID() const
394{
395 return mVertexArrayID;
Geoff Langf9a6f082015-01-22 13:32:49 -0500396}
397
Geoff Lang294cad92015-05-26 15:11:23 -0400398GLuint VertexArrayGL::getAppliedElementArrayBufferID() const
399{
Jamie Madill77a90c22015-08-11 16:33:17 -0400400 if (mAppliedElementArrayBuffer.get() == nullptr)
401 {
402 return mStreamingElementArrayBuffer;
403 }
404
405 return GetImplAs<BufferGL>(mAppliedElementArrayBuffer.get())->getBufferID();
Geoff Lang294cad92015-05-26 15:11:23 -0400406}
407
Jamie Madill0b9e9032015-08-17 11:51:52 +0000408void VertexArrayGL::updateNeedsStreaming(size_t attribIndex)
409{
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800410 const auto &attrib = mData.getVertexAttribute(attribIndex);
411 const auto &binding = mData.getBindingFromAttribIndex(attribIndex);
412 mAttributesNeedStreaming.set(attribIndex, AttributeNeedsStreaming(attrib, binding));
Geoff Langf9a6f082015-01-22 13:32:49 -0500413}
Jamie Madill0b9e9032015-08-17 11:51:52 +0000414
415void VertexArrayGL::updateAttribEnabled(size_t attribIndex)
416{
417 const VertexAttribute &attrib = mData.getVertexAttribute(attribIndex);
418 if (mAppliedAttributes[attribIndex].enabled == attrib.enabled)
419 {
420 return;
421 }
422
423 updateNeedsStreaming(attribIndex);
424
425 mStateManager->bindVertexArray(mVertexArrayID, getAppliedElementArrayBufferID());
426 if (attrib.enabled)
427 {
428 mFunctions->enableVertexAttribArray(static_cast<GLuint>(attribIndex));
429 }
430 else
431 {
432 mFunctions->disableVertexAttribArray(static_cast<GLuint>(attribIndex));
433 }
434 mAppliedAttributes[attribIndex].enabled = attrib.enabled;
435}
436
437void VertexArrayGL::updateAttribPointer(size_t attribIndex)
438{
439 const VertexAttribute &attrib = mData.getVertexAttribute(attribIndex);
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800440 ASSERT(attribIndex == attrib.bindingIndex);
441
442 GLuint bindingIndex = attrib.bindingIndex;
443 const VertexBinding &binding = mData.getVertexBinding(bindingIndex);
444
445 if (mAppliedAttributes[attribIndex] == attrib && mAppliedBindings[bindingIndex] == binding)
Jamie Madill0b9e9032015-08-17 11:51:52 +0000446 {
447 return;
448 }
449
450 updateNeedsStreaming(attribIndex);
Jamie Madill0b9e9032015-08-17 11:51:52 +0000451
452 // If we need to stream, defer the attribPointer to the draw call.
453 if (mAttributesNeedStreaming[attribIndex])
454 {
455 return;
456 }
457
458 mStateManager->bindVertexArray(mVertexArrayID, getAppliedElementArrayBufferID());
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800459 const Buffer *arrayBuffer = binding.buffer.get();
Jamie Madill0b9e9032015-08-17 11:51:52 +0000460 if (arrayBuffer != nullptr)
461 {
462 const BufferGL *arrayBufferGL = GetImplAs<BufferGL>(arrayBuffer);
463 mStateManager->bindBuffer(GL_ARRAY_BUFFER, arrayBufferGL->getBufferID());
464 }
465 else
466 {
467 mStateManager->bindBuffer(GL_ARRAY_BUFFER, 0);
468 }
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800469
470 mAppliedBindings[bindingIndex].buffer = binding.buffer;
471
472 const GLvoid *inputPointer = nullptr;
473 if (arrayBuffer != nullptr)
474 {
475 inputPointer = static_cast<const GLvoid *>(
476 reinterpret_cast<const uint8_t *>(binding.offset + attrib.relativeOffset));
477 }
478 else
479 {
480 // Attributes using client memory ignore the VERTEX_ATTRIB_BINDING state.
481 // https://www.opengl.org/registry/specs/ARB/vertex_attrib_binding
482 inputPointer = attrib.pointer;
483 }
Jamie Madill0b9e9032015-08-17 11:51:52 +0000484
485 if (attrib.pureInteger)
486 {
487 mFunctions->vertexAttribIPointer(static_cast<GLuint>(attribIndex), attrib.size, attrib.type,
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800488 binding.stride, inputPointer);
Jamie Madill0b9e9032015-08-17 11:51:52 +0000489 }
490 else
491 {
492 mFunctions->vertexAttribPointer(static_cast<GLuint>(attribIndex), attrib.size, attrib.type,
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800493 attrib.normalized, binding.stride, inputPointer);
Jamie Madill0b9e9032015-08-17 11:51:52 +0000494 }
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800495 mAppliedAttributes[attribIndex].size = attrib.size;
496 mAppliedAttributes[attribIndex].type = attrib.type;
497 mAppliedAttributes[attribIndex].normalized = attrib.normalized;
498 mAppliedAttributes[attribIndex].pureInteger = attrib.pureInteger;
499 mAppliedAttributes[attribIndex].pointer = attrib.pointer;
500 mAppliedAttributes[attribIndex].relativeOffset = attrib.relativeOffset;
501 mAppliedAttributes[attribIndex].vertexAttribArrayStride = attrib.vertexAttribArrayStride;
502
503 mAppliedBindings[bindingIndex].stride = binding.stride;
504 mAppliedBindings[bindingIndex].offset = binding.offset;
505}
506
507void VertexArrayGL::updateAttribDivisor(size_t attribIndex)
508{
509 ASSERT(attribIndex == mData.getBindingIndexFromAttribIndex(attribIndex));
510
511 const VertexBinding &binding = mData.getVertexBinding(attribIndex);
512 if (mAppliedBindings[attribIndex].divisor != binding.divisor)
513 {
514 mStateManager->bindVertexArray(mVertexArrayID, getAppliedElementArrayBufferID());
515 mFunctions->vertexAttribDivisor(static_cast<GLuint>(attribIndex), binding.divisor);
516
517 mAppliedBindings[attribIndex].divisor = binding.divisor;
518 }
Jamie Madill0b9e9032015-08-17 11:51:52 +0000519}
520
521void VertexArrayGL::syncState(const VertexArray::DirtyBits &dirtyBits)
522{
523 for (unsigned long dirtyBit : angle::IterateBitSet(dirtyBits))
524 {
525 if (dirtyBit == VertexArray::DIRTY_BIT_ELEMENT_ARRAY_BUFFER)
526 {
527 // TODO(jmadill): Element array buffer bindings
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800528 continue;
Jamie Madill0b9e9032015-08-17 11:51:52 +0000529 }
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800530
531 size_t index = VertexArray::GetAttribIndex(dirtyBit);
532 if (dirtyBit >= VertexArray::DIRTY_BIT_ATTRIB_0_ENABLED &&
533 dirtyBit < VertexArray::DIRTY_BIT_ATTRIB_MAX_ENABLED)
Jamie Madill0b9e9032015-08-17 11:51:52 +0000534 {
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800535 updateAttribEnabled(index);
Jamie Madill0b9e9032015-08-17 11:51:52 +0000536 }
537 else if (dirtyBit >= VertexArray::DIRTY_BIT_ATTRIB_0_POINTER &&
538 dirtyBit < VertexArray::DIRTY_BIT_ATTRIB_MAX_POINTER)
539 {
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800540 updateAttribPointer(index);
Jamie Madill0b9e9032015-08-17 11:51:52 +0000541 }
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800542 else if (dirtyBit >= VertexArray::DIRTY_BIT_ATTRIB_0_FORMAT &&
543 dirtyBit < VertexArray::DIRTY_BIT_BINDING_MAX_BUFFER)
Jamie Madill0b9e9032015-08-17 11:51:52 +0000544 {
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800545 // TODO(jiawei.shao@intel.com): Vertex Attrib Bindings
546 ASSERT(index == mData.getBindingIndexFromAttribIndex(index));
547 }
548 else if (dirtyBit >= VertexArray::DIRTY_BIT_BINDING_0_DIVISOR &&
549 dirtyBit < VertexArray::DIRTY_BIT_BINDING_MAX_DIVISOR)
550 {
551 updateAttribDivisor(index);
Jamie Madill0b9e9032015-08-17 11:51:52 +0000552 }
553 else
554 UNREACHABLE();
555 }
556}
557
558} // rx