| 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 | **/ |
| Jason Sams | 7e5ab3b | 2009-12-15 13:27:04 -0800 | [diff] [blame^] | 28 | public class ProgramFragment extends Program { |
| 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) { |
| Jason Sams | 7e5ab3b | 2009-12-15 13:27:04 -0800 | [diff] [blame^] | 44 | super(id, rs); |
| Jason Sams | 2253417 | 2009-08-04 16:58:20 -0700 | [diff] [blame] | 45 | } |
| 46 | |
| Jason Sams | 110195f | 2009-08-04 18:47:46 -0700 | [diff] [blame] | 47 | public void bindTexture(Allocation va, int slot) |
| 48 | throws IllegalArgumentException { |
| Jason Sams | 771bebb | 2009-12-07 12:40:12 -0800 | [diff] [blame] | 49 | mRS.validate(); |
| Jason Sams | 110195f | 2009-08-04 18:47:46 -0700 | [diff] [blame] | 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 { |
| Jason Sams | 771bebb | 2009-12-07 12:40:12 -0800 | [diff] [blame] | 59 | mRS.validate(); |
| Jason Sams | 110195f | 2009-08-04 18:47:46 -0700 | [diff] [blame] | 60 | if((slot < 0) || (slot >= MAX_SLOT)) { |
| 61 | throw new IllegalArgumentException("Slot ID out of range."); |
| 62 | } |
| 63 | |
| Jason Sams | 2253417 | 2009-08-04 16:58:20 -0700 | [diff] [blame] | 64 | mRS.nProgramFragmentBindSampler(mID, slot, vs.mID); |
| 65 | } |
| 66 | |
| Jason Sams | 7e5ab3b | 2009-12-15 13:27:04 -0800 | [diff] [blame^] | 67 | 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 Sams | 2253417 | 2009-08-04 16:58:20 -0700 | [diff] [blame] | 98 | |
| 99 | public static class Builder { |
| Jason Sams | 2253417 | 2009-08-04 16:58:20 -0700 | [diff] [blame] | 100 | RenderScript mRS; |
| 101 | Element mIn; |
| 102 | Element mOut; |
| Jason Sams | 25ffcdc | 2009-08-20 16:10:36 -0700 | [diff] [blame] | 103 | boolean mPointSpriteEnable; |
| Jason Sams | 54c0ec1 | 2009-11-30 14:49:55 -0800 | [diff] [blame] | 104 | String mShader; |
| Jason Sams | 2253417 | 2009-08-04 16:58:20 -0700 | [diff] [blame] | 105 | |
| 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 Sams | 25ffcdc | 2009-08-20 16:10:36 -0700 | [diff] [blame] | 122 | mPointSpriteEnable = false; |
| Jason Sams | 2253417 | 2009-08-04 16:58:20 -0700 | [diff] [blame] | 123 | for(int ct=0; ct < MAX_SLOT; ct++) { |
| 124 | mSlots[ct] = new Slot(); |
| 125 | } |
| 126 | } |
| 127 | |
| Jason Sams | 54c0ec1 | 2009-11-30 14:49:55 -0800 | [diff] [blame] | 128 | public void setShader(String s) { |
| 129 | mShader = s; |
| 130 | } |
| 131 | |
| Jason Sams | 2253417 | 2009-08-04 16:58:20 -0700 | [diff] [blame] | 132 | 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 Sams | 25ffcdc | 2009-08-20 16:10:36 -0700 | [diff] [blame] | 159 | public void setPointSpriteTexCoordinateReplacement(boolean enable) { |
| 160 | mPointSpriteEnable = enable; |
| 161 | } |
| Jason Sams | 2253417 | 2009-08-04 16:58:20 -0700 | [diff] [blame] | 162 | |
| 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 Sams | 25ffcdc | 2009-08-20 16:10:36 -0700 | [diff] [blame] | 172 | rs.nProgramFragmentBegin(inID, outID, b.mPointSpriteEnable); |
| Jason Sams | 2253417 | 2009-08-04 16:58:20 -0700 | [diff] [blame] | 173 | for(int ct=0; ct < MAX_SLOT; ct++) { |
| 174 | if(b.mSlots[ct].mTexEnable) { |
| 175 | Slot s = b.mSlots[ct]; |
| Jason Sams | 25ffcdc | 2009-08-20 16:10:36 -0700 | [diff] [blame] | 176 | int typeID = 0; |
| Jason Sams | 2253417 | 2009-08-04 16:58:20 -0700 | [diff] [blame] | 177 | if(s.mType != null) { |
| Jason Sams | 25ffcdc | 2009-08-20 16:10:36 -0700 | [diff] [blame] | 178 | typeID = s.mType.mID; |
| Jason Sams | 2253417 | 2009-08-04 16:58:20 -0700 | [diff] [blame] | 179 | } |
| Jason Sams | 25ffcdc | 2009-08-20 16:10:36 -0700 | [diff] [blame] | 180 | rs.nProgramFragmentSetSlot(ct, true, s.mEnv.mID, typeID); |
| Jason Sams | 2253417 | 2009-08-04 16:58:20 -0700 | [diff] [blame] | 181 | } |
| 182 | } |
| 183 | |
| Jason Sams | 54c0ec1 | 2009-11-30 14:49:55 -0800 | [diff] [blame] | 184 | if (b.mShader != null) { |
| 185 | rs.nProgramFragmentSetShader(b.mShader); |
| 186 | } |
| 187 | |
| Jason Sams | 2253417 | 2009-08-04 16:58:20 -0700 | [diff] [blame] | 188 | int id = rs.nProgramFragmentCreate(); |
| 189 | return new ProgramFragment(id, rs); |
| 190 | } |
| 191 | |
| 192 | public ProgramFragment create() { |
| Jason Sams | 771bebb | 2009-12-07 12:40:12 -0800 | [diff] [blame] | 193 | mRS.validate(); |
| Jason Sams | 2253417 | 2009-08-04 16:58:20 -0700 | [diff] [blame] | 194 | return internalCreate(mRS, this); |
| 195 | } |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | |
| 200 | |