blob: 68f589f45f828fcdb643a6ec8a52d4b48983654c [file] [log] [blame]
Jason Samsd19f10d2009-05-22 14:03:28 -07001/*
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 Sams4b962e52009-06-22 17:15:15 -070020#include <GLES/gl.h>
21#include <GLES/glext.h>
22
Jason Samsd19f10d2009-05-22 14:03:28 -070023using namespace android;
24using namespace android::renderscript;
25
26
Jason Samsa9e7a052009-09-25 14:51:22 -070027ProgramVertex::ProgramVertex(Context *rsc, Element *in, Element *out) :
28 Program(rsc, in, out)
Jason Samsd19f10d2009-05-22 14:03:28 -070029{
Jason Sams61f08d62009-09-25 16:37:33 -070030 mAllocFile = __FILE__;
31 mAllocLine = __LINE__;
Jason Samsd19f10d2009-05-22 14:03:28 -070032 mTextureMatrixEnable = false;
Jason Samsee411122009-07-21 12:20:54 -070033 mLightCount = 0;
Jason Samsd19f10d2009-05-22 14:03:28 -070034}
35
36ProgramVertex::~ProgramVertex()
37{
38}
39
Jason Samsb37c0a52009-06-16 17:49:58 -070040static void logMatrix(const char *txt, const float *f)
41{
Jason Samsf29ca502009-06-23 12:22:47 -070042 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 Samsb37c0a52009-06-16 17:49:58 -070047}
48
Jason Samsb13ada52009-08-25 11:34:49 -070049void ProgramVertex::setupGL(const Context *rsc, ProgramVertexState *state)
Jason Samsd19f10d2009-05-22 14:03:28 -070050{
Jason Sams9bee51c2009-08-05 13:57:03 -070051 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 Samsd19f10d2009-05-22 14:03:28 -070057
58 glMatrixMode(GL_TEXTURE);
59 if (mTextureMatrixEnable) {
60 glLoadMatrixf(&f[RS_PROGRAM_VERTEX_TEXTURE_OFFSET]);
61 } else {
62 glLoadIdentity();
63 }
64
Jason Samsee411122009-07-21 12:20:54 -070065 glMatrixMode(GL_MODELVIEW);
66 glLoadIdentity();
67 if (mLightCount) {
Romain Guyb62627e2009-08-06 22:52:13 -070068 int v = 0;
Jason Samsee411122009-07-21 12:20:54 -070069 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 Sams9bee51c2009-08-05 13:57:03 -070082
Jason Samsee411122009-07-21 12:20:54 -070083 if (!f) {
84 LOGE("Must bind constants to vertex program");
85 }
Jason Samsd19f10d2009-05-22 14:03:28 -070086
87 glMatrixMode(GL_PROJECTION);
Jason Samsb37c0a52009-06-16 17:49:58 -070088 glLoadMatrixf(&f[RS_PROGRAM_VERTEX_PROJECTION_OFFSET]);
Jason Samsd19f10d2009-05-22 14:03:28 -070089 glMatrixMode(GL_MODELVIEW);
Jason Samsb37c0a52009-06-16 17:49:58 -070090 glLoadMatrixf(&f[RS_PROGRAM_VERTEX_MODELVIEW_OFFSET]);
Jason Samsd19f10d2009-05-22 14:03:28 -070091
Jason Sams9bee51c2009-08-05 13:57:03 -070092 mDirty = false;
Jason Samsd19f10d2009-05-22 14:03:28 -070093}
94
Jason Samsee411122009-07-21 12:20:54 -070095void ProgramVertex::addLight(const Light *l)
96{
97 if (mLightCount < MAX_LIGHTS) {
98 mLights[mLightCount].set(l);
99 mLightCount++;
100 }
101}
102
Jason Samsb0ec1b42009-07-28 12:02:16 -0700103void ProgramVertex::setProjectionMatrix(const rsc_Matrix *m) const
104{
Jason Sams9bee51c2009-08-05 13:57:03 -0700105 float *f = static_cast<float *>(mConstants->getPtr());
Jason Samsb0ec1b42009-07-28 12:02:16 -0700106 memcpy(&f[RS_PROGRAM_VERTEX_PROJECTION_OFFSET], m, sizeof(rsc_Matrix));
Jason Sams9bee51c2009-08-05 13:57:03 -0700107 mDirty = true;
Jason Samsb0ec1b42009-07-28 12:02:16 -0700108}
109
110void ProgramVertex::setModelviewMatrix(const rsc_Matrix *m) const
111{
Jason Sams9bee51c2009-08-05 13:57:03 -0700112 float *f = static_cast<float *>(mConstants->getPtr());
Jason Samsb0ec1b42009-07-28 12:02:16 -0700113 memcpy(&f[RS_PROGRAM_VERTEX_MODELVIEW_OFFSET], m, sizeof(rsc_Matrix));
Jason Sams9bee51c2009-08-05 13:57:03 -0700114 mDirty = true;
Jason Samsb0ec1b42009-07-28 12:02:16 -0700115}
116
117void ProgramVertex::setTextureMatrix(const rsc_Matrix *m) const
118{
Jason Sams9bee51c2009-08-05 13:57:03 -0700119 float *f = static_cast<float *>(mConstants->getPtr());
Jason Samsb0ec1b42009-07-28 12:02:16 -0700120 memcpy(&f[RS_PROGRAM_VERTEX_TEXTURE_OFFSET], m, sizeof(rsc_Matrix));
Jason Sams9bee51c2009-08-05 13:57:03 -0700121 mDirty = true;
Jason Samsb0ec1b42009-07-28 12:02:16 -0700122}
123
Jason Samse9ad9a72009-09-30 17:36:20 -0700124void 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 Samsd19f10d2009-05-22 14:03:28 -0700132
133ProgramVertexState::ProgramVertexState()
134{
135 mPV = NULL;
136}
137
138ProgramVertexState::~ProgramVertexState()
139{
140 delete mPV;
141}
142
Jason Sams9c54bdb2009-06-17 16:52:59 -0700143void ProgramVertexState::init(Context *rsc, int32_t w, int32_t h)
144{
Jason Samsb6f2b132009-09-24 12:33:45 -0700145 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 Sams61f08d62009-09-25 16:37:33 -0700151 mAllocType.set((Type *)rsi_TypeCreate(rsc));
Jason Samsb6f2b132009-09-24 12:33:45 -0700152
Jason Samsa9e7a052009-09-25 14:51:22 -0700153 ProgramVertex *pv = new ProgramVertex(rsc, NULL, NULL);
Jason Sams61f08d62009-09-25 16:37:33 -0700154 Allocation *alloc = (Allocation *)rsi_AllocationCreateTyped(rsc, mAllocType.get());
Jason Sams9c54bdb2009-06-17 16:52:59 -0700155 mDefaultAlloc.set(alloc);
156 mDefault.set(pv);
157
Jason Sams9bee51c2009-08-05 13:57:03 -0700158 pv->bindAllocation(alloc);
159
Jason Samseb4b0312009-11-12 16:09:45 -0800160 updateSize(rsc, w, h);
161}
162
163void ProgramVertexState::updateSize(Context *rsc, int32_t w, int32_t h)
164{
Jason Sams9c54bdb2009-06-17 16:52:59 -0700165 Matrix m;
166 m.loadOrtho(0,w, h,0, -1,1);
Jason Samseb4b0312009-11-12 16:09:45 -0800167 mDefaultAlloc->subData(RS_PROGRAM_VERTEX_PROJECTION_OFFSET, 16, &m.m[0], 16*4);
Jason Sams9c54bdb2009-06-17 16:52:59 -0700168
169 m.loadIdentity();
Jason Samseb4b0312009-11-12 16:09:45 -0800170 mDefaultAlloc->subData(RS_PROGRAM_VERTEX_MODELVIEW_OFFSET, 16, &m.m[0], 16*4);
Jason Sams9c54bdb2009-06-17 16:52:59 -0700171}
Jason Samsd19f10d2009-05-22 14:03:28 -0700172
Jason Sams61f08d62009-09-25 16:37:33 -0700173void 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 Samsd19f10d2009-05-22 14:03:28 -0700183
184namespace android {
185namespace renderscript {
186
187void rsi_ProgramVertexBegin(Context *rsc, RsElement in, RsElement out)
188{
189 delete rsc->mStateVertex.mPV;
Jason Samsa9e7a052009-09-25 14:51:22 -0700190 rsc->mStateVertex.mPV = new ProgramVertex(rsc, (Element *)in, (Element *)out);
Jason Samsd19f10d2009-05-22 14:03:28 -0700191}
192
193RsProgramVertex rsi_ProgramVertexCreate(Context *rsc)
194{
195 ProgramVertex *pv = rsc->mStateVertex.mPV;
Jason Sams07ae4062009-08-27 20:23:34 -0700196 pv->incUserRef();
Jason Samsd19f10d2009-05-22 14:03:28 -0700197 rsc->mStateVertex.mPV = 0;
Jason Samsd19f10d2009-05-22 14:03:28 -0700198 return pv;
199}
200
Jason Sams9bee51c2009-08-05 13:57:03 -0700201void rsi_ProgramVertexBindAllocation(Context *rsc, RsProgramVertex vpgm, RsAllocation constants)
Jason Samsd19f10d2009-05-22 14:03:28 -0700202{
203 ProgramVertex *pv = static_cast<ProgramVertex *>(vpgm);
Jason Sams9bee51c2009-08-05 13:57:03 -0700204 pv->bindAllocation(static_cast<Allocation *>(constants));
Jason Samsd19f10d2009-05-22 14:03:28 -0700205}
206
Jason Samsd19f10d2009-05-22 14:03:28 -0700207void rsi_ProgramVertexSetTextureMatrixEnable(Context *rsc, bool enable)
208{
209 rsc->mStateVertex.mPV->setTextureMatrixEnable(enable);
210}
211
Jason Samsee411122009-07-21 12:20:54 -0700212void rsi_ProgramVertexAddLight(Context *rsc, RsLight light)
213{
214 rsc->mStateVertex.mPV->addLight(static_cast<const Light *>(light));
215}
Jason Samsd19f10d2009-05-22 14:03:28 -0700216
217
218}
219}