annotate tests/test_materials.nim @ 243:3b388986c7fd

did: refactor texture data structures, add more complete (untested) material import
author Sam <sam@basx.dev>
date Mon, 22 May 2023 00:50:41 +0700
parents 3918e42bf26e
children f52fccedf5ab
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()))
200
1ba005328615 add: few improvments for working with textures
Sam <sam@basx.dev>
parents: 199
diff changeset
7 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
8 let
234
3918e42bf26e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents: 230
diff changeset
9 t1 = Image(width: 7, height: 5, imagedata: @[
201
ab626e67a1ee add: support for arrays of samplers
Sam <sam@basx.dev>
parents: 200
diff changeset
10 RT, RT, RT, RT, RT, RT, RT,
ab626e67a1ee add: support for arrays of samplers
Sam <sam@basx.dev>
parents: 200
diff changeset
11 WT, WT, WT, WT, WT, WT, WT,
ab626e67a1ee add: support for arrays of samplers
Sam <sam@basx.dev>
parents: 200
diff changeset
12 PT, PT, PT, PT, PT, PT, PT,
ab626e67a1ee add: support for arrays of samplers
Sam <sam@basx.dev>
parents: 200
diff changeset
13 WT, WT, WT, WT, WT, WT, WT,
ab626e67a1ee add: support for arrays of samplers
Sam <sam@basx.dev>
parents: 200
diff changeset
14 RT, RT, RT, RT, RT, RT, RT,
ab626e67a1ee add: support for arrays of samplers
Sam <sam@basx.dev>
parents: 200
diff changeset
15 ])
234
3918e42bf26e add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents: 230
diff changeset
16 t2 = loadImage("flag.png")
211
744285b47a4d did: refactor image handling
Sam <sam@basx.dev>
parents: 201
diff changeset
17 scene.addTextures("my_texture", @[t1, t2], interpolation=VK_FILTER_NEAREST)
230
027f6ff06585 add: test mesh
Sam <sam@basx.dev>
parents: 211
diff changeset
18 scene.addShaderGlobalArray("test2", @[0'f32, 0'f32])
199
da68fe12f600 add: new test file to test to do future test with materials etc.
Sam <sam@basx.dev>
parents:
diff changeset
19
da68fe12f600 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")
da68fe12f600 add: new test file to test to do future test with materials etc.
Sam <sam@basx.dev>
parents:
diff changeset
21 const
da68fe12f600 add: new test file to test to do future test with materials etc.
Sam <sam@basx.dev>
parents:
diff changeset
22 vertexInput = @[
da68fe12f600 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),
da68fe12f600 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),
da68fe12f600 add: new test file to test to do future test with materials etc.
Sam <sam@basx.dev>
parents:
diff changeset
25 ]
da68fe12f600 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")]
230
027f6ff06585 add: test mesh
Sam <sam@basx.dev>
parents: 211
diff changeset
27 uniforms = @[attr[float32]("test2", arrayCount=2)]
201
ab626e67a1ee add: support for arrays of samplers
Sam <sam@basx.dev>
parents: 200
diff changeset
28 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
29 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
30 vertexCode = compileGlslShader(
da68fe12f600 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,
da68fe12f600 add: new test file to test to do future test with materials etc.
Sam <sam@basx.dev>
parents:
diff changeset
32 inputs=vertexInput,
201
ab626e67a1ee add: support for arrays of samplers
Sam <sam@basx.dev>
parents: 200
diff changeset
33 uniforms=uniforms,
199
da68fe12f600 add: new test file to test to do future test with materials etc.
Sam <sam@basx.dev>
parents:
diff changeset
34 samplers=samplers,
da68fe12f600 add: new test file to test to do future test with materials etc.
Sam <sam@basx.dev>
parents:
diff changeset
35 outputs=vertexOutput,
230
027f6ff06585 add: test mesh
Sam <sam@basx.dev>
parents: 211
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;"""
199
da68fe12f600 add: new test file to test to do future test with materials etc.
Sam <sam@basx.dev>
parents:
diff changeset
37 )
da68fe12f600 add: new test file to test to do future test with materials etc.
Sam <sam@basx.dev>
parents:
diff changeset
38 fragmentCode = 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_FRAGMENT_BIT,
da68fe12f600 add: new test file to test to do future test with materials etc.
Sam <sam@basx.dev>
parents:
diff changeset
40 inputs=vertexOutput,
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=fragOutput,
201
ab626e67a1ee add: support for arrays of samplers
Sam <sam@basx.dev>
parents: 200
diff changeset
44 main="""
230
027f6ff06585 add: test mesh
Sam <sam@basx.dev>
parents: 211
diff changeset
45 float d = sin(Uniforms.test2[0]) * 0.5 + 0.5;
201
ab626e67a1ee add: support for arrays of samplers
Sam <sam@basx.dev>
parents: 200
diff changeset
46 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
47 """
199
da68fe12f600 add: new test file to test to do future test with materials etc.
Sam <sam@basx.dev>
parents:
diff changeset
48 )
da68fe12f600 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))
da68fe12f600 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)
201
ab626e67a1ee add: support for arrays of samplers
Sam <sam@basx.dev>
parents: 200
diff changeset
51 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
52 while engine.updateInputs() == Running and not engine.keyIsDown(Escape):
230
027f6ff06585 add: test mesh
Sam <sam@basx.dev>
parents: 211
diff changeset
53 var d = float32(cpuTime() - t)
027f6ff06585 add: test mesh
Sam <sam@basx.dev>
parents: 211
diff changeset
54 setShaderGlobalArray(scene, "test2", @[d, d * 2])
199
da68fe12f600 add: new test file to test to do future test with materials etc.
Sam <sam@basx.dev>
parents:
diff changeset
55 engine.renderScene(scene)
da68fe12f600 add: new test file to test to do future test with materials etc.
Sam <sam@basx.dev>
parents:
diff changeset
56 engine.destroy()
da68fe12f600 add: new test file to test to do future test with materials etc.
Sam <sam@basx.dev>
parents:
diff changeset
57
da68fe12f600 add: new test file to test to do future test with materials etc.
Sam <sam@basx.dev>
parents:
diff changeset
58
da68fe12f600 add: new test file to test to do future test with materials etc.
Sam <sam@basx.dev>
parents:
diff changeset
59 when isMainModule:
da68fe12f600 add: new test file to test to do future test with materials etc.
Sam <sam@basx.dev>
parents:
diff changeset
60 main()