Mercurial > games > semicongine
comparison tests/test_vulkan_wrapper.nim @ 323:9defff46da48
add: first complete working version of multiple materials and shaders per scene, yipie :)
author | Sam <sam@basx.dev> |
---|---|
date | Sat, 19 Aug 2023 22:24:06 +0700 |
parents | 6dab370d1758 |
children | cbfe96272205 |
comparison
equal
deleted
inserted
replaced
322:6dab370d1758 | 323:9defff46da48 |
---|---|
1 import std/tables | 1 import std/tables |
2 | 2 |
3 import semicongine | 3 import semicongine |
4 | 4 |
5 | 5 |
6 let sampler = Sampler( | 6 let |
7 sampler = Sampler( | |
7 magnification: VK_FILTER_NEAREST, | 8 magnification: VK_FILTER_NEAREST, |
8 minification: VK_FILTER_NEAREST, | 9 minification: VK_FILTER_NEAREST, |
9 wrapModeS: VK_SAMPLER_ADDRESS_MODE_REPEAT, | 10 wrapModeS: VK_SAMPLER_ADDRESS_MODE_REPEAT, |
10 wrapModeT: VK_SAMPLER_ADDRESS_MODE_REPEAT, | 11 wrapModeT: VK_SAMPLER_ADDRESS_MODE_REPEAT, |
11 ) | 12 ) |
12 let (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]) |
13 let mat = Material( | 14 mat = Material( |
15 name: "mat", | |
14 materialType: "my_material", | 16 materialType: "my_material", |
15 textures: { | 17 textures: { |
16 "my_little_texture": Texture(image: Image(width: 5, height: 5, imagedata: @[ | 18 "my_little_texture": Texture(image: Image(width: 5, height: 5, imagedata: @[ |
17 R, R, R, R, R, | 19 R, R, R, R, R, |
18 R, R, W, R, R, | 20 R, R, W, R, R, |
20 R, R, W, R, R, | 22 R, R, W, R, R, |
21 R, R, R, R, R, | 23 R, R, R, R, R, |
22 ]), sampler: sampler) | 24 ]), sampler: sampler) |
23 }.toTable | 25 }.toTable |
24 ) | 26 ) |
27 mat2 = Material( | |
28 name: "mat2", | |
29 materialType: "my_material", | |
30 textures: { | |
31 "my_little_texture": Texture(image: Image(width: 5, height: 5, imagedata: @[ | |
32 R, W, R, W, R, | |
33 W, R, W, R, W, | |
34 R, W, R, W, R, | |
35 W, R, W, R, W, | |
36 R, W, R, W, R, | |
37 ]), sampler: sampler) | |
38 }.toTable | |
39 ) | |
40 mat3 = Material( | |
41 name: "mat3", | |
42 materialType: "my_special_material", | |
43 constants: { | |
44 "colors": toGPUValue(newVec4f(0.5, 0.5, 0)) | |
45 }.toTable | |
46 ) | |
25 | 47 |
26 proc scene_different_mesh_types(): Entity = | 48 proc scene_different_mesh_types(): Entity = |
27 result = newEntity("root", [], | 49 result = newEntity("root", [], |
28 newEntity("triangle1", {"mesh": Component(newMesh( | 50 newEntity("triangle1", {"mesh": Component(newMesh( |
29 positions=[newVec3f(0.0, -0.5), newVec3f(0.5, 0.5), newVec3f(-0.5, 0.5)], | 51 positions=[newVec3f(0.0, -0.5), newVec3f(0.5, 0.5), newVec3f(-0.5, 0.5)], |
37 ))}), | 59 ))}), |
38 newEntity("triangle2a", {"mesh": Component(newMesh( | 60 newEntity("triangle2a", {"mesh": Component(newMesh( |
39 positions=[newVec3f(0.0, 0.5), newVec3f(0.5, -0.5), newVec3f(-0.5, -0.5)], | 61 positions=[newVec3f(0.0, 0.5), newVec3f(0.5, -0.5), newVec3f(-0.5, -0.5)], |
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)], | 62 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)], |
41 indices=[[0'u16, 2'u16, 1'u16]], | 63 indices=[[0'u16, 2'u16, 1'u16]], |
42 material=mat, | 64 material=mat2, |
43 ))}), | 65 ))}), |
44 newEntity("triangle2b", {"mesh": Component(newMesh( | 66 newEntity("triangle2b", {"mesh": Component(newMesh( |
45 positions=[newVec3f(0.0, 0.4), newVec3f(0.4, -0.4), newVec3f(-0.4, -0.4)], | 67 positions=[newVec3f(0.0, 0.4), newVec3f(0.4, -0.4), newVec3f(-0.4, -0.4)], |
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)], | 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)], |
47 indices=[[0'u16, 2'u16, 1'u16]], | 69 indices=[[0'u16, 2'u16, 1'u16]], |
48 material=mat, | 70 material=mat2, |
49 ))}), | 71 ))}), |
50 newEntity("triangle3a", {"mesh": Component(newMesh( | 72 newEntity("triangle3a", {"mesh": Component(newMesh( |
51 positions=[newVec3f(0.4, 0.5), newVec3f(0.9, -0.3), newVec3f(0.0, -0.3)], | 73 positions=[newVec3f(0.4, 0.5), newVec3f(0.9, -0.3), newVec3f(0.0, -0.3)], |
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)], | 74 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, 2'u32, 1'u32]], | 75 indices=[[0'u32, 2'u32, 1'u32]], |
54 autoResize=false, | 76 autoResize=false, |
55 material=mat, | 77 material=mat2, |
56 ))}), | 78 ))}), |
57 newEntity("triangle3b", {"mesh": Component(newMesh( | 79 newEntity("triangle3b", {"mesh": Component(newMesh( |
58 positions=[newVec3f(0.4, 0.5), newVec3f(0.9, -0.3), newVec3f(0.0, -0.3)], | 80 positions=[newVec3f(0.4, 0.5), newVec3f(0.9, -0.3), newVec3f(0.0, -0.3)], |
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)], | 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)], |
60 indices=[[0'u32, 2'u32, 1'u32]], | 82 indices=[[0'u32, 2'u32, 1'u32]], |
61 autoResize=false, | 83 autoResize=false, |
62 material=mat, | 84 material=mat2, |
63 ))}), | 85 ))}), |
64 ) | 86 ) |
65 for mesh in allComponentsOfType[Mesh](result): | 87 for mesh in allComponentsOfType[Mesh](result): |
66 mesh.setInstanceData("translate", @[newVec3f()]) | 88 mesh.setInstanceData("translate", @[newVec3f()]) |
89 result[0]["mesh", Mesh()].updateInstanceData("translate", @[newVec3f(-0.6, -0.6)]) | |
90 result[1]["mesh", Mesh()].updateInstanceData("translate", @[newVec3f(-0.6, 0.6)]) | |
91 result[2]["mesh", Mesh()].updateInstanceData("translate", @[newVec3f(0.6, -0.6)]) | |
92 result[3]["mesh", Mesh()].updateInstanceData("translate", @[newVec3f(0.6, 0.6)]) | |
67 | 93 |
68 proc scene_simple(): Entity = | 94 proc scene_simple(): Entity = |
69 var mymesh1 = newMesh( | 95 var mymesh1 = newMesh( |
70 positions=[newVec3f(0.0, -0.3), newVec3f(0.3, 0.3), newVec3f(-0.3, 0.3)], | 96 positions=[newVec3f(0.0, -0.3), newVec3f(0.3, 0.3), newVec3f(-0.3, 0.3)], |
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)], | 97 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)], |
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)], | 114 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)], |
89 indices=[[0'u16, 1'u16, 2'u16]], | 115 indices=[[0'u16, 1'u16, 2'u16]], |
90 instanceCount=2, | 116 instanceCount=2, |
91 material=mat, | 117 material=mat, |
92 ) | 118 ) |
93 mymesh1.setInstanceData("translate", @[newVec3f(0.3, 0.0)]) | 119 mymesh1.setInstanceData("translate", @[newVec3f( 0.4, 0.4)]) |
94 mymesh2.setInstanceData("translate", @[newVec3f(0.0, 0.3)]) | 120 mymesh2.setInstanceData("translate", @[newVec3f( 0.4, -0.4)]) |
95 mymesh3.setInstanceData("translate", @[newVec3f(-0.3, 0.0)]) | 121 mymesh3.setInstanceData("translate", @[newVec3f(-0.4, -0.4)]) |
96 mymesh4.setInstanceData("translate", @[newVec3f(0.0, -0.3), newVec3f(0.0, 0.5)]) | 122 mymesh4.setInstanceData("translate", @[newVec3f(-0.4, 0.4), newVec3f(0.0, 0.0)]) |
97 result = newEntity("root", [], newEntity("triangle", {"mesh1": Component(mymesh4), "mesh2": Component(mymesh3), "mesh3": Component(mymesh2), "mesh4": Component(mymesh1)})) | 123 result = newEntity("root", [], newEntity("triangle", {"mesh1": Component(mymesh4), "mesh2": Component(mymesh3), "mesh3": Component(mymesh2), "mesh4": Component(mymesh1)})) |
98 | 124 |
99 proc scene_primitives(): Entity = | 125 proc scene_primitives(): Entity = |
100 var r = rect(color="ff0000") | 126 var r = rect(color="ff0000") |
101 var t = tri(color="0000ff") | 127 var t = tri(color="0000ff") |
102 var c = circle(color="00ff00") | 128 var c = circle(color="00ff00") |
103 t.material = mat | 129 r.material = mat |
104 t.material = mat | 130 t.material = mat |
105 c.material = mat | 131 c.material = mat |
106 | 132 |
107 r.setInstanceData("translate", @[newVec3f(0.5, -0.3)]) | 133 r.setInstanceData("translate", @[newVec3f(0.5, -0.3)]) |
108 t.setInstanceData("translate", @[newVec3f(0.3, 0.3)]) | 134 t.setInstanceData("translate", @[newVec3f(0.3, 0.3)]) |
109 c.setInstanceData("translate", @[newVec3f(-0.3, 0.1)]) | 135 c.setInstanceData("translate", @[newVec3f(-0.3, 0.1)]) |
110 result = newEntity("root", {"mesh1": Component(t), "mesh2": Component(r), "mesh3": Component(c)}) | 136 result = newEntity("root", {"mesh1": Component(t), "mesh2": Component(r), "mesh3": Component(c)}) |
111 | 137 |
112 proc scene_flag(): Entity = | 138 proc scene_flag(): Entity = |
113 var r = rect(color="ff0000") | 139 var r = rect(color="ffffff") |
114 r.material = mat | 140 r.material = mat |
115 r.updateMeshData("color", @[newVec4f(0, 0), newVec4f(1, 0), newVec4f(1, 1), newVec4f(0, 1)]) | |
116 result = newEntity("root", {"mesh": Component(r)}) | 141 result = newEntity("root", {"mesh": Component(r)}) |
142 | |
143 proc scene_multi_material(): Entity = | |
144 var | |
145 r1 = rect(color="ffffff") | |
146 r2 = rect(color="000000") | |
147 r1.material = mat | |
148 r2.material = mat3 | |
149 r1.setInstanceData("translate", @[newVec3f(-0.5)]) | |
150 r2.setInstanceData("translate", @[newVec3f(+0.5)]) | |
151 result = newEntity("root", {"mesh1": Component(r1), "mesh2": Component(r2)}) | |
117 | 152 |
118 proc main() = | 153 proc main() = |
119 var engine = initEngine("Test") | 154 var engine = initEngine("Test") |
120 | 155 |
121 # INIT RENDERER: | 156 # INIT RENDERER: |
122 const | 157 const |
123 shaderConfiguration = createShaderConfiguration( | 158 shaderConfiguration = createShaderConfiguration( |
124 inputs=[ | 159 inputs=[ |
125 attr[Vec3f]("position", memoryPerformanceHint=PreferFastRead), | 160 attr[Vec3f]("position", memoryPerformanceHint=PreferFastRead), |
126 attr[Vec4f]("color", memoryPerformanceHint=PreferFastWrite), | 161 attr[Vec4f]("color", memoryPerformanceHint=PreferFastWrite), |
127 attr[Vec3f]("translate", perInstance=true) | 162 attr[Vec3f]("translate", perInstance=true), |
163 attr[uint16]("materialIndex", perInstance=true), | |
128 ], | 164 ], |
129 intermediates=[attr[Vec4f]("outcolor")], | 165 intermediates=[ |
166 attr[Vec4f]("outcolor"), | |
167 attr[uint16]("materialIndexOut", noInterpolation=true), | |
168 ], | |
130 outputs=[attr[Vec4f]("color")], | 169 outputs=[attr[Vec4f]("color")], |
131 uniforms=[attr[float32]("time")], | 170 uniforms=[attr[float32]("time")], |
132 samplers=[attr[Sampler2DType]("my_little_texture")], | 171 samplers=[ |
133 vertexCode="""gl_Position = vec4(position + translate, 1.0); outcolor = color;""", | 172 attr[Sampler2DType]("my_little_texture", arrayCount=2) |
134 fragmentCode="color = texture(my_little_texture, outcolor.xy) * 0.5 + outcolor * 0.5;", | 173 ], |
174 vertexCode="""gl_Position = vec4(position + translate, 1.0); outcolor = color; materialIndexOut = materialIndex;""", | |
175 fragmentCode="color = texture(my_little_texture[materialIndexOut], outcolor.xy) * 0.5 + outcolor * 0.5;", | |
135 ) | 176 ) |
136 engine.setRenderer({"my_material": shaderConfiguration}.toTable) | 177 engine.initRenderer({ |
178 "my_material": shaderConfiguration, | |
179 "my_special_material": shaderConfiguration, | |
180 }.toTable) | |
137 | 181 |
138 # INIT SCENES | 182 # INIT SCENES |
139 var scenes = [ | 183 var scenes = [ |
140 newScene("simple", scene_simple(), transformAttribute="", materialIndexAttribute=""), | 184 newScene("simple", scene_simple(), transformAttribute=""), |
141 newScene("different mesh types", scene_different_mesh_types(), transformAttribute="", materialIndexAttribute=""), | 185 newScene("different mesh types", scene_different_mesh_types(), transformAttribute=""), |
142 newScene("primitives", scene_primitives(), transformAttribute="", materialIndexAttribute=""), | 186 newScene("primitives", scene_primitives(), transformAttribute=""), |
143 newScene("flag", scene_flag(), transformAttribute="", materialIndexAttribute=""), | 187 newScene("flag", scene_multi_material(), transformAttribute=""), |
144 ] | 188 ] |
145 | 189 |
146 for scene in scenes.mitems: | 190 for scene in scenes.mitems: |
147 scene.addShaderGlobal("time", 0.0'f32) | 191 scene.addShaderGlobal("time", 0.0'f32) |
148 engine.addScene(scene) | 192 engine.addScene(scene) |