blob: b07ae7d53f7657f295ee0b19eacee225dd79a3f7 [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/**
Jason Sams0011bcf2009-12-15 12:58:36 -080030 *
31 **/
32public class Program extends BaseObj {
33 public static final int MAX_INPUT = 8;
34 public static final int MAX_OUTPUT = 8;
35 public static final int MAX_CONSTANT = 8;
Jason Sams7e5ab3b2009-12-15 13:27:04 -080036 public static final int MAX_TEXTURE = 8;
Jason Sams0011bcf2009-12-15 12:58:36 -080037
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -080038 public enum TextureType {
39 TEXTURE_2D (0),
40 TEXTURE_CUBE (1);
41
42 int mID;
43 TextureType(int id) {
44 mID = id;
45 }
46 }
47
48 enum ProgramParam {
49 INPUT (0),
50 OUTPUT (1),
51 CONSTANT (2),
52 TEXTURE_TYPE (3);
53
54 int mID;
55 ProgramParam(int id) {
56 mID = id;
57 }
58 };
59
Jason Sams0011bcf2009-12-15 12:58:36 -080060 Element mInputs[];
61 Element mOutputs[];
62 Type mConstants[];
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -080063 TextureType mTextures[];
Jason Sams7e5ab3b2009-12-15 13:27:04 -080064 int mTextureCount;
Jason Sams0011bcf2009-12-15 12:58:36 -080065 String mShader;
66
67 Program(int id, RenderScript rs) {
Alex Sakhartchouk0de94442010-08-11 14:41:28 -070068 super(id, rs);
Jason Sams0011bcf2009-12-15 12:58:36 -080069 }
70
71 public void bindConstants(Allocation a, int slot) {
Jason Samsc1d62102010-11-04 14:32:19 -070072 if (slot < 0 || slot >= mConstants.length) {
73 throw new IllegalArgumentException("Slot ID out of range.");
74 }
75 if (a != null &&
76 a.getType().getID() != mConstants[slot].getID()) {
77 throw new IllegalArgumentException("Allocation type does not match slot type.");
78 }
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -080079 int id = a != null ? a.getID() : 0;
80 mRS.nProgramBindConstants(getID(), slot, id);
Jason Sams0011bcf2009-12-15 12:58:36 -080081 }
82
Jason Sams68afd012009-12-17 16:55:08 -080083 public void bindTexture(Allocation va, int slot)
84 throws IllegalArgumentException {
85 mRS.validate();
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -080086 if ((slot < 0) || (slot >= mTextureCount)) {
Jason Sams68afd012009-12-17 16:55:08 -080087 throw new IllegalArgumentException("Slot ID out of range.");
88 }
Jason Samsbf6ef8d72010-12-06 15:59:59 -080089 if (va != null && va.getType().hasFaces() &&
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -080090 mTextures[slot] != TextureType.TEXTURE_CUBE) {
91 throw new IllegalArgumentException("Cannot bind cubemap to 2d texture slot");
92 }
Jason Sams68afd012009-12-17 16:55:08 -080093
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -080094 int id = va != null ? va.getID() : 0;
95 mRS.nProgramBindTexture(getID(), slot, id);
Jason Sams68afd012009-12-17 16:55:08 -080096 }
97
98 public void bindSampler(Sampler vs, int slot)
99 throws IllegalArgumentException {
100 mRS.validate();
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800101 if ((slot < 0) || (slot >= mTextureCount)) {
Jason Sams68afd012009-12-17 16:55:08 -0800102 throw new IllegalArgumentException("Slot ID out of range.");
103 }
104
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800105 int id = vs != null ? vs.getID() : 0;
106 mRS.nProgramBindSampler(getID(), slot, id);
Jason Sams68afd012009-12-17 16:55:08 -0800107 }
108
109
Jason Sams0011bcf2009-12-15 12:58:36 -0800110 public static class BaseProgramBuilder {
111 RenderScript mRS;
112 Element mInputs[];
113 Element mOutputs[];
114 Type mConstants[];
115 Type mTextures[];
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800116 TextureType mTextureTypes[];
Jason Sams0011bcf2009-12-15 12:58:36 -0800117 int mInputCount;
118 int mOutputCount;
119 int mConstantCount;
120 int mTextureCount;
121 String mShader;
122
123
124 protected BaseProgramBuilder(RenderScript rs) {
125 mRS = rs;
126 mInputs = new Element[MAX_INPUT];
127 mOutputs = new Element[MAX_OUTPUT];
128 mConstants = new Type[MAX_CONSTANT];
129 mInputCount = 0;
130 mOutputCount = 0;
131 mConstantCount = 0;
Jason Sams7e5ab3b2009-12-15 13:27:04 -0800132 mTextureCount = 0;
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800133 mTextureTypes = new TextureType[MAX_TEXTURE];
Jason Sams0011bcf2009-12-15 12:58:36 -0800134 }
135
Jim Shuma288c8712010-07-07 14:24:21 -0700136 public BaseProgramBuilder setShader(String s) {
Jason Sams0011bcf2009-12-15 12:58:36 -0800137 mShader = s;
Jim Shuma288c8712010-07-07 14:24:21 -0700138 return this;
Jason Sams0011bcf2009-12-15 12:58:36 -0800139 }
140
Alex Sakhartchouka41174e2010-08-27 16:10:55 -0700141 public BaseProgramBuilder setShader(Resources resources, int resourceID) {
142 byte[] str;
143 int strLength;
144 InputStream is = resources.openRawResource(resourceID);
145 try {
146 try {
147 str = new byte[1024];
148 strLength = 0;
149 while(true) {
150 int bytesLeft = str.length - strLength;
151 if (bytesLeft == 0) {
152 byte[] buf2 = new byte[str.length * 2];
153 System.arraycopy(str, 0, buf2, 0, str.length);
154 str = buf2;
155 bytesLeft = str.length - strLength;
156 }
157 int bytesRead = is.read(str, strLength, bytesLeft);
158 if (bytesRead <= 0) {
159 break;
160 }
161 strLength += bytesRead;
162 }
163 } finally {
164 is.close();
165 }
166 } catch(IOException e) {
167 throw new Resources.NotFoundException();
168 }
169
170 try {
171 mShader = new String(str, 0, strLength, "UTF-8");
172 } catch (UnsupportedEncodingException e) {
173 Log.e("Renderscript shader creation", "Could not decode shader string");
174 }
175
176 return this;
177 }
178
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800179 public int getCurrentConstantIndex() {
180 return mConstantCount - 1;
Jason Sams0011bcf2009-12-15 12:58:36 -0800181 }
182
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800183 public int getCurrentTextureIndex() {
184 return mTextureCount - 1;
Jason Sams0011bcf2009-12-15 12:58:36 -0800185 }
186
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800187 public BaseProgramBuilder addConstant(Type t) throws IllegalStateException {
Jason Sams0011bcf2009-12-15 12:58:36 -0800188 // Should check for consistant and non-conflicting names...
189 if(mConstantCount >= MAX_CONSTANT) {
Jason Samsc1d62102010-11-04 14:32:19 -0700190 throw new RSIllegalArgumentException("Max input count exceeded.");
191 }
192 if (t.getElement().isComplex()) {
193 throw new RSIllegalArgumentException("Complex elements not allowed.");
Jason Sams0011bcf2009-12-15 12:58:36 -0800194 }
Jason Samsea87e962010-01-12 12:12:28 -0800195 mConstants[mConstantCount] = t;
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800196 mConstantCount++;
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800197 return this;
198 }
199
200 public BaseProgramBuilder addTexture(TextureType texType) throws IllegalArgumentException {
201 if(mTextureCount >= MAX_TEXTURE) {
202 throw new IllegalArgumentException("Max texture count exceeded.");
203 }
204 mTextureTypes[mTextureCount ++] = texType;
Jim Shuma288c8712010-07-07 14:24:21 -0700205 return this;
Jason Sams0011bcf2009-12-15 12:58:36 -0800206 }
207
208 protected void initProgram(Program p) {
209 p.mInputs = new Element[mInputCount];
210 System.arraycopy(mInputs, 0, p.mInputs, 0, mInputCount);
211 p.mOutputs = new Element[mOutputCount];
212 System.arraycopy(mOutputs, 0, p.mOutputs, 0, mOutputCount);
213 p.mConstants = new Type[mConstantCount];
214 System.arraycopy(mConstants, 0, p.mConstants, 0, mConstantCount);
Jason Sams7e5ab3b2009-12-15 13:27:04 -0800215 p.mTextureCount = mTextureCount;
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800216 p.mTextures = new TextureType[mTextureCount];
217 System.arraycopy(mTextureTypes, 0, p.mTextures, 0, mTextureCount);
Jason Sams0011bcf2009-12-15 12:58:36 -0800218 }
219 }
220
221}
222
223