Jason Sams | bb51c40 | 2009-11-25 13:22:07 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2009 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 | |
| 17 | #include "rsContext.h" |
| 18 | |
| 19 | #include <GLES/gl.h> |
| 20 | #include <GLES2/gl2.h> |
| 21 | |
| 22 | using namespace android; |
| 23 | using namespace android::renderscript; |
| 24 | |
| 25 | |
| 26 | VertexArray::VertexArray() |
| 27 | { |
Jason Sams | bb51c40 | 2009-11-25 13:22:07 -0800 | [diff] [blame] | 28 | mActiveBuffer = 0; |
Jason Sams | a09a6e1 | 2010-01-06 11:57:52 -0800 | [diff] [blame^] | 29 | mUserCount = 0; |
Jason Sams | bb51c40 | 2009-11-25 13:22:07 -0800 | [diff] [blame] | 30 | } |
| 31 | |
| 32 | VertexArray::~VertexArray() |
| 33 | { |
| 34 | } |
| 35 | |
| 36 | |
| 37 | void VertexArray::clearAll() |
| 38 | { |
Jason Sams | a09a6e1 | 2010-01-06 11:57:52 -0800 | [diff] [blame^] | 39 | for (uint32_t ct=0; ct < RS_MAX_ATTRIBS; ct++) { |
| 40 | mAttribs[ct].clear(); |
| 41 | } |
Jason Sams | bb51c40 | 2009-11-25 13:22:07 -0800 | [diff] [blame] | 42 | mActiveBuffer = 0; |
Jason Sams | a09a6e1 | 2010-01-06 11:57:52 -0800 | [diff] [blame^] | 43 | mUserCount = 0; |
| 44 | } |
| 45 | |
| 46 | VertexArray::Attrib::Attrib() |
| 47 | { |
| 48 | clear(); |
| 49 | } |
| 50 | |
| 51 | void VertexArray::Attrib::set(const Attrib &a) |
| 52 | { |
| 53 | buffer = a.buffer; |
| 54 | offset = a.offset; |
| 55 | type = a.type; |
| 56 | size = a.size; |
| 57 | stride = a.stride; |
| 58 | normalized = a.normalized; |
| 59 | name.setTo(a.name); |
| 60 | } |
| 61 | |
| 62 | void VertexArray::Attrib::clear() |
| 63 | { |
| 64 | buffer = 0; |
| 65 | offset = 0; |
| 66 | type = 0; |
| 67 | size = 0; |
| 68 | stride = 0; |
| 69 | normalized = false; |
| 70 | name.setTo(""); |
Jason Sams | bb51c40 | 2009-11-25 13:22:07 -0800 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | void VertexArray::clear(AttribName n) |
| 74 | { |
Jason Sams | a09a6e1 | 2010-01-06 11:57:52 -0800 | [diff] [blame^] | 75 | mAttribs[n].clear(); |
Jason Sams | bb51c40 | 2009-11-25 13:22:07 -0800 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | void VertexArray::setPosition(uint32_t size, uint32_t type, uint32_t stride, uint32_t offset) |
| 79 | { |
| 80 | mAttribs[POSITION].buffer = mActiveBuffer; |
| 81 | mAttribs[POSITION].type = type; |
| 82 | mAttribs[POSITION].size = size; |
| 83 | mAttribs[POSITION].offset = offset; |
| 84 | mAttribs[POSITION].stride = stride; |
| 85 | mAttribs[POSITION].normalized = false; |
| 86 | } |
| 87 | |
| 88 | void VertexArray::setColor(uint32_t size, uint32_t type, uint32_t stride, uint32_t offset) |
| 89 | { |
| 90 | mAttribs[COLOR].buffer = mActiveBuffer; |
| 91 | mAttribs[COLOR].type = type; |
| 92 | mAttribs[COLOR].size = size; |
| 93 | mAttribs[COLOR].offset = offset; |
| 94 | mAttribs[COLOR].stride = stride; |
| 95 | mAttribs[COLOR].normalized = type != GL_FLOAT; |
| 96 | } |
| 97 | |
| 98 | void VertexArray::setNormal(uint32_t type, uint32_t stride, uint32_t offset) |
| 99 | { |
| 100 | mAttribs[NORMAL].buffer = mActiveBuffer; |
| 101 | mAttribs[NORMAL].type = type; |
| 102 | mAttribs[NORMAL].size = 3; |
| 103 | mAttribs[NORMAL].offset = offset; |
| 104 | mAttribs[NORMAL].stride = stride; |
| 105 | mAttribs[NORMAL].normalized = type != GL_FLOAT; |
| 106 | } |
| 107 | |
| 108 | void VertexArray::setPointSize(uint32_t type, uint32_t stride, uint32_t offset) |
| 109 | { |
| 110 | mAttribs[POINT_SIZE].buffer = mActiveBuffer; |
| 111 | mAttribs[POINT_SIZE].type = type; |
| 112 | mAttribs[POINT_SIZE].size = 1; |
| 113 | mAttribs[POINT_SIZE].offset = offset; |
| 114 | mAttribs[POINT_SIZE].stride = stride; |
| 115 | mAttribs[POINT_SIZE].normalized = false; |
| 116 | } |
| 117 | |
Jason Sams | a09a6e1 | 2010-01-06 11:57:52 -0800 | [diff] [blame^] | 118 | void VertexArray::setTexture(uint32_t size, uint32_t type, uint32_t stride, uint32_t offset) |
Jason Sams | bb51c40 | 2009-11-25 13:22:07 -0800 | [diff] [blame] | 119 | { |
Jason Sams | a09a6e1 | 2010-01-06 11:57:52 -0800 | [diff] [blame^] | 120 | mAttribs[TEXTURE].buffer = mActiveBuffer; |
| 121 | mAttribs[TEXTURE].type = type; |
| 122 | mAttribs[TEXTURE].size = size; |
| 123 | mAttribs[TEXTURE].offset = offset; |
| 124 | mAttribs[TEXTURE].stride = stride; |
| 125 | mAttribs[TEXTURE].normalized = false; |
Jason Sams | bb51c40 | 2009-11-25 13:22:07 -0800 | [diff] [blame] | 126 | } |
| 127 | |
Jason Sams | a09a6e1 | 2010-01-06 11:57:52 -0800 | [diff] [blame^] | 128 | void VertexArray::setUser(const Attrib &a, uint32_t stride) |
| 129 | { |
| 130 | mAttribs[mUserCount].set(a); |
| 131 | mAttribs[mUserCount].buffer = mActiveBuffer; |
| 132 | mAttribs[mUserCount].stride = stride; |
| 133 | mUserCount ++; |
| 134 | } |
| 135 | |
| 136 | void VertexArray::logAttrib(uint32_t idx, uint32_t slot) const { |
| 137 | LOGE("va %i: slot=%i name=%s buf=%i size=%i type=0x%x stride=0x%x norm=%i offset=0x%x", idx, slot, |
| 138 | mAttribs[idx].name.string(), |
Jason Sams | bb51c40 | 2009-11-25 13:22:07 -0800 | [diff] [blame] | 139 | mAttribs[idx].buffer, |
| 140 | mAttribs[idx].size, |
| 141 | mAttribs[idx].type, |
| 142 | mAttribs[idx].stride, |
Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 143 | mAttribs[idx].normalized, |
Jason Sams | bb51c40 | 2009-11-25 13:22:07 -0800 | [diff] [blame] | 144 | mAttribs[idx].offset); |
| 145 | } |
| 146 | |
Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 147 | void VertexArray::setupGL(const Context *rsc, class VertexArrayState *state) const |
Jason Sams | bb51c40 | 2009-11-25 13:22:07 -0800 | [diff] [blame] | 148 | { |
| 149 | if (mAttribs[POSITION].size) { |
| 150 | //logAttrib(POSITION); |
| 151 | glEnableClientState(GL_VERTEX_ARRAY); |
| 152 | glBindBuffer(GL_ARRAY_BUFFER, mAttribs[POSITION].buffer); |
| 153 | glVertexPointer(mAttribs[POSITION].size, |
| 154 | mAttribs[POSITION].type, |
| 155 | mAttribs[POSITION].stride, |
| 156 | (void *)mAttribs[POSITION].offset); |
| 157 | } else { |
| 158 | rsAssert(0); |
| 159 | } |
| 160 | |
| 161 | if (mAttribs[NORMAL].size) { |
| 162 | //logAttrib(NORMAL); |
| 163 | glEnableClientState(GL_NORMAL_ARRAY); |
| 164 | rsAssert(mAttribs[NORMAL].size == 3); |
| 165 | glBindBuffer(GL_ARRAY_BUFFER, mAttribs[NORMAL].buffer); |
| 166 | glNormalPointer(mAttribs[NORMAL].type, |
| 167 | mAttribs[NORMAL].stride, |
| 168 | (void *)mAttribs[NORMAL].offset); |
| 169 | } else { |
| 170 | glDisableClientState(GL_NORMAL_ARRAY); |
| 171 | } |
| 172 | |
| 173 | if (mAttribs[COLOR].size) { |
| 174 | //logAttrib(COLOR); |
| 175 | glEnableClientState(GL_COLOR_ARRAY); |
| 176 | glBindBuffer(GL_ARRAY_BUFFER, mAttribs[COLOR].buffer); |
| 177 | glColorPointer(mAttribs[COLOR].size, |
| 178 | mAttribs[COLOR].type, |
| 179 | mAttribs[COLOR].stride, |
| 180 | (void *)mAttribs[COLOR].offset); |
| 181 | } else { |
| 182 | glDisableClientState(GL_COLOR_ARRAY); |
| 183 | } |
| 184 | |
Jason Sams | bb51c40 | 2009-11-25 13:22:07 -0800 | [diff] [blame] | 185 | glClientActiveTexture(GL_TEXTURE0); |
Jason Sams | a09a6e1 | 2010-01-06 11:57:52 -0800 | [diff] [blame^] | 186 | if (mAttribs[TEXTURE].size) { |
| 187 | //logAttrib(TEXTURE); |
| 188 | glEnableClientState(GL_TEXTURE_COORD_ARRAY); |
| 189 | glBindBuffer(GL_ARRAY_BUFFER, mAttribs[TEXTURE].buffer); |
| 190 | glTexCoordPointer(mAttribs[TEXTURE].size, |
| 191 | mAttribs[TEXTURE].type, |
| 192 | mAttribs[TEXTURE].stride, |
| 193 | (void *)mAttribs[TEXTURE].offset); |
| 194 | } else { |
| 195 | glDisableClientState(GL_TEXTURE_COORD_ARRAY); |
| 196 | } |
Jason Sams | bb51c40 | 2009-11-25 13:22:07 -0800 | [diff] [blame] | 197 | |
| 198 | if (mAttribs[POINT_SIZE].size) { |
| 199 | //logAttrib(POINT_SIZE); |
| 200 | glEnableClientState(GL_POINT_SIZE_ARRAY_OES); |
| 201 | glBindBuffer(GL_ARRAY_BUFFER, mAttribs[POINT_SIZE].buffer); |
| 202 | glPointSizePointerOES(mAttribs[POINT_SIZE].type, |
| 203 | mAttribs[POINT_SIZE].stride, |
| 204 | (void *)mAttribs[POINT_SIZE].offset); |
| 205 | } else { |
| 206 | glDisableClientState(GL_POINT_SIZE_ARRAY_OES); |
| 207 | } |
Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 208 | rsc->checkError("VertexArray::setupGL"); |
Jason Sams | bb51c40 | 2009-11-25 13:22:07 -0800 | [diff] [blame] | 209 | } |
| 210 | |
Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 211 | void VertexArray::setupGL2(const Context *rsc, class VertexArrayState *state, ShaderCache *sc) const |
Jason Sams | bb51c40 | 2009-11-25 13:22:07 -0800 | [diff] [blame] | 212 | { |
| 213 | for (int ct=1; ct < _LAST; ct++) { |
| 214 | glDisableVertexAttribArray(ct); |
| 215 | } |
| 216 | |
Jason Sams | a09a6e1 | 2010-01-06 11:57:52 -0800 | [diff] [blame^] | 217 | for (uint32_t ct=0; ct < RS_MAX_ATTRIBS; ct++) { |
Jason Sams | bb51c40 | 2009-11-25 13:22:07 -0800 | [diff] [blame] | 218 | if (mAttribs[ct].size) { |
Jason Sams | a09a6e1 | 2010-01-06 11:57:52 -0800 | [diff] [blame^] | 219 | //logAttrib(ct, sc->vtxAttribSlot(ct)); |
| 220 | rsAssert(sc->vtxAttribSlot(ct) >= 0); |
Jason Sams | bb51c40 | 2009-11-25 13:22:07 -0800 | [diff] [blame] | 221 | glEnableVertexAttribArray(sc->vtxAttribSlot(ct)); |
| 222 | glBindBuffer(GL_ARRAY_BUFFER, mAttribs[ct].buffer); |
Jason Sams | bb51c40 | 2009-11-25 13:22:07 -0800 | [diff] [blame] | 223 | |
| 224 | glVertexAttribPointer(sc->vtxAttribSlot(ct), |
| 225 | mAttribs[ct].size, |
| 226 | mAttribs[ct].type, |
| 227 | mAttribs[ct].normalized, |
| 228 | mAttribs[ct].stride, |
| 229 | (void *)mAttribs[ct].offset); |
Jason Sams | bb51c40 | 2009-11-25 13:22:07 -0800 | [diff] [blame] | 230 | } |
| 231 | } |
Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 232 | rsc->checkError("VertexArray::setupGL2"); |
Jason Sams | bb51c40 | 2009-11-25 13:22:07 -0800 | [diff] [blame] | 233 | } |
| 234 | //////////////////////////////////////////// |
| 235 | |
| 236 | void VertexArrayState::init(Context *) { |
Jason Sams | bb51c40 | 2009-11-25 13:22:07 -0800 | [diff] [blame] | 237 | } |
| 238 | |