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 | { |
| 28 | memset(mAttribs, 0, sizeof(mAttribs)); |
| 29 | mActiveBuffer = 0; |
| 30 | } |
| 31 | |
| 32 | VertexArray::~VertexArray() |
| 33 | { |
| 34 | } |
| 35 | |
| 36 | |
| 37 | void VertexArray::clearAll() |
| 38 | { |
| 39 | memset(mAttribs, 0, sizeof(mAttribs)); |
| 40 | mActiveBuffer = 0; |
| 41 | } |
| 42 | |
| 43 | void VertexArray::clear(AttribName n) |
| 44 | { |
| 45 | mAttribs[n].size = 0; |
| 46 | } |
| 47 | |
| 48 | void VertexArray::setPosition(uint32_t size, uint32_t type, uint32_t stride, uint32_t offset) |
| 49 | { |
| 50 | mAttribs[POSITION].buffer = mActiveBuffer; |
| 51 | mAttribs[POSITION].type = type; |
| 52 | mAttribs[POSITION].size = size; |
| 53 | mAttribs[POSITION].offset = offset; |
| 54 | mAttribs[POSITION].stride = stride; |
| 55 | mAttribs[POSITION].normalized = false; |
| 56 | } |
| 57 | |
| 58 | void VertexArray::setColor(uint32_t size, uint32_t type, uint32_t stride, uint32_t offset) |
| 59 | { |
| 60 | mAttribs[COLOR].buffer = mActiveBuffer; |
| 61 | mAttribs[COLOR].type = type; |
| 62 | mAttribs[COLOR].size = size; |
| 63 | mAttribs[COLOR].offset = offset; |
| 64 | mAttribs[COLOR].stride = stride; |
| 65 | mAttribs[COLOR].normalized = type != GL_FLOAT; |
| 66 | } |
| 67 | |
| 68 | void VertexArray::setNormal(uint32_t type, uint32_t stride, uint32_t offset) |
| 69 | { |
| 70 | mAttribs[NORMAL].buffer = mActiveBuffer; |
| 71 | mAttribs[NORMAL].type = type; |
| 72 | mAttribs[NORMAL].size = 3; |
| 73 | mAttribs[NORMAL].offset = offset; |
| 74 | mAttribs[NORMAL].stride = stride; |
| 75 | mAttribs[NORMAL].normalized = type != GL_FLOAT; |
| 76 | } |
| 77 | |
| 78 | void VertexArray::setPointSize(uint32_t type, uint32_t stride, uint32_t offset) |
| 79 | { |
| 80 | mAttribs[POINT_SIZE].buffer = mActiveBuffer; |
| 81 | mAttribs[POINT_SIZE].type = type; |
| 82 | mAttribs[POINT_SIZE].size = 1; |
| 83 | mAttribs[POINT_SIZE].offset = offset; |
| 84 | mAttribs[POINT_SIZE].stride = stride; |
| 85 | mAttribs[POINT_SIZE].normalized = false; |
| 86 | } |
| 87 | |
| 88 | void VertexArray::setTexture(uint32_t size, uint32_t type, uint32_t stride, uint32_t offset, uint32_t num) |
| 89 | { |
| 90 | mAttribs[TEXTURE_0 + num].buffer = mActiveBuffer; |
| 91 | mAttribs[TEXTURE_0 + num].type = type; |
| 92 | mAttribs[TEXTURE_0 + num].size = size; |
| 93 | mAttribs[TEXTURE_0 + num].offset = offset; |
| 94 | mAttribs[TEXTURE_0 + num].stride = stride; |
| 95 | mAttribs[TEXTURE_0 + num].normalized = false; |
| 96 | } |
| 97 | |
| 98 | void VertexArray::logAttrib(uint32_t idx) const { |
Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 99 | LOGE("va %i: buf=%i size=%i type=0x%x stride=0x%x norm=%i offset=0x%x", idx, |
Jason Sams | bb51c40 | 2009-11-25 13:22:07 -0800 | [diff] [blame] | 100 | mAttribs[idx].buffer, |
| 101 | mAttribs[idx].size, |
| 102 | mAttribs[idx].type, |
| 103 | mAttribs[idx].stride, |
Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 104 | mAttribs[idx].normalized, |
Jason Sams | bb51c40 | 2009-11-25 13:22:07 -0800 | [diff] [blame] | 105 | mAttribs[idx].offset); |
| 106 | } |
| 107 | |
Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 108 | void VertexArray::setupGL(const Context *rsc, class VertexArrayState *state) const |
Jason Sams | bb51c40 | 2009-11-25 13:22:07 -0800 | [diff] [blame] | 109 | { |
| 110 | if (mAttribs[POSITION].size) { |
| 111 | //logAttrib(POSITION); |
| 112 | glEnableClientState(GL_VERTEX_ARRAY); |
| 113 | glBindBuffer(GL_ARRAY_BUFFER, mAttribs[POSITION].buffer); |
| 114 | glVertexPointer(mAttribs[POSITION].size, |
| 115 | mAttribs[POSITION].type, |
| 116 | mAttribs[POSITION].stride, |
| 117 | (void *)mAttribs[POSITION].offset); |
| 118 | } else { |
| 119 | rsAssert(0); |
| 120 | } |
| 121 | |
| 122 | if (mAttribs[NORMAL].size) { |
| 123 | //logAttrib(NORMAL); |
| 124 | glEnableClientState(GL_NORMAL_ARRAY); |
| 125 | rsAssert(mAttribs[NORMAL].size == 3); |
| 126 | glBindBuffer(GL_ARRAY_BUFFER, mAttribs[NORMAL].buffer); |
| 127 | glNormalPointer(mAttribs[NORMAL].type, |
| 128 | mAttribs[NORMAL].stride, |
| 129 | (void *)mAttribs[NORMAL].offset); |
| 130 | } else { |
| 131 | glDisableClientState(GL_NORMAL_ARRAY); |
| 132 | } |
| 133 | |
| 134 | if (mAttribs[COLOR].size) { |
| 135 | //logAttrib(COLOR); |
| 136 | glEnableClientState(GL_COLOR_ARRAY); |
| 137 | glBindBuffer(GL_ARRAY_BUFFER, mAttribs[COLOR].buffer); |
| 138 | glColorPointer(mAttribs[COLOR].size, |
| 139 | mAttribs[COLOR].type, |
| 140 | mAttribs[COLOR].stride, |
| 141 | (void *)mAttribs[COLOR].offset); |
| 142 | } else { |
| 143 | glDisableClientState(GL_COLOR_ARRAY); |
| 144 | } |
| 145 | |
| 146 | for (uint32_t ct=0; ct < RS_MAX_TEXTURE; ct++) { |
| 147 | glClientActiveTexture(GL_TEXTURE0 + ct); |
| 148 | if (mAttribs[TEXTURE_0 + ct].size) { |
| 149 | //logAttrib(TEXTURE_0 + ct); |
| 150 | glEnableClientState(GL_TEXTURE_COORD_ARRAY); |
| 151 | glBindBuffer(GL_ARRAY_BUFFER, mAttribs[TEXTURE_0 + ct].buffer); |
| 152 | glTexCoordPointer(mAttribs[TEXTURE_0 + ct].size, |
| 153 | mAttribs[TEXTURE_0 + ct].type, |
| 154 | mAttribs[TEXTURE_0 + ct].stride, |
| 155 | (void *)mAttribs[TEXTURE_0 + ct].offset); |
| 156 | } else { |
| 157 | glDisableClientState(GL_TEXTURE_COORD_ARRAY); |
| 158 | } |
| 159 | } |
| 160 | glClientActiveTexture(GL_TEXTURE0); |
| 161 | |
| 162 | if (mAttribs[POINT_SIZE].size) { |
| 163 | //logAttrib(POINT_SIZE); |
| 164 | glEnableClientState(GL_POINT_SIZE_ARRAY_OES); |
| 165 | glBindBuffer(GL_ARRAY_BUFFER, mAttribs[POINT_SIZE].buffer); |
| 166 | glPointSizePointerOES(mAttribs[POINT_SIZE].type, |
| 167 | mAttribs[POINT_SIZE].stride, |
| 168 | (void *)mAttribs[POINT_SIZE].offset); |
| 169 | } else { |
| 170 | glDisableClientState(GL_POINT_SIZE_ARRAY_OES); |
| 171 | } |
Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 172 | rsc->checkError("VertexArray::setupGL"); |
Jason Sams | bb51c40 | 2009-11-25 13:22:07 -0800 | [diff] [blame] | 173 | } |
| 174 | |
Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 175 | void VertexArray::setupGL2(const Context *rsc, class VertexArrayState *state, ShaderCache *sc) const |
Jason Sams | bb51c40 | 2009-11-25 13:22:07 -0800 | [diff] [blame] | 176 | { |
| 177 | for (int ct=1; ct < _LAST; ct++) { |
| 178 | glDisableVertexAttribArray(ct); |
| 179 | } |
| 180 | |
| 181 | for (int ct=0; ct < _LAST; ct++) { |
| 182 | if (mAttribs[ct].size) { |
| 183 | //logAttrib(ct); |
| 184 | glEnableVertexAttribArray(sc->vtxAttribSlot(ct)); |
| 185 | glBindBuffer(GL_ARRAY_BUFFER, mAttribs[ct].buffer); |
| 186 | //LOGV("attp %i %i", ct, sc->vtxAttribSlot(ct)); |
| 187 | |
| 188 | glVertexAttribPointer(sc->vtxAttribSlot(ct), |
| 189 | mAttribs[ct].size, |
| 190 | mAttribs[ct].type, |
| 191 | mAttribs[ct].normalized, |
| 192 | mAttribs[ct].stride, |
| 193 | (void *)mAttribs[ct].offset); |
| 194 | } else { |
| 195 | //glDisableVertexAttribArray(ct); |
| 196 | rsAssert(ct); |
| 197 | } |
| 198 | } |
Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 199 | rsc->checkError("VertexArray::setupGL2"); |
Jason Sams | bb51c40 | 2009-11-25 13:22:07 -0800 | [diff] [blame] | 200 | } |
| 201 | //////////////////////////////////////////// |
| 202 | |
| 203 | void VertexArrayState::init(Context *) { |
| 204 | memset(this, 0, sizeof(this)); |
| 205 | } |
| 206 | |