blob: 170b792194b8738a766316e2c65b42ef9199a383 [file] [log] [blame]
Jason Sams1bada8c2009-08-09 17:01:55 -07001/*
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
Alex Sakhartchoukaa7d2882010-05-21 12:53:13 -070017#ifndef ANDROID_RS_BUILD_FOR_HOST
Jason Sams1bada8c2009-08-09 17:01:55 -070018#include "rsContext.h"
19
Alex Sakhartchoukaa7d2882010-05-21 12:53:13 -070020#include <GLES/gl.h>
21#include <GLES2/gl2.h>
22#include <GLES/glext.h>
23#else
24#include "rsContextHostStub.h"
25
26#include <OpenGL/gl.h>
27#include <OpenGl/glext.h>
28#endif
29
Jason Sams1bada8c2009-08-09 17:01:55 -070030using namespace android;
31using namespace android::renderscript;
32
Alex Sakhartchoukaa7d2882010-05-21 12:53:13 -070033
34
Jason Sams1bada8c2009-08-09 17:01:55 -070035
Jason Samsa9e7a052009-09-25 14:51:22 -070036SimpleMesh::SimpleMesh(Context *rsc) : ObjectBase(rsc)
Jason Sams1bada8c2009-08-09 17:01:55 -070037{
Jason Sams61f08d62009-09-25 16:37:33 -070038 mAllocFile = __FILE__;
39 mAllocLine = __LINE__;
Jason Sams1bada8c2009-08-09 17:01:55 -070040}
41
42SimpleMesh::~SimpleMesh()
43{
Jason Sams61f08d62009-09-25 16:37:33 -070044 delete[] mVertexTypes;
45 delete[] mVertexBuffers;
Jason Sams1bada8c2009-08-09 17:01:55 -070046}
47
Jason Samsbb51c402009-11-25 13:22:07 -080048void SimpleMesh::render(Context *rsc) const
Jason Sams1bada8c2009-08-09 17:01:55 -070049{
50 if (mPrimitiveType.get()) {
Jason Samsbb51c402009-11-25 13:22:07 -080051 renderRange(rsc, 0, mPrimitiveType->getDimX());
Jason Sams1bada8c2009-08-09 17:01:55 -070052 return;
53 }
54
55 if (mIndexType.get()) {
Jason Samsbb51c402009-11-25 13:22:07 -080056 renderRange(rsc, 0, mIndexType->getDimX());
Jason Sams1bada8c2009-08-09 17:01:55 -070057 return;
58 }
59
Jason Samsbb51c402009-11-25 13:22:07 -080060 renderRange(rsc, 0, mVertexTypes[0]->getDimX());
Jason Sams1bada8c2009-08-09 17:01:55 -070061}
62
Jason Samsbb51c402009-11-25 13:22:07 -080063void SimpleMesh::renderRange(Context *rsc, uint32_t start, uint32_t len) const
Jason Sams1bada8c2009-08-09 17:01:55 -070064{
65 if (len < 1) {
66 return;
67 }
68
Jason Samsa09a6e12010-01-06 11:57:52 -080069 rsc->checkError("SimpleMesh::renderRange 1");
Jason Samsbb51c402009-11-25 13:22:07 -080070 VertexArray va;
Jason Samsbb51c402009-11-25 13:22:07 -080071 if (rsc->checkVersion2_0()) {
Jason Samsa09a6e12010-01-06 11:57:52 -080072 for (uint32_t ct=0; ct < mVertexTypeCount; ct++) {
73 mVertexBuffers[ct]->uploadCheck(rsc);
74 va.setActiveBuffer(mVertexBuffers[ct]->getBufferObjectID());
75 mVertexTypes[ct]->enableGLVertexBuffer2(&va);
76 }
Jason Sams5dbfe932010-01-27 14:41:43 -080077 va.setupGL2(rsc, &rsc->mStateVertexArray, &rsc->mShaderCache);
Jason Samsbb51c402009-11-25 13:22:07 -080078 } else {
Jason Samsa09a6e12010-01-06 11:57:52 -080079 for (uint32_t ct=0; ct < mVertexTypeCount; ct++) {
80 mVertexBuffers[ct]->uploadCheck(rsc);
81 va.setActiveBuffer(mVertexBuffers[ct]->getBufferObjectID());
82 mVertexTypes[ct]->enableGLVertexBuffer(&va);
83 }
Jason Sams718cd1f2009-12-23 14:35:29 -080084 va.setupGL(rsc, 0);
Jason Sams1bada8c2009-08-09 17:01:55 -070085 }
86
Jason Samsa09a6e12010-01-06 11:57:52 -080087 rsc->checkError("SimpleMesh::renderRange 2");
Jason Sams1bada8c2009-08-09 17:01:55 -070088 if (mIndexType.get()) {
Jason Sams3b7d39b2009-12-14 12:57:40 -080089 mIndexBuffer->uploadCheck(rsc);
Jason Sams1bada8c2009-08-09 17:01:55 -070090 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mIndexBuffer->getBufferObjectID());
Jason Sams07ae4062009-08-27 20:23:34 -070091 glDrawElements(mGLPrimitive, len, GL_UNSIGNED_SHORT, (uint16_t *)(start * 2));
Jason Sams1bada8c2009-08-09 17:01:55 -070092 } else {
93 glDrawArrays(mGLPrimitive, start, len);
94 }
Jason Sams718cd1f2009-12-23 14:35:29 -080095
96 rsc->checkError("SimpleMesh::renderRange");
Jason Sams1bada8c2009-08-09 17:01:55 -070097}
98
Jason Samsbb51c402009-11-25 13:22:07 -080099void SimpleMesh::uploadAll(Context *rsc)
Jason Sams6b9dec02009-09-23 16:38:37 -0700100{
101 for (uint32_t ct=0; ct < mVertexTypeCount; ct++) {
102 if (mVertexBuffers[ct].get()) {
Jason Sams3b7d39b2009-12-14 12:57:40 -0800103 mVertexBuffers[ct]->deferedUploadToBufferObject(rsc);
Jason Sams6b9dec02009-09-23 16:38:37 -0700104 }
105 }
106 if (mIndexBuffer.get()) {
Jason Sams3b7d39b2009-12-14 12:57:40 -0800107 mIndexBuffer->deferedUploadToBufferObject(rsc);
Jason Sams6b9dec02009-09-23 16:38:37 -0700108 }
109 if (mPrimitiveBuffer.get()) {
Jason Sams3b7d39b2009-12-14 12:57:40 -0800110 mPrimitiveBuffer->deferedUploadToBufferObject(rsc);
Jason Sams6b9dec02009-09-23 16:38:37 -0700111 }
Jason Sams718cd1f2009-12-23 14:35:29 -0800112 rsc->checkError("SimpleMesh::uploadAll");
Jason Sams6b9dec02009-09-23 16:38:37 -0700113}
Jason Sams1bada8c2009-08-09 17:01:55 -0700114
Alex Sakhartchoukaa7d2882010-05-21 12:53:13 -0700115void SimpleMesh::updateGLPrimitive()
116{
117 switch(mPrimitive) {
118 case RS_PRIMITIVE_POINT: mGLPrimitive = GL_POINTS; break;
119 case RS_PRIMITIVE_LINE: mGLPrimitive = GL_LINES; break;
120 case RS_PRIMITIVE_LINE_STRIP: mGLPrimitive = GL_LINE_STRIP; break;
121 case RS_PRIMITIVE_TRIANGLE: mGLPrimitive = GL_TRIANGLES; break;
122 case RS_PRIMITIVE_TRIANGLE_STRIP: mGLPrimitive = GL_TRIANGLE_STRIP; break;
123 case RS_PRIMITIVE_TRIANGLE_FAN: mGLPrimitive = GL_TRIANGLE_FAN; break;
124 }
125}
126
127void SimpleMesh::serialize(OStream *stream) const
128{
129 // Need to identify ourselves
130 stream->addU32((uint32_t)getClassId());
131
132 String8 name(getName());
133 stream->addString(&name);
134
135 // Add primitive type
136 stream->addU8((uint8_t)mPrimitive);
137
138 // And now serialize the allocations
139 mIndexBuffer->serialize(stream);
140
141 // We need to indicate if the primitive buffer is present
142 if(mPrimitiveBuffer.get() != NULL) {
143 // Write if the primitive buffer is present
144 stream->addU32(1);
145 mPrimitiveBuffer->serialize(stream);
146 }
147 else {
148 // No buffer present, will need this when we read
149 stream->addU32(0);
150 }
151
152 // Store number of vertex streams
153 stream->addU32(mVertexTypeCount);
154 for(uint32_t vCount = 0; vCount < mVertexTypeCount; vCount ++) {
155 mVertexBuffers[vCount]->serialize(stream);
156 }
157}
158
159SimpleMesh *SimpleMesh::createFromStream(Context *rsc, IStream *stream)
160{
161 // First make sure we are reading the correct object
162 A3DClassID classID = (A3DClassID)stream->loadU32();
163 if(classID != A3D_CLASS_ID_SIMPLE_MESH) {
164 LOGE("simple mesh loading skipped due to invalid class id");
165 return NULL;
166 }
167
168 SimpleMesh * mesh = new SimpleMesh(rsc);
169
170 String8 name;
171 stream->loadString(&name);
172 mesh->setName(name.string(), name.size());
173
174 mesh->mPrimitive = (RsPrimitive)stream->loadU8();
175 mesh->updateGLPrimitive();
176
177 Allocation *indexAlloc = Allocation::createFromStream(rsc, stream);
178 const Type *indexType = indexAlloc->getType();
179 mesh->mIndexBuffer.set(indexAlloc);
180 mesh->mIndexType.set(indexType);
181
182 bool isPrimitivePresent = stream->loadU32() != 0;
183 if(isPrimitivePresent) {
184 mesh->mPrimitiveBuffer.set(Allocation::createFromStream(rsc, stream));
185 mesh->mPrimitiveType.set(mesh->mPrimitiveBuffer->getType());
186 }
187
188 mesh->mVertexTypeCount = stream->loadU32();
189 if(mesh->mVertexTypeCount) {
190 mesh->mVertexTypes = new ObjectBaseRef<const Type>[mesh->mVertexTypeCount];
191 mesh->mVertexBuffers = new ObjectBaseRef<Allocation>[mesh->mVertexTypeCount];
192
193 for(uint32_t vCount = 0; vCount < mesh->mVertexTypeCount; vCount ++) {
194 Allocation *vertexAlloc = Allocation::createFromStream(rsc, stream);
195 const Type *vertexType = vertexAlloc->getType();
196 mesh->mVertexBuffers[vCount].set(vertexAlloc);
197 mesh->mVertexTypes[vCount].set(vertexType);
198 }
199 }
200
201 return mesh;
202}
203
Jason Sams1bada8c2009-08-09 17:01:55 -0700204
205SimpleMeshContext::SimpleMeshContext()
206{
207}
208
209SimpleMeshContext::~SimpleMeshContext()
210{
211}
212
213
214namespace android {
215namespace renderscript {
216
217
218RsSimpleMesh rsi_SimpleMeshCreate(Context *rsc, RsType prim, RsType idx, RsType *vtx, uint32_t vtxCount, uint32_t primType)
219{
Jason Samsa9e7a052009-09-25 14:51:22 -0700220 SimpleMesh *sm = new SimpleMesh(rsc);
Jason Sams07ae4062009-08-27 20:23:34 -0700221 sm->incUserRef();
Jason Sams1bada8c2009-08-09 17:01:55 -0700222
223 sm->mIndexType.set((const Type *)idx);
224 sm->mPrimitiveType.set((const Type *)prim);
225
226 sm->mVertexTypeCount = vtxCount;
227 sm->mVertexTypes = new ObjectBaseRef<const Type>[vtxCount];
228 sm->mVertexBuffers = new ObjectBaseRef<Allocation>[vtxCount];
229 for (uint32_t ct=0; ct < vtxCount; ct++) {
230 sm->mVertexTypes[ct].set((const Type *)vtx[ct]);
231 }
232
233 sm->mPrimitive = (RsPrimitive)primType;
Alex Sakhartchoukaa7d2882010-05-21 12:53:13 -0700234 sm->updateGLPrimitive();
Jason Sams1bada8c2009-08-09 17:01:55 -0700235 return sm;
236}
237
238void rsi_SimpleMeshBindVertex(Context *rsc, RsSimpleMesh mv, RsAllocation va, uint32_t slot)
239{
240 SimpleMesh *sm = static_cast<SimpleMesh *>(mv);
241 rsAssert(slot < sm->mVertexTypeCount);
242
243 sm->mVertexBuffers[slot].set((Allocation *)va);
244}
245
246void rsi_SimpleMeshBindIndex(Context *rsc, RsSimpleMesh mv, RsAllocation va)
247{
248 SimpleMesh *sm = static_cast<SimpleMesh *>(mv);
249 sm->mIndexBuffer.set((Allocation *)va);
250}
251
252void rsi_SimpleMeshBindPrimitive(Context *rsc, RsSimpleMesh mv, RsAllocation va)
253{
254 SimpleMesh *sm = static_cast<SimpleMesh *>(mv);
255 sm->mPrimitiveBuffer.set((Allocation *)va);
256}
257
Jason Sams1bada8c2009-08-09 17:01:55 -0700258
259
260
261}}
262