1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

sokol: type alias all gfx structs (#13014)

This commit is contained in:
Larpon
2022-01-02 19:36:01 +01:00
committed by GitHub
parent 41e763f79c
commit 4d4398fa8a
25 changed files with 531 additions and 431 deletions

View File

@@ -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
@@ -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
}
@@ -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
@@ -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)
}
/******************************************************************************
@@ -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)))
}
@@ -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)))
}
@@ -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))
@@ -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
@@ -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)
}
@@ -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)
}
@@ -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)
@@ -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