Jason Sams | d19f10d | 2009-05-22 14:03:28 -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 | #include "rsProgramVertex.h" |
| 19 | |
Jason Sams | 4b962e5 | 2009-06-22 17:15:15 -0700 | [diff] [blame] | 20 | #include <GLES/gl.h> |
| 21 | #include <GLES/glext.h> |
Jason Sams | bb51c40 | 2009-11-25 13:22:07 -0800 | [diff] [blame] | 22 | #include <GLES2/gl2.h> |
| 23 | #include <GLES2/gl2ext.h> |
Jason Sams | 4b962e5 | 2009-06-22 17:15:15 -0700 | [diff] [blame] | 24 | |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 25 | using namespace android; |
| 26 | using namespace android::renderscript; |
| 27 | |
| 28 | |
Jason Sams | a9e7a05 | 2009-09-25 14:51:22 -0700 | [diff] [blame] | 29 | ProgramVertex::ProgramVertex(Context *rsc, Element *in, Element *out) : |
| 30 | Program(rsc, in, out) |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 31 | { |
Jason Sams | 61f08d6 | 2009-09-25 16:37:33 -0700 | [diff] [blame] | 32 | mAllocFile = __FILE__; |
| 33 | mAllocLine = __LINE__; |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 34 | mTextureMatrixEnable = false; |
Jason Sams | ee41112 | 2009-07-21 12:20:54 -0700 | [diff] [blame] | 35 | mLightCount = 0; |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 36 | } |
| 37 | |
| 38 | ProgramVertex::~ProgramVertex() |
| 39 | { |
| 40 | } |
| 41 | |
Jason Sams | b37c0a5 | 2009-06-16 17:49:58 -0700 | [diff] [blame] | 42 | static void logMatrix(const char *txt, const float *f) |
| 43 | { |
Jason Sams | f29ca50 | 2009-06-23 12:22:47 -0700 | [diff] [blame] | 44 | LOGV("Matrix %s, %p", txt, f); |
| 45 | LOGV("%6.2f, %6.2f, %6.2f, %6.2f", f[0], f[4], f[8], f[12]); |
| 46 | LOGV("%6.2f, %6.2f, %6.2f, %6.2f", f[1], f[5], f[9], f[13]); |
| 47 | LOGV("%6.2f, %6.2f, %6.2f, %6.2f", f[2], f[6], f[10], f[14]); |
| 48 | LOGV("%6.2f, %6.2f, %6.2f, %6.2f", f[3], f[7], f[11], f[15]); |
Jason Sams | b37c0a5 | 2009-06-16 17:49:58 -0700 | [diff] [blame] | 49 | } |
| 50 | |
Jason Sams | b13ada5 | 2009-08-25 11:34:49 -0700 | [diff] [blame] | 51 | void ProgramVertex::setupGL(const Context *rsc, ProgramVertexState *state) |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 52 | { |
Jason Sams | 9bee51c | 2009-08-05 13:57:03 -0700 | [diff] [blame] | 53 | if ((state->mLast.get() == this) && !mDirty) { |
| 54 | return; |
| 55 | } |
| 56 | state->mLast.set(this); |
| 57 | |
| 58 | const float *f = static_cast<const float *>(mConstants->getPtr()); |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 59 | |
| 60 | glMatrixMode(GL_TEXTURE); |
| 61 | if (mTextureMatrixEnable) { |
| 62 | glLoadMatrixf(&f[RS_PROGRAM_VERTEX_TEXTURE_OFFSET]); |
| 63 | } else { |
| 64 | glLoadIdentity(); |
| 65 | } |
| 66 | |
Jason Sams | ee41112 | 2009-07-21 12:20:54 -0700 | [diff] [blame] | 67 | glMatrixMode(GL_MODELVIEW); |
| 68 | glLoadIdentity(); |
| 69 | if (mLightCount) { |
Romain Guy | b62627e | 2009-08-06 22:52:13 -0700 | [diff] [blame] | 70 | int v = 0; |
Jason Sams | ee41112 | 2009-07-21 12:20:54 -0700 | [diff] [blame] | 71 | glEnable(GL_LIGHTING); |
| 72 | glLightModelxv(GL_LIGHT_MODEL_TWO_SIDE, &v); |
| 73 | for (uint32_t ct = 0; ct < mLightCount; ct++) { |
| 74 | const Light *l = mLights[ct].get(); |
| 75 | glEnable(GL_LIGHT0 + ct); |
| 76 | l->setupGL(ct); |
| 77 | } |
| 78 | for (uint32_t ct = mLightCount; ct < MAX_LIGHTS; ct++) { |
| 79 | glDisable(GL_LIGHT0 + ct); |
| 80 | } |
| 81 | } else { |
| 82 | glDisable(GL_LIGHTING); |
| 83 | } |
Jason Sams | 9bee51c | 2009-08-05 13:57:03 -0700 | [diff] [blame] | 84 | |
Jason Sams | ee41112 | 2009-07-21 12:20:54 -0700 | [diff] [blame] | 85 | if (!f) { |
| 86 | LOGE("Must bind constants to vertex program"); |
| 87 | } |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 88 | |
| 89 | glMatrixMode(GL_PROJECTION); |
Jason Sams | b37c0a5 | 2009-06-16 17:49:58 -0700 | [diff] [blame] | 90 | glLoadMatrixf(&f[RS_PROGRAM_VERTEX_PROJECTION_OFFSET]); |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 91 | glMatrixMode(GL_MODELVIEW); |
Jason Sams | b37c0a5 | 2009-06-16 17:49:58 -0700 | [diff] [blame] | 92 | glLoadMatrixf(&f[RS_PROGRAM_VERTEX_MODELVIEW_OFFSET]); |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 93 | |
Jason Sams | 9bee51c | 2009-08-05 13:57:03 -0700 | [diff] [blame] | 94 | mDirty = false; |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 95 | } |
| 96 | |
Jason Sams | bb51c40 | 2009-11-25 13:22:07 -0800 | [diff] [blame] | 97 | void ProgramVertex::loadShader() { |
| 98 | Program::loadShader(GL_VERTEX_SHADER); |
| 99 | } |
| 100 | |
| 101 | void ProgramVertex::createShader() |
| 102 | { |
| 103 | mShader.setTo(""); |
| 104 | |
| 105 | for (uint32_t ct=0; ct < mAttribCount; ct++) { |
| 106 | mShader.append("attribute vec4 "); |
| 107 | mShader.append(mAttribNames[ct]); |
| 108 | mShader.append(";\n"); |
| 109 | } |
| 110 | |
| 111 | for (uint32_t ct=0; ct < mUniformCount; ct++) { |
| 112 | mShader.append("uniform mat4 "); |
| 113 | mShader.append(mUniformNames[ct]); |
| 114 | mShader.append(";\n"); |
| 115 | } |
| 116 | |
| 117 | mShader.append("varying vec4 varColor;\n"); |
| 118 | mShader.append("varying vec4 varTex0;\n"); |
| 119 | |
Jason Sams | 54c0ec1 | 2009-11-30 14:49:55 -0800 | [diff] [blame^] | 120 | if (mUserShader.length() > 1) { |
| 121 | mShader.append(mUserShader); |
Jason Sams | bb51c40 | 2009-11-25 13:22:07 -0800 | [diff] [blame] | 122 | } else { |
Jason Sams | 54c0ec1 | 2009-11-30 14:49:55 -0800 | [diff] [blame^] | 123 | mShader.append("void main() {\n"); |
| 124 | mShader.append(" gl_Position = uni_MVP * attrib_Position;\n"); |
| 125 | mShader.append(" gl_PointSize = attrib_PointSize.x;\n"); |
| 126 | |
| 127 | mShader.append(" varColor = attrib_Color;\n"); |
| 128 | if (mTextureMatrixEnable) { |
| 129 | mShader.append(" varTex0 = uni_TexMatrix * attrib_T0;\n"); |
| 130 | } else { |
| 131 | mShader.append(" varTex0 = attrib_T0;\n"); |
| 132 | } |
| 133 | //mShader.append(" pos.x = pos.x / 480.0;\n"); |
| 134 | //mShader.append(" pos.y = pos.y / 800.0;\n"); |
| 135 | //mShader.append(" gl_Position = pos;\n"); |
| 136 | mShader.append("}\n"); |
Jason Sams | bb51c40 | 2009-11-25 13:22:07 -0800 | [diff] [blame] | 137 | } |
Jason Sams | bb51c40 | 2009-11-25 13:22:07 -0800 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | void ProgramVertex::setupGL2(const Context *rsc, ProgramVertexState *state, ShaderCache *sc) |
| 141 | { |
| 142 | //LOGE("sgl2 vtx1 %x", glGetError()); |
| 143 | if ((state->mLast.get() == this) && !mDirty) { |
| 144 | //return; |
| 145 | } |
| 146 | |
| 147 | const float *f = static_cast<const float *>(mConstants->getPtr()); |
| 148 | |
| 149 | Matrix mvp; |
| 150 | mvp.load(&f[RS_PROGRAM_VERTEX_PROJECTION_OFFSET]); |
| 151 | Matrix t; |
| 152 | t.load(&f[RS_PROGRAM_VERTEX_MODELVIEW_OFFSET]); |
| 153 | mvp.multiply(&t); |
| 154 | |
| 155 | glUniformMatrix4fv(sc->vtxUniformSlot(0), 1, GL_FALSE, mvp.m); |
| 156 | if (mTextureMatrixEnable) { |
| 157 | glUniformMatrix4fv(sc->vtxUniformSlot(1), 1, GL_FALSE, |
| 158 | &f[RS_PROGRAM_VERTEX_TEXTURE_OFFSET]); |
| 159 | } |
| 160 | |
| 161 | state->mLast.set(this); |
| 162 | //LOGE("sgl2 vtx2 %x", glGetError()); |
| 163 | } |
| 164 | |
Jason Sams | ee41112 | 2009-07-21 12:20:54 -0700 | [diff] [blame] | 165 | void ProgramVertex::addLight(const Light *l) |
| 166 | { |
| 167 | if (mLightCount < MAX_LIGHTS) { |
| 168 | mLights[mLightCount].set(l); |
| 169 | mLightCount++; |
| 170 | } |
| 171 | } |
| 172 | |
Jason Sams | b0ec1b4 | 2009-07-28 12:02:16 -0700 | [diff] [blame] | 173 | void ProgramVertex::setProjectionMatrix(const rsc_Matrix *m) const |
| 174 | { |
Jason Sams | 9bee51c | 2009-08-05 13:57:03 -0700 | [diff] [blame] | 175 | float *f = static_cast<float *>(mConstants->getPtr()); |
Jason Sams | b0ec1b4 | 2009-07-28 12:02:16 -0700 | [diff] [blame] | 176 | memcpy(&f[RS_PROGRAM_VERTEX_PROJECTION_OFFSET], m, sizeof(rsc_Matrix)); |
Jason Sams | 9bee51c | 2009-08-05 13:57:03 -0700 | [diff] [blame] | 177 | mDirty = true; |
Jason Sams | b0ec1b4 | 2009-07-28 12:02:16 -0700 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | void ProgramVertex::setModelviewMatrix(const rsc_Matrix *m) const |
| 181 | { |
Jason Sams | 9bee51c | 2009-08-05 13:57:03 -0700 | [diff] [blame] | 182 | float *f = static_cast<float *>(mConstants->getPtr()); |
Jason Sams | b0ec1b4 | 2009-07-28 12:02:16 -0700 | [diff] [blame] | 183 | memcpy(&f[RS_PROGRAM_VERTEX_MODELVIEW_OFFSET], m, sizeof(rsc_Matrix)); |
Jason Sams | 9bee51c | 2009-08-05 13:57:03 -0700 | [diff] [blame] | 184 | mDirty = true; |
Jason Sams | b0ec1b4 | 2009-07-28 12:02:16 -0700 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | void ProgramVertex::setTextureMatrix(const rsc_Matrix *m) const |
| 188 | { |
Jason Sams | 9bee51c | 2009-08-05 13:57:03 -0700 | [diff] [blame] | 189 | float *f = static_cast<float *>(mConstants->getPtr()); |
Jason Sams | b0ec1b4 | 2009-07-28 12:02:16 -0700 | [diff] [blame] | 190 | memcpy(&f[RS_PROGRAM_VERTEX_TEXTURE_OFFSET], m, sizeof(rsc_Matrix)); |
Jason Sams | 9bee51c | 2009-08-05 13:57:03 -0700 | [diff] [blame] | 191 | mDirty = true; |
Jason Sams | b0ec1b4 | 2009-07-28 12:02:16 -0700 | [diff] [blame] | 192 | } |
| 193 | |
Jason Sams | e9ad9a7 | 2009-09-30 17:36:20 -0700 | [diff] [blame] | 194 | void ProgramVertex::transformToScreen(const Context *rsc, float *v4out, const float *v3in) const |
| 195 | { |
| 196 | float *f = static_cast<float *>(mConstants->getPtr()); |
| 197 | Matrix mvp; |
| 198 | mvp.loadMultiply((Matrix *)&f[RS_PROGRAM_VERTEX_MODELVIEW_OFFSET], |
| 199 | (Matrix *)&f[RS_PROGRAM_VERTEX_PROJECTION_OFFSET]); |
| 200 | mvp.vectorMultiply(v4out, v3in); |
| 201 | } |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 202 | |
Jason Sams | bb51c40 | 2009-11-25 13:22:07 -0800 | [diff] [blame] | 203 | void ProgramVertex::init(Context *rsc) |
| 204 | { |
| 205 | mAttribCount = 6; |
| 206 | mAttribNames[VertexArray::POSITION].setTo("attrib_Position"); |
| 207 | mAttribNames[VertexArray::COLOR].setTo("attrib_Color"); |
| 208 | mAttribNames[VertexArray::NORMAL].setTo("attrib_Normal"); |
| 209 | mAttribNames[VertexArray::POINT_SIZE].setTo("attrib_PointSize"); |
| 210 | mAttribNames[VertexArray::TEXTURE_0].setTo("attrib_T0"); |
| 211 | mAttribNames[VertexArray::TEXTURE_1].setTo("attrib_T1"); |
| 212 | |
| 213 | mUniformCount = 2; |
| 214 | mUniformNames[0].setTo("uni_MVP"); |
| 215 | mUniformNames[1].setTo("uni_TexMatrix"); |
| 216 | |
| 217 | createShader(); |
| 218 | } |
| 219 | |
Jason Sams | bb51c40 | 2009-11-25 13:22:07 -0800 | [diff] [blame] | 220 | /////////////////////////////////////////////////////////////////////// |
| 221 | |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 222 | ProgramVertexState::ProgramVertexState() |
| 223 | { |
| 224 | mPV = NULL; |
| 225 | } |
| 226 | |
| 227 | ProgramVertexState::~ProgramVertexState() |
| 228 | { |
| 229 | delete mPV; |
| 230 | } |
| 231 | |
Jason Sams | 9c54bdb | 2009-06-17 16:52:59 -0700 | [diff] [blame] | 232 | void ProgramVertexState::init(Context *rsc, int32_t w, int32_t h) |
| 233 | { |
Jason Sams | b6f2b13 | 2009-09-24 12:33:45 -0700 | [diff] [blame] | 234 | rsi_ElementBegin(rsc); |
| 235 | rsi_ElementAdd(rsc, RS_KIND_USER, RS_TYPE_FLOAT, false, 32, NULL); |
| 236 | RsElement e = rsi_ElementCreate(rsc); |
| 237 | |
| 238 | rsi_TypeBegin(rsc, e); |
| 239 | rsi_TypeAdd(rsc, RS_DIMENSION_X, 48); |
Jason Sams | 61f08d6 | 2009-09-25 16:37:33 -0700 | [diff] [blame] | 240 | mAllocType.set((Type *)rsi_TypeCreate(rsc)); |
Jason Sams | b6f2b13 | 2009-09-24 12:33:45 -0700 | [diff] [blame] | 241 | |
Jason Sams | a9e7a05 | 2009-09-25 14:51:22 -0700 | [diff] [blame] | 242 | ProgramVertex *pv = new ProgramVertex(rsc, NULL, NULL); |
Jason Sams | 61f08d6 | 2009-09-25 16:37:33 -0700 | [diff] [blame] | 243 | Allocation *alloc = (Allocation *)rsi_AllocationCreateTyped(rsc, mAllocType.get()); |
Jason Sams | 9c54bdb | 2009-06-17 16:52:59 -0700 | [diff] [blame] | 244 | mDefaultAlloc.set(alloc); |
| 245 | mDefault.set(pv); |
Jason Sams | bb51c40 | 2009-11-25 13:22:07 -0800 | [diff] [blame] | 246 | pv->init(rsc); |
Jason Sams | 9bee51c | 2009-08-05 13:57:03 -0700 | [diff] [blame] | 247 | pv->bindAllocation(alloc); |
| 248 | |
Jason Sams | eb4b031 | 2009-11-12 16:09:45 -0800 | [diff] [blame] | 249 | updateSize(rsc, w, h); |
| 250 | } |
| 251 | |
| 252 | void ProgramVertexState::updateSize(Context *rsc, int32_t w, int32_t h) |
| 253 | { |
Jason Sams | 9c54bdb | 2009-06-17 16:52:59 -0700 | [diff] [blame] | 254 | Matrix m; |
| 255 | m.loadOrtho(0,w, h,0, -1,1); |
Jason Sams | eb4b031 | 2009-11-12 16:09:45 -0800 | [diff] [blame] | 256 | mDefaultAlloc->subData(RS_PROGRAM_VERTEX_PROJECTION_OFFSET, 16, &m.m[0], 16*4); |
Jason Sams | 9c54bdb | 2009-06-17 16:52:59 -0700 | [diff] [blame] | 257 | |
| 258 | m.loadIdentity(); |
Jason Sams | eb4b031 | 2009-11-12 16:09:45 -0800 | [diff] [blame] | 259 | mDefaultAlloc->subData(RS_PROGRAM_VERTEX_MODELVIEW_OFFSET, 16, &m.m[0], 16*4); |
Jason Sams | 9c54bdb | 2009-06-17 16:52:59 -0700 | [diff] [blame] | 260 | } |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 261 | |
Jason Sams | 61f08d6 | 2009-09-25 16:37:33 -0700 | [diff] [blame] | 262 | void ProgramVertexState::deinit(Context *rsc) |
| 263 | { |
| 264 | mDefaultAlloc.clear(); |
| 265 | mDefault.clear(); |
| 266 | mAllocType.clear(); |
| 267 | mLast.clear(); |
| 268 | delete mPV; |
| 269 | mPV = NULL; |
| 270 | } |
| 271 | |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 272 | |
| 273 | namespace android { |
| 274 | namespace renderscript { |
| 275 | |
| 276 | void rsi_ProgramVertexBegin(Context *rsc, RsElement in, RsElement out) |
| 277 | { |
| 278 | delete rsc->mStateVertex.mPV; |
Jason Sams | a9e7a05 | 2009-09-25 14:51:22 -0700 | [diff] [blame] | 279 | rsc->mStateVertex.mPV = new ProgramVertex(rsc, (Element *)in, (Element *)out); |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 280 | } |
| 281 | |
| 282 | RsProgramVertex rsi_ProgramVertexCreate(Context *rsc) |
| 283 | { |
| 284 | ProgramVertex *pv = rsc->mStateVertex.mPV; |
Jason Sams | 07ae406 | 2009-08-27 20:23:34 -0700 | [diff] [blame] | 285 | pv->incUserRef(); |
Jason Sams | bb51c40 | 2009-11-25 13:22:07 -0800 | [diff] [blame] | 286 | pv->init(rsc); |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 287 | rsc->mStateVertex.mPV = 0; |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 288 | return pv; |
| 289 | } |
| 290 | |
Jason Sams | 9bee51c | 2009-08-05 13:57:03 -0700 | [diff] [blame] | 291 | void rsi_ProgramVertexBindAllocation(Context *rsc, RsProgramVertex vpgm, RsAllocation constants) |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 292 | { |
| 293 | ProgramVertex *pv = static_cast<ProgramVertex *>(vpgm); |
Jason Sams | 9bee51c | 2009-08-05 13:57:03 -0700 | [diff] [blame] | 294 | pv->bindAllocation(static_cast<Allocation *>(constants)); |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 295 | } |
| 296 | |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 297 | void rsi_ProgramVertexSetTextureMatrixEnable(Context *rsc, bool enable) |
| 298 | { |
| 299 | rsc->mStateVertex.mPV->setTextureMatrixEnable(enable); |
| 300 | } |
| 301 | |
Jason Sams | 54c0ec1 | 2009-11-30 14:49:55 -0800 | [diff] [blame^] | 302 | void rsi_ProgramVertexSetShader(Context *rsc, const char *txt, uint32_t len) |
| 303 | { |
| 304 | rsc->mStateVertex.mPV->setShader(txt, len); |
| 305 | } |
| 306 | |
Jason Sams | ee41112 | 2009-07-21 12:20:54 -0700 | [diff] [blame] | 307 | void rsi_ProgramVertexAddLight(Context *rsc, RsLight light) |
| 308 | { |
| 309 | rsc->mStateVertex.mPV->addLight(static_cast<const Light *>(light)); |
| 310 | } |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 311 | |
| 312 | |
| 313 | } |
| 314 | } |