blob: dbfa0f4f4a73f9004007e786a6558d27c4c9c167 [file] [log] [blame]
bsalomon@google.com6918d482013-03-07 19:09:11 +00001/*
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"
cdaltone2e71c22016-04-07 18:13:29 -07009#include "GrGLBuffer.h"
jvanverth39edf762014-12-22 11:44:19 -080010#include "GrGLGpu.h"
bsalomon@google.com6918d482013-03-07 19:09:11 +000011
cdalton793dc262016-02-08 10:11:47 -080012struct AttribLayout {
csmartdaltonb37cb232017-02-08 14:56:27 -050013 bool fNormalized; // Only used by floating point types.
14 uint8_t fCount;
15 uint16_t fType;
cdalton793dc262016-02-08 10:11:47 -080016};
bsalomon@google.com6918d482013-03-07 19:09:11 +000017
csmartdaltonb37cb232017-02-08 14:56:27 -050018GR_STATIC_ASSERT(4 == sizeof(AttribLayout));
cdalton793dc262016-02-08 10:11:47 -080019
csmartdaltonb37cb232017-02-08 14:56:27 -050020static AttribLayout attrib_layout(GrVertexAttribType type) {
21 switch (type) {
22 case kFloat_GrVertexAttribType:
23 return {false, 1, GR_GL_FLOAT};
Ethan Nicholasfa7ee242017-09-25 09:52:04 -040024 case kFloat2_GrVertexAttribType:
csmartdaltonb37cb232017-02-08 14:56:27 -050025 return {false, 2, GR_GL_FLOAT};
Ethan Nicholasfa7ee242017-09-25 09:52:04 -040026 case kFloat3_GrVertexAttribType:
csmartdaltonb37cb232017-02-08 14:56:27 -050027 return {false, 3, GR_GL_FLOAT};
Ethan Nicholasfa7ee242017-09-25 09:52:04 -040028 case kFloat4_GrVertexAttribType:
csmartdaltonb37cb232017-02-08 14:56:27 -050029 return {false, 4, GR_GL_FLOAT};
Ethan Nicholasfa7ee242017-09-25 09:52:04 -040030 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:
csmartdaltonb37cb232017-02-08 14:56:27 -050039 return {false, 2, GR_GL_INT};
Ethan Nicholasfa7ee242017-09-25 09:52:04 -040040 case kInt3_GrVertexAttribType:
csmartdaltonb37cb232017-02-08 14:56:27 -050041 return {false, 3, GR_GL_INT};
Ethan Nicholasfa7ee242017-09-25 09:52:04 -040042 case kInt4_GrVertexAttribType:
csmartdaltonb37cb232017-02-08 14:56:27 -050043 return {false, 4, GR_GL_INT};
Ethan Nicholasfa7ee242017-09-25 09:52:04 -040044 case kUByte_norm_GrVertexAttribType:
csmartdaltonb37cb232017-02-08 14:56:27 -050045 return {true, 1, GR_GL_UNSIGNED_BYTE};
Ethan Nicholasfa7ee242017-09-25 09:52:04 -040046 case kUByte4_norm_GrVertexAttribType:
csmartdaltonb37cb232017-02-08 14:56:27 -050047 return {true, 4, GR_GL_UNSIGNED_BYTE};
Chris Daltona045eea2017-10-24 13:22:10 -060048 case kShort2_GrVertexAttribType:
49 return {false, 2, GR_GL_SHORT};
Ethan Nicholasfa7ee242017-09-25 09:52:04 -040050 case kUShort2_GrVertexAttribType:
Robert Phillips8296e752017-08-25 08:45:21 -040051 return {false, 2, GR_GL_UNSIGNED_SHORT};
Chris Daltona045eea2017-10-24 13:22:10 -060052 case kUShort2_norm_GrVertexAttribType:
53 return {true, 2, GR_GL_UNSIGNED_SHORT};
csmartdaltonb37cb232017-02-08 14:56:27 -050054 case kInt_GrVertexAttribType:
55 return {false, 1, GR_GL_INT};
56 case kUint_GrVertexAttribType:
57 return {false, 1, GR_GL_UNSIGNED_INT};
58 }
Ben Wagnerb4aab9a2017-08-16 10:53:04 -040059 SK_ABORT("Unknown vertex attrib type");
csmartdaltonb37cb232017-02-08 14:56:27 -050060 return {false, 0, 0};
61};
bsalomon6df86402015-06-01 10:41:49 -070062
Robert Phillips8296e752017-08-25 08:45:21 -040063static bool GrVertexAttribTypeIsIntType(const GrShaderCaps* shaderCaps,
64 GrVertexAttribType type) {
65 switch (type) {
66 case kFloat_GrVertexAttribType:
67 return false;
Ethan Nicholasfa7ee242017-09-25 09:52:04 -040068 case kFloat2_GrVertexAttribType:
Robert Phillips8296e752017-08-25 08:45:21 -040069 return false;
Ethan Nicholasfa7ee242017-09-25 09:52:04 -040070 case kFloat3_GrVertexAttribType:
Robert Phillips8296e752017-08-25 08:45:21 -040071 return false;
Ethan Nicholasfa7ee242017-09-25 09:52:04 -040072 case kFloat4_GrVertexAttribType:
Robert Phillips8296e752017-08-25 08:45:21 -040073 return false;
Ethan Nicholasfa7ee242017-09-25 09:52:04 -040074 case kHalf_GrVertexAttribType:
75 return false;
76 case kHalf2_GrVertexAttribType:
77 return false;
78 case kHalf3_GrVertexAttribType:
79 return false;
80 case kHalf4_GrVertexAttribType:
81 return false;
82 case kInt2_GrVertexAttribType:
Robert Phillips8296e752017-08-25 08:45:21 -040083 return true;
Ethan Nicholasfa7ee242017-09-25 09:52:04 -040084 case kInt3_GrVertexAttribType:
Robert Phillips8296e752017-08-25 08:45:21 -040085 return true;
Ethan Nicholasfa7ee242017-09-25 09:52:04 -040086 case kInt4_GrVertexAttribType:
Robert Phillips8296e752017-08-25 08:45:21 -040087 return true;
Ethan Nicholasfa7ee242017-09-25 09:52:04 -040088 case kUByte_norm_GrVertexAttribType:
Robert Phillips8296e752017-08-25 08:45:21 -040089 return false;
Ethan Nicholasfa7ee242017-09-25 09:52:04 -040090 case kUByte4_norm_GrVertexAttribType:
Robert Phillips8296e752017-08-25 08:45:21 -040091 return false;
Chris Daltona045eea2017-10-24 13:22:10 -060092 case kShort2_GrVertexAttribType:
93 return true;
94 case kUShort2_GrVertexAttribType:
95 return shaderCaps->integerSupport(); // FIXME: caller should handle this.
Ethan Nicholasfa7ee242017-09-25 09:52:04 -040096 case kUShort2_norm_GrVertexAttribType:
Robert Phillips8296e752017-08-25 08:45:21 -040097 return false;
Robert Phillips8296e752017-08-25 08:45:21 -040098 case kInt_GrVertexAttribType:
99 return true;
100 case kUint_GrVertexAttribType:
101 return true;
102 }
103 SK_ABORT("Unexpected attribute type");
104 return false;
105}
106
bsalomon6df86402015-06-01 10:41:49 -0700107void GrGLAttribArrayState::set(GrGLGpu* gpu,
bsalomon@google.com6918d482013-03-07 19:09:11 +0000108 int index,
csmartdalton485a1202016-07-13 10:16:32 -0700109 const GrBuffer* vertexBuffer,
cdalton793dc262016-02-08 10:11:47 -0800110 GrVertexAttribType type,
bsalomon@google.com6918d482013-03-07 19:09:11 +0000111 GrGLsizei stride,
Chris Dalton1d616352017-05-31 12:51:23 -0600112 size_t offsetInBytes,
113 int divisor) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000114 SkASSERT(index >= 0 && index < fAttribArrayStates.count());
Chris Dalton1d616352017-05-31 12:51:23 -0600115 SkASSERT(0 == divisor || gpu->caps()->instanceAttribSupport());
bsalomon@google.com6918d482013-03-07 19:09:11 +0000116 AttribArrayState* array = &fAttribArrayStates[index];
robertphillips8abb3702016-08-31 14:04:06 -0700117 if (array->fVertexBufferUniqueID != vertexBuffer->uniqueID() ||
cdalton793dc262016-02-08 10:11:47 -0800118 array->fType != type ||
bsalomon@google.com6918d482013-03-07 19:09:11 +0000119 array->fStride != stride ||
Chris Dalton8e45b4f2017-05-05 14:00:56 -0400120 array->fOffset != offsetInBytes) {
cdaltone2e71c22016-04-07 18:13:29 -0700121 gpu->bindBuffer(kVertex_GrBufferType, vertexBuffer);
csmartdaltonb37cb232017-02-08 14:56:27 -0500122 const AttribLayout& layout = attrib_layout(type);
Chris Dalton8e45b4f2017-05-05 14:00:56 -0400123 const GrGLvoid* offsetAsPtr = reinterpret_cast<const GrGLvoid*>(offsetInBytes);
Robert Phillips8296e752017-08-25 08:45:21 -0400124 if (!GrVertexAttribTypeIsIntType(gpu->caps()->shaderCaps(), type)) {
cdalton793dc262016-02-08 10:11:47 -0800125 GR_GL_CALL(gpu->glInterface(), VertexAttribPointer(index,
126 layout.fCount,
127 layout.fType,
128 layout.fNormalized,
129 stride,
Chris Dalton8e45b4f2017-05-05 14:00:56 -0400130 offsetAsPtr));
cdalton793dc262016-02-08 10:11:47 -0800131 } else {
132 SkASSERT(gpu->caps()->shaderCaps()->integerSupport());
133 SkASSERT(!layout.fNormalized);
134 GR_GL_CALL(gpu->glInterface(), VertexAttribIPointer(index,
135 layout.fCount,
136 layout.fType,
137 stride,
Chris Dalton8e45b4f2017-05-05 14:00:56 -0400138 offsetAsPtr));
cdalton793dc262016-02-08 10:11:47 -0800139 }
robertphillips8abb3702016-08-31 14:04:06 -0700140 array->fVertexBufferUniqueID = vertexBuffer->uniqueID();
cdalton793dc262016-02-08 10:11:47 -0800141 array->fType = type;
dchengc4d196c2016-02-06 15:08:54 -0800142 array->fStride = stride;
Chris Dalton8e45b4f2017-05-05 14:00:56 -0400143 array->fOffset = offsetInBytes;
bsalomon@google.com6918d482013-03-07 19:09:11 +0000144 }
Chris Dalton1d616352017-05-31 12:51:23 -0600145 if (gpu->caps()->instanceAttribSupport() && array->fDivisor != divisor) {
146 SkASSERT(0 == divisor || 1 == divisor); // not necessarily a requirement but what we expect.
147 GR_GL_CALL(gpu->glInterface(), VertexAttribDivisor(index, divisor));
148 array->fDivisor = divisor;
149 }
bsalomon@google.com6918d482013-03-07 19:09:11 +0000150}
151
Chris Dalton27059d32018-01-23 14:06:50 -0700152void GrGLAttribArrayState::enableVertexArrays(const GrGLGpu* gpu, int enabledCount,
153 EnablePrimitiveRestart enablePrimitiveRestart) {
Chris Dalton8e45b4f2017-05-05 14:00:56 -0400154 SkASSERT(enabledCount <= fAttribArrayStates.count());
Chris Dalton27059d32018-01-23 14:06:50 -0700155
156 if (!fEnableStateIsValid || enabledCount != fNumEnabledArrays) {
157 int firstIdxToEnable = fEnableStateIsValid ? fNumEnabledArrays : 0;
158 for (int i = firstIdxToEnable; i < enabledCount; ++i) {
159 GR_GL_CALL(gpu->glInterface(), EnableVertexAttribArray(i));
160 }
161
162 int endIdxToDisable = fEnableStateIsValid ? fNumEnabledArrays : fAttribArrayStates.count();
163 for (int i = enabledCount; i < endIdxToDisable; ++i) {
164 GR_GL_CALL(gpu->glInterface(), DisableVertexAttribArray(i));
165 }
166
167 fNumEnabledArrays = enabledCount;
Chris Dalton1d616352017-05-31 12:51:23 -0600168 }
Chris Dalton8e45b4f2017-05-05 14:00:56 -0400169
Chris Dalton27059d32018-01-23 14:06:50 -0700170 SkASSERT(EnablePrimitiveRestart::kNo == enablePrimitiveRestart ||
171 gpu->caps()->usePrimitiveRestart());
172
173 if (gpu->caps()->usePrimitiveRestart() &&
174 (!fEnableStateIsValid || enablePrimitiveRestart != fPrimitiveRestartEnabled)) {
175 if (EnablePrimitiveRestart::kYes == enablePrimitiveRestart) {
176 GR_GL_CALL(gpu->glInterface(), Enable(GR_GL_PRIMITIVE_RESTART_FIXED_INDEX));
177 } else {
178 GR_GL_CALL(gpu->glInterface(), Disable(GR_GL_PRIMITIVE_RESTART_FIXED_INDEX));
179 }
180
181 fPrimitiveRestartEnabled = enablePrimitiveRestart;
bsalomon@google.com6918d482013-03-07 19:09:11 +0000182 }
Chris Dalton8e45b4f2017-05-05 14:00:56 -0400183
Chris Dalton27059d32018-01-23 14:06:50 -0700184 fEnableStateIsValid = true;
bsalomon@google.com6918d482013-03-07 19:09:11 +0000185}
186
187///////////////////////////////////////////////////////////////////////////////////////////////////
188
bsalomon8780bc62015-05-13 09:56:37 -0700189GrGLVertexArray::GrGLVertexArray(GrGLint id, int attribCount)
190 : fID(id)
bsalomon@google.com6918d482013-03-07 19:09:11 +0000191 , fAttribArrays(attribCount)
cdaltone2e71c22016-04-07 18:13:29 -0700192 , fIndexBufferUniqueID(SK_InvalidUniqueID) {
bsalomon@google.com6918d482013-03-07 19:09:11 +0000193}
194
bsalomon8780bc62015-05-13 09:56:37 -0700195GrGLAttribArrayState* GrGLVertexArray::bind(GrGLGpu* gpu) {
bsalomon@google.com6918d482013-03-07 19:09:11 +0000196 if (0 == fID) {
halcanary96fcdcc2015-08-27 07:41:13 -0700197 return nullptr;
bsalomon@google.com6918d482013-03-07 19:09:11 +0000198 }
bsalomon8780bc62015-05-13 09:56:37 -0700199 gpu->bindVertexArray(fID);
bsalomon@google.com6918d482013-03-07 19:09:11 +0000200 return &fAttribArrays;
201}
skia.committer@gmail.com754a3eb2013-03-08 07:01:25 +0000202
csmartdalton485a1202016-07-13 10:16:32 -0700203GrGLAttribArrayState* GrGLVertexArray::bindWithIndexBuffer(GrGLGpu* gpu, const GrBuffer* ibuff) {
bsalomon8780bc62015-05-13 09:56:37 -0700204 GrGLAttribArrayState* state = this->bind(gpu);
robertphillips8abb3702016-08-31 14:04:06 -0700205 if (state && fIndexBufferUniqueID != ibuff->uniqueID()) {
csmartdalton485a1202016-07-13 10:16:32 -0700206 if (ibuff->isCPUBacked()) {
207 GR_GL_CALL(gpu->glInterface(), BindBuffer(GR_GL_ELEMENT_ARRAY_BUFFER, 0));
208 } else {
209 const GrGLBuffer* glBuffer = static_cast<const GrGLBuffer*>(ibuff);
210 GR_GL_CALL(gpu->glInterface(), BindBuffer(GR_GL_ELEMENT_ARRAY_BUFFER,
211 glBuffer->bufferID()));
212 }
robertphillips8abb3702016-08-31 14:04:06 -0700213 fIndexBufferUniqueID = ibuff->uniqueID();
bsalomon@google.com6918d482013-03-07 19:09:11 +0000214 }
215 return state;
216}
217
bsalomon@google.com6918d482013-03-07 19:09:11 +0000218void GrGLVertexArray::invalidateCachedState() {
commit-bot@chromium.orgce6da4d2013-09-09 14:55:37 +0000219 fAttribArrays.invalidate();
Robert Phillips294870f2016-11-11 12:38:40 -0500220 fIndexBufferUniqueID.makeInvalid();
bsalomon@google.com6918d482013-03-07 19:09:11 +0000221}