blob: f8625e88dc06d464f89da24f0dd1596e73650e86 [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 **/
28public class ProgramFragment extends BaseObj {
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) {
44 super(rs);
45 mID = id;
46 }
47
Jason Sams110195f2009-08-04 18:47:46 -070048 public void bindTexture(Allocation va, int slot)
49 throws IllegalArgumentException {
50 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 {
59 if((slot < 0) || (slot >= MAX_SLOT)) {
60 throw new IllegalArgumentException("Slot ID out of range.");
61 }
62
Jason Sams22534172009-08-04 16:58:20 -070063 mRS.nProgramFragmentBindSampler(mID, slot, vs.mID);
64 }
65
66
67 public static class Builder {
Jason Sams22534172009-08-04 16:58:20 -070068 RenderScript mRS;
69 Element mIn;
70 Element mOut;
Jason Sams25ffcdc2009-08-20 16:10:36 -070071 boolean mPointSpriteEnable;
Jason Sams54c0ec12009-11-30 14:49:55 -080072 String mShader;
Jason Sams22534172009-08-04 16:58:20 -070073
74 private class Slot {
75 Type mType;
76 EnvMode mEnv;
77 boolean mTexEnable;
78
79 Slot() {
80 mTexEnable = false;
81 }
82 }
83 Slot[] mSlots;
84
85 public Builder(RenderScript rs, Element in, Element out) {
86 mRS = rs;
87 mIn = in;
88 mOut = out;
89 mSlots = new Slot[MAX_SLOT];
Jason Sams25ffcdc2009-08-20 16:10:36 -070090 mPointSpriteEnable = false;
Jason Sams22534172009-08-04 16:58:20 -070091 for(int ct=0; ct < MAX_SLOT; ct++) {
92 mSlots[ct] = new Slot();
93 }
94 }
95
Jason Sams54c0ec12009-11-30 14:49:55 -080096 public void setShader(String s) {
97 mShader = s;
98 }
99
Jason Sams22534172009-08-04 16:58:20 -0700100 public void setType(int slot, Type t)
101 throws IllegalArgumentException {
102 if((slot < 0) || (slot >= MAX_SLOT)) {
103 throw new IllegalArgumentException("Slot ID out of range.");
104 }
105
106 mSlots[slot].mType = t;
107 }
108
109 public void setTexEnable(boolean enable, int slot)
110 throws IllegalArgumentException {
111 if((slot < 0) || (slot >= MAX_SLOT)) {
112 throw new IllegalArgumentException("Slot ID out of range.");
113 }
114
115 mSlots[slot].mTexEnable = enable;
116 }
117
118 public void setTexEnvMode(EnvMode env, int slot)
119 throws IllegalArgumentException {
120 if((slot < 0) || (slot >= MAX_SLOT)) {
121 throw new IllegalArgumentException("Slot ID out of range.");
122 }
123
124 mSlots[slot].mEnv = env;
125 }
126
Jason Sams25ffcdc2009-08-20 16:10:36 -0700127 public void setPointSpriteTexCoordinateReplacement(boolean enable) {
128 mPointSpriteEnable = enable;
129 }
Jason Sams22534172009-08-04 16:58:20 -0700130
131 static synchronized ProgramFragment internalCreate(RenderScript rs, Builder b) {
132 int inID = 0;
133 int outID = 0;
134 if (b.mIn != null) {
135 inID = b.mIn.mID;
136 }
137 if (b.mOut != null) {
138 outID = b.mOut.mID;
139 }
Jason Sams25ffcdc2009-08-20 16:10:36 -0700140 rs.nProgramFragmentBegin(inID, outID, b.mPointSpriteEnable);
Jason Sams22534172009-08-04 16:58:20 -0700141 for(int ct=0; ct < MAX_SLOT; ct++) {
142 if(b.mSlots[ct].mTexEnable) {
143 Slot s = b.mSlots[ct];
Jason Sams25ffcdc2009-08-20 16:10:36 -0700144 int typeID = 0;
Jason Sams22534172009-08-04 16:58:20 -0700145 if(s.mType != null) {
Jason Sams25ffcdc2009-08-20 16:10:36 -0700146 typeID = s.mType.mID;
Jason Sams22534172009-08-04 16:58:20 -0700147 }
Jason Sams25ffcdc2009-08-20 16:10:36 -0700148 rs.nProgramFragmentSetSlot(ct, true, s.mEnv.mID, typeID);
Jason Sams22534172009-08-04 16:58:20 -0700149 }
150 }
151
Jason Sams54c0ec12009-11-30 14:49:55 -0800152 if (b.mShader != null) {
153 rs.nProgramFragmentSetShader(b.mShader);
154 }
155
Jason Sams22534172009-08-04 16:58:20 -0700156 int id = rs.nProgramFragmentCreate();
157 return new ProgramFragment(id, rs);
158 }
159
160 public ProgramFragment create() {
161 return internalCreate(mRS, this);
162 }
163 }
164}
165
166
167