Skip to content

Commit

Permalink
First pass jolt physics impl
Browse files Browse the repository at this point in the history
  • Loading branch information
Honeybunch committed Oct 2, 2023
1 parent 89f9165 commit d29738a
Show file tree
Hide file tree
Showing 22 changed files with 557 additions and 31 deletions.
9 changes: 7 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ find_package(zstd CONFIG REQUIRED)
find_package(Ktx CONFIG REQUIRED)
find_package(Threads REQUIRED)
find_package(flecs CONFIG REQUIRED)
find_package(unofficial-joltphysics CONFIG REQUIRED)
if(PROFILE_TRACY)
find_package(Tracy CONFIG REQUIRED)
endif()
Expand Down Expand Up @@ -133,7 +134,7 @@ find_program(GLTFPACK gltfpack
REQUIRED)

# The list of all external targets to link
set(library_list "$<IF:$<TARGET_EXISTS:flecs::flecs>,flecs::flecs,flecs::flecs_static>;SDL2::SDL2main;volk::volk;volk::volk_headers;imgui::imgui;mimalloc;mimalloc-static;KTX::ktx;json-c::json-c;meshoptimizer::meshoptimizer")
set(library_list "$<IF:$<TARGET_EXISTS:flecs::flecs>,flecs::flecs,flecs::flecs_static>;SDL2::SDL2main;volk::volk;volk::volk_headers;imgui::imgui;mimalloc;mimalloc-static;KTX::ktx;json-c::json-c;meshoptimizer::meshoptimizer;unofficial::joltphysics::Jolt")

# Helper function to cook shaders
function(cook_shaders out_shader_sources out_shader_headers)
Expand Down Expand Up @@ -339,14 +340,17 @@ add_library(toybox STATIC ${lib_source})
if(WIN32)
# This really only matters for clang-cl
# ${CMAKE_C_COMPILER_ID} being MSVC would fail the above check
# So thing of this more as if(MSVC-style-compiler)
# So think of this more as `if(MSVC-style-compiler)`
if(MSVC)
target_compile_options(toybox PUBLIC -Wno-reserved-id-macro)
target_compile_options(toybox PUBLIC -Wno-documentation)
endif()

# Folding VLAs to constant arrays is desired
target_compile_options(toybox PUBLIC -Wno-gnu-folding-constant)
# We use anonymous structs
target_compile_options(toybox PUBLIC -Wno-gnu-anonymous-struct)
target_compile_options(toybox PUBLIC -Wno-nested-anon-types)

elseif(ANDROID)
if(CMAKE_C_COMPILER_ARCHITECTURE_ID MATCHES "arm")
Expand Down Expand Up @@ -403,6 +407,7 @@ endif()
target_link_libraries(toybox PUBLIC $<IF:$<TARGET_EXISTS:SDL2_mixer::SDL2_mixer>,SDL2_mixer::SDL2_mixer,SDL2_mixer::SDL2_mixer-static>)

target_compile_features(toybox PUBLIC c_std_11)
target_compile_features(toybox PUBLIC cxx_std_20)

# Turn on warnings
target_compile_options(toybox PUBLIC
Expand Down
2 changes: 1 addition & 1 deletion CMakePresets.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"CMAKE_CXX_COMPILER": "clang++",
"CMAKE_RC_COMPILER": "llvm-rc",
"CMAKE_EXPORT_COMPILE_COMMANDS": "ON",
"FINAL": "OFF",
"FINAL": "ON",
"PROFILE_TRACY": "ON"
},
"condition": {
Expand Down
4 changes: 2 additions & 2 deletions assets/scenes/PBRTest.glb
Git LFS file not shown
3 changes: 3 additions & 0 deletions assets/scenes/phystest.blend
Git LFS file not shown
3 changes: 3 additions & 0 deletions assets/scenes/phystest.glb
Git LFS file not shown
4 changes: 4 additions & 0 deletions include/allocator.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
typedef struct mi_heap_s mi_heap_t;

typedef void *alloc_fn(void *user_data, size_t size);
typedef void *alloc_aligned_fn(void *user_data, size_t size, size_t alignment);
typedef void *realloc_fn(void *user_data, void *original, size_t size);
typedef void *realloc_aligned_fn(void *user_data, void *original, size_t size,
size_t alignment);
Expand All @@ -15,6 +16,8 @@ typedef void free_fn(void *user_data, void *ptr);
#define tb_alloc(a, size) (a).alloc((a).user_data, (size))
#define tb_alloc_tp(a, T) (T *)(a).alloc((a).user_data, sizeof(T))
#define tb_alloc_nm_tp(a, n, T) (T *)(a).alloc((a).user_data, n * sizeof(T))
#define tb_alloc_aligned(a, size, align) \
(a).alloc_aligned((a).user_data, (size), (align));
#define tb_realloc(a, orig, size) (a).realloc((a).user_data, (orig), (size))
#define tb_realloc_tp(a, orig, T) \
(T *)(a).realloc((a).user_data, (orig), sizeof(T))
Expand All @@ -27,6 +30,7 @@ typedef void free_fn(void *user_data, void *ptr);
typedef struct Allocator {
void *user_data;
alloc_fn *alloc;
alloc_aligned_fn *alloc_aligned;
realloc_fn *realloc;
realloc_aligned_fn *realloc_aligned;
free_fn *free;
Expand Down
14 changes: 14 additions & 0 deletions include/physicssystem.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#pragma once

#ifdef __cplusplus
extern "C" {
#endif

typedef struct TbWorld TbWorld;

void tb_register_physics_sys(TbWorld *world);
void tb_unregister_physics_sys(TbWorld *world);

#ifdef __cplusplus
}
#endif
33 changes: 33 additions & 0 deletions include/physicssystem.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#pragma once

#include "allocator.h"

namespace JPH {
class PhysicsSystem;
class TempAllocator;
class JobSystemThreadPool;
} // namespace JPH

struct ecs_query_t;
namespace flecs {
typedef ecs_query_t query_t;
} // namespace flecs

class ObjectLayerPairFilterImpl;
class BPLayerInterfaceImpl;
class ObjectVsBroadPhaseLayerFilterImpl;

struct TbPhysicsSystem {
Allocator std_alloc;
Allocator tmp_alloc;

JPH::PhysicsSystem *jolt_phys;
JPH::TempAllocator *jolt_tmp_alloc;
JPH::JobSystemThreadPool *jolt_job_sys;

flecs::query_t *rigidbody_query;

BPLayerInterfaceImpl *bpl_interface;
ObjectVsBroadPhaseLayerFilterImpl *obp_filter;
ObjectLayerPairFilterImpl *olp_filter;
};
33 changes: 33 additions & 0 deletions include/physlayers.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#pragma once

#ifdef __cplusplus

#include <Jolt/Physics/Collision/BroadPhase/BroadPhaseLayer.h>
#include <Jolt/Physics/Collision/ObjectLayer.h>

// Layer that objects can be in, determines which other objects it can collide
// with Typically you at least want to have 1 layer for moving bodies and 1
// layer for static bodies, but you can have more layers if you want. E.g. you
// could have a layer for high detail collision (which is not used by the
// physics simulation but only if you do collision testing).
namespace Layers {
static constexpr JPH::ObjectLayer NON_MOVING = 0;
static constexpr JPH::ObjectLayer MOVING = 1;
static constexpr JPH::ObjectLayer NUM_LAYERS = 2;
}; // namespace Layers

// Each broadphase layer results in a separate bounding volume tree in the
// broad phase. You at least want to have a layer for non-moving and moving
// objects to avoid having to update a tree full of static objects every
// frame. You can have a 1-on-1 mapping between object layers and broadphase
// layers (like in this case) but if you have many object layers you'll be
// creating many broad phase trees, which is not efficient. If you want to
// fine tune your broadphase layers define JPH_TRACK_BROADPHASE_STATS and look
// at the stats reported on the TTY.
namespace BroadPhaseLayers {
static constexpr JPH::BroadPhaseLayer NON_MOVING(0);
static constexpr JPH::BroadPhaseLayer MOVING(1);
static constexpr uint32_t NUM_LAYERS(2);
}; // namespace BroadPhaseLayers

#endif
3 changes: 3 additions & 0 deletions include/profiling.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
#define TRACY_CALLSTACK 32
#endif
#include <tracy/TracyC.h>
#ifdef __cplusplus
#include <tracy/Tracy.hpp>
#endif

#define TracyCategoryColorCore 0xe066ff
#define TracyCategoryColorRendering 0x7fff00
Expand Down
22 changes: 22 additions & 0 deletions include/rigidbodycomponent.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#pragma once

#define RigidbodyComponentId 0xBAD33333
#define RigidbodyComponentIdStr "0xBAD33333"

#ifdef __cplusplus
extern "C" {
#endif

#include <SDL2/SDL_stdinc.h>

typedef struct TbWorld TbWorld;

typedef struct TbRigidbodyComponent {
uint32_t body; // Actually a JPH::BodyId
} TbRigidbodyComponent;

void tb_register_rigidbody_component(TbWorld *world);

#ifdef __cplusplus
}
#endif
10 changes: 10 additions & 0 deletions include/simd.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@
they are not
*/

#ifdef __clang__
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wgnu-anonymous-struct"
#pragma clang diagnostic ignored "-Wnested-anon-types"
#endif

#ifndef TB_USE_INVERSE_DEPTH
// #define TB_USE_INVERSE_DEPTH 1
#endif
Expand Down Expand Up @@ -244,3 +250,7 @@ float3 clampf3(float3 v, float3 min, float3 max);
float tb_randf(float min, float max);

#endif

#ifdef __clang__
#pragma clang diagnostic pop
#endif
3 changes: 0 additions & 3 deletions samples/samplecore/source/samplecore.c
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,6 @@ int32_t SDL_main(int32_t argc, char *argv[]) {
float delta_time_seconds = 0.0f;

while (running) {
TracyCFrameMarkStart("Simulation Frame");
TracyCZoneN(trcy_ctx, "Simulation Frame", true);
TracyCZoneColor(trcy_ctx, TracyCategoryColorCore);

Expand All @@ -142,15 +141,13 @@ int32_t SDL_main(int32_t argc, char *argv[]) {
if (!tb_tick_world(&world, delta_time_seconds)) {
running = false;
TracyCZoneEnd(trcy_ctx);
TracyCFrameMarkEnd("Simulation Frame");
break;
}

// Reset the arena allocator
arena = reset_arena(arena, true); // Just allow it to grow for now

TracyCZoneEnd(trcy_ctx);
TracyCFrameMarkEnd("Simulation Frame");
}

// This doesn't quite work yet
Expand Down
21 changes: 21 additions & 0 deletions source/allocator.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,14 @@ static void *arena_alloc(void *user_data, size_t size) {
return ptr;
}

static void *arena_alloc_aligned(void *user_data, size_t size,
size_t alignment) {
// In the arena allocator we're not going to bother to really implement
// realloc for now...
(void)alignment;
return arena_alloc(user_data, size);
}

static void *arena_realloc(void *user_data, void *original, size_t size) {
// In the arena allocator we're not going to bother to really implement
// realloc for now...
Expand Down Expand Up @@ -73,6 +81,7 @@ void create_arena_allocator(const char *name, ArenaAllocator *a,
.alloc =
(Allocator){
.alloc = arena_alloc,
.alloc_aligned = arena_alloc_aligned,
.realloc = arena_realloc,
.realloc_aligned = arena_realloc_aligned,
.free = arena_free,
Expand Down Expand Up @@ -117,6 +126,17 @@ static void *standard_alloc(void *user_data, size_t size) {
return ptr;
}

static void *standard_alloc_aligned(void *user_data, size_t size,
size_t alignment) {
TracyCZone(ctx, true);
TracyCZoneColor(ctx, TracyCategoryColorMemory);
StandardAllocator *alloc = (StandardAllocator *)user_data;
void *ptr = mi_heap_calloc_aligned(alloc->heap, 1, size, alignment);
TracyCAllocN(ptr, size, alloc->name);
TracyCZoneEnd(ctx);
return ptr;
}

static void *standard_realloc(void *user_data, void *original, size_t size) {
TracyCZone(ctx, true);
TracyCZoneColor(ctx, TracyCategoryColorMemory);
Expand Down Expand Up @@ -157,6 +177,7 @@ void create_standard_allocator(StandardAllocator *a, const char *name) {
.alloc =
{
.alloc = standard_alloc,
.alloc_aligned = standard_alloc_aligned,
.realloc = standard_realloc,
.realloc_aligned = standard_realloc_aligned,
.free = standard_free,
Expand Down
6 changes: 2 additions & 4 deletions source/oceansystem.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#include "oceansystem.h"

#include "assets.h"
#include "assetsystem.h"
#include "audiosystem.h"
#include "cameracomponent.h"
#include "cgltf.h"
Expand Down Expand Up @@ -81,7 +80,7 @@ void ocean_record(VkCommandBuffer buffer, uint32_t batch_count,
void ocean_prepass_record(TracyCGPUContext *gpu_ctx, VkCommandBuffer buffer,
uint32_t batch_count, const DrawBatch *batches) {
TracyCZoneNC(ctx, "Ocean Prepass Record", TracyCategoryColorRendering, true);
TracyCVkNamedZone(gpu_ctx, frame_scope, buffer, "Ocean Prepass", 3, true);
TracyCVkNamedZone(gpu_ctx, frame_scope, buffer, "Ocean Prepass", 2, true);
cmd_begin_label(buffer, "Ocean Prepass", f4(0.0f, 0.4f, 0.4f, 1.0f));

ocean_record(buffer, batch_count, batches);
Expand All @@ -94,7 +93,7 @@ void ocean_prepass_record(TracyCGPUContext *gpu_ctx, VkCommandBuffer buffer,
void ocean_pass_record(TracyCGPUContext *gpu_ctx, VkCommandBuffer buffer,
uint32_t batch_count, const DrawBatch *batches) {
TracyCZoneNC(ctx, "Ocean Record", TracyCategoryColorRendering, true);
TracyCVkNamedZone(gpu_ctx, frame_scope, buffer, "Ocean", 3, true);
TracyCVkNamedZone(gpu_ctx, frame_scope, buffer, "Ocean", 2, true);
cmd_begin_label(buffer, "Ocean", f4(0.0f, 0.8f, 0.8f, 1.0f));

ocean_record(buffer, batch_count, batches);
Expand Down Expand Up @@ -976,7 +975,6 @@ void tb_register_ocean_sys(ecs_world_t *ecs, Allocator std_alloc,
ECS_COMPONENT(ecs, RenderTargetSystem);
ECS_COMPONENT(ecs, VisualLoggingSystem);
ECS_COMPONENT(ecs, AudioSystem);
ECS_COMPONENT(ecs, AssetSystem);
ECS_COMPONENT(ecs, OceanSystem);
ECS_COMPONENT(ecs, OceanComponent);
ECS_COMPONENT(ecs, CameraComponent);
Expand Down
Loading

0 comments on commit d29738a

Please sign in to comment.