blob: 53ce5cdbd444a8f21d363d1341a2c74be831cd0a [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
17#include "rsContext.h"
18
19using namespace android;
20using namespace android::renderscript;
21
22#include <GLES/gl.h>
23#include <GLES/glext.h>
24
Jason Samse514b452009-09-25 14:51:22 -070025SimpleMesh::SimpleMesh(Context *rsc) : ObjectBase(rsc)
Jason Samse5ffb872009-08-09 17:01:55 -070026{
Jason Samsf2649a92009-09-25 16:37:33 -070027 mAllocFile = __FILE__;
28 mAllocLine = __LINE__;
Jason Samse5ffb872009-08-09 17:01:55 -070029}
30
31SimpleMesh::~SimpleMesh()
32{
Jason Samsf2649a92009-09-25 16:37:33 -070033 delete[] mVertexTypes;
34 delete[] mVertexBuffers;
Jason Samse5ffb872009-08-09 17:01:55 -070035}
36
Jason Samsc460e552009-11-25 13:22:07 -080037void SimpleMesh::render(Context *rsc) const
Jason Samse5ffb872009-08-09 17:01:55 -070038{
39 if (mPrimitiveType.get()) {
Jason Samsc460e552009-11-25 13:22:07 -080040 renderRange(rsc, 0, mPrimitiveType->getDimX());
Jason Samse5ffb872009-08-09 17:01:55 -070041 return;
42 }
43
44 if (mIndexType.get()) {
Jason Samsc460e552009-11-25 13:22:07 -080045 renderRange(rsc, 0, mIndexType->getDimX());
Jason Samse5ffb872009-08-09 17:01:55 -070046 return;
47 }
48
Jason Samsc460e552009-11-25 13:22:07 -080049 renderRange(rsc, 0, mVertexTypes[0]->getDimX());
Jason Samse5ffb872009-08-09 17:01:55 -070050}
51
Jason Samsc460e552009-11-25 13:22:07 -080052void SimpleMesh::renderRange(Context *rsc, uint32_t start, uint32_t len) const
Jason Samse5ffb872009-08-09 17:01:55 -070053{
54 if (len < 1) {
55 return;
56 }
57
Jason Sams433eca32010-01-06 11:57:52 -080058 rsc->checkError("SimpleMesh::renderRange 1");
Jason Samsc460e552009-11-25 13:22:07 -080059 VertexArray va;
Jason Samsc460e552009-11-25 13:22:07 -080060 if (rsc->checkVersion2_0()) {
Jason Sams433eca32010-01-06 11:57:52 -080061 for (uint32_t ct=0; ct < mVertexTypeCount; ct++) {
62 mVertexBuffers[ct]->uploadCheck(rsc);
63 va.setActiveBuffer(mVertexBuffers[ct]->getBufferObjectID());
64 mVertexTypes[ct]->enableGLVertexBuffer2(&va);
65 }
Jason Sams3eb28f02010-01-27 14:41:43 -080066 va.setupGL2(rsc, &rsc->mStateVertexArray, &rsc->mShaderCache);
Jason Samsc460e552009-11-25 13:22:07 -080067 } else {
Jason Sams433eca32010-01-06 11:57:52 -080068 for (uint32_t ct=0; ct < mVertexTypeCount; ct++) {
69 mVertexBuffers[ct]->uploadCheck(rsc);
70 va.setActiveBuffer(mVertexBuffers[ct]->getBufferObjectID());
71 mVertexTypes[ct]->enableGLVertexBuffer(&va);
72 }
Jason Samsd01d9702009-12-23 14:35:29 -080073 va.setupGL(rsc, 0);
Jason Samse5ffb872009-08-09 17:01:55 -070074 }
75
Jason Sams433eca32010-01-06 11:57:52 -080076 rsc->checkError("SimpleMesh::renderRange 2");
Jason Samse5ffb872009-08-09 17:01:55 -070077 if (mIndexType.get()) {
Jason Samscf4c7c92009-12-14 12:57:40 -080078 mIndexBuffer->uploadCheck(rsc);
Jason Samse5ffb872009-08-09 17:01:55 -070079 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mIndexBuffer->getBufferObjectID());
Jason Sams9397e302009-08-27 20:23:34 -070080 glDrawElements(mGLPrimitive, len, GL_UNSIGNED_SHORT, (uint16_t *)(start * 2));
Jason Samse5ffb872009-08-09 17:01:55 -070081 } else {
82 glDrawArrays(mGLPrimitive, start, len);
83 }
Jason Samsd01d9702009-12-23 14:35:29 -080084
85 rsc->checkError("SimpleMesh::renderRange");
Jason Samse5ffb872009-08-09 17:01:55 -070086}
87
Jason Samsc460e552009-11-25 13:22:07 -080088void SimpleMesh::uploadAll(Context *rsc)
Jason Samsa2b54c42009-09-23 16:38:37 -070089{
90 for (uint32_t ct=0; ct < mVertexTypeCount; ct++) {
91 if (mVertexBuffers[ct].get()) {
Jason Samscf4c7c92009-12-14 12:57:40 -080092 mVertexBuffers[ct]->deferedUploadToBufferObject(rsc);
Jason Samsa2b54c42009-09-23 16:38:37 -070093 }
94 }
95 if (mIndexBuffer.get()) {
Jason Samscf4c7c92009-12-14 12:57:40 -080096 mIndexBuffer->deferedUploadToBufferObject(rsc);
Jason Samsa2b54c42009-09-23 16:38:37 -070097 }
98 if (mPrimitiveBuffer.get()) {
Jason Samscf4c7c92009-12-14 12:57:40 -080099 mPrimitiveBuffer->deferedUploadToBufferObject(rsc);
Jason Samsa2b54c42009-09-23 16:38:37 -0700100 }
Jason Samsd01d9702009-12-23 14:35:29 -0800101 rsc->checkError("SimpleMesh::uploadAll");
Jason Samsa2b54c42009-09-23 16:38:37 -0700102}
Jason Samse5ffb872009-08-09 17:01:55 -0700103
104
105SimpleMeshContext::SimpleMeshContext()
106{
107}
108
109SimpleMeshContext::~SimpleMeshContext()
110{
111}
112
113
114namespace android {
115namespace renderscript {
116
117
118RsSimpleMesh rsi_SimpleMeshCreate(Context *rsc, RsType prim, RsType idx, RsType *vtx, uint32_t vtxCount, uint32_t primType)
119{
Jason Samse514b452009-09-25 14:51:22 -0700120 SimpleMesh *sm = new SimpleMesh(rsc);
Jason Sams9397e302009-08-27 20:23:34 -0700121 sm->incUserRef();
Jason Samse5ffb872009-08-09 17:01:55 -0700122
123 sm->mIndexType.set((const Type *)idx);
124 sm->mPrimitiveType.set((const Type *)prim);
125
126 sm->mVertexTypeCount = vtxCount;
127 sm->mVertexTypes = new ObjectBaseRef<const Type>[vtxCount];
128 sm->mVertexBuffers = new ObjectBaseRef<Allocation>[vtxCount];
129 for (uint32_t ct=0; ct < vtxCount; ct++) {
130 sm->mVertexTypes[ct].set((const Type *)vtx[ct]);
131 }
132
133 sm->mPrimitive = (RsPrimitive)primType;
134 switch(sm->mPrimitive) {
135 case RS_PRIMITIVE_POINT: sm->mGLPrimitive = GL_POINTS; break;
136 case RS_PRIMITIVE_LINE: sm->mGLPrimitive = GL_LINES; break;
137 case RS_PRIMITIVE_LINE_STRIP: sm->mGLPrimitive = GL_LINE_STRIP; break;
138 case RS_PRIMITIVE_TRIANGLE: sm->mGLPrimitive = GL_TRIANGLES; break;
139 case RS_PRIMITIVE_TRIANGLE_STRIP: sm->mGLPrimitive = GL_TRIANGLE_STRIP; break;
140 case RS_PRIMITIVE_TRIANGLE_FAN: sm->mGLPrimitive = GL_TRIANGLE_FAN; break;
141 }
142 return sm;
143}
144
145void rsi_SimpleMeshBindVertex(Context *rsc, RsSimpleMesh mv, RsAllocation va, uint32_t slot)
146{
147 SimpleMesh *sm = static_cast<SimpleMesh *>(mv);
148 rsAssert(slot < sm->mVertexTypeCount);
149
150 sm->mVertexBuffers[slot].set((Allocation *)va);
151}
152
153void rsi_SimpleMeshBindIndex(Context *rsc, RsSimpleMesh mv, RsAllocation va)
154{
155 SimpleMesh *sm = static_cast<SimpleMesh *>(mv);
156 sm->mIndexBuffer.set((Allocation *)va);
157}
158
159void rsi_SimpleMeshBindPrimitive(Context *rsc, RsSimpleMesh mv, RsAllocation va)
160{
161 SimpleMesh *sm = static_cast<SimpleMesh *>(mv);
162 sm->mPrimitiveBuffer.set((Allocation *)va);
163}
164
Jason Samse5ffb872009-08-09 17:01:55 -0700165
166
167
168}}
169