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