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

vfmt: implement support for // vfmt off and // vfmt on, with it, v fmt -w . now works. (#16335)

This commit is contained in:
Delyan Angelov
2022-11-05 08:08:01 +02:00
committed by GitHub
parent 131d07aede
commit b52b8429d4
21 changed files with 760 additions and 503 deletions

View File

@ -122,6 +122,7 @@ fn init_cube_glsl(mut app App) {
// d := u16(32767) // for compatibility with D3D11, 32767 stand for 1
d := f32(1.0)
c := u32(0xFFFFFF_FF) // color RGBA8
// vfmt off
vertices := [
// Face 0
Vertex_t{-1.0, -1.0, -1.0, c, 0, 0},
@ -154,8 +155,11 @@ fn init_cube_glsl(mut app App) {
Vertex_t{ 1.0, 1.0, 1.0, c, d, d},
Vertex_t{ 1.0, 1.0, -1.0, c, 0, d},
]
// vfmt on
mut vert_buffer_desc := gfx.BufferDesc{label: c'cube-vertices'}
mut vert_buffer_desc := gfx.BufferDesc{
label: c'cube-vertices'
}
unsafe { vmemset(&vert_buffer_desc, 0, int(sizeof(vert_buffer_desc))) }
vert_buffer_desc.size = usize(vertices.len * int(sizeof(Vertex_t)))
@ -168,17 +172,21 @@ fn init_cube_glsl(mut app App) {
vbuf := gfx.make_buffer(&vert_buffer_desc)
// create an index buffer for the cube
// vfmt off
indices := [
u16(0), 1, 2, 0, 2, 3,
6, 5, 4, 7, 6, 4,
8, 9, 10, 8, 10, 11,
14, 13, 12, 15, 14, 12,
16, 17, 18, 16, 18, 19,
22, 21, 20, 23, 22, 20,
u16(0), 1, 2, 0, 2, 3,
6, 5, 4, 7, 6, 4,
8, 9, 10, 8, 10, 11,
14, 13, 12, 15, 14, 12,
16, 17, 18, 16, 18, 19,
22, 21, 20, 23, 22, 20,
]
// vfmt on
mut index_buffer_desc := gfx.BufferDesc{label: c'cube-indices'}
unsafe {vmemset(&index_buffer_desc, 0, int(sizeof(index_buffer_desc)))}
mut index_buffer_desc := gfx.BufferDesc{
label: c'cube-indices'
}
unsafe { vmemset(&index_buffer_desc, 0, int(sizeof(index_buffer_desc))) }
index_buffer_desc.size = usize(indices.len * int(sizeof(u16)))
index_buffer_desc.data = gfx.Range{
@ -186,7 +194,7 @@ fn init_cube_glsl(mut app App) {
size: usize(indices.len * int(sizeof(u16)))
}
index_buffer_desc.@type = .indexbuffer
index_buffer_desc.@type = .indexbuffer
ibuf := gfx.make_buffer(&index_buffer_desc)
// create shader
@ -197,9 +205,9 @@ fn init_cube_glsl(mut app App) {
pipdesc.layout.buffers[0].stride = int(sizeof(Vertex_t))
// the constants [C.ATTR_vs_pos, C.ATTR_vs_color0, C.ATTR_vs_texcoord0] are generated by sokol-shdc
pipdesc.layout.attrs[C.ATTR_vs_pos ].format = .float3 // x,y,z as f32
pipdesc.layout.attrs[C.ATTR_vs_color0 ].format = .ubyte4n // color as u32
pipdesc.layout.attrs[C.ATTR_vs_texcoord0].format = .float2 // u,v as f32
pipdesc.layout.attrs[C.ATTR_vs_pos].format = .float3 // x,y,z as f32
pipdesc.layout.attrs[C.ATTR_vs_color0].format = .ubyte4n // color as u32
pipdesc.layout.attrs[C.ATTR_vs_texcoord0].format = .float2 // u,v as f32
// pipdesc.layout.attrs[C.ATTR_vs_texcoord0].format = .short2n // u,v as u16
pipdesc.shader = shader
@ -222,21 +230,24 @@ fn init_cube_glsl(mut app App) {
[inline]
fn vec4(x f32, y f32, z f32, w f32) m4.Vec4 {
return m4.Vec4{e:[x, y, z, w]!}
return m4.Vec4{
e: [x, y, z, w]!
}
}
fn calc_tr_matrices(w f32, h f32, rx f32, ry f32, in_scale f32) m4.Mat4 {
proj := m4.perspective(60, w/h, 0.01, 10.0)
view := m4.look_at(vec4(f32(0.0) ,0 , 6, 0), vec4(f32(0), 0, 0, 0), vec4(f32(0), 1, 0, 0))
proj := m4.perspective(60, w / h, 0.01, 10.0)
view := m4.look_at(vec4(f32(0.0), 0, 6, 0), vec4(f32(0), 0, 0, 0), vec4(f32(0), 1,
0, 0))
view_proj := view * proj
rxm := m4.rotate(m4.rad(rx), vec4(f32(1), 0, 0, 0))
rym := m4.rotate(m4.rad(ry), vec4(f32(0), 1, 0, 0))
model := rym * rxm
model := rym * rxm
scale_m := m4.scale(vec4(in_scale, in_scale, in_scale, 1))
res := (scale_m * model) * view_proj
res := (scale_m * model) * view_proj
return res
}
@ -270,6 +281,7 @@ fn draw_cube_glsl(app App) {
}
gfx.apply_uniforms(.vs, C.SLOT_vs_params, &vs_uniforms_range)
// vfmt off
// *** fragment shader uniforms ***
time_ticks := f32(time.ticks() - app.ticks) / 1000
mut tmp_fs_params := [
@ -282,6 +294,7 @@ fn draw_cube_glsl(app App) {
0,
0 // padding bytes , see "fs_params" struct paddings in rt_glsl.h
]!
// vfmt on
fs_uniforms_range := gfx.Range{
ptr: unsafe { &tmp_fs_params }
size: usize(sizeof(tmp_fs_params))
@ -355,9 +368,9 @@ fn my_init(mut app App) {
tmp_txt[i + 3] = u8(0xFF)
} else {
col := if ((x + y) & 1) == 1 { 0xFF } else { 128 }
tmp_txt[i + 0] = u8(col) // red
tmp_txt[i + 1] = u8(col) // green
tmp_txt[i + 2] = u8(col) // blue
tmp_txt[i + 0] = u8(col) // red
tmp_txt[i + 1] = u8(col) // green
tmp_txt[i + 2] = u8(col) // blue
tmp_txt[i + 3] = u8(0xFF) // alpha
}
i += 4