Skip to content

Commit 8cd9831

Browse files
authored
[CI] Hook up shader compilation to build (MonoGame#8916)
This is the first part to making shader compilation be done on the CI. The 2nd part will automatically commit the files so a fresh repo clone would not require shader recompilation. ~~I've removed the RebuildMGFX.bat scripts, would you like me to make a build_shaders.bat/.sh on top level of the repo by any chance? ~~ Added scripts folder to the repo root with new versions of RebuildMGFX ~~DX12 shader build step weren't working and I can't easily get to the bottom of it from macOS / Linux :(~~ This PR is needed to unblock MonoGame#8903 from getting reviewed and merged CC. @tomspilman @ThomasFOG
1 parent c1a0e36 commit 8cd9831

25 files changed

Lines changed: 180 additions & 89 deletions

.github/workflows/main.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ jobs:
3333
- name: Setup DotNet on MacOS
3434
if: runner.environment == 'github-hosted' && runner.os == 'macos'
3535
run: |
36-
dotnet workload install android macos ios
36+
dotnet workload install macos ios
3737
3838
- name: Add msbuild to PATH
3939
if: runner.os == 'Windows'

MonoGame.Framework.Content.Pipeline/ExternalTool.cs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
using System.IO;
88
using System.Net;
99
using System.Reflection;
10+
using System.Runtime.InteropServices;
1011
using System.Threading;
11-
using MonoGame.Framework.Utilities;
1212

1313
namespace Microsoft.Xna.Framework.Content.Pipeline
1414
{
@@ -32,31 +32,31 @@ public static int Run(string command, string arguments)
3232
static void RestoreDotnetTool(string command, string toolName, string toolVersion, string path)
3333
{
3434
Directory.CreateDirectory(path);
35-
var exe = CurrentPlatform.OS == OS.Windows ? "dotnet.exe" : "dotnet";
35+
var exe = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? "dotnet.exe" : "dotnet";
3636
var dotnetRoot = Environment.GetEnvironmentVariable("DOTNET_ROOT");
3737
if (!string.IsNullOrEmpty(dotnetRoot))
3838
{
3939
exe = Path.Combine(dotnetRoot, exe);
4040
}
41-
if (Run(exe, $"tool {command} {toolName} --version {toolVersion} --tool-path .", out string stdout, out string stderr, workingDirectory: path) != 0)
41+
if (Run(exe, $"tool {command} {toolName} --version {toolVersion} --tool-path .", out string stdout, out string stderr, workingDirectory: path) != 0)
4242
{
4343
// install the latest
44-
Debug.WriteLine ($"{command} returned {stdout} {stderr}. Trying backup path.");
45-
Run(exe, $"tool {command} {toolName} --tool-path .", out stdout, out stderr, workingDirectory: path);
44+
Debug.WriteLine($"{command} returned {stdout} {stderr}. Trying backup path.");
45+
Run(exe, $"tool {command} {toolName} --tool-path .", out stdout, out stderr, workingDirectory: path);
4646
}
4747
}
4848

4949
/// <summary>
5050
/// Run a dotnet tool. The tool should be installed in a .config/dotnet-tools.json file somewhere in the project lineage.
5151
/// </summary>
52-
public static int RunDotnetTool(string toolName, string args, out string stdOut, out string stdErr, string stdIn=null, string workingDirectory=null)
52+
public static int RunDotnetTool(string toolName, string args, out string stdOut, out string stdErr, string stdIn = null, string workingDirectory = null)
5353
{
5454
var exe = FindCommand(toolName);
55-
var finalizedArgs = args;
55+
var finalizedArgs = args;
5656
return ExternalTool.Run(exe, finalizedArgs, out stdOut, out stdErr, stdIn, workingDirectory);
5757
}
5858

59-
public static int Run(string command, string arguments, out string stdout, out string stderr, string stdin = null, string workingDirectory=null)
59+
public static int Run(string command, string arguments, out string stdout, out string stderr, string stdin = null, string workingDirectory = null)
6060
{
6161
// This particular case is likely to be the most common and thus
6262
// warrants its own specific error message rather than falling
@@ -164,12 +164,12 @@ private static string FindCommand(string command)
164164

165165
// For Linux check specific subfolder
166166
var lincom = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "linux", command);
167-
if (CurrentPlatform.OS == OS.Linux && File.Exists(lincom))
167+
if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux) && File.Exists(lincom))
168168
return lincom;
169169

170170
// For Mac check specific subfolder
171171
var maccom = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "osx", command);
172-
if (CurrentPlatform.OS == OS.MacOSX && File.Exists(maccom))
172+
if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX) && File.Exists(maccom))
173173
return maccom;
174174

175175
// We don't have a full path, so try running through the system path to find it.
@@ -184,7 +184,7 @@ private static string FindCommand(string command)
184184
if (File.Exists(fullName))
185185
return fullName;
186186

187-
if (CurrentPlatform.OS == OS.Windows)
187+
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
188188
{
189189
var fullExeName = string.Concat(fullName, ".exe");
190190
if (File.Exists(fullExeName))

MonoGame.Framework/Platform/Graphics/Effect/Resources/RebuildMGFX.bat

Lines changed: 0 additions & 32 deletions
This file was deleted.

Tools/MonoGame.Effect.Compiler/Effect/ShaderProfile.Vulkan.cs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
using System.Diagnostics;
1313
using System.Threading;
1414
using Microsoft.Xna.Framework.Content.Pipeline;
15+
using MonoGame.Tool;
16+
using System.Runtime.InteropServices;
1517

1618
namespace MonoGame.Effect
1719
{
@@ -224,7 +226,18 @@ internal override ShaderData CreateShader(ShaderResult shaderResult, string shad
224226
}
225227
toolArgs += "\"" + hlslFile + "\"";
226228

227-
toolResult = ExternalTool.Run("dxc", toolArgs, out stdout, out stderr);
229+
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
230+
{
231+
toolResult = ExternalTool.Run("dxc", toolArgs, out stdout, out stderr);
232+
}
233+
else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
234+
{
235+
toolResult = ExternalTool.Run(Path.Combine(AppContext.BaseDirectory, "osx/bin/dxc"), toolArgs, out stdout, out stderr);
236+
}
237+
else
238+
{
239+
toolResult = Dxc.Run(toolArgs, out stdout, out stderr);
240+
}
228241

229242
errorsAndWarnings += stderr;
230243

Tools/MonoGame.Effect.Compiler/MonoGame.Effect.Compiler.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616

1717
<ItemGroup>
1818
<Compile Include="..\..\MonoGame.Framework.Content.Pipeline\ExternalTool.cs" />
19-
<Compile Include="..\..\MonoGame.Framework\Platform\Utilities\CurrentPlatform.cs" />
2019
</ItemGroup>
2120

2221
<ItemGroup>
@@ -32,6 +31,7 @@
3231
</ItemGroup>
3332

3433
<ItemGroup>
34+
<PackageReference Include="MonoGame.Tool.Dxc" Version="1.8.2505.7" />
3535
<PackageReference Include="SharpDX" Version="4.0.1" />
3636
<PackageReference Include="SharpDX.D3DCompiler" Version="4.0.1" />
3737
</ItemGroup>

Tools/MonoGame.Effect.Compiler/Program.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public static int Main(string[] args)
3636

3737
// We don't support running MGFXC on Unix platforms
3838
// however Wine can be used to make it work so lets try that.
39-
if (Environment.OSVersion.Platform == PlatformID.Unix)
39+
if (Environment.OSVersion.Platform == PlatformID.Unix && options.Profile != ShaderProfile.Vulkan)
4040
{
4141
Environment.SetEnvironmentVariable("MGFXC_USE_WINE", "1");
4242
return WineHelper.Run(options);

build/Build.csproj

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,18 @@
3333
<PackageReference Include="Cake.FileHelpers" Version="7.0.0" />
3434
<PackageReference Include="Cake.Frosting" Version="5.0.0" />
3535
<PackageReference Include="Cake.Git" Version="4.0.0" />
36+
<PackageReference Include="Microsoft.Direct3D.DXC" Version="1.8.2505.32" />
37+
<PackageReference Include="MonoGame.Tool.Dxc" Version="1.8.2505.7" />
3638
<PackageReference Include="NuGet.Packaging" Version="6.11.1" />
3739
<PackageReference Include="System.Formats.Asn1" Version="9.0.0" />
3840
</ItemGroup>
3941

42+
<Target Name="CopyNuGetToolFiles" AfterTargets="Build">
43+
<ItemGroup>
44+
<ToolFiles Include="$(NuGetPackageRoot)Microsoft.Direct3D.DXC\1.8.2505.32\build\native\bin\x64\*.dll" />
45+
<ToolFiles Include="$(NuGetPackageRoot)Microsoft.Direct3D.DXC\1.8.2505.32\build\native\bin\x64\*.exe" />
46+
</ItemGroup>
47+
<Copy SourceFiles="@(ToolFiles)" DestinationFolder="$(OutputPath)" />
48+
</Target>
49+
4050
</Project>

build/BuildContext.cs

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1+
using Cake.Common.Tools.DotNet.Run;
12
using Cake.Git;
3+
using MonoGame.Tool;
4+
using System.Runtime.InteropServices;
25
using System.Text.RegularExpressions;
36

47
namespace BuildScripts;
@@ -84,11 +87,25 @@ public BuildContext(ICakeContext context) : base(context)
8487
Configuration = buildConfiguration
8588
};
8689

90+
DotNetRunSettings = new DotNetRunSettings
91+
{
92+
NoBuild = true,
93+
NoRestore = true,
94+
Configuration = "Release"
95+
};
96+
8797
Console.WriteLine($"Version: {Version}");
8898
Console.WriteLine($"RepositoryUrl: {repositoryUrl}");
8999
Console.WriteLine($"BuildConfiguration: {buildConfiguration}");
90100

91-
if (!context.IsRunningOnWindows())
101+
if (context.IsRunningOnWindows())
102+
{
103+
// SET PATH SO PROCESSESS CAN FIND DXC.EXE
104+
var pathEnv = System.Environment.GetEnvironmentVariable("PATH");
105+
pathEnv += ";" + AppDomain.CurrentDomain.BaseDirectory;
106+
System.Environment.SetEnvironmentVariable("PATH", pathEnv);
107+
}
108+
else
92109
{
93110
// SET MGFXC_WINE_PATH for building shaders on macOS and Linux
94111
System.Environment.SetEnvironmentVariable("MGFXC_WINE_PATH", context.EnvironmentVariable("HOME") + "/.winemonogame");
@@ -113,6 +130,8 @@ public BuildContext(ICakeContext context) : base(context)
113130

114131
public DotNetPublishSettings DotNetPublishSettingsForMac { get; }
115132

133+
public DotNetRunSettings DotNetRunSettings { get; }
134+
116135
public MSBuildSettings MSBuildSettings { get; }
117136

118137
public MSBuildSettings MSPackSettings { get; }
@@ -151,6 +170,16 @@ public void Shell(string command, string args)
151170
}
152171
}
153172

173+
public void DotNetRun(string project, string args, DirectoryPath? workingDir = null)
174+
{
175+
var mgfxc = System.IO.Path.Combine(Directory.GetCurrentDirectory(), project);
176+
DotNetRunSettings.WorkingDirectory = workingDir ?? "";
177+
this.DotNetRun(mgfxc, args, DotNetRunSettings);
178+
DotNetRunSettings.WorkingDirectory = "";
179+
}
180+
181+
public int DxcRun(string args) => RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? this.StartProcess("dxc.exe", args) : Dxc.Run(args, out _, out _);
182+
154183
public bool IsWorkloadInstalled(string workload)
155184
{
156185
this.StartProcess(

build/BuildFrameworksTasks/BuildAndroidTask.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
namespace BuildScripts;
33

44
[TaskName("Build Android")]
5+
[IsDependentOn(typeof(BuildShadersOGLTask))]
56
public sealed class BuildAndroidTask : FrostingTask<BuildContext>
67
{
78
public override bool ShouldRun(BuildContext context) => context.IsWorkloadInstalled("android");

build/BuildFrameworksTasks/BuildDesktopGLTask.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
namespace BuildScripts;
33

44
[TaskName("Build DesktopGL")]
5+
[IsDependentOn(typeof(BuildShadersOGLTask))]
56
public sealed class BuildDesktopGLTask : FrostingTask<BuildContext>
67
{
78
public override void Run(BuildContext context)

0 commit comments

Comments
 (0)