Compare commits

...

18 Commits

Author SHA1 Message Date
d018ba9234 Update README.md 2025-05-31 03:05:43 +01:00
78f782c5ea Add README.md 2025-05-31 03:03:48 +01:00
62a305451b Loop for set count instead of time 2025-05-31 03:01:14 +01:00
89a72ad7c1 Update .gitignore 2025-05-31 03:00:56 +01:00
231e14997b Remove Raylib compilation 2025-05-28 22:37:15 +01:00
Abdelrahman Said
4671e1c177 Test without Raylib 2025-05-28 22:35:21 +01:00
Abdelrahman Said
f8b859b4e8 Fix submodules 2025-05-27 15:22:52 +01:00
Abdelrahman Said
d94c7f8c63 Switch to using https for submodules 2025-05-27 15:20:50 +01:00
Abdelrahman Said
cdb02bd209 Enable rendering 2025-05-27 15:06:51 +01:00
cadba71330 Add threading 2025-05-24 03:50:31 +01:00
e6d39028fb Modify run_perf 2025-05-24 03:22:48 +01:00
a9d4b4eae1 Add count 2025-05-24 03:17:25 +01:00
61f66ae554 Fix entity id size to fit the entity count 2025-05-24 01:57:10 +01:00
a7d255dba6 Reformat 2025-05-24 01:37:23 +01:00
961afcd4f4 Persist multipliers 2025-05-24 01:35:37 +01:00
3696205f47 Remove old velocity code 2025-05-24 01:31:16 +01:00
c6cac7be8a Switch position, scale and velocity to i16 2025-05-24 00:19:23 +01:00
60df7c5fbb Use CMake on Linux and Make on macOS 2025-05-21 22:58:11 +01:00
7 changed files with 140 additions and 156 deletions

1
.gitignore vendored
View File

@@ -8,3 +8,4 @@ raylib-build
*.data
*.ods
*.dSYM
*.out

4
.gitmodules vendored
View File

@@ -1,6 +1,6 @@
[submodule "wapp"]
path = wapp
url = git@git.thewizardapprentice.com:abdelrahman/wizapp-stdlib
url = https://git.thewizardapprentice.com/abdelrahman/wizapp-stdlib
[submodule "raylib-src"]
path = raylib-src
url = git@github.com:raysan5/raylib
url = https://github.com/raysan5/raylib

View File

@@ -1,34 +1,26 @@
CC = clang
CFLAGS = -g -O3 -Iraylib/include -Iwapp/src
LDFLAGS = '-Wl,-rpath,$$ORIGIN/raylib/lib' -Lraylib/lib -lraylib
CFLAGS = -g -O3 -Iwapp/src
LDFLAGS = -lm
BASEDIR = $(shell realpath ./)
RL_SRCDIR = ${BASEDIR}/raylib-src/src
RL_BUILDDIR = ${BASEDIR}/raylib-build
RL_DESTDIR = ${BASEDIR}/raylib
RL_LIBDIR = ${RL_DESTDIR}/lib
RL_INCLUDEDIR = ${RL_DESTDIR}/include
PLATFORM = $(shell uname)
.PHONY: all raylib wapp no_dod dod
.PHONY: all raylib wapp no_dod dod clean
all: no_dod dod
# raylib:
# cmake -S raylib-src -B raylib-build -DCMAKE_INSTALL_PREFIX=raylib -DCMAKE_CONFIGURATION_TYPES=Release -DBUILD_SHARED_LIBS=ON -G "Ninja Multi-Config"
# cmake --build raylib-build --config=Release
# cmake --install raylib-build --config=Release
raylib:
mkdir -p ${RL_BUILDDIR} ${RL_LIBDIR} ${RL_INCLUDEDIR}
cd ${RL_SRCDIR} && \
make PLATFORM=PLATFORM_DESKTOP RAYLIB_RELEASE_PATH=${RL_BUILDDIR} RAYLIB_LIBTYPE=SHARED && \
cp ${RL_BUILDDIR}/* ${RL_LIBDIR} && \
cp ${RL_SRCDIR}/*.h ${RL_INCLUDEDIR}
wapp:
cd wapp && python3 -m codegen
no_dod: raylib wapp
no_dod: wapp
$(CC) $(CFLAGS) $(LDFLAGS) no_dod.c wapp/src/wapp.c -o no_dod
dod: raylib wapp
dod: wapp
$(CC) $(CFLAGS) $(LDFLAGS) dod.c wapp/src/wapp.c -o dod
clean:
rm -rf ${RL_BUILDDIR} ${RL_DESTDIR}

3
README.md Normal file
View File

@@ -0,0 +1,3 @@
# Data-Oriented Design Test
Playing around with [DOD](https://en.wikipedia.org/wiki/Data-oriented_design) concepts and comparing performance of resulting applications.

View File

@@ -7,6 +7,8 @@
#define MAX_WANDERER_DIM 8
#define MIN_WANDERER_DIM 3
#define MIN_ZONE_DIM 30
#define MIN_ABS_VELOCITY 200
#define MAX_ABS_VELOCITY 600
#define BG_COLOR (Color){.r = 0xea, .g = 0xf2, .b = 0xe3, .a = 0xff}
#define FG_COLOR (Color){.r = 0x42, .g = 0x4C, .b = 0x55, .a = 0xff}
#define ZONE_COLOR (Color){.r = 0xb4, .g = 0x65, .b = 0x4a, .a = 0xff}
@@ -15,6 +17,9 @@
#define WANDERER_SLOWDOWN_FACTOR 0.5f
#define WANDERER_SPEEDUP_FACTOR 2.0f
#define MSG_BUF_LEN 4096
#define LINE_BUF_LEN 1024
#define LOOPS 100
#define abs(A) (A < 0 ? A * -1 : A)
#define min(A, B) (A < B ? A : B)
#define max(A, B) (A > B ? A : B)

172
dod.c
View File

@@ -1,6 +1,6 @@
#include "wapp.h"
#include "common.h"
#include "raylib.h"
#include <math.h>
#include <stdint.h>
#include <stdbool.h>
#include <assert.h>
@@ -20,19 +20,19 @@ enum EntityTag {
typedef struct Entity Entity;
struct Entity {
u16 id;
u32 id;
};
typedef struct Position Position;
struct Position {
f32 x;
f32 y;
i16 x;
i16 y;
};
typedef struct Scale Scale;
struct Scale {
f32 width;
f32 height;
i16 width;
i16 height;
};
typedef struct Rect Rect;
@@ -43,8 +43,8 @@ struct Rect {
typedef struct Velocity Velocity;
struct Velocity {
f32 x;
f32 y;
i16 x;
i16 y;
};
typedef struct Manager Manager;
@@ -58,7 +58,6 @@ struct Manager {
typedef void (*ScaleInitialiser)(Scale *scale, XOR256State *state);
typedef void (*VelocityInitialiser)(Velocity *velocity, XOR256State *state);
typedef void (*RaylibDrawRectFunc)(int posX, int posY, int width, int height, Color color);
void init_manager(const Allocator *allocator, Manager *manager);
void init_position(Position *position, XOR256State *state);
@@ -66,16 +65,14 @@ void init_scale_wanderer(Scale *scale, XOR256State *state);
void init_scale_zone(Scale *scale, XOR256State *state);
void init_velocity(Velocity *velocity, XOR256State *state);
void zero_velocity(Velocity *velocity, XOR256State *state);
void update_positions(u8 *tags, Rect *rects, Velocity *velocities, u64 count);
u64 update_positions(u8 *tags, Rect *rects, Velocity *velocities, u64 count, f32 delta);
void render_entities(const u8 *tags, const Rect *rects, u64 count);
u64 collides(const Rect *rect, const Rect *collider);
f32 get_random_float(XOR256State *state);
u8 collides(const Rect *rect, const Rect *collider);
i16 get_random_velocity(XOR256State *state);
u64 log_data(const Rect *rects, u64 count, FILE *fp, char *line);
int main(void) {
SetTraceLogLevel(LOG_NONE);
InitWindow(WIDTH, HEIGHT, "DOD test");
// SetTargetFPS(120);
Allocator arena = wapp_mem_arena_allocator_init(MB(20));
assert(!wapp_mem_allocator_invalid(&arena));
@@ -98,28 +95,24 @@ int main(void) {
velocity_initialisers[is_zone](&(manager.velocities[i]), &state);
}
while (!WindowShouldClose()) {
f64 time = GetTime();
if (time >= 20.0) {
break;
}
update_positions(manager.tags, manager.rects, manager.velocities, manager.count);
BeginDrawing();
ClearBackground(BG_COLOR);
// render_entities(manager.tags, manager.rects, manager.count);
DrawFPS(10, 10);
EndDrawing();
FILE *fp = fopen("dod.out", "w");
assert(fp != NULL);
u64 update_counter = 0;
u64 render_counter = 0;
f32 delta = 0.016666666f;
for (u64 i = 0; i < LOOPS; ++i) {
update_counter += update_positions(manager.tags, manager.rects, manager.velocities, manager.count, delta);
char line[LINE_BUF_LEN] = {0};
render_counter += log_data(manager.rects, manager.count, fp, line);
}
fclose(fp);
wapp_mem_arena_allocator_destroy(&arena);
CloseWindow();
printf("UPDATE: %lu, RENDER: %lu\n", update_counter, render_counter);
return 0;
}
@@ -128,6 +121,8 @@ void init_manager(const Allocator *allocator, Manager *manager) {
assert(allocator != NULL && manager != NULL);
u64 total_count = (u64)WANDERER_COUNT + (u64)ZONE_COUNT;
assert(total_count < (1lu << 32)); // Ensure we're not exceeding the maximum limit of entities
u64 entities_size = sizeof(Entity) * total_count;
u64 rects_size = sizeof(Rect) * total_count;
u64 velocities_size = sizeof(Velocity) * total_count;
@@ -147,24 +142,24 @@ void init_manager(const Allocator *allocator, Manager *manager) {
}
void init_position(Position *position, XOR256State *state) {
position->x = wapp_prng_xorshift_256(state) % WIDTH;
position->y = wapp_prng_xorshift_256(state) % HEIGHT;
position->x = wapp_prng_xorshift_256ss(state) % WIDTH;
position->y = wapp_prng_xorshift_256ss(state) % HEIGHT;
}
void init_scale_wanderer(Scale *scale, XOR256State *state) {
f32 value = (f32)((wapp_prng_xorshift_256(state) % (MAX_WANDERER_DIM + 1 - MIN_WANDERER_DIM)) + MIN_WANDERER_DIM);
i16 value = (i16)((wapp_prng_xorshift_256p(state) % (MAX_WANDERER_DIM + 1 - MIN_WANDERER_DIM)) + MIN_WANDERER_DIM);
scale->width = value;
scale->height = value;
}
void init_scale_zone(Scale *scale, XOR256State *state) {
scale->width = wapp_prng_xorshift_256(state) % ((u64)HALF_WIDTH - MIN_ZONE_DIM) + MIN_ZONE_DIM;
scale->height = wapp_prng_xorshift_256(state) % ((u64)HALF_HEIGHT - MIN_ZONE_DIM) + MIN_ZONE_DIM;
scale->width = wapp_prng_xorshift_256p(state) % ((u64)HALF_WIDTH - MIN_ZONE_DIM) + MIN_ZONE_DIM;
scale->height = wapp_prng_xorshift_256p(state) % ((u64)HALF_HEIGHT - MIN_ZONE_DIM) + MIN_ZONE_DIM;
}
void init_velocity(Velocity *velocity, XOR256State *state) {
velocity->x = get_random_float(state);
velocity->y = get_random_float(state);
velocity->x = get_random_velocity(state);
velocity->y = get_random_velocity(state);
}
void zero_velocity(Velocity *velocity, XOR256State *state) {
@@ -172,11 +167,15 @@ void zero_velocity(Velocity *velocity, XOR256State *state) {
velocity->y = 0.0f;
}
void update_positions(u8 *tags, Rect *rects, Velocity *velocities, u64 count) {
u8 index = 0;
f32 multipliers[2] = {1.0f, 0.5f};
u64 update_positions(u8 *tags, Rect *rects, Velocity *velocities, u64 count, f32 delta) {
persistent f32 multipliers[2] = {1.0f, 0.5f};
persistent u64 inside_zone_mask = 0x7;
u8 index = 0;
f32 pos_x, pos_y;
f32 max_x, max_y;
u64 total = 0;
for (u64 i = ZONE_COUNT; i < count; ++i) {
tags[i] &= inside_zone_mask;
for (u64 j = 0; j < ZONE_COUNT; ++j) {
@@ -185,58 +184,67 @@ void update_positions(u8 *tags, Rect *rects, Velocity *velocities, u64 count) {
index = (tags[i] & ENTITY_TAG_INSIDE_ZONE) >> INSIDE_ZONE_TAG_SHIFT;
rects[i].position.x += velocities[i].x * multipliers[index];
rects[i].position.y += velocities[i].y * multipliers[index];
max_x = WIDTH - rects[i].scale.width;
max_y = HEIGHT - rects[i].scale.height;
f32 max_x = WIDTH - rects[i].scale.width;
f32 max_y = HEIGHT - rects[i].scale.height;
pos_x = rects[i].position.x + velocities[i].x * multipliers[index] * delta;
pos_y = rects[i].position.y + velocities[i].y * multipliers[index] * delta;
if (rects[i].position.x < 0 || rects[i].position.x >= max_x) {
rects[i].position.x = min(max(rects[i].position.x, 0), max_x);
if (pos_x < 0 || pos_x >= max_x) {
pos_x = min(max(pos_x, 0), max_x);
velocities[i].x *= -1;
}
if (rects[i].position.y < 0 || rects[i].position.y >= max_y) {
rects[i].position.y = min(max(rects[i].position.y, 0), max_y);
if (pos_y < 0 || pos_y >= max_y) {
pos_y = min(max(pos_y, 0), max_y);
velocities[i].y *= -1;
}
}
rects[i].position.x = roundf(pos_x);
rects[i].position.y = roundf(pos_y);
++total;
}
void render_entities(const u8 *tags, const Rect *rects, u64 count) {
persistent RaylibDrawRectFunc renderers[2] = {DrawRectangle, DrawRectangleLines};
persistent Color colors[2] = {FG_COLOR, ZONE_COLOR};
for (u64 i = 0; i < count; ++i) {
u8 func_index = (tags[i] & ENTITY_TAG_COLLIDER) >> COLLIDER_TAG_SHIFT;
u8 color_index = func_index | ((tags[i] & ENTITY_TAG_INSIDE_ZONE) >> INSIDE_ZONE_TAG_SHIFT);
renderers[func_index](
rects[i].position.x,
rects[i].position.y,
rects[i].scale.width,
rects[i].scale.height,
colors[color_index]
);
}
return total;
}
u64 collides(const Rect *rect, const Rect *collider) {
f32 rect_min_x = rect->position.x + rect->scale.width;
f32 rect_min_y = rect->position.y + rect->scale.height;
f32 rect_max_x = rect->position.x;
f32 rect_max_y = rect->position.y;
u8 collides(const Rect *rect, const Rect *collider) {
i16 rect_min_x = rect->position.x + rect->scale.width;
i16 rect_min_y = rect->position.y + rect->scale.height;
i16 rect_max_x = rect->position.x;
i16 rect_max_y = rect->position.y;
f32 zone_x0 = collider->position.x;
f32 zone_y0 = collider->position.y;
f32 zone_x1 = collider->position.x + collider->scale.width;
f32 zone_y1 = collider->position.y + collider->scale.height;
i16 zone_x0 = collider->position.x;
i16 zone_y0 = collider->position.y;
i16 zone_x1 = collider->position.x + collider->scale.width;
i16 zone_y1 = collider->position.y + collider->scale.height;
return (u64)(rect_min_x > zone_x0 && rect_min_y > zone_y0 &&
return (u8)(rect_min_x > zone_x0 && rect_min_y > zone_y0 &&
rect_max_x < zone_x1 && rect_max_y < zone_y1);
}
f32 get_random_float(XOR256State *state) {
i64 random = (i64)wapp_prng_xorshift_256(state) - INT64_MAX;
return (f32)(random) / (f32)(INT64_MAX);
i16 get_random_velocity(XOR256State *state) {
return (wapp_prng_xorshift_256(state) % (MAX_ABS_VELOCITY + 1 - MIN_ABS_VELOCITY)) + MIN_ABS_VELOCITY;
}
u64 log_data(const Rect *rects, u64 count, FILE *fp, char *line) {
u64 total = 0;
for (u64 i = 0; i < count; ++i) {
snprintf(
line, LINE_BUF_LEN,
"x: %d, y: %d, width: %d, height: %d\n",
rects[i].position.x,
rects[i].position.y,
rects[i].scale.width,
rects[i].scale.height
);
u64 size = strlen(line);
fwrite(line, sizeof(char), size, fp);
memset(line, 0, size);
++total;
}
return total;
}

View File

@@ -1,9 +1,10 @@
#include "wapp.h"
#include "common.h"
#include "raylib.h"
#include <stdint.h>
#include <stdbool.h>
#include <assert.h>
#include <time.h>
#include <stdio.h>
#define OBJECT_COMMON struct {Position position; Scale scale;}
@@ -39,19 +40,11 @@ struct SlowZone {
void init_wanderer(Wanderer *wanderer, XOR256State *state);
void move_wanderer(Wanderer *wanderer, const SlowZone *zones);
void render_wanderer(const Wanderer *wanderer);
void init_slow_zone(SlowZone *zone, XOR256State *state);
void render_slow_zone(const SlowZone *zone);
bool inside_zone(const Wanderer *wanderer, const SlowZone *zone);
f32 get_random_float(XOR256State *state);
int main(void) {
SetTraceLogLevel(LOG_NONE);
InitWindow(WIDTH, HEIGHT, "No-DOD test");
// SetTargetFPS(120);
Allocator arena = wapp_mem_arena_allocator_init(MB(20));
XOR256State state = wapp_prng_xorshift_init_state();
u64 wanderers_size = sizeof(Wanderer) * WANDERER_COUNT;
@@ -70,36 +63,39 @@ int main(void) {
init_slow_zone(&(zones[i]),&state);
}
while (!WindowShouldClose()) {
f64 time = GetTime();
if (time >= 20.0) {
break;
}
FILE *fp = fopen("no_dod.out", "w");
assert(fp != NULL);
u64 update_counter = 0;
u64 render_counter = 0;
for (u64 i = 0; i < LOOPS; ++i) {
for (u64 i = 0; i < WANDERER_COUNT; ++i) {
move_wanderer(&(wanderers[i]), zones);
++update_counter;
}
BeginDrawing();
char line[LINE_BUF_LEN] = {0};
ClearBackground(BG_COLOR);
// for (u64 i = 0; i < ZONE_COUNT; ++i) {
// render_slow_zone(&(zones[i]));
// }
//
// for (u64 i = 0; i < WANDERER_COUNT; ++i) {
// render_wanderer(&(wanderers[i]));
// }
DrawFPS(10, 10);
EndDrawing();
for (u64 i = 0; i < WANDERER_COUNT; ++i) {
snprintf(
line, LINE_BUF_LEN,
"x: %f, y: %f, width: %f, height: %f\n",
wanderers[i].position.x,
wanderers[i].position.y,
wanderers[i].scale.width,
wanderers[i].scale.height
);
u64 size = strlen(line);
fwrite(line, sizeof(char), size, fp);
memset(line, 0, size);
++render_counter;
}
}
fclose(fp);
wapp_mem_arena_allocator_destroy(&arena);
CloseWindow();
printf("UPDATE: %lu, RENDER: %lu\n", update_counter, render_counter);
return 0;
}
@@ -157,17 +153,6 @@ void move_wanderer(Wanderer *wanderer, const SlowZone *zones) {
}
}
void render_wanderer(const Wanderer *wanderer) {
Color color = wanderer->slow ? ZONE_COLOR : FG_COLOR;
DrawRectangle(
(i32)wanderer->position.x,
(i32)wanderer->position.y,
(i32)wanderer->scale.width,
(i32)wanderer->scale.height,
color
);
}
void init_slow_zone(SlowZone *zone, XOR256State *state) {
zone->position = (Position){
.x = wapp_prng_xorshift_256(state) % WIDTH,
@@ -180,16 +165,6 @@ void init_slow_zone(SlowZone *zone, XOR256State *state) {
};
}
void render_slow_zone(const SlowZone *zone) {
DrawRectangleLines(
zone->position.x,
zone->position.y,
zone->scale.width,
zone->scale.height,
ZONE_COLOR
);
}
bool inside_zone(const Wanderer *wanderer, const SlowZone *zone) {
f32 wanderer_min_x = wanderer->position.x + wanderer->scale.width;
f32 wanderer_min_y = wanderer->position.y + wanderer->scale.height;