blob: afd1476935d98054b64623546e058abc267fc730 [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}
Jamie Madill0b9e9032015-08-17 11:51:52 +000051} // anonymous namespace
52
Jamie Madill3f572682016-04-26 13:41:36 -040053VertexArrayGL::VertexArrayGL(const VertexArrayState &state,
Jamie Madill77a90c22015-08-11 16:33:17 -040054 const FunctionsGL *functions,
55 StateManagerGL *stateManager)
Jamie Madill3f572682016-04-26 13:41:36 -040056 : VertexArrayImpl(state),
Geoff Langba4c4a82015-02-24 12:38:46 -050057 mFunctions(functions),
58 mStateManager(stateManager),
59 mVertexArrayID(0),
Jamie Madill77a90c22015-08-11 16:33:17 -040060 mAppliedElementArrayBuffer(),
Jiawei-Shao2597fb62016-12-09 16:38:02 +080061 mAppliedBindings(state.getMaxBindings()),
Geoff Lang7c82bc42015-03-09 16:18:08 -040062 mStreamingElementArrayBufferSize(0),
63 mStreamingElementArrayBuffer(0),
64 mStreamingArrayBufferSize(0),
65 mStreamingArrayBuffer(0)
Geoff Langba4c4a82015-02-24 12:38:46 -050066{
67 ASSERT(mFunctions);
68 ASSERT(mStateManager);
69 mFunctions->genVertexArrays(1, &mVertexArrayID);
70
Jiawei-Shao2597fb62016-12-09 16:38:02 +080071 // Set the cached vertex attribute array and vertex attribute binding array size
72 GLuint maxVertexAttribs = static_cast<GLuint>(state.getMaxAttribs());
73 for (GLuint i = 0; i < maxVertexAttribs; i++)
74 {
75 mAppliedAttributes.emplace_back(i);
76 }
Geoff Langba4c4a82015-02-24 12:38:46 -050077}
Geoff Langf9a6f082015-01-22 13:32:49 -050078
Jamie Madill4928b7c2017-06-20 12:57:39 -040079void VertexArrayGL::destroy(const gl::Context *context)
Geoff Langba4c4a82015-02-24 12:38:46 -050080{
Geoff Lang1eb708e2015-05-04 14:58:23 -040081 mStateManager->deleteVertexArray(mVertexArrayID);
82 mVertexArrayID = 0;
Geoff Langba4c4a82015-02-24 12:38:46 -050083
Geoff Lang1eb708e2015-05-04 14:58:23 -040084 mStateManager->deleteBuffer(mStreamingElementArrayBuffer);
85 mStreamingElementArrayBufferSize = 0;
Shao80957d92017-02-20 21:25:59 +080086 mStreamingElementArrayBuffer = 0;
Geoff Lang7c82bc42015-03-09 16:18:08 -040087
Geoff Lang1eb708e2015-05-04 14:58:23 -040088 mStateManager->deleteBuffer(mStreamingArrayBuffer);
89 mStreamingArrayBufferSize = 0;
Shao80957d92017-02-20 21:25:59 +080090 mStreamingArrayBuffer = 0;
Geoff Lang7c82bc42015-03-09 16:18:08 -040091
Jamie Madill4928b7c2017-06-20 12:57:39 -040092 mAppliedElementArrayBuffer.set(context, nullptr);
Jiawei-Shao2597fb62016-12-09 16:38:02 +080093 for (auto &binding : mAppliedBindings)
Geoff Langba4c4a82015-02-24 12:38:46 -050094 {
Jamie Madill4928b7c2017-06-20 12:57:39 -040095 binding.setBuffer(context, nullptr);
Geoff Langba4c4a82015-02-24 12:38:46 -050096 }
97}
Geoff Langf9a6f082015-01-22 13:32:49 -050098
Jamie Madill4928b7c2017-06-20 12:57:39 -040099gl::Error VertexArrayGL::syncDrawArraysState(const gl::Context *context,
100 const gl::AttributesMask &activeAttributesMask,
Jamie Madill0b9e9032015-08-17 11:51:52 +0000101 GLint first,
Geoff Lang3cf12ce2015-08-27 14:40:48 -0400102 GLsizei count,
103 GLsizei instanceCount) const
Geoff Lang7c82bc42015-03-09 16:18:08 -0400104{
Jamie Madill4928b7c2017-06-20 12:57:39 -0400105 return syncDrawState(context, activeAttributesMask, first, count, GL_NONE, nullptr,
106 instanceCount, false, nullptr);
Geoff Lang7c82bc42015-03-09 16:18:08 -0400107}
108
Jamie Madill4928b7c2017-06-20 12:57:39 -0400109gl::Error VertexArrayGL::syncDrawElementsState(const gl::Context *context,
110 const gl::AttributesMask &activeAttributesMask,
Jamie Madill0b9e9032015-08-17 11:51:52 +0000111 GLsizei count,
112 GLenum type,
Jamie Madill876429b2017-04-20 15:46:24 -0400113 const void *indices,
Geoff Lang3cf12ce2015-08-27 14:40:48 -0400114 GLsizei instanceCount,
Geoff Lang3edfe032015-09-04 16:38:24 -0400115 bool primitiveRestartEnabled,
Jamie Madill876429b2017-04-20 15:46:24 -0400116 const void **outIndices) const
Geoff Lang7c82bc42015-03-09 16:18:08 -0400117{
Jamie Madill4928b7c2017-06-20 12:57:39 -0400118 return syncDrawState(context, activeAttributesMask, 0, count, type, indices, instanceCount,
Geoff Lang3edfe032015-09-04 16:38:24 -0400119 primitiveRestartEnabled, outIndices);
Geoff Lang7c82bc42015-03-09 16:18:08 -0400120}
121
Jamie Madill4928b7c2017-06-20 12:57:39 -0400122gl::Error VertexArrayGL::syncElementArrayState(const gl::Context *context) const
Jiajia Qind9671222016-11-29 16:30:31 +0800123{
124 gl::Buffer *elementArrayBuffer = mData.getElementArrayBuffer().get();
125 ASSERT(elementArrayBuffer);
126 if (elementArrayBuffer != mAppliedElementArrayBuffer.get())
127 {
128 const BufferGL *bufferGL = GetImplAs<BufferGL>(elementArrayBuffer);
129 mStateManager->bindBuffer(GL_ELEMENT_ARRAY_BUFFER, bufferGL->getBufferID());
Jamie Madill4928b7c2017-06-20 12:57:39 -0400130 mAppliedElementArrayBuffer.set(context, elementArrayBuffer);
Jiajia Qind9671222016-11-29 16:30:31 +0800131 }
132
133 return gl::NoError();
134}
135
Jamie Madill4928b7c2017-06-20 12:57:39 -0400136gl::Error VertexArrayGL::syncDrawState(const gl::Context *context,
137 const gl::AttributesMask &activeAttributesMask,
Jamie Madill0b9e9032015-08-17 11:51:52 +0000138 GLint first,
139 GLsizei count,
140 GLenum type,
Jamie Madill876429b2017-04-20 15:46:24 -0400141 const void *indices,
Geoff Lang3cf12ce2015-08-27 14:40:48 -0400142 GLsizei instanceCount,
Geoff Lang3edfe032015-09-04 16:38:24 -0400143 bool primitiveRestartEnabled,
Jamie Madill876429b2017-04-20 15:46:24 -0400144 const void **outIndices) const
Geoff Lang6ae6efc2015-03-09 14:42:35 -0400145{
Jamie Madill77a90c22015-08-11 16:33:17 -0400146 mStateManager->bindVertexArray(mVertexArrayID, getAppliedElementArrayBufferID());
Geoff Lang6ae6efc2015-03-09 14:42:35 -0400147
Shao80957d92017-02-20 21:25:59 +0800148 // Check if any attributes need to be streamed, determines if the index range needs to be
149 // computed
Jamie Madill0b9e9032015-08-17 11:51:52 +0000150 bool attributesNeedStreaming = mAttributesNeedStreaming.any();
Geoff Lang7c82bc42015-03-09 16:18:08 -0400151
Shao80957d92017-02-20 21:25:59 +0800152 // Determine if an index buffer needs to be streamed and the range of vertices that need to be
153 // copied
Geoff Lang3edfe032015-09-04 16:38:24 -0400154 IndexRange indexRange;
Geoff Lang7c82bc42015-03-09 16:18:08 -0400155 if (type != GL_NONE)
Geoff Langba4c4a82015-02-24 12:38:46 -0500156 {
Jamie Madill4928b7c2017-06-20 12:57:39 -0400157 ANGLE_TRY(syncIndexData(context, count, type, indices, primitiveRestartEnabled,
158 attributesNeedStreaming, &indexRange, outIndices));
Geoff Lang7c82bc42015-03-09 16:18:08 -0400159 }
160 else
161 {
Corentin Wallez2c34a4b2015-08-25 16:26:02 -0400162 // Not an indexed call, set the range to [first, first + count - 1]
Geoff Lang7c82bc42015-03-09 16:18:08 -0400163 indexRange.start = first;
Shao80957d92017-02-20 21:25:59 +0800164 indexRange.end = first + count - 1;
Geoff Langba4c4a82015-02-24 12:38:46 -0500165 }
166
Jamie Madill0b9e9032015-08-17 11:51:52 +0000167 if (attributesNeedStreaming)
Geoff Langba4c4a82015-02-24 12:38:46 -0500168 {
Jamie Madill4928b7c2017-06-20 12:57:39 -0400169 ANGLE_TRY(streamAttributes(activeAttributesMask, instanceCount, indexRange));
Geoff Lang7c82bc42015-03-09 16:18:08 -0400170 }
171
Yuly Novikovc4d18aa2017-03-09 18:45:02 -0500172 return gl::NoError();
Geoff Lang7c82bc42015-03-09 16:18:08 -0400173}
174
Jamie Madill4928b7c2017-06-20 12:57:39 -0400175gl::Error VertexArrayGL::syncIndexData(const gl::Context *context,
176 GLsizei count,
Jiajia Qind9671222016-11-29 16:30:31 +0800177 GLenum type,
Jamie Madill876429b2017-04-20 15:46:24 -0400178 const void *indices,
Jiajia Qind9671222016-11-29 16:30:31 +0800179 bool primitiveRestartEnabled,
180 bool attributesNeedStreaming,
181 IndexRange *outIndexRange,
Jamie Madill876429b2017-04-20 15:46:24 -0400182 const void **outIndices) const
Geoff Lang7c82bc42015-03-09 16:18:08 -0400183{
184 ASSERT(outIndices);
185
Jamie Madill8e344942015-07-09 14:22:07 -0400186 gl::Buffer *elementArrayBuffer = mData.getElementArrayBuffer().get();
187
Geoff Lang7c82bc42015-03-09 16:18:08 -0400188 // Need to check the range of indices if attributes need to be streamed
Jamie Madill8e344942015-07-09 14:22:07 -0400189 if (elementArrayBuffer != nullptr)
Geoff Lang7c82bc42015-03-09 16:18:08 -0400190 {
Jamie Madill77a90c22015-08-11 16:33:17 -0400191 if (elementArrayBuffer != mAppliedElementArrayBuffer.get())
Geoff Lang7c82bc42015-03-09 16:18:08 -0400192 {
Jamie Madill77a90c22015-08-11 16:33:17 -0400193 const BufferGL *bufferGL = GetImplAs<BufferGL>(elementArrayBuffer);
194 mStateManager->bindBuffer(GL_ELEMENT_ARRAY_BUFFER, bufferGL->getBufferID());
Jamie Madill4928b7c2017-06-20 12:57:39 -0400195 mAppliedElementArrayBuffer.set(context, elementArrayBuffer);
Geoff Lang7c82bc42015-03-09 16:18:08 -0400196 }
197
198 // Only compute the index range if the attributes also need to be streamed
199 if (attributesNeedStreaming)
200 {
201 ptrdiff_t elementArrayBufferOffset = reinterpret_cast<ptrdiff_t>(indices);
Shao80957d92017-02-20 21:25:59 +0800202 Error error = mData.getElementArrayBuffer()->getIndexRange(
Geoff Lang3edfe032015-09-04 16:38:24 -0400203 type, elementArrayBufferOffset, count, primitiveRestartEnabled, outIndexRange);
Geoff Lang520c4ae2015-05-05 13:12:36 -0400204 if (error.isError())
Geoff Lang7c82bc42015-03-09 16:18:08 -0400205 {
Geoff Lang520c4ae2015-05-05 13:12:36 -0400206 return error;
Geoff Lang7c82bc42015-03-09 16:18:08 -0400207 }
208 }
209
Shao80957d92017-02-20 21:25:59 +0800210 // Indices serves as an offset into the index buffer in this case, use the same value for
211 // the draw call
Geoff Lang7c82bc42015-03-09 16:18:08 -0400212 *outIndices = indices;
213 }
214 else
215 {
216 // Need to stream the index buffer
217 // TODO: if GLES, nothing needs to be streamed
218
219 // Only compute the index range if the attributes also need to be streamed
220 if (attributesNeedStreaming)
221 {
Geoff Lang3edfe032015-09-04 16:38:24 -0400222 *outIndexRange = ComputeIndexRange(type, indices, count, primitiveRestartEnabled);
Geoff Lang7c82bc42015-03-09 16:18:08 -0400223 }
224
225 // Allocate the streaming element array buffer
226 if (mStreamingElementArrayBuffer == 0)
227 {
228 mFunctions->genBuffers(1, &mStreamingElementArrayBuffer);
229 mStreamingElementArrayBufferSize = 0;
230 }
231
232 mStateManager->bindBuffer(GL_ELEMENT_ARRAY_BUFFER, mStreamingElementArrayBuffer);
Jamie Madill4928b7c2017-06-20 12:57:39 -0400233 mAppliedElementArrayBuffer.set(context, nullptr);
Geoff Lang7c82bc42015-03-09 16:18:08 -0400234
235 // Make sure the element array buffer is large enough
Jamie Madill0b9e9032015-08-17 11:51:52 +0000236 const Type &indexTypeInfo = GetTypeInfo(type);
Geoff Lang7c82bc42015-03-09 16:18:08 -0400237 size_t requiredStreamingBufferSize = indexTypeInfo.bytes * count;
238 if (requiredStreamingBufferSize > mStreamingElementArrayBufferSize)
239 {
240 // Copy the indices in while resizing the buffer
Shao80957d92017-02-20 21:25:59 +0800241 mFunctions->bufferData(GL_ELEMENT_ARRAY_BUFFER, requiredStreamingBufferSize, indices,
242 GL_DYNAMIC_DRAW);
Geoff Lang7c82bc42015-03-09 16:18:08 -0400243 mStreamingElementArrayBufferSize = requiredStreamingBufferSize;
244 }
245 else
246 {
247 // Put the indices at the beginning of the buffer
Shao80957d92017-02-20 21:25:59 +0800248 mFunctions->bufferSubData(GL_ELEMENT_ARRAY_BUFFER, 0, requiredStreamingBufferSize,
249 indices);
Geoff Lang7c82bc42015-03-09 16:18:08 -0400250 }
251
Shao80957d92017-02-20 21:25:59 +0800252 // Set the index offset for the draw call to zero since the supplied index pointer is to
253 // client data
Geoff Lang7c82bc42015-03-09 16:18:08 -0400254 *outIndices = nullptr;
255 }
256
Yuly Novikovc4d18aa2017-03-09 18:45:02 -0500257 return gl::NoError();
Geoff Lang7c82bc42015-03-09 16:18:08 -0400258}
259
Jamie Madill0b9e9032015-08-17 11:51:52 +0000260void VertexArrayGL::computeStreamingAttributeSizes(const gl::AttributesMask &activeAttributesMask,
Geoff Lang3cf12ce2015-08-27 14:40:48 -0400261 GLsizei instanceCount,
Geoff Lang3edfe032015-09-04 16:38:24 -0400262 const gl::IndexRange &indexRange,
Jamie Madill0b9e9032015-08-17 11:51:52 +0000263 size_t *outStreamingDataSize,
264 size_t *outMaxAttributeDataSize) const
Geoff Lang7c82bc42015-03-09 16:18:08 -0400265{
Jamie Madill0b9e9032015-08-17 11:51:52 +0000266 *outStreamingDataSize = 0;
267 *outMaxAttributeDataSize = 0;
268
269 ASSERT(mAttributesNeedStreaming.any());
270
Shao80957d92017-02-20 21:25:59 +0800271 const auto &attribs = mData.getVertexAttributes();
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800272 const auto &bindings = mData.getVertexBindings();
Jamie Madill6de51852017-04-12 09:53:01 -0400273
274 gl::AttributesMask attribsToStream = (mAttributesNeedStreaming & activeAttributesMask);
275
276 for (auto idx : attribsToStream)
Jamie Madill0b9e9032015-08-17 11:51:52 +0000277 {
Shao80957d92017-02-20 21:25:59 +0800278 const auto &attrib = attribs[idx];
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800279 const auto &binding = bindings[attrib.bindingIndex];
280 ASSERT(AttributeNeedsStreaming(attrib, binding));
Jamie Madill0b9e9032015-08-17 11:51:52 +0000281
Jamie Madill0b9e9032015-08-17 11:51:52 +0000282 // If streaming is going to be required, compute the size of the required buffer
283 // and how much slack space at the beginning of the buffer will be required by determining
284 // the attribute with the largest data size.
285 size_t typeSize = ComputeVertexAttributeTypeSize(attrib);
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800286 *outStreamingDataSize += typeSize * ComputeVertexBindingElementCount(
287 binding, indexRange.vertexCount(), instanceCount);
Jamie Madill0b9e9032015-08-17 11:51:52 +0000288 *outMaxAttributeDataSize = std::max(*outMaxAttributeDataSize, typeSize);
289 }
290}
291
292gl::Error VertexArrayGL::streamAttributes(const gl::AttributesMask &activeAttributesMask,
Geoff Lang3cf12ce2015-08-27 14:40:48 -0400293 GLsizei instanceCount,
Geoff Lang3edfe032015-09-04 16:38:24 -0400294 const gl::IndexRange &indexRange) const
Jamie Madill0b9e9032015-08-17 11:51:52 +0000295{
296 // Sync the vertex attribute state and track what data needs to be streamed
297 size_t streamingDataSize = 0;
298 size_t maxAttributeDataSize = 0;
299
Geoff Lang3cf12ce2015-08-27 14:40:48 -0400300 computeStreamingAttributeSizes(activeAttributesMask, instanceCount, indexRange,
301 &streamingDataSize, &maxAttributeDataSize);
Jamie Madill0b9e9032015-08-17 11:51:52 +0000302
303 if (streamingDataSize == 0)
304 {
He Yunchaoacd18982017-01-04 10:46:42 +0800305 return gl::NoError();
Jamie Madill0b9e9032015-08-17 11:51:52 +0000306 }
307
Geoff Lang7c82bc42015-03-09 16:18:08 -0400308 if (mStreamingArrayBuffer == 0)
309 {
310 mFunctions->genBuffers(1, &mStreamingArrayBuffer);
311 mStreamingArrayBufferSize = 0;
312 }
313
Shao80957d92017-02-20 21:25:59 +0800314 // If first is greater than zero, a slack space needs to be left at the beginning of the buffer
315 // so that the same 'first' argument can be passed into the draw call.
316 const size_t bufferEmptySpace = maxAttributeDataSize * indexRange.start;
Geoff Lang7c82bc42015-03-09 16:18:08 -0400317 const size_t requiredBufferSize = streamingDataSize + bufferEmptySpace;
318
319 mStateManager->bindBuffer(GL_ARRAY_BUFFER, mStreamingArrayBuffer);
320 if (requiredBufferSize > mStreamingArrayBufferSize)
321 {
322 mFunctions->bufferData(GL_ARRAY_BUFFER, requiredBufferSize, nullptr, GL_DYNAMIC_DRAW);
323 mStreamingArrayBufferSize = requiredBufferSize;
324 }
325
326 // Unmapping a buffer can return GL_FALSE to indicate that the system has corrupted the data
Shao80957d92017-02-20 21:25:59 +0800327 // somehow (such as by a screen change), retry writing the data a few times and return
328 // OUT_OF_MEMORY if that fails.
329 GLboolean unmapResult = GL_FALSE;
Geoff Lang7c82bc42015-03-09 16:18:08 -0400330 size_t unmapRetryAttempts = 5;
331 while (unmapResult != GL_TRUE && --unmapRetryAttempts > 0)
332 {
Geoff Langb2ebb5e2016-06-08 18:17:55 +0000333 uint8_t *bufferPointer = MapBufferRangeWithFallback(mFunctions, GL_ARRAY_BUFFER, 0,
334 requiredBufferSize, GL_MAP_WRITE_BIT);
Geoff Lang7c82bc42015-03-09 16:18:08 -0400335 size_t curBufferOffset = bufferEmptySpace;
336
Shao80957d92017-02-20 21:25:59 +0800337 const auto &attribs = mData.getVertexAttributes();
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800338 const auto &bindings = mData.getVertexBindings();
Jamie Madill6de51852017-04-12 09:53:01 -0400339
340 gl::AttributesMask attribsToStream = (mAttributesNeedStreaming & activeAttributesMask);
341
342 for (auto idx : attribsToStream)
Geoff Lang7c82bc42015-03-09 16:18:08 -0400343 {
Shao80957d92017-02-20 21:25:59 +0800344 const auto &attrib = attribs[idx];
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800345 const auto &binding = bindings[attrib.bindingIndex];
346 ASSERT(AttributeNeedsStreaming(attrib, binding));
Geoff Lang7c82bc42015-03-09 16:18:08 -0400347
Geoff Lang3cf12ce2015-08-27 14:40:48 -0400348 const size_t streamedVertexCount =
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800349 ComputeVertexBindingElementCount(binding, indexRange.vertexCount(), instanceCount);
Geoff Lang3cf12ce2015-08-27 14:40:48 -0400350
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800351 const size_t sourceStride = ComputeVertexAttributeStride(attrib, binding);
Jamie Madill0b9e9032015-08-17 11:51:52 +0000352 const size_t destStride = ComputeVertexAttributeTypeSize(attrib);
353
Geoff Lang38a24a92017-02-15 13:53:06 -0500354 // Vertices do not apply the 'start' offset when the divisor is non-zero even when doing
355 // a non-instanced draw call
Martin Radevdd5f27e2017-06-07 10:17:09 +0300356 const size_t firstIndex = binding.getDivisor() == 0 ? indexRange.start : 0;
Geoff Lang38a24a92017-02-15 13:53:06 -0500357
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800358 // Attributes using client memory ignore the VERTEX_ATTRIB_BINDING state.
359 // https://www.opengl.org/registry/specs/ARB/vertex_attrib_binding.txt
Jamie Madill0b9e9032015-08-17 11:51:52 +0000360 const uint8_t *inputPointer = reinterpret_cast<const uint8_t *>(attrib.pointer);
361
362 // Pack the data when copying it, user could have supplied a very large stride that
363 // would cause the buffer to be much larger than needed.
364 if (destStride == sourceStride)
Jamie Madill8e344942015-07-09 14:22:07 -0400365 {
Jamie Madill0b9e9032015-08-17 11:51:52 +0000366 // Can copy in one go, the data is packed
Geoff Lang38a24a92017-02-15 13:53:06 -0500367 memcpy(bufferPointer + curBufferOffset, inputPointer + (sourceStride * firstIndex),
Jamie Madill0b9e9032015-08-17 11:51:52 +0000368 destStride * streamedVertexCount);
Jamie Madill6d51c702015-08-14 10:38:10 -0400369 }
Jamie Madill0b9e9032015-08-17 11:51:52 +0000370 else
371 {
372 // Copy each vertex individually
Geoff Lang6b10ddb2015-09-02 15:55:10 -0400373 for (size_t vertexIdx = 0; vertexIdx < streamedVertexCount; vertexIdx++)
Jamie Madill0b9e9032015-08-17 11:51:52 +0000374 {
Shao80957d92017-02-20 21:25:59 +0800375 uint8_t *out = bufferPointer + curBufferOffset + (destStride * vertexIdx);
Geoff Lang38a24a92017-02-15 13:53:06 -0500376 const uint8_t *in = inputPointer + sourceStride * (vertexIdx + firstIndex);
Corentin Wallez4d5362d2015-08-25 11:25:14 -0400377 memcpy(out, in, destStride);
Jamie Madill0b9e9032015-08-17 11:51:52 +0000378 }
379 }
380
381 // Compute where the 0-index vertex would be.
Geoff Lang38a24a92017-02-15 13:53:06 -0500382 const size_t vertexStartOffset = curBufferOffset - (firstIndex * destStride);
Jamie Madill0b9e9032015-08-17 11:51:52 +0000383
Shaodf682a82017-03-31 15:13:21 +0800384 callVertexAttribPointer(static_cast<GLuint>(idx), attrib,
385 static_cast<GLsizei>(destStride),
386 static_cast<GLintptr>(vertexStartOffset));
Jamie Madill0b9e9032015-08-17 11:51:52 +0000387
388 curBufferOffset += destStride * streamedVertexCount;
Geoff Lang7c82bc42015-03-09 16:18:08 -0400389 }
390
391 unmapResult = mFunctions->unmapBuffer(GL_ARRAY_BUFFER);
392 }
393
394 if (unmapResult != GL_TRUE)
395 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -0500396 return gl::OutOfMemory() << "Failed to unmap the client data streaming buffer.";
Geoff Lang7c82bc42015-03-09 16:18:08 -0400397 }
398
Yuly Novikovc4d18aa2017-03-09 18:45:02 -0500399 return gl::NoError();
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
Geoff Lang294cad92015-05-26 15:11:23 -0400407GLuint VertexArrayGL::getAppliedElementArrayBufferID() const
408{
Jamie Madill77a90c22015-08-11 16:33:17 -0400409 if (mAppliedElementArrayBuffer.get() == nullptr)
410 {
411 return mStreamingElementArrayBuffer;
412 }
413
414 return GetImplAs<BufferGL>(mAppliedElementArrayBuffer.get())->getBufferID();
Geoff Lang294cad92015-05-26 15:11:23 -0400415}
416
Jamie Madill0b9e9032015-08-17 11:51:52 +0000417void VertexArrayGL::updateNeedsStreaming(size_t attribIndex)
418{
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800419 const auto &attrib = mData.getVertexAttribute(attribIndex);
420 const auto &binding = mData.getBindingFromAttribIndex(attribIndex);
421 mAttributesNeedStreaming.set(attribIndex, AttributeNeedsStreaming(attrib, binding));
Geoff Langf9a6f082015-01-22 13:32:49 -0500422}
Jamie Madill0b9e9032015-08-17 11:51:52 +0000423
424void VertexArrayGL::updateAttribEnabled(size_t attribIndex)
425{
Shaodf682a82017-03-31 15:13:21 +0800426 const bool enabled = mData.getVertexAttribute(attribIndex).enabled;
427 if (mAppliedAttributes[attribIndex].enabled == enabled)
Jamie Madill0b9e9032015-08-17 11:51:52 +0000428 {
429 return;
430 }
431
432 updateNeedsStreaming(attribIndex);
433
Shaodf682a82017-03-31 15:13:21 +0800434 if (enabled)
Jamie Madill0b9e9032015-08-17 11:51:52 +0000435 {
436 mFunctions->enableVertexAttribArray(static_cast<GLuint>(attribIndex));
437 }
438 else
439 {
440 mFunctions->disableVertexAttribArray(static_cast<GLuint>(attribIndex));
441 }
Shaodf682a82017-03-31 15:13:21 +0800442
443 mAppliedAttributes[attribIndex].enabled = enabled;
Jamie Madill0b9e9032015-08-17 11:51:52 +0000444}
445
Jamie Madill4928b7c2017-06-20 12:57:39 -0400446void VertexArrayGL::updateAttribPointer(const gl::Context *context, size_t attribIndex)
Jamie Madill0b9e9032015-08-17 11:51:52 +0000447{
448 const VertexAttribute &attrib = mData.getVertexAttribute(attribIndex);
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800449
Shaodf682a82017-03-31 15:13:21 +0800450 // TODO(jiawei.shao@intel.com): Vertex Attrib Binding
451 ASSERT(IsVertexAttribPointerSupported(attribIndex, attrib));
452
453 const GLuint bindingIndex = attrib.bindingIndex;
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800454 const VertexBinding &binding = mData.getVertexBinding(bindingIndex);
455
Shaodf682a82017-03-31 15:13:21 +0800456 // We do not need to compare attrib.pointer because when we use a different client memory
457 // pointer, we don't need to update mAttributesNeedStreaming by binding.buffer and we won't
458 // update attribPointer in this function.
459 if (SameVertexAttribFormat(mAppliedAttributes[attribIndex], attrib) &&
460 mAppliedAttributes[attribIndex].bindingIndex == bindingIndex &&
461 SameVertexBuffer(mAppliedBindings[attribIndex], binding))
Jamie Madill0b9e9032015-08-17 11:51:52 +0000462 {
463 return;
464 }
465
466 updateNeedsStreaming(attribIndex);
Jamie Madill0b9e9032015-08-17 11:51:52 +0000467
468 // If we need to stream, defer the attribPointer to the draw call.
Shao4181bc92017-03-17 14:55:25 +0800469 // Skip the attribute that is disabled and uses a client memory pointer.
Martin Radevdd5f27e2017-06-07 10:17:09 +0300470 const Buffer *arrayBuffer = binding.getBuffer().get();
Shao4181bc92017-03-17 14:55:25 +0800471 if (arrayBuffer == nullptr)
Jamie Madill0b9e9032015-08-17 11:51:52 +0000472 {
Shaodf682a82017-03-31 15:13:21 +0800473 // Mark the applied binding is using a client memory pointer by setting its buffer to
474 // nullptr so that if it doesn't use a client memory pointer later, there is no chance that
475 // the caching will skip it.
Jamie Madill4928b7c2017-06-20 12:57:39 -0400476 mAppliedBindings[bindingIndex].setBuffer(context, nullptr);
Shao4181bc92017-03-17 14:55:25 +0800477 return;
Jamie Madill0b9e9032015-08-17 11:51:52 +0000478 }
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800479
Shao4181bc92017-03-17 14:55:25 +0800480 // Since ANGLE always uses a non-zero VAO, we cannot use a client memory pointer on it:
481 // [OpenGL ES 3.0.2] Section 2.8 page 24:
482 // An INVALID_OPERATION error is generated when a non-zero vertex array object is bound,
483 // zero is bound to the ARRAY_BUFFER buffer object binding point, and the pointer argument
484 // is not NULL.
Shaodf682a82017-03-31 15:13:21 +0800485
Shao4181bc92017-03-17 14:55:25 +0800486 const BufferGL *arrayBufferGL = GetImplAs<BufferGL>(arrayBuffer);
487 mStateManager->bindBuffer(GL_ARRAY_BUFFER, arrayBufferGL->getBufferID());
Jamie Madill0b9e9032015-08-17 11:51:52 +0000488
Martin Radevdd5f27e2017-06-07 10:17:09 +0300489 callVertexAttribPointer(static_cast<GLuint>(attribIndex), attrib, binding.getStride(),
490 binding.getOffset());
Shaodf682a82017-03-31 15:13:21 +0800491
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800492 mAppliedAttributes[attribIndex].size = attrib.size;
493 mAppliedAttributes[attribIndex].type = attrib.type;
494 mAppliedAttributes[attribIndex].normalized = attrib.normalized;
495 mAppliedAttributes[attribIndex].pureInteger = attrib.pureInteger;
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800496 mAppliedAttributes[attribIndex].relativeOffset = attrib.relativeOffset;
Shaodf682a82017-03-31 15:13:21 +0800497
498 mAppliedAttributes[attribIndex].bindingIndex = bindingIndex;
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800499
Martin Radevdd5f27e2017-06-07 10:17:09 +0300500 mAppliedBindings[bindingIndex].setStride(binding.getStride());
501 mAppliedBindings[bindingIndex].setOffset(binding.getOffset());
Jamie Madill4928b7c2017-06-20 12:57:39 -0400502 mAppliedBindings[bindingIndex].setBuffer(context, binding.getBuffer().get());
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800503}
504
Shaodf682a82017-03-31 15:13:21 +0800505void VertexArrayGL::callVertexAttribPointer(GLuint attribIndex,
506 const VertexAttribute &attrib,
507 GLsizei stride,
508 GLintptr offset) const
509{
510 const GLvoid *pointer = reinterpret_cast<const GLvoid *>(offset);
511 if (attrib.pureInteger)
512 {
513 ASSERT(!attrib.normalized);
514 mFunctions->vertexAttribIPointer(attribIndex, attrib.size, attrib.type, stride, pointer);
515 }
516 else
517 {
518 mFunctions->vertexAttribPointer(attribIndex, attrib.size, attrib.type, attrib.normalized,
519 stride, pointer);
520 }
521}
522
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800523void VertexArrayGL::updateAttribDivisor(size_t attribIndex)
524{
Shaodf682a82017-03-31 15:13:21 +0800525 const GLuint bindingIndex = mData.getVertexAttribute(attribIndex).bindingIndex;
526 ASSERT(attribIndex == bindingIndex);
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800527
Martin Radevdd5f27e2017-06-07 10:17:09 +0300528 const GLuint divisor = mData.getVertexBinding(bindingIndex).getDivisor();
Shaodf682a82017-03-31 15:13:21 +0800529 if (mAppliedAttributes[attribIndex].bindingIndex == bindingIndex &&
Martin Radevdd5f27e2017-06-07 10:17:09 +0300530 mAppliedBindings[bindingIndex].getDivisor() == divisor)
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800531 {
Shaodf682a82017-03-31 15:13:21 +0800532 return;
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800533 }
Shaodf682a82017-03-31 15:13:21 +0800534
535 mFunctions->vertexAttribDivisor(static_cast<GLuint>(attribIndex), divisor);
536
537 mAppliedAttributes[attribIndex].bindingIndex = bindingIndex;
Martin Radevdd5f27e2017-06-07 10:17:09 +0300538 mAppliedBindings[bindingIndex].setDivisor(divisor);
Jamie Madill0b9e9032015-08-17 11:51:52 +0000539}
540
Jamie Madillc564c072017-06-01 12:45:42 -0400541void VertexArrayGL::syncState(const gl::Context *context, const VertexArray::DirtyBits &dirtyBits)
Jamie Madill0b9e9032015-08-17 11:51:52 +0000542{
Shaodf682a82017-03-31 15:13:21 +0800543 mStateManager->bindVertexArray(mVertexArrayID, getAppliedElementArrayBufferID());
544
Jamie Madill6de51852017-04-12 09:53:01 -0400545 for (size_t dirtyBit : dirtyBits)
Jamie Madill0b9e9032015-08-17 11:51:52 +0000546 {
547 if (dirtyBit == VertexArray::DIRTY_BIT_ELEMENT_ARRAY_BUFFER)
548 {
549 // TODO(jmadill): Element array buffer bindings
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800550 continue;
Jamie Madill0b9e9032015-08-17 11:51:52 +0000551 }
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800552
553 size_t index = VertexArray::GetAttribIndex(dirtyBit);
554 if (dirtyBit >= VertexArray::DIRTY_BIT_ATTRIB_0_ENABLED &&
555 dirtyBit < VertexArray::DIRTY_BIT_ATTRIB_MAX_ENABLED)
Jamie Madill0b9e9032015-08-17 11:51:52 +0000556 {
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800557 updateAttribEnabled(index);
Jamie Madill0b9e9032015-08-17 11:51:52 +0000558 }
559 else if (dirtyBit >= VertexArray::DIRTY_BIT_ATTRIB_0_POINTER &&
560 dirtyBit < VertexArray::DIRTY_BIT_ATTRIB_MAX_POINTER)
561 {
Jamie Madill4928b7c2017-06-20 12:57:39 -0400562 updateAttribPointer(context, index);
Jamie Madill0b9e9032015-08-17 11:51:52 +0000563 }
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800564 else if (dirtyBit >= VertexArray::DIRTY_BIT_ATTRIB_0_FORMAT &&
565 dirtyBit < VertexArray::DIRTY_BIT_BINDING_MAX_BUFFER)
Jamie Madill0b9e9032015-08-17 11:51:52 +0000566 {
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800567 // TODO(jiawei.shao@intel.com): Vertex Attrib Bindings
568 ASSERT(index == mData.getBindingIndexFromAttribIndex(index));
569 }
570 else if (dirtyBit >= VertexArray::DIRTY_BIT_BINDING_0_DIVISOR &&
571 dirtyBit < VertexArray::DIRTY_BIT_BINDING_MAX_DIVISOR)
572 {
573 updateAttribDivisor(index);
Jamie Madill0b9e9032015-08-17 11:51:52 +0000574 }
575 else
576 UNREACHABLE();
577 }
578}
579
580} // rx