sokol, gg, examples: update to match uptream at 058a4c5 (#20953)

This commit is contained in:
Larpon 2024-03-05 16:04:05 +01:00 committed by GitHub
parent 60b4fb31a1
commit 790ea2f1bd
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
20 changed files with 3092 additions and 2362 deletions

View file

@ -387,7 +387,8 @@ fn draw_cube_glsl(app App) {
} }
mut pass_action := gfx.PassAction{} mut pass_action := gfx.PassAction{}
pass_action.colors[0] = color_action pass_action.colors[0] = color_action
gfx.begin_default_pass(&pass_action, ws.width, ws.height) pass := sapp.create_default_pass(pass_action)
gfx.begin_pass(&pass)
{ {
rot := [f32(app.mouse_y), f32(app.mouse_x)] rot := [f32(app.mouse_y), f32(app.mouse_x)]
// ratio := f32(ws.width)/ws.height // ratio := f32(ws.width)/ws.height

View file

@ -314,8 +314,6 @@ fn draw_cube_glsl(app App) {
} }
fn frame(mut app App) { fn frame(mut app App) {
ws := gg.window_size_real_pixels()
// clear // clear
mut color_action := gfx.ColorAttachmentAction{ mut color_action := gfx.ColorAttachmentAction{
load_action: .clear load_action: .clear
@ -329,7 +327,8 @@ fn frame(mut app App) {
mut pass_action := gfx.PassAction{} mut pass_action := gfx.PassAction{}
pass_action.colors[0] = color_action pass_action.colors[0] = color_action
gfx.begin_default_pass(&pass_action, ws.width, ws.height) pass := sapp.create_default_pass(pass_action)
gfx.begin_pass(&pass)
// glsl cube // glsl cube
draw_cube_glsl(app) draw_cube_glsl(app)

View file

@ -502,8 +502,6 @@ fn draw_end_glsl(app App) {
} }
fn frame(mut app App) { fn frame(mut app App) {
ws := gg.window_size_real_pixels()
// clear // clear
mut color_action := gfx.ColorAttachmentAction{ mut color_action := gfx.ColorAttachmentAction{
load_action: .clear load_action: .clear
@ -516,7 +514,8 @@ fn frame(mut app App) {
} }
mut pass_action := gfx.PassAction{} mut pass_action := gfx.PassAction{}
pass_action.colors[0] = color_action pass_action.colors[0] = color_action
gfx.begin_default_pass(&pass_action, ws.width, ws.height) pass := sapp.create_default_pass(pass_action)
gfx.begin_pass(&pass)
/* /*
// glsl cube // glsl cube

View file

@ -388,8 +388,6 @@ fn draw_end_glsl(app App) {
} }
fn frame(mut app App) { fn frame(mut app App) {
ws := gg.window_size_real_pixels()
// clear // clear
mut color_action := gfx.ColorAttachmentAction{ mut color_action := gfx.ColorAttachmentAction{
load_action: .clear load_action: .clear
@ -402,7 +400,8 @@ fn frame(mut app App) {
} }
mut pass_action := gfx.PassAction{} mut pass_action := gfx.PassAction{}
pass_action.colors[0] = color_action pass_action.colors[0] = color_action
gfx.begin_default_pass(&pass_action, ws.width, ws.height) pass := gg.create_default_pass(pass_action)
gfx.begin_pass(&pass)
draw_start_glsl(app) draw_start_glsl(app)
draw_cube_glsl_i(mut app) draw_cube_glsl_i(mut app)

View file

@ -144,8 +144,6 @@ fn draw_model(app App, model_pos m4.Vec4) u32 {
} }
fn frame(mut app App) { fn frame(mut app App) {
ws := gg.window_size_real_pixels()
// clear // clear
mut color_action := gfx.ColorAttachmentAction{ mut color_action := gfx.ColorAttachmentAction{
load_action: .clear load_action: .clear
@ -159,7 +157,8 @@ fn frame(mut app App) {
mut pass_action := gfx.PassAction{} mut pass_action := gfx.PassAction{}
pass_action.colors[0] = color_action pass_action.colors[0] = color_action
gfx.begin_default_pass(&pass_action, ws.width, ws.height) pass := sapp.create_default_pass(pass_action)
gfx.begin_pass(&pass)
// render the data // render the data
draw_start_glsl(app) draw_start_glsl(app)

View file

@ -11,7 +11,7 @@ const used_import = sokol.used_import
fn main() { fn main() {
state := &AppState{ state := &AppState{
pass_action: gfx.create_clear_pass(0.1, 0.1, 0.1, 1.0) pass_action: gfx.create_clear_pass_action(0.1, 0.1, 0.1, 1.0)
} }
title := 'Sokol Drawing Template' title := 'Sokol Drawing Template'
desc := sapp.Desc{ desc := sapp.Desc{
@ -36,7 +36,8 @@ fn init(user_data voidptr) {
fn frame(state &AppState) { fn frame(state &AppState) {
// println('frame') // println('frame')
draw() draw()
gfx.begin_default_pass(&state.pass_action, sapp.width(), sapp.height()) pass := sapp.create_default_pass(state.pass_action)
gfx.begin_pass(&pass)
sgl.draw() sgl.draw()
gfx.end_pass() gfx.end_pass()
gfx.commit() gfx.commit()

View file

@ -14,19 +14,8 @@ mut:
} }
fn main() { fn main() {
mut color_action := gfx.ColorAttachmentAction{
load_action: .clear
clear_value: gfx.Color{
r: 0.3
g: 0.3
b: 0.32
a: 1.0
}
}
mut pass_action := gfx.PassAction{}
pass_action.colors[0] = color_action
state := &AppState{ state := &AppState{
pass_action: pass_action pass_action: gfx.create_clear_pass_action(0.3, 0.3, 0.32, 1.0)
font_context: unsafe { nil } // &fontstash.Context(0) font_context: unsafe { nil } // &fontstash.Context(0)
} }
title := 'V Metal/GL Text Rendering' title := 'V Metal/GL Text Rendering'
@ -57,7 +46,8 @@ fn init(mut state AppState) {
fn frame(mut state AppState) { fn frame(mut state AppState) {
state.render_font() state.render_font()
gfx.begin_default_pass(&state.pass_action, sapp.width(), sapp.height()) pass := sapp.create_default_pass(state.pass_action)
gfx.begin_pass(&pass)
sgl.draw() sgl.draw()
gfx.end_pass() gfx.end_pass()
gfx.commit() gfx.commit()

View file

@ -107,7 +107,8 @@ fn init(mut state AppState) {
fn frame(mut state AppState) { fn frame(mut state AppState) {
state.render_font() state.render_font()
gfx.begin_default_pass(&state.pass_action, sapp.width(), sapp.height()) pass := sapp.create_default_pass(state.pass_action)
gfx.begin_pass(&pass)
sgl.draw() sgl.draw()
gfx.end_pass() gfx.end_pass()
gfx.commit() gfx.commit()

View file

@ -15,7 +15,7 @@ fn main() {
mut app := &App{ mut app := &App{
width: 800 width: 800
height: 400 height: 400
pass_action: gfx.create_clear_pass(0.1, 0.1, 0.1, 1.0) pass_action: gfx.create_clear_pass_action(0.1, 0.1, 0.1, 1.0)
} }
app.init() app.init()
app.run() app.run()
@ -105,7 +105,8 @@ fn frame(mut app App) {
dt := f64(t - app.last) / 1000.0 dt := f64(t - app.last) / 1000.0
app.ps.update(dt) app.ps.update(dt)
draw(app) draw(app)
gfx.begin_default_pass(&app.pass_action, app.width, app.height) pass := sapp.create_default_pass(app.pass_action)
gfx.begin_pass(&pass)
sgl.default_pipeline() sgl.default_pipeline()
sgl.draw() sgl.draw()
gfx.end_pass() gfx.end_pass()

View file

@ -36,7 +36,7 @@ fn main() {
mut app := &App{ mut app := &App{
width: 800 width: 800
height: 400 height: 400
pass_action: gfx.create_clear_pass(0.0, 0.0, 0.0, 1.0) // This will create a black color as a default pass (window background color) pass_action: gfx.create_clear_pass_action(0.0, 0.0, 0.0, 1.0) // This will create a black color as a default pass (window background color)
} }
app.run() app.run()
} }
@ -143,7 +143,8 @@ fn cleanup(user_data voidptr) {
fn frame(user_data voidptr) { fn frame(user_data voidptr) {
mut app := unsafe { &App(user_data) } mut app := unsafe { &App(user_data) }
gfx.begin_default_pass(&app.pass_action, sapp.width(), sapp.height()) pass := sapp.create_default_pass(state.pass_action)
gfx.begin_pass(&pass)
gfx.apply_pipeline(app.shader_pipeline) gfx.apply_pipeline(app.shader_pipeline)
gfx.apply_bindings(&app.bind) gfx.apply_bindings(&app.bind)

File diff suppressed because it is too large Load diff

File diff suppressed because it is too large Load diff

View file

@ -833,7 +833,7 @@ SOKOL_GL_API_DECL sgl_context sgl_get_context(void);
SOKOL_GL_API_DECL sgl_context sgl_default_context(void); SOKOL_GL_API_DECL sgl_context sgl_default_context(void);
/* draw recorded commands (call inside a sokol-gfx render pass) */ /* draw recorded commands (call inside a sokol-gfx render pass) */
SOKOL_GL_API_DECL void sgl_draw(); SOKOL_GL_API_DECL void sgl_draw(void);
SOKOL_GL_API_DECL void sgl_context_draw(sgl_context ctx); SOKOL_GL_API_DECL void sgl_context_draw(sgl_context ctx);
SOKOL_GL_API_DECL void sgl_draw_layer(int layer_id); SOKOL_GL_API_DECL void sgl_draw_layer(int layer_id);
SOKOL_GL_API_DECL void sgl_context_draw_layer(sgl_context ctx, int layer_id); SOKOL_GL_API_DECL void sgl_context_draw_layer(sgl_context ctx, int layer_id);

View file

@ -509,8 +509,8 @@ pub fn (ctx &Context) quit() {
// set_bg_color sets the color of the window background to `c`. // set_bg_color sets the color of the window background to `c`.
pub fn (mut ctx Context) set_bg_color(c gx.Color) { pub fn (mut ctx Context) set_bg_color(c gx.Color) {
ctx.clear_pass = gfx.create_clear_pass(f32(c.r) / 255.0, f32(c.g) / 255.0, f32(c.b) / 255.0, ctx.clear_pass = gfx.create_clear_pass_action(f32(c.r) / 255.0, f32(c.g) / 255.0,
f32(c.a) / 255.0) f32(c.b) / 255.0, f32(c.a) / 255.0)
} }
// Resize the context's Window // Resize the context's Window
@ -566,6 +566,10 @@ const dontcare_pass = gfx.PassAction{
]! ]!
} }
pub fn create_default_pass(action gfx.PassAction) gfx.Pass {
return sapp.create_default_pass(action)
}
// end finishes all the drawing for the context ctx. // end finishes all the drawing for the context ctx.
// All accumulated draw calls before ctx.end(), will be done in a separate Sokol pass. // All accumulated draw calls before ctx.end(), will be done in a separate Sokol pass.
// //
@ -592,14 +596,15 @@ pub fn (ctx &Context) end(options EndOptions) {
ctx.show_fps() ctx.show_fps()
} }
} }
match options.how { pass := match options.how {
.clear { .clear {
gfx.begin_default_pass(ctx.clear_pass, sapp.width(), sapp.height()) create_default_pass(ctx.clear_pass)
} }
.passthru { .passthru {
gfx.begin_default_pass(gg.dontcare_pass, sapp.width(), sapp.height()) create_default_pass(gg.dontcare_pass)
} }
} }
gfx.begin_pass(pass)
sgl.draw() sgl.draw()
gfx.end_pass() gfx.end_pass()
gfx.commit() gfx.commit()

View file

@ -65,9 +65,11 @@ pub fn make_pipeline(desc &PipelineDesc) Pipeline {
return C.sg_make_pipeline(desc) return C.sg_make_pipeline(desc)
} }
// make_attachments creates an `Attachments` instance from `const_desc`.
// See also: documentation at the top of thirdparty/sokol/sokol_gfx.h
@[inline] @[inline]
pub fn make_pass(desc &PassDesc) Pass { pub fn make_attachments(const_desc &AttachmentsDesc) Attachments {
return C.sg_make_pass(desc) return C.sg_make_attachments(const_desc)
} }
@[inline] @[inline]
@ -95,9 +97,11 @@ pub fn destroy_pipeline(pip Pipeline) {
C.sg_destroy_pipeline(pip) C.sg_destroy_pipeline(pip)
} }
// destroy_attachments destroys the `atts` `Attachments`
// See also: documentation at the top of thirdparty/sokol/sokol_gfx.h
@[inline] @[inline]
pub fn destroy_pass(pass Pass) { pub fn destroy_attachments(atts Attachments) {
C.sg_destroy_pass(pass) C.sg_destroy_attachments(atts)
} }
@[inline] @[inline]
@ -121,14 +125,16 @@ pub fn query_buffer_overflow(buf Buffer) bool {
} }
// rendering functions // rendering functions
@[inline]
pub fn begin_default_pass(actions &PassAction, width int, height int) {
C.sg_begin_default_pass(actions, width, height)
}
@[deprecated: 'use begin_pass instead, please see examples/sokol/* for how to utilize new unified begin_pass']
@[inline] @[inline]
pub fn begin_pass(pass Pass, actions &PassAction) { pub fn begin_default_pass(actions &PassAction, width int, height int) {}
C.sg_begin_pass(pass, actions)
// begin_pass begins a rendering pass.
// See also: documentation at the top of thirdparty/sokol/sokol_gfx.h
@[inline]
pub fn begin_pass(const_pass &Pass) {
C.sg_begin_pass(const_pass)
} }
@[inline] @[inline]
@ -218,9 +224,11 @@ pub fn query_pipeline_state(pip Pipeline) ResourceState {
return ResourceState(C.sg_query_pipeline_state(pip)) return ResourceState(C.sg_query_pipeline_state(pip))
} }
// query_attachments_state returns the current state of a resource (INITIAL, ALLOC, VALID, FAILED, INVALID)
// See also: documentation at the top of thirdparty/sokol/sokol_gfx.h
@[inline] @[inline]
pub fn query_pass_state(pass Pass) ResourceState { pub fn query_attachments_state(atts Attachments) ResourceState {
return ResourceState(C.sg_query_pass_state(pass)) return ResourceState(C.sg_query_attachments_state(atts))
} }
// get runtime information about a resource // get runtime information about a resource
@ -244,9 +252,11 @@ pub fn query_pipeline_info(pip Pipeline) PipelineInfo {
return C.sg_query_pipeline_info(pip) return C.sg_query_pipeline_info(pip)
} }
// query_attachments_info returns runtime information about the `atts` / `Attachments`.
// See also: documentation at the top of thirdparty/sokol/sokol_gfx.h
@[inline] @[inline]
pub fn query_pass_info(pass Pass) PassInfo { pub fn query_attachments_info(atts Attachments) AttachmentsInfo {
return C.sg_query_pass_info(pass) return C.sg_query_attachments_info(atts)
} }
// get resource creation desc struct with their default values replaced // get resource creation desc struct with their default values replaced
@ -270,25 +280,11 @@ pub fn query_pipeline_defaults(desc &Pipeline) PipelineDesc {
return C.sg_query_pipeline_defaults(unsafe { &PipelineDesc(voidptr(desc)) }) return C.sg_query_pipeline_defaults(unsafe { &PipelineDesc(voidptr(desc)) })
} }
// query_attachments_defaults returns `AttachmentsDesc` with default values replaced.
// See also: documentation at the top of thirdparty/sokol/sokol_gfx.h
@[inline] @[inline]
pub fn query_pass_defaults(desc &Pass) PassDesc { pub fn query_attachments_defaults(desc &AttachmentsDesc) AttachmentsDesc {
return C.sg_query_pass_defaults(unsafe { &PassDesc(voidptr(desc)) }) return C.sg_query_attachments_defaults(unsafe { &AttachmentsDesc(voidptr(desc)) })
}
// rendering contexts (optional)
@[inline]
pub fn setup_context() Context {
return C.sg_setup_context()
}
@[inline]
pub fn activate_context(ctx_id Context) {
C.sg_activate_context(ctx_id)
}
@[inline]
pub fn discard_context(ctx_id Context) {
C.sg_discard_context(ctx_id)
} }
// frame stats // frame stats

View file

@ -17,13 +17,13 @@ fn C.sg_make_image(const_desc &C.sg_image_desc) C.sg_image
fn C.sg_make_sampler(const_desc &C.sg_sampler_desc) C.sg_sampler fn C.sg_make_sampler(const_desc &C.sg_sampler_desc) C.sg_sampler
fn C.sg_make_shader(const_desc &C.sg_shader_desc) C.sg_shader fn C.sg_make_shader(const_desc &C.sg_shader_desc) C.sg_shader
fn C.sg_make_pipeline(const_desc &C.sg_pipeline_desc) C.sg_pipeline fn C.sg_make_pipeline(const_desc &C.sg_pipeline_desc) C.sg_pipeline
fn C.sg_make_pass(const_desc &C.sg_pass_desc) C.sg_pass fn C.sg_make_attachments(const_desc &C.sg_attachments_desc) C.sg_attachments
fn C.sg_destroy_buffer(buf C.sg_buffer) fn C.sg_destroy_buffer(buf C.sg_buffer)
fn C.sg_destroy_image(img C.sg_image) fn C.sg_destroy_image(img C.sg_image)
fn C.sg_destroy_sampler(smp C.sg_sampler) fn C.sg_destroy_sampler(smp C.sg_sampler)
fn C.sg_destroy_shader(shd C.sg_shader) fn C.sg_destroy_shader(shd C.sg_shader)
fn C.sg_destroy_pipeline(pip C.sg_pipeline) fn C.sg_destroy_pipeline(pip C.sg_pipeline)
fn C.sg_destroy_pass(pass C.sg_pass) fn C.sg_destroy_attachments(atts C.sg_attachments)
fn C.sg_update_buffer(buf C.sg_buffer, data &C.sg_range) fn C.sg_update_buffer(buf C.sg_buffer, data &C.sg_range)
fn C.sg_update_image(img C.sg_image, data &C.sg_image_data) fn C.sg_update_image(img C.sg_image, data &C.sg_image_data)
fn C.sg_append_buffer(buf C.sg_buffer, data &C.sg_range) int fn C.sg_append_buffer(buf C.sg_buffer, data &C.sg_range) int
@ -31,8 +31,7 @@ fn C.sg_query_buffer_overflow(buf C.sg_buffer) bool
fn C.sg_query_buffer_will_overflow(buf C.sg_buffer, size usize) bool fn C.sg_query_buffer_will_overflow(buf C.sg_buffer, size usize) bool
// rendering functions // rendering functions
fn C.sg_begin_default_pass(actions &C.sg_pass_action, width int, height int) fn C.sg_begin_pass(const_pass &C.sg_pass)
fn C.sg_begin_pass(pass C.sg_pass, actions &C.sg_pass_action)
fn C.sg_apply_viewport(x int, y int, width int, height int, origin_top_left bool) fn C.sg_apply_viewport(x int, y int, width int, height int, origin_top_left bool)
fn C.sg_apply_viewportf(x f32, y f32, width f32, height f32, origin_top_left bool) fn C.sg_apply_viewportf(x f32, y f32, width f32, height f32, origin_top_left bool)
fn C.sg_apply_scissor_rect(x int, y int, width int, height int, origin_top_left bool) fn C.sg_apply_scissor_rect(x int, y int, width int, height int, origin_top_left bool)
@ -57,7 +56,7 @@ fn C.sg_query_image_state(img C.sg_image) ResourceState
fn C.sg_query_sampler_state(smp C.sg_sampler) ResourceState fn C.sg_query_sampler_state(smp C.sg_sampler) ResourceState
fn C.sg_query_shader_state(shd C.sg_shader) ResourceState fn C.sg_query_shader_state(shd C.sg_shader) ResourceState
fn C.sg_query_pipeline_state(pip C.sg_pipeline) ResourceState fn C.sg_query_pipeline_state(pip C.sg_pipeline) ResourceState
fn C.sg_query_pass_state(pass C.sg_pass) ResourceState fn C.sg_query_attachments_state(atts C.sg_attachments) ResourceState
// get runtime information about a resource // get runtime information about a resource
fn C.sg_query_buffer_info(buf C.sg_buffer) C.sg_buffer_info fn C.sg_query_buffer_info(buf C.sg_buffer) C.sg_buffer_info
@ -65,7 +64,7 @@ fn C.sg_query_image_info(img C.sg_image) C.sg_image_info
fn C.sg_query_sampler_info(smp C.sg_sampler) C.sg_sampler_info fn C.sg_query_sampler_info(smp C.sg_sampler) C.sg_sampler_info
fn C.sg_query_shader_info(shd C.sg_shader) C.sg_shader_info fn C.sg_query_shader_info(shd C.sg_shader) C.sg_shader_info
fn C.sg_query_pipeline_info(pip C.sg_pipeline) C.sg_pipeline_info fn C.sg_query_pipeline_info(pip C.sg_pipeline) C.sg_pipeline_info
fn C.sg_query_pass_info(pass C.sg_pass) C.sg_pass_info fn C.sg_query_attachments_info(atts C.sg_attachments) C.sg_attachments_info
// get desc structs matching a specific resource (NOTE that not all creation attributes may be provided) // get desc structs matching a specific resource (NOTE that not all creation attributes may be provided)
fn C.sg_query_buffer_desc(buf C.sg_buffer) C.sg_buffer_desc fn C.sg_query_buffer_desc(buf C.sg_buffer) C.sg_buffer_desc
@ -73,7 +72,7 @@ fn C.sg_query_image_desc(img C.sg_image) C.sg_image_desc
fn C.sg_query_sampler_desc(smp C.sg_sampler) C.sg_sampler_desc fn C.sg_query_sampler_desc(smp C.sg_sampler) C.sg_sampler_desc
fn C.sg_query_shader_desc(shd C.sg_shader) C.sg_shader_desc fn C.sg_query_shader_desc(shd C.sg_shader) C.sg_shader_desc
fn C.sg_query_pipeline_desc(pip C.sg_pipeline) C.sg_pipeline_desc fn C.sg_query_pipeline_desc(pip C.sg_pipeline) C.sg_pipeline_desc
fn C.sg_query_pass_desc(pass C.sg_pass) C.sg_pass_desc fn C.sg_query_attachments_desc(atts C.sg_attachments) C.sg_attachments_desc
// get resource creation desc struct with their default values replaced // get resource creation desc struct with their default values replaced
fn C.sg_query_buffer_defaults(const_desc &C.sg_buffer_desc) C.sg_buffer_desc fn C.sg_query_buffer_defaults(const_desc &C.sg_buffer_desc) C.sg_buffer_desc
@ -81,7 +80,7 @@ fn C.sg_query_image_defaults(const_desc &C.sg_image_desc) C.sg_image_desc
fn C.sg_query_sampler_defaults(const_desc &C.sg_sampler_desc) C.sg_sampler_desc fn C.sg_query_sampler_defaults(const_desc &C.sg_sampler_desc) C.sg_sampler_desc
fn C.sg_query_shader_defaults(const_desc &C.sg_shader_desc) C.sg_shader_desc fn C.sg_query_shader_defaults(const_desc &C.sg_shader_desc) C.sg_shader_desc
fn C.sg_query_pipeline_defaults(const_desc &C.sg_pipeline_desc) C.sg_pipeline_desc fn C.sg_query_pipeline_defaults(const_desc &C.sg_pipeline_desc) C.sg_pipeline_desc
fn C.sg_query_pass_defaults(const_desc &C.sg_pass_desc) C.sg_pass_desc fn C.sg_query_attachments_defaults(const_desc &C.sg_attachments_desc) C.sg_attachments_desc
// separate resource allocation and initialization (for async setup) // separate resource allocation and initialization (for async setup)
fn C.sg_alloc_buffer() C.sg_buffer fn C.sg_alloc_buffer() C.sg_buffer
@ -89,39 +88,34 @@ fn C.sg_alloc_image() C.sg_image
fn C.sg_alloc_sampler() C.sg_sampler fn C.sg_alloc_sampler() C.sg_sampler
fn C.sg_alloc_shader() C.sg_shader fn C.sg_alloc_shader() C.sg_shader
fn C.sg_alloc_pipeline() C.sg_pipeline fn C.sg_alloc_pipeline() C.sg_pipeline
fn C.sg_alloc_pass() C.sg_pass fn C.sg_alloc_attachments() C.sg_attachments
fn C.sg_dealloc_buffer(buf C.sg_buffer) fn C.sg_dealloc_buffer(buf C.sg_buffer)
fn C.sg_dealloc_image(img C.sg_image) fn C.sg_dealloc_image(img C.sg_image)
fn C.sg_dealloc_sampler(smp C.sg_sampler) fn C.sg_dealloc_sampler(smp C.sg_sampler)
fn C.sg_dealloc_shader(shd C.sg_shader) fn C.sg_dealloc_shader(shd C.sg_shader)
fn C.sg_dealloc_pipeline(pip C.sg_pipeline) fn C.sg_dealloc_pipeline(pip C.sg_pipeline)
fn C.sg_dealloc_pass(pass C.sg_pass) fn C.sg_dealloc_attachments(atts C.sg_attachments)
fn C.sg_init_buffer(buf C.sg_buffer, const_desc &C.sg_buffer_desc) fn C.sg_init_buffer(buf C.sg_buffer, const_desc &C.sg_buffer_desc)
fn C.sg_init_image(img C.sg_image, const_desc &C.sg_buffer_desc) fn C.sg_init_image(img C.sg_image, const_desc &C.sg_buffer_desc)
fn C.sg_init_sampler(smg C.sg_sampler, const_desc &C.sg_sampler_desc) fn C.sg_init_sampler(smg C.sg_sampler, const_desc &C.sg_sampler_desc)
fn C.sg_init_shader(shd C.sg_shader, const_desc &C.sg_shader_desc) fn C.sg_init_shader(shd C.sg_shader, const_desc &C.sg_shader_desc)
fn C.sg_init_pipeline(pip C.sg_pipeline, const_desc &C.sg_pipeline_desc) fn C.sg_init_pipeline(pip C.sg_pipeline, const_desc &C.sg_pipeline_desc)
fn C.sg_init_pass(pass C.sg_pass, const_desc &C.sg_pass_desc) fn C.sg_init_attachments(atts C.sg_attachments, const_desc &C.sg_attachments_desc)
fn C.sg_uninit_buffer(buf C.sg_buffer) fn C.sg_uninit_buffer(buf C.sg_buffer)
fn C.sg_uninit_image(img C.sg_image) fn C.sg_uninit_image(img C.sg_image)
fn C.sg_uninit_sampler(smp C.sg_sampler) fn C.sg_uninit_sampler(smp C.sg_sampler)
fn C.sg_uninit_shader(shd C.sg_shader) fn C.sg_uninit_shader(shd C.sg_shader)
fn C.sg_uninit_pipeline(pip C.sg_pipeline) fn C.sg_uninit_pipeline(pip C.sg_pipeline)
fn C.sg_uninit_pass(pass C.sg_pass) fn C.sg_uninit_attachments(atts C.sg_attachments)
fn C.sg_fail_buffer(buf C.sg_buffer) fn C.sg_fail_buffer(buf C.sg_buffer)
fn C.sg_fail_image(img C.sg_image) fn C.sg_fail_image(img C.sg_image)
fn C.sg_fail_sampler(smp C.sg_sampler) fn C.sg_fail_sampler(smp C.sg_sampler)
fn C.sg_fail_shader(shd C.sg_shader) fn C.sg_fail_shader(shd C.sg_shader)
fn C.sg_fail_pipeline(pip C.sg_pipeline) fn C.sg_fail_pipeline(pip C.sg_pipeline)
fn C.sg_fail_pass(pass C.sg_pass) fn C.sg_fail_attachments(atts C.sg_attachments)
// frame stats // frame stats
fn C.sg_enable_frame_stats() fn C.sg_enable_frame_stats()
fn C.sg_disable_frame_stats() fn C.sg_disable_frame_stats()
fn C.sg_frame_stats_enabled() bool fn C.sg_frame_stats_enabled() bool
fn C.sg_query_frame_stats() C.sg_frame_stats fn C.sg_query_frame_stats() C.sg_frame_stats
// rendering contexts (optional)
fn C.sg_setup_context() C.sg_context
fn C.sg_activate_context(ctx_id C.sg_context)
fn C.sg_discard_context(ctx_id C.sg_context)

View file

@ -3,64 +3,28 @@ module gfx
// C.sg_desc describes // C.sg_desc describes
pub struct C.sg_desc { pub struct C.sg_desc {
pub mut: pub mut:
_start_canary u32
buffer_pool_size int buffer_pool_size int
image_pool_size int image_pool_size int
sampler_pool_size int sampler_pool_size int
shader_pool_size int shader_pool_size int
pipeline_pool_size int pipeline_pool_size int
pass_pool_size int attachments_pool_size int
context_pool_size int
uniform_buffer_size int uniform_buffer_size int
max_commit_listeners int max_commit_listeners int
disable_validation bool // disable validation layer even in debug mode, useful for tests disable_validation bool // disable validation layer even in debug mode, useful for tests
mtl_force_managed_storage_mode bool // for debugging: use Metal managed storage mode for resources even with UMA mtl_force_managed_storage_mode bool // for debugging: use Metal managed storage mode for resources even with UMA
mtl_use_command_buffer_with_retained_references bool // Metal: use a managed MTLCommandBuffer which ref-counts used resources
wgpu_disable_bindgroups_cache bool // set to true to disable the WebGPU backend BindGroup cache wgpu_disable_bindgroups_cache bool // set to true to disable the WebGPU backend BindGroup cache
wgpu_bindgroups_cache_size int // number of slots in the WebGPU bindgroup cache (must be 2^N) wgpu_bindgroups_cache_size int // number of slots in the WebGPU bindgroup cache (must be 2^N)
//
allocator C.sg_allocator allocator C.sg_allocator
logger C.sg_logger logger C.sg_logger // optional log function override
// environment C.sg_environment
context C.sg_context_desc _end_canary u32
} }
pub type Desc = C.sg_desc pub type Desc = C.sg_desc
pub struct C.sg_context_desc {
pub mut:
color_format PixelFormat
depth_format PixelFormat
sample_count int
gl GLContextDesc
metal MetalContextDesc
d3d11 D3D11ContextDesc
// TODO C.sg_wgpu_context_desc wgpu;
}
pub type ContextDesc = C.sg_context_desc
pub struct C.sg_gl_context_desc {
force_gles2 bool
}
pub type GLContextDesc = C.sg_gl_context_desc
pub struct C.sg_metal_context_desc {
device voidptr
renderpass_descriptor_cb fn () voidptr = unsafe { nil }
drawable_cb fn () voidptr = unsafe { nil }
}
pub type MetalContextDesc = C.sg_metal_context_desc
pub struct C.sg_d3d11_context_desc {
device voidptr
device_context voidptr
render_target_view_cb fn () voidptr = unsafe { nil }
depth_stencil_view_cb fn () voidptr = unsafe { nil }
}
pub type D3D11ContextDesc = C.sg_d3d11_context_desc
pub struct C.sg_color_target_state { pub struct C.sg_color_target_state {
pub mut: pub mut:
pixel_format PixelFormat pixel_format PixelFormat
@ -108,6 +72,47 @@ pub fn (mut p C.sg_pipeline) free() {
C.sg_destroy_pipeline(*p) C.sg_destroy_pipeline(*p)
} }
pub struct C.sg_attachments {
id u32
}
// Attachments represents data that can be attached to a render pass.
// See also: documentation at the top of thirdparty/sokol/sokol_gfx.h
pub type Attachments = C.sg_attachments
// free frees the resources occupied by the struct
pub fn (mut a C.sg_attachments) free() {
C.sg_destroy_attachments(*a)
}
pub struct C.sg_attachment_desc {
pub mut:
image Image
mip_level int
slice int
}
pub type AttachmentDesc = C.sg_attachment_desc
pub struct C.sg_attachments_desc {
pub mut:
_start_canary u32
colors [4]AttachmentDesc
resolves [4]AttachmentDesc
depth_stencil AttachmentDesc
label &char = unsafe { nil }
_end_canary u32
}
pub type AttachmentsDesc = C.sg_attachments_desc
pub struct C.sg_attachments_info {
pub mut:
slot SlotInfo // resource pool slot info
}
pub type AttachmentsInfo = C.sg_attachments_info
pub struct C.sg_bindings { pub struct C.sg_bindings {
pub mut: pub mut:
_start_canary u32 _start_canary u32
@ -314,13 +319,6 @@ pub:
pub type ShaderInfo = C.sg_shader_info pub type ShaderInfo = C.sg_shader_info
pub struct C.sg_context {
pub:
id u32
}
pub type Context = C.sg_context
pub struct C.sg_range { pub struct C.sg_range {
pub mut: pub mut:
ptr voidptr ptr voidptr
@ -350,21 +348,6 @@ pub fn (mut s Shader) free() {
C.sg_destroy_shader(*s) C.sg_destroy_shader(*s)
} }
pub struct C.sg_pass_desc {
pub mut:
color_attachments [4]PassAttachmentDesc
depth_stencil_attachment PassAttachmentDesc
label &char
}
pub type PassDesc = C.sg_pass_desc
pub struct C.sg_pass_info {
info SlotInfo
}
pub type PassInfo = C.sg_pass_info
@[typedef] @[typedef]
pub struct C.sg_frame_stats_gl { pub struct C.sg_frame_stats_gl {
num_bind_buffer u32 num_bind_buffer u32
@ -571,16 +554,73 @@ pub mut:
pub type PassAction = C.sg_pass_action pub type PassAction = C.sg_pass_action
@[typedef]
pub struct C.sg_metal_swapchain {
pub mut:
current_drawable voidptr
depth_stencil_texture voidptr // MTLTexture
msaa_color_texture voidptr // MTLTexture
}
pub type MetalSwapchain = C.sg_metal_swapchain
@[typedef]
pub struct C.sg_d3d11_swapchain {
pub mut:
render_view voidptr // ID3D11RenderTargetView
resolve_view voidptr // ID3D11RenderTargetView
depth_stencil_view voidptr // ID3D11DepthStencilView
}
pub type D3d11Swapchain = C.sg_d3d11_swapchain
@[typedef]
pub struct C.sg_wgpu_swapchain {
pub mut:
render_view voidptr // WGPUTextureView
resolve_view voidptr // WGPUTextureView
depth_stencil_view voidptr // WGPUTextureView
}
pub type WgpuSwapchain = C.sg_wgpu_swapchain
@[typedef]
pub struct C.sg_gl_swapchain {
pub mut:
framebuffer u32
}
pub type GlSwapchain = C.sg_gl_swapchain
@[typedef]
pub struct C.sg_swapchain {
pub mut:
width int
height int
sample_count int
color_format PixelFormat
depth_format PixelFormat
metal MetalSwapchain
d3d11 D3d11Swapchain
wgpu WgpuSwapchain
gl GlSwapchain
}
pub type Swapchain = C.sg_swapchain
@[typedef]
pub struct C.sg_pass { pub struct C.sg_pass {
id u32 pub mut:
_start_canary u32
action PassAction
attachments Attachments
swapchain Swapchain
label &char = unsafe { nil }
_end_canary u32
} }
pub type Pass = C.sg_pass pub type Pass = C.sg_pass
pub fn (mut p Pass) free() {
C.sg_destroy_pass(*p)
}
pub struct C.sg_buffer_desc { pub struct C.sg_buffer_desc {
pub mut: pub mut:
size usize size usize
@ -745,7 +785,7 @@ pub:
max_image_size_array int // max width/height pf SG_IMAGETYPE_ARRAY images max_image_size_array int // max width/height pf SG_IMAGETYPE_ARRAY images
max_image_array_layers int // max number of layers in SG_IMAGETYPE_ARRAY images max_image_array_layers int // max number of layers in SG_IMAGETYPE_ARRAY images
max_vertex_attrs int // <= SG_MAX_VERTEX_ATTRIBUTES (only on some GLES2 impls) max_vertex_attrs int // <= SG_MAX_VERTEX_ATTRIBUTES (only on some GLES2 impls)
gl_max_vertex_uniform_vectors int // <= GL_MAX_VERTEX_UNIFORM_VECTORS (only on GL backends) gl_max_vertex_uniform_components int // <= GL_MAX_VERTEX_UNIFORM_COMPONENTS (only on GL backends)
gl_max_combined_texture_image_units int // <= GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS (only on GL backends) gl_max_combined_texture_image_units int // <= GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS (only on GL backends)
} }
@ -868,21 +908,51 @@ pub:
pub type PixelFormatInfo = C.sg_pixelformat_info pub type PixelFormatInfo = C.sg_pixelformat_info
pub struct C.sg_pass_attachment_desc { @[typedef]
pub struct C.sg_environment_defaults {
pub mut: pub mut:
image Image color_format PixelFormat
mip_level int depth_format PixelFormat
slice int sample_count int
// image sg_image
// mip_level int
// union {
// face int
// layer int
// slice int
// }
} }
pub type PassAttachmentDesc = C.sg_pass_attachment_desc pub type EnvironmentDefaults = C.sg_environment_defaults
@[typedef]
pub struct C.sg_metal_environment {
pub mut:
device voidptr
}
pub type MetalEnvironment = C.sg_metal_environment
@[typedef]
pub struct C.sg_d3d11_environment {
pub mut:
device voidptr
device_context voidptr
}
pub type D3d11Environment = C.sg_d3d11_environment
@[typedef]
pub struct C.sg_wgpu_environment {
pub mut:
device voidptr
}
pub type WgpuEnvironment = C.sg_wgpu_environment
@[typedef]
pub struct C.sg_environment {
pub mut:
defaults EnvironmentDefaults
metal MetalEnvironment
d3d11 D3d11Environment
wgpu WgpuEnvironment
}
pub type Environment = C.sg_environment
// C.sg_commit_listener is used with sg_add_commit_listener, to add a callback, // C.sg_commit_listener is used with sg_add_commit_listener, to add a callback,
// which will be called in sg_commit(). This is useful for libraries building // which will be called in sg_commit(). This is useful for libraries building

View file

@ -1,8 +1,16 @@
module gfx module gfx
@[deprecated: 'use create_clear_pass_action instead']
@[deprecated_after: '2024-09-03']
pub fn create_clear_pass(r f32, g f32, b f32, a f32) PassAction { pub fn create_clear_pass(r f32, g f32, b f32, a f32) PassAction {
return create_clear_pass_action(r, g, b, a)
}
// create_clear_pass_action returns a *clearing* `PassAction` that clears the `Pass`
// with the color defined by the color components `r`ed,`g`reen, `b`lue and `a`lpha.
pub fn create_clear_pass_action(r f32, g f32, b f32, a f32) PassAction {
mut color_action := ColorAttachmentAction{ mut color_action := ColorAttachmentAction{
load_action: .clear // unsafe { Action(C.SG_ACTION_CLEAR) } load_action: .clear
clear_value: Color{ clear_value: Color{
r: r r: r
g: g g: g

View file

@ -10,28 +10,70 @@ pub const used_import = gfx.used_import
__global g_desc C.sapp_desc __global g_desc C.sapp_desc
pub fn create_desc() gfx.Desc { pub fn create_desc() gfx.Desc {
metal_desc := gfx.MetalContextDesc{
device: metal_get_device()
renderpass_descriptor_cb: metal_get_renderpass_descriptor
drawable_cb: metal_get_drawable
}
d3d11_desc := gfx.D3D11ContextDesc{
device: d3d11_get_device()
device_context: d3d11_get_device_context()
render_target_view_cb: d3d11_get_render_target_view
depth_stencil_view_cb: d3d11_get_depth_stencil_view
}
return gfx.Desc{ return gfx.Desc{
context: gfx.ContextDesc{ environment: glue_environment()
metal: metal_desc
d3d11: d3d11_desc
color_format: .bgra8
}
image_pool_size: 1000 image_pool_size: 1000
} }
} }
// create_default_pass creates a default `gfx.Pass` compatible with `sapp` and `sokol.gfx.begin_pass/1`.
pub fn create_default_pass(action gfx.PassAction) gfx.Pass {
return gfx.Pass{
action: action
swapchain: glue_swapchain()
}
}
// glue_environment returns a `gfx.Environment` compatible for use with `sapp` specific `gfx.Pass`es.
// The retuned `gfx.Environment` can be used when rendering via `sapp`.
// See also: documentation at the top of thirdparty/sokol/sokol_gfx.h
pub fn glue_environment() gfx.Environment {
mut env := gfx.Environment{}
unsafe { vmemset(&env, 0, int(sizeof(env))) }
env.defaults.color_format = gfx.PixelFormat.from(color_format()) or { gfx.PixelFormat.@none }
env.defaults.depth_format = gfx.PixelFormat.from(depth_format()) or { gfx.PixelFormat.@none }
env.defaults.sample_count = sample_count()
$if macos && !darwin_sokol_glcore33 ? {
env.metal.device = metal_get_device()
}
// if windows and dx3d11
// env.d3d11.device = d3d11_get_device()
// env.d3d11.device_context = d3d11_get_device_context()
// if webgpu
// env.wgpu.device = wgpu_get_device()
return env
}
// glue_swapchain returns a `gfx.Swapchain` compatible for use with `sapp` specific display/rendering `gfx.Pass`es.
// The retuned `gfx.Swapchain` can be used when rendering via `sapp`.
// See also: documentation at the top of thirdparty/sokol/sokol_gfx.h
pub fn glue_swapchain() gfx.Swapchain {
mut swapchain := gfx.Swapchain{}
unsafe { vmemset(&swapchain, 0, int(sizeof(swapchain))) }
swapchain.width = width()
swapchain.height = height()
swapchain.sample_count = sample_count()
swapchain.color_format = gfx.PixelFormat.from(color_format()) or { gfx.PixelFormat.@none }
swapchain.depth_format = gfx.PixelFormat.from(depth_format()) or { gfx.PixelFormat.@none }
$if macos && !darwin_sokol_glcore33 ? {
swapchain.metal.current_drawable = metal_get_current_drawable()
swapchain.metal.depth_stencil_texture = metal_get_depth_stencil_texture()
swapchain.metal.msaa_color_texture = metal_get_msaa_color_texture()
}
// if windows and dx3d11
// swapchain.d3d11.render_view = d3d11_get_render_view()
// swapchain.d3d11.resolve_view = d3d11_get_resolve_view()
// swapchain.d3d11.depth_stencil_view = d3d11_get_depth_stencil_view()
// if webgpu
// swapchain.wgpu.render_view = wgpu_get_render_view()
// swapchain.wgpu.resolve_view = wgpu_get_resolve_view()
// swapchain.wgpu.depth_stencil_view = wgpu_get_depth_stencil_view()
$else {
swapchain.gl.framebuffer = gl_get_framebuffer()
}
return swapchain
}
// returns true after sokol-app has been initialized // returns true after sokol-app has been initialized
@[inline] @[inline]
pub fn isvalid() bool { pub fn isvalid() bool {
@ -210,16 +252,19 @@ pub fn metal_get_device() voidptr {
return voidptr(C.sapp_metal_get_device()) return voidptr(C.sapp_metal_get_device())
} }
// Metal: get ARC-bridged pointer to this frame's renderpass descriptor // Metal: get ARC-bridged pointer to current drawable
@[inline] pub fn metal_get_current_drawable() voidptr {
pub fn metal_get_renderpass_descriptor() voidptr { return C.sapp_metal_get_current_drawable()
return voidptr(C.sapp_metal_get_renderpass_descriptor())
} }
// Metal: get ARC-bridged pointer to current drawable // Metal: get bridged pointer to MTKView's depth-stencil texture of type MTLTexture
@[inline] pub fn metal_get_depth_stencil_texture() voidptr {
pub fn metal_get_drawable() voidptr { return C.sapp_metal_get_depth_stencil_texture()
return voidptr(C.sapp_metal_get_drawable()) }
// Metal: get bridged pointer to MTKView's msaa-color-texture of type MTLTexture (may be null)
pub fn metal_get_msaa_color_texture() voidptr {
return C.sapp_metal_get_msaa_color_texture()
} }
// macOS: get ARC-bridged pointer to macOS NSWindow // macOS: get ARC-bridged pointer to macOS NSWindow
@ -246,10 +291,16 @@ pub fn d3d11_get_device_context() voidptr {
return voidptr(C.sapp_d3d11_get_device_context()) return voidptr(C.sapp_d3d11_get_device_context())
} }
// D3D11: get pointer to ID3D11RenderTargetView object // D3D11: get pointer to ID3D11RenderView object
@[inline] @[inline]
pub fn d3d11_get_render_target_view() voidptr { pub fn d3d11_get_render_view() voidptr {
return voidptr(C.sapp_d3d11_get_render_target_view()) return voidptr(C.sapp_d3d11_get_render_view())
}
// D3D11: get pointer ID3D11RenderTargetView object for msaa-resolve (may return null)
@[inline]
pub fn d3d11_get_resolve_view() voidptr {
return C.sapp_d3d11_get_resolve_view()
} }
// D3D11: get pointer to ID3D11DepthStencilView // D3D11: get pointer to ID3D11DepthStencilView
@ -264,6 +315,32 @@ pub fn win32_get_hwnd() voidptr {
return voidptr(C.sapp_win32_get_hwnd()) return voidptr(C.sapp_win32_get_hwnd())
} }
// WebGPU: get WGPUDevice handle
pub fn wgpu_get_device() voidptr {
return C.sapp_wgpu_get_device()
}
// WebGPU: get swapchain's WGPUTextureView handle for rendering
pub fn wgpu_get_render_view() voidptr {
return C.sapp_wgpu_get_render_view()
}
// WebGPU: get swapchain's MSAA-resolve WGPUTextureView (may return null)
pub fn wgpu_get_resolve_view() voidptr {
return C.sapp_wgpu_get_resolve_view()
}
// WebGPU: get swapchain's WGPUTextureView for the depth-stencil surface
pub fn wgpu_get_depth_stencil_view() voidptr {
return C.sapp_wgpu_get_depth_stencil_view()
}
// GL: get framebuffer object
@[inline]
pub fn gl_get_framebuffer() u32 {
return C.sapp_gl_get_framebuffer()
}
// Android: get native activity handle // Android: get native activity handle
@[inline] @[inline]
pub fn android_get_native_activity() voidptr { pub fn android_get_native_activity() voidptr {

View file

@ -110,11 +110,14 @@ fn C.sapp_html5_ask_leave_site(ask bool)
// Metal: get ARC-bridged pointer to Metal device object // Metal: get ARC-bridged pointer to Metal device object
fn C.sapp_metal_get_device() voidptr fn C.sapp_metal_get_device() voidptr
// Metal: get ARC-bridged pointer to this frame's renderpass descriptor
fn C.sapp_metal_get_renderpass_descriptor() voidptr
// Metal: get ARC-bridged pointer to current drawable // Metal: get ARC-bridged pointer to current drawable
fn C.sapp_metal_get_drawable() voidptr fn C.sapp_metal_get_current_drawable() voidptr
// Metal: get bridged pointer to MTKView's depth-stencil texture of type MTLTexture
fn C.sapp_metal_get_depth_stencil_texture() voidptr
// Metal: get bridged pointer to MTKView's msaa-color-texture of type MTLTexture (may be null)
fn C.sapp_metal_get_msaa_color_texture() voidptr
// macOS: get ARC-bridged pointer to macOS NSWindow // macOS: get ARC-bridged pointer to macOS NSWindow
fn C.sapp_macos_get_window() voidptr fn C.sapp_macos_get_window() voidptr
@ -131,8 +134,11 @@ fn C.sapp_d3d11_get_device_context() voidptr
// D3D11: get pointer to IDXGISwapChain object // D3D11: get pointer to IDXGISwapChain object
fn C.sapp_d3d11_get_swap_chain() voidptr fn C.sapp_d3d11_get_swap_chain() voidptr
// D3D11: get pointer to ID3D11RenderTargetView object // D3D11: get pointer to ID3D11RenderView object
fn C.sapp_d3d11_get_render_target_view() voidptr fn C.sapp_d3d11_get_render_view() voidptr
// D3D11: get pointer ID3D11RenderTargetView object for msaa-resolve (may return null)
fn C.sapp_d3d11_get_resolve_view() voidptr
// D3D11: get pointer to ID3D11DepthStencilView // D3D11: get pointer to ID3D11DepthStencilView
fn C.sapp_d3d11_get_depth_stencil_view() voidptr fn C.sapp_d3d11_get_depth_stencil_view() voidptr
@ -152,5 +158,8 @@ fn C.sapp_wgpu_get_resolve_view() voidptr
// WebGPU: get swapchain's WGPUTextureView for the depth-stencil surface // WebGPU: get swapchain's WGPUTextureView for the depth-stencil surface
fn C.sapp_wgpu_get_depth_stencil_view() voidptr fn C.sapp_wgpu_get_depth_stencil_view() voidptr
// GL: get framebuffer object
fn C.sapp_gl_get_framebuffer() u32
// Android: get native activity handle // Android: get native activity handle
fn C.sapp_android_get_native_activity() voidptr fn C.sapp_android_get_native_activity() voidptr