www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - sokol-d: Static Struct

reply Matheus Catarino <matheus-catarino hotmail.com> writes:
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.
```d
 disable struct State
{
      disable this();
      disable this(this);
     __gshared Display display;
     __gshared Offscreen offscreen;
}
static assert(!__traits(isDisabled, State));
static assert(!__traits(isCopyable, State));
```



![sokol_d_sglcontext](https://github.com/kassane/sokol-d/assets/6756180/0e8391f6-056e-46db-b8e8-021309954ffe)



![sokol_zig_sglcontext](https://github.com/kassane/sokol-d/assets/6756180/576176f8-005a-408d-9ae7-00948161b19f)
Dec 30 2023
next sibling parent reply ryuukk_ <ryuukk.dev gmail.com> writes:
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
Dec 30 2023
parent reply Matheus Catarino <matheus-catarino hotmail.com> writes:
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
Jan 03
parent reply ryuukk_ <ryuukk.dev gmail.com> writes:
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! llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) (/usr/lib/libLLVM-16.so+0xe1f503) (/usr/lib/libLLVM-16.so+0xe1c7bf) const&, llvm::AnalysisManager<llvm::Function>&) (/usr/lib/libLLVM-16.so+0x2ae9556) llvm::AnalysisManager<llvm::Function>::getResultImpl(llvm::AnalysisKey*, llvm::Function&) (/usr/lib/libLLVM-16.so+0xffbe7e) llvm::AssumptionAnalysis::run(llvm::Function&, llvm::AnalysisManager<llvm::Function>&) (/usr/lib/libLLVM-16.so+0x27c9af0) llvm::AnalysisManager<llvm::Function>::getResultImpl(llvm::AnalysisKey*, llvm::Function&) (/usr/lib/libLLVM-16.so+0xffbe7e) llvm::AnalysisManager<llvm::Function>&) (/usr/lib/libLLVM-16.so+0x27daecb) llvm::AnalysisManager<llvm::Function>::getResultImpl(llvm::AnalysisKey*, llvm::Function&) (/usr/lib/libLLVM-16.so+0xffbe7e) llvm::AnalysisManager<llvm::Function>&) (/usr/lib/libLLVM-16.so+0x27b760b) llvm::AnalysisManager<llvm::Function>::getResultImpl(llvm::AnalysisKey*, llvm::Function&) (/usr/lib/libLLVM-16.so+0xffbe7e) llvm::AlwaysInlinerPass::run(llvm::Module&, llvm::AnalysisManager<llvm::Module>&) (/usr/lib/libLLVM-16.so+0x23fc56b) llvm::AnalysisManager<llvm::Module>>::run(llvm::Module&, llvm::AnalysisManager<llvm::Module>&) (/usr/lib/libLLVM-16.so+0xff8466) (/usr/bin/ldc2+0x9c37a2) (/usr/bin/ldc2+0x9c510f) (/usr/bin/ldc2+0xaabc2d) ldc::CodeGenerator::writeAndFreeLLModule(char const*) (/usr/bin/ldc2+0xaa61f8) (/usr/bin/ldc2+0xa6f512) Array<char const*>&) (/usr/bin/ldc2+0x6bf728) _D2rt6dmain212_d_run_main2UAAamPUQgZiZ6runAllMFZv (/usr/bin/ldc2+0xc18a9d) (/usr/lib/libc.so.6+0x27d8a) 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 ```
Jan 03
parent Matheus Catarino <matheus-catarino hotmail.com> writes:
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?
Jan 03
prev sibling next sibling parent reply Renato <renato athaydes.com> writes:
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.
Dec 30 2023
parent Matheus Catarino <matheus-catarino hotmail.com> writes:
 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.
Dec 30 2023
prev sibling parent Matheus Catarino <matheus-catarino hotmail.com> writes:
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. ![](https://github.com/kassane/sokol-d/assets/6756180/198f1b6b-26c0-4dfc-b372-533df1bc77d3)
Feb 02