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