blob: e5c2eb5afa1940b9df8b619dcd90f741795a03a6 [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 Sams8cb39de2010-06-01 15:47:01 -070071 for (uint32_t ct=0; ct < mVertexTypeCount; ct++) {
72 mVertexBuffers[ct]->uploadCheck(rsc);
Jason Samseeeaccc2010-06-25 12:45:41 -070073 if (mVertexBuffers[ct]->getIsBufferObject()) {
74 va.setActiveBuffer(mVertexBuffers[ct]->getBufferObjectID());
75 } else {
76 va.setActiveBuffer(mVertexBuffers[ct]->getPtr());
77 }
Jason Sams8cb39de2010-06-01 15:47:01 -070078 mVertexTypes[ct]->enableGLVertexBuffer(&va);
Jason Sams1bada8c2009-08-09 17:01:55 -070079 }
Jason Sams8cb39de2010-06-01 15:47:01 -070080 va.setupGL2(rsc, &rsc->mStateVertexArray, &rsc->mShaderCache);
Jason Sams1bada8c2009-08-09 17:01:55 -070081
Jason Samsa09a6e12010-01-06 11:57:52 -080082 rsc->checkError("SimpleMesh::renderRange 2");
Jason Sams1bada8c2009-08-09 17:01:55 -070083 if (mIndexType.get()) {
Jason Sams3b7d39b2009-12-14 12:57:40 -080084 mIndexBuffer->uploadCheck(rsc);
Jason Sams1bada8c2009-08-09 17:01:55 -070085 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mIndexBuffer->getBufferObjectID());
Jason Sams07ae4062009-08-27 20:23:34 -070086 glDrawElements(mGLPrimitive, len, GL_UNSIGNED_SHORT, (uint16_t *)(start * 2));
Jason Sams1bada8c2009-08-09 17:01:55 -070087 } else {
88 glDrawArrays(mGLPrimitive, start, len);
89 }
Jason Sams718cd1f2009-12-23 14:35:29 -080090
91 rsc->checkError("SimpleMesh::renderRange");
Jason Sams1bada8c2009-08-09 17:01:55 -070092}
93
Jason Samsbb51c402009-11-25 13:22:07 -080094void SimpleMesh::uploadAll(Context *rsc)
Jason Sams6b9dec02009-09-23 16:38:37 -070095{
96 for (uint32_t ct=0; ct < mVertexTypeCount; ct++) {
97 if (mVertexBuffers[ct].get()) {
Jason Sams3b7d39b2009-12-14 12:57:40 -080098 mVertexBuffers[ct]->deferedUploadToBufferObject(rsc);
Jason Sams6b9dec02009-09-23 16:38:37 -070099 }
100 }
101 if (mIndexBuffer.get()) {
Jason Sams3b7d39b2009-12-14 12:57:40 -0800102 mIndexBuffer->deferedUploadToBufferObject(rsc);
Jason Sams6b9dec02009-09-23 16:38:37 -0700103 }
104 if (mPrimitiveBuffer.get()) {
Jason Sams3b7d39b2009-12-14 12:57:40 -0800105 mPrimitiveBuffer->deferedUploadToBufferObject(rsc);
Jason Sams6b9dec02009-09-23 16:38:37 -0700106 }
Jason Sams718cd1f2009-12-23 14:35:29 -0800107 rsc->checkError("SimpleMesh::uploadAll");
Jason Sams6b9dec02009-09-23 16:38:37 -0700108}
Jason Sams1bada8c2009-08-09 17:01:55 -0700109
Alex Sakhartchoukaa7d2882010-05-21 12:53:13 -0700110void SimpleMesh::updateGLPrimitive()
111{
112 switch(mPrimitive) {
113 case RS_PRIMITIVE_POINT: mGLPrimitive = GL_POINTS; break;
114 case RS_PRIMITIVE_LINE: mGLPrimitive = GL_LINES; break;
115 case RS_PRIMITIVE_LINE_STRIP: mGLPrimitive = GL_LINE_STRIP; break;
116 case RS_PRIMITIVE_TRIANGLE: mGLPrimitive = GL_TRIANGLES; break;
117 case RS_PRIMITIVE_TRIANGLE_STRIP: mGLPrimitive = GL_TRIANGLE_STRIP; break;
118 case RS_PRIMITIVE_TRIANGLE_FAN: mGLPrimitive = GL_TRIANGLE_FAN; break;
119 }
120}
121
122void SimpleMesh::serialize(OStream *stream) const
123{
124 // Need to identify ourselves
125 stream->addU32((uint32_t)getClassId());
126
127 String8 name(getName());
128 stream->addString(&name);
129
130 // Add primitive type
131 stream->addU8((uint8_t)mPrimitive);
132
133 // And now serialize the allocations
134 mIndexBuffer->serialize(stream);
135
136 // We need to indicate if the primitive buffer is present
137 if(mPrimitiveBuffer.get() != NULL) {
138 // Write if the primitive buffer is present
139 stream->addU32(1);
140 mPrimitiveBuffer->serialize(stream);
141 }
142 else {
143 // No buffer present, will need this when we read
144 stream->addU32(0);
145 }
146
147 // Store number of vertex streams
148 stream->addU32(mVertexTypeCount);
149 for(uint32_t vCount = 0; vCount < mVertexTypeCount; vCount ++) {
150 mVertexBuffers[vCount]->serialize(stream);
151 }
152}
153
154SimpleMesh *SimpleMesh::createFromStream(Context *rsc, IStream *stream)
155{
156 // First make sure we are reading the correct object
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -0700157 RsA3DClassID classID = (RsA3DClassID)stream->loadU32();
158 if(classID != RS_A3D_CLASS_ID_SIMPLE_MESH) {
Alex Sakhartchoukaa7d2882010-05-21 12:53:13 -0700159 LOGE("simple mesh loading skipped due to invalid class id");
160 return NULL;
161 }
162
163 SimpleMesh * mesh = new SimpleMesh(rsc);
164
165 String8 name;
166 stream->loadString(&name);
167 mesh->setName(name.string(), name.size());
168
169 mesh->mPrimitive = (RsPrimitive)stream->loadU8();
170 mesh->updateGLPrimitive();
171
172 Allocation *indexAlloc = Allocation::createFromStream(rsc, stream);
173 const Type *indexType = indexAlloc->getType();
174 mesh->mIndexBuffer.set(indexAlloc);
175 mesh->mIndexType.set(indexType);
176
177 bool isPrimitivePresent = stream->loadU32() != 0;
178 if(isPrimitivePresent) {
179 mesh->mPrimitiveBuffer.set(Allocation::createFromStream(rsc, stream));
180 mesh->mPrimitiveType.set(mesh->mPrimitiveBuffer->getType());
181 }
182
183 mesh->mVertexTypeCount = stream->loadU32();
184 if(mesh->mVertexTypeCount) {
185 mesh->mVertexTypes = new ObjectBaseRef<const Type>[mesh->mVertexTypeCount];
186 mesh->mVertexBuffers = new ObjectBaseRef<Allocation>[mesh->mVertexTypeCount];
187
188 for(uint32_t vCount = 0; vCount < mesh->mVertexTypeCount; vCount ++) {
189 Allocation *vertexAlloc = Allocation::createFromStream(rsc, stream);
190 const Type *vertexType = vertexAlloc->getType();
191 mesh->mVertexBuffers[vCount].set(vertexAlloc);
192 mesh->mVertexTypes[vCount].set(vertexType);
193 }
194 }
195
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -0700196 mesh->uploadAll(rsc);
197
Alex Sakhartchoukaa7d2882010-05-21 12:53:13 -0700198 return mesh;
199}
200
Jason Sams1bada8c2009-08-09 17:01:55 -0700201
202SimpleMeshContext::SimpleMeshContext()
203{
204}
205
206SimpleMeshContext::~SimpleMeshContext()
207{
208}
209
210
211namespace android {
212namespace renderscript {
213
214
215RsSimpleMesh rsi_SimpleMeshCreate(Context *rsc, RsType prim, RsType idx, RsType *vtx, uint32_t vtxCount, uint32_t primType)
216{
Jason Samsa9e7a052009-09-25 14:51:22 -0700217 SimpleMesh *sm = new SimpleMesh(rsc);
Jason Sams07ae4062009-08-27 20:23:34 -0700218 sm->incUserRef();
Jason Sams1bada8c2009-08-09 17:01:55 -0700219
220 sm->mIndexType.set((const Type *)idx);
221 sm->mPrimitiveType.set((const Type *)prim);
222
223 sm->mVertexTypeCount = vtxCount;
224 sm->mVertexTypes = new ObjectBaseRef<const Type>[vtxCount];
225 sm->mVertexBuffers = new ObjectBaseRef<Allocation>[vtxCount];
226 for (uint32_t ct=0; ct < vtxCount; ct++) {
227 sm->mVertexTypes[ct].set((const Type *)vtx[ct]);
228 }
229
230 sm->mPrimitive = (RsPrimitive)primType;
Alex Sakhartchoukaa7d2882010-05-21 12:53:13 -0700231 sm->updateGLPrimitive();
Jason Sams1bada8c2009-08-09 17:01:55 -0700232 return sm;
233}
234
235void rsi_SimpleMeshBindVertex(Context *rsc, RsSimpleMesh mv, RsAllocation va, uint32_t slot)
236{
237 SimpleMesh *sm = static_cast<SimpleMesh *>(mv);
238 rsAssert(slot < sm->mVertexTypeCount);
239
240 sm->mVertexBuffers[slot].set((Allocation *)va);
241}
242
243void rsi_SimpleMeshBindIndex(Context *rsc, RsSimpleMesh mv, RsAllocation va)
244{
245 SimpleMesh *sm = static_cast<SimpleMesh *>(mv);
246 sm->mIndexBuffer.set((Allocation *)va);
247}
248
249void rsi_SimpleMeshBindPrimitive(Context *rsc, RsSimpleMesh mv, RsAllocation va)
250{
251 SimpleMesh *sm = static_cast<SimpleMesh *>(mv);
252 sm->mPrimitiveBuffer.set((Allocation *)va);
253}
254
Jason Sams1bada8c2009-08-09 17:01:55 -0700255
256
257
258}}
259