From 313b5b8b63a33fe50cf61d58bc74c49b294d1875 Mon Sep 17 00:00:00 2001 From: Romain Guy Date: Wed, 19 Aug 2009 17:16:17 -0700 Subject: [PATCH] Improved performance in Galaxy and Fall --- libs/rs/java/Fall/res/raw/fall.c | 47 +++++++++++++---- .../java/Fall/src/com/android/fall/rs/FallRS.java | 29 +++++++--- libs/rs/java/Galaxy/res/raw/galaxy.c | 61 +++++++++------------- .../Galaxy/src/com/android/galaxy/rs/GalaxyRS.java | 40 ++++---------- .../src/com/android/galaxy/rs/GalaxyView.java | 11 +--- 5 files changed, 97 insertions(+), 91 deletions(-) diff --git a/libs/rs/java/Fall/res/raw/fall.c b/libs/rs/java/Fall/res/raw/fall.c index a67764e4c7ee..4f8377f682aa 100644 --- a/libs/rs/java/Fall/res/raw/fall.c +++ b/libs/rs/java/Fall/res/raw/fall.c @@ -240,7 +240,7 @@ void generateRipples() { n2y = v3y - v1y; n2z = v3z - v1z; - // Avegare of previous normal and N1 x N2 + // Average of previous normal and N1 x N2 n3x = n3x / 2.0f + (n1y * n2z - n1z * n2y) / 2.0f; n3y = n3y / 2.0f + (n1z * n2x - n1x * n2z) / 2.0f; n3z = n3z / 2.0f + (n1x * n2y - n1y * n2x) / 2.0f; @@ -264,10 +264,10 @@ void generateRipples() { float averageZ(float x1, float x2, float y1, float y2, float* vertices, int meshWidth, int meshHeight, float glWidth, float glHeight) { - x1 = ((x1 + glWidth / 2.0f) / glWidth) * meshWidth; - x2 = ((x2 + glWidth / 2.0f) / glWidth) * meshWidth; - y1 = ((y1 + glHeight / 2.0f) / glHeight) * meshHeight; - y2 = ((y2 + glHeight / 2.0f) / glHeight) * meshHeight; + x1 = ((x1 + glWidth * 0.5f) / glWidth) * meshWidth; + x2 = ((x2 + glWidth * 0.5f) / glWidth) * meshWidth; + y1 = ((y1 + glHeight * 0.5f) / glHeight) * meshHeight; + y2 = ((y2 + glHeight * 0.5f) / glHeight) * meshHeight; int quadX1 = clamp(x1, 0, meshWidth); int quadX2 = clamp(x2, 0, meshWidth); @@ -319,10 +319,10 @@ void drawLeaf(int index, float* vertices, int meshWidth, int meshHeight, if (a > 0.0f) { tz = -a; } else { - z1 = averageZ(x1, x, y1, y, vertices, meshWidth, meshHeight, glWidth, glHeight); - z2 = averageZ(x, x2, y1, y, vertices, meshWidth, meshHeight, glWidth, glHeight); - z3 = averageZ(x, x2, y, y2, vertices, meshWidth, meshHeight, glWidth, glHeight); - z4 = averageZ(x1, x, y, y2, vertices, meshWidth, meshHeight, glWidth, glHeight); +// z1 = averageZ(x1, x, y1, y, vertices, meshWidth, meshHeight, glWidth, glHeight); +// z2 = averageZ(x, x2, y1, y, vertices, meshWidth, meshHeight, glWidth, glHeight); +// z3 = averageZ(x, x2, y, y2, vertices, meshWidth, meshHeight, glWidth, glHeight); +// z4 = averageZ(x1, x, y, y2, vertices, meshWidth, meshHeight, glWidth, glHeight); } x1 -= x; @@ -454,6 +454,34 @@ void drawLighting() { drawTriangleMesh(NAMED_WaterMesh); } +void drawNormals() { + int width = State_meshWidth; + int height = State_meshHeight; + + float *vertices = loadTriangleMeshVerticesF(NAMED_WaterMesh); + + bindProgramVertex(NAMED_PVSky); + bindProgramFragment(NAMED_PFLighting); + + color(1.0f, 0.0f, 0.0f, 1.0f); + + int y = 0; + for ( ; y < height; y++) { + int yOffset = y * width; + int x = 0; + for ( ; x < width; x++) { + int offset = (yOffset + x) * 8; + float vx = vertices[offset + 5]; + float vy = vertices[offset + 6]; + float vz = vertices[offset + 7]; + float nx = vertices[offset + 0]; + float ny = vertices[offset + 1]; + float nz = vertices[offset + 2]; + drawLine(vx, vy, vz, vx + nx / 10.0f, vy + ny / 10.0f, vz + nz / 10.0f); + } + } +} + int main(int index) { int dropX = Drop_dropX; if (dropX != -1) { @@ -471,6 +499,7 @@ int main(int index) { drawSky(); drawLighting(); drawLeaves(); + //drawNormals(); return 1; } diff --git a/libs/rs/java/Fall/src/com/android/fall/rs/FallRS.java b/libs/rs/java/Fall/src/com/android/fall/rs/FallRS.java index 02d47379453f..3b13bed579f3 100644 --- a/libs/rs/java/Fall/src/com/android/fall/rs/FallRS.java +++ b/libs/rs/java/Fall/src/com/android/fall/rs/FallRS.java @@ -177,22 +177,37 @@ class FallRS { hResolution += 2; for (int y = 0; y <= hResolution; y++) { + final boolean shift = (y & 0x1) == 0; final float yOffset = y * quadHeight - glHeight / 2.0f - quadHeight; final float t = 1.0f - y / (float) hResolution; for (int x = 0; x <= wResolution; x++) { - rs.triangleMeshAddVertex_XYZ_ST_NORM( - -1.0f + x * quadWidth - quadWidth, yOffset, 0.0f, - x / (float) wResolution, t, - 0.0f, 0.0f, -1.0f); + if (shift) { + rs.triangleMeshAddVertex_XYZ_ST_NORM( + -1.0f + x * quadWidth - quadWidth, yOffset, 0.0f, + x / (float) wResolution, t, + 0.0f, 0.0f, -1.0f); + } else { + rs.triangleMeshAddVertex_XYZ_ST_NORM( + -1.0f + x * quadWidth - quadWidth * 0.5f, yOffset, 0.0f, + x / (float) wResolution, t, + 0.0f, 0.0f, -1.0f); + } } } for (int y = 0; y < hResolution; y++) { + final boolean shift = (y & 0x1) == 0; + final int yOffset = y * (wResolution + 1); for (int x = 0; x < wResolution; x++) { - final int index = y * (wResolution + 1) + x; + final int index = yOffset + x; final int iWR1 = index + wResolution + 1; - rs.triangleMeshAddTriangle(index, index + 1, iWR1); - rs.triangleMeshAddTriangle(index + 1, iWR1, iWR1 + 1); + if (shift) { + rs.triangleMeshAddTriangle(index, index + 1, iWR1); + rs.triangleMeshAddTriangle(index + 1, iWR1 + 1, iWR1); + } else { + rs.triangleMeshAddTriangle(index, iWR1 + 1, iWR1); + rs.triangleMeshAddTriangle(index, index + 1, iWR1 + 1); + } } } diff --git a/libs/rs/java/Galaxy/res/raw/galaxy.c b/libs/rs/java/Galaxy/res/raw/galaxy.c index 59c31a1a0901..9ff449f225ef 100644 --- a/libs/rs/java/Galaxy/res/raw/galaxy.c +++ b/libs/rs/java/Galaxy/res/raw/galaxy.c @@ -19,11 +19,13 @@ #define RSID_PARTICLES 1 -#define PARTICLE_STRUCT_FIELDS_COUNT 4 +#define PARTICLE_STRUCT_FIELDS_COUNT 6 #define PARTICLE_STRUCT_ANGLE 0 #define PARTICLE_STRUCT_DISTANCE 1 #define PARTICLE_STRUCT_SPEED 2 #define PARTICLE_STRUCT_RADIUS 3 +#define PARTICLE_STRUCT_S 4 +#define PARTICLE_STRUCT_T 5 #define RSID_PARTICLES_BUFFER 2 #define PARTICLE_BUFFER_COMPONENTS_COUNT 5 @@ -31,19 +33,14 @@ #define PARTICLES_TEXTURES_COUNT 2 #define ELLIPSE_RATIO 0.892f -#define ELLIPSE_TWIST 0.02333333333f void drawSpace(int width, int height) { bindTexture(NAMED_PFBackground, 0, NAMED_TSpace); drawQuadTexCoords( - 0.0f, 0.0f, 0.0f, - 0.0f, 1.0f, - width, 0.0f, 0.0f, - 2.0f, 1.0f, - width, height, 0.0f, - 2.0f, 0.0f, - 0.0f, height, 0.0f, - 0.0f, 0.0f); + 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, + width, 0.0f, 0.0f, 2.0f, 1.0f, + width, height, 0.0f, 2.0f, 0.0f, + 0.0f, height, 0.0f, 0.0f, 0.0f); } void drawLights(int width, int height) { @@ -61,39 +58,34 @@ void drawLights(int width, int height) { x + 512.0f * 1.1f, y + 512.0f, 0.0f); } -void drawParticle(float *particle, int index, float *particleBuffer, int bufferIndex, - float w, float h) { - - float distance = particle[index + PARTICLE_STRUCT_DISTANCE]; - float angle = particle[index + PARTICLE_STRUCT_ANGLE]; - float speed = particle[index + PARTICLE_STRUCT_SPEED]; - float r = particle[index + PARTICLE_STRUCT_RADIUS]; +void drawParticle(float *particle, float *particleBuffer, float w, float h) { + float distance = particle[PARTICLE_STRUCT_DISTANCE]; + float angle = particle[PARTICLE_STRUCT_ANGLE]; + float speed = particle[PARTICLE_STRUCT_SPEED]; + float r = particle[PARTICLE_STRUCT_RADIUS]; float a = angle + speed; float x = distance * sinf_fast(a); float y = distance * cosf_fast(a) * ELLIPSE_RATIO; - float z = distance * ELLIPSE_TWIST; - float s = cosf_fast(z); - float t = sinf_fast(z); + float s = particle[PARTICLE_STRUCT_S]; + float t = particle[PARTICLE_STRUCT_T]; float sX = t * x + s * y + w; float sY = s * x - t * y + h; // lower left vertex of the particle's triangle - particleBuffer[bufferIndex + 1] = sX - r; // X - particleBuffer[bufferIndex + 2] = sY + r; // Y + particleBuffer[1] = sX - r; // X + particleBuffer[2] = sY + r; // Y // lower right vertex of the particle's triangle - bufferIndex += PARTICLE_BUFFER_COMPONENTS_COUNT; - particleBuffer[bufferIndex + 1] = sX + r; // X - particleBuffer[bufferIndex + 2] = sY + r; // Y + particleBuffer[6] = sX + r; // X + particleBuffer[7] = sY + r; // Y // upper middle vertex of the particle's triangle - bufferIndex += PARTICLE_BUFFER_COMPONENTS_COUNT; - particleBuffer[bufferIndex + 1] = sX; // X - particleBuffer[bufferIndex + 2] = sY - r; // Y + particleBuffer[11] = sX; // X + particleBuffer[12] = sY - r; // Y - particle[index + PARTICLE_STRUCT_ANGLE] = a; + particle[PARTICLE_STRUCT_ANGLE] = a; } void drawParticles(int width, int height) { @@ -103,7 +95,6 @@ void drawParticles(int width, int height) { int radius = State_galaxyRadius; int particlesCount = State_particlesCount; - int count = particlesCount * PARTICLE_STRUCT_FIELDS_COUNT; float *particle = loadArrayF(RSID_PARTICLES, 0); float *particleBuffer = loadArrayF(RSID_PARTICLES_BUFFER, 0); @@ -112,11 +103,11 @@ void drawParticles(int width, int height) { float h = height * 0.5f; int i = 0; - int bufferIndex = 0; - for ( ; i < count; i += PARTICLE_STRUCT_FIELDS_COUNT) { - drawParticle(particle, i, particleBuffer, bufferIndex, w, h); - // each particle is a triangle (3 vertices) of 6 properties (ABGR, X, Y, Z, S, T) - bufferIndex += 3 * PARTICLE_BUFFER_COMPONENTS_COUNT; + for ( ; i < particlesCount; i++) { + drawParticle(particle, particleBuffer, w, h); + particle += PARTICLE_STRUCT_FIELDS_COUNT; + // each particle is a triangle (3 vertices) of 5 properties (ABGR, X, Y, S, T) + particleBuffer += 3 * PARTICLE_BUFFER_COMPONENTS_COUNT; } uploadToBufferObject(NAMED_ParticlesBuffer); diff --git a/libs/rs/java/Galaxy/src/com/android/galaxy/rs/GalaxyRS.java b/libs/rs/java/Galaxy/src/com/android/galaxy/rs/GalaxyRS.java index 717100dc9707..c6f5816edf14 100644 --- a/libs/rs/java/Galaxy/src/com/android/galaxy/rs/GalaxyRS.java +++ b/libs/rs/java/Galaxy/src/com/android/galaxy/rs/GalaxyRS.java @@ -42,9 +42,11 @@ import static android.util.MathUtils.*; import java.util.TimeZone; +@SuppressWarnings({"FieldCanBeLocal"}) class GalaxyRS { private static final int GALAXY_RADIUS = 300; private static final int PARTICLES_COUNT = 12000; + private static final float ELLIPSE_TWIST = 0.023333333f; private static final int RSID_STATE = 0; @@ -54,11 +56,13 @@ class GalaxyRS { private static final int RSID_TEXTURE_FLARES = 2; private static final int RSID_PARTICLES = 1; - private static final int PARTICLE_STRUCT_FIELDS_COUNT = 4; + private static final int PARTICLE_STRUCT_FIELDS_COUNT = 6; private static final int PARTICLE_STRUCT_ANGLE = 0; private static final int PARTICLE_STRUCT_DISTANCE = 1; private static final int PARTICLE_STRUCT_SPEED = 2; private static final int PARTICLE_STRUCT_RADIUS = 3; + private static final int PARTICLE_STRUCT_S = 4; + private static final int PARTICLE_STRUCT_T = 5; private static final int RSID_PARTICLES_BUFFER = 2; @@ -103,35 +107,6 @@ class GalaxyRS { initRS(); } - public void destroy() { - mScript.destroy(); - mSampler.destroy(); - mLightSampler.destroy(); - mPfBackground.destroy(); - mPfsBackground.destroy(); - mPvBackground.destroy(); - mPvOrthoAlloc.mAlloc.destroy(); - for (Allocation a : mTextures) { - a.destroy(); - } - mState.destroy(); - mPfLighting.destroy(); - mParticles.destroy(); - mPfsLights.destroy(); - mParticlesMesh.destroy(); - mParticlesBuffer.destroy(); - mStateType.destroy(); - } - - @Override - protected void finalize() throws Throwable { - try { - destroy(); - } finally { - super.finalize(); - } - } - private void initRS() { createProgramVertex(); createProgramFragmentStore(); @@ -218,13 +193,16 @@ class GalaxyRS { float d = abs(randomGauss()) * GALAXY_RADIUS / 2.0f + random(-4.0f, 4.0f); float z = randomGauss() * 0.5f * 0.8f * ((GALAXY_RADIUS - d) / (float) GALAXY_RADIUS); z += 1.0f; + float p = d * ELLIPSE_TWIST; particles[index + PARTICLE_STRUCT_ANGLE] = random(0.0f, (float) (Math.PI * 2.0)); particles[index + PARTICLE_STRUCT_DISTANCE] = d; particles[index + PARTICLE_STRUCT_SPEED] = random(0.0015f, 0.0025f) * (0.5f + (0.5f * (float) GALAXY_RADIUS / d)) * 0.7f; particles[index + PARTICLE_STRUCT_RADIUS] = z * random(1.2f, 2.1f); - + particles[index + PARTICLE_STRUCT_S] = (float) Math.cos(p); + particles[index + PARTICLE_STRUCT_T] = (float) Math.sin(p); + int red, green, blue; if (d < GALAXY_RADIUS / 3.0f) { red = (int) (220 + (d / (float) GALAXY_RADIUS) * 35); diff --git a/libs/rs/java/Galaxy/src/com/android/galaxy/rs/GalaxyView.java b/libs/rs/java/Galaxy/src/com/android/galaxy/rs/GalaxyView.java index 341293bd9a7c..4f6d3f09e94c 100644 --- a/libs/rs/java/Galaxy/src/com/android/galaxy/rs/GalaxyView.java +++ b/libs/rs/java/Galaxy/src/com/android/galaxy/rs/GalaxyView.java @@ -22,8 +22,6 @@ import android.renderscript.RenderScript; import android.renderscript.RSSurfaceView; class GalaxyView extends RSSurfaceView { - private GalaxyRS mRender; - public GalaxyView(Context context) { super(context); setFocusable(true); @@ -34,12 +32,7 @@ class GalaxyView extends RSSurfaceView { super.surfaceChanged(holder, format, w, h); RenderScript RS = createRenderScript(); - mRender = new GalaxyRS(w, h); - mRender.init(RS, getResources()); - } - - @Override - public void surfaceDestroyed(SurfaceHolder holder) { - if (mRender != null) mRender.destroy(); + GalaxyRS render = new GalaxyRS(w, h); + render.init(RS, getResources()); } } -- 2.11.0