blob: a9eaec33385d65cfd113caecfe2c47570bb4749e [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
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -080039 public enum TextureType {
40 TEXTURE_2D (0),
41 TEXTURE_CUBE (1);
42
43 int mID;
44 TextureType(int id) {
45 mID = id;
46 }
47 }
48
49 enum ProgramParam {
50 INPUT (0),
51 OUTPUT (1),
52 CONSTANT (2),
53 TEXTURE_TYPE (3);
54
55 int mID;
56 ProgramParam(int id) {
57 mID = id;
58 }
59 };
60
Jason Sams0011bcf2009-12-15 12:58:36 -080061 Element mInputs[];
62 Element mOutputs[];
63 Type mConstants[];
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -080064 TextureType mTextures[];
Jason Sams7e5ab3b2009-12-15 13:27:04 -080065 int mTextureCount;
Jason Sams0011bcf2009-12-15 12:58:36 -080066 String mShader;
67
68 Program(int id, RenderScript rs) {
Alex Sakhartchouk0de94442010-08-11 14:41:28 -070069 super(id, rs);
Jason Sams0011bcf2009-12-15 12:58:36 -080070 }
71
72 public void bindConstants(Allocation a, int slot) {
Jason Samsc1d62102010-11-04 14:32:19 -070073 if (slot < 0 || slot >= mConstants.length) {
74 throw new IllegalArgumentException("Slot ID out of range.");
75 }
76 if (a != null &&
77 a.getType().getID() != mConstants[slot].getID()) {
78 throw new IllegalArgumentException("Allocation type does not match slot type.");
79 }
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -080080 int id = a != null ? a.getID() : 0;
81 mRS.nProgramBindConstants(getID(), slot, id);
Jason Sams0011bcf2009-12-15 12:58:36 -080082 }
83
Jason Sams68afd012009-12-17 16:55:08 -080084 public void bindTexture(Allocation va, int slot)
85 throws IllegalArgumentException {
86 mRS.validate();
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -080087 if ((slot < 0) || (slot >= mTextureCount)) {
Jason Sams68afd012009-12-17 16:55:08 -080088 throw new IllegalArgumentException("Slot ID out of range.");
89 }
Jason Samsbf6ef8d2010-12-06 15:59:59 -080090 if (va != null && va.getType().hasFaces() &&
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -080091 mTextures[slot] != TextureType.TEXTURE_CUBE) {
92 throw new IllegalArgumentException("Cannot bind cubemap to 2d texture slot");
93 }
Jason Sams68afd012009-12-17 16:55:08 -080094
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -080095 int id = va != null ? va.getID() : 0;
96 mRS.nProgramBindTexture(getID(), slot, id);
Jason Sams68afd012009-12-17 16:55:08 -080097 }
98
99 public void bindSampler(Sampler vs, int slot)
100 throws IllegalArgumentException {
101 mRS.validate();
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800102 if ((slot < 0) || (slot >= mTextureCount)) {
Jason Sams68afd012009-12-17 16:55:08 -0800103 throw new IllegalArgumentException("Slot ID out of range.");
104 }
105
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800106 int id = vs != null ? vs.getID() : 0;
107 mRS.nProgramBindSampler(getID(), slot, id);
Jason Sams68afd012009-12-17 16:55:08 -0800108 }
109
110
Jason Sams0011bcf2009-12-15 12:58:36 -0800111 public static class BaseProgramBuilder {
112 RenderScript mRS;
113 Element mInputs[];
114 Element mOutputs[];
115 Type mConstants[];
116 Type mTextures[];
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800117 TextureType mTextureTypes[];
Jason Sams0011bcf2009-12-15 12:58:36 -0800118 int mInputCount;
119 int mOutputCount;
120 int mConstantCount;
121 int mTextureCount;
122 String mShader;
123
124
125 protected BaseProgramBuilder(RenderScript rs) {
126 mRS = rs;
127 mInputs = new Element[MAX_INPUT];
128 mOutputs = new Element[MAX_OUTPUT];
129 mConstants = new Type[MAX_CONSTANT];
130 mInputCount = 0;
131 mOutputCount = 0;
132 mConstantCount = 0;
Jason Sams7e5ab3b2009-12-15 13:27:04 -0800133 mTextureCount = 0;
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800134 mTextureTypes = new TextureType[MAX_TEXTURE];
Jason Sams0011bcf2009-12-15 12:58:36 -0800135 }
136
Jim Shuma288c8712010-07-07 14:24:21 -0700137 public BaseProgramBuilder setShader(String s) {
Jason Sams0011bcf2009-12-15 12:58:36 -0800138 mShader = s;
Jim Shuma288c8712010-07-07 14:24:21 -0700139 return this;
Jason Sams0011bcf2009-12-15 12:58:36 -0800140 }
141
Alex Sakhartchouka41174e2010-08-27 16:10:55 -0700142 public BaseProgramBuilder setShader(Resources resources, int resourceID) {
143 byte[] str;
144 int strLength;
145 InputStream is = resources.openRawResource(resourceID);
146 try {
147 try {
148 str = new byte[1024];
149 strLength = 0;
150 while(true) {
151 int bytesLeft = str.length - strLength;
152 if (bytesLeft == 0) {
153 byte[] buf2 = new byte[str.length * 2];
154 System.arraycopy(str, 0, buf2, 0, str.length);
155 str = buf2;
156 bytesLeft = str.length - strLength;
157 }
158 int bytesRead = is.read(str, strLength, bytesLeft);
159 if (bytesRead <= 0) {
160 break;
161 }
162 strLength += bytesRead;
163 }
164 } finally {
165 is.close();
166 }
167 } catch(IOException e) {
168 throw new Resources.NotFoundException();
169 }
170
171 try {
172 mShader = new String(str, 0, strLength, "UTF-8");
173 } catch (UnsupportedEncodingException e) {
174 Log.e("Renderscript shader creation", "Could not decode shader string");
175 }
176
177 return this;
178 }
179
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800180 public int getCurrentConstantIndex() {
181 return mConstantCount - 1;
Jason Sams0011bcf2009-12-15 12:58:36 -0800182 }
183
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800184 public int getCurrentTextureIndex() {
185 return mTextureCount - 1;
Jason Sams0011bcf2009-12-15 12:58:36 -0800186 }
187
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800188 public BaseProgramBuilder addConstant(Type t) throws IllegalStateException {
Jason Sams0011bcf2009-12-15 12:58:36 -0800189 // Should check for consistant and non-conflicting names...
190 if(mConstantCount >= MAX_CONSTANT) {
Jason Samsc1d62102010-11-04 14:32:19 -0700191 throw new RSIllegalArgumentException("Max input count exceeded.");
192 }
193 if (t.getElement().isComplex()) {
194 throw new RSIllegalArgumentException("Complex elements not allowed.");
Jason Sams0011bcf2009-12-15 12:58:36 -0800195 }
Jason Samsea87e962010-01-12 12:12:28 -0800196 mConstants[mConstantCount] = t;
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800197 mConstantCount++;
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800198 return this;
199 }
200
201 public BaseProgramBuilder addTexture(TextureType texType) throws IllegalArgumentException {
202 if(mTextureCount >= MAX_TEXTURE) {
203 throw new IllegalArgumentException("Max texture count exceeded.");
204 }
205 mTextureTypes[mTextureCount ++] = texType;
Jim Shuma288c8712010-07-07 14:24:21 -0700206 return this;
Jason Sams0011bcf2009-12-15 12:58:36 -0800207 }
208
209 protected void initProgram(Program p) {
210 p.mInputs = new Element[mInputCount];
211 System.arraycopy(mInputs, 0, p.mInputs, 0, mInputCount);
212 p.mOutputs = new Element[mOutputCount];
213 System.arraycopy(mOutputs, 0, p.mOutputs, 0, mOutputCount);
214 p.mConstants = new Type[mConstantCount];
215 System.arraycopy(mConstants, 0, p.mConstants, 0, mConstantCount);
Jason Sams7e5ab3b2009-12-15 13:27:04 -0800216 p.mTextureCount = mTextureCount;
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800217 p.mTextures = new TextureType[mTextureCount];
218 System.arraycopy(mTextureTypes, 0, p.mTextures, 0, mTextureCount);
Jason Sams0011bcf2009-12-15 12:58:36 -0800219 }
220 }
221
222}
223
224