Finish dod and set perf testing
This commit is contained in:
		
							
								
								
									
										11
									
								
								Makefile
									
									
									
									
									
								
							
							
						
						
									
										11
									
								
								Makefile
									
									
									
									
									
								
							@@ -1,13 +1,6 @@
 | 
			
		||||
BUILD_TYPE = debug
 | 
			
		||||
CC         = clang
 | 
			
		||||
CFLAGS     = -Iraylib/include -Iwapp/src
 | 
			
		||||
LDFLAGS    = '-Wl,-rpath,$$ORIGIN/raylib/lib' -Lraylib/lib -lraylib
 | 
			
		||||
 | 
			
		||||
ifeq ($(BUILD_TYPE),debug)
 | 
			
		||||
	CFLAGS += -g
 | 
			
		||||
else
 | 
			
		||||
	CFLAGS += -O3 -DNDEBUG
 | 
			
		||||
endif
 | 
			
		||||
CFLAGS     = -g -O3 -Iraylib/include -Iwapp/src
 | 
			
		||||
LDFLAGS    = '-Wl,-rpath,$$ORIGIN/raylib/lib' -Lraylib/lib -lraylib -lprofiler
 | 
			
		||||
 | 
			
		||||
.PHONY: all raylib no_dod dod
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										116
									
								
								dod.c
									
									
									
									
									
								
							
							
						
						
									
										116
									
								
								dod.c
									
									
									
									
									
								
							@@ -7,15 +7,15 @@
 | 
			
		||||
#include <string.h>
 | 
			
		||||
 | 
			
		||||
enum EntityTag {
 | 
			
		||||
  ENTITY_TAG_MOVABLE    = 1 << 0,
 | 
			
		||||
  ENTITY_TAG_RENDERABLE = 1 << 1,
 | 
			
		||||
  ENTITY_TAG_COLLIDER   = 1 << 2,
 | 
			
		||||
  ENTITY_TAG_MOVABLE     = 1 << 0,
 | 
			
		||||
  ENTITY_TAG_RENDERABLE  = 1 << 1,
 | 
			
		||||
  ENTITY_TAG_COLLIDER    = 1 << 2,
 | 
			
		||||
  ENTITY_TAG_INSIDE_ZONE = 1 << 3,
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
typedef struct Entity Entity;
 | 
			
		||||
struct Entity {
 | 
			
		||||
  u16 id;
 | 
			
		||||
  u8  tag;
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
typedef struct Position Position;
 | 
			
		||||
@@ -30,6 +30,12 @@ struct Scale {
 | 
			
		||||
  f32 height;
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
typedef struct Rect Rect;
 | 
			
		||||
struct Rect {
 | 
			
		||||
  Position position;
 | 
			
		||||
  Scale scale;
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
typedef struct Velocity Velocity;
 | 
			
		||||
struct Velocity {
 | 
			
		||||
  f32 x;
 | 
			
		||||
@@ -39,9 +45,9 @@ struct Velocity {
 | 
			
		||||
typedef struct Manager Manager;
 | 
			
		||||
struct Manager {
 | 
			
		||||
  Entity   *entities;
 | 
			
		||||
  Position *positions;
 | 
			
		||||
  Scale    *scales;
 | 
			
		||||
  Rect     *rects;
 | 
			
		||||
  Velocity *velocities;
 | 
			
		||||
  u8       *tags;
 | 
			
		||||
  u64      count;
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
@@ -55,12 +61,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(Position *positions, Scale *scales, Velocity *velocities, u64 count);
 | 
			
		||||
void get_colliders();
 | 
			
		||||
void render_entities(const Entity *entities, const Position *positions, const Scale *scales, u64 count);
 | 
			
		||||
void check_collisions(u8 *tags, Rect *rects, u64 count);
 | 
			
		||||
void update_positions(const u8 *tags, Rect *rects, Velocity *velocities, u64 count);
 | 
			
		||||
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);
 | 
			
		||||
 | 
			
		||||
int main(void) {
 | 
			
		||||
  SetTraceLogLevel(LOG_NONE);
 | 
			
		||||
  InitWindow(WIDTH, HEIGHT, "DOD test");
 | 
			
		||||
  SetTargetFPS(120);
 | 
			
		||||
 | 
			
		||||
@@ -79,15 +87,10 @@ int main(void) {
 | 
			
		||||
    u8 is_zone = i < ZONE_COUNT;
 | 
			
		||||
 | 
			
		||||
    manager.entities[i].id  = i;
 | 
			
		||||
    manager.entities[i].tag = (ENTITY_TAG_COLLIDER & (is_zone << 2)) | ENTITY_TAG_MOVABLE;
 | 
			
		||||
#ifndef NDEBUG
 | 
			
		||||
    manager.entities[i].tag |= ENTITY_TAG_RENDERABLE;
 | 
			
		||||
#else
 | 
			
		||||
    manager.entities[i].tag |= ENTITY_TAG_RENDERABLE & ~(is_zone << 1);
 | 
			
		||||
#endif
 | 
			
		||||
    manager.tags[i]         = (ENTITY_TAG_COLLIDER & (is_zone << 2)) | ENTITY_TAG_MOVABLE | ENTITY_TAG_RENDERABLE;
 | 
			
		||||
 | 
			
		||||
    init_position(&(manager.positions[i]), &state);
 | 
			
		||||
    scale_initialisers[is_zone](&(manager.scales[i]), &state);
 | 
			
		||||
    init_position(&(manager.rects[i].position), &state);
 | 
			
		||||
    scale_initialisers[is_zone](&(manager.rects[i].scale), &state);
 | 
			
		||||
    velocity_initialisers[is_zone](&(manager.velocities[i]), &state);
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
@@ -97,13 +100,15 @@ int main(void) {
 | 
			
		||||
      break;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    update_positions(manager.positions, manager.scales, manager.velocities, manager.count);
 | 
			
		||||
    check_collisions(manager.tags, manager.rects, manager.count);
 | 
			
		||||
 | 
			
		||||
    update_positions(manager.tags, manager.rects, manager.velocities, manager.count);
 | 
			
		||||
 | 
			
		||||
    BeginDrawing();
 | 
			
		||||
 | 
			
		||||
    ClearBackground(BG_COLOR);
 | 
			
		||||
 | 
			
		||||
    render_entities(manager.entities, manager.positions, manager.scales, manager.count);
 | 
			
		||||
    render_entities(manager.tags, manager.rects, manager.count);
 | 
			
		||||
 | 
			
		||||
    DrawFPS(10, 10);
 | 
			
		||||
 | 
			
		||||
@@ -122,10 +127,10 @@ void init_manager(const Allocator *allocator, Manager *manager) {
 | 
			
		||||
 | 
			
		||||
  u64 total_count      = (u64)WANDERER_COUNT + (u64)ZONE_COUNT;
 | 
			
		||||
  u64 entities_size    = sizeof(Entity)   * total_count;
 | 
			
		||||
  u64 positions_size   = sizeof(Position) * total_count;
 | 
			
		||||
  u64 scales_size      = sizeof(Scale)    * total_count;
 | 
			
		||||
  u64 rects_size       = sizeof(Rect)     * total_count;
 | 
			
		||||
  u64 velocities_size  = sizeof(Velocity) * total_count;
 | 
			
		||||
  u64 allocation_size  = entities_size + positions_size + scales_size + velocities_size;
 | 
			
		||||
  u64 tags_size        = sizeof(u8)       * total_count;
 | 
			
		||||
  u64 allocation_size  = entities_size + rects_size + velocities_size + tags_size;
 | 
			
		||||
  u8 *buffer           = wapp_mem_allocator_alloc(allocator, allocation_size);
 | 
			
		||||
 | 
			
		||||
  assert(buffer != NULL);
 | 
			
		||||
@@ -133,9 +138,9 @@ void init_manager(const Allocator *allocator, Manager *manager) {
 | 
			
		||||
  memset(buffer, 0, allocation_size);
 | 
			
		||||
 | 
			
		||||
  manager->entities   = (Entity *)buffer;
 | 
			
		||||
  manager->positions  = (Position *)(buffer + entities_size);
 | 
			
		||||
  manager->scales     = (Scale *)(buffer + entities_size + positions_size);
 | 
			
		||||
  manager->velocities = (Velocity *)(buffer + entities_size + positions_size + scales_size);
 | 
			
		||||
  manager->rects      = (Rect *)(buffer + entities_size);
 | 
			
		||||
  manager->velocities = (Velocity *)(buffer + entities_size + rects_size);
 | 
			
		||||
  manager->tags       = (u8 *)(buffer + entities_size + rects_size + velocities_size);
 | 
			
		||||
  manager->count      = total_count;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@@ -165,37 +170,74 @@ void zero_velocity(Velocity *velocity, XOR256State *state) {
 | 
			
		||||
  velocity->y = 0.0f;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void update_positions(Position *positions, Scale *scales, Velocity *velocities, u64 count) {
 | 
			
		||||
void update_positions(const u8 *tags, Rect *rects, Velocity *velocities, u64 count) {
 | 
			
		||||
  u8 index = 0;
 | 
			
		||||
  f32 multipliers[2] = {1.0f, 0.5f};
 | 
			
		||||
 | 
			
		||||
  for (u64 i = 0; i < count; ++i) {
 | 
			
		||||
    positions[i].x += velocities[i].x;
 | 
			
		||||
    positions[i].y += velocities[i].y;
 | 
			
		||||
    index = (tags[i] & ENTITY_TAG_INSIDE_ZONE) == ENTITY_TAG_INSIDE_ZONE;
 | 
			
		||||
 | 
			
		||||
    f32 max_x = WIDTH - scales[i].width;
 | 
			
		||||
    f32 max_y = HEIGHT - scales[i].height;
 | 
			
		||||
    rects[i].position.x += velocities[i].x * multipliers[index];
 | 
			
		||||
    rects[i].position.y += velocities[i].y * multipliers[index];
 | 
			
		||||
 | 
			
		||||
    if (positions[i].x < 0 || positions[i].x >= max_x) {
 | 
			
		||||
      positions[i].x = min(max(positions[i].x, 0), max_x);
 | 
			
		||||
    f32 max_x = WIDTH - rects[i].scale.width;
 | 
			
		||||
    f32 max_y = HEIGHT - rects[i].scale.height;
 | 
			
		||||
 | 
			
		||||
    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);
 | 
			
		||||
      velocities[i].x *= -1;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    if (positions[i].y < 0 || positions[i].y >= max_y) {
 | 
			
		||||
      positions[i].y = min(max(positions[i].y, 0), max_y);
 | 
			
		||||
    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);
 | 
			
		||||
      velocities[i].y *= -1;
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void render_entities(const Entity *entities, const Position *positions, const Scale *scales, u64 count) {
 | 
			
		||||
void check_collisions(u8 *tags, Rect *rects, u64 count) {
 | 
			
		||||
  persistent u64 inside_zone_mask = 0x7;
 | 
			
		||||
  for (u64 i = ZONE_COUNT; i < count; ++i) {
 | 
			
		||||
    tags[i] &= inside_zone_mask;
 | 
			
		||||
    for (u64 j = 0; j < ZONE_COUNT; ++j) {
 | 
			
		||||
      tags[i] |= collides(&rects[i], &rects[j]) << 3;
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
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) {
 | 
			
		||||
    u64 is_collider = (entities[i].tag & ENTITY_TAG_COLLIDER) == ENTITY_TAG_COLLIDER;
 | 
			
		||||
    u8 func_index  = (tags[i] & ENTITY_TAG_COLLIDER) == ENTITY_TAG_COLLIDER;
 | 
			
		||||
    u8 color_index = func_index || (tags[i] & ENTITY_TAG_INSIDE_ZONE) == ENTITY_TAG_INSIDE_ZONE;
 | 
			
		||||
 | 
			
		||||
    renderers[is_collider](positions[i].x, positions[i].y, scales[i].width, scales[i].height, colors[is_collider]);
 | 
			
		||||
    renderers[func_index](
 | 
			
		||||
      rects[i].position.x,
 | 
			
		||||
      rects[i].position.y,
 | 
			
		||||
      rects[i].scale.width,
 | 
			
		||||
      rects[i].scale.height,
 | 
			
		||||
      colors[color_index]
 | 
			
		||||
    );
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
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;
 | 
			
		||||
 | 
			
		||||
  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;
 | 
			
		||||
 | 
			
		||||
  return (u64)(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);
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										10
									
								
								no_dod.c
									
									
									
									
									
								
							
							
						
						
									
										10
									
								
								no_dod.c
									
									
									
									
									
								
							@@ -1,3 +1,4 @@
 | 
			
		||||
#include "raylib-src/src/raylib.h"
 | 
			
		||||
#include "wapp.h"
 | 
			
		||||
#include "common.h"
 | 
			
		||||
#include "raylib.h"
 | 
			
		||||
@@ -42,15 +43,14 @@ void move_wanderer(Wanderer *wanderer, const SlowZone *zones);
 | 
			
		||||
void render_wanderer(const Wanderer *wanderer);
 | 
			
		||||
void init_slow_zone(SlowZone *zone,  XOR256State *state);
 | 
			
		||||
 | 
			
		||||
#ifndef NDEBUG
 | 
			
		||||
void render_slow_zone(const SlowZone *zone);
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
bool inside_zone(const Wanderer *wanderer, const SlowZone *zone);
 | 
			
		||||
f32 get_random_float(XOR256State *state);
 | 
			
		||||
 | 
			
		||||
int main(void) {
 | 
			
		||||
  InitWindow(WIDTH, HEIGHT, "DOD test");
 | 
			
		||||
  SetTraceLogLevel(LOG_NONE);
 | 
			
		||||
  InitWindow(WIDTH, HEIGHT, "No-DOD test");
 | 
			
		||||
  SetTargetFPS(120);
 | 
			
		||||
 | 
			
		||||
  Allocator arena       = wapp_mem_arena_allocator_init(MB(20));
 | 
			
		||||
@@ -85,11 +85,9 @@ int main(void) {
 | 
			
		||||
 | 
			
		||||
    ClearBackground(BG_COLOR);
 | 
			
		||||
 | 
			
		||||
#ifndef NDEBUG
 | 
			
		||||
    for (u64 i = 0; i < ZONE_COUNT; ++i) {
 | 
			
		||||
      render_slow_zone(&(zones[i]));
 | 
			
		||||
    }
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
    for (u64 i = 0; i < WANDERER_COUNT; ++i) {
 | 
			
		||||
      render_wanderer(&(wanderers[i]));
 | 
			
		||||
@@ -183,7 +181,6 @@ void init_slow_zone(SlowZone *zone,  XOR256State *state) {
 | 
			
		||||
  };
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#ifndef NDEBUG
 | 
			
		||||
void render_slow_zone(const SlowZone *zone) {
 | 
			
		||||
  DrawRectangleLines(
 | 
			
		||||
    zone->position.x,
 | 
			
		||||
@@ -193,7 +190,6 @@ void render_slow_zone(const SlowZone *zone) {
 | 
			
		||||
    ZONE_COLOR
 | 
			
		||||
  );
 | 
			
		||||
}
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
bool inside_zone(const Wanderer *wanderer, const SlowZone *zone) {
 | 
			
		||||
  f32 wanderer_min_x = wanderer->position.x + wanderer->scale.width;
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										30
									
								
								run_perf
									
									
									
									
									
										Executable file
									
								
							
							
						
						
									
										30
									
								
								run_perf
									
									
									
									
									
										Executable file
									
								
							@@ -0,0 +1,30 @@
 | 
			
		||||
#!/bin/bash
 | 
			
		||||
 | 
			
		||||
REPEATS=10
 | 
			
		||||
 | 
			
		||||
EVENTS="branches,\
 | 
			
		||||
branch-misses,\
 | 
			
		||||
bus-cycles,\
 | 
			
		||||
cache-references,\
 | 
			
		||||
cache-misses,\
 | 
			
		||||
L1-dcache-load-misses,\
 | 
			
		||||
L1-icache-load-misses,\
 | 
			
		||||
LLC-load-misses,\
 | 
			
		||||
LLC-store-misses,\
 | 
			
		||||
icache.misses,\
 | 
			
		||||
ref-cycles,\
 | 
			
		||||
cycles,\
 | 
			
		||||
instructions,\
 | 
			
		||||
alignment-faults,\
 | 
			
		||||
cgroup-switches,\
 | 
			
		||||
context-switches,\
 | 
			
		||||
cpu-clock,\
 | 
			
		||||
cpu-migrations,\
 | 
			
		||||
emulation-faults,\
 | 
			
		||||
page-faults,\
 | 
			
		||||
major-faults,\
 | 
			
		||||
minor-faults,\
 | 
			
		||||
task-clock"
 | 
			
		||||
 | 
			
		||||
perf stat -e $EVENTS -r $REPEATS -o no_dod_stats ./no_dod
 | 
			
		||||
perf stat -e $EVENTS -r $REPEATS -o dod_stats ./dod
 | 
			
		||||
		Reference in New Issue
	
	Block a user