comparison tests/test_vulkan_wrapper.nim @ 368:51ee41c1d8ed

fix: first example to work correctly with new material system
author Sam <sam@basx.dev>
date Sat, 21 Oct 2023 01:06:51 +0700
parents 61c5d5fe9d93
children f054b8bacab8
comparison
equal deleted inserted replaced
367:7ef01f1841b3 368:51ee41c1d8ed
9 minification: VK_FILTER_NEAREST, 9 minification: VK_FILTER_NEAREST,
10 wrapModeS: VK_SAMPLER_ADDRESS_MODE_REPEAT, 10 wrapModeS: VK_SAMPLER_ADDRESS_MODE_REPEAT,
11 wrapModeT: VK_SAMPLER_ADDRESS_MODE_REPEAT, 11 wrapModeT: VK_SAMPLER_ADDRESS_MODE_REPEAT,
12 ) 12 )
13 (R, W) = ([255'u8, 0'u8, 0'u8, 255'u8], [255'u8, 255'u8, 255'u8, 255'u8]) 13 (R, W) = ([255'u8, 0'u8, 0'u8, 255'u8], [255'u8, 255'u8, 255'u8, 255'u8])
14 mat = Material( 14 mat = SINGLE_TEXTURE_MATERIAL.initMaterialData(
15 name: "mat", 15 name="mat",
16 textures: { 16 attributes={
17 "my_little_texture": Texture(image: Image(width: 5, height: 5, imagedata: @[ 17 "texture1": initDataList(@[Texture(image: Image(width: 5, height: 5, imagedata: @[
18 R, R, R, R, R, 18 R, R, R, R, R,
19 R, R, W, R, R, 19 R, R, W, R, R,
20 R, W, W, W, R, 20 R, W, W, W, R,
21 R, R, W, R, R, 21 R, R, W, R, R,
22 R, R, R, R, R, 22 R, R, R, R, R,
23 ]), sampler: sampler) 23 ]), sampler: sampler)])
24 }.toTable 24 }.toTable
25 ) 25 )
26 mat2 = Material( 26 mat2 = SINGLE_TEXTURE_MATERIAL.initMaterialData(
27 name: "mat2", 27 name="mat2",
28 textures: { 28 attributes={
29 "my_little_texture": Texture(image: Image(width: 5, height: 5, imagedata: @[ 29 "texture1": initDataList(@[Texture(image: Image(width: 5, height: 5, imagedata: @[
30 R, W, R, W, R, 30 R, W, R, W, R,
31 W, R, W, R, W, 31 W, R, W, R, W,
32 R, W, R, W, R, 32 R, W, R, W, R,
33 W, R, W, R, W, 33 W, R, W, R, W,
34 R, W, R, W, R, 34 R, W, R, W, R,
35 ]), sampler: sampler) 35 ]), sampler: sampler)])
36 }.toTable 36 }.toTable
37 ) 37 )
38 mat3 = Material( 38 mat3 = SINGLE_COLOR_MATERIAL.initMaterialData(
39 name: "mat3", 39 name="mat3",
40 constants: { 40 attributes={
41 "color": toGPUValue(@[newVec4f(0, 1, 0, 1)]) 41 "color": initDataList(@[newVec4f(0, 1, 0, 1)])
42 }.toTable 42 }.toTable
43 ) 43 )
44 44
45 proc scene_different_mesh_types(): seq[Mesh] = 45 proc scene_different_mesh_types(): seq[Mesh] =
46 @[ 46 @[
47 newMesh( 47 newMesh(
48 positions=[newVec3f(0.0, -0.5), newVec3f(0.5, 0.5), newVec3f(-0.5, 0.5)], 48 positions=[newVec3f(0.0, -0.5), newVec3f(0.5, 0.5), newVec3f(-0.5, 0.5)],
49 uvs=[newVec2f(0.0, -0.5), newVec2f(0.5, 0.5), newVec2f(-0.5, 0.5)],
49 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)], 50 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)],
50 material=mat, 51 material=mat,
51 transform=translate(-0.7, -0.5), 52 transform=translate(-0.7, -0.5),
52 ), 53 ),
53 newMesh( 54 newMesh(
54 positions=[newVec3f(0.0, -0.4), newVec3f(0.4, 0.4), newVec3f(-0.4, 0.5)], 55 positions=[newVec3f(0.0, -0.4), newVec3f(0.4, 0.4), newVec3f(-0.4, 0.5)],
56 uvs=[newVec2f(0.0, -0.4), newVec2f(0.4, 0.4), newVec2f(-0.4, 0.5)],
55 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)], 57 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)],
56 material=mat, 58 material=mat,
57 transform=translate(0, -0.5), 59 transform=translate(0, -0.5),
58 ), 60 ),
59 newMesh( 61 newMesh(
60 positions=[newVec3f(0.0, 0.5), newVec3f(0.5, -0.5), newVec3f(-0.5, -0.5)], 62 positions=[newVec3f(0.0, 0.5), newVec3f(0.5, -0.5), newVec3f(-0.5, -0.5)],
63 uvs=[newVec2f(0.0, 0.5), newVec2f(0.5, -0.5), newVec2f(-0.5, -0.5)],
61 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)], 64 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)],
62 indices=[[0'u16, 2'u16, 1'u16]], 65 indices=[[0'u16, 2'u16, 1'u16]],
63 material=mat2, 66 material=mat2,
64 transform=translate(0.7, -0.5), 67 transform=translate(0.7, -0.5),
65 ), 68 ),
66 newMesh( 69 newMesh(
67 positions=[newVec3f(0.0, 0.4), newVec3f(0.4, -0.4), newVec3f(-0.4, -0.4)], 70 positions=[newVec3f(0.0, 0.4), newVec3f(0.4, -0.4), newVec3f(-0.4, -0.4)],
71 uvs=[newVec2f(0.0, 0.4), newVec2f(0.4, -0.4), newVec2f(-0.4, -0.4)],
68 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 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)],
69 indices=[[0'u16, 2'u16, 1'u16]], 73 indices=[[0'u16, 2'u16, 1'u16]],
70 material=mat2, 74 material=mat2,
71 transform=translate(-0.7, 0.5), 75 transform=translate(-0.7, 0.5),
72 ), 76 ),
73 newMesh( 77 newMesh(
74 positions=[newVec3f(0.4, 0.5), newVec3f(0.9, -0.3), newVec3f(0.0, -0.3)], 78 positions=[newVec3f(0.4, 0.5), newVec3f(0.9, -0.3), newVec3f(0.0, -0.3)],
79 uvs=[newVec2f(0.4, 0.5), newVec2f(0.9, -0.3), newVec2f(0.0, -0.3)],
75 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)], 80 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)],
76 indices=[[0'u32, 2'u32, 1'u32]], 81 indices=[[0'u32, 2'u32, 1'u32]],
77 autoResize=false, 82 autoResize=false,
78 material=mat2, 83 material=mat2,
79 transform=translate(0, 0.5), 84 transform=translate(0, 0.5),
80 ), 85 ),
81 newMesh( 86 newMesh(
82 positions=[newVec3f(0.4, 0.5), newVec3f(0.9, -0.3), newVec3f(0.0, -0.3)], 87 positions=[newVec3f(0.4, 0.5), newVec3f(0.9, -0.3), newVec3f(0.0, -0.3)],
88 uvs=[newVec2f(0.4, 0.5), newVec2f(0.9, -0.3), newVec2f(0.0, -0.3)],
83 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)], 89 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)],
84 indices=[[0'u32, 2'u32, 1'u32]], 90 indices=[[0'u32, 2'u32, 1'u32]],
85 autoResize=false, 91 autoResize=false,
86 material=mat2, 92 material=mat2,
87 transform=translate(0.7, 0.5), 93 transform=translate(0.7, 0.5),
91 proc scene_simple(): seq[Mesh] = 97 proc scene_simple(): seq[Mesh] =
92 @[ 98 @[
93 newMesh( 99 newMesh(
94 positions=[newVec3f(0.0, -0.3), newVec3f(0.3, 0.3), newVec3f(-0.3, 0.3)], 100 positions=[newVec3f(0.0, -0.3), newVec3f(0.3, 0.3), newVec3f(-0.3, 0.3)],
95 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)], 101 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)],
102 uvs=[newVec2f(0.0, -0.3), newVec2f(0.3, 0.3), newVec2f(-0.3, 0.3)],
96 material=mat, 103 material=mat,
97 transform=translate(0.4, 0.4), 104 transform=translate(0.4, 0.4),
98 ), 105 ),
99 newMesh( 106 newMesh(
100 positions=[newVec3f(0.0, -0.5), newVec3f(0.5, 0.5), newVec3f(-0.5, 0.5)], 107 positions=[newVec3f(0.0, -0.5), newVec3f(0.5, 0.5), newVec3f(-0.5, 0.5)],
101 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)], 108 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)],
109 uvs=[newVec2f(0.0, -0.5), newVec2f(0.5, 0.5), newVec2f(-0.5, 0.5)],
102 material=mat, 110 material=mat,
103 transform=translate(0.4, -0.4), 111 transform=translate(0.4, -0.4),
104 ), 112 ),
105 newMesh( 113 newMesh(
106 positions=[newVec3f(0.0, -0.6), newVec3f(0.6, 0.6), newVec3f(-0.6, 0.6)], 114 positions=[newVec3f(0.0, -0.6), newVec3f(0.6, 0.6), newVec3f(-0.6, 0.6)],
107 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)], 115 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)],
116 uvs=[newVec2f(0.0, -0.6), newVec2f(0.6, 0.6), newVec2f(-0.6, 0.6)],
108 indices=[[0'u32, 1'u32, 2'u32]], 117 indices=[[0'u32, 1'u32, 2'u32]],
109 autoResize=false, 118 autoResize=false,
110 material=mat, 119 material=mat,
111 transform=translate(-0.4, 0.4), 120 transform=translate(-0.4, 0.4),
112 ), 121 ),
113 newMesh( 122 newMesh(
114 positions=[newVec3f(0.0, -0.8), newVec3f(0.8, 0.8), newVec3f(-0.8, 0.8)], 123 positions=[newVec3f(0.0, -0.8), newVec3f(0.8, 0.8), newVec3f(-0.8, 0.8)],
115 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)], 124 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)],
125 uvs=[newVec2f(0.0, -0.8), newVec2f(0.8, 0.8), newVec2f(-0.8, 0.8)],
116 indices=[[0'u16, 1'u16, 2'u16]], 126 indices=[[0'u16, 1'u16, 2'u16]],
117 instanceTransforms=[Unit4F32, Unit4F32], 127 instanceTransforms=[Unit4F32, Unit4F32],
118 material=mat, 128 material=mat,
119 transform=translate(-0.4, -0.4), 129 transform=translate(-0.4, -0.4),
120 ) 130 )
122 132
123 proc scene_primitives(): seq[Mesh] = 133 proc scene_primitives(): seq[Mesh] =
124 var r = rect(color="ff0000") 134 var r = rect(color="ff0000")
125 var t = tri(color="0000ff") 135 var t = tri(color="0000ff")
126 var c = circle(color="00ff00") 136 var c = circle(color="00ff00")
127 r.materials = @[mat] 137 r.material = mat
128 t.materials = @[mat] 138 t.material = mat
129 c.materials = @[mat] 139 c.material = mat
130 r.transform = translate(newVec3f(0.5, -0.3)) 140 r.transform = translate(newVec3f(0.5, -0.3))
131 t.transform = translate(newVec3f(0.3, 0.3)) 141 t.transform = translate(newVec3f(0.3, 0.3))
132 c.transform = translate(newVec3f(-0.3, 0.1)) 142 c.transform = translate(newVec3f(-0.3, 0.1))
133 result = @[r, c, t] 143 result = @[r, c, t]
134 144
135 proc scene_flag(): seq[Mesh] = 145 proc scene_flag(): seq[Mesh] =
136 @[ 146 @[
137 newMesh( 147 newMesh(
138 positions=[newVec3f(-1.0, -1.0), newVec3f(1.0, -1.0), newVec3f(1.0, 1.0), newVec3f(-1.0, 1.0)], 148 positions=[newVec3f(-1.0, -1.0), newVec3f(1.0, -1.0), newVec3f(1.0, 1.0), newVec3f(-1.0, 1.0)],
149 uvs=[newVec2f(-1.0, -1.0), newVec2f(1.0, -1.0), newVec2f(1.0, 1.0), newVec2f(-1.0, 1.0)],
139 colors=[newVec4f(-1, -1, 1, 1), newVec4f(1, -1, 1, 1), newVec4f(1, 1, 1, 1), newVec4f(-1, 1, 1, 1)], 150 colors=[newVec4f(-1, -1, 1, 1), newVec4f(1, -1, 1, 1), newVec4f(1, 1, 1, 1), newVec4f(-1, 1, 1, 1)],
140 indices=[[0'u16, 1'u16, 2'u16], [2'u16, 3'u16, 0'u16]], 151 indices=[[0'u16, 1'u16, 2'u16], [2'u16, 3'u16, 0'u16]],
141 material=mat, 152 material=mat,
142 transform=scale(0.5, 0.5) 153 transform=scale(0.5, 0.5)
143 ) 154 )
145 156
146 proc scene_multi_material(): seq[Mesh] = 157 proc scene_multi_material(): seq[Mesh] =
147 var 158 var
148 r1 = rect(color="ffffff") 159 r1 = rect(color="ffffff")
149 r2 = rect(color="000000") 160 r2 = rect(color="000000")
150 r1.materials = @[mat] 161 r1.material = mat
151 r2.materials = @[mat3] 162 r2.material = mat3
152 r1.transform = translate(newVec3f(-0.5)) 163 r1.transform = translate(newVec3f(-0.5))
153 r2.transform = translate(newVec3f(+0.5)) 164 r2.transform = translate(newVec3f(+0.5))
154 result = @[r1, r2] 165 result = @[r1, r2]
155 166
156 proc main() = 167 proc main() =
167 intermediates=[ 178 intermediates=[
168 attr[Vec4f]("outcolor"), 179 attr[Vec4f]("outcolor"),
169 ], 180 ],
170 outputs=[attr[Vec4f]("color")], 181 outputs=[attr[Vec4f]("color")],
171 samplers=[ 182 samplers=[
172 attr[Texture]("my_little_texture") 183 attr[Texture]("texture1")
173 ], 184 ],
174 vertexCode="""gl_Position = vec4(position, 1.0) * transform; outcolor = color;""", 185 vertexCode="""gl_Position = vec4(position, 1.0) * transform; outcolor = color;""",
175 fragmentCode="color = texture(my_little_texture, outcolor.xy) * 0.5 + outcolor * 0.5;", 186 fragmentCode="color = texture(texture1, outcolor.xy) * 0.5 + outcolor * 0.5;",
176 ) 187 )
177 shaderConfiguration2 = createShaderConfiguration( 188 shaderConfiguration2 = createShaderConfiguration(
178 inputs=[ 189 inputs=[
179 attr[Vec3f]("position", memoryPerformanceHint=PreferFastRead), 190 attr[Vec3f]("position", memoryPerformanceHint=PreferFastRead),
180 attr[Mat4]("transform", perInstance=true), 191 attr[Mat4]("transform", perInstance=true),
184 uniforms=[attr[Vec4f]("color", arrayCount=1)], 195 uniforms=[attr[Vec4f]("color", arrayCount=1)],
185 vertexCode="""gl_Position = vec4(position, 1.0) * transform; outcolor = Uniforms.color[0];""", 196 vertexCode="""gl_Position = vec4(position, 1.0) * transform; outcolor = Uniforms.color[0];""",
186 fragmentCode="color = outcolor;", 197 fragmentCode="color = outcolor;",
187 ) 198 )
188 engine.initRenderer({ 199 engine.initRenderer({
189 "mat": shaderConfiguration1, 200 SINGLE_TEXTURE_MATERIAL: shaderConfiguration1,
190 "mat2": shaderConfiguration1, 201 SINGLE_TEXTURE_MATERIAL: shaderConfiguration1,
191 "mat3": shaderConfiguration2, 202 SINGLE_COLOR_MATERIAL: shaderConfiguration2,
192 }) 203 })
193 204
194 # INIT SCENES 205 # INIT SCENES
195 var scenes = [ 206 var scenes = [
196 Scene(name: "simple", meshes: scene_simple()), 207 Scene(name: "simple", meshes: scene_simple()),
199 Scene(name: "flag", meshes: scene_flag()), 210 Scene(name: "flag", meshes: scene_flag()),
200 Scene(name: "multimaterial", meshes: scene_multi_material()), 211 Scene(name: "multimaterial", meshes: scene_multi_material()),
201 ] 212 ]
202 213
203 for scene in scenes.mitems: 214 for scene in scenes.mitems:
204 engine.addScene(scene) 215 engine.loadScene(scene)
205 216
206 # MAINLOOP 217 # MAINLOOP
207 echo "Setup successfull, start rendering" 218 echo "Setup successfull, start rendering"
208 for i in 0 ..< 3: 219 for i in 0 ..< 3:
209 for scene in scenes.mitems: 220 for scene in scenes.mitems: