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 | using namespace android; |
| 27 | using namespace android::renderscript; |
| 28 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 29 | VertexArray::VertexArray(const Attrib *attribs, uint32_t numAttribs) { |
Alex Sakhartchouk | 9d71e21 | 2010-11-08 15:10:52 -0800 | [diff] [blame] | 30 | mAttribs = attribs; |
| 31 | mCount = numAttribs; |
Jason Sams | bb51c40 | 2009-11-25 13:22:07 -0800 | [diff] [blame] | 32 | } |
| 33 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 34 | VertexArray::~VertexArray() { |
Jason Sams | bb51c40 | 2009-11-25 13:22:07 -0800 | [diff] [blame] | 35 | } |
| 36 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 37 | VertexArray::Attrib::Attrib() { |
Jason Sams | a09a6e1 | 2010-01-06 11:57:52 -0800 | [diff] [blame] | 38 | clear(); |
| 39 | } |
| 40 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 41 | void VertexArray::Attrib::clear() { |
Jason Sams | a09a6e1 | 2010-01-06 11:57:52 -0800 | [diff] [blame] | 42 | buffer = 0; |
| 43 | offset = 0; |
| 44 | type = 0; |
| 45 | size = 0; |
| 46 | stride = 0; |
Jason Sams | e519b37 | 2010-06-30 12:07:41 -0700 | [diff] [blame] | 47 | ptr = NULL; |
Jason Sams | a09a6e1 | 2010-01-06 11:57:52 -0800 | [diff] [blame] | 48 | normalized = false; |
| 49 | name.setTo(""); |
Jason Sams | bb51c40 | 2009-11-25 13:22:07 -0800 | [diff] [blame] | 50 | } |
| 51 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 52 | void VertexArray::Attrib::set(uint32_t type, uint32_t size, uint32_t stride, |
| 53 | bool normalized, uint32_t offset, |
| 54 | const char *name) { |
Alex Sakhartchouk | 9d71e21 | 2010-11-08 15:10:52 -0800 | [diff] [blame] | 55 | clear(); |
| 56 | this->type = type; |
| 57 | this->size = size; |
| 58 | this->offset = offset; |
| 59 | this->normalized = normalized; |
| 60 | this->stride = stride; |
| 61 | this->name.setTo(name); |
Jason Sams | a09a6e1 | 2010-01-06 11:57:52 -0800 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | void VertexArray::logAttrib(uint32_t idx, uint32_t slot) const { |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 65 | if (idx == 0) { |
Alex Sakhartchouk | 4378f11 | 2010-09-29 09:49:13 -0700 | [diff] [blame] | 66 | LOGV("Starting vertex attribute binding"); |
| 67 | } |
| 68 | LOGV("va %i: slot=%i name=%s buf=%i ptr=%p size=%i type=0x%x stride=0x%x norm=%i offset=0x%x", |
| 69 | idx, slot, |
Jason Sams | a09a6e1 | 2010-01-06 11:57:52 -0800 | [diff] [blame] | 70 | mAttribs[idx].name.string(), |
Jason Sams | bb51c40 | 2009-11-25 13:22:07 -0800 | [diff] [blame] | 71 | mAttribs[idx].buffer, |
Jason Sams | eeeaccc | 2010-06-25 12:45:41 -0700 | [diff] [blame] | 72 | mAttribs[idx].ptr, |
Jason Sams | bb51c40 | 2009-11-25 13:22:07 -0800 | [diff] [blame] | 73 | mAttribs[idx].size, |
| 74 | mAttribs[idx].type, |
| 75 | mAttribs[idx].stride, |
Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 76 | mAttribs[idx].normalized, |
Jason Sams | bb51c40 | 2009-11-25 13:22:07 -0800 | [diff] [blame] | 77 | mAttribs[idx].offset); |
| 78 | } |
| 79 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 80 | void VertexArray::setupGL2(const Context *rsc, |
| 81 | class VertexArrayState *state, |
| 82 | ShaderCache *sc) const { |
Jason Sams | 5dbfe93 | 2010-01-27 14:41:43 -0800 | [diff] [blame] | 83 | rsc->checkError("VertexArray::setupGL2 start"); |
Jason Sams | 5476b45 | 2010-12-08 16:14:36 -0800 | [diff] [blame] | 84 | for (uint32_t ct=1; ct <= state->mLastEnableCount; ct++) { |
Jason Sams | bb51c40 | 2009-11-25 13:22:07 -0800 | [diff] [blame] | 85 | glDisableVertexAttribArray(ct); |
| 86 | } |
| 87 | |
Jason Sams | 5dbfe93 | 2010-01-27 14:41:43 -0800 | [diff] [blame] | 88 | rsc->checkError("VertexArray::setupGL2 disabled"); |
Jason Sams | df48b57 | 2010-01-25 12:31:24 -0800 | [diff] [blame] | 89 | for (uint32_t ct=0; ct < mCount; ct++) { |
Alex Sakhartchouk | 4378f11 | 2010-09-29 09:49:13 -0700 | [diff] [blame] | 90 | int32_t slot = sc->vtxAttribSlot(mAttribs[ct].name); |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 91 | if (rsc->props.mLogShadersAttr) { |
Alex Sakhartchouk | 4378f11 | 2010-09-29 09:49:13 -0700 | [diff] [blame] | 92 | logAttrib(ct, slot); |
Jason Sams | bb51c40 | 2009-11-25 13:22:07 -0800 | [diff] [blame] | 93 | } |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 94 | if (slot < 0) { |
Alex Sakhartchouk | a41174e | 2010-08-27 16:10:55 -0700 | [diff] [blame] | 95 | continue; |
| 96 | } |
Jason Sams | df48b57 | 2010-01-25 12:31:24 -0800 | [diff] [blame] | 97 | glEnableVertexAttribArray(slot); |
| 98 | glBindBuffer(GL_ARRAY_BUFFER, mAttribs[ct].buffer); |
Jason Sams | df48b57 | 2010-01-25 12:31:24 -0800 | [diff] [blame] | 99 | glVertexAttribPointer(slot, |
| 100 | mAttribs[ct].size, |
| 101 | mAttribs[ct].type, |
| 102 | mAttribs[ct].normalized, |
| 103 | mAttribs[ct].stride, |
Jason Sams | eeeaccc | 2010-06-25 12:45:41 -0700 | [diff] [blame] | 104 | mAttribs[ct].ptr + mAttribs[ct].offset); |
Jason Sams | bb51c40 | 2009-11-25 13:22:07 -0800 | [diff] [blame] | 105 | } |
Jason Sams | 5dbfe93 | 2010-01-27 14:41:43 -0800 | [diff] [blame] | 106 | state->mLastEnableCount = mCount; |
| 107 | rsc->checkError("VertexArray::setupGL2 done"); |
Jason Sams | bb51c40 | 2009-11-25 13:22:07 -0800 | [diff] [blame] | 108 | } |
| 109 | //////////////////////////////////////////// |
| 110 | |
| 111 | void VertexArrayState::init(Context *) { |
Jason Sams | 5dbfe93 | 2010-01-27 14:41:43 -0800 | [diff] [blame] | 112 | mLastEnableCount = 0; |
Jason Sams | bb51c40 | 2009-11-25 13:22:07 -0800 | [diff] [blame] | 113 | } |
| 114 | |