Jason Sams | 1bada8c | 2009-08-09 17:01:55 -0700 | [diff] [blame] | 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 | |
Jason Sams | a9e7a05 | 2009-09-25 14:51:22 -0700 | [diff] [blame^] | 25 | SimpleMesh::SimpleMesh(Context *rsc) : ObjectBase(rsc) |
Jason Sams | 1bada8c | 2009-08-09 17:01:55 -0700 | [diff] [blame] | 26 | { |
| 27 | } |
| 28 | |
| 29 | SimpleMesh::~SimpleMesh() |
| 30 | { |
| 31 | } |
| 32 | |
| 33 | void 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 | |
| 48 | void 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 Sams | 07ae406 | 2009-08-27 20:23:34 -0700 | [diff] [blame] | 70 | glDrawElements(mGLPrimitive, len, GL_UNSIGNED_SHORT, (uint16_t *)(start * 2)); |
Jason Sams | 1bada8c | 2009-08-09 17:01:55 -0700 | [diff] [blame] | 71 | } else { |
| 72 | glDrawArrays(mGLPrimitive, start, len); |
| 73 | } |
| 74 | } |
| 75 | |
Jason Sams | 6b9dec0 | 2009-09-23 16:38:37 -0700 | [diff] [blame] | 76 | void 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 Sams | 1bada8c | 2009-08-09 17:01:55 -0700 | [diff] [blame] | 90 | |
| 91 | |
| 92 | SimpleMeshContext::SimpleMeshContext() |
| 93 | { |
| 94 | } |
| 95 | |
| 96 | SimpleMeshContext::~SimpleMeshContext() |
| 97 | { |
| 98 | } |
| 99 | |
| 100 | |
| 101 | namespace android { |
| 102 | namespace renderscript { |
| 103 | |
| 104 | |
| 105 | RsSimpleMesh rsi_SimpleMeshCreate(Context *rsc, RsType prim, RsType idx, RsType *vtx, uint32_t vtxCount, uint32_t primType) |
| 106 | { |
Jason Sams | a9e7a05 | 2009-09-25 14:51:22 -0700 | [diff] [blame^] | 107 | SimpleMesh *sm = new SimpleMesh(rsc); |
Jason Sams | 07ae406 | 2009-08-27 20:23:34 -0700 | [diff] [blame] | 108 | sm->incUserRef(); |
Jason Sams | 1bada8c | 2009-08-09 17:01:55 -0700 | [diff] [blame] | 109 | |
| 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 | |
| 132 | void 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 | |
| 140 | void rsi_SimpleMeshBindIndex(Context *rsc, RsSimpleMesh mv, RsAllocation va) |
| 141 | { |
| 142 | SimpleMesh *sm = static_cast<SimpleMesh *>(mv); |
| 143 | sm->mIndexBuffer.set((Allocation *)va); |
| 144 | } |
| 145 | |
| 146 | void 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 Sams | 1bada8c | 2009-08-09 17:01:55 -0700 | [diff] [blame] | 152 | |
| 153 | |
| 154 | |
| 155 | }} |
| 156 | |