Mercurial > games > semicongine
comparison old_tests/test_vulkan_wrapper.nim @ 1203:6360c8d17ce0 compiletime-tests
did: preprations to add rendering tests
author | sam <sam@basx.dev> |
---|---|
date | Mon, 15 Jul 2024 20:06:42 +0700 |
parents | tests/test_vulkan_wrapper.nim@114f395b9144 |
children |
comparison
equal
deleted
inserted
replaced
1202:a8864fe6fe6e | 1203:6360c8d17ce0 |
---|---|
1 import std/tables | |
2 | |
3 import semicongine | |
4 | |
5 | |
6 let | |
7 sampler = Sampler( | |
8 magnification: VK_FILTER_NEAREST, | |
9 minification: VK_FILTER_NEAREST, | |
10 wrapModeS: VK_SAMPLER_ADDRESS_MODE_REPEAT, | |
11 wrapModeT: VK_SAMPLER_ADDRESS_MODE_REPEAT, | |
12 ) | |
13 (R, W) = ([255'u8, 0'u8, 0'u8, 255'u8], [255'u8, 255'u8, 255'u8, 255'u8]) | |
14 Mat1Type = MaterialType( | |
15 name: "single texture material 1", | |
16 vertexAttributes: { | |
17 "color": Vec4F32, | |
18 "position": Vec3F32, | |
19 "uv": Vec2F32, | |
20 }.toTable, | |
21 attributes: {"baseTexture": TextureType}.toTable | |
22 ) | |
23 mat = Mat1Type.InitMaterialData( | |
24 name = "mat", | |
25 attributes = { | |
26 "baseTexture": InitDataList(@[Texture(isGrayscale: false, colorImage: Image[RGBAPixel](width: 5, height: 5, imagedata: @[ | |
27 R, R, R, R, R, | |
28 R, R, W, R, R, | |
29 R, W, W, W, R, | |
30 R, R, W, R, R, | |
31 R, R, R, R, R, | |
32 ]), sampler: sampler)]) | |
33 }.toTable | |
34 ) | |
35 Mat2Type = MaterialType( | |
36 name: "single texture material 2", | |
37 vertexAttributes: { | |
38 "color": Vec4F32, | |
39 "position": Vec3F32, | |
40 "uv": Vec2F32, | |
41 }.toTable, | |
42 attributes: {"baseTexture": TextureType}.toTable | |
43 ) | |
44 mat2 = Mat2Type.InitMaterialData( | |
45 name = "mat2", | |
46 attributes = { | |
47 "baseTexture": InitDataList(@[Texture(isGrayscale: false, colorImage: Image[RGBAPixel](width: 5, height: 5, imagedata: @[ | |
48 R, W, R, W, R, | |
49 W, R, W, R, W, | |
50 R, W, R, W, R, | |
51 W, R, W, R, W, | |
52 R, W, R, W, R, | |
53 ]), sampler: sampler)]) | |
54 }.toTable | |
55 ) | |
56 mat3 = SINGLE_COLOR_MATERIAL.InitMaterialData( | |
57 name = "mat3", | |
58 attributes = { | |
59 "color": InitDataList(@[NewVec4f(0, 1, 0, 1)]) | |
60 }.toTable | |
61 ) | |
62 | |
63 proc scene_different_mesh_types(): seq[Mesh] = | |
64 @[ | |
65 NewMesh( | |
66 positions = [NewVec3f(0.0, -0.5), NewVec3f(0.5, 0.5), NewVec3f(-0.5, 0.5)], | |
67 uvs = [NewVec2f(0.0, -0.5), NewVec2f(0.5, 0.5), NewVec2f(-0.5, 0.5)], | |
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)], | |
69 material = mat, | |
70 transform = Translate(-0.7, -0.5), | |
71 ), | |
72 NewMesh( | |
73 positions = [NewVec3f(0.0, -0.4), NewVec3f(0.4, 0.4), NewVec3f(-0.4, 0.5)], | |
74 uvs = [NewVec2f(0.0, -0.4), NewVec2f(0.4, 0.4), NewVec2f(-0.4, 0.5)], | |
75 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 material = mat, | |
77 transform = Translate(0, -0.5), | |
78 ), | |
79 NewMesh( | |
80 positions = [NewVec3f(0.0, 0.5), NewVec3f(0.5, -0.5), NewVec3f(-0.5, -0.5)], | |
81 uvs = [NewVec2f(0.0, 0.5), NewVec2f(0.5, -0.5), NewVec2f(-0.5, -0.5)], | |
82 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)], | |
83 indices = [[0'u16, 2'u16, 1'u16]], | |
84 material = mat2, | |
85 transform = Translate(0.7, -0.5), | |
86 ), | |
87 NewMesh( | |
88 positions = [NewVec3f(0.0, 0.4), NewVec3f(0.4, -0.4), NewVec3f(-0.4, -0.4)], | |
89 uvs = [NewVec2f(0.0, 0.4), NewVec2f(0.4, -0.4), NewVec2f(-0.4, -0.4)], | |
90 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)], | |
91 indices = [[0'u16, 2'u16, 1'u16]], | |
92 material = mat2, | |
93 transform = Translate(-0.7, 0.5), | |
94 ), | |
95 NewMesh( | |
96 positions = [NewVec3f(0.4, 0.5), NewVec3f(0.9, -0.3), NewVec3f(0.0, -0.3)], | |
97 uvs = [NewVec2f(0.4, 0.5), NewVec2f(0.9, -0.3), NewVec2f(0.0, -0.3)], | |
98 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)], | |
99 indices = [[0'u32, 2'u32, 1'u32]], | |
100 autoResize = false, | |
101 material = mat2, | |
102 transform = Translate(0, 0.5), | |
103 ), | |
104 NewMesh( | |
105 positions = [NewVec3f(0.4, 0.5), NewVec3f(0.9, -0.3), NewVec3f(0.0, -0.3)], | |
106 uvs = [NewVec2f(0.4, 0.5), NewVec2f(0.9, -0.3), NewVec2f(0.0, -0.3)], | |
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)], | |
108 indices = [[0'u32, 2'u32, 1'u32]], | |
109 autoResize = false, | |
110 material = mat2, | |
111 transform = Translate(0.7, 0.5), | |
112 ), | |
113 ] | |
114 | |
115 proc scene_simple(): seq[Mesh] = | |
116 @[ | |
117 NewMesh( | |
118 positions = [NewVec3f(0.0, -0.3), NewVec3f(0.3, 0.3), NewVec3f(-0.3, 0.3)], | |
119 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)], | |
120 uvs = [NewVec2f(0.0, -0.3), NewVec2f(0.3, 0.3), NewVec2f(-0.3, 0.3)], | |
121 material = mat, | |
122 transform = Translate(0.4, 0.4), | |
123 ), | |
124 NewMesh( | |
125 positions = [NewVec3f(0.0, -0.5), NewVec3f(0.5, 0.5), NewVec3f(-0.5, 0.5)], | |
126 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)], | |
127 uvs = [NewVec2f(0.0, -0.5), NewVec2f(0.5, 0.5), NewVec2f(-0.5, 0.5)], | |
128 material = mat, | |
129 transform = Translate(0.4, -0.4), | |
130 ), | |
131 NewMesh( | |
132 positions = [NewVec3f(0.0, -0.6), NewVec3f(0.6, 0.6), NewVec3f(-0.6, 0.6)], | |
133 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)], | |
134 uvs = [NewVec2f(0.0, -0.6), NewVec2f(0.6, 0.6), NewVec2f(-0.6, 0.6)], | |
135 indices = [[0'u32, 1'u32, 2'u32]], | |
136 autoResize = false, | |
137 material = mat, | |
138 transform = Translate(-0.4, 0.4), | |
139 ), | |
140 NewMesh( | |
141 positions = [NewVec3f(0.0, -0.8), NewVec3f(0.8, 0.8), NewVec3f(-0.8, 0.8)], | |
142 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)], | |
143 uvs = [NewVec2f(0.0, -0.8), NewVec2f(0.8, 0.8), NewVec2f(-0.8, 0.8)], | |
144 indices = [[0'u16, 1'u16, 2'u16]], | |
145 instanceTransforms = [Unit4F32, Unit4F32], | |
146 material = mat, | |
147 transform = Translate(-0.4, -0.4), | |
148 ) | |
149 ] | |
150 | |
151 proc scene_primitives(): seq[Mesh] = | |
152 var r = Rect(color = "ff0000") | |
153 var t = Tri(color = "0000ff") | |
154 var c = Circle(color = "00ff00") | |
155 r.material = mat | |
156 t.material = mat | |
157 c.material = mat | |
158 r.transform = Translate(NewVec3f(0.5, -0.3)) | |
159 t.transform = Translate(NewVec3f(0.3, 0.3)) | |
160 c.transform = Translate(NewVec3f(-0.3, 0.1)) | |
161 result = @[r, c, t] | |
162 | |
163 proc scene_flag(): seq[Mesh] = | |
164 @[ | |
165 NewMesh( | |
166 positions = [NewVec3f(-1.0, -1.0), NewVec3f(1.0, -1.0), NewVec3f(1.0, 1.0), NewVec3f(-1.0, 1.0)], | |
167 uvs = [NewVec2f(-1.0, -1.0), NewVec2f(1.0, -1.0), NewVec2f(1.0, 1.0), NewVec2f(-1.0, 1.0)], | |
168 colors = [NewVec4f(-1, -1, 1, 1), NewVec4f(1, -1, 1, 1), NewVec4f(1, 1, 1, 1), NewVec4f(-1, 1, 1, 1)], | |
169 indices = [[0'u16, 1'u16, 2'u16], [2'u16, 3'u16, 0'u16]], | |
170 material = mat, | |
171 transform = Scale(0.5, 0.5) | |
172 ) | |
173 ] | |
174 | |
175 proc scene_multi_material(): seq[Mesh] = | |
176 var | |
177 r1 = Rect(color = "ffffff") | |
178 r2 = Rect(color = "000000") | |
179 r1.material = mat | |
180 r2.material = mat3 | |
181 r1.transform = Translate(NewVec3f(-0.5)) | |
182 r2.transform = Translate(NewVec3f(+0.5)) | |
183 result = @[r1, r2] | |
184 | |
185 proc main() = | |
186 var engine = InitEngine("Test") | |
187 | |
188 # INIT RENDERER: | |
189 const | |
190 shaderConfiguration1 = CreateShaderConfiguration( | |
191 name = "shader1", | |
192 inputs = [ | |
193 Attr[Vec3f]("position", memoryPerformanceHint = PreferFastRead), | |
194 Attr[Vec4f]("color", memoryPerformanceHint = PreferFastWrite), | |
195 Attr[Mat4]("transform", perInstance = true), | |
196 ], | |
197 intermediates = [ | |
198 Attr[Vec4f]("outcolor"), | |
199 ], | |
200 outputs = [Attr[Vec4f]("color")], | |
201 samplers = [Attr[Texture]("baseTexture")], | |
202 vertexCode = """gl_Position = vec4(position, 1.0) * transform; outcolor = color;""", | |
203 fragmentCode = "color = texture(baseTexture, outcolor.xy) * 0.5 + outcolor * 0.5;", | |
204 ) | |
205 shaderConfiguration2 = CreateShaderConfiguration( | |
206 name = "shader2", | |
207 inputs = [ | |
208 Attr[Vec3f]("position", memoryPerformanceHint = PreferFastRead), | |
209 Attr[Mat4]("transform", perInstance = true), | |
210 ], | |
211 intermediates = [Attr[Vec4f]("outcolor")], | |
212 outputs = [Attr[Vec4f]("color")], | |
213 uniforms = [Attr[Vec4f]("color", arrayCount = 1)], | |
214 vertexCode = """gl_Position = vec4(position, 1.0) * transform; outcolor = Uniforms.color[0];""", | |
215 fragmentCode = "color = outcolor;", | |
216 ) | |
217 engine.InitRenderer({ | |
218 Mat1Type: shaderConfiguration1, | |
219 Mat1Type: shaderConfiguration1, | |
220 Mat2Type: shaderConfiguration1, | |
221 SINGLE_COLOR_MATERIAL: shaderConfiguration2, | |
222 }) | |
223 | |
224 # INIT SCENES | |
225 var scenes = [ | |
226 Scene(name: "simple", meshes: scene_simple()), | |
227 Scene(name: "different mesh types", meshes: scene_different_mesh_types()), | |
228 Scene(name: "primitives", meshes: scene_primitives()), | |
229 Scene(name: "flag", meshes: scene_flag()), | |
230 Scene(name: "multimaterial", meshes: scene_multi_material()), | |
231 ] | |
232 | |
233 for scene in scenes.mitems: | |
234 engine.LoadScene(scene) | |
235 | |
236 # MAINLOOP | |
237 echo "Setup successfull, start rendering" | |
238 for i in 0 ..< 3: | |
239 for scene in scenes.mitems: | |
240 echo "rendering scene ", scene.name | |
241 for i in 0 ..< 1000: | |
242 if not engine.UpdateInputs() or KeyIsDown(Escape): | |
243 engine.Destroy() | |
244 return | |
245 engine.RenderScene(scene) | |
246 | |
247 # cleanup | |
248 echo "Start cleanup" | |
249 engine.Destroy() | |
250 | |
251 when isMainModule: | |
252 main() |