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