# HG changeset patch # User Sam # Date 1692462625 -25200 # Node ID 1880ab140165c578aea2905b4153baa9046c138e # Parent fa39e67dded79557d450d7ca8e078652278e13db add: first complete working version of multiple materials and shaders per scene, yipie :) diff -r fa39e67dded7 -r 1880ab140165 src/semicongine/mesh.nim --- a/src/semicongine/mesh.nim Sat Aug 19 22:24:06 2023 +0700 +++ b/src/semicongine/mesh.nim Sat Aug 19 23:30:25 2023 +0700 @@ -236,8 +236,10 @@ proc updateAttributeData*[T: GPUType|int|uint|float](mesh: Mesh, attribute: string, data: seq[T]) = if mesh.vertexData.contains(attribute): + assert data.len < mesh.vertexData[attribute].len setValues(mesh.vertexData[attribute], data) elif mesh.instanceData.contains(attribute): + assert data.len < mesh.instanceData[attribute].len setValues(mesh.instanceData[attribute], data) else: raise newException(Exception, &"Attribute {attribute} is not defined for mesh {mesh}") @@ -245,8 +247,10 @@ proc updateAttributeData*[T: GPUType|int|uint|float](mesh: Mesh, attribute: string, i: uint32, value: T) = if mesh.vertexData.contains(attribute): + assert i < mesh.vertexData[attribute].len setValue(mesh.vertexData[attribute], i, value) elif mesh.instanceData.contains(attribute): + assert i < mesh.instanceData[attribute].len setValue(mesh.instanceData[attribute], i, value) else: raise newException(Exception, &"Attribute {attribute} is not defined for mesh {mesh}") diff -r fa39e67dded7 -r 1880ab140165 tests/test_vulkan_wrapper.nim --- a/tests/test_vulkan_wrapper.nim Sat Aug 19 22:24:06 2023 +0700 +++ b/tests/test_vulkan_wrapper.nim Sat Aug 19 23:30:25 2023 +0700 @@ -41,7 +41,7 @@ name: "mat3", materialType: "my_special_material", constants: { - "colors": toGPUValue(newVec4f(0.5, 0.5, 0)) + "color": toGPUValue(newVec4f(0.5, 0.5, 0)) }.toTable ) @@ -155,7 +155,7 @@ # INIT RENDERER: const - shaderConfiguration = createShaderConfiguration( + shaderConfiguration1 = createShaderConfiguration( inputs=[ attr[Vec3f]("position", memoryPerformanceHint=PreferFastRead), attr[Vec4f]("color", memoryPerformanceHint=PreferFastWrite), @@ -174,9 +174,20 @@ vertexCode="""gl_Position = vec4(position + translate, 1.0); outcolor = color; materialIndexOut = materialIndex;""", fragmentCode="color = texture(my_little_texture[materialIndexOut], outcolor.xy) * 0.5 + outcolor * 0.5;", ) + shaderConfiguration2 = createShaderConfiguration( + inputs=[ + attr[Vec3f]("position", memoryPerformanceHint=PreferFastRead), + attr[Vec3f]("translate", perInstance=true), + ], + intermediates=[attr[Vec4f]("outcolor")], + outputs=[attr[Vec4f]("color")], + uniforms=[attr[Vec4f]("color")], + vertexCode="""gl_Position = vec4(position + translate, 1.0); outcolor = Uniforms.color;""", + fragmentCode="color = outcolor;", + ) engine.initRenderer({ - "my_material": shaderConfiguration, - "my_special_material": shaderConfiguration, + "my_material": shaderConfiguration1, + "my_special_material": shaderConfiguration2, }.toTable) # INIT SCENES