blob: f7d14a545993cd35dcbd3e52e61b14c54dd94c8d [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()) {
65 va.setupGL2(0, &rsc->mShaderCache);
66 } else {
67 va.setupGL(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 }
77}
78
Jason Samsbb51c402009-11-25 13:22:07 -080079void SimpleMesh::uploadAll(Context *rsc)
Jason Sams6b9dec02009-09-23 16:38:37 -070080{
81 for (uint32_t ct=0; ct < mVertexTypeCount; ct++) {
82 if (mVertexBuffers[ct].get()) {
Jason Sams3b7d39b2009-12-14 12:57:40 -080083 mVertexBuffers[ct]->deferedUploadToBufferObject(rsc);
Jason Sams6b9dec02009-09-23 16:38:37 -070084 }
85 }
86 if (mIndexBuffer.get()) {
Jason Sams3b7d39b2009-12-14 12:57:40 -080087 mIndexBuffer->deferedUploadToBufferObject(rsc);
Jason Sams6b9dec02009-09-23 16:38:37 -070088 }
89 if (mPrimitiveBuffer.get()) {
Jason Sams3b7d39b2009-12-14 12:57:40 -080090 mPrimitiveBuffer->deferedUploadToBufferObject(rsc);
Jason Sams6b9dec02009-09-23 16:38:37 -070091 }
92}
Jason Sams1bada8c2009-08-09 17:01:55 -070093
94
95SimpleMeshContext::SimpleMeshContext()
96{
97}
98
99SimpleMeshContext::~SimpleMeshContext()
100{
101}
102
103
104namespace android {
105namespace renderscript {
106
107
108RsSimpleMesh rsi_SimpleMeshCreate(Context *rsc, RsType prim, RsType idx, RsType *vtx, uint32_t vtxCount, uint32_t primType)
109{
Jason Samsa9e7a052009-09-25 14:51:22 -0700110 SimpleMesh *sm = new SimpleMesh(rsc);
Jason Sams07ae4062009-08-27 20:23:34 -0700111 sm->incUserRef();
Jason Sams1bada8c2009-08-09 17:01:55 -0700112
113 sm->mIndexType.set((const Type *)idx);
114 sm->mPrimitiveType.set((const Type *)prim);
115
116 sm->mVertexTypeCount = vtxCount;
117 sm->mVertexTypes = new ObjectBaseRef<const Type>[vtxCount];
118 sm->mVertexBuffers = new ObjectBaseRef<Allocation>[vtxCount];
119 for (uint32_t ct=0; ct < vtxCount; ct++) {
120 sm->mVertexTypes[ct].set((const Type *)vtx[ct]);
121 }
122
123 sm->mPrimitive = (RsPrimitive)primType;
124 switch(sm->mPrimitive) {
125 case RS_PRIMITIVE_POINT: sm->mGLPrimitive = GL_POINTS; break;
126 case RS_PRIMITIVE_LINE: sm->mGLPrimitive = GL_LINES; break;
127 case RS_PRIMITIVE_LINE_STRIP: sm->mGLPrimitive = GL_LINE_STRIP; break;
128 case RS_PRIMITIVE_TRIANGLE: sm->mGLPrimitive = GL_TRIANGLES; break;
129 case RS_PRIMITIVE_TRIANGLE_STRIP: sm->mGLPrimitive = GL_TRIANGLE_STRIP; break;
130 case RS_PRIMITIVE_TRIANGLE_FAN: sm->mGLPrimitive = GL_TRIANGLE_FAN; break;
131 }
132 return sm;
133}
134
135void rsi_SimpleMeshBindVertex(Context *rsc, RsSimpleMesh mv, RsAllocation va, uint32_t slot)
136{
137 SimpleMesh *sm = static_cast<SimpleMesh *>(mv);
138 rsAssert(slot < sm->mVertexTypeCount);
139
140 sm->mVertexBuffers[slot].set((Allocation *)va);
141}
142
143void rsi_SimpleMeshBindIndex(Context *rsc, RsSimpleMesh mv, RsAllocation va)
144{
145 SimpleMesh *sm = static_cast<SimpleMesh *>(mv);
146 sm->mIndexBuffer.set((Allocation *)va);
147}
148
149void rsi_SimpleMeshBindPrimitive(Context *rsc, RsSimpleMesh mv, RsAllocation va)
150{
151 SimpleMesh *sm = static_cast<SimpleMesh *>(mv);
152 sm->mPrimitiveBuffer.set((Allocation *)va);
153}
154
Jason Sams1bada8c2009-08-09 17:01:55 -0700155
156
157
158}}
159