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 | 2253417 | 2009-08-04 16:58:20 -0700 | [diff] [blame] | 29 | ProgramFragment(int id, RenderScript rs) { |
Jason Sams | 7e5ab3b | 2009-12-15 13:27:04 -0800 | [diff] [blame] | 30 | super(id, rs); |
Jason Sams | 2253417 | 2009-08-04 16:58:20 -0700 | [diff] [blame] | 31 | } |
| 32 | |
Jason Sams | 7e5ab3b | 2009-12-15 13:27:04 -0800 | [diff] [blame] | 33 | public static class ShaderBuilder extends BaseProgramBuilder { |
| 34 | public ShaderBuilder(RenderScript rs) { |
| 35 | super(rs); |
| 36 | } |
| 37 | |
| 38 | public ProgramFragment create() { |
| 39 | mRS.validate(); |
| 40 | int[] tmp = new int[(mInputCount + mOutputCount + mConstantCount + 1) * 2]; |
| 41 | int idx = 0; |
| 42 | |
| 43 | for (int i=0; i < mInputCount; i++) { |
| 44 | tmp[idx++] = 0; |
| 45 | tmp[idx++] = mInputs[i].mID; |
| 46 | } |
| 47 | for (int i=0; i < mOutputCount; i++) { |
| 48 | tmp[idx++] = 1; |
| 49 | tmp[idx++] = mOutputs[i].mID; |
| 50 | } |
| 51 | for (int i=0; i < mConstantCount; i++) { |
| 52 | tmp[idx++] = 2; |
| 53 | tmp[idx++] = mConstants[i].mID; |
| 54 | } |
| 55 | tmp[idx++] = 3; |
| 56 | tmp[idx++] = mTextureCount; |
| 57 | |
| 58 | int id = mRS.nProgramFragmentCreate2(mShader, tmp); |
| 59 | ProgramFragment pf = new ProgramFragment(id, mRS); |
| 60 | initProgram(pf); |
| 61 | return pf; |
| 62 | } |
| 63 | } |
Jason Sams | 2253417 | 2009-08-04 16:58:20 -0700 | [diff] [blame] | 64 | |
| 65 | public static class Builder { |
Jason Sams | 68afd01 | 2009-12-17 16:55:08 -0800 | [diff] [blame] | 66 | public static final int MAX_TEXTURE = 2; |
Jason Sams | 2253417 | 2009-08-04 16:58:20 -0700 | [diff] [blame] | 67 | RenderScript mRS; |
Jason Sams | 25ffcdc | 2009-08-20 16:10:36 -0700 | [diff] [blame] | 68 | boolean mPointSpriteEnable; |
Jason Sams | 68afd01 | 2009-12-17 16:55:08 -0800 | [diff] [blame] | 69 | |
| 70 | public enum EnvMode { |
| 71 | REPLACE (1), |
| 72 | MODULATE (2), |
| 73 | DECAL (3); |
| 74 | |
| 75 | int mID; |
| 76 | EnvMode(int id) { |
| 77 | mID = id; |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | public enum Format { |
| 82 | ALPHA (1), |
| 83 | LUMINANCE_ALPHA (2), |
| 84 | RGB (3), |
| 85 | RGBA (4); |
| 86 | |
| 87 | int mID; |
| 88 | Format(int id) { |
| 89 | mID = id; |
| 90 | } |
| 91 | } |
Jason Sams | 2253417 | 2009-08-04 16:58:20 -0700 | [diff] [blame] | 92 | |
| 93 | private class Slot { |
Jason Sams | 68afd01 | 2009-12-17 16:55:08 -0800 | [diff] [blame] | 94 | EnvMode env; |
| 95 | Format format; |
| 96 | Slot(EnvMode _env, Format _fmt) { |
| 97 | env = _env; |
| 98 | format = _fmt; |
Jason Sams | 2253417 | 2009-08-04 16:58:20 -0700 | [diff] [blame] | 99 | } |
| 100 | } |
| 101 | Slot[] mSlots; |
| 102 | |
Jason Sams | 68afd01 | 2009-12-17 16:55:08 -0800 | [diff] [blame] | 103 | public Builder(RenderScript rs) { |
Jason Sams | 2253417 | 2009-08-04 16:58:20 -0700 | [diff] [blame] | 104 | mRS = rs; |
Jason Sams | 68afd01 | 2009-12-17 16:55:08 -0800 | [diff] [blame] | 105 | mSlots = new Slot[MAX_TEXTURE]; |
Jason Sams | 25ffcdc | 2009-08-20 16:10:36 -0700 | [diff] [blame] | 106 | mPointSpriteEnable = false; |
Jason Sams | 2253417 | 2009-08-04 16:58:20 -0700 | [diff] [blame] | 107 | } |
| 108 | |
Jason Sams | 68afd01 | 2009-12-17 16:55:08 -0800 | [diff] [blame] | 109 | public void setTexture(EnvMode env, Format fmt, int slot) |
Jason Sams | 2253417 | 2009-08-04 16:58:20 -0700 | [diff] [blame] | 110 | throws IllegalArgumentException { |
Jason Sams | 68afd01 | 2009-12-17 16:55:08 -0800 | [diff] [blame] | 111 | if((slot < 0) || (slot >= MAX_TEXTURE)) { |
| 112 | throw new IllegalArgumentException("MAX_TEXTURE exceeded."); |
Jason Sams | 2253417 | 2009-08-04 16:58:20 -0700 | [diff] [blame] | 113 | } |
Jason Sams | 68afd01 | 2009-12-17 16:55:08 -0800 | [diff] [blame] | 114 | mSlots[slot] = new Slot(env, fmt); |
Jason Sams | 2253417 | 2009-08-04 16:58:20 -0700 | [diff] [blame] | 115 | } |
| 116 | |
Jason Sams | 25ffcdc | 2009-08-20 16:10:36 -0700 | [diff] [blame] | 117 | public void setPointSpriteTexCoordinateReplacement(boolean enable) { |
| 118 | mPointSpriteEnable = enable; |
| 119 | } |
Jason Sams | 2253417 | 2009-08-04 16:58:20 -0700 | [diff] [blame] | 120 | |
Jason Sams | 2253417 | 2009-08-04 16:58:20 -0700 | [diff] [blame] | 121 | public ProgramFragment create() { |
Jason Sams | 771bebb | 2009-12-07 12:40:12 -0800 | [diff] [blame] | 122 | mRS.validate(); |
Jason Sams | 68afd01 | 2009-12-17 16:55:08 -0800 | [diff] [blame] | 123 | int[] tmp = new int[MAX_TEXTURE * 2 + 1]; |
| 124 | if (mSlots[0] != null) { |
| 125 | tmp[0] = mSlots[0].env.mID; |
| 126 | tmp[1] = mSlots[0].format.mID; |
| 127 | } |
| 128 | if (mSlots[1] != null) { |
| 129 | tmp[2] = mSlots[1].env.mID; |
| 130 | tmp[3] = mSlots[1].format.mID; |
| 131 | } |
| 132 | tmp[4] = mPointSpriteEnable ? 1 : 0; |
| 133 | int id = mRS.nProgramFragmentCreate(tmp); |
| 134 | ProgramFragment pf = new ProgramFragment(id, mRS); |
| 135 | pf.mTextureCount = MAX_TEXTURE; |
| 136 | return pf; |
Jason Sams | 2253417 | 2009-08-04 16:58:20 -0700 | [diff] [blame] | 137 | } |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | |
| 142 | |