From ff8bec00f6639444a5bc6288f63cd747b10a7c9d Mon Sep 17 00:00:00 2001 From: emil28092005 Date: Thu, 18 Jun 2026 19:35:44 +0300 Subject: [PATCH] fix: use MemoryCopy instead of Marshal.StructureToPtr for UBO packing - Marshal.StructureToPtr may not handle Matrix4x4 correctly (record struct) - Replaced with MemoryCopy from local copies of vp, lightPos, lightColor - Removed per-frame light debug logging - Simple diffuse shader still active for testing --- src/Engine.Graphics.Vulkan/VulkanRenderer.cs | 24 ++++++++++++-------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/src/Engine.Graphics.Vulkan/VulkanRenderer.cs b/src/Engine.Graphics.Vulkan/VulkanRenderer.cs index 12edcf1..64db632 100644 --- a/src/Engine.Graphics.Vulkan/VulkanRenderer.cs +++ b/src/Engine.Graphics.Vulkan/VulkanRenderer.cs @@ -86,7 +86,6 @@ internal sealed unsafe class VulkanRenderer : IRenderer, Engine.Graphics.IScreen { lightPos = new Vector4(lt.Position, l.Intensity); lightColor = new Vector4(l.Color, l.Range); - Console.WriteLine($"[Vulkan] Point light: pos={lt.Position}, intensity={l.Intensity}, range={l.Range}, color={l.Color}"); } }); if (lightPos.W == 0) @@ -97,20 +96,27 @@ internal sealed unsafe class VulkanRenderer : IRenderer, Engine.Graphics.IScreen { lightPos = new Vector4(l.Position, l.Intensity); lightColor = new Vector4(l.Color, l.Range); - Console.WriteLine($"[Vulkan] Point light (fallback): pos={l.Position}, intensity={l.Intensity}"); } }); } - if (lightPos.W == 0) - { - Console.WriteLine("[Vulkan] WARNING: No point light found in ECS!"); - } // Pack UBO: mat4 vp (64 bytes) + vec4 lightPos (16) + vec4 lightColor (16) = 96 bytes var uboData = stackalloc byte[128]; - Marshal.StructureToPtr(vp, (nint)uboData, false); - *(Vector4*)(uboData + 64) = lightPos; - *(Vector4*)(uboData + 80) = lightColor; + { + var vpCopy = vp; + var src = &vpCopy; + System.Buffer.MemoryCopy(src, uboData, 64, 64); + } + { + var lpCopy = lightPos; + var src = &lpCopy; + System.Buffer.MemoryCopy(src, uboData + 64, 16, 16); + } + { + var lcCopy = lightColor; + var src = &lcCopy; + System.Buffer.MemoryCopy(src, uboData + 80, 16, 16); + } var drawCalls = new List<(VkBuffer vertexBuf, VkBuffer indexBuf, uint indexCount, Matrix4x4 model)>();