changeset 1107:6a83e6540e36

did: add name for material types and fix shader-materialtype-compatability check
author sam <sam@basx.dev>
date Sat, 13 Apr 2024 11:09:02 +0700
parents ed3896a10762
children 2e7b9807f0e1
files semicongine/material.nim semicongine/mesh.nim semicongine/panel.nim semicongine/text.nim semicongine/vulkan/renderpass.nim semicongine/vulkan/shader.nim
diffstat 6 files changed, 28 insertions(+), 9 deletions(-) [+]
line wrap: on
line diff
--- a/semicongine/material.nim	Sat Apr 13 00:22:45 2024 +0700
+++ b/semicongine/material.nim	Sat Apr 13 11:09:02 2024 +0700
@@ -50,10 +50,14 @@
   material.dirtyAttributes.reset
 
 proc `$`*(materialType: MaterialType): string =
-  var attributes: seq[string]
-  for key, value in materialType.attributes.pairs:
-    attributes.add &"{key}: {value}"
-  return &"""MaterialType '{materialType.name}' | Attributes: [{attributes.join(", ")}]"""
+  return materialType.name
+
+proc assertCanRender*(shader: ShaderConfiguration, materialType: MaterialType) =
+  for attr in shader.inputs:
+    if attr.perInstance:
+      assert materialType.instanceAttributes.contains(attr.name), &"MaterialType '{materialType}' requires instance attribute {attr.name} in order to be renderable with the assigned shader '{shader}'"
+    else:
+      assert materialType.vertexAttributes.contains(attr.name), &"MaterialType '{materialType}' requires vertex attribute {attr.name} in order to be renderable with the assigned shader '{shader}'"
 
 proc `$`*(material: MaterialData): string =
   var attributes: seq[string]
@@ -129,6 +133,7 @@
     attributes: {"baseTexture": TextureType, "color": Vec4F32}.toTable
   )
   EMPTY_SHADER* = createShaderConfiguration(
+    name = "empty shader",
     inputs = [
       attr[Mat4](TRANSFORM_ATTRIB, memoryPerformanceHint = PreferFastWrite, perInstance = true),
       attr[Vec3f]("position", memoryPerformanceHint = PreferFastRead),
--- a/semicongine/mesh.nim	Sat Apr 13 00:22:45 2024 +0700
+++ b/semicongine/mesh.nim	Sat Apr 13 11:09:02 2024 +0700
@@ -46,11 +46,14 @@
 func `material=`*(mesh: var MeshObject, material: MaterialData) =
   for name, theType in material.theType.vertexAttributes:
     if mesh.vertexData.contains(name):
-      assert mesh.vertexData[name].theType == theType, &"{material.theType} expected mesh attribute '{name}' to be '{theType}' but it is {mesh.vertexData[name].theType}"
-    elif mesh.instanceData.contains(name):
-      assert mesh.instanceData[name].theType == theType, &"{material.theType} expected mesh attribute '{name}' to be '{theType}' but it is {mesh.instanceData[name].theType}"
+      assert mesh.vertexData[name].theType == theType, &"{material.theType} expected vertex attribute '{name}' to be '{theType}' but it is {mesh.vertexData[name].theType}"
     else:
-      assert false, &"Mesh '{mesh.name}' is missing required mesh attribute '{name}: {theType}' for {material.theType}"
+      assert false, &"Mesh '{mesh.name}' is missing required vertex attribute '{name}: {theType}' for {material.theType}"
+  for name, theType in material.theType.instanceAttributes:
+    if mesh.instanceData.contains(name):
+      assert mesh.instanceData[name].theType == theType, &"{material.theType} expected instance attribute '{name}' to be '{theType}' but it is {mesh.instanceData[name].theType}"
+    else:
+      assert false, &"Mesh '{mesh.name}' is missing required instance attribute '{name}: {theType}' for {material.theType}"
   mesh.material = material
 
 func instanceCount*(mesh: MeshObject): int =
--- a/semicongine/panel.nim	Sat Apr 13 00:22:45 2024 +0700
+++ b/semicongine/panel.nim	Sat Apr 13 11:09:02 2024 +0700
@@ -19,6 +19,7 @@
     attributes: {"panelTexture": TextureType, "color": Vec4F32}.toTable,
   )
   PANEL_SHADER* = createShaderConfiguration(
+    name = "panel shader",
     inputs = [
       attr[Mat4](TRANSFORM_ATTRIB, memoryPerformanceHint = PreferFastWrite, perInstance = true),
       attr[Vec3f](POSITION_ATTRIB, memoryPerformanceHint = PreferFastWrite),
--- a/semicongine/text.nim	Sat Apr 13 00:22:45 2024 +0700
+++ b/semicongine/text.nim	Sat Apr 13 11:09:02 2024 +0700
@@ -23,6 +23,7 @@
     attributes: {"fontAtlas": TextureType, "color": Vec4F32}.toTable,
   )
   TEXT_SHADER* = createShaderConfiguration(
+    name = "font shader",
     inputs = [
       attr[Mat4](TRANSFORM_ATTRIB, memoryPerformanceHint = PreferFastWrite, perInstance = true),
       attr[Vec3f](POSITION_ATTRIB, memoryPerformanceHint = PreferFastWrite),
--- a/semicongine/vulkan/renderpass.nim	Sat Apr 13 00:22:45 2024 +0700
+++ b/semicongine/vulkan/renderpass.nim	Sat Apr 13 11:09:02 2024 +0700
@@ -21,6 +21,11 @@
   inFlightFrames = 2,
 ): RenderPass =
   assert device.vk.valid
+
+  # some asserts
+  for (materialtype, shaderconfig) in shaders:
+    shaderconfig.assertCanRender(materialtype)
+
   var
     attachments = @[VkAttachmentDescription(
         format: device.physicalDevice.getSurfaceFormats().filterSurfaceFormat().format,
--- a/semicongine/vulkan/shader.nim	Sat Apr 13 00:22:45 2024 +0700
+++ b/semicongine/vulkan/shader.nim	Sat Apr 13 11:09:02 2024 +0700
@@ -22,6 +22,7 @@
     stage*: VkShaderStageFlagBits
     configuration*: ShaderConfiguration
   ShaderConfiguration* = object
+    name*: string
     vertexBinary: seq[uint32]
     fragmentBinary: seq[uint32]
     entrypoint: string
@@ -32,7 +33,8 @@
     samplers*: seq[ShaderAttribute]
 
 proc `$`*(shader: ShaderConfiguration): string =
-  &"Inputs: {shader.inputs}, Uniforms: {shader.uniforms}, Samplers: {shader.samplers}"
+  shader.name
+  # &"Inputs: {shader.inputs}, Uniforms: {shader.uniforms}, Samplers: {shader.samplers}"
 
 proc compileGlslToSPIRV(stage: VkShaderStageFlagBits, shaderSource: string, entrypoint: string): seq[uint32] {.compileTime.} =
   func stage2string(stage: VkShaderStageFlagBits): string {.compileTime.} =
@@ -107,6 +109,7 @@
   compileGlslToSPIRV(stage, code.join("\n"), entrypoint)
 
 proc createShaderConfiguration*(
+  name: string,
   inputs: openArray[ShaderAttribute] = [],
   intermediates: openArray[ShaderAttribute] = [],
   outputs: openArray[ShaderAttribute] = [],
@@ -118,6 +121,7 @@
   fragmentCode: string,
 ): ShaderConfiguration {.compileTime.} =
   ShaderConfiguration(
+    name: name,
     vertexBinary: compileGlslCode(
       stage = VK_SHADER_STAGE_VERTEX_BIT,
       inputs = inputs,