| bsalomon@google.com | 6918d48 | 2013-03-07 19:09:11 +0000 | [diff] [blame^] | 1 | /* |
| 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" |
| 9 | #include "GrGpuGL.h" |
| 10 | |
| 11 | #define GPUGL static_cast<GrGpuGL*>(this->getGpu()) |
| 12 | #define GL_CALL(X) GR_GL_CALL(GPUGL->glInterface(), X); |
| 13 | |
| 14 | void GrGLAttribArrayState::set(const GrGpuGL* gpu, |
| 15 | int index, |
| 16 | GrGLVertexBuffer* buffer, |
| 17 | GrGLint size, |
| 18 | GrGLenum type, |
| 19 | GrGLboolean normalized, |
| 20 | GrGLsizei stride, |
| 21 | GrGLvoid* offset) { |
| 22 | GrAssert(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; |
| 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 | |
| 52 | void GrGLAttribArrayState::disableUnusedAttribArrays(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; |
| 60 | } |
| 61 | } |
| 62 | // if the count is greater than 64 then this will become 0 and we will disable arrays 64+. |
| 63 | usedMask >>= 1; |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 68 | |
| 69 | GrGLVertexArray::GrGLVertexArray(GrGpuGL* gpu, GrGLint id, int attribCount) |
| 70 | : GrResource(gpu, false) |
| 71 | , fID(id) |
| 72 | , fAttribArrays(attribCount) |
| 73 | , fIndexBufferIDIsValid(false) { |
| 74 | } |
| 75 | |
| 76 | void GrGLVertexArray::onAbandon() { |
| 77 | fID = 0; |
| 78 | INHERITED::onAbandon(); |
| 79 | } |
| 80 | |
| 81 | void GrGLVertexArray::onRelease() { |
| 82 | if (0 != fID) { |
| 83 | GL_CALL(DeleteVertexArrays(1, &fID)); |
| 84 | GPUGL->notifyVertexArrayDelete(fID); |
| 85 | fID = 0; |
| 86 | } |
| 87 | INHERITED::onRelease(); |
| 88 | } |
| 89 | |
| 90 | GrGLAttribArrayState* GrGLVertexArray::bind() { |
| 91 | if (0 == fID) { |
| 92 | return NULL; |
| 93 | } |
| 94 | GPUGL->bindVertexArray(fID); |
| 95 | return &fAttribArrays; |
| 96 | } |
| 97 | |
| 98 | GrGLAttribArrayState* GrGLVertexArray::bindWithIndexBuffer(const GrGLIndexBuffer* buffer) { |
| 99 | GrGLAttribArrayState* state = this->bind(); |
| 100 | if (NULL != state && NULL != buffer) { |
| 101 | GrGLuint bufferID = buffer->bufferID(); |
| 102 | if (!fIndexBufferIDIsValid || bufferID != fIndexBufferID) { |
| 103 | GL_CALL(BindBuffer(GR_GL_ELEMENT_ARRAY_BUFFER, bufferID)); |
| 104 | fIndexBufferIDIsValid = true; |
| 105 | fIndexBufferID = bufferID; |
| 106 | } |
| 107 | } |
| 108 | return state; |
| 109 | } |
| 110 | |
| 111 | void GrGLVertexArray::notifyIndexBufferDelete(GrGLuint bufferID) { |
| 112 | if (fIndexBufferIDIsValid && bufferID == fIndexBufferID) { |
| 113 | fIndexBufferID = 0; |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | void GrGLVertexArray::invalidateCachedState() { |
| 118 | int count = fAttribArrays.count(); |
| 119 | for (int i = 0; i < count; ++i) { |
| 120 | fAttribArrays.invalidate(); |
| 121 | } |
| 122 | fIndexBufferIDIsValid = false; |
| 123 | } |