blob: 8cfa8d655050fa5b69e453970c035e6af7d534fe [file] [log] [blame]
bsalomon@google.com6918d482013-03-07 19:09:11 +00001/*
2 * Copyright 2013 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8#include "GrGLVertexArray.h"
jvanverth39edf762014-12-22 11:44:19 -08009#include "GrGLGpu.h"
bsalomon@google.com6918d482013-03-07 19:09:11 +000010
bsalomon@google.com6918d482013-03-07 19:09:11 +000011
bsalomon6df86402015-06-01 10:41:49 -070012
13void GrGLAttribArrayState::set(GrGLGpu* gpu,
bsalomon@google.com6918d482013-03-07 19:09:11 +000014 int index,
bsalomon6df86402015-06-01 10:41:49 -070015 GrGLuint vertexBufferID,
bsalomon@google.com6918d482013-03-07 19:09:11 +000016 GrGLint size,
17 GrGLenum type,
18 GrGLboolean normalized,
19 GrGLsizei stride,
20 GrGLvoid* offset) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +000021 SkASSERT(index >= 0 && index < fAttribArrayStates.count());
bsalomon@google.com6918d482013-03-07 19:09:11 +000022 AttribArrayState* array = &fAttribArrayStates[index];
23 if (!array->fEnableIsValid || !array->fEnabled) {
24 GR_GL_CALL(gpu->glInterface(), EnableVertexAttribArray(index));
25 array->fEnableIsValid = true;
26 array->fEnabled = true;
27 }
28 if (!array->fAttribPointerIsValid ||
bsalomon6df86402015-06-01 10:41:49 -070029 array->fVertexBufferID != vertexBufferID ||
bsalomon@google.com6918d482013-03-07 19:09:11 +000030 array->fSize != size ||
31 array->fNormalized != normalized ||
32 array->fStride != stride ||
33 array->fOffset != offset) {
34
bsalomon6df86402015-06-01 10:41:49 -070035 gpu->bindVertexBuffer(vertexBufferID);
bsalomon@google.com6918d482013-03-07 19:09:11 +000036 GR_GL_CALL(gpu->glInterface(), VertexAttribPointer(index,
37 size,
38 type,
39 normalized,
40 stride,
41 offset));
42 array->fAttribPointerIsValid = true;
bsalomon6df86402015-06-01 10:41:49 -070043 array->fVertexBufferID = vertexBufferID;
bsalomon@google.com6918d482013-03-07 19:09:11 +000044 array->fSize = size;
45 array->fNormalized = normalized;
46 array->fStride = stride;
47 array->fOffset = offset;
48 }
49}
50
bsalomon861e1032014-12-16 07:33:49 -080051void GrGLAttribArrayState::disableUnusedArrays(const GrGLGpu* gpu, uint64_t usedMask) {
bsalomon@google.com6918d482013-03-07 19:09:11 +000052 int count = fAttribArrayStates.count();
53 for (int i = 0; i < count; ++i) {
54 if (!(usedMask & 0x1)) {
55 if (!fAttribArrayStates[i].fEnableIsValid || fAttribArrayStates[i].fEnabled) {
56 GR_GL_CALL(gpu->glInterface(), DisableVertexAttribArray(i));
57 fAttribArrayStates[i].fEnableIsValid = true;
58 fAttribArrayStates[i].fEnabled = false;
59 }
commit-bot@chromium.orgce6da4d2013-09-09 14:55:37 +000060 } else {
61 SkASSERT(fAttribArrayStates[i].fEnableIsValid && fAttribArrayStates[i].fEnabled);
bsalomon@google.com6918d482013-03-07 19:09:11 +000062 }
63 // if the count is greater than 64 then this will become 0 and we will disable arrays 64+.
64 usedMask >>= 1;
65 }
66}
67
68///////////////////////////////////////////////////////////////////////////////////////////////////
69
bsalomon8780bc62015-05-13 09:56:37 -070070GrGLVertexArray::GrGLVertexArray(GrGLint id, int attribCount)
71 : fID(id)
bsalomon@google.com6918d482013-03-07 19:09:11 +000072 , fAttribArrays(attribCount)
73 , fIndexBufferIDIsValid(false) {
74}
75
bsalomon8780bc62015-05-13 09:56:37 -070076GrGLAttribArrayState* GrGLVertexArray::bind(GrGLGpu* gpu) {
bsalomon@google.com6918d482013-03-07 19:09:11 +000077 if (0 == fID) {
halcanary96fcdcc2015-08-27 07:41:13 -070078 return nullptr;
bsalomon@google.com6918d482013-03-07 19:09:11 +000079 }
bsalomon8780bc62015-05-13 09:56:37 -070080 gpu->bindVertexArray(fID);
bsalomon@google.com6918d482013-03-07 19:09:11 +000081 return &fAttribArrays;
82}
skia.committer@gmail.com754a3eb2013-03-08 07:01:25 +000083
bsalomon6df86402015-06-01 10:41:49 -070084GrGLAttribArrayState* GrGLVertexArray::bindWithIndexBuffer(GrGLGpu* gpu, GrGLuint ibufferID) {
bsalomon8780bc62015-05-13 09:56:37 -070085 GrGLAttribArrayState* state = this->bind(gpu);
bsalomon6df86402015-06-01 10:41:49 -070086 if (state) {
87 if (!fIndexBufferIDIsValid || ibufferID != fIndexBufferID) {
88 GR_GL_CALL(gpu->glInterface(), BindBuffer(GR_GL_ELEMENT_ARRAY_BUFFER, ibufferID));
bsalomon@google.com6918d482013-03-07 19:09:11 +000089 fIndexBufferIDIsValid = true;
bsalomon6df86402015-06-01 10:41:49 -070090 fIndexBufferID = ibufferID;
bsalomon@google.com6918d482013-03-07 19:09:11 +000091 }
92 }
93 return state;
94}
95
96void GrGLVertexArray::notifyIndexBufferDelete(GrGLuint bufferID) {
97 if (fIndexBufferIDIsValid && bufferID == fIndexBufferID) {
98 fIndexBufferID = 0;
99 }
100 }
101
102void GrGLVertexArray::invalidateCachedState() {
commit-bot@chromium.orgce6da4d2013-09-09 14:55:37 +0000103 fAttribArrays.invalidate();
bsalomon@google.com6918d482013-03-07 19:09:11 +0000104 fIndexBufferIDIsValid = false;
105}