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

@@ -26,6 +26,7 @@ import time
#flag -I @VMODROOT/.
#include "rt_glsl_march.h" # Should be generated with `v shader .` (see the instructions at the top of this file)
#include "rt_glsl_puppy.h" # Should be generated with `v shader .` (see the instructions at the top of this file)
fn C.rt_march_shader_desc(gfx.Backend) &gfx.ShaderDesc
fn C.rt_puppy_shader_desc(gfx.Backend) &gfx.ShaderDesc
@@ -45,10 +46,10 @@ mut:
mouse_y int = -1
mouse_down bool
// glsl
cube_pip_glsl gfx.Pipeline
cube_bind gfx.Bindings
pipe map[string]gfx.Pipeline
bind map[string]gfx.Bindings
cube_pip_glsl gfx.Pipeline
cube_bind gfx.Bindings
pipe map[string]gfx.Pipeline
bind map[string]gfx.Bindings
// time
ticks i64
}
@@ -124,6 +125,7 @@ fn init_cube_glsl_m(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},
@@ -156,8 +158,11 @@ fn init_cube_glsl_m(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)))
vert_buffer_desc.data = gfx.Range{
@@ -167,19 +172,23 @@ fn init_cube_glsl_m(mut app App) {
vert_buffer_desc.@type = .vertexbuffer
vbuf := gfx.make_buffer(&vert_buffer_desc)
/* create an index buffer for the cube */
// 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,
u16(0), 1, 2, 0, 2, 3,
6 , 5, 4, 7, 6, 4,
8 , 9, 10, 8, 10, 11,
/*
u16(14), 13, 12, 15, 14, 12,
16, 17, 18, 16, 18, 19,
22, 21, 20, 23, 22, 20
u16(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'}
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{
@@ -196,11 +205,13 @@ fn init_cube_glsl_m(mut app App) {
unsafe { vmemset(&pipdesc, 0, int(sizeof(pipdesc))) }
pipdesc.layout.buffers[0].stride = int(sizeof(Vertex_t))
// vfmt off
// the constants [C.ATTR_vs_m_pos, C.ATTR_vs_m_color0, C.ATTR_vs_m_texcoord0] are generated by sokol-shdc
pipdesc.layout.attrs[C.ATTR_vs_m_pos ].format = .float3 // x,y,z as f32
pipdesc.layout.attrs[C.ATTR_vs_m_color0 ].format = .ubyte4n // color as u32
pipdesc.layout.attrs[C.ATTR_vs_m_texcoord0].format = .float2 // u,v as f32
// pipdesc.layout.attrs[C.ATTR_vs_m_texcoord0].format = .short2n // u,v as u16
// vfmt on
pipdesc.shader = shader
pipdesc.index_type = .uint16
@@ -230,6 +241,7 @@ fn init_cube_glsl_p(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},
@@ -262,6 +274,7 @@ fn init_cube_glsl_p(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 off
mut vert_buffer_desc := gfx.BufferDesc{label: c'cube-vertices'}
unsafe { vmemset(&vert_buffer_desc, 0, int(sizeof(vert_buffer_desc))) }
@@ -274,19 +287,22 @@ fn init_cube_glsl_p(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,
u16(0), 1, 2, 0, 2, 3,
6, 5, 4, 7, 6, 4,
8, 9, 10, 8, 10, 11,
*/
u16(14), 13, 12, 15, 14, 12,
16, 17, 18, 16, 18, 19,
22, 21, 20, 23, 22, 20
u16(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'}
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{
@@ -303,11 +319,13 @@ fn init_cube_glsl_p(mut app App) {
unsafe { vmemset(&pipdesc, 0, int(sizeof(pipdesc))) }
pipdesc.layout.buffers[0].stride = int(sizeof(Vertex_t))
// vfmt off
// the constants [C.ATTR_vs_p_pos, C.ATTR_vs_p_color0, C.ATTR_vs_p_texcoord0] are generated by sokol-shdc
pipdesc.layout.attrs[C.ATTR_vs_p_pos ].format = .float3 // x,y,z as f32
pipdesc.layout.attrs[C.ATTR_vs_p_color0 ].format = .ubyte4n // color as u32
pipdesc.layout.attrs[C.ATTR_vs_p_texcoord0].format = .float2 // u,v as f32
// pipdesc.layout.attrs[C.ATTR_vs_p_texcoord0].format = .short2n // u,v as u16
// vfmt on
pipdesc.shader = shader
pipdesc.index_type = .uint16
@@ -334,21 +352,24 @@ fn init_cube_glsl_p(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
}
@@ -381,6 +402,7 @@ fn draw_cube_glsl_m(app App) {
// *** fragment shader uniforms ***
time_ticks := f32(time.ticks() - app.ticks) / 1000
// vfmt off
mut tmp_fs_params := [
f32(ws.width),
ws.height * ratio, // x,y resolution to pass to FS
@@ -393,6 +415,7 @@ fn draw_cube_glsl_m(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))
@@ -438,12 +461,12 @@ fn draw_cube_glsl_p(app App) {
ws.height * ratio, // x,y resolution to pass to FS
0,
0, // dont send mouse position
/* app.mouse_x, // mouse x */
/* ws.height - app.mouse_y*2, // mouse y scaled */
time_ticks, // time as f32
// app.mouse_x, // mouse x
// ws.height - app.mouse_y*2, // mouse y scaled
time_ticks, // time as f32
app.frame_count, // frame count
0,
0 // padding bytes , see "fs_params" struct paddings in rt_glsl.h
0, // padding bytes , see "fs_params" struct paddings in rt_glsl.h
]!
fs_uniforms_range := gfx.Range{
ptr: unsafe { &tmp_fs_params }
@@ -543,9 +566,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