blob: b082fd7db26b97fe2cbaa94cc0bba4ab0fe602b1 [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
37void SimpleMesh::render() const
38{
39 if (mPrimitiveType.get()) {
40 renderRange(0, mPrimitiveType->getDimX());
41 return;
42 }
43
44 if (mIndexType.get()) {
45 renderRange(0, mIndexType->getDimX());
46 return;
47 }
48
49 renderRange(0, mVertexTypes[0]->getDimX());
50}
51
52void SimpleMesh::renderRange(uint32_t start, uint32_t len) const
53{
54 if (len < 1) {
55 return;
56 }
57
58 glDisableClientState(GL_VERTEX_ARRAY);
59 glDisableClientState(GL_NORMAL_ARRAY);
60 glDisableClientState(GL_COLOR_ARRAY);
61 for (uint32_t ct=0; ct < RS_MAX_TEXTURE; ct++) {
62 glClientActiveTexture(GL_TEXTURE0 + ct);
63 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
64 }
65 glClientActiveTexture(GL_TEXTURE0);
66
67 for (uint32_t ct=0; ct < mVertexTypeCount; ct++) {
68 glBindBuffer(GL_ARRAY_BUFFER, mVertexBuffers[ct]->getBufferObjectID());
69 mVertexTypes[ct]->enableGLVertexBuffer();
70 }
71
72 if (mIndexType.get()) {
73 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mIndexBuffer->getBufferObjectID());
Jason Sams9397e302009-08-27 20:23:34 -070074 glDrawElements(mGLPrimitive, len, GL_UNSIGNED_SHORT, (uint16_t *)(start * 2));
Jason Samse5ffb872009-08-09 17:01:55 -070075 } else {
76 glDrawArrays(mGLPrimitive, start, len);
77 }
78}
79
Jason Samsa2b54c42009-09-23 16:38:37 -070080void SimpleMesh::uploadAll()
81{
82 for (uint32_t ct=0; ct < mVertexTypeCount; ct++) {
83 if (mVertexBuffers[ct].get()) {
84 mVertexBuffers[ct]->uploadToBufferObject();
85 }
86 }
87 if (mIndexBuffer.get()) {
88 mIndexBuffer->uploadToBufferObject();
89 }
90 if (mPrimitiveBuffer.get()) {
91 mPrimitiveBuffer->uploadToBufferObject();
92 }
93}
Jason Samse5ffb872009-08-09 17:01:55 -070094
95
96SimpleMeshContext::SimpleMeshContext()
97{
98}
99
100SimpleMeshContext::~SimpleMeshContext()
101{
102}
103
104
105namespace android {
106namespace renderscript {
107
108
109RsSimpleMesh rsi_SimpleMeshCreate(Context *rsc, RsType prim, RsType idx, RsType *vtx, uint32_t vtxCount, uint32_t primType)
110{
Jason Samse514b452009-09-25 14:51:22 -0700111 SimpleMesh *sm = new SimpleMesh(rsc);
Jason Sams9397e302009-08-27 20:23:34 -0700112 sm->incUserRef();
Jason Samse5ffb872009-08-09 17:01:55 -0700113
114 sm->mIndexType.set((const Type *)idx);
115 sm->mPrimitiveType.set((const Type *)prim);
116
117 sm->mVertexTypeCount = vtxCount;
118 sm->mVertexTypes = new ObjectBaseRef<const Type>[vtxCount];
119 sm->mVertexBuffers = new ObjectBaseRef<Allocation>[vtxCount];
120 for (uint32_t ct=0; ct < vtxCount; ct++) {
121 sm->mVertexTypes[ct].set((const Type *)vtx[ct]);
122 }
123
124 sm->mPrimitive = (RsPrimitive)primType;
125 switch(sm->mPrimitive) {
126 case RS_PRIMITIVE_POINT: sm->mGLPrimitive = GL_POINTS; break;
127 case RS_PRIMITIVE_LINE: sm->mGLPrimitive = GL_LINES; break;
128 case RS_PRIMITIVE_LINE_STRIP: sm->mGLPrimitive = GL_LINE_STRIP; break;
129 case RS_PRIMITIVE_TRIANGLE: sm->mGLPrimitive = GL_TRIANGLES; break;
130 case RS_PRIMITIVE_TRIANGLE_STRIP: sm->mGLPrimitive = GL_TRIANGLE_STRIP; break;
131 case RS_PRIMITIVE_TRIANGLE_FAN: sm->mGLPrimitive = GL_TRIANGLE_FAN; break;
132 }
133 return sm;
134}
135
136void rsi_SimpleMeshBindVertex(Context *rsc, RsSimpleMesh mv, RsAllocation va, uint32_t slot)
137{
138 SimpleMesh *sm = static_cast<SimpleMesh *>(mv);
139 rsAssert(slot < sm->mVertexTypeCount);
140
141 sm->mVertexBuffers[slot].set((Allocation *)va);
142}
143
144void rsi_SimpleMeshBindIndex(Context *rsc, RsSimpleMesh mv, RsAllocation va)
145{
146 SimpleMesh *sm = static_cast<SimpleMesh *>(mv);
147 sm->mIndexBuffer.set((Allocation *)va);
148}
149
150void rsi_SimpleMeshBindPrimitive(Context *rsc, RsSimpleMesh mv, RsAllocation va)
151{
152 SimpleMesh *sm = static_cast<SimpleMesh *>(mv);
153 sm->mPrimitiveBuffer.set((Allocation *)va);
154}
155
Jason Samse5ffb872009-08-09 17:01:55 -0700156
157
158
159}}
160