changeset 793:c31e42d72253

fix: material tests
author Sam <sam@basx.dev>
date Sun, 03 Sep 2023 17:46:40 +0700
parents d65b62812d34
children 59c54c4486c4
files src/semicongine/core/imagetypes.nim tests/test_materials.nim
diffstat 2 files changed, 43 insertions(+), 60 deletions(-) [+]
line wrap: on
line diff
--- a/src/semicongine/core/imagetypes.nim	Sun Sep 03 17:34:29 2023 +0700
+++ b/src/semicongine/core/imagetypes.nim	Sun Sep 03 17:46:40 2023 +0700
@@ -7,21 +7,16 @@
     height*: int
     imagedata*: seq[Pixel]
   Sampler* = object
-    magnification*: VkFilter
-    minification*: VkFilter
-    wrapModeS*: VkSamplerAddressMode
-    wrapModeT*: VkSamplerAddressMode
+    magnification*: VkFilter = VK_FILTER_LINEAR
+    minification*: VkFilter = VK_FILTER_LINEAR
+    wrapModeS*: VkSamplerAddressMode = VK_SAMPLER_ADDRESS_MODE_REPEAT
+    wrapModeT*: VkSamplerAddressMode = VK_SAMPLER_ADDRESS_MODE_REPEAT
 
   Image* = ref ImageObject
   Texture* = object
     name*: string
     image*: Image
-    sampler*: Sampler = Sampler(
-      magnification: VK_FILTER_LINEAR,
-      minification: VK_FILTER_LINEAR,
-      wrapModeS: VK_SAMPLER_ADDRESS_MODE_REPEAT,
-      wrapModeT: VK_SAMPLER_ADDRESS_MODE_REPEAT,
-    )
+    sampler*: Sampler
 
 proc `[]`*(image: Image, x, y: int): Pixel =
   assert x < image.width
--- a/tests/test_materials.nim	Sun Sep 03 17:34:29 2023 +0700
+++ b/tests/test_materials.nim	Sun Sep 03 17:46:40 2023 +0700
@@ -3,69 +3,57 @@
 
 import semicongine
 
+let
+  sampler = Sampler(magnification: VK_FILTER_NEAREST, minification: VK_FILTER_NEAREST)
+  (RT, WT, PT) = (hexToColorAlpha("A51931").asPixel, hexToColorAlpha("F4F5F8").asPixel, hexToColorAlpha("2D2A4A").asPixel)
+  thai = Image(width: 7, height: 5, imagedata: @[
+    RT, RT, RT, RT, RT, RT, RT,
+    WT, WT, WT, WT, WT, WT, WT,
+    PT, PT, PT, PT, PT, PT, PT,
+    WT, WT, WT, WT, WT, WT, WT,
+    RT, RT, RT, RT, RT, RT, RT,
+  ])
+  swiss = loadImage("flag.png")
+  material = Material(name: "material", textures: {
+    "tex1": Texture(image: thai, sampler: sampler),
+    "tex2": Texture(image: swiss, sampler: sampler),
+  }.toTable)
+
 proc main() =
   var flag = rect()
-  flag.materials = @["material2"]
-  var scene = newScene("main", root=newEntity("rect", {"mesh": Component(flag)}))
-  let (RT, WT, PT) = (hexToColorAlpha("A51931").asPixel, hexToColorAlpha("F4F5F8").asPixel, hexToColorAlpha("2D2A4A").asPixel)
-  let
-    # image from memory
-    thai = Image(width: 7, height: 5, imagedata: @[
-      RT, RT, RT, RT, RT, RT, RT,
-      WT, WT, WT, WT, WT, WT, WT,
-      PT, PT, PT, PT, PT, PT, PT,
-      WT, WT, WT, WT, WT, WT, WT,
-      RT, RT, RT, RT, RT, RT, RT,
-    ])
-  let
-    # image from file
-    swiss = loadImage("flag.png")
-
-  var sampler = DefaultSampler()
-  sampler.magnification = VK_FILTER_NEAREST
-  sampler.minification = VK_FILTER_NEAREST
-  scene.addMaterial(Material(name:"material1", textures: {
-    "tex1": Texture(image: swiss, sampler: sampler),
-    "tex2": Texture(image: thai, sampler: sampler),
-  }.toTable))
-  scene.addMaterial(Material(name:"material2", textures: {
-    "tex1": Texture(image: thai, sampler: sampler),
-    "tex2": Texture(image: swiss, sampler: sampler),
-  }.toTable))
+  flag.material = material
+  var scene = Scene(name: "main", meshes: @[flag])
   scene.addShaderGlobalArray("test2", @[0'f32, 0'f32])
 
   var engine = initEngine("Test materials")
 
   const
-    vertexInput = @[
-      attr[Vec3f]("position", memoryPerformanceHint=PreferFastRead),
-      attr[uint16]("materialIndex", memoryPerformanceHint=PreferFastRead, perInstance=true),
-      attr[Vec2f]("uv", memoryPerformanceHint=PreferFastRead),
-    ]
-    vertexOutput = @[attr[Vec2f]("uvout"), attr[uint16]("materialIndexOut", noInterpolation=true)]
-    uniforms = @[attr[float32]("test2", arrayCount=2)]
-    samplers = @[
-      attr[Sampler2DType]("tex1", arrayCount=2),
-      attr[Sampler2DType]("tex2", arrayCount=2),
-    ]
-    fragOutput = @[attr[Vec4f]("color")]
-    (vertexCode, fragmentCode) = compileVertexFragmentShaderSet(
-      inputs=vertexInput,
-      intermediate=vertexOutput,
-      outputs=fragOutput,
-      samplers=samplers,
-      uniforms=uniforms,
+    shaderConfiguration1 = createShaderConfiguration(
+      inputs=[
+        attr[Vec3f]("position", memoryPerformanceHint=PreferFastRead),
+        attr[Vec2f]("uv", memoryPerformanceHint=PreferFastRead),
+      ],
+      intermediates=[
+        attr[Vec2f]("uvout"),
+      ],
+      uniforms=[attr[float32]("test2", arrayCount=2)],
+      samplers = @[
+        attr[Sampler2DType]("tex1", arrayCount=2),
+        attr[Sampler2DType]("tex2", arrayCount=2),
+      ],
+      outputs=[attr[Vec4f]("color")],
       vertexCode="""
       gl_Position = vec4(position.x, position.y + sin(Uniforms.test2[1]) / Uniforms.test2[1] * 0.5, position.z, 1.0);
-      uvout = uv;
-      materialIndexOut = materialIndex;""",
+      uvout = uv;""",
       fragmentCode="""
       float d = sin(Uniforms.test2[0]) * 0.5 + 0.5;
-      color = texture(tex1[materialIndexOut], uvout) * (1 - d) + texture(tex2[materialIndexOut], uvout) * d;
+      color = texture(tex1[0], uvout) * (1 - d) + texture(tex2[0], uvout) * d;
       """,
     )
-  engine.setRenderer(engine.gpuDevice.simpleForwardRenderPass(vertexCode, fragmentCode))
-  engine.addScene(scene, vertexInput, samplers, transformAttribute="")
+  engine.initRenderer({
+    "material": shaderConfiguration1,
+  }.toTable)
+  engine.addScene(scene)
   var t = cpuTime()
   while engine.updateInputs() == Running and not engine.keyIsDown(Escape):
     var d = float32(cpuTime() - t)