blob: 00d7d8dff7684607fe26bdba62d8d06cca3da2e4 [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};
77 }
Ben Wagnerb4aab9a2017-08-16 10:53:04 -040078 SK_ABORT("Unknown vertex attrib type");
csmartdaltonb37cb232017-02-08 14:56:27 -050079 return {false, 0, 0};
80};
bsalomon6df86402015-06-01 10:41:49 -070081
82void GrGLAttribArrayState::set(GrGLGpu* gpu,
bsalomon@google.com6918d482013-03-07 19:09:11 +000083 int index,
csmartdalton485a1202016-07-13 10:16:32 -070084 const GrBuffer* vertexBuffer,
Brian Osman4a3f5c82018-09-18 16:16:38 -040085 GrVertexAttribType cpuType,
86 GrSLType gpuType,
bsalomon@google.com6918d482013-03-07 19:09:11 +000087 GrGLsizei stride,
Chris Dalton1d616352017-05-31 12:51:23 -060088 size_t offsetInBytes,
89 int divisor) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +000090 SkASSERT(index >= 0 && index < fAttribArrayStates.count());
Chris Dalton1d616352017-05-31 12:51:23 -060091 SkASSERT(0 == divisor || gpu->caps()->instanceAttribSupport());
bsalomon@google.com6918d482013-03-07 19:09:11 +000092 AttribArrayState* array = &fAttribArrayStates[index];
Brian Salomondbf70722019-02-07 11:31:24 -050093 const char* offsetAsPtr;
94 bool bufferChanged = false;
95 if (vertexBuffer->isCpuBuffer()) {
96 if (!array->fUsingCpuBuffer) {
97 bufferChanged = true;
98 array->fUsingCpuBuffer = true;
99 }
100 offsetAsPtr = static_cast<const GrCpuBuffer*>(vertexBuffer)->data() + offsetInBytes;
101 } else {
102 auto gpuBuffer = static_cast<const GrGpuBuffer*>(vertexBuffer);
103 if (array->fUsingCpuBuffer || array->fVertexBufferUniqueID != gpuBuffer->uniqueID()) {
104 bufferChanged = true;
105 array->fVertexBufferUniqueID = gpuBuffer->uniqueID();
106 }
107 offsetAsPtr = reinterpret_cast<const char*>(offsetInBytes);
108 }
109 if (bufferChanged ||
Brian Osman4a3f5c82018-09-18 16:16:38 -0400110 array->fCPUType != cpuType ||
111 array->fGPUType != gpuType ||
bsalomon@google.com6918d482013-03-07 19:09:11 +0000112 array->fStride != stride ||
Brian Salomondbf70722019-02-07 11:31:24 -0500113 array->fOffset != offsetAsPtr) {
114 // We always have to call this if we're going to change the array pointer. 'array' is
115 // tracking the last buffer used to setup attrib pointers, not the last buffer bound.
116 // GrGLGpu will avoid redundant binds.
Brian Salomonae64c192019-02-05 09:41:37 -0500117 gpu->bindBuffer(GrGpuBufferType::kVertex, vertexBuffer);
Brian Osman4a3f5c82018-09-18 16:16:38 -0400118 const AttribLayout& layout = attrib_layout(cpuType);
Brian Osman4a3f5c82018-09-18 16:16:38 -0400119 if (GrSLTypeIsFloatType(gpuType)) {
cdalton793dc262016-02-08 10:11:47 -0800120 GR_GL_CALL(gpu->glInterface(), VertexAttribPointer(index,
121 layout.fCount,
122 layout.fType,
123 layout.fNormalized,
124 stride,
Chris Dalton8e45b4f2017-05-05 14:00:56 -0400125 offsetAsPtr));
cdalton793dc262016-02-08 10:11:47 -0800126 } else {
127 SkASSERT(gpu->caps()->shaderCaps()->integerSupport());
128 SkASSERT(!layout.fNormalized);
129 GR_GL_CALL(gpu->glInterface(), VertexAttribIPointer(index,
130 layout.fCount,
131 layout.fType,
132 stride,
Chris Dalton8e45b4f2017-05-05 14:00:56 -0400133 offsetAsPtr));
cdalton793dc262016-02-08 10:11:47 -0800134 }
Brian Osman4a3f5c82018-09-18 16:16:38 -0400135 array->fCPUType = cpuType;
136 array->fGPUType = gpuType;
dchengc4d196c2016-02-06 15:08:54 -0800137 array->fStride = stride;
Brian Salomondbf70722019-02-07 11:31:24 -0500138 array->fOffset = offsetAsPtr;
bsalomon@google.com6918d482013-03-07 19:09:11 +0000139 }
Chris Dalton1d616352017-05-31 12:51:23 -0600140 if (gpu->caps()->instanceAttribSupport() && array->fDivisor != divisor) {
141 SkASSERT(0 == divisor || 1 == divisor); // not necessarily a requirement but what we expect.
142 GR_GL_CALL(gpu->glInterface(), VertexAttribDivisor(index, divisor));
143 array->fDivisor = divisor;
144 }
bsalomon@google.com6918d482013-03-07 19:09:11 +0000145}
146
Chris Dalton27059d32018-01-23 14:06:50 -0700147void GrGLAttribArrayState::enableVertexArrays(const GrGLGpu* gpu, int enabledCount,
Brian Salomon802cb312018-06-08 18:05:20 -0400148 GrPrimitiveRestart enablePrimitiveRestart) {
Chris Dalton8e45b4f2017-05-05 14:00:56 -0400149 SkASSERT(enabledCount <= fAttribArrayStates.count());
Chris Dalton27059d32018-01-23 14:06:50 -0700150
151 if (!fEnableStateIsValid || enabledCount != fNumEnabledArrays) {
152 int firstIdxToEnable = fEnableStateIsValid ? fNumEnabledArrays : 0;
153 for (int i = firstIdxToEnable; i < enabledCount; ++i) {
154 GR_GL_CALL(gpu->glInterface(), EnableVertexAttribArray(i));
155 }
156
157 int endIdxToDisable = fEnableStateIsValid ? fNumEnabledArrays : fAttribArrayStates.count();
158 for (int i = enabledCount; i < endIdxToDisable; ++i) {
159 GR_GL_CALL(gpu->glInterface(), DisableVertexAttribArray(i));
160 }
161
162 fNumEnabledArrays = enabledCount;
Chris Dalton1d616352017-05-31 12:51:23 -0600163 }
Chris Dalton8e45b4f2017-05-05 14:00:56 -0400164
Brian Salomon802cb312018-06-08 18:05:20 -0400165 SkASSERT(GrPrimitiveRestart::kNo == enablePrimitiveRestart ||
Chris Dalton27059d32018-01-23 14:06:50 -0700166 gpu->caps()->usePrimitiveRestart());
167
168 if (gpu->caps()->usePrimitiveRestart() &&
169 (!fEnableStateIsValid || enablePrimitiveRestart != fPrimitiveRestartEnabled)) {
Brian Salomon802cb312018-06-08 18:05:20 -0400170 if (GrPrimitiveRestart::kYes == enablePrimitiveRestart) {
Chris Dalton27059d32018-01-23 14:06:50 -0700171 GR_GL_CALL(gpu->glInterface(), Enable(GR_GL_PRIMITIVE_RESTART_FIXED_INDEX));
172 } else {
173 GR_GL_CALL(gpu->glInterface(), Disable(GR_GL_PRIMITIVE_RESTART_FIXED_INDEX));
174 }
175
176 fPrimitiveRestartEnabled = enablePrimitiveRestart;
bsalomon@google.com6918d482013-03-07 19:09:11 +0000177 }
Chris Dalton8e45b4f2017-05-05 14:00:56 -0400178
Chris Dalton27059d32018-01-23 14:06:50 -0700179 fEnableStateIsValid = true;
bsalomon@google.com6918d482013-03-07 19:09:11 +0000180}
181
182///////////////////////////////////////////////////////////////////////////////////////////////////
183
bsalomon8780bc62015-05-13 09:56:37 -0700184GrGLVertexArray::GrGLVertexArray(GrGLint id, int attribCount)
185 : fID(id)
bsalomon@google.com6918d482013-03-07 19:09:11 +0000186 , fAttribArrays(attribCount)
cdaltone2e71c22016-04-07 18:13:29 -0700187 , fIndexBufferUniqueID(SK_InvalidUniqueID) {
bsalomon@google.com6918d482013-03-07 19:09:11 +0000188}
189
bsalomon8780bc62015-05-13 09:56:37 -0700190GrGLAttribArrayState* GrGLVertexArray::bind(GrGLGpu* gpu) {
bsalomon@google.com6918d482013-03-07 19:09:11 +0000191 if (0 == fID) {
halcanary96fcdcc2015-08-27 07:41:13 -0700192 return nullptr;
bsalomon@google.com6918d482013-03-07 19:09:11 +0000193 }
bsalomon8780bc62015-05-13 09:56:37 -0700194 gpu->bindVertexArray(fID);
bsalomon@google.com6918d482013-03-07 19:09:11 +0000195 return &fAttribArrays;
196}
skia.committer@gmail.com754a3eb2013-03-08 07:01:25 +0000197
csmartdalton485a1202016-07-13 10:16:32 -0700198GrGLAttribArrayState* GrGLVertexArray::bindWithIndexBuffer(GrGLGpu* gpu, const GrBuffer* ibuff) {
bsalomon8780bc62015-05-13 09:56:37 -0700199 GrGLAttribArrayState* state = this->bind(gpu);
Brian Salomondbf70722019-02-07 11:31:24 -0500200 if (!state) {
201 return nullptr;
202 }
203 if (ibuff->isCpuBuffer()) {
204 GR_GL_CALL(gpu->glInterface(), BindBuffer(GR_GL_ELEMENT_ARRAY_BUFFER, 0));
205 } else {
206 const GrGLBuffer* glBuffer = static_cast<const GrGLBuffer*>(ibuff);
207 if (fIndexBufferUniqueID != glBuffer->uniqueID()) {
csmartdalton485a1202016-07-13 10:16:32 -0700208 const GrGLBuffer* glBuffer = static_cast<const GrGLBuffer*>(ibuff);
Brian Salomondbf70722019-02-07 11:31:24 -0500209 GR_GL_CALL(gpu->glInterface(),
210 BindBuffer(GR_GL_ELEMENT_ARRAY_BUFFER, glBuffer->bufferID()));
211 fIndexBufferUniqueID = glBuffer->uniqueID();
csmartdalton485a1202016-07-13 10:16:32 -0700212 }
bsalomon@google.com6918d482013-03-07 19:09:11 +0000213 }
214 return state;
215}
216
bsalomon@google.com6918d482013-03-07 19:09:11 +0000217void GrGLVertexArray::invalidateCachedState() {
commit-bot@chromium.orgce6da4d2013-09-09 14:55:37 +0000218 fAttribArrays.invalidate();
Robert Phillips294870f2016-11-11 12:38:40 -0500219 fIndexBufferUniqueID.makeInvalid();
bsalomon@google.com6918d482013-03-07 19:09:11 +0000220}