blob: 5f5622d52fd5f329c739da170c8192fb4b76d02e [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
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 Samsa9e7a052009-09-25 14:51:22 -070025SimpleMesh::SimpleMesh(Context *rsc) : ObjectBase(rsc)
Jason Sams1bada8c2009-08-09 17:01:55 -070026{
Jason Sams61f08d62009-09-25 16:37:33 -070027 mAllocFile = __FILE__;
28 mAllocLine = __LINE__;
Jason Sams1bada8c2009-08-09 17:01:55 -070029}
30
31SimpleMesh::~SimpleMesh()
32{
Jason Sams61f08d62009-09-25 16:37:33 -070033 delete[] mVertexTypes;
34 delete[] mVertexBuffers;
Jason Sams1bada8c2009-08-09 17:01:55 -070035}
36
Jason Samsbb51c402009-11-25 13:22:07 -080037void SimpleMesh::render(Context *rsc) const
Jason Sams1bada8c2009-08-09 17:01:55 -070038{
39 if (mPrimitiveType.get()) {
Jason Samsbb51c402009-11-25 13:22:07 -080040 renderRange(rsc, 0, mPrimitiveType->getDimX());
Jason Sams1bada8c2009-08-09 17:01:55 -070041 return;
42 }
43
44 if (mIndexType.get()) {
Jason Samsbb51c402009-11-25 13:22:07 -080045 renderRange(rsc, 0, mIndexType->getDimX());
Jason Sams1bada8c2009-08-09 17:01:55 -070046 return;
47 }
48
Jason Samsbb51c402009-11-25 13:22:07 -080049 renderRange(rsc, 0, mVertexTypes[0]->getDimX());
Jason Sams1bada8c2009-08-09 17:01:55 -070050}
51
Jason Samsbb51c402009-11-25 13:22:07 -080052void SimpleMesh::renderRange(Context *rsc, uint32_t start, uint32_t len) const
Jason Sams1bada8c2009-08-09 17:01:55 -070053{
54 if (len < 1) {
55 return;
56 }
57
Jason Samsbb51c402009-11-25 13:22:07 -080058 VertexArray va;
Jason Sams1bada8c2009-08-09 17:01:55 -070059 for (uint32_t ct=0; ct < mVertexTypeCount; ct++) {
Jason Sams3b7d39b2009-12-14 12:57:40 -080060 mVertexBuffers[ct]->uploadCheck(rsc);
Jason Samsbb51c402009-11-25 13:22:07 -080061 va.setActiveBuffer(mVertexBuffers[ct]->getBufferObjectID());
62 mVertexTypes[ct]->enableGLVertexBuffer(&va);
63 }
64 if (rsc->checkVersion2_0()) {
Jason Sams718cd1f2009-12-23 14:35:29 -080065 va.setupGL2(rsc, 0, &rsc->mShaderCache);
Jason Samsbb51c402009-11-25 13:22:07 -080066 } else {
Jason Sams718cd1f2009-12-23 14:35:29 -080067 va.setupGL(rsc, 0);
Jason Sams1bada8c2009-08-09 17:01:55 -070068 }
69
70 if (mIndexType.get()) {
Jason Sams3b7d39b2009-12-14 12:57:40 -080071 mIndexBuffer->uploadCheck(rsc);
Jason Sams1bada8c2009-08-09 17:01:55 -070072 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mIndexBuffer->getBufferObjectID());
Jason Sams07ae4062009-08-27 20:23:34 -070073 glDrawElements(mGLPrimitive, len, GL_UNSIGNED_SHORT, (uint16_t *)(start * 2));
Jason Sams1bada8c2009-08-09 17:01:55 -070074 } else {
75 glDrawArrays(mGLPrimitive, start, len);
76 }
Jason Sams718cd1f2009-12-23 14:35:29 -080077
78 rsc->checkError("SimpleMesh::renderRange");
Jason Sams1bada8c2009-08-09 17:01:55 -070079}
80
Jason Samsbb51c402009-11-25 13:22:07 -080081void SimpleMesh::uploadAll(Context *rsc)
Jason Sams6b9dec02009-09-23 16:38:37 -070082{
83 for (uint32_t ct=0; ct < mVertexTypeCount; ct++) {
84 if (mVertexBuffers[ct].get()) {
Jason Sams3b7d39b2009-12-14 12:57:40 -080085 mVertexBuffers[ct]->deferedUploadToBufferObject(rsc);
Jason Sams6b9dec02009-09-23 16:38:37 -070086 }
87 }
88 if (mIndexBuffer.get()) {
Jason Sams3b7d39b2009-12-14 12:57:40 -080089 mIndexBuffer->deferedUploadToBufferObject(rsc);
Jason Sams6b9dec02009-09-23 16:38:37 -070090 }
91 if (mPrimitiveBuffer.get()) {
Jason Sams3b7d39b2009-12-14 12:57:40 -080092 mPrimitiveBuffer->deferedUploadToBufferObject(rsc);
Jason Sams6b9dec02009-09-23 16:38:37 -070093 }
Jason Sams718cd1f2009-12-23 14:35:29 -080094 rsc->checkError("SimpleMesh::uploadAll");
Jason Sams6b9dec02009-09-23 16:38:37 -070095}
Jason Sams1bada8c2009-08-09 17:01:55 -070096
97
98SimpleMeshContext::SimpleMeshContext()
99{
100}
101
102SimpleMeshContext::~SimpleMeshContext()
103{
104}
105
106
107namespace android {
108namespace renderscript {
109
110
111RsSimpleMesh rsi_SimpleMeshCreate(Context *rsc, RsType prim, RsType idx, RsType *vtx, uint32_t vtxCount, uint32_t primType)
112{
Jason Samsa9e7a052009-09-25 14:51:22 -0700113 SimpleMesh *sm = new SimpleMesh(rsc);
Jason Sams07ae4062009-08-27 20:23:34 -0700114 sm->incUserRef();
Jason Sams1bada8c2009-08-09 17:01:55 -0700115
116 sm->mIndexType.set((const Type *)idx);
117 sm->mPrimitiveType.set((const Type *)prim);
118
119 sm->mVertexTypeCount = vtxCount;
120 sm->mVertexTypes = new ObjectBaseRef<const Type>[vtxCount];
121 sm->mVertexBuffers = new ObjectBaseRef<Allocation>[vtxCount];
122 for (uint32_t ct=0; ct < vtxCount; ct++) {
123 sm->mVertexTypes[ct].set((const Type *)vtx[ct]);
124 }
125
126 sm->mPrimitive = (RsPrimitive)primType;
127 switch(sm->mPrimitive) {
128 case RS_PRIMITIVE_POINT: sm->mGLPrimitive = GL_POINTS; break;
129 case RS_PRIMITIVE_LINE: sm->mGLPrimitive = GL_LINES; break;
130 case RS_PRIMITIVE_LINE_STRIP: sm->mGLPrimitive = GL_LINE_STRIP; break;
131 case RS_PRIMITIVE_TRIANGLE: sm->mGLPrimitive = GL_TRIANGLES; break;
132 case RS_PRIMITIVE_TRIANGLE_STRIP: sm->mGLPrimitive = GL_TRIANGLE_STRIP; break;
133 case RS_PRIMITIVE_TRIANGLE_FAN: sm->mGLPrimitive = GL_TRIANGLE_FAN; break;
134 }
135 return sm;
136}
137
138void rsi_SimpleMeshBindVertex(Context *rsc, RsSimpleMesh mv, RsAllocation va, uint32_t slot)
139{
140 SimpleMesh *sm = static_cast<SimpleMesh *>(mv);
141 rsAssert(slot < sm->mVertexTypeCount);
142
143 sm->mVertexBuffers[slot].set((Allocation *)va);
144}
145
146void rsi_SimpleMeshBindIndex(Context *rsc, RsSimpleMesh mv, RsAllocation va)
147{
148 SimpleMesh *sm = static_cast<SimpleMesh *>(mv);
149 sm->mIndexBuffer.set((Allocation *)va);
150}
151
152void rsi_SimpleMeshBindPrimitive(Context *rsc, RsSimpleMesh mv, RsAllocation va)
153{
154 SimpleMesh *sm = static_cast<SimpleMesh *>(mv);
155 sm->mPrimitiveBuffer.set((Allocation *)va);
156}
157
Jason Sams1bada8c2009-08-09 17:01:55 -0700158
159
160
161}}
162