# HG changeset patch # User sam # Date 1712981342 -25200 # Node ID 6a83e6540e362bf270261ffc3b2646966cf2928d # Parent ed3896a10762bfbfc847dd6355a2350f5ce0ed21 did: add name for material types and fix shader-materialtype-compatability check diff -r ed3896a10762 -r 6a83e6540e36 semicongine/material.nim --- 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), diff -r ed3896a10762 -r 6a83e6540e36 semicongine/mesh.nim --- 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 = diff -r ed3896a10762 -r 6a83e6540e36 semicongine/panel.nim --- 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), diff -r ed3896a10762 -r 6a83e6540e36 semicongine/text.nim --- 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), diff -r ed3896a10762 -r 6a83e6540e36 semicongine/vulkan/renderpass.nim --- 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, diff -r ed3896a10762 -r 6a83e6540e36 semicongine/vulkan/shader.nim --- 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,