blob: 06cfc936476de045177e9465d520bbce4a613f1b [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 **/
Jason Sams0011bcf2009-12-15 12:58:36 -080028public class ProgramVertex extends Program {
Jason Sams110195f2009-08-04 18:47:46 -070029 public static final int MAX_LIGHT = 8;
30
Jason Sams0011bcf2009-12-15 12:58:36 -080031
Jason Sams110195f2009-08-04 18:47:46 -070032 ProgramVertex(int id, RenderScript rs) {
Jason Sams0011bcf2009-12-15 12:58:36 -080033 super(id, rs);
Jason Sams110195f2009-08-04 18:47:46 -070034 }
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 Sams0011bcf2009-12-15 12:58:36 -080038 bindConstants(va.mAlloc, 0);
Jason Sams110195f2009-08-04 18:47:46 -070039 }
40
41
42 public static class Builder {
43 RenderScript mRS;
Jason Sams110195f2009-08-04 18:47:46 -070044 boolean mTextureMatrixEnable;
Jason Sams110195f2009-08-04 18:47:46 -070045
46 public Builder(RenderScript rs, Element in, Element out) {
47 mRS = rs;
Jason Sams110195f2009-08-04 18:47:46 -070048 }
49
50 public void setTextureMatrixEnable(boolean enable) {
51 mTextureMatrixEnable = enable;
52 }
53
Jason Sams0011bcf2009-12-15 12:58:36 -080054 public ProgramVertex create() {
55 int id = mRS.nProgramVertexCreate(mTextureMatrixEnable);
56 return new ProgramVertex(id, mRS);
Jason Sams54c0ec12009-11-30 14:49:55 -080057 }
Jason Sams0011bcf2009-12-15 12:58:36 -080058 }
Jason Sams54c0ec12009-11-30 14:49:55 -080059
Jason Sams0011bcf2009-12-15 12:58:36 -080060 public static class ShaderBuilder extends BaseProgramBuilder {
61 public ShaderBuilder(RenderScript rs) {
62 super(rs);
Jason Sams110195f2009-08-04 18:47:46 -070063 }
64
65 public ProgramVertex create() {
Jason Sams771bebb2009-12-07 12:40:12 -080066 mRS.validate();
Jason Sams0011bcf2009-12-15 12:58:36 -080067 int[] tmp = new int[(mInputCount + mOutputCount + mConstantCount +1) * 2];
68 int idx = 0;
69
70 for (int i=0; i < mInputCount; i++) {
71 tmp[idx++] = 0;
72 tmp[idx++] = mInputs[i].mID;
73 }
74 for (int i=0; i < mOutputCount; i++) {
75 tmp[idx++] = 1;
76 tmp[idx++] = mOutputs[i].mID;
77 }
78 for (int i=0; i < mConstantCount; i++) {
79 tmp[idx++] = 2;
80 tmp[idx++] = mConstants[i].mID;
81 }
82 for (int i=0; i < mTextureCount; i++) {
83 tmp[idx++] = 3;
84 tmp[idx++] = mTextures[i].mID;
85 }
86
87 int id = mRS.nProgramVertexCreate2(mShader, tmp);
88 ProgramVertex pv = new ProgramVertex(id, mRS);
89 initProgram(pv);
90 return pv;
Jason Sams110195f2009-08-04 18:47:46 -070091 }
92 }
93
94
95
96 public static class MatrixAllocation {
97 static final int MODELVIEW_OFFSET = 0;
98 static final int PROJECTION_OFFSET = 16;
99 static final int TEXTURE_OFFSET = 32;
100
101 Matrix mModel;
102 Matrix mProjection;
103 Matrix mTexture;
104
105 public Allocation mAlloc;
106
107 public MatrixAllocation(RenderScript rs) {
108 mModel = new Matrix();
109 mProjection = new Matrix();
110 mTexture = new Matrix();
111
Jason Sams3c0dfba2009-09-27 17:50:38 -0700112 mAlloc = Allocation.createSized(rs, Element.USER_F32(rs), 48);
Jason Sams110195f2009-08-04 18:47:46 -0700113 mAlloc.subData1D(MODELVIEW_OFFSET, 16, mModel.mMat);
114 mAlloc.subData1D(PROJECTION_OFFSET, 16, mProjection.mMat);
115 mAlloc.subData1D(TEXTURE_OFFSET, 16, mTexture.mMat);
116 }
117
118 public void destroy() {
119 mAlloc.destroy();
120 mAlloc = null;
121 }
122
123 public void loadModelview(Matrix m) {
124 mModel = m;
125 mAlloc.subData1D(MODELVIEW_OFFSET, 16, m.mMat);
126 }
127
128 public void loadProjection(Matrix m) {
129 mProjection = m;
130 mAlloc.subData1D(PROJECTION_OFFSET, 16, m.mMat);
131 }
132
133 public void loadTexture(Matrix m) {
134 mTexture = m;
135 mAlloc.subData1D(TEXTURE_OFFSET, 16, m.mMat);
136 }
137
138 public void setupOrthoWindow(int w, int h) {
139 mProjection.loadOrtho(0,w, h,0, -1,1);
140 mAlloc.subData1D(PROJECTION_OFFSET, 16, mProjection.mMat);
141 }
142
143 public void setupOrthoNormalized(int w, int h) {
144 // range -1,1 in the narrow axis.
145 if(w > h) {
146 float aspect = ((float)w) / h;
147 mProjection.loadOrtho(-aspect,aspect, -1,1, -1,1);
148 } else {
149 float aspect = ((float)h) / w;
150 mProjection.loadOrtho(-1,1, -aspect,aspect, -1,1);
151 }
152 mAlloc.subData1D(PROJECTION_OFFSET, 16, mProjection.mMat);
153 }
154
155 public void setupProjectionNormalized(int w, int h) {
156 // range -1,1 in the narrow axis at z = 0.
157 Matrix m1 = new Matrix();
158 Matrix m2 = new Matrix();
159
160 if(w > h) {
161 float aspect = ((float)w) / h;
162 m1.loadFrustum(-aspect,aspect, -1,1, 1,100);
163 } else {
164 float aspect = ((float)h) / w;
165 m1.loadFrustum(-1,1, -aspect,aspect, 1,100);
166 }
167
168 m2.loadRotate(180, 0, 1, 0);
169 m1.loadMultiply(m1, m2);
170
171 m2.loadScale(-2, 2, 1);
172 m1.loadMultiply(m1, m2);
173
174 m2.loadTranslate(0, 0, 2);
175 m1.loadMultiply(m1, m2);
176
177 mProjection = m1;
178 mAlloc.subData1D(PROJECTION_OFFSET, 16, mProjection.mMat);
179 }
180
181 }
182
183}
184