floooh / sokol-tools

Command line tools for use with sokol headers
MIT License
219 stars 54 forks source link

primtypes-sapp point list rendering broken (sokol-shdc bug) #118

Closed floooh closed 7 months ago

floooh commented 7 months ago

Something's broken with the point-size in the primtypes sample:

Screenshot 2024-02-10 at 15 59 39

...alternatively the point size is fixed at 1 pixel (depending on rendering backend and platform).

The sgl-points-sapp sample works as expected though.

floooh commented 7 months ago

Aha, it's caused by the #ifndef SOKOL_WGSL in the shader:

void main() {
    gl_Position = mvp * vec4(position.xy, 0, 1);
    // WebGPU doesn't support point size
    #ifndef SOKOL_WGSL
    gl_PointSize = point_size;
    #endif
    color = color0;
}

...but why?

floooh commented 7 months ago

Ok... that ifndef block is removed from the GLSL shader for some reason, looks like a bug in sokol-shdc with preprocessor defines.

    #version 330

    uniform vec4 vs_params[5];
    layout(location = 0) in vec2 position;
    out vec4 color;
    layout(location = 1) in vec4 color0;

    void main()
    {
        gl_Position = mat4(vs_params[0], vs_params[1], vs_params[2], vs_params[3]) * vec4(position, 0.0, 1.0);
        color = color0;
    }

...Metal:

    #include <metal_stdlib>
    #include <simd/simd.h>

    using namespace metal;

    struct vs_params
    {
        float4x4 mvp;
        float point_size;
    };

    struct main0_out
    {
        float4 color [[user(locn0)]];
        float4 gl_Position [[position]];
    };

    struct main0_in
    {
        float2 position [[attribute(0)]];
        float4 color0 [[attribute(1)]];
    };

    vertex main0_out main0(main0_in in [[stage_in]], constant vs_params& _19 [[buffer(0)]])
    {
        main0_out out = {};
        out.gl_Position = _19.mvp * float4(in.position, 0.0, 1.0);
        out.color = in.color0;
        return out;
    }
floooh commented 7 months ago

The reason was that the #ifndef SOKOL_WGSL doesn't work as one expect it would, since the define SOKOL_WGSL always exists but might be (0) or (1). So only #if would work. This confusing behaviour has now been fixed in sokol-shdc.