summaryrefslogtreecommitdiff
path: root/graphics/java/android/renderscript/Program.java
diff options
context:
space:
mode:
Diffstat (limited to 'graphics/java/android/renderscript/Program.java')
-rw-r--r--graphics/java/android/renderscript/Program.java89
1 files changed, 78 insertions, 11 deletions
diff --git a/graphics/java/android/renderscript/Program.java b/graphics/java/android/renderscript/Program.java
index 1614ec590cbd..83c3601a484b 100644
--- a/graphics/java/android/renderscript/Program.java
+++ b/graphics/java/android/renderscript/Program.java
@@ -17,6 +17,11 @@
package android.renderscript;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.UnsupportedEncodingException;
+
+import android.content.res.Resources;
import android.util.Config;
import android.util.Log;
@@ -38,12 +43,18 @@ public class Program extends BaseObj {
String mShader;
Program(int id, RenderScript rs) {
- super(rs);
- mID = id;
+ super(id, rs);
}
public void bindConstants(Allocation a, int slot) {
- mRS.nProgramBindConstants(mID, slot, a.mID);
+ if (slot < 0 || slot >= mConstants.length) {
+ throw new IllegalArgumentException("Slot ID out of range.");
+ }
+ if (a != null &&
+ a.getType().getID() != mConstants[slot].getID()) {
+ throw new IllegalArgumentException("Allocation type does not match slot type.");
+ }
+ mRS.nProgramBindConstants(getID(), slot, a.getID());
}
public void bindTexture(Allocation va, int slot)
@@ -53,7 +64,7 @@ public class Program extends BaseObj {
throw new IllegalArgumentException("Slot ID out of range.");
}
- mRS.nProgramBindTexture(mID, slot, va.mID);
+ mRS.nProgramBindTexture(getID(), slot, va.getID());
}
public void bindSampler(Sampler vs, int slot)
@@ -63,7 +74,7 @@ public class Program extends BaseObj {
throw new IllegalArgumentException("Slot ID out of range.");
}
- mRS.nProgramBindSampler(mID, slot, vs.mID);
+ mRS.nProgramBindSampler(getID(), slot, vs.getID());
}
@@ -91,14 +102,56 @@ public class Program extends BaseObj {
mTextureCount = 0;
}
- public void setShader(String s) {
+ public BaseProgramBuilder setShader(String s) {
mShader = s;
+ return this;
+ }
+
+ public BaseProgramBuilder setShader(Resources resources, int resourceID) {
+ byte[] str;
+ int strLength;
+ InputStream is = resources.openRawResource(resourceID);
+ try {
+ try {
+ str = new byte[1024];
+ strLength = 0;
+ while(true) {
+ int bytesLeft = str.length - strLength;
+ if (bytesLeft == 0) {
+ byte[] buf2 = new byte[str.length * 2];
+ System.arraycopy(str, 0, buf2, 0, str.length);
+ str = buf2;
+ bytesLeft = str.length - strLength;
+ }
+ int bytesRead = is.read(str, strLength, bytesLeft);
+ if (bytesRead <= 0) {
+ break;
+ }
+ strLength += bytesRead;
+ }
+ } finally {
+ is.close();
+ }
+ } catch(IOException e) {
+ throw new Resources.NotFoundException();
+ }
+
+ try {
+ mShader = new String(str, 0, strLength, "UTF-8");
+ } catch (UnsupportedEncodingException e) {
+ Log.e("Renderscript shader creation", "Could not decode shader string");
+ }
+
+ return this;
}
public void addInput(Element e) throws IllegalStateException {
// Should check for consistant and non-conflicting names...
if(mInputCount >= MAX_INPUT) {
- throw new IllegalArgumentException("Max input count exceeded.");
+ throw new RSIllegalArgumentException("Max input count exceeded.");
+ }
+ if (e.isComplex()) {
+ throw new RSIllegalArgumentException("Complex elements not allowed.");
}
mInputs[mInputCount++] = e;
}
@@ -106,26 +159,40 @@ public class Program extends BaseObj {
public void addOutput(Element e) throws IllegalStateException {
// Should check for consistant and non-conflicting names...
if(mOutputCount >= MAX_OUTPUT) {
- throw new IllegalArgumentException("Max output count exceeded.");
+ throw new RSIllegalArgumentException("Max output count exceeded.");
+ }
+ if (e.isComplex()) {
+ throw new RSIllegalArgumentException("Complex elements not allowed.");
}
mOutputs[mOutputCount++] = e;
}
+ void resetConstant() {
+ mConstantCount = 0;
+ for(int i = 0; i < MAX_CONSTANT; i ++) {
+ mConstants[i] = null;
+ }
+ }
+
public int addConstant(Type t) throws IllegalStateException {
// Should check for consistant and non-conflicting names...
if(mConstantCount >= MAX_CONSTANT) {
- throw new IllegalArgumentException("Max input count exceeded.");
+ throw new RSIllegalArgumentException("Max input count exceeded.");
+ }
+ if (t.getElement().isComplex()) {
+ throw new RSIllegalArgumentException("Complex elements not allowed.");
}
mConstants[mConstantCount] = t;
return mConstantCount++;
}
- public void setTextureCount(int count) throws IllegalArgumentException {
+ public BaseProgramBuilder setTextureCount(int count) throws IllegalArgumentException {
// Should check for consistant and non-conflicting names...
- if(count >= MAX_CONSTANT) {
+ if(count >= MAX_TEXTURE) {
throw new IllegalArgumentException("Max texture count exceeded.");
}
mTextureCount = count;
+ return this;
}
protected void initProgram(Program p) {