From 61440e8a8bc5d0d3c7edc66eccb609e5f07fea6e Mon Sep 17 00:00:00 2001 From: Alex Sakhartchouk Date: Tue, 17 Jan 2012 09:49:44 -0800 Subject: Finishing light linking. Change-Id: I5d76115410bf0b9a7ea882e57242a0b0bba6ddcc --- .../src/com/android/scenegraph/Float4Param.java | 36 +++++++++++++++++--- .../src/com/android/scenegraph/Renderable.java | 10 +++--- .../src/com/android/scenegraph/Scene.java | 4 +++ .../src/com/android/scenegraph/SceneManager.java | 38 ++++++++++++++++------ .../src/com/android/scenegraph/TestAppRS.java | 14 +++++++- .../src/com/android/scenegraph/export.rs | 2 +- .../src/com/android/scenegraph/params.rs | 33 +++++++++++++++++-- .../src/com/android/scenegraph/render.rs | 1 + .../src/com/android/scenegraph/transform_def.rsh | 13 ++++++-- 9 files changed, 126 insertions(+), 25 deletions(-) (limited to 'tests/RenderScriptTests/SceneGraph/src') diff --git a/tests/RenderScriptTests/SceneGraph/src/com/android/scenegraph/Float4Param.java b/tests/RenderScriptTests/SceneGraph/src/com/android/scenegraph/Float4Param.java index f3f69e0..57294da 100644 --- a/tests/RenderScriptTests/SceneGraph/src/com/android/scenegraph/Float4Param.java +++ b/tests/RenderScriptTests/SceneGraph/src/com/android/scenegraph/Float4Param.java @@ -19,19 +19,23 @@ package com.android.scenegraph; import java.lang.Math; import java.util.ArrayList; -import android.renderscript.RenderScriptGL; +import com.android.scenegraph.Scene; +import com.android.scenegraph.SceneManager; + +import android.renderscript.Element; import android.renderscript.Float4; import android.renderscript.Matrix4f; import android.renderscript.ProgramFragment; import android.renderscript.ProgramStore; import android.renderscript.ProgramVertex; -import android.renderscript.Element; +import android.renderscript.RenderScriptGL; import android.util.Log; /** * @hide */ public class Float4Param extends ShaderParam { + private static String TAG = "Float4Param"; Float4 mValue; Camera mCamera; @@ -97,17 +101,39 @@ public class Float4Param extends ShaderParam { mLight = l; } + boolean findLight(String property) { + String indexStr = mParamName.substring(property.length() + 1); + if (indexStr == null) { + Log.e(TAG, "Invalid light index."); + return false; + } + int index = Integer.parseInt(indexStr); + if (index == -1) { + return false; + } + Scene parentScene = SceneManager.getInstance().getActiveScene(); + ArrayList allLights = parentScene.getLights(); + if (index >= allLights.size()) { + return false; + } + mLight = allLights.get(index); + if (mLight == null) { + return false; + } + return true; + } + int getTypeFromName() { int paramType = FLOAT4_DATA; if (mParamName.equalsIgnoreCase(cameraPos)) { paramType = FLOAT4_CAMERA_POS; } else if(mParamName.equalsIgnoreCase(cameraDir)) { paramType = FLOAT4_CAMERA_DIR; - } else if(mParamName.startsWith(lightColor)) { + } else if(mParamName.startsWith(lightColor) && findLight(lightColor)) { paramType = FLOAT4_LIGHT_COLOR; - } else if(mParamName.startsWith(lightPos)) { + } else if(mParamName.startsWith(lightPos) && findLight(lightPos)) { paramType = FLOAT4_LIGHT_POS; - } else if(mParamName.startsWith(lightDir)) { + } else if(mParamName.startsWith(lightDir) && findLight(lightDir)) { paramType = FLOAT4_LIGHT_DIR; } return paramType; diff --git a/tests/RenderScriptTests/SceneGraph/src/com/android/scenegraph/Renderable.java b/tests/RenderScriptTests/SceneGraph/src/com/android/scenegraph/Renderable.java index d042b77..3fa2a51 100644 --- a/tests/RenderScriptTests/SceneGraph/src/com/android/scenegraph/Renderable.java +++ b/tests/RenderScriptTests/SceneGraph/src/com/android/scenegraph/Renderable.java @@ -167,13 +167,11 @@ public class Renderable extends RenderableBase { String inputName = constantElem.getSubElementName(i); int offset = constantElem.getSubElementOffsetBytes(i); ShaderParam matchingParam = findParamByName(inputName); + Element subElem = constantElem.getSubElement(i); // Make one if it's not there if (matchingParam == null) { - Element subElem = constantElem.getSubElement(i); if (subElem.getDataType() == Element.DataType.FLOAT_32) { - Float4Param fParam = new Float4Param(inputName); - fParam.setVecSize(subElem.getVectorSize()); - matchingParam = fParam; + matchingParam = new Float4Param(inputName); } else if (subElem.getDataType() == Element.DataType.MATRIX_4X4) { TransformParam trParam = new TransformParam(inputName); trParam.setTransform(mTransform); @@ -181,6 +179,10 @@ public class Renderable extends RenderableBase { } } matchingParam.setOffset(offset); + if (subElem.getDataType() == Element.DataType.FLOAT_32) { + Float4Param fParam = (Float4Param)matchingParam; + fParam.setVecSize(subElem.getVectorSize()); + } paramList.add(matchingParam); } } diff --git a/tests/RenderScriptTests/SceneGraph/src/com/android/scenegraph/Scene.java b/tests/RenderScriptTests/SceneGraph/src/com/android/scenegraph/Scene.java index d5d374c..f00bf13 100644 --- a/tests/RenderScriptTests/SceneGraph/src/com/android/scenegraph/Scene.java +++ b/tests/RenderScriptTests/SceneGraph/src/com/android/scenegraph/Scene.java @@ -116,6 +116,10 @@ public class Scene extends SceneGraphBase { return mCameras; } + public ArrayList getLights() { + return mLights; + } + public void appendRenderable(RenderableBase d) { mRenderables.add(d); mRenderableMap.put(d.getName(), d); diff --git a/tests/RenderScriptTests/SceneGraph/src/com/android/scenegraph/SceneManager.java b/tests/RenderScriptTests/SceneGraph/src/com/android/scenegraph/SceneManager.java index 4321bd8..5232a64 100644 --- a/tests/RenderScriptTests/SceneGraph/src/com/android/scenegraph/SceneManager.java +++ b/tests/RenderScriptTests/SceneGraph/src/com/android/scenegraph/SceneManager.java @@ -16,30 +16,30 @@ package com.android.scenegraph; +import java.io.BufferedInputStream; import java.io.File; +import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; -import java.io.FileInputStream; -import java.io.BufferedInputStream; import java.io.Writer; - import java.lang.Math; import java.util.ArrayList; import java.util.HashMap; import java.util.regex.Matcher; import java.util.regex.Pattern; +import com.android.scenegraph.Scene; + +import android.content.res.Resources; import android.graphics.Bitmap; import android.graphics.BitmapFactory; - -import android.renderscript.RenderScriptGL; -import android.renderscript.Mesh; +import android.os.AsyncTask; import android.renderscript.*; import android.renderscript.Allocation.MipmapControl; -import android.content.res.Resources; -import android.view.SurfaceHolder; +import android.renderscript.Mesh; +import android.renderscript.RenderScriptGL; import android.util.Log; -import android.os.AsyncTask; +import android.view.SurfaceHolder; /** * @hide @@ -58,6 +58,9 @@ public class SceneManager extends SceneGraphBase { int mWidth; int mHeight; + Scene mActiveScene; + private static SceneManager sSceneManager; + public static boolean isSDCardPath(String path) { int sdCardIndex = path.indexOf("sdcard/"); // We are looking for /sdcard/ or sdcard/ @@ -140,7 +143,22 @@ public class SceneManager extends SceneGraphBase { } } - public SceneManager() { + public Scene getActiveScene() { + return mActiveScene; + } + + public void setActiveScene(Scene s) { + mActiveScene = s; + } + + public static SceneManager getInstance() { + if (sSceneManager == null) { + sSceneManager = new SceneManager(); + } + return sSceneManager; + } + + protected SceneManager() { } public void loadModel(String name, SceneLoadedCallback cb) { diff --git a/tests/RenderScriptTests/SceneGraph/src/com/android/scenegraph/TestAppRS.java b/tests/RenderScriptTests/SceneGraph/src/com/android/scenegraph/TestAppRS.java index 6390cb8..3e88afb 100644 --- a/tests/RenderScriptTests/SceneGraph/src/com/android/scenegraph/TestAppRS.java +++ b/tests/RenderScriptTests/SceneGraph/src/com/android/scenegraph/TestAppRS.java @@ -63,11 +63,13 @@ public class TestAppRS { private RenderScriptGL mRS; private ProgramFragment mPF_Paint; + private ProgramFragment mPF_Lights; private ProgramFragment mPF_Aluminum; private ProgramFragment mPF_Plastic; private ProgramFragment mPF_Diffuse; private ProgramFragment mPF_Texture; ScriptField_FShaderParams_s mFsConst; + ScriptField_FShaderLightParams_s mFsConst2; private ProgramVertex mPV_Paint; ScriptField_VShaderParams_s mVsConst; @@ -107,7 +109,7 @@ public class TestAppRS { mTouchHandler = new TouchHandler(); - mSceneManager = new SceneManager(); + mSceneManager = SceneManager.getInstance(); // Initializes all the RS specific scenegraph elements mSceneManager.initRS(mRS, mRes, mWidth, mHeight); @@ -199,6 +201,7 @@ public class TestAppRS { mPV_Paint = vb.create(); mFsConst = new ScriptField_FShaderParams_s(mRS, 1); + mFsConst2 = new ScriptField_FShaderLightParams_s(mRS, 1); mPF_Paint = createFromResource(R.raw.paintf, true); mPF_Aluminum = createFromResource(R.raw.metal, true); @@ -207,6 +210,11 @@ public class TestAppRS { mPF_Diffuse = createFromResource(R.raw.diffuse, false); mPF_Texture = createFromResource(R.raw.texture, false); + ProgramFragment.Builder fb = new ProgramFragment.Builder(mRS); + fb.addConstant(mFsConst2.getAllocation().getType()); + fb.setShader(mRes, R.raw.plastic_lights); + mPF_Lights = fb.create(); + FullscreenBlur.initShaders(mRes, mRS, mVsConst, mFsConst); } @@ -235,11 +243,13 @@ public class TestAppRS { } public void prepareToRender(Scene s) { + mSceneManager.setActiveScene(s); mActiveScene = s; RenderState plastic = new RenderState(mPV_Paint, mPF_Plastic, null, null); RenderState diffuse = new RenderState(mPV_Paint, mPF_Diffuse, null, null); RenderState paint = new RenderState(mPV_Paint, mPF_Paint, null, null); RenderState aluminum = new RenderState(mPV_Paint, mPF_Aluminum, null, null); + RenderState lights = new RenderState(mPV_Paint, mPF_Lights, null, null); RenderState glassTransp = new RenderState(mPV_Paint, mPF_Paint, ProgramStore.BLEND_ALPHA_DEPTH_TEST(mRS), @@ -261,6 +271,8 @@ public class TestAppRS { mActiveScene.assignRenderStateToMaterial(glassTransp, "^#GlassLight"); + mActiveScene.assignRenderStateToMaterial(lights, "^#LightBlinn"); + Renderable plane = (Renderable)mActiveScene.getRenderableByName("pPlaneShape1"); if (plane != null) { RenderState texState = new RenderState(mPV_Paint, mPF_Texture, null, null); diff --git a/tests/RenderScriptTests/SceneGraph/src/com/android/scenegraph/export.rs b/tests/RenderScriptTests/SceneGraph/src/com/android/scenegraph/export.rs index 1065d87..0976afa 100644 --- a/tests/RenderScriptTests/SceneGraph/src/com/android/scenegraph/export.rs +++ b/tests/RenderScriptTests/SceneGraph/src/com/android/scenegraph/export.rs @@ -31,4 +31,4 @@ VertexShaderInputs *iExport; VShaderParams *vConst; FShaderParams *fConst; - +FShaderLightParams *fConts2; diff --git a/tests/RenderScriptTests/SceneGraph/src/com/android/scenegraph/params.rs b/tests/RenderScriptTests/SceneGraph/src/com/android/scenegraph/params.rs index 873c7e6..61dc482 100644 --- a/tests/RenderScriptTests/SceneGraph/src/com/android/scenegraph/params.rs +++ b/tests/RenderScriptTests/SceneGraph/src/com/android/scenegraph/params.rs @@ -18,7 +18,14 @@ #include "transform_def.rsh" +//#define DEBUG_PARAMS + static void writeFloatData(float *ptr, const float4 *input, uint32_t vecSize) { +#ifdef DEBUG_PARAMS + rsDebug("Writing value ", *input); + rsDebug("Writing vec size ", vecSize); +#endif // DEBUG_PARAMS + switch (vecSize) { case 1: *ptr = input->x; @@ -39,10 +46,28 @@ static void writeFloatData(float *ptr, const float4 *input, uint32_t vecSize) { } static void processParam(SgShaderParam *p, uint8_t *constantBuffer, const SgCamera *currentCam) { +#ifdef DEBUG_PARAMS + rsDebug("____________ Param bufferOffset", p->bufferOffset); + rsDebug("Param Type ", p->type); +#endif // DEBUG_PARAMS + uint8_t *dataPtr = constantBuffer + p->bufferOffset; const SgTransform *pTransform = NULL; if (rsIsObject(p->transform)) { pTransform = (const SgTransform *)rsGetElementAt(p->transform, 0); + +#ifdef DEBUG_PARAMS + rsDebug("Param transform", pTransform); + printName(pTransform->name); +#endif // DEBUG_PARAMS + } + + const SgLight *pLight = NULL; + if (rsIsObject(p->light)) { + pLight = (const SgLight *)rsGetElementAt(p->light, 0); +#ifdef DEBUG_PARAMS + printLightInfo(pLight); +#endif // DEBUG_PARAMS } switch(p->type) { @@ -53,8 +78,12 @@ static void processParam(SgShaderParam *p, uint8_t *constantBuffer, const SgCame writeFloatData((float*)dataPtr, ¤tCam->position, p->float_vecSize); break; case SHADER_PARAM_FLOAT4_CAMERA_DIR: break; - case SHADER_PARAM_FLOAT4_LIGHT_COLOR: break; - case SHADER_PARAM_FLOAT4_LIGHT_POS: break; + case SHADER_PARAM_FLOAT4_LIGHT_COLOR: + writeFloatData((float*)dataPtr, &pLight->color, p->float_vecSize); + break; + case SHADER_PARAM_FLOAT4_LIGHT_POS: + writeFloatData((float*)dataPtr, &pLight->position, p->float_vecSize); + break; case SHADER_PARAM_FLOAT4_LIGHT_DIR: break; case SHADER_PARAM_TRANSFORM_DATA: diff --git a/tests/RenderScriptTests/SceneGraph/src/com/android/scenegraph/render.rs b/tests/RenderScriptTests/SceneGraph/src/com/android/scenegraph/render.rs index c63b680..e9077f2 100644 --- a/tests/RenderScriptTests/SceneGraph/src/com/android/scenegraph/render.rs +++ b/tests/RenderScriptTests/SceneGraph/src/com/android/scenegraph/render.rs @@ -154,6 +154,7 @@ void root(const void *v_in, void *v_out) { rsForEach(gTransformScript, gRootNode->children, nullAlloc, 0, 0); prepareCameras(); + prepareLights(); rsgClearDepth(1.0f); diff --git a/tests/RenderScriptTests/SceneGraph/src/com/android/scenegraph/transform_def.rsh b/tests/RenderScriptTests/SceneGraph/src/com/android/scenegraph/transform_def.rsh index 20d63bb..2c81151 100644 --- a/tests/RenderScriptTests/SceneGraph/src/com/android/scenegraph/transform_def.rsh +++ b/tests/RenderScriptTests/SceneGraph/src/com/android/scenegraph/transform_def.rsh @@ -161,6 +161,15 @@ typedef struct FShaderParams_s { float4 cameraPos; } FShaderParams; +typedef struct FShaderLightParams_s { + float4 lightPos_0; + float4 lightColor_0; + float4 lightPos_1; + float4 lightColor_1; + float4 cameraPos; + float4 diffuse; +} FShaderLightParams; + typedef struct FBlurOffsets_s { float blurOffset0; float blurOffset1; @@ -174,7 +183,7 @@ typedef struct VertexShaderInputs_s { float2 texture0; } VertexShaderInputs; -static void printCameraInfo(SgCamera *cam) { +static void printCameraInfo(const SgCamera *cam) { rsDebug("***** Camera information. ptr:", cam); printName(cam->name); const SgTransform *camTransform = (const SgTransform *)rsGetElementAt(cam->transformMatrix, 0); @@ -190,7 +199,7 @@ static void printCameraInfo(SgCamera *cam) { rsDebug("View: ", &cam->view); } -static void printLightInfo(SgLight *light) { +static void printLightInfo(const SgLight *light) { rsDebug("***** Light information. ptr:", light); printName(light->name); const SgTransform *lTransform = (const SgTransform *)rsGetElementAt(light->transformMatrix, 0); -- cgit v1.1