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}; |
Ethan Nicholas | fa7ee24 | 2017-09-25 09:52:04 -0400 | [diff] [blame] | 24 | case kFloat2_GrVertexAttribType: |
csmartdalton | b37cb23 | 2017-02-08 14:56:27 -0500 | [diff] [blame] | 25 | return {false, 2, GR_GL_FLOAT}; |
Ethan Nicholas | fa7ee24 | 2017-09-25 09:52:04 -0400 | [diff] [blame] | 26 | case kFloat3_GrVertexAttribType: |
csmartdalton | b37cb23 | 2017-02-08 14:56:27 -0500 | [diff] [blame] | 27 | return {false, 3, GR_GL_FLOAT}; |
Ethan Nicholas | fa7ee24 | 2017-09-25 09:52:04 -0400 | [diff] [blame] | 28 | case kFloat4_GrVertexAttribType: |
csmartdalton | b37cb23 | 2017-02-08 14:56:27 -0500 | [diff] [blame] | 29 | return {false, 4, GR_GL_FLOAT}; |
Ethan Nicholas | fa7ee24 | 2017-09-25 09:52:04 -0400 | [diff] [blame] | 30 | case kHalf_GrVertexAttribType: |
| 31 | return {false, 1, GR_GL_FLOAT}; |
| 32 | case kHalf2_GrVertexAttribType: |
| 33 | return {false, 2, GR_GL_FLOAT}; |
| 34 | case kHalf3_GrVertexAttribType: |
| 35 | return {false, 3, GR_GL_FLOAT}; |
| 36 | case kHalf4_GrVertexAttribType: |
| 37 | return {false, 4, GR_GL_FLOAT}; |
| 38 | case kInt2_GrVertexAttribType: |
csmartdalton | b37cb23 | 2017-02-08 14:56:27 -0500 | [diff] [blame] | 39 | return {false, 2, GR_GL_INT}; |
Ethan Nicholas | fa7ee24 | 2017-09-25 09:52:04 -0400 | [diff] [blame] | 40 | case kInt3_GrVertexAttribType: |
csmartdalton | b37cb23 | 2017-02-08 14:56:27 -0500 | [diff] [blame] | 41 | return {false, 3, GR_GL_INT}; |
Ethan Nicholas | fa7ee24 | 2017-09-25 09:52:04 -0400 | [diff] [blame] | 42 | case kInt4_GrVertexAttribType: |
csmartdalton | b37cb23 | 2017-02-08 14:56:27 -0500 | [diff] [blame] | 43 | return {false, 4, GR_GL_INT}; |
Ethan Nicholas | fa7ee24 | 2017-09-25 09:52:04 -0400 | [diff] [blame] | 44 | case kUByte_norm_GrVertexAttribType: |
csmartdalton | b37cb23 | 2017-02-08 14:56:27 -0500 | [diff] [blame] | 45 | return {true, 1, GR_GL_UNSIGNED_BYTE}; |
Ethan Nicholas | fa7ee24 | 2017-09-25 09:52:04 -0400 | [diff] [blame] | 46 | case kUByte4_norm_GrVertexAttribType: |
csmartdalton | b37cb23 | 2017-02-08 14:56:27 -0500 | [diff] [blame] | 47 | return {true, 4, GR_GL_UNSIGNED_BYTE}; |
Ethan Nicholas | fa7ee24 | 2017-09-25 09:52:04 -0400 | [diff] [blame] | 48 | case kUShort2_norm_GrVertexAttribType: |
csmartdalton | b37cb23 | 2017-02-08 14:56:27 -0500 | [diff] [blame] | 49 | return {true, 2, GR_GL_UNSIGNED_SHORT}; |
Ethan Nicholas | fa7ee24 | 2017-09-25 09:52:04 -0400 | [diff] [blame] | 50 | case kUShort2_GrVertexAttribType: |
Robert Phillips | 8296e75 | 2017-08-25 08:45:21 -0400 | [diff] [blame] | 51 | return {false, 2, GR_GL_UNSIGNED_SHORT}; |
csmartdalton | b37cb23 | 2017-02-08 14:56:27 -0500 | [diff] [blame] | 52 | case kInt_GrVertexAttribType: |
| 53 | return {false, 1, GR_GL_INT}; |
| 54 | case kUint_GrVertexAttribType: |
| 55 | return {false, 1, GR_GL_UNSIGNED_INT}; |
| 56 | } |
Ben Wagner | b4aab9a | 2017-08-16 10:53:04 -0400 | [diff] [blame] | 57 | SK_ABORT("Unknown vertex attrib type"); |
csmartdalton | b37cb23 | 2017-02-08 14:56:27 -0500 | [diff] [blame] | 58 | return {false, 0, 0}; |
| 59 | }; |
bsalomon | 6df8640 | 2015-06-01 10:41:49 -0700 | [diff] [blame] | 60 | |
Robert Phillips | 8296e75 | 2017-08-25 08:45:21 -0400 | [diff] [blame] | 61 | static bool GrVertexAttribTypeIsIntType(const GrShaderCaps* shaderCaps, |
| 62 | GrVertexAttribType type) { |
| 63 | switch (type) { |
| 64 | case kFloat_GrVertexAttribType: |
| 65 | return false; |
Ethan Nicholas | fa7ee24 | 2017-09-25 09:52:04 -0400 | [diff] [blame] | 66 | case kFloat2_GrVertexAttribType: |
Robert Phillips | 8296e75 | 2017-08-25 08:45:21 -0400 | [diff] [blame] | 67 | return false; |
Ethan Nicholas | fa7ee24 | 2017-09-25 09:52:04 -0400 | [diff] [blame] | 68 | case kFloat3_GrVertexAttribType: |
Robert Phillips | 8296e75 | 2017-08-25 08:45:21 -0400 | [diff] [blame] | 69 | return false; |
Ethan Nicholas | fa7ee24 | 2017-09-25 09:52:04 -0400 | [diff] [blame] | 70 | case kFloat4_GrVertexAttribType: |
Robert Phillips | 8296e75 | 2017-08-25 08:45:21 -0400 | [diff] [blame] | 71 | return false; |
Ethan Nicholas | fa7ee24 | 2017-09-25 09:52:04 -0400 | [diff] [blame] | 72 | case kHalf_GrVertexAttribType: |
| 73 | return false; |
| 74 | case kHalf2_GrVertexAttribType: |
| 75 | return false; |
| 76 | case kHalf3_GrVertexAttribType: |
| 77 | return false; |
| 78 | case kHalf4_GrVertexAttribType: |
| 79 | return false; |
| 80 | case kInt2_GrVertexAttribType: |
Robert Phillips | 8296e75 | 2017-08-25 08:45:21 -0400 | [diff] [blame] | 81 | return true; |
Ethan Nicholas | fa7ee24 | 2017-09-25 09:52:04 -0400 | [diff] [blame] | 82 | case kInt3_GrVertexAttribType: |
Robert Phillips | 8296e75 | 2017-08-25 08:45:21 -0400 | [diff] [blame] | 83 | return true; |
Ethan Nicholas | fa7ee24 | 2017-09-25 09:52:04 -0400 | [diff] [blame] | 84 | case kInt4_GrVertexAttribType: |
Robert Phillips | 8296e75 | 2017-08-25 08:45:21 -0400 | [diff] [blame] | 85 | return true; |
Ethan Nicholas | fa7ee24 | 2017-09-25 09:52:04 -0400 | [diff] [blame] | 86 | case kUByte_norm_GrVertexAttribType: |
Robert Phillips | 8296e75 | 2017-08-25 08:45:21 -0400 | [diff] [blame] | 87 | return false; |
Ethan Nicholas | fa7ee24 | 2017-09-25 09:52:04 -0400 | [diff] [blame] | 88 | case kUByte4_norm_GrVertexAttribType: |
Robert Phillips | 8296e75 | 2017-08-25 08:45:21 -0400 | [diff] [blame] | 89 | return false; |
Ethan Nicholas | fa7ee24 | 2017-09-25 09:52:04 -0400 | [diff] [blame] | 90 | case kUShort2_norm_GrVertexAttribType: |
Robert Phillips | 8296e75 | 2017-08-25 08:45:21 -0400 | [diff] [blame] | 91 | return false; |
Ethan Nicholas | fa7ee24 | 2017-09-25 09:52:04 -0400 | [diff] [blame] | 92 | case kUShort2_GrVertexAttribType: |
Robert Phillips | 8296e75 | 2017-08-25 08:45:21 -0400 | [diff] [blame] | 93 | return shaderCaps->integerSupport(); |
| 94 | case kInt_GrVertexAttribType: |
| 95 | return true; |
| 96 | case kUint_GrVertexAttribType: |
| 97 | return true; |
| 98 | } |
| 99 | SK_ABORT("Unexpected attribute type"); |
| 100 | return false; |
| 101 | } |
| 102 | |
bsalomon | 6df8640 | 2015-06-01 10:41:49 -0700 | [diff] [blame] | 103 | void GrGLAttribArrayState::set(GrGLGpu* gpu, |
bsalomon@google.com | 6918d48 | 2013-03-07 19:09:11 +0000 | [diff] [blame] | 104 | int index, |
csmartdalton | 485a120 | 2016-07-13 10:16:32 -0700 | [diff] [blame] | 105 | const GrBuffer* vertexBuffer, |
cdalton | 793dc26 | 2016-02-08 10:11:47 -0800 | [diff] [blame] | 106 | GrVertexAttribType type, |
bsalomon@google.com | 6918d48 | 2013-03-07 19:09:11 +0000 | [diff] [blame] | 107 | GrGLsizei stride, |
Chris Dalton | 1d61635 | 2017-05-31 12:51:23 -0600 | [diff] [blame] | 108 | size_t offsetInBytes, |
| 109 | int divisor) { |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 110 | SkASSERT(index >= 0 && index < fAttribArrayStates.count()); |
Chris Dalton | 1d61635 | 2017-05-31 12:51:23 -0600 | [diff] [blame] | 111 | SkASSERT(0 == divisor || gpu->caps()->instanceAttribSupport()); |
bsalomon@google.com | 6918d48 | 2013-03-07 19:09:11 +0000 | [diff] [blame] | 112 | AttribArrayState* array = &fAttribArrayStates[index]; |
robertphillips | 8abb370 | 2016-08-31 14:04:06 -0700 | [diff] [blame] | 113 | if (array->fVertexBufferUniqueID != vertexBuffer->uniqueID() || |
cdalton | 793dc26 | 2016-02-08 10:11:47 -0800 | [diff] [blame] | 114 | array->fType != type || |
bsalomon@google.com | 6918d48 | 2013-03-07 19:09:11 +0000 | [diff] [blame] | 115 | array->fStride != stride || |
Chris Dalton | 8e45b4f | 2017-05-05 14:00:56 -0400 | [diff] [blame] | 116 | array->fOffset != offsetInBytes) { |
cdalton | e2e71c2 | 2016-04-07 18:13:29 -0700 | [diff] [blame] | 117 | gpu->bindBuffer(kVertex_GrBufferType, vertexBuffer); |
csmartdalton | b37cb23 | 2017-02-08 14:56:27 -0500 | [diff] [blame] | 118 | const AttribLayout& layout = attrib_layout(type); |
Chris Dalton | 8e45b4f | 2017-05-05 14:00:56 -0400 | [diff] [blame] | 119 | const GrGLvoid* offsetAsPtr = reinterpret_cast<const GrGLvoid*>(offsetInBytes); |
Robert Phillips | 8296e75 | 2017-08-25 08:45:21 -0400 | [diff] [blame] | 120 | if (!GrVertexAttribTypeIsIntType(gpu->caps()->shaderCaps(), type)) { |
cdalton | 793dc26 | 2016-02-08 10:11:47 -0800 | [diff] [blame] | 121 | GR_GL_CALL(gpu->glInterface(), VertexAttribPointer(index, |
| 122 | layout.fCount, |
| 123 | layout.fType, |
| 124 | layout.fNormalized, |
| 125 | stride, |
Chris Dalton | 8e45b4f | 2017-05-05 14:00:56 -0400 | [diff] [blame] | 126 | offsetAsPtr)); |
cdalton | 793dc26 | 2016-02-08 10:11:47 -0800 | [diff] [blame] | 127 | } else { |
| 128 | SkASSERT(gpu->caps()->shaderCaps()->integerSupport()); |
| 129 | SkASSERT(!layout.fNormalized); |
| 130 | GR_GL_CALL(gpu->glInterface(), VertexAttribIPointer(index, |
| 131 | layout.fCount, |
| 132 | layout.fType, |
| 133 | stride, |
Chris Dalton | 8e45b4f | 2017-05-05 14:00:56 -0400 | [diff] [blame] | 134 | offsetAsPtr)); |
cdalton | 793dc26 | 2016-02-08 10:11:47 -0800 | [diff] [blame] | 135 | } |
robertphillips | 8abb370 | 2016-08-31 14:04:06 -0700 | [diff] [blame] | 136 | array->fVertexBufferUniqueID = vertexBuffer->uniqueID(); |
cdalton | 793dc26 | 2016-02-08 10:11:47 -0800 | [diff] [blame] | 137 | array->fType = type; |
dcheng | c4d196c | 2016-02-06 15:08:54 -0800 | [diff] [blame] | 138 | array->fStride = stride; |
Chris Dalton | 8e45b4f | 2017-05-05 14:00:56 -0400 | [diff] [blame] | 139 | array->fOffset = offsetInBytes; |
bsalomon@google.com | 6918d48 | 2013-03-07 19:09:11 +0000 | [diff] [blame] | 140 | } |
Chris Dalton | 1d61635 | 2017-05-31 12:51:23 -0600 | [diff] [blame] | 141 | if (gpu->caps()->instanceAttribSupport() && array->fDivisor != divisor) { |
| 142 | SkASSERT(0 == divisor || 1 == divisor); // not necessarily a requirement but what we expect. |
| 143 | GR_GL_CALL(gpu->glInterface(), VertexAttribDivisor(index, divisor)); |
| 144 | array->fDivisor = divisor; |
| 145 | } |
bsalomon@google.com | 6918d48 | 2013-03-07 19:09:11 +0000 | [diff] [blame] | 146 | } |
| 147 | |
Chris Dalton | 8e45b4f | 2017-05-05 14:00:56 -0400 | [diff] [blame] | 148 | void GrGLAttribArrayState::enableVertexArrays(const GrGLGpu* gpu, int enabledCount) { |
| 149 | SkASSERT(enabledCount <= fAttribArrayStates.count()); |
Chris Dalton | 1d61635 | 2017-05-31 12:51:23 -0600 | [diff] [blame] | 150 | if (fEnabledCountIsValid && enabledCount == fNumEnabledArrays) { |
| 151 | return; |
| 152 | } |
Chris Dalton | 8e45b4f | 2017-05-05 14:00:56 -0400 | [diff] [blame] | 153 | |
| 154 | int firstIdxToEnable = fEnabledCountIsValid ? fNumEnabledArrays : 0; |
| 155 | for (int i = firstIdxToEnable; i < enabledCount; ++i) { |
| 156 | GR_GL_CALL(gpu->glInterface(), EnableVertexAttribArray(i)); |
bsalomon@google.com | 6918d48 | 2013-03-07 19:09:11 +0000 | [diff] [blame] | 157 | } |
Chris Dalton | 8e45b4f | 2017-05-05 14:00:56 -0400 | [diff] [blame] | 158 | |
| 159 | int endIdxToDisable = fEnabledCountIsValid ? fNumEnabledArrays : fAttribArrayStates.count(); |
| 160 | for (int i = enabledCount; i < endIdxToDisable; ++i) { |
| 161 | GR_GL_CALL(gpu->glInterface(), DisableVertexAttribArray(i)); |
| 162 | } |
| 163 | |
| 164 | fNumEnabledArrays = enabledCount; |
| 165 | fEnabledCountIsValid = true; |
bsalomon@google.com | 6918d48 | 2013-03-07 19:09:11 +0000 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 169 | |
bsalomon | 8780bc6 | 2015-05-13 09:56:37 -0700 | [diff] [blame] | 170 | GrGLVertexArray::GrGLVertexArray(GrGLint id, int attribCount) |
| 171 | : fID(id) |
bsalomon@google.com | 6918d48 | 2013-03-07 19:09:11 +0000 | [diff] [blame] | 172 | , fAttribArrays(attribCount) |
cdalton | e2e71c2 | 2016-04-07 18:13:29 -0700 | [diff] [blame] | 173 | , fIndexBufferUniqueID(SK_InvalidUniqueID) { |
bsalomon@google.com | 6918d48 | 2013-03-07 19:09:11 +0000 | [diff] [blame] | 174 | } |
| 175 | |
bsalomon | 8780bc6 | 2015-05-13 09:56:37 -0700 | [diff] [blame] | 176 | GrGLAttribArrayState* GrGLVertexArray::bind(GrGLGpu* gpu) { |
bsalomon@google.com | 6918d48 | 2013-03-07 19:09:11 +0000 | [diff] [blame] | 177 | if (0 == fID) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 178 | return nullptr; |
bsalomon@google.com | 6918d48 | 2013-03-07 19:09:11 +0000 | [diff] [blame] | 179 | } |
bsalomon | 8780bc6 | 2015-05-13 09:56:37 -0700 | [diff] [blame] | 180 | gpu->bindVertexArray(fID); |
bsalomon@google.com | 6918d48 | 2013-03-07 19:09:11 +0000 | [diff] [blame] | 181 | return &fAttribArrays; |
| 182 | } |
skia.committer@gmail.com | 754a3eb | 2013-03-08 07:01:25 +0000 | [diff] [blame] | 183 | |
csmartdalton | 485a120 | 2016-07-13 10:16:32 -0700 | [diff] [blame] | 184 | GrGLAttribArrayState* GrGLVertexArray::bindWithIndexBuffer(GrGLGpu* gpu, const GrBuffer* ibuff) { |
bsalomon | 8780bc6 | 2015-05-13 09:56:37 -0700 | [diff] [blame] | 185 | GrGLAttribArrayState* state = this->bind(gpu); |
robertphillips | 8abb370 | 2016-08-31 14:04:06 -0700 | [diff] [blame] | 186 | if (state && fIndexBufferUniqueID != ibuff->uniqueID()) { |
csmartdalton | 485a120 | 2016-07-13 10:16:32 -0700 | [diff] [blame] | 187 | if (ibuff->isCPUBacked()) { |
| 188 | GR_GL_CALL(gpu->glInterface(), BindBuffer(GR_GL_ELEMENT_ARRAY_BUFFER, 0)); |
| 189 | } else { |
| 190 | const GrGLBuffer* glBuffer = static_cast<const GrGLBuffer*>(ibuff); |
| 191 | GR_GL_CALL(gpu->glInterface(), BindBuffer(GR_GL_ELEMENT_ARRAY_BUFFER, |
| 192 | glBuffer->bufferID())); |
| 193 | } |
robertphillips | 8abb370 | 2016-08-31 14:04:06 -0700 | [diff] [blame] | 194 | fIndexBufferUniqueID = ibuff->uniqueID(); |
bsalomon@google.com | 6918d48 | 2013-03-07 19:09:11 +0000 | [diff] [blame] | 195 | } |
| 196 | return state; |
| 197 | } |
| 198 | |
bsalomon@google.com | 6918d48 | 2013-03-07 19:09:11 +0000 | [diff] [blame] | 199 | void GrGLVertexArray::invalidateCachedState() { |
commit-bot@chromium.org | ce6da4d | 2013-09-09 14:55:37 +0000 | [diff] [blame] | 200 | fAttribArrays.invalidate(); |
Robert Phillips | 294870f | 2016-11-11 12:38:40 -0500 | [diff] [blame] | 201 | fIndexBufferUniqueID.makeInvalid(); |
bsalomon@google.com | 6918d48 | 2013-03-07 19:09:11 +0000 | [diff] [blame] | 202 | } |