annotate tests/test_materials.nim @ 698:c136bfdca960

fix: incorrect vertex data alignment
author Sam <sam@basx.dev>
date Sat, 20 May 2023 18:17:21 +0700
parents d2e8b5edea7e
children f52fccedf5ab
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
9848403320f0 add: support for arrays of samplers
Sam <sam@basx.dev>
parents: 661
diff changeset
2
660
c6414ade79f0 add: new test file to test to do future test with materials etc.
Sam <sam@basx.dev>
parents:
diff changeset
3 import semicongine
c6414ade79f0 add: new test file to test to do future test with materials etc.
Sam <sam@basx.dev>
parents:
diff changeset
4
c6414ade79f0 add: new test file to test to do future test with materials etc.
Sam <sam@basx.dev>
parents:
diff changeset
5 proc main() =
c6414ade79f0 add: new test file to test to do future test with materials etc.
Sam <sam@basx.dev>
parents:
diff changeset
6 var scene = newScene("main", root=newEntity("rect", rect()))
661
9995702c34b2 add: few improvments for working with textures
Sam <sam@basx.dev>
parents: 660
diff changeset
7 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
8 let
695
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents: 691
diff changeset
9 t1 = Image(width: 7, height: 5, imagedata: @[
662
9848403320f0 add: support for arrays of samplers
Sam <sam@basx.dev>
parents: 661
diff changeset
10 RT, RT, RT, RT, RT, RT, RT,
9848403320f0 add: support for arrays of samplers
Sam <sam@basx.dev>
parents: 661
diff changeset
11 WT, WT, WT, WT, WT, WT, WT,
9848403320f0 add: support for arrays of samplers
Sam <sam@basx.dev>
parents: 661
diff changeset
12 PT, PT, PT, PT, PT, PT, PT,
9848403320f0 add: support for arrays of samplers
Sam <sam@basx.dev>
parents: 661
diff changeset
13 WT, WT, WT, WT, WT, WT, WT,
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 ])
695
d2e8b5edea7e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents: 691
diff changeset
16 t2 = loadImage("flag.png")
672
d7c61e6580ea did: refactor image handling
Sam <sam@basx.dev>
parents: 662
diff changeset
17 scene.addTextures("my_texture", @[t1, t2], interpolation=VK_FILTER_NEAREST)
691
9182a5d2ea3a add: test mesh
Sam <sam@basx.dev>
parents: 672
diff changeset
18 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
19
c6414ade79f0 add: new test file to test to do future test with materials etc.
Sam <sam@basx.dev>
parents:
diff changeset
20 var engine = initEngine("Test materials")
c6414ade79f0 add: new test file to test to do future test with materials etc.
Sam <sam@basx.dev>
parents:
diff changeset
21 const
c6414ade79f0 add: new test file to test to do future test with materials etc.
Sam <sam@basx.dev>
parents:
diff changeset
22 vertexInput = @[
c6414ade79f0 add: new test file to test to do future test with materials etc.
Sam <sam@basx.dev>
parents:
diff changeset
23 attr[Vec3f]("position", memoryPerformanceHint=PreferFastRead),
c6414ade79f0 add: new test file to test to do future test with materials etc.
Sam <sam@basx.dev>
parents:
diff changeset
24 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
25 ]
c6414ade79f0 add: new test file to test to do future test with materials etc.
Sam <sam@basx.dev>
parents:
diff changeset
26 vertexOutput = @[attr[Vec2f]("uvout")]
691
9182a5d2ea3a add: test mesh
Sam <sam@basx.dev>
parents: 672
diff changeset
27 uniforms = @[attr[float32]("test2", arrayCount=2)]
662
9848403320f0 add: support for arrays of samplers
Sam <sam@basx.dev>
parents: 661
diff changeset
28 samplers = @[attr[Sampler2DType]("my_texture", arrayCount=2)]
660
c6414ade79f0 add: new test file to test to do future test with materials etc.
Sam <sam@basx.dev>
parents:
diff changeset
29 fragOutput = @[attr[Vec4f]("color")]
c6414ade79f0 add: new test file to test to do future test with materials etc.
Sam <sam@basx.dev>
parents:
diff changeset
30 vertexCode = compileGlslShader(
c6414ade79f0 add: new test file to test to do future test with materials etc.
Sam <sam@basx.dev>
parents:
diff changeset
31 stage=VK_SHADER_STAGE_VERTEX_BIT,
c6414ade79f0 add: new test file to test to do future test with materials etc.
Sam <sam@basx.dev>
parents:
diff changeset
32 inputs=vertexInput,
662
9848403320f0 add: support for arrays of samplers
Sam <sam@basx.dev>
parents: 661
diff changeset
33 uniforms=uniforms,
660
c6414ade79f0 add: new test file to test to do future test with materials etc.
Sam <sam@basx.dev>
parents:
diff changeset
34 samplers=samplers,
c6414ade79f0 add: new test file to test to do future test with materials etc.
Sam <sam@basx.dev>
parents:
diff changeset
35 outputs=vertexOutput,
691
9182a5d2ea3a add: test mesh
Sam <sam@basx.dev>
parents: 672
diff changeset
36 main="""gl_Position = vec4(position.x, position.y + sin(Uniforms.test2[1]) / Uniforms.test2[1] * 0.5, position.z, 1.0); uvout = uv;"""
660
c6414ade79f0 add: new test file to test to do future test with materials etc.
Sam <sam@basx.dev>
parents:
diff changeset
37 )
c6414ade79f0 add: new test file to test to do future test with materials etc.
Sam <sam@basx.dev>
parents:
diff changeset
38 fragmentCode = compileGlslShader(
c6414ade79f0 add: new test file to test to do future test with materials etc.
Sam <sam@basx.dev>
parents:
diff changeset
39 stage=VK_SHADER_STAGE_FRAGMENT_BIT,
c6414ade79f0 add: new test file to test to do future test with materials etc.
Sam <sam@basx.dev>
parents:
diff changeset
40 inputs=vertexOutput,
662
9848403320f0 add: support for arrays of samplers
Sam <sam@basx.dev>
parents: 661
diff changeset
41 uniforms=uniforms,
660
c6414ade79f0 add: new test file to test to do future test with materials etc.
Sam <sam@basx.dev>
parents:
diff changeset
42 samplers=samplers,
c6414ade79f0 add: new test file to test to do future test with materials etc.
Sam <sam@basx.dev>
parents:
diff changeset
43 outputs=fragOutput,
662
9848403320f0 add: support for arrays of samplers
Sam <sam@basx.dev>
parents: 661
diff changeset
44 main="""
691
9182a5d2ea3a add: test mesh
Sam <sam@basx.dev>
parents: 672
diff changeset
45 float d = sin(Uniforms.test2[0]) * 0.5 + 0.5;
662
9848403320f0 add: support for arrays of samplers
Sam <sam@basx.dev>
parents: 661
diff changeset
46 color = texture(my_texture[0], uvout) * (1 - d) + texture(my_texture[1], uvout) * d;
9848403320f0 add: support for arrays of samplers
Sam <sam@basx.dev>
parents: 661
diff changeset
47 """
660
c6414ade79f0 add: new test file to test to do future test with materials etc.
Sam <sam@basx.dev>
parents:
diff changeset
48 )
c6414ade79f0 add: new test file to test to do future test with materials etc.
Sam <sam@basx.dev>
parents:
diff changeset
49 engine.setRenderer(engine.gpuDevice.simpleForwardRenderPass(vertexCode, fragmentCode))
c6414ade79f0 add: new test file to test to do future test with materials etc.
Sam <sam@basx.dev>
parents:
diff changeset
50 engine.addScene(scene, vertexInput)
662
9848403320f0 add: support for arrays of samplers
Sam <sam@basx.dev>
parents: 661
diff changeset
51 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
52 while engine.updateInputs() == Running and not engine.keyIsDown(Escape):
691
9182a5d2ea3a add: test mesh
Sam <sam@basx.dev>
parents: 672
diff changeset
53 var d = float32(cpuTime() - t)
9182a5d2ea3a add: test mesh
Sam <sam@basx.dev>
parents: 672
diff changeset
54 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
55 engine.renderScene(scene)
c6414ade79f0 add: new test file to test to do future test with materials etc.
Sam <sam@basx.dev>
parents:
diff changeset
56 engine.destroy()
c6414ade79f0 add: new test file to test to do future test with materials etc.
Sam <sam@basx.dev>
parents:
diff changeset
57
c6414ade79f0 add: new test file to test to do future test with materials etc.
Sam <sam@basx.dev>
parents:
diff changeset
58
c6414ade79f0 add: new test file to test to do future test with materials etc.
Sam <sam@basx.dev>
parents:
diff changeset
59 when isMainModule:
c6414ade79f0 add: new test file to test to do future test with materials etc.
Sam <sam@basx.dev>
parents:
diff changeset
60 main()