diff --git a/src/Engine.Graphics.Vulkan/Shaders/triangle.frag b/src/Engine.Graphics.Vulkan/Shaders/triangle.frag index b0205a1..7122ce8 100644 --- a/src/Engine.Graphics.Vulkan/Shaders/triangle.frag +++ b/src/Engine.Graphics.Vulkan/Shaders/triangle.frag @@ -1,11 +1,8 @@ #version 450 layout(location = 0) in vec3 fragColor; -layout(location = 1) in vec3 fragNormal; layout(location = 0) out vec4 outColor; void main() { - vec3 lightDir = normalize(vec3(0.5, 0.8, 0.3)); - float diff = max(dot(normalize(fragNormal), lightDir), 0.2); - outColor = vec4(fragColor * diff, 1.0); + outColor = vec4(fragColor, 1.0); } diff --git a/src/Engine.Graphics.Vulkan/Shaders/triangle.frag.spv b/src/Engine.Graphics.Vulkan/Shaders/triangle.frag.spv index 4a5184b..828f1e5 100644 Binary files a/src/Engine.Graphics.Vulkan/Shaders/triangle.frag.spv and b/src/Engine.Graphics.Vulkan/Shaders/triangle.frag.spv differ diff --git a/src/Engine.Graphics.Vulkan/Shaders/triangle.vert b/src/Engine.Graphics.Vulkan/Shaders/triangle.vert index 56154c8..7519551 100644 --- a/src/Engine.Graphics.Vulkan/Shaders/triangle.vert +++ b/src/Engine.Graphics.Vulkan/Shaders/triangle.vert @@ -5,19 +5,23 @@ layout(location = 1) in vec3 inColor; layout(location = 2) in vec3 inNormal; layout(location = 0) out vec3 fragColor; -layout(location = 1) out vec3 fragNormal; layout(row_major, set = 0, binding = 0) uniform CameraUBO { mat4 vp; }; -layout(row_major, push_constant) uniform PC { - mat4 model; +layout(push_constant) uniform PC { + float angle; } pc; void main() { - gl_Position = vp * pc.model * vec4(inPosition, 1.0); - gl_Position.z = (gl_Position.z + gl_Position.w) * 0.5; + float c = cos(pc.angle); + float s = sin(pc.angle); + vec3 rotated = vec3( + inPosition.x * c - inPosition.y * s, + inPosition.x * s + inPosition.y * c, + inPosition.z + ); + gl_Position = vp * vec4(rotated, 1.0); fragColor = inColor; - fragNormal = mat3(pc.model) * inNormal; } diff --git a/src/Engine.Graphics.Vulkan/Shaders/triangle.vert.spv b/src/Engine.Graphics.Vulkan/Shaders/triangle.vert.spv index 2c16878..c1afffd 100644 Binary files a/src/Engine.Graphics.Vulkan/Shaders/triangle.vert.spv and b/src/Engine.Graphics.Vulkan/Shaders/triangle.vert.spv differ diff --git a/src/Engine.Graphics.Vulkan/VulkanPipeline.cs b/src/Engine.Graphics.Vulkan/VulkanPipeline.cs index 45bf2ba..6cfadfd 100644 --- a/src/Engine.Graphics.Vulkan/VulkanPipeline.cs +++ b/src/Engine.Graphics.Vulkan/VulkanPipeline.cs @@ -14,7 +14,7 @@ internal sealed unsafe class VulkanPipeline : IDisposable private readonly VkDevice _device; private bool _disposed; - public VulkanPipeline(VkDevice device, VkFormat colorFormat, VkFormat depthFormat, byte[] vertSpv, byte[] fragSpv) + public VulkanPipeline(VkDevice device, VkFormat colorFormat, byte[] vertSpv, byte[] fragSpv) { _device = device; VertModule = CreateShaderModule(vertSpv); @@ -115,16 +115,6 @@ internal sealed unsafe class VulkanPipeline : IDisposable sampleShadingEnable = VkBool32.False, }; - var depthStencilState = new VkPipelineDepthStencilStateCreateInfo - { - sType = VkStructureType.PipelineDepthStencilStateCreateInfo, - depthTestEnable = VkBool32.False, - depthWriteEnable = VkBool32.False, - depthCompareOp = VkCompareOp.Less, - depthBoundsTestEnable = VkBool32.False, - stencilTestEnable = VkBool32.False, - }; - var blendAttachment = new VkPipelineColorBlendAttachmentState { blendEnable = VkBool32.False, @@ -154,7 +144,7 @@ internal sealed unsafe class VulkanPipeline : IDisposable { stageFlags = VkShaderStageFlags.Vertex, offset = 0, - size = 64, + size = 4, }; var descLayout = DescriptorSetLayout; @@ -179,7 +169,6 @@ internal sealed unsafe class VulkanPipeline : IDisposable sType = VkStructureType.PipelineRenderingCreateInfo, colorAttachmentCount = 1, pColorAttachmentFormats = &colorFormat, - depthAttachmentFormat = depthFormat, }; var pipelineInfo = new VkGraphicsPipelineCreateInfo @@ -193,7 +182,6 @@ internal sealed unsafe class VulkanPipeline : IDisposable pViewportState = &viewportState, pRasterizationState = &rasterizationState, pMultisampleState = &multisampleState, - pDepthStencilState = &depthStencilState, pColorBlendState = &colorBlendState, pDynamicState = &dynamicState, layout = PipelineLayout, diff --git a/src/Engine.Graphics.Vulkan/VulkanRenderer.cs b/src/Engine.Graphics.Vulkan/VulkanRenderer.cs index 8b82534..6854f1c 100644 --- a/src/Engine.Graphics.Vulkan/VulkanRenderer.cs +++ b/src/Engine.Graphics.Vulkan/VulkanRenderer.cs @@ -34,7 +34,7 @@ internal sealed unsafe class VulkanRenderer : IRenderer, Engine.Graphics.IScreen var vertSpv = LoadShader("Shaders/triangle.vert.spv"); var fragSpv = LoadShader("Shaders/triangle.frag.spv"); - _pipeline = new VulkanPipeline(ctx.Device, swapchain.Format, swapchain.DepthFormat, vertSpv, fragSpv); + _pipeline = new VulkanPipeline(ctx.Device, swapchain.Format, vertSpv, fragSpv); _frameResources = new VulkanFrameResources(ctx.Device, ctx.GraphicsQueueFamilyIndex, swapchain.ImageCount, ctx, _pipeline.DescriptorSetLayout); @@ -115,11 +115,6 @@ internal sealed unsafe class VulkanRenderer : IRenderer, Engine.Graphics.IScreen 0, 0, 0x400, 0x100); - TransitionImageLayoutDepth(cmd, _swapchain.DepthImage, - VkImageLayout.Undefined, VkImageLayout.DepthStencilAttachmentOptimal, - 0, 0, - 0x100, 0x200); - var clearValue = new VkClearValue { Color = new VkClearColorValue { Float0 = 0.02f, Float1 = 0.02f, Float2 = 0.02f, Float3 = 1.0f }, @@ -140,16 +135,6 @@ internal sealed unsafe class VulkanRenderer : IRenderer, Engine.Graphics.IScreen clearValue = clearValue, }; - var depthAttachment = new VkRenderingAttachmentInfo - { - sType = VkStructureType.RenderingAttachmentInfo, - imageView = _swapchain.DepthImageView, - imageLayout = VkImageLayout.DepthStencilAttachmentOptimal, - loadOp = VkAttachmentLoadOp.Clear, - storeOp = VkAttachmentStoreOp.Store, - clearValue = depthClearValue, - }; - var renderingInfo = new VkRenderingInfo { sType = VkStructureType.RenderingInfo, @@ -161,7 +146,6 @@ internal sealed unsafe class VulkanRenderer : IRenderer, Engine.Graphics.IScreen layerCount = 1, colorAttachmentCount = 1, pColorAttachments = &colorAttachment, - pDepthAttachment = &depthAttachment, }; Vk.vkCmdBeginRendering(cmd, &renderingInfo); @@ -192,11 +176,10 @@ internal sealed unsafe class VulkanRenderer : IRenderer, Engine.Graphics.IScreen ulong offset = 0; Vk.vkCmdBindVertexBuffers(cmd, 0, 1, &bufferHandle, &offset); - Vk.vkCmdBindIndexBuffer(cmd, _indexBuffer.Buffer, 0, 0); + Vk.vkCmdBindIndexBuffer(cmd, _indexBuffer.Buffer, 0, 1); var angle = _totalTime; - var model = Matrix4x4.CreateRotationZ(angle) * Matrix4x4.CreateRotationX(angle * 0.3f); - Vk.vkCmdPushConstants(cmd, _pipeline.PipelineLayout, VkShaderStageFlags.Vertex, 0, 64, &model); + Vk.vkCmdPushConstants(cmd, _pipeline.PipelineLayout, VkShaderStageFlags.Vertex, 0, 4, &angle); Vk.vkCmdDrawIndexed(cmd, _indexCount, 1, 0, 0, 0); @@ -273,8 +256,8 @@ internal sealed unsafe class VulkanRenderer : IRenderer, Engine.Graphics.IScreen { var aspect = (float)_swapchain.Extent.Width / (float)_swapchain.Extent.Height; var proj = Matrix4x4.CreatePerspectiveFieldOfView(MathF.PI / 4f, aspect, 0.1f, 100f); - var view = Matrix4x4.CreateLookAt(new Vector3(0, 0, 3), Vector3.Zero, Vector3.UnitY); - return proj * view; + var view = Matrix4x4.CreateLookAt(new Vector3(0, 0, -2), Vector3.Zero, Vector3.UnitY); + return view * proj; } private static void TransitionImageLayout(VkCommandBuffer cmd, VkImage image,