Skip to content

Commit

Permalink
sokol: type alias all gfx structs (#13014)
Browse files Browse the repository at this point in the history
  • Loading branch information
larpon authored Jan 2, 2022
1 parent 41e763f commit 4d4398f
Show file tree
Hide file tree
Showing 25 changed files with 531 additions and 431 deletions.
2 changes: 1 addition & 1 deletion cmd/tools/vshader.v
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ fn compile_shader(opt CompileOptions, shader_file string) ? {
}
if opt.verbose {
program_name := shader_program_name(shader_file)
eprintln('$tool_name usage example in V:\n\nimport sokol.gfx\n\n#include "$header_name"\n\nfn C.${program_name}_shader_desc(gfx.Backend) &C.sg_shader_desc\n')
eprintln('$tool_name usage example in V:\n\nimport sokol.gfx\n\n#include "$header_name"\n\nfn C.${program_name}_shader_desc(gfx.Backend) &gfx.ShaderDesc\n')
}
}

Expand Down
36 changes: 18 additions & 18 deletions examples/sokol/01_cubes/cube.v
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ struct App {
mut:
gg &gg.Context
pip_3d C.sgl_pipeline
texture C.sg_image
texture gfx.Image
init_flag bool
frame_count int
mouse_x int = -1
Expand All @@ -39,9 +39,9 @@ mut:
* Texture functions
*
******************************************************************************/
fn create_texture(w int, h int, buf &u8) C.sg_image {
fn create_texture(w int, h int, buf &u8) gfx.Image {
sz := w * h * 4
mut img_desc := C.sg_image_desc{
mut img_desc := gfx.ImageDesc{
width: w
height: h
num_mipmaps: 0
Expand All @@ -54,28 +54,28 @@ fn create_texture(w int, h int, buf &u8) C.sg_image {
d3d11_texture: 0
}
// commen if .dynamic is enabled
img_desc.data.subimage[0][0] = C.sg_range{
img_desc.data.subimage[0][0] = gfx.Range{
ptr: buf
size: usize(sz)
}

sg_img := C.sg_make_image(&img_desc)
sg_img := gfx.make_image(&img_desc)
return sg_img
}

fn destroy_texture(sg_img C.sg_image) {
C.sg_destroy_image(sg_img)
fn destroy_texture(sg_img gfx.Image) {
gfx.destroy_image(sg_img)
}

// Use only if usage: .dynamic is enabled
fn update_text_texture(sg_img C.sg_image, w int, h int, buf &byte) {
fn update_text_texture(sg_img gfx.Image, w int, h int, buf &byte) {
sz := w * h * 4
mut tmp_sbc := C.sg_image_data{}
tmp_sbc.subimage[0][0] = C.sg_range{
mut tmp_sbc := gfx.ImageData{}
tmp_sbc.subimage[0][0] = gfx.Range{
ptr: buf
size: usize(sz)
}
C.sg_update_image(sg_img, &tmp_sbc)
gfx.update_image(sg_img, &tmp_sbc)
}

/******************************************************************************
Expand Down Expand Up @@ -321,21 +321,21 @@ fn my_init(mut app App) {
sgl.setup(&sgl_desc)

// 3d pipeline
mut pipdesc := C.sg_pipeline_desc{}
mut pipdesc := gfx.PipelineDesc{}
unsafe { C.memset(&pipdesc, 0, sizeof(pipdesc)) }

color_state := C.sg_color_state{
blend: C.sg_blend_state{
color_state := gfx.ColorState{
blend: gfx.BlendState{
enabled: true
src_factor_rgb: gfx.BlendFactor(C.SG_BLENDFACTOR_SRC_ALPHA)
dst_factor_rgb: gfx.BlendFactor(C.SG_BLENDFACTOR_ONE_MINUS_SRC_ALPHA)
src_factor_rgb: .src_alpha
dst_factor_rgb: .one_minus_src_alpha
}
}
pipdesc.colors[0] = color_state

pipdesc.depth = C.sg_depth_state{
pipdesc.depth = gfx.DepthState{
write_enabled: true
compare: gfx.CompareFunc(C.SG_COMPAREFUNC_LESS_EQUAL)
compare: .less_equal
}
pipdesc.cull_mode = .back
app.pip_3d = sgl.make_pipeline(&pipdesc)
Expand Down
66 changes: 33 additions & 33 deletions examples/sokol/02_cubes_glsl/cube_glsl.v
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import gg.m4
#flag -I @VMODROOT/.
#include "cube_glsl.h" # Should be generated with `v shader .` (see the instructions at the top of this file)

fn C.cube_shader_desc(gfx.Backend) &C.sg_shader_desc
fn C.cube_shader_desc(gfx.Backend) &gfx.ShaderDesc

const (
win_width = 800
Expand All @@ -38,14 +38,14 @@ struct App {
mut:
gg &gg.Context
pip_3d C.sgl_pipeline
texture C.sg_image
texture gfx.Image
init_flag bool
frame_count int
mouse_x int = -1
mouse_y int = -1
// glsl
cube_pip_glsl C.sg_pipeline
cube_bind C.sg_bindings
cube_pip_glsl gfx.Pipeline
cube_bind gfx.Bindings
// time
ticks i64
}
Expand All @@ -55,9 +55,9 @@ mut:
* Texture functions
*
******************************************************************************/
fn create_texture(w int, h int, buf &byte) C.sg_image {
fn create_texture(w int, h int, buf &byte) gfx.Image {
sz := w * h * 4
mut img_desc := C.sg_image_desc{
mut img_desc := gfx.ImageDesc{
width: w
height: h
num_mipmaps: 0
Expand All @@ -70,28 +70,28 @@ fn create_texture(w int, h int, buf &byte) C.sg_image {
d3d11_texture: 0
}
// comment if .dynamic is enabled
img_desc.data.subimage[0][0] = C.sg_range{
img_desc.data.subimage[0][0] = gfx.Range{
ptr: buf
size: usize(sz)
}

sg_img := C.sg_make_image(&img_desc)
sg_img := gfx.make_image(&img_desc)
return sg_img
}

fn destroy_texture(sg_img C.sg_image) {
C.sg_destroy_image(sg_img)
fn destroy_texture(sg_img gfx.Image) {
gfx.destroy_image(sg_img)
}

// Use only if usage: .dynamic is enabled
fn update_text_texture(sg_img C.sg_image, w int, h int, buf &byte) {
fn update_text_texture(sg_img gfx.Image, w int, h int, buf &byte) {
sz := w * h * 4
mut tmp_sbc := C.sg_image_data{}
tmp_sbc.subimage[0][0] = C.sg_range{
mut tmp_sbc := gfx.ImageData{}
tmp_sbc.subimage[0][0] = gfx.Range{
ptr: buf
size: usize(sz)
}
C.sg_update_image(sg_img, &tmp_sbc)
gfx.update_image(sg_img, &tmp_sbc)
}

/******************************************************************************
Expand Down Expand Up @@ -276,11 +276,11 @@ fn init_cube_glsl(mut app App) {
Vertex_t{ 1.0, 1.0, -1.0, c, 0, d},
]

mut vert_buffer_desc := C.sg_buffer_desc{label: c'cube-vertices'}
mut vert_buffer_desc := gfx.BufferDesc{label: c'cube-vertices'}
unsafe { C.memset(&vert_buffer_desc, 0, sizeof(vert_buffer_desc)) }

vert_buffer_desc.size = usize(vertices.len * int(sizeof(Vertex_t)))
vert_buffer_desc.data = C.sg_range{
vert_buffer_desc.data = gfx.Range{
ptr: vertices.data
size: usize(vertices.len * int(sizeof(Vertex_t)))
}
Expand All @@ -299,11 +299,11 @@ fn init_cube_glsl(mut app App) {
22, 21, 20, 23, 22, 20
]

mut index_buffer_desc := C.sg_buffer_desc{label: c'cube-indices'}
mut index_buffer_desc := gfx.BufferDesc{label: c'cube-indices'}
unsafe { C.memset(&index_buffer_desc, 0, sizeof(index_buffer_desc)) }

index_buffer_desc.size = usize(indices.len * int(sizeof(u16)))
index_buffer_desc.data = C.sg_range{
index_buffer_desc.data = gfx.Range{
ptr: indices.data
size: usize(indices.len * int(sizeof(u16)))
}
Expand All @@ -314,7 +314,7 @@ fn init_cube_glsl(mut app App) {
// create shader
shader := gfx.make_shader(C.cube_shader_desc(C.sg_query_backend()))

mut pipdesc := C.sg_pipeline_desc{}
mut pipdesc := gfx.PipelineDesc{}
unsafe { C.memset(&pipdesc, 0, sizeof(pipdesc)) }

pipdesc.layout.buffers[0].stride = int(sizeof(Vertex_t))
Expand All @@ -327,9 +327,9 @@ fn init_cube_glsl(mut app App) {
pipdesc.shader = shader
pipdesc.index_type = .uint16

pipdesc.depth = C.sg_depth_state{
pipdesc.depth = gfx.DepthState{
write_enabled: true
compare: gfx.CompareFunc(C.SG_COMPAREFUNC_LESS_EQUAL)
compare: .less_equal
}
pipdesc.cull_mode = .back

Expand Down Expand Up @@ -366,7 +366,7 @@ fn draw_cube_glsl(app App) {
//***************
// passing the view matrix as uniform
// res is a 4x4 matrix of f32 thus: 4*16 byte of size
vs_uniforms_range := C.sg_range{
vs_uniforms_range := gfx.Range{
ptr: &tr_matrix
size: usize(4 * 16)
}
Expand All @@ -380,7 +380,7 @@ fn draw_cube_glsl(app App) {
time_ticks, /* time as f32 */
0 /* padding 4 Bytes == 1 f32 */,
]!
fs_uniforms_range := C.sg_range{
fs_uniforms_range := gfx.Range{
ptr: unsafe { &text_res }
size: usize(4 * 4)
}
Expand Down Expand Up @@ -457,16 +457,16 @@ fn frame(mut app App) {
app.gg.end()

// clear
mut color_action := C.sg_color_attachment_action{
mut color_action := gfx.ColorAttachmentAction{
action: gfx.Action(C.SG_ACTION_DONTCARE) // C.SG_ACTION_CLEAR)
value: C.sg_color{
value: gfx.Color{
r: 1.0
g: 1.0
b: 1.0
a: 1.0
}
}
mut pass_action := C.sg_pass_action{}
mut pass_action := gfx.PassAction{}
pass_action.colors[0] = color_action
gfx.begin_default_pass(&pass_action, ws.width, ws.height)

Expand All @@ -492,21 +492,21 @@ fn my_init(mut app App) {
sgl.setup(&sgl_desc)

// 3d pipeline
mut pipdesc := C.sg_pipeline_desc{}
mut pipdesc := gfx.PipelineDesc{}
unsafe { C.memset(&pipdesc, 0, sizeof(pipdesc)) }

color_state := C.sg_color_state{
blend: C.sg_blend_state{
color_state := gfx.ColorState{
blend: gfx.BlendState{
enabled: true
src_factor_rgb: gfx.BlendFactor(C.SG_BLENDFACTOR_SRC_ALPHA)
dst_factor_rgb: gfx.BlendFactor(C.SG_BLENDFACTOR_ONE_MINUS_SRC_ALPHA)
src_factor_rgb: .src_alpha
dst_factor_rgb: .one_minus_src_alpha
}
}
pipdesc.colors[0] = color_state

pipdesc.depth = C.sg_depth_state{
pipdesc.depth = gfx.DepthState{
write_enabled: true
compare: gfx.CompareFunc(C.SG_COMPAREFUNC_LESS_EQUAL)
compare: .less_equal
}
pipdesc.cull_mode = .back

Expand Down
Loading

0 comments on commit 4d4398f

Please sign in to comment.