blob: edfe9679c65ed825c98c9117ddb07bab15e6ee3a [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 Samsbb51c402009-11-25 13:22:07 -080060 va.setActiveBuffer(mVertexBuffers[ct]->getBufferObjectID());
61 mVertexTypes[ct]->enableGLVertexBuffer(&va);
62 }
63 if (rsc->checkVersion2_0()) {
64 va.setupGL2(0, &rsc->mShaderCache);
65 } else {
66 va.setupGL(0);
Jason Sams1bada8c2009-08-09 17:01:55 -070067 }
68
69 if (mIndexType.get()) {
70 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mIndexBuffer->getBufferObjectID());
Jason Sams07ae4062009-08-27 20:23:34 -070071 glDrawElements(mGLPrimitive, len, GL_UNSIGNED_SHORT, (uint16_t *)(start * 2));
Jason Sams1bada8c2009-08-09 17:01:55 -070072 } else {
73 glDrawArrays(mGLPrimitive, start, len);
74 }
75}
76
Jason Samsbb51c402009-11-25 13:22:07 -080077void SimpleMesh::uploadAll(Context *rsc)
Jason Sams6b9dec02009-09-23 16:38:37 -070078{
79 for (uint32_t ct=0; ct < mVertexTypeCount; ct++) {
80 if (mVertexBuffers[ct].get()) {
81 mVertexBuffers[ct]->uploadToBufferObject();
82 }
83 }
84 if (mIndexBuffer.get()) {
85 mIndexBuffer->uploadToBufferObject();
86 }
87 if (mPrimitiveBuffer.get()) {
88 mPrimitiveBuffer->uploadToBufferObject();
89 }
90}
Jason Sams1bada8c2009-08-09 17:01:55 -070091
92
93SimpleMeshContext::SimpleMeshContext()
94{
95}
96
97SimpleMeshContext::~SimpleMeshContext()
98{
99}
100
101
102namespace android {
103namespace renderscript {
104
105
106RsSimpleMesh rsi_SimpleMeshCreate(Context *rsc, RsType prim, RsType idx, RsType *vtx, uint32_t vtxCount, uint32_t primType)
107{
Jason Samsa9e7a052009-09-25 14:51:22 -0700108 SimpleMesh *sm = new SimpleMesh(rsc);
Jason Sams07ae4062009-08-27 20:23:34 -0700109 sm->incUserRef();
Jason Sams1bada8c2009-08-09 17:01:55 -0700110
111 sm->mIndexType.set((const Type *)idx);
112 sm->mPrimitiveType.set((const Type *)prim);
113
114 sm->mVertexTypeCount = vtxCount;
115 sm->mVertexTypes = new ObjectBaseRef<const Type>[vtxCount];
116 sm->mVertexBuffers = new ObjectBaseRef<Allocation>[vtxCount];
117 for (uint32_t ct=0; ct < vtxCount; ct++) {
118 sm->mVertexTypes[ct].set((const Type *)vtx[ct]);
119 }
120
121 sm->mPrimitive = (RsPrimitive)primType;
122 switch(sm->mPrimitive) {
123 case RS_PRIMITIVE_POINT: sm->mGLPrimitive = GL_POINTS; break;
124 case RS_PRIMITIVE_LINE: sm->mGLPrimitive = GL_LINES; break;
125 case RS_PRIMITIVE_LINE_STRIP: sm->mGLPrimitive = GL_LINE_STRIP; break;
126 case RS_PRIMITIVE_TRIANGLE: sm->mGLPrimitive = GL_TRIANGLES; break;
127 case RS_PRIMITIVE_TRIANGLE_STRIP: sm->mGLPrimitive = GL_TRIANGLE_STRIP; break;
128 case RS_PRIMITIVE_TRIANGLE_FAN: sm->mGLPrimitive = GL_TRIANGLE_FAN; break;
129 }
130 return sm;
131}
132
133void rsi_SimpleMeshBindVertex(Context *rsc, RsSimpleMesh mv, RsAllocation va, uint32_t slot)
134{
135 SimpleMesh *sm = static_cast<SimpleMesh *>(mv);
136 rsAssert(slot < sm->mVertexTypeCount);
137
138 sm->mVertexBuffers[slot].set((Allocation *)va);
139}
140
141void rsi_SimpleMeshBindIndex(Context *rsc, RsSimpleMesh mv, RsAllocation va)
142{
143 SimpleMesh *sm = static_cast<SimpleMesh *>(mv);
144 sm->mIndexBuffer.set((Allocation *)va);
145}
146
147void rsi_SimpleMeshBindPrimitive(Context *rsc, RsSimpleMesh mv, RsAllocation va)
148{
149 SimpleMesh *sm = static_cast<SimpleMesh *>(mv);
150 sm->mPrimitiveBuffer.set((Allocation *)va);
151}
152
Jason Sams1bada8c2009-08-09 17:01:55 -0700153
154
155
156}}
157