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
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
21 changed files with 760 additions and 503 deletions

View File

@ -1,3 +1,6 @@
## V 0.3.3
-*Not yet released*
- vfmt now supports `// vfmt off` and `// vfmt on` for turning off the formatting locally for *short* snippets of code. Useful for keeping your carefully arranged matrices in tact.
## V 0.3.2
*31 Oct 2022*
- New simplified string interpolation: `println("Hello, {name}!")`. It will be the only way, old syntax (`${name}` and `$name`)

View File

@ -151,13 +151,11 @@ pub fn (mut ts TestSession) print_messages() {
pub fn new_test_session(_vargs string, will_compile bool) TestSession {
mut skip_files := []string{}
if will_compile {
// Skip the call_v_from_c files. They need special instructions for compilation.
// Check the README.md for detailed information.
skip_files << 'examples/call_v_from_c/v_test_print.v'
skip_files << 'examples/call_v_from_c/v_test_math.v'
if will_compile {
$if msvc {
skip_files << 'vlib/v/tests/const_comptime_eval_before_vinit_test.v' // _constructor used
}

View File

@ -181,7 +181,7 @@ fn (foptions &FormatOptions) format_pipe() {
input_text := os.get_raw_lines_joined()
file_ast := parser.parse_text(input_text, '', table, .parse_comments, prefs)
// checker.new_checker(table, prefs).check(file_ast)
formatted_content := fmt.fmt(file_ast, table, prefs, foptions.is_debug)
formatted_content := fmt.fmt(file_ast, table, prefs, foptions.is_debug, source_text: input_text)
print(formatted_content)
flush_stdout()
foptions.vlog('fmt.fmt worked and $formatted_content.len bytes were written to stdout.')

View File

@ -33,20 +33,7 @@ const vet_folders = [
'examples/term.ui',
]
const verify_known_failing_exceptions = [
// Handcrafted meaningful formatting of code parts (mostly arrays)
'examples/sokol/02_cubes_glsl/cube_glsl.v',
'examples/sokol/03_march_tracing_glsl/rt_glsl.v',
'examples/sokol/04_multi_shader_glsl/rt_glsl.v',
'examples/sokol/05_instancing_glsl/rt_glsl.v',
'vlib/v/checker/tests/modules/deprecated_module/main.v' /* adds deprecated_module. module prefix to imports, even though the folder has v.mod */,
'vlib/gg/m4/graphic.v',
'vlib/gg/m4/m4_test.v',
'vlib/gg/m4/matrix.v'
// TODOs and unfixed vfmt bugs
'vlib/v/tests/inout/string_interpolation_inner_expr_cbr.vv', /* for new string interpolation, prevent resolving to nested interpolation */
'vlib/v/tests/string_new_interpolation_test.v', /* new string interpolation */
]
const verify_known_failing_exceptions = []string{}
const vfmt_verify_list = [
'cmd/',
@ -55,11 +42,7 @@ const vfmt_verify_list = [
'vlib/',
]
const vfmt_known_failing_exceptions = arrays.merge(verify_known_failing_exceptions, [
'vlib/regex/regex_test.v' /* contains meaningfull formatting of the test case data */,
'vlib/crypto/sha512/sha512block_generic.v' /* formatting of large constant arrays wraps to too many lines */,
'vlib/crypto/aes/const.v' /* formatting of large constant arrays wraps to too many lines */,
])
const vfmt_known_failing_exceptions = arrays.merge(verify_known_failing_exceptions, []string{})
const vexe = os.getenv('VEXE')

View File

@ -103,9 +103,11 @@ fn draw_triangle() {
sgl.defaults()
sgl.begin_triangles()
{
// vfmt off
sgl.v2f_c3b( 0.0, 0.5, 255, 0 , 0 )
sgl.v2f_c3b(-0.5, -0.5, 0, 0 , 255)
sgl.v2f_c3b( 0.5, -0.5, 0, 255, 0 )
// vfmt on
}
sgl.end()
}
@ -114,9 +116,9 @@ fn draw_triangle() {
fn cube() {
sgl.begin_quads()
{
// edge color
sgl.c3f(1.0, 0.0, 0.0)
// edge coord
// vfmt off
sgl.c3f(1.0, 0.0, 0.0) // edge color
// edge coordinates
// x,y,z, texture cord: u,v
sgl.v3f_t2f(-1.0, 1.0, -1.0, -1.0, 1.0)
sgl.v3f_t2f( 1.0, 1.0, -1.0, 1.0, 1.0)
@ -147,6 +149,7 @@ fn cube() {
sgl.v3f_t2f(-1.0, 1.0, 1.0, 1.0, 1.0)
sgl.v3f_t2f( 1.0, 1.0, 1.0, 1.0, -1.0)
sgl.v3f_t2f( 1.0, 1.0, -1.0, -1.0, -1.0)
// vfmt on
}
sgl.end()
}
@ -177,8 +180,10 @@ fn draw_cubes(app App) {
{
sgl.translate(0.0, 0.0, 3.0)
sgl.scale(0.5, 0.5, 0.5)
// vfmt off
sgl.rotate(-3.0 * sgl.rad(2 * rot[0]), 1.0, 0.0, 0.0)
sgl.rotate( 3.0 * sgl.rad(2 * rot[1]), 0.0, 0.0, 1.0)
// vfmt on
cube()
}
sgl.pop_matrix()
@ -189,10 +194,10 @@ fn draw_cubes(app App) {
fn cube_texture(r f32, g f32, b f32) {
sgl.begin_quads()
{
// edge color
sgl.c3f(r, g, b)
sgl.c3f(r, g, b) // edge color
// edge coord
// x,y,z, texture cord: u,v
// vfmt off
sgl.v3f_t2f(-1.0, 1.0, -1.0, 0.0 , 0.25)
sgl.v3f_t2f( 1.0, 1.0, -1.0, 0.25, 0.25)
sgl.v3f_t2f( 1.0, -1.0, -1.0, 0.25, 0.0 )
@ -222,6 +227,7 @@ fn cube_texture(r f32, g f32, b f32) {
sgl.v3f_t2f(-1.0, 1.0, 1.0, 0.25, 0.25)
sgl.v3f_t2f( 1.0, 1.0, 1.0, 0.25, 0.0 )
sgl.v3f_t2f( 1.0, 1.0, -1.0, 0.0 , 0.0 )
// vfmt on
}
sgl.end()
}
@ -253,6 +259,7 @@ fn init_cube_glsl(mut app App) {
// d := u16(32767/8) // for compatibility with D3D11, 32767 stand for 1
d := f32(1.0) // 0.05)
c := u32(0xFFFFFF_FF) // color RGBA8
// vfmt off
vertices := [
// Face 0
Vertex_t{-1.0, -1.0, -1.0, c, 0, 0},
@ -285,8 +292,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)))
@ -300,14 +310,16 @@ 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
22, 21, 20, 23, 22, 20,
]
// vfmt on
mut index_buffer_desc := gfx.BufferDesc{
label: c'cube-indices'
@ -443,8 +455,10 @@ fn draw_texture_cubes(app App) {
{
sgl.translate(0.0, 0.0, 3.0)
sgl.scale(0.5, 0.5, 0.5)
// vfmt off
sgl.rotate(-3.0 * sgl.rad(2 * rot[0]), 1.0, 0.0, 0.0)
sgl.rotate( 3.0 * sgl.rad(2 * rot[1]), 0.0, 0.0, 1.0)
// vfmt on
cube_texture(1, 1, 1)
}
sgl.pop_matrix()

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,6 +172,7 @@ 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,
@ -176,8 +181,11 @@ fn init_cube_glsl(mut app App) {
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)))
@ -222,12 +230,15 @@ 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))
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))
@ -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))

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
@ -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,7 +172,8 @@ 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,
@ -175,11 +181,14 @@ fn init_cube_glsl_m(mut app App) {
/*
u16(14), 13, 12, 15, 14, 12,
16, 17, 18, 16, 18, 19,
22, 21, 20, 23, 22, 20
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,6 +287,7 @@ 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,
@ -282,11 +296,13 @@ fn init_cube_glsl_p(mut app App) {
*/
u16(14), 13, 12, 15, 14, 12,
16 , 17, 18, 16, 18, 19,
22, 21, 20, 23, 22, 20
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,12 +352,15 @@ 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))
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))
@ -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 */
// 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 }

View File

@ -17,10 +17,8 @@ import gg
import gg.m4
import gx
import math
import sokol.gfx
// import sokol.sgl
import time
const (
@ -40,20 +38,16 @@ mut:
mouse_x int = -1
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
// time
ticks i64
// instances
inst_pos [num_inst]m4.Vec4
// camera
camera_x f32
camera_z f32
@ -64,6 +58,7 @@ mut:
******************************************************************************/
#flag -I @VMODROOT/.
#include "rt_glsl_instancing.h" # Should be generated with `v shader .` (see the instructions at the top of this file)
fn C.instancing_shader_desc(gfx.Backend) &gfx.ShaderDesc
/******************************************************************************
@ -71,6 +66,7 @@ fn C.instancing_shader_desc(gfx.Backend) &gfx.ShaderDesc
******************************************************************************/
fn create_texture(w int, h int, buf byteptr) gfx.Image {
sz := w * h * 4
// vfmt off
mut img_desc := gfx.ImageDesc{
width: w
height: h
@ -83,6 +79,7 @@ fn create_texture(w int, h int, buf byteptr) gfx.Image{
label: &u8(0)
d3d11_texture: 0
}
// vfmt on
// comment if .dynamic is enabled
img_desc.data.subimage[0][0] = gfx.Range{
ptr: buf
@ -126,7 +123,6 @@ struct Vertex_t {
y f32
z f32
color u32
// u u16 // for compatibility with D3D11
// v u16 // for compatibility with D3D11
u f32
@ -135,10 +131,11 @@ struct Vertex_t {
// march shader init
fn init_cube_glsl_i(mut app App) {
/* cube vertex buffer */
// cube vertex buffer
// 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},
@ -171,8 +168,11 @@ fn init_cube_glsl_i(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{
@ -182,8 +182,10 @@ fn init_cube_glsl_i(mut app App) {
vert_buffer_desc.@type = .vertexbuffer
vbuf := gfx.make_buffer(&vert_buffer_desc)
/* create an instance buffer for the cube */
mut inst_buffer_desc := gfx.BufferDesc{label: c'instance-data'}
// create an instance buffer for the cube
mut inst_buffer_desc := gfx.BufferDesc{
label: c'instance-data'
}
unsafe { vmemset(&inst_buffer_desc, 0, int(sizeof(inst_buffer_desc))) }
inst_buffer_desc.size = usize(num_inst * int(sizeof(m4.Vec4)))
@ -191,18 +193,21 @@ fn init_cube_glsl_i(mut app App) {
inst_buffer_desc.usage = .stream
inst_buf := gfx.make_buffer(&inst_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,
14, 13, 12, 15, 14, 12,
16, 17, 18, 16, 18, 19,
22, 21, 20, 23, 22, 20
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{
@ -212,13 +217,14 @@ fn init_cube_glsl_i(mut app App) {
index_buffer_desc.@type = .indexbuffer
ibuf := gfx.make_buffer(&index_buffer_desc)
/* create shader */
// create shader
shader := gfx.make_shader(C.instancing_shader_desc(C.sg_query_backend()))
mut pipdesc := gfx.PipelineDesc{}
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_i_pos ].format = .float3 // x,y,z as f32
pipdesc.layout.attrs[C.ATTR_vs_i_pos ].buffer_index = 0
@ -233,6 +239,7 @@ fn init_cube_glsl_i(mut app App) {
pipdesc.layout.buffers[1].step_func = .per_instance // we will pass a single parameter for each instance!!
pipdesc.layout.attrs[C.ATTR_vs_i_inst_pos ].format = .float4
pipdesc.layout.attrs[C.ATTR_vs_i_inst_pos ].buffer_index = 1
// vfmt on
pipdesc.shader = shader
pipdesc.index_type = .uint16
@ -243,7 +250,7 @@ fn init_cube_glsl_i(mut app App) {
}
pipdesc.cull_mode = .back
pipdesc.label = "glsl_shader pipeline".str
pipdesc.label = 'glsl_shader pipeline'.str
mut bind := gfx.Bindings{}
unsafe { vmemset(&bind, 0, int(sizeof(bind))) }
@ -254,16 +261,18 @@ fn init_cube_glsl_i(mut app App) {
app.bind['inst'] = bind
app.pipe['inst'] = gfx.make_pipeline(&pipdesc)
println("GLSL March init DONE!")
println('GLSL March init DONE!')
}
fn calc_tr_matrices(w f32, h f32, rx f32, ry f32, in_scale f32) m4.Mat4 {
// vfmt off
proj := m4.perspective(60, w/h, 0.01, 4000.0)
view := m4.look_at(m4.Vec4{e:[f32(0.0),100,6,0]!}, m4.Vec4{e:[f32(0),0,0,0]!}, m4.Vec4{e:[f32(0),1.0,0,0]!})
view_proj := view * proj
rxm := m4.rotate(m4.rad(rx), m4.Vec4{e:[f32(1),0,0,0]!})
rym := m4.rotate(m4.rad(ry), m4.Vec4{e:[f32(0),1,0,0]!})
// vfmt on
model := rym * rxm
scale_m := m4.scale(m4.Vec4{ e: [in_scale, in_scale, in_scale, 1]! })
@ -308,7 +317,9 @@ fn draw_cube_glsl_i(mut app App){
// r := ((x-cx)*(x-cx)+(z-cz)*(z-cz))/(sz/2)
// y := f32(math.sin(r+time_ticks)*4.0)
spare_param := f32(index % 10)
// vfmt off
app.inst_pos[index] = m4.Vec4{e:[f32((x - cx - app.camera_x) * cube_size),y ,f32( (z - cz - app.camera_z) * cube_size),spare_param]!}
// vfmt on
}
range := gfx.Range{
ptr: unsafe { &app.inst_pos }
@ -329,6 +340,7 @@ fn draw_cube_glsl_i(mut 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
0,0, // dont send mouse position
@ -338,6 +350,7 @@ fn draw_cube_glsl_i(mut app App){
app.frame_count, // frame count
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))
@ -348,7 +361,6 @@ fn draw_cube_glsl_i(mut app App){
gfx.draw(0, (3 * 2) * 6, num_inst)
}
fn draw_start_glsl(app App) {
if app.init_flag == false {
return
@ -431,7 +443,6 @@ fn my_init(mut app App) {
app.texture = create_texture(w, h, tmp_txt)
free(tmp_txt)
}
// glsl
init_cube_glsl_i(mut app)
app.init_flag = true
@ -481,6 +492,7 @@ fn main(){
gg: 0
}
// vfmt off
app.gg = gg.new_context(
width: win_width
height: win_height
@ -492,6 +504,7 @@ fn main(){
init_fn: my_init
event_fn: my_event_manager
)
// vfmt on
app.ticks = time.ticks()
app.gg.run()

View File

@ -48,6 +48,8 @@ const (
]
)
// vfmt off
// FIPS-197 Figure 7. S-box substitution values in hexadecimal format.
const (
s_box0 = [
@ -372,3 +374,5 @@ const (
0xa8017139, 0x0cb3de08, 0xb4e49cd8, 0x56c19064, 0xcb84617b, 0x32b670d5, 0x6c5c7448, 0xb85742d0,
]
)
// vfmt on

View File

@ -9,8 +9,9 @@ module sha512
import math.bits
const (
_k = [u64(0x428a2f98d728ae22), u64(0x7137449123ef65cd), u64(0xb5c0fbcfec4d3b2f), u64(0xe9b5dba58189dbbc),
// vfmt off
const _k = [
u64(0x428a2f98d728ae22), u64(0x7137449123ef65cd), u64(0xb5c0fbcfec4d3b2f), u64(0xe9b5dba58189dbbc),
u64(0x3956c25bf348b538), u64(0x59f111f1b605d019), u64(0x923f82a4af194f9b), u64(0xab1c5ed5da6d8118),
u64(0xd807aa98a3030242), u64(0x12835b0145706fbe), u64(0x243185be4ee4b28c), u64(0x550c7dc3d5ffb4e2),
u64(0x72be5d74f27b896f), u64(0x80deb1fe3b1696b1), u64(0x9bdc06a725c71235), u64(0xc19bf174cf692694),
@ -29,8 +30,9 @@ const (
u64(0xca273eceea26619c), u64(0xd186b8c721c0c207), u64(0xeada7dd6cde0eb1e), u64(0xf57d4f7fee6ed178),
u64(0x06f067aa72176fba), u64(0x0a637dc5a2c898a6), u64(0x113f9804bef90dae), u64(0x1b710b35131c471b),
u64(0x28db77f523047d84), u64(0x32caab7b40c72493), u64(0x3c9ebe0a15c9bebc), u64(0x431d67c49c100d4c),
u64(0x4cc5d4becb3e42b6), u64(0x597f299cfc657e2a), u64(0x5fcb6fab3ad6faec), u64(0x6c44198c4a475817)]
)
u64(0x4cc5d4becb3e42b6), u64(0x597f299cfc657e2a), u64(0x5fcb6fab3ad6faec), u64(0x6c44198c4a475817),
]
// vfmt on
fn block_generic(mut dig Digest, p_ []u8) {
unsafe {
@ -47,16 +49,26 @@ fn block_generic(mut dig Digest, p_ []u8) {
for p.len >= chunk {
for i in 0 .. 16 {
j := i * 8
w[i] = (u64(p[j]) << 56) |
(u64(p[j + 1]) << 48) | (u64(p[j + 2]) << 40) |
(u64(p[j + 3]) << 32) | (u64(p[j + 4]) << 24) |
(u64(p[j + 5]) << 16) | (u64(p[j + 6]) << 8) | u64(p[j + 7])
// vfmt off
w[i] = (
u64(p[j ]) << 56) |
(u64(p[j + 1]) << 48) |
(u64(p[j + 2]) << 40) |
(u64(p[j + 3]) << 32) |
(u64(p[j + 4]) << 24) |
(u64(p[j + 5]) << 16) |
(u64(p[j + 6]) << 8) |
u64(p[j + 7]
)
// vfmt on
}
for i := 16; i < 80; i++ {
v1 := w[i - 2]
// vfmt off
t1 := bits.rotate_left_64(v1, -19) ^ bits.rotate_left_64(v1, -61) ^ (v1 >> 6)
v2 := w[i - 15]
t2 := bits.rotate_left_64(v2, -1) ^ bits.rotate_left_64(v2, -8) ^ (v2 >> 7)
// vfmt on
w[i] = t1 + w[i - 7] + t2 + w[i - 16]
}
mut a := h0
@ -68,11 +80,10 @@ fn block_generic(mut dig Digest, p_ []u8) {
mut g := h6
mut h := h7
for i in 0 .. 80 {
t1 := h +
(bits.rotate_left_64(e, -14) ^ bits.rotate_left_64(e, -18) ^ bits.rotate_left_64(e, -41)) +
((e & f) ^ (~e & g)) + _k[i] + w[i]
t2 := (bits.rotate_left_64(a, -28) ^ bits.rotate_left_64(a, -34) ^ bits.rotate_left_64(a, -39)) +
((a & b) ^ (a & c) ^ (b & c))
// vfmt off
t1 := h + (bits.rotate_left_64(e, -14) ^ bits.rotate_left_64(e, -18) ^ bits.rotate_left_64(e, -41)) + ((e & f) ^ (~e & g)) + _k[i] + w[i]
t2 := (bits.rotate_left_64(a, -28) ^ bits.rotate_left_64(a, -34) ^ bits.rotate_left_64(a, -39)) + ((a & b) ^ (a & c) ^ (b & c))
// vfmt on
h = g
g = f
f = e

View File

@ -1,15 +1,11 @@
module m4
/**********************************************************************
*
* Simply vector/matrix graphic utility
*
* Simple vector/matrix graphic utility
* Copyright (c) 2021 Dario Deledda. All rights reserved.
* Use of this source code is governed by an MIT license
* that can be found in the LICENSE file.
*
* TODO:
**********************************************************************/
module m4
import math
// Translate degrees to radians
@ -32,8 +28,10 @@ pub fn ortho(left f32, right f32, bottom f32, top f32, z_near f32, z_far f32) Ma
tpb := top + bottom
fmn := z_far - z_near
fpn := z_far + z_near
// vfmt off
if fmn != 0 {
return Mat4{ e: [
return Mat4{
e: [
2 / rml, 0, 0, -(rpl / rml),
0 , 2 / tmb, 0, -(tpb / tmb),
0 , 0, 2 / fmn, -(fpn / fmn),
@ -41,25 +39,30 @@ pub fn ortho(left f32, right f32, bottom f32, top f32, z_near f32, z_far f32) Ma
]!
}
}
return Mat4{ e: [
return Mat4{
e: [
2 / rml, 0, 0, -(rpl / rml),
0, 2 / tmb, 0, -(tpb / tmb),
0, 0, 0, 0,
0, 0, 0, 1,
]!
}
// vfmt on
}
// Calculate the perspective matrix using (fov:fov, ar:aspect_ratio ,n:near_pane, f:far_plane) as parameters
pub fn perspective(fov f32, ar f32, n f32, f f32) Mat4 {
ctan := f32(1.0 / math.tan(fov * (f32(math.pi) / 360.0))) // for the FOV we use 360 instead 180
return Mat4{ e: [
// vfmt off
return Mat4{
e: [
ctan / ar, 0, 0, 0,
0, ctan, 0, 0,
0, 0, (n + f) / (n - f), -1.0,
0, 0, (2.0 * n * f) / (n - f), 0,
]!
}
// vfmt on
}
// Calculate the look-at matrix
@ -67,44 +70,33 @@ pub fn look_at(eye Vec4, center Vec4, up Vec4) Mat4 {
f := (center - eye).normalize3()
s := (f % up).normalize3()
u := (s % f)
// vfmt off
return Mat4{e: [
/* [0][0] */ s.e[0],
/* [0][1] */ u.e[0],
/* [0][2] */ - f.e[0],
/* [0][3] */ 0,
/* [1][1] */ s.e[1],
/* [1][1] */ u.e[1],
/* [1][2] */ - f.e[1],
/* [1][3] */ 0,
/* [2][0] */ s.e[2],
/* [2][1] */ u.e[2],
/* [2][2] */ - f.e[2],
/* [2][3] */ 0,
/* [3][0] */ - (s * eye),
/* [3][1] */ - (u * eye),
/* [3][2] */ f * eye,
/* [3][3] */ 1,
]!
/* [0][0] */ s.e[0], /* [0][1] */ u.e[0], /* [0][2] */ -f.e[0], /* [0][3] */ 0,
/* [1][1] */ s.e[1], /* [1][1] */ u.e[1], /* [1][2] */ -f.e[1], /* [1][3] */ 0,
/* [2][0] */ s.e[2], /* [2][1] */ u.e[2], /* [2][2] */ -f.e[2], /* [2][3] */ 0,
/* [3][0] */ -(s * eye), /* [3][1] */ -(u * eye), /* [3][2] */ f * eye, /* [3][3] */ 1,
]!}
// vfmt on
}
}
// Get the complete transformation matrix for GLSL demos
pub fn calc_tr_matrices(w f32, h f32, rx f32, ry f32, in_scale f32) Mat4 {
proj := perspective(60, w / h, 0.01, 10.0)
view := look_at(Vec4{ e: [f32(0.0), 1.5, 6, 0]! }, Vec4{ e: [f32(0), 0, 0, 0]! }, Vec4{ e: [f32(0), 1.0, 0, 0]! })
// vfmt off
view := look_at(
Vec4{ e: [f32(0.0), 1.5, 6, 0]! },
Vec4{ e: [f32(0), 0, 0, 0]!},
Vec4{ e: [f32(0), 1.0, 0, 0]!}
)
view_proj := view * proj
rxm := rotate(rad(rx), Vec4{ e: [f32(1), 0, 0, 0]! })
rym := rotate(rad(ry), Vec4{ e: [f32(0), 1, 0, 0]! })
scale_m := scale(Vec4{ e: [in_scale, in_scale, in_scale, 1]! })
// vfmt on
model := rym * rxm
scale_m := scale(Vec4{ e: [in_scale, in_scale, in_scale, 1]! })
res := (scale_m * model) * view_proj
return res
}

View File

@ -1,5 +1,6 @@
import gg.m4
// vfmt off
pub fn test_m4() {
unsafe {
// Test Mat4
@ -233,3 +234,4 @@ fn test_proj() {
assert m4.mul_vec(ort, m4.Vec4{[ f32(0), 0, 0, 1]!}) == m4.Vec4{[ f32(-1), -1, 0, 1]!}
assert m4.mul_vec(ort, m4.Vec4{[ f32(300), 200, 0, 1]!}) == m4.Vec4{[ f32(1), 1, 0, 1]!}
}
// vfmt on

View File

@ -30,10 +30,13 @@ pub const precision = f32(10e-7)
// String representation of the matrix
pub fn (x Mat4) str() string {
unsafe {
return '|${x.e[0]:-6.3},${x.e[1]:-6.3},${x.e[2]:-6.3},${x.e[3]:-6.3}|\n' +
// vfmt off
return
'|${x.e[0]: -6.3},${x.e[1]: -6.3},${x.e[2]: -6.3},${x.e[3]: -6.3}|\n' +
'|${x.e[4]: -6.3},${x.e[5]: -6.3},${x.e[6]: -6.3},${x.e[7]: -6.3}|\n' +
'|${x.e[8]: -6.3},${x.e[9]: -6.3},${x.e[10]:-6.3},${x.e[11]:-6.3}|\n' +
'|${x.e[12]:-6.3},${x.e[13]:-6.3},${x.e[14]:-6.3},${x.e[15]:-6.3}|'
// vfmt on
}
}
@ -110,10 +113,12 @@ pub fn (mut x Mat4) set_f(index_col int, index_row int, value f32) {
pub fn (mut x Mat4) copy(y Mat4) {
unsafe {
x.e = [
// vfmt off
y.e[0 ], y.e[1 ], y.e[2 ], y.e[3 ],
y.e[4 ], y.e[5 ], y.e[6 ], y.e[7 ],
y.e[8 ], y.e[9 ], y.e[10], y.e[11],
y.e[12], y.e[13], y.e[14], y.e[15],
// vfmt on
]!
}
}
@ -121,29 +126,35 @@ pub fn (mut x Mat4) copy(y Mat4) {
// Set the trace of the matrix using a vec4
pub fn (mut x Mat4) set_trace(v3 Vec4) {
unsafe {
// vfmt off
x.e[0 ] = v3.e[0]
x.e[5 ] = v3.e[1]
x.e[10] = v3.e[2]
x.e[15] = v3.e[3]
// vfmt on
}
}
// Get the trace of the matrix
pub fn (x Mat4) get_trace() Vec4 {
unsafe {
// vfmt off
return Vec4{ e: [ x.e[0], x.e[5], x.e[10], x.e[15], ]! }
// vfmt on
}
}
// Set all the matrix elements to value
pub fn (mut x Mat4) set_f32(value f32) {
unsafe {
// vfmt off
x.e = [
value, value, value, value,
value, value, value, value,
value, value, value, value,
value, value, value, value,
]!
// vfmt on
}
}
@ -151,8 +162,7 @@ pub fn (mut x Mat4) set_f32(value f32) {
// Rows/Column access
//-------------------------------------
// Set the row as the input vec4
[direct_array_access]
[unsafe]
[direct_array_access; unsafe]
pub fn (mut x Mat4) set_row(row int, v3 Vec4) {
unsafe {
x.e[row * 4 + 0] = v3.e[0]
@ -163,54 +173,43 @@ pub fn (mut x Mat4) set_row(row int, v3 Vec4) {
}
// Get a row from a matrix
[direct_array_access]
[unsafe]
[direct_array_access; unsafe]
pub fn (x Mat4) get_row(row int) Vec4 {
unsafe {
return Vec4{
e: [
x.e[row * 4 + 0],
x.e[row * 4 + 1],
x.e[row * 4 + 2],
x.e[row * 4 + 3],
]!
}
// vfmt off
return Vec4{ e: [ x.e[row * 4], x.e[row * 4 + 1], x.e[row * 4 + 2], x.e[row * 4 + 3], ]! }
// vfmt on
}
}
// Set the column as the input vec4
[direct_array_access]
[unsafe]
[direct_array_access; unsafe]
pub fn (mut x Mat4) set_col(col int, v3 Vec4) {
unsafe {
// vfmt off
x.e[col ] = v3.e[0]
x.e[col + 4 ] = v3.e[1]
x.e[col + 8 ] = v3.e[2]
x.e[col + 12] = v3.e[3]
// vfmt on
}
}
// Get a column from a matrix
[direct_array_access]
[unsafe]
[direct_array_access; unsafe]
pub fn (x Mat4) get_col(col int) Vec4 {
unsafe {
return Vec4{
e: [
x.e[col],
x.e[col + 4 ],
x.e[col + 8 ],
x.e[col + 12],
]!
}
// vfmt off
return Vec4{ e: [ x.e[col], x.e[col + 4 ], x.e[col + 8 ], x.e[col + 12], ]!}
// vfmt on
}
}
// Swap two columns in the matrix
[direct_array_access]
[unsafe]
[direct_array_access; unsafe]
pub fn (mut x Mat4) swap_col(col1 int, col2 int) {
unsafe {
// vfmt off
v0 := x.e[col1 ]
v1 := x.e[col1 + 4 ]
v2 := x.e[col1 + 8 ]
@ -225,12 +224,12 @@ pub fn (mut x Mat4) swap_col(col1 int, col2 int) {
x.e[col2 + 4 ] = v1
x.e[col2 + 8 ] = v2
x.e[col2 + 12] = v3
// vfmt on
}
}
// Swap two rows in the matrix
[direct_array_access]
[unsafe]
[direct_array_access; unsafe]
pub fn (mut x Mat4) swap_row(row1 int, row2 int) {
unsafe {
v0 := x.e[row1 * 4 + 0]
@ -256,26 +255,28 @@ pub fn (mut x Mat4) swap_row(row1 int, row2 int) {
// Transpose the matrix
pub fn (x Mat4) transpose() Mat4 {
unsafe {
// vfmt off
return Mat4{ e: [
x.e[0 ], x.e[4 ], x.e[8 ], x.e[12],
x.e[1 ], x.e[5 ], x.e[9 ], x.e[13],
x.e[2 ], x.e[6 ], x.e[10], x.e[14],
x.e[3 ], x.e[7 ], x.e[11], x.e[15],
]!
}
]!}
// vfmt on
}
}
// Multiply the all the elements of the matrix by a scalar
pub fn (x Mat4) mul_scalar(s f32) Mat4 {
unsafe {
// vfmt off
return Mat4{ e: [
x.e[0 ] * s, x.e[1 ] * s, x.e[2 ] * s, x.e[3 ] * s,
x.e[4 ] * s, x.e[5 ] * s, x.e[6 ] * s, x.e[7 ] * s,
x.e[8 ] * s, x.e[9 ] * s, x.e[10] * s, x.e[11] * s,
x.e[12] * s, x.e[13] * s, x.e[14] * s, x.e[15] * s,
]!
}
]!}
// vfmt on
}
}
@ -286,35 +287,38 @@ pub fn (x Mat4) mul_scalar(s f32) Mat4 {
*********************************************************************/
// Return a zero matrix
pub fn zero_m4() Mat4 {
// vfmt off
return Mat4{ e: [
f32(0), 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0,
]!
}
]!}
// vfmt on
}
// Return a unity matrix
pub fn unit_m4() Mat4 {
// vfmt off
return Mat4{ e: [
f32(1), 0, 0, 0,
0, 1, 0, 0,
0, 0, 1, 0,
0, 0, 0, 1,
]!
}
]!}
// vfmt on
}
// Return a matrix initialized with value
pub fn set_m4(value f32) Mat4 {
// vfmt off
return Mat4{ e: [
value, value, value, value,
value, value, value, value,
value, value, value, value,
value, value, value, value,
]!
}
]!}
// vfmt on
}
/*********************************************************************
@ -326,55 +330,57 @@ pub fn set_m4(value f32) Mat4 {
// Sum of matrix, operator +
pub fn (a Mat4) + (b Mat4) Mat4 {
unsafe {
// vfmt off
return Mat4{ e: [
a.e[0 ] + b.e[0 ], a.e[1 ] + b.e[1 ], a.e[2 ] + b.e[2 ], a.e[3 ] + b.e[3 ],
a.e[4 ] + b.e[4 ], a.e[5 ] + b.e[5 ], a.e[6 ] + b.e[6 ], a.e[7 ] + b.e[7 ],
a.e[8 ] + b.e[8 ], a.e[9 ] + b.e[9 ], a.e[10] + b.e[10], a.e[11] + b.e[11],
a.e[12] + b.e[12], a.e[13] + b.e[13], a.e[14] + b.e[14], a.e[15] + b.e[15],
]!
}
]!}
// vfmt on
}
}
// Subtraction of matrix, operator -
pub fn (a Mat4) - (b Mat4) Mat4 {
unsafe {
// vfmt off
return Mat4{ e: [
a.e[0 ] - b.e[0 ], a.e[1 ] - b.e[1 ], a.e[2 ] - b.e[2 ], a.e[3 ] - b.e[3 ],
a.e[4 ] - b.e[4 ], a.e[5 ] - b.e[5 ], a.e[6 ] - b.e[6 ], a.e[7 ] - b.e[7 ],
a.e[8 ] - b.e[8 ], a.e[9 ] - b.e[9 ], a.e[10] - b.e[10], a.e[11] - b.e[11],
a.e[12] - b.e[12], a.e[13] - b.e[13], a.e[14] - b.e[14], a.e[15] - b.e[15],
]!
}
]!}
// vfmt on
}
}
// Multiplication of matrix, operator *
pub fn (a Mat4) * (b Mat4) Mat4 {
unsafe {
return Mat4{
e: [
/* [0][0] */ a.f[0][0] * b.f[0][0] + a.f[0][1] * b.f[1][0] + a.f[0][2] * b.f[2][0] + a.f[0][3] * b.f[3][0]
/* [0][1] */, a.f[0][0] * b.f[0][1] + a.f[0][1] * b.f[1][1] + a.f[0][2] * b.f[2][1] + a.f[0][3] * b.f[3][1]
/* [0][2] */, a.f[0][0] * b.f[0][2] + a.f[0][1] * b.f[1][2] + a.f[0][2] * b.f[2][2] + a.f[0][3] * b.f[3][2]
/* [0][3] */, a.f[0][0] * b.f[0][3] + a.f[0][1] * b.f[1][3] + a.f[0][2] * b.f[2][3] + a.f[0][3] * b.f[3][3]
// vfmt off
return Mat4{ e: [
/* [0][0] */ a.f[0][0] * b.f[0][0] + a.f[0][1] * b.f[1][0] + a.f[0][2] * b.f[2][0] + a.f[0][3] * b.f[3][0],
/* [0][1] */ a.f[0][0] * b.f[0][1] + a.f[0][1] * b.f[1][1] + a.f[0][2] * b.f[2][1] + a.f[0][3] * b.f[3][1],
/* [0][2] */ a.f[0][0] * b.f[0][2] + a.f[0][1] * b.f[1][2] + a.f[0][2] * b.f[2][2] + a.f[0][3] * b.f[3][2],
/* [0][3] */ a.f[0][0] * b.f[0][3] + a.f[0][1] * b.f[1][3] + a.f[0][2] * b.f[2][3] + a.f[0][3] * b.f[3][3],
/* [1][0] */, a.f[1][0] * b.f[0][0] + a.f[1][1] * b.f[1][0] + a.f[1][2] * b.f[2][0] + a.f[1][3] * b.f[3][0]
/* [1][1] */, a.f[1][0] * b.f[0][1] + a.f[1][1] * b.f[1][1] + a.f[1][2] * b.f[2][1] + a.f[1][3] * b.f[3][1]
/* [1][2] */, a.f[1][0] * b.f[0][2] + a.f[1][1] * b.f[1][2] + a.f[1][2] * b.f[2][2] + a.f[1][3] * b.f[3][2]
/* [1][3] */, a.f[1][0] * b.f[0][3] + a.f[1][1] * b.f[1][3] + a.f[1][2] * b.f[2][3] + a.f[1][3] * b.f[3][3]
/* [1][0] */ a.f[1][0] * b.f[0][0] + a.f[1][1] * b.f[1][0] + a.f[1][2] * b.f[2][0] + a.f[1][3] * b.f[3][0],
/* [1][1] */ a.f[1][0] * b.f[0][1] + a.f[1][1] * b.f[1][1] + a.f[1][2] * b.f[2][1] + a.f[1][3] * b.f[3][1],
/* [1][2] */ a.f[1][0] * b.f[0][2] + a.f[1][1] * b.f[1][2] + a.f[1][2] * b.f[2][2] + a.f[1][3] * b.f[3][2],
/* [1][3] */ a.f[1][0] * b.f[0][3] + a.f[1][1] * b.f[1][3] + a.f[1][2] * b.f[2][3] + a.f[1][3] * b.f[3][3],
/* [2][0] */, a.f[2][0] * b.f[0][0] + a.f[2][1] * b.f[1][0] + a.f[2][2] * b.f[2][0] + a.f[2][3] * b.f[3][0]
/* [2][1] */, a.f[2][0] * b.f[0][1] + a.f[2][1] * b.f[1][1] + a.f[2][2] * b.f[2][1] + a.f[2][3] * b.f[3][1]
/* [2][2] */, a.f[2][0] * b.f[0][2] + a.f[2][1] * b.f[1][2] + a.f[2][2] * b.f[2][2] + a.f[2][3] * b.f[3][2]
/* [2][3] */, a.f[2][0] * b.f[0][3] + a.f[2][1] * b.f[1][3] + a.f[2][2] * b.f[2][3] + a.f[2][3] * b.f[3][3]
/* [2][0] */ a.f[2][0] * b.f[0][0] + a.f[2][1] * b.f[1][0] + a.f[2][2] * b.f[2][0] + a.f[2][3] * b.f[3][0],
/* [2][1] */ a.f[2][0] * b.f[0][1] + a.f[2][1] * b.f[1][1] + a.f[2][2] * b.f[2][1] + a.f[2][3] * b.f[3][1],
/* [2][2] */ a.f[2][0] * b.f[0][2] + a.f[2][1] * b.f[1][2] + a.f[2][2] * b.f[2][2] + a.f[2][3] * b.f[3][2],
/* [2][3] */ a.f[2][0] * b.f[0][3] + a.f[2][1] * b.f[1][3] + a.f[2][2] * b.f[2][3] + a.f[2][3] * b.f[3][3],
/* [3][0] */, a.f[3][0] * b.f[0][0] + a.f[3][1] * b.f[1][0] + a.f[3][2] * b.f[2][0] + a.f[3][3] * b.f[3][0]
/* [3][1] */, a.f[3][0] * b.f[0][1] + a.f[3][1] * b.f[1][1] + a.f[3][2] * b.f[2][1] + a.f[3][3] * b.f[3][1]
/* [3][2] */, a.f[3][0] * b.f[0][2] + a.f[3][1] * b.f[1][2] + a.f[3][2] * b.f[2][2] + a.f[3][3] * b.f[3][2]
/* [3][3] */, a.f[3][0] * b.f[0][3] + a.f[3][1] * b.f[1][3] + a.f[3][2] * b.f[2][3] + a.f[3][3] * b.f[3][3],
]!
}
/* [3][0] */ a.f[3][0] * b.f[0][0] + a.f[3][1] * b.f[1][0] + a.f[3][2] * b.f[2][0] + a.f[3][3] * b.f[3][0],
/* [3][1] */ a.f[3][0] * b.f[0][1] + a.f[3][1] * b.f[1][1] + a.f[3][2] * b.f[2][1] + a.f[3][3] * b.f[3][1],
/* [3][2] */ a.f[3][0] * b.f[0][2] + a.f[3][1] * b.f[1][2] + a.f[3][2] * b.f[2][2] + a.f[3][3] * b.f[3][2],
/* [3][3] */ a.f[3][0] * b.f[0][3] + a.f[3][1] * b.f[1][3] + a.f[3][2] * b.f[2][3] + a.f[3][3] * b.f[3][3],
]!}
// vfmt on
}
}
@ -402,13 +408,14 @@ pub fn mul(a Mat4, b Mat4) Mat4 {
// Multiply a Matrix by a vector
pub fn mul_vec(a Mat4, v Vec4) Vec4 {
unsafe {
// vfmt off
return Vec4{ e: [
a.e[0 ] * v.e[0] + a.e[1 ] * v.e[1] + a.e[2 ] * v.e[2] + a.e[3 ] * v.e[3],
a.e[4 ] * v.e[0] + a.e[5 ] * v.e[1] + a.e[6 ] * v.e[2] + a.e[7 ] * v.e[3],
a.e[8 ] * v.e[0] + a.e[9 ] * v.e[1] + a.e[10] * v.e[2] + a.e[11] * v.e[3],
a.e[12] * v.e[0] + a.e[13] * v.e[1] + a.e[14] * v.e[2] + a.e[15] * v.e[3],
]!
}
]!}
// vfmt on
}
}
@ -440,11 +447,13 @@ pub fn det(x Mat4) f32 {
t[4] = x02 * x23 - x03 * x22
t[5] = x02 * x13 - x03 * x12
return 0.0 +
// vfmt off
return
x00 * (x11 * t[0] - x21 * t[1] + x31 * t[2]) -
x10 * (x01 * t[0] - x21 * t[3] + x31 * t[4]) +
x20 * (x01 * t[1] - x11 * t[3] + x31 * t[5]) -
x30 * (x01 * t[2] - x11 * t[4] + x21 * t[5])
// vfmt on
}
}
@ -454,6 +463,7 @@ pub fn (x Mat4) inverse() Mat4 {
mut t := [6]f32{}
mut det := f32(0)
// vfmt off
a := x.f[0][0]
b := x.f[1][0]
c := x.f[2][0]
@ -512,6 +522,7 @@ pub fn (x Mat4) inverse() Mat4 {
dest.f[3][1] = a * t[0] - c * t[3] + d * t[4]
dest.f[3][2] = -(a * t[1] - b * t[3] + d * t[5])
dest.f[3][3] = a * t[2] - b * t[4] + c * t[5]
// vfmt on
tmp := (a * dest.f[0][0] + b * dest.f[0][1] + c * dest.f[0][2] + d * dest.f[0][3])
if tmp != 0 {
@ -538,28 +549,14 @@ pub fn rotate(angle f32, w Vec4) Mat4 {
ay := axis.e[1]
az := axis.e[2]
// vfmt off
return Mat4{ e: [
/* [0][0] */ (ax * ax * cv) + cs
/* [0][1] */, (ax * ay * cv) + az * sn
/* [0][2] */, (ax * az * cv) - ay * sn
/* [0][3] */, 0
/* [1][0] */, (ay * ax * cv) - az * sn
/* [1][1] */, (ay * ay * cv) + cs
/* [1][2] */, (ay * az * cv) + ax * sn
/* [1][3] */, 0
/* [2][0] */, (az * ax * cv) + ay * sn
/* [2][1] */, (az * ay * cv) - ax * sn
/* [2][2] */, (az * az * cv) + cs
/* [2][3] */, 0
/* [3][0] */, 0
/* [3][1] */, 0
/* [3][2] */, 0
/* [3][3] */, 1,
]!
}
/* [0][0] */ (ax * ax * cv) + cs , /* [0][1] */ (ax * ay * cv) + az * sn , /* [0][2] */ (ax * az * cv) - ay * sn , /* [0][3] */ 0,
/* [1][0] */ (ay * ax * cv) - az * sn , /* [1][1] */ (ay * ay * cv) + cs , /* [1][2] */ (ay * az * cv) + ax * sn , /* [1][3] */ 0,
/* [2][0] */ (az * ax * cv) + ay * sn , /* [2][1] */ (az * ay * cv) - ax * sn , /* [2][2] */ (az * az * cv) + cs , /* [2][3] */ 0,
/* [3][0] */ 0, /* [3][1] */ 0 , /* [3][2] */ 0 , /* [3][3] */ 1,
]!}
// vfmt on
}
}
@ -571,25 +568,27 @@ pub fn rotate(angle f32, w Vec4) Mat4 {
// Get a matrix translated by a vector w
pub fn (x Mat4) translate(w Vec4) Mat4 {
unsafe {
// vfmt off
return Mat4{ e: [
x.e[0], x.e[1], x.e[2 ], x.e[3 ],
x.e[4], x.e[5], x.e[6 ], x.e[7 ],
x.e[8], x.e[9], x.e[10], x.e[11],
x.e[12] + w.e[0], x.e[13] + w.e[1], x.e[14] + w.e[2], x.e[15],
]!
}
]!}
// vfmt on
}
}
// Get a scale matrix, the scale vector is w, only xyz are evaluated.
pub fn scale(w Vec4) Mat4 {
unsafe {
// vfmt off
return Mat4{ e: [
w.e[0], 0, 0, 0,
0, w.e[1], 0, 0,
0, 0, w.e[2], 0,
0, 0, 0, 1,
]!
}
]!}
// vfmt on
}
}

View File

@ -2,6 +2,8 @@ import regex
import rand
import strings
const debug = true // true for debug println
/******************************************************************************
*
* Test section
@ -14,6 +16,7 @@ struct TestItem {
e int
}
// vfmt off
const(
match_test_suite = [
// minus in CC
@ -422,10 +425,7 @@ const (
Test_split{'1234', r'\d+', []},
]
)
const (
debug = true // true for debug println
)
// vfmt on
fn test_regex() {
// check capturing groups
@ -590,7 +590,9 @@ fn test_regex() {
// check replace simple
for c, to in match_test_suite_replace_simple {
// debug print
if debug { println('#$c [$to.src] q[$to.q] $to.r') }
if debug {
println('#$c [$to.src] q[$to.q] $to.r')
}
mut re := regex.regex_opt(to.q) or {
eprintln('err: $err')
@ -609,7 +611,9 @@ fn test_regex() {
// check match and find
for c, to in match_test_suite {
// debug print
if debug { println('#$c [$to.src] q[$to.q] $to.s $to.e') }
if debug {
println('#$c [$to.src] q[$to.q] $to.s $to.e')
}
// test the find
if to.s > 0 {
@ -675,7 +679,9 @@ fn test_regex() {
}
}
if debug { println('DONE!') }
if debug {
println('DONE!')
}
}
// test regex_base function
@ -790,6 +796,7 @@ struct Test_find_groups {
res []int // groups indexes
}
// vfmt off
const (
find_groups_test_suite = [
Test_find_groups{
@ -815,6 +822,7 @@ find_groups_test_suite = [
},
]
)
// vfmt on
fn test_groups_in_find() {
for test_obj in find_groups_test_suite {

View File

@ -1,12 +1,13 @@
vlib/v/checker/tests/modules/deprecated_module/main.v:2:1: notice: module `deprecated_module.www.ttt` will be deprecated after 2999-01-01, and will become an error after 2999-06-30; use xxx.yyy
1 | import bbb.ccc
2 | import www.ttt
vlib/v/checker/tests/modules/deprecated_module/main.v:5:1: notice: module `deprecated_module.www.ttt` will be deprecated after 2999-01-01, and will become an error after 2999-06-30; use xxx.yyy
3 | // That is unrelated to what this file tests, but should be investigated further and fixed when the module lookup disrepancy is fixed.
4 | import bbb.ccc
5 | import www.ttt
| ~~~~~~~~~~~~~~
3 | import xxx.yyy
4 |
vlib/v/checker/tests/modules/deprecated_module/main.v:12:11: error: undefined ident: `deprecated_module.www.ttt.non_existing`
10 | dump(ttt.f())
11 | dump(yyy.f())
12 | dump(ttt.non_existing)
6 | import xxx.yyy
7 | // vfmt on
vlib/v/checker/tests/modules/deprecated_module/main.v:16:11: error: undefined ident: `deprecated_module.www.ttt.non_existing`
14 | dump(ttt.f())
15 | dump(yyy.f())
16 | dump(ttt.non_existing)
| ~~~~~~~~~~~~
13 | }
17 | }

View File

@ -1,6 +1,10 @@
// vfmt off
// TODO: without vfmt off, vfmt is buggy, and keeps converting the imports below to `import deprecated_module.bbb.ccc` for some reason, even though the folder has `v.mod`.
// That is unrelated to what this file tests, but should be investigated further and fixed when the module lookup disrepancy is fixed.
import bbb.ccc
import www.ttt
import xxx.yyy
// vfmt on
// Note: www.ttt has been deprecated.
// => compiling this should produce an error,

View File

@ -26,6 +26,16 @@ pub struct CommentsOptions {
}
pub fn (mut f Fmt) comment(node ast.Comment, options CommentsOptions) {
if node.text.starts_with('\x01 vfmt on') {
f.vfmt_on(node.pos.line_nr)
}
defer {
// ensure that the `vfmt off` comment itself was sent to the output,
// by defering the check for that state transition:
if node.text.starts_with('\x01 vfmt off') {
f.vfmt_off(node.pos.line_nr)
}
}
// Shebang in .vsh files
if node.text.starts_with('#!') {
f.writeln(node.text)

View File

@ -50,9 +50,16 @@ pub mut:
is_mbranch_expr bool // match a { x...y { } }
fn_scope &ast.Scope = unsafe { nil }
wsinfix_depth int
format_state FormatState
source_text string // can be set by `echo "println('hi')" | v fmt`, i.e. when processing source not from a file, but from stdin. In this case, it will contain the entire input text. You can use f.file.path otherwise, and read from that file.
}
pub fn fmt(file ast.File, table &ast.Table, pref &pref.Preferences, is_debug bool) string {
[params]
pub struct FmtOptions {
source_text string
}
pub fn fmt(file ast.File, table &ast.Table, pref &pref.Preferences, is_debug bool, options FmtOptions) string {
mut f := Fmt{
file: file
table: table
@ -61,6 +68,7 @@ pub fn fmt(file ast.File, table &ast.Table, pref &pref.Preferences, is_debug boo
out: strings.new_builder(1000)
out_imports: strings.new_builder(200)
}
f.source_text = options.source_text
f.process_file_imports(file)
f.set_current_module_name('main')
// As these are toplevel stmts, the indent increase done in f.stmts() has to be compensated
@ -328,9 +336,18 @@ pub fn (mut f Fmt) imports(imports []ast.Import) {
continue
}
already_imported[import_text] = true
if !f.format_state.is_vfmt_on {
import_original_source_lines := f.get_source_lines()#[imp.pos.line_nr..
imp.pos.last_line + 1].join('\n')
f.out_imports.writeln(import_original_source_lines)
// NOTE: imp.comments are on the *same line*, so they are already included in import_original_source_lines
f.import_comments(imp.next_comments)
} else {
f.out_imports.writeln(import_text)
f.import_comments(imp.comments, inline: true)
f.import_comments(imp.next_comments)
}
num_imports++
}
if num_imports > 0 {
@ -441,7 +458,7 @@ pub fn (mut f Fmt) stmts(stmts []ast.Stmt) {
pub fn (mut f Fmt) stmt(node ast.Stmt) {
if f.is_debug {
eprintln('stmt: ${node.pos:-42} | node: ${node.type_name():-20}')
eprintln('stmt ${node.type_name():-20} | pos: $node.pos.line_str()')
}
match node {
ast.EmptyStmt, ast.NodeError {}
@ -544,7 +561,7 @@ fn stmt_is_single_line(stmt ast.Stmt) bool {
pub fn (mut f Fmt) expr(node_ ast.Expr) {
mut node := unsafe { node_ }
if f.is_debug {
eprintln('expr: ${node.pos():-42} | node: ${node.type_name():-20} | $node.str()')
eprintln('expr ${node.type_name():-20} | pos: $node.pos().line_str() | $node.str()')
}
match mut node {
ast.NodeError {}

View File

@ -0,0 +1,96 @@
// This file checks that using a single comment line like this: `// vfmt off`
// should turn off the formatting that vfmt does, *till the end of the file*
// or till it encounters a single comment line like this: `// vfmt on` .
//
// NOTE: all lines after a `// vfmt off` line, should be copied 1:1 from the
// input source code, without any changes to the spacing/formatting.
import sokol.sgl
struct Abc {
b string
}
// vfmt off
struct St { a int }
// vfmt on
fn xyz() {
println('hi')
}
// vfmt off
fn abc() {
println('hello')
println('another')
}
// vfmt on
fn main() {
println('hi')
// vfmt off
println( 'unformatted' )
// some ASCII art here
code( )
another( )
// vfmt on
println('end')
}
// vertex specification for a cube with colored sides and texture coords
fn cube() {
sgl.begin_quads()
{
// edge color
sgl.c3f(1.0, 0.0, 0.0)
// edge coord
// x,y,z, texture cord: u,v
// vfmt off
sgl.v3f_t2f(-1.0, 1.0, -1.0, -1.0, 1.0)
sgl.v3f_t2f( 1.0, 1.0, -1.0, 1.0, 1.0)
sgl.v3f_t2f( 1.0, -1.0, -1.0, 1.0, -1.0)
sgl.v3f_t2f(-1.0, -1.0, -1.0, -1.0, -1.0)
sgl.c3f(0.0, 1.0, 0.0)
sgl.v3f_t2f(-1.0, -1.0, 1.0, -1.0, 1.0)
sgl.v3f_t2f( 1.0, -1.0, 1.0, 1.0, 1.0)
sgl.v3f_t2f( 1.0, 1.0, 1.0, 1.0, -1.0)
sgl.v3f_t2f(-1.0, 1.0, 1.0, -1.0, -1.0)
sgl.c3f(0.0, 0.0, 1.0)
sgl.v3f_t2f(-1.0, -1.0, 1.0, -1.0, 1.0)
sgl.v3f_t2f(-1.0, 1.0, 1.0, 1.0, 1.0)
sgl.v3f_t2f(-1.0, 1.0, -1.0, 1.0, -1.0)
sgl.v3f_t2f(-1.0, -1.0, -1.0, -1.0, -1.0)
sgl.c3f(1.0, 0.5, 0.0)
sgl.v3f_t2f(1.0, -1.0, 1.0, -1.0, 1.0)
sgl.v3f_t2f(1.0, -1.0, -1.0, 1.0, 1.0)
sgl.v3f_t2f(1.0, 1.0, -1.0, 1.0, -1.0)
sgl.v3f_t2f(1.0, 1.0, 1.0, -1.0, -1.0)
sgl.c3f(0.0, 0.5, 1.0)
sgl.v3f_t2f( 1.0, -1.0, -1.0, -1.0, 1.0)
sgl.v3f_t2f( 1.0, -1.0, 1.0, 1.0, 1.0)
sgl.v3f_t2f(-1.0, -1.0, 1.0, 1.0, -1.0)
sgl.v3f_t2f(-1.0, -1.0, -1.0, -1.0, -1.0)
sgl.c3f(1.0, 0.0, 0.5)
sgl.v3f_t2f(-1.0, 1.0, -1.0, -1.0, 1.0)
sgl.v3f_t2f(-1.0, 1.0, 1.0, 1.0, 1.0)
sgl.v3f_t2f( 1.0, 1.0, 1.0, 1.0, -1.0)
sgl.v3f_t2f( 1.0, 1.0, -1.0, -1.0, -1.0)
// vfmt on
}
sgl.end()
}
fn a_matrix_whose_formatting_vfmt_will_not_destroy() {
// 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,
]
// vfmt on
println(indices)
}

66
vlib/v/fmt/vfmt_on_off.v Normal file
View File

@ -0,0 +1,66 @@
module fmt
import v.util
// By default, vfmt *always reformats all source code passed to it* in an uniform way.
// This works in the vast majority of the cases, providing a very nice default and
// consistent look & feel for most V codebases, and eliminating countless hours of bike
// shedding discussions about whether using spaces or tabs is better, and where {} and
// () should be put, and whether comments should be aligned or not.
//
// However, there are indeed situations, where careful manual formatting can increase
// understanding and maintainability of the code. For example, if you write matrix heavy
// code, you may want to preserve the look of your carefully written beautiful matrices,
// so that every reader of your code can bask in their glory.
//
// To enable such manual formatting, this file supports using `vfmt off` and `vfmt on`
// line comments, which can be used for turning off and on vfmt *locally* in selected
// blocks of code, while leaving the vast majority of the code, to be formatted by vfmt.
//
// TLDR:
// All lines after `// vfmt off` are passed on *without any modification* to the output.
// All lines after `// vfmt on` are going to be formatted, and then added to the output.
struct FormatState {
mut:
is_vfmt_on bool = true
last_off_source_line int // the source line where // vfmt off was encountered first
last_off_out_len int // f.out.len when // vfmt off was encountered first
}
fn (mut fs FormatState) reset() {
fs.is_vfmt_on = true
fs.last_off_source_line = 0
fs.last_off_out_len = 0
}
pub fn (mut f Fmt) vfmt_off(off_line int) {
// only trigger once on on->off edges:
if !f.format_state.is_vfmt_on {
return
}
f.format_state.is_vfmt_on = false
f.format_state.last_off_source_line = off_line
f.format_state.last_off_out_len = f.out.len
}
pub fn (mut f Fmt) vfmt_on(on_line int) {
// only trigger once on off->on edges:
if f.format_state.is_vfmt_on {
return
}
f.out.cut_to(f.format_state.last_off_out_len)
f.out.writeln('')
source_lines := f.get_source_lines()#[f.format_state.last_off_source_line + 1..on_line]
for line in source_lines {
f.out.writeln(line)
}
f.format_state.reset()
}
pub fn (mut f Fmt) get_source_lines() []string {
if f.file.path != '' {
return unsafe { util.cached_file2sourcelines(f.file.path) }
}
return f.source_text.split('\n')
}