blob: cfac573241220f12bff64b051aeca6bf8c42b5b8 [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "src/gpu/GrCpuBuffer.h"
9#include "src/gpu/gl/GrGLBuffer.h"
10#include "src/gpu/gl/GrGLGpu.h"
11#include "src/gpu/gl/GrGLVertexArray.h"
bsalomon@google.com6918d482013-03-07 19:09:11 +000012
cdalton793dc262016-02-08 10:11:47 -080013struct AttribLayout {
csmartdaltonb37cb232017-02-08 14:56:27 -050014 bool fNormalized; // Only used by floating point types.
15 uint8_t fCount;
16 uint16_t fType;
cdalton793dc262016-02-08 10:11:47 -080017};
bsalomon@google.com6918d482013-03-07 19:09:11 +000018
csmartdaltonb37cb232017-02-08 14:56:27 -050019GR_STATIC_ASSERT(4 == sizeof(AttribLayout));
cdalton793dc262016-02-08 10:11:47 -080020
csmartdaltonb37cb232017-02-08 14:56:27 -050021static AttribLayout attrib_layout(GrVertexAttribType type) {
22 switch (type) {
23 case kFloat_GrVertexAttribType:
24 return {false, 1, GR_GL_FLOAT};
Ethan Nicholasfa7ee242017-09-25 09:52:04 -040025 case kFloat2_GrVertexAttribType:
csmartdaltonb37cb232017-02-08 14:56:27 -050026 return {false, 2, GR_GL_FLOAT};
Ethan Nicholasfa7ee242017-09-25 09:52:04 -040027 case kFloat3_GrVertexAttribType:
csmartdaltonb37cb232017-02-08 14:56:27 -050028 return {false, 3, GR_GL_FLOAT};
Ethan Nicholasfa7ee242017-09-25 09:52:04 -040029 case kFloat4_GrVertexAttribType:
csmartdaltonb37cb232017-02-08 14:56:27 -050030 return {false, 4, GR_GL_FLOAT};
Ethan Nicholasfa7ee242017-09-25 09:52:04 -040031 case kHalf_GrVertexAttribType:
Brian Osmand4c29702018-09-14 16:16:55 -040032 return {false, 1, GR_GL_HALF_FLOAT};
Ethan Nicholasfa7ee242017-09-25 09:52:04 -040033 case kHalf2_GrVertexAttribType:
Brian Osmand4c29702018-09-14 16:16:55 -040034 return {false, 2, GR_GL_HALF_FLOAT};
Ethan Nicholasfa7ee242017-09-25 09:52:04 -040035 case kHalf3_GrVertexAttribType:
Brian Osmand4c29702018-09-14 16:16:55 -040036 return {false, 3, GR_GL_HALF_FLOAT};
Ethan Nicholasfa7ee242017-09-25 09:52:04 -040037 case kHalf4_GrVertexAttribType:
Brian Osmand4c29702018-09-14 16:16:55 -040038 return {false, 4, GR_GL_HALF_FLOAT};
Ethan Nicholasfa7ee242017-09-25 09:52:04 -040039 case kInt2_GrVertexAttribType:
csmartdaltonb37cb232017-02-08 14:56:27 -050040 return {false, 2, GR_GL_INT};
Ethan Nicholasfa7ee242017-09-25 09:52:04 -040041 case kInt3_GrVertexAttribType:
csmartdaltonb37cb232017-02-08 14:56:27 -050042 return {false, 3, GR_GL_INT};
Ethan Nicholasfa7ee242017-09-25 09:52:04 -040043 case kInt4_GrVertexAttribType:
csmartdaltonb37cb232017-02-08 14:56:27 -050044 return {false, 4, GR_GL_INT};
Ruiqi Maob609e6d2018-07-17 10:19:38 -040045 case kByte_GrVertexAttribType:
46 return {false, 1, GR_GL_BYTE};
47 case kByte2_GrVertexAttribType:
48 return {false, 2, GR_GL_BYTE};
49 case kByte3_GrVertexAttribType:
50 return {false, 3, GR_GL_BYTE};
51 case kByte4_GrVertexAttribType:
52 return {false, 4, GR_GL_BYTE};
53 case kUByte_GrVertexAttribType:
54 return {false, 1, GR_GL_UNSIGNED_BYTE};
55 case kUByte2_GrVertexAttribType:
56 return {false, 2, GR_GL_UNSIGNED_BYTE};
57 case kUByte3_GrVertexAttribType:
58 return {false, 3, GR_GL_UNSIGNED_BYTE};
59 case kUByte4_GrVertexAttribType:
60 return {false, 4, GR_GL_UNSIGNED_BYTE};
Ethan Nicholasfa7ee242017-09-25 09:52:04 -040061 case kUByte_norm_GrVertexAttribType:
csmartdaltonb37cb232017-02-08 14:56:27 -050062 return {true, 1, GR_GL_UNSIGNED_BYTE};
Ethan Nicholasfa7ee242017-09-25 09:52:04 -040063 case kUByte4_norm_GrVertexAttribType:
csmartdaltonb37cb232017-02-08 14:56:27 -050064 return {true, 4, GR_GL_UNSIGNED_BYTE};
Chris Daltona045eea2017-10-24 13:22:10 -060065 case kShort2_GrVertexAttribType:
66 return {false, 2, GR_GL_SHORT};
Brian Osmana5c578f2018-09-19 14:19:02 -040067 case kShort4_GrVertexAttribType:
68 return {false, 4, GR_GL_SHORT};
Ethan Nicholasfa7ee242017-09-25 09:52:04 -040069 case kUShort2_GrVertexAttribType:
Robert Phillips8296e752017-08-25 08:45:21 -040070 return {false, 2, GR_GL_UNSIGNED_SHORT};
Chris Daltona045eea2017-10-24 13:22:10 -060071 case kUShort2_norm_GrVertexAttribType:
72 return {true, 2, GR_GL_UNSIGNED_SHORT};
csmartdaltonb37cb232017-02-08 14:56:27 -050073 case kInt_GrVertexAttribType:
74 return {false, 1, GR_GL_INT};
75 case kUint_GrVertexAttribType:
76 return {false, 1, GR_GL_UNSIGNED_INT};
Robert Phillipsfe18de52019-06-06 17:21:50 -040077 case kUShort_norm_GrVertexAttribType:
78 return {true, 1, GR_GL_UNSIGNED_SHORT};
Robert Phillips66a46032019-06-18 08:00:42 -040079 case kUShort4_norm_GrVertexAttribType:
80 return {true, 4, GR_GL_UNSIGNED_SHORT};
csmartdaltonb37cb232017-02-08 14:56:27 -050081 }
Ben Wagnerb4aab9a2017-08-16 10:53:04 -040082 SK_ABORT("Unknown vertex attrib type");
csmartdaltonb37cb232017-02-08 14:56:27 -050083};
bsalomon6df86402015-06-01 10:41:49 -070084
85void GrGLAttribArrayState::set(GrGLGpu* gpu,
bsalomon@google.com6918d482013-03-07 19:09:11 +000086 int index,
csmartdalton485a1202016-07-13 10:16:32 -070087 const GrBuffer* vertexBuffer,
Brian Osman4a3f5c82018-09-18 16:16:38 -040088 GrVertexAttribType cpuType,
89 GrSLType gpuType,
bsalomon@google.com6918d482013-03-07 19:09:11 +000090 GrGLsizei stride,
Chris Dalton1d616352017-05-31 12:51:23 -060091 size_t offsetInBytes,
92 int divisor) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +000093 SkASSERT(index >= 0 && index < fAttribArrayStates.count());
Chris Dalton1d616352017-05-31 12:51:23 -060094 SkASSERT(0 == divisor || gpu->caps()->instanceAttribSupport());
bsalomon@google.com6918d482013-03-07 19:09:11 +000095 AttribArrayState* array = &fAttribArrayStates[index];
Brian Salomondbf70722019-02-07 11:31:24 -050096 const char* offsetAsPtr;
97 bool bufferChanged = false;
98 if (vertexBuffer->isCpuBuffer()) {
99 if (!array->fUsingCpuBuffer) {
100 bufferChanged = true;
101 array->fUsingCpuBuffer = true;
102 }
103 offsetAsPtr = static_cast<const GrCpuBuffer*>(vertexBuffer)->data() + offsetInBytes;
104 } else {
105 auto gpuBuffer = static_cast<const GrGpuBuffer*>(vertexBuffer);
106 if (array->fUsingCpuBuffer || array->fVertexBufferUniqueID != gpuBuffer->uniqueID()) {
107 bufferChanged = true;
108 array->fVertexBufferUniqueID = gpuBuffer->uniqueID();
109 }
110 offsetAsPtr = reinterpret_cast<const char*>(offsetInBytes);
111 }
112 if (bufferChanged ||
Brian Osman4a3f5c82018-09-18 16:16:38 -0400113 array->fCPUType != cpuType ||
114 array->fGPUType != gpuType ||
bsalomon@google.com6918d482013-03-07 19:09:11 +0000115 array->fStride != stride ||
Brian Salomondbf70722019-02-07 11:31:24 -0500116 array->fOffset != offsetAsPtr) {
117 // We always have to call this if we're going to change the array pointer. 'array' is
118 // tracking the last buffer used to setup attrib pointers, not the last buffer bound.
119 // GrGLGpu will avoid redundant binds.
Brian Salomonae64c192019-02-05 09:41:37 -0500120 gpu->bindBuffer(GrGpuBufferType::kVertex, vertexBuffer);
Brian Osman4a3f5c82018-09-18 16:16:38 -0400121 const AttribLayout& layout = attrib_layout(cpuType);
Brian Osman4a3f5c82018-09-18 16:16:38 -0400122 if (GrSLTypeIsFloatType(gpuType)) {
cdalton793dc262016-02-08 10:11:47 -0800123 GR_GL_CALL(gpu->glInterface(), VertexAttribPointer(index,
124 layout.fCount,
125 layout.fType,
126 layout.fNormalized,
127 stride,
Chris Dalton8e45b4f2017-05-05 14:00:56 -0400128 offsetAsPtr));
cdalton793dc262016-02-08 10:11:47 -0800129 } else {
130 SkASSERT(gpu->caps()->shaderCaps()->integerSupport());
131 SkASSERT(!layout.fNormalized);
132 GR_GL_CALL(gpu->glInterface(), VertexAttribIPointer(index,
133 layout.fCount,
134 layout.fType,
135 stride,
Chris Dalton8e45b4f2017-05-05 14:00:56 -0400136 offsetAsPtr));
cdalton793dc262016-02-08 10:11:47 -0800137 }
Brian Osman4a3f5c82018-09-18 16:16:38 -0400138 array->fCPUType = cpuType;
139 array->fGPUType = gpuType;
dchengc4d196c2016-02-06 15:08:54 -0800140 array->fStride = stride;
Brian Salomondbf70722019-02-07 11:31:24 -0500141 array->fOffset = offsetAsPtr;
bsalomon@google.com6918d482013-03-07 19:09:11 +0000142 }
Chris Dalton1d616352017-05-31 12:51:23 -0600143 if (gpu->caps()->instanceAttribSupport() && array->fDivisor != divisor) {
144 SkASSERT(0 == divisor || 1 == divisor); // not necessarily a requirement but what we expect.
145 GR_GL_CALL(gpu->glInterface(), VertexAttribDivisor(index, divisor));
146 array->fDivisor = divisor;
147 }
bsalomon@google.com6918d482013-03-07 19:09:11 +0000148}
149
Chris Dalton27059d32018-01-23 14:06:50 -0700150void GrGLAttribArrayState::enableVertexArrays(const GrGLGpu* gpu, int enabledCount,
Brian Salomon802cb312018-06-08 18:05:20 -0400151 GrPrimitiveRestart enablePrimitiveRestart) {
Chris Dalton8e45b4f2017-05-05 14:00:56 -0400152 SkASSERT(enabledCount <= fAttribArrayStates.count());
Chris Dalton27059d32018-01-23 14:06:50 -0700153
154 if (!fEnableStateIsValid || enabledCount != fNumEnabledArrays) {
155 int firstIdxToEnable = fEnableStateIsValid ? fNumEnabledArrays : 0;
156 for (int i = firstIdxToEnable; i < enabledCount; ++i) {
157 GR_GL_CALL(gpu->glInterface(), EnableVertexAttribArray(i));
158 }
159
160 int endIdxToDisable = fEnableStateIsValid ? fNumEnabledArrays : fAttribArrayStates.count();
161 for (int i = enabledCount; i < endIdxToDisable; ++i) {
162 GR_GL_CALL(gpu->glInterface(), DisableVertexAttribArray(i));
163 }
164
165 fNumEnabledArrays = enabledCount;
Chris Dalton1d616352017-05-31 12:51:23 -0600166 }
Chris Dalton8e45b4f2017-05-05 14:00:56 -0400167
Brian Salomon802cb312018-06-08 18:05:20 -0400168 SkASSERT(GrPrimitiveRestart::kNo == enablePrimitiveRestart ||
Chris Dalton27059d32018-01-23 14:06:50 -0700169 gpu->caps()->usePrimitiveRestart());
170
171 if (gpu->caps()->usePrimitiveRestart() &&
172 (!fEnableStateIsValid || enablePrimitiveRestart != fPrimitiveRestartEnabled)) {
Brian Salomon802cb312018-06-08 18:05:20 -0400173 if (GrPrimitiveRestart::kYes == enablePrimitiveRestart) {
Chris Dalton27059d32018-01-23 14:06:50 -0700174 GR_GL_CALL(gpu->glInterface(), Enable(GR_GL_PRIMITIVE_RESTART_FIXED_INDEX));
175 } else {
176 GR_GL_CALL(gpu->glInterface(), Disable(GR_GL_PRIMITIVE_RESTART_FIXED_INDEX));
177 }
178
179 fPrimitiveRestartEnabled = enablePrimitiveRestart;
bsalomon@google.com6918d482013-03-07 19:09:11 +0000180 }
Chris Dalton8e45b4f2017-05-05 14:00:56 -0400181
Chris Dalton27059d32018-01-23 14:06:50 -0700182 fEnableStateIsValid = true;
bsalomon@google.com6918d482013-03-07 19:09:11 +0000183}
184
185///////////////////////////////////////////////////////////////////////////////////////////////////
186
bsalomon8780bc62015-05-13 09:56:37 -0700187GrGLVertexArray::GrGLVertexArray(GrGLint id, int attribCount)
188 : fID(id)
bsalomon@google.com6918d482013-03-07 19:09:11 +0000189 , fAttribArrays(attribCount)
cdaltone2e71c22016-04-07 18:13:29 -0700190 , fIndexBufferUniqueID(SK_InvalidUniqueID) {
bsalomon@google.com6918d482013-03-07 19:09:11 +0000191}
192
bsalomon8780bc62015-05-13 09:56:37 -0700193GrGLAttribArrayState* GrGLVertexArray::bind(GrGLGpu* gpu) {
bsalomon@google.com6918d482013-03-07 19:09:11 +0000194 if (0 == fID) {
halcanary96fcdcc2015-08-27 07:41:13 -0700195 return nullptr;
bsalomon@google.com6918d482013-03-07 19:09:11 +0000196 }
bsalomon8780bc62015-05-13 09:56:37 -0700197 gpu->bindVertexArray(fID);
bsalomon@google.com6918d482013-03-07 19:09:11 +0000198 return &fAttribArrays;
199}
skia.committer@gmail.com754a3eb2013-03-08 07:01:25 +0000200
csmartdalton485a1202016-07-13 10:16:32 -0700201GrGLAttribArrayState* GrGLVertexArray::bindWithIndexBuffer(GrGLGpu* gpu, const GrBuffer* ibuff) {
bsalomon8780bc62015-05-13 09:56:37 -0700202 GrGLAttribArrayState* state = this->bind(gpu);
Brian Salomondbf70722019-02-07 11:31:24 -0500203 if (!state) {
204 return nullptr;
205 }
206 if (ibuff->isCpuBuffer()) {
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 if (fIndexBufferUniqueID != glBuffer->uniqueID()) {
csmartdalton485a1202016-07-13 10:16:32 -0700211 const GrGLBuffer* glBuffer = static_cast<const GrGLBuffer*>(ibuff);
Brian Salomondbf70722019-02-07 11:31:24 -0500212 GR_GL_CALL(gpu->glInterface(),
213 BindBuffer(GR_GL_ELEMENT_ARRAY_BUFFER, glBuffer->bufferID()));
214 fIndexBufferUniqueID = glBuffer->uniqueID();
csmartdalton485a1202016-07-13 10:16:32 -0700215 }
bsalomon@google.com6918d482013-03-07 19:09:11 +0000216 }
217 return state;
218}
219
bsalomon@google.com6918d482013-03-07 19:09:11 +0000220void GrGLVertexArray::invalidateCachedState() {
commit-bot@chromium.orgce6da4d2013-09-09 14:55:37 +0000221 fAttribArrays.invalidate();
Robert Phillips294870f2016-11-11 12:38:40 -0500222 fIndexBufferUniqueID.makeInvalid();
bsalomon@google.com6918d482013-03-07 19:09:11 +0000223}