Jason Sams | 0011bcf | 2009-12-15 12:58:36 -0800 | [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 Program extends BaseObj { |
| 29 | public static final int MAX_INPUT = 8; |
| 30 | public static final int MAX_OUTPUT = 8; |
| 31 | public static final int MAX_CONSTANT = 8; |
Jason Sams | 7e5ab3b | 2009-12-15 13:27:04 -0800 | [diff] [blame] | 32 | public static final int MAX_TEXTURE = 8; |
Jason Sams | 0011bcf | 2009-12-15 12:58:36 -0800 | [diff] [blame] | 33 | |
| 34 | Element mInputs[]; |
| 35 | Element mOutputs[]; |
| 36 | Type mConstants[]; |
Jason Sams | 7e5ab3b | 2009-12-15 13:27:04 -0800 | [diff] [blame] | 37 | int mTextureCount; |
Jason Sams | 0011bcf | 2009-12-15 12:58:36 -0800 | [diff] [blame] | 38 | String mShader; |
| 39 | |
| 40 | Program(int id, RenderScript rs) { |
| 41 | super(rs); |
| 42 | mID = id; |
| 43 | } |
| 44 | |
| 45 | public void bindConstants(Allocation a, int slot) { |
| 46 | mRS.nProgramBindConstants(mID, slot, a.mID); |
| 47 | } |
| 48 | |
Jason Sams | 68afd01 | 2009-12-17 16:55:08 -0800 | [diff] [blame^] | 49 | public void bindTexture(Allocation va, int slot) |
| 50 | throws IllegalArgumentException { |
| 51 | mRS.validate(); |
| 52 | if((slot < 0) || (slot >= mTextureCount)) { |
| 53 | throw new IllegalArgumentException("Slot ID out of range."); |
| 54 | } |
| 55 | |
| 56 | mRS.nProgramBindTexture(mID, slot, va.mID); |
| 57 | } |
| 58 | |
| 59 | public void bindSampler(Sampler vs, int slot) |
| 60 | throws IllegalArgumentException { |
| 61 | mRS.validate(); |
| 62 | if((slot < 0) || (slot >= mTextureCount)) { |
| 63 | throw new IllegalArgumentException("Slot ID out of range."); |
| 64 | } |
| 65 | |
| 66 | mRS.nProgramBindSampler(mID, slot, vs.mID); |
| 67 | } |
| 68 | |
| 69 | |
Jason Sams | 0011bcf | 2009-12-15 12:58:36 -0800 | [diff] [blame] | 70 | public static class BaseProgramBuilder { |
| 71 | RenderScript mRS; |
| 72 | Element mInputs[]; |
| 73 | Element mOutputs[]; |
| 74 | Type mConstants[]; |
| 75 | Type mTextures[]; |
| 76 | int mInputCount; |
| 77 | int mOutputCount; |
| 78 | int mConstantCount; |
| 79 | int mTextureCount; |
| 80 | String mShader; |
| 81 | |
| 82 | |
| 83 | protected BaseProgramBuilder(RenderScript rs) { |
| 84 | mRS = rs; |
| 85 | mInputs = new Element[MAX_INPUT]; |
| 86 | mOutputs = new Element[MAX_OUTPUT]; |
| 87 | mConstants = new Type[MAX_CONSTANT]; |
| 88 | mInputCount = 0; |
| 89 | mOutputCount = 0; |
| 90 | mConstantCount = 0; |
Jason Sams | 7e5ab3b | 2009-12-15 13:27:04 -0800 | [diff] [blame] | 91 | mTextureCount = 0; |
Jason Sams | 0011bcf | 2009-12-15 12:58:36 -0800 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | public void setShader(String s) { |
| 95 | mShader = s; |
| 96 | } |
| 97 | |
| 98 | public void addInput(Element e) throws IllegalStateException { |
| 99 | // Should check for consistant and non-conflicting names... |
| 100 | if(mInputCount >= MAX_INPUT) { |
| 101 | throw new IllegalArgumentException("Max input count exceeded."); |
| 102 | } |
| 103 | mInputs[mInputCount++] = e; |
| 104 | } |
| 105 | |
| 106 | public void addOutput(Element e) throws IllegalStateException { |
| 107 | // Should check for consistant and non-conflicting names... |
| 108 | if(mOutputCount >= MAX_OUTPUT) { |
| 109 | throw new IllegalArgumentException("Max output count exceeded."); |
| 110 | } |
| 111 | mOutputs[mOutputCount++] = e; |
| 112 | } |
| 113 | |
| 114 | public void addConstant(Type t) throws IllegalStateException { |
| 115 | // Should check for consistant and non-conflicting names... |
| 116 | if(mConstantCount >= MAX_CONSTANT) { |
| 117 | throw new IllegalArgumentException("Max input count exceeded."); |
| 118 | } |
| 119 | mConstants[mConstantCount++] = t; |
| 120 | } |
| 121 | |
Jason Sams | 7e5ab3b | 2009-12-15 13:27:04 -0800 | [diff] [blame] | 122 | public void setTextureCount(int count) throws IllegalArgumentException { |
Jason Sams | 0011bcf | 2009-12-15 12:58:36 -0800 | [diff] [blame] | 123 | // Should check for consistant and non-conflicting names... |
Jason Sams | 7e5ab3b | 2009-12-15 13:27:04 -0800 | [diff] [blame] | 124 | if(count >= MAX_CONSTANT) { |
| 125 | throw new IllegalArgumentException("Max texture count exceeded."); |
Jason Sams | 0011bcf | 2009-12-15 12:58:36 -0800 | [diff] [blame] | 126 | } |
Jason Sams | 7e5ab3b | 2009-12-15 13:27:04 -0800 | [diff] [blame] | 127 | mTextureCount = count; |
Jason Sams | 0011bcf | 2009-12-15 12:58:36 -0800 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | protected void initProgram(Program p) { |
| 131 | p.mInputs = new Element[mInputCount]; |
| 132 | System.arraycopy(mInputs, 0, p.mInputs, 0, mInputCount); |
| 133 | p.mOutputs = new Element[mOutputCount]; |
| 134 | System.arraycopy(mOutputs, 0, p.mOutputs, 0, mOutputCount); |
| 135 | p.mConstants = new Type[mConstantCount]; |
| 136 | System.arraycopy(mConstants, 0, p.mConstants, 0, mConstantCount); |
Jason Sams | 7e5ab3b | 2009-12-15 13:27:04 -0800 | [diff] [blame] | 137 | p.mTextureCount = mTextureCount; |
Jason Sams | 0011bcf | 2009-12-15 12:58:36 -0800 | [diff] [blame] | 138 | } |
| 139 | } |
| 140 | |
| 141 | } |
| 142 | |
| 143 | |