blob: 2ba0ef96c5e015fedffc5dcb4eed69eb8c24e492 [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 {
Jason Samsd01d9702009-12-23 14:35:29 -080099 LOGE("va %i: buf=%i size=%i type=0x%x stride=0x%x norm=%i offset=0x%x", idx,
Jason Samsc460e552009-11-25 13:22:07 -0800100 mAttribs[idx].buffer,
101 mAttribs[idx].size,
102 mAttribs[idx].type,
103 mAttribs[idx].stride,
Jason Samsd01d9702009-12-23 14:35:29 -0800104 mAttribs[idx].normalized,
Jason Samsc460e552009-11-25 13:22:07 -0800105 mAttribs[idx].offset);
106}
107
Jason Samsd01d9702009-12-23 14:35:29 -0800108void VertexArray::setupGL(const Context *rsc, class VertexArrayState *state) const
Jason Samsc460e552009-11-25 13:22:07 -0800109{
110 if (mAttribs[POSITION].size) {
111 //logAttrib(POSITION);
112 glEnableClientState(GL_VERTEX_ARRAY);
113 glBindBuffer(GL_ARRAY_BUFFER, mAttribs[POSITION].buffer);
114 glVertexPointer(mAttribs[POSITION].size,
115 mAttribs[POSITION].type,
116 mAttribs[POSITION].stride,
117 (void *)mAttribs[POSITION].offset);
118 } else {
119 rsAssert(0);
120 }
121
122 if (mAttribs[NORMAL].size) {
123 //logAttrib(NORMAL);
124 glEnableClientState(GL_NORMAL_ARRAY);
125 rsAssert(mAttribs[NORMAL].size == 3);
126 glBindBuffer(GL_ARRAY_BUFFER, mAttribs[NORMAL].buffer);
127 glNormalPointer(mAttribs[NORMAL].type,
128 mAttribs[NORMAL].stride,
129 (void *)mAttribs[NORMAL].offset);
130 } else {
131 glDisableClientState(GL_NORMAL_ARRAY);
132 }
133
134 if (mAttribs[COLOR].size) {
135 //logAttrib(COLOR);
136 glEnableClientState(GL_COLOR_ARRAY);
137 glBindBuffer(GL_ARRAY_BUFFER, mAttribs[COLOR].buffer);
138 glColorPointer(mAttribs[COLOR].size,
139 mAttribs[COLOR].type,
140 mAttribs[COLOR].stride,
141 (void *)mAttribs[COLOR].offset);
142 } else {
143 glDisableClientState(GL_COLOR_ARRAY);
144 }
145
146 for (uint32_t ct=0; ct < RS_MAX_TEXTURE; ct++) {
147 glClientActiveTexture(GL_TEXTURE0 + ct);
148 if (mAttribs[TEXTURE_0 + ct].size) {
149 //logAttrib(TEXTURE_0 + ct);
150 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
151 glBindBuffer(GL_ARRAY_BUFFER, mAttribs[TEXTURE_0 + ct].buffer);
152 glTexCoordPointer(mAttribs[TEXTURE_0 + ct].size,
153 mAttribs[TEXTURE_0 + ct].type,
154 mAttribs[TEXTURE_0 + ct].stride,
155 (void *)mAttribs[TEXTURE_0 + ct].offset);
156 } else {
157 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
158 }
159 }
160 glClientActiveTexture(GL_TEXTURE0);
161
162 if (mAttribs[POINT_SIZE].size) {
163 //logAttrib(POINT_SIZE);
164 glEnableClientState(GL_POINT_SIZE_ARRAY_OES);
165 glBindBuffer(GL_ARRAY_BUFFER, mAttribs[POINT_SIZE].buffer);
166 glPointSizePointerOES(mAttribs[POINT_SIZE].type,
167 mAttribs[POINT_SIZE].stride,
168 (void *)mAttribs[POINT_SIZE].offset);
169 } else {
170 glDisableClientState(GL_POINT_SIZE_ARRAY_OES);
171 }
Jason Samsd01d9702009-12-23 14:35:29 -0800172 rsc->checkError("VertexArray::setupGL");
Jason Samsc460e552009-11-25 13:22:07 -0800173}
174
Jason Samsd01d9702009-12-23 14:35:29 -0800175void VertexArray::setupGL2(const Context *rsc, class VertexArrayState *state, ShaderCache *sc) const
Jason Samsc460e552009-11-25 13:22:07 -0800176{
177 for (int ct=1; ct < _LAST; ct++) {
178 glDisableVertexAttribArray(ct);
179 }
180
181 for (int ct=0; ct < _LAST; ct++) {
182 if (mAttribs[ct].size) {
183 //logAttrib(ct);
184 glEnableVertexAttribArray(sc->vtxAttribSlot(ct));
185 glBindBuffer(GL_ARRAY_BUFFER, mAttribs[ct].buffer);
186 //LOGV("attp %i %i", ct, sc->vtxAttribSlot(ct));
187
188 glVertexAttribPointer(sc->vtxAttribSlot(ct),
189 mAttribs[ct].size,
190 mAttribs[ct].type,
191 mAttribs[ct].normalized,
192 mAttribs[ct].stride,
193 (void *)mAttribs[ct].offset);
194 } else {
195 //glDisableVertexAttribArray(ct);
196 rsAssert(ct);
197 }
198 }
Jason Samsd01d9702009-12-23 14:35:29 -0800199 rsc->checkError("VertexArray::setupGL2");
Jason Samsc460e552009-11-25 13:22:07 -0800200}
201////////////////////////////////////////////
202
203void VertexArrayState::init(Context *) {
204 memset(this, 0, sizeof(this));
205}
206