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