comparison tests/test_materials.nim @ 201:ab626e67a1ee

add: support for arrays of samplers
author Sam <sam@basx.dev>
date Mon, 08 May 2023 00:38:05 +0700
parents 1ba005328615
children 744285b47a4d
comparison
equal deleted inserted replaced
200:1ba005328615 201:ab626e67a1ee
1 import std/times
2
1 import semicongine 3 import semicongine
2 4
3 proc main() = 5 proc main() =
4 var scene = newScene("main", root=newEntity("rect", rect())) 6 var scene = newScene("main", root=newEntity("rect", rect()))
5 let (R, W) = ([255'u8, 0'u8, 0'u8, 255'u8], [255'u8, 255'u8, 255'u8, 255'u8]) 7 let (R, W) = ([255'u8, 0'u8, 0'u8, 255'u8], [255'u8, 255'u8, 255'u8, 255'u8])
6 let (RT, WT, PT) = (hexToColorAlpha("A51931").asPixel, hexToColorAlpha("F4F5F8").asPixel, hexToColorAlpha("2D2A4A").asPixel) 8 let (RT, WT, PT) = (hexToColorAlpha("A51931").asPixel, hexToColorAlpha("F4F5F8").asPixel, hexToColorAlpha("2D2A4A").asPixel)
7 scene.addTexture("my_texture", TextureImage(width: 13, height: 5, imagedata: @[ 9 let
8 R, R, R, R, R, W, RT, RT, RT, RT, RT, RT, RT, 10 t1 = TextureImage(width: 5, height: 5, imagedata: @[
9 R, R, W, R, R, W, WT, WT, WT, WT, WT, WT, WT, 11 R, R, R, R, R,
10 R, W, W, W, R, W, PT, PT, PT, PT, PT, PT, PT, 12 R, R, W, R, R,
11 R, R, W, R, R, W, WT, WT, WT, WT, WT, WT, WT, 13 R, W, W, W, R,
12 R, R, R, R, R, W, RT, RT, RT, RT, RT, RT, RT, 14 R, R, W, R, R,
13 ])) 15 R, R, R, R, R,
16 ])
17 t2 = TextureImage(width: 7, height: 5, imagedata: @[
18 RT, RT, RT, RT, RT, RT, RT,
19 WT, WT, WT, WT, WT, WT, WT,
20 PT, PT, PT, PT, PT, PT, PT,
21 WT, WT, WT, WT, WT, WT, WT,
22 RT, RT, RT, RT, RT, RT, RT,
23 ])
24 scene.addTextures("my_texture", @[t1, t2])
25 scene.addShaderGlobal("time", 0'f32)
14 var m: Mesh = Mesh(scene.root.components[0]) 26 var m: Mesh = Mesh(scene.root.components[0])
15 27
16 var engine = initEngine("Test materials") 28 var engine = initEngine("Test materials")
17 const 29 const
18 vertexInput = @[ 30 vertexInput = @[
19 attr[Vec3f]("position", memoryPerformanceHint=PreferFastRead), 31 attr[Vec3f]("position", memoryPerformanceHint=PreferFastRead),
20 attr[Vec2f]("uv", memoryPerformanceHint=PreferFastRead), 32 attr[Vec2f]("uv", memoryPerformanceHint=PreferFastRead),
21 ] 33 ]
22 vertexOutput = @[attr[Vec2f]("uvout")] 34 vertexOutput = @[attr[Vec2f]("uvout")]
23 samplers = @[attr[Sampler2DType]("my_texture")] 35 uniforms = @[attr[float32]("time")]
36 samplers = @[attr[Sampler2DType]("my_texture", arrayCount=2)]
24 fragOutput = @[attr[Vec4f]("color")] 37 fragOutput = @[attr[Vec4f]("color")]
25 vertexCode = compileGlslShader( 38 vertexCode = compileGlslShader(
26 stage=VK_SHADER_STAGE_VERTEX_BIT, 39 stage=VK_SHADER_STAGE_VERTEX_BIT,
27 inputs=vertexInput, 40 inputs=vertexInput,
41 uniforms=uniforms,
28 samplers=samplers, 42 samplers=samplers,
29 outputs=vertexOutput, 43 outputs=vertexOutput,
30 main="""gl_Position = vec4(position, 1.0); uvout = uv;""" 44 main="""gl_Position = vec4(position, 1.0); uvout = uv;"""
31 ) 45 )
32 fragmentCode = compileGlslShader( 46 fragmentCode = compileGlslShader(
33 stage=VK_SHADER_STAGE_FRAGMENT_BIT, 47 stage=VK_SHADER_STAGE_FRAGMENT_BIT,
34 inputs=vertexOutput, 48 inputs=vertexOutput,
49 uniforms=uniforms,
35 samplers=samplers, 50 samplers=samplers,
36 outputs=fragOutput, 51 outputs=fragOutput,
37 main="color = texture(my_texture, uvout);" 52 main="""
53 float d = sin(Uniforms.time * 0.5) * 0.5 + 0.5;
54 color = texture(my_texture[0], uvout) * (1 - d) + texture(my_texture[1], uvout) * d;
55 """
38 ) 56 )
39 engine.setRenderer(engine.gpuDevice.simpleForwardRenderPass(vertexCode, fragmentCode)) 57 engine.setRenderer(engine.gpuDevice.simpleForwardRenderPass(vertexCode, fragmentCode))
40 engine.addScene(scene, vertexInput) 58 engine.addScene(scene, vertexInput)
59 var t = cpuTime()
41 while engine.updateInputs() == Running and not engine.keyIsDown(Escape): 60 while engine.updateInputs() == Running and not engine.keyIsDown(Escape):
61 setShaderGlobal(scene, "time", float32(cpuTime() - t))
42 engine.renderScene(scene) 62 engine.renderScene(scene)
43 engine.destroy() 63 engine.destroy()
44 64
45 65
46 when isMainModule: 66 when isMainModule: