blob: 34d42ed8b01ed570fd04d43bc693d0287f8b538f [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{
28 memset(mAttribs, 0, sizeof(mAttribs));
29 mActiveBuffer = 0;
30}
31
32VertexArray::~VertexArray()
33{
34}
35
36
37void VertexArray::clearAll()
38{
39 memset(mAttribs, 0, sizeof(mAttribs));
40 mActiveBuffer = 0;
41}
42
43void VertexArray::clear(AttribName n)
44{
45 mAttribs[n].size = 0;
46}
47
48void VertexArray::setPosition(uint32_t size, uint32_t type, uint32_t stride, uint32_t offset)
49{
50 mAttribs[POSITION].buffer = mActiveBuffer;
51 mAttribs[POSITION].type = type;
52 mAttribs[POSITION].size = size;
53 mAttribs[POSITION].offset = offset;
54 mAttribs[POSITION].stride = stride;
55 mAttribs[POSITION].normalized = false;
56}
57
58void VertexArray::setColor(uint32_t size, uint32_t type, uint32_t stride, uint32_t offset)
59{
60 mAttribs[COLOR].buffer = mActiveBuffer;
61 mAttribs[COLOR].type = type;
62 mAttribs[COLOR].size = size;
63 mAttribs[COLOR].offset = offset;
64 mAttribs[COLOR].stride = stride;
65 mAttribs[COLOR].normalized = type != GL_FLOAT;
66}
67
68void VertexArray::setNormal(uint32_t type, uint32_t stride, uint32_t offset)
69{
70 mAttribs[NORMAL].buffer = mActiveBuffer;
71 mAttribs[NORMAL].type = type;
72 mAttribs[NORMAL].size = 3;
73 mAttribs[NORMAL].offset = offset;
74 mAttribs[NORMAL].stride = stride;
75 mAttribs[NORMAL].normalized = type != GL_FLOAT;
76}
77
78void VertexArray::setPointSize(uint32_t type, uint32_t stride, uint32_t offset)
79{
80 mAttribs[POINT_SIZE].buffer = mActiveBuffer;
81 mAttribs[POINT_SIZE].type = type;
82 mAttribs[POINT_SIZE].size = 1;
83 mAttribs[POINT_SIZE].offset = offset;
84 mAttribs[POINT_SIZE].stride = stride;
85 mAttribs[POINT_SIZE].normalized = false;
86}
87
88void VertexArray::setTexture(uint32_t size, uint32_t type, uint32_t stride, uint32_t offset, uint32_t num)
89{
90 mAttribs[TEXTURE_0 + num].buffer = mActiveBuffer;
91 mAttribs[TEXTURE_0 + num].type = type;
92 mAttribs[TEXTURE_0 + num].size = size;
93 mAttribs[TEXTURE_0 + num].offset = offset;
94 mAttribs[TEXTURE_0 + num].stride = stride;
95 mAttribs[TEXTURE_0 + num].normalized = false;
96}
97
98void VertexArray::logAttrib(uint32_t idx) const {
99 LOGE("va %i: buf=%i size=%i type=0x%x stride=0x%x offset=0x%x", idx,
100 mAttribs[idx].buffer,
101 mAttribs[idx].size,
102 mAttribs[idx].type,
103 mAttribs[idx].stride,
104 mAttribs[idx].offset);
105}
106
107void VertexArray::setupGL(class VertexArrayState *state) const
108{
109 if (mAttribs[POSITION].size) {
110 //logAttrib(POSITION);
111 glEnableClientState(GL_VERTEX_ARRAY);
112 glBindBuffer(GL_ARRAY_BUFFER, mAttribs[POSITION].buffer);
113 glVertexPointer(mAttribs[POSITION].size,
114 mAttribs[POSITION].type,
115 mAttribs[POSITION].stride,
116 (void *)mAttribs[POSITION].offset);
117 } else {
118 rsAssert(0);
119 }
120
121 if (mAttribs[NORMAL].size) {
122 //logAttrib(NORMAL);
123 glEnableClientState(GL_NORMAL_ARRAY);
124 rsAssert(mAttribs[NORMAL].size == 3);
125 glBindBuffer(GL_ARRAY_BUFFER, mAttribs[NORMAL].buffer);
126 glNormalPointer(mAttribs[NORMAL].type,
127 mAttribs[NORMAL].stride,
128 (void *)mAttribs[NORMAL].offset);
129 } else {
130 glDisableClientState(GL_NORMAL_ARRAY);
131 }
132
133 if (mAttribs[COLOR].size) {
134 //logAttrib(COLOR);
135 glEnableClientState(GL_COLOR_ARRAY);
136 glBindBuffer(GL_ARRAY_BUFFER, mAttribs[COLOR].buffer);
137 glColorPointer(mAttribs[COLOR].size,
138 mAttribs[COLOR].type,
139 mAttribs[COLOR].stride,
140 (void *)mAttribs[COLOR].offset);
141 } else {
142 glDisableClientState(GL_COLOR_ARRAY);
143 }
144
145 for (uint32_t ct=0; ct < RS_MAX_TEXTURE; ct++) {
146 glClientActiveTexture(GL_TEXTURE0 + ct);
147 if (mAttribs[TEXTURE_0 + ct].size) {
148 //logAttrib(TEXTURE_0 + ct);
149 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
150 glBindBuffer(GL_ARRAY_BUFFER, mAttribs[TEXTURE_0 + ct].buffer);
151 glTexCoordPointer(mAttribs[TEXTURE_0 + ct].size,
152 mAttribs[TEXTURE_0 + ct].type,
153 mAttribs[TEXTURE_0 + ct].stride,
154 (void *)mAttribs[TEXTURE_0 + ct].offset);
155 } else {
156 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
157 }
158 }
159 glClientActiveTexture(GL_TEXTURE0);
160
161 if (mAttribs[POINT_SIZE].size) {
162 //logAttrib(POINT_SIZE);
163 glEnableClientState(GL_POINT_SIZE_ARRAY_OES);
164 glBindBuffer(GL_ARRAY_BUFFER, mAttribs[POINT_SIZE].buffer);
165 glPointSizePointerOES(mAttribs[POINT_SIZE].type,
166 mAttribs[POINT_SIZE].stride,
167 (void *)mAttribs[POINT_SIZE].offset);
168 } else {
169 glDisableClientState(GL_POINT_SIZE_ARRAY_OES);
170 }
171}
172
173void VertexArray::setupGL2(class VertexArrayState *state, ShaderCache *sc) const
174{
175 for (int ct=1; ct < _LAST; ct++) {
176 glDisableVertexAttribArray(ct);
177 }
178
179 for (int ct=0; ct < _LAST; ct++) {
180 if (mAttribs[ct].size) {
181 //logAttrib(ct);
182 glEnableVertexAttribArray(sc->vtxAttribSlot(ct));
183 glBindBuffer(GL_ARRAY_BUFFER, mAttribs[ct].buffer);
184 //LOGV("attp %i %i", ct, sc->vtxAttribSlot(ct));
185
186 glVertexAttribPointer(sc->vtxAttribSlot(ct),
187 mAttribs[ct].size,
188 mAttribs[ct].type,
189 mAttribs[ct].normalized,
190 mAttribs[ct].stride,
191 (void *)mAttribs[ct].offset);
192 } else {
193 //glDisableVertexAttribArray(ct);
194 rsAssert(ct);
195 }
196 }
197}
198////////////////////////////////////////////
199
200void VertexArrayState::init(Context *) {
201 memset(this, 0, sizeof(this));
202}
203