blob: 2b2557dd36ec290728bd32bb537cdc5dbcd6af59 [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 {
Jason Sams771bebb2009-12-07 12:40:12 -080050 mRS.validate();
Jason Sams110195f2009-08-04 18:47:46 -070051 if((slot < 0) || (slot >= MAX_SLOT)) {
52 throw new IllegalArgumentException("Slot ID out of range.");
53 }
54
Jason Sams22534172009-08-04 16:58:20 -070055 mRS.nProgramFragmentBindTexture(mID, slot, va.mID);
56 }
57
Jason Sams110195f2009-08-04 18:47:46 -070058 public void bindSampler(Sampler vs, int slot)
59 throws IllegalArgumentException {
Jason Sams771bebb2009-12-07 12:40:12 -080060 mRS.validate();
Jason Sams110195f2009-08-04 18:47:46 -070061 if((slot < 0) || (slot >= MAX_SLOT)) {
62 throw new IllegalArgumentException("Slot ID out of range.");
63 }
64
Jason Sams22534172009-08-04 16:58:20 -070065 mRS.nProgramFragmentBindSampler(mID, slot, vs.mID);
66 }
67
68
69 public static class Builder {
Jason Sams22534172009-08-04 16:58:20 -070070 RenderScript mRS;
71 Element mIn;
72 Element mOut;
Jason Sams25ffcdc2009-08-20 16:10:36 -070073 boolean mPointSpriteEnable;
Jason Sams54c0ec12009-11-30 14:49:55 -080074 String mShader;
Jason Sams22534172009-08-04 16:58:20 -070075
76 private class Slot {
77 Type mType;
78 EnvMode mEnv;
79 boolean mTexEnable;
80
81 Slot() {
82 mTexEnable = false;
83 }
84 }
85 Slot[] mSlots;
86
87 public Builder(RenderScript rs, Element in, Element out) {
88 mRS = rs;
89 mIn = in;
90 mOut = out;
91 mSlots = new Slot[MAX_SLOT];
Jason Sams25ffcdc2009-08-20 16:10:36 -070092 mPointSpriteEnable = false;
Jason Sams22534172009-08-04 16:58:20 -070093 for(int ct=0; ct < MAX_SLOT; ct++) {
94 mSlots[ct] = new Slot();
95 }
96 }
97
Jason Sams54c0ec12009-11-30 14:49:55 -080098 public void setShader(String s) {
99 mShader = s;
100 }
101
Jason Sams22534172009-08-04 16:58:20 -0700102 public void setType(int slot, Type t)
103 throws IllegalArgumentException {
104 if((slot < 0) || (slot >= MAX_SLOT)) {
105 throw new IllegalArgumentException("Slot ID out of range.");
106 }
107
108 mSlots[slot].mType = t;
109 }
110
111 public void setTexEnable(boolean enable, int slot)
112 throws IllegalArgumentException {
113 if((slot < 0) || (slot >= MAX_SLOT)) {
114 throw new IllegalArgumentException("Slot ID out of range.");
115 }
116
117 mSlots[slot].mTexEnable = enable;
118 }
119
120 public void setTexEnvMode(EnvMode env, int slot)
121 throws IllegalArgumentException {
122 if((slot < 0) || (slot >= MAX_SLOT)) {
123 throw new IllegalArgumentException("Slot ID out of range.");
124 }
125
126 mSlots[slot].mEnv = env;
127 }
128
Jason Sams25ffcdc2009-08-20 16:10:36 -0700129 public void setPointSpriteTexCoordinateReplacement(boolean enable) {
130 mPointSpriteEnable = enable;
131 }
Jason Sams22534172009-08-04 16:58:20 -0700132
133 static synchronized ProgramFragment internalCreate(RenderScript rs, Builder b) {
134 int inID = 0;
135 int outID = 0;
136 if (b.mIn != null) {
137 inID = b.mIn.mID;
138 }
139 if (b.mOut != null) {
140 outID = b.mOut.mID;
141 }
Jason Sams25ffcdc2009-08-20 16:10:36 -0700142 rs.nProgramFragmentBegin(inID, outID, b.mPointSpriteEnable);
Jason Sams22534172009-08-04 16:58:20 -0700143 for(int ct=0; ct < MAX_SLOT; ct++) {
144 if(b.mSlots[ct].mTexEnable) {
145 Slot s = b.mSlots[ct];
Jason Sams25ffcdc2009-08-20 16:10:36 -0700146 int typeID = 0;
Jason Sams22534172009-08-04 16:58:20 -0700147 if(s.mType != null) {
Jason Sams25ffcdc2009-08-20 16:10:36 -0700148 typeID = s.mType.mID;
Jason Sams22534172009-08-04 16:58:20 -0700149 }
Jason Sams25ffcdc2009-08-20 16:10:36 -0700150 rs.nProgramFragmentSetSlot(ct, true, s.mEnv.mID, typeID);
Jason Sams22534172009-08-04 16:58:20 -0700151 }
152 }
153
Jason Sams54c0ec12009-11-30 14:49:55 -0800154 if (b.mShader != null) {
155 rs.nProgramFragmentSetShader(b.mShader);
156 }
157
Jason Sams22534172009-08-04 16:58:20 -0700158 int id = rs.nProgramFragmentCreate();
159 return new ProgramFragment(id, rs);
160 }
161
162 public ProgramFragment create() {
Jason Sams771bebb2009-12-07 12:40:12 -0800163 mRS.validate();
Jason Sams22534172009-08-04 16:58:20 -0700164 return internalCreate(mRS, this);
165 }
166 }
167}
168
169
170