comparison tests/test_vulkan_wrapper.nim @ 783:893ec0fbfd44

add: first, incomplete version of material use
author Sam <sam@basx.dev>
date Sat, 19 Aug 2023 01:10:42 +0700
parents b6950ea89b37
children 9defff46da48
comparison
equal deleted inserted replaced
782:b6950ea89b37 783:893ec0fbfd44
1 import std/tables 1 import std/tables
2 2
3 import semicongine 3 import semicongine
4
5
6 let sampler = Sampler(
7 magnification: VK_FILTER_NEAREST,
8 minification: VK_FILTER_NEAREST,
9 wrapModeS: VK_SAMPLER_ADDRESS_MODE_REPEAT,
10 wrapModeT: VK_SAMPLER_ADDRESS_MODE_REPEAT,
11 )
12 let (R, W) = ([255'u8, 0'u8, 0'u8, 255'u8], [255'u8, 255'u8, 255'u8, 255'u8])
13 let mat = Material(
14 materialType: "my_material",
15 textures: {
16 "my_little_texture": Texture(image: Image(width: 5, height: 5, imagedata: @[
17 R, R, R, R, R,
18 R, R, W, R, R,
19 R, W, W, W, R,
20 R, R, W, R, R,
21 R, R, R, R, R,
22 ]), sampler: sampler)
23 }.toTable
24 )
4 25
5 proc scene_different_mesh_types(): Entity = 26 proc scene_different_mesh_types(): Entity =
6 result = newEntity("root", [], 27 result = newEntity("root", [],
7 newEntity("triangle1", {"mesh": Component(newMesh( 28 newEntity("triangle1", {"mesh": Component(newMesh(
8 positions=[newVec3f(0.0, -0.5), newVec3f(0.5, 0.5), newVec3f(-0.5, 0.5)], 29 positions=[newVec3f(0.0, -0.5), newVec3f(0.5, 0.5), newVec3f(-0.5, 0.5)],
9 colors=[newVec4f(1.0, 0.0, 0.0, 1), newVec4f(0.0, 1.0, 0.0, 1), newVec4f(0.0, 0.0, 1.0, 1)], 30 colors=[newVec4f(1.0, 0.0, 0.0, 1), newVec4f(0.0, 1.0, 0.0, 1), newVec4f(0.0, 0.0, 1.0, 1)],
31 material=mat,
10 ))}), 32 ))}),
11 newEntity("triangle1b", {"mesh": Component(newMesh( 33 newEntity("triangle1b", {"mesh": Component(newMesh(
12 positions=[newVec3f(0.0, -0.4), newVec3f(0.4, 0.4), newVec3f(-0.4, 0.5)], 34 positions=[newVec3f(0.0, -0.4), newVec3f(0.4, 0.4), newVec3f(-0.4, 0.5)],
13 colors=[newVec4f(1.0, 0.0, 0.0, 1), newVec4f(0.0, 1.0, 0.0, 1), newVec4f(0.0, 0.0, 1.0, 1)], 35 colors=[newVec4f(1.0, 0.0, 0.0, 1), newVec4f(0.0, 1.0, 0.0, 1), newVec4f(0.0, 0.0, 1.0, 1)],
36 material=mat,
14 ))}), 37 ))}),
15 newEntity("triangle2a", {"mesh": Component(newMesh( 38 newEntity("triangle2a", {"mesh": Component(newMesh(
16 positions=[newVec3f(0.0, 0.5), newVec3f(0.5, -0.5), newVec3f(-0.5, -0.5)], 39 positions=[newVec3f(0.0, 0.5), newVec3f(0.5, -0.5), newVec3f(-0.5, -0.5)],
17 colors=[newVec4f(1.0, 0.0, 0.0, 1), newVec4f(0.0, 1.0, 0.0, 1), newVec4f(0.0, 0.0, 1.0, 1)], 40 colors=[newVec4f(1.0, 0.0, 0.0, 1), newVec4f(0.0, 1.0, 0.0, 1), newVec4f(0.0, 0.0, 1.0, 1)],
18 indices=[[0'u16, 2'u16, 1'u16]] 41 indices=[[0'u16, 2'u16, 1'u16]],
42 material=mat,
19 ))}), 43 ))}),
20 newEntity("triangle2b", {"mesh": Component(newMesh( 44 newEntity("triangle2b", {"mesh": Component(newMesh(
21 positions=[newVec3f(0.0, 0.4), newVec3f(0.4, -0.4), newVec3f(-0.4, -0.4)], 45 positions=[newVec3f(0.0, 0.4), newVec3f(0.4, -0.4), newVec3f(-0.4, -0.4)],
22 colors=[newVec4f(1.0, 0.0, 0.0, 1), newVec4f(0.0, 1.0, 0.0, 1), newVec4f(0.0, 0.0, 1.0, 1)], 46 colors=[newVec4f(1.0, 0.0, 0.0, 1), newVec4f(0.0, 1.0, 0.0, 1), newVec4f(0.0, 0.0, 1.0, 1)],
23 indices=[[0'u16, 2'u16, 1'u16]] 47 indices=[[0'u16, 2'u16, 1'u16]],
48 material=mat,
24 ))}), 49 ))}),
25 newEntity("triangle3a", {"mesh": Component(newMesh( 50 newEntity("triangle3a", {"mesh": Component(newMesh(
26 positions=[newVec3f(0.4, 0.5), newVec3f(0.9, -0.3), newVec3f(0.0, -0.3)], 51 positions=[newVec3f(0.4, 0.5), newVec3f(0.9, -0.3), newVec3f(0.0, -0.3)],
27 colors=[newVec4f(1.0, 1.0, 0.0, 1), newVec4f(1.0, 1.0, 0.0, 1), newVec4f(1.0, 1.0, 0.0, 1)], 52 colors=[newVec4f(1.0, 1.0, 0.0, 1), newVec4f(1.0, 1.0, 0.0, 1), newVec4f(1.0, 1.0, 0.0, 1)],
28 indices=[[0'u32, 2'u32, 1'u32]], 53 indices=[[0'u32, 2'u32, 1'u32]],
29 autoResize=false 54 autoResize=false,
55 material=mat,
30 ))}), 56 ))}),
31 newEntity("triangle3b", {"mesh": Component(newMesh( 57 newEntity("triangle3b", {"mesh": Component(newMesh(
32 positions=[newVec3f(0.4, 0.5), newVec3f(0.9, -0.3), newVec3f(0.0, -0.3)], 58 positions=[newVec3f(0.4, 0.5), newVec3f(0.9, -0.3), newVec3f(0.0, -0.3)],
33 colors=[newVec4f(1.0, 1.0, 0.0, 1), newVec4f(1.0, 1.0, 0.0, 1), newVec4f(1.0, 1.0, 0.0, 1)], 59 colors=[newVec4f(1.0, 1.0, 0.0, 1), newVec4f(1.0, 1.0, 0.0, 1), newVec4f(1.0, 1.0, 0.0, 1)],
34 indices=[[0'u32, 2'u32, 1'u32]], 60 indices=[[0'u32, 2'u32, 1'u32]],
35 autoResize=false 61 autoResize=false,
62 material=mat,
36 ))}), 63 ))}),
37 ) 64 )
38 for mesh in allComponentsOfType[Mesh](result): 65 for mesh in allComponentsOfType[Mesh](result):
39 mesh.setInstanceData("translate", @[newVec3f()]) 66 mesh.setInstanceData("translate", @[newVec3f()])
40 67
41 proc scene_simple(): Entity = 68 proc scene_simple(): Entity =
42 var mymesh1 = newMesh( 69 var mymesh1 = newMesh(
43 positions=[newVec3f(0.0, -0.3), newVec3f(0.3, 0.3), newVec3f(-0.3, 0.3)], 70 positions=[newVec3f(0.0, -0.3), newVec3f(0.3, 0.3), newVec3f(-0.3, 0.3)],
44 colors=[newVec4f(1.0, 0.0, 0.0, 1), newVec4f(0.0, 1.0, 0.0, 1), newVec4f(0.0, 0.0, 1.0, 1)], 71 colors=[newVec4f(1.0, 0.0, 0.0, 1), newVec4f(0.0, 1.0, 0.0, 1), newVec4f(0.0, 0.0, 1.0, 1)],
72 material=mat,
45 ) 73 )
46 var mymesh2 = newMesh( 74 var mymesh2 = newMesh(
47 positions=[newVec3f(0.0, -0.5), newVec3f(0.5, 0.5), newVec3f(-0.5, 0.5)], 75 positions=[newVec3f(0.0, -0.5), newVec3f(0.5, 0.5), newVec3f(-0.5, 0.5)],
48 colors=[newVec4f(1.0, 0.0, 0.0, 1), newVec4f(0.0, 1.0, 0.0, 1), newVec4f(0.0, 0.0, 1.0, 1)], 76 colors=[newVec4f(1.0, 0.0, 0.0, 1), newVec4f(0.0, 1.0, 0.0, 1), newVec4f(0.0, 0.0, 1.0, 1)],
77 material=mat,
49 ) 78 )
50 var mymesh3 = newMesh( 79 var mymesh3 = newMesh(
51 positions=[newVec3f(0.0, -0.6), newVec3f(0.6, 0.6), newVec3f(-0.6, 0.6)], 80 positions=[newVec3f(0.0, -0.6), newVec3f(0.6, 0.6), newVec3f(-0.6, 0.6)],
52 colors=[newVec4f(1.0, 1.0, 0.0, 1), newVec4f(1.0, 1.0, 0.0, 1), newVec4f(1.0, 1.0, 0.0, 1)], 81 colors=[newVec4f(1.0, 1.0, 0.0, 1), newVec4f(1.0, 1.0, 0.0, 1), newVec4f(1.0, 1.0, 0.0, 1)],
53 indices=[[0'u32, 1'u32, 2'u32]], 82 indices=[[0'u32, 1'u32, 2'u32]],
54 autoResize=false 83 autoResize=false,
84 material=mat,
55 ) 85 )
56 var mymesh4 = newMesh( 86 var mymesh4 = newMesh(
57 positions=[newVec3f(0.0, -0.8), newVec3f(0.8, 0.8), newVec3f(-0.8, 0.8)], 87 positions=[newVec3f(0.0, -0.8), newVec3f(0.8, 0.8), newVec3f(-0.8, 0.8)],
58 colors=[newVec4f(0.0, 0.0, 1.0, 1), newVec4f(0.0, 0.0, 1.0, 1), newVec4f(0.0, 0.0, 1.0, 1)], 88 colors=[newVec4f(0.0, 0.0, 1.0, 1), newVec4f(0.0, 0.0, 1.0, 1), newVec4f(0.0, 0.0, 1.0, 1)],
59 indices=[[0'u16, 1'u16, 2'u16]], 89 indices=[[0'u16, 1'u16, 2'u16]],
60 instanceCount=2 90 instanceCount=2,
91 material=mat,
61 ) 92 )
62 mymesh1.setInstanceData("translate", @[newVec3f(0.3, 0.0)]) 93 mymesh1.setInstanceData("translate", @[newVec3f(0.3, 0.0)])
63 mymesh2.setInstanceData("translate", @[newVec3f(0.0, 0.3)]) 94 mymesh2.setInstanceData("translate", @[newVec3f(0.0, 0.3)])
64 mymesh3.setInstanceData("translate", @[newVec3f(-0.3, 0.0)]) 95 mymesh3.setInstanceData("translate", @[newVec3f(-0.3, 0.0)])
65 mymesh4.setInstanceData("translate", @[newVec3f(0.0, -0.3), newVec3f(0.0, 0.5)]) 96 mymesh4.setInstanceData("translate", @[newVec3f(0.0, -0.3), newVec3f(0.0, 0.5)])
67 98
68 proc scene_primitives(): Entity = 99 proc scene_primitives(): Entity =
69 var r = rect(color="ff0000") 100 var r = rect(color="ff0000")
70 var t = tri(color="0000ff") 101 var t = tri(color="0000ff")
71 var c = circle(color="00ff00") 102 var c = circle(color="00ff00")
103 t.material = mat
104 t.material = mat
105 c.material = mat
72 106
73 r.setInstanceData("translate", @[newVec3f(0.5, -0.3)]) 107 r.setInstanceData("translate", @[newVec3f(0.5, -0.3)])
74 t.setInstanceData("translate", @[newVec3f(0.3, 0.3)]) 108 t.setInstanceData("translate", @[newVec3f(0.3, 0.3)])
75 c.setInstanceData("translate", @[newVec3f(-0.3, 0.1)]) 109 c.setInstanceData("translate", @[newVec3f(-0.3, 0.1)])
76 result = newEntity("root", {"mesh1": Component(t), "mesh1": Component(r), "mesh1": Component(c)}) 110 result = newEntity("root", {"mesh1": Component(t), "mesh2": Component(r), "mesh3": Component(c)})
77 111
78 proc scene_flag(): Entity = 112 proc scene_flag(): Entity =
79 var r = rect(color="ff0000") 113 var r = rect(color="ff0000")
114 r.material = mat
80 r.updateMeshData("color", @[newVec4f(0, 0), newVec4f(1, 0), newVec4f(1, 1), newVec4f(0, 1)]) 115 r.updateMeshData("color", @[newVec4f(0, 0), newVec4f(1, 0), newVec4f(1, 1), newVec4f(0, 1)])
81 result = newEntity("root", {"mesh": Component(r)}) 116 result = newEntity("root", {"mesh": Component(r)})
82 117
83 proc main() = 118 proc main() =
84 var engine = initEngine("Test") 119 var engine = initEngine("Test")
96 uniforms=[attr[float32]("time")], 131 uniforms=[attr[float32]("time")],
97 samplers=[attr[Sampler2DType]("my_little_texture")], 132 samplers=[attr[Sampler2DType]("my_little_texture")],
98 vertexCode="""gl_Position = vec4(position + translate, 1.0); outcolor = color;""", 133 vertexCode="""gl_Position = vec4(position + translate, 1.0); outcolor = color;""",
99 fragmentCode="color = texture(my_little_texture, outcolor.xy) * 0.5 + outcolor * 0.5;", 134 fragmentCode="color = texture(my_little_texture, outcolor.xy) * 0.5 + outcolor * 0.5;",
100 ) 135 )
101 var renderPass = engine.gpuDevice.simpleForwardRenderPass(shaderConfiguration) 136 engine.setRenderer({"my_material": shaderConfiguration}.toTable)
102 engine.setRenderer(renderPass)
103 137
104 # INIT SCENES 138 # INIT SCENES
105 var scenes = [ 139 var scenes = [
106 newScene("simple", scene_simple()), 140 newScene("simple", scene_simple(), transformAttribute="", materialIndexAttribute=""),
107 newScene("different mesh types", scene_different_mesh_types()), 141 newScene("different mesh types", scene_different_mesh_types(), transformAttribute="", materialIndexAttribute=""),
108 newScene("primitives", scene_primitives()), 142 newScene("primitives", scene_primitives(), transformAttribute="", materialIndexAttribute=""),
109 newScene("flag", scene_flag()), 143 newScene("flag", scene_flag(), transformAttribute="", materialIndexAttribute=""),
110 ] 144 ]
111 var sampler = DefaultSampler() 145
112 sampler.magnification = VK_FILTER_NEAREST
113 sampler.minification = VK_FILTER_NEAREST
114 for scene in scenes.mitems: 146 for scene in scenes.mitems:
115 scene.addShaderGlobal("time", 0.0'f32) 147 scene.addShaderGlobal("time", 0.0'f32)
116 let (R, W) = ([255'u8, 0'u8, 0'u8, 255'u8], [255'u8, 255'u8, 255'u8, 255'u8]) 148 engine.addScene(scene)
117 scene.addMaterial(Material(
118 name: "my_material",
119 textures: {
120 "my_little_texture": Texture(image: Image(width: 5, height: 5, imagedata: @[
121 R, R, R, R, R,
122 R, R, W, R, R,
123 R, W, W, W, R,
124 R, R, W, R, R,
125 R, R, R, R, R,
126 ]), sampler: sampler)
127 }.toTable
128 ))
129 engine.addScene(scene, vertexInput, samplers, transformAttribute="", materialIndexAttribute="")
130 149
131 # MAINLOOP 150 # MAINLOOP
132 echo "Setup successfull, start rendering" 151 echo "Setup successfull, start rendering"
133 for i in 0 ..< 3: 152 for i in 0 ..< 3:
134 for scene in scenes.mitems: 153 for scene in scenes.mitems: