| Jason Sams | 2253417 | 2009-08-04 16:58:20 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package android.renderscript; |
| 18 | |
| 19 | |
| 20 | import android.util.Config; |
| 21 | import android.util.Log; |
| 22 | |
| 23 | |
| 24 | /** |
| 25 | * @hide |
| 26 | * |
| 27 | **/ |
| 28 | public class ProgramFragment extends BaseObj { |
| Jason Sams | 110195f | 2009-08-04 18:47:46 -0700 | [diff] [blame] | 29 | public static final int MAX_SLOT = 2; |
| 30 | |
| Jason Sams | 2253417 | 2009-08-04 16:58:20 -0700 | [diff] [blame] | 31 | 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 Sams | 110195f | 2009-08-04 18:47:46 -0700 | [diff] [blame] | 48 | public void bindTexture(Allocation va, int slot) |
| 49 | throws IllegalArgumentException { |
| Jason Sams | 771bebb | 2009-12-07 12:40:12 -0800 | [diff] [blame^] | 50 | mRS.validate(); |
| Jason Sams | 110195f | 2009-08-04 18:47:46 -0700 | [diff] [blame] | 51 | if((slot < 0) || (slot >= MAX_SLOT)) { |
| 52 | throw new IllegalArgumentException("Slot ID out of range."); |
| 53 | } |
| 54 | |
| Jason Sams | 2253417 | 2009-08-04 16:58:20 -0700 | [diff] [blame] | 55 | mRS.nProgramFragmentBindTexture(mID, slot, va.mID); |
| 56 | } |
| 57 | |
| Jason Sams | 110195f | 2009-08-04 18:47:46 -0700 | [diff] [blame] | 58 | public void bindSampler(Sampler vs, int slot) |
| 59 | throws IllegalArgumentException { |
| Jason Sams | 771bebb | 2009-12-07 12:40:12 -0800 | [diff] [blame^] | 60 | mRS.validate(); |
| Jason Sams | 110195f | 2009-08-04 18:47:46 -0700 | [diff] [blame] | 61 | if((slot < 0) || (slot >= MAX_SLOT)) { |
| 62 | throw new IllegalArgumentException("Slot ID out of range."); |
| 63 | } |
| 64 | |
| Jason Sams | 2253417 | 2009-08-04 16:58:20 -0700 | [diff] [blame] | 65 | mRS.nProgramFragmentBindSampler(mID, slot, vs.mID); |
| 66 | } |
| 67 | |
| 68 | |
| 69 | public static class Builder { |
| Jason Sams | 2253417 | 2009-08-04 16:58:20 -0700 | [diff] [blame] | 70 | RenderScript mRS; |
| 71 | Element mIn; |
| 72 | Element mOut; |
| Jason Sams | 25ffcdc | 2009-08-20 16:10:36 -0700 | [diff] [blame] | 73 | boolean mPointSpriteEnable; |
| Jason Sams | 2253417 | 2009-08-04 16:58:20 -0700 | [diff] [blame] | 74 | |
| 75 | private class Slot { |
| 76 | Type mType; |
| 77 | EnvMode mEnv; |
| 78 | boolean mTexEnable; |
| 79 | |
| 80 | Slot() { |
| 81 | mTexEnable = false; |
| 82 | } |
| 83 | } |
| 84 | Slot[] mSlots; |
| 85 | |
| 86 | public Builder(RenderScript rs, Element in, Element out) { |
| 87 | mRS = rs; |
| 88 | mIn = in; |
| 89 | mOut = out; |
| 90 | mSlots = new Slot[MAX_SLOT]; |
| Jason Sams | 25ffcdc | 2009-08-20 16:10:36 -0700 | [diff] [blame] | 91 | mPointSpriteEnable = false; |
| Jason Sams | 2253417 | 2009-08-04 16:58:20 -0700 | [diff] [blame] | 92 | for(int ct=0; ct < MAX_SLOT; ct++) { |
| 93 | mSlots[ct] = new Slot(); |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | public void setType(int slot, Type t) |
| 98 | throws IllegalArgumentException { |
| 99 | if((slot < 0) || (slot >= MAX_SLOT)) { |
| 100 | throw new IllegalArgumentException("Slot ID out of range."); |
| 101 | } |
| 102 | |
| 103 | mSlots[slot].mType = t; |
| 104 | } |
| 105 | |
| 106 | public void setTexEnable(boolean enable, int slot) |
| 107 | throws IllegalArgumentException { |
| 108 | if((slot < 0) || (slot >= MAX_SLOT)) { |
| 109 | throw new IllegalArgumentException("Slot ID out of range."); |
| 110 | } |
| 111 | |
| 112 | mSlots[slot].mTexEnable = enable; |
| 113 | } |
| 114 | |
| 115 | public void setTexEnvMode(EnvMode env, int slot) |
| 116 | throws IllegalArgumentException { |
| 117 | if((slot < 0) || (slot >= MAX_SLOT)) { |
| 118 | throw new IllegalArgumentException("Slot ID out of range."); |
| 119 | } |
| 120 | |
| 121 | mSlots[slot].mEnv = env; |
| 122 | } |
| 123 | |
| Jason Sams | 25ffcdc | 2009-08-20 16:10:36 -0700 | [diff] [blame] | 124 | public void setPointSpriteTexCoordinateReplacement(boolean enable) { |
| 125 | mPointSpriteEnable = enable; |
| 126 | } |
| Jason Sams | 2253417 | 2009-08-04 16:58:20 -0700 | [diff] [blame] | 127 | |
| 128 | static synchronized ProgramFragment internalCreate(RenderScript rs, Builder b) { |
| 129 | int inID = 0; |
| 130 | int outID = 0; |
| 131 | if (b.mIn != null) { |
| 132 | inID = b.mIn.mID; |
| 133 | } |
| 134 | if (b.mOut != null) { |
| 135 | outID = b.mOut.mID; |
| 136 | } |
| Jason Sams | 25ffcdc | 2009-08-20 16:10:36 -0700 | [diff] [blame] | 137 | rs.nProgramFragmentBegin(inID, outID, b.mPointSpriteEnable); |
| Jason Sams | 2253417 | 2009-08-04 16:58:20 -0700 | [diff] [blame] | 138 | for(int ct=0; ct < MAX_SLOT; ct++) { |
| 139 | if(b.mSlots[ct].mTexEnable) { |
| 140 | Slot s = b.mSlots[ct]; |
| Jason Sams | 25ffcdc | 2009-08-20 16:10:36 -0700 | [diff] [blame] | 141 | int typeID = 0; |
| Jason Sams | 2253417 | 2009-08-04 16:58:20 -0700 | [diff] [blame] | 142 | if(s.mType != null) { |
| Jason Sams | 25ffcdc | 2009-08-20 16:10:36 -0700 | [diff] [blame] | 143 | typeID = s.mType.mID; |
| Jason Sams | 2253417 | 2009-08-04 16:58:20 -0700 | [diff] [blame] | 144 | } |
| Jason Sams | 25ffcdc | 2009-08-20 16:10:36 -0700 | [diff] [blame] | 145 | rs.nProgramFragmentSetSlot(ct, true, s.mEnv.mID, typeID); |
| Jason Sams | 2253417 | 2009-08-04 16:58:20 -0700 | [diff] [blame] | 146 | } |
| 147 | } |
| 148 | |
| Jason Sams | 2253417 | 2009-08-04 16:58:20 -0700 | [diff] [blame] | 149 | int id = rs.nProgramFragmentCreate(); |
| 150 | return new ProgramFragment(id, rs); |
| 151 | } |
| 152 | |
| 153 | public ProgramFragment create() { |
| Jason Sams | 771bebb | 2009-12-07 12:40:12 -0800 | [diff] [blame^] | 154 | mRS.validate(); |
| Jason Sams | 2253417 | 2009-08-04 16:58:20 -0700 | [diff] [blame] | 155 | return internalCreate(mRS, this); |
| 156 | } |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | |
| 161 | |