blob: 1628a9725407b548b3f6d49b51c52cff178b42fe [file] [log] [blame]
Jason Sams0011bcf2009-12-15 12:58:36 -08001/*
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;
Jason Sams7e5ab3b2009-12-15 13:27:04 -080032 public static final int MAX_TEXTURE = 8;
Jason Sams0011bcf2009-12-15 12:58:36 -080033
34 Element mInputs[];
35 Element mOutputs[];
36 Type mConstants[];
Jason Sams7e5ab3b2009-12-15 13:27:04 -080037 int mTextureCount;
Jason Sams0011bcf2009-12-15 12:58:36 -080038 String mShader;
39
40 Program(int id, RenderScript rs) {
Alex Sakhartchouk0de94442010-08-11 14:41:28 -070041 super(id, rs);
Jason Sams0011bcf2009-12-15 12:58:36 -080042 }
43
44 public void bindConstants(Allocation a, int slot) {
45 mRS.nProgramBindConstants(mID, slot, a.mID);
46 }
47
Jason Sams68afd012009-12-17 16:55:08 -080048 public void bindTexture(Allocation va, int slot)
49 throws IllegalArgumentException {
50 mRS.validate();
51 if((slot < 0) || (slot >= mTextureCount)) {
52 throw new IllegalArgumentException("Slot ID out of range.");
53 }
54
55 mRS.nProgramBindTexture(mID, slot, va.mID);
56 }
57
58 public void bindSampler(Sampler vs, int slot)
59 throws IllegalArgumentException {
60 mRS.validate();
61 if((slot < 0) || (slot >= mTextureCount)) {
62 throw new IllegalArgumentException("Slot ID out of range.");
63 }
64
65 mRS.nProgramBindSampler(mID, slot, vs.mID);
66 }
67
68
Jason Sams0011bcf2009-12-15 12:58:36 -080069 public static class BaseProgramBuilder {
70 RenderScript mRS;
71 Element mInputs[];
72 Element mOutputs[];
73 Type mConstants[];
74 Type mTextures[];
75 int mInputCount;
76 int mOutputCount;
77 int mConstantCount;
78 int mTextureCount;
79 String mShader;
80
81
82 protected BaseProgramBuilder(RenderScript rs) {
83 mRS = rs;
84 mInputs = new Element[MAX_INPUT];
85 mOutputs = new Element[MAX_OUTPUT];
86 mConstants = new Type[MAX_CONSTANT];
87 mInputCount = 0;
88 mOutputCount = 0;
89 mConstantCount = 0;
Jason Sams7e5ab3b2009-12-15 13:27:04 -080090 mTextureCount = 0;
Jason Sams0011bcf2009-12-15 12:58:36 -080091 }
92
Jim Shuma288c8712010-07-07 14:24:21 -070093 public BaseProgramBuilder setShader(String s) {
Jason Sams0011bcf2009-12-15 12:58:36 -080094 mShader = s;
Jim Shuma288c8712010-07-07 14:24:21 -070095 return this;
Jason Sams0011bcf2009-12-15 12:58:36 -080096 }
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
Jason Samsea87e962010-01-12 12:12:28 -0800114 public int addConstant(Type t) throws IllegalStateException {
Jason Sams0011bcf2009-12-15 12:58:36 -0800115 // Should check for consistant and non-conflicting names...
116 if(mConstantCount >= MAX_CONSTANT) {
117 throw new IllegalArgumentException("Max input count exceeded.");
118 }
Jason Samsea87e962010-01-12 12:12:28 -0800119 mConstants[mConstantCount] = t;
120 return mConstantCount++;
Jason Sams0011bcf2009-12-15 12:58:36 -0800121 }
122
Jim Shuma288c8712010-07-07 14:24:21 -0700123 public BaseProgramBuilder setTextureCount(int count) throws IllegalArgumentException {
Jason Sams0011bcf2009-12-15 12:58:36 -0800124 // Should check for consistant and non-conflicting names...
Jason Sams7e5ab3b2009-12-15 13:27:04 -0800125 if(count >= MAX_CONSTANT) {
126 throw new IllegalArgumentException("Max texture count exceeded.");
Jason Sams0011bcf2009-12-15 12:58:36 -0800127 }
Jason Sams7e5ab3b2009-12-15 13:27:04 -0800128 mTextureCount = count;
Jim Shuma288c8712010-07-07 14:24:21 -0700129 return this;
Jason Sams0011bcf2009-12-15 12:58:36 -0800130 }
131
132 protected void initProgram(Program p) {
133 p.mInputs = new Element[mInputCount];
134 System.arraycopy(mInputs, 0, p.mInputs, 0, mInputCount);
135 p.mOutputs = new Element[mOutputCount];
136 System.arraycopy(mOutputs, 0, p.mOutputs, 0, mOutputCount);
137 p.mConstants = new Type[mConstantCount];
138 System.arraycopy(mConstants, 0, p.mConstants, 0, mConstantCount);
Jason Sams7e5ab3b2009-12-15 13:27:04 -0800139 p.mTextureCount = mTextureCount;
Jason Sams0011bcf2009-12-15 12:58:36 -0800140 }
141 }
142
143}
144
145