From 1476763e882866995c284cd25a6b44e451343012 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Jean-Philippe=20Bruy=C3=A8re?= Date: Mon, 7 Oct 2019 19:39:26 +0200 Subject: [PATCH] SpirVTasks Optimisation attribute, DefineConstants attribute for passing macros to glslc, project DefineConstants is automatically added --- SpirVTasks/CompileGLSLTask.cs | 50 +++++++++++++++++++++++++++----- SpirVTasks/SpirVTasks.csproj | 2 +- SpirVTasks/SpirVTasks.targets | 5 ++-- SpirVTasks/spirv.xml | 2 ++ addons/Directory.Build.props | 2 +- samples/Directory.Build.props | 2 +- samples/pbr/pbr.csproj | 3 +- samples/pbr/shaders/pbr_khr.frag | 5 ++-- vke/src/VkWindow.cs | 19 ++++++++---- vke/vke.csproj | 2 +- 10 files changed, 69 insertions(+), 23 deletions(-) diff --git a/SpirVTasks/CompileGLSLTask.cs b/SpirVTasks/CompileGLSLTask.cs index f66d959..d978d62 100644 --- a/SpirVTasks/CompileGLSLTask.cs +++ b/SpirVTasks/CompileGLSLTask.cs @@ -6,6 +6,7 @@ using System.Collections.Generic; using System.Diagnostics; using System.IO; using System.Linq; +using System.Text; using Microsoft.Build.Framework; namespace SpirVTasks { @@ -41,20 +42,32 @@ namespace SpirVTasks { get; set; } + [Required] + [Output] + public ITaskItem DestinationFile { + get; + set; + } + public ITaskItem AdditionalIncludeDirectories { get; set; } /// - /// Optional, Specify the comple glslc executable path. + /// Optional, set macros to be passed to the compiler, project 'DefineConstants' item is automatically added /// - public ITaskItem SpirVCompilerPath { + public ITaskItem[] DefineConstants { get; set; } - [Required] - [Output] - public ITaskItem DestinationFile { + public ITaskItem Optimisation { + get; + set; + } + /// + /// Optional, Specify the comple glslc executable path. + /// + public ITaskItem SpirVCompilerPath { get; set; } @@ -113,7 +126,7 @@ namespace SpirVTasks { } /// - /// Use the SpirVCompilerPath element if present. if not search 'VULKAN_SDK' environment, then PATH. + /// Use the SpirVCompilerPath element if present. if not search 'VULKAN_SDK' environment, then PATH env variable. /// bool tryFindGlslcExecutable (out string glslcPath) { if (!string.IsNullOrEmpty (SpirVCompilerPath?.ItemSpec)) { @@ -177,6 +190,27 @@ namespace SpirVTasks { Directory.CreateDirectory (Path.GetDirectoryName (DestinationFile.ItemSpec)); + //build macros parameter + StringBuilder macros = new StringBuilder (); + if (DefineConstants != null) { + for (int i = 0; i < DefineConstants.Length; i++) { + if (!string.IsNullOrEmpty (DefineConstants[i]?.ItemSpec)) { + foreach (string macro in DefineConstants [i].ItemSpec.Split (new char[]{ ';'}, StringSplitOptions.RemoveEmptyEntries)) + macros.Append ($"-D{macro} "); + } + } + } + string optimisationStr = ""; + if (!string.IsNullOrEmpty (Optimisation?.ItemSpec)) { + if (string.Equals (Optimisation.ItemSpec, "perf", StringComparison.OrdinalIgnoreCase)) + optimisationStr = "-O"; + else if (string.Equals (Optimisation.ItemSpec, "size", StringComparison.OrdinalIgnoreCase)) + optimisationStr = "-Os"; + else if (string.Equals (Optimisation.ItemSpec, "none", StringComparison.OrdinalIgnoreCase)) + optimisationStr = "-O0"; + }else + optimisationStr = "-O"; + Process glslc = new Process(); //glslc.StartInfo.StandardOutputEncoding = System.Text.Encoding.ASCII; //glslc.StartInfo.StandardErrorEncoding = System.Text.Encoding.ASCII; @@ -184,13 +218,15 @@ namespace SpirVTasks { glslc.StartInfo.RedirectStandardOutput = true; glslc.StartInfo.RedirectStandardError = true; glslc.StartInfo.FileName = glslcPath; - glslc.StartInfo.Arguments = $"{tempFile} -o {DestinationFile.ItemSpec}"; + glslc.StartInfo.Arguments = $"{tempFile} -o {DestinationFile.ItemSpec} {macros.ToString()} {optimisationStr}"; glslc.StartInfo.CreateNoWindow = true; glslc.EnableRaisingEvents = true; glslc.OutputDataReceived += Glslc_OutputDataReceived; glslc.ErrorDataReceived += Glslc_ErrorDataReceived; + Log.LogMessage (MessageImportance.High, $"-> glslc {glslc.StartInfo.Arguments}"); + glslc.Start (); glslc.BeginErrorReadLine (); diff --git a/SpirVTasks/SpirVTasks.csproj b/SpirVTasks/SpirVTasks.csproj index 171380d..1727ee6 100644 --- a/SpirVTasks/SpirVTasks.csproj +++ b/SpirVTasks/SpirVTasks.csproj @@ -1,7 +1,7 @@ netstandard2.0 - 0.1.11 + 0.1.15 false $(ReleaseVersion) MSBuild addon to compile and embed spirV shaders diff --git a/SpirVTasks/SpirVTasks.targets b/SpirVTasks/SpirVTasks.targets index 9138d21..6da971e 100644 --- a/SpirVTasks/SpirVTasks.targets +++ b/SpirVTasks/SpirVTasks.targets @@ -8,7 +8,8 @@ - + diff --git a/SpirVTasks/spirv.xml b/SpirVTasks/spirv.xml index 9635ad6..d22b7a8 100644 --- a/SpirVTasks/spirv.xml +++ b/SpirVTasks/spirv.xml @@ -6,4 +6,6 @@ + + diff --git a/addons/Directory.Build.props b/addons/Directory.Build.props index a165eee..29b7f6f 100644 --- a/addons/Directory.Build.props +++ b/addons/Directory.Build.props @@ -34,7 +34,7 @@ - + diff --git a/samples/Directory.Build.props b/samples/Directory.Build.props index cd80060..f96af28 100644 --- a/samples/Directory.Build.props +++ b/samples/Directory.Build.props @@ -33,7 +33,7 @@ - + diff --git a/samples/pbr/pbr.csproj b/samples/pbr/pbr.csproj index 5a79334..4a986e3 100644 --- a/samples/pbr/pbr.csproj +++ b/samples/pbr/pbr.csproj @@ -20,7 +20,8 @@ deferred.%(Filename)%(Extension) - + + diff --git a/samples/pbr/shaders/pbr_khr.frag b/samples/pbr/shaders/pbr_khr.frag index 1f8e056..6992f26 100644 --- a/samples/pbr/shaders/pbr_khr.frag +++ b/samples/pbr/shaders/pbr_khr.frag @@ -7,7 +7,6 @@ #extension GL_ARB_shading_language_420pack : enable #define MANUAL_SRGB 0 -#define DEBUG 1 struct Material { vec4 baseColorFactor; @@ -47,7 +46,7 @@ layout (set = 0, binding = 0) uniform UBO { float gamma; float prefilteredCubeMipLevels; float scaleIBLAmbient; -#if DEBUG +#ifdef DEBUG float debugViewInputs; float debugViewEquation; #endif @@ -376,7 +375,7 @@ void main() color += SRGBtoLINEAR(texture(emissiveMap, inUV1)).rgb * u_EmissiveFactor; outColor = vec4(color, baseColor.a); -#if DEBUG +#ifdef DEBUG // Shader inputs debug visualization if (ubo.debugViewInputs > 0.0) { int index = int(ubo.debugViewInputs); diff --git a/vke/src/VkWindow.cs b/vke/src/VkWindow.cs index f03f66b..40aa3a8 100644 --- a/vke/src/VkWindow.cs +++ b/vke/src/VkWindow.cs @@ -198,8 +198,8 @@ namespace vke { updateViewRequested = true; } } - protected virtual void onMouseButtonDown (Glfw.MouseButton button) { } - protected virtual void onMouseButtonUp (Glfw.MouseButton button) { } + protected virtual void onMouseButtonDown (MouseButton button) { } + protected virtual void onMouseButtonUp (MouseButton button) { } protected virtual void onKeyDown (Key key, int scanCode, Modifier modifiers) { switch (key) { case Key.F4: @@ -305,15 +305,22 @@ namespace vke { Glfw3.PollEvents (); } } - public virtual void UpdateView () { } /// - /// custom update method called at UpdateFrequency + /// Suitable for updating the matrices, called at least once before the rendering loop just + /// after 'OnResize'. Then, triggered each time 'updateViewRequested' is true in the render loop, don't forget to + /// reset 'updateViewRequested' to 'false' or call the 'base.UpdateView()' which will reset this boolean. + /// + public virtual void UpdateView () { + updateViewRequested = false; + } + /// + /// custom update method called at UpdateFrequency, base method is empty. /// public virtual void Update () { } /// - /// called when swapchain has been resized, override this method to resize your framebuffers coupled to the swapchain. - /// The base method will update Window width and height with new swapchain's dimensions. + /// Called when swapchain has been resized, override this method to resize your framebuffers coupled to the swapchain. + /// The base method will update Window 'Width' and 'Height' properties with new swapchain's dimensions. /// protected virtual void OnResize () { Width = swapChain.Width; diff --git a/vke/vke.csproj b/vke/vke.csproj index 538540e..1430087 100644 --- a/vke/vke.csproj +++ b/vke/vke.csproj @@ -48,7 +48,7 @@ - + -- 2.47.3