diff options
Diffstat (limited to 'graphics/java/android/renderscript/Program.java')
-rw-r--r-- | graphics/java/android/renderscript/Program.java | 52 |
1 files changed, 48 insertions, 4 deletions
diff --git a/graphics/java/android/renderscript/Program.java b/graphics/java/android/renderscript/Program.java index 1614ec590cbd..c6ed72a53d84 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,8 +43,7 @@ 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) { @@ -91,8 +95,47 @@ 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 { @@ -120,12 +163,13 @@ public class Program extends BaseObj { 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) { throw new IllegalArgumentException("Max texture count exceeded."); } mTextureCount = count; + return this; } protected void initProgram(Program p) { |