Add named objects and implement support for ProgramFragmentStore and ProgramFragment to be used by name in scripts.

This commit is contained in:
Jason Sams
2009-06-10 15:04:38 -07:00
parent da423d8479
commit 3eaa338e11
17 changed files with 202 additions and 137 deletions

View File

@@ -3,8 +3,8 @@
#pragma version(1)
#pragma stateVertex(orthoWindow)
#pragma stateRaster(flat)
#pragma stateFragment(color)
#pragma stateStore(parent)
#pragma stateFragment(PgmFragBackground)
#pragma stateFragmentStore(MyBlend)
int main(void* con, int ft, int launchID) {
@@ -35,7 +35,7 @@ int main(void* con, int ft, int launchID) {
}
}
contextBindProgramFragment(con, loadI32(con, 0, 7));
//contextBindProgramFragment(con, loadI32(con, 0, 7));
drawRect(con, 0, 256, 0, 512);
contextBindProgramFragment(con, loadI32(con, 0, 6));

View File

@@ -82,6 +82,7 @@ public class FountainView extends RSSurfaceView {
mRS.programFragmentStoreBlendFunc(RenderScript.BlendSrcFunc.SRC_ALPHA, RenderScript.BlendDstFunc.ONE);
mRS.programFragmentStoreDepthFunc(RenderScript.DepthFunc.ALWAYS);
mPFS = mRS.programFragmentStoreCreate();
mPFS.setName("MyBlend");
mRS.contextBindProgramFragmentStore(mPFS);
mRS.samplerBegin();
@@ -92,6 +93,7 @@ public class FountainView extends RSSurfaceView {
mRS.programFragmentBegin(null, null);
mPF = mRS.programFragmentCreate();
mPF.setName("PgmFragParts");
//mRS.contextBindProgramFragment(mPF);
mRS.programFragmentBegin(null, null);
@@ -100,6 +102,7 @@ public class FountainView extends RSSurfaceView {
mRS.contextBindProgramFragment(mPF2);
mPF2.bindTexture(mTexture, 0);
mPF2.bindSampler(mSampler, 0);
mPF2.setName("PgmFragBackground");
mParams[0] = 0;
mParams[1] = partCount;

View File

@@ -68,6 +68,8 @@ public class RenderScript {
native private void nContextBindProgramFragmentStore(int pfs);
native private void nContextBindProgramFragment(int pf);
native private void nAssignName(int obj, byte[] name);
native private void nElementBegin();
native private void nElementAddPredefined(int predef);
native private void nElementAdd(int kind, int type, int norm, int bits);
@@ -135,6 +137,7 @@ public class RenderScript {
native private void nProgramFragmentStoreBlendFunc(int src, int dst);
native private void nProgramFragmentStoreDither(boolean enable);
native private int nProgramFragmentStoreCreate();
native private void nProgramFragmentStoreDestroy(int pgm);
native private void nProgramFragmentBegin(int in, int out);
native private void nProgramFragmentBindTexture(int vpf, int slot, int a);
@@ -143,6 +146,7 @@ public class RenderScript {
native private void nProgramFragmentSetEnvMode(int slot, int env);
native private void nProgramFragmentSetTexEnable(int slot, boolean enable);
native private int nProgramFragmentCreate();
native private void nProgramFragmentDestroy(int pgm);
private int mDev;
@@ -166,6 +170,26 @@ public class RenderScript {
}
int mID;
String mName;
public void setName(String s) throws IllegalStateException, IllegalArgumentException
{
if(s.length() < 1) {
throw new IllegalArgumentException("setName does not accept a zero length string.");
}
if(mName != null) {
throw new IllegalArgumentException("setName object already has a name.");
}
try {
byte[] bytes = s.getBytes("UTF-8");
nAssignName(mID, bytes);
mName = s;
} catch (java.io.UnsupportedEncodingException e) {
throw new RuntimeException(e);
}
}
protected void finalize() throws Throwable
{
if (mID != 0) {
@@ -661,7 +685,7 @@ public class RenderScript {
}
public void destroy() {
nScriptDestroy(mID);
nProgramFragmentStoreDestroy(mID);
mID = 0;
}
}
@@ -712,7 +736,7 @@ public class RenderScript {
}
public void destroy() {
nScriptDestroy(mID);
nProgramFragmentDestroy(mID);
mID = 0;
}

View File

@@ -60,6 +60,21 @@ static void _nInit(JNIEnv *_env, jclass _this)
}
// ---------------------------------------------------------------------------
static void
nAssignName(JNIEnv *_env, jobject _this, jint obj, jbyteArray str)
{
RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
LOG_API("nAssignName, con(%p), obj(%p)", con, obj);
jint len = _env->GetArrayLength(str);
jbyte * cptr = (jbyte *) _env->GetPrimitiveArrayCritical(str, 0);
rsAssignName((void *)obj, (const char *)cptr);
_env->ReleasePrimitiveArrayCritical(str, cptr, JNI_ABORT);
}
// ---------------------------------------------------------------------------
static jint
@@ -662,9 +677,18 @@ nProgramFragmentStoreCreate(JNIEnv *_env, jobject _this)
{
RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
LOG_API("nProgramFragmentStoreCreate, con(%p)", con);
return (jint)rsProgramFragmentStoreCreate();
}
static void
nProgramFragmentStoreDestroy(JNIEnv *_env, jobject _this, jint pgm)
{
RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
LOG_API("nProgramFragmentStoreDestroy, con(%p), pgm(%i)", con, pgm);
rsProgramFragmentStoreDestroy((RsProgramFragmentStore)pgm);
}
// ---------------------------------------------------------------------------
static void
@@ -723,6 +747,14 @@ nProgramFragmentCreate(JNIEnv *_env, jobject _this, jint slot, jboolean enable)
return (jint)rsProgramFragmentCreate();
}
static void
nProgramFragmentDestroy(JNIEnv *_env, jobject _this, jint pgm)
{
RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
LOG_API("nProgramFragmentDestroy, con(%p), pgm(%i)", con, pgm);
rsProgramFragmentDestroy((RsProgramFragment)pgm);
}
// ---------------------------------------------------------------------------
@@ -796,6 +828,7 @@ static JNINativeMethod methods[] = {
{"nDeviceDestroy", "(I)V", (void*)nDeviceDestroy },
{"nContextCreate", "(ILandroid/view/Surface;I)I", (void*)nContextCreate },
{"nContextDestroy", "(I)V", (void*)nContextDestroy },
{"nAssignName", "(I[B)V", (void*)nAssignName },
{"nElementBegin", "()V", (void*)nElementBegin },
{"nElementAddPredefined", "(I)V", (void*)nElementAddPredefined },
@@ -858,6 +891,7 @@ static JNINativeMethod methods[] = {
{"nProgramFragmentStoreBlendFunc", "(II)V", (void*)nProgramFragmentStoreBlendFunc },
{"nProgramFragmentStoreDither", "(Z)V", (void*)nProgramFragmentStoreDither },
{"nProgramFragmentStoreCreate", "()I", (void*)nProgramFragmentStoreCreate },
{"nProgramFragmentStoreDestroy", "(I)V", (void*)nProgramFragmentStoreDestroy },
{"nProgramFragmentBegin", "(II)V", (void*)nProgramFragmentBegin },
{"nProgramFragmentBindTexture", "(III)V", (void*)nProgramFragmentBindTexture },
@@ -866,6 +900,7 @@ static JNINativeMethod methods[] = {
{"nProgramFragmentSetEnvMode", "(II)V", (void*)nProgramFragmentSetEnvMode },
{"nProgramFragmentSetTexEnable", "(IZ)V", (void*)nProgramFragmentSetTexEnable },
{"nProgramFragmentCreate", "()I", (void*)nProgramFragmentCreate },
{"nProgramFragmentDestroy", "(I)V", (void*)nProgramFragmentDestroy },
{"nContextBindRootScript", "(I)V", (void*)nContextBindRootScript },
{"nContextBindProgramFragmentStore","(I)V", (void*)nContextBindProgramFragmentStore },

View File

@@ -16,6 +16,10 @@ ContextBindProgramVertex {
param RsProgramVertex pgm
}
AssignName {
param void *obj
param const char *name
}
ElementBegin {
}
@@ -332,6 +336,9 @@ ProgramFragmentStoreCreate {
ret RsProgramFragmentStore
}
ProgramFragmentStoreDestroy {
param RsProgramFragmentStore pfs
}
ProgramFragmentBegin {
@@ -370,6 +377,9 @@ ProgramFragmentCreate {
ret RsProgramFragment
}
ProgramFragmentDestroy {
param RsProgramFragment pf
}
ProgramVertexBegin {

View File

@@ -57,14 +57,17 @@ void Context::initEGL()
eglQuerySurface(mDisplay, mSurface, EGL_HEIGHT, &mHeight);
}
bool Context::runScript(Script *s)
bool Context::runScript(Script *s, uint32_t launchID)
{
ObjectBaseRef<ProgramFragment> frag(mFragment);
ObjectBaseRef<ProgramVertex> vtx(mVertex);
ObjectBaseRef<ProgramFragmentStore> store(mFragmentStore);
bool ret = s->run(this, launchID);
mFragment.set(frag);
mVertex.set(vtx);
mFragmentStore.set(store);
return true;
}
@@ -107,7 +110,7 @@ bool Context::runRootScript()
glClear(GL_COLOR_BUFFER_BIT);
glClear(GL_DEPTH_BUFFER_BIT);
return mRootScript->run(this, 0);
return runScript(mRootScript.get(), 0);
}
void Context::setupCheck()
@@ -243,6 +246,33 @@ void Context::setVertex(ProgramVertex *pv)
pv->setupGL();
}
void Context::assignName(ObjectBase *obj, const char *name)
{
rsAssert(!obj->getName());
obj->setName(name);
mNames.add(obj);
}
void Context::removeName(ObjectBase *obj)
{
for(size_t ct=0; ct < mNames.size(); ct++) {
if (obj == mNames[ct]) {
mNames.removeAt(ct);
return;
}
}
}
ObjectBase * Context::lookupName(const char *name) const
{
for(size_t ct=0; ct < mNames.size(); ct++) {
if (!strcmp(name, mNames[ct]->getName())) {
return mNames[ct];
}
}
return NULL;
}
///////////////////////////////////////////////////////////////////////////////////////////
//
@@ -286,6 +316,11 @@ void rsi_ContextBindProgramVertex(Context *rsc, RsProgramVertex vpv)
rsc->setVertex(pv);
}
void rsi_AssignName(Context *rsc, void * obj, const char *name)
{
ObjectBase *ob = static_cast<ObjectBase *>(obj);
rsc->assignName(ob, name);
}
}

View File

@@ -42,7 +42,6 @@
namespace android {
namespace renderscript {
class Context
{
public:
@@ -77,6 +76,10 @@ public:
void setupCheck();
void assignName(ObjectBase *obj, const char *name);
void removeName(ObjectBase *obj);
ObjectBase * lookupName(const char *name) const;
protected:
Device *mDev;
@@ -112,7 +115,7 @@ private:
void initEGL();
bool runScript(Script *s);
bool runScript(Script *s, uint32_t launchID);
bool runRootScript();
static void * threadProc(void *);
@@ -120,6 +123,8 @@ private:
// todo: put in TLS
static Context *gCon;
Surface *mWndSurface;
Vector<ObjectBase *> mNames;
};

View File

@@ -23,6 +23,7 @@ using namespace android::renderscript;
ObjectBase::ObjectBase()
{
mRefCount = 0;
mName = NULL;
}
ObjectBase::~ObjectBase()
@@ -46,3 +47,12 @@ void ObjectBase::decRef() const
}
}
void ObjectBase::setName(const char *name)
{
delete mName;
mName = NULL;
if (name) {
mName = new char[strlen(name) +1];
strcpy(mName, name);
}
}

View File

@@ -33,7 +33,13 @@ public:
void incRef() const;
void decRef() const;
const char * getName() const {
return mName;
}
void setName(const char *);
private:
char * mName;
mutable int32_t mRefCount;
@@ -49,12 +55,16 @@ public:
ObjectBaseRef(const ObjectBaseRef &ref) {
mRef = ref.get();
mRef->incRef();
if (mRef) {
mRef->incRef();
}
}
ObjectBaseRef(T *ref) {
mRef = ref;
ref->incRef();
if (mRef) {
ref->incRef();
}
}
~ObjectBaseRef() {
@@ -65,10 +75,16 @@ public:
if (mRef != ref) {
clear();
mRef = ref;
ref->incRef();
if (mRef) {
ref->incRef();
}
}
}
void set(const ObjectBaseRef &ref) {
set(ref.mRef);
}
void clear() {
if (mRef) {
mRef->decRef();

View File

@@ -212,6 +212,14 @@ RsProgramFragment rsi_ProgramFragmentCreate(Context *rsc)
return pf;
}
void rsi_ProgramFragmentDestroy(Context *rsc, RsProgramFragment vpf)
{
ProgramFragment *pf = (ProgramFragment *)vpf;
if (pf->getName()) {
rsc->removeName(pf);
}
pf->decRef();
}
}

View File

@@ -85,7 +85,7 @@ public:
ObjectBaseRef<Type> mTextureTypes[ProgramFragment::MAX_TEXTURE];
Vector<ProgramFragment *> mPrograms;
};

View File

@@ -202,7 +202,6 @@ ProgramFragmentStoreState::~ProgramFragmentStoreState()
}
namespace android {
namespace renderscript {
@@ -238,7 +237,6 @@ RsProgramFragmentStore rsi_ProgramFragmentStoreCreate(Context *rsc)
ProgramFragmentStore *pfs = rsc->mStateFragmentStore.mPFS;
pfs->incRef();
rsc->mStateFragmentStore.mPFS = 0;
return pfs;
}
@@ -247,6 +245,17 @@ void rsi_ProgramFragmentStoreDither(Context *rsc, bool enable)
rsc->mStateFragmentStore.mPFS->setDitherEnable(enable);
}
void rsi_ProgramFragmentStoreDestroy(Context *rsc, RsProgramFragmentStore vpfs)
{
ProgramFragmentStore *pfs = (ProgramFragmentStore *)vpfs;
if (pfs->getName()) {
rsc->removeName(pfs);
}
pfs->decRef();
}
}
}

View File

@@ -76,8 +76,6 @@ public:
~ProgramFragmentStoreState();
ProgramFragmentStore *mPFS;
};

View File

@@ -97,7 +97,6 @@ RsProgramVertex rsi_ProgramVertexCreate(Context *rsc)
ProgramVertex *pv = rsc->mStateVertex.mPV;
pv->incRef();
rsc->mStateVertex.mPV = 0;
return pv;
}

View File

@@ -19,11 +19,15 @@
#include "rsAllocation.h"
// ---------------------------------------------------------------------------
namespace android {
namespace renderscript {
class ProgramVertex;
class ProgramFragment;
class ProgramRaster;
class ProgramFragmentStore;
class Script : public ObjectBase
{
@@ -40,37 +44,10 @@ public:
float mClearDepth;
uint32_t mClearStencil;
enum StateVertex {
VTX_ORTHO_WINDOW,
VTX_ORTHO_NORMALIZED,
VTX_PROJECTION,
VTX_PARENT
};
StateVertex mStateVertex;
enum StateRaster {
RASTER_FLAT,
RASTER_SMOOTH,
RASTER_PARENT
};
StateRaster mStateRaster;
enum StateFragment {
FRAGMENT_COLOR,
FRAGMENT_TEX_REPLACE,
FRAGMENT_TEX_MODULATE,
FRAGMENT_PARENT
};
StateFragment mStateFragment;
enum StateFragmentStore {
FRAGMENT_STORE_ALWAYS_REPLACE,
FRAGMENT_STORE_ALWAYS_BLEND,
FRAGMENT_STORE_DEPTH_LESS_REPLACE,
FRAGMENT_STORE_DEPTH_LESS_BLEND,
FRAGMENT_STORE_PARENT
};
StateFragmentStore mStateFragmentStore;
ObjectBaseRef<ProgramVertex> mVertex;
ObjectBaseRef<ProgramFragment> mFragment;
//ObjectBaseRef<ProgramRaster> mRaster;
ObjectBaseRef<ProgramFragmentStore> mFragmentStore;
};
Enviroment_t mEnviroment;

View File

@@ -390,6 +390,14 @@ static rsc_FunctionTable scriptCPtrTable = {
bool ScriptC::run(Context *rsc, uint32_t launchID)
{
Env e = {rsc, this};
if (mEnviroment.mFragmentStore.get()) {
rsc->setFragmentStore(mEnviroment.mFragmentStore.get());
}
if (mEnviroment.mFragment.get()) {
rsc->setFragment(mEnviroment.mFragment.get());
}
return mProgram.mScript(&e, &scriptCPtrTable, launchID) != 0;
}
@@ -425,7 +433,7 @@ void ScriptCState::clear()
}
void ScriptCState::runCompiler()
void ScriptCState::runCompiler(Context *rsc)
{
mAccScript = accCreateScript();
@@ -442,16 +450,6 @@ void ScriptCState::runCompiler()
ACCchar * str[pragmaMax];
accGetPragmas(mAccScript, &pragmaCount, pragmaMax, &str[0]);
// Start with defaults
mEnviroment.mStateVertex =
Script::Enviroment_t::VTX_ORTHO_WINDOW;
mEnviroment.mStateRaster =
Script::Enviroment_t::RASTER_FLAT;
mEnviroment.mStateFragment =
Script::Enviroment_t::FRAGMENT_COLOR;
mEnviroment.mStateFragmentStore =
Script::Enviroment_t::FRAGMENT_STORE_ALWAYS_REPLACE;
for (int ct=0; ct < pragmaCount; ct+=2) {
LOGE("pragma %i %s %s", ct, str[ct], str[ct+1]);
@@ -462,96 +460,34 @@ void ScriptCState::runCompiler()
if (!strcmp(str[ct], "stateVertex")) {
if (!strcmp(str[ct+1], "orthoWindow")) {
mEnviroment.mStateVertex =
Script::Enviroment_t::VTX_ORTHO_WINDOW;
continue;
}
if (!strcmp(str[ct+1], "orthoNormalized")) {
mEnviroment.mStateVertex =
Script::Enviroment_t::VTX_ORTHO_NORMALIZED;
continue;
}
if (!strcmp(str[ct+1], "projection")) {
mEnviroment.mStateVertex =
Script::Enviroment_t::VTX_PROJECTION;
continue;
}
if (!strcmp(str[ct+1], "parent")) {
mEnviroment.mStateVertex =
Script::Enviroment_t::VTX_PARENT;
continue;
}
LOGE("Unreconized value %s passed to stateVertex", str[ct+1]);
}
if (!strcmp(str[ct], "stateRaster")) {
if (!strcmp(str[ct+1], "flat")) {
mEnviroment.mStateRaster =
Script::Enviroment_t::RASTER_FLAT;
continue;
}
if (!strcmp(str[ct+1], "smooth")) {
mEnviroment.mStateRaster =
Script::Enviroment_t::RASTER_SMOOTH;
continue;
}
if (!strcmp(str[ct+1], "parent")) {
mEnviroment.mStateRaster =
Script::Enviroment_t::RASTER_PARENT;
continue;
}
LOGE("Unreconized value %s passed to stateRaster", str[ct+1]);
}
if (!strcmp(str[ct], "stateFragment")) {
if (!strcmp(str[ct+1], "color")) {
mEnviroment.mStateFragment =
Script::Enviroment_t::FRAGMENT_COLOR;
continue;
}
if (!strcmp(str[ct+1], "texReplace")) {
mEnviroment.mStateFragment =
Script::Enviroment_t::FRAGMENT_TEX_REPLACE;
continue;
}
if (!strcmp(str[ct+1], "texModulate")) {
mEnviroment.mStateFragment =
Script::Enviroment_t::FRAGMENT_TEX_MODULATE;
continue;
}
if (!strcmp(str[ct+1], "parent")) {
mEnviroment.mStateFragment =
Script::Enviroment_t::FRAGMENT_PARENT;
ProgramFragment * pf =
(ProgramFragment *)rsc->lookupName(str[ct+1]);
if (pf != NULL) {
mEnviroment.mFragment.set(pf);
continue;
}
LOGE("Unreconized value %s passed to stateFragment", str[ct+1]);
}
if (!strcmp(str[ct], "stateFragmentStore")) {
if (!strcmp(str[ct+1], "alwaysReplace")) {
mEnviroment.mStateFragmentStore =
Script::Enviroment_t::FRAGMENT_STORE_ALWAYS_REPLACE;
continue;
}
if (!strcmp(str[ct+1], "alwaysBlend")) {
mEnviroment.mStateFragmentStore =
Script::Enviroment_t::FRAGMENT_STORE_ALWAYS_BLEND;
continue;
}
if (!strcmp(str[ct+1], "depthLessReplace")) {
mEnviroment.mStateFragmentStore =
Script::Enviroment_t::FRAGMENT_STORE_DEPTH_LESS_REPLACE;
continue;
}
if (!strcmp(str[ct+1], "depthLessBlend")) {
mEnviroment.mStateFragmentStore =
Script::Enviroment_t::FRAGMENT_STORE_DEPTH_LESS_BLEND;
ProgramFragmentStore * pfs =
(ProgramFragmentStore *)rsc->lookupName(str[ct+1]);
if (pfs != NULL) {
mEnviroment.mFragmentStore.set(pfs);
continue;
}
if (!strcmp(str[ct+1], "parent")) {
mEnviroment.mStateFragmentStore =
Script::Enviroment_t::FRAGMENT_STORE_PARENT;
//mEnviroment.mStateFragmentStore =
//Script::Enviroment_t::FRAGMENT_STORE_PARENT;
continue;
}
LOGE("Unreconized value %s passed to stateFragmentStore", str[ct+1]);
@@ -632,7 +568,7 @@ RsScript rsi_ScriptCCreate(Context * rsc)
{
ScriptCState *ss = &rsc->mScriptC;
ss->runCompiler();
ss->runCompiler(rsc);
ScriptC *s = new ScriptC();
s->incRef();

View File

@@ -74,7 +74,7 @@ public:
Vector<const Type *> mConstantBufferTypes;
void clear();
void runCompiler();
void runCompiler(Context *rsc);
};