blob: 7124eb568979b7eca305e92c6a9a4e5041982671 [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 Samsc460e552009-11-25 13:22:07 -080028 mActiveBuffer = 0;
29}
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 Sams433eca32010-01-06 11:57:52 -080042}
43
44VertexArray::Attrib::Attrib()
45{
46 clear();
47}
48
49void VertexArray::Attrib::set(const Attrib &a)
50{
51 buffer = a.buffer;
52 offset = a.offset;
53 type = a.type;
54 size = a.size;
55 stride = a.stride;
56 normalized = a.normalized;
57 name.setTo(a.name);
58}
59
60void VertexArray::Attrib::clear()
61{
62 buffer = 0;
63 offset = 0;
64 type = 0;
65 size = 0;
66 stride = 0;
67 normalized = false;
68 name.setTo("");
Jason Samsc460e552009-11-25 13:22:07 -080069}
70
71void VertexArray::clear(AttribName n)
72{
Jason Sams433eca32010-01-06 11:57:52 -080073 mAttribs[n].clear();
Jason Samsc460e552009-11-25 13:22:07 -080074}
75
76void VertexArray::setPosition(uint32_t size, uint32_t type, uint32_t stride, uint32_t offset)
77{
78 mAttribs[POSITION].buffer = mActiveBuffer;
79 mAttribs[POSITION].type = type;
80 mAttribs[POSITION].size = size;
81 mAttribs[POSITION].offset = offset;
82 mAttribs[POSITION].stride = stride;
83 mAttribs[POSITION].normalized = false;
84}
85
86void VertexArray::setColor(uint32_t size, uint32_t type, uint32_t stride, uint32_t offset)
87{
88 mAttribs[COLOR].buffer = mActiveBuffer;
89 mAttribs[COLOR].type = type;
90 mAttribs[COLOR].size = size;
91 mAttribs[COLOR].offset = offset;
92 mAttribs[COLOR].stride = stride;
93 mAttribs[COLOR].normalized = type != GL_FLOAT;
94}
95
96void VertexArray::setNormal(uint32_t type, uint32_t stride, uint32_t offset)
97{
98 mAttribs[NORMAL].buffer = mActiveBuffer;
99 mAttribs[NORMAL].type = type;
100 mAttribs[NORMAL].size = 3;
101 mAttribs[NORMAL].offset = offset;
102 mAttribs[NORMAL].stride = stride;
103 mAttribs[NORMAL].normalized = type != GL_FLOAT;
104}
105
106void VertexArray::setPointSize(uint32_t type, uint32_t stride, uint32_t offset)
107{
108 mAttribs[POINT_SIZE].buffer = mActiveBuffer;
109 mAttribs[POINT_SIZE].type = type;
110 mAttribs[POINT_SIZE].size = 1;
111 mAttribs[POINT_SIZE].offset = offset;
112 mAttribs[POINT_SIZE].stride = stride;
113 mAttribs[POINT_SIZE].normalized = false;
114}
115
Jason Sams433eca32010-01-06 11:57:52 -0800116void VertexArray::setTexture(uint32_t size, uint32_t type, uint32_t stride, uint32_t offset)
Jason Samsc460e552009-11-25 13:22:07 -0800117{
Jason Sams433eca32010-01-06 11:57:52 -0800118 mAttribs[TEXTURE].buffer = mActiveBuffer;
119 mAttribs[TEXTURE].type = type;
120 mAttribs[TEXTURE].size = size;
121 mAttribs[TEXTURE].offset = offset;
122 mAttribs[TEXTURE].stride = stride;
123 mAttribs[TEXTURE].normalized = false;
Jason Samsc460e552009-11-25 13:22:07 -0800124}
125
Jason Sams433eca32010-01-06 11:57:52 -0800126void VertexArray::setUser(const Attrib &a, uint32_t stride)
127{
Jason Samscacee552010-01-07 15:40:45 -0800128 // Find empty slot, some may be taken by legacy 1.1 slots.
129 uint32_t slot = 0;
130 while (mAttribs[slot].size) slot++;
131 rsAssert(slot < RS_MAX_ATTRIBS);
132 mAttribs[slot].set(a);
133 mAttribs[slot].buffer = mActiveBuffer;
134 mAttribs[slot].stride = stride;
Jason Sams433eca32010-01-06 11:57:52 -0800135}
136
137void VertexArray::logAttrib(uint32_t idx, uint32_t slot) const {
138 LOGE("va %i: slot=%i name=%s buf=%i size=%i type=0x%x stride=0x%x norm=%i offset=0x%x", idx, slot,
139 mAttribs[idx].name.string(),
Jason Samsc460e552009-11-25 13:22:07 -0800140 mAttribs[idx].buffer,
141 mAttribs[idx].size,
142 mAttribs[idx].type,
143 mAttribs[idx].stride,
Jason Samsd01d9702009-12-23 14:35:29 -0800144 mAttribs[idx].normalized,
Jason Samsc460e552009-11-25 13:22:07 -0800145 mAttribs[idx].offset);
146}
147
Jason Samsd01d9702009-12-23 14:35:29 -0800148void VertexArray::setupGL(const Context *rsc, class VertexArrayState *state) const
Jason Samsc460e552009-11-25 13:22:07 -0800149{
150 if (mAttribs[POSITION].size) {
151 //logAttrib(POSITION);
152 glEnableClientState(GL_VERTEX_ARRAY);
153 glBindBuffer(GL_ARRAY_BUFFER, mAttribs[POSITION].buffer);
154 glVertexPointer(mAttribs[POSITION].size,
155 mAttribs[POSITION].type,
156 mAttribs[POSITION].stride,
157 (void *)mAttribs[POSITION].offset);
158 } else {
159 rsAssert(0);
160 }
161
162 if (mAttribs[NORMAL].size) {
163 //logAttrib(NORMAL);
164 glEnableClientState(GL_NORMAL_ARRAY);
165 rsAssert(mAttribs[NORMAL].size == 3);
166 glBindBuffer(GL_ARRAY_BUFFER, mAttribs[NORMAL].buffer);
167 glNormalPointer(mAttribs[NORMAL].type,
168 mAttribs[NORMAL].stride,
169 (void *)mAttribs[NORMAL].offset);
170 } else {
171 glDisableClientState(GL_NORMAL_ARRAY);
172 }
173
174 if (mAttribs[COLOR].size) {
175 //logAttrib(COLOR);
176 glEnableClientState(GL_COLOR_ARRAY);
177 glBindBuffer(GL_ARRAY_BUFFER, mAttribs[COLOR].buffer);
178 glColorPointer(mAttribs[COLOR].size,
179 mAttribs[COLOR].type,
180 mAttribs[COLOR].stride,
181 (void *)mAttribs[COLOR].offset);
182 } else {
183 glDisableClientState(GL_COLOR_ARRAY);
184 }
185
Jason Samsc460e552009-11-25 13:22:07 -0800186 glClientActiveTexture(GL_TEXTURE0);
Jason Sams433eca32010-01-06 11:57:52 -0800187 if (mAttribs[TEXTURE].size) {
188 //logAttrib(TEXTURE);
189 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
190 glBindBuffer(GL_ARRAY_BUFFER, mAttribs[TEXTURE].buffer);
191 glTexCoordPointer(mAttribs[TEXTURE].size,
192 mAttribs[TEXTURE].type,
193 mAttribs[TEXTURE].stride,
194 (void *)mAttribs[TEXTURE].offset);
195 } else {
196 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
197 }
Jason Samsc460e552009-11-25 13:22:07 -0800198
199 if (mAttribs[POINT_SIZE].size) {
200 //logAttrib(POINT_SIZE);
201 glEnableClientState(GL_POINT_SIZE_ARRAY_OES);
202 glBindBuffer(GL_ARRAY_BUFFER, mAttribs[POINT_SIZE].buffer);
203 glPointSizePointerOES(mAttribs[POINT_SIZE].type,
204 mAttribs[POINT_SIZE].stride,
205 (void *)mAttribs[POINT_SIZE].offset);
206 } else {
207 glDisableClientState(GL_POINT_SIZE_ARRAY_OES);
208 }
Jason Samsd01d9702009-12-23 14:35:29 -0800209 rsc->checkError("VertexArray::setupGL");
Jason Samsc460e552009-11-25 13:22:07 -0800210}
211
Jason Samsd01d9702009-12-23 14:35:29 -0800212void VertexArray::setupGL2(const Context *rsc, class VertexArrayState *state, ShaderCache *sc) const
Jason Samsc460e552009-11-25 13:22:07 -0800213{
214 for (int ct=1; ct < _LAST; ct++) {
215 glDisableVertexAttribArray(ct);
216 }
217
Jason Sams433eca32010-01-06 11:57:52 -0800218 for (uint32_t ct=0; ct < RS_MAX_ATTRIBS; ct++) {
Jason Samscacee552010-01-07 15:40:45 -0800219 if (mAttribs[ct].size && (sc->vtxAttribSlot(ct) >= 0)) {
Jason Sams433eca32010-01-06 11:57:52 -0800220 //logAttrib(ct, sc->vtxAttribSlot(ct));
Jason Samsc460e552009-11-25 13:22:07 -0800221 glEnableVertexAttribArray(sc->vtxAttribSlot(ct));
222 glBindBuffer(GL_ARRAY_BUFFER, mAttribs[ct].buffer);
Jason Samsc460e552009-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 Samsc460e552009-11-25 13:22:07 -0800230 }
231 }
Jason Samsd01d9702009-12-23 14:35:29 -0800232 rsc->checkError("VertexArray::setupGL2");
Jason Samsc460e552009-11-25 13:22:07 -0800233}
234////////////////////////////////////////////
235
236void VertexArrayState::init(Context *) {
Jason Samsc460e552009-11-25 13:22:07 -0800237}
238