blob: 417ba6aed13ec57d2ab5b72af4fb95e7afd8e138 [file] [log] [blame]
Jason Sams326e0dd2009-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 Sams1aa5a4e2009-06-22 17:15:15 -070020#include <GLES/gl.h>
21#include <GLES/glext.h>
22
Jason Sams326e0dd2009-05-22 14:03:28 -070023using namespace android;
24using namespace android::renderscript;
25
26
27ProgramVertex::ProgramVertex(Element *in, Element *out) :
28 Program(in, out)
29{
30 mTextureMatrixEnable = false;
Jason Samsb5909ce2009-07-21 12:20:54 -070031 mLightCount = 0;
Jason Sams326e0dd2009-05-22 14:03:28 -070032}
33
34ProgramVertex::~ProgramVertex()
35{
36}
37
Jason Sams56bc1af2009-06-16 17:49:58 -070038static void logMatrix(const char *txt, const float *f)
39{
Jason Sams992a0b72009-06-23 12:22:47 -070040 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 Sams56bc1af2009-06-16 17:49:58 -070045}
46
Jason Sams326e0dd2009-05-22 14:03:28 -070047void ProgramVertex::setupGL()
48{
49 const float *f = static_cast<const float *>(mConstants[0]->getPtr());
50
51 glMatrixMode(GL_TEXTURE);
52 if (mTextureMatrixEnable) {
53 glLoadMatrixf(&f[RS_PROGRAM_VERTEX_TEXTURE_OFFSET]);
54 } else {
55 glLoadIdentity();
56 }
57
Jason Samsb5909ce2009-07-21 12:20:54 -070058
59 LOGE("lights %i ", mLightCount);
60 glMatrixMode(GL_MODELVIEW);
61 glLoadIdentity();
62 if (mLightCount) {
63 int v = 1;
64 glEnable(GL_LIGHTING);
65 glLightModelxv(GL_LIGHT_MODEL_TWO_SIDE, &v);
66 for (uint32_t ct = 0; ct < mLightCount; ct++) {
67 const Light *l = mLights[ct].get();
68 glEnable(GL_LIGHT0 + ct);
69 l->setupGL(ct);
70 }
71 for (uint32_t ct = mLightCount; ct < MAX_LIGHTS; ct++) {
72 glDisable(GL_LIGHT0 + ct);
73 }
74 } else {
75 glDisable(GL_LIGHTING);
76 }
77
78 if (!f) {
79 LOGE("Must bind constants to vertex program");
80 }
Jason Sams326e0dd2009-05-22 14:03:28 -070081
82 glMatrixMode(GL_PROJECTION);
Jason Sams56bc1af2009-06-16 17:49:58 -070083 glLoadMatrixf(&f[RS_PROGRAM_VERTEX_PROJECTION_OFFSET]);
Jason Sams326e0dd2009-05-22 14:03:28 -070084 glMatrixMode(GL_MODELVIEW);
Jason Sams56bc1af2009-06-16 17:49:58 -070085 glLoadMatrixf(&f[RS_PROGRAM_VERTEX_MODELVIEW_OFFSET]);
Jason Sams326e0dd2009-05-22 14:03:28 -070086}
87
88void ProgramVertex::setConstantType(uint32_t slot, const Type *t)
89{
90 mConstantTypes[slot].set(t);
91}
92
93void ProgramVertex::bindAllocation(uint32_t slot, Allocation *a)
94{
95 mConstants[slot].set(a);
96}
97
Jason Samsb5909ce2009-07-21 12:20:54 -070098void ProgramVertex::addLight(const Light *l)
99{
100 if (mLightCount < MAX_LIGHTS) {
101 mLights[mLightCount].set(l);
102 mLightCount++;
103 }
104}
105
Jason Sams326e0dd2009-05-22 14:03:28 -0700106
107ProgramVertexState::ProgramVertexState()
108{
109 mPV = NULL;
110}
111
112ProgramVertexState::~ProgramVertexState()
113{
114 delete mPV;
115}
116
Jason Sams8ce125b2009-06-17 16:52:59 -0700117void ProgramVertexState::init(Context *rsc, int32_t w, int32_t h)
118{
119 ProgramVertex *pv = new ProgramVertex(NULL, NULL);
120 Allocation *alloc = (Allocation *)
121 rsi_AllocationCreatePredefSized(rsc, RS_ELEMENT_USER_FLOAT, 48);
122 mDefaultAlloc.set(alloc);
123 mDefault.set(pv);
124
125 pv->bindAllocation(0, alloc);
126
127 Matrix m;
128 m.loadOrtho(0,w, h,0, -1,1);
129 alloc->subData(RS_PROGRAM_VERTEX_PROJECTION_OFFSET, 16, &m.m[0]);
130
131 m.loadIdentity();
132 alloc->subData(RS_PROGRAM_VERTEX_MODELVIEW_OFFSET, 16, &m.m[0]);
133}
Jason Sams326e0dd2009-05-22 14:03:28 -0700134
135
136namespace android {
137namespace renderscript {
138
139void rsi_ProgramVertexBegin(Context *rsc, RsElement in, RsElement out)
140{
141 delete rsc->mStateVertex.mPV;
142 rsc->mStateVertex.mPV = new ProgramVertex((Element *)in, (Element *)out);
143}
144
145RsProgramVertex rsi_ProgramVertexCreate(Context *rsc)
146{
147 ProgramVertex *pv = rsc->mStateVertex.mPV;
148 pv->incRef();
149 rsc->mStateVertex.mPV = 0;
Jason Sams326e0dd2009-05-22 14:03:28 -0700150 return pv;
151}
152
153void rsi_ProgramVertexBindAllocation(Context *rsc, RsProgramVertex vpgm, uint32_t slot, RsAllocation constants)
154{
155 ProgramVertex *pv = static_cast<ProgramVertex *>(vpgm);
156 pv->bindAllocation(slot, static_cast<Allocation *>(constants));
157}
158
159void rsi_ProgramVertexSetType(Context *rsc, uint32_t slot, RsType constants)
160{
161 rsc->mStateVertex.mPV->setConstantType(slot, static_cast<const Type *>(constants));
162}
163
Jason Sams326e0dd2009-05-22 14:03:28 -0700164void rsi_ProgramVertexSetTextureMatrixEnable(Context *rsc, bool enable)
165{
166 rsc->mStateVertex.mPV->setTextureMatrixEnable(enable);
167}
168
Jason Samsb5909ce2009-07-21 12:20:54 -0700169void rsi_ProgramVertexAddLight(Context *rsc, RsLight light)
170{
171 rsc->mStateVertex.mPV->addLight(static_cast<const Light *>(light));
172}
Jason Sams326e0dd2009-05-22 14:03:28 -0700173
174
175}
176}