Enable light sources and update film test app.
diff --git a/java/Film/res/raw/filmstrip.c b/java/Film/res/raw/filmstrip.c
index 863b930..6885251 100644
--- a/java/Film/res/raw/filmstrip.c
+++ b/java/Film/res/raw/filmstrip.c
@@ -1,7 +1,7 @@
// Fountain test script
#pragma version(1)
-#pragma stateVertex(PV)
+#pragma stateVertex(PVBackground)
#pragma stateFragment(PFBackground)
#pragma stateFragmentStore(PFSBackground)
@@ -43,8 +43,6 @@
//materialDiffuse(con, 0.0f, 0.0f, 0.0f, 1.0f);
//materialSpecular(con, 0.5f, 0.5f, 0.5f, 0.5f);
//materialShininess(intToFloat(20));
- //lightPosition(con, 0.2f, -0.2f, -2.0f, 0.0f);
- //enable(con, GL_LIGHTING);
drawTriangleMesh(NAMED_mesh);
@@ -53,6 +51,7 @@
bindProgramFragmentStore(NAMED_PFImages);
bindProgramFragment(NAMED_PFSImages);
+ bindProgramVertex(NAMED_PVImages);
//focusPos = loadF(1, 2);
//focusID = 0;
diff --git a/java/Film/src/com/android/film/FilmRS.java b/java/Film/src/com/android/film/FilmRS.java
index 2711bf0..fca0818 100644
--- a/java/Film/src/com/android/film/FilmRS.java
+++ b/java/Film/src/com/android/film/FilmRS.java
@@ -45,7 +45,6 @@
public void init(RenderScript rs, Resources res, int width, int height) {
mRS = rs;
mRes = res;
- initNamed();
initRS();
}
@@ -77,7 +76,8 @@
private RenderScript.ProgramFragmentStore mPFSImages;
private RenderScript.ProgramFragment mPFBackground;
private RenderScript.ProgramFragment mPFImages;
- private RenderScript.ProgramVertex mPV;
+ private RenderScript.ProgramVertex mPVBackground;
+ private RenderScript.ProgramVertex mPVImages;
private ProgramVertexAlloc mPVA;
private RenderScript.Allocation mAllocEnv;
@@ -90,20 +90,7 @@
private float[] mBufferPos;
private float[] mBufferPV;
- private void initNamed() {
- mElementVertex = mRS.elementGetPredefined(
- RenderScript.ElementPredefined.NORM_ST_XYZ_F32);
- mElementIndex = mRS.elementGetPredefined(
- RenderScript.ElementPredefined.INDEX_16);
-
- mRS.triangleMeshBegin(mElementVertex, mElementIndex);
- FilmStripMesh fsm = new FilmStripMesh();
- fsm.init(mRS);
- mMesh = mRS.triangleMeshCreate();
- mMesh.setName("mesh");
- Log.e("rs", "Done loading strips");
-
-
+ private void initSamplers() {
mRS.samplerBegin();
mRS.samplerSet(RenderScript.SamplerParam.FILTER_MIN,
RenderScript.SamplerValue.LINEAR_MIP_LINEAR);
@@ -112,19 +99,9 @@
mRS.samplerSet(RenderScript.SamplerParam.WRAP_MODE_T,
RenderScript.SamplerValue.CLAMP);
mSampler = mRS.samplerCreate();
+ }
- mRS.programFragmentBegin(null, null);
- mPFBackground = mRS.programFragmentCreate();
- mPFBackground.setName("PFBackground");
-
- mRS.programFragmentBegin(null, null);
- mRS.programFragmentSetTexEnable(0, true);
- //mRS.programFragmentSetEnvMode(0, RS_TEX_ENV_MODE_REPLACE);
- //rsProgramFragmentSetType(0, gEnv.tex[0]->getType());
- mPFImages = mRS.programFragmentCreate();
- mPFImages.setName("PFImages");
- mPFImages.bindSampler(mSampler, 0);
-
+ private void initPFS() {
mRS.programFragmentStoreBegin(null, null);
mRS.programFragmentStoreDepthFunc(RenderScript.DepthFunc.LESS);
mRS.programFragmentStoreDitherEnable(true);
@@ -139,26 +116,59 @@
RenderScript.BlendDstFunc.ONE);
mPFSImages = mRS.programFragmentStoreCreate();
mPFSImages.setName("PFSImages");
+ }
- mRS.programVertexBegin(null, null);
- mRS.programVertexSetTextureMatrixEnable(true);
- mPV = mRS.programVertexCreate();
- mPV.setName("PV");
+ private void initPF() {
+ mRS.programFragmentBegin(null, null);
+ mPFBackground = mRS.programFragmentCreate();
+ mPFBackground.setName("PFBackground");
+ mRS.programFragmentBegin(null, null);
+ mRS.programFragmentSetTexEnable(0, true);
+ //mRS.programFragmentSetEnvMode(0, RS_TEX_ENV_MODE_REPLACE);
+ //rsProgramFragmentSetType(0, gEnv.tex[0]->getType());
+ mPFImages = mRS.programFragmentCreate();
+ mPFImages.setName("PFImages");
+ }
+
+ private void initPV() {
mRS.lightBegin();
mLight = mRS.lightCreate();
mLight.setPosition(0, -0.5f, -1.0f);
- Log.e("rs", "Done loading named");
+ mRS.programVertexBegin(null, null);
+ mRS.programVertexSetTextureMatrixEnable(true);
+ mRS.programVertexAddLight(mLight);
+ mPVBackground = mRS.programVertexCreate();
+ mPVBackground.setName("PVBackground");
+
+ mRS.programVertexBegin(null, null);
+ mPVImages = mRS.programVertexCreate();
+ mPVImages.setName("PVImages");
}
- private Bitmap mBackground;
-
int mParams[] = new int[10];
private void initRS() {
- int partCount = 1024;
+ mElementVertex = mRS.elementGetPredefined(
+ RenderScript.ElementPredefined.NORM_ST_XYZ_F32);
+ mElementIndex = mRS.elementGetPredefined(
+ RenderScript.ElementPredefined.INDEX_16);
+
+ mRS.triangleMeshBegin(mElementVertex, mElementIndex);
+ FilmStripMesh fsm = new FilmStripMesh();
+ fsm.init(mRS);
+ mMesh = mRS.triangleMeshCreate();
+ mMesh.setName("mesh");
+
+ initPFS();
+ initSamplers();
+ initPF();
+ initPV();
+ mPFImages.bindSampler(mSampler, 0);
+
+ Log.e("rs", "Done loading named");
mRS.scriptCBegin();
mRS.scriptCSetClearColor(0.0f, 0.0f, 0.0f, 1.0f);
@@ -172,7 +182,8 @@
mBufferPos.length);
mPVA = new ProgramVertexAlloc(mRS);
- mPV.bindAllocation(0, mPVA.mAlloc);
+ mPVBackground.bindAllocation(0, mPVA.mAlloc);
+ mPVImages.bindAllocation(0, mPVA.mAlloc);
mPVA.setupProjectionNormalized(320, 480);
@@ -181,10 +192,6 @@
mScriptStrip.bindAllocation(mPVA.mAlloc, 3);
- //mIntAlloc = mRS.allocationCreatePredefSized(RenderScript.ElementPredefined.USER_I32, 10);
- //mPartAlloc = mRS.allocationCreatePredefSized(RenderScript.ElementPredefined.USER_I32, partCount * 3 * 3);
- //mPartAlloc.setName("PartBuffer");
- //mVertAlloc = mRS.allocationCreatePredefSized(RenderScript.ElementPredefined.USER_I32, partCount * 5 + 1);
/*
{
Resources res = getResources();
@@ -203,25 +210,6 @@
mPFS = mRS.programFragmentStoreCreate();
mPFS.setName("MyBlend");
mRS.contextBindProgramFragmentStore(mPFS);
-
- mRS.samplerBegin();
- mRS.samplerSet(RenderScript.SamplerParam.FILTER_MAG, RenderScript.SamplerValue.LINEAR);
- mRS.samplerSet(RenderScript.SamplerParam.FILTER_MIN, RenderScript.SamplerValue.LINEAR);
- mSampler = mRS.samplerCreate();
-
-
- mParams[0] = 0;
- mParams[1] = partCount;
- mParams[2] = 0;
- mParams[3] = 0;
- mParams[4] = 0;
- mIntAlloc.data(mParams);
-
- int t2[] = new int[partCount * 4*3];
- for (int ct=0; ct < t2.length; ct++) {
- t2[ct] = 0;
- }
- mPartAlloc.data(t2);
*/
setFilmStripPosition(0, 0);
diff --git a/java/RenderScript/android/renderscript/RenderScript.java b/java/RenderScript/android/renderscript/RenderScript.java
index 733009e..e355635 100644
--- a/java/RenderScript/android/renderscript/RenderScript.java
+++ b/java/RenderScript/android/renderscript/RenderScript.java
@@ -156,6 +156,7 @@
native private void nProgramVertexBegin(int inID, int outID);
native private void nProgramVertexSetType(int slot, int mID);
native private void nProgramVertexSetTextureMatrixEnable(boolean enable);
+ native private void nProgramVertexAddLight(int id);
native private int nProgramVertexCreate();
native private void nLightBegin();
@@ -720,7 +721,6 @@
public void bindAllocation(int slot, Allocation va) {
nProgramVertexBindAllocation(mID, slot, va.mID);
}
-
}
public void programVertexBegin(Element in, Element out) {
@@ -743,6 +743,10 @@
nProgramVertexSetTextureMatrixEnable(enable);
}
+ public void programVertexAddLight(Light l) {
+ nProgramVertexAddLight(l.mID);
+ }
+
public ProgramVertex programVertexCreate() {
int id = nProgramVertexCreate();
return new ProgramVertex(id);
diff --git a/jni/RenderScript_jni.cpp b/jni/RenderScript_jni.cpp
index 248a6bd..1747673 100644
--- a/jni/RenderScript_jni.cpp
+++ b/jni/RenderScript_jni.cpp
@@ -810,6 +810,14 @@
rsProgramVertexSetTextureMatrixEnable(enable);
}
+static void
+nProgramVertexAddLight(JNIEnv *_env, jobject _this, jint light)
+{
+ RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
+ LOG_API("nProgramVertexAddLight, con(%p), light(%p)", con, (RsLight)light);
+ rsProgramVertexAddLight((RsLight)light);
+}
+
static jint
nProgramVertexCreate(JNIEnv *_env, jobject _this)
{
@@ -1048,6 +1056,7 @@
{"nProgramVertexBegin", "(II)V", (void*)nProgramVertexBegin },
{"nProgramVertexSetType", "(II)V", (void*)nProgramVertexSetType },
{"nProgramVertexSetTextureMatrixEnable", "(Z)V", (void*)nProgramVertexSetTextureMatrixEnable },
+{"nProgramVertexAddLight", "(I)V", (void*)nProgramVertexAddLight },
{"nProgramVertexCreate", "()I", (void*)nProgramVertexCreate },
{"nLightBegin", "()V", (void*)nLightBegin },
diff --git a/rs.spec b/rs.spec
index 62533af..2f99808 100644
--- a/rs.spec
+++ b/rs.spec
@@ -403,6 +403,10 @@
param bool enable
}
+ProgramVertexAddLight {
+ param RsLight light
+ }
+
LightBegin {
}
diff --git a/rsContext.h b/rsContext.h
index 442a920..497dbcf 100644
--- a/rsContext.h
+++ b/rsContext.h
@@ -32,10 +32,10 @@
#include "rsAllocation.h"
#include "rsAdapter.h"
#include "rsSampler.h"
+#include "rsLight.h"
#include "rsProgramFragment.h"
#include "rsProgramFragmentStore.h"
#include "rsProgramVertex.h"
-#include "rsLight.h"
#include "rsgApiStructs.h"
#include "rsLocklessFifo.h"
diff --git a/rsLight.cpp b/rsLight.cpp
index 67d0095..24b58b6 100644
--- a/rsLight.cpp
+++ b/rsLight.cpp
@@ -16,6 +16,8 @@
#include "rsContext.h"
+#include <GLES/gl.h>
+
using namespace android;
using namespace android::renderscript;
@@ -25,13 +27,15 @@
mIsLocal = isLocal;
mIsMono = isMono;
- mX = 0;
- mY = 0;
- mZ = 0;
+ mPosition[0] = 0;
+ mPosition[1] = 0;
+ mPosition[2] = 1;
+ mPosition[3] = 0;
- mR = 1.f;
- mG = 1.f;
- mB = 1.f;
+ mColor[0] = 1.f;
+ mColor[1] = 1.f;
+ mColor[2] = 1.f;
+ mColor[3] = 1.f;
}
Light::~Light()
@@ -40,16 +44,23 @@
void Light::setPosition(float x, float y, float z)
{
- mX = x;
- mY = y;
- mZ = z;
+ mPosition[0] = x;
+ mPosition[1] = y;
+ mPosition[2] = z;
}
void Light::setColor(float r, float g, float b)
{
- mR = r;
- mG = g;
- mB = b;
+ mColor[0] = r;
+ mColor[1] = g;
+ mColor[2] = b;
+}
+
+void Light::setupGL(uint32_t num) const
+{
+ glLightfv(GL_LIGHT0 + num, GL_DIFFUSE, mColor);
+ glLightfv(GL_LIGHT0 + num, GL_SPECULAR, mColor);
+ glLightfv(GL_LIGHT0 + num, GL_POSITION, mPosition);
}
////////////////////////////////////////////
diff --git a/rsLight.h b/rsLight.h
index 76d1ecc..b0c3386 100644
--- a/rsLight.h
+++ b/rsLight.h
@@ -36,9 +36,11 @@
void setPosition(float x, float y, float z);
void setColor(float r, float g, float b);
+ void setupGL(uint32_t num) const;
+
protected:
- float mR, mG, mB;
- float mX, mY, mZ;
+ float mColor[4];
+ float mPosition[4];
bool mIsLocal;
bool mIsMono;
};
diff --git a/rsProgramVertex.cpp b/rsProgramVertex.cpp
index 4089507..417ba6a 100644
--- a/rsProgramVertex.cpp
+++ b/rsProgramVertex.cpp
@@ -28,6 +28,7 @@
Program(in, out)
{
mTextureMatrixEnable = false;
+ mLightCount = 0;
}
ProgramVertex::~ProgramVertex()
@@ -54,8 +55,29 @@
glLoadIdentity();
}
- //logMatrix("prog", &f[RS_PROGRAM_VERTEX_PROJECTION_OFFSET]);
- //logMatrix("model", &f[RS_PROGRAM_VERTEX_MODELVIEW_OFFSET]);
+
+ LOGE("lights %i ", mLightCount);
+ glMatrixMode(GL_MODELVIEW);
+ glLoadIdentity();
+ if (mLightCount) {
+ int v = 1;
+ glEnable(GL_LIGHTING);
+ glLightModelxv(GL_LIGHT_MODEL_TWO_SIDE, &v);
+ for (uint32_t ct = 0; ct < mLightCount; ct++) {
+ const Light *l = mLights[ct].get();
+ glEnable(GL_LIGHT0 + ct);
+ l->setupGL(ct);
+ }
+ for (uint32_t ct = mLightCount; ct < MAX_LIGHTS; ct++) {
+ glDisable(GL_LIGHT0 + ct);
+ }
+ } else {
+ glDisable(GL_LIGHTING);
+ }
+
+ if (!f) {
+ LOGE("Must bind constants to vertex program");
+ }
glMatrixMode(GL_PROJECTION);
glLoadMatrixf(&f[RS_PROGRAM_VERTEX_PROJECTION_OFFSET]);
@@ -73,6 +95,14 @@
mConstants[slot].set(a);
}
+void ProgramVertex::addLight(const Light *l)
+{
+ if (mLightCount < MAX_LIGHTS) {
+ mLights[mLightCount].set(l);
+ mLightCount++;
+ }
+}
+
ProgramVertexState::ProgramVertexState()
{
@@ -136,6 +166,10 @@
rsc->mStateVertex.mPV->setTextureMatrixEnable(enable);
}
+void rsi_ProgramVertexAddLight(Context *rsc, RsLight light)
+{
+ rsc->mStateVertex.mPV->addLight(static_cast<const Light *>(light));
+}
}
diff --git a/rsProgramVertex.h b/rsProgramVertex.h
index 1a92f01..ac15b70 100644
--- a/rsProgramVertex.h
+++ b/rsProgramVertex.h
@@ -28,6 +28,7 @@
{
public:
const static uint32_t MAX_CONSTANTS = 2;
+ const static uint32_t MAX_LIGHTS = 8;
ProgramVertex(Element *in, Element *out);
virtual ~ProgramVertex();
@@ -38,12 +39,16 @@
void setConstantType(uint32_t slot, const Type *);
void bindAllocation(uint32_t slot, Allocation *);
void setTextureMatrixEnable(bool e) {mTextureMatrixEnable = e;}
+ void addLight(const Light *);
protected:
bool mDirty;
+ uint32_t mLightCount;
ObjectBaseRef<Allocation> mConstants[MAX_CONSTANTS];
ObjectBaseRef<const Type> mConstantTypes[MAX_CONSTANTS];
+ ObjectBaseRef<const Light> mLights[MAX_LIGHTS];
+
// Hacks to create a program for now
bool mTextureMatrixEnable;
@@ -61,6 +66,8 @@
ObjectBaseRef<ProgramVertex> mDefault;
ObjectBaseRef<Allocation> mDefaultAlloc;
+
+
ProgramVertex *mPV;
diff --git a/rsScriptC.cpp b/rsScriptC.cpp
index e0928c3..842c836 100644
--- a/rsScriptC.cpp
+++ b/rsScriptC.cpp
@@ -103,14 +103,10 @@
static ACCvoid* symbolLookup(ACCvoid* pContext, const ACCchar* name)
{
const ScriptCState::SymbolTable_t *sym = ScriptCState::lookupSymbol(name);
-
if (sym) {
return sym->mPtr;
}
-
LOGE("ScriptC sym lookup failed for %s", name);
-
- // Default to calling dlsym to allow any global symbol:
return NULL;
}
@@ -121,7 +117,7 @@
rsc->appendNameDefines(&tmp);
appendDecls(&tmp);
- //tmp.append("#line 1\n");
+ tmp.append("#line 1\n");
const char* scriptSource[] = {tmp.string(), mProgram.mScriptText};
int scriptLength[] = {tmp.length(), mProgram.mScriptTextLength} ;
diff --git a/rsScriptC_Lib.cpp b/rsScriptC_Lib.cpp
index 59d8e5f..129b19f 100644
--- a/rsScriptC_Lib.cpp
+++ b/rsScriptC_Lib.cpp
@@ -249,6 +249,12 @@
}
+static void SC_bindProgramVertex(RsProgramVertex pv)
+{
+ GET_TLS();
+ rsi_ContextBindProgramVertex(rsc, pv);
+
+}
//////////////////////////////////////////////////////////////////////////////
// Drawing
@@ -468,6 +474,8 @@
"void", "(int)" },
{ "bindProgramFragmentStore", (void *)&SC_bindProgramFragmentStore,
"void", "(int)" },
+ { "bindProgramVertex", (void *)&SC_bindProgramVertex,
+ "void", "(int)" },
{ "bindSampler", (void *)&SC_bindSampler,
"void", "(int, int, int)" },
{ "bindTexture", (void *)&SC_bindTexture,