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