annotate tests/test_materials.nim @ 211:744285b47a4d

did: refactor image handling
author Sam <sam@basx.dev>
date Tue, 09 May 2023 19:52:57 +0700
parents ab626e67a1ee
children 027f6ff06585
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
201
ab626e67a1ee add: support for arrays of samplers
Sam <sam@basx.dev>
parents: 200
diff changeset
1 import std/times
ab626e67a1ee add: support for arrays of samplers
Sam <sam@basx.dev>
parents: 200
diff changeset
2
199
da68fe12f600 add: new test file to test to do future test with materials etc.
Sam <sam@basx.dev>
parents:
diff changeset
3 import semicongine
da68fe12f600 add: new test file to test to do future test with materials etc.
Sam <sam@basx.dev>
parents:
diff changeset
4
da68fe12f600 add: new test file to test to do future test with materials etc.
Sam <sam@basx.dev>
parents:
diff changeset
5 proc main() =
da68fe12f600 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()))
da68fe12f600 add: new test file to test to do future test with materials etc.
Sam <sam@basx.dev>
parents:
diff changeset
7 let (R, W) = ([255'u8, 0'u8, 0'u8, 255'u8], [255'u8, 255'u8, 255'u8, 255'u8])
200
1ba005328615 add: few improvments for working with textures
Sam <sam@basx.dev>
parents: 199
diff changeset
8 let (RT, WT, PT) = (hexToColorAlpha("A51931").asPixel, hexToColorAlpha("F4F5F8").asPixel, hexToColorAlpha("2D2A4A").asPixel)
201
ab626e67a1ee add: support for arrays of samplers
Sam <sam@basx.dev>
parents: 200
diff changeset
9 let
211
744285b47a4d did: refactor image handling
Sam <sam@basx.dev>
parents: 201
diff changeset
10 t1 = Image(width: 5, height: 5, imagedata: @[
201
ab626e67a1ee add: support for arrays of samplers
Sam <sam@basx.dev>
parents: 200
diff changeset
11 R, R, R, R, R,
ab626e67a1ee add: support for arrays of samplers
Sam <sam@basx.dev>
parents: 200
diff changeset
12 R, R, W, R, R,
ab626e67a1ee add: support for arrays of samplers
Sam <sam@basx.dev>
parents: 200
diff changeset
13 R, W, W, W, R,
ab626e67a1ee add: support for arrays of samplers
Sam <sam@basx.dev>
parents: 200
diff changeset
14 R, R, W, R, R,
ab626e67a1ee add: support for arrays of samplers
Sam <sam@basx.dev>
parents: 200
diff changeset
15 R, R, R, R, R,
ab626e67a1ee add: support for arrays of samplers
Sam <sam@basx.dev>
parents: 200
diff changeset
16 ])
211
744285b47a4d did: refactor image handling
Sam <sam@basx.dev>
parents: 201
diff changeset
17 t2 = Image(width: 7, height: 5, imagedata: @[
201
ab626e67a1ee add: support for arrays of samplers
Sam <sam@basx.dev>
parents: 200
diff changeset
18 RT, RT, RT, RT, RT, RT, RT,
ab626e67a1ee add: support for arrays of samplers
Sam <sam@basx.dev>
parents: 200
diff changeset
19 WT, WT, WT, WT, WT, WT, WT,
ab626e67a1ee add: support for arrays of samplers
Sam <sam@basx.dev>
parents: 200
diff changeset
20 PT, PT, PT, PT, PT, PT, PT,
ab626e67a1ee add: support for arrays of samplers
Sam <sam@basx.dev>
parents: 200
diff changeset
21 WT, WT, WT, WT, WT, WT, WT,
ab626e67a1ee add: support for arrays of samplers
Sam <sam@basx.dev>
parents: 200
diff changeset
22 RT, RT, RT, RT, RT, RT, RT,
ab626e67a1ee add: support for arrays of samplers
Sam <sam@basx.dev>
parents: 200
diff changeset
23 ])
211
744285b47a4d did: refactor image handling
Sam <sam@basx.dev>
parents: 201
diff changeset
24 scene.addTextures("my_texture", @[t1, t2], interpolation=VK_FILTER_NEAREST)
201
ab626e67a1ee add: support for arrays of samplers
Sam <sam@basx.dev>
parents: 200
diff changeset
25 scene.addShaderGlobal("time", 0'f32)
200
1ba005328615 add: few improvments for working with textures
Sam <sam@basx.dev>
parents: 199
diff changeset
26 var m: Mesh = Mesh(scene.root.components[0])
199
da68fe12f600 add: new test file to test to do future test with materials etc.
Sam <sam@basx.dev>
parents:
diff changeset
27
da68fe12f600 add: new test file to test to do future test with materials etc.
Sam <sam@basx.dev>
parents:
diff changeset
28 var engine = initEngine("Test materials")
da68fe12f600 add: new test file to test to do future test with materials etc.
Sam <sam@basx.dev>
parents:
diff changeset
29 const
da68fe12f600 add: new test file to test to do future test with materials etc.
Sam <sam@basx.dev>
parents:
diff changeset
30 vertexInput = @[
da68fe12f600 add: new test file to test to do future test with materials etc.
Sam <sam@basx.dev>
parents:
diff changeset
31 attr[Vec3f]("position", memoryPerformanceHint=PreferFastRead),
da68fe12f600 add: new test file to test to do future test with materials etc.
Sam <sam@basx.dev>
parents:
diff changeset
32 attr[Vec2f]("uv", memoryPerformanceHint=PreferFastRead),
da68fe12f600 add: new test file to test to do future test with materials etc.
Sam <sam@basx.dev>
parents:
diff changeset
33 ]
da68fe12f600 add: new test file to test to do future test with materials etc.
Sam <sam@basx.dev>
parents:
diff changeset
34 vertexOutput = @[attr[Vec2f]("uvout")]
201
ab626e67a1ee add: support for arrays of samplers
Sam <sam@basx.dev>
parents: 200
diff changeset
35 uniforms = @[attr[float32]("time")]
ab626e67a1ee add: support for arrays of samplers
Sam <sam@basx.dev>
parents: 200
diff changeset
36 samplers = @[attr[Sampler2DType]("my_texture", arrayCount=2)]
199
da68fe12f600 add: new test file to test to do future test with materials etc.
Sam <sam@basx.dev>
parents:
diff changeset
37 fragOutput = @[attr[Vec4f]("color")]
da68fe12f600 add: new test file to test to do future test with materials etc.
Sam <sam@basx.dev>
parents:
diff changeset
38 vertexCode = compileGlslShader(
da68fe12f600 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_VERTEX_BIT,
da68fe12f600 add: new test file to test to do future test with materials etc.
Sam <sam@basx.dev>
parents:
diff changeset
40 inputs=vertexInput,
201
ab626e67a1ee add: support for arrays of samplers
Sam <sam@basx.dev>
parents: 200
diff changeset
41 uniforms=uniforms,
199
da68fe12f600 add: new test file to test to do future test with materials etc.
Sam <sam@basx.dev>
parents:
diff changeset
42 samplers=samplers,
da68fe12f600 add: new test file to test to do future test with materials etc.
Sam <sam@basx.dev>
parents:
diff changeset
43 outputs=vertexOutput,
da68fe12f600 add: new test file to test to do future test with materials etc.
Sam <sam@basx.dev>
parents:
diff changeset
44 main="""gl_Position = vec4(position, 1.0); uvout = uv;"""
da68fe12f600 add: new test file to test to do future test with materials etc.
Sam <sam@basx.dev>
parents:
diff changeset
45 )
da68fe12f600 add: new test file to test to do future test with materials etc.
Sam <sam@basx.dev>
parents:
diff changeset
46 fragmentCode = compileGlslShader(
da68fe12f600 add: new test file to test to do future test with materials etc.
Sam <sam@basx.dev>
parents:
diff changeset
47 stage=VK_SHADER_STAGE_FRAGMENT_BIT,
da68fe12f600 add: new test file to test to do future test with materials etc.
Sam <sam@basx.dev>
parents:
diff changeset
48 inputs=vertexOutput,
201
ab626e67a1ee add: support for arrays of samplers
Sam <sam@basx.dev>
parents: 200
diff changeset
49 uniforms=uniforms,
199
da68fe12f600 add: new test file to test to do future test with materials etc.
Sam <sam@basx.dev>
parents:
diff changeset
50 samplers=samplers,
da68fe12f600 add: new test file to test to do future test with materials etc.
Sam <sam@basx.dev>
parents:
diff changeset
51 outputs=fragOutput,
201
ab626e67a1ee add: support for arrays of samplers
Sam <sam@basx.dev>
parents: 200
diff changeset
52 main="""
ab626e67a1ee add: support for arrays of samplers
Sam <sam@basx.dev>
parents: 200
diff changeset
53 float d = sin(Uniforms.time * 0.5) * 0.5 + 0.5;
ab626e67a1ee add: support for arrays of samplers
Sam <sam@basx.dev>
parents: 200
diff changeset
54 color = texture(my_texture[0], uvout) * (1 - d) + texture(my_texture[1], uvout) * d;
ab626e67a1ee add: support for arrays of samplers
Sam <sam@basx.dev>
parents: 200
diff changeset
55 """
199
da68fe12f600 add: new test file to test to do future test with materials etc.
Sam <sam@basx.dev>
parents:
diff changeset
56 )
da68fe12f600 add: new test file to test to do future test with materials etc.
Sam <sam@basx.dev>
parents:
diff changeset
57 engine.setRenderer(engine.gpuDevice.simpleForwardRenderPass(vertexCode, fragmentCode))
da68fe12f600 add: new test file to test to do future test with materials etc.
Sam <sam@basx.dev>
parents:
diff changeset
58 engine.addScene(scene, vertexInput)
201
ab626e67a1ee add: support for arrays of samplers
Sam <sam@basx.dev>
parents: 200
diff changeset
59 var t = cpuTime()
199
da68fe12f600 add: new test file to test to do future test with materials etc.
Sam <sam@basx.dev>
parents:
diff changeset
60 while engine.updateInputs() == Running and not engine.keyIsDown(Escape):
201
ab626e67a1ee add: support for arrays of samplers
Sam <sam@basx.dev>
parents: 200
diff changeset
61 setShaderGlobal(scene, "time", float32(cpuTime() - t))
199
da68fe12f600 add: new test file to test to do future test with materials etc.
Sam <sam@basx.dev>
parents:
diff changeset
62 engine.renderScene(scene)
da68fe12f600 add: new test file to test to do future test with materials etc.
Sam <sam@basx.dev>
parents:
diff changeset
63 engine.destroy()
da68fe12f600 add: new test file to test to do future test with materials etc.
Sam <sam@basx.dev>
parents:
diff changeset
64
da68fe12f600 add: new test file to test to do future test with materials etc.
Sam <sam@basx.dev>
parents:
diff changeset
65
da68fe12f600 add: new test file to test to do future test with materials etc.
Sam <sam@basx.dev>
parents:
diff changeset
66 when isMainModule:
da68fe12f600 add: new test file to test to do future test with materials etc.
Sam <sam@basx.dev>
parents:
diff changeset
67 main()