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