blob: 8a22138365d9514a9a0ce735da0ddc9da8536ae4 [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;
Jason Sams54c0ec12009-11-30 14:49:55 -080048 String mShader;
Jason Sams110195f2009-08-04 18:47:46 -070049
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
Jason Sams54c0ec12009-11-30 14:49:55 -080063 public void setShader(String s) {
64 mShader = s;
65 }
66
Jason Sams110195f2009-08-04 18:47:46 -070067 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);
Jason Sams54c0ec12009-11-30 14:49:55 -080087 if (b.mShader != null) {
88 rs.nProgramVertexSetShader(b.mShader);
89 } else {
90 for(int ct=0; ct < b.mLightCount; ct++) {
91 rs.nProgramVertexAddLight(b.mLights[ct].mID);
92 }
93 rs.nProgramVertexSetTextureMatrixEnable(b.mTextureMatrixEnable);
Jason Sams110195f2009-08-04 18:47:46 -070094 }
Jason Sams110195f2009-08-04 18:47:46 -070095 int id = rs.nProgramVertexCreate();
96 return new ProgramVertex(id, rs);
97 }
98
99 public ProgramVertex create() {
100 return internalCreate(mRS, this);
101 }
102 }
103
104
105
106 public static class MatrixAllocation {
107 static final int MODELVIEW_OFFSET = 0;
108 static final int PROJECTION_OFFSET = 16;
109 static final int TEXTURE_OFFSET = 32;
110
111 Matrix mModel;
112 Matrix mProjection;
113 Matrix mTexture;
114
115 public Allocation mAlloc;
116
117 public MatrixAllocation(RenderScript rs) {
118 mModel = new Matrix();
119 mProjection = new Matrix();
120 mTexture = new Matrix();
121
Jason Sams3c0dfba2009-09-27 17:50:38 -0700122 mAlloc = Allocation.createSized(rs, Element.USER_F32(rs), 48);
Jason Sams110195f2009-08-04 18:47:46 -0700123 mAlloc.subData1D(MODELVIEW_OFFSET, 16, mModel.mMat);
124 mAlloc.subData1D(PROJECTION_OFFSET, 16, mProjection.mMat);
125 mAlloc.subData1D(TEXTURE_OFFSET, 16, mTexture.mMat);
126 }
127
128 public void destroy() {
129 mAlloc.destroy();
130 mAlloc = null;
131 }
132
133 public void loadModelview(Matrix m) {
134 mModel = m;
135 mAlloc.subData1D(MODELVIEW_OFFSET, 16, m.mMat);
136 }
137
138 public void loadProjection(Matrix m) {
139 mProjection = m;
140 mAlloc.subData1D(PROJECTION_OFFSET, 16, m.mMat);
141 }
142
143 public void loadTexture(Matrix m) {
144 mTexture = m;
145 mAlloc.subData1D(TEXTURE_OFFSET, 16, m.mMat);
146 }
147
148 public void setupOrthoWindow(int w, int h) {
149 mProjection.loadOrtho(0,w, h,0, -1,1);
150 mAlloc.subData1D(PROJECTION_OFFSET, 16, mProjection.mMat);
151 }
152
153 public void setupOrthoNormalized(int w, int h) {
154 // range -1,1 in the narrow axis.
155 if(w > h) {
156 float aspect = ((float)w) / h;
157 mProjection.loadOrtho(-aspect,aspect, -1,1, -1,1);
158 } else {
159 float aspect = ((float)h) / w;
160 mProjection.loadOrtho(-1,1, -aspect,aspect, -1,1);
161 }
162 mAlloc.subData1D(PROJECTION_OFFSET, 16, mProjection.mMat);
163 }
164
165 public void setupProjectionNormalized(int w, int h) {
166 // range -1,1 in the narrow axis at z = 0.
167 Matrix m1 = new Matrix();
168 Matrix m2 = new Matrix();
169
170 if(w > h) {
171 float aspect = ((float)w) / h;
172 m1.loadFrustum(-aspect,aspect, -1,1, 1,100);
173 } else {
174 float aspect = ((float)h) / w;
175 m1.loadFrustum(-1,1, -aspect,aspect, 1,100);
176 }
177
178 m2.loadRotate(180, 0, 1, 0);
179 m1.loadMultiply(m1, m2);
180
181 m2.loadScale(-2, 2, 1);
182 m1.loadMultiply(m1, m2);
183
184 m2.loadTranslate(0, 0, 2);
185 m1.loadMultiply(m1, m2);
186
187 mProjection = m1;
188 mAlloc.subData1D(PROJECTION_OFFSET, 16, mProjection.mMat);
189 }
190
191 }
192
193}
194