blob: 43da1ca03a7b3962b22fcbaddf18aecd63808d48 [file] [log] [blame]
Geoff Langf9a6f082015-01-22 13:32:49 -05001//
2// Copyright 2015 The ANGLE Project Authors. All rights reserved.
3// Use of this source code is governed by a BSD-style license that can be
4// found in the LICENSE file.
5//
6
7// VertexArrayGL.cpp: Implements the class methods for VertexArrayGL.
8
9#include "libANGLE/renderer/gl/VertexArrayGL.h"
10
Jamie Madill20e005b2017-04-07 14:19:22 -040011#include "common/bitset_utils.h"
Geoff Langf9a6f082015-01-22 13:32:49 -050012#include "common/debug.h"
Geoff Lang7c82bc42015-03-09 16:18:08 -040013#include "common/mathutil.h"
Geoff Lang831b1952015-05-05 11:02:27 -040014#include "common/utilities.h"
Geoff Lang6ae6efc2015-03-09 14:42:35 -040015#include "libANGLE/Buffer.h"
Geoff Langba4c4a82015-02-24 12:38:46 -050016#include "libANGLE/angletypes.h"
Geoff Lang7c82bc42015-03-09 16:18:08 -040017#include "libANGLE/formatutils.h"
Geoff Langba4c4a82015-02-24 12:38:46 -050018#include "libANGLE/renderer/gl/BufferGL.h"
19#include "libANGLE/renderer/gl/FunctionsGL.h"
20#include "libANGLE/renderer/gl/StateManagerGL.h"
Jamie Madill231c7f52017-04-26 13:45:37 -040021#include "libANGLE/renderer/gl/renderergl_utils.h"
Geoff Langf9a6f082015-01-22 13:32:49 -050022
Jamie Madill0b9e9032015-08-17 11:51:52 +000023using namespace gl;
24
Geoff Langf9a6f082015-01-22 13:32:49 -050025namespace rx
26{
Jamie Madill0b9e9032015-08-17 11:51:52 +000027namespace
28{
Jiawei-Shao2597fb62016-12-09 16:38:02 +080029// Warning: you should ensure binding really matches attrib.bindingIndex before using this function.
30bool AttributeNeedsStreaming(const VertexAttribute &attrib, const VertexBinding &binding)
Jamie Madill0b9e9032015-08-17 11:51:52 +000031{
Martin Radevdd5f27e2017-06-07 10:17:09 +030032 return (attrib.enabled && binding.getBuffer().get() == nullptr);
Jamie Madill0b9e9032015-08-17 11:51:52 +000033}
Shaodf682a82017-03-31 15:13:21 +080034
35bool SameVertexAttribFormat(const VertexAttribute &a, const VertexAttribute &b)
36{
37 return a.size == b.size && a.type == b.type && a.normalized == b.normalized &&
38 a.pureInteger == b.pureInteger && a.relativeOffset == b.relativeOffset;
39}
40
41bool SameVertexBuffer(const VertexBinding &a, const VertexBinding &b)
42{
Martin Radevdd5f27e2017-06-07 10:17:09 +030043 return a.getStride() == b.getStride() && a.getOffset() == b.getOffset() &&
44 a.getBuffer().get() == b.getBuffer().get();
Shaodf682a82017-03-31 15:13:21 +080045}
46
47bool IsVertexAttribPointerSupported(size_t attribIndex, const VertexAttribute &attrib)
48{
49 return (attribIndex == attrib.bindingIndex && attrib.relativeOffset == 0);
50}
Martin Radev553590a2017-07-31 16:40:39 +030051
52GLuint GetAdjustedDivisor(GLuint numViews, GLuint divisor)
53{
54 return numViews * divisor;
55}
56
Jamie Madill0b9e9032015-08-17 11:51:52 +000057} // anonymous namespace
58
Jamie Madill3f572682016-04-26 13:41:36 -040059VertexArrayGL::VertexArrayGL(const VertexArrayState &state,
Jamie Madill77a90c22015-08-11 16:33:17 -040060 const FunctionsGL *functions,
61 StateManagerGL *stateManager)
Jamie Madill3f572682016-04-26 13:41:36 -040062 : VertexArrayImpl(state),
Geoff Langba4c4a82015-02-24 12:38:46 -050063 mFunctions(functions),
64 mStateManager(stateManager),
65 mVertexArrayID(0),
Martin Radev553590a2017-07-31 16:40:39 +030066 mAppliedNumViews(1),
Jamie Madill77a90c22015-08-11 16:33:17 -040067 mAppliedElementArrayBuffer(),
Jiawei-Shao2597fb62016-12-09 16:38:02 +080068 mAppliedBindings(state.getMaxBindings()),
Geoff Lang7c82bc42015-03-09 16:18:08 -040069 mStreamingElementArrayBufferSize(0),
70 mStreamingElementArrayBuffer(0),
71 mStreamingArrayBufferSize(0),
72 mStreamingArrayBuffer(0)
Geoff Langba4c4a82015-02-24 12:38:46 -050073{
74 ASSERT(mFunctions);
75 ASSERT(mStateManager);
76 mFunctions->genVertexArrays(1, &mVertexArrayID);
77
Jiawei-Shao2597fb62016-12-09 16:38:02 +080078 // Set the cached vertex attribute array and vertex attribute binding array size
79 GLuint maxVertexAttribs = static_cast<GLuint>(state.getMaxAttribs());
80 for (GLuint i = 0; i < maxVertexAttribs; i++)
81 {
82 mAppliedAttributes.emplace_back(i);
83 }
Geoff Langba4c4a82015-02-24 12:38:46 -050084}
Geoff Langf9a6f082015-01-22 13:32:49 -050085
Jamie Madill4928b7c2017-06-20 12:57:39 -040086void VertexArrayGL::destroy(const gl::Context *context)
Geoff Langba4c4a82015-02-24 12:38:46 -050087{
Geoff Lang1eb708e2015-05-04 14:58:23 -040088 mStateManager->deleteVertexArray(mVertexArrayID);
89 mVertexArrayID = 0;
Martin Radev553590a2017-07-31 16:40:39 +030090 mAppliedNumViews = 1;
Geoff Langba4c4a82015-02-24 12:38:46 -050091
Geoff Lang1eb708e2015-05-04 14:58:23 -040092 mStateManager->deleteBuffer(mStreamingElementArrayBuffer);
93 mStreamingElementArrayBufferSize = 0;
Shao80957d92017-02-20 21:25:59 +080094 mStreamingElementArrayBuffer = 0;
Geoff Lang7c82bc42015-03-09 16:18:08 -040095
Geoff Lang1eb708e2015-05-04 14:58:23 -040096 mStateManager->deleteBuffer(mStreamingArrayBuffer);
97 mStreamingArrayBufferSize = 0;
Shao80957d92017-02-20 21:25:59 +080098 mStreamingArrayBuffer = 0;
Geoff Lang7c82bc42015-03-09 16:18:08 -040099
Jamie Madill4928b7c2017-06-20 12:57:39 -0400100 mAppliedElementArrayBuffer.set(context, nullptr);
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800101 for (auto &binding : mAppliedBindings)
Geoff Langba4c4a82015-02-24 12:38:46 -0500102 {
Jamie Madill4928b7c2017-06-20 12:57:39 -0400103 binding.setBuffer(context, nullptr);
Geoff Langba4c4a82015-02-24 12:38:46 -0500104 }
105}
Geoff Langf9a6f082015-01-22 13:32:49 -0500106
Jamie Madill4928b7c2017-06-20 12:57:39 -0400107gl::Error VertexArrayGL::syncDrawArraysState(const gl::Context *context,
108 const gl::AttributesMask &activeAttributesMask,
Jamie Madill0b9e9032015-08-17 11:51:52 +0000109 GLint first,
Geoff Lang3cf12ce2015-08-27 14:40:48 -0400110 GLsizei count,
111 GLsizei instanceCount) const
Geoff Lang7c82bc42015-03-09 16:18:08 -0400112{
Jamie Madill4928b7c2017-06-20 12:57:39 -0400113 return syncDrawState(context, activeAttributesMask, first, count, GL_NONE, nullptr,
114 instanceCount, false, nullptr);
Geoff Lang7c82bc42015-03-09 16:18:08 -0400115}
116
Jamie Madill4928b7c2017-06-20 12:57:39 -0400117gl::Error VertexArrayGL::syncDrawElementsState(const gl::Context *context,
118 const gl::AttributesMask &activeAttributesMask,
Jamie Madill0b9e9032015-08-17 11:51:52 +0000119 GLsizei count,
120 GLenum type,
Jamie Madill876429b2017-04-20 15:46:24 -0400121 const void *indices,
Geoff Lang3cf12ce2015-08-27 14:40:48 -0400122 GLsizei instanceCount,
Geoff Lang3edfe032015-09-04 16:38:24 -0400123 bool primitiveRestartEnabled,
Jamie Madill876429b2017-04-20 15:46:24 -0400124 const void **outIndices) const
Geoff Lang7c82bc42015-03-09 16:18:08 -0400125{
Jamie Madill4928b7c2017-06-20 12:57:39 -0400126 return syncDrawState(context, activeAttributesMask, 0, count, type, indices, instanceCount,
Geoff Lang3edfe032015-09-04 16:38:24 -0400127 primitiveRestartEnabled, outIndices);
Geoff Lang7c82bc42015-03-09 16:18:08 -0400128}
129
Jamie Madill4928b7c2017-06-20 12:57:39 -0400130gl::Error VertexArrayGL::syncElementArrayState(const gl::Context *context) const
Jiajia Qind9671222016-11-29 16:30:31 +0800131{
132 gl::Buffer *elementArrayBuffer = mData.getElementArrayBuffer().get();
133 ASSERT(elementArrayBuffer);
134 if (elementArrayBuffer != mAppliedElementArrayBuffer.get())
135 {
136 const BufferGL *bufferGL = GetImplAs<BufferGL>(elementArrayBuffer);
137 mStateManager->bindBuffer(GL_ELEMENT_ARRAY_BUFFER, bufferGL->getBufferID());
Jamie Madill4928b7c2017-06-20 12:57:39 -0400138 mAppliedElementArrayBuffer.set(context, elementArrayBuffer);
Jiajia Qind9671222016-11-29 16:30:31 +0800139 }
140
141 return gl::NoError();
142}
143
Jamie Madill4928b7c2017-06-20 12:57:39 -0400144gl::Error VertexArrayGL::syncDrawState(const gl::Context *context,
145 const gl::AttributesMask &activeAttributesMask,
Jamie Madill0b9e9032015-08-17 11:51:52 +0000146 GLint first,
147 GLsizei count,
148 GLenum type,
Jamie Madill876429b2017-04-20 15:46:24 -0400149 const void *indices,
Geoff Lang3cf12ce2015-08-27 14:40:48 -0400150 GLsizei instanceCount,
Geoff Lang3edfe032015-09-04 16:38:24 -0400151 bool primitiveRestartEnabled,
Jamie Madill876429b2017-04-20 15:46:24 -0400152 const void **outIndices) const
Geoff Lang6ae6efc2015-03-09 14:42:35 -0400153{
Jamie Madill77a90c22015-08-11 16:33:17 -0400154 mStateManager->bindVertexArray(mVertexArrayID, getAppliedElementArrayBufferID());
Geoff Lang6ae6efc2015-03-09 14:42:35 -0400155
Shao80957d92017-02-20 21:25:59 +0800156 // Check if any attributes need to be streamed, determines if the index range needs to be
157 // computed
Jamie Madill0b9e9032015-08-17 11:51:52 +0000158 bool attributesNeedStreaming = mAttributesNeedStreaming.any();
Geoff Lang7c82bc42015-03-09 16:18:08 -0400159
Shao80957d92017-02-20 21:25:59 +0800160 // Determine if an index buffer needs to be streamed and the range of vertices that need to be
161 // copied
Geoff Lang3edfe032015-09-04 16:38:24 -0400162 IndexRange indexRange;
Geoff Lang7c82bc42015-03-09 16:18:08 -0400163 if (type != GL_NONE)
Geoff Langba4c4a82015-02-24 12:38:46 -0500164 {
Jamie Madill4928b7c2017-06-20 12:57:39 -0400165 ANGLE_TRY(syncIndexData(context, count, type, indices, primitiveRestartEnabled,
166 attributesNeedStreaming, &indexRange, outIndices));
Geoff Lang7c82bc42015-03-09 16:18:08 -0400167 }
168 else
169 {
Corentin Wallez2c34a4b2015-08-25 16:26:02 -0400170 // Not an indexed call, set the range to [first, first + count - 1]
Geoff Lang7c82bc42015-03-09 16:18:08 -0400171 indexRange.start = first;
Shao80957d92017-02-20 21:25:59 +0800172 indexRange.end = first + count - 1;
Geoff Langba4c4a82015-02-24 12:38:46 -0500173 }
174
Jamie Madill0b9e9032015-08-17 11:51:52 +0000175 if (attributesNeedStreaming)
Geoff Langba4c4a82015-02-24 12:38:46 -0500176 {
Jamie Madill4928b7c2017-06-20 12:57:39 -0400177 ANGLE_TRY(streamAttributes(activeAttributesMask, instanceCount, indexRange));
Geoff Lang7c82bc42015-03-09 16:18:08 -0400178 }
179
Yuly Novikovc4d18aa2017-03-09 18:45:02 -0500180 return gl::NoError();
Geoff Lang7c82bc42015-03-09 16:18:08 -0400181}
182
Jamie Madill4928b7c2017-06-20 12:57:39 -0400183gl::Error VertexArrayGL::syncIndexData(const gl::Context *context,
184 GLsizei count,
Jiajia Qind9671222016-11-29 16:30:31 +0800185 GLenum type,
Jamie Madill876429b2017-04-20 15:46:24 -0400186 const void *indices,
Jiajia Qind9671222016-11-29 16:30:31 +0800187 bool primitiveRestartEnabled,
188 bool attributesNeedStreaming,
189 IndexRange *outIndexRange,
Jamie Madill876429b2017-04-20 15:46:24 -0400190 const void **outIndices) const
Geoff Lang7c82bc42015-03-09 16:18:08 -0400191{
192 ASSERT(outIndices);
193
Jamie Madill8e344942015-07-09 14:22:07 -0400194 gl::Buffer *elementArrayBuffer = mData.getElementArrayBuffer().get();
195
Geoff Lang7c82bc42015-03-09 16:18:08 -0400196 // Need to check the range of indices if attributes need to be streamed
Jamie Madill8e344942015-07-09 14:22:07 -0400197 if (elementArrayBuffer != nullptr)
Geoff Lang7c82bc42015-03-09 16:18:08 -0400198 {
Jamie Madill77a90c22015-08-11 16:33:17 -0400199 if (elementArrayBuffer != mAppliedElementArrayBuffer.get())
Geoff Lang7c82bc42015-03-09 16:18:08 -0400200 {
Jamie Madill77a90c22015-08-11 16:33:17 -0400201 const BufferGL *bufferGL = GetImplAs<BufferGL>(elementArrayBuffer);
202 mStateManager->bindBuffer(GL_ELEMENT_ARRAY_BUFFER, bufferGL->getBufferID());
Jamie Madill4928b7c2017-06-20 12:57:39 -0400203 mAppliedElementArrayBuffer.set(context, elementArrayBuffer);
Geoff Lang7c82bc42015-03-09 16:18:08 -0400204 }
205
206 // Only compute the index range if the attributes also need to be streamed
207 if (attributesNeedStreaming)
208 {
209 ptrdiff_t elementArrayBufferOffset = reinterpret_cast<ptrdiff_t>(indices);
Shao80957d92017-02-20 21:25:59 +0800210 Error error = mData.getElementArrayBuffer()->getIndexRange(
Geoff Lang3edfe032015-09-04 16:38:24 -0400211 type, elementArrayBufferOffset, count, primitiveRestartEnabled, outIndexRange);
Geoff Lang520c4ae2015-05-05 13:12:36 -0400212 if (error.isError())
Geoff Lang7c82bc42015-03-09 16:18:08 -0400213 {
Geoff Lang520c4ae2015-05-05 13:12:36 -0400214 return error;
Geoff Lang7c82bc42015-03-09 16:18:08 -0400215 }
216 }
217
Shao80957d92017-02-20 21:25:59 +0800218 // Indices serves as an offset into the index buffer in this case, use the same value for
219 // the draw call
Geoff Lang7c82bc42015-03-09 16:18:08 -0400220 *outIndices = indices;
221 }
222 else
223 {
224 // Need to stream the index buffer
225 // TODO: if GLES, nothing needs to be streamed
226
227 // Only compute the index range if the attributes also need to be streamed
228 if (attributesNeedStreaming)
229 {
Geoff Lang3edfe032015-09-04 16:38:24 -0400230 *outIndexRange = ComputeIndexRange(type, indices, count, primitiveRestartEnabled);
Geoff Lang7c82bc42015-03-09 16:18:08 -0400231 }
232
233 // Allocate the streaming element array buffer
234 if (mStreamingElementArrayBuffer == 0)
235 {
236 mFunctions->genBuffers(1, &mStreamingElementArrayBuffer);
237 mStreamingElementArrayBufferSize = 0;
238 }
239
240 mStateManager->bindBuffer(GL_ELEMENT_ARRAY_BUFFER, mStreamingElementArrayBuffer);
Jamie Madill4928b7c2017-06-20 12:57:39 -0400241 mAppliedElementArrayBuffer.set(context, nullptr);
Geoff Lang7c82bc42015-03-09 16:18:08 -0400242
243 // Make sure the element array buffer is large enough
Jamie Madill0b9e9032015-08-17 11:51:52 +0000244 const Type &indexTypeInfo = GetTypeInfo(type);
Geoff Lang7c82bc42015-03-09 16:18:08 -0400245 size_t requiredStreamingBufferSize = indexTypeInfo.bytes * count;
246 if (requiredStreamingBufferSize > mStreamingElementArrayBufferSize)
247 {
248 // Copy the indices in while resizing the buffer
Shao80957d92017-02-20 21:25:59 +0800249 mFunctions->bufferData(GL_ELEMENT_ARRAY_BUFFER, requiredStreamingBufferSize, indices,
250 GL_DYNAMIC_DRAW);
Geoff Lang7c82bc42015-03-09 16:18:08 -0400251 mStreamingElementArrayBufferSize = requiredStreamingBufferSize;
252 }
253 else
254 {
255 // Put the indices at the beginning of the buffer
Shao80957d92017-02-20 21:25:59 +0800256 mFunctions->bufferSubData(GL_ELEMENT_ARRAY_BUFFER, 0, requiredStreamingBufferSize,
257 indices);
Geoff Lang7c82bc42015-03-09 16:18:08 -0400258 }
259
Shao80957d92017-02-20 21:25:59 +0800260 // Set the index offset for the draw call to zero since the supplied index pointer is to
261 // client data
Geoff Lang7c82bc42015-03-09 16:18:08 -0400262 *outIndices = nullptr;
263 }
264
Yuly Novikovc4d18aa2017-03-09 18:45:02 -0500265 return gl::NoError();
Geoff Lang7c82bc42015-03-09 16:18:08 -0400266}
267
Jamie Madill0b9e9032015-08-17 11:51:52 +0000268void VertexArrayGL::computeStreamingAttributeSizes(const gl::AttributesMask &activeAttributesMask,
Geoff Lang3cf12ce2015-08-27 14:40:48 -0400269 GLsizei instanceCount,
Geoff Lang3edfe032015-09-04 16:38:24 -0400270 const gl::IndexRange &indexRange,
Jamie Madill0b9e9032015-08-17 11:51:52 +0000271 size_t *outStreamingDataSize,
272 size_t *outMaxAttributeDataSize) const
Geoff Lang7c82bc42015-03-09 16:18:08 -0400273{
Jamie Madill0b9e9032015-08-17 11:51:52 +0000274 *outStreamingDataSize = 0;
275 *outMaxAttributeDataSize = 0;
276
277 ASSERT(mAttributesNeedStreaming.any());
278
Shao80957d92017-02-20 21:25:59 +0800279 const auto &attribs = mData.getVertexAttributes();
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800280 const auto &bindings = mData.getVertexBindings();
Jamie Madill6de51852017-04-12 09:53:01 -0400281
282 gl::AttributesMask attribsToStream = (mAttributesNeedStreaming & activeAttributesMask);
283
284 for (auto idx : attribsToStream)
Jamie Madill0b9e9032015-08-17 11:51:52 +0000285 {
Shao80957d92017-02-20 21:25:59 +0800286 const auto &attrib = attribs[idx];
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800287 const auto &binding = bindings[attrib.bindingIndex];
288 ASSERT(AttributeNeedsStreaming(attrib, binding));
Jamie Madill0b9e9032015-08-17 11:51:52 +0000289
Jamie Madill0b9e9032015-08-17 11:51:52 +0000290 // If streaming is going to be required, compute the size of the required buffer
291 // and how much slack space at the beginning of the buffer will be required by determining
292 // the attribute with the largest data size.
293 size_t typeSize = ComputeVertexAttributeTypeSize(attrib);
Martin Radev553590a2017-07-31 16:40:39 +0300294 GLuint adjustedDivisor = GetAdjustedDivisor(mAppliedNumViews, binding.getDivisor());
295 *outStreamingDataSize +=
296 typeSize * ComputeVertexBindingElementCount(adjustedDivisor, indexRange.vertexCount(),
297 instanceCount);
Jamie Madill0b9e9032015-08-17 11:51:52 +0000298 *outMaxAttributeDataSize = std::max(*outMaxAttributeDataSize, typeSize);
299 }
300}
301
302gl::Error VertexArrayGL::streamAttributes(const gl::AttributesMask &activeAttributesMask,
Geoff Lang3cf12ce2015-08-27 14:40:48 -0400303 GLsizei instanceCount,
Geoff Lang3edfe032015-09-04 16:38:24 -0400304 const gl::IndexRange &indexRange) const
Jamie Madill0b9e9032015-08-17 11:51:52 +0000305{
306 // Sync the vertex attribute state and track what data needs to be streamed
307 size_t streamingDataSize = 0;
308 size_t maxAttributeDataSize = 0;
309
Geoff Lang3cf12ce2015-08-27 14:40:48 -0400310 computeStreamingAttributeSizes(activeAttributesMask, instanceCount, indexRange,
311 &streamingDataSize, &maxAttributeDataSize);
Jamie Madill0b9e9032015-08-17 11:51:52 +0000312
313 if (streamingDataSize == 0)
314 {
He Yunchaoacd18982017-01-04 10:46:42 +0800315 return gl::NoError();
Jamie Madill0b9e9032015-08-17 11:51:52 +0000316 }
317
Geoff Lang7c82bc42015-03-09 16:18:08 -0400318 if (mStreamingArrayBuffer == 0)
319 {
320 mFunctions->genBuffers(1, &mStreamingArrayBuffer);
321 mStreamingArrayBufferSize = 0;
322 }
323
Shao80957d92017-02-20 21:25:59 +0800324 // If first is greater than zero, a slack space needs to be left at the beginning of the buffer
325 // so that the same 'first' argument can be passed into the draw call.
326 const size_t bufferEmptySpace = maxAttributeDataSize * indexRange.start;
Geoff Lang7c82bc42015-03-09 16:18:08 -0400327 const size_t requiredBufferSize = streamingDataSize + bufferEmptySpace;
328
329 mStateManager->bindBuffer(GL_ARRAY_BUFFER, mStreamingArrayBuffer);
330 if (requiredBufferSize > mStreamingArrayBufferSize)
331 {
332 mFunctions->bufferData(GL_ARRAY_BUFFER, requiredBufferSize, nullptr, GL_DYNAMIC_DRAW);
333 mStreamingArrayBufferSize = requiredBufferSize;
334 }
335
336 // Unmapping a buffer can return GL_FALSE to indicate that the system has corrupted the data
Shao80957d92017-02-20 21:25:59 +0800337 // somehow (such as by a screen change), retry writing the data a few times and return
338 // OUT_OF_MEMORY if that fails.
339 GLboolean unmapResult = GL_FALSE;
Geoff Lang7c82bc42015-03-09 16:18:08 -0400340 size_t unmapRetryAttempts = 5;
341 while (unmapResult != GL_TRUE && --unmapRetryAttempts > 0)
342 {
Geoff Langb2ebb5e2016-06-08 18:17:55 +0000343 uint8_t *bufferPointer = MapBufferRangeWithFallback(mFunctions, GL_ARRAY_BUFFER, 0,
344 requiredBufferSize, GL_MAP_WRITE_BIT);
Geoff Lang7c82bc42015-03-09 16:18:08 -0400345 size_t curBufferOffset = bufferEmptySpace;
346
Shao80957d92017-02-20 21:25:59 +0800347 const auto &attribs = mData.getVertexAttributes();
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800348 const auto &bindings = mData.getVertexBindings();
Jamie Madill6de51852017-04-12 09:53:01 -0400349
350 gl::AttributesMask attribsToStream = (mAttributesNeedStreaming & activeAttributesMask);
351
352 for (auto idx : attribsToStream)
Geoff Lang7c82bc42015-03-09 16:18:08 -0400353 {
Shao80957d92017-02-20 21:25:59 +0800354 const auto &attrib = attribs[idx];
Shaodde78e82017-05-22 14:13:27 +0800355 ASSERT(IsVertexAttribPointerSupported(idx, attrib));
356
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800357 const auto &binding = bindings[attrib.bindingIndex];
358 ASSERT(AttributeNeedsStreaming(attrib, binding));
Geoff Lang7c82bc42015-03-09 16:18:08 -0400359
Martin Radev553590a2017-07-31 16:40:39 +0300360 GLuint adjustedDivisor = GetAdjustedDivisor(mAppliedNumViews, binding.getDivisor());
361 const size_t streamedVertexCount = ComputeVertexBindingElementCount(
362 adjustedDivisor, indexRange.vertexCount(), instanceCount);
Geoff Lang3cf12ce2015-08-27 14:40:48 -0400363
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800364 const size_t sourceStride = ComputeVertexAttributeStride(attrib, binding);
Jamie Madill0b9e9032015-08-17 11:51:52 +0000365 const size_t destStride = ComputeVertexAttributeTypeSize(attrib);
366
Geoff Lang38a24a92017-02-15 13:53:06 -0500367 // Vertices do not apply the 'start' offset when the divisor is non-zero even when doing
368 // a non-instanced draw call
Martin Radev553590a2017-07-31 16:40:39 +0300369 const size_t firstIndex = adjustedDivisor == 0 ? indexRange.start : 0;
Geoff Lang38a24a92017-02-15 13:53:06 -0500370
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800371 // Attributes using client memory ignore the VERTEX_ATTRIB_BINDING state.
372 // https://www.opengl.org/registry/specs/ARB/vertex_attrib_binding.txt
Jamie Madill0b9e9032015-08-17 11:51:52 +0000373 const uint8_t *inputPointer = reinterpret_cast<const uint8_t *>(attrib.pointer);
374
375 // Pack the data when copying it, user could have supplied a very large stride that
376 // would cause the buffer to be much larger than needed.
377 if (destStride == sourceStride)
Jamie Madill8e344942015-07-09 14:22:07 -0400378 {
Jamie Madill0b9e9032015-08-17 11:51:52 +0000379 // Can copy in one go, the data is packed
Geoff Lang38a24a92017-02-15 13:53:06 -0500380 memcpy(bufferPointer + curBufferOffset, inputPointer + (sourceStride * firstIndex),
Jamie Madill0b9e9032015-08-17 11:51:52 +0000381 destStride * streamedVertexCount);
Jamie Madill6d51c702015-08-14 10:38:10 -0400382 }
Jamie Madill0b9e9032015-08-17 11:51:52 +0000383 else
384 {
385 // Copy each vertex individually
Geoff Lang6b10ddb2015-09-02 15:55:10 -0400386 for (size_t vertexIdx = 0; vertexIdx < streamedVertexCount; vertexIdx++)
Jamie Madill0b9e9032015-08-17 11:51:52 +0000387 {
Shao80957d92017-02-20 21:25:59 +0800388 uint8_t *out = bufferPointer + curBufferOffset + (destStride * vertexIdx);
Geoff Lang38a24a92017-02-15 13:53:06 -0500389 const uint8_t *in = inputPointer + sourceStride * (vertexIdx + firstIndex);
Corentin Wallez4d5362d2015-08-25 11:25:14 -0400390 memcpy(out, in, destStride);
Jamie Madill0b9e9032015-08-17 11:51:52 +0000391 }
392 }
393
394 // Compute where the 0-index vertex would be.
Geoff Lang38a24a92017-02-15 13:53:06 -0500395 const size_t vertexStartOffset = curBufferOffset - (firstIndex * destStride);
Jamie Madill0b9e9032015-08-17 11:51:52 +0000396
Shaodf682a82017-03-31 15:13:21 +0800397 callVertexAttribPointer(static_cast<GLuint>(idx), attrib,
398 static_cast<GLsizei>(destStride),
399 static_cast<GLintptr>(vertexStartOffset));
Jamie Madill0b9e9032015-08-17 11:51:52 +0000400
401 curBufferOffset += destStride * streamedVertexCount;
Geoff Lang7c82bc42015-03-09 16:18:08 -0400402 }
403
404 unmapResult = mFunctions->unmapBuffer(GL_ARRAY_BUFFER);
405 }
406
407 if (unmapResult != GL_TRUE)
408 {
Yuly Novikovc4d18aa2017-03-09 18:45:02 -0500409 return gl::OutOfMemory() << "Failed to unmap the client data streaming buffer.";
Geoff Lang7c82bc42015-03-09 16:18:08 -0400410 }
411
Yuly Novikovc4d18aa2017-03-09 18:45:02 -0500412 return gl::NoError();
Geoff Langba4c4a82015-02-24 12:38:46 -0500413}
414
415GLuint VertexArrayGL::getVertexArrayID() const
416{
417 return mVertexArrayID;
Geoff Langf9a6f082015-01-22 13:32:49 -0500418}
419
Geoff Lang294cad92015-05-26 15:11:23 -0400420GLuint VertexArrayGL::getAppliedElementArrayBufferID() const
421{
Jamie Madill77a90c22015-08-11 16:33:17 -0400422 if (mAppliedElementArrayBuffer.get() == nullptr)
423 {
424 return mStreamingElementArrayBuffer;
425 }
426
427 return GetImplAs<BufferGL>(mAppliedElementArrayBuffer.get())->getBufferID();
Geoff Lang294cad92015-05-26 15:11:23 -0400428}
429
Jamie Madill0b9e9032015-08-17 11:51:52 +0000430void VertexArrayGL::updateNeedsStreaming(size_t attribIndex)
431{
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800432 const auto &attrib = mData.getVertexAttribute(attribIndex);
433 const auto &binding = mData.getBindingFromAttribIndex(attribIndex);
434 mAttributesNeedStreaming.set(attribIndex, AttributeNeedsStreaming(attrib, binding));
Geoff Langf9a6f082015-01-22 13:32:49 -0500435}
Jamie Madill0b9e9032015-08-17 11:51:52 +0000436
437void VertexArrayGL::updateAttribEnabled(size_t attribIndex)
438{
Shaodf682a82017-03-31 15:13:21 +0800439 const bool enabled = mData.getVertexAttribute(attribIndex).enabled;
440 if (mAppliedAttributes[attribIndex].enabled == enabled)
Jamie Madill0b9e9032015-08-17 11:51:52 +0000441 {
442 return;
443 }
444
445 updateNeedsStreaming(attribIndex);
446
Shaodf682a82017-03-31 15:13:21 +0800447 if (enabled)
Jamie Madill0b9e9032015-08-17 11:51:52 +0000448 {
449 mFunctions->enableVertexAttribArray(static_cast<GLuint>(attribIndex));
450 }
451 else
452 {
453 mFunctions->disableVertexAttribArray(static_cast<GLuint>(attribIndex));
454 }
Shaodf682a82017-03-31 15:13:21 +0800455
456 mAppliedAttributes[attribIndex].enabled = enabled;
Jamie Madill0b9e9032015-08-17 11:51:52 +0000457}
458
Jamie Madill4928b7c2017-06-20 12:57:39 -0400459void VertexArrayGL::updateAttribPointer(const gl::Context *context, size_t attribIndex)
Jamie Madill0b9e9032015-08-17 11:51:52 +0000460{
461 const VertexAttribute &attrib = mData.getVertexAttribute(attribIndex);
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800462
Shaodde78e82017-05-22 14:13:27 +0800463 // According to SPEC, VertexAttribPointer should update the binding indexed attribIndex instead
464 // of the binding indexed attrib.bindingIndex (unless attribIndex == attrib.bindingIndex).
465 const VertexBinding &binding = mData.getVertexBinding(attribIndex);
Shaodf682a82017-03-31 15:13:21 +0800466
Shaodde78e82017-05-22 14:13:27 +0800467 // Since mAttributesNeedStreaming[attribIndex] keeps the value set in the last draw, here we
468 // only need to update it when the buffer has been changed. e.g. When we set an attribute to be
469 // streamed in the last draw, and only change its format in this draw without calling
470 // updateNeedsStreaming, it will still be streamed because the flag is already on.
471 const auto &bindingBuffer = binding.getBuffer();
472 if (bindingBuffer != mAppliedBindings[attribIndex].getBuffer())
473 {
474 updateNeedsStreaming(attribIndex);
475 }
476
477 // Early return when the vertex attribute isn't using a buffer object:
478 // - If we need to stream, defer the attribPointer to the draw call.
479 // - Skip the attribute that is disabled and uses a client memory pointer.
480 // - Skip the attribute whose buffer is detached by BindVertexBuffer. Since it cannot have a
481 // client memory pointer either, it must be disabled and shouldn't affect the draw.
482 const Buffer *arrayBuffer = bindingBuffer.get();
483 if (arrayBuffer == nullptr)
484 {
485 // Mark the applied binding isn't using a buffer by setting its buffer to nullptr so that if
486 // it starts to use a buffer later, there is no chance that the caching will skip it.
487 mAppliedBindings[attribIndex].setBuffer(context, nullptr);
488 return;
489 }
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800490
Shaodf682a82017-03-31 15:13:21 +0800491 // We do not need to compare attrib.pointer because when we use a different client memory
492 // pointer, we don't need to update mAttributesNeedStreaming by binding.buffer and we won't
493 // update attribPointer in this function.
Shaodde78e82017-05-22 14:13:27 +0800494 if ((SameVertexAttribFormat(mAppliedAttributes[attribIndex], attrib)) &&
495 (mAppliedAttributes[attribIndex].bindingIndex == attrib.bindingIndex) &&
496 (SameVertexBuffer(mAppliedBindings[attribIndex], binding)))
Jamie Madill0b9e9032015-08-17 11:51:52 +0000497 {
498 return;
499 }
500
Shao4181bc92017-03-17 14:55:25 +0800501 // Since ANGLE always uses a non-zero VAO, we cannot use a client memory pointer on it:
502 // [OpenGL ES 3.0.2] Section 2.8 page 24:
503 // An INVALID_OPERATION error is generated when a non-zero vertex array object is bound,
504 // zero is bound to the ARRAY_BUFFER buffer object binding point, and the pointer argument
505 // is not NULL.
Shaodf682a82017-03-31 15:13:21 +0800506
Shao4181bc92017-03-17 14:55:25 +0800507 const BufferGL *arrayBufferGL = GetImplAs<BufferGL>(arrayBuffer);
508 mStateManager->bindBuffer(GL_ARRAY_BUFFER, arrayBufferGL->getBufferID());
Martin Radevdd5f27e2017-06-07 10:17:09 +0300509 callVertexAttribPointer(static_cast<GLuint>(attribIndex), attrib, binding.getStride(),
510 binding.getOffset());
Shaodf682a82017-03-31 15:13:21 +0800511
Shaodde78e82017-05-22 14:13:27 +0800512 mAppliedAttributes[attribIndex].size = attrib.size;
513 mAppliedAttributes[attribIndex].type = attrib.type;
514 mAppliedAttributes[attribIndex].normalized = attrib.normalized;
515 mAppliedAttributes[attribIndex].pureInteger = attrib.pureInteger;
Shaodf682a82017-03-31 15:13:21 +0800516
Shaodde78e82017-05-22 14:13:27 +0800517 // After VertexAttribPointer, attrib.relativeOffset is set to 0 and attrib.bindingIndex is set
518 // to attribIndex in driver. If attrib.relativeOffset != 0 or attrib.bindingIndex !=
519 // attribIndex, they should be set in updateAttribFormat and updateAttribBinding. The cache
520 // should be consistent with driver so that we won't miss anything.
521 mAppliedAttributes[attribIndex].relativeOffset = 0;
522 mAppliedAttributes[attribIndex].bindingIndex = static_cast<GLuint>(attribIndex);
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800523
Shaodde78e82017-05-22 14:13:27 +0800524 mAppliedBindings[attribIndex].setStride(binding.getStride());
525 mAppliedBindings[attribIndex].setOffset(binding.getOffset());
526 mAppliedBindings[attribIndex].setBuffer(context, binding.getBuffer().get());
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800527}
528
Shaodf682a82017-03-31 15:13:21 +0800529void VertexArrayGL::callVertexAttribPointer(GLuint attribIndex,
530 const VertexAttribute &attrib,
531 GLsizei stride,
532 GLintptr offset) const
533{
534 const GLvoid *pointer = reinterpret_cast<const GLvoid *>(offset);
535 if (attrib.pureInteger)
536 {
537 ASSERT(!attrib.normalized);
538 mFunctions->vertexAttribIPointer(attribIndex, attrib.size, attrib.type, stride, pointer);
539 }
540 else
541 {
542 mFunctions->vertexAttribPointer(attribIndex, attrib.size, attrib.type, attrib.normalized,
543 stride, pointer);
544 }
545}
546
Shaodde78e82017-05-22 14:13:27 +0800547bool VertexArrayGL::supportVertexAttribBinding() const
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800548{
Shaodde78e82017-05-22 14:13:27 +0800549 ASSERT(mFunctions);
550 return (mFunctions->vertexAttribBinding != nullptr);
551}
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800552
Shaodde78e82017-05-22 14:13:27 +0800553void VertexArrayGL::updateAttribFormat(size_t attribIndex)
554{
555 ASSERT(supportVertexAttribBinding());
556
557 const VertexAttribute &attrib = mData.getVertexAttribute(attribIndex);
558 if (SameVertexAttribFormat(mAppliedAttributes[attribIndex], attrib))
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800559 {
Shaodf682a82017-03-31 15:13:21 +0800560 return;
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800561 }
Shaodf682a82017-03-31 15:13:21 +0800562
Shaodde78e82017-05-22 14:13:27 +0800563 if (attrib.pureInteger)
564 {
565 ASSERT(!attrib.normalized);
566 mFunctions->vertexAttribIFormat(static_cast<GLuint>(attribIndex), attrib.size, attrib.type,
567 attrib.relativeOffset);
568 }
569 else
570 {
571 mFunctions->vertexAttribFormat(static_cast<GLuint>(attribIndex), attrib.size, attrib.type,
572 attrib.normalized, attrib.relativeOffset);
573 }
574
575 mAppliedAttributes[attribIndex].size = attrib.size;
576 mAppliedAttributes[attribIndex].type = attrib.type;
577 mAppliedAttributes[attribIndex].normalized = attrib.normalized;
578 mAppliedAttributes[attribIndex].pureInteger = attrib.pureInteger;
579 mAppliedAttributes[attribIndex].relativeOffset = attrib.relativeOffset;
580}
581
582void VertexArrayGL::updateAttribBinding(size_t attribIndex)
583{
584 ASSERT(supportVertexAttribBinding());
585
586 GLuint bindingIndex = mData.getVertexAttribute(attribIndex).bindingIndex;
587 if (mAppliedAttributes[attribIndex].bindingIndex == bindingIndex)
588 {
589 return;
590 }
591
592 mFunctions->vertexAttribBinding(static_cast<GLuint>(attribIndex), bindingIndex);
Shaodf682a82017-03-31 15:13:21 +0800593
594 mAppliedAttributes[attribIndex].bindingIndex = bindingIndex;
Shaodde78e82017-05-22 14:13:27 +0800595}
596
597void VertexArrayGL::updateBindingBuffer(const gl::Context *context, size_t bindingIndex)
598{
599 ASSERT(supportVertexAttribBinding());
600
601 const VertexBinding &binding = mData.getVertexBinding(bindingIndex);
602 if (SameVertexBuffer(mAppliedBindings[bindingIndex], binding))
603 {
604 return;
605 }
606
607 const Buffer *arrayBuffer = binding.getBuffer().get();
608 GLuint bufferId = 0;
609 if (arrayBuffer != nullptr)
610 {
611 bufferId = GetImplAs<BufferGL>(arrayBuffer)->getBufferID();
612 }
613
614 mFunctions->bindVertexBuffer(static_cast<GLuint>(bindingIndex), bufferId, binding.getOffset(),
615 binding.getStride());
616
617 mAppliedBindings[bindingIndex].setStride(binding.getStride());
618 mAppliedBindings[bindingIndex].setOffset(binding.getOffset());
619 mAppliedBindings[bindingIndex].setBuffer(context, binding.getBuffer().get());
620}
621
622void VertexArrayGL::updateBindingDivisor(size_t bindingIndex)
623{
Martin Radev553590a2017-07-31 16:40:39 +0300624 GLuint adjustedDivisor =
625 GetAdjustedDivisor(mAppliedNumViews, mData.getVertexBinding(bindingIndex).getDivisor());
626 if (mAppliedBindings[bindingIndex].getDivisor() == adjustedDivisor)
Shaodde78e82017-05-22 14:13:27 +0800627 {
628 return;
629 }
630
631 if (supportVertexAttribBinding())
632 {
Martin Radev553590a2017-07-31 16:40:39 +0300633 mFunctions->vertexBindingDivisor(static_cast<GLuint>(bindingIndex), adjustedDivisor);
Shaodde78e82017-05-22 14:13:27 +0800634 }
635 else
636 {
637 // We can only use VertexAttribDivisor on platforms that don't support Vertex Attrib
638 // Binding.
Martin Radev553590a2017-07-31 16:40:39 +0300639 mFunctions->vertexAttribDivisor(static_cast<GLuint>(bindingIndex), adjustedDivisor);
Shaodde78e82017-05-22 14:13:27 +0800640 }
641
Martin Radev553590a2017-07-31 16:40:39 +0300642 mAppliedBindings[bindingIndex].setDivisor(adjustedDivisor);
Jamie Madill0b9e9032015-08-17 11:51:52 +0000643}
644
Jamie Madillc564c072017-06-01 12:45:42 -0400645void VertexArrayGL::syncState(const gl::Context *context, const VertexArray::DirtyBits &dirtyBits)
Jamie Madill0b9e9032015-08-17 11:51:52 +0000646{
Shaodf682a82017-03-31 15:13:21 +0800647 mStateManager->bindVertexArray(mVertexArrayID, getAppliedElementArrayBufferID());
648
Jamie Madill6de51852017-04-12 09:53:01 -0400649 for (size_t dirtyBit : dirtyBits)
Jamie Madill0b9e9032015-08-17 11:51:52 +0000650 {
651 if (dirtyBit == VertexArray::DIRTY_BIT_ELEMENT_ARRAY_BUFFER)
652 {
653 // TODO(jmadill): Element array buffer bindings
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800654 continue;
Jamie Madill0b9e9032015-08-17 11:51:52 +0000655 }
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800656
Shaodde78e82017-05-22 14:13:27 +0800657 size_t index = VertexArray::GetVertexIndexFromDirtyBit(dirtyBit);
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800658 if (dirtyBit >= VertexArray::DIRTY_BIT_ATTRIB_0_ENABLED &&
659 dirtyBit < VertexArray::DIRTY_BIT_ATTRIB_MAX_ENABLED)
Jamie Madill0b9e9032015-08-17 11:51:52 +0000660 {
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800661 updateAttribEnabled(index);
Jamie Madill0b9e9032015-08-17 11:51:52 +0000662 }
663 else if (dirtyBit >= VertexArray::DIRTY_BIT_ATTRIB_0_POINTER &&
664 dirtyBit < VertexArray::DIRTY_BIT_ATTRIB_MAX_POINTER)
665 {
Jamie Madill4928b7c2017-06-20 12:57:39 -0400666 updateAttribPointer(context, index);
Jamie Madill0b9e9032015-08-17 11:51:52 +0000667 }
Shaodde78e82017-05-22 14:13:27 +0800668
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800669 else if (dirtyBit >= VertexArray::DIRTY_BIT_ATTRIB_0_FORMAT &&
Shaodde78e82017-05-22 14:13:27 +0800670 dirtyBit < VertexArray::DIRTY_BIT_ATTRIB_MAX_FORMAT)
671 {
672 ASSERT(supportVertexAttribBinding());
673 updateAttribFormat(index);
674 }
675 else if (dirtyBit >= VertexArray::DIRTY_BIT_ATTRIB_0_BINDING &&
676 dirtyBit < VertexArray::DIRTY_BIT_ATTRIB_MAX_BINDING)
677 {
678 ASSERT(supportVertexAttribBinding());
679 updateAttribBinding(index);
680 }
681 else if (dirtyBit >= VertexArray::DIRTY_BIT_BINDING_0_BUFFER &&
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800682 dirtyBit < VertexArray::DIRTY_BIT_BINDING_MAX_BUFFER)
Jamie Madill0b9e9032015-08-17 11:51:52 +0000683 {
Shaodde78e82017-05-22 14:13:27 +0800684 ASSERT(supportVertexAttribBinding());
685 updateBindingBuffer(context, index);
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800686 }
Shaodde78e82017-05-22 14:13:27 +0800687
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800688 else if (dirtyBit >= VertexArray::DIRTY_BIT_BINDING_0_DIVISOR &&
689 dirtyBit < VertexArray::DIRTY_BIT_BINDING_MAX_DIVISOR)
690 {
Shaodde78e82017-05-22 14:13:27 +0800691 updateBindingDivisor(index);
Jamie Madill0b9e9032015-08-17 11:51:52 +0000692 }
693 else
694 UNREACHABLE();
695 }
696}
697
Martin Radev553590a2017-07-31 16:40:39 +0300698void VertexArrayGL::applyNumViewsToDivisor(int numViews)
699{
700 if (numViews != mAppliedNumViews)
701 {
702 mStateManager->bindVertexArray(mVertexArrayID, getAppliedElementArrayBufferID());
703 mAppliedNumViews = numViews;
704 for (size_t index = 0u; index < mAppliedBindings.size(); ++index)
705 {
706 updateBindingDivisor(index);
707 }
708 }
709}
710
Jamie Madill0b9e9032015-08-17 11:51:52 +0000711} // rx