blob: 6ec1a3b77f53b7e0b8433e83a49b8a6254b7db92 [file] [log] [blame]
Jason Samse5ffb872009-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 Sakhartchoukfb6b6142010-05-21 12:53:13 -070017#ifndef ANDROID_RS_BUILD_FOR_HOST
Jason Samse5ffb872009-08-09 17:01:55 -070018#include "rsContext.h"
19
Alex Sakhartchoukfb6b6142010-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 Samse5ffb872009-08-09 17:01:55 -070030using namespace android;
31using namespace android::renderscript;
32
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -070033
34
Jason Samse5ffb872009-08-09 17:01:55 -070035
Jason Samse514b452009-09-25 14:51:22 -070036SimpleMesh::SimpleMesh(Context *rsc) : ObjectBase(rsc)
Jason Samse5ffb872009-08-09 17:01:55 -070037{
Jason Samsf2649a92009-09-25 16:37:33 -070038 mAllocFile = __FILE__;
39 mAllocLine = __LINE__;
Jason Samse5ffb872009-08-09 17:01:55 -070040}
41
42SimpleMesh::~SimpleMesh()
43{
Jason Samsf2649a92009-09-25 16:37:33 -070044 delete[] mVertexTypes;
45 delete[] mVertexBuffers;
Jason Samse5ffb872009-08-09 17:01:55 -070046}
47
Jason Samsc460e552009-11-25 13:22:07 -080048void SimpleMesh::render(Context *rsc) const
Jason Samse5ffb872009-08-09 17:01:55 -070049{
50 if (mPrimitiveType.get()) {
Jason Samsc460e552009-11-25 13:22:07 -080051 renderRange(rsc, 0, mPrimitiveType->getDimX());
Jason Samse5ffb872009-08-09 17:01:55 -070052 return;
53 }
54
55 if (mIndexType.get()) {
Jason Samsc460e552009-11-25 13:22:07 -080056 renderRange(rsc, 0, mIndexType->getDimX());
Jason Samse5ffb872009-08-09 17:01:55 -070057 return;
58 }
59
Jason Samsc460e552009-11-25 13:22:07 -080060 renderRange(rsc, 0, mVertexTypes[0]->getDimX());
Jason Samse5ffb872009-08-09 17:01:55 -070061}
62
Jason Samsc460e552009-11-25 13:22:07 -080063void SimpleMesh::renderRange(Context *rsc, uint32_t start, uint32_t len) const
Jason Samse5ffb872009-08-09 17:01:55 -070064{
65 if (len < 1) {
66 return;
67 }
68
Jason Sams433eca32010-01-06 11:57:52 -080069 rsc->checkError("SimpleMesh::renderRange 1");
Jason Samsc460e552009-11-25 13:22:07 -080070 VertexArray va;
Jason Sams79f52df2010-06-01 15:47:01 -070071 for (uint32_t ct=0; ct < mVertexTypeCount; ct++) {
72 mVertexBuffers[ct]->uploadCheck(rsc);
73 va.setActiveBuffer(mVertexBuffers[ct]->getBufferObjectID());
74 mVertexTypes[ct]->enableGLVertexBuffer(&va);
Jason Samse5ffb872009-08-09 17:01:55 -070075 }
Jason Sams79f52df2010-06-01 15:47:01 -070076 va.setupGL2(rsc, &rsc->mStateVertexArray, &rsc->mShaderCache);
Jason Samse5ffb872009-08-09 17:01:55 -070077
Jason Sams433eca32010-01-06 11:57:52 -080078 rsc->checkError("SimpleMesh::renderRange 2");
Jason Samse5ffb872009-08-09 17:01:55 -070079 if (mIndexType.get()) {
Jason Samscf4c7c92009-12-14 12:57:40 -080080 mIndexBuffer->uploadCheck(rsc);
Jason Samse5ffb872009-08-09 17:01:55 -070081 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mIndexBuffer->getBufferObjectID());
Jason Sams9397e302009-08-27 20:23:34 -070082 glDrawElements(mGLPrimitive, len, GL_UNSIGNED_SHORT, (uint16_t *)(start * 2));
Jason Samse5ffb872009-08-09 17:01:55 -070083 } else {
84 glDrawArrays(mGLPrimitive, start, len);
85 }
Jason Samsd01d9702009-12-23 14:35:29 -080086
87 rsc->checkError("SimpleMesh::renderRange");
Jason Samse5ffb872009-08-09 17:01:55 -070088}
89
Jason Samsc460e552009-11-25 13:22:07 -080090void SimpleMesh::uploadAll(Context *rsc)
Jason Samsa2b54c42009-09-23 16:38:37 -070091{
92 for (uint32_t ct=0; ct < mVertexTypeCount; ct++) {
93 if (mVertexBuffers[ct].get()) {
Jason Samscf4c7c92009-12-14 12:57:40 -080094 mVertexBuffers[ct]->deferedUploadToBufferObject(rsc);
Jason Samsa2b54c42009-09-23 16:38:37 -070095 }
96 }
97 if (mIndexBuffer.get()) {
Jason Samscf4c7c92009-12-14 12:57:40 -080098 mIndexBuffer->deferedUploadToBufferObject(rsc);
Jason Samsa2b54c42009-09-23 16:38:37 -070099 }
100 if (mPrimitiveBuffer.get()) {
Jason Samscf4c7c92009-12-14 12:57:40 -0800101 mPrimitiveBuffer->deferedUploadToBufferObject(rsc);
Jason Samsa2b54c42009-09-23 16:38:37 -0700102 }
Jason Samsd01d9702009-12-23 14:35:29 -0800103 rsc->checkError("SimpleMesh::uploadAll");
Jason Samsa2b54c42009-09-23 16:38:37 -0700104}
Jason Samse5ffb872009-08-09 17:01:55 -0700105
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700106void SimpleMesh::updateGLPrimitive()
107{
108 switch(mPrimitive) {
109 case RS_PRIMITIVE_POINT: mGLPrimitive = GL_POINTS; break;
110 case RS_PRIMITIVE_LINE: mGLPrimitive = GL_LINES; break;
111 case RS_PRIMITIVE_LINE_STRIP: mGLPrimitive = GL_LINE_STRIP; break;
112 case RS_PRIMITIVE_TRIANGLE: mGLPrimitive = GL_TRIANGLES; break;
113 case RS_PRIMITIVE_TRIANGLE_STRIP: mGLPrimitive = GL_TRIANGLE_STRIP; break;
114 case RS_PRIMITIVE_TRIANGLE_FAN: mGLPrimitive = GL_TRIANGLE_FAN; break;
115 }
116}
117
118void SimpleMesh::serialize(OStream *stream) const
119{
120 // Need to identify ourselves
121 stream->addU32((uint32_t)getClassId());
122
123 String8 name(getName());
124 stream->addString(&name);
125
126 // Add primitive type
127 stream->addU8((uint8_t)mPrimitive);
128
129 // And now serialize the allocations
130 mIndexBuffer->serialize(stream);
131
132 // We need to indicate if the primitive buffer is present
133 if(mPrimitiveBuffer.get() != NULL) {
134 // Write if the primitive buffer is present
135 stream->addU32(1);
136 mPrimitiveBuffer->serialize(stream);
137 }
138 else {
139 // No buffer present, will need this when we read
140 stream->addU32(0);
141 }
142
143 // Store number of vertex streams
144 stream->addU32(mVertexTypeCount);
145 for(uint32_t vCount = 0; vCount < mVertexTypeCount; vCount ++) {
146 mVertexBuffers[vCount]->serialize(stream);
147 }
148}
149
150SimpleMesh *SimpleMesh::createFromStream(Context *rsc, IStream *stream)
151{
152 // First make sure we are reading the correct object
Alex Sakhartchoukb825f672010-06-04 10:06:50 -0700153 RsA3DClassID classID = (RsA3DClassID)stream->loadU32();
154 if(classID != RS_A3D_CLASS_ID_SIMPLE_MESH) {
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700155 LOGE("simple mesh loading skipped due to invalid class id");
156 return NULL;
157 }
158
159 SimpleMesh * mesh = new SimpleMesh(rsc);
160
161 String8 name;
162 stream->loadString(&name);
163 mesh->setName(name.string(), name.size());
164
165 mesh->mPrimitive = (RsPrimitive)stream->loadU8();
166 mesh->updateGLPrimitive();
167
168 Allocation *indexAlloc = Allocation::createFromStream(rsc, stream);
169 const Type *indexType = indexAlloc->getType();
170 mesh->mIndexBuffer.set(indexAlloc);
171 mesh->mIndexType.set(indexType);
172
173 bool isPrimitivePresent = stream->loadU32() != 0;
174 if(isPrimitivePresent) {
175 mesh->mPrimitiveBuffer.set(Allocation::createFromStream(rsc, stream));
176 mesh->mPrimitiveType.set(mesh->mPrimitiveBuffer->getType());
177 }
178
179 mesh->mVertexTypeCount = stream->loadU32();
180 if(mesh->mVertexTypeCount) {
181 mesh->mVertexTypes = new ObjectBaseRef<const Type>[mesh->mVertexTypeCount];
182 mesh->mVertexBuffers = new ObjectBaseRef<Allocation>[mesh->mVertexTypeCount];
183
184 for(uint32_t vCount = 0; vCount < mesh->mVertexTypeCount; vCount ++) {
185 Allocation *vertexAlloc = Allocation::createFromStream(rsc, stream);
186 const Type *vertexType = vertexAlloc->getType();
187 mesh->mVertexBuffers[vCount].set(vertexAlloc);
188 mesh->mVertexTypes[vCount].set(vertexType);
189 }
190 }
191
Alex Sakhartchoukb825f672010-06-04 10:06:50 -0700192 mesh->uploadAll(rsc);
193
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700194 return mesh;
195}
196
Jason Samse5ffb872009-08-09 17:01:55 -0700197
198SimpleMeshContext::SimpleMeshContext()
199{
200}
201
202SimpleMeshContext::~SimpleMeshContext()
203{
204}
205
206
207namespace android {
208namespace renderscript {
209
210
211RsSimpleMesh rsi_SimpleMeshCreate(Context *rsc, RsType prim, RsType idx, RsType *vtx, uint32_t vtxCount, uint32_t primType)
212{
Jason Samse514b452009-09-25 14:51:22 -0700213 SimpleMesh *sm = new SimpleMesh(rsc);
Jason Sams9397e302009-08-27 20:23:34 -0700214 sm->incUserRef();
Jason Samse5ffb872009-08-09 17:01:55 -0700215
216 sm->mIndexType.set((const Type *)idx);
217 sm->mPrimitiveType.set((const Type *)prim);
218
219 sm->mVertexTypeCount = vtxCount;
220 sm->mVertexTypes = new ObjectBaseRef<const Type>[vtxCount];
221 sm->mVertexBuffers = new ObjectBaseRef<Allocation>[vtxCount];
222 for (uint32_t ct=0; ct < vtxCount; ct++) {
223 sm->mVertexTypes[ct].set((const Type *)vtx[ct]);
224 }
225
226 sm->mPrimitive = (RsPrimitive)primType;
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700227 sm->updateGLPrimitive();
Jason Samse5ffb872009-08-09 17:01:55 -0700228 return sm;
229}
230
231void rsi_SimpleMeshBindVertex(Context *rsc, RsSimpleMesh mv, RsAllocation va, uint32_t slot)
232{
233 SimpleMesh *sm = static_cast<SimpleMesh *>(mv);
234 rsAssert(slot < sm->mVertexTypeCount);
235
236 sm->mVertexBuffers[slot].set((Allocation *)va);
237}
238
239void rsi_SimpleMeshBindIndex(Context *rsc, RsSimpleMesh mv, RsAllocation va)
240{
241 SimpleMesh *sm = static_cast<SimpleMesh *>(mv);
242 sm->mIndexBuffer.set((Allocation *)va);
243}
244
245void rsi_SimpleMeshBindPrimitive(Context *rsc, RsSimpleMesh mv, RsAllocation va)
246{
247 SimpleMesh *sm = static_cast<SimpleMesh *>(mv);
248 sm->mPrimitiveBuffer.set((Allocation *)va);
249}
250
Jason Samse5ffb872009-08-09 17:01:55 -0700251
252
253
254}}
255