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> |
| 22 | |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 23 | using namespace android; |
| 24 | using namespace android::renderscript; |
| 25 | |
| 26 | |
Jason Sams | a9e7a05 | 2009-09-25 14:51:22 -0700 | [diff] [blame] | 27 | ProgramVertex::ProgramVertex(Context *rsc, Element *in, Element *out) : |
| 28 | Program(rsc, in, out) |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 29 | { |
Jason Sams | 61f08d6 | 2009-09-25 16:37:33 -0700 | [diff] [blame] | 30 | mAllocFile = __FILE__; |
| 31 | mAllocLine = __LINE__; |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 32 | mTextureMatrixEnable = false; |
Jason Sams | ee41112 | 2009-07-21 12:20:54 -0700 | [diff] [blame] | 33 | mLightCount = 0; |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 34 | } |
| 35 | |
| 36 | ProgramVertex::~ProgramVertex() |
| 37 | { |
| 38 | } |
| 39 | |
Jason Sams | b37c0a5 | 2009-06-16 17:49:58 -0700 | [diff] [blame] | 40 | static void logMatrix(const char *txt, const float *f) |
| 41 | { |
Jason Sams | f29ca50 | 2009-06-23 12:22:47 -0700 | [diff] [blame] | 42 | LOGV("Matrix %s, %p", txt, f); |
| 43 | LOGV("%6.2f, %6.2f, %6.2f, %6.2f", f[0], f[4], f[8], f[12]); |
| 44 | LOGV("%6.2f, %6.2f, %6.2f, %6.2f", f[1], f[5], f[9], f[13]); |
| 45 | LOGV("%6.2f, %6.2f, %6.2f, %6.2f", f[2], f[6], f[10], f[14]); |
| 46 | 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] | 47 | } |
| 48 | |
Jason Sams | b13ada5 | 2009-08-25 11:34:49 -0700 | [diff] [blame] | 49 | void ProgramVertex::setupGL(const Context *rsc, ProgramVertexState *state) |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 50 | { |
Jason Sams | 9bee51c | 2009-08-05 13:57:03 -0700 | [diff] [blame] | 51 | if ((state->mLast.get() == this) && !mDirty) { |
| 52 | return; |
| 53 | } |
| 54 | state->mLast.set(this); |
| 55 | |
| 56 | const float *f = static_cast<const float *>(mConstants->getPtr()); |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 57 | |
| 58 | glMatrixMode(GL_TEXTURE); |
| 59 | if (mTextureMatrixEnable) { |
| 60 | glLoadMatrixf(&f[RS_PROGRAM_VERTEX_TEXTURE_OFFSET]); |
| 61 | } else { |
| 62 | glLoadIdentity(); |
| 63 | } |
| 64 | |
Jason Sams | ee41112 | 2009-07-21 12:20:54 -0700 | [diff] [blame] | 65 | glMatrixMode(GL_MODELVIEW); |
| 66 | glLoadIdentity(); |
| 67 | if (mLightCount) { |
Romain Guy | b62627e | 2009-08-06 22:52:13 -0700 | [diff] [blame] | 68 | int v = 0; |
Jason Sams | ee41112 | 2009-07-21 12:20:54 -0700 | [diff] [blame] | 69 | glEnable(GL_LIGHTING); |
| 70 | glLightModelxv(GL_LIGHT_MODEL_TWO_SIDE, &v); |
| 71 | for (uint32_t ct = 0; ct < mLightCount; ct++) { |
| 72 | const Light *l = mLights[ct].get(); |
| 73 | glEnable(GL_LIGHT0 + ct); |
| 74 | l->setupGL(ct); |
| 75 | } |
| 76 | for (uint32_t ct = mLightCount; ct < MAX_LIGHTS; ct++) { |
| 77 | glDisable(GL_LIGHT0 + ct); |
| 78 | } |
| 79 | } else { |
| 80 | glDisable(GL_LIGHTING); |
| 81 | } |
Jason Sams | 9bee51c | 2009-08-05 13:57:03 -0700 | [diff] [blame] | 82 | |
Jason Sams | ee41112 | 2009-07-21 12:20:54 -0700 | [diff] [blame] | 83 | if (!f) { |
| 84 | LOGE("Must bind constants to vertex program"); |
| 85 | } |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 86 | |
| 87 | glMatrixMode(GL_PROJECTION); |
Jason Sams | b37c0a5 | 2009-06-16 17:49:58 -0700 | [diff] [blame] | 88 | glLoadMatrixf(&f[RS_PROGRAM_VERTEX_PROJECTION_OFFSET]); |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 89 | glMatrixMode(GL_MODELVIEW); |
Jason Sams | b37c0a5 | 2009-06-16 17:49:58 -0700 | [diff] [blame] | 90 | glLoadMatrixf(&f[RS_PROGRAM_VERTEX_MODELVIEW_OFFSET]); |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 91 | |
Jason Sams | 9bee51c | 2009-08-05 13:57:03 -0700 | [diff] [blame] | 92 | mDirty = false; |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 93 | } |
| 94 | |
Jason Sams | ee41112 | 2009-07-21 12:20:54 -0700 | [diff] [blame] | 95 | void ProgramVertex::addLight(const Light *l) |
| 96 | { |
| 97 | if (mLightCount < MAX_LIGHTS) { |
| 98 | mLights[mLightCount].set(l); |
| 99 | mLightCount++; |
| 100 | } |
| 101 | } |
| 102 | |
Jason Sams | b0ec1b4 | 2009-07-28 12:02:16 -0700 | [diff] [blame] | 103 | void ProgramVertex::setProjectionMatrix(const rsc_Matrix *m) const |
| 104 | { |
Jason Sams | 9bee51c | 2009-08-05 13:57:03 -0700 | [diff] [blame] | 105 | float *f = static_cast<float *>(mConstants->getPtr()); |
Jason Sams | b0ec1b4 | 2009-07-28 12:02:16 -0700 | [diff] [blame] | 106 | memcpy(&f[RS_PROGRAM_VERTEX_PROJECTION_OFFSET], m, sizeof(rsc_Matrix)); |
Jason Sams | 9bee51c | 2009-08-05 13:57:03 -0700 | [diff] [blame] | 107 | mDirty = true; |
Jason Sams | b0ec1b4 | 2009-07-28 12:02:16 -0700 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | void ProgramVertex::setModelviewMatrix(const rsc_Matrix *m) const |
| 111 | { |
Jason Sams | 9bee51c | 2009-08-05 13:57:03 -0700 | [diff] [blame] | 112 | float *f = static_cast<float *>(mConstants->getPtr()); |
Jason Sams | b0ec1b4 | 2009-07-28 12:02:16 -0700 | [diff] [blame] | 113 | memcpy(&f[RS_PROGRAM_VERTEX_MODELVIEW_OFFSET], m, sizeof(rsc_Matrix)); |
Jason Sams | 9bee51c | 2009-08-05 13:57:03 -0700 | [diff] [blame] | 114 | mDirty = true; |
Jason Sams | b0ec1b4 | 2009-07-28 12:02:16 -0700 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | void ProgramVertex::setTextureMatrix(const rsc_Matrix *m) const |
| 118 | { |
Jason Sams | 9bee51c | 2009-08-05 13:57:03 -0700 | [diff] [blame] | 119 | float *f = static_cast<float *>(mConstants->getPtr()); |
Jason Sams | b0ec1b4 | 2009-07-28 12:02:16 -0700 | [diff] [blame] | 120 | memcpy(&f[RS_PROGRAM_VERTEX_TEXTURE_OFFSET], m, sizeof(rsc_Matrix)); |
Jason Sams | 9bee51c | 2009-08-05 13:57:03 -0700 | [diff] [blame] | 121 | mDirty = true; |
Jason Sams | b0ec1b4 | 2009-07-28 12:02:16 -0700 | [diff] [blame] | 122 | } |
| 123 | |
Jason Sams | e9ad9a7 | 2009-09-30 17:36:20 -0700 | [diff] [blame] | 124 | void ProgramVertex::transformToScreen(const Context *rsc, float *v4out, const float *v3in) const |
| 125 | { |
| 126 | float *f = static_cast<float *>(mConstants->getPtr()); |
| 127 | Matrix mvp; |
| 128 | mvp.loadMultiply((Matrix *)&f[RS_PROGRAM_VERTEX_MODELVIEW_OFFSET], |
| 129 | (Matrix *)&f[RS_PROGRAM_VERTEX_PROJECTION_OFFSET]); |
| 130 | mvp.vectorMultiply(v4out, v3in); |
| 131 | } |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 132 | |
| 133 | ProgramVertexState::ProgramVertexState() |
| 134 | { |
| 135 | mPV = NULL; |
| 136 | } |
| 137 | |
| 138 | ProgramVertexState::~ProgramVertexState() |
| 139 | { |
| 140 | delete mPV; |
| 141 | } |
| 142 | |
Jason Sams | 9c54bdb | 2009-06-17 16:52:59 -0700 | [diff] [blame] | 143 | void ProgramVertexState::init(Context *rsc, int32_t w, int32_t h) |
| 144 | { |
Jason Sams | b6f2b13 | 2009-09-24 12:33:45 -0700 | [diff] [blame] | 145 | rsi_ElementBegin(rsc); |
| 146 | rsi_ElementAdd(rsc, RS_KIND_USER, RS_TYPE_FLOAT, false, 32, NULL); |
| 147 | RsElement e = rsi_ElementCreate(rsc); |
| 148 | |
| 149 | rsi_TypeBegin(rsc, e); |
| 150 | rsi_TypeAdd(rsc, RS_DIMENSION_X, 48); |
Jason Sams | 61f08d6 | 2009-09-25 16:37:33 -0700 | [diff] [blame] | 151 | mAllocType.set((Type *)rsi_TypeCreate(rsc)); |
Jason Sams | b6f2b13 | 2009-09-24 12:33:45 -0700 | [diff] [blame] | 152 | |
Jason Sams | a9e7a05 | 2009-09-25 14:51:22 -0700 | [diff] [blame] | 153 | ProgramVertex *pv = new ProgramVertex(rsc, NULL, NULL); |
Jason Sams | 61f08d6 | 2009-09-25 16:37:33 -0700 | [diff] [blame] | 154 | Allocation *alloc = (Allocation *)rsi_AllocationCreateTyped(rsc, mAllocType.get()); |
Jason Sams | 9c54bdb | 2009-06-17 16:52:59 -0700 | [diff] [blame] | 155 | mDefaultAlloc.set(alloc); |
| 156 | mDefault.set(pv); |
| 157 | |
Jason Sams | 9bee51c | 2009-08-05 13:57:03 -0700 | [diff] [blame] | 158 | pv->bindAllocation(alloc); |
| 159 | |
Jason Sams | eb4b031 | 2009-11-12 16:09:45 -0800 | [diff] [blame] | 160 | updateSize(rsc, w, h); |
| 161 | } |
| 162 | |
| 163 | void ProgramVertexState::updateSize(Context *rsc, int32_t w, int32_t h) |
| 164 | { |
Jason Sams | 9c54bdb | 2009-06-17 16:52:59 -0700 | [diff] [blame] | 165 | Matrix m; |
| 166 | m.loadOrtho(0,w, h,0, -1,1); |
Jason Sams | eb4b031 | 2009-11-12 16:09:45 -0800 | [diff] [blame] | 167 | 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] | 168 | |
| 169 | m.loadIdentity(); |
Jason Sams | eb4b031 | 2009-11-12 16:09:45 -0800 | [diff] [blame] | 170 | 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] | 171 | } |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 172 | |
Jason Sams | 61f08d6 | 2009-09-25 16:37:33 -0700 | [diff] [blame] | 173 | void ProgramVertexState::deinit(Context *rsc) |
| 174 | { |
| 175 | mDefaultAlloc.clear(); |
| 176 | mDefault.clear(); |
| 177 | mAllocType.clear(); |
| 178 | mLast.clear(); |
| 179 | delete mPV; |
| 180 | mPV = NULL; |
| 181 | } |
| 182 | |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 183 | |
| 184 | namespace android { |
| 185 | namespace renderscript { |
| 186 | |
| 187 | void rsi_ProgramVertexBegin(Context *rsc, RsElement in, RsElement out) |
| 188 | { |
| 189 | delete rsc->mStateVertex.mPV; |
Jason Sams | a9e7a05 | 2009-09-25 14:51:22 -0700 | [diff] [blame] | 190 | rsc->mStateVertex.mPV = new ProgramVertex(rsc, (Element *)in, (Element *)out); |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | RsProgramVertex rsi_ProgramVertexCreate(Context *rsc) |
| 194 | { |
| 195 | ProgramVertex *pv = rsc->mStateVertex.mPV; |
Jason Sams | 07ae406 | 2009-08-27 20:23:34 -0700 | [diff] [blame] | 196 | pv->incUserRef(); |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 197 | rsc->mStateVertex.mPV = 0; |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 198 | return pv; |
| 199 | } |
| 200 | |
Jason Sams | 9bee51c | 2009-08-05 13:57:03 -0700 | [diff] [blame] | 201 | void rsi_ProgramVertexBindAllocation(Context *rsc, RsProgramVertex vpgm, RsAllocation constants) |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 202 | { |
| 203 | ProgramVertex *pv = static_cast<ProgramVertex *>(vpgm); |
Jason Sams | 9bee51c | 2009-08-05 13:57:03 -0700 | [diff] [blame] | 204 | pv->bindAllocation(static_cast<Allocation *>(constants)); |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 205 | } |
| 206 | |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 207 | void rsi_ProgramVertexSetTextureMatrixEnable(Context *rsc, bool enable) |
| 208 | { |
| 209 | rsc->mStateVertex.mPV->setTextureMatrixEnable(enable); |
| 210 | } |
| 211 | |
Jason Sams | ee41112 | 2009-07-21 12:20:54 -0700 | [diff] [blame] | 212 | void rsi_ProgramVertexAddLight(Context *rsc, RsLight light) |
| 213 | { |
| 214 | rsc->mStateVertex.mPV->addLight(static_cast<const Light *>(light)); |
| 215 | } |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 216 | |
| 217 | |
| 218 | } |
| 219 | } |