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