blob: c6ed72a53d84ef9c69331ab861af59557d18c27f [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
Alex Sakhartchouka41174e2010-08-27 16:10:55 -070020import java.io.IOException;
21import java.io.InputStream;
22import java.io.UnsupportedEncodingException;
23
24import android.content.res.Resources;
Jason Sams0011bcf2009-12-15 12:58:36 -080025import android.util.Config;
26import android.util.Log;
27
28
29/**
30 * @hide
31 *
32 **/
33public class Program extends BaseObj {
34 public static final int MAX_INPUT = 8;
35 public static final int MAX_OUTPUT = 8;
36 public static final int MAX_CONSTANT = 8;
Jason Sams7e5ab3b2009-12-15 13:27:04 -080037 public static final int MAX_TEXTURE = 8;
Jason Sams0011bcf2009-12-15 12:58:36 -080038
39 Element mInputs[];
40 Element mOutputs[];
41 Type mConstants[];
Jason Sams7e5ab3b2009-12-15 13:27:04 -080042 int mTextureCount;
Jason Sams0011bcf2009-12-15 12:58:36 -080043 String mShader;
44
45 Program(int id, RenderScript rs) {
Alex Sakhartchouk0de94442010-08-11 14:41:28 -070046 super(id, rs);
Jason Sams0011bcf2009-12-15 12:58:36 -080047 }
48
49 public void bindConstants(Allocation a, int slot) {
50 mRS.nProgramBindConstants(mID, slot, a.mID);
51 }
52
Jason Sams68afd012009-12-17 16:55:08 -080053 public void bindTexture(Allocation va, int slot)
54 throws IllegalArgumentException {
55 mRS.validate();
56 if((slot < 0) || (slot >= mTextureCount)) {
57 throw new IllegalArgumentException("Slot ID out of range.");
58 }
59
60 mRS.nProgramBindTexture(mID, slot, va.mID);
61 }
62
63 public void bindSampler(Sampler vs, int slot)
64 throws IllegalArgumentException {
65 mRS.validate();
66 if((slot < 0) || (slot >= mTextureCount)) {
67 throw new IllegalArgumentException("Slot ID out of range.");
68 }
69
70 mRS.nProgramBindSampler(mID, slot, vs.mID);
71 }
72
73
Jason Sams0011bcf2009-12-15 12:58:36 -080074 public static class BaseProgramBuilder {
75 RenderScript mRS;
76 Element mInputs[];
77 Element mOutputs[];
78 Type mConstants[];
79 Type mTextures[];
80 int mInputCount;
81 int mOutputCount;
82 int mConstantCount;
83 int mTextureCount;
84 String mShader;
85
86
87 protected BaseProgramBuilder(RenderScript rs) {
88 mRS = rs;
89 mInputs = new Element[MAX_INPUT];
90 mOutputs = new Element[MAX_OUTPUT];
91 mConstants = new Type[MAX_CONSTANT];
92 mInputCount = 0;
93 mOutputCount = 0;
94 mConstantCount = 0;
Jason Sams7e5ab3b2009-12-15 13:27:04 -080095 mTextureCount = 0;
Jason Sams0011bcf2009-12-15 12:58:36 -080096 }
97
Jim Shuma288c8712010-07-07 14:24:21 -070098 public BaseProgramBuilder setShader(String s) {
Jason Sams0011bcf2009-12-15 12:58:36 -080099 mShader = s;
Jim Shuma288c8712010-07-07 14:24:21 -0700100 return this;
Jason Sams0011bcf2009-12-15 12:58:36 -0800101 }
102
Alex Sakhartchouka41174e2010-08-27 16:10:55 -0700103 public BaseProgramBuilder setShader(Resources resources, int resourceID) {
104 byte[] str;
105 int strLength;
106 InputStream is = resources.openRawResource(resourceID);
107 try {
108 try {
109 str = new byte[1024];
110 strLength = 0;
111 while(true) {
112 int bytesLeft = str.length - strLength;
113 if (bytesLeft == 0) {
114 byte[] buf2 = new byte[str.length * 2];
115 System.arraycopy(str, 0, buf2, 0, str.length);
116 str = buf2;
117 bytesLeft = str.length - strLength;
118 }
119 int bytesRead = is.read(str, strLength, bytesLeft);
120 if (bytesRead <= 0) {
121 break;
122 }
123 strLength += bytesRead;
124 }
125 } finally {
126 is.close();
127 }
128 } catch(IOException e) {
129 throw new Resources.NotFoundException();
130 }
131
132 try {
133 mShader = new String(str, 0, strLength, "UTF-8");
134 } catch (UnsupportedEncodingException e) {
135 Log.e("Renderscript shader creation", "Could not decode shader string");
136 }
137
138 return this;
139 }
140
Jason Sams0011bcf2009-12-15 12:58:36 -0800141 public void addInput(Element e) throws IllegalStateException {
142 // Should check for consistant and non-conflicting names...
143 if(mInputCount >= MAX_INPUT) {
144 throw new IllegalArgumentException("Max input count exceeded.");
145 }
146 mInputs[mInputCount++] = e;
147 }
148
149 public void addOutput(Element e) throws IllegalStateException {
150 // Should check for consistant and non-conflicting names...
151 if(mOutputCount >= MAX_OUTPUT) {
152 throw new IllegalArgumentException("Max output count exceeded.");
153 }
154 mOutputs[mOutputCount++] = e;
155 }
156
Jason Samsea87e962010-01-12 12:12:28 -0800157 public int addConstant(Type t) throws IllegalStateException {
Jason Sams0011bcf2009-12-15 12:58:36 -0800158 // Should check for consistant and non-conflicting names...
159 if(mConstantCount >= MAX_CONSTANT) {
160 throw new IllegalArgumentException("Max input count exceeded.");
161 }
Jason Samsea87e962010-01-12 12:12:28 -0800162 mConstants[mConstantCount] = t;
163 return mConstantCount++;
Jason Sams0011bcf2009-12-15 12:58:36 -0800164 }
165
Jim Shuma288c8712010-07-07 14:24:21 -0700166 public BaseProgramBuilder setTextureCount(int count) throws IllegalArgumentException {
Jason Sams0011bcf2009-12-15 12:58:36 -0800167 // Should check for consistant and non-conflicting names...
Jason Sams7e5ab3b2009-12-15 13:27:04 -0800168 if(count >= MAX_CONSTANT) {
169 throw new IllegalArgumentException("Max texture count exceeded.");
Jason Sams0011bcf2009-12-15 12:58:36 -0800170 }
Jason Sams7e5ab3b2009-12-15 13:27:04 -0800171 mTextureCount = count;
Jim Shuma288c8712010-07-07 14:24:21 -0700172 return this;
Jason Sams0011bcf2009-12-15 12:58:36 -0800173 }
174
175 protected void initProgram(Program p) {
176 p.mInputs = new Element[mInputCount];
177 System.arraycopy(mInputs, 0, p.mInputs, 0, mInputCount);
178 p.mOutputs = new Element[mOutputCount];
179 System.arraycopy(mOutputs, 0, p.mOutputs, 0, mOutputCount);
180 p.mConstants = new Type[mConstantCount];
181 System.arraycopy(mConstants, 0, p.mConstants, 0, mConstantCount);
Jason Sams7e5ab3b2009-12-15 13:27:04 -0800182 p.mTextureCount = mTextureCount;
Jason Sams0011bcf2009-12-15 12:58:36 -0800183 }
184 }
185
186}
187
188