2 * Copyright 2013 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
8 #include "GrGLVertexArray.h"
11 #define GPUGL static_cast<GrGpuGL*>(this->getGpu())
12 #define GL_CALL(X) GR_GL_CALL(GPUGL->glInterface(), X);
14 void GrGLAttribArrayState::set(const GrGpuGL* gpu,
16 GrGLVertexBuffer* buffer,
19 GrGLboolean normalized,
22 SkASSERT(index >= 0 && index < fAttribArrayStates.count());
23 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;
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) {
37 GR_GL_CALL(gpu->glInterface(), VertexAttribPointer(index,
43 array->fAttribPointerIsValid = true;
44 array->fVertexBufferID = buffer->bufferID();
46 array->fNormalized = normalized;
47 array->fStride = stride;
48 array->fOffset = offset;
52 void GrGLAttribArrayState::disableUnusedArrays(const GrGpuGL* gpu, uint64_t usedMask) {
53 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;
62 SkASSERT(fAttribArrayStates[i].fEnableIsValid && fAttribArrayStates[i].fEnabled);
64 // if the count is greater than 64 then this will become 0 and we will disable arrays 64+.
69 ///////////////////////////////////////////////////////////////////////////////////////////////////
71 GrGLVertexArray::GrGLVertexArray(GrGpuGL* gpu, GrGLint id, int attribCount)
72 : INHERITED(gpu, false)
74 , fAttribArrays(attribCount)
75 , fIndexBufferIDIsValid(false) {
76 this->registerWithCache();
79 void GrGLVertexArray::onAbandon() {
81 INHERITED::onAbandon();
84 void GrGLVertexArray::onRelease() {
86 GL_CALL(DeleteVertexArrays(1, &fID));
87 GPUGL->notifyVertexArrayDelete(fID);
90 INHERITED::onRelease();
93 GrGLAttribArrayState* GrGLVertexArray::bind() {
97 GPUGL->bindVertexArray(fID);
98 return &fAttribArrays;
101 GrGLAttribArrayState* GrGLVertexArray::bindWithIndexBuffer(const GrGLIndexBuffer* buffer) {
102 GrGLAttribArrayState* state = this->bind();
103 if (state && buffer) {
104 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;
114 void GrGLVertexArray::notifyIndexBufferDelete(GrGLuint bufferID) {
115 if (fIndexBufferIDIsValid && bufferID == fIndexBufferID) {
120 void GrGLVertexArray::invalidateCachedState() {
121 fAttribArrays.invalidate();
122 fIndexBufferIDIsValid = false;