blob: 7a8e054a582c11a762c0851fa524a649eccd3925 [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 Samsbb51c402009-11-25 13:22:07 -080028 mActiveBuffer = 0;
Jason Samsa09a6e12010-01-06 11:57:52 -080029 mUserCount = 0;
Jason Samsbb51c402009-11-25 13:22:07 -080030}
31
32VertexArray::~VertexArray()
33{
34}
35
36
37void VertexArray::clearAll()
38{
Jason Samsa09a6e12010-01-06 11:57:52 -080039 for (uint32_t ct=0; ct < RS_MAX_ATTRIBS; ct++) {
40 mAttribs[ct].clear();
41 }
Jason Samsbb51c402009-11-25 13:22:07 -080042 mActiveBuffer = 0;
Jason Samsa09a6e12010-01-06 11:57:52 -080043 mUserCount = 0;
44}
45
46VertexArray::Attrib::Attrib()
47{
48 clear();
49}
50
51void VertexArray::Attrib::set(const Attrib &a)
52{
53 buffer = a.buffer;
54 offset = a.offset;
55 type = a.type;
56 size = a.size;
57 stride = a.stride;
58 normalized = a.normalized;
59 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
73void VertexArray::clear(AttribName n)
74{
Jason Samsa09a6e12010-01-06 11:57:52 -080075 mAttribs[n].clear();
Jason Samsbb51c402009-11-25 13:22:07 -080076}
77
78void VertexArray::setPosition(uint32_t size, uint32_t type, uint32_t stride, uint32_t offset)
79{
80 mAttribs[POSITION].buffer = mActiveBuffer;
81 mAttribs[POSITION].type = type;
82 mAttribs[POSITION].size = size;
83 mAttribs[POSITION].offset = offset;
84 mAttribs[POSITION].stride = stride;
85 mAttribs[POSITION].normalized = false;
86}
87
88void VertexArray::setColor(uint32_t size, uint32_t type, uint32_t stride, uint32_t offset)
89{
90 mAttribs[COLOR].buffer = mActiveBuffer;
91 mAttribs[COLOR].type = type;
92 mAttribs[COLOR].size = size;
93 mAttribs[COLOR].offset = offset;
94 mAttribs[COLOR].stride = stride;
95 mAttribs[COLOR].normalized = type != GL_FLOAT;
96}
97
98void VertexArray::setNormal(uint32_t type, uint32_t stride, uint32_t offset)
99{
100 mAttribs[NORMAL].buffer = mActiveBuffer;
101 mAttribs[NORMAL].type = type;
102 mAttribs[NORMAL].size = 3;
103 mAttribs[NORMAL].offset = offset;
104 mAttribs[NORMAL].stride = stride;
105 mAttribs[NORMAL].normalized = type != GL_FLOAT;
106}
107
108void VertexArray::setPointSize(uint32_t type, uint32_t stride, uint32_t offset)
109{
110 mAttribs[POINT_SIZE].buffer = mActiveBuffer;
111 mAttribs[POINT_SIZE].type = type;
112 mAttribs[POINT_SIZE].size = 1;
113 mAttribs[POINT_SIZE].offset = offset;
114 mAttribs[POINT_SIZE].stride = stride;
115 mAttribs[POINT_SIZE].normalized = false;
116}
117
Jason Samsa09a6e12010-01-06 11:57:52 -0800118void VertexArray::setTexture(uint32_t size, uint32_t type, uint32_t stride, uint32_t offset)
Jason Samsbb51c402009-11-25 13:22:07 -0800119{
Jason Samsa09a6e12010-01-06 11:57:52 -0800120 mAttribs[TEXTURE].buffer = mActiveBuffer;
121 mAttribs[TEXTURE].type = type;
122 mAttribs[TEXTURE].size = size;
123 mAttribs[TEXTURE].offset = offset;
124 mAttribs[TEXTURE].stride = stride;
125 mAttribs[TEXTURE].normalized = false;
Jason Samsbb51c402009-11-25 13:22:07 -0800126}
127
Jason Samsa09a6e12010-01-06 11:57:52 -0800128void VertexArray::setUser(const Attrib &a, uint32_t stride)
129{
130 mAttribs[mUserCount].set(a);
131 mAttribs[mUserCount].buffer = mActiveBuffer;
132 mAttribs[mUserCount].stride = stride;
133 mUserCount ++;
134}
135
136void VertexArray::logAttrib(uint32_t idx, uint32_t slot) const {
137 LOGE("va %i: slot=%i name=%s buf=%i size=%i type=0x%x stride=0x%x norm=%i offset=0x%x", idx, slot,
138 mAttribs[idx].name.string(),
Jason Samsbb51c402009-11-25 13:22:07 -0800139 mAttribs[idx].buffer,
140 mAttribs[idx].size,
141 mAttribs[idx].type,
142 mAttribs[idx].stride,
Jason Sams718cd1f2009-12-23 14:35:29 -0800143 mAttribs[idx].normalized,
Jason Samsbb51c402009-11-25 13:22:07 -0800144 mAttribs[idx].offset);
145}
146
Jason Sams718cd1f2009-12-23 14:35:29 -0800147void VertexArray::setupGL(const Context *rsc, class VertexArrayState *state) const
Jason Samsbb51c402009-11-25 13:22:07 -0800148{
149 if (mAttribs[POSITION].size) {
150 //logAttrib(POSITION);
151 glEnableClientState(GL_VERTEX_ARRAY);
152 glBindBuffer(GL_ARRAY_BUFFER, mAttribs[POSITION].buffer);
153 glVertexPointer(mAttribs[POSITION].size,
154 mAttribs[POSITION].type,
155 mAttribs[POSITION].stride,
156 (void *)mAttribs[POSITION].offset);
157 } else {
158 rsAssert(0);
159 }
160
161 if (mAttribs[NORMAL].size) {
162 //logAttrib(NORMAL);
163 glEnableClientState(GL_NORMAL_ARRAY);
164 rsAssert(mAttribs[NORMAL].size == 3);
165 glBindBuffer(GL_ARRAY_BUFFER, mAttribs[NORMAL].buffer);
166 glNormalPointer(mAttribs[NORMAL].type,
167 mAttribs[NORMAL].stride,
168 (void *)mAttribs[NORMAL].offset);
169 } else {
170 glDisableClientState(GL_NORMAL_ARRAY);
171 }
172
173 if (mAttribs[COLOR].size) {
174 //logAttrib(COLOR);
175 glEnableClientState(GL_COLOR_ARRAY);
176 glBindBuffer(GL_ARRAY_BUFFER, mAttribs[COLOR].buffer);
177 glColorPointer(mAttribs[COLOR].size,
178 mAttribs[COLOR].type,
179 mAttribs[COLOR].stride,
180 (void *)mAttribs[COLOR].offset);
181 } else {
182 glDisableClientState(GL_COLOR_ARRAY);
183 }
184
Jason Samsbb51c402009-11-25 13:22:07 -0800185 glClientActiveTexture(GL_TEXTURE0);
Jason Samsa09a6e12010-01-06 11:57:52 -0800186 if (mAttribs[TEXTURE].size) {
187 //logAttrib(TEXTURE);
188 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
189 glBindBuffer(GL_ARRAY_BUFFER, mAttribs[TEXTURE].buffer);
190 glTexCoordPointer(mAttribs[TEXTURE].size,
191 mAttribs[TEXTURE].type,
192 mAttribs[TEXTURE].stride,
193 (void *)mAttribs[TEXTURE].offset);
194 } else {
195 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
196 }
Jason Samsbb51c402009-11-25 13:22:07 -0800197
198 if (mAttribs[POINT_SIZE].size) {
199 //logAttrib(POINT_SIZE);
200 glEnableClientState(GL_POINT_SIZE_ARRAY_OES);
201 glBindBuffer(GL_ARRAY_BUFFER, mAttribs[POINT_SIZE].buffer);
202 glPointSizePointerOES(mAttribs[POINT_SIZE].type,
203 mAttribs[POINT_SIZE].stride,
204 (void *)mAttribs[POINT_SIZE].offset);
205 } else {
206 glDisableClientState(GL_POINT_SIZE_ARRAY_OES);
207 }
Jason Sams718cd1f2009-12-23 14:35:29 -0800208 rsc->checkError("VertexArray::setupGL");
Jason Samsbb51c402009-11-25 13:22:07 -0800209}
210
Jason Sams718cd1f2009-12-23 14:35:29 -0800211void VertexArray::setupGL2(const Context *rsc, class VertexArrayState *state, ShaderCache *sc) const
Jason Samsbb51c402009-11-25 13:22:07 -0800212{
213 for (int ct=1; ct < _LAST; ct++) {
214 glDisableVertexAttribArray(ct);
215 }
216
Jason Samsa09a6e12010-01-06 11:57:52 -0800217 for (uint32_t ct=0; ct < RS_MAX_ATTRIBS; ct++) {
Jason Samsbb51c402009-11-25 13:22:07 -0800218 if (mAttribs[ct].size) {
Jason Samsa09a6e12010-01-06 11:57:52 -0800219 //logAttrib(ct, sc->vtxAttribSlot(ct));
220 rsAssert(sc->vtxAttribSlot(ct) >= 0);
Jason Samsbb51c402009-11-25 13:22:07 -0800221 glEnableVertexAttribArray(sc->vtxAttribSlot(ct));
222 glBindBuffer(GL_ARRAY_BUFFER, mAttribs[ct].buffer);
Jason Samsbb51c402009-11-25 13:22:07 -0800223
224 glVertexAttribPointer(sc->vtxAttribSlot(ct),
225 mAttribs[ct].size,
226 mAttribs[ct].type,
227 mAttribs[ct].normalized,
228 mAttribs[ct].stride,
229 (void *)mAttribs[ct].offset);
Jason Samsbb51c402009-11-25 13:22:07 -0800230 }
231 }
Jason Sams718cd1f2009-12-23 14:35:29 -0800232 rsc->checkError("VertexArray::setupGL2");
Jason Samsbb51c402009-11-25 13:22:07 -0800233}
234////////////////////////////////////////////
235
236void VertexArrayState::init(Context *) {
Jason Samsbb51c402009-11-25 13:22:07 -0800237}
238