• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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 
19 using namespace android;
20 using namespace android::renderscript;
21 
22 #include <GLES/gl.h>
23 #include <GLES/glext.h>
24 
SimpleMesh(Context * rsc)25 SimpleMesh::SimpleMesh(Context *rsc) : ObjectBase(rsc)
26 {
27     mAllocFile = __FILE__;
28     mAllocLine = __LINE__;
29 }
30 
~SimpleMesh()31 SimpleMesh::~SimpleMesh()
32 {
33     delete[] mVertexTypes;
34     delete[] mVertexBuffers;
35 }
36 
render() const37 void 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 
renderRange(uint32_t start,uint32_t len) const52 void 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());
74         glDrawElements(mGLPrimitive, len, GL_UNSIGNED_SHORT, (uint16_t *)(start * 2));
75     } else {
76         glDrawArrays(mGLPrimitive, start, len);
77     }
78 }
79 
uploadAll()80 void 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 }
94 
95 
SimpleMeshContext()96 SimpleMeshContext::SimpleMeshContext()
97 {
98 }
99 
~SimpleMeshContext()100 SimpleMeshContext::~SimpleMeshContext()
101 {
102 }
103 
104 
105 namespace android {
106 namespace renderscript {
107 
108 
rsi_SimpleMeshCreate(Context * rsc,RsType prim,RsType idx,RsType * vtx,uint32_t vtxCount,uint32_t primType)109 RsSimpleMesh rsi_SimpleMeshCreate(Context *rsc, RsType prim, RsType idx, RsType *vtx, uint32_t vtxCount, uint32_t primType)
110 {
111     SimpleMesh *sm = new SimpleMesh(rsc);
112     sm->incUserRef();
113 
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 
rsi_SimpleMeshBindVertex(Context * rsc,RsSimpleMesh mv,RsAllocation va,uint32_t slot)136 void 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 
rsi_SimpleMeshBindIndex(Context * rsc,RsSimpleMesh mv,RsAllocation va)144 void rsi_SimpleMeshBindIndex(Context *rsc, RsSimpleMesh mv, RsAllocation va)
145 {
146     SimpleMesh *sm = static_cast<SimpleMesh *>(mv);
147     sm->mIndexBuffer.set((Allocation *)va);
148 }
149 
rsi_SimpleMeshBindPrimitive(Context * rsc,RsSimpleMesh mv,RsAllocation va)150 void rsi_SimpleMeshBindPrimitive(Context *rsc, RsSimpleMesh mv, RsAllocation va)
151 {
152     SimpleMesh *sm = static_cast<SimpleMesh *>(mv);
153     sm->mPrimitiveBuffer.set((Allocation *)va);
154 }
155 
156 
157 
158 
159 }}
160 
161