floooh / sokol-nim

nim bindings for https://github.com/floooh/sokol
MIT License
76 stars 14 forks source link

Updated README.md and added a few examples #8

Closed garettbass closed 3 years ago

garettbass commented 3 years ago

I hope this will make it easier for others to get started :)

garettbass commented 3 years ago

I will try to test this on macOS in the near future. I developed this on a Mac under Windows. I can reboot in macOS, but don’t have a dev environment set up there yet.

floooh commented 3 years ago

I'll have a look at this now.

floooh commented 3 years ago

My nim is a bit rusty :) But I'm getting the following error both on Windows and macOS with nim 1.4.4 when following the build instructions in the README:

E:\projects\sokol-nim\src\examples\cube.nim(7, 13) Error: cannot open file: sokol/app

I figured out that I need to install the glm package via nimble, is there anything else I need to do so that nim finds the sokol modules?

garettbass commented 3 years ago

I will investigate and update readme once I know exactly what to do

garettbass commented 3 years ago

Ok, I made some adjustments based on a warning I got when running nimble install to install sokol as a nim package. I also added some info to the README describing installation of the sokol and glm packages required for the examples.

geotre commented 3 years ago

Looks like the recent changes from @garettbass have fixed issue #5 , as all these examples run on Linux now!

They only compile when using the -d:gl switch, maybe that could be on by default when Linux is detected?

garettbass commented 3 years ago

@geotre I have pushed a change that I think will address the issue. Please try it out and let me know.

Also, if you are able to test whether the samples build on Linux with both gcc and clang, I can add those the README.

geotre commented 3 years ago

@garettbass I confirm that the app.gl change works, and the samples all build and run with both gcc and clang.

I forgot to mention that I needed to change line 33 in clear.nim:

from

passAction.colors[0].value.g = if g > 1.f: 0.f else: g

to

passAction.colors[0].value.g = if g > 1.0: 0.0 else: g

I don't think the .f thing is valid nim, does that compile on osx?

garettbass commented 3 years ago

Ah, that is a silly mistake I made getting started with Nim. Will fix shortly. Thanks for finding it @geotre

garettbass commented 3 years ago

@geotre, I fixed the compile error in clear.nim. When I was getting started, I had a proc f(float64):float32 in there, which was being called by .f to imitate C/C++ style float literal syntax. I later discovered you could write 1f or 1.0f in Nim to produce a float32 literal. I deleted the proc f, but neglected to recompile and discover that it was still being referenced.

garettbass commented 3 years ago

@geotre, can you tell me the --version of gcc and clang that you tested? I will add those to the README

geotre commented 3 years ago

@garettbass ah I see. In Nim a float32 literal can be written as 1.0'f32 or 1'f32

I used gcc 7.5.0 and clang 10.0.0. Happy to test other versions if needed

garettbass commented 3 years ago

@floooh relevant issues should now be resolved.

floooh commented 3 years ago

Btw, it's now possible to split sokol_app_gfx.c into two independent files (with yesterday's sokol update). For instance see sokol_app.c and sokol_gfx.c in the Zig bindings:

https://github.com/floooh/sokol-zig/tree/master/src/sokol/c

...not sure if it makes sense with the Nim bindings though (for instance, in the Zig bindings I simply wrote a native Zig module for the app/gfx-glue by hand instead of using sokol_glue.h:

https://github.com/floooh/sokol-zig/blob/master/src/sokol/app_gfx_glue.zig

Also, I haven't tested your latest commits here yet, will do so ASAP.

garettbass commented 3 years ago

Thanks, this is great news. I will look into refactoring sokol-nim to take advantage of this separation.

floooh commented 3 years ago

Ok, using the new instructions in the readme, the demos are working (with -d:gl, after merging I can add the Metal shader sources, we can also consider adding a nim-output option to sokol-shdc, similar to how I did for Zig.

One thing that's strange, when running the demos I see "event spam" in the terminal window, any idea where that's coming from? Looking through the sources I'm not seeing any obvious debug output from the sokol_app.h event handling code.

macOS x86-64 OpenGL 3.3 clang v.12.0.0 (clang-1200.0.32.29)
MouseMove
MouseMove
MouseMove
MouseMove
MouseMove
MouseMove
...
floooh commented 3 years ago

...I'm just going ahead and merge this for now :) Any additional fixes can go into new PRs I think.

garettbass commented 3 years ago

The event spam is coming from line 13 in cube.nim, or similar in another example:

# the app.event callback will be invoked for each user input event
app.event = (e:app.Event) => echo(e.type)
floooh commented 3 years ago

Ah whoops ok, makes sense. I was grepping for the actual event strings :)

floooh commented 3 years ago

I'll add the missing Metal shader sources and add a small blurb to the Readme about macOS support.

floooh commented 3 years ago

The blend.nim sample looks a bit odd currently (the second vertex shader is missing the layout(location=x) statements, I'll fix that. Currently it also doesn't seem to have the HLSL shaders, I'll just add the Metal source for now.

Screen Shot 2021-03-23 at 7 34 40 PM
garettbass commented 3 years ago

Yes, I don’t see where I went wrong, but the geometry is not right.

floooh commented 3 years ago

The vertex attribute definition in here:

https://github.com/floooh/sokol-nim/blob/5eab0c56f54cccfce39a38fcdb14d634aefa3d74/examples/blend.nim#L102-L103

...should look like this:

      layout(location=0) in vec4 position;
      layout(location=1) in vec4 color0;

...because the vertex layout in the pipeline description doesn't bind the vertex attributes by name. GLSL compilers tend to place vertex attributes at random positions, so without the location-attribute, position most likely ended up at slot 1, and color0 at slot 0.

With the fix it looks much better:

Screen Shot 2021-03-23 at 7 48 46 PM
floooh commented 3 years ago

Ok, cube.nim and blend.nim now also work with Metal:

Screen Shot 2021-03-23 at 8 11 30 PM Screen Shot 2021-03-23 at 8 11 07 PM

I didn't get around today to add the missing D3D1/HLSL source code to the blend.nim example though.

floooh commented 3 years ago

FYI I'm currently seeing somewhat rare weird behaviour on macOS when mouse-clicking the window close button. Sometimes the button gets stuck and the window doesn't close. Currently I'm suspecting the new event polling code in sokol_app.h, that's why I have temporarily commented-out the event polling (and if I still see this behaviour during the next few days, at least that's not the culprit):

https://github.com/floooh/sokol/commit/d44328073d2316f73cec5a99b846593fadd51ba3

(I also just saw this with the Nim examples, that's why I'm writing here).