diff options
| author | Jean-Philippe Bruyère <jp_bruyere@hotmail.com> | 2019-04-15 14:01:29 +0200 |
|---|---|---|
| committer | Jean-Philippe Bruyère <jp_bruyere@hotmail.com> | 2019-04-15 14:01:29 +0200 |
| commit | cd45cff2eaa7d52f64ca917fd822befb634be1da (patch) | |
| tree | a2f6ef7c1e13adbc9ea865c10d3c9529aa627913 /samples/deferred | |
| parent | a43c35a03a2ec9d42b3a7a01de98f616465a9593 (diff) | |
wip
Diffstat (limited to 'samples/deferred')
| -rw-r--r-- | samples/deferred/DebuDrawPipeline.cs | 47 | ||||
| -rw-r--r-- | samples/deferred/main.cs | 125 | ||||
| -rw-r--r-- | samples/deferred/shaders/debug.frag | 12 | ||||
| -rw-r--r-- | samples/deferred/shaders/debug.vert | 30 | ||||
| -rw-r--r-- | samples/deferred/shaders/pbrtest.frag | 8 |
5 files changed, 134 insertions, 88 deletions
diff --git a/samples/deferred/DebuDrawPipeline.cs b/samples/deferred/DebuDrawPipeline.cs new file mode 100644 index 0000000..1a79f19 --- /dev/null +++ b/samples/deferred/DebuDrawPipeline.cs @@ -0,0 +1,47 @@ +using System; +using System.Numerics; +using System.Runtime.InteropServices; +using VK; + +namespace VKE { + public class DebugDrawPipeline : Pipeline { + + public HostBuffer Vertices; + uint vertexCount; + uint vboLength = 100 * 6 * sizeof (float); + + public DebugDrawPipeline (Device dev, VkFormat colorFormat, VkSampleCountFlags samples = VkSampleCountFlags.SampleCount1) : + base (new RenderPass (dev, colorFormat), "Debug draw pipeline") { + + PipelineConfig cfg = PipelineConfig.CreateDefault (VkPrimitiveTopology.LineList, samples); + cfg.RenderPass = RenderPass; + cfg.Layout = new PipelineLayout(dev); + cfg.AddVertexBinding (0, 6 * sizeof(float)); + cfg.SetVertexAttributes (0, VkFormat.R32g32b32Sfloat, VkFormat.R32g32b32Sfloat); + + cfg.AddShader (VkShaderStageFlags.Vertex, "shaders/pbrtest.vert.spv"); + cfg.AddShader (VkShaderStageFlags.Fragment, "shaders/pbrtest.frag.spv"); + + layout = cfg.Layout; + + init (cfg); + + Vertices = new HostBuffer (dev, VkBufferUsageFlags.VertexBuffer, vboLength); + Vertices.Map (); + } + + public void AddLine (Vector3 start, Vector3 end, float r, float g, float b) { + float[] data = { }; + Vertices.Update (data, 12 * sizeof (float), vertexCount * 6 * sizeof (float)); + vertexCount++; + } + + protected override void Dispose (bool disposing) { + Vertices.Unmap (); + Vertices.Dispose (); + + base.Dispose (disposing); + } + } + +} diff --git a/samples/deferred/main.cs b/samples/deferred/main.cs index 7c5917a..560a8c3 100644 --- a/samples/deferred/main.cs +++ b/samples/deferred/main.cs @@ -16,8 +16,6 @@ namespace PbrSample { VkSampleCountFlags samples = VkSampleCountFlags.SampleCount1; - Camera Camera = new Camera (Utils.DegreesToRadians (60f), 1f); - public struct Matrices { public Matrix4x4 projection; public Matrix4x4 view; @@ -33,22 +31,27 @@ namespace PbrSample { exposure = 2.0f, }; +#if DEBUG PipelineStatisticsQueryPool statPool; TimestampQueryPool timestampQPool; ulong[] results; +#endif protected override void configureEnabledFeatures (ref VkPhysicalDeviceFeatures features) { base.configureEnabledFeatures (ref features); +#if DEBUG features.pipelineStatisticsQuery = true; +#endif } Program () { - Camera.Model = Matrix4x4.CreateRotationX (Utils.DegreesToRadians (-90)) * Matrix4x4.CreateRotationY (Utils.DegreesToRadians (180)); - Camera.SetRotation (-0.1f,-0.4f); - Camera.SetPosition (0, 0, -3); + camera.Model = Matrix4x4.CreateRotationX (Utils.DegreesToRadians (-90)) * Matrix4x4.CreateRotationY (Utils.DegreesToRadians (180)); + camera.SetRotation (-0.1f,-0.4f); + camera.SetPosition (0, 0, -3); init (); +#if DEBUG statPool = new PipelineStatisticsQueryPool (dev, VkQueryPipelineStatisticFlags.InputAssemblyVertices | VkQueryPipelineStatisticFlags.InputAssemblyPrimitives | @@ -57,6 +60,7 @@ namespace PbrSample { VkQueryPipelineStatisticFlags.FragmentShaderInvocations); timestampQPool = new TimestampQueryPool (dev); +#endif } Framebuffer[] frameBuffers; @@ -146,7 +150,7 @@ namespace PbrSample { dsGBuff = descriptorPool.Allocate (descLayoutGBuff); PipelineConfig cfg = PipelineConfig.CreateDefault (VkPrimitiveTopology.TriangleList, samples); - cfg.Layout = new PipelineLayout (dev, descLayoutMain, descLayoutModelTextures); + cfg.Layout = new PipelineLayout (dev, descLayoutMain, descLayoutModelTextures, descLayoutGBuff); cfg.Layout.AddPushConstants ( new VkPushConstantRange (VkShaderStageFlags.Vertex, (uint)Marshal.SizeOf<Matrix4x4> ()), new VkPushConstantRange (VkShaderStageFlags.Fragment, (uint)Marshal.SizeOf<Model.PbrMaterial> (), 64) @@ -162,10 +166,11 @@ namespace PbrSample { cfg.AddShader (VkShaderStageFlags.Fragment, "shaders/GBuffPbr.frag.spv"); gBuffPipeline = new Pipeline (cfg); - + cfg.blendAttachments.Clear (); + cfg.blendAttachments.Add (new VkPipelineColorBlendAttachmentState (false)); cfg.ResetShadersAndVerticesInfos (); cfg.SubpassIndex = 1; - cfg.Layout = new PipelineLayout (dev, descLayoutMain, descLayoutGBuff); + cfg.Layout = gBuffPipeline.Layout; cfg.depthStencilState.depthTestEnable = false; cfg.depthStencilState.depthWriteEnable = false; cfg.AddShader (VkShaderStageFlags.Vertex, "shaders/FullScreenQuad.vert.spv"); @@ -186,6 +191,8 @@ namespace PbrSample { envCube.WriteDesc (uboMats.Descriptor); model = new Model (presentQueue, "../data/models/DamagedHelmet/glTF/DamagedHelmet.gltf"); + //model = new Model (presentQueue, "../data/models/chess.gltf"); + //model = new Model (presentQueue, "../data/models/Sponza/glTF/Sponza.gltf"); //model = new Model (dev, presentQueue, "../data/models/icosphere.gltf"); //model = new Model (dev, presentQueue, cmdPool, "../data/models/cube.gltf"); model.WriteMaterialsDescriptorSets (descLayoutModelTextures, @@ -203,11 +210,13 @@ namespace PbrSample { cmds[i] = cmdPool.AllocateCommandBuffer (); cmds[i].Start (); +#if DEBUG statPool.Begin (cmds[i]); - recordDraw (cmds[i], frameBuffers[i]); - statPool.End (cmds[i]); +#else + recordDraw (cmds[i], frameBuffers[i]); +#endif cmds[i].End (); } @@ -225,8 +234,7 @@ namespace PbrSample { renderPass.BeginSubPass (cmd); - cmd.BindDescriptorSet (composePipeline.Layout, dsMain); - cmd.BindDescriptorSet (composePipeline.Layout, dsGBuff, 1); + cmd.BindDescriptorSet (composePipeline.Layout, dsGBuff, 2); composePipeline.Bind (cmd); cmd.Draw (3, 1, 0, 0); @@ -234,13 +242,13 @@ namespace PbrSample { renderPass.End (cmd); } - #region update +#region update void updateMatrices () { - Camera.AspectRatio = (float)swapChain.Width / swapChain.Height; + camera.AspectRatio = (float)swapChain.Width / swapChain.Height; - matrices.projection = Camera.Projection; - matrices.view = Camera.View; - matrices.model = Camera.Model; + matrices.projection = camera.Projection; + matrices.view = camera.View; + matrices.model = camera.Model; uboMats.Update (matrices, (uint)Marshal.SizeOf<Matrices> ()); matrices.view *= Matrix4x4.CreateTranslation (-matrices.view.Translation); matrices.model = Matrix4x4.Identity; @@ -252,10 +260,11 @@ namespace PbrSample { updateViewRequested = false; } public override void Update () { +#if DEBUG results = statPool.GetResults (); - +#endif } - #endregion +#endregion @@ -284,7 +293,11 @@ namespace PbrSample { gbEmitMetal.Descriptor, gbN.Descriptor, gbPos.Descriptor); - } + gbColorRough.SetName ("GBuffColorRough"); + gbEmitMetal.SetName ("GBuffEmitMetal"); + gbN.SetName ("GBuffN"); + gbPos.SetName ("GBuffPos"); + } protected override void OnResize () { updateMatrices (); @@ -299,16 +312,7 @@ namespace PbrSample { for (int i = 0; i < swapChain.ImageCount; ++i) { frameBuffers[i] = new Framebuffer (renderPass, swapChain.Width, swapChain.Height, new Image[] { - swapChain.images[i], null, gbColorRough, gbEmitMetal, gbN, gbPos}); - //frameBuffers[i] = new Framebuffer (renderPass, swapChain.Width, swapChain.Height, - //(renderPass.Samples == VkSampleCountFlags.SampleCount1) ? new Image[] { - // swapChain.images[i], - // null - //} : new Image[] { - // null, - // null, - // swapChain.images[i] - //}); + swapChain.images[i], null, gbColorRough, gbEmitMetal, gbN, gbPos}); } buildCommandBuffers (); @@ -316,56 +320,8 @@ namespace PbrSample { #region Mouse and keyboard - protected override void onMouseMove (double xPos, double yPos) { - double diffX = lastMouseX - xPos; - double diffY = lastMouseY - yPos; - if (MouseButton[0]) { - Camera.Rotate ((float)-diffX, (float)-diffY); - } else if (MouseButton[1]) { - Camera.Zoom ((float)diffY); - } - - updateViewRequested = true; - } - protected override void onKeyDown (Key key, int scanCode, Modifier modifiers) { switch (key) { - case Key.Up: - if (modifiers.HasFlag (Modifier.Shift)) - matrices.lightPos += new Vector4 (0, 0, 1, 0); - else - Camera.Move (0, 0, 1); - break; - case Key.Down: - if (modifiers.HasFlag (Modifier.Shift)) - matrices.lightPos += new Vector4 (0, 0, -1, 0); - else - Camera.Move (0, 0, -1); - break; - case Key.Left: - if (modifiers.HasFlag (Modifier.Shift)) - matrices.lightPos += new Vector4 (1, 0, 0, 0); - else - Camera.Move (1, 0, 0); - break; - case Key.Right: - if (modifiers.HasFlag (Modifier.Shift)) - matrices.lightPos += new Vector4 (-1, 0, 0, 0); - else - Camera.Move (-1, 0, 0); - break; - case Key.PageUp: - if (modifiers.HasFlag (Modifier.Shift)) - matrices.lightPos += new Vector4 (0, 1, 0, 0); - else - Camera.Move (0, 1, 0); - break; - case Key.PageDown: - if (modifiers.HasFlag (Modifier.Shift)) - matrices.lightPos += new Vector4 (0, -1, 0, 0); - else - Camera.Move (0, -1, 0); - break; case Key.F1: if (modifiers.HasFlag (Modifier.Shift)) matrices.exposure -= 0.3f; @@ -379,11 +335,11 @@ namespace PbrSample { matrices.gamma += 0.1f; break; case Key.F3: - if (Camera.Type == Camera.CamType.FirstPerson) - Camera.Type = Camera.CamType.LookAt; + if (camera.Type == Camera.CamType.FirstPerson) + camera.Type = Camera.CamType.LookAt; else - Camera.Type = Camera.CamType.FirstPerson; - Console.WriteLine ($"camera type = {Camera.Type}"); + camera.Type = Camera.CamType.FirstPerson; + Console.WriteLine ($"camera type = {camera.Type}"); break; default: base.onKeyDown (key, scanCode, modifiers); @@ -391,7 +347,7 @@ namespace PbrSample { } updateViewRequested = true; } - #endregion +#endregion protected override void Dispose (bool disposing) { if (disposing) { @@ -408,9 +364,10 @@ namespace PbrSample { descLayoutGBuff.Dispose (); descriptorPool.Dispose (); - +#if DEBUG timestampQPool?.Dispose (); statPool?.Dispose (); +#endif } } diff --git a/samples/deferred/shaders/debug.frag b/samples/deferred/shaders/debug.frag new file mode 100644 index 0000000..fd419ff --- /dev/null +++ b/samples/deferred/shaders/debug.frag @@ -0,0 +1,12 @@ +#version 450 + +#extension GL_ARB_separate_shader_objects : enable +#extension GL_ARB_shading_language_420pack : enable + +layout (location = 0) in vec3 inColor; +layout (location = 0) out vec4 outFragColor; + +void main() +{ + outFragColor = vec4(inColor, 1.0); +}
\ No newline at end of file diff --git a/samples/deferred/shaders/debug.vert b/samples/deferred/shaders/debug.vert new file mode 100644 index 0000000..ac67a01 --- /dev/null +++ b/samples/deferred/shaders/debug.vert @@ -0,0 +1,30 @@ +#version 450 + +#extension GL_ARB_separate_shader_objects : enable +#extension GL_ARB_shading_language_420pack : enable + +layout (location = 0) in vec3 inPos; +layout (location = 1) in vec3 inColor; + +layout (binding = 0) uniform UBO +{ + mat4 projectionMatrix; + mat4 viewMatrix; + mat4 modelMatrix; + vec4 lightPos; + float gamma; + float exposure; +} ubo; + +layout (location = 0) out vec3 outColor; + +out gl_PerVertex +{ + vec4 gl_Position; +}; + +void main() +{ + outColor = inColor; + gl_Position = ubo.projectionMatrix * ubo.viewMatrix * ubo.modelMatrix * vec4(inPos.xyz, 1.0);; +} diff --git a/samples/deferred/shaders/pbrtest.frag b/samples/deferred/shaders/pbrtest.frag index b5549b7..af6ecc3 100644 --- a/samples/deferred/shaders/pbrtest.frag +++ b/samples/deferred/shaders/pbrtest.frag @@ -13,10 +13,10 @@ layout (binding = 0) uniform UBO float exposure; } ubo; -layout (input_attachment_index = 0, set = 1, binding = 0) uniform subpassInput samplerColorRough; -layout (input_attachment_index = 1, set = 1, binding = 1) uniform subpassInput samplerEmitMetal; -layout (input_attachment_index = 2, set = 1, binding = 2) uniform subpassInput samplerN; -layout (input_attachment_index = 3, set = 1, binding = 3) uniform subpassInput samplerPos; +layout (input_attachment_index = 0, set = 2, binding = 0) uniform subpassInput samplerColorRough; +layout (input_attachment_index = 1, set = 2, binding = 1) uniform subpassInput samplerEmitMetal; +layout (input_attachment_index = 2, set = 2, binding = 2) uniform subpassInput samplerN; +layout (input_attachment_index = 3, set = 2, binding = 3) uniform subpassInput samplerPos; layout (set = 0, binding = 1) uniform sampler2D samplerBRDFLUT; layout (set = 0, binding = 2) uniform samplerCube samplerIrradiance; |