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