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" |
jvanverth | 39edf76 | 2014-12-22 11:44:19 -0800 | [diff] [blame] | 9 | #include "GrGLGpu.h" |
bsalomon@google.com | 6918d48 | 2013-03-07 19:09:11 +0000 | [diff] [blame] | 10 | |
bsalomon@google.com | 6918d48 | 2013-03-07 19:09:11 +0000 | [diff] [blame] | 11 | |
bsalomon | 6df8640 | 2015-06-01 10:41:49 -0700 | [diff] [blame] | 12 | |
| 13 | void GrGLAttribArrayState::set(GrGLGpu* gpu, |
bsalomon@google.com | 6918d48 | 2013-03-07 19:09:11 +0000 | [diff] [blame] | 14 | int index, |
bsalomon | 6df8640 | 2015-06-01 10:41:49 -0700 | [diff] [blame] | 15 | GrGLuint vertexBufferID, |
bsalomon@google.com | 6918d48 | 2013-03-07 19:09:11 +0000 | [diff] [blame] | 16 | GrGLint size, |
| 17 | GrGLenum type, |
| 18 | GrGLboolean normalized, |
| 19 | GrGLsizei stride, |
| 20 | GrGLvoid* offset) { |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 21 | SkASSERT(index >= 0 && index < fAttribArrayStates.count()); |
bsalomon@google.com | 6918d48 | 2013-03-07 19:09:11 +0000 | [diff] [blame] | 22 | 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 || |
bsalomon | 6df8640 | 2015-06-01 10:41:49 -0700 | [diff] [blame] | 29 | array->fVertexBufferID != vertexBufferID || |
bsalomon@google.com | 6918d48 | 2013-03-07 19:09:11 +0000 | [diff] [blame] | 30 | array->fSize != size || |
| 31 | array->fNormalized != normalized || |
| 32 | array->fStride != stride || |
| 33 | array->fOffset != offset) { |
| 34 | |
bsalomon | 6df8640 | 2015-06-01 10:41:49 -0700 | [diff] [blame] | 35 | gpu->bindVertexBuffer(vertexBufferID); |
bsalomon@google.com | 6918d48 | 2013-03-07 19:09:11 +0000 | [diff] [blame] | 36 | GR_GL_CALL(gpu->glInterface(), VertexAttribPointer(index, |
| 37 | size, |
| 38 | type, |
| 39 | normalized, |
| 40 | stride, |
| 41 | offset)); |
| 42 | array->fAttribPointerIsValid = true; |
bsalomon | 6df8640 | 2015-06-01 10:41:49 -0700 | [diff] [blame] | 43 | array->fVertexBufferID = vertexBufferID; |
bsalomon@google.com | 6918d48 | 2013-03-07 19:09:11 +0000 | [diff] [blame] | 44 | array->fSize = size; |
| 45 | array->fNormalized = normalized; |
| 46 | array->fStride = stride; |
| 47 | array->fOffset = offset; |
| 48 | } |
| 49 | } |
| 50 | |
bsalomon | 861e103 | 2014-12-16 07:33:49 -0800 | [diff] [blame] | 51 | void GrGLAttribArrayState::disableUnusedArrays(const GrGLGpu* gpu, uint64_t usedMask) { |
bsalomon@google.com | 6918d48 | 2013-03-07 19:09:11 +0000 | [diff] [blame] | 52 | 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.org | ce6da4d | 2013-09-09 14:55:37 +0000 | [diff] [blame] | 60 | } else { |
| 61 | SkASSERT(fAttribArrayStates[i].fEnableIsValid && fAttribArrayStates[i].fEnabled); |
bsalomon@google.com | 6918d48 | 2013-03-07 19:09:11 +0000 | [diff] [blame] | 62 | } |
| 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 | |
bsalomon | 8780bc6 | 2015-05-13 09:56:37 -0700 | [diff] [blame] | 70 | GrGLVertexArray::GrGLVertexArray(GrGLint id, int attribCount) |
| 71 | : fID(id) |
bsalomon@google.com | 6918d48 | 2013-03-07 19:09:11 +0000 | [diff] [blame] | 72 | , fAttribArrays(attribCount) |
| 73 | , fIndexBufferIDIsValid(false) { |
| 74 | } |
| 75 | |
bsalomon | 8780bc6 | 2015-05-13 09:56:37 -0700 | [diff] [blame] | 76 | GrGLAttribArrayState* GrGLVertexArray::bind(GrGLGpu* gpu) { |
bsalomon@google.com | 6918d48 | 2013-03-07 19:09:11 +0000 | [diff] [blame] | 77 | if (0 == fID) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 78 | return nullptr; |
bsalomon@google.com | 6918d48 | 2013-03-07 19:09:11 +0000 | [diff] [blame] | 79 | } |
bsalomon | 8780bc6 | 2015-05-13 09:56:37 -0700 | [diff] [blame] | 80 | gpu->bindVertexArray(fID); |
bsalomon@google.com | 6918d48 | 2013-03-07 19:09:11 +0000 | [diff] [blame] | 81 | return &fAttribArrays; |
| 82 | } |
skia.committer@gmail.com | 754a3eb | 2013-03-08 07:01:25 +0000 | [diff] [blame] | 83 | |
bsalomon | 6df8640 | 2015-06-01 10:41:49 -0700 | [diff] [blame] | 84 | GrGLAttribArrayState* GrGLVertexArray::bindWithIndexBuffer(GrGLGpu* gpu, GrGLuint ibufferID) { |
bsalomon | 8780bc6 | 2015-05-13 09:56:37 -0700 | [diff] [blame] | 85 | GrGLAttribArrayState* state = this->bind(gpu); |
bsalomon | 6df8640 | 2015-06-01 10:41:49 -0700 | [diff] [blame] | 86 | if (state) { |
| 87 | if (!fIndexBufferIDIsValid || ibufferID != fIndexBufferID) { |
| 88 | GR_GL_CALL(gpu->glInterface(), BindBuffer(GR_GL_ELEMENT_ARRAY_BUFFER, ibufferID)); |
bsalomon@google.com | 6918d48 | 2013-03-07 19:09:11 +0000 | [diff] [blame] | 89 | fIndexBufferIDIsValid = true; |
bsalomon | 6df8640 | 2015-06-01 10:41:49 -0700 | [diff] [blame] | 90 | fIndexBufferID = ibufferID; |
bsalomon@google.com | 6918d48 | 2013-03-07 19:09:11 +0000 | [diff] [blame] | 91 | } |
| 92 | } |
| 93 | return state; |
| 94 | } |
| 95 | |
| 96 | void GrGLVertexArray::notifyIndexBufferDelete(GrGLuint bufferID) { |
| 97 | if (fIndexBufferIDIsValid && bufferID == fIndexBufferID) { |
| 98 | fIndexBufferID = 0; |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | void GrGLVertexArray::invalidateCachedState() { |
commit-bot@chromium.org | ce6da4d | 2013-09-09 14:55:37 +0000 | [diff] [blame] | 103 | fAttribArrays.invalidate(); |
bsalomon@google.com | 6918d48 | 2013-03-07 19:09:11 +0000 | [diff] [blame] | 104 | fIndexBufferIDIsValid = false; |
| 105 | } |