blob: 74c005f36aa82ce7f9a01391c819d8210c37a112 [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
36 public void destroy() {
Jason Sams1bada8c2009-08-09 17:01:55 -070037 if(mDestroyed) {
38 throw new IllegalStateException("Object already destroyed.");
39 }
40 mDestroyed = true;
Jason Sams110195f2009-08-04 18:47:46 -070041 mRS.nProgramVertexDestroy(mID);
Jason Sams110195f2009-08-04 18:47:46 -070042 }
43
Jason Sams9bee51c2009-08-05 13:57:03 -070044 public void bindAllocation(MatrixAllocation va) {
45 mRS.nProgramVertexBindAllocation(mID, va.mAlloc.mID);
Jason Sams110195f2009-08-04 18:47:46 -070046 }
47
48
49 public static class Builder {
50 RenderScript mRS;
51 Element mIn;
52 Element mOut;
53 Light[] mLights;
54 int mLightCount;
55 boolean mTextureMatrixEnable;
56
57
58 public Builder(RenderScript rs, Element in, Element out) {
59 mRS = rs;
60 mIn = in;
61 mOut = out;
62 mLights = new Light[MAX_LIGHT];
63 mLightCount = 0;
64 }
65
66 public void setTextureMatrixEnable(boolean enable) {
67 mTextureMatrixEnable = enable;
68 }
69
70 public void addLight(Light l) throws IllegalStateException {
71 if(mLightCount >= MAX_LIGHT) {
72 throw new IllegalArgumentException("Max light count exceeded.");
73 }
74 mLights[mLightCount] = l;
75 mLightCount++;
76 }
77
78
79
80 static synchronized ProgramVertex internalCreate(RenderScript rs, Builder b) {
81 int inID = 0;
82 int outID = 0;
83 if (b.mIn != null) {
84 inID = b.mIn.mID;
85 }
86 if (b.mOut != null) {
87 outID = b.mOut.mID;
88 }
89 rs.nProgramVertexBegin(inID, outID);
90 for(int ct=0; ct < b.mLightCount; ct++) {
91 rs.nProgramVertexAddLight(b.mLights[ct].mID);
92 }
93 rs.nProgramVertexSetTextureMatrixEnable(b.mTextureMatrixEnable);
94 int id = rs.nProgramVertexCreate();
95 return new ProgramVertex(id, rs);
96 }
97
98 public ProgramVertex create() {
99 return internalCreate(mRS, this);
100 }
101 }
102
103
104
105 public static class MatrixAllocation {
106 static final int MODELVIEW_OFFSET = 0;
107 static final int PROJECTION_OFFSET = 16;
108 static final int TEXTURE_OFFSET = 32;
109
110 Matrix mModel;
111 Matrix mProjection;
112 Matrix mTexture;
113
114 public Allocation mAlloc;
115
116 public MatrixAllocation(RenderScript rs) {
117 mModel = new Matrix();
118 mProjection = new Matrix();
119 mTexture = new Matrix();
120
121 mAlloc = Allocation.createSized(rs, Element.USER_FLOAT, 48);
122 mAlloc.subData1D(MODELVIEW_OFFSET, 16, mModel.mMat);
123 mAlloc.subData1D(PROJECTION_OFFSET, 16, mProjection.mMat);
124 mAlloc.subData1D(TEXTURE_OFFSET, 16, mTexture.mMat);
125 }
126
127 public void destroy() {
128 mAlloc.destroy();
129 mAlloc = null;
130 }
131
132 public void loadModelview(Matrix m) {
133 mModel = m;
134 mAlloc.subData1D(MODELVIEW_OFFSET, 16, m.mMat);
135 }
136
137 public void loadProjection(Matrix m) {
138 mProjection = m;
139 mAlloc.subData1D(PROJECTION_OFFSET, 16, m.mMat);
140 }
141
142 public void loadTexture(Matrix m) {
143 mTexture = m;
144 mAlloc.subData1D(TEXTURE_OFFSET, 16, m.mMat);
145 }
146
147 public void setupOrthoWindow(int w, int h) {
148 mProjection.loadOrtho(0,w, h,0, -1,1);
149 mAlloc.subData1D(PROJECTION_OFFSET, 16, mProjection.mMat);
150 }
151
152 public void setupOrthoNormalized(int w, int h) {
153 // range -1,1 in the narrow axis.
154 if(w > h) {
155 float aspect = ((float)w) / h;
156 mProjection.loadOrtho(-aspect,aspect, -1,1, -1,1);
157 } else {
158 float aspect = ((float)h) / w;
159 mProjection.loadOrtho(-1,1, -aspect,aspect, -1,1);
160 }
161 mAlloc.subData1D(PROJECTION_OFFSET, 16, mProjection.mMat);
162 }
163
164 public void setupProjectionNormalized(int w, int h) {
165 // range -1,1 in the narrow axis at z = 0.
166 Matrix m1 = new Matrix();
167 Matrix m2 = new Matrix();
168
169 if(w > h) {
170 float aspect = ((float)w) / h;
171 m1.loadFrustum(-aspect,aspect, -1,1, 1,100);
172 } else {
173 float aspect = ((float)h) / w;
174 m1.loadFrustum(-1,1, -aspect,aspect, 1,100);
175 }
176
177 m2.loadRotate(180, 0, 1, 0);
178 m1.loadMultiply(m1, m2);
179
180 m2.loadScale(-2, 2, 1);
181 m1.loadMultiply(m1, m2);
182
183 m2.loadTranslate(0, 0, 2);
184 m1.loadMultiply(m1, m2);
185
186 mProjection = m1;
187 mAlloc.subData1D(PROJECTION_OFFSET, 16, mProjection.mMat);
188 }
189
190 }
191
192}
193