blob: a1fd744250a4f322ca22aa784a6801f8c7bdee87 [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
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 Samsbe504f22010-01-25 12:31:24 -080028 clearAll();
Jason Samsc460e552009-11-25 13:22:07 -080029}
30
31VertexArray::~VertexArray()
32{
33}
34
35
36void VertexArray::clearAll()
37{
Jason Sams433eca32010-01-06 11:57:52 -080038 for (uint32_t ct=0; ct < RS_MAX_ATTRIBS; ct++) {
39 mAttribs[ct].clear();
40 }
Jason Samsc460e552009-11-25 13:22:07 -080041 mActiveBuffer = 0;
Jason Samsbe504f22010-01-25 12:31:24 -080042 mCount = 0;
Jason Sams433eca32010-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 Samsbe504f22010-01-25 12:31:24 -080058 kind = RS_KIND_USER;
Jason Sams433eca32010-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 Samsc460e552009-11-25 13:22:07 -080071}
72
Jason Samsbe504f22010-01-25 12:31:24 -080073void VertexArray::clear(uint32_t n)
Jason Samsc460e552009-11-25 13:22:07 -080074{
Jason Sams433eca32010-01-06 11:57:52 -080075 mAttribs[n].clear();
Jason Samsc460e552009-11-25 13:22:07 -080076}
77
Jason Samsbe504f22010-01-25 12:31:24 -080078void VertexArray::addUser(const Attrib &a, uint32_t stride)
Jason Samsc460e552009-11-25 13:22:07 -080079{
Jason Samsbe504f22010-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 Samsc460e552009-11-25 13:22:07 -080086}
87
Jason Samsbe504f22010-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 Samsc460e552009-11-25 13:22:07 -080089{
Jason Samsbe504f22010-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 Sams433eca32010-01-06 11:57:52 -0800100}
101
102void VertexArray::logAttrib(uint32_t idx, uint32_t slot) const {
Jason Samsbe504f22010-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 Sams433eca32010-01-06 11:57:52 -0800104 mAttribs[idx].name.string(),
Jason Samsc460e552009-11-25 13:22:07 -0800105 mAttribs[idx].buffer,
106 mAttribs[idx].size,
107 mAttribs[idx].type,
Jason Samsbe504f22010-01-25 12:31:24 -0800108 mAttribs[idx].kind,
Jason Samsc460e552009-11-25 13:22:07 -0800109 mAttribs[idx].stride,
Jason Samsd01d9702009-12-23 14:35:29 -0800110 mAttribs[idx].normalized,
Jason Samsc460e552009-11-25 13:22:07 -0800111 mAttribs[idx].offset);
112}
113
Jason Samsd01d9702009-12-23 14:35:29 -0800114void VertexArray::setupGL(const Context *rsc, class VertexArrayState *state) const
Jason Samsc460e552009-11-25 13:22:07 -0800115{
Jason Samsc460e552009-11-25 13:22:07 -0800116 glClientActiveTexture(GL_TEXTURE0);
Jason Samsbe504f22010-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 Sams433eca32010-01-06 11:57:52 -0800176 }
Jason Samsc460e552009-11-25 13:22:07 -0800177
Jason Samsd01d9702009-12-23 14:35:29 -0800178 rsc->checkError("VertexArray::setupGL");
Jason Samsc460e552009-11-25 13:22:07 -0800179}
180
Jason Samsd01d9702009-12-23 14:35:29 -0800181void VertexArray::setupGL2(const Context *rsc, class VertexArrayState *state, ShaderCache *sc) const
Jason Samsc460e552009-11-25 13:22:07 -0800182{
Jason Samsbe504f22010-01-25 12:31:24 -0800183 for (int ct=1; ct < RS_MAX_ATTRIBS; ct++) {
Jason Samsc460e552009-11-25 13:22:07 -0800184 glDisableVertexAttribArray(ct);
185 }
186
Jason Samsbe504f22010-01-25 12:31:24 -0800187 for (uint32_t ct=0; ct < mCount; ct++) {
188 uint32_t slot = 0;
189 if (sc->isUserVertexProgram()) {
190 slot = sc->vtxAttribSlot(ct);
191 } else {
192 slot = sc->vtxAttribSlot(mAttribs[ct].kind);
Jason Samsc460e552009-11-25 13:22:07 -0800193 }
Jason Samsbe504f22010-01-25 12:31:24 -0800194
195 //logAttrib(ct, slot);
196 glEnableVertexAttribArray(slot);
197 glBindBuffer(GL_ARRAY_BUFFER, mAttribs[ct].buffer);
198
199 glVertexAttribPointer(slot,
200 mAttribs[ct].size,
201 mAttribs[ct].type,
202 mAttribs[ct].normalized,
203 mAttribs[ct].stride,
204 (void *)mAttribs[ct].offset);
Jason Samsc460e552009-11-25 13:22:07 -0800205 }
Jason Samsd01d9702009-12-23 14:35:29 -0800206 rsc->checkError("VertexArray::setupGL2");
Jason Samsc460e552009-11-25 13:22:07 -0800207}
208////////////////////////////////////////////
209
210void VertexArrayState::init(Context *) {
Jason Samsc460e552009-11-25 13:22:07 -0800211}
212