blob: c99efd6a7a8ebf26cf9c0698b5552657693122e9 [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 }
Alex Sakhartchouk32e09b52010-08-23 10:24:10 -070049 public Builder(RenderScript rs) {
50 mRS = rs;
51 }
Jason Sams110195f2009-08-04 18:47:46 -070052
Jim Shuma288c8712010-07-07 14:24:21 -070053 public Builder setTextureMatrixEnable(boolean enable) {
Jason Sams110195f2009-08-04 18:47:46 -070054 mTextureMatrixEnable = enable;
Jim Shuma288c8712010-07-07 14:24:21 -070055 return this;
Jason Sams110195f2009-08-04 18:47:46 -070056 }
57
Jason Sams0011bcf2009-12-15 12:58:36 -080058 public ProgramVertex create() {
59 int id = mRS.nProgramVertexCreate(mTextureMatrixEnable);
60 return new ProgramVertex(id, mRS);
Jason Sams54c0ec12009-11-30 14:49:55 -080061 }
Jason Sams0011bcf2009-12-15 12:58:36 -080062 }
Jason Sams54c0ec12009-11-30 14:49:55 -080063
Jason Sams0011bcf2009-12-15 12:58:36 -080064 public static class ShaderBuilder extends BaseProgramBuilder {
65 public ShaderBuilder(RenderScript rs) {
66 super(rs);
Jason Sams110195f2009-08-04 18:47:46 -070067 }
68
69 public ProgramVertex create() {
Jason Sams771bebb2009-12-07 12:40:12 -080070 mRS.validate();
Jason Sams0011bcf2009-12-15 12:58:36 -080071 int[] tmp = new int[(mInputCount + mOutputCount + mConstantCount +1) * 2];
72 int idx = 0;
73
74 for (int i=0; i < mInputCount; i++) {
75 tmp[idx++] = 0;
76 tmp[idx++] = mInputs[i].mID;
77 }
78 for (int i=0; i < mOutputCount; i++) {
79 tmp[idx++] = 1;
80 tmp[idx++] = mOutputs[i].mID;
81 }
82 for (int i=0; i < mConstantCount; i++) {
83 tmp[idx++] = 2;
84 tmp[idx++] = mConstants[i].mID;
85 }
Jason Sams7e5ab3b2009-12-15 13:27:04 -080086 tmp[idx++] = 3;
87 tmp[idx++] = mTextureCount;
Jason Sams0011bcf2009-12-15 12:58:36 -080088
89 int id = mRS.nProgramVertexCreate2(mShader, tmp);
90 ProgramVertex pv = new ProgramVertex(id, mRS);
91 initProgram(pv);
92 return pv;
Jason Sams110195f2009-08-04 18:47:46 -070093 }
94 }
95
96
97
98 public static class MatrixAllocation {
99 static final int MODELVIEW_OFFSET = 0;
100 static final int PROJECTION_OFFSET = 16;
101 static final int TEXTURE_OFFSET = 32;
102
Jason Sams25430d02010-02-02 15:26:40 -0800103 Matrix4f mModel;
104 Matrix4f mProjection;
105 Matrix4f mTexture;
Jason Sams110195f2009-08-04 18:47:46 -0700106
107 public Allocation mAlloc;
108
109 public MatrixAllocation(RenderScript rs) {
Jason Sams25430d02010-02-02 15:26:40 -0800110 mModel = new Matrix4f();
111 mProjection = new Matrix4f();
112 mTexture = new Matrix4f();
Jason Sams110195f2009-08-04 18:47:46 -0700113
Jason Sams718cd1f2009-12-23 14:35:29 -0800114 mAlloc = Allocation.createSized(rs, Element.createUser(rs, Element.DataType.FLOAT_32), 48);
Jason Sams110195f2009-08-04 18:47:46 -0700115 mAlloc.subData1D(MODELVIEW_OFFSET, 16, mModel.mMat);
116 mAlloc.subData1D(PROJECTION_OFFSET, 16, mProjection.mMat);
117 mAlloc.subData1D(TEXTURE_OFFSET, 16, mTexture.mMat);
118 }
119
120 public void destroy() {
121 mAlloc.destroy();
122 mAlloc = null;
123 }
124
Jason Sams25430d02010-02-02 15:26:40 -0800125 public void loadModelview(Matrix4f m) {
Jason Sams110195f2009-08-04 18:47:46 -0700126 mModel = m;
127 mAlloc.subData1D(MODELVIEW_OFFSET, 16, m.mMat);
128 }
129
Jason Sams25430d02010-02-02 15:26:40 -0800130 public void loadProjection(Matrix4f m) {
Jason Sams110195f2009-08-04 18:47:46 -0700131 mProjection = m;
132 mAlloc.subData1D(PROJECTION_OFFSET, 16, m.mMat);
133 }
134
Jason Sams25430d02010-02-02 15:26:40 -0800135 public void loadTexture(Matrix4f m) {
Jason Sams110195f2009-08-04 18:47:46 -0700136 mTexture = m;
137 mAlloc.subData1D(TEXTURE_OFFSET, 16, m.mMat);
138 }
139
140 public void setupOrthoWindow(int w, int h) {
141 mProjection.loadOrtho(0,w, h,0, -1,1);
142 mAlloc.subData1D(PROJECTION_OFFSET, 16, mProjection.mMat);
143 }
144
145 public void setupOrthoNormalized(int w, int h) {
146 // range -1,1 in the narrow axis.
147 if(w > h) {
148 float aspect = ((float)w) / h;
149 mProjection.loadOrtho(-aspect,aspect, -1,1, -1,1);
150 } else {
151 float aspect = ((float)h) / w;
152 mProjection.loadOrtho(-1,1, -aspect,aspect, -1,1);
153 }
154 mAlloc.subData1D(PROJECTION_OFFSET, 16, mProjection.mMat);
155 }
156
157 public void setupProjectionNormalized(int w, int h) {
158 // range -1,1 in the narrow axis at z = 0.
Jason Sams25430d02010-02-02 15:26:40 -0800159 Matrix4f m1 = new Matrix4f();
160 Matrix4f m2 = new Matrix4f();
Jason Sams110195f2009-08-04 18:47:46 -0700161
162 if(w > h) {
163 float aspect = ((float)w) / h;
164 m1.loadFrustum(-aspect,aspect, -1,1, 1,100);
165 } else {
166 float aspect = ((float)h) / w;
167 m1.loadFrustum(-1,1, -aspect,aspect, 1,100);
168 }
169
170 m2.loadRotate(180, 0, 1, 0);
171 m1.loadMultiply(m1, m2);
172
173 m2.loadScale(-2, 2, 1);
174 m1.loadMultiply(m1, m2);
175
176 m2.loadTranslate(0, 0, 2);
177 m1.loadMultiply(m1, m2);
178
179 mProjection = m1;
180 mAlloc.subData1D(PROJECTION_OFFSET, 16, mProjection.mMat);
181 }
182
183 }
184
185}
186