From 8b2c065dfc16c148e2829a19e83d2269b9bcd4cc Mon Sep 17 00:00:00 2001 From: Jason Sams Date: Wed, 12 Aug 2009 17:54:11 -0700 Subject: Implement reflecting Java objects into the ACC enviroment. --- java/Fountain/res/raw/fountain.c | 53 +++++++---------- .../src/com/android/fountain/FountainRS.java | 47 +++++++-------- rs.spec | 1 + rsComponent.cpp | 5 +- rsComponent.h | 5 +- rsContext.cpp | 1 - rsContext.h | 3 - rsElement.cpp | 66 +++++++++++----------- rsFileA3D.cpp | 2 +- rsScript.h | 5 +- rsScriptC.cpp | 37 +++++++++++- rsScriptC.h | 4 +- rsScriptC_Lib.cpp | 1 - rsUtils.h | 2 + 14 files changed, 126 insertions(+), 106 deletions(-) diff --git a/java/Fountain/res/raw/fountain.c b/java/Fountain/res/raw/fountain.c index 5ed9ed51..6e6afcdc 100644 --- a/java/Fountain/res/raw/fountain.c +++ b/java/Fountain/res/raw/fountain.c @@ -7,38 +7,23 @@ int main(int launchID) { - int count, touch, x, y, rate; - int ct, ct2; - int newPart; - int drawCount; - int idx; - float dx, dy; - float posx,posy; - int c; - int srcIdx; - int dstIdx; - - count = loadI32(0, 1); - touch = loadI32(0, 2); - x = loadI32(0, 3); - y = loadI32(0, 4); - - rate = 4; + int ct; + int count = loadI32(0, OFFSETOF_SomeData_count); + int touch = loadI32(0, OFFSETOF_SomeData_touch); + int rate = 4; int maxLife = (count / rate) - 1; if (touch) { - newPart = loadI32(2, 0); - for (ct2=0; ct2= count) { newPart = 0; @@ -47,21 +32,21 @@ int main(int launchID) { storeI32(2, 0, newPart); } - drawCount = 0; + int drawCount = 0; float height = getHeight(); for (ct=0; ct < count; ct++) { - srcIdx = ct * 5 + 1; + int srcIdx = ct * 5 + 1; - dx = loadF(2, srcIdx); - dy = loadF(2, srcIdx + 1); int life = loadI32(2, srcIdx + 2); - posx = loadF(2, srcIdx + 3); - posy = loadF(2, srcIdx + 4); if (life) { + float posx = loadF(2, srcIdx + 3); + float posy = loadF(2, srcIdx + 4); + float dx = loadF(2, srcIdx); + float dy = loadF(2, srcIdx + 1); if (posy < height) { - dstIdx = drawCount * 9; - c = 0xcfcfcfcf; + int dstIdx = drawCount * 9; + int c = 0xcfcfcfcf; storeI32(1, dstIdx, c); storeF(1, dstIdx + 1, posx); diff --git a/java/Fountain/src/com/android/fountain/FountainRS.java b/java/Fountain/src/com/android/fountain/FountainRS.java index ad4f9497..3e680e35 100644 --- a/java/Fountain/src/com/android/fountain/FountainRS.java +++ b/java/Fountain/src/com/android/fountain/FountainRS.java @@ -23,24 +23,21 @@ import android.content.res.Resources; import android.graphics.Bitmap; import android.graphics.drawable.BitmapDrawable; import android.graphics.drawable.Drawable; +import android.renderscript.*; import android.util.Log; -import android.renderscript.RenderScript; -import android.renderscript.ProgramVertex; -import android.renderscript.Element; -import android.renderscript.Allocation; -import android.renderscript.Script; -import android.renderscript.ScriptC; -import android.renderscript.ProgramFragment; -import android.renderscript.ProgramStore; -import android.renderscript.SimpleMesh; -import android.renderscript.Type; -import android.renderscript.Primitive; - public class FountainRS { public static final int PART_COUNT = 4000; + static class SomeData { + public int x; + public int y; + public int touch; + public int rate; + public int count; + } + public FountainRS() { } @@ -51,10 +48,10 @@ public class FountainRS { } public void newTouchPosition(int x, int y) { - mParams[0] = 1; - mParams[1] = x; - mParams[2] = y; - mIntAlloc.subData1D(2, 3, mParams); + mSD.touch = 1; + mSD.x = x; + mSD.y = y; + mIntAlloc.data(mSD); } @@ -73,10 +70,13 @@ public class FountainRS { private Bitmap mBackground; - int mParams[] = new int[10]; + SomeData mSD = new SomeData(); + private Type mSDType; private void initRS() { - mIntAlloc = Allocation.createSized(mRS, Element.USER_I32, 10); + mSD = new SomeData(); + mSDType = Type.createFromClass(mRS, SomeData.class, 1, "SomeData"); + mIntAlloc = Allocation.createTyped(mRS, mSDType); mVertAlloc = Allocation.createSized(mRS, Element.USER_I32, PART_COUNT * 5 + 1); ProgramStore.Builder bs = new ProgramStore.Builder(mRS, null, null); @@ -91,14 +91,8 @@ public class FountainRS { mPF = bf.create(); mPF.setName("PgmFragParts"); - mParams[0] = 0; - mParams[1] = PART_COUNT; - mParams[2] = 0; - mParams[3] = 0; - mParams[4] = 0; - mParams[5] = 0; - mParams[6] = 0; - mIntAlloc.data(mParams); + mSD.count = PART_COUNT; + mIntAlloc.data(mSD); Element.Builder eb = new Element.Builder(mRS); eb.add(Element.DataType.UNSIGNED, Element.DataKind.RED, true, 8); @@ -124,6 +118,7 @@ public class FountainRS { ScriptC.Builder sb = new ScriptC.Builder(mRS); sb.setScript(mRes, R.raw.fountain); sb.setRoot(true); + sb.addType(mSDType); mScript = sb.create(); mScript.setClearColor(0.0f, 0.0f, 0.0f, 1.0f); diff --git a/rs.spec b/rs.spec index 7dbf4123..d4fbf6b2 100644 --- a/rs.spec +++ b/rs.spec @@ -44,6 +44,7 @@ ElementAdd { param RsDataType dataType param bool isNormalized param size_t bits + param const char * name } ElementCreate { diff --git a/rsComponent.cpp b/rsComponent.cpp index fde62a00..831580b2 100644 --- a/rsComponent.cpp +++ b/rsComponent.cpp @@ -31,12 +31,15 @@ Component::Component() Component::Component( DataKind dk, DataType dt, - bool isNormalized, uint32_t bits) + bool isNormalized, uint32_t bits, const char * name) { mType = dt; mKind = dk; mIsNormalized = isNormalized; mBits = bits; + if (name) { + mName = name; + } } Component::~Component() diff --git a/rsComponent.h b/rsComponent.h index 9db107fc..5ee95c71 100644 --- a/rsComponent.h +++ b/rsComponent.h @@ -44,7 +44,7 @@ public: }; - Component(DataKind dk, DataType dt, bool isNormalized, uint32_t bits); + Component(DataKind dk, DataType dt, bool isNorm, uint32_t bits, const char *); virtual ~Component(); DataType getType() const {return mType;} @@ -54,12 +54,15 @@ public: uint32_t getGLType() const; + const char * getComponentName() const {return mName.string();} + protected: DataType mType; bool mIsNormalized; DataKind mKind; uint32_t mBits; + String8 mName; private: Component(); diff --git a/rsContext.cpp b/rsContext.cpp index 9de23b32..28604528 100644 --- a/rsContext.cpp +++ b/rsContext.cpp @@ -17,7 +17,6 @@ #include "rsDevice.h" #include "rsContext.h" #include "rsThreadIO.h" -#include "utils/String8.h" #include #include diff --git a/rsContext.h b/rsContext.h index 54c555ff..60a526bd 100644 --- a/rsContext.h +++ b/rsContext.h @@ -19,9 +19,6 @@ #include "rsUtils.h" -#include -#include -#include #include #include "rsType.h" diff --git a/rsElement.cpp b/rsElement.cpp index 0c5cb18d..97b18c06 100644 --- a/rsElement.cpp +++ b/rsElement.cpp @@ -23,45 +23,45 @@ using namespace android::renderscript; void ElementState::initPredefined() { - Component * u_8 = new Component(Component::USER, Component::UNSIGNED, true, 8); - Component * i_8 = new Component(Component::USER, Component::SIGNED, true, 8); - Component * u_16 = new Component(Component::USER, Component::UNSIGNED, true, 16); - Component * i_16 = new Component(Component::USER, Component::SIGNED, true, 16); - Component * u_32 = new Component(Component::USER, Component::UNSIGNED, true, 32); - Component * i_32 = new Component(Component::USER, Component::SIGNED, true, 32); - Component * f_32 = new Component(Component::USER, Component::FLOAT, true, 32); + Component * u_8 = new Component(Component::USER, Component::UNSIGNED, true, 8, 0); + Component * i_8 = new Component(Component::USER, Component::SIGNED, true, 8, 0); + Component * u_16 = new Component(Component::USER, Component::UNSIGNED, true, 16, 0); + Component * i_16 = new Component(Component::USER, Component::SIGNED, true, 16, 0); + Component * u_32 = new Component(Component::USER, Component::UNSIGNED, true, 32, 0); + Component * i_32 = new Component(Component::USER, Component::SIGNED, true, 32, 0); + Component * f_32 = new Component(Component::USER, Component::FLOAT, true, 32, 0); - Component * r_4 = new Component(Component::RED, Component::UNSIGNED, true, 4); - Component * r_5 = new Component(Component::RED, Component::UNSIGNED, true, 5); - Component * r_8 = new Component(Component::RED, Component::UNSIGNED, true, 8); + Component * r_4 = new Component(Component::RED, Component::UNSIGNED, true, 4, 0); + Component * r_5 = new Component(Component::RED, Component::UNSIGNED, true, 5, 0); + Component * r_8 = new Component(Component::RED, Component::UNSIGNED, true, 8, 0); - Component * g_4 = new Component(Component::GREEN, Component::UNSIGNED, true, 4); - Component * g_5 = new Component(Component::GREEN, Component::UNSIGNED, true, 5); - Component * g_6 = new Component(Component::GREEN, Component::UNSIGNED, true, 6); - Component * g_8 = new Component(Component::GREEN, Component::UNSIGNED, true, 8); + Component * g_4 = new Component(Component::GREEN, Component::UNSIGNED, true, 4, 0); + Component * g_5 = new Component(Component::GREEN, Component::UNSIGNED, true, 5, 0); + Component * g_6 = new Component(Component::GREEN, Component::UNSIGNED, true, 6, 0); + Component * g_8 = new Component(Component::GREEN, Component::UNSIGNED, true, 8, 0); - Component * b_4 = new Component(Component::BLUE, Component::UNSIGNED, true, 4); - Component * b_5 = new Component(Component::BLUE, Component::UNSIGNED, true, 5); - Component * b_8 = new Component(Component::BLUE, Component::UNSIGNED, true, 8); + Component * b_4 = new Component(Component::BLUE, Component::UNSIGNED, true, 4, 0); + Component * b_5 = new Component(Component::BLUE, Component::UNSIGNED, true, 5, 0); + Component * b_8 = new Component(Component::BLUE, Component::UNSIGNED, true, 8, 0); - Component * a_1 = new Component(Component::ALPHA, Component::UNSIGNED, true, 1); - Component * a_4 = new Component(Component::ALPHA, Component::UNSIGNED, true, 4); - Component * a_8 = new Component(Component::ALPHA, Component::UNSIGNED, true, 8); + Component * a_1 = new Component(Component::ALPHA, Component::UNSIGNED, true, 1, 0); + Component * a_4 = new Component(Component::ALPHA, Component::UNSIGNED, true, 4, 0); + Component * a_8 = new Component(Component::ALPHA, Component::UNSIGNED, true, 8, 0); - Component * idx_16 = new Component(Component::INDEX, Component::UNSIGNED, false, 16); - Component * idx_32 = new Component(Component::INDEX, Component::UNSIGNED, false, 32); + Component * idx_16 = new Component(Component::INDEX, Component::UNSIGNED, false, 16, 0); + Component * idx_32 = new Component(Component::INDEX, Component::UNSIGNED, false, 32, 0); - Component * x = new Component(Component::X, Component::FLOAT, false, 32); - Component * y = new Component(Component::Y, Component::FLOAT, false, 32); - Component * z = new Component(Component::Z, Component::FLOAT, false, 32); + Component * x = new Component(Component::X, Component::FLOAT, false, 32, 0); + Component * y = new Component(Component::Y, Component::FLOAT, false, 32, 0); + Component * z = new Component(Component::Z, Component::FLOAT, false, 32, 0); - Component * nx = new Component(Component::NX, Component::FLOAT, false, 32); - Component * ny = new Component(Component::NY, Component::FLOAT, false, 32); - Component * nz = new Component(Component::NZ, Component::FLOAT, false, 32); + Component * nx = new Component(Component::NX, Component::FLOAT, false, 32, 0); + Component * ny = new Component(Component::NY, Component::FLOAT, false, 32, 0); + Component * nz = new Component(Component::NZ, Component::FLOAT, false, 32, 0); - Component * s = new Component(Component::S, Component::FLOAT, false, 32); - Component * t = new Component(Component::T, Component::FLOAT, false, 32); + Component * s = new Component(Component::S, Component::FLOAT, false, 32, 0); + Component * t = new Component(Component::T, Component::FLOAT, false, 32, 0); Element * e; @@ -391,14 +391,14 @@ RsElement rsi_ElementGetPredefined(Context *rsc, RsElementPredefined predef) return e; } -void rsi_ElementAdd(Context *rsc, RsDataKind dk, RsDataType dt, bool isNormalized, size_t bits) +void rsi_ElementAdd(Context *rsc, RsDataKind dk, RsDataType dt, bool isNormalized, size_t bits, const char *name) { ElementState * sec = &rsc->mStateElement; - Component *c = new Component(static_cast(dk), static_cast(dt), isNormalized, - bits); + bits, + name); sec->mComponentBuildList.add(c); } diff --git a/rsFileA3D.cpp b/rsFileA3D.cpp index 86d294bf..347ef232 100644 --- a/rsFileA3D.cpp +++ b/rsFileA3D.cpp @@ -334,7 +334,7 @@ void FileA3D::processChunk_Element(Context *rsc, IO *io, A3DIndexEntry *ie) uint32_t bits = io->loadU8(); bool isNorm = io->loadU8() != 0; LOGE(" %i %i %i %i", dk, dt, bits, isNorm); - rsi_ElementAdd(rsc, dk, dt, isNorm, bits); + rsi_ElementAdd(rsc, dk, dt, isNorm, bits, 0); } LOGE("processChunk_Element create"); ie->mRsObj = rsi_ElementCreate(rsc); diff --git a/rsScript.h b/rsScript.h index a8e04a68..6983ef43 100644 --- a/rsScript.h +++ b/rsScript.h @@ -29,6 +29,8 @@ class ProgramFragment; class ProgramRaster; class ProgramFragmentStore; +#define MAX_SCRIPT_BANKS 16 + class Script : public ObjectBase { public: @@ -57,7 +59,8 @@ public: const Type * mConstantBufferTypes; uint32_t mCounstantBufferCount; - ObjectBaseRef mSlots[16]; + ObjectBaseRef mSlots[MAX_SCRIPT_BANKS]; + ObjectBaseRef mTypes[MAX_SCRIPT_BANKS]; virtual bool run(Context *, uint32_t launchID) = 0; }; diff --git a/rsScriptC.cpp b/rsScriptC.cpp index 3343db51..652283d3 100644 --- a/rsScriptC.cpp +++ b/rsScriptC.cpp @@ -19,7 +19,6 @@ #include "rsMatrix.h" #include "acc/acc.h" -#include "utils/String8.h" #include "utils/Timers.h" #include @@ -91,7 +90,10 @@ void ScriptCState::clear() { memset(&mProgram, 0, sizeof(mProgram)); - mConstantBufferTypes.clear(); + mConstantTypeCount = 0; + for (uint32_t ct=0; ct < MAX_SCRIPT_BANKS; ct++) { + mConstantBufferTypes[ct].clear(); + } memset(&mEnviroment, 0, sizeof(mEnviroment)); mEnviroment.mClearColor[0] = 0; @@ -127,6 +129,7 @@ void ScriptCState::runCompiler(Context *rsc) appendDecls(&tmp); rsc->appendVarDefines(&tmp); appendVarDefines(&tmp); + appendTypes(&tmp); tmp.append("#line 1\n"); const char* scriptSource[] = {tmp.string(), mProgram.mScriptText}; @@ -242,6 +245,31 @@ void ScriptCState::appendVarDefines(String8 *str) } } +void ScriptCState::appendTypes(String8 *str) +{ + char buf[256]; + + for (size_t ct=0; ct < mConstantTypeCount; ct++) { + const Type *t = mConstantBufferTypes[ct].get(); + const Element *e = t->getElement(); + + if (!t->getName()) { + continue; + } + for (size_t ct2=0; ct2 < e->getComponentCount(); ct2++) { + const Component *c = e->getComponent(ct2); + str->append("#define OFFSETOF_"); + str->append(t->getName()); + str->append("_"); + str->append(c->getComponentName()); + sprintf(buf, " %i\n", ct2); + str->append(buf); + } + + } + +} + namespace android { namespace renderscript { @@ -255,7 +283,10 @@ void rsi_ScriptCBegin(Context * rsc) void rsi_ScriptCAddType(Context * rsc, RsType vt) { ScriptCState *ss = &rsc->mScriptC; - ss->mConstantBufferTypes.add(static_cast(vt)); + const Type *t = static_cast(vt); + + ss->mConstantBufferTypes[ss->mConstantTypeCount].set(t); + ss->mConstantTypeCount ++; } void rsi_ScriptCSetScript(Context * rsc, void *vp) diff --git a/rsScriptC.h b/rsScriptC.h index ad0e3ee1..60a6fba1 100644 --- a/rsScriptC.h +++ b/rsScriptC.h @@ -68,11 +68,13 @@ public: ScriptC::Program_t mProgram; Script::Enviroment_t mEnviroment; - Vector mConstantBufferTypes; + ObjectBaseRef mConstantBufferTypes[MAX_SCRIPT_BANKS]; + uint32_t mConstantTypeCount; void clear(); void runCompiler(Context *rsc); void appendVarDefines(String8 *str); + void appendTypes(String8 *str); struct SymbolTable_t { const char * mName; diff --git a/rsScriptC_Lib.cpp b/rsScriptC_Lib.cpp index 4ee112dc..c13894b7 100644 --- a/rsScriptC_Lib.cpp +++ b/rsScriptC_Lib.cpp @@ -20,7 +20,6 @@ #include "rsNoise.h" #include "acc/acc.h" -#include "utils/String8.h" #include "utils/Timers.h" #include diff --git a/rsUtils.h b/rsUtils.h index f0e47506..ec928db6 100644 --- a/rsUtils.h +++ b/rsUtils.h @@ -21,6 +21,8 @@ #define LOG_TAG "rs" #include #include +#include +#include #include #include #include -- cgit v1.2.3