Adding shader textures and removing specialized code.
Change-Id: Ie42928f9f7c1cee51ee2e0206b75e8239b75960a
diff --git a/tests/RenderScriptTests/SceneGraph/src/com/android/scenegraph/FragmentShader.java b/tests/RenderScriptTests/SceneGraph/src/com/android/scenegraph/FragmentShader.java
index fb81f73..0c6312f 100644
--- a/tests/RenderScriptTests/SceneGraph/src/com/android/scenegraph/FragmentShader.java
+++ b/tests/RenderScriptTests/SceneGraph/src/com/android/scenegraph/FragmentShader.java
@@ -56,9 +56,15 @@
return this;
}
+ public Builder addShaderTexture(Program.TextureType texType, String name) {
+ mShader.mShaderTextureNames.add(name);
+ mShader.mShaderTextureTypes.add(texType);
+ return this;
+ }
+
public Builder addTexture(Program.TextureType texType, String name) {
- mBuilder.addTexture(texType);
mShader.mTextureNames.add(name);
+ mShader.mTextureTypes.add(texType);
return this;
}
@@ -69,6 +75,13 @@
if (mShader.mPerObjConstants != null) {
mBuilder.addConstant(mShader.mPerObjConstants);
}
+ for (int i = 0; i < mShader.mTextureTypes.size(); i ++) {
+ mBuilder.addTexture(mShader.mTextureTypes.get(i));
+ }
+ for (int i = 0; i < mShader.mShaderTextureTypes.size(); i ++) {
+ mBuilder.addTexture(mShader.mShaderTextureTypes.get(i));
+ }
+
mShader.mProgram = mBuilder.create();
return mShader;
}
@@ -77,7 +90,18 @@
FragmentShader() {
}
- public ScriptField_FragmentShader_s getRSData(RenderScriptGL rs) {
+ public void updateTextures(RenderScriptGL rs, Resources res) {
+ int shaderTextureStart = mTextureTypes.size();
+ for (int i = 0; i < mShaderTextureNames.size(); i ++) {
+ ShaderParam sp = mSourceParams.get(mShaderTextureNames.get(i));
+ if (sp != null && sp instanceof TextureParam) {
+ TextureParam p = (TextureParam)sp;
+ mProgram.bindTexture(p.getTexture().getRsData(rs, res), shaderTextureStart + i);
+ }
+ }
+ }
+
+ public ScriptField_FragmentShader_s getRSData(RenderScriptGL rs, Resources res) {
if (mField != null) {
return mField;
}
@@ -85,7 +109,7 @@
ScriptField_FragmentShader_s.Item item = new ScriptField_FragmentShader_s.Item();
item.program = mProgram;
- linkConstants(rs);
+ linkConstants(rs, res);
if (mPerShaderConstants != null) {
item.shaderConst = mConstantBuffer;
item.shaderConstParams = mConstantBufferParams;
diff --git a/tests/RenderScriptTests/SceneGraph/src/com/android/scenegraph/RenderState.java b/tests/RenderScriptTests/SceneGraph/src/com/android/scenegraph/RenderState.java
index 2cc8c6b..8546e95 100644
--- a/tests/RenderScriptTests/SceneGraph/src/com/android/scenegraph/RenderState.java
+++ b/tests/RenderScriptTests/SceneGraph/src/com/android/scenegraph/RenderState.java
@@ -18,6 +18,7 @@
import java.lang.Math;
import java.util.ArrayList;
+import android.content.res.Resources;
import android.renderscript.Allocation;
import android.renderscript.Element;
@@ -75,14 +76,14 @@
mRaster = pr;
}
- public ScriptField_RenderState_s getRSData(RenderScriptGL rs) {
+ public ScriptField_RenderState_s getRSData(RenderScriptGL rs, Resources res) {
if (mField != null) {
return mField;
}
ScriptField_RenderState_s.Item item = new ScriptField_RenderState_s.Item();
- item.pv = mVertex.getRSData(rs).getAllocation();
- item.pf = mFragment.getRSData(rs).getAllocation();
+ item.pv = mVertex.getRSData(rs, res).getAllocation();
+ item.pf = mFragment.getRSData(rs, res).getAllocation();
item.ps = mStore;
item.pr = mRaster;
diff --git a/tests/RenderScriptTests/SceneGraph/src/com/android/scenegraph/Renderable.java b/tests/RenderScriptTests/SceneGraph/src/com/android/scenegraph/Renderable.java
index c73b32e..cf4a846 100644
--- a/tests/RenderScriptTests/SceneGraph/src/com/android/scenegraph/Renderable.java
+++ b/tests/RenderScriptTests/SceneGraph/src/com/android/scenegraph/Renderable.java
@@ -231,7 +231,7 @@
mRsFieldItem.transformMatrix = mTransform.getRSData(rs).getAllocation();
}
mRsFieldItem.name = SceneManager.getStringAsAllocation(rs, getName());
- mRsFieldItem.render_state = mRenderState.getRSData(rs).getAllocation();
+ mRsFieldItem.render_state = mRenderState.getRSData(rs, res).getAllocation();
mRsFieldItem.bVolInitialized = 0;
mRsFieldItem.cullType = mCullType;
}
diff --git a/tests/RenderScriptTests/SceneGraph/src/com/android/scenegraph/Scene.java b/tests/RenderScriptTests/SceneGraph/src/com/android/scenegraph/Scene.java
index 3d48547..653ce51 100644
--- a/tests/RenderScriptTests/SceneGraph/src/com/android/scenegraph/Scene.java
+++ b/tests/RenderScriptTests/SceneGraph/src/com/android/scenegraph/Scene.java
@@ -51,6 +51,22 @@
}
}
+ private class ShaderImageLoader extends AsyncTask<ArrayList<FragmentShader>, Void, Boolean> {
+ protected Boolean doInBackground(ArrayList<FragmentShader>... objects) {
+ long start = System.currentTimeMillis();
+ for (int i = 0; i < objects[0].size(); i ++) {
+ FragmentShader sI = objects[0].get(i);
+ sI.updateTextures(mRS, mRes);
+ }
+ long end = System.currentTimeMillis();
+ Log.v(TIMER_TAG, "Shader texture init time: " + (end - start));
+ return new Boolean(true);
+ }
+
+ protected void onPostExecute(Boolean result) {
+ }
+ }
+
CompoundTransform mRootTransforms;
HashMap<String, Transform> mTransformMap;
ArrayList<RenderPass> mRenderPasses;
@@ -210,6 +226,7 @@
}
public void initRenderPassRS(RenderScriptGL rs, SceneManager sceneManager) {
+ new ShaderImageLoader().execute(mFragmentShaders);
if (mRenderPasses.size() != 0) {
mRenderPassAlloc = new ScriptField_RenderPass_s(mRS, mRenderPasses.size());
for (int i = 0; i < mRenderPasses.size(); i ++) {
@@ -246,7 +263,7 @@
Allocation[] shaderAllocs = new Allocation[mVertexShaders.size()];
for (int i = 0; i < mVertexShaders.size(); i ++) {
VertexShader sI = mVertexShaders.get(i);
- shaderAllocs[i] = sI.getRSData(rs).getAllocation();
+ shaderAllocs[i] = sI.getRSData(rs, res).getAllocation();
}
shaderData.copyFrom(shaderAllocs);
sceneManager.mRenderLoop.set_gVertexShaders(shaderData);
@@ -255,7 +272,7 @@
shaderAllocs = new Allocation[mFragmentShaders.size()];
for (int i = 0; i < mFragmentShaders.size(); i ++) {
FragmentShader sI = mFragmentShaders.get(i);
- shaderAllocs[i] = sI.getRSData(rs).getAllocation();
+ shaderAllocs[i] = sI.getRSData(rs, res).getAllocation();
}
shaderData.copyFrom(shaderAllocs);
sceneManager.mRenderLoop.set_gFragmentShaders(shaderData);
diff --git a/tests/RenderScriptTests/SceneGraph/src/com/android/scenegraph/Shader.java b/tests/RenderScriptTests/SceneGraph/src/com/android/scenegraph/Shader.java
index 9713eb5..7541115 100644
--- a/tests/RenderScriptTests/SceneGraph/src/com/android/scenegraph/Shader.java
+++ b/tests/RenderScriptTests/SceneGraph/src/com/android/scenegraph/Shader.java
@@ -22,6 +22,7 @@
import android.content.res.Resources;
import android.renderscript.*;
+import android.renderscript.ProgramFragment.Builder;
import android.util.Log;
/**
@@ -33,7 +34,10 @@
protected HashMap<String, ShaderParam> mSourceParams;
protected ArrayList<ShaderParam> mParamList;
+ protected ArrayList<String> mShaderTextureNames;
+ protected ArrayList<Program.TextureType > mShaderTextureTypes;
protected ArrayList<String> mTextureNames;
+ protected ArrayList<Program.TextureType > mTextureTypes;
protected Allocation mConstantBuffer;
protected Allocation mConstantBufferParams;
@@ -41,7 +45,10 @@
public Shader() {
mSourceParams = new HashMap<String, ShaderParam>();
mParamList = new ArrayList<ShaderParam>();
+ mShaderTextureNames = new ArrayList<String>();
+ mShaderTextureTypes = new ArrayList<Program.TextureType>();
mTextureNames = new ArrayList<String>();
+ mTextureTypes = new ArrayList<Program.TextureType>();
}
public void appendSourceParams(ShaderParam p) {
@@ -56,7 +63,7 @@
return mPerObjConstants;
}
- void linkConstants(RenderScriptGL rs) {
+ void linkConstants(RenderScriptGL rs, Resources res) {
if (mPerShaderConstants == null) {
return;
}
diff --git a/tests/RenderScriptTests/SceneGraph/src/com/android/scenegraph/TestAppRS.java b/tests/RenderScriptTests/SceneGraph/src/com/android/scenegraph/TestAppRS.java
index b334b0a..087b776 100644
--- a/tests/RenderScriptTests/SceneGraph/src/com/android/scenegraph/TestAppRS.java
+++ b/tests/RenderScriptTests/SceneGraph/src/com/android/scenegraph/TestAppRS.java
@@ -78,9 +78,8 @@
private VertexShader mPaintV;
private Allocation mDefaultCube;
- private Allocation mAllocPV;
- private Allocation mEnvCube;
- private Allocation mDiffCube;
+ private TextureCube mEnvCube;
+ private TextureCube mDiffCube;
Scene mActiveScene;
@@ -135,35 +134,6 @@
mSceneManager.loadModel(path, mLoadedCallback);
}
- // We use this to laod environment maps off the UI thread
- private class ImageLoaderTask extends AsyncTask<String, Void, Boolean> {
- Allocation tempEnv;
- Allocation tempDiff;
-
- protected Boolean doInBackground(String... names) {
- long start = System.currentTimeMillis();
-
- tempEnv = SceneManager.loadCubemap("sdcard/scenegraph/cube_env.png", mRS, mRes);
- tempDiff = SceneManager.loadCubemap("sdcard/scenegraph/cube_spec.png", mRS, mRes);
-
- long end = System.currentTimeMillis();
- Log.v("TIMER", "Image load time: " + (end - start));
- return new Boolean(true);
- }
-
- protected void onPostExecute(Boolean result) {
- if (tempEnv != null) {
- mEnvCube = tempEnv;
- mPaintF.mProgram.bindTexture(mEnvCube, 1);
- }
-
- if (tempDiff != null) {
- mDiffCube = tempDiff;
- mAluminumF.mProgram.bindTexture(mDiffCube, 1);
- }
- }
- }
-
public void onActionDown(float x, float y) {
mTouchHandler.onActionDown(x, y);
@@ -184,7 +154,7 @@
fb.setShader(mRes, id);
fb.addTexture(TextureType.TEXTURE_2D, "diffuse");
if (addCubemap) {
- fb.addTexture(TextureType.TEXTURE_CUBE, "reflection");
+ fb.addShaderTexture(TextureType.TEXTURE_CUBE, "reflection");
}
FragmentShader pf = fb.create();
pf.mProgram.bindSampler(Sampler.WRAP_LINEAR_MIP_LINEAR(mRS), 0);
@@ -209,7 +179,9 @@
mFsConst2 = new ScriptField_FShaderLightParams_s(mRS, 1);
mPaintF = createFromResource(R.raw.paintf, true);
+ mPaintF.appendSourceParams(new TextureParam("reflection", mEnvCube));
mAluminumF = createFromResource(R.raw.metal, true);
+ mAluminumF.appendSourceParams(new TextureParam("reflection", mDiffCube));
mPlasticF = createFromResource(R.raw.plastic, false);
mDiffuseF = createFromResource(R.raw.diffuse, false);
@@ -307,15 +279,10 @@
private void initRS() {
FullscreenBlur.createRenderTargets(mRS, mWidth, mHeight);
- initPaintShaders();
-
- Bitmap b = BitmapFactory.decodeResource(mRes, R.drawable.defaultcube);
- mDefaultCube = Allocation.createCubemapFromBitmap(mRS, b);
- mPaintF.mProgram.bindTexture(mDefaultCube, 1);
- mAluminumF.mProgram.bindTexture(mDefaultCube, 1);
-
// Reflection maps from SD card
- new ImageLoaderTask().execute();
+ mEnvCube = new TextureCube("sdcard/scenegraph/", "cube_env.png");
+ mDiffCube = new TextureCube("sdcard/scenegraph/", "cube_spec.png");
+ initPaintShaders();
ScriptC_render renderLoop = mSceneManager.getRenderLoop();
diff --git a/tests/RenderScriptTests/SceneGraph/src/com/android/scenegraph/TextureCube.java b/tests/RenderScriptTests/SceneGraph/src/com/android/scenegraph/TextureCube.java
index fad52d0..e338be4 100644
--- a/tests/RenderScriptTests/SceneGraph/src/com/android/scenegraph/TextureCube.java
+++ b/tests/RenderScriptTests/SceneGraph/src/com/android/scenegraph/TextureCube.java
@@ -39,6 +39,11 @@
setTexture(tex);
}
+ public TextureCube(String dir, String file) {
+ setFileDir(dir);
+ setFileName(file);
+ }
+
public void setFileDir(String dir) {
mFileDir = dir;
}
diff --git a/tests/RenderScriptTests/SceneGraph/src/com/android/scenegraph/TextureParam.java b/tests/RenderScriptTests/SceneGraph/src/com/android/scenegraph/TextureParam.java
index df7147a..d7d196b 100644
--- a/tests/RenderScriptTests/SceneGraph/src/com/android/scenegraph/TextureParam.java
+++ b/tests/RenderScriptTests/SceneGraph/src/com/android/scenegraph/TextureParam.java
@@ -34,22 +34,22 @@
*/
public class TextureParam extends ShaderParam {
- Texture2D mTexture;
+ TextureBase mTexture;
public TextureParam(String name) {
super(name);
}
- public TextureParam(String name, Texture2D t) {
+ public TextureParam(String name, TextureBase t) {
super(name);
setTexture(t);
}
- public void setTexture(Texture2D t) {
+ public void setTexture(TextureBase t) {
mTexture = t;
}
- public Texture2D getTexture() {
+ public TextureBase getTexture() {
return mTexture;
}
diff --git a/tests/RenderScriptTests/SceneGraph/src/com/android/scenegraph/VertexShader.java b/tests/RenderScriptTests/SceneGraph/src/com/android/scenegraph/VertexShader.java
index 7bf806e..4351f66 100644
--- a/tests/RenderScriptTests/SceneGraph/src/com/android/scenegraph/VertexShader.java
+++ b/tests/RenderScriptTests/SceneGraph/src/com/android/scenegraph/VertexShader.java
@@ -74,7 +74,7 @@
VertexShader() {
}
- public ScriptField_VertexShader_s getRSData(RenderScriptGL rs) {
+ public ScriptField_VertexShader_s getRSData(RenderScriptGL rs, Resources res) {
if (mField != null) {
return mField;
}
@@ -82,7 +82,7 @@
ScriptField_VertexShader_s.Item item = new ScriptField_VertexShader_s.Item();
item.program = mProgram;
- linkConstants(rs);
+ linkConstants(rs, res);
if (mPerShaderConstants != null) {
item.shaderConst = mConstantBuffer;
item.shaderConstParams = mConstantBufferParams;