blob: ddb23ac967dcb1a1a450a5c2960b52cee6498a0b [file] [log] [blame]
Jason Sams110195f2009-08-04 18:47:46 -07001/*
2 * Copyright (C) 2008 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
17package android.renderscript;
18
19
20import android.util.Config;
21import android.util.Log;
22
23
24/**
25 * @hide
26 *
27 **/
28public class ProgramVertex extends BaseObj {
29 public static final int MAX_LIGHT = 8;
30
31 ProgramVertex(int id, RenderScript rs) {
32 super(rs);
33 mID = id;
34 }
35
Jason Sams9bee51c2009-08-05 13:57:03 -070036 public void bindAllocation(MatrixAllocation va) {
37 mRS.nProgramVertexBindAllocation(mID, va.mAlloc.mID);
Jason Sams110195f2009-08-04 18:47:46 -070038 }
39
40
41 public static class Builder {
42 RenderScript mRS;
43 Element mIn;
44 Element mOut;
45 Light[] mLights;
46 int mLightCount;
47 boolean mTextureMatrixEnable;
48
49
50 public Builder(RenderScript rs, Element in, Element out) {
51 mRS = rs;
52 mIn = in;
53 mOut = out;
54 mLights = new Light[MAX_LIGHT];
55 mLightCount = 0;
56 }
57
58 public void setTextureMatrixEnable(boolean enable) {
59 mTextureMatrixEnable = enable;
60 }
61
62 public void addLight(Light l) throws IllegalStateException {
63 if(mLightCount >= MAX_LIGHT) {
64 throw new IllegalArgumentException("Max light count exceeded.");
65 }
66 mLights[mLightCount] = l;
67 mLightCount++;
68 }
69
70
71
72 static synchronized ProgramVertex internalCreate(RenderScript rs, Builder b) {
73 int inID = 0;
74 int outID = 0;
75 if (b.mIn != null) {
76 inID = b.mIn.mID;
77 }
78 if (b.mOut != null) {
79 outID = b.mOut.mID;
80 }
81 rs.nProgramVertexBegin(inID, outID);
82 for(int ct=0; ct < b.mLightCount; ct++) {
83 rs.nProgramVertexAddLight(b.mLights[ct].mID);
84 }
85 rs.nProgramVertexSetTextureMatrixEnable(b.mTextureMatrixEnable);
86 int id = rs.nProgramVertexCreate();
87 return new ProgramVertex(id, rs);
88 }
89
90 public ProgramVertex create() {
91 return internalCreate(mRS, this);
92 }
93 }
94
95
96
97 public static class MatrixAllocation {
98 static final int MODELVIEW_OFFSET = 0;
99 static final int PROJECTION_OFFSET = 16;
100 static final int TEXTURE_OFFSET = 32;
101
102 Matrix mModel;
103 Matrix mProjection;
104 Matrix mTexture;
105
106 public Allocation mAlloc;
107
108 public MatrixAllocation(RenderScript rs) {
109 mModel = new Matrix();
110 mProjection = new Matrix();
111 mTexture = new Matrix();
112
Jason Sams3c0dfba2009-09-27 17:50:38 -0700113 mAlloc = Allocation.createSized(rs, Element.USER_F32(rs), 48);
Jason Sams110195f2009-08-04 18:47:46 -0700114 mAlloc.subData1D(MODELVIEW_OFFSET, 16, mModel.mMat);
115 mAlloc.subData1D(PROJECTION_OFFSET, 16, mProjection.mMat);
116 mAlloc.subData1D(TEXTURE_OFFSET, 16, mTexture.mMat);
117 }
118
119 public void destroy() {
120 mAlloc.destroy();
121 mAlloc = null;
122 }
123
124 public void loadModelview(Matrix m) {
125 mModel = m;
126 mAlloc.subData1D(MODELVIEW_OFFSET, 16, m.mMat);
127 }
128
129 public void loadProjection(Matrix m) {
130 mProjection = m;
131 mAlloc.subData1D(PROJECTION_OFFSET, 16, m.mMat);
132 }
133
134 public void loadTexture(Matrix m) {
135 mTexture = m;
136 mAlloc.subData1D(TEXTURE_OFFSET, 16, m.mMat);
137 }
138
139 public void setupOrthoWindow(int w, int h) {
140 mProjection.loadOrtho(0,w, h,0, -1,1);
141 mAlloc.subData1D(PROJECTION_OFFSET, 16, mProjection.mMat);
142 }
143
144 public void setupOrthoNormalized(int w, int h) {
145 // range -1,1 in the narrow axis.
146 if(w > h) {
147 float aspect = ((float)w) / h;
148 mProjection.loadOrtho(-aspect,aspect, -1,1, -1,1);
149 } else {
150 float aspect = ((float)h) / w;
151 mProjection.loadOrtho(-1,1, -aspect,aspect, -1,1);
152 }
153 mAlloc.subData1D(PROJECTION_OFFSET, 16, mProjection.mMat);
154 }
155
156 public void setupProjectionNormalized(int w, int h) {
157 // range -1,1 in the narrow axis at z = 0.
158 Matrix m1 = new Matrix();
159 Matrix m2 = new Matrix();
160
161 if(w > h) {
162 float aspect = ((float)w) / h;
163 m1.loadFrustum(-aspect,aspect, -1,1, 1,100);
164 } else {
165 float aspect = ((float)h) / w;
166 m1.loadFrustum(-1,1, -aspect,aspect, 1,100);
167 }
168
169 m2.loadRotate(180, 0, 1, 0);
170 m1.loadMultiply(m1, m2);
171
172 m2.loadScale(-2, 2, 1);
173 m1.loadMultiply(m1, m2);
174
175 m2.loadTranslate(0, 0, 2);
176 m1.loadMultiply(m1, m2);
177
178 mProjection = m1;
179 mAlloc.subData1D(PROJECTION_OFFSET, 16, mProjection.mMat);
180 }
181
182 }
183
184}
185