Merge "Now utilizing ability to bind constants. Fixing small copy/paste typo." into graphics-dev
diff --git a/libs/rs/rsScriptC_LibGL.cpp b/libs/rs/rsScriptC_LibGL.cpp
index dece363..97469d3 100644
--- a/libs/rs/rsScriptC_LibGL.cpp
+++ b/libs/rs/rsScriptC_LibGL.cpp
@@ -56,10 +56,10 @@
     pf->bindAllocation(rsc, a, slot);
 }
 
-void rsrBindConstant(Context *rsc, Script *sc, ProgramVertex *pf, uint32_t slot, Allocation *a) {
+void rsrBindConstant(Context *rsc, Script *sc, ProgramVertex *pv, uint32_t slot, Allocation *a) {
     CHECK_OBJ_OR_NULL(a);
-    CHECK_OBJ(pf);
-    pf->bindAllocation(rsc, a, slot);
+    CHECK_OBJ(pv);
+    pv->bindAllocation(rsc, a, slot);
 }
 
 void rsrBindSampler(Context *rsc, Script *sc, ProgramFragment *pf, uint32_t slot, Sampler *s) {
diff --git a/tests/RenderScriptTests/SceneGraph/src/com/android/scenegraph/TestAppRS.java b/tests/RenderScriptTests/SceneGraph/src/com/android/scenegraph/TestAppRS.java
index 8987408..58af120 100644
--- a/tests/RenderScriptTests/SceneGraph/src/com/android/scenegraph/TestAppRS.java
+++ b/tests/RenderScriptTests/SceneGraph/src/com/android/scenegraph/TestAppRS.java
@@ -261,7 +261,6 @@
             fb.addTexture(TextureType.TEXTURE_CUBE);
         }
         ProgramFragment pf = fb.create();
-        pf.bindConstants(mFsConst.getAllocation(), 0);
         pf.bindSampler(Sampler.WRAP_LINEAR_MIP_LINEAR(mRS), 0);
         if (addCubemap) {
             pf.bindSampler(Sampler.CLAMP_LINEAR_MIP_LINEAR(mRS), 1);
@@ -278,7 +277,6 @@
         vb.addInput(ScriptField_VertexShaderInputs_s.createElement(mRS));
         vb.setShader(mRes, R.raw.shader2v);
         mPV_Paint = vb.create();
-        mPV_Paint.bindConstants(mVsConst.getAllocation(), 0);
 
         mFsConst = new ScriptField_FShaderParams_s(mRS, 1);
 
@@ -422,8 +420,6 @@
         new ImageLoaderTask().execute();
 
         ScriptC_render renderLoop = mSceneManager.getRenderLoop();
-        renderLoop.bind_vConst(mVsConst);
-        renderLoop.bind_fConst(mFsConst);
 
         mScript.set_gRenderLoop(renderLoop);
         Allocation dummyAlloc = Allocation.createSized(mRS, Element.I32(mRS), 1);
diff --git a/tests/RenderScriptTests/SceneGraph/src/com/android/scenegraph/export.rs b/tests/RenderScriptTests/SceneGraph/src/com/android/scenegraph/export.rs
index b8edd2a..1065d87 100644
--- a/tests/RenderScriptTests/SceneGraph/src/com/android/scenegraph/export.rs
+++ b/tests/RenderScriptTests/SceneGraph/src/com/android/scenegraph/export.rs
@@ -27,4 +27,8 @@
 SgLight *exportPtrLight;
 SgShaderParam *spExport;
 FBlurOffsets *blurExport;
-VertexShaderInputs *iExport;
\ No newline at end of file
+VertexShaderInputs *iExport;
+
+VShaderParams *vConst;
+FShaderParams *fConst;
+
diff --git a/tests/RenderScriptTests/SceneGraph/src/com/android/scenegraph/params.rs b/tests/RenderScriptTests/SceneGraph/src/com/android/scenegraph/params.rs
index 270fa58..873c7e6 100644
--- a/tests/RenderScriptTests/SceneGraph/src/com/android/scenegraph/params.rs
+++ b/tests/RenderScriptTests/SceneGraph/src/com/android/scenegraph/params.rs
@@ -1,4 +1,4 @@
-// Copyright (C) 2011 The Android Open Source Project
+// Copyright (C) 2012 The Android Open Source Project
 //
 // Licensed under the Apache License, Version 2.0 (the "License");
 // you may not use this file except in compliance with the License.
@@ -16,7 +16,6 @@
 
 #pragma rs java_package_name(com.android.scenegraph)
 
-//#define DEBUG_PARAMS
 #include "transform_def.rsh"
 
 static void writeFloatData(float *ptr, const float4 *input, uint32_t vecSize) {
@@ -128,7 +127,7 @@
     return !rsIsSphereInFrustum(&obj->worldBoundingSphere,
                                 &cam->frustumPlanes[0], &cam->frustumPlanes[1],
                                 &cam->frustumPlanes[2], &cam->frustumPlanes[3],
-                                &cam->frustumPlanes[3], &cam->frustumPlanes[4]);
+                                &cam->frustumPlanes[4], &cam->frustumPlanes[5]);
 }
 
 
@@ -149,24 +148,24 @@
 #ifdef DEBUG_RENDERABLES
         rsDebug("Culled", drawable);
         printName(drawable->name);
-#endif //DEBUG_RENDERABLES
+#endif // DEBUG_RENDERABLES
         return;
     }
     drawable->isVisible = 1;
 
     // Data we are updating
-    /*if (rsIsObject(drawable->pf_const)) {
+    if (rsIsObject(drawable->pf_const)) {
         uint8_t *constantBuffer = (uint8_t*)rsGetElementAt(drawable->pf_const, 0);
 
         int numParams = 0;
         if (rsIsObject(drawable->pf_constParams)) {
-            rsAllocationGetDimX(drawable->pf_constParams);
+            numParams = rsAllocationGetDimX(drawable->pf_constParams);
         }
         for (int i = 0; i < numParams; i ++) {
             SgShaderParam *current = (SgShaderParam*)rsGetElementAt(drawable->pf_constParams, i);
             processParam(current, constantBuffer, camera);
         }
-        rsgAllocationSyncAll(drawable->pf_const);
+        //rsgAllocationSyncAll(drawable->pf_const);
     }
 
     if (rsIsObject(drawable->pv_const)) {
@@ -180,9 +179,6 @@
             SgShaderParam *current = (SgShaderParam*)rsGetElementAt(drawable->pv_constParams, i);
             processParam(current, constantBuffer, camera);
         }
-        rsgAllocationSyncAll(drawable->pv_const);
-    }*/
-
-#ifdef DEBUG_PARAMS
-#endif //DEBUG_PARAMS
+        //rsgAllocationSyncAll(drawable->pv_const);
+    }
 }
diff --git a/tests/RenderScriptTests/SceneGraph/src/com/android/scenegraph/render.rs b/tests/RenderScriptTests/SceneGraph/src/com/android/scenegraph/render.rs
index ceaca40..fc337f5 100644
--- a/tests/RenderScriptTests/SceneGraph/src/com/android/scenegraph/render.rs
+++ b/tests/RenderScriptTests/SceneGraph/src/com/android/scenegraph/render.rs
@@ -1,4 +1,4 @@
-// Copyright (C) 2011 The Android Open Source Project
+// Copyright (C) 2011-2012 The Android Open Source Project
 //
 // Licensed under the Apache License, Version 2.0 (the "License");
 // you may not use this file except in compliance with the License.
@@ -35,9 +35,6 @@
 rs_allocation gTGrid;
 rs_program_store gPFSBackground;
 
-VShaderParams *vConst;
-FShaderParams *fConst;
-
 uint32_t *gFrontToBack;
 static uint32_t gFrontToBackCount = 0;
 uint32_t *gBackToFront;
@@ -47,113 +44,6 @@
 
 static rs_allocation nullAlloc;
 
-static void writeFloatData(float *ptr, const float4 *input, uint32_t vecSize) {
-    switch (vecSize) {
-    case 1:
-        *ptr = input->x;
-        break;
-    case 2:
-        *ptr++ = input->x;
-        *ptr = input->y;
-        break;
-    case 3:
-        *ptr++ = input->x;
-        *ptr++ = input->y;
-        *ptr = input->z;
-        break;
-    case 4:
-        *((float4*)ptr) = *input;
-        break;
-    }
-}
-
-static void processParam(SgShaderParam *p, uint8_t *constantBuffer, const SgCamera *currentCam) {
-    uint8_t *dataPtr = constantBuffer + p->bufferOffset;
-    const SgTransform *pTransform = NULL;
-    if (rsIsObject(p->transform)) {
-        pTransform = (const SgTransform *)rsGetElementAt(p->transform, 0);
-    }
-
-    rsDebug("data ptr: ", (void*)dataPtr);
-    rsDebug("p type: ", p->type);
-
-    switch(p->type) {
-    case SHADER_PARAM_FLOAT4_DATA:
-        writeFloatData((float*)dataPtr, &p->float_value, p->float_vecSize);
-        break;
-    case SHADER_PARAM_FLOAT4_CAMERA_POS:
-        writeFloatData((float*)dataPtr, &currentCam->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_DIR: break;
-
-    case SHADER_PARAM_TRANSFORM_DATA:
-        rsMatrixLoad((rs_matrix4x4*)dataPtr, &pTransform->globalMat);
-        break;
-    case SHADER_PARAM_TRANSFORM_VIEW:
-        rsMatrixLoad((rs_matrix4x4*)dataPtr, &currentCam->view);
-        break;
-    case SHADER_PARAM_TRANSFORM_PROJ:
-        rsMatrixLoad((rs_matrix4x4*)dataPtr, &currentCam->proj);
-        break;
-    case SHADER_PARAM_TRANSFORM_VIEW_PROJ:
-        rsDebug("View proj ptr: ", (void*)&vConst->viewProj);
-        rsMatrixLoad((rs_matrix4x4*)dataPtr, &currentCam->viewProj);
-        break;
-    case SHADER_PARAM_TRANSFORM_MODEL:
-        rsDebug("Model ptr: ", (void*)&vConst->model);
-        rsMatrixLoad((rs_matrix4x4*)dataPtr, &pTransform->globalMat);
-        break;
-    case SHADER_PARAM_TRANSFORM_MODEL_VIEW:
-        rsMatrixLoad((rs_matrix4x4*)dataPtr, &currentCam->view);
-        rsMatrixLoadMultiply((rs_matrix4x4*)dataPtr,
-                             (rs_matrix4x4*)dataPtr,
-                             &pTransform->globalMat);
-        break;
-    case SHADER_PARAM_TRANSFORM_MODEL_VIEW_PROJ:
-        rsMatrixLoad((rs_matrix4x4*)dataPtr, &currentCam->viewProj);
-        rsMatrixLoadMultiply((rs_matrix4x4*)dataPtr,
-                             (rs_matrix4x4*)dataPtr,
-                             &pTransform->globalMat);
-        break;
-    }
-}
-
-static void updateParams(SgRenderable *drawable) {
-    if (rsIsObject(drawable->pf_const)) {
-        uint8_t *constantBuffer = (uint8_t*)fConst;
-
-        int numParams = 0;
-        if (rsIsObject(drawable->pf_constParams)) {
-            rsAllocationGetDimX(drawable->pf_constParams);
-        }
-        for (int i = 0; i < numParams; i ++) {
-            SgShaderParam *current = (SgShaderParam*)rsGetElementAt(drawable->pf_constParams, i);
-            processParam(current, constantBuffer, gActiveCamera);
-            rsDebug("Setting f param", i);
-        }
-        rsgAllocationSyncAll(rsGetAllocation(fConst));
-    }
-
-    if (rsIsObject(drawable->pv_const)) {
-        uint8_t *constantBuffer = (uint8_t*)vConst;
-
-        rsDebug("_______________________", 0);
-
-        int numParams = 0;
-        if (rsIsObject(drawable->pv_constParams)) {
-            numParams = rsAllocationGetDimX(drawable->pv_constParams);
-        }
-        for (int i = 0; i < numParams; i ++) {
-            SgShaderParam *current = (SgShaderParam*)rsGetElementAt(drawable->pv_constParams, i);
-            processParam(current, constantBuffer, gActiveCamera);
-        }
-        rsgAllocationSyncAll(rsGetAllocation(vConst));
-    }
-}
-
 //#define DEBUG_RENDERABLES
 static void draw(SgRenderable *obj) {
 
@@ -166,14 +56,8 @@
     printName(obj->name);
 #endif //DEBUG_RENDERABLES
 
-    updateParams(obj);
-    /*SgCamera *cam = gActiveCamera;
-
-    rsMatrixLoad(&vConst->model, &objTransform->globalMat);
-    rsMatrixLoad(&vConst->viewProj, &cam->viewProj);
-    rsgAllocationSyncAll(rsGetAllocation(vConst));*/
-    fConst->cameraPos = gActiveCamera->position;
-    rsgAllocationSyncAll(rsGetAllocation(fConst));
+    rsgBindConstant(renderState->pv, 0, obj->pv_const);
+    rsgBindConstant(renderState->pf, 0, obj->pf_const);
 
     if (rsIsObject(renderState->ps)) {
         rsgBindProgramStore(renderState->ps);
@@ -203,13 +87,7 @@
 static void sortToBucket(SgRenderable *obj) {
     const SgRenderState *renderState = (const SgRenderState *)rsGetElementAt(obj->render_state, 0);
     if (rsIsObject(renderState->ps)) {
-#define MR1_API
-#ifndef MR1_API
-        bool isOpaque = (rsgProgramStoreGetBlendSrcFunc(renderState->ps) == RS_BLEND_SRC_ONE) &&
-                        (rsgProgramStoreGetBlendDstFunc(renderState->ps) == RS_BLEND_DST_ZERO);
-#else
         bool isOpaque = false;
-#endif
         if (isOpaque) {
             gFrontToBack[gFrontToBackCount++] = (uint32_t)obj;
         } else {