blob: 6c2002d79e10e0b033564c3b2ec675f3ca8f71fb [file] [log] [blame]
Jason Samsbb51c402009-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
17#include "rsContext.h"
18
19#include <GLES/gl.h>
20#include <GLES2/gl2.h>
21
22using namespace android;
23using namespace android::renderscript;
24
25
26VertexArray::VertexArray()
27{
Jason Samsdf48b572010-01-25 12:31:24 -080028 clearAll();
Jason Samsbb51c402009-11-25 13:22:07 -080029}
30
31VertexArray::~VertexArray()
32{
33}
34
35
36void VertexArray::clearAll()
37{
Jason Samsa09a6e12010-01-06 11:57:52 -080038 for (uint32_t ct=0; ct < RS_MAX_ATTRIBS; ct++) {
39 mAttribs[ct].clear();
40 }
Jason Samsbb51c402009-11-25 13:22:07 -080041 mActiveBuffer = 0;
Jason Samsdf48b572010-01-25 12:31:24 -080042 mCount = 0;
Jason Samsa09a6e12010-01-06 11:57:52 -080043}
44
45VertexArray::Attrib::Attrib()
46{
47 clear();
48}
49
50void VertexArray::Attrib::set(const Attrib &a)
51{
52 buffer = a.buffer;
53 offset = a.offset;
54 type = a.type;
55 size = a.size;
56 stride = a.stride;
57 normalized = a.normalized;
Jason Samsdf48b572010-01-25 12:31:24 -080058 kind = RS_KIND_USER;
Jason Samsa09a6e12010-01-06 11:57:52 -080059 name.setTo(a.name);
60}
61
62void VertexArray::Attrib::clear()
63{
64 buffer = 0;
65 offset = 0;
66 type = 0;
67 size = 0;
68 stride = 0;
69 normalized = false;
70 name.setTo("");
Jason Samsbb51c402009-11-25 13:22:07 -080071}
72
Jason Samsdf48b572010-01-25 12:31:24 -080073void VertexArray::clear(uint32_t n)
Jason Samsbb51c402009-11-25 13:22:07 -080074{
Jason Samsa09a6e12010-01-06 11:57:52 -080075 mAttribs[n].clear();
Jason Samsbb51c402009-11-25 13:22:07 -080076}
77
Jason Samsdf48b572010-01-25 12:31:24 -080078void VertexArray::addUser(const Attrib &a, uint32_t stride)
Jason Samsbb51c402009-11-25 13:22:07 -080079{
Jason Samsdf48b572010-01-25 12:31:24 -080080 assert(mCount < RS_MAX_ATTRIBS);
81 mAttribs[mCount].set(a);
82 mAttribs[mCount].buffer = mActiveBuffer;
83 mAttribs[mCount].stride = stride;
84 mAttribs[mCount].kind = RS_KIND_USER;
85 mCount ++;
Jason Samsbb51c402009-11-25 13:22:07 -080086}
87
Jason Samsdf48b572010-01-25 12:31:24 -080088void VertexArray::addLegacy(uint32_t type, uint32_t size, uint32_t stride, RsDataKind kind, bool normalized, uint32_t offset)
Jason Samsbb51c402009-11-25 13:22:07 -080089{
Jason Samsdf48b572010-01-25 12:31:24 -080090 assert(mCount < RS_MAX_ATTRIBS);
91 mAttribs[mCount].clear();
92 mAttribs[mCount].type = type;
93 mAttribs[mCount].size = size;
94 mAttribs[mCount].offset = offset;
95 mAttribs[mCount].normalized = normalized;
96 mAttribs[mCount].buffer = mActiveBuffer;
97 mAttribs[mCount].stride = stride;
98 mAttribs[mCount].kind = kind;
99 mCount ++;
Jason Samsa09a6e12010-01-06 11:57:52 -0800100}
101
102void VertexArray::logAttrib(uint32_t idx, uint32_t slot) const {
Jason Samsdf48b572010-01-25 12:31:24 -0800103 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 Samsa09a6e12010-01-06 11:57:52 -0800104 mAttribs[idx].name.string(),
Jason Samsbb51c402009-11-25 13:22:07 -0800105 mAttribs[idx].buffer,
106 mAttribs[idx].size,
107 mAttribs[idx].type,
Jason Samsdf48b572010-01-25 12:31:24 -0800108 mAttribs[idx].kind,
Jason Samsbb51c402009-11-25 13:22:07 -0800109 mAttribs[idx].stride,
Jason Sams718cd1f2009-12-23 14:35:29 -0800110 mAttribs[idx].normalized,
Jason Samsbb51c402009-11-25 13:22:07 -0800111 mAttribs[idx].offset);
112}
113
Jason Sams718cd1f2009-12-23 14:35:29 -0800114void VertexArray::setupGL(const Context *rsc, class VertexArrayState *state) const
Jason Samsbb51c402009-11-25 13:22:07 -0800115{
Jason Samsbb51c402009-11-25 13:22:07 -0800116 glClientActiveTexture(GL_TEXTURE0);
Jason Samsdf48b572010-01-25 12:31:24 -0800117 glDisableClientState(GL_NORMAL_ARRAY);
118 glDisableClientState(GL_COLOR_ARRAY);
119 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
120 glDisableClientState(GL_POINT_SIZE_ARRAY_OES);
121
122 for (uint32_t ct=0; ct < mCount; ct++) {
123 switch(mAttribs[ct].kind) {
124 case RS_KIND_POSITION:
125 //logAttrib(POSITION);
126 glEnableClientState(GL_VERTEX_ARRAY);
127 glBindBuffer(GL_ARRAY_BUFFER, mAttribs[ct].buffer);
128 glVertexPointer(mAttribs[ct].size,
129 mAttribs[ct].type,
130 mAttribs[ct].stride,
131 (void *)mAttribs[ct].offset);
132 break;
133
134 case RS_KIND_NORMAL:
135 //logAttrib(NORMAL);
136 glEnableClientState(GL_NORMAL_ARRAY);
137 rsAssert(mAttribs[ct].size == 3);
138 glBindBuffer(GL_ARRAY_BUFFER, mAttribs[ct].buffer);
139 glNormalPointer(mAttribs[ct].type,
140 mAttribs[ct].stride,
141 (void *)mAttribs[ct].offset);
142 break;
143
144 case RS_KIND_COLOR:
145 //logAttrib(COLOR);
146 glEnableClientState(GL_COLOR_ARRAY);
147 glBindBuffer(GL_ARRAY_BUFFER, mAttribs[ct].buffer);
148 glColorPointer(mAttribs[ct].size,
149 mAttribs[ct].type,
150 mAttribs[ct].stride,
151 (void *)mAttribs[ct].offset);
152 break;
153
154 case RS_KIND_TEXTURE:
155 //logAttrib(TEXTURE);
156 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
157 glBindBuffer(GL_ARRAY_BUFFER, mAttribs[ct].buffer);
158 glTexCoordPointer(mAttribs[ct].size,
159 mAttribs[ct].type,
160 mAttribs[ct].stride,
161 (void *)mAttribs[ct].offset);
162 break;
163
164 case RS_KIND_POINT_SIZE:
165 //logAttrib(POINT_SIZE);
166 glEnableClientState(GL_POINT_SIZE_ARRAY_OES);
167 glBindBuffer(GL_ARRAY_BUFFER, mAttribs[ct].buffer);
168 glPointSizePointerOES(mAttribs[ct].type,
169 mAttribs[ct].stride,
170 (void *)mAttribs[ct].offset);
171 break;
172
173 default:
174 rsAssert(0);
175 }
Jason Samsa09a6e12010-01-06 11:57:52 -0800176 }
Jason Samsbb51c402009-11-25 13:22:07 -0800177
Jason Sams718cd1f2009-12-23 14:35:29 -0800178 rsc->checkError("VertexArray::setupGL");
Jason Samsbb51c402009-11-25 13:22:07 -0800179}
180
Jason Sams718cd1f2009-12-23 14:35:29 -0800181void VertexArray::setupGL2(const Context *rsc, class VertexArrayState *state, ShaderCache *sc) const
Jason Samsbb51c402009-11-25 13:22:07 -0800182{
Jason Sams5dbfe932010-01-27 14:41:43 -0800183 rsc->checkError("VertexArray::setupGL2 start");
184 for (uint32_t ct=1; ct <= state->mLastEnableCount; ct++) {
Jason Samsbb51c402009-11-25 13:22:07 -0800185 glDisableVertexAttribArray(ct);
186 }
187
Jason Sams5dbfe932010-01-27 14:41:43 -0800188 rsc->checkError("VertexArray::setupGL2 disabled");
Jason Samsdf48b572010-01-25 12:31:24 -0800189 for (uint32_t ct=0; ct < mCount; ct++) {
190 uint32_t slot = 0;
191 if (sc->isUserVertexProgram()) {
192 slot = sc->vtxAttribSlot(ct);
193 } else {
Jason Sams5bec3aa2010-02-08 16:31:39 -0800194 if (mAttribs[ct].kind == RS_KIND_USER) {
195 continue;
196 }
Jason Samsdf48b572010-01-25 12:31:24 -0800197 slot = sc->vtxAttribSlot(mAttribs[ct].kind);
Jason Samsbb51c402009-11-25 13:22:07 -0800198 }
Jason Samsdf48b572010-01-25 12:31:24 -0800199
200 //logAttrib(ct, slot);
201 glEnableVertexAttribArray(slot);
202 glBindBuffer(GL_ARRAY_BUFFER, mAttribs[ct].buffer);
203
204 glVertexAttribPointer(slot,
205 mAttribs[ct].size,
206 mAttribs[ct].type,
207 mAttribs[ct].normalized,
208 mAttribs[ct].stride,
209 (void *)mAttribs[ct].offset);
Jason Samsbb51c402009-11-25 13:22:07 -0800210 }
Jason Sams5dbfe932010-01-27 14:41:43 -0800211 state->mLastEnableCount = mCount;
212 rsc->checkError("VertexArray::setupGL2 done");
Jason Samsbb51c402009-11-25 13:22:07 -0800213}
214////////////////////////////////////////////
215
216void VertexArrayState::init(Context *) {
Jason Sams5dbfe932010-01-27 14:41:43 -0800217 mLastEnableCount = 0;
Jason Samsbb51c402009-11-25 13:22:07 -0800218}
219