Merge change 20124

* changes:
  Remove useless slot from ProgramVertex.  Optimize GL state setup.
This commit is contained in:
Android (Google) Code Review
2009-08-05 14:00:02 -07:00
20 changed files with 123 additions and 114 deletions

View File

@@ -38,8 +38,8 @@ public class ProgramVertex extends BaseObj {
mID = 0;
}
public void bindAllocation(int slot, MatrixAllocation va) {
mRS.nProgramVertexBindAllocation(mID, slot, va.mAlloc.mID);
public void bindAllocation(MatrixAllocation va) {
mRS.nProgramVertexBindAllocation(mID, va.mAlloc.mID);
}

View File

@@ -164,9 +164,8 @@ public class RenderScript {
native void nProgramFragmentDestroy(int pgm);
native void nProgramVertexDestroy(int pv);
native void nProgramVertexBindAllocation(int pv, int slot, int mID);
native void nProgramVertexBindAllocation(int pv, int mID);
native void nProgramVertexBegin(int inID, int outID);
native void nProgramVertexSetType(int slot, int mID);
native void nProgramVertexSetTextureMatrixEnable(boolean enable);
native void nProgramVertexAddLight(int id);
native int nProgramVertexCreate();

View File

@@ -909,19 +909,11 @@ nProgramVertexBegin(JNIEnv *_env, jobject _this, jint in, jint out)
}
static void
nProgramVertexBindAllocation(JNIEnv *_env, jobject _this, jint vpv, jint slot, jint a)
nProgramVertexBindAllocation(JNIEnv *_env, jobject _this, jint vpv, jint a)
{
RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
LOG_API("nProgramVertexBindAllocation, con(%p), vpf(%p), slot(%i), a(%p)", con, (RsProgramVertex)vpv, slot, (RsAllocation)a);
rsProgramVertexBindAllocation((RsProgramFragment)vpv, slot, (RsAllocation)a);
}
static void
nProgramVertexSetType(JNIEnv *_env, jobject _this, jint slot, jint t)
{
RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
LOG_API("nProgramVertexSetType, con(%p), vpf(%p), slot(%i), a(%p)", con, (RsProgramVertex)vpv, slot, (RsType)t);
rsProgramVertexSetType(slot, (RsType)t);
rsProgramVertexBindAllocation((RsProgramFragment)vpv, (RsAllocation)a);
}
static void
@@ -1186,9 +1178,8 @@ static JNINativeMethod methods[] = {
{"nProgramFragmentDestroy", "(I)V", (void*)nProgramFragmentDestroy },
{"nProgramVertexDestroy", "(I)V", (void*)nProgramVertexDestroy },
{"nProgramVertexBindAllocation", "(III)V", (void*)nProgramVertexBindAllocation },
{"nProgramVertexBindAllocation", "(II)V", (void*)nProgramVertexBindAllocation },
{"nProgramVertexBegin", "(II)V", (void*)nProgramVertexBegin },
{"nProgramVertexSetType", "(II)V", (void*)nProgramVertexSetType },
{"nProgramVertexSetTextureMatrixEnable", "(Z)V", (void*)nProgramVertexSetTextureMatrixEnable },
{"nProgramVertexAddLight", "(I)V", (void*)nProgramVertexAddLight },
{"nProgramVertexCreate", "()I", (void*)nProgramVertexCreate },

View File

@@ -21,6 +21,7 @@ import android.renderscript.RenderScript;
import android.renderscript.ScriptC;
import android.renderscript.ProgramFragment;
import android.renderscript.ProgramStore;
import android.renderscript.ProgramVertex;
import android.renderscript.Allocation;
import android.renderscript.Sampler;
import android.renderscript.ProgramVertex;
@@ -79,7 +80,7 @@ class FallRS {
mResources = res;
initRS();
}
public void destroy() {
mScript.destroy();
mSampler.destroy();
@@ -126,7 +127,7 @@ class FallRS {
}
private void createMesh() {
}
private void createScriptStructures() {
@@ -210,7 +211,7 @@ class FallRS {
ProgramVertex.Builder pvb = new ProgramVertex.Builder(mRS, null, null);
pvb.setTextureMatrixEnable(true);
mPvBackground = pvb.create();
mPvBackground.bindAllocation(0, mPvOrthoAlloc);
mPvBackground.bindAllocation(mPvOrthoAlloc);
mPvBackground.setName("PVBackground");
}
}

View File

@@ -240,8 +240,8 @@ public class FilmRS {
initState();
mPVA = new ProgramVertex.MatrixAllocation(mRS);
mPVBackground.bindAllocation(0, mPVA);
mPVImages.bindAllocation(0, mPVA);
mPVBackground.bindAllocation(mPVA);
mPVImages.bindAllocation(mPVA);
mPVA.setupProjectionNormalized(320, 480);

View File

@@ -265,7 +265,7 @@ class GrassRS {
ProgramVertex.Builder pvb = new ProgramVertex.Builder(mRS, null, null);
pvb.setTextureMatrixEnable(true);
mPvBackground = pvb.create();
mPvBackground.bindAllocation(0, mPvOrthoAlloc);
mPvBackground.bindAllocation(mPvOrthoAlloc);
mPvBackground.setName("PVBackground");
}
}

View File

@@ -168,7 +168,7 @@ public class RolloRS {
ProgramVertex.Builder pvb = new ProgramVertex.Builder(mRS, null, null);
mPV = pvb.create();
mPV.setName("PV");
mPV.bindAllocation(0, mPVAlloc);
mPV.bindAllocation(mPVAlloc);
mPVOrthoAlloc = new ProgramVertex.MatrixAllocation(mRS);
mPVOrthoAlloc.setupOrthoWindow(mWidth, mHeight);
@@ -176,7 +176,7 @@ public class RolloRS {
pvb.setTextureMatrixEnable(true);
mPVOrtho = pvb.create();
mPVOrtho.setName("PVOrtho");
mPVOrtho.bindAllocation(0, mPVOrthoAlloc);
mPVOrtho.bindAllocation(mPVOrthoAlloc);
mRS.contextBindProgramVertex(mPV);

View File

@@ -409,15 +409,9 @@ ProgramVertexCreate {
ProgramVertexBindAllocation {
param RsProgramVertex vpgm
param uint32_t slot
param RsAllocation constants
}
ProgramVertexSetType {
param uint32_t slot
param RsType constants
}
ProgramVertexSetTextureMatrixEnable {
param bool enable
}

View File

@@ -501,23 +501,25 @@ RsAllocation rsi_AllocationCreateFromFile(Context *rsc, const char *file, bool g
return texAlloc;
}
void rsi_AllocationData(Context *rsc, RsAllocation va, const void *data)
{
Allocation *a = static_cast<Allocation *>(va);
a->data(data);
rsc->allocationCheck(a);
}
void rsi_Allocation1DSubData(Context *rsc, RsAllocation va, uint32_t xoff, uint32_t count, const void *data)
{
Allocation *a = static_cast<Allocation *>(va);
a->subData(xoff, count, data);
rsc->allocationCheck(a);
}
void rsi_Allocation2DSubData(Context *rsc, RsAllocation va, uint32_t xoff, uint32_t yoff, uint32_t w, uint32_t h, const void *data)
{
Allocation *a = static_cast<Allocation *>(va);
a->subData(xoff, yoff, w, h, data);
rsc->allocationCheck(a);
}

View File

@@ -85,10 +85,15 @@ bool Context::runScript(Script *s, uint32_t launchID)
bool Context::runRootScript()
{
#if RS_LOG_TIMES
struct timespec beginTime;
clock_gettime(CLOCK_MONOTONIC, &beginTime);
#endif
rsAssert(mRootScript->mEnviroment.mIsRoot);
glColor4f(1,1,1,1);
glEnable(GL_LIGHT0);
//glColor4f(1,1,1,1);
//glEnable(GL_LIGHT0);
glViewport(0, 0, mWidth, mHeight);
glDepthMask(GL_TRUE);
@@ -102,19 +107,34 @@ bool Context::runRootScript()
glClear(GL_COLOR_BUFFER_BIT);
glClear(GL_DEPTH_BUFFER_BIT);
return runScript(mRootScript.get(), 0);
#if RS_LOG_TIMES
struct timespec startTime;
clock_gettime(CLOCK_MONOTONIC, &startTime);
#endif
bool ret = runScript(mRootScript.get(), 0);
#if RS_LOG_TIMES
struct timespec endTime;
clock_gettime(CLOCK_MONOTONIC, &endTime);
int t1 = ((unsigned long)startTime.tv_nsec - (unsigned long)beginTime.tv_nsec) / 1000 / 1000;
int t2 = ((unsigned long)endTime.tv_nsec - (unsigned long)startTime.tv_nsec) / 1000 / 1000;
LOGE("times %i, %i", t1, t2);
#endif
return ret;
}
void Context::setupCheck()
{
if (mFragmentStore.get()) {
mFragmentStore->setupGL();
mFragmentStore->setupGL(&mStateFragmentStore);
}
if (mFragment.get()) {
mFragment->setupGL();
mFragment->setupGL(&mStateFragment);
}
if (mVertex.get()) {
mVertex->setupGL();
mVertex->setupGL(&mStateVertex);
}
}
@@ -245,7 +265,6 @@ void Context::setFragmentStore(ProgramFragmentStore *pfs)
} else {
mFragmentStore.set(pfs);
}
mFragmentStore->setupGL();
}
void Context::setFragment(ProgramFragment *pf)
@@ -255,7 +274,13 @@ void Context::setFragment(ProgramFragment *pf)
} else {
mFragment.set(pf);
}
mFragment->setupGL();
}
void Context::allocationCheck(const Allocation *a)
{
mVertex->checkUpdatedAllocation(a);
mFragment->checkUpdatedAllocation(a);
mFragmentStore->checkUpdatedAllocation(a);
}
void Context::setVertex(ProgramVertex *pv)
@@ -265,7 +290,6 @@ void Context::setVertex(ProgramVertex *pv)
} else {
mVertex.set(pv);
}
mVertex->setupGL();
}
void Context::assignName(ObjectBase *obj, const char *name, uint32_t len)

View File

@@ -45,7 +45,7 @@
namespace android {
namespace renderscript {
class Context
class Context
{
public:
Context(Device *, Surface *);
@@ -86,6 +86,7 @@ public:
const ProgramVertex * getVertex() {return mVertex.get();}
void setupCheck();
void allocationCheck(const Allocation *);
void assignName(ObjectBase *obj, const char *name, uint32_t len);
void removeName(ObjectBase *obj);

View File

@@ -34,12 +34,19 @@ Program::~Program()
}
void Program::setAllocation(Allocation *alloc)
void Program::bindAllocation(Allocation *alloc)
{
mConstants.set(alloc);
mDirty = true;
}
void Program::checkUpdatedAllocation(const Allocation *alloc)
{
if (mConstants.get() == alloc) {
mDirty = true;
}
}
void Program::setupGL()
{

View File

@@ -33,10 +33,12 @@ public:
virtual ~Program();
void setAllocation(Allocation *);
void bindAllocation(Allocation *);
virtual void setupGL();
void checkUpdatedAllocation(const Allocation *);
protected:
// Components not listed in "in" will be passed though
// unless overwritten by components in out.
@@ -45,8 +47,7 @@ protected:
ObjectBaseRef<Allocation> mConstants;
bool mDirty;
mutable bool mDirty;
};

View File

@@ -39,8 +39,13 @@ ProgramFragment::~ProgramFragment()
{
}
void ProgramFragment::setupGL()
void ProgramFragment::setupGL(ProgramFragmentState *state)
{
if ((state->mLast.get() == this) && !mDirty) {
return;
}
state->mLast.set(this);
for (uint32_t ct=0; ct < MAX_TEXTURE; ct++) {
glActiveTexture(GL_TEXTURE0 + ct);
if (!(mTextureEnableMask & (1 << ct)) || !mTextures[ct].get()) {
@@ -90,8 +95,8 @@ void ProgramFragment::setupGL()
}
}
glActiveTexture(GL_TEXTURE0);
mDirty = false;
}
@@ -104,6 +109,7 @@ void ProgramFragment::bindTexture(uint32_t slot, Allocation *a)
//LOGE("bindtex %i %p", slot, a);
mTextures[slot].set(a);
mDirty = true;
}
void ProgramFragment::bindSampler(uint32_t slot, Sampler *s)
@@ -114,6 +120,7 @@ void ProgramFragment::bindSampler(uint32_t slot, Sampler *s)
}
mSamplers[slot].set(s);
mDirty = true;
}
void ProgramFragment::setType(uint32_t slot, const Element *e, uint32_t dim)
@@ -190,7 +197,7 @@ void rsi_ProgramFragmentBindTexture(Context *rsc, RsProgramFragment vpf, uint32_
ProgramFragment *pf = static_cast<ProgramFragment *>(vpf);
pf->bindTexture(slot, static_cast<Allocation *>(a));
if (pf == rsc->getFragment()) {
pf->setupGL();
//pf->setupGL();
}
}
@@ -200,7 +207,7 @@ void rsi_ProgramFragmentBindSampler(Context *rsc, RsProgramFragment vpf, uint32_
pf->bindSampler(slot, static_cast<Sampler *>(s));
if (pf == rsc->getFragment()) {
pf->setupGL();
//pf->setupGL();
}
}

View File

@@ -23,19 +23,19 @@
namespace android {
namespace renderscript {
class ProgramFragmentState;
class ProgramFragment : public Program
{
public:
const static uint32_t MAX_TEXTURE = 2;
const static uint32_t MAX_CONSTANTS = 2;
ProgramFragment(Element *in, Element *out);
virtual ~ProgramFragment();
virtual void setupGL();
virtual void setupGL(ProgramFragmentState *);
@@ -53,7 +53,7 @@ protected:
// Texture lookups go though a sampler which in effect converts normalized
// coordinates into type specific. Multiple samples may also be taken
// and filtered.
//
//
// Constants are strictly accessed by programetic loads.
ObjectBaseRef<Allocation> mTextures[MAX_TEXTURE];
ObjectBaseRef<Sampler> mSamplers[MAX_TEXTURE];
@@ -61,21 +61,12 @@ protected:
uint32_t mTextureDimensions[MAX_TEXTURE];
ObjectBaseRef<Allocation> mConstants[MAX_CONSTANTS];
ObjectBaseRef<Type> mConstantTypes[MAX_CONSTANTS];
// Hacks to create a program for now
RsTexEnvMode mEnvModes[MAX_TEXTURE];
uint32_t mTextureEnableMask;
};
class ProgramFragmentState
class ProgramFragmentState
{
public:
ProgramFragmentState();
@@ -87,6 +78,8 @@ public:
ObjectBaseRef<Type> mTextureTypes[ProgramFragment::MAX_TEXTURE];
ObjectBaseRef<ProgramFragment> mDefault;
Vector<ProgramFragment *> mPrograms;
ObjectBaseRef<ProgramFragment> mLast;
};

View File

@@ -48,8 +48,13 @@ ProgramFragmentStore::~ProgramFragmentStore()
{
}
void ProgramFragmentStore::setupGL()
void ProgramFragmentStore::setupGL(ProgramFragmentStoreState *state)
{
if (state->mLast.get() == this) {
return;
}
state->mLast.set(this);
glColorMask(mColorRWriteEnable,
mColorGWriteEnable,
mColorBWriteEnable,
@@ -123,7 +128,7 @@ void ProgramFragmentStore::setDepthMask(bool mask)
void ProgramFragmentStore::setBlendFunc(RsBlendSrcFunc src, RsBlendDstFunc dst)
{
mBlendEnable = true;
if ((src == RS_BLEND_SRC_ONE) &&
if ((src == RS_BLEND_SRC_ONE) &&
(dst == RS_BLEND_DST_ZERO)) {
mBlendEnable = false;
}

View File

@@ -23,18 +23,15 @@
namespace android {
namespace renderscript {
class ProgramFragmentStoreState;
class ProgramFragmentStore : public Program
{
public:
ProgramFragmentStore(Element *in, Element *out);
virtual ~ProgramFragmentStore();
virtual void setupGL();
virtual void setupGL(ProgramFragmentStoreState *);
void setDepthFunc(RsDepthFunc);
void setDepthMask(bool);
@@ -55,21 +52,14 @@ protected:
int32_t mBlendSrc;
int32_t mBlendDst;
bool mDepthTestEnable;
bool mDepthWriteEnable;
int32_t mDepthFunc;
bool mStencilTestEnable;
};
class ProgramFragmentStoreState
class ProgramFragmentStoreState
{
public:
ProgramFragmentStoreState();
@@ -77,6 +67,9 @@ public:
void init(Context *rsc, int32_t w, int32_t h);
ObjectBaseRef<ProgramFragmentStore> mDefault;
ObjectBaseRef<ProgramFragmentStore> mLast;
ProgramFragmentStore *mPFS;
};

View File

@@ -44,9 +44,14 @@ static void logMatrix(const char *txt, const float *f)
LOGV("%6.2f, %6.2f, %6.2f, %6.2f", f[3], f[7], f[11], f[15]);
}
void ProgramVertex::setupGL()
void ProgramVertex::setupGL(ProgramVertexState *state)
{
const float *f = static_cast<const float *>(mConstants[0]->getPtr());
if ((state->mLast.get() == this) && !mDirty) {
return;
}
state->mLast.set(this);
const float *f = static_cast<const float *>(mConstants->getPtr());
glMatrixMode(GL_TEXTURE);
if (mTextureMatrixEnable) {
@@ -72,7 +77,7 @@ void ProgramVertex::setupGL()
} else {
glDisable(GL_LIGHTING);
}
if (!f) {
LOGE("Must bind constants to vertex program");
}
@@ -81,16 +86,8 @@ void ProgramVertex::setupGL()
glLoadMatrixf(&f[RS_PROGRAM_VERTEX_PROJECTION_OFFSET]);
glMatrixMode(GL_MODELVIEW);
glLoadMatrixf(&f[RS_PROGRAM_VERTEX_MODELVIEW_OFFSET]);
}
void ProgramVertex::setConstantType(uint32_t slot, const Type *t)
{
mConstantTypes[slot].set(t);
}
void ProgramVertex::bindAllocation(uint32_t slot, Allocation *a)
{
mConstants[slot].set(a);
mDirty = false;
}
void ProgramVertex::addLight(const Light *l)
@@ -103,20 +100,23 @@ void ProgramVertex::addLight(const Light *l)
void ProgramVertex::setProjectionMatrix(const rsc_Matrix *m) const
{
float *f = static_cast<float *>(mConstants[0]->getPtr());
float *f = static_cast<float *>(mConstants->getPtr());
memcpy(&f[RS_PROGRAM_VERTEX_PROJECTION_OFFSET], m, sizeof(rsc_Matrix));
mDirty = true;
}
void ProgramVertex::setModelviewMatrix(const rsc_Matrix *m) const
{
float *f = static_cast<float *>(mConstants[0]->getPtr());
float *f = static_cast<float *>(mConstants->getPtr());
memcpy(&f[RS_PROGRAM_VERTEX_MODELVIEW_OFFSET], m, sizeof(rsc_Matrix));
mDirty = true;
}
void ProgramVertex::setTextureMatrix(const rsc_Matrix *m) const
{
float *f = static_cast<float *>(mConstants[0]->getPtr());
float *f = static_cast<float *>(mConstants->getPtr());
memcpy(&f[RS_PROGRAM_VERTEX_TEXTURE_OFFSET], m, sizeof(rsc_Matrix));
mDirty = true;
}
@@ -139,8 +139,8 @@ void ProgramVertexState::init(Context *rsc, int32_t w, int32_t h)
mDefaultAlloc.set(alloc);
mDefault.set(pv);
pv->bindAllocation(0, alloc);
pv->bindAllocation(alloc);
Matrix m;
m.loadOrtho(0,w, h,0, -1,1);
alloc->subData(RS_PROGRAM_VERTEX_PROJECTION_OFFSET, 16, &m.m[0]);
@@ -167,15 +167,10 @@ RsProgramVertex rsi_ProgramVertexCreate(Context *rsc)
return pv;
}
void rsi_ProgramVertexBindAllocation(Context *rsc, RsProgramVertex vpgm, uint32_t slot, RsAllocation constants)
void rsi_ProgramVertexBindAllocation(Context *rsc, RsProgramVertex vpgm, RsAllocation constants)
{
ProgramVertex *pv = static_cast<ProgramVertex *>(vpgm);
pv->bindAllocation(slot, static_cast<Allocation *>(constants));
}
void rsi_ProgramVertexSetType(Context *rsc, uint32_t slot, RsType constants)
{
rsc->mStateVertex.mPV->setConstantType(slot, static_cast<const Type *>(constants));
pv->bindAllocation(static_cast<Allocation *>(constants));
}
void rsi_ProgramVertexSetTextureMatrixEnable(Context *rsc, bool enable)

View File

@@ -23,21 +23,19 @@
namespace android {
namespace renderscript {
class ProgramVertexState;
class ProgramVertex : public Program
{
public:
const static uint32_t MAX_CONSTANTS = 2;
const static uint32_t MAX_LIGHTS = 8;
ProgramVertex(Element *in, Element *out);
virtual ~ProgramVertex();
virtual void setupGL();
virtual void setupGL(ProgramVertexState *state);
void setConstantType(uint32_t slot, const Type *);
void bindAllocation(uint32_t slot, Allocation *);
void setTextureMatrixEnable(bool e) {mTextureMatrixEnable = e;}
void addLight(const Light *);
@@ -46,21 +44,15 @@ public:
void setTextureMatrix(const rsc_Matrix *) const;
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;
};
class ProgramVertexState
class ProgramVertexState
{
public:
ProgramVertexState();
@@ -69,8 +61,9 @@ public:
void init(Context *rsc, int32_t w, int32_t h);
ObjectBaseRef<ProgramVertex> mDefault;
ObjectBaseRef<ProgramVertex> mLast;
ObjectBaseRef<Allocation> mDefaultAlloc;
ProgramVertex *mPV;

View File

@@ -17,12 +17,13 @@
#ifndef ANDROID_RS_UTILS_H
#define ANDROID_RS_UTILS_H
#define LOG_NDEBUG 0
#define LOG_NDEBUG 0
#define LOG_TAG "rs"
#include <utils/Log.h>
#include <utils/Vector.h>
#include <stdlib.h>
#include <pthread.h>
#include <time.h>
#include <EGL/egl.h>
#include <math.h>
@@ -38,6 +39,8 @@ namespace renderscript {
#define rsAssert(v) while(0)
#endif
#define RS_LOG_TIMES 0
template<typename T>
T rsMin(T in1, T in2)
{