Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 50 additions & 17 deletions examples/18_particles/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,10 @@ int main()
rdpq_init();
//rdpq_debug_start();

uint64_t rspTimeTPX = 0;
uint64_t rdpTimeBusy = 0;
#if RSPQ_PROFILE
rspq_profile_data_t profile_data = (rspq_profile_data_t){};
uint64_t rdpTimeBusy = 0;
uint64_t rspTimeTPX = 0;
rspq_profile_start();
#endif

Expand All @@ -65,13 +65,20 @@ int main()
// Meaning you only have to allocate an buffer of arbitrary size here and fill it with data.
uint32_t particleCountMax = 100'000;
uint32_t particleCount = 2000;

// NOTE: just like with vertices, particles are interleaved in pairs of 2.
// So one TPXParticle struct always contains 2 particles.
// So one TPXParticleS8 struct always contains 2 particles.
// If you need an odd number, just set the second particle size to 0.
uint32_t allocSize = sizeof(TPXParticle) * particleCountMax / 2;
TPXParticle *particles = malloc_uncached(allocSize);
uint32_t allocSize = sizeof(TPXParticleS8) * particleCountMax / 2;
TPXParticleS8 *particlesS8 = malloc_uncached(allocSize);
debugf("Particle-Buffer %ldkb\n", allocSize / 1024);
generate_particles_random(particles, particleCount);

// Additionally, a 16bit version of particles is available.
// This one takes up more space (24 bytes vs 16 bytes per pair) and is slightly slower.
// In return, it can cover a larger range which can be useful for 3D sprites placed in a scene.
// The 8bit variant should be preferred when possible (e.g. in local particle effects)
allocSize = sizeof(TPXParticleS16) * particleCountMax / 2;
TPXParticleS16 *particlesS16 = malloc_uncached(allocSize);

// Now some regular 3D stuff, not related to particles.
T3DModel *model = t3d_model_load("rom://scene.t3dm");
Expand Down Expand Up @@ -111,6 +118,7 @@ int main()
float time = 0;
bool needRebuild = true;
int frameIdx = 0;
bool measureTime = false;

for(;;)
{
Expand All @@ -132,6 +140,10 @@ int main()
if(joypad.btn.c_up)partSizeY += deltaTime * 0.6f;
if(joypad.btn.c_down)partSizeY -= deltaTime * 0.6f;

#if RSPQ_PROFILE
measureTime = joypad.btn.z;
#endif

partSizeX = fmaxf(0.01f, fminf(1.0f, partSizeX));
partSizeY = fmaxf(0.01f, fminf(1.0f, partSizeY));

Expand Down Expand Up @@ -173,42 +185,44 @@ int main()

// A few example particles systems.
// This will modify the particle buffer on the CPU side.
bool isS16 = false;
switch(example)
{
case 0: // Random
time += deltaTime * 0.2f;
particleRot = (T3DVec3){{time,time*0.77f,time*1.42f}};
particleMatScale = (T3DVec3){{partMatScaleVal, partMatScaleVal, partMatScaleVal}};

if(needRebuild)generate_particles_random(particles, particleCount);
if(needRebuild)generate_particles_random(particlesS8, particleCount);
rdpq_set_env_color((color_t){0xFF, 0xFF, 0xFF, 0xFF});
break;
break;
case 1: // Flame
particleRot = (T3DVec3){{0,0,0}};
if(!joypad.btn.z)time += deltaTime * 1.0f;
particleCount = 128;
float posX = fm_cosf(time) * 80.0f;
float posZ = fm_sinf(2*time) * 40.0f;

simulate_particles_fire(particles, particleCount, posX, posZ);
simulate_particles_fire(particlesS8, particleCount, posX, posZ);
particleMatScale = (T3DVec3){{0.9f, partMatScaleVal, 0.9f}};
particlePos.y = partMatScaleVal * 130.0f;
rdpq_set_env_color((color_t){0xFF, 0xFF, 0xFF, 0xFF});
break;
break;
case 2: // Grass
time += deltaTime * 1.0f;
particleRot = (T3DVec3){{0,0,0}};
particlePos.y = 0;
if(needRebuild) {
particleCount = simulate_particles_grass(particles, particleCount);
particleCount = simulate_particles_grass(particlesS16, particleCount);
}
particleMatScale = (T3DVec3){{partMatScaleVal, partSizeY * 2.9f, partMatScaleVal}};
rdpq_set_env_color(blend_colors(
(color_t){0xAA, 0xFF, 0x55, 0xFF},
(color_t){0xFF, 0xAA, 0x55, 0xFF},
fm_sinf(time)*0.5f+0.5f
));
break;
isS16 = true;
break;
}
needRebuild = false;

Expand Down Expand Up @@ -267,10 +281,29 @@ int main()
// This can only scale particles down, so the range is 0.0 - 1.0.
tpx_state_set_scale(partSizeX, partSizeY);

if(measureTime) {
rspq_wait();
rspq_highpri_begin();
wait_ms(2);
rspTimeTPX = get_ticks();
}

// Now draw particles. internally this will load, transform and draw them in one go on the RSP.
// While the ucode can only handle a 344 at a time, this function will automatically batch them
// so you can specify an arbitrary amount of particles (as long as it's an even count)
tpx_particle_draw(particles, particleCount);
if(isS16) {
tpx_particle_draw_s16(particlesS16, particleCount);
} else {
tpx_particle_draw_s8(particlesS8, particleCount);
}

if(measureTime)
{
rspq_highpri_end();
rspq_highpri_sync();
rspTimeTPX = get_ticks() - rspTimeTPX;
rspTimeTPX = TICKS_TO_US(rspTimeTPX);
}

// Make sure end up at the same stack level as before.
tpx_matrix_pop(1);
Expand All @@ -283,18 +316,18 @@ int main()
t3d_debug_printf(20, 30, "[C] %.2f %.2f", partSizeX, partSizeY);
t3d_debug_printf(220, 18, "FPS: %.2f", display_get_fps());

#if RSPQ_PROFILE
if(measureTime)
{
double timePerPart = 0;
if(particleCount > 0) {
timePerPart = (double)rspTimeTPX / (double)particleCount * 1000;
}
t3d_debug_printf(20, 240-34, "RSP/tpx: %6lldus %.1f", rspTimeTPX, timePerPart);
//t3d_debug_printf(20, 240-34, "RSP/tpx: %6lldus", rspTimeTPX);
t3d_debug_printf(20, 240-24, "RDP : %6lldus", rdpTimeBusy);
#else
} else {
t3d_debug_printf(20, 240-24, "[L/R]: %s", EXAMPLE_NAMES[example]);
#endif

}
rdpq_detach_show();

#if RSPQ_PROFILE
Expand Down
16 changes: 8 additions & 8 deletions examples/18_particles/partSim.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ static int currentPart = 0;
/**
* Basic static particles with random positions and colors.
*/
static void generate_particles_random(TPXParticle *particles, uint32_t count) {
static void generate_particles_random(TPXParticleS8 *particles, uint32_t count) {
for (int i = 0; i < count; i++) {
int p = i / 2;
int8_t *ptPos = i % 2 == 0 ? particles[p].posA : particles[p].posB;
Expand Down Expand Up @@ -47,7 +47,7 @@ static int noise_2d(int x, int y) {
* Static particles simulating grass.
* This will create a random grid of 3 particles stacked on top of each other representing grass-blades.
*/
static int simulate_particles_grass(TPXParticle *particles, uint32_t partCount) {
static int simulate_particles_grass(TPXParticleS16 *particles, uint32_t partCount) {

int dist = 3;
int heightParts = 3;
Expand All @@ -56,14 +56,14 @@ static int simulate_particles_grass(TPXParticle *particles, uint32_t partCount)
int p = 0;
for(int y=heightParts-1; y>=0; --y)
{
int8_t ptPosX = -(dist * sideLen) / 2;
int16_t ptPosX = -(dist * sideLen) / 2;
for(int x=0; x<sideLen; ++x)
{
int8_t ptPosZ = -(dist * sideLen) / 2;
int16_t ptPosZ = -(dist * sideLen) / 2;
for(int z=0; z<sideLen; ++z)
{
int8_t *ptPos = tpx_buffer_get_pos(particles, p);
uint8_t *ptColor = tpx_buffer_get_rgba(particles, p);
int16_t *ptPos = tpx_buffer_s16_get_pos(particles, p);
uint8_t *ptColor = tpx_buffer_s16_get_rgba(particles, p);

int rnd = noise_2d(x, z);
float height = fm_sinf((x + z) * 0.1f) * 0.5f + 0.5f;
Expand All @@ -75,7 +75,7 @@ static int simulate_particles_grass(TPXParticle *particles, uint32_t partCount)
ptPos[0] = ptPosX + ((rnd % 3) - 1);
ptPos[1] = y + height;
ptPos[2] = ptPosZ + ((rnd % 3) - 1);
*tpx_buffer_get_size(particles, p) = size;
*tpx_buffer_s16_get_size(particles, p) = size;

ptPosZ += dist;

Expand Down Expand Up @@ -119,7 +119,7 @@ static void gradient_fire(uint8_t *color, float t) {
* This will simulate particles over time by moving them up and changing their color.
* The current position is used to spawn new particles, so it can move over time leaving a trail behind.
*/
static void simulate_particles_fire(TPXParticle *particles, uint32_t partCount, float posX, float posZ) {
static void simulate_particles_fire(TPXParticleS8 *particles, uint32_t partCount, float posX, float posZ) {
uint32_t p = currentPart / 2;
if(currentPart % (1+(rand() % 3)) == 0) {
int8_t *ptPos = currentPart % 2 == 0 ? particles[p].posA : particles[p].posB;
Expand Down
56 changes: 45 additions & 11 deletions examples/19_particles_tex/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,10 @@ int main()
rdpq_init();
//rdpq_debug_start();

uint64_t rdpTimeBusy = 0;
uint64_t rspTimeTPX = 0;
#if RSPQ_PROFILE
rspq_profile_data_t profile_data = (rspq_profile_data_t){};
uint64_t rdpTimeBusy = 0;
uint64_t rspTimeTPX = 0;
rspq_profile_start();
#endif

Expand All @@ -87,10 +87,17 @@ int main()
// There is no special struct for textured particles compared to colored ones.
// The only difference is that the alpha channel of the color is used for the texture offset.
// You can still define a global alpha value via the CC ofc.
uint32_t allocSize = sizeof(TPXParticle) * particleCountMax / 2;
TPXParticle *particles = malloc_uncached(allocSize);
uint32_t allocSize = sizeof(TPXParticleS8) * particleCountMax / 2;
TPXParticleS8 *particlesS8 = malloc_uncached(allocSize);
debugf("Particle-Buffer %ldkb\n", allocSize / 1024);

// Additionally, a 16bit version of particles is available.
// This one takes up more space (24 bytes vs 16 bytes per pair) and is slightly slower.
// In return, it can cover a larger range which can be useful for 3D sprites placed in a scene.
// The 8bit variant should be preferred when possible (e.g. in local particle effects)
allocSize = sizeof(TPXParticleS16) * particleCountMax / 2;
TPXParticleS16 *particlesS16 = malloc_uncached(allocSize);

sprite_t *texTest[] = {
sprite_load("rom://tex8.i8.sprite"),
sprite_load("rom://tex16.i8.sprite"),
Expand Down Expand Up @@ -137,6 +144,7 @@ int main()
float time = 0;
float timeTile = 0;
bool needRebuild = true;
bool measureTime = false;
int frameIdx = 0;

for(;;)
Expand All @@ -160,6 +168,10 @@ int main()
if(joypad.btn.c_up)partSizeY += deltaTime * 0.6f;
if(joypad.btn.c_down)partSizeY -= deltaTime * 0.6f;

#if RSPQ_PROFILE
measureTime = joypad.btn.z;
#endif

partSizeX = fmaxf(0.01f, fminf(1.0f, partSizeX));
partSizeY = fmaxf(0.01f, fminf(1.0f, partSizeY));

Expand Down Expand Up @@ -200,6 +212,7 @@ int main()
camTarget.v[2] = camPos.v[2] + camDir.v[2];
}

bool is16Bit = false;
bool isSpriteRot = false;
switch(example)
{
Expand All @@ -211,7 +224,7 @@ int main()
float posX = fm_cosf(time) * 80.0f;
float posZ = fm_sinf(2*time) * 40.0f;

simulate_particles_fire(particles, particleCount, posX, posZ);
simulate_particles_fire(particlesS8, particleCount, posX, posZ);
particleMatScale = (T3DVec3){{0.9f, partMatScaleVal, 0.9f}};
particlePos.y = partMatScaleVal * 130.0f;
rdpq_set_env_color((color_t){0xFF, 0xFF, 0xFF, 0xFF});
Expand All @@ -223,18 +236,19 @@ int main()
particleRot = (T3DVec3){{0,0,0}};
particlePos.y = 0;
if(needRebuild) {
particleCount = simulate_particles_coins(particles, particleCount);
particleCount = simulate_particles_coins(particlesS16, particleCount);
}
particleMatScale = (T3DVec3){{partMatScaleVal, partSizeY * 2.9f, partMatScaleVal}};
rdpq_set_env_color((color_t){0xFF, 0xFF, 0xFF, 0xFF});
is16Bit = true;
break;
default: // Random
time += deltaTime * 0.2f;
timeTile += deltaTime * 0.1f;
particleRot = (T3DVec3){{time,time*0.77f,time*1.42f}};
particleMatScale = (T3DVec3){{partMatScaleVal, partMatScaleVal, partMatScaleVal}};

if(needRebuild)generate_particles_random(particles, particleCount);
if(needRebuild)generate_particles_random(particlesS8, particleCount);
rdpq_set_env_color((color_t){0xFF, 0xFF, 0xFF, 0xFF});
isSpriteRot = true;
break;
Expand Down Expand Up @@ -326,7 +340,26 @@ int main()
case 5: tpx_state_set_tex_params((int16_t)tileIdx, 0); break;
}

tpx_particle_draw_tex(particles, particleCount);
if(measureTime) {
rspq_wait();
rspq_highpri_begin();
wait_ms(2);
rspTimeTPX = get_ticks();
}

if(is16Bit) {
tpx_particle_draw_tex_s16(particlesS16, particleCount);
} else {
tpx_particle_draw_tex_s8(particlesS8, particleCount);
}

if(measureTime)
{
rspq_highpri_end();
rspq_highpri_sync();
rspTimeTPX = get_ticks() - rspTimeTPX;
rspTimeTPX = TICKS_TO_US(rspTimeTPX);
}

tpx_matrix_pop(1);

Expand All @@ -335,17 +368,18 @@ int main()
t3d_debug_printf(20, 30, "[C] %.2f %.2f", partSizeX, partSizeY);
t3d_debug_printf(220, 18, "FPS: %.2f", display_get_fps());

#if RSPQ_PROFILE
if(measureTime)
{
double timePerPart = 0;
if(particleCount > 0) {
timePerPart = (double)rspTimeTPX / (double)particleCount * 1000;
}
t3d_debug_printf(20, 240-34, "RSP/tpx: %6lldus %.1f", rspTimeTPX, timePerPart);
//t3d_debug_printf(20, 240-34, "RSP/tpx: %6lldus", rspTimeTPX);
t3d_debug_printf(20, 240-24, "RDP : %6lldus", rdpTimeBusy);
#else
} else {
t3d_debug_printf(20, 240-24, "[L/R]: %s", EXAMPLE_NAMES[example]);
#endif
}

rdpq_detach_show();

Expand Down
Loading
Loading