Merge change 21926 into eclair

* changes:
  Move the sky
This commit is contained in:
Android (Google) Code Review
2009-08-19 12:10:33 -07:00
5 changed files with 168 additions and 206 deletions

View File

@@ -18,10 +18,6 @@ package android.renderscript;
import java.lang.reflect.Field;
import android.renderscript.Element;
import android.util.Config;
import android.util.Log;
/**
* @hide
*
@@ -113,9 +109,7 @@ public class Type extends BaseObj {
public void add(Dimension d, int value) {
if(mEntries.length >= mEntryCount) {
Entry[] en = new Entry[mEntryCount + 8];
for(int ct=0; ct < mEntries.length; ct++) {
en[ct] = mEntries[ct];
}
System.arraycopy(mEntries, 0, en, 0, mEntries.length);
mEntries = en;
}
mEntries[mEntryCount] = new Entry();

BIN
libs/rs/java/Fall/res/drawable-hdpi/sky.jpg Executable file → Normal file

Binary file not shown.

Before

Width:  |  Height:  |  Size: 89 KiB

After

Width:  |  Height:  |  Size: 80 KiB

View File

@@ -18,25 +18,10 @@
#pragma stateFragmentStore(PFSBackground)
#define RSID_STATE 0
#define RSID_FRAME_COUNT 0
#define RSID_WIDTH 1
#define RSID_HEIGHT 2
#define RSID_MESH_WIDTH 3
#define RSID_MESH_HEIGHT 4
#define RSID_RIPPLE_MAP_SIZE 5
#define RSID_RIPPLE_INDEX 6
#define RSID_DROP_X 7
#define RSID_DROP_Y 8
#define RSID_RUNNING 9
#define RSID_LEAVES_COUNT 10
#define RSID_RIPPLE_MAP 1
#define RSID_REFRACTION_MAP 2
#define RSID_LEAVES 3
#define RSID_GL_STATE 4
#define RSID_GL_WIDTH 0
#define RSID_GL_HEIGHT 1
#define RSID_DROP 4
#define LEAF_STRUCT_FIELDS_COUNT 11
#define LEAF_STRUCT_X 0
@@ -67,8 +52,8 @@ int offset(int x, int y, int width) {
}
void dropWithStrength(int x, int y, int r, int s) {
int width = loadI32(RSID_STATE, RSID_MESH_WIDTH);
int height = loadI32(RSID_STATE, RSID_MESH_HEIGHT);
int width = State_meshWidth;
int height = State_meshHeight;
if (x < r) x = r;
if (y < r) y = r;
@@ -77,8 +62,8 @@ void dropWithStrength(int x, int y, int r, int s) {
x = width - x;
int rippleMapSize = loadI32(RSID_STATE, RSID_RIPPLE_MAP_SIZE);
int index = loadI32(RSID_STATE, RSID_RIPPLE_INDEX);
int rippleMapSize = State_rippleMapSize;
int index = State_rippleIndex;
int origin = offset(0, 0, width);
int* current = loadArrayI32(RSID_RIPPLE_MAP, index * rippleMapSize + origin);
@@ -108,16 +93,16 @@ void drop(int x, int y, int r) {
}
void updateRipples() {
int rippleMapSize = loadI32(RSID_STATE, RSID_RIPPLE_MAP_SIZE);
int width = loadI32(RSID_STATE, RSID_MESH_WIDTH);
int height = loadI32(RSID_STATE, RSID_MESH_HEIGHT);
int index = loadI32(RSID_STATE, RSID_RIPPLE_INDEX);
int rippleMapSize = State_rippleMapSize;
int width = State_meshWidth;
int height = State_meshHeight;
int index = State_rippleIndex;
int origin = offset(0, 0, width);
int* current = loadArrayI32(RSID_RIPPLE_MAP, index * rippleMapSize + origin);
int* next = loadArrayI32(RSID_RIPPLE_MAP, (1 - index) * rippleMapSize + origin);
storeI32(RSID_STATE, RSID_RIPPLE_INDEX, 1 - index);
storeI32(RSID_STATE, OFFSETOF_WorldState_rippleIndex, 1 - index);
int a = 1;
int b = width + 2;
@@ -152,17 +137,17 @@ int refraction(int d, int wave, int *map) {
}
void generateRipples() {
int rippleMapSize = loadI32(RSID_STATE, RSID_RIPPLE_MAP_SIZE);
int width = loadI32(RSID_STATE, RSID_MESH_WIDTH);
int height = loadI32(RSID_STATE, RSID_MESH_HEIGHT);
int index = loadI32(RSID_STATE, RSID_RIPPLE_INDEX);
int rippleMapSize = loadI32(RSID_STATE, OFFSETOF_WorldState_rippleMapSize);
int width = State_meshWidth;
int height = State_meshHeight;
int index = State_rippleIndex;
int origin = offset(0, 0, width);
int b = width + 2;
int* current = loadArrayI32(RSID_RIPPLE_MAP, index * rippleMapSize + origin);
int *map = loadArrayI32(RSID_REFRACTION_MAP, 0);
float *vertices = loadTriangleMeshVerticesF(NAMED_mesh);
float *vertices = loadTriangleMeshVerticesF(NAMED_WaterMesh);
int h = height - 1;
while (h >= 0) {
@@ -305,7 +290,7 @@ float averageZ(float x1, float x2, float y1, float y2, float* vertices,
return 55.0f * z / vertexCount;
}
void drawLeaf(int index, int frameCount, float* vertices, int meshWidth, int meshHeight,
void drawLeaf(int index, float* vertices, int meshWidth, int meshHeight,
float glWidth, float glHeight) {
float *leafStruct = loadArrayF(RSID_LEAVES, index);
@@ -368,9 +353,9 @@ void drawLeaf(int index, int frameCount, float* vertices, int meshWidth, int mes
leafStruct[LEAF_STRUCT_SPIN] = spin;
leafStruct[LEAF_STRUCT_RIPPLED] = 1.0f;
} else {
dropWithStrength(((x + glWidth / 2.0f) / glWidth) * meshWidth,
meshHeight - ((y + glHeight / 2.0f) / glHeight) * meshHeight,
2, 5);
// dropWithStrength(((x + glWidth / 2.0f) / glWidth) * meshWidth,
// meshHeight - ((y + glHeight / 2.0f) / glHeight) * meshHeight,
// 2, 5);
}
leafStruct[LEAF_STRUCT_X] = x + leafStruct[LEAF_STRUCT_DELTAX];
leafStruct[LEAF_STRUCT_Y] = y + leafStruct[LEAF_STRUCT_DELTAY];
@@ -398,23 +383,24 @@ void drawLeaf(int index, int frameCount, float* vertices, int meshWidth, int mes
}
}
void drawLeaves(int frameCount) {
void drawLeaves() {
bindProgramFragment(NAMED_PFBackground);
bindProgramFragmentStore(NAMED_PFSLeaf);
bindProgramVertex(NAMED_PVSky);
bindTexture(NAMED_PFBackground, 0, NAMED_TLeaves);
int leavesCount = loadI32(RSID_STATE, RSID_LEAVES_COUNT);
int leavesCount = State_leavesCount;
int count = leavesCount * LEAF_STRUCT_FIELDS_COUNT;
int width = loadI32(RSID_STATE, RSID_MESH_WIDTH);
int height = loadI32(RSID_STATE, RSID_MESH_HEIGHT);
float glWidth = loadF(RSID_GL_STATE, RSID_GL_WIDTH);
float glHeight = loadF(RSID_GL_STATE, RSID_GL_HEIGHT);
int width = State_meshWidth;
int height = State_meshHeight;
float glWidth = State_glWidth;
float glHeight = State_glHeight;
float *vertices = loadTriangleMeshVerticesF(NAMED_mesh);
float *vertices = loadTriangleMeshVerticesF(NAMED_WaterMesh);
int i = 0;
for ( ; i < count; i += LEAF_STRUCT_FIELDS_COUNT) {
drawLeaf(i, frameCount, vertices, width, height, glWidth, glHeight);
drawLeaf(i, vertices, width, height, glWidth, glHeight);
}
float matrix[16];
@@ -422,47 +408,69 @@ void drawLeaves(int frameCount) {
vpLoadModelMatrix(matrix);
}
int main(int index) {
int frameCount = loadI32(RSID_STATE, RSID_FRAME_COUNT);
int dropX = loadI32(RSID_STATE, RSID_DROP_X);
if (dropX != -1) {
int dropY = loadI32(RSID_STATE, RSID_DROP_Y);
drop(dropX, dropY, DROP_RADIUS);
storeI32(RSID_STATE, RSID_DROP_X, -1);
storeI32(RSID_STATE, RSID_DROP_Y, -1);
}
int isRunning = loadI32(RSID_STATE, RSID_RUNNING);
if (isRunning) {
updateRipples();
generateRipples();
updateTriangleMesh(NAMED_mesh);
}
void drawRiverbed() {
bindTexture(NAMED_PFBackground, 0, NAMED_TRiverbed);
drawTriangleMesh(NAMED_mesh);
color(1.0f, 1.0f, 1.0f, 0.7f);
drawTriangleMesh(NAMED_WaterMesh);
}
void drawSky() {
color(1.0f, 1.0f, 1.0f, 0.8f);
bindProgramFragment(NAMED_PFSky);
bindProgramFragmentStore(NAMED_PFSLeaf);
bindTexture(NAMED_PFSky, 0, NAMED_TSky);
drawTriangleMesh(NAMED_mesh);
float x = State_skyOffsetX + State_skySpeedX;
float y = State_skyOffsetY + State_skySpeedY;
if (x > 1.0f) x = 0.0f;
if (x < -1.0f) x = 0.0f;
if (y > 1.0f) y = 0.0f;
storeF(RSID_STATE, OFFSETOF_WorldState_skyOffsetX, x);
storeF(RSID_STATE, OFFSETOF_WorldState_skyOffsetY, y);
float matrix[16];
matrixLoadTranslate(matrix, x, y, 0.0f);
vpLoadTextureMatrix(matrix);
drawTriangleMesh(NAMED_WaterMesh);
matrixLoadIdentity(matrix);
vpLoadTextureMatrix(matrix);
}
void drawLighting() {
ambient(0.0f, 0.0f, 0.0f, 1.0f);
diffuse(0.0f, 0.0f, 0.0f, 1.0f);
specular(0.44f, 0.44f, 0.44f, 1.0f);
shininess(40.0f);
bindProgramFragmentStore(NAMED_PFSBackground);
bindProgramFragment(NAMED_PFLighting);
bindProgramVertex(NAMED_PVLight);
drawTriangleMesh(NAMED_mesh);
bindProgramVertex(NAMED_PVSky);
drawLeaves(frameCount);
drawTriangleMesh(NAMED_WaterMesh);
}
frameCount++;
storeI32(RSID_STATE, RSID_FRAME_COUNT, frameCount);
int main(int index) {
int dropX = Drop_dropX;
if (dropX != -1) {
int dropY = Drop_dropY;
drop(dropX, dropY, DROP_RADIUS);
storeI32(RSID_DROP, OFFSETOF_DropState_dropX, -1);
storeI32(RSID_DROP, OFFSETOF_DropState_dropY, -1);
}
updateRipples();
generateRipples();
updateTriangleMesh(NAMED_WaterMesh);
drawRiverbed();
drawSky();
drawLighting();
drawLeaves();
return 1;
}

View File

@@ -26,6 +26,7 @@ import android.renderscript.Allocation;
import android.renderscript.Sampler;
import android.renderscript.Element;
import android.renderscript.Light;
import android.renderscript.Type;
import static android.renderscript.Sampler.Value.LINEAR;
import static android.renderscript.Sampler.Value.WRAP;
import static android.renderscript.ProgramStore.DepthFunc.*;
@@ -43,18 +44,7 @@ class FallRS {
private static final int MESH_RESOLUTION = 48;
private static final int RSID_STATE = 0;
private static final int RSID_STATE_FRAMECOUNT = 0;
private static final int RSID_STATE_WIDTH = 1;
private static final int RSID_STATE_HEIGHT = 2;
private static final int RSID_STATE_MESH_WIDTH = 3;
private static final int RSID_STATE_MESH_HEIGHT = 4;
private static final int RSID_STATE_RIPPLE_MAP_SIZE = 5;
private static final int RSID_STATE_RIPPLE_INDEX = 6;
private static final int RSID_STATE_DROP_X = 7;
private static final int RSID_STATE_DROP_Y = 8;
private static final int RSID_STATE_RUNNING = 9;
private static final int RSID_STATE_LEAVES_COUNT = 10;
private static final int TEXTURES_COUNT = 3;
private static final int LEAVES_TEXTURES_COUNT = 4;
private static final int RSID_TEXTURE_RIVERBED = 0;
@@ -80,12 +70,8 @@ class FallRS {
private static final int LEAF_STRUCT_DELTAX = 9;
private static final int LEAF_STRUCT_DELTAY = 10;
private static final int RSID_GL_STATE = 4;
private static final int RSID_STATE_GL_WIDTH = 0;
private static final int RSID_STATE_GL_HEIGHT = 1;
private boolean mIsRunning = true;
private static final int RSID_DROP = 4;
private Resources mResources;
private RenderScript mRS;
@@ -94,34 +80,35 @@ class FallRS {
private final int mWidth;
private final int mHeight;
private ScriptC mScript;
private Sampler mSampler;
@SuppressWarnings({"FieldCanBeLocal"})
private ProgramFragment mPfBackground;
@SuppressWarnings({"FieldCanBeLocal"})
private ProgramFragment mPfLighting;
@SuppressWarnings({"FieldCanBeLocal"})
private ProgramFragment mPfSky;
@SuppressWarnings({"FieldCanBeLocal"})
private ProgramStore mPfsBackground;
@SuppressWarnings({"FieldCanBeLocal"})
private ProgramStore mPfsLeaf;
@SuppressWarnings({"FieldCanBeLocal"})
private ProgramVertex mPvLight;
@SuppressWarnings({"FieldCanBeLocal"})
private ProgramVertex mPvSky;
private ProgramVertex.MatrixAllocation mPvOrthoAlloc;
private Light mLight;
private Allocation[] mTextures;
private Allocation mState;
private RenderScript.TriangleMesh mMesh;
private Allocation mDropState;
private DropState mDrop;
private Type mStateType;
private Type mDropType;
private int mMeshWidth;
private int mMeshHeight;
private int mMeshHeight;
private Allocation mRippleMap;
private Allocation mRefractionMap;
private Allocation mLeaves;
private Allocation mGlState;
private float mGlHeight;
private final int[] mIntData2 = new int[2];
public FallRS(int width, int height) {
mWidth = width;
mHeight = height;
@@ -135,38 +122,6 @@ class FallRS {
initRS();
}
public void destroy() {
mScript.destroy();
mSampler.destroy();
mPfBackground.destroy();
mPfsBackground.destroy();
mPvLight.destroy();
mPvOrthoAlloc.mAlloc.destroy();
for (Allocation a : mTextures) {
a.destroy();
}
mState.destroy();
mMesh.destroy();
mLight.destroy();
mRippleMap.destroy();
mRefractionMap.destroy();
mPvSky.destroy();
mPfLighting.destroy();
mLeaves.destroy();
mPfsLeaf.destroy();
mPfSky.destroy();
mGlState.destroy();
}
@Override
protected void finalize() throws Throwable {
try {
destroy();
} finally {
super.finalize();
}
}
private void initRS() {
createProgramVertex();
createProgramFragmentStore();
@@ -176,19 +131,22 @@ class FallRS {
loadTextures();
ScriptC.Builder sb = new ScriptC.Builder(mRS);
sb.setType(mStateType, "State", RSID_STATE);
sb.setType(mDropType, "Drop", RSID_DROP);
sb.setScript(mResources, R.raw.fall);
sb.setRoot(true);
mScript = sb.create();
mScript.setClearColor(0.0f, 0.0f, 0.0f, 1.0f);
mScript.setTimeZone(TimeZone.getDefault().getID());
mScript.bindAllocation(mState, RSID_STATE);
mScript.bindAllocation(mRippleMap, RSID_RIPPLE_MAP);
mScript.bindAllocation(mRefractionMap, RSID_REFRACTION_MAP);
mScript.bindAllocation(mLeaves, RSID_LEAVES);
mScript.bindAllocation(mGlState, RSID_GL_STATE);
ScriptC script = sb.create();
script.setClearColor(0.0f, 0.0f, 0.0f, 1.0f);
script.setTimeZone(TimeZone.getDefault().getID());
mRS.contextBindRootScript(mScript);
script.bindAllocation(mState, RSID_STATE);
script.bindAllocation(mRippleMap, RSID_RIPPLE_MAP);
script.bindAllocation(mRefractionMap, RSID_REFRACTION_MAP);
script.bindAllocation(mLeaves, RSID_LEAVES);
script.bindAllocation(mDropState, RSID_DROP);
mRS.contextBindRootScript(script);
}
private void createMesh() {
@@ -238,8 +196,8 @@ class FallRS {
}
}
mMesh = rs.triangleMeshCreate();
mMesh.setName("mesh");
RenderScript.TriangleMesh mesh = rs.triangleMeshCreate();
mesh.setName("WaterMesh");
mMeshWidth = wResolution + 1;
mMeshHeight = hResolution + 1;
@@ -249,7 +207,6 @@ class FallRS {
final int rippleMapSize = (mMeshWidth + 2) * (mMeshHeight + 2);
createState(rippleMapSize);
createGlState();
createRippleMap(rippleMapSize);
createRefractionMap();
createLeaves();
@@ -281,29 +238,53 @@ class FallRS {
mRippleMap.data(rippleMap);
}
private void createGlState() {
final float[] meshState = new float[2];
mGlState = Allocation.createSized(mRS, USER_FLOAT, meshState.length);
meshState[RSID_STATE_GL_WIDTH] = 2.0f;
meshState[RSID_STATE_GL_HEIGHT] = mGlHeight;
mGlState.data(meshState);
static class WorldState {
public int frameCount;
public int width;
public int height;
public int meshWidth;
public int meshHeight;
public int rippleMapSize;
public int rippleIndex;
public int leavesCount;
public float glWidth;
public float glHeight;
public float skyOffsetX;
public float skyOffsetY;
public float skySpeedX;
public float skySpeedY;
}
static class DropState {
public int dropX;
public int dropY;
}
private void createState(int rippleMapSize) {
final int[] data = new int[11];
mState = Allocation.createSized(mRS, USER_I32, data.length);
data[RSID_STATE_FRAMECOUNT] = 0;
data[RSID_STATE_WIDTH] = mWidth;
data[RSID_STATE_HEIGHT] = mHeight;
data[RSID_STATE_MESH_WIDTH] = mMeshWidth;
data[RSID_STATE_MESH_HEIGHT] = mMeshHeight;
data[RSID_STATE_RIPPLE_MAP_SIZE] = rippleMapSize;
data[RSID_STATE_RIPPLE_INDEX] = 0;
data[RSID_STATE_DROP_X] = -1;
data[RSID_STATE_DROP_Y] = -1;
data[RSID_STATE_RUNNING] = 1;
data[RSID_STATE_LEAVES_COUNT] = LEAVES_COUNT;
mState.data(data);
WorldState worldState = new WorldState();
worldState.width = mWidth;
worldState.height = mHeight;
worldState.meshWidth = mMeshWidth;
worldState.meshHeight = mMeshHeight;
worldState.rippleMapSize = rippleMapSize;
worldState.rippleIndex = 0;
worldState.leavesCount = LEAVES_COUNT;
worldState.glWidth = 2.0f;
worldState.glHeight = mGlHeight;
worldState.skySpeedX = random(-0.001f, 0.001f);
worldState.skySpeedY = random(0.00008f, 0.0002f);
mStateType = Type.createFromClass(mRS, WorldState.class, 1, "WorldState");
mState = Allocation.createTyped(mRS, mStateType);
mState.data(worldState);
mDrop = new DropState();
mDrop.dropX = -1;
mDrop.dropY = -1;
mDropType = Type.createFromClass(mRS, DropState.class, 1, "DropState");
mDropState = Allocation.createTyped(mRS, mDropType);
mDropState.data(mDrop);
}
private void createLeaf(float[] leaves, int index) {
@@ -323,9 +304,7 @@ class FallRS {
}
private void loadTextures() {
mTextures = new Allocation[TEXTURES_COUNT];
final Allocation[] textures = mTextures;
final Allocation[] textures = new Allocation[TEXTURES_COUNT];
textures[RSID_TEXTURE_RIVERBED] = loadTexture(R.drawable.riverbed, "TRiverbed");
textures[RSID_TEXTURE_LEAVES] = loadTextureARGB(R.drawable.leaves, "TLeaves");
textures[RSID_TEXTURE_SKY] = loadTextureARGB(R.drawable.sky, "TSky");
@@ -357,27 +336,27 @@ class FallRS {
sampleBuilder.setMag(LINEAR);
sampleBuilder.setWrapS(WRAP);
sampleBuilder.setWrapT(WRAP);
mSampler = sampleBuilder.create();
Sampler sampler = sampleBuilder.create();
ProgramFragment.Builder builder = new ProgramFragment.Builder(mRS, null, null);
builder.setTexEnable(true, 0);
builder.setTexEnvMode(REPLACE, 0);
mPfBackground = builder.create();
mPfBackground.setName("PFBackground");
mPfBackground.bindSampler(mSampler, 0);
mPfBackground.bindSampler(sampler, 0);
builder = new ProgramFragment.Builder(mRS, null, null);
builder.setTexEnable(false, 0);
mPfLighting = builder.create();
mPfLighting.setName("PFLighting");
mPfLighting.bindSampler(mSampler, 0);
mPfLighting.bindSampler(sampler, 0);
builder = new ProgramFragment.Builder(mRS, null, null);
builder.setTexEnable(true, 0);
builder.setTexEnvMode(MODULATE, 0);
mPfSky = builder.create();
mPfSky.setName("PFSky");
mPfSky.bindSampler(mSampler, 0);
mPfSky.bindSampler(sampler, 0);
}
private void createProgramFragmentStore() {
@@ -399,33 +378,28 @@ class FallRS {
}
private void createProgramVertex() {
mPvOrthoAlloc = new ProgramVertex.MatrixAllocation(mRS);
mPvOrthoAlloc.setupProjectionNormalized(mWidth, mHeight);
ProgramVertex.MatrixAllocation pvOrthoAlloc = new ProgramVertex.MatrixAllocation(mRS);
pvOrthoAlloc.setupProjectionNormalized(mWidth, mHeight);
mLight = new Light.Builder(mRS).create();
mLight.setPosition(0.0f, 2.0f, -8.0f);
Light light = new Light.Builder(mRS).create();
light.setPosition(0.0f, 2.0f, -8.0f);
ProgramVertex.Builder builder = new ProgramVertex.Builder(mRS, null, null);
builder.setTextureMatrixEnable(true);
builder.addLight(mLight);
builder.addLight(light);
mPvLight = builder.create();
mPvLight.bindAllocation(mPvOrthoAlloc);
mPvLight.bindAllocation(pvOrthoAlloc);
mPvLight.setName("PVLight");
builder = new ProgramVertex.Builder(mRS, null, null);
builder.setTextureMatrixEnable(true);
mPvSky = builder.create();
mPvSky.bindAllocation(mPvOrthoAlloc);
mPvSky.bindAllocation(pvOrthoAlloc);
mPvSky.setName("PVSky");
}
void addDrop(float x, float y) {
mIntData2[0] = (int) ((x / mWidth) * mMeshWidth);
mIntData2[1] = (int) ((y / mHeight) * mMeshHeight);
mState.subData1D(RSID_STATE_DROP_X, 2, mIntData2);
}
void togglePause() {
mIsRunning = !mIsRunning;
mState.subData1D(RSID_STATE_RUNNING, 1, new int[] { mIsRunning ? 1 : 0 });
mDrop.dropX = (int) ((x / mWidth) * mMeshWidth);
mDrop.dropY = (int) ((y / mHeight) * mMeshHeight);
mDropState.data(mDrop);
}
}

View File

@@ -41,20 +41,6 @@ class FallView extends RSSurfaceView {
mRender.init(RS, getResources());
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
if (mRender != null) mRender.destroy();
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_DPAD_CENTER ||
keyCode == KeyEvent.KEYCODE_MENU) {
mRender.togglePause();
}
return super.onKeyDown(keyCode, event);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()) {