blob: 1614ec590cbd0ca702a32522d91381e3d6675f9d [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
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
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
Jason Sams7e5ab3b2009-12-15 13:27:04 -0800123 public void 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;
Jason Sams0011bcf2009-12-15 12:58:36 -0800129 }
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);
Jason Sams7e5ab3b2009-12-15 13:27:04 -0800138 p.mTextureCount = mTextureCount;
Jason Sams0011bcf2009-12-15 12:58:36 -0800139 }
140 }
141
142}
143
144