blob: 5e04f0c57526f765de620ce6d80aa6bdd0289bd5 [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
65 public static class Builder {
Jason Sams68afd012009-12-17 16:55:08 -080066 public static final int MAX_TEXTURE = 2;
Jason Sams22534172009-08-04 16:58:20 -070067 RenderScript mRS;
Jason Sams25ffcdc2009-08-20 16:10:36 -070068 boolean mPointSpriteEnable;
Jason Sams68afd012009-12-17 16:55:08 -080069
70 public enum EnvMode {
71 REPLACE (1),
72 MODULATE (2),
73 DECAL (3);
74
75 int mID;
76 EnvMode(int id) {
77 mID = id;
78 }
79 }
80
81 public enum Format {
82 ALPHA (1),
83 LUMINANCE_ALPHA (2),
84 RGB (3),
85 RGBA (4);
86
87 int mID;
88 Format(int id) {
89 mID = id;
90 }
91 }
Jason Sams22534172009-08-04 16:58:20 -070092
93 private class Slot {
Jason Sams68afd012009-12-17 16:55:08 -080094 EnvMode env;
95 Format format;
96 Slot(EnvMode _env, Format _fmt) {
97 env = _env;
98 format = _fmt;
Jason Sams22534172009-08-04 16:58:20 -070099 }
100 }
101 Slot[] mSlots;
102
Jason Sams68afd012009-12-17 16:55:08 -0800103 public Builder(RenderScript rs) {
Jason Sams22534172009-08-04 16:58:20 -0700104 mRS = rs;
Jason Sams68afd012009-12-17 16:55:08 -0800105 mSlots = new Slot[MAX_TEXTURE];
Jason Sams25ffcdc2009-08-20 16:10:36 -0700106 mPointSpriteEnable = false;
Jason Sams22534172009-08-04 16:58:20 -0700107 }
108
Jason Sams68afd012009-12-17 16:55:08 -0800109 public void setTexture(EnvMode env, Format fmt, int slot)
Jason Sams22534172009-08-04 16:58:20 -0700110 throws IllegalArgumentException {
Jason Sams68afd012009-12-17 16:55:08 -0800111 if((slot < 0) || (slot >= MAX_TEXTURE)) {
112 throw new IllegalArgumentException("MAX_TEXTURE exceeded.");
Jason Sams22534172009-08-04 16:58:20 -0700113 }
Jason Sams68afd012009-12-17 16:55:08 -0800114 mSlots[slot] = new Slot(env, fmt);
Jason Sams22534172009-08-04 16:58:20 -0700115 }
116
Jason Sams25ffcdc2009-08-20 16:10:36 -0700117 public void setPointSpriteTexCoordinateReplacement(boolean enable) {
118 mPointSpriteEnable = enable;
119 }
Jason Sams22534172009-08-04 16:58:20 -0700120
Jason Sams22534172009-08-04 16:58:20 -0700121 public ProgramFragment create() {
Jason Sams771bebb2009-12-07 12:40:12 -0800122 mRS.validate();
Jason Sams68afd012009-12-17 16:55:08 -0800123 int[] tmp = new int[MAX_TEXTURE * 2 + 1];
124 if (mSlots[0] != null) {
125 tmp[0] = mSlots[0].env.mID;
126 tmp[1] = mSlots[0].format.mID;
127 }
128 if (mSlots[1] != null) {
129 tmp[2] = mSlots[1].env.mID;
130 tmp[3] = mSlots[1].format.mID;
131 }
132 tmp[4] = mPointSpriteEnable ? 1 : 0;
133 int id = mRS.nProgramFragmentCreate(tmp);
134 ProgramFragment pf = new ProgramFragment(id, mRS);
135 pf.mTextureCount = MAX_TEXTURE;
136 return pf;
Jason Sams22534172009-08-04 16:58:20 -0700137 }
138 }
139}
140
141
142