Skip to content

Commit 93f2918

Browse files
committed
Version
- Make all vtable functions non-optional, to improve error messages when `pub` is missing - Fix up test output when PRINT_OUTPUT is enabled - Move defines to a common build function - Add -Dverbose to set PRINT_OUTPUT
1 parent 9ab6b83 commit 93f2918

File tree

4 files changed

+51
-33
lines changed

4 files changed

+51
-33
lines changed

build.zig

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,16 @@
11
const std = @import("std");
22

3+
fn addMacros(module: *std.Build.Module, options: anytype) void {
4+
if (options.enable_cross_platform_determinism)
5+
module.addCMacro("JPH_CROSS_PLATFORM_DETERMINISTIC", "");
6+
if (options.enable_debug_renderer)
7+
module.addCMacro("JPH_DEBUG_RENDERER", "");
8+
if (options.use_double_precision)
9+
module.addCMacro("JPH_DOUBLE_PRECISION", "");
10+
if (options.enable_asserts)
11+
module.addCMacro("JPH_ENABLE_ASSERTS", "");
12+
}
13+
314
pub fn build(b: *std.Build) void {
415
const optimize = b.standardOptimizeOption(.{});
516
const target = b.standardTargetOptions(.{});
@@ -84,15 +95,12 @@ pub fn build(b: *std.Build) void {
8495
const src_dir = "libs/Jolt";
8596
const c_flags = &.{
8697
"-std=c++17",
87-
if (options.enable_cross_platform_determinism) "-DJPH_CROSS_PLATFORM_DETERMINISTIC" else "",
88-
if (options.enable_debug_renderer) "-DJPH_DEBUG_RENDERER" else "",
89-
if (options.use_double_precision) "-DJPH_DOUBLE_PRECISION" else "",
90-
if (options.enable_asserts) "-DJPH_ENABLE_ASSERTS" else "",
9198
if (options.no_exceptions) "-fno-exceptions" else "",
9299
"-fno-access-control",
93100
"-fno-sanitize=undefined",
94101
};
95102

103+
addMacros(joltc.root_module, options);
96104
joltc.addCSourceFiles(.{
97105
.files = &.{
98106
"libs/JoltC/JoltPhysicsC.cpp",
@@ -258,17 +266,17 @@ pub fn build(b: *std.Build) void {
258266
tests.want_lto = false;
259267
}
260268

269+
addMacros(tests.root_module, options);
261270
tests.addCSourceFile(.{
262271
.file = b.path("libs/JoltC/JoltPhysicsC_Tests.c"),
263272
.flags = &.{
264-
if (options.enable_cross_platform_determinism) "-DJPH_CROSS_PLATFORM_DETERMINISTIC" else "",
265-
if (options.enable_debug_renderer) "-DJPH_DEBUG_RENDERER" else "",
266-
if (options.use_double_precision) "-DJPH_DOUBLE_PRECISION" else "",
267-
if (options.enable_asserts) "-DJPH_ENABLE_ASSERTS" else "",
268273
"-fno-sanitize=undefined",
269274
},
270275
});
271276

277+
if (b.option(bool, "verbose", "Print verbose test debug output to stderr") orelse false)
278+
tests.root_module.addCMacro("PRINT_OUTPUT", "");
279+
272280
tests.root_module.addImport("zphysics_options", options_module);
273281
tests.addIncludePath(b.path("libs/JoltC"));
274282
tests.linkLibrary(joltc);

libs/JoltC/JoltPhysicsC.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -739,7 +739,7 @@ class DebugRendererImpl final : public JPH::DebugRenderer
739739
BatchImpl(const JPC_DebugRenderer_Primitive *c_primitive) : RenderPrimitive(c_primitive) {}
740740
~BatchImpl()
741741
{
742-
if (sInstance && sInstance->c_renderer->vtbl->DestroyTriangleBatch)
742+
if (sInstance)
743743
{
744744
sInstance->c_renderer->vtbl->DestroyTriangleBatch(sInstance->c_renderer, (void*)c_primitive);
745745
}
@@ -790,6 +790,7 @@ class DebugRendererImpl final : public JPH::DebugRenderer
790790
valid &= (c_renderer->vtbl->DrawTriangle != nullptr);
791791
valid &= (c_renderer->vtbl->CreateTriangleBatch != nullptr);
792792
valid &= (c_renderer->vtbl->CreateTriangleBatchIndexed != nullptr);
793+
valid &= (c_renderer->vtbl->DestroyTriangleBatch != nullptr);
793794
valid &= (c_renderer->vtbl->DrawGeometry != nullptr);
794795
valid &= (c_renderer->vtbl->DrawText3D != nullptr);
795796
return valid ? JPC_DEBUGRENDERER_SUCCESS : JPC_DEBUGRENDERER_INCOMPLETE_IMPL;

libs/JoltC/JoltPhysicsC_Tests.c

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55
#include <stdio.h>
66
#include <string.h>
77

8-
//#define PRINT_OUTPUT
9-
108
// Object layers
119
#define NUM_OBJ_LAYERS 2
1210
#define OBJ_LAYER_NON_MOVING 0
@@ -97,9 +95,9 @@ MyContactListener_OnContactValidate(void *in_self,
9795
"\tOnContactValidate(): in_base_offset (%f, %f, %f)\n",
9896
in_base_offset[0], in_base_offset[1], in_base_offset[2]);
9997
fprintf(stderr, "\tOnContactValidate(): penetration_depth (%f)\n", in_collision_result->penetration_depth);
100-
fprintf(stderr, "\tOnContactValidate(): shape1_sub_shape_id (%d)\n", in_collision_result->shape1_sub_shape_id);
101-
fprintf(stderr, "\tOnContactValidate(): shape2_sub_shape_id (%d)\n", in_collision_result->shape2_sub_shape_id);
102-
fprintf(stderr, "\tOnContactValidate(): body2_id (%d)\n", in_collision_result->body2_id);
98+
fprintf(stderr, "\tOnContactValidate(): shape1_sub_shape_id (%d)\n", in_collision_result->shape1_sub_shape_id.id);
99+
fprintf(stderr, "\tOnContactValidate(): shape2_sub_shape_id (%d)\n", in_collision_result->shape2_sub_shape_id.id);
100+
fprintf(stderr, "\tOnContactValidate(): body2_id (%d)\n", in_collision_result->body2_id.id);
103101
#endif
104102
return JPC_VALIDATE_RESULT_ACCEPT_ALL_CONTACTS;
105103
}
@@ -214,7 +212,7 @@ MyDebugRenderer_CreateTriangleBatch(void *in_self,
214212
(JPC_DebugRenderer_Primitive *)prim
215213
);
216214
#ifdef PRINT_OUTPUT
217-
fprintf(stderr, "\tDebugRenderer: CreateTriangleBatch called. Created primitive %x\n", prim);
215+
fprintf(stderr, "\tDebugRenderer: CreateTriangleBatch called. Created primitive %llx\n", (uint64_t)prim);
218216
#endif
219217
return batch;
220218
}
@@ -232,11 +230,21 @@ MyDebugRenderer_CreateTriangleBatchIndexed(void *in_self,
232230
(JPC_DebugRenderer_Primitive *)prim
233231
);
234232
#ifdef PRINT_OUTPUT
235-
fprintf(stderr, "\tDebugRenderer: CreateTriangleBatchIndexed called. Created primitive %x\n", prim);
233+
fprintf(stderr, "\tDebugRenderer: CreateTriangleBatchIndexed called. Created primitive %llx\n", (uint64_t)prim);
236234
#endif
237235
return batch;
238236
}
239237

238+
static void
239+
MyDebugRenderer_DestroyTriangleBatch(void *in_self, void *in_batch)
240+
{
241+
struct MyDebugRenderer *self = (struct MyDebugRenderer*) in_self;
242+
struct MyRenderPrimitive *prim = (MyRenderPrimitive*)in_batch;
243+
#ifdef PRINT_OUTPUT
244+
fprintf(stderr, "\tDebugRenderer: DestroyTriangleBatch called. Destroyed primitive %llx\n", (uint64_t)prim);
245+
#endif
246+
}
247+
240248
static void
241249
MyDebugRenderer_DrawGeometry(void *in_self,
242250
const JPC_RMatrix *inMatrix,
@@ -252,7 +260,7 @@ MyDebugRenderer_DrawGeometry(void *in_self,
252260
JPC_DebugRenderer_TriangleBatch *batch = in_geometry->LODs[0].batch;
253261
const JPC_DebugRenderer_Primitive *prim = JPC_DebugRenderer_TriangleBatch_GetPrimitive(batch);
254262
#ifdef PRINT_OUTPUT
255-
fprintf(stderr, "\tDebugRenderer: DrawGeometry called for %x\n", prim);
263+
fprintf(stderr, "\tDebugRenderer: DrawGeometry called for %llx\n", (uint64_t)prim);
256264
#endif
257265
}
258266

@@ -277,6 +285,7 @@ MyDebugRenderer_Init(void)
277285
.DrawTriangle = MyDebugRenderer_DrawTriangle,
278286
.CreateTriangleBatch = MyDebugRenderer_CreateTriangleBatch,
279287
.CreateTriangleBatchIndexed = MyDebugRenderer_CreateTriangleBatchIndexed,
288+
.DestroyTriangleBatch = MyDebugRenderer_DestroyTriangleBatch,
280289
.DrawGeometry = MyDebugRenderer_DrawGeometry,
281290
.DrawText3D = MyDebugRenderer_DrawText3D,
282291
};

src/zphysics.zig

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -131,8 +131,11 @@ fn initInterface(comptime T: type, comptime VTableT: type) *const VTableT {
131131
};
132132

133133
if (opt_fn_info) |fn_info| {
134+
if (is_opt)
135+
@compileError("vtable function pointer " ++ field.name ++ " must be non-optional");
136+
134137
if (!fn_info.calling_convention.eql(std.builtin.CallingConvention.c))
135-
@compileError("vtable function " ++ field.name ++ " must be callconv(.c)");
138+
@compileError("vtable function pointer " ++ field.name ++ " must be callconv(.c)");
136139

137140
if (@hasDecl(T, field.name)) {
138141
@field(vtable, field.name) = &@field(T, field.name);
@@ -512,34 +515,31 @@ pub const ContactListener = extern struct {
512515

513516
pub const VTable = extern struct {
514517
__header: VTableHeader = .{},
515-
onContactValidate: ?*const fn (
518+
onContactValidate: *const fn (
516519
self: *ContactListener,
517520
body1: *const Body,
518521
body2: *const Body,
519522
base_offset: *const [3]Real,
520523
collision_result: *const CollideShapeResult,
521-
) callconv(.c) ValidateResult = null,
522-
523-
onContactAdded: ?*const fn (
524+
) callconv(.c) ValidateResult,
525+
onContactAdded: *const fn (
524526
self: *ContactListener,
525527
body1: *const Body,
526528
body2: *const Body,
527529
manifold: *const ContactManifold,
528530
settings: *ContactSettings,
529-
) callconv(.c) void = null,
530-
531-
onContactPersisted: ?*const fn (
531+
) callconv(.c) void,
532+
onContactPersisted: *const fn (
532533
self: *ContactListener,
533534
body1: *const Body,
534535
body2: *const Body,
535536
manifold: *const ContactManifold,
536537
settings: *ContactSettings,
537-
) callconv(.c) void = null,
538-
539-
onContactRemoved: ?*const fn (
538+
) callconv(.c) void,
539+
onContactRemoved: *const fn (
540540
self: *ContactListener,
541541
sub_shape_pair: *const SubShapeIdPair,
542-
) callconv(.c) void = null,
542+
) callconv(.c) void,
543543
};
544544

545545
comptime {
@@ -1086,13 +1086,13 @@ pub const DebugRenderer = if (!debug_renderer_enabled) extern struct {} else ext
10861086
indices: [*]u32,
10871087
index_count: u32,
10881088
) callconv(.c) *TriangleBatch,
1089-
/// If this function is available, it will be called when the reference count
1090-
/// of the TriangleBatch returned by the above functions. `batch` will be the
1089+
/// Will be called when the reference count of the TriangleBatch
1090+
/// returned by the above functions reaches zero. `batch` will be the
10911091
/// value that was passed to `DebugRenderer.createTriangleBatch`.
1092-
destroyTriangleBatch: ?*const fn (
1092+
destroyTriangleBatch: *const fn (
10931093
self: *T,
10941094
batch: *anyopaque,
1095-
) callconv(.c) void = null,
1095+
) callconv(.c) void,
10961096
drawGeometry: *const fn (
10971097
self: *T,
10981098
model_matrix: *const RMatrix,

0 commit comments

Comments
 (0)