blob: d6cc42ac547c2f1722f836dd68e744a7d0713e96 [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
bsalomon861e1032014-12-16 07:33:49 -080011#define GPUGL static_cast<GrGLGpu*>(this->getGpu())
bsalomon@google.com6918d482013-03-07 19:09:11 +000012#define GL_CALL(X) GR_GL_CALL(GPUGL->glInterface(), X);
13
bsalomon861e1032014-12-16 07:33:49 -080014void GrGLAttribArrayState::set(const GrGLGpu* gpu,
bsalomon@google.com6918d482013-03-07 19:09:11 +000015 int index,
16 GrGLVertexBuffer* buffer,
17 GrGLint size,
18 GrGLenum type,
19 GrGLboolean normalized,
20 GrGLsizei stride,
21 GrGLvoid* offset) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +000022 SkASSERT(index >= 0 && index < fAttribArrayStates.count());
bsalomon@google.com6918d482013-03-07 19:09:11 +000023 AttribArrayState* array = &fAttribArrayStates[index];
24 if (!array->fEnableIsValid || !array->fEnabled) {
25 GR_GL_CALL(gpu->glInterface(), EnableVertexAttribArray(index));
26 array->fEnableIsValid = true;
27 array->fEnabled = true;
28 }
29 if (!array->fAttribPointerIsValid ||
30 array->fVertexBufferID != buffer->bufferID() ||
31 array->fSize != size ||
32 array->fNormalized != normalized ||
33 array->fStride != stride ||
34 array->fOffset != offset) {
35
36 buffer->bind();
37 GR_GL_CALL(gpu->glInterface(), VertexAttribPointer(index,
38 size,
39 type,
40 normalized,
41 stride,
42 offset));
43 array->fAttribPointerIsValid = true;
44 array->fVertexBufferID = buffer->bufferID();
45 array->fSize = size;
46 array->fNormalized = normalized;
47 array->fStride = stride;
48 array->fOffset = offset;
49 }
50}
51
bsalomon861e1032014-12-16 07:33:49 -080052void GrGLAttribArrayState::disableUnusedArrays(const GrGLGpu* gpu, uint64_t usedMask) {
bsalomon@google.com6918d482013-03-07 19:09:11 +000053 int count = fAttribArrayStates.count();
54 for (int i = 0; i < count; ++i) {
55 if (!(usedMask & 0x1)) {
56 if (!fAttribArrayStates[i].fEnableIsValid || fAttribArrayStates[i].fEnabled) {
57 GR_GL_CALL(gpu->glInterface(), DisableVertexAttribArray(i));
58 fAttribArrayStates[i].fEnableIsValid = true;
59 fAttribArrayStates[i].fEnabled = false;
60 }
commit-bot@chromium.orgce6da4d2013-09-09 14:55:37 +000061 } else {
62 SkASSERT(fAttribArrayStates[i].fEnableIsValid && fAttribArrayStates[i].fEnabled);
bsalomon@google.com6918d482013-03-07 19:09:11 +000063 }
64 // if the count is greater than 64 then this will become 0 and we will disable arrays 64+.
65 usedMask >>= 1;
66 }
67}
68
69///////////////////////////////////////////////////////////////////////////////////////////////////
70
bsalomon861e1032014-12-16 07:33:49 -080071GrGLVertexArray::GrGLVertexArray(GrGLGpu* gpu, GrGLint id, int attribCount)
commit-bot@chromium.org089a7802014-05-02 21:38:22 +000072 : INHERITED(gpu, false)
bsalomon@google.com6918d482013-03-07 19:09:11 +000073 , fID(id)
74 , fAttribArrays(attribCount)
75 , fIndexBufferIDIsValid(false) {
bsalomon16961262014-08-26 14:01:07 -070076 this->registerWithCache();
bsalomon@google.com6918d482013-03-07 19:09:11 +000077}
78
79void GrGLVertexArray::onAbandon() {
80 fID = 0;
81 INHERITED::onAbandon();
82}
83
84void GrGLVertexArray::onRelease() {
85 if (0 != fID) {
86 GL_CALL(DeleteVertexArrays(1, &fID));
87 GPUGL->notifyVertexArrayDelete(fID);
88 fID = 0;
89 }
90 INHERITED::onRelease();
91}
92
93GrGLAttribArrayState* GrGLVertexArray::bind() {
94 if (0 == fID) {
95 return NULL;
96 }
97 GPUGL->bindVertexArray(fID);
98 return &fAttribArrays;
99}
skia.committer@gmail.com754a3eb2013-03-08 07:01:25 +0000100
bsalomon@google.com6918d482013-03-07 19:09:11 +0000101GrGLAttribArrayState* GrGLVertexArray::bindWithIndexBuffer(const GrGLIndexBuffer* buffer) {
102 GrGLAttribArrayState* state = this->bind();
bsalomon49f085d2014-09-05 13:34:00 -0700103 if (state && buffer) {
bsalomon@google.com6918d482013-03-07 19:09:11 +0000104 GrGLuint bufferID = buffer->bufferID();
105 if (!fIndexBufferIDIsValid || bufferID != fIndexBufferID) {
106 GL_CALL(BindBuffer(GR_GL_ELEMENT_ARRAY_BUFFER, bufferID));
107 fIndexBufferIDIsValid = true;
108 fIndexBufferID = bufferID;
109 }
110 }
111 return state;
112}
113
114void GrGLVertexArray::notifyIndexBufferDelete(GrGLuint bufferID) {
115 if (fIndexBufferIDIsValid && bufferID == fIndexBufferID) {
116 fIndexBufferID = 0;
117 }
118 }
119
120void GrGLVertexArray::invalidateCachedState() {
commit-bot@chromium.orgce6da4d2013-09-09 14:55:37 +0000121 fAttribArrays.invalidate();
bsalomon@google.com6918d482013-03-07 19:09:11 +0000122 fIndexBufferIDIsValid = false;
123}