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