annotate tests/test_materials.nim @ 777:754835bf175e

add: changing rendering system, not finished yet, also upgrading to Nim 2
author Sam <sam@basx.dev>
date Mon, 07 Aug 2023 00:23:00 +0700
parents 002d9c576756
children e4528d97a687
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
662
9848403320f0 add: support for arrays of samplers
Sam <sam@basx.dev>
parents: 661
diff changeset
1 import std/times
733
07c755e65a4a add: final font-rendering, API changes fixed
Sam <sam@basx.dev>
parents: 714
diff changeset
2 import std/tables
662
9848403320f0 add: support for arrays of samplers
Sam <sam@basx.dev>
parents: 661
diff changeset
3
660
c6414ade79f0 add: new test file to test to do future test with materials etc.
Sam <sam@basx.dev>
parents:
diff changeset
4 import semicongine
c6414ade79f0 add: new test file to test to do future test with materials etc.
Sam <sam@basx.dev>
parents:
diff changeset
5
c6414ade79f0 add: new test file to test to do future test with materials etc.
Sam <sam@basx.dev>
parents:
diff changeset
6 proc main() =
776
002d9c576756 did: preparations to refactor material system, still tons to do
Sam <sam@basx.dev>
parents: 763
diff changeset
7 var flag = rect()
777
754835bf175e add: changing rendering system, not finished yet, also upgrading to Nim 2
Sam <sam@basx.dev>
parents: 776
diff changeset
8 flag.materials = @["material2"]
776
002d9c576756 did: preparations to refactor material system, still tons to do
Sam <sam@basx.dev>
parents: 763
diff changeset
9 var scene = newScene("main", root=newEntity("rect", {"mesh": Component(flag)}))
661
9995702c34b2 add: few improvments for working with textures
Sam <sam@basx.dev>
parents: 660
diff changeset
10 let (RT, WT, PT) = (hexToColorAlpha("A51931").asPixel, hexToColorAlpha("F4F5F8").asPixel, hexToColorAlpha("2D2A4A").asPixel)
662
9848403320f0 add: support for arrays of samplers
Sam <sam@basx.dev>
parents: 661
diff changeset
11 let
705
37ef17c96bdb did: adjust to new API
Sam <sam@basx.dev>
parents: 695
diff changeset
12 # image from memory
776
002d9c576756 did: preparations to refactor material system, still tons to do
Sam <sam@basx.dev>
parents: 763
diff changeset
13 thai = Image(width: 7, height: 5, imagedata: @[
662
9848403320f0 add: support for arrays of samplers
Sam <sam@basx.dev>
parents: 661
diff changeset
14 RT, RT, RT, RT, RT, RT, RT,
9848403320f0 add: support for arrays of samplers
Sam <sam@basx.dev>
parents: 661
diff changeset
15 WT, WT, WT, WT, WT, WT, WT,
9848403320f0 add: support for arrays of samplers
Sam <sam@basx.dev>
parents: 661
diff changeset
16 PT, PT, PT, PT, PT, PT, PT,
9848403320f0 add: support for arrays of samplers
Sam <sam@basx.dev>
parents: 661
diff changeset
17 WT, WT, WT, WT, WT, WT, WT,
9848403320f0 add: support for arrays of samplers
Sam <sam@basx.dev>
parents: 661
diff changeset
18 RT, RT, RT, RT, RT, RT, RT,
9848403320f0 add: support for arrays of samplers
Sam <sam@basx.dev>
parents: 661
diff changeset
19 ])
714
5f7ec8d1bd33 fix: API changes
sam <sam@basx.dev>
parents: 705
diff changeset
20 let
705
37ef17c96bdb did: adjust to new API
Sam <sam@basx.dev>
parents: 695
diff changeset
21 # image from file
776
002d9c576756 did: preparations to refactor material system, still tons to do
Sam <sam@basx.dev>
parents: 763
diff changeset
22 swiss = loadImage("flag.png")
714
5f7ec8d1bd33 fix: API changes
sam <sam@basx.dev>
parents: 705
diff changeset
23
705
37ef17c96bdb did: adjust to new API
Sam <sam@basx.dev>
parents: 695
diff changeset
24 var sampler = DefaultSampler()
37ef17c96bdb did: adjust to new API
Sam <sam@basx.dev>
parents: 695
diff changeset
25 sampler.magnification = VK_FILTER_NEAREST
37ef17c96bdb did: adjust to new API
Sam <sam@basx.dev>
parents: 695
diff changeset
26 sampler.minification = VK_FILTER_NEAREST
776
002d9c576756 did: preparations to refactor material system, still tons to do
Sam <sam@basx.dev>
parents: 763
diff changeset
27 scene.addMaterial(Material(name:"material1", textures: {
777
754835bf175e add: changing rendering system, not finished yet, also upgrading to Nim 2
Sam <sam@basx.dev>
parents: 776
diff changeset
28 "tex1": Texture(image: swiss, sampler: sampler),
754835bf175e add: changing rendering system, not finished yet, also upgrading to Nim 2
Sam <sam@basx.dev>
parents: 776
diff changeset
29 "tex2": Texture(image: thai, sampler: sampler),
776
002d9c576756 did: preparations to refactor material system, still tons to do
Sam <sam@basx.dev>
parents: 763
diff changeset
30 }.toTable))
002d9c576756 did: preparations to refactor material system, still tons to do
Sam <sam@basx.dev>
parents: 763
diff changeset
31 scene.addMaterial(Material(name:"material2", textures: {
777
754835bf175e add: changing rendering system, not finished yet, also upgrading to Nim 2
Sam <sam@basx.dev>
parents: 776
diff changeset
32 "tex1": Texture(image: thai, sampler: sampler),
754835bf175e add: changing rendering system, not finished yet, also upgrading to Nim 2
Sam <sam@basx.dev>
parents: 776
diff changeset
33 "tex2": Texture(image: swiss, sampler: sampler),
776
002d9c576756 did: preparations to refactor material system, still tons to do
Sam <sam@basx.dev>
parents: 763
diff changeset
34 }.toTable))
691
9182a5d2ea3a add: test mesh
Sam <sam@basx.dev>
parents: 672
diff changeset
35 scene.addShaderGlobalArray("test2", @[0'f32, 0'f32])
660
c6414ade79f0 add: new test file to test to do future test with materials etc.
Sam <sam@basx.dev>
parents:
diff changeset
36
c6414ade79f0 add: new test file to test to do future test with materials etc.
Sam <sam@basx.dev>
parents:
diff changeset
37 var engine = initEngine("Test materials")
714
5f7ec8d1bd33 fix: API changes
sam <sam@basx.dev>
parents: 705
diff changeset
38
660
c6414ade79f0 add: new test file to test to do future test with materials etc.
Sam <sam@basx.dev>
parents:
diff changeset
39 const
c6414ade79f0 add: new test file to test to do future test with materials etc.
Sam <sam@basx.dev>
parents:
diff changeset
40 vertexInput = @[
c6414ade79f0 add: new test file to test to do future test with materials etc.
Sam <sam@basx.dev>
parents:
diff changeset
41 attr[Vec3f]("position", memoryPerformanceHint=PreferFastRead),
777
754835bf175e add: changing rendering system, not finished yet, also upgrading to Nim 2
Sam <sam@basx.dev>
parents: 776
diff changeset
42 attr[uint16]("materialIndex", memoryPerformanceHint=PreferFastRead, perInstance=true),
660
c6414ade79f0 add: new test file to test to do future test with materials etc.
Sam <sam@basx.dev>
parents:
diff changeset
43 attr[Vec2f]("uv", memoryPerformanceHint=PreferFastRead),
c6414ade79f0 add: new test file to test to do future test with materials etc.
Sam <sam@basx.dev>
parents:
diff changeset
44 ]
777
754835bf175e add: changing rendering system, not finished yet, also upgrading to Nim 2
Sam <sam@basx.dev>
parents: 776
diff changeset
45 vertexOutput = @[attr[Vec2f]("uvout"), attr[uint16]("materialIndexOut", noInterpolation=true)]
691
9182a5d2ea3a add: test mesh
Sam <sam@basx.dev>
parents: 672
diff changeset
46 uniforms = @[attr[float32]("test2", arrayCount=2)]
776
002d9c576756 did: preparations to refactor material system, still tons to do
Sam <sam@basx.dev>
parents: 763
diff changeset
47 samplers = @[
777
754835bf175e add: changing rendering system, not finished yet, also upgrading to Nim 2
Sam <sam@basx.dev>
parents: 776
diff changeset
48 attr[Sampler2DType]("tex1", arrayCount=2),
754835bf175e add: changing rendering system, not finished yet, also upgrading to Nim 2
Sam <sam@basx.dev>
parents: 776
diff changeset
49 attr[Sampler2DType]("tex2", arrayCount=2),
776
002d9c576756 did: preparations to refactor material system, still tons to do
Sam <sam@basx.dev>
parents: 763
diff changeset
50 ]
660
c6414ade79f0 add: new test file to test to do future test with materials etc.
Sam <sam@basx.dev>
parents:
diff changeset
51 fragOutput = @[attr[Vec4f]("color")]
777
754835bf175e add: changing rendering system, not finished yet, also upgrading to Nim 2
Sam <sam@basx.dev>
parents: 776
diff changeset
52 (vertexCode, fragmentCode) = compileVertexFragmentShaderSet(
660
c6414ade79f0 add: new test file to test to do future test with materials etc.
Sam <sam@basx.dev>
parents:
diff changeset
53 inputs=vertexInput,
777
754835bf175e add: changing rendering system, not finished yet, also upgrading to Nim 2
Sam <sam@basx.dev>
parents: 776
diff changeset
54 intermediate=vertexOutput,
754835bf175e add: changing rendering system, not finished yet, also upgrading to Nim 2
Sam <sam@basx.dev>
parents: 776
diff changeset
55 outputs=fragOutput,
660
c6414ade79f0 add: new test file to test to do future test with materials etc.
Sam <sam@basx.dev>
parents:
diff changeset
56 samplers=samplers,
662
9848403320f0 add: support for arrays of samplers
Sam <sam@basx.dev>
parents: 661
diff changeset
57 uniforms=uniforms,
777
754835bf175e add: changing rendering system, not finished yet, also upgrading to Nim 2
Sam <sam@basx.dev>
parents: 776
diff changeset
58 vertexCode="""
754835bf175e add: changing rendering system, not finished yet, also upgrading to Nim 2
Sam <sam@basx.dev>
parents: 776
diff changeset
59 gl_Position = vec4(position.x, position.y + sin(Uniforms.test2[1]) / Uniforms.test2[1] * 0.5, position.z, 1.0);
754835bf175e add: changing rendering system, not finished yet, also upgrading to Nim 2
Sam <sam@basx.dev>
parents: 776
diff changeset
60 uvout = uv;
754835bf175e add: changing rendering system, not finished yet, also upgrading to Nim 2
Sam <sam@basx.dev>
parents: 776
diff changeset
61 materialIndexOut = materialIndex;""",
754835bf175e add: changing rendering system, not finished yet, also upgrading to Nim 2
Sam <sam@basx.dev>
parents: 776
diff changeset
62 fragmentCode="""
754835bf175e add: changing rendering system, not finished yet, also upgrading to Nim 2
Sam <sam@basx.dev>
parents: 776
diff changeset
63 float d = sin(Uniforms.test2[0]) * 0.5 + 0.5;
754835bf175e add: changing rendering system, not finished yet, also upgrading to Nim 2
Sam <sam@basx.dev>
parents: 776
diff changeset
64 color = texture(tex1[materialIndexOut], uvout) * (1 - d) + texture(tex2[materialIndexOut], uvout) * d;
754835bf175e add: changing rendering system, not finished yet, also upgrading to Nim 2
Sam <sam@basx.dev>
parents: 776
diff changeset
65 """,
660
c6414ade79f0 add: new test file to test to do future test with materials etc.
Sam <sam@basx.dev>
parents:
diff changeset
66 )
c6414ade79f0 add: new test file to test to do future test with materials etc.
Sam <sam@basx.dev>
parents:
diff changeset
67 engine.setRenderer(engine.gpuDevice.simpleForwardRenderPass(vertexCode, fragmentCode))
777
754835bf175e add: changing rendering system, not finished yet, also upgrading to Nim 2
Sam <sam@basx.dev>
parents: 776
diff changeset
68 engine.addScene(scene, vertexInput, samplers, transformAttribute="")
662
9848403320f0 add: support for arrays of samplers
Sam <sam@basx.dev>
parents: 661
diff changeset
69 var t = cpuTime()
660
c6414ade79f0 add: new test file to test to do future test with materials etc.
Sam <sam@basx.dev>
parents:
diff changeset
70 while engine.updateInputs() == Running and not engine.keyIsDown(Escape):
691
9182a5d2ea3a add: test mesh
Sam <sam@basx.dev>
parents: 672
diff changeset
71 var d = float32(cpuTime() - t)
9182a5d2ea3a add: test mesh
Sam <sam@basx.dev>
parents: 672
diff changeset
72 setShaderGlobalArray(scene, "test2", @[d, d * 2])
660
c6414ade79f0 add: new test file to test to do future test with materials etc.
Sam <sam@basx.dev>
parents:
diff changeset
73 engine.renderScene(scene)
c6414ade79f0 add: new test file to test to do future test with materials etc.
Sam <sam@basx.dev>
parents:
diff changeset
74 engine.destroy()
c6414ade79f0 add: new test file to test to do future test with materials etc.
Sam <sam@basx.dev>
parents:
diff changeset
75
c6414ade79f0 add: new test file to test to do future test with materials etc.
Sam <sam@basx.dev>
parents:
diff changeset
76
c6414ade79f0 add: new test file to test to do future test with materials etc.
Sam <sam@basx.dev>
parents:
diff changeset
77 when isMainModule:
c6414ade79f0 add: new test file to test to do future test with materials etc.
Sam <sam@basx.dev>
parents:
diff changeset
78 main()