Skip to content

Commit 87dbfd0

Browse files
authored
Merge pull request #16 from kcbanner/writer_fixup
New writer API, batch body adding bindings
2 parents 1b4717b + 9106492 commit 87dbfd0

File tree

4 files changed

+112
-31
lines changed

4 files changed

+112
-31
lines changed

build.zig.zon

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
.name = .zphysics,
33
.fingerprint = 0x1def6aac00c4909d,
44
.version = "0.2.0-dev",
5-
.minimum_zig_version = "0.14.0",
5+
.minimum_zig_version = "0.15.1",
66
.paths = .{
77
"build.zig",
88
"build.zig.zon",

libs/JoltC/JoltPhysicsC.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,7 @@ FN(toJpc)(JPH::CollisionGroup *in) { assert(in); return reinterpret_cast<JPC_Col
302302

303303
FN(toJph)(const JPC_SubShapeID *in) { assert(in); return reinterpret_cast<const JPH::SubShapeID *>(in); }
304304
FN(toJph)(const JPC_BodyID *in) { assert(in); return reinterpret_cast<const JPH::BodyID *>(in); }
305+
FN(toJph)(JPC_BodyID *in) { assert(in); return reinterpret_cast<JPH::BodyID *>(in); }
305306

306307
FN(toJpc)(const JPH::SubShapeIDCreator *in) { assert(in); return reinterpret_cast<const JPC_SubShapeIDCreator *>(in); }
307308
FN(toJph)(const JPC_SubShapeIDCreator *in) { assert(in); return reinterpret_cast<const JPH::SubShapeIDCreator *>(in); }
@@ -353,6 +354,9 @@ FN(toJph)(const JPC_BodyInterface *in) { assert(in); return reinterpret_cast<con
353354
FN(toJpc)(JPH::BodyInterface *in) { assert(in); return reinterpret_cast<JPC_BodyInterface *>(in); }
354355
FN(toJph)(JPC_BodyInterface *in) { assert(in); return reinterpret_cast<JPH::BodyInterface *>(in); }
355356

357+
FN(toJpc)(JPH::BodyInterface::AddState in) { assert(in); return reinterpret_cast<JPC_BodyInterface_AddState* >(in); }
358+
FN(toJph)(JPC_BodyInterface_AddState* in) { assert(in); return reinterpret_cast<JPH::BodyInterface::AddState >(in); }
359+
356360
FN(toJpc)(const JPH::TransformedShape *in) { assert(in); return reinterpret_cast<const JPC_TransformedShape *>(in); }
357361

358362
FN(toJph)(const JPC_MassProperties *in) { assert(in); return reinterpret_cast<const JPH::MassProperties *>(in); }
@@ -2356,6 +2360,31 @@ JPC_BodyInterface_DestroyBody(JPC_BodyInterface *in_iface, JPC_BodyID in_body_id
23562360
}
23572361
//--------------------------------------------------------------------------------------------------
23582362
JPC_API void
2363+
JPC_BodyInterface_AddBodiesAbort(JPC_BodyInterface *in_iface,
2364+
JPC_BodyID* in_body_ids,
2365+
int in_num_bodies,
2366+
JPC_BodyInterface_AddState* add_state)
2367+
{
2368+
return toJph(in_iface)->AddBodiesAbort(toJph(in_body_ids), in_num_bodies, toJph(add_state));
2369+
}
2370+
//--------------------------------------------------------------------------------------------------
2371+
JPC_API void
2372+
JPC_BodyInterface_AddBodiesFinalize(JPC_BodyInterface *in_iface,
2373+
JPC_BodyID* in_body_ids,
2374+
int in_num_bodies,
2375+
JPC_BodyInterface_AddState* add_state,
2376+
JPC_Activation in_mode)
2377+
{
2378+
return toJph(in_iface)->AddBodiesFinalize(toJph(in_body_ids), in_num_bodies, toJph(add_state), static_cast<JPH::EActivation>(in_mode));
2379+
}
2380+
//--------------------------------------------------------------------------------------------------
2381+
JPC_API JPC_BodyInterface_AddState*
2382+
JPC_BodyInterface_AddBodiesPrepare(JPC_BodyInterface *in_iface, JPC_BodyID* in_body_ids, int in_num_bodies)
2383+
{
2384+
return toJpc(toJph(in_iface)->AddBodiesPrepare(toJph(in_body_ids), in_num_bodies));
2385+
}
2386+
//--------------------------------------------------------------------------------------------------
2387+
JPC_API void
23592388
JPC_BodyInterface_AddBody(JPC_BodyInterface *in_iface, JPC_BodyID in_body_id, JPC_Activation in_mode)
23602389
{
23612390
toJph(in_iface)->AddBody(toJph(in_body_id), static_cast<JPH::EActivation>(in_mode));

libs/JoltC/JoltPhysicsC.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,7 @@ typedef bool (*JPC_AssertFailedFunction)(
330330
typedef struct JPC_TempAllocator JPC_TempAllocator;
331331
typedef struct JPC_JobSystem JPC_JobSystem;
332332
typedef struct JPC_BodyInterface JPC_BodyInterface;
333+
typedef struct JPC_BodyInterface_AddState JPC_BodyInterface_AddState;
333334
typedef struct JPC_BodyLockInterface JPC_BodyLockInterface;
334335
typedef struct JPC_NarrowPhaseQuery JPC_NarrowPhaseQuery;
335336

@@ -2026,6 +2027,22 @@ JPC_BodyInterface_CreateBodyWithID(JPC_BodyInterface *in_iface,
20262027
JPC_API void
20272028
JPC_BodyInterface_DestroyBody(JPC_BodyInterface *in_iface, JPC_BodyID in_body_id);
20282029

2030+
JPC_API void
2031+
JPC_BodyInterface_AddBodiesAbort(JPC_BodyInterface *in_iface,
2032+
JPC_BodyID* in_body_ids,
2033+
int in_num_bodies,
2034+
JPC_BodyInterface_AddState* add_state);
2035+
2036+
JPC_API void
2037+
JPC_BodyInterface_AddBodiesFinalize(JPC_BodyInterface *in_iface,
2038+
JPC_BodyID* in_body_ids,
2039+
int in_num_bodies,
2040+
JPC_BodyInterface_AddState* add_state,
2041+
JPC_Activation in_mode);
2042+
2043+
JPC_API JPC_BodyInterface_AddState*
2044+
JPC_BodyInterface_AddBodiesPrepare(JPC_BodyInterface *in_iface, JPC_BodyID* in_body_ids, int in_num_bodies);
2045+
20292046
JPC_API void
20302047
JPC_BodyInterface_AddBody(JPC_BodyInterface *in_iface, JPC_BodyID in_body_id, JPC_Activation in_mode);
20312048

src/zphysics.zig

Lines changed: 65 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ fn initInterface(comptime T: type, comptime VTableT: type) *const VTableT {
148148
}
149149
} else {
150150
if (field.default_value_ptr) |default_value_ptr| {
151-
@field(vtable, field.name) = @as(*const field.type, @alignCast(@ptrCast(default_value_ptr))).*;
151+
@field(vtable, field.name) = @as(*const field.type, @ptrCast(@alignCast(default_value_ptr))).*;
152152
} else @compileError("non-pointer vtable field " ++ field.name ++ " must have a default value");
153153
}
154154
}
@@ -184,24 +184,24 @@ pub const StreamOut = extern struct {
184184
}
185185
};
186186

187-
pub const AnyWriterStreamOut = extern struct {
187+
pub const WriterStreamOut = extern struct {
188188
stream_out: StreamOut = .init(@This()),
189-
writer: *const std.io.AnyWriter,
189+
writer: *std.Io.Writer,
190190
failed: bool = false,
191191

192-
pub fn init(writer: *const std.io.AnyWriter) AnyWriterStreamOut {
192+
pub fn init(writer: *std.Io.Writer) WriterStreamOut {
193193
return .{ .writer = writer };
194194
}
195195

196196
pub fn writeBytes(stream_out: *StreamOut, data: [*]const u8, num_bytes: usize) callconv(.c) void {
197-
const self: *AnyWriterStreamOut = @alignCast(@fieldParentPtr("stream_out", stream_out));
197+
const self: *WriterStreamOut = @alignCast(@fieldParentPtr("stream_out", stream_out));
198198
self.writer.writeAll(data[0..num_bytes]) catch {
199199
self.failed = true;
200200
};
201201
}
202202

203203
pub fn isFailed(stream_out: *StreamOut) callconv(.c) bool {
204-
const self: *AnyWriterStreamOut = @alignCast(@fieldParentPtr("stream_out", stream_out));
204+
const self: *WriterStreamOut = @alignCast(@fieldParentPtr("stream_out", stream_out));
205205
return self.failed;
206206
}
207207
};
@@ -225,19 +225,19 @@ pub const StreamIn = extern struct {
225225
}
226226
};
227227

228-
pub const AnyReaderStreamIn = extern struct {
228+
pub const ReaderStreamIn = extern struct {
229229
stream_in: StreamIn = .init(@This()),
230-
reader: *const std.io.AnyReader,
230+
reader: *std.Io.Reader,
231231
failed: bool = false,
232232
eof: bool = false,
233233

234-
pub fn init(reader: *const std.io.AnyReader) AnyReaderStreamIn {
234+
pub fn init(reader: *std.Io.Reader) ReaderStreamIn {
235235
return .{ .reader = reader };
236236
}
237237

238238
pub fn readBytes(stream_in: *StreamIn, data: [*]u8, num_bytes: usize) callconv(.c) void {
239239
const self: *@This() = @alignCast(@fieldParentPtr("stream_in", stream_in));
240-
self.reader.readNoEof(data[0..num_bytes]) catch |err| switch (err) {
240+
self.reader.readSliceAll(data[0..num_bytes]) catch |err| switch (err) {
241241
error.EndOfStream => self.eof = true,
242242
else => self.failed = true,
243243
};
@@ -1509,8 +1509,12 @@ pub const PhysicsSystem = opaque {
15091509
c.JPC_PhysicsSystem_DrawConstraintReferenceFrame(@ptrCast(physics_system));
15101510
}
15111511

1512-
pub fn getBodyIds(physics_system: *const PhysicsSystem, body_ids: *std.ArrayList(BodyId)) !void {
1513-
try body_ids.ensureTotalCapacityPrecise(physics_system.getMaxBodies());
1512+
pub fn getBodyIds(
1513+
physics_system: *const PhysicsSystem,
1514+
allocator: std.mem.Allocator,
1515+
body_ids: *std.ArrayList(BodyId),
1516+
) !void {
1517+
try body_ids.ensureTotalCapacityPrecise(allocator, physics_system.getMaxBodies());
15141518
var num_body_ids: u32 = 0;
15151519
c.JPC_PhysicsSystem_GetBodyIDs(
15161520
@as(*const c.JPC_PhysicsSystem, @ptrCast(physics_system)),
@@ -1521,8 +1525,12 @@ pub const PhysicsSystem = opaque {
15211525
body_ids.items.len = num_body_ids;
15221526
}
15231527

1524-
pub fn getActiveBodyIds(physics_system: *const PhysicsSystem, body_ids: *std.ArrayList(BodyId)) !void {
1525-
try body_ids.ensureTotalCapacityPrecise(physics_system.getMaxBodies());
1528+
pub fn getActiveBodyIds(
1529+
physics_system: *const PhysicsSystem,
1530+
allocator: std.mem.Allocator,
1531+
body_ids: *std.ArrayList(BodyId),
1532+
) !void {
1533+
try body_ids.ensureTotalCapacityPrecise(allocator, physics_system.getMaxBodies());
15261534
var num_body_ids: u32 = 0;
15271535
c.JPC_PhysicsSystem_GetActiveBodyIDs(
15281536
@as(*const c.JPC_PhysicsSystem, @ptrCast(physics_system)),
@@ -1618,6 +1626,8 @@ pub const BodyLockWrite = extern struct {
16181626
//
16191627
//--------------------------------------------------------------------------------------------------
16201628
pub const BodyInterface = opaque {
1629+
pub const AddState = *opaque {};
1630+
16211631
pub fn createBody(body_iface: *BodyInterface, settings: BodyCreationSettings) !*Body {
16221632
const body = c.JPC_BodyInterface_CreateBody(
16231633
@as(*c.JPC_BodyInterface, @ptrCast(body_iface)),
@@ -1646,6 +1656,33 @@ pub const BodyInterface = opaque {
16461656
);
16471657
}
16481658

1659+
pub fn addBodiesAbort(body_iface: *BodyInterface, body_ids: []BodyId, add_state: AddState) void {
1660+
c.JPC_BodyInterface_AddBodiesAbort(
1661+
@as(*c.JPC_BodyInterface, @ptrCast(body_iface)),
1662+
@ptrCast(body_ids.ptr),
1663+
@intCast(body_ids.len),
1664+
@ptrCast(add_state),
1665+
);
1666+
}
1667+
1668+
pub fn addBodiesFinalize(body_iface: *BodyInterface, body_ids: []BodyId, add_state: AddState, mode: Activation) void {
1669+
c.JPC_BodyInterface_AddBodiesFinalize(
1670+
@as(*c.JPC_BodyInterface, @ptrCast(body_iface)),
1671+
@ptrCast(body_ids.ptr),
1672+
@intCast(body_ids.len),
1673+
@ptrCast(add_state),
1674+
@intFromEnum(mode),
1675+
);
1676+
}
1677+
1678+
pub fn addBodiesPrepare(body_iface: *BodyInterface, body_ids: []BodyId) AddState {
1679+
return @ptrCast(c.JPC_BodyInterface_AddBodiesPrepare(
1680+
@as(*c.JPC_BodyInterface, @ptrCast(body_iface)),
1681+
@ptrCast(body_ids.ptr),
1682+
@intCast(body_ids.len),
1683+
));
1684+
}
1685+
16491686
pub fn addBody(body_iface: *BodyInterface, body_id: BodyId, mode: Activation) void {
16501687
c.JPC_BodyInterface_AddBody(
16511688
@as(*c.JPC_BodyInterface, @ptrCast(body_iface)),
@@ -3390,7 +3427,7 @@ pub const Shape = opaque {
33903427

33913428
pub fn getLocalBounds(shape: *const Shape) AABox {
33923429
const aabox = c.JPC_Shape_GetLocalBounds(@ptrCast(shape));
3393-
return @as(*AABox, @constCast(@ptrCast(&aabox))).*;
3430+
return @as(*AABox, @ptrCast(@constCast(&aabox))).*;
33943431
}
33953432

33963433
pub fn getSurfaceNormal(shape: *const Shape, sub_shape_id: SubShapeId, local_pos: [3]f32) [3]f32 {
@@ -4356,18 +4393,18 @@ test "zphysics.body.basic" {
43564393
}
43574394

43584395
{
4359-
var body_ids = std.ArrayList(BodyId).init(std.testing.allocator);
4360-
defer body_ids.deinit();
4361-
try physics_system.getBodyIds(&body_ids);
4396+
var body_ids: std.ArrayList(BodyId) = .empty;
4397+
defer body_ids.deinit(std.testing.allocator);
4398+
try physics_system.getBodyIds(std.testing.allocator, &body_ids);
43624399
try expect(body_ids.items.len == 1);
43634400
try expect(body_ids.capacity >= physics_system.getMaxBodies());
43644401
try expect(body_ids.items[0] == body_id);
43654402
}
43664403

43674404
{
4368-
var body_ids = std.ArrayList(BodyId).init(std.testing.allocator);
4369-
defer body_ids.deinit();
4370-
try physics_system.getActiveBodyIds(&body_ids);
4405+
var body_ids: std.ArrayList(BodyId) = .empty;
4406+
defer body_ids.deinit(std.testing.allocator);
4407+
try physics_system.getActiveBodyIds(std.testing.allocator, &body_ids);
43714408
try expect(body_ids.items.len == 0);
43724409
try expect(body_ids.capacity >= physics_system.getMaxBodies());
43734410
}
@@ -4599,20 +4636,18 @@ test "zphysics.serialization" {
45994636
const shape = try shape_settings.asShapeSettings().createShape();
46004637
defer shape.release();
46014638

4602-
var buf: std.ArrayListUnmanaged(u8) = .{};
4603-
defer buf.deinit(std.testing.allocator);
4639+
var buf: std.Io.Writer.Allocating = .init(std.testing.allocator);
4640+
defer buf.deinit();
46044641

46054642
{
4606-
const writer = buf.writer(std.testing.allocator).any();
4607-
var stream_out = AnyWriterStreamOut.init(&writer);
4643+
var stream_out = WriterStreamOut.init(&buf.writer);
46084644
shape.saveBinaryState(@ptrCast(&stream_out));
4609-
try std.testing.expectEqual(1 + 8 + 4 + 12 + 4, buf.items.len);
4645+
try std.testing.expectEqual(1 + 8 + 4 + 12 + 4, buf.written().len);
46104646
}
46114647

46124648
{
4613-
var stream = std.io.fixedBufferStream(buf.items);
4614-
const reader = stream.reader().any();
4615-
var stream_in = AnyReaderStreamIn.init(&reader);
4649+
var reader: std.Io.Reader = .fixed(buf.written());
4650+
var stream_in = ReaderStreamIn.init(&reader);
46164651
const shape_restored = try Shape.restoreFromBinaryState(@ptrCast(&stream_in));
46174652
defer shape_restored.release();
46184653

@@ -4795,7 +4830,7 @@ const test_cb1 = struct {
47954830
batch: *anyopaque,
47964831
) callconv(.c) void {
47974832
_ = self;
4798-
const primitive: *MyRenderPrimitive = @alignCast(@ptrCast(batch));
4833+
const primitive: *MyRenderPrimitive = @ptrCast(@alignCast(batch));
47994834
primitive.allocated = false;
48004835
}
48014836
fn drawGeometry(

0 commit comments

Comments
 (0)