Chris Craik | 96a5c4c | 2015-01-27 15:46:35 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
| 22 | namespace android { |
| 23 | namespace uirenderer { |
| 24 | |
| 25 | MeshState::MeshState() |
Chris Craik | 117bdbc | 2015-02-05 10:12:38 -0800 | [diff] [blame^] | 26 | : mCurrentIndicesBuffer(0) |
| 27 | , mCurrentPixelBuffer(0) |
| 28 | , mCurrentPositionPointer(this) |
Chris Craik | 96a5c4c | 2015-01-27 15:46:35 -0800 | [diff] [blame] | 29 | , mCurrentPositionStride(0) |
| 30 | , mCurrentTexCoordsPointer(this) |
| 31 | , mCurrentTexCoordsStride(0) |
Chris Craik | 117bdbc | 2015-02-05 10:12:38 -0800 | [diff] [blame^] | 32 | , mTexCoordsArrayEnabled(false) |
| 33 | , mQuadListIndices(0) { |
Chris Craik | 96a5c4c | 2015-01-27 15:46:35 -0800 | [diff] [blame] | 34 | |
Chris Craik | 0318887 | 2015-02-02 18:39:33 -0800 | [diff] [blame] | 35 | glGenBuffers(1, &mUnitQuadBuffer); |
| 36 | glBindBuffer(GL_ARRAY_BUFFER, mUnitQuadBuffer); |
| 37 | glBufferData(GL_ARRAY_BUFFER, sizeof(kUnitQuadVertices), kUnitQuadVertices, GL_STATIC_DRAW); |
Chris Craik | 96a5c4c | 2015-01-27 15:46:35 -0800 | [diff] [blame] | 38 | |
Chris Craik | 0318887 | 2015-02-02 18:39:33 -0800 | [diff] [blame] | 39 | mCurrentBuffer = mUnitQuadBuffer; |
Chris Craik | 6c15ffa | 2015-02-02 13:50:55 -0800 | [diff] [blame] | 40 | |
| 41 | // position attribute always enabled |
| 42 | glEnableVertexAttribArray(Program::kBindingPosition); |
Chris Craik | 96a5c4c | 2015-01-27 15:46:35 -0800 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | MeshState::~MeshState() { |
Chris Craik | 0318887 | 2015-02-02 18:39:33 -0800 | [diff] [blame] | 46 | glDeleteBuffers(1, &mUnitQuadBuffer); |
Chris Craik | 96a5c4c | 2015-01-27 15:46:35 -0800 | [diff] [blame] | 47 | mCurrentBuffer = 0; |
| 48 | |
| 49 | glDeleteBuffers(1, &mQuadListIndices); |
| 50 | mQuadListIndices = 0; |
Chris Craik | 117bdbc | 2015-02-05 10:12:38 -0800 | [diff] [blame^] | 51 | } |
Chris Craik | 96a5c4c | 2015-01-27 15:46:35 -0800 | [diff] [blame] | 52 | |
Chris Craik | 117bdbc | 2015-02-05 10:12:38 -0800 | [diff] [blame^] | 53 | void MeshState::dump() { |
| 54 | ALOGD("MeshState vertices: unitQuad %d, current %d", mUnitQuadBuffer, mCurrentBuffer); |
Chris Craik | 96a5c4c | 2015-01-27 15:46:35 -0800 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | /////////////////////////////////////////////////////////////////////////////// |
| 58 | // Buffer Objects |
| 59 | /////////////////////////////////////////////////////////////////////////////// |
| 60 | |
| 61 | bool MeshState::bindMeshBuffer() { |
Chris Craik | 0318887 | 2015-02-02 18:39:33 -0800 | [diff] [blame] | 62 | return bindMeshBuffer(mUnitQuadBuffer); |
Chris Craik | 96a5c4c | 2015-01-27 15:46:35 -0800 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | bool MeshState::bindMeshBuffer(GLuint buffer) { |
Chris Craik | 0318887 | 2015-02-02 18:39:33 -0800 | [diff] [blame] | 66 | if (!buffer) buffer = mUnitQuadBuffer; |
Chris Craik | 117bdbc | 2015-02-05 10:12:38 -0800 | [diff] [blame^] | 67 | return bindMeshBufferInternal(buffer); |
Chris Craik | 96a5c4c | 2015-01-27 15:46:35 -0800 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | bool MeshState::unbindMeshBuffer() { |
Chris Craik | 117bdbc | 2015-02-05 10:12:38 -0800 | [diff] [blame^] | 71 | return bindMeshBufferInternal(0); |
| 72 | } |
| 73 | |
| 74 | bool MeshState::bindMeshBufferInternal(GLuint buffer) { |
| 75 | if (mCurrentBuffer != buffer) { |
| 76 | glBindBuffer(GL_ARRAY_BUFFER, buffer); |
| 77 | mCurrentBuffer = buffer; |
Chris Craik | 96a5c4c | 2015-01-27 15:46:35 -0800 | [diff] [blame] | 78 | return true; |
| 79 | } |
| 80 | return false; |
| 81 | } |
| 82 | |
| 83 | /////////////////////////////////////////////////////////////////////////////// |
| 84 | // Vertices |
| 85 | /////////////////////////////////////////////////////////////////////////////// |
| 86 | |
Chris Craik | 6c15ffa | 2015-02-02 13:50:55 -0800 | [diff] [blame] | 87 | void MeshState::bindPositionVertexPointer(bool force, const GLvoid* vertices, GLsizei stride) { |
Chris Craik | 96a5c4c | 2015-01-27 15:46:35 -0800 | [diff] [blame] | 88 | if (force || vertices != mCurrentPositionPointer || stride != mCurrentPositionStride) { |
Chris Craik | 6c15ffa | 2015-02-02 13:50:55 -0800 | [diff] [blame] | 89 | glVertexAttribPointer(Program::kBindingPosition, 2, GL_FLOAT, GL_FALSE, stride, vertices); |
Chris Craik | 96a5c4c | 2015-01-27 15:46:35 -0800 | [diff] [blame] | 90 | mCurrentPositionPointer = vertices; |
| 91 | mCurrentPositionStride = stride; |
| 92 | } |
| 93 | } |
| 94 | |
Chris Craik | 6c15ffa | 2015-02-02 13:50:55 -0800 | [diff] [blame] | 95 | void MeshState::bindTexCoordsVertexPointer(bool force, const GLvoid* vertices, GLsizei stride) { |
Chris Craik | 96a5c4c | 2015-01-27 15:46:35 -0800 | [diff] [blame] | 96 | if (force || vertices != mCurrentTexCoordsPointer || stride != mCurrentTexCoordsStride) { |
Chris Craik | 6c15ffa | 2015-02-02 13:50:55 -0800 | [diff] [blame] | 97 | glVertexAttribPointer(Program::kBindingTexCoords, 2, GL_FLOAT, GL_FALSE, stride, vertices); |
Chris Craik | 96a5c4c | 2015-01-27 15:46:35 -0800 | [diff] [blame] | 98 | mCurrentTexCoordsPointer = vertices; |
| 99 | mCurrentTexCoordsStride = stride; |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | void MeshState::resetVertexPointers() { |
| 104 | mCurrentPositionPointer = this; |
| 105 | mCurrentTexCoordsPointer = this; |
| 106 | } |
| 107 | |
| 108 | void MeshState::resetTexCoordsVertexPointer() { |
| 109 | mCurrentTexCoordsPointer = this; |
| 110 | } |
| 111 | |
| 112 | void MeshState::enableTexCoordsVertexArray() { |
| 113 | if (!mTexCoordsArrayEnabled) { |
| 114 | glEnableVertexAttribArray(Program::kBindingTexCoords); |
| 115 | mCurrentTexCoordsPointer = this; |
| 116 | mTexCoordsArrayEnabled = true; |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | void MeshState::disableTexCoordsVertexArray() { |
| 121 | if (mTexCoordsArrayEnabled) { |
| 122 | glDisableVertexAttribArray(Program::kBindingTexCoords); |
| 123 | mTexCoordsArrayEnabled = false; |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | /////////////////////////////////////////////////////////////////////////////// |
| 128 | // Indices |
| 129 | /////////////////////////////////////////////////////////////////////////////// |
| 130 | |
| 131 | bool MeshState::bindIndicesBufferInternal(const GLuint buffer) { |
| 132 | if (mCurrentIndicesBuffer != buffer) { |
| 133 | glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, buffer); |
| 134 | mCurrentIndicesBuffer = buffer; |
| 135 | return true; |
| 136 | } |
| 137 | return false; |
| 138 | } |
| 139 | |
| 140 | bool MeshState::bindQuadIndicesBuffer() { |
| 141 | if (!mQuadListIndices) { |
| 142 | std::unique_ptr<uint16_t[]> regionIndices(new uint16_t[kMaxNumberOfQuads * 6]); |
| 143 | for (uint32_t i = 0; i < kMaxNumberOfQuads; i++) { |
| 144 | uint16_t quad = i * 4; |
| 145 | int index = i * 6; |
| 146 | regionIndices[index ] = quad; // top-left |
| 147 | regionIndices[index + 1] = quad + 1; // top-right |
| 148 | regionIndices[index + 2] = quad + 2; // bottom-left |
| 149 | regionIndices[index + 3] = quad + 2; // bottom-left |
| 150 | regionIndices[index + 4] = quad + 1; // top-right |
| 151 | regionIndices[index + 5] = quad + 3; // bottom-right |
| 152 | } |
| 153 | |
| 154 | glGenBuffers(1, &mQuadListIndices); |
| 155 | bool force = bindIndicesBufferInternal(mQuadListIndices); |
| 156 | glBufferData(GL_ELEMENT_ARRAY_BUFFER, kMaxNumberOfQuads * 6 * sizeof(uint16_t), |
| 157 | regionIndices.get(), GL_STATIC_DRAW); |
| 158 | return force; |
| 159 | } |
| 160 | |
| 161 | return bindIndicesBufferInternal(mQuadListIndices); |
| 162 | } |
| 163 | |
Chris Craik | 96a5c4c | 2015-01-27 15:46:35 -0800 | [diff] [blame] | 164 | bool MeshState::unbindIndicesBuffer() { |
| 165 | if (mCurrentIndicesBuffer) { |
| 166 | glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); |
| 167 | mCurrentIndicesBuffer = 0; |
| 168 | return true; |
| 169 | } |
| 170 | return false; |
| 171 | } |
| 172 | |
| 173 | } /* namespace uirenderer */ |
| 174 | } /* namespace android */ |
| 175 | |