Jason Sams | c460e55 | 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 | fb6b614 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 17 | #ifndef ANDROID_RS_BUILD_FOR_HOST |
Jason Sams | c460e55 | 2009-11-25 13:22:07 -0800 | [diff] [blame] | 18 | #include "rsContext.h" |
Jason Sams | c460e55 | 2009-11-25 13:22:07 -0800 | [diff] [blame] | 19 | #include <GLES/gl.h> |
| 20 | #include <GLES2/gl2.h> |
Alex Sakhartchouk | fb6b614 | 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 | c460e55 | 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 | be504f2 | 2010-01-25 12:31:24 -0800 | [diff] [blame] | 33 | clearAll(); |
Jason Sams | c460e55 | 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 | 433eca3 | 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 | c460e55 | 2009-11-25 13:22:07 -0800 | [diff] [blame] | 46 | mActiveBuffer = 0; |
Jason Sams | be504f2 | 2010-01-25 12:31:24 -0800 | [diff] [blame] | 47 | mCount = 0; |
Jason Sams | 433eca3 | 2010-01-06 11:57:52 -0800 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | VertexArray::Attrib::Attrib() |
| 51 | { |
| 52 | clear(); |
| 53 | } |
| 54 | |
| 55 | void VertexArray::Attrib::set(const Attrib &a) |
| 56 | { |
| 57 | buffer = a.buffer; |
| 58 | offset = a.offset; |
| 59 | type = a.type; |
| 60 | size = a.size; |
| 61 | stride = a.stride; |
| 62 | normalized = a.normalized; |
| 63 | name.setTo(a.name); |
| 64 | } |
| 65 | |
| 66 | void VertexArray::Attrib::clear() |
| 67 | { |
| 68 | buffer = 0; |
| 69 | offset = 0; |
| 70 | type = 0; |
| 71 | size = 0; |
| 72 | stride = 0; |
| 73 | normalized = false; |
| 74 | name.setTo(""); |
Jason Sams | c460e55 | 2009-11-25 13:22:07 -0800 | [diff] [blame] | 75 | } |
| 76 | |
Jason Sams | be504f2 | 2010-01-25 12:31:24 -0800 | [diff] [blame] | 77 | void VertexArray::clear(uint32_t n) |
Jason Sams | c460e55 | 2009-11-25 13:22:07 -0800 | [diff] [blame] | 78 | { |
Jason Sams | 433eca3 | 2010-01-06 11:57:52 -0800 | [diff] [blame] | 79 | mAttribs[n].clear(); |
Jason Sams | c460e55 | 2009-11-25 13:22:07 -0800 | [diff] [blame] | 80 | } |
| 81 | |
Jason Sams | 79f52df | 2010-06-01 15:47:01 -0700 | [diff] [blame] | 82 | void VertexArray::add(const Attrib &a, uint32_t stride) |
Jason Sams | c460e55 | 2009-11-25 13:22:07 -0800 | [diff] [blame] | 83 | { |
Alex Sakhartchouk | fb6b614 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 84 | rsAssert(mCount < RS_MAX_ATTRIBS); |
Jason Sams | be504f2 | 2010-01-25 12:31:24 -0800 | [diff] [blame] | 85 | mAttribs[mCount].set(a); |
| 86 | mAttribs[mCount].buffer = mActiveBuffer; |
Jason Sams | 760f1f7 | 2010-06-25 12:45:41 -0700 | [diff] [blame^] | 87 | mAttribs[mCount].ptr = mActivePointer; |
Jason Sams | be504f2 | 2010-01-25 12:31:24 -0800 | [diff] [blame] | 88 | mAttribs[mCount].stride = stride; |
Jason Sams | be504f2 | 2010-01-25 12:31:24 -0800 | [diff] [blame] | 89 | mCount ++; |
Jason Sams | c460e55 | 2009-11-25 13:22:07 -0800 | [diff] [blame] | 90 | } |
| 91 | |
Jason Sams | 79f52df | 2010-06-01 15:47:01 -0700 | [diff] [blame] | 92 | void VertexArray::add(uint32_t type, uint32_t size, uint32_t stride, bool normalized, uint32_t offset, const char *name) |
Jason Sams | c460e55 | 2009-11-25 13:22:07 -0800 | [diff] [blame] | 93 | { |
Alex Sakhartchouk | fb6b614 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 94 | rsAssert(mCount < RS_MAX_ATTRIBS); |
Jason Sams | be504f2 | 2010-01-25 12:31:24 -0800 | [diff] [blame] | 95 | mAttribs[mCount].clear(); |
| 96 | mAttribs[mCount].type = type; |
| 97 | mAttribs[mCount].size = size; |
| 98 | mAttribs[mCount].offset = offset; |
| 99 | mAttribs[mCount].normalized = normalized; |
Jason Sams | be504f2 | 2010-01-25 12:31:24 -0800 | [diff] [blame] | 100 | mAttribs[mCount].stride = stride; |
Jason Sams | 79f52df | 2010-06-01 15:47:01 -0700 | [diff] [blame] | 101 | mAttribs[mCount].name.setTo(name); |
Jason Sams | 760f1f7 | 2010-06-25 12:45:41 -0700 | [diff] [blame^] | 102 | |
| 103 | mAttribs[mCount].buffer = mActiveBuffer; |
| 104 | mAttribs[mCount].ptr = mActivePointer; |
Jason Sams | be504f2 | 2010-01-25 12:31:24 -0800 | [diff] [blame] | 105 | mCount ++; |
Jason Sams | 433eca3 | 2010-01-06 11:57:52 -0800 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | void VertexArray::logAttrib(uint32_t idx, uint32_t slot) const { |
Jason Sams | 760f1f7 | 2010-06-25 12:45:41 -0700 | [diff] [blame^] | 109 | 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 | 433eca3 | 2010-01-06 11:57:52 -0800 | [diff] [blame] | 110 | mAttribs[idx].name.string(), |
Jason Sams | c460e55 | 2009-11-25 13:22:07 -0800 | [diff] [blame] | 111 | mAttribs[idx].buffer, |
Jason Sams | 760f1f7 | 2010-06-25 12:45:41 -0700 | [diff] [blame^] | 112 | mAttribs[idx].ptr, |
Jason Sams | c460e55 | 2009-11-25 13:22:07 -0800 | [diff] [blame] | 113 | mAttribs[idx].size, |
| 114 | mAttribs[idx].type, |
| 115 | mAttribs[idx].stride, |
Jason Sams | d01d970 | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 116 | mAttribs[idx].normalized, |
Jason Sams | c460e55 | 2009-11-25 13:22:07 -0800 | [diff] [blame] | 117 | mAttribs[idx].offset); |
| 118 | } |
| 119 | |
Jason Sams | d01d970 | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 120 | void VertexArray::setupGL2(const Context *rsc, class VertexArrayState *state, ShaderCache *sc) const |
Jason Sams | c460e55 | 2009-11-25 13:22:07 -0800 | [diff] [blame] | 121 | { |
Jason Sams | 3eb28f0 | 2010-01-27 14:41:43 -0800 | [diff] [blame] | 122 | rsc->checkError("VertexArray::setupGL2 start"); |
Jason Sams | 79f52df | 2010-06-01 15:47:01 -0700 | [diff] [blame] | 123 | for (uint32_t ct=1; ct <= 0xf/*state->mLastEnableCount*/; ct++) { |
Jason Sams | c460e55 | 2009-11-25 13:22:07 -0800 | [diff] [blame] | 124 | glDisableVertexAttribArray(ct); |
| 125 | } |
| 126 | |
Jason Sams | 3eb28f0 | 2010-01-27 14:41:43 -0800 | [diff] [blame] | 127 | rsc->checkError("VertexArray::setupGL2 disabled"); |
Jason Sams | be504f2 | 2010-01-25 12:31:24 -0800 | [diff] [blame] | 128 | for (uint32_t ct=0; ct < mCount; ct++) { |
| 129 | uint32_t slot = 0; |
Jason Sams | bdb0460 | 2010-06-17 18:05:38 -0700 | [diff] [blame] | 130 | |
| 131 | if (mAttribs[ct].name[0] == '#') { |
| 132 | continue; |
| 133 | } |
| 134 | |
Jason Sams | be504f2 | 2010-01-25 12:31:24 -0800 | [diff] [blame] | 135 | if (sc->isUserVertexProgram()) { |
| 136 | slot = sc->vtxAttribSlot(ct); |
| 137 | } else { |
Jason Sams | 79f52df | 2010-06-01 15:47:01 -0700 | [diff] [blame] | 138 | if (mAttribs[ct].name == "position") { |
| 139 | slot = 0; |
| 140 | } else if (mAttribs[ct].name == "color") { |
| 141 | slot = 1; |
| 142 | } else if (mAttribs[ct].name == "normal") { |
| 143 | slot = 2; |
| 144 | } else if (mAttribs[ct].name == "pointSize") { |
| 145 | slot = 3; |
| 146 | } else if (mAttribs[ct].name == "texture0") { |
| 147 | slot = 4; |
| 148 | } else { |
Jason Sams | aef1458 | 2010-02-08 16:31:39 -0800 | [diff] [blame] | 149 | continue; |
| 150 | } |
Jason Sams | c460e55 | 2009-11-25 13:22:07 -0800 | [diff] [blame] | 151 | } |
Jason Sams | be504f2 | 2010-01-25 12:31:24 -0800 | [diff] [blame] | 152 | |
Jason Sams | 976e283 | 2010-06-02 12:40:19 -0700 | [diff] [blame] | 153 | //logAttrib(ct, slot); |
Jason Sams | be504f2 | 2010-01-25 12:31:24 -0800 | [diff] [blame] | 154 | glEnableVertexAttribArray(slot); |
| 155 | glBindBuffer(GL_ARRAY_BUFFER, mAttribs[ct].buffer); |
Jason Sams | be504f2 | 2010-01-25 12:31:24 -0800 | [diff] [blame] | 156 | glVertexAttribPointer(slot, |
| 157 | mAttribs[ct].size, |
| 158 | mAttribs[ct].type, |
| 159 | mAttribs[ct].normalized, |
| 160 | mAttribs[ct].stride, |
Jason Sams | 760f1f7 | 2010-06-25 12:45:41 -0700 | [diff] [blame^] | 161 | mAttribs[ct].ptr + mAttribs[ct].offset); |
Jason Sams | c460e55 | 2009-11-25 13:22:07 -0800 | [diff] [blame] | 162 | } |
Jason Sams | 3eb28f0 | 2010-01-27 14:41:43 -0800 | [diff] [blame] | 163 | state->mLastEnableCount = mCount; |
| 164 | rsc->checkError("VertexArray::setupGL2 done"); |
Jason Sams | c460e55 | 2009-11-25 13:22:07 -0800 | [diff] [blame] | 165 | } |
| 166 | //////////////////////////////////////////// |
| 167 | |
| 168 | void VertexArrayState::init(Context *) { |
Jason Sams | 3eb28f0 | 2010-01-27 14:41:43 -0800 | [diff] [blame] | 169 | mLastEnableCount = 0; |
Jason Sams | c460e55 | 2009-11-25 13:22:07 -0800 | [diff] [blame] | 170 | } |
| 171 | |