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" |
cdalton | e2e71c2 | 2016-04-07 18:13:29 -0700 | [diff] [blame] | 9 | #include "GrGLBuffer.h" |
jvanverth | 39edf76 | 2014-12-22 11:44:19 -0800 | [diff] [blame] | 10 | #include "GrGLGpu.h" |
bsalomon@google.com | 6918d48 | 2013-03-07 19:09:11 +0000 | [diff] [blame] | 11 | |
cdalton | 793dc26 | 2016-02-08 10:11:47 -0800 | [diff] [blame] | 12 | struct AttribLayout { |
csmartdalton | b37cb23 | 2017-02-08 14:56:27 -0500 | [diff] [blame] | 13 | bool fNormalized; // Only used by floating point types. |
| 14 | uint8_t fCount; |
| 15 | uint16_t fType; |
cdalton | 793dc26 | 2016-02-08 10:11:47 -0800 | [diff] [blame] | 16 | }; |
bsalomon@google.com | 6918d48 | 2013-03-07 19:09:11 +0000 | [diff] [blame] | 17 | |
csmartdalton | b37cb23 | 2017-02-08 14:56:27 -0500 | [diff] [blame] | 18 | GR_STATIC_ASSERT(4 == sizeof(AttribLayout)); |
cdalton | 793dc26 | 2016-02-08 10:11:47 -0800 | [diff] [blame] | 19 | |
csmartdalton | b37cb23 | 2017-02-08 14:56:27 -0500 | [diff] [blame] | 20 | static AttribLayout attrib_layout(GrVertexAttribType type) { |
| 21 | switch (type) { |
| 22 | case kFloat_GrVertexAttribType: |
| 23 | return {false, 1, GR_GL_FLOAT}; |
| 24 | case kVec2f_GrVertexAttribType: |
| 25 | return {false, 2, GR_GL_FLOAT}; |
| 26 | case kVec3f_GrVertexAttribType: |
| 27 | return {false, 3, GR_GL_FLOAT}; |
| 28 | case kVec4f_GrVertexAttribType: |
| 29 | return {false, 4, GR_GL_FLOAT}; |
| 30 | case kVec2i_GrVertexAttribType: |
| 31 | return {false, 2, GR_GL_INT}; |
| 32 | case kVec3i_GrVertexAttribType: |
| 33 | return {false, 3, GR_GL_INT}; |
| 34 | case kVec4i_GrVertexAttribType: |
| 35 | return {false, 4, GR_GL_INT}; |
| 36 | case kUByte_GrVertexAttribType: |
| 37 | return {true, 1, GR_GL_UNSIGNED_BYTE}; |
| 38 | case kVec4ub_GrVertexAttribType: |
| 39 | return {true, 4, GR_GL_UNSIGNED_BYTE}; |
| 40 | case kVec2us_GrVertexAttribType: |
| 41 | return {true, 2, GR_GL_UNSIGNED_SHORT}; |
| 42 | case kInt_GrVertexAttribType: |
| 43 | return {false, 1, GR_GL_INT}; |
| 44 | case kUint_GrVertexAttribType: |
| 45 | return {false, 1, GR_GL_UNSIGNED_INT}; |
| 46 | } |
| 47 | SkFAIL("Unknown vertex attrib type"); |
| 48 | return {false, 0, 0}; |
| 49 | }; |
bsalomon | 6df8640 | 2015-06-01 10:41:49 -0700 | [diff] [blame] | 50 | |
| 51 | void GrGLAttribArrayState::set(GrGLGpu* gpu, |
bsalomon@google.com | 6918d48 | 2013-03-07 19:09:11 +0000 | [diff] [blame] | 52 | int index, |
csmartdalton | 485a120 | 2016-07-13 10:16:32 -0700 | [diff] [blame] | 53 | const GrBuffer* vertexBuffer, |
cdalton | 793dc26 | 2016-02-08 10:11:47 -0800 | [diff] [blame] | 54 | GrVertexAttribType type, |
bsalomon@google.com | 6918d48 | 2013-03-07 19:09:11 +0000 | [diff] [blame] | 55 | GrGLsizei stride, |
Chris Dalton | 1d61635 | 2017-05-31 12:51:23 -0600 | [diff] [blame] | 56 | size_t offsetInBytes, |
| 57 | int divisor) { |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 58 | SkASSERT(index >= 0 && index < fAttribArrayStates.count()); |
Chris Dalton | 1d61635 | 2017-05-31 12:51:23 -0600 | [diff] [blame] | 59 | SkASSERT(0 == divisor || gpu->caps()->instanceAttribSupport()); |
bsalomon@google.com | 6918d48 | 2013-03-07 19:09:11 +0000 | [diff] [blame] | 60 | AttribArrayState* array = &fAttribArrayStates[index]; |
robertphillips | 8abb370 | 2016-08-31 14:04:06 -0700 | [diff] [blame] | 61 | if (array->fVertexBufferUniqueID != vertexBuffer->uniqueID() || |
cdalton | 793dc26 | 2016-02-08 10:11:47 -0800 | [diff] [blame] | 62 | array->fType != type || |
bsalomon@google.com | 6918d48 | 2013-03-07 19:09:11 +0000 | [diff] [blame] | 63 | array->fStride != stride || |
Chris Dalton | 8e45b4f | 2017-05-05 14:00:56 -0400 | [diff] [blame] | 64 | array->fOffset != offsetInBytes) { |
cdalton | e2e71c2 | 2016-04-07 18:13:29 -0700 | [diff] [blame] | 65 | gpu->bindBuffer(kVertex_GrBufferType, vertexBuffer); |
csmartdalton | b37cb23 | 2017-02-08 14:56:27 -0500 | [diff] [blame] | 66 | const AttribLayout& layout = attrib_layout(type); |
Chris Dalton | 8e45b4f | 2017-05-05 14:00:56 -0400 | [diff] [blame] | 67 | const GrGLvoid* offsetAsPtr = reinterpret_cast<const GrGLvoid*>(offsetInBytes); |
cdalton | 793dc26 | 2016-02-08 10:11:47 -0800 | [diff] [blame] | 68 | if (!GrVertexAttribTypeIsIntType(type)) { |
| 69 | GR_GL_CALL(gpu->glInterface(), VertexAttribPointer(index, |
| 70 | layout.fCount, |
| 71 | layout.fType, |
| 72 | layout.fNormalized, |
| 73 | stride, |
Chris Dalton | 8e45b4f | 2017-05-05 14:00:56 -0400 | [diff] [blame] | 74 | offsetAsPtr)); |
cdalton | 793dc26 | 2016-02-08 10:11:47 -0800 | [diff] [blame] | 75 | } else { |
| 76 | SkASSERT(gpu->caps()->shaderCaps()->integerSupport()); |
| 77 | SkASSERT(!layout.fNormalized); |
| 78 | GR_GL_CALL(gpu->glInterface(), VertexAttribIPointer(index, |
| 79 | layout.fCount, |
| 80 | layout.fType, |
| 81 | stride, |
Chris Dalton | 8e45b4f | 2017-05-05 14:00:56 -0400 | [diff] [blame] | 82 | offsetAsPtr)); |
cdalton | 793dc26 | 2016-02-08 10:11:47 -0800 | [diff] [blame] | 83 | } |
robertphillips | 8abb370 | 2016-08-31 14:04:06 -0700 | [diff] [blame] | 84 | array->fVertexBufferUniqueID = vertexBuffer->uniqueID(); |
cdalton | 793dc26 | 2016-02-08 10:11:47 -0800 | [diff] [blame] | 85 | array->fType = type; |
dcheng | c4d196c | 2016-02-06 15:08:54 -0800 | [diff] [blame] | 86 | array->fStride = stride; |
Chris Dalton | 8e45b4f | 2017-05-05 14:00:56 -0400 | [diff] [blame] | 87 | array->fOffset = offsetInBytes; |
bsalomon@google.com | 6918d48 | 2013-03-07 19:09:11 +0000 | [diff] [blame] | 88 | } |
Chris Dalton | 1d61635 | 2017-05-31 12:51:23 -0600 | [diff] [blame] | 89 | if (gpu->caps()->instanceAttribSupport() && array->fDivisor != divisor) { |
| 90 | SkASSERT(0 == divisor || 1 == divisor); // not necessarily a requirement but what we expect. |
| 91 | GR_GL_CALL(gpu->glInterface(), VertexAttribDivisor(index, divisor)); |
| 92 | array->fDivisor = divisor; |
| 93 | } |
bsalomon@google.com | 6918d48 | 2013-03-07 19:09:11 +0000 | [diff] [blame] | 94 | } |
| 95 | |
Chris Dalton | 8e45b4f | 2017-05-05 14:00:56 -0400 | [diff] [blame] | 96 | void GrGLAttribArrayState::enableVertexArrays(const GrGLGpu* gpu, int enabledCount) { |
| 97 | SkASSERT(enabledCount <= fAttribArrayStates.count()); |
Chris Dalton | 1d61635 | 2017-05-31 12:51:23 -0600 | [diff] [blame] | 98 | if (fEnabledCountIsValid && enabledCount == fNumEnabledArrays) { |
| 99 | return; |
| 100 | } |
Chris Dalton | 8e45b4f | 2017-05-05 14:00:56 -0400 | [diff] [blame] | 101 | |
| 102 | int firstIdxToEnable = fEnabledCountIsValid ? fNumEnabledArrays : 0; |
| 103 | for (int i = firstIdxToEnable; i < enabledCount; ++i) { |
| 104 | GR_GL_CALL(gpu->glInterface(), EnableVertexAttribArray(i)); |
bsalomon@google.com | 6918d48 | 2013-03-07 19:09:11 +0000 | [diff] [blame] | 105 | } |
Chris Dalton | 8e45b4f | 2017-05-05 14:00:56 -0400 | [diff] [blame] | 106 | |
| 107 | int endIdxToDisable = fEnabledCountIsValid ? fNumEnabledArrays : fAttribArrayStates.count(); |
| 108 | for (int i = enabledCount; i < endIdxToDisable; ++i) { |
| 109 | GR_GL_CALL(gpu->glInterface(), DisableVertexAttribArray(i)); |
| 110 | } |
| 111 | |
| 112 | fNumEnabledArrays = enabledCount; |
| 113 | fEnabledCountIsValid = true; |
bsalomon@google.com | 6918d48 | 2013-03-07 19:09:11 +0000 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 117 | |
bsalomon | 8780bc6 | 2015-05-13 09:56:37 -0700 | [diff] [blame] | 118 | GrGLVertexArray::GrGLVertexArray(GrGLint id, int attribCount) |
| 119 | : fID(id) |
bsalomon@google.com | 6918d48 | 2013-03-07 19:09:11 +0000 | [diff] [blame] | 120 | , fAttribArrays(attribCount) |
cdalton | e2e71c2 | 2016-04-07 18:13:29 -0700 | [diff] [blame] | 121 | , fIndexBufferUniqueID(SK_InvalidUniqueID) { |
bsalomon@google.com | 6918d48 | 2013-03-07 19:09:11 +0000 | [diff] [blame] | 122 | } |
| 123 | |
bsalomon | 8780bc6 | 2015-05-13 09:56:37 -0700 | [diff] [blame] | 124 | GrGLAttribArrayState* GrGLVertexArray::bind(GrGLGpu* gpu) { |
bsalomon@google.com | 6918d48 | 2013-03-07 19:09:11 +0000 | [diff] [blame] | 125 | if (0 == fID) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 126 | return nullptr; |
bsalomon@google.com | 6918d48 | 2013-03-07 19:09:11 +0000 | [diff] [blame] | 127 | } |
bsalomon | 8780bc6 | 2015-05-13 09:56:37 -0700 | [diff] [blame] | 128 | gpu->bindVertexArray(fID); |
bsalomon@google.com | 6918d48 | 2013-03-07 19:09:11 +0000 | [diff] [blame] | 129 | return &fAttribArrays; |
| 130 | } |
skia.committer@gmail.com | 754a3eb | 2013-03-08 07:01:25 +0000 | [diff] [blame] | 131 | |
csmartdalton | 485a120 | 2016-07-13 10:16:32 -0700 | [diff] [blame] | 132 | GrGLAttribArrayState* GrGLVertexArray::bindWithIndexBuffer(GrGLGpu* gpu, const GrBuffer* ibuff) { |
bsalomon | 8780bc6 | 2015-05-13 09:56:37 -0700 | [diff] [blame] | 133 | GrGLAttribArrayState* state = this->bind(gpu); |
robertphillips | 8abb370 | 2016-08-31 14:04:06 -0700 | [diff] [blame] | 134 | if (state && fIndexBufferUniqueID != ibuff->uniqueID()) { |
csmartdalton | 485a120 | 2016-07-13 10:16:32 -0700 | [diff] [blame] | 135 | if (ibuff->isCPUBacked()) { |
| 136 | GR_GL_CALL(gpu->glInterface(), BindBuffer(GR_GL_ELEMENT_ARRAY_BUFFER, 0)); |
| 137 | } else { |
| 138 | const GrGLBuffer* glBuffer = static_cast<const GrGLBuffer*>(ibuff); |
| 139 | GR_GL_CALL(gpu->glInterface(), BindBuffer(GR_GL_ELEMENT_ARRAY_BUFFER, |
| 140 | glBuffer->bufferID())); |
| 141 | } |
robertphillips | 8abb370 | 2016-08-31 14:04:06 -0700 | [diff] [blame] | 142 | fIndexBufferUniqueID = ibuff->uniqueID(); |
bsalomon@google.com | 6918d48 | 2013-03-07 19:09:11 +0000 | [diff] [blame] | 143 | } |
| 144 | return state; |
| 145 | } |
| 146 | |
bsalomon@google.com | 6918d48 | 2013-03-07 19:09:11 +0000 | [diff] [blame] | 147 | void GrGLVertexArray::invalidateCachedState() { |
commit-bot@chromium.org | ce6da4d | 2013-09-09 14:55:37 +0000 | [diff] [blame] | 148 | fAttribArrays.invalidate(); |
Robert Phillips | 294870f | 2016-11-11 12:38:40 -0500 | [diff] [blame] | 149 | fIndexBufferUniqueID.makeInvalid(); |
bsalomon@google.com | 6918d48 | 2013-03-07 19:09:11 +0000 | [diff] [blame] | 150 | } |