Mercurial > games > semicongine
comparison tests/test_vulkan_wrapper.nim @ 326:4ec852355750
fix: many issues, better mesh-handling, still need to cope with different binding numbers when using different pipelines...
| author | Sam <sam@basx.dev> |
|---|---|
| date | Fri, 25 Aug 2023 00:29:51 +0700 |
| parents | 0c3f4f6f1aa2 |
| children | a63bd8f29252 |
comparison
equal
deleted
inserted
replaced
| 325:0c3f4f6f1aa2 | 326:4ec852355750 |
|---|---|
| 39 ) | 39 ) |
| 40 mat3 = Material( | 40 mat3 = Material( |
| 41 name: "mat3", | 41 name: "mat3", |
| 42 materialType: "my_special_material", | 42 materialType: "my_special_material", |
| 43 constants: { | 43 constants: { |
| 44 "color": toGPUValue(newVec4f(0.5, 0.5, 0)) | 44 "color": toGPUValue(newVec4f(0, 1, 0, 1)) |
| 45 }.toTable | 45 }.toTable |
| 46 ) | 46 ) |
| 47 | 47 |
| 48 proc scene_different_mesh_types(): Entity = | 48 proc scene_different_mesh_types(): seq[Mesh] = |
| 49 result = newEntity("root", [], | 49 @[ |
| 50 newEntity("triangle1", {"mesh": Component(newMesh( | 50 newMesh( |
| 51 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)], |
| 52 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)], | 52 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)], |
| 53 material=mat, | 53 material=mat, |
| 54 ))}), | 54 transform=translate(-0.7, -0.5), |
| 55 newEntity("triangle1b", {"mesh": Component(newMesh( | 55 ), |
| 56 newMesh( | |
| 56 positions=[newVec3f(0.0, -0.4), newVec3f(0.4, 0.4), newVec3f(-0.4, 0.5)], | 57 positions=[newVec3f(0.0, -0.4), newVec3f(0.4, 0.4), newVec3f(-0.4, 0.5)], |
| 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)], | 58 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)], |
| 58 material=mat, | 59 material=mat, |
| 59 ))}), | 60 transform=translate(0, -0.5), |
| 60 newEntity("triangle2a", {"mesh": Component(newMesh( | 61 ), |
| 62 newMesh( | |
| 61 positions=[newVec3f(0.0, 0.5), newVec3f(0.5, -0.5), newVec3f(-0.5, -0.5)], | 63 positions=[newVec3f(0.0, 0.5), newVec3f(0.5, -0.5), newVec3f(-0.5, -0.5)], |
| 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)], | 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)], |
| 63 indices=[[0'u16, 2'u16, 1'u16]], | 65 indices=[[0'u16, 2'u16, 1'u16]], |
| 64 material=mat2, | 66 material=mat2, |
| 65 ))}), | 67 transform=translate(0.7, -0.5), |
| 66 newEntity("triangle2b", {"mesh": Component(newMesh( | 68 ), |
| 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)], |
| 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)], | 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)], |
| 69 indices=[[0'u16, 2'u16, 1'u16]], | 72 indices=[[0'u16, 2'u16, 1'u16]], |
| 70 material=mat2, | 73 material=mat2, |
| 71 ))}), | 74 transform=translate(-0.7, 0.5), |
| 72 newEntity("triangle3a", {"mesh": Component(newMesh( | 75 ), |
| 76 newMesh( | |
| 73 positions=[newVec3f(0.4, 0.5), newVec3f(0.9, -0.3), newVec3f(0.0, -0.3)], | 77 positions=[newVec3f(0.4, 0.5), newVec3f(0.9, -0.3), newVec3f(0.0, -0.3)], |
| 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)], | 78 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)], |
| 75 indices=[[0'u32, 2'u32, 1'u32]], | 79 indices=[[0'u32, 2'u32, 1'u32]], |
| 76 autoResize=false, | 80 autoResize=false, |
| 77 material=mat2, | 81 material=mat2, |
| 78 ))}), | 82 transform=translate(0, 0.5), |
| 79 newEntity("triangle3b", {"mesh": Component(newMesh( | 83 ), |
| 84 newMesh( | |
| 80 positions=[newVec3f(0.4, 0.5), newVec3f(0.9, -0.3), newVec3f(0.0, -0.3)], | 85 positions=[newVec3f(0.4, 0.5), newVec3f(0.9, -0.3), newVec3f(0.0, -0.3)], |
| 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)], | 86 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)], |
| 82 indices=[[0'u32, 2'u32, 1'u32]], | 87 indices=[[0'u32, 2'u32, 1'u32]], |
| 83 autoResize=false, | 88 autoResize=false, |
| 84 material=mat2, | 89 material=mat2, |
| 85 ))}), | 90 transform=translate(0.7, 0.5), |
| 86 ) | 91 ), |
| 87 for mesh in allComponentsOfType[Mesh](result): | 92 ] |
| 88 mesh.setInstanceData("translate", @[newVec3f()]) | 93 |
| 89 result[0]["mesh", Mesh()].updateInstanceData("translate", @[newVec3f(-0.6, -0.6)]) | 94 proc scene_simple(): seq[Mesh] = |
| 90 result[1]["mesh", Mesh()].updateInstanceData("translate", @[newVec3f(-0.6, 0.6)]) | 95 @[ |
| 91 result[2]["mesh", Mesh()].updateInstanceData("translate", @[newVec3f(0.6, -0.6)]) | 96 newMesh( |
| 92 result[3]["mesh", Mesh()].updateInstanceData("translate", @[newVec3f(0.6, 0.6)]) | 97 positions=[newVec3f(0.0, -0.3), newVec3f(0.3, 0.3), newVec3f(-0.3, 0.3)], |
| 93 | 98 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)], |
| 94 proc scene_simple(): Entity = | 99 material=mat, |
| 95 var mymesh1 = newMesh( | 100 transform=translate(0.4, 0.4), |
| 96 positions=[newVec3f(0.0, -0.3), newVec3f(0.3, 0.3), newVec3f(-0.3, 0.3)], | 101 ), |
| 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)], | 102 newMesh( |
| 98 material=mat, | 103 positions=[newVec3f(0.0, -0.5), newVec3f(0.5, 0.5), newVec3f(-0.5, 0.5)], |
| 99 ) | 104 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)], |
| 100 var mymesh2 = newMesh( | 105 material=mat, |
| 101 positions=[newVec3f(0.0, -0.5), newVec3f(0.5, 0.5), newVec3f(-0.5, 0.5)], | 106 transform=translate(0.4, -0.4), |
| 102 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)], | 107 ), |
| 103 material=mat, | 108 newMesh( |
| 104 ) | 109 positions=[newVec3f(0.0, -0.6), newVec3f(0.6, 0.6), newVec3f(-0.6, 0.6)], |
| 105 var mymesh3 = newMesh( | 110 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)], |
| 106 positions=[newVec3f(0.0, -0.6), newVec3f(0.6, 0.6), newVec3f(-0.6, 0.6)], | 111 indices=[[0'u32, 1'u32, 2'u32]], |
| 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)], | 112 autoResize=false, |
| 108 indices=[[0'u32, 1'u32, 2'u32]], | 113 material=mat, |
| 109 autoResize=false, | 114 transform=translate(-0.4, 0.4), |
| 110 material=mat, | 115 ), |
| 111 ) | 116 newMesh( |
| 112 var mymesh4 = newMesh( | 117 positions=[newVec3f(0.0, -0.8), newVec3f(0.8, 0.8), newVec3f(-0.8, 0.8)], |
| 113 positions=[newVec3f(0.0, -0.8), newVec3f(0.8, 0.8), newVec3f(-0.8, 0.8)], | 118 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)], | 119 indices=[[0'u16, 1'u16, 2'u16]], |
| 115 indices=[[0'u16, 1'u16, 2'u16]], | 120 instanceTransforms=[Unit4F32, Unit4F32], |
| 116 instanceCount=2, | 121 material=mat, |
| 117 material=mat, | 122 transform=translate(-0.4, -0.4), |
| 118 ) | 123 ) |
| 119 mymesh1.setInstanceData("translate", @[newVec3f( 0.4, 0.4)]) | 124 ] |
| 120 mymesh2.setInstanceData("translate", @[newVec3f( 0.4, -0.4)]) | 125 |
| 121 mymesh3.setInstanceData("translate", @[newVec3f(-0.4, -0.4)]) | 126 proc scene_primitives(): seq[Mesh] = |
| 122 mymesh4.setInstanceData("translate", @[newVec3f(-0.4, 0.4), newVec3f(0.0, 0.0)]) | |
| 123 result = newEntity("root", [], newEntity("triangle", {"mesh1": Component(mymesh4), "mesh2": Component(mymesh3), "mesh3": Component(mymesh2), "mesh4": Component(mymesh1)})) | |
| 124 | |
| 125 proc scene_primitives(): Entity = | |
| 126 var r = rect(color="ff0000") | 127 var r = rect(color="ff0000") |
| 127 var t = tri(color="0000ff") | 128 var t = tri(color="0000ff") |
| 128 var c = circle(color="00ff00") | 129 var c = circle(color="00ff00") |
| 129 r.material = mat | 130 r.material = mat |
| 130 t.material = mat | 131 t.material = mat |
| 131 c.material = mat | 132 c.material = mat |
| 132 | 133 r.transform = translate(newVec3f(0.5, -0.3)) |
| 133 r.setInstanceData("translate", @[newVec3f(0.5, -0.3)]) | 134 t.transform = translate(newVec3f(0.3, 0.3)) |
| 134 t.setInstanceData("translate", @[newVec3f(0.3, 0.3)]) | 135 c.transform = translate(newVec3f(-0.3, 0.1)) |
| 135 c.setInstanceData("translate", @[newVec3f(-0.3, 0.1)]) | 136 result = @[r, c, t] |
| 136 result = newEntity("root", {"mesh1": Component(t), "mesh2": Component(r), "mesh3": Component(c)}) | 137 |
| 137 | 138 proc scene_flag(): seq[Mesh] = |
| 138 proc scene_flag(): Entity = | 139 @[ |
| 139 var r = rect(color="ffffff") | 140 newMesh( |
| 140 r.material = mat | 141 positions=[newVec3f(-1.0, -1.0), newVec3f(1.0, -1.0), newVec3f(1.0, 1.0), newVec3f(-1.0, 1.0)], |
| 141 result = newEntity("root", {"mesh": Component(r)}) | 142 colors=[newVec4f(-1, -1, 1, 1), newVec4f(1, -1, 1, 1), newVec4f(1, 1, 1, 1), newVec4f(-1, 1, 1, 1)], |
| 142 | 143 indices=[[0'u16, 1'u16, 2'u16], [2'u16, 3'u16, 0'u16]], |
| 143 proc scene_multi_material(): Entity = | 144 material=mat, |
| 145 transform=scale(0.5, 0.5) | |
| 146 ) | |
| 147 ] | |
| 148 | |
| 149 proc scene_multi_material(): seq[Mesh] = | |
| 144 var | 150 var |
| 145 r1 = rect(color="ffffff") | 151 r1 = rect(color="ffffff") |
| 146 r2 = rect(color="000000") | 152 r2 = rect(color="000000") |
| 147 r1.material = mat | 153 r1.material = mat |
| 148 r2.material = mat3 | 154 r2.material = mat3 |
| 149 r1.setInstanceData("translate", @[newVec3f(-0.5)]) | 155 r1.transform = translate(newVec3f(-0.5)) |
| 150 r2.setInstanceData("translate", @[newVec3f(+0.5)]) | 156 r2.transform = translate(newVec3f(+0.5)) |
| 151 result = newEntity("root", {"mesh1": Component(r1), "mesh2": Component(r2)}) | 157 result = @[r1, r2] |
| 152 | 158 |
| 153 proc main() = | 159 proc main() = |
| 154 var engine = initEngine("Test") | 160 var engine = initEngine("Test") |
| 155 | 161 |
| 156 # INIT RENDERER: | 162 # INIT RENDERER: |
| 157 const | 163 const |
| 158 shaderConfiguration1 = createShaderConfiguration( | 164 shaderConfiguration1 = createShaderConfiguration( |
| 159 inputs=[ | 165 inputs=[ |
| 160 attr[Vec3f]("position", memoryPerformanceHint=PreferFastRead), | 166 attr[Vec3f]("position", memoryPerformanceHint=PreferFastRead), |
| 161 attr[Vec4f]("color", memoryPerformanceHint=PreferFastWrite), | 167 attr[Vec4f]("color", memoryPerformanceHint=PreferFastWrite), |
| 162 attr[Vec3f]("translate", perInstance=true), | 168 attr[Mat4]("transform", perInstance=true), |
| 163 attr[uint16]("materialIndex", perInstance=true), | 169 attr[uint16]("materialIndex", perInstance=true), |
| 164 ], | 170 ], |
| 165 intermediates=[ | 171 intermediates=[ |
| 166 attr[Vec4f]("outcolor"), | 172 attr[Vec4f]("outcolor"), |
| 167 attr[uint16]("materialIndexOut", noInterpolation=true), | 173 attr[uint16]("materialIndexOut", noInterpolation=true), |
| 169 outputs=[attr[Vec4f]("color")], | 175 outputs=[attr[Vec4f]("color")], |
| 170 uniforms=[attr[float32]("time")], | 176 uniforms=[attr[float32]("time")], |
| 171 samplers=[ | 177 samplers=[ |
| 172 attr[Sampler2DType]("my_little_texture", arrayCount=2) | 178 attr[Sampler2DType]("my_little_texture", arrayCount=2) |
| 173 ], | 179 ], |
| 174 vertexCode="""gl_Position = vec4(position + translate, 1.0); outcolor = color; materialIndexOut = materialIndex;""", | 180 vertexCode="""gl_Position = vec4(position, 1.0) * transform; outcolor = color; materialIndexOut = materialIndex;""", |
| 175 fragmentCode="color = texture(my_little_texture[materialIndexOut], outcolor.xy) * 0.5 + outcolor * 0.5;", | 181 fragmentCode="color = texture(my_little_texture[materialIndexOut], outcolor.xy) * 0.5 + outcolor * 0.5;", |
| 176 ) | 182 ) |
| 177 shaderConfiguration2 = createShaderConfiguration( | 183 shaderConfiguration2 = createShaderConfiguration( |
| 178 inputs=[ | 184 inputs=[ |
| 179 attr[Vec3f]("position", memoryPerformanceHint=PreferFastRead), | 185 attr[Vec3f]("position", memoryPerformanceHint=PreferFastRead), |
| 180 attr[Vec3f]("translate", perInstance=true), | 186 attr[Mat4]("transform", perInstance=true), |
| 181 ], | 187 ], |
| 182 intermediates=[attr[Vec4f]("outcolor")], | 188 intermediates=[attr[Vec4f]("outcolor")], |
| 183 outputs=[attr[Vec4f]("color")], | 189 outputs=[attr[Vec4f]("color")], |
| 184 uniforms=[attr[Vec4f]("color")], | 190 uniforms=[attr[Vec4f]("color")], |
| 185 vertexCode="""gl_Position = vec4(position + translate, 1.0); outcolor = Uniforms.color;""", | 191 vertexCode="""gl_Position = vec4(position, 1.0) * transform; outcolor = Uniforms.color;""", |
| 186 fragmentCode="color = outcolor;", | 192 fragmentCode="color = vec4(0, 0, 1, 1);", |
| 187 ) | 193 ) |
| 188 engine.initRenderer({ | 194 engine.initRenderer({ |
| 189 "my_material": shaderConfiguration1, | 195 "my_material": shaderConfiguration1, |
| 190 "my_special_material": shaderConfiguration2, | 196 "my_special_material": shaderConfiguration2, |
| 191 }.toTable) | 197 }.toTable) |
| 192 | 198 |
| 193 # INIT SCENES | 199 # INIT SCENES |
| 194 var scenes = [ | 200 var scenes = [ |
| 195 newScene("simple", scene_simple(), transformAttribute=""), | 201 # Scene(name: "simple", meshes: scene_simple()), |
| 196 newScene("different mesh types", scene_different_mesh_types(), transformAttribute=""), | 202 # Scene(name: "different mesh types", meshes: scene_different_mesh_types()), |
| 197 newScene("primitives", scene_primitives(), transformAttribute=""), | 203 # Scene(name: "primitives", meshes: scene_primitives()), |
| 198 newScene("flag", scene_multi_material(), transformAttribute=""), | 204 # Scene(name: "flag", meshes: scene_flag()), |
| 199 ] | 205 # Scene(name: "multimaterial", meshes: scene_multi_material(), materialIndexAttribute: ""), |
| 200 | 206 Scene(name: "multimaterial", meshes: scene_multi_material()), |
| 201 scenes[3].addShaderGlobal("color", newVec4f(1, 0, 1)) | 207 ] |
| 208 | |
| 209 scenes[0].addShaderGlobal("color", newVec4f(1, 0, 0, 1)) | |
| 202 for scene in scenes.mitems: | 210 for scene in scenes.mitems: |
| 203 scene.addShaderGlobal("time", 0.0'f32) | 211 scene.addShaderGlobal("time", 0.0'f32) |
| 204 engine.addScene(scene) | 212 engine.addScene(scene) |
| 205 | |
| 206 | 213 |
| 207 # MAINLOOP | 214 # MAINLOOP |
| 208 echo "Setup successfull, start rendering" | 215 echo "Setup successfull, start rendering" |
| 209 for i in 0 ..< 3: | 216 for i in 0 ..< 3: |
| 210 for scene in scenes.mitems: | 217 for scene in scenes.mitems: |
| 218 echo "rendering scene ", scene.name | |
| 211 for i in 0 ..< 1000: | 219 for i in 0 ..< 1000: |
| 212 if engine.updateInputs() != Running or engine.keyIsDown(Escape): | 220 if engine.updateInputs() != Running or engine.keyIsDown(Escape): |
| 213 engine.destroy() | 221 engine.destroy() |
| 214 return | 222 return |
| 215 setShaderGlobal(scene, "time", getShaderGlobal[float32](scene, "time") + 0.0005'f) | 223 setShaderGlobal(scene, "time", getShaderGlobal[float32](scene, "time") + 0.0005'f) |
