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 | |
Alex Sakhartchouk | aa7d288 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 17 | #ifndef ANDROID_RS_BUILD_FOR_HOST |
Jason Sams | bb51c40 | 2009-11-25 13:22:07 -0800 | [diff] [blame] | 18 | #include "rsContext.h" |
Jason Sams | bb51c40 | 2009-11-25 13:22:07 -0800 | [diff] [blame] | 19 | #include <GLES/gl.h> |
| 20 | #include <GLES2/gl2.h> |
Alex Sakhartchouk | aa7d288 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 21 | #else |
| 22 | #include "rsContextHostStub.h" |
| 23 | #include <OpenGL/gl.h> |
| 24 | #endif |
| 25 | |
Jason Sams | bb51c40 | 2009-11-25 13:22:07 -0800 | [diff] [blame] | 26 | |
| 27 | using namespace android; |
| 28 | using namespace android::renderscript; |
| 29 | |
| 30 | |
| 31 | VertexArray::VertexArray() |
| 32 | { |
Jason Sams | df48b57 | 2010-01-25 12:31:24 -0800 | [diff] [blame] | 33 | clearAll(); |
Jason Sams | bb51c40 | 2009-11-25 13:22:07 -0800 | [diff] [blame] | 34 | } |
| 35 | |
| 36 | VertexArray::~VertexArray() |
| 37 | { |
| 38 | } |
| 39 | |
| 40 | |
| 41 | void VertexArray::clearAll() |
| 42 | { |
Jason Sams | a09a6e1 | 2010-01-06 11:57:52 -0800 | [diff] [blame] | 43 | for (uint32_t ct=0; ct < RS_MAX_ATTRIBS; ct++) { |
| 44 | mAttribs[ct].clear(); |
| 45 | } |
Jason Sams | bb51c40 | 2009-11-25 13:22:07 -0800 | [diff] [blame] | 46 | mActiveBuffer = 0; |
Jason Sams | e519b37 | 2010-06-30 12:07:41 -0700 | [diff] [blame] | 47 | mActivePointer = NULL; |
Jason Sams | df48b57 | 2010-01-25 12:31:24 -0800 | [diff] [blame] | 48 | mCount = 0; |
Jason Sams | a09a6e1 | 2010-01-06 11:57:52 -0800 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | VertexArray::Attrib::Attrib() |
| 52 | { |
| 53 | clear(); |
| 54 | } |
| 55 | |
| 56 | void VertexArray::Attrib::set(const Attrib &a) |
| 57 | { |
| 58 | buffer = a.buffer; |
Jason Sams | e519b37 | 2010-06-30 12:07:41 -0700 | [diff] [blame] | 59 | ptr = a.ptr; |
Jason Sams | a09a6e1 | 2010-01-06 11:57:52 -0800 | [diff] [blame] | 60 | offset = a.offset; |
| 61 | type = a.type; |
| 62 | size = a.size; |
| 63 | stride = a.stride; |
| 64 | normalized = a.normalized; |
| 65 | name.setTo(a.name); |
| 66 | } |
| 67 | |
| 68 | void VertexArray::Attrib::clear() |
| 69 | { |
| 70 | buffer = 0; |
| 71 | offset = 0; |
| 72 | type = 0; |
| 73 | size = 0; |
| 74 | stride = 0; |
Jason Sams | e519b37 | 2010-06-30 12:07:41 -0700 | [diff] [blame] | 75 | ptr = NULL; |
Jason Sams | a09a6e1 | 2010-01-06 11:57:52 -0800 | [diff] [blame] | 76 | normalized = false; |
| 77 | name.setTo(""); |
Jason Sams | bb51c40 | 2009-11-25 13:22:07 -0800 | [diff] [blame] | 78 | } |
| 79 | |
Jason Sams | df48b57 | 2010-01-25 12:31:24 -0800 | [diff] [blame] | 80 | void VertexArray::clear(uint32_t n) |
Jason Sams | bb51c40 | 2009-11-25 13:22:07 -0800 | [diff] [blame] | 81 | { |
Jason Sams | a09a6e1 | 2010-01-06 11:57:52 -0800 | [diff] [blame] | 82 | mAttribs[n].clear(); |
Jason Sams | bb51c40 | 2009-11-25 13:22:07 -0800 | [diff] [blame] | 83 | } |
| 84 | |
Jason Sams | 8cb39de | 2010-06-01 15:47:01 -0700 | [diff] [blame] | 85 | void VertexArray::add(const Attrib &a, uint32_t stride) |
Jason Sams | bb51c40 | 2009-11-25 13:22:07 -0800 | [diff] [blame] | 86 | { |
Alex Sakhartchouk | 7215b51 | 2010-09-27 10:29:47 -0700 | [diff] [blame] | 87 | // Skip padding |
| 88 | if(a.name[0] == '#') { |
| 89 | return; |
| 90 | } |
Alex Sakhartchouk | aa7d288 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 91 | rsAssert(mCount < RS_MAX_ATTRIBS); |
Jason Sams | df48b57 | 2010-01-25 12:31:24 -0800 | [diff] [blame] | 92 | mAttribs[mCount].set(a); |
| 93 | mAttribs[mCount].buffer = mActiveBuffer; |
Jason Sams | eeeaccc | 2010-06-25 12:45:41 -0700 | [diff] [blame] | 94 | mAttribs[mCount].ptr = mActivePointer; |
Jason Sams | df48b57 | 2010-01-25 12:31:24 -0800 | [diff] [blame] | 95 | mAttribs[mCount].stride = stride; |
Jason Sams | df48b57 | 2010-01-25 12:31:24 -0800 | [diff] [blame] | 96 | mCount ++; |
Jason Sams | bb51c40 | 2009-11-25 13:22:07 -0800 | [diff] [blame] | 97 | } |
| 98 | |
Jason Sams | 8cb39de | 2010-06-01 15:47:01 -0700 | [diff] [blame] | 99 | void VertexArray::add(uint32_t type, uint32_t size, uint32_t stride, bool normalized, uint32_t offset, const char *name) |
Jason Sams | bb51c40 | 2009-11-25 13:22:07 -0800 | [diff] [blame] | 100 | { |
Alex Sakhartchouk | 7215b51 | 2010-09-27 10:29:47 -0700 | [diff] [blame] | 101 | // Skip padding |
| 102 | if(name[0] == '#') { |
| 103 | return; |
| 104 | } |
Alex Sakhartchouk | aa7d288 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 105 | rsAssert(mCount < RS_MAX_ATTRIBS); |
Jason Sams | df48b57 | 2010-01-25 12:31:24 -0800 | [diff] [blame] | 106 | mAttribs[mCount].clear(); |
| 107 | mAttribs[mCount].type = type; |
| 108 | mAttribs[mCount].size = size; |
| 109 | mAttribs[mCount].offset = offset; |
| 110 | mAttribs[mCount].normalized = normalized; |
Jason Sams | df48b57 | 2010-01-25 12:31:24 -0800 | [diff] [blame] | 111 | mAttribs[mCount].stride = stride; |
Jason Sams | 8cb39de | 2010-06-01 15:47:01 -0700 | [diff] [blame] | 112 | mAttribs[mCount].name.setTo(name); |
Jason Sams | eeeaccc | 2010-06-25 12:45:41 -0700 | [diff] [blame] | 113 | |
| 114 | mAttribs[mCount].buffer = mActiveBuffer; |
| 115 | mAttribs[mCount].ptr = mActivePointer; |
Jason Sams | df48b57 | 2010-01-25 12:31:24 -0800 | [diff] [blame] | 116 | mCount ++; |
Jason Sams | a09a6e1 | 2010-01-06 11:57:52 -0800 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | void VertexArray::logAttrib(uint32_t idx, uint32_t slot) const { |
Jason Sams | eeeaccc | 2010-06-25 12:45:41 -0700 | [diff] [blame] | 120 | LOGE("va %i: slot=%i name=%s buf=%i ptr=%p size=%i type=0x%x stride=0x%x norm=%i offset=0x%x", idx, slot, |
Jason Sams | a09a6e1 | 2010-01-06 11:57:52 -0800 | [diff] [blame] | 121 | mAttribs[idx].name.string(), |
Jason Sams | bb51c40 | 2009-11-25 13:22:07 -0800 | [diff] [blame] | 122 | mAttribs[idx].buffer, |
Jason Sams | eeeaccc | 2010-06-25 12:45:41 -0700 | [diff] [blame] | 123 | mAttribs[idx].ptr, |
Jason Sams | bb51c40 | 2009-11-25 13:22:07 -0800 | [diff] [blame] | 124 | mAttribs[idx].size, |
| 125 | mAttribs[idx].type, |
| 126 | mAttribs[idx].stride, |
Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 127 | mAttribs[idx].normalized, |
Jason Sams | bb51c40 | 2009-11-25 13:22:07 -0800 | [diff] [blame] | 128 | mAttribs[idx].offset); |
| 129 | } |
| 130 | |
Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 131 | void VertexArray::setupGL2(const Context *rsc, class VertexArrayState *state, ShaderCache *sc) const |
Jason Sams | bb51c40 | 2009-11-25 13:22:07 -0800 | [diff] [blame] | 132 | { |
Jason Sams | 5dbfe93 | 2010-01-27 14:41:43 -0800 | [diff] [blame] | 133 | rsc->checkError("VertexArray::setupGL2 start"); |
Jason Sams | 8cb39de | 2010-06-01 15:47:01 -0700 | [diff] [blame] | 134 | for (uint32_t ct=1; ct <= 0xf/*state->mLastEnableCount*/; ct++) { |
Jason Sams | bb51c40 | 2009-11-25 13:22:07 -0800 | [diff] [blame] | 135 | glDisableVertexAttribArray(ct); |
| 136 | } |
| 137 | |
Jason Sams | 5dbfe93 | 2010-01-27 14:41:43 -0800 | [diff] [blame] | 138 | rsc->checkError("VertexArray::setupGL2 disabled"); |
Jason Sams | df48b57 | 2010-01-25 12:31:24 -0800 | [diff] [blame] | 139 | for (uint32_t ct=0; ct < mCount; ct++) { |
Alex Sakhartchouk | 7215b51 | 2010-09-27 10:29:47 -0700 | [diff] [blame] | 140 | int32_t slot = -1; |
Jason Sams | df48b57 | 2010-01-25 12:31:24 -0800 | [diff] [blame] | 141 | if (sc->isUserVertexProgram()) { |
| 142 | slot = sc->vtxAttribSlot(ct); |
| 143 | } else { |
Jason Sams | 8cb39de | 2010-06-01 15:47:01 -0700 | [diff] [blame] | 144 | if (mAttribs[ct].name == "position") { |
| 145 | slot = 0; |
| 146 | } else if (mAttribs[ct].name == "color") { |
| 147 | slot = 1; |
| 148 | } else if (mAttribs[ct].name == "normal") { |
| 149 | slot = 2; |
Jason Sams | 8cb39de | 2010-06-01 15:47:01 -0700 | [diff] [blame] | 150 | } else if (mAttribs[ct].name == "texture0") { |
Jason Sams | 53a93d5 | 2010-07-09 15:34:32 -0700 | [diff] [blame] | 151 | slot = 3; |
Jason Sams | 5bec3aa | 2010-02-08 16:31:39 -0800 | [diff] [blame] | 152 | } |
Jason Sams | bb51c40 | 2009-11-25 13:22:07 -0800 | [diff] [blame] | 153 | } |
Alex Sakhartchouk | a41174e | 2010-08-27 16:10:55 -0700 | [diff] [blame] | 154 | if(slot < 0) { |
| 155 | continue; |
| 156 | } |
Jason Sams | ab67fb9 | 2010-06-02 12:40:19 -0700 | [diff] [blame] | 157 | //logAttrib(ct, slot); |
Jason Sams | df48b57 | 2010-01-25 12:31:24 -0800 | [diff] [blame] | 158 | glEnableVertexAttribArray(slot); |
| 159 | glBindBuffer(GL_ARRAY_BUFFER, mAttribs[ct].buffer); |
Jason Sams | df48b57 | 2010-01-25 12:31:24 -0800 | [diff] [blame] | 160 | glVertexAttribPointer(slot, |
| 161 | mAttribs[ct].size, |
| 162 | mAttribs[ct].type, |
| 163 | mAttribs[ct].normalized, |
| 164 | mAttribs[ct].stride, |
Jason Sams | eeeaccc | 2010-06-25 12:45:41 -0700 | [diff] [blame] | 165 | mAttribs[ct].ptr + mAttribs[ct].offset); |
Jason Sams | bb51c40 | 2009-11-25 13:22:07 -0800 | [diff] [blame] | 166 | } |
Jason Sams | 5dbfe93 | 2010-01-27 14:41:43 -0800 | [diff] [blame] | 167 | state->mLastEnableCount = mCount; |
| 168 | rsc->checkError("VertexArray::setupGL2 done"); |
Jason Sams | bb51c40 | 2009-11-25 13:22:07 -0800 | [diff] [blame] | 169 | } |
| 170 | //////////////////////////////////////////// |
| 171 | |
| 172 | void VertexArrayState::init(Context *) { |
Jason Sams | 5dbfe93 | 2010-01-27 14:41:43 -0800 | [diff] [blame] | 173 | mLastEnableCount = 0; |
Jason Sams | bb51c40 | 2009-11-25 13:22:07 -0800 | [diff] [blame] | 174 | } |
| 175 | |