blob: b42d1c4c5c36e9cff8ac63426aa0226a43a1fc88 [file] [log] [blame]
Jason Samsc460e552009-11-25 13:22:07 -08001/*
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 Sakhartchoukfb6b6142010-05-21 12:53:13 -070017#ifndef ANDROID_RS_BUILD_FOR_HOST
Jason Samsc460e552009-11-25 13:22:07 -080018#include "rsContext.h"
Jason Samsc460e552009-11-25 13:22:07 -080019#include <GLES/gl.h>
20#include <GLES2/gl2.h>
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -070021#else
22#include "rsContextHostStub.h"
23#include <OpenGL/gl.h>
24#endif
25
Jason Samsc460e552009-11-25 13:22:07 -080026
27using namespace android;
28using namespace android::renderscript;
29
30
31VertexArray::VertexArray()
32{
Jason Samsbe504f22010-01-25 12:31:24 -080033 clearAll();
Jason Samsc460e552009-11-25 13:22:07 -080034}
35
36VertexArray::~VertexArray()
37{
38}
39
40
41void VertexArray::clearAll()
42{
Jason Sams433eca32010-01-06 11:57:52 -080043 for (uint32_t ct=0; ct < RS_MAX_ATTRIBS; ct++) {
44 mAttribs[ct].clear();
45 }
Jason Samsc460e552009-11-25 13:22:07 -080046 mActiveBuffer = 0;
Jason Samsbe504f22010-01-25 12:31:24 -080047 mCount = 0;
Jason Sams433eca32010-01-06 11:57:52 -080048}
49
50VertexArray::Attrib::Attrib()
51{
52 clear();
53}
54
55void 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;
Jason Samsbe504f22010-01-25 12:31:24 -080063 kind = RS_KIND_USER;
Jason Sams433eca32010-01-06 11:57:52 -080064 name.setTo(a.name);
65}
66
67void VertexArray::Attrib::clear()
68{
69 buffer = 0;
70 offset = 0;
71 type = 0;
72 size = 0;
73 stride = 0;
74 normalized = false;
75 name.setTo("");
Jason Samsc460e552009-11-25 13:22:07 -080076}
77
Jason Samsbe504f22010-01-25 12:31:24 -080078void VertexArray::clear(uint32_t n)
Jason Samsc460e552009-11-25 13:22:07 -080079{
Jason Sams433eca32010-01-06 11:57:52 -080080 mAttribs[n].clear();
Jason Samsc460e552009-11-25 13:22:07 -080081}
82
Jason Samsbe504f22010-01-25 12:31:24 -080083void VertexArray::addUser(const Attrib &a, uint32_t stride)
Jason Samsc460e552009-11-25 13:22:07 -080084{
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -070085 rsAssert(mCount < RS_MAX_ATTRIBS);
Jason Samsbe504f22010-01-25 12:31:24 -080086 mAttribs[mCount].set(a);
87 mAttribs[mCount].buffer = mActiveBuffer;
88 mAttribs[mCount].stride = stride;
89 mAttribs[mCount].kind = RS_KIND_USER;
90 mCount ++;
Jason Samsc460e552009-11-25 13:22:07 -080091}
92
Jason Samsbe504f22010-01-25 12:31:24 -080093void VertexArray::addLegacy(uint32_t type, uint32_t size, uint32_t stride, RsDataKind kind, bool normalized, uint32_t offset)
Jason Samsc460e552009-11-25 13:22:07 -080094{
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -070095 rsAssert(mCount < RS_MAX_ATTRIBS);
Jason Samsbe504f22010-01-25 12:31:24 -080096 mAttribs[mCount].clear();
97 mAttribs[mCount].type = type;
98 mAttribs[mCount].size = size;
99 mAttribs[mCount].offset = offset;
100 mAttribs[mCount].normalized = normalized;
101 mAttribs[mCount].buffer = mActiveBuffer;
102 mAttribs[mCount].stride = stride;
103 mAttribs[mCount].kind = kind;
104 mCount ++;
Jason Sams433eca32010-01-06 11:57:52 -0800105}
106
107void VertexArray::logAttrib(uint32_t idx, uint32_t slot) const {
Jason Samsbe504f22010-01-25 12:31:24 -0800108 LOGE("va %i: slot=%i name=%s buf=%i size=%i type=0x%x kind=%i stride=0x%x norm=%i offset=0x%x", idx, slot,
Jason Sams433eca32010-01-06 11:57:52 -0800109 mAttribs[idx].name.string(),
Jason Samsc460e552009-11-25 13:22:07 -0800110 mAttribs[idx].buffer,
111 mAttribs[idx].size,
112 mAttribs[idx].type,
Jason Samsbe504f22010-01-25 12:31:24 -0800113 mAttribs[idx].kind,
Jason Samsc460e552009-11-25 13:22:07 -0800114 mAttribs[idx].stride,
Jason Samsd01d9702009-12-23 14:35:29 -0800115 mAttribs[idx].normalized,
Jason Samsc460e552009-11-25 13:22:07 -0800116 mAttribs[idx].offset);
117}
118
Jason Samsd01d9702009-12-23 14:35:29 -0800119void VertexArray::setupGL(const Context *rsc, class VertexArrayState *state) const
Jason Samsc460e552009-11-25 13:22:07 -0800120{
Jason Samsc460e552009-11-25 13:22:07 -0800121 glClientActiveTexture(GL_TEXTURE0);
Jason Samsbe504f22010-01-25 12:31:24 -0800122 glDisableClientState(GL_NORMAL_ARRAY);
123 glDisableClientState(GL_COLOR_ARRAY);
124 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700125#ifndef ANDROID_RS_BUILD_FOR_HOST // GLES only
Jason Samsbe504f22010-01-25 12:31:24 -0800126 glDisableClientState(GL_POINT_SIZE_ARRAY_OES);
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700127#endif //ANDROID_RS_BUILD_FOR_HOST
Jason Samsbe504f22010-01-25 12:31:24 -0800128
129 for (uint32_t ct=0; ct < mCount; ct++) {
130 switch(mAttribs[ct].kind) {
131 case RS_KIND_POSITION:
132 //logAttrib(POSITION);
133 glEnableClientState(GL_VERTEX_ARRAY);
134 glBindBuffer(GL_ARRAY_BUFFER, mAttribs[ct].buffer);
135 glVertexPointer(mAttribs[ct].size,
136 mAttribs[ct].type,
137 mAttribs[ct].stride,
138 (void *)mAttribs[ct].offset);
139 break;
140
141 case RS_KIND_NORMAL:
142 //logAttrib(NORMAL);
143 glEnableClientState(GL_NORMAL_ARRAY);
144 rsAssert(mAttribs[ct].size == 3);
145 glBindBuffer(GL_ARRAY_BUFFER, mAttribs[ct].buffer);
146 glNormalPointer(mAttribs[ct].type,
147 mAttribs[ct].stride,
148 (void *)mAttribs[ct].offset);
149 break;
150
151 case RS_KIND_COLOR:
152 //logAttrib(COLOR);
153 glEnableClientState(GL_COLOR_ARRAY);
154 glBindBuffer(GL_ARRAY_BUFFER, mAttribs[ct].buffer);
155 glColorPointer(mAttribs[ct].size,
156 mAttribs[ct].type,
157 mAttribs[ct].stride,
158 (void *)mAttribs[ct].offset);
159 break;
160
161 case RS_KIND_TEXTURE:
162 //logAttrib(TEXTURE);
163 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
164 glBindBuffer(GL_ARRAY_BUFFER, mAttribs[ct].buffer);
165 glTexCoordPointer(mAttribs[ct].size,
166 mAttribs[ct].type,
167 mAttribs[ct].stride,
168 (void *)mAttribs[ct].offset);
169 break;
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700170#ifndef ANDROID_RS_BUILD_FOR_HOST // GLES only
Jason Samsbe504f22010-01-25 12:31:24 -0800171 case RS_KIND_POINT_SIZE:
172 //logAttrib(POINT_SIZE);
173 glEnableClientState(GL_POINT_SIZE_ARRAY_OES);
174 glBindBuffer(GL_ARRAY_BUFFER, mAttribs[ct].buffer);
175 glPointSizePointerOES(mAttribs[ct].type,
176 mAttribs[ct].stride,
177 (void *)mAttribs[ct].offset);
178 break;
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700179#endif //ANDROID_RS_BUILD_FOR_HOST
Jason Samsbe504f22010-01-25 12:31:24 -0800180
181 default:
182 rsAssert(0);
183 }
Jason Sams433eca32010-01-06 11:57:52 -0800184 }
Jason Samsc460e552009-11-25 13:22:07 -0800185
Jason Samsd01d9702009-12-23 14:35:29 -0800186 rsc->checkError("VertexArray::setupGL");
Jason Samsc460e552009-11-25 13:22:07 -0800187}
188
Jason Samsd01d9702009-12-23 14:35:29 -0800189void VertexArray::setupGL2(const Context *rsc, class VertexArrayState *state, ShaderCache *sc) const
Jason Samsc460e552009-11-25 13:22:07 -0800190{
Jason Sams3eb28f02010-01-27 14:41:43 -0800191 rsc->checkError("VertexArray::setupGL2 start");
192 for (uint32_t ct=1; ct <= state->mLastEnableCount; ct++) {
Jason Samsc460e552009-11-25 13:22:07 -0800193 glDisableVertexAttribArray(ct);
194 }
195
Jason Sams3eb28f02010-01-27 14:41:43 -0800196 rsc->checkError("VertexArray::setupGL2 disabled");
Jason Samsbe504f22010-01-25 12:31:24 -0800197 for (uint32_t ct=0; ct < mCount; ct++) {
198 uint32_t slot = 0;
199 if (sc->isUserVertexProgram()) {
200 slot = sc->vtxAttribSlot(ct);
201 } else {
Jason Samsaef14582010-02-08 16:31:39 -0800202 if (mAttribs[ct].kind == RS_KIND_USER) {
203 continue;
204 }
Jason Samsbe504f22010-01-25 12:31:24 -0800205 slot = sc->vtxAttribSlot(mAttribs[ct].kind);
Jason Samsc460e552009-11-25 13:22:07 -0800206 }
Jason Samsbe504f22010-01-25 12:31:24 -0800207
208 //logAttrib(ct, slot);
209 glEnableVertexAttribArray(slot);
210 glBindBuffer(GL_ARRAY_BUFFER, mAttribs[ct].buffer);
211
212 glVertexAttribPointer(slot,
213 mAttribs[ct].size,
214 mAttribs[ct].type,
215 mAttribs[ct].normalized,
216 mAttribs[ct].stride,
217 (void *)mAttribs[ct].offset);
Jason Samsc460e552009-11-25 13:22:07 -0800218 }
Jason Sams3eb28f02010-01-27 14:41:43 -0800219 state->mLastEnableCount = mCount;
220 rsc->checkError("VertexArray::setupGL2 done");
Jason Samsc460e552009-11-25 13:22:07 -0800221}
222////////////////////////////////////////////
223
224void VertexArrayState::init(Context *) {
Jason Sams3eb28f02010-01-27 14:41:43 -0800225 mLastEnableCount = 0;
Jason Samsc460e552009-11-25 13:22:07 -0800226}
227