blob: f1500436eae17469250b4f68dfb54cb1f9a97b65 [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 Sams110195f2009-08-04 18:47:46 -070029 public static final int MAX_SLOT = 2;
30
Jason Sams22534172009-08-04 16:58:20 -070031 public enum EnvMode {
32 REPLACE (0),
33 MODULATE (1),
34 DECAL (2);
35
36 int mID;
37 EnvMode(int id) {
38 mID = id;
39 }
40 }
41
42
43 ProgramFragment(int id, RenderScript rs) {
Jason Sams7e5ab3b2009-12-15 13:27:04 -080044 super(id, rs);
Jason Sams22534172009-08-04 16:58:20 -070045 }
46
Jason Sams110195f2009-08-04 18:47:46 -070047 public void bindTexture(Allocation va, int slot)
48 throws IllegalArgumentException {
Jason Sams771bebb2009-12-07 12:40:12 -080049 mRS.validate();
Jason Sams110195f2009-08-04 18:47:46 -070050 if((slot < 0) || (slot >= MAX_SLOT)) {
51 throw new IllegalArgumentException("Slot ID out of range.");
52 }
53
Jason Sams22534172009-08-04 16:58:20 -070054 mRS.nProgramFragmentBindTexture(mID, slot, va.mID);
55 }
56
Jason Sams110195f2009-08-04 18:47:46 -070057 public void bindSampler(Sampler vs, int slot)
58 throws IllegalArgumentException {
Jason Sams771bebb2009-12-07 12:40:12 -080059 mRS.validate();
Jason Sams110195f2009-08-04 18:47:46 -070060 if((slot < 0) || (slot >= MAX_SLOT)) {
61 throw new IllegalArgumentException("Slot ID out of range.");
62 }
63
Jason Sams22534172009-08-04 16:58:20 -070064 mRS.nProgramFragmentBindSampler(mID, slot, vs.mID);
65 }
66
Jason Sams7e5ab3b2009-12-15 13:27:04 -080067 public static class ShaderBuilder extends BaseProgramBuilder {
68 public ShaderBuilder(RenderScript rs) {
69 super(rs);
70 }
71
72 public ProgramFragment create() {
73 mRS.validate();
74 int[] tmp = new int[(mInputCount + mOutputCount + mConstantCount + 1) * 2];
75 int idx = 0;
76
77 for (int i=0; i < mInputCount; i++) {
78 tmp[idx++] = 0;
79 tmp[idx++] = mInputs[i].mID;
80 }
81 for (int i=0; i < mOutputCount; i++) {
82 tmp[idx++] = 1;
83 tmp[idx++] = mOutputs[i].mID;
84 }
85 for (int i=0; i < mConstantCount; i++) {
86 tmp[idx++] = 2;
87 tmp[idx++] = mConstants[i].mID;
88 }
89 tmp[idx++] = 3;
90 tmp[idx++] = mTextureCount;
91
92 int id = mRS.nProgramFragmentCreate2(mShader, tmp);
93 ProgramFragment pf = new ProgramFragment(id, mRS);
94 initProgram(pf);
95 return pf;
96 }
97 }
Jason Sams22534172009-08-04 16:58:20 -070098
99 public static class Builder {
Jason Sams22534172009-08-04 16:58:20 -0700100 RenderScript mRS;
101 Element mIn;
102 Element mOut;
Jason Sams25ffcdc2009-08-20 16:10:36 -0700103 boolean mPointSpriteEnable;
Jason Sams54c0ec12009-11-30 14:49:55 -0800104 String mShader;
Jason Sams22534172009-08-04 16:58:20 -0700105
106 private class Slot {
107 Type mType;
108 EnvMode mEnv;
109 boolean mTexEnable;
110
111 Slot() {
112 mTexEnable = false;
113 }
114 }
115 Slot[] mSlots;
116
117 public Builder(RenderScript rs, Element in, Element out) {
118 mRS = rs;
119 mIn = in;
120 mOut = out;
121 mSlots = new Slot[MAX_SLOT];
Jason Sams25ffcdc2009-08-20 16:10:36 -0700122 mPointSpriteEnable = false;
Jason Sams22534172009-08-04 16:58:20 -0700123 for(int ct=0; ct < MAX_SLOT; ct++) {
124 mSlots[ct] = new Slot();
125 }
126 }
127
Jason Sams54c0ec12009-11-30 14:49:55 -0800128 public void setShader(String s) {
129 mShader = s;
130 }
131
Jason Sams22534172009-08-04 16:58:20 -0700132 public void setType(int slot, Type t)
133 throws IllegalArgumentException {
134 if((slot < 0) || (slot >= MAX_SLOT)) {
135 throw new IllegalArgumentException("Slot ID out of range.");
136 }
137
138 mSlots[slot].mType = t;
139 }
140
141 public void setTexEnable(boolean enable, int slot)
142 throws IllegalArgumentException {
143 if((slot < 0) || (slot >= MAX_SLOT)) {
144 throw new IllegalArgumentException("Slot ID out of range.");
145 }
146
147 mSlots[slot].mTexEnable = enable;
148 }
149
150 public void setTexEnvMode(EnvMode env, int slot)
151 throws IllegalArgumentException {
152 if((slot < 0) || (slot >= MAX_SLOT)) {
153 throw new IllegalArgumentException("Slot ID out of range.");
154 }
155
156 mSlots[slot].mEnv = env;
157 }
158
Jason Sams25ffcdc2009-08-20 16:10:36 -0700159 public void setPointSpriteTexCoordinateReplacement(boolean enable) {
160 mPointSpriteEnable = enable;
161 }
Jason Sams22534172009-08-04 16:58:20 -0700162
163 static synchronized ProgramFragment internalCreate(RenderScript rs, Builder b) {
164 int inID = 0;
165 int outID = 0;
166 if (b.mIn != null) {
167 inID = b.mIn.mID;
168 }
169 if (b.mOut != null) {
170 outID = b.mOut.mID;
171 }
Jason Sams25ffcdc2009-08-20 16:10:36 -0700172 rs.nProgramFragmentBegin(inID, outID, b.mPointSpriteEnable);
Jason Sams22534172009-08-04 16:58:20 -0700173 for(int ct=0; ct < MAX_SLOT; ct++) {
174 if(b.mSlots[ct].mTexEnable) {
175 Slot s = b.mSlots[ct];
Jason Sams25ffcdc2009-08-20 16:10:36 -0700176 int typeID = 0;
Jason Sams22534172009-08-04 16:58:20 -0700177 if(s.mType != null) {
Jason Sams25ffcdc2009-08-20 16:10:36 -0700178 typeID = s.mType.mID;
Jason Sams22534172009-08-04 16:58:20 -0700179 }
Jason Sams25ffcdc2009-08-20 16:10:36 -0700180 rs.nProgramFragmentSetSlot(ct, true, s.mEnv.mID, typeID);
Jason Sams22534172009-08-04 16:58:20 -0700181 }
182 }
183
Jason Sams54c0ec12009-11-30 14:49:55 -0800184 if (b.mShader != null) {
185 rs.nProgramFragmentSetShader(b.mShader);
186 }
187
Jason Sams22534172009-08-04 16:58:20 -0700188 int id = rs.nProgramFragmentCreate();
189 return new ProgramFragment(id, rs);
190 }
191
192 public ProgramFragment create() {
Jason Sams771bebb2009-12-07 12:40:12 -0800193 mRS.validate();
Jason Sams22534172009-08-04 16:58:20 -0700194 return internalCreate(mRS, this);
195 }
196 }
197}
198
199
200