floooh / sokol

minimal cross-platform standalone C headers
https://floooh.github.io/sokol-html5
zlib License
6.54k stars 469 forks source link

How to fill the new sg_swapchain struct when using SDL2? #1018

Closed roig closed 3 months ago

roig commented 3 months ago

Hello! I'm using sokol (OpenGL 3.3) and SDL2 for initializing the window and a GL3.3 context.

With the new change I don't know what exactly I should put in the new sg_swapchain struct. I'm not using D3D, metal etc..

Using try and error I found that this works but it's because I have my pipeline configured with these color, depth and sample.

const chain = std.mem.zeroInit(sg.sg_swapchain, .{
    .width = vp_size.x,
    .height = vp_size.y,
    // ???
     .sample_count = 1,
     .color_format = sg.SG_PIXELFORMAT_RGBA8,
     .depth_format = sg.SG_PIXELFORMAT_DEPTH_STENCIL,
     .gl = .{ .framebuffer = 0 },
});

I'm supposed to call SDL_GL_GetAttribute with:

SDL_GL_RED_SIZE 
SDL_GL_GREEN_SIZE
SDL_GL_BLUE_SIZE
SDL_GL_ALPHA_SIZE

And make a gigantic switch for each combination for color_format?

The same with for depth_format?

SDL_GL_DEPTH_SIZE
SDL_GL_STENCIL_SIZE

For sample_count I'm not sure which one is the correct one:

SDL_GL_MULTISAMPLESAMPLES

For the gl.framebuffer should be 0 always?

And just curiosity but, why all this information is needed now?

Thanks.

floooh commented 3 months ago

For GL you can cheat a bit, since OpenGL doesn't care, only the sokol-gfx validation layer will complain if the pixel formats and sample counts of the pipeline objects used in a swapchain pass don't match. For the Metal and WebGPU backends, that information is more critical though.

Inside sg_begin_pass() zero-initialized fields are replaced with the common defaults for the pixel formats and sample count (the same defaults used when creating pipeline objects), so you don't actually need to provide pixel formats and sample count if it matches the defaults that were already provided in the sg_setup() call:

https://github.com/floooh/sokol/blob/7f7cd64c6d9d1d4ed08d88a3879b1d69841bf0a4/sokol_gfx.h#L16995-L17004

So typically it should work to just provide an 'empty' (zero-initialized) sg_swapchain struct - except for width and height - in the sg_begin_pass() call.

You can also check how I'm doing it in the GLFW samples:

https://github.com/floooh/sokol-samples/blob/2a74ef5905fe7841eaae6a70248d45ebed275fcc/glfw/glfw_glue.c#L52-L76

...for the GL backend, it doesn't actually matter whether the actual framebuffer is RGBA8 or something else, it just needs to be consistent for the sokol-gfx validation layer.

In general I think it's a good idea if you create a similar looking 'sdl_glue.h/.c' file, or at least create similar helper functions to return an sg_environment and sg_swapchain struct.

And just curiosity but, why all this information is needed now?

That same information was taken from the sg_desc.context struct before, so it was already there, just a bit reshuffled.