Program.java revision ea87e96959895ef94cc3aa9576f41a660d2bbf03
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
17package android.renderscript;
18
19
20import android.util.Config;
21import android.util.Log;
22
23
24/**
25 * @hide
26 *
27 **/
28public 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;
32    public static final int MAX_TEXTURE = 8;
33
34    Element mInputs[];
35    Element mOutputs[];
36    Type mConstants[];
37    int mTextureCount;
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
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
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;
91            mTextureCount = 0;
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 int 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            return mConstantCount++;
121        }
122
123        public void setTextureCount(int count) throws IllegalArgumentException {
124            // Should check for consistant and non-conflicting names...
125            if(count >= MAX_CONSTANT) {
126                throw new IllegalArgumentException("Max texture count exceeded.");
127            }
128            mTextureCount = count;
129        }
130
131        protected void initProgram(Program p) {
132            p.mInputs = new Element[mInputCount];
133            System.arraycopy(mInputs, 0, p.mInputs, 0, mInputCount);
134            p.mOutputs = new Element[mOutputCount];
135            System.arraycopy(mOutputs, 0, p.mOutputs, 0, mOutputCount);
136            p.mConstants = new Type[mConstantCount];
137            System.arraycopy(mConstants, 0, p.mConstants, 0, mConstantCount);
138            p.mTextureCount = mTextureCount;
139        }
140    }
141
142}
143
144
145