blob: b575c696586eeb88765efce661121e74638df831 [file] [log] [blame]
Chris Craik96a5c4c2015-01-27 15:46:35 -08001/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16#include "renderstate/MeshState.h"
17
18#include "Program.h"
19
20#include "ShadowTessellator.h"
21
22namespace android {
23namespace uirenderer {
24
25MeshState::MeshState()
Chris Craik117bdbc2015-02-05 10:12:38 -080026 : mCurrentIndicesBuffer(0)
27 , mCurrentPixelBuffer(0)
28 , mCurrentPositionPointer(this)
Chris Craik96a5c4c2015-01-27 15:46:35 -080029 , mCurrentPositionStride(0)
30 , mCurrentTexCoordsPointer(this)
31 , mCurrentTexCoordsStride(0)
Chris Craik117bdbc2015-02-05 10:12:38 -080032 , mTexCoordsArrayEnabled(false)
33 , mQuadListIndices(0) {
Chris Craik03188872015-02-02 18:39:33 -080034 glGenBuffers(1, &mUnitQuadBuffer);
35 glBindBuffer(GL_ARRAY_BUFFER, mUnitQuadBuffer);
36 glBufferData(GL_ARRAY_BUFFER, sizeof(kUnitQuadVertices), kUnitQuadVertices, GL_STATIC_DRAW);
Chris Craik03188872015-02-02 18:39:33 -080037 mCurrentBuffer = mUnitQuadBuffer;
Chris Craik6c15ffa2015-02-02 13:50:55 -080038
Chris Craik182952f2015-03-09 14:17:29 -070039 uint16_t regionIndices[kMaxNumberOfQuads * 6];
Chris Craik2ab95d72015-02-06 15:25:51 -080040 for (uint32_t i = 0; i < kMaxNumberOfQuads; i++) {
41 uint16_t quad = i * 4;
42 int index = i * 6;
43 regionIndices[index ] = quad; // top-left
44 regionIndices[index + 1] = quad + 1; // top-right
45 regionIndices[index + 2] = quad + 2; // bottom-left
46 regionIndices[index + 3] = quad + 2; // bottom-left
47 regionIndices[index + 4] = quad + 1; // top-right
48 regionIndices[index + 5] = quad + 3; // bottom-right
49 }
50 glGenBuffers(1, &mQuadListIndices);
51 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mQuadListIndices);
Chris Craik182952f2015-03-09 14:17:29 -070052 glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(regionIndices), regionIndices, GL_STATIC_DRAW);
Chris Craik2ab95d72015-02-06 15:25:51 -080053 mCurrentIndicesBuffer = mQuadListIndices;
54
Chris Craik6c15ffa2015-02-02 13:50:55 -080055 // position attribute always enabled
56 glEnableVertexAttribArray(Program::kBindingPosition);
Chris Craik96a5c4c2015-01-27 15:46:35 -080057}
58
59MeshState::~MeshState() {
Chris Craik03188872015-02-02 18:39:33 -080060 glDeleteBuffers(1, &mUnitQuadBuffer);
Chris Craik96a5c4c2015-01-27 15:46:35 -080061 mCurrentBuffer = 0;
62
63 glDeleteBuffers(1, &mQuadListIndices);
64 mQuadListIndices = 0;
Chris Craik117bdbc2015-02-05 10:12:38 -080065}
Chris Craik96a5c4c2015-01-27 15:46:35 -080066
Chris Craik117bdbc2015-02-05 10:12:38 -080067void MeshState::dump() {
Chris Craik08fa43f2015-02-09 18:58:32 -080068 ALOGD("MeshState VBOs: unitQuad %d, current %d", mUnitQuadBuffer, mCurrentBuffer);
Chris Craikf27133d2015-02-19 09:51:53 -080069 ALOGD("MeshState IBOs: quadList %d, current %d", mQuadListIndices, mCurrentIndicesBuffer);
Chris Craik08fa43f2015-02-09 18:58:32 -080070 ALOGD("MeshState vertices: vertex data %p, stride %d",
71 mCurrentPositionPointer, mCurrentPositionStride);
72 ALOGD("MeshState texCoord: data %p, stride %d",
73 mCurrentTexCoordsPointer, mCurrentTexCoordsStride);
Chris Craik96a5c4c2015-01-27 15:46:35 -080074}
75
76///////////////////////////////////////////////////////////////////////////////
77// Buffer Objects
78///////////////////////////////////////////////////////////////////////////////
79
Chris Craik1b7db402016-02-24 18:25:32 -080080void MeshState::bindMeshBuffer(GLuint buffer) {
Chris Craik117bdbc2015-02-05 10:12:38 -080081 if (mCurrentBuffer != buffer) {
82 glBindBuffer(GL_ARRAY_BUFFER, buffer);
83 mCurrentBuffer = buffer;
Chris Craik1b7db402016-02-24 18:25:32 -080084
85 // buffer has changed, so invalidate cached vertex pos/texcoord pointers
86 resetVertexPointers();
Chris Craik96a5c4c2015-01-27 15:46:35 -080087 }
Chris Craik1b7db402016-02-24 18:25:32 -080088}
89
90void MeshState::unbindMeshBuffer() {
91 return bindMeshBuffer(0);
Chris Craik96a5c4c2015-01-27 15:46:35 -080092}
93
Chris Craik8d2cf942015-11-02 14:52:21 -080094void MeshState::genOrUpdateMeshBuffer(GLuint* buffer, GLsizeiptr size,
95 const void* data, GLenum usage) {
96 if (!*buffer) {
97 glGenBuffers(1, buffer);
98 }
99 bindMeshBuffer(*buffer);
100 glBufferData(GL_ARRAY_BUFFER, size, data, usage);
101}
102
103void MeshState::deleteMeshBuffer(GLuint buffer) {
104 if (buffer == mCurrentBuffer) {
105 // GL defines that deleting the currently bound VBO rebinds to 0 (no VBO).
106 // Reflect this in our cached value.
107 mCurrentBuffer = 0;
108 }
109 glDeleteBuffers(1, &buffer);
110}
111
Chris Craik96a5c4c2015-01-27 15:46:35 -0800112///////////////////////////////////////////////////////////////////////////////
113// Vertices
114///////////////////////////////////////////////////////////////////////////////
115
Chris Craik1b7db402016-02-24 18:25:32 -0800116void MeshState::bindPositionVertexPointer(const GLvoid* vertices, GLsizei stride) {
117 // update pos coords if !current vbo, since vertices may point into mutable memory (e.g. stack)
118 if (mCurrentBuffer == 0
119 || vertices != mCurrentPositionPointer
120 || stride != mCurrentPositionStride) {
Chris Craik6c15ffa2015-02-02 13:50:55 -0800121 glVertexAttribPointer(Program::kBindingPosition, 2, GL_FLOAT, GL_FALSE, stride, vertices);
Chris Craik96a5c4c2015-01-27 15:46:35 -0800122 mCurrentPositionPointer = vertices;
123 mCurrentPositionStride = stride;
124 }
125}
126
Chris Craik1b7db402016-02-24 18:25:32 -0800127void MeshState::bindTexCoordsVertexPointer(const GLvoid* vertices, GLsizei stride) {
128 // update tex coords if !current vbo, since vertices may point into mutable memory (e.g. stack)
129 if (mCurrentBuffer == 0
130 || vertices != mCurrentTexCoordsPointer
131 || stride != mCurrentTexCoordsStride) {
Chris Craik6c15ffa2015-02-02 13:50:55 -0800132 glVertexAttribPointer(Program::kBindingTexCoords, 2, GL_FLOAT, GL_FALSE, stride, vertices);
Chris Craik96a5c4c2015-01-27 15:46:35 -0800133 mCurrentTexCoordsPointer = vertices;
134 mCurrentTexCoordsStride = stride;
135 }
136}
137
138void MeshState::resetVertexPointers() {
139 mCurrentPositionPointer = this;
140 mCurrentTexCoordsPointer = this;
141}
142
Chris Craik96a5c4c2015-01-27 15:46:35 -0800143void MeshState::enableTexCoordsVertexArray() {
144 if (!mTexCoordsArrayEnabled) {
145 glEnableVertexAttribArray(Program::kBindingTexCoords);
146 mCurrentTexCoordsPointer = this;
147 mTexCoordsArrayEnabled = true;
148 }
149}
150
151void MeshState::disableTexCoordsVertexArray() {
152 if (mTexCoordsArrayEnabled) {
153 glDisableVertexAttribArray(Program::kBindingTexCoords);
154 mTexCoordsArrayEnabled = false;
155 }
156}
157
158///////////////////////////////////////////////////////////////////////////////
159// Indices
160///////////////////////////////////////////////////////////////////////////////
161
Chris Craik1b7db402016-02-24 18:25:32 -0800162void MeshState::bindIndicesBuffer(const GLuint buffer) {
Chris Craik96a5c4c2015-01-27 15:46:35 -0800163 if (mCurrentIndicesBuffer != buffer) {
164 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, buffer);
165 mCurrentIndicesBuffer = buffer;
Chris Craik96a5c4c2015-01-27 15:46:35 -0800166 }
Chris Craik96a5c4c2015-01-27 15:46:35 -0800167}
168
Chris Craik1b7db402016-02-24 18:25:32 -0800169void MeshState::unbindIndicesBuffer() {
Chris Craik96a5c4c2015-01-27 15:46:35 -0800170 if (mCurrentIndicesBuffer) {
171 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
172 mCurrentIndicesBuffer = 0;
Chris Craik96a5c4c2015-01-27 15:46:35 -0800173 }
Chris Craik96a5c4c2015-01-27 15:46:35 -0800174}
175
176} /* namespace uirenderer */
177} /* namespace android */
178