diff options
| author | Jean-Philippe Bruyère <jp_bruyere@hotmail.com> | 2019-04-25 20:53:19 +0200 |
|---|---|---|
| committer | Jean-Philippe Bruyère <jp_bruyere@hotmail.com> | 2019-04-25 20:53:19 +0200 |
| commit | 01ea60eb8fc7b7a30f388f7be6e7c401ee94e441 (patch) | |
| tree | 13edcd859f36ae5b074cf797223fe611000c44cc | |
| parent | 53a2d61a35e3fee03db78090c050867ad21f7c06 (diff) | |
specialization infos helper class
| -rw-r--r-- | VK/Structs.cs | 2 | ||||
| -rw-r--r-- | VK/VK.csproj | 7 | ||||
| -rw-r--r-- | samples/deferred/EnvironmentPipeline.cs | 31 | ||||
| -rw-r--r-- | samples/deferred/main.cs | 33 | ||||
| -rw-r--r-- | samples/deferred/shaders/GBuffPbr.frag | 6 | ||||
| -rw-r--r-- | vke/src/Camera.cs | 6 | ||||
| -rw-r--r-- | vke/src/GraphicPipelineConfig.cs | 4 | ||||
| -rw-r--r-- | vke/src/ShaderInfo.cs | 7 | ||||
| -rw-r--r-- | vke/src/SpecializationConstant.cs | 105 |
9 files changed, 156 insertions, 45 deletions
diff --git a/VK/Structs.cs b/VK/Structs.cs index 9a6fe62..4065055 100644 --- a/VK/Structs.cs +++ b/VK/Structs.cs @@ -215,5 +215,7 @@ namespace VK { this.layout = layout; } } + + } diff --git a/VK/VK.csproj b/VK/VK.csproj index 02004f4..47d501d 100644 --- a/VK/VK.csproj +++ b/VK/VK.csproj @@ -29,7 +29,12 @@ <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' "> <AllowUnsafeBlocks>true</AllowUnsafeBlocks> </PropertyGroup> - <Target Name="rewrite" AfterTargets="CoreBuild"> + <ItemGroup> + <Compile Include="..\vk.generator\bin\**\*.cs"> + <Link>generated\%(Filename)%(Extension)</Link> + </Compile> + </ItemGroup> + <Target Name="rewrite" AfterTargets="CoreBuild"> <Exec Condition="$(OS) == 'Windows_NT'" Command="$(SolutionDir)build/$(TargetFramework)rewrite/vk.rewrite.exe $(OutputPath)VK.dll" /> <Exec Condition="$(OS) != 'Windows_NT'" Command="mono ../vk.rewrite/bin/Release/vk.rewrite.exe $(OutputPath)VK.dll" ConsoleToMSBuild="true" EchoOff="true"> <Output TaskParameter="ConsoleOutput" PropertyName="OutputOfExec" /> diff --git a/samples/deferred/EnvironmentPipeline.cs b/samples/deferred/EnvironmentPipeline.cs index 063f5e2..68128cc 100644 --- a/samples/deferred/EnvironmentPipeline.cs +++ b/samples/deferred/EnvironmentPipeline.cs @@ -14,7 +14,6 @@ namespace CVKL { public Image irradianceCube { get; private set; } public Image prefilterCube { get; set; } - public Image debugImg; public int debugMip = 0; public int debugFace = 0; @@ -51,11 +50,6 @@ namespace CVKL { } - //public void WriteDesc (VkDescriptorBufferInfo matrixDesc) { - // DescriptorSetWrites uboUpdate = new DescriptorSetWrites (descLayoutMain); - // uboUpdate.Write (Dev, dsSkybox, matrixDesc, cubemap.Descriptor); - //} - public void RecordDraw (CommandBuffer cmd) { Bind (cmd); cmd.BindVertexBuffer (vboSkybox); @@ -265,7 +259,7 @@ namespace CVKL { cmd.PushConstant (pl.Layout, VkShaderStageFlags.Vertex | VkShaderStageFlags.Fragment, deltaTheta, (uint)Marshal.SizeOf<Matrix4x4> () + 4); } else { cmd.PushConstant (pl.Layout, VkShaderStageFlags.Vertex | VkShaderStageFlags.Fragment, roughness, (uint)Marshal.SizeOf<Matrix4x4> ()); - cmd.PushConstant (pl.Layout, VkShaderStageFlags.Vertex | VkShaderStageFlags.Fragment, 32u, (uint)Marshal.SizeOf<Matrix4x4> () + 4); + cmd.PushConstant (pl.Layout, VkShaderStageFlags.Vertex | VkShaderStageFlags.Fragment, 512u, (uint)Marshal.SizeOf<Matrix4x4> () + 4); } cmd.BindDescriptorSet (pl.Layout, dset); @@ -288,27 +282,6 @@ namespace CVKL { 1, region.Pin ()); region.Unpin (); - /* - //debug img - if (target == CBTarget.PREFILTEREDENV && m == debugMip && f == debugFace) { - debugImg?.Dispose (); - debugImg = new Image (Dev, VkFormat.R16g16b16a16Sfloat, VkImageUsageFlags.Sampled | VkImageUsageFlags.TransferDst, VkMemoryPropertyFlags.DeviceLocal, - (uint)viewPortSize, (uint)viewPortSize); - debugImg.CreateView (); - debugImg.CreateSampler (); - region.dstSubresource.baseArrayLayer = 0; - region.dstSubresource.mipLevel = 0; - debugImg.SetLayout (cmd, VkImageAspectFlags.Color, - VkImageLayout.Undefined, VkImageLayout.TransferDstOptimal); - Vk.vkCmdCopyImage (cmd.Handle, - imgFbOffscreen.Handle, VkImageLayout.TransferSrcOptimal, - debugImg.Handle, VkImageLayout.TransferDstOptimal, - 1, region.Pin ()); - debugImg.SetLayout (cmd, VkImageAspectFlags.Color, - VkImageLayout.TransferDstOptimal, VkImageLayout.ShaderReadOnlyOptimal); - region.Unpin (); - }*/ - imgFbOffscreen.SetLayout (cmd, VkImageAspectFlags.Color, VkImageLayout.TransferSrcOptimal, VkImageLayout.ColorAttachmentOptimal); @@ -346,8 +319,6 @@ namespace CVKL { irradianceCube.Dispose (); prefilterCube.Dispose (); - debugImg?.Dispose (); - base.Dispose (disposing); } } diff --git a/samples/deferred/main.cs b/samples/deferred/main.cs index 929026c..1b1d720 100644 --- a/samples/deferred/main.cs +++ b/samples/deferred/main.cs @@ -36,6 +36,7 @@ namespace deferred { } DebugView currentDebugView = DebugView.none; + public struct Matrices { public Matrix4x4 projection; public Matrix4x4 model; @@ -46,19 +47,31 @@ namespace deferred { public float gamma; public float prefilteredCubeMipLevels; public float scaleIBLAmbient; - public float debugViewInputs; + public uint lightsCount; public float debugViewEquation; } + public struct Light { + public Vector4 position; + public Vector4 color; + } + public Matrices matrices = new Matrices { lightPos = Vector4.Normalize (new Vector4 (0.7f, 0.6f, 0.2f, 0.0f)), gamma = 1.2f, exposure = 2.0f, scaleIBLAmbient = 1.0f, - debugViewInputs = 0, + lightsCount = 1, debugViewEquation = 0 }; + Light[] lights = { + new Light { + position = new Vector4(1,1,1,0), + color = new Vector4(1) + } + }; + Framebuffer[] frameBuffers; Image gbColorRough, gbEmitMetal, gbN_AO, gbPos, hdrImg; @@ -84,7 +97,10 @@ namespace deferred { const int SP_TONE_MAPPING = 3; Program () { + camera = new Camera (Utils.DegreesToRadians (45f), 1f, 0.1f, 16f); + init (); + camera.SetPosition (0, 0, 3); camera.Model = Matrix4x4.CreateScale (1f / Math.Max (Math.Max (modelAABB.max.X, modelAABB.max.Y), modelAABB.max.Z)); //camera.Model = Matrix4x4.CreateScale (0.02f); @@ -204,10 +220,16 @@ namespace deferred { cfg.AddVertexBinding<PbrModel.Vertex> (0); cfg.SetVertexAttributes (0, VkFormat.R32g32b32Sfloat, VkFormat.R32g32b32Sfloat, VkFormat.R32g32Sfloat, VkFormat.R32g32Sfloat); - cfg.AddShader (VkShaderStageFlags.Vertex, "shaders/GBuffPbr.vert.spv"); - cfg.AddShader (VkShaderStageFlags.Fragment, "shaders/GBuffPbr.frag.spv"); - gBuffPipeline = new GraphicPipeline (cfg); + using (SpecializationInfo constants = new SpecializationInfo ( + new SpecializationConstant<float> (0, camera.NearPlane), + new SpecializationConstant<float> (1, camera.FarPlane))) { + + cfg.AddShader (VkShaderStageFlags.Vertex, "shaders/GBuffPbr.vert.spv"); + cfg.AddShader (VkShaderStageFlags.Fragment, "shaders/GBuffPbr.frag.spv", constants); + + gBuffPipeline = new GraphicPipeline (cfg); + } //COMPOSE PIPELINE cfg.blendAttachments.Clear (); @@ -345,7 +367,6 @@ namespace deferred { camera.Position.Z * (float)Math.Cos (camera.Rotation.Y) * (float)Math.Cos (camera.Rotation.X), 0 ); - //matrices.debugViewInputs = (float)currentDebugView; uboMats.Update (matrices, (uint)Marshal.SizeOf<Matrices> ()); diff --git a/samples/deferred/shaders/GBuffPbr.frag b/samples/deferred/shaders/GBuffPbr.frag index cd46c6a..0a98717 100644 --- a/samples/deferred/shaders/GBuffPbr.frag +++ b/samples/deferred/shaders/GBuffPbr.frag @@ -5,6 +5,9 @@ #define MANUAL_SRGB 0 #define DEBUG 0 +layout (constant_id = 0) const float NEAR_PLANE = 0.1f; +layout (constant_id = 1) const float FAR_PLANE = 256.0f; + struct Material { vec4 baseColorFactor; vec4 emissiveFactor; @@ -137,9 +140,6 @@ float convertMetallic(vec3 diffuse, vec3 specular, float maxSpecular) { return clamp((-b + sqrt(D)) / (2.0 * a), 0.0, 1.0); } -layout (constant_id = 0) const float NEAR_PLANE = 0.1f; -layout (constant_id = 1) const float FAR_PLANE = 256.0f; - float linearDepth(float depth) { float z = depth * 2.0f - 1.0f; diff --git a/vke/src/Camera.cs b/vke/src/Camera.cs index 0d96147..c49717c 100644 --- a/vke/src/Camera.cs +++ b/vke/src/Camera.cs @@ -25,6 +25,8 @@ namespace CVKL { public Vector3 Position => position; public Vector3 Rotation => rotation; + public float NearPlane => zNear; + public float FarPlane => zFar; public CamType Type; @@ -46,9 +48,11 @@ namespace CVKL { get { return Matrix4x4.CreatePerspectiveFieldOfView (fov, aspectRatio, zNear, zFar); } } - public Camera (float fieldOfView, float aspectRatio) { + public Camera (float fieldOfView, float aspectRatio, float nearPlane = 0.1f, float farPlane = 256f) { fov = fieldOfView; this.aspectRatio = aspectRatio; + zNear = nearPlane; + zFar = farPlane; Update (); } diff --git a/vke/src/GraphicPipelineConfig.cs b/vke/src/GraphicPipelineConfig.cs index be6d684..31d9d7d 100644 --- a/vke/src/GraphicPipelineConfig.cs +++ b/vke/src/GraphicPipelineConfig.cs @@ -110,8 +110,8 @@ namespace CVKL { public void AddVertexBinding<T> (uint binding, VkVertexInputRate inputRate = VkVertexInputRate.Vertex) { vertexBindings.Add (new VkVertexInputBindingDescription (binding, (uint)Marshal.SizeOf<T> (), inputRate)); } - public void AddShader (VkShaderStageFlags _stageFlags, string _spirvPath, string _entryPoint = "main") { - shaders.Add (new ShaderInfo (_stageFlags, _spirvPath, _entryPoint)); + public void AddShader (VkShaderStageFlags _stageFlags, string _spirvPath, SpecializationInfo specializationInfo = null, string _entryPoint = "main") { + shaders.Add (new ShaderInfo (_stageFlags, _spirvPath, specializationInfo, _entryPoint)); } public void ResetShadersAndVerticesInfos () { diff --git a/vke/src/ShaderInfo.cs b/vke/src/ShaderInfo.cs index 0946df5..61c99cf 100644 --- a/vke/src/ShaderInfo.cs +++ b/vke/src/ShaderInfo.cs @@ -31,11 +31,13 @@ namespace CVKL { public VkShaderStageFlags StageFlags; public string SpirvPath; public FixedUtf8String EntryPoint; + public SpecializationInfo SpecializationInfo; - public ShaderInfo (VkShaderStageFlags _stageFlags, string _spirvPath, string _entryPoint = "main") { + public ShaderInfo (VkShaderStageFlags _stageFlags, string _spirvPath, SpecializationInfo specializationInfo = null, string _entryPoint = "main") { StageFlags = _stageFlags; SpirvPath = _spirvPath; EntryPoint = new FixedUtf8String (_entryPoint); + this.SpecializationInfo = specializationInfo; } public VkPipelineShaderStageCreateInfo GetStageCreateInfo (Device dev) { @@ -44,7 +46,8 @@ namespace CVKL { stage = StageFlags, pName = EntryPoint, module = dev.LoadSPIRVShader (SpirvPath), - }; + pSpecializationInfo = (SpecializationInfo == null) ? IntPtr.Zero : SpecializationInfo.InfosPtr + }; } #region IDisposable Support diff --git a/vke/src/SpecializationConstant.cs b/vke/src/SpecializationConstant.cs new file mode 100644 index 0000000..adb51d9 --- /dev/null +++ b/vke/src/SpecializationConstant.cs @@ -0,0 +1,105 @@ +// +// SpecializationConstant.cs +// +// Author: +// Jean-Philippe Bruyère <jp_bruyere@hotmail.com> +// +// Copyright (c) 2019 jp +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. +using System; +using System.Collections.Generic; +using System.Runtime.InteropServices; +using VK; + +namespace CVKL { + public class SpecializationConstant<T> : SpecializationConstant { + T val; + IntPtr ptr = IntPtr.Zero; + + public T Value { + get { return val; } + set { + val = value; + if (ptr != IntPtr.Zero) + WriteTo (ptr); + } + } + public SpecializationConstant(uint id, T value) : base(id) { + val = value; + } + public override uint Size => (uint)Marshal.SizeOf<T> (); + public unsafe override void WriteTo (IntPtr ptr) { + this.ptr = ptr; + + if (typeof(T) == typeof(float)) { + float val = Convert.ToSingle(Value); + System.Buffer.MemoryCopy (&val, ptr.ToPointer (), 4, 4); + } + } + } + public abstract class SpecializationConstant { + public uint id; + public SpecializationConstant (uint id) { + this.id = id; + } + public abstract uint Size { get; } + public abstract void WriteTo (IntPtr ptr); + } + + public class SpecializationInfo : IDisposable { + IntPtr pData; + VkSpecializationMapEntry[] entries; + + VkSpecializationInfo infos; + + public IntPtr InfosPtr { get; private set; } + + public SpecializationInfo (params SpecializationConstant[] constants) { + uint offset = 0; + entries = new VkSpecializationMapEntry[constants.Length]; + for (int i = 0; i < constants.Length; i++) { + entries[i] = new VkSpecializationMapEntry { constantID = constants[i].id, offset = offset, size = (UIntPtr)constants[i].Size }; + offset += constants[i].Size; + } + int totSize = (int)offset; + offset = 0; + pData = Marshal.AllocHGlobal (totSize); + IntPtr curPtr = pData; + foreach (SpecializationConstant sc in constants) { + sc.WriteTo (curPtr); + curPtr += (int)sc.Size; + } + + infos = new VkSpecializationInfo { + mapEntryCount = (uint)constants.Length, + pMapEntries = entries.Pin (), + pData = pData, + dataSize = (UIntPtr)totSize + }; + InfosPtr = infos.Pin (); + } + + public void Dispose () { + infos.Unpin (); + Marshal.FreeHGlobal (pData); + entries.Unpin (); + } + } +} |