Thread overview
sokol-d: Static Struct
Dec 30
ryuukk_
Jan 03
ryuukk_
Dec 30
Renato
December 30

Hi everyone. 👋

Currently I'm working on D binding for sokol project (truly a dual bindgen [sokol-tools, sokol-header]) which could be merged into the upstream project.

Up to now, my "ideal" configuration has been to run all the ported examples (from other supported bindings) on Drt or betterC provided @safe has DIP1000 enabled (not obligatory).

Except for the "clear" example (no have structs) works, the rest of the examples have partial execution, without 2D/3D animations, like the original ported examples.

e.g.:
https://github.com/kassane/sokol-d/blob/main/src/examples/sgl_context.d

Based on the articles in the official documentation, I tried to use __gshared (no @safe) to attribute both the display and offscreen structures inside state, but it didn't work.

@disable struct State
{
    @disable this();
    @disable this(this);
    __gshared Display display;
    __gshared Offscreen offscreen;
}
static assert(!__traits(isDisabled, State));
static assert(!__traits(isCopyable, State));

Reproduce

sokol_d_sglcontext

Expected

sokol_zig_sglcontext

December 30

Use https://renderdoc.org/ and check and compare frames for both your working and non-working example

That'll give you an idea at what could be wrong

I suspect you have a typo in one of your definition

I'll try to take a look later

December 30

On Saturday, 30 December 2023 at 19:27:08 UTC, Matheus Catarino wrote:

>

Hi everyone. 👋

Currently I'm working on D binding for sokol project (truly a dual bindgen [sokol-tools, sokol-header]) which could be merged into the upstream project.

Sorry if I sidetrack the discussion, but I was amazed by the Zig build file in that project: https://github.com/kassane/sokol-d/blob/main/build.zig

Do you have more information about how to integrate C and D using Zig as the build tool?? That sounds very interesting.

December 30
>

Use https://renderdoc.org/ and check and compare frames for both your working and non-working example

Nice!! I'll try.

>

That'll give you an idea at what could be wrong

I suspect you have a typo in one of your definition

I don't doubt it. I'm experiencing D in just a week. (Newbie)😅

On Saturday, 30 December 2023 at 20:56:39 UTC, Renato wrote:

>

Sorry if I sidetrack the discussion, but I was amazed by the Zig build file in that project: https://github.com/kassane/sokol-d/blob/main/build.zig

Do you have more information about how to integrate C and D using Zig as the build tool?? That sounds very interesting.

Hi Renato,

I'm glad you liked it. This is an experiment, as I haven't adapted to the dub yet.

Zig's goal at this point is to share libsokol configurations and replicate in the D compiler (currently, ldc2 only). If you want to see the result, just run zig build clear --verbose.

<Zig build> -> build(exe) -> build libsokol -> (optional) zigcc-wrapper -> build D example (using libsokol config) -> switch (cc/linker system or zigcc) -> binary (exe/lib)

I added some comments to build.zig explaining each point. If you have any questions, you can ask in the repo.
I saw that you are a MacOS user, I suggest you see issue/1 [closed] focused on zigcc wrapper.

TODO: It is intended to add dub support and will continue with Zig build-system support, including zig-pkg(v0.12.0/master - git url) for downloading and integrating C and D packages.

January 03

On Saturday, 30 December 2023 at 20:20:50 UTC, ryuukk_ wrote:

>

I suspect you have a typo in one of your definition

I debugged some existing bindings, and despite any user-level errors (via code) there's some conflict between ABIs.

In fact, it's mentioned by sokol's author below:

>

I've been seeing weird platform-specific ABI related issues in language bindings where the struct params were corrupted on their way to the other language (for instance in Zig on Intel Macs).

https://github.com/kassane/sokol-d/issues/5#issuecomment-1875665075

January 03

On Wednesday, 3 January 2024 at 17:50:19 UTC, Matheus Catarino wrote:

>

On Saturday, 30 December 2023 at 20:20:50 UTC, ryuukk_ wrote:

>

I suspect you have a typo in one of your definition

I debugged some existing bindings, and despite any user-level errors (via code) there's some conflict between ABIs.

In fact, it's mentioned by sokol's author below:

>

I've been seeing weird platform-specific ABI related issues in language bindings where the struct params were corrupted on their way to the other language (for instance in Zig on Intel Macs).

https://github.com/kassane/sokol-d/issues/5#issuecomment-1875665075

I managed to compile your project, sgl_context is working for me, it renders and roate

I had to remove this from your build script tho:

try cmds.append(b.fmt("--mcpu={s}", .{lib.target.getCpuModel().name}));

Otherwise it crashes

ryuukk@ark:~/dev/tmp/sokol-d (main)
$ zig build sgl_context && ./zig-out/bin/sgl_context
'x86_64' is not a recognized processor for this target (ignoring processor)
'x86_64' is not a recognized processor for this target (ignoring processor)
/home/ryuukk/dev/tmp/sokol-d/src/examples/sgl_context.d(36,20): `display` is thread local
/home/ryuukk/dev/tmp/sokol-d/src/examples/sgl_context.d(37,22): `offscreen` is thread local
'x86_64' is not a recognized processor for this target (ignoring processor)
'x86_64' is not a recognized processor for this target (ignoring processor)
'x86_64' is not a recognized processor for this target (ignoring processor)
'x86_64' is not a recognized processor for this target (ignoring processor)
LLVM ERROR: 64-bit code requested on a subtarget that doesn't support it!
 #0 0x00007f8df5c1f503 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) (/usr/lib/libLLVM-16.so+0xe1f503)
 #1 0x00007f8df5c1c7bf llvm::sys::RunSignalHandlers() (/usr/lib/libLLVM-16.so+0xe1c7bf)
 #2 0x00007f8df5c1c90d (/usr/lib/libLLVM-16.so+0xe1c90d)
 #3 0x00007f8df485c710 (/usr/lib/libc.so.6+0x3e710)
 #4 0x00007f8df48ac83c (/usr/lib/libc.so.6+0x8e83c)
 #5 0x00007f8df485c668 gsignal (/usr/lib/libc.so.6+0x3e668)
 #6 0x00007f8df48444b8 abort (/usr/lib/libc.so.6+0x264b8)
 #7 0x00007f8df594d25f (/usr/lib/libLLVM-16.so+0xb4d25f)
 #8 0x00007f8df5b1f19e (/usr/lib/libLLVM-16.so+0xd1f19e)
 #9 0x00007f8df999688c (/usr/lib/libLLVM-16.so+0x4b9688c)
#10 0x00007f8df99970b9 (/usr/lib/libLLVM-16.so+0x4b970b9)
#11 0x00007f8df999eb48 (/usr/lib/libLLVM-16.so+0x4b9eb48)
#12 0x00007f8df999ee95 (/usr/lib/libLLVM-16.so+0x4b9ee95)
#13 0x00007f8df822d39a (/usr/lib/libLLVM-16.so+0x342d39a)
#14 0x00007f8df78e9556 llvm::TargetIRAnalysis::run(llvm::Function const&, llvm::AnalysisManager<llvm::Function>&) (/usr/lib/libLLVM-16.so+0x2ae9556)
#15 0x00007f8df6a42b48 (/usr/lib/libLLVM-16.so+0x1c42b48)
#16 0x00007f8df5dfbe7e llvm::AnalysisManager<llvm::Function>::getResultImpl(llvm::AnalysisKey*, llvm::Function&) (/usr/lib/libLLVM-16.so+0xffbe7e)
#17 0x00007f8df75c9af0 llvm::AssumptionAnalysis::run(llvm::Function&, llvm::AnalysisManager<llvm::Function>&) (/usr/lib/libLLVM-16.so+0x27c9af0)
#18 0x00007f8df6a839d2 (/usr/lib/libLLVM-16.so+0x1c839d2)
#19 0x00007f8df5dfbe7e llvm::AnalysisManager<llvm::Function>::getResultImpl(llvm::AnalysisKey*, llvm::Function&) (/usr/lib/libLLVM-16.so+0xffbe7e)
#20 0x00007f8df75daecb llvm::BasicAA::run(llvm::Function&, llvm::AnalysisManager<llvm::Function>&) (/usr/lib/libLLVM-16.so+0x27daecb)
#21 0x00007f8df9b7086f (/usr/lib/libLLVM-16.so+0x4d7086f)
#22 0x00007f8df5dfbe7e llvm::AnalysisManager<llvm::Function>::getResultImpl(llvm::AnalysisKey*, llvm::Function&) (/usr/lib/libLLVM-16.so+0xffbe7e)
#23 0x00007f8df9bd352c (/usr/lib/libLLVM-16.so+0x4dd352c)
#24 0x00007f8df75b760b llvm::AAManager::run(llvm::Function&, llvm::AnalysisManager<llvm::Function>&) (/usr/lib/libLLVM-16.so+0x27b760b)
#25 0x00007f8df7957b39 (/usr/lib/libLLVM-16.so+0x2b57b39)
#26 0x00007f8df5dfbe7e llvm::AnalysisManager<llvm::Function>::getResultImpl(llvm::AnalysisKey*, llvm::Function&) (/usr/lib/libLLVM-16.so+0xffbe7e)
#27 0x00007f8df71fc56b llvm::AlwaysInlinerPass::run(llvm::Module&, llvm::AnalysisManager<llvm::Module>&) (/usr/lib/libLLVM-16.so+0x23fc56b)
#28 0x00007f8df9b727f6 (/usr/lib/libLLVM-16.so+0x4d727f6)
#29 0x00007f8df5df8466 llvm::PassManager<llvm::Module, llvm::AnalysisManager<llvm::Module>>::run(llvm::Module&, llvm::AnalysisManager<llvm::Module>&) (/usr/lib/libLLVM-16.so+0xff8466)
#30 0x0000558a65baa7a2 runOptimizationPasses(llvm::Module*) (/usr/bin/ldc2+0x9c37a2)
#31 0x0000558a65bac10f ldc_optimize_module(llvm::Module*) (/usr/bin/ldc2+0x9c510f)
#32 0x0000558a65c92c2d writeModule(llvm::Module*, char const*) (/usr/bin/ldc2+0xaabc2d)
#33 0x0000558a65c8d1f8 ldc::CodeGenerator::writeAndFreeLLModule(char const*) (/usr/bin/ldc2+0xaa61f8)
#34 0x0000558a65c56512 codegenModules(Array<Module*>&) (/usr/bin/ldc2+0xa6f512)
#35 0x0000558a658a6728 mars_mainBody(Param&, Array<char const*>&, Array<char const*>&) (/usr/bin/ldc2+0x6bf728)
#36 0x0000558a65c547c7 cppmain() (/usr/bin/ldc2+0xa6d7c7)
#37 0x0000558a65dffa9d _D2rt6dmain212_d_run_main2UAAamPUQgZiZ6runAllMFZv (/usr/bin/ldc2+0xc18a9d)
#38 0x0000558a65dff8b7 _d_run_main2 (/usr/bin/ldc2+0xc188b7)
#39 0x0000558a65dff70e _d_run_main (/usr/bin/ldc2+0xc1870e)
#40 0x0000558a65691be6 main (/usr/bin/ldc2+0x4aabe6)
#41 0x00007f8df4845cd0 (/usr/lib/libc.so.6+0x27cd0)
#42 0x00007f8df4845d8a __libc_start_main (/usr/lib/libc.so.6+0x27d8a)
#43 0x0000558a656946e5 _start (/usr/bin/ldc2+0x4ad6e5)
Error: Error executing /usr/bin/ldc2: Aborted (core dumped)
ldc2: error: the following command exited with error code 1:
/usr/bin/ldmd2 -wi -preview=dip1000 -d-debug --gc -g --O0 -vgc -vtls -verrors=context --vcolumns -od=/home/ryuukk/dev/tmp/sokol-d/zig-cache/o/b2e3e271a78ddb5e4d1fd8939558a0e1 --oq --Hkeep-all-bodies -I/home/ryuukk/dev/tmp/sokol-d/src/sokol -i /home/ryuukk/dev/tmp/sokol-d/src/sokol/app.d /home/ryuukk/dev/tmp/sokol-d/src/sokol/audio.d /home/ryuukk/dev/tmp/sokol-d/src/sokol/gl.d /home/ryuukk/dev/tmp/sokol-d/src/sokol/gfx.d /home/ryuukk/dev/tmp/sokol-d/src/sokol/glue.d /home/ryuukk/dev/tmp/sokol-d/src/sokol/log.d /home/ryuukk/dev/tmp/sokol-d/src/sokol/shape.d /home/ryuukk/dev/tmp/sokol-d/src/sokol/time.d /home/ryuukk/dev/tmp/sokol-d/src/sokol/debugtext.d /home/ryuukk/dev/tmp/sokol-d/src/examples/sgl_context.d -L-L/home/ryuukk/dev/tmp/sokol-d/zig-out/lib -L-lsokol -L-lasound -L-lGL -L-lX11 -L-lXi -L-lXcursor --Xcc=-DIMPL --Xcc=-DSOKOL_GLCORE33 --Xcc=-DSOKOL_DISABLE_WAYLAND -L--no-as-needed --mcpu=x86_64 --of=/home/ryuukk/dev/tmp/sokol-d/zig-out/bin/sgl_context
Build Summary: 3/5 steps succeeded; 1 failed (disable with --summary none)
sgl_context transitive failure
└─ ldc2 failure
error: the following build command failed with exit code 1:
/home/ryuukk/dev/tmp/sokol-d/zig-cache/o/3fee843303e331e650973da56e226ce7/build /usr/bin/zig /home/ryuukk/dev/tmp/sokol-d /home/ryuukk/dev/tmp/sokol-d/zig-cache /home/ryuukk/.cache/zig sgl_context

January 03

On Wednesday, 3 January 2024 at 18:57:44 UTC, ryuukk_ wrote:

>

I managed to compile your project, sgl_context is working for me, it renders and roate

[...]

Wow! Cube and blend work, too?

And on my machine it doesn't rotate, just like I initially posted.

>

I had to remove this from your build script tho:

try cmds.append(b.fmt("--mcpu={s}", .{lib.target.getCpuModel().name}));

[...]

Please! This deserves opening an issue about it. Because Zig is normally set native build by default.
What version of LDC is used? 1.35?

February 02

On Saturday, 30 December 2023 at 19:27:08 UTC, Matheus Catarino wrote:

>

Hi everyone. 👋

Currently I'm working on D binding for sokol project (truly a dual bindgen [sokol-tools, sokol-header]) which could be merged into the upstream project.

Up to now, my "ideal" configuration has been to run all the ported examples (from other supported bindings) on Drt or betterC provided @safe has DIP1000 enabled (not obligatory).

Except for the "clear" example (no have structs) works, the rest of the examples have partial execution, without 2D/3D animations, like the original ported examples.

e.g.:
https://github.com/kassane/sokol-d/blob/main/src/examples/sgl_context.d

Finally fixed!! Works in, linux/macos/windows/wasm32(need emsdk)

I was having trouble with the floating/double init (nan) and solved this issue by giving default values to structs fields.