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 | #ifndef RENDERSTATE_MESHSTATE_H |
| 17 | #define RENDERSTATE_MESHSTATE_H |
| 18 | |
| 19 | #include "Vertex.h" |
| 20 | |
| 21 | #include <GLES2/gl2.h> |
| 22 | #include <GLES2/gl2ext.h> |
| 23 | #include <memory> |
| 24 | |
| 25 | namespace android { |
| 26 | namespace uirenderer { |
| 27 | |
| 28 | class Program; |
| 29 | |
| 30 | // Maximum number of quads that pre-allocated meshes can draw |
| 31 | const uint32_t kMaxNumberOfQuads = 2048; |
| 32 | |
| 33 | // This array is never used directly but used as a memcpy source in the |
| 34 | // OpenGLRenderer constructor |
Chris Craik | 0318887 | 2015-02-02 18:39:33 -0800 | [diff] [blame] | 35 | const TextureVertex kUnitQuadVertices[] = { |
Chris Craik | 96a5c4c | 2015-01-27 15:46:35 -0800 | [diff] [blame] | 36 | { 0, 0, 0, 0 }, |
| 37 | { 1, 0, 1, 0 }, |
| 38 | { 0, 1, 0, 1 }, |
| 39 | { 1, 1, 1, 1 }, |
| 40 | }; |
| 41 | |
| 42 | const GLsizei kVertexStride = sizeof(Vertex); |
| 43 | const GLsizei kAlphaVertexStride = sizeof(AlphaVertex); |
| 44 | const GLsizei kTextureVertexStride = sizeof(TextureVertex); |
Chris Craik | ef25074 | 2015-02-25 17:16:16 -0800 | [diff] [blame] | 45 | const GLsizei kColorTextureVertexStride = sizeof(ColorTextureVertex); |
Chris Craik | 96a5c4c | 2015-01-27 15:46:35 -0800 | [diff] [blame] | 46 | |
| 47 | const GLsizei kMeshTextureOffset = 2 * sizeof(float); |
| 48 | const GLsizei kVertexAlphaOffset = 2 * sizeof(float); |
| 49 | const GLsizei kVertexAAWidthOffset = 2 * sizeof(float); |
| 50 | const GLsizei kVertexAALengthOffset = 3 * sizeof(float); |
Chris Craik | 117bdbc | 2015-02-05 10:12:38 -0800 | [diff] [blame] | 51 | const GLsizei kUnitQuadCount = 4; |
Chris Craik | 96a5c4c | 2015-01-27 15:46:35 -0800 | [diff] [blame] | 52 | |
| 53 | class MeshState { |
| 54 | private: |
| 55 | friend class RenderState; |
| 56 | |
| 57 | public: |
| 58 | ~MeshState(); |
Chris Craik | 117bdbc | 2015-02-05 10:12:38 -0800 | [diff] [blame] | 59 | void dump(); |
Chris Craik | 96a5c4c | 2015-01-27 15:46:35 -0800 | [diff] [blame] | 60 | /////////////////////////////////////////////////////////////////////////////// |
| 61 | // Buffer objects |
| 62 | /////////////////////////////////////////////////////////////////////////////// |
Chris Craik | 96a5c4c | 2015-01-27 15:46:35 -0800 | [diff] [blame] | 63 | |
| 64 | /** |
| 65 | * Binds the specified VBO if needed. If buffer == 0, binds default simple textured quad. |
| 66 | */ |
Chris Craik | 1b7db40 | 2016-02-24 18:25:32 -0800 | [diff] [blame^] | 67 | void bindMeshBuffer(GLuint buffer); |
Chris Craik | 96a5c4c | 2015-01-27 15:46:35 -0800 | [diff] [blame] | 68 | |
| 69 | /** |
Chris Craik | 1b7db40 | 2016-02-24 18:25:32 -0800 | [diff] [blame^] | 70 | * Unbinds the current VBO if active. |
Chris Craik | 96a5c4c | 2015-01-27 15:46:35 -0800 | [diff] [blame] | 71 | */ |
Chris Craik | 1b7db40 | 2016-02-24 18:25:32 -0800 | [diff] [blame^] | 72 | void unbindMeshBuffer(); |
Chris Craik | 96a5c4c | 2015-01-27 15:46:35 -0800 | [diff] [blame] | 73 | |
Chris Craik | 8d2cf94 | 2015-11-02 14:52:21 -0800 | [diff] [blame] | 74 | void genOrUpdateMeshBuffer(GLuint* buffer, GLsizeiptr size, const void* data, GLenum usage); |
| 75 | void deleteMeshBuffer(GLuint); |
| 76 | |
Chris Craik | 96a5c4c | 2015-01-27 15:46:35 -0800 | [diff] [blame] | 77 | /////////////////////////////////////////////////////////////////////////////// |
| 78 | // Vertices |
| 79 | /////////////////////////////////////////////////////////////////////////////// |
| 80 | /** |
| 81 | * Binds an attrib to the specified float vertex pointer. |
| 82 | * Assumes a stride of gTextureVertexStride and a size of 2. |
| 83 | */ |
Chris Craik | 1b7db40 | 2016-02-24 18:25:32 -0800 | [diff] [blame^] | 84 | void bindPositionVertexPointer(const GLvoid* vertices, |
Chris Craik | 6c15ffa | 2015-02-02 13:50:55 -0800 | [diff] [blame] | 85 | GLsizei stride = kTextureVertexStride); |
Chris Craik | 96a5c4c | 2015-01-27 15:46:35 -0800 | [diff] [blame] | 86 | |
| 87 | /** |
| 88 | * Binds an attrib to the specified float vertex pointer. |
| 89 | * Assumes a stride of gTextureVertexStride and a size of 2. |
| 90 | */ |
Chris Craik | 1b7db40 | 2016-02-24 18:25:32 -0800 | [diff] [blame^] | 91 | void bindTexCoordsVertexPointer(const GLvoid* vertices, |
Chris Craik | 6c15ffa | 2015-02-02 13:50:55 -0800 | [diff] [blame] | 92 | GLsizei stride = kTextureVertexStride); |
Chris Craik | 96a5c4c | 2015-01-27 15:46:35 -0800 | [diff] [blame] | 93 | |
| 94 | /** |
| 95 | * Resets the vertex pointers. |
| 96 | */ |
| 97 | void resetVertexPointers(); |
Chris Craik | 96a5c4c | 2015-01-27 15:46:35 -0800 | [diff] [blame] | 98 | |
| 99 | void enableTexCoordsVertexArray(); |
| 100 | void disableTexCoordsVertexArray(); |
| 101 | |
| 102 | /////////////////////////////////////////////////////////////////////////////// |
| 103 | // Indices |
| 104 | /////////////////////////////////////////////////////////////////////////////// |
Chris Craik | 1b7db40 | 2016-02-24 18:25:32 -0800 | [diff] [blame^] | 105 | void bindIndicesBuffer(const GLuint buffer); |
| 106 | void unbindIndicesBuffer(); |
Chris Craik | 96a5c4c | 2015-01-27 15:46:35 -0800 | [diff] [blame] | 107 | |
Chris Craik | 0318887 | 2015-02-02 18:39:33 -0800 | [diff] [blame] | 108 | /////////////////////////////////////////////////////////////////////////////// |
| 109 | // Getters - for use in Glop building |
| 110 | /////////////////////////////////////////////////////////////////////////////// |
| 111 | GLuint getUnitQuadVBO() { return mUnitQuadBuffer; } |
Chris Craik | 2ab95d7 | 2015-02-06 15:25:51 -0800 | [diff] [blame] | 112 | GLuint getQuadListIBO() { return mQuadListIndices; } |
Chris Craik | 96a5c4c | 2015-01-27 15:46:35 -0800 | [diff] [blame] | 113 | private: |
| 114 | MeshState(); |
Chris Craik | 96a5c4c | 2015-01-27 15:46:35 -0800 | [diff] [blame] | 115 | |
Chris Craik | 0318887 | 2015-02-02 18:39:33 -0800 | [diff] [blame] | 116 | GLuint mUnitQuadBuffer; |
Chris Craik | 96a5c4c | 2015-01-27 15:46:35 -0800 | [diff] [blame] | 117 | |
| 118 | GLuint mCurrentBuffer; |
| 119 | GLuint mCurrentIndicesBuffer; |
| 120 | GLuint mCurrentPixelBuffer; |
| 121 | |
| 122 | const void* mCurrentPositionPointer; |
| 123 | GLsizei mCurrentPositionStride; |
| 124 | const void* mCurrentTexCoordsPointer; |
| 125 | GLsizei mCurrentTexCoordsStride; |
| 126 | |
| 127 | bool mTexCoordsArrayEnabled; |
| 128 | |
| 129 | // Global index buffer |
| 130 | GLuint mQuadListIndices; |
Chris Craik | 96a5c4c | 2015-01-27 15:46:35 -0800 | [diff] [blame] | 131 | }; |
| 132 | |
| 133 | } /* namespace uirenderer */ |
| 134 | } /* namespace android */ |
| 135 | |
| 136 | #endif // RENDERSTATE_MESHSTATE_H |