Mercurial > games > semicongine
diff tests/test_vulkan_wrapper.nim @ 784:fa39e67dded7
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 | 893ec0fbfd44 |
children | cbfe96272205 |
line wrap: on
line diff
--- a/tests/test_vulkan_wrapper.nim Sat Aug 19 01:10:42 2023 +0700 +++ b/tests/test_vulkan_wrapper.nim Sat Aug 19 22:24:06 2023 +0700 @@ -3,14 +3,16 @@ import semicongine -let sampler = Sampler( +let + sampler = Sampler( magnification: VK_FILTER_NEAREST, minification: VK_FILTER_NEAREST, wrapModeS: VK_SAMPLER_ADDRESS_MODE_REPEAT, wrapModeT: VK_SAMPLER_ADDRESS_MODE_REPEAT, ) -let (R, W) = ([255'u8, 0'u8, 0'u8, 255'u8], [255'u8, 255'u8, 255'u8, 255'u8]) -let mat = Material( + (R, W) = ([255'u8, 0'u8, 0'u8, 255'u8], [255'u8, 255'u8, 255'u8, 255'u8]) + mat = Material( + name: "mat", materialType: "my_material", textures: { "my_little_texture": Texture(image: Image(width: 5, height: 5, imagedata: @[ @@ -22,6 +24,26 @@ ]), sampler: sampler) }.toTable ) + mat2 = Material( + name: "mat2", + materialType: "my_material", + textures: { + "my_little_texture": Texture(image: Image(width: 5, height: 5, imagedata: @[ + R, W, R, W, R, + W, R, W, R, W, + R, W, R, W, R, + W, R, W, R, W, + R, W, R, W, R, + ]), sampler: sampler) + }.toTable + ) + mat3 = Material( + name: "mat3", + materialType: "my_special_material", + constants: { + "colors": toGPUValue(newVec4f(0.5, 0.5, 0)) + }.toTable + ) proc scene_different_mesh_types(): Entity = result = newEntity("root", [], @@ -39,31 +61,35 @@ positions=[newVec3f(0.0, 0.5), newVec3f(0.5, -0.5), newVec3f(-0.5, -0.5)], 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)], indices=[[0'u16, 2'u16, 1'u16]], - material=mat, + material=mat2, ))}), newEntity("triangle2b", {"mesh": Component(newMesh( positions=[newVec3f(0.0, 0.4), newVec3f(0.4, -0.4), newVec3f(-0.4, -0.4)], 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)], indices=[[0'u16, 2'u16, 1'u16]], - material=mat, + material=mat2, ))}), newEntity("triangle3a", {"mesh": Component(newMesh( positions=[newVec3f(0.4, 0.5), newVec3f(0.9, -0.3), newVec3f(0.0, -0.3)], 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)], indices=[[0'u32, 2'u32, 1'u32]], autoResize=false, - material=mat, + material=mat2, ))}), newEntity("triangle3b", {"mesh": Component(newMesh( positions=[newVec3f(0.4, 0.5), newVec3f(0.9, -0.3), newVec3f(0.0, -0.3)], 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)], indices=[[0'u32, 2'u32, 1'u32]], autoResize=false, - material=mat, + material=mat2, ))}), ) for mesh in allComponentsOfType[Mesh](result): mesh.setInstanceData("translate", @[newVec3f()]) + result[0]["mesh", Mesh()].updateInstanceData("translate", @[newVec3f(-0.6, -0.6)]) + result[1]["mesh", Mesh()].updateInstanceData("translate", @[newVec3f(-0.6, 0.6)]) + result[2]["mesh", Mesh()].updateInstanceData("translate", @[newVec3f(0.6, -0.6)]) + result[3]["mesh", Mesh()].updateInstanceData("translate", @[newVec3f(0.6, 0.6)]) proc scene_simple(): Entity = var mymesh1 = newMesh( @@ -90,17 +116,17 @@ instanceCount=2, material=mat, ) - mymesh1.setInstanceData("translate", @[newVec3f(0.3, 0.0)]) - mymesh2.setInstanceData("translate", @[newVec3f(0.0, 0.3)]) - mymesh3.setInstanceData("translate", @[newVec3f(-0.3, 0.0)]) - mymesh4.setInstanceData("translate", @[newVec3f(0.0, -0.3), newVec3f(0.0, 0.5)]) + mymesh1.setInstanceData("translate", @[newVec3f( 0.4, 0.4)]) + mymesh2.setInstanceData("translate", @[newVec3f( 0.4, -0.4)]) + mymesh3.setInstanceData("translate", @[newVec3f(-0.4, -0.4)]) + mymesh4.setInstanceData("translate", @[newVec3f(-0.4, 0.4), newVec3f(0.0, 0.0)]) result = newEntity("root", [], newEntity("triangle", {"mesh1": Component(mymesh4), "mesh2": Component(mymesh3), "mesh3": Component(mymesh2), "mesh4": Component(mymesh1)})) proc scene_primitives(): Entity = var r = rect(color="ff0000") var t = tri(color="0000ff") var c = circle(color="00ff00") - t.material = mat + r.material = mat t.material = mat c.material = mat @@ -110,11 +136,20 @@ result = newEntity("root", {"mesh1": Component(t), "mesh2": Component(r), "mesh3": Component(c)}) proc scene_flag(): Entity = - var r = rect(color="ff0000") + var r = rect(color="ffffff") r.material = mat - r.updateMeshData("color", @[newVec4f(0, 0), newVec4f(1, 0), newVec4f(1, 1), newVec4f(0, 1)]) result = newEntity("root", {"mesh": Component(r)}) +proc scene_multi_material(): Entity = + var + r1 = rect(color="ffffff") + r2 = rect(color="000000") + r1.material = mat + r2.material = mat3 + r1.setInstanceData("translate", @[newVec3f(-0.5)]) + r2.setInstanceData("translate", @[newVec3f(+0.5)]) + result = newEntity("root", {"mesh1": Component(r1), "mesh2": Component(r2)}) + proc main() = var engine = initEngine("Test") @@ -124,23 +159,32 @@ inputs=[ attr[Vec3f]("position", memoryPerformanceHint=PreferFastRead), attr[Vec4f]("color", memoryPerformanceHint=PreferFastWrite), - attr[Vec3f]("translate", perInstance=true) + attr[Vec3f]("translate", perInstance=true), + attr[uint16]("materialIndex", perInstance=true), ], - intermediates=[attr[Vec4f]("outcolor")], + intermediates=[ + attr[Vec4f]("outcolor"), + attr[uint16]("materialIndexOut", noInterpolation=true), + ], outputs=[attr[Vec4f]("color")], uniforms=[attr[float32]("time")], - samplers=[attr[Sampler2DType]("my_little_texture")], - vertexCode="""gl_Position = vec4(position + translate, 1.0); outcolor = color;""", - fragmentCode="color = texture(my_little_texture, outcolor.xy) * 0.5 + outcolor * 0.5;", + samplers=[ + attr[Sampler2DType]("my_little_texture", arrayCount=2) + ], + vertexCode="""gl_Position = vec4(position + translate, 1.0); outcolor = color; materialIndexOut = materialIndex;""", + fragmentCode="color = texture(my_little_texture[materialIndexOut], outcolor.xy) * 0.5 + outcolor * 0.5;", ) - engine.setRenderer({"my_material": shaderConfiguration}.toTable) + engine.initRenderer({ + "my_material": shaderConfiguration, + "my_special_material": shaderConfiguration, + }.toTable) # INIT SCENES var scenes = [ - newScene("simple", scene_simple(), transformAttribute="", materialIndexAttribute=""), - newScene("different mesh types", scene_different_mesh_types(), transformAttribute="", materialIndexAttribute=""), - newScene("primitives", scene_primitives(), transformAttribute="", materialIndexAttribute=""), - newScene("flag", scene_flag(), transformAttribute="", materialIndexAttribute=""), + newScene("simple", scene_simple(), transformAttribute=""), + newScene("different mesh types", scene_different_mesh_types(), transformAttribute=""), + newScene("primitives", scene_primitives(), transformAttribute=""), + newScene("flag", scene_multi_material(), transformAttribute=""), ] for scene in scenes.mitems: