blob: b16dac1f1e5bb251c777668018f3869318ac8e73 [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) {
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 Sams68afd012009-12-17 16:55:08 -080049 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 Sams0011bcf2009-12-15 12:58:36 -080070 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 Sams7e5ab3b2009-12-15 13:27:04 -080091 mTextureCount = 0;
Jason Sams0011bcf2009-12-15 12:58:36 -080092 }
93
Jim Shuma288c8712010-07-07 14:24:21 -070094 public BaseProgramBuilder setShader(String s) {
Jason Sams0011bcf2009-12-15 12:58:36 -080095 mShader = s;
Jim Shuma288c8712010-07-07 14:24:21 -070096 return this;
Jason Sams0011bcf2009-12-15 12:58:36 -080097 }
98
99 public void addInput(Element e) throws IllegalStateException {
100 // Should check for consistant and non-conflicting names...
101 if(mInputCount >= MAX_INPUT) {
102 throw new IllegalArgumentException("Max input count exceeded.");
103 }
104 mInputs[mInputCount++] = e;
105 }
106
107 public void addOutput(Element e) throws IllegalStateException {
108 // Should check for consistant and non-conflicting names...
109 if(mOutputCount >= MAX_OUTPUT) {
110 throw new IllegalArgumentException("Max output count exceeded.");
111 }
112 mOutputs[mOutputCount++] = e;
113 }
114
Jason Samsea87e962010-01-12 12:12:28 -0800115 public int addConstant(Type t) throws IllegalStateException {
Jason Sams0011bcf2009-12-15 12:58:36 -0800116 // Should check for consistant and non-conflicting names...
117 if(mConstantCount >= MAX_CONSTANT) {
118 throw new IllegalArgumentException("Max input count exceeded.");
119 }
Jason Samsea87e962010-01-12 12:12:28 -0800120 mConstants[mConstantCount] = t;
121 return mConstantCount++;
Jason Sams0011bcf2009-12-15 12:58:36 -0800122 }
123
Jim Shuma288c8712010-07-07 14:24:21 -0700124 public BaseProgramBuilder setTextureCount(int count) throws IllegalArgumentException {
Jason Sams0011bcf2009-12-15 12:58:36 -0800125 // Should check for consistant and non-conflicting names...
Jason Sams7e5ab3b2009-12-15 13:27:04 -0800126 if(count >= MAX_CONSTANT) {
127 throw new IllegalArgumentException("Max texture count exceeded.");
Jason Sams0011bcf2009-12-15 12:58:36 -0800128 }
Jason Sams7e5ab3b2009-12-15 13:27:04 -0800129 mTextureCount = count;
Jim Shuma288c8712010-07-07 14:24:21 -0700130 return this;
Jason Sams0011bcf2009-12-15 12:58:36 -0800131 }
132
133 protected void initProgram(Program p) {
134 p.mInputs = new Element[mInputCount];
135 System.arraycopy(mInputs, 0, p.mInputs, 0, mInputCount);
136 p.mOutputs = new Element[mOutputCount];
137 System.arraycopy(mOutputs, 0, p.mOutputs, 0, mOutputCount);
138 p.mConstants = new Type[mConstantCount];
139 System.arraycopy(mConstants, 0, p.mConstants, 0, mConstantCount);
Jason Sams7e5ab3b2009-12-15 13:27:04 -0800140 p.mTextureCount = mTextureCount;
Jason Sams0011bcf2009-12-15 12:58:36 -0800141 }
142 }
143
144}
145
146