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 | { |
Jason Sams | 61f08d6 | 2009-09-25 16:37:33 -0700 | [diff] [blame] | 27 | mAllocFile = __FILE__; |
| 28 | mAllocLine = __LINE__; |
Jason Sams | 1bada8c | 2009-08-09 17:01:55 -0700 | [diff] [blame] | 29 | } |
| 30 | |
| 31 | SimpleMesh::~SimpleMesh() |
| 32 | { |
Jason Sams | 61f08d6 | 2009-09-25 16:37:33 -0700 | [diff] [blame] | 33 | delete[] mVertexTypes; |
| 34 | delete[] mVertexBuffers; |
Jason Sams | 1bada8c | 2009-08-09 17:01:55 -0700 | [diff] [blame] | 35 | } |
| 36 | |
Jason Sams | bb51c40 | 2009-11-25 13:22:07 -0800 | [diff] [blame] | 37 | void SimpleMesh::render(Context *rsc) const |
Jason Sams | 1bada8c | 2009-08-09 17:01:55 -0700 | [diff] [blame] | 38 | { |
| 39 | if (mPrimitiveType.get()) { |
Jason Sams | bb51c40 | 2009-11-25 13:22:07 -0800 | [diff] [blame] | 40 | renderRange(rsc, 0, mPrimitiveType->getDimX()); |
Jason Sams | 1bada8c | 2009-08-09 17:01:55 -0700 | [diff] [blame] | 41 | return; |
| 42 | } |
| 43 | |
| 44 | if (mIndexType.get()) { |
Jason Sams | bb51c40 | 2009-11-25 13:22:07 -0800 | [diff] [blame] | 45 | renderRange(rsc, 0, mIndexType->getDimX()); |
Jason Sams | 1bada8c | 2009-08-09 17:01:55 -0700 | [diff] [blame] | 46 | return; |
| 47 | } |
| 48 | |
Jason Sams | bb51c40 | 2009-11-25 13:22:07 -0800 | [diff] [blame] | 49 | renderRange(rsc, 0, mVertexTypes[0]->getDimX()); |
Jason Sams | 1bada8c | 2009-08-09 17:01:55 -0700 | [diff] [blame] | 50 | } |
| 51 | |
Jason Sams | bb51c40 | 2009-11-25 13:22:07 -0800 | [diff] [blame] | 52 | void SimpleMesh::renderRange(Context *rsc, uint32_t start, uint32_t len) const |
Jason Sams | 1bada8c | 2009-08-09 17:01:55 -0700 | [diff] [blame] | 53 | { |
| 54 | if (len < 1) { |
| 55 | return; |
| 56 | } |
| 57 | |
Jason Sams | bb51c40 | 2009-11-25 13:22:07 -0800 | [diff] [blame] | 58 | VertexArray va; |
Jason Sams | 1bada8c | 2009-08-09 17:01:55 -0700 | [diff] [blame] | 59 | for (uint32_t ct=0; ct < mVertexTypeCount; ct++) { |
Jason Sams | 3b7d39b | 2009-12-14 12:57:40 -0800 | [diff] [blame^] | 60 | mVertexBuffers[ct]->uploadCheck(rsc); |
Jason Sams | bb51c40 | 2009-11-25 13:22:07 -0800 | [diff] [blame] | 61 | 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 Sams | 1bada8c | 2009-08-09 17:01:55 -0700 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | if (mIndexType.get()) { |
Jason Sams | 3b7d39b | 2009-12-14 12:57:40 -0800 | [diff] [blame^] | 71 | mIndexBuffer->uploadCheck(rsc); |
Jason Sams | 1bada8c | 2009-08-09 17:01:55 -0700 | [diff] [blame] | 72 | glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mIndexBuffer->getBufferObjectID()); |
Jason Sams | 07ae406 | 2009-08-27 20:23:34 -0700 | [diff] [blame] | 73 | glDrawElements(mGLPrimitive, len, GL_UNSIGNED_SHORT, (uint16_t *)(start * 2)); |
Jason Sams | 1bada8c | 2009-08-09 17:01:55 -0700 | [diff] [blame] | 74 | } else { |
| 75 | glDrawArrays(mGLPrimitive, start, len); |
| 76 | } |
| 77 | } |
| 78 | |
Jason Sams | bb51c40 | 2009-11-25 13:22:07 -0800 | [diff] [blame] | 79 | void SimpleMesh::uploadAll(Context *rsc) |
Jason Sams | 6b9dec0 | 2009-09-23 16:38:37 -0700 | [diff] [blame] | 80 | { |
| 81 | for (uint32_t ct=0; ct < mVertexTypeCount; ct++) { |
| 82 | if (mVertexBuffers[ct].get()) { |
Jason Sams | 3b7d39b | 2009-12-14 12:57:40 -0800 | [diff] [blame^] | 83 | mVertexBuffers[ct]->deferedUploadToBufferObject(rsc); |
Jason Sams | 6b9dec0 | 2009-09-23 16:38:37 -0700 | [diff] [blame] | 84 | } |
| 85 | } |
| 86 | if (mIndexBuffer.get()) { |
Jason Sams | 3b7d39b | 2009-12-14 12:57:40 -0800 | [diff] [blame^] | 87 | mIndexBuffer->deferedUploadToBufferObject(rsc); |
Jason Sams | 6b9dec0 | 2009-09-23 16:38:37 -0700 | [diff] [blame] | 88 | } |
| 89 | if (mPrimitiveBuffer.get()) { |
Jason Sams | 3b7d39b | 2009-12-14 12:57:40 -0800 | [diff] [blame^] | 90 | mPrimitiveBuffer->deferedUploadToBufferObject(rsc); |
Jason Sams | 6b9dec0 | 2009-09-23 16:38:37 -0700 | [diff] [blame] | 91 | } |
| 92 | } |
Jason Sams | 1bada8c | 2009-08-09 17:01:55 -0700 | [diff] [blame] | 93 | |
| 94 | |
| 95 | SimpleMeshContext::SimpleMeshContext() |
| 96 | { |
| 97 | } |
| 98 | |
| 99 | SimpleMeshContext::~SimpleMeshContext() |
| 100 | { |
| 101 | } |
| 102 | |
| 103 | |
| 104 | namespace android { |
| 105 | namespace renderscript { |
| 106 | |
| 107 | |
| 108 | RsSimpleMesh rsi_SimpleMeshCreate(Context *rsc, RsType prim, RsType idx, RsType *vtx, uint32_t vtxCount, uint32_t primType) |
| 109 | { |
Jason Sams | a9e7a05 | 2009-09-25 14:51:22 -0700 | [diff] [blame] | 110 | SimpleMesh *sm = new SimpleMesh(rsc); |
Jason Sams | 07ae406 | 2009-08-27 20:23:34 -0700 | [diff] [blame] | 111 | sm->incUserRef(); |
Jason Sams | 1bada8c | 2009-08-09 17:01:55 -0700 | [diff] [blame] | 112 | |
| 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 | |
| 135 | void 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 | |
| 143 | void rsi_SimpleMeshBindIndex(Context *rsc, RsSimpleMesh mv, RsAllocation va) |
| 144 | { |
| 145 | SimpleMesh *sm = static_cast<SimpleMesh *>(mv); |
| 146 | sm->mIndexBuffer.set((Allocation *)va); |
| 147 | } |
| 148 | |
| 149 | void 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 Sams | 1bada8c | 2009-08-09 17:01:55 -0700 | [diff] [blame] | 155 | |
| 156 | |
| 157 | |
| 158 | }} |
| 159 | |