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 | df48b57 | 2010-01-25 12:31:24 -0800 | [diff] [blame] | 28 | clearAll(); |
Jason Sams | bb51c40 | 2009-11-25 13:22:07 -0800 | [diff] [blame] | 29 | } |
| 30 | |
| 31 | VertexArray::~VertexArray() |
| 32 | { |
| 33 | } |
| 34 | |
| 35 | |
| 36 | void VertexArray::clearAll() |
| 37 | { |
Jason Sams | a09a6e1 | 2010-01-06 11:57:52 -0800 | [diff] [blame] | 38 | for (uint32_t ct=0; ct < RS_MAX_ATTRIBS; ct++) { |
| 39 | mAttribs[ct].clear(); |
| 40 | } |
Jason Sams | bb51c40 | 2009-11-25 13:22:07 -0800 | [diff] [blame] | 41 | mActiveBuffer = 0; |
Jason Sams | df48b57 | 2010-01-25 12:31:24 -0800 | [diff] [blame] | 42 | mCount = 0; |
Jason Sams | a09a6e1 | 2010-01-06 11:57:52 -0800 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | VertexArray::Attrib::Attrib() |
| 46 | { |
| 47 | clear(); |
| 48 | } |
| 49 | |
| 50 | void VertexArray::Attrib::set(const Attrib &a) |
| 51 | { |
| 52 | buffer = a.buffer; |
| 53 | offset = a.offset; |
| 54 | type = a.type; |
| 55 | size = a.size; |
| 56 | stride = a.stride; |
| 57 | normalized = a.normalized; |
Jason Sams | df48b57 | 2010-01-25 12:31:24 -0800 | [diff] [blame] | 58 | kind = RS_KIND_USER; |
Jason Sams | a09a6e1 | 2010-01-06 11:57:52 -0800 | [diff] [blame] | 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 | |
Jason Sams | df48b57 | 2010-01-25 12:31:24 -0800 | [diff] [blame] | 73 | void VertexArray::clear(uint32_t n) |
Jason Sams | bb51c40 | 2009-11-25 13:22:07 -0800 | [diff] [blame] | 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 | |
Jason Sams | df48b57 | 2010-01-25 12:31:24 -0800 | [diff] [blame] | 78 | void VertexArray::addUser(const Attrib &a, uint32_t stride) |
Jason Sams | bb51c40 | 2009-11-25 13:22:07 -0800 | [diff] [blame] | 79 | { |
Jason Sams | df48b57 | 2010-01-25 12:31:24 -0800 | [diff] [blame] | 80 | assert(mCount < RS_MAX_ATTRIBS); |
| 81 | mAttribs[mCount].set(a); |
| 82 | mAttribs[mCount].buffer = mActiveBuffer; |
| 83 | mAttribs[mCount].stride = stride; |
| 84 | mAttribs[mCount].kind = RS_KIND_USER; |
| 85 | mCount ++; |
Jason Sams | bb51c40 | 2009-11-25 13:22:07 -0800 | [diff] [blame] | 86 | } |
| 87 | |
Jason Sams | df48b57 | 2010-01-25 12:31:24 -0800 | [diff] [blame] | 88 | void VertexArray::addLegacy(uint32_t type, uint32_t size, uint32_t stride, RsDataKind kind, bool normalized, uint32_t offset) |
Jason Sams | bb51c40 | 2009-11-25 13:22:07 -0800 | [diff] [blame] | 89 | { |
Jason Sams | df48b57 | 2010-01-25 12:31:24 -0800 | [diff] [blame] | 90 | assert(mCount < RS_MAX_ATTRIBS); |
| 91 | mAttribs[mCount].clear(); |
| 92 | mAttribs[mCount].type = type; |
| 93 | mAttribs[mCount].size = size; |
| 94 | mAttribs[mCount].offset = offset; |
| 95 | mAttribs[mCount].normalized = normalized; |
| 96 | mAttribs[mCount].buffer = mActiveBuffer; |
| 97 | mAttribs[mCount].stride = stride; |
| 98 | mAttribs[mCount].kind = kind; |
| 99 | mCount ++; |
Jason Sams | a09a6e1 | 2010-01-06 11:57:52 -0800 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | void VertexArray::logAttrib(uint32_t idx, uint32_t slot) const { |
Jason Sams | df48b57 | 2010-01-25 12:31:24 -0800 | [diff] [blame] | 103 | LOGE("va %i: slot=%i name=%s buf=%i size=%i type=0x%x kind=%i stride=0x%x norm=%i offset=0x%x", idx, slot, |
Jason Sams | a09a6e1 | 2010-01-06 11:57:52 -0800 | [diff] [blame] | 104 | mAttribs[idx].name.string(), |
Jason Sams | bb51c40 | 2009-11-25 13:22:07 -0800 | [diff] [blame] | 105 | mAttribs[idx].buffer, |
| 106 | mAttribs[idx].size, |
| 107 | mAttribs[idx].type, |
Jason Sams | df48b57 | 2010-01-25 12:31:24 -0800 | [diff] [blame] | 108 | mAttribs[idx].kind, |
Jason Sams | bb51c40 | 2009-11-25 13:22:07 -0800 | [diff] [blame] | 109 | mAttribs[idx].stride, |
Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 110 | mAttribs[idx].normalized, |
Jason Sams | bb51c40 | 2009-11-25 13:22:07 -0800 | [diff] [blame] | 111 | mAttribs[idx].offset); |
| 112 | } |
| 113 | |
Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 114 | void VertexArray::setupGL(const Context *rsc, class VertexArrayState *state) const |
Jason Sams | bb51c40 | 2009-11-25 13:22:07 -0800 | [diff] [blame] | 115 | { |
Jason Sams | bb51c40 | 2009-11-25 13:22:07 -0800 | [diff] [blame] | 116 | glClientActiveTexture(GL_TEXTURE0); |
Jason Sams | df48b57 | 2010-01-25 12:31:24 -0800 | [diff] [blame] | 117 | glDisableClientState(GL_NORMAL_ARRAY); |
| 118 | glDisableClientState(GL_COLOR_ARRAY); |
| 119 | glDisableClientState(GL_TEXTURE_COORD_ARRAY); |
| 120 | glDisableClientState(GL_POINT_SIZE_ARRAY_OES); |
| 121 | |
| 122 | for (uint32_t ct=0; ct < mCount; ct++) { |
| 123 | switch(mAttribs[ct].kind) { |
| 124 | case RS_KIND_POSITION: |
| 125 | //logAttrib(POSITION); |
| 126 | glEnableClientState(GL_VERTEX_ARRAY); |
| 127 | glBindBuffer(GL_ARRAY_BUFFER, mAttribs[ct].buffer); |
| 128 | glVertexPointer(mAttribs[ct].size, |
| 129 | mAttribs[ct].type, |
| 130 | mAttribs[ct].stride, |
| 131 | (void *)mAttribs[ct].offset); |
| 132 | break; |
| 133 | |
| 134 | case RS_KIND_NORMAL: |
| 135 | //logAttrib(NORMAL); |
| 136 | glEnableClientState(GL_NORMAL_ARRAY); |
| 137 | rsAssert(mAttribs[ct].size == 3); |
| 138 | glBindBuffer(GL_ARRAY_BUFFER, mAttribs[ct].buffer); |
| 139 | glNormalPointer(mAttribs[ct].type, |
| 140 | mAttribs[ct].stride, |
| 141 | (void *)mAttribs[ct].offset); |
| 142 | break; |
| 143 | |
| 144 | case RS_KIND_COLOR: |
| 145 | //logAttrib(COLOR); |
| 146 | glEnableClientState(GL_COLOR_ARRAY); |
| 147 | glBindBuffer(GL_ARRAY_BUFFER, mAttribs[ct].buffer); |
| 148 | glColorPointer(mAttribs[ct].size, |
| 149 | mAttribs[ct].type, |
| 150 | mAttribs[ct].stride, |
| 151 | (void *)mAttribs[ct].offset); |
| 152 | break; |
| 153 | |
| 154 | case RS_KIND_TEXTURE: |
| 155 | //logAttrib(TEXTURE); |
| 156 | glEnableClientState(GL_TEXTURE_COORD_ARRAY); |
| 157 | glBindBuffer(GL_ARRAY_BUFFER, mAttribs[ct].buffer); |
| 158 | glTexCoordPointer(mAttribs[ct].size, |
| 159 | mAttribs[ct].type, |
| 160 | mAttribs[ct].stride, |
| 161 | (void *)mAttribs[ct].offset); |
| 162 | break; |
| 163 | |
| 164 | case RS_KIND_POINT_SIZE: |
| 165 | //logAttrib(POINT_SIZE); |
| 166 | glEnableClientState(GL_POINT_SIZE_ARRAY_OES); |
| 167 | glBindBuffer(GL_ARRAY_BUFFER, mAttribs[ct].buffer); |
| 168 | glPointSizePointerOES(mAttribs[ct].type, |
| 169 | mAttribs[ct].stride, |
| 170 | (void *)mAttribs[ct].offset); |
| 171 | break; |
| 172 | |
| 173 | default: |
| 174 | rsAssert(0); |
| 175 | } |
Jason Sams | a09a6e1 | 2010-01-06 11:57:52 -0800 | [diff] [blame] | 176 | } |
Jason Sams | bb51c40 | 2009-11-25 13:22:07 -0800 | [diff] [blame] | 177 | |
Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 178 | rsc->checkError("VertexArray::setupGL"); |
Jason Sams | bb51c40 | 2009-11-25 13:22:07 -0800 | [diff] [blame] | 179 | } |
| 180 | |
Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 181 | void VertexArray::setupGL2(const Context *rsc, class VertexArrayState *state, ShaderCache *sc) const |
Jason Sams | bb51c40 | 2009-11-25 13:22:07 -0800 | [diff] [blame] | 182 | { |
Jason Sams | 5dbfe93 | 2010-01-27 14:41:43 -0800 | [diff] [blame] | 183 | rsc->checkError("VertexArray::setupGL2 start"); |
| 184 | for (uint32_t ct=1; ct <= state->mLastEnableCount; ct++) { |
Jason Sams | bb51c40 | 2009-11-25 13:22:07 -0800 | [diff] [blame] | 185 | glDisableVertexAttribArray(ct); |
| 186 | } |
| 187 | |
Jason Sams | 5dbfe93 | 2010-01-27 14:41:43 -0800 | [diff] [blame] | 188 | rsc->checkError("VertexArray::setupGL2 disabled"); |
Jason Sams | df48b57 | 2010-01-25 12:31:24 -0800 | [diff] [blame] | 189 | for (uint32_t ct=0; ct < mCount; ct++) { |
| 190 | uint32_t slot = 0; |
| 191 | if (sc->isUserVertexProgram()) { |
| 192 | slot = sc->vtxAttribSlot(ct); |
| 193 | } else { |
Jason Sams | 5bec3aa | 2010-02-08 16:31:39 -0800 | [diff] [blame] | 194 | if (mAttribs[ct].kind == RS_KIND_USER) { |
| 195 | continue; |
| 196 | } |
Jason Sams | df48b57 | 2010-01-25 12:31:24 -0800 | [diff] [blame] | 197 | slot = sc->vtxAttribSlot(mAttribs[ct].kind); |
Jason Sams | bb51c40 | 2009-11-25 13:22:07 -0800 | [diff] [blame] | 198 | } |
Jason Sams | df48b57 | 2010-01-25 12:31:24 -0800 | [diff] [blame] | 199 | |
| 200 | //logAttrib(ct, slot); |
| 201 | glEnableVertexAttribArray(slot); |
| 202 | glBindBuffer(GL_ARRAY_BUFFER, mAttribs[ct].buffer); |
| 203 | |
| 204 | glVertexAttribPointer(slot, |
| 205 | mAttribs[ct].size, |
| 206 | mAttribs[ct].type, |
| 207 | mAttribs[ct].normalized, |
| 208 | mAttribs[ct].stride, |
| 209 | (void *)mAttribs[ct].offset); |
Jason Sams | bb51c40 | 2009-11-25 13:22:07 -0800 | [diff] [blame] | 210 | } |
Jason Sams | 5dbfe93 | 2010-01-27 14:41:43 -0800 | [diff] [blame] | 211 | state->mLastEnableCount = mCount; |
| 212 | rsc->checkError("VertexArray::setupGL2 done"); |
Jason Sams | bb51c40 | 2009-11-25 13:22:07 -0800 | [diff] [blame] | 213 | } |
| 214 | //////////////////////////////////////////// |
| 215 | |
| 216 | void VertexArrayState::init(Context *) { |
Jason Sams | 5dbfe93 | 2010-01-27 14:41:43 -0800 | [diff] [blame] | 217 | mLastEnableCount = 0; |
Jason Sams | bb51c40 | 2009-11-25 13:22:07 -0800 | [diff] [blame] | 218 | } |
| 219 | |