-
-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Engine: Add new ShaderCompiler for build shader compiling (pretty har…
…dcoded now)
- Loading branch information
1 parent
f38c8ab
commit 4fffe65
Showing
13 changed files
with
190 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<Project> | ||
<Import Project="$(MSBuildThisFileDirectory)..\Directory.Build.props" /> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<Project> | ||
<Import Project="$(MSBuildThisFileDirectory)..\Directory.Build.targets" /> | ||
|
||
<PropertyGroup> | ||
<_ShaderCompilerPath>$(AlimerSrcDirectory)assets/Alimer.ShaderCompiler/bin/$(Configuration)/net8.0/Alimer.ShaderCompiler.dll</_ShaderCompilerPath> | ||
<_ProcessDir>$(BaseIntermediateOutputPath)/CompiledShaders</_ProcessDir> | ||
</PropertyGroup> | ||
|
||
<Target Name="ProcessShaders" AfterTargets="AssignTargetPaths" Condition="'@(Shader)' != ''"> | ||
<PropertyGroup> | ||
<_Command>dotnet $(_ShaderCompilerPath)</_Command> | ||
<_Command>$(_Command) $(_ProcessDir)</_Command> | ||
<_Command>$(_Command) @(Shader->'%(Identity)', ' ')</_Command> | ||
</PropertyGroup> | ||
<Message Importance="high" Text="Executing: $(_Command)" /> | ||
<Exec Command="$(_Command)" /> | ||
<ItemGroup> | ||
<_ProcessedAsset Include="$(_ProcessDir)/*.bin" /> | ||
<EmbeddedResource Include="@(_ProcessedAsset)"> | ||
<LogicalName>%(FileName)%(Extension)</LogicalName> | ||
</EmbeddedResource> | ||
</ItemGroup> | ||
|
||
</Target> | ||
|
||
<ItemGroup Condition=" '$(_AlimerNoTargetPlatform)' == 'True' "> | ||
<ProjectReference Include="$(AlimerSrcDirectory)assets/Alimer.ShaderCompiler/Alimer.ShaderCompiler.csproj"> | ||
<ReferenceOutputAssembly>false</ReferenceOutputAssembly> | ||
<SkipGetTargetFrameworkProperties>true</SkipGetTargetFrameworkProperties> | ||
</ProjectReference> | ||
</ItemGroup> | ||
|
||
</Project> |
14 changes: 14 additions & 0 deletions
14
src/assets/Alimer.ShaderCompiler/Alimer.ShaderCompiler.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net8.0</TargetFramework> | ||
<OutputType>Exe</OutputType> | ||
<IsPackable>false</IsPackable> | ||
<RootNamespace>Alimer.Shaders</RootNamespace> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\Alimer.Shaders\Alimer.Shaders.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
// Copyright (c) Amer Koleci and Contributors. | ||
// Licensed under the MIT License (MIT). See LICENSE in the repository root for more information. | ||
|
||
using Alimer.Graphics; | ||
using Alimer.Shaders; | ||
|
||
namespace Alimer.Shaders; | ||
|
||
class Program | ||
{ | ||
public static int Main(string[] args) | ||
{ | ||
string outputDirectory = args[0]; | ||
if (!Directory.Exists(outputDirectory)) | ||
{ | ||
Directory.CreateDirectory(outputDirectory); | ||
} | ||
//else | ||
//{ | ||
// Directory.Delete(outputDirectory, true); | ||
// Directory.CreateDirectory(outputDirectory); | ||
//} | ||
|
||
for (int i = 1; i < args.Length; i++) | ||
{ | ||
string arg = args[i]; | ||
|
||
Console.WriteLine($"Compile SPIRV: {arg}"); | ||
Compile(outputDirectory, arg, ShaderFormat.SPIRV, ShaderStages.Vertex, "vertexMain"); | ||
Compile(outputDirectory, arg, ShaderFormat.SPIRV, ShaderStages.Fragment, "fragmentMain"); | ||
|
||
Console.WriteLine($"Compile DXIL: {arg}"); | ||
Compile(outputDirectory, arg, ShaderFormat.DXIL, ShaderStages.Vertex, "vertexMain"); | ||
Compile(outputDirectory, arg, ShaderFormat.DXIL, ShaderStages.Fragment, "fragmentMain"); | ||
} | ||
|
||
|
||
return 0; | ||
} | ||
|
||
private static void Compile(string outputDirectory, string shaderSourceFileName, | ||
ShaderFormat format, ShaderStages stage, string entryPoint) | ||
{ | ||
string shadersPath = Path.GetDirectoryName(shaderSourceFileName); | ||
string shaderSource = File.ReadAllText(shaderSourceFileName); | ||
|
||
ShaderCompilationOptions options = new() | ||
{ | ||
SourceFileName = shaderSourceFileName, | ||
ShaderStage = stage, | ||
EntryPoint = entryPoint, | ||
IncludeDirs = | ||
{ | ||
shadersPath | ||
}, | ||
}; | ||
|
||
//f (defines != null && defines.Count > 0) | ||
// | ||
// foreach (KeyValuePair<string, string> pair in defines) | ||
// { | ||
// options.Defines.Add(pair.Key, pair.Value); | ||
// } | ||
// | ||
|
||
using ShaderCompilationResult result = ShaderCompiler.Instance.Compile(format, shaderSource, in options); | ||
if (result.Failed) | ||
{ | ||
Console.Error.WriteLine($"Shader compilation failed: {result.ErrorMessage}."); | ||
return; | ||
//throw new GraphicsException(result.ErrorMessage); | ||
} | ||
|
||
string shaderFile = Path.GetFileNameWithoutExtension(shaderSourceFileName); | ||
string outputFile = Path.GetFileNameWithoutExtension(shaderFile) + "_" + options.EntryPoint + "_" + result.Format.ToString().ToLower(); | ||
switch (result.Format) | ||
{ | ||
case ShaderFormat.DXIL: | ||
//outputFile = Path.ChangeExtension(outputFile, ".cso"); | ||
outputFile = Path.ChangeExtension(outputFile, ".bin"); | ||
break; | ||
case ShaderFormat.SPIRV: | ||
//outputFile = Path.ChangeExtension(outputFile, ".spv"); | ||
outputFile = Path.ChangeExtension(outputFile, ".bin"); | ||
break; | ||
} | ||
|
||
File.WriteAllBytes(Path.Combine(outputDirectory, outputFile), result.GetByteCode().ToArray()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters