blob: 00c5cf1f18b93c5593263294dd74d8b6b681ffcf [file] [log] [blame]
Jason Sams22534172009-08-04 16:58:20 -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 Sams7e5ab3b2009-12-15 13:27:04 -080028public class ProgramFragment extends Program {
Jason Sams22534172009-08-04 16:58:20 -070029 ProgramFragment(int id, RenderScript rs) {
Jason Sams7e5ab3b2009-12-15 13:27:04 -080030 super(id, rs);
Jason Sams22534172009-08-04 16:58:20 -070031 }
32
Jason Sams7e5ab3b2009-12-15 13:27:04 -080033 public static class ShaderBuilder extends BaseProgramBuilder {
34 public ShaderBuilder(RenderScript rs) {
35 super(rs);
36 }
37
38 public ProgramFragment create() {
39 mRS.validate();
40 int[] tmp = new int[(mInputCount + mOutputCount + mConstantCount + 1) * 2];
41 int idx = 0;
42
43 for (int i=0; i < mInputCount; i++) {
44 tmp[idx++] = 0;
45 tmp[idx++] = mInputs[i].mID;
46 }
47 for (int i=0; i < mOutputCount; i++) {
48 tmp[idx++] = 1;
49 tmp[idx++] = mOutputs[i].mID;
50 }
51 for (int i=0; i < mConstantCount; i++) {
52 tmp[idx++] = 2;
53 tmp[idx++] = mConstants[i].mID;
54 }
55 tmp[idx++] = 3;
56 tmp[idx++] = mTextureCount;
57
58 int id = mRS.nProgramFragmentCreate2(mShader, tmp);
59 ProgramFragment pf = new ProgramFragment(id, mRS);
60 initProgram(pf);
61 return pf;
62 }
63 }
Jason Sams22534172009-08-04 16:58:20 -070064
Alex Sakhartchoukc984dd72010-09-14 09:50:43 -070065 public static class Builder extends ShaderBuilder {
Jason Sams68afd012009-12-17 16:55:08 -080066 public static final int MAX_TEXTURE = 2;
Alex Sakhartchoukc984dd72010-09-14 09:50:43 -070067 int mNumTextures;
Jason Sams25ffcdc2009-08-20 16:10:36 -070068 boolean mPointSpriteEnable;
Jason Sams442a6472010-08-04 17:50:20 -070069 boolean mVaryingColorEnable;
Jason Sams68afd012009-12-17 16:55:08 -080070
71 public enum EnvMode {
72 REPLACE (1),
73 MODULATE (2),
74 DECAL (3);
75
76 int mID;
77 EnvMode(int id) {
78 mID = id;
79 }
80 }
81
82 public enum Format {
83 ALPHA (1),
84 LUMINANCE_ALPHA (2),
85 RGB (3),
86 RGBA (4);
87
88 int mID;
89 Format(int id) {
90 mID = id;
91 }
92 }
Jason Sams22534172009-08-04 16:58:20 -070093
94 private class Slot {
Jason Sams68afd012009-12-17 16:55:08 -080095 EnvMode env;
96 Format format;
97 Slot(EnvMode _env, Format _fmt) {
98 env = _env;
99 format = _fmt;
Jason Sams22534172009-08-04 16:58:20 -0700100 }
101 }
102 Slot[] mSlots;
103
Alex Sakhartchoukc984dd72010-09-14 09:50:43 -0700104 private void buildShaderString() {
105 mShader = "//rs_shader_internal\n";
106 mShader += "varying lowp vec4 varColor;\n";
107 mShader += "varying vec4 varTex0;\n";
108
109 mShader += "void main() {\n";
110 if (mVaryingColorEnable) {
111 mShader += " lowp vec4 col = varColor;\n";
112 } else {
113 mShader += " lowp vec4 col = UNI_Color;\n";
114 }
115
116 if (mNumTextures != 0) {
117 if (mPointSpriteEnable) {
118 mShader += " vec2 t0 = gl_PointCoord;\n";
119 } else {
120 mShader += " vec2 t0 = varTex0.xy;\n";
121 }
122 }
123
124 for(int i = 0; i < mNumTextures; i ++) {
125 switch(mSlots[i].env) {
126 case REPLACE:
127 switch (mSlots[i].format) {
128 case ALPHA:
129 mShader += " col.a = texture2D(UNI_Tex0, t0).a;\n";
130 break;
131 case LUMINANCE_ALPHA:
132 mShader += " col.rgba = texture2D(UNI_Tex0, t0).rgba;\n";
133 break;
134 case RGB:
135 mShader += " col.rgb = texture2D(UNI_Tex0, t0).rgb;\n";
136 break;
137 case RGBA:
138 mShader += " col.rgba = texture2D(UNI_Tex0, t0).rgba;\n";
139 break;
140 }
141 break;
142 case MODULATE:
143 switch (mSlots[i].format) {
144 case ALPHA:
145 mShader += " col.a *= texture2D(UNI_Tex0, t0).a;\n";
146 break;
147 case LUMINANCE_ALPHA:
148 mShader += " col.rgba *= texture2D(UNI_Tex0, t0).rgba;\n";
149 break;
150 case RGB:
151 mShader += " col.rgb *= texture2D(UNI_Tex0, t0).rgb;\n";
152 break;
153 case RGBA:
154 mShader += " col.rgba *= texture2D(UNI_Tex0, t0).rgba;\n";
155 break;
156 }
157 break;
158 case DECAL:
159 mShader += " col = texture2D(UNI_Tex0, t0);\n";
160 break;
161 }
162 }
163
164 mShader += " gl_FragColor = col;\n";
165 mShader += "}\n";
166 }
167
Jason Sams68afd012009-12-17 16:55:08 -0800168 public Builder(RenderScript rs) {
Alex Sakhartchoukc984dd72010-09-14 09:50:43 -0700169 super(rs);
Jason Sams22534172009-08-04 16:58:20 -0700170 mRS = rs;
Jason Sams68afd012009-12-17 16:55:08 -0800171 mSlots = new Slot[MAX_TEXTURE];
Jason Sams25ffcdc2009-08-20 16:10:36 -0700172 mPointSpriteEnable = false;
Jason Sams22534172009-08-04 16:58:20 -0700173 }
174
Jim Shuma288c8712010-07-07 14:24:21 -0700175 public Builder setTexture(EnvMode env, Format fmt, int slot)
Jason Sams22534172009-08-04 16:58:20 -0700176 throws IllegalArgumentException {
Jason Sams68afd012009-12-17 16:55:08 -0800177 if((slot < 0) || (slot >= MAX_TEXTURE)) {
178 throw new IllegalArgumentException("MAX_TEXTURE exceeded.");
Jason Sams22534172009-08-04 16:58:20 -0700179 }
Jason Sams68afd012009-12-17 16:55:08 -0800180 mSlots[slot] = new Slot(env, fmt);
Jim Shuma288c8712010-07-07 14:24:21 -0700181 return this;
Jason Sams22534172009-08-04 16:58:20 -0700182 }
183
Jim Shuma288c8712010-07-07 14:24:21 -0700184 public Builder setPointSpriteTexCoordinateReplacement(boolean enable) {
Jason Sams25ffcdc2009-08-20 16:10:36 -0700185 mPointSpriteEnable = enable;
Jim Shuma288c8712010-07-07 14:24:21 -0700186 return this;
Jason Sams25ffcdc2009-08-20 16:10:36 -0700187 }
Jason Sams22534172009-08-04 16:58:20 -0700188
Jason Sams442a6472010-08-04 17:50:20 -0700189 public Builder setVaryingColor(boolean enable) {
190 mVaryingColorEnable = enable;
191 return this;
192 }
193
Alex Sakhartchoukc984dd72010-09-14 09:50:43 -0700194 @Override
Jason Sams22534172009-08-04 16:58:20 -0700195 public ProgramFragment create() {
Alex Sakhartchoukc984dd72010-09-14 09:50:43 -0700196 mNumTextures = 0;
197 for(int i = 0; i < MAX_TEXTURE; i ++) {
198 if(mSlots[i] != null) {
199 mNumTextures ++;
200 }
Jason Sams68afd012009-12-17 16:55:08 -0800201 }
Alex Sakhartchoukc984dd72010-09-14 09:50:43 -0700202 buildShaderString();
203 Type constType = null;
204 if (!mVaryingColorEnable) {
205 Element.Builder b = new Element.Builder(mRS);
206 b.add(Element.F32_4(mRS), "Color");
207 Type.Builder typeBuilder = new Type.Builder(mRS, b.create());
208 typeBuilder.add(Dimension.X, 1);
209 constType = typeBuilder.create();
210 addConstant(constType);
Jason Sams68afd012009-12-17 16:55:08 -0800211 }
Alex Sakhartchoukc984dd72010-09-14 09:50:43 -0700212 setTextureCount(mNumTextures);
213
214 ProgramFragment pf = super.create();
Jason Sams68afd012009-12-17 16:55:08 -0800215 pf.mTextureCount = MAX_TEXTURE;
Alex Sakhartchoukc984dd72010-09-14 09:50:43 -0700216 if (!mVaryingColorEnable) {
217 Allocation constantData = Allocation.createTyped(mRS,constType);
218 float[] data = new float[4];
219 data[0] = data[1] = data[2] = data[3] = 1.0f;
220 constantData.data(data);
221 pf.bindConstants(constantData, 0);
222 }
Jason Sams68afd012009-12-17 16:55:08 -0800223 return pf;
Jason Sams22534172009-08-04 16:58:20 -0700224 }
225 }
226}
227
228
229