Skip to content

Commit

Permalink
WIP mellinoe#65, creating test compute shaders.
Browse files Browse the repository at this point in the history
  • Loading branch information
thargy committed May 19, 2018
1 parent 5b07502 commit 124c692
Show file tree
Hide file tree
Showing 6 changed files with 295 additions and 71 deletions.
63 changes: 45 additions & 18 deletions src/ShaderGen.Tests/ShaderBuiltinsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,45 +6,72 @@
using ShaderGen.Tests.Tools;
using TestShaders;
using Xunit;
using Xunit.Abstractions;

namespace ShaderGen.Tests
{
public class ShaderBuiltinsTests
{
private readonly ITestOutputHelper _output;

public ShaderBuiltinsTests(ITestOutputHelper output)
{
_output = output;
}

[Fact]
public void TestShaderBuiltins_Compute()
{
string csFunctionName = $"TestShaders.ShaderBuiltinsComputeTest.CS";
string csFunctionName = "TestShaders.ShaderBuiltinsComputeTest.CS";
Compilation compilation = TestUtil.GetTestProjectCompilation();

// Get backends for every toolchain that is available
LanguageBackend[] backends = new[] { ToolChain.Glsl330.CreateBackend(compilation) };
/* This is how yet get all, for now we're just using Glsl330
LanguageBackend[] backends = // new[] { ToolChain.Glsl330.CreateBackend(compilation) };
// This is how yet get all, for now we're just using Glsl330
ToolChain.All
.Where(t => t.IsAvailable)
.Select(t => t.CreateBackend(compilation))
.ToArray();
*/

ShaderSetProcessor processor = new ShaderSetProcessor();

ShaderGenerator sg = new ShaderGenerator(
compilation,
backends,
null, null, csFunctionName, processor);
ShaderGenerationResult genResult = sg.GenerateShaders();
/*
IReadOnlyList<GeneratedShaderSet> sets = genResult.GetOutput(backend);
Assert.Equal(1, sets.Count);
ShaderModel shaderModel = sets[0].Model;
Assert.Equal(2, shaderModel.Structures.Length);
Assert.Equal(3, shaderModel.AllResources.Length);
ShaderFunction vsEntry = shaderModel.GetFunction(csFunctionName);
Assert.Equal("VS", vsEntry.Name);
Assert.Single(vsEntry.Parameters);
Assert.True(vsEntry.IsEntryPoint);
Assert.Equal(ShaderFunctionType.VertexEntryPoint, vsEntry.Type);*/
null,
null,
csFunctionName,
processor);

ShaderGenerationResult generationResult = sg.GenerateShaders();

string spacer1 = new string('=', 80);
string spacer2 = new string('-', 80);

bool failed = false;
foreach (LanguageBackend backend in backends)
{
ToolChain toolChain = ToolChain.Get(backend);
GeneratedShaderSet set = generationResult.GetOutput(backend).Single();
_output.WriteLine(spacer1);
_output.WriteLine($"Generated shader set for {toolChain.Name} backend.");

ToolResult result = toolChain.Compile(set.ComputeShaderCode, Stage.Compute, set.ComputeFunction.Name);
if (result.HasError)
{
_output.WriteLine($"Failed to compile Compute Shader from set \"{set.Name}\"!");
_output.WriteLine(result.ToString());
failed = true;
}
else
_output.WriteLine($"Compiled Compute Shader from set \"{set.Name}\"!");

_output.WriteLine(string.Empty);
}

Assert.False(failed);
}

private class ShaderSetProcessor : IShaderSetProcessor
{
public string Result { get; private set; }
Expand Down
6 changes: 1 addition & 5 deletions src/ShaderGen.Tests/ShaderGen.Tests.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp2.0</TargetFramework>
Expand All @@ -22,10 +22,6 @@
</Content>
</ItemGroup>

<ItemGroup>
<Compile Remove="TestAssets\ShaderBuiltinsComputeTest.cs" />
</ItemGroup>

<ItemGroup>
<None Remove="xunit.runner.json" />
</ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion src/ShaderGen.Tests/ShaderGeneratorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ public void AllSetsEndToEnd()
failed = true;
}
else
_output.WriteLine($"Compiled Vertex Compute from set \"{set.Name}\"!");
_output.WriteLine($"Compiled Compute Shader from set \"{set.Name}\"!");
}
}

Expand Down
226 changes: 226 additions & 0 deletions src/ShaderGen.Tests/TestAssets/ComputeShaderParameters.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,226 @@
using System.Numerics;

namespace TestShaders
{
public struct ComputeShaderParameters
{
public /*readonly*/ Matrix4x4 P1Matrix;
public /*readonly*/ Matrix4x4 P2Matrix;

public /*readonly*/ Vector4 P1Vector4;
public /*readonly*/ Vector4 P2Vector4;
public /*readonly*/ Vector4 P3Vector4;
public /*readonly*/ Vector4 OutVector4;

public /*readonly*/ Vector3 P1Vector3;
public /*readonly*/ float P1Float;
public /*readonly*/ Vector3 P2Vector3;
public /*readonly*/ float P2Float;
public /*readonly*/ Vector3 P3Vector3;
public /*readonly*/ float P3Float;
public /*readonly*/ Vector3 OutVector3;
public /*readonly*/ float OutFloat;

public /*readonly*/ Vector2 P1Vector2;
public /*readonly*/ Vector2 P2Vector2;
public /*readonly*/ Vector2 P3Vector2;
public /*readonly*/ Vector2 OutVector2;


public /*readonly*/ bool OutFloatSet;
public /*readonly*/ bool OutVector2Set;
public /*readonly*/ bool OutVector3Set;
public /*readonly*/ bool OutVector4Set;

/*
public ComputeShaderParameters(
float p1Float = default(float),
float p2Float = default(float),
float p3Float = default(float),
Vector2 p1Vector2 = default(Vector2),
Vector2 p2Vector2 = default(Vector2),
Vector2 p3Vector2 = default(Vector2),
Vector3 p1Vector3 = default(Vector3),
Vector3 p2Vector3 = default(Vector3),
Vector3 p3Vector3 = default(Vector3),
Vector4 p1Vector4 = default(Vector4),
Vector4 p2Vector4 = default(Vector4),
Vector4 p3Vector4 = default(Vector4),
Matrix4x4 p1Matrix = default(Matrix4x4),
Matrix4x4 p2Matrix = default(Matrix4x4))
: this(
p1Float,
p2Float,
p3Float,
p1Vector2,
p2Vector2,
p3Vector2,
p1Vector3,
p2Vector3,
p3Vector3,
p1Vector4,
p2Vector4,
p3Vector4,
p1Matrix,
p2Matrix,
default(float),
default(Vector2),
default(Vector3),
default(Vector4),
false,
false,
false,
false)
{
}
private ComputeShaderParameters(
ComputeShaderParameters parameters,
float outFloat,
Vector2 outVector2,
Vector3 outVector3,
Vector4 outVector4,
bool outFloatSet,
bool outVector2Set,
bool outVector3Set,
bool outVector4Set)
: this(
parameters.P1Float,
parameters.P2Float,
parameters.P3Float,
parameters.P1Vector2,
parameters.P2Vector2,
parameters.P3Vector2,
parameters.P1Vector3,
parameters.P2Vector3,
parameters.P3Vector3,
parameters.P1Vector4,
parameters.P2Vector4,
parameters.P3Vector4,
parameters.P1Matrix,
parameters.P2Matrix,
outFloat,
outVector2,
outVector3,
outVector4,
outFloatSet,
outVector2Set,
outVector3Set,
outVector4Set)
{
}
private ComputeShaderParameters(
float p1Float,
float p2Float,
float p3Float,
Vector2 p1Vector2,
Vector2 p2Vector2,
Vector2 p3Vector2,
Vector3 p1Vector3,
Vector3 p2Vector3,
Vector3 p3Vector3,
Vector4 p1Vector4,
Vector4 p2Vector4,
Vector4 p3Vector4,
Matrix4x4 p1Matrix,
Matrix4x4 p2Matrix,
float outFloat,
Vector2 outVector2,
Vector3 outVector3,
Vector4 outVector4,
bool outFloatSet,
bool outVector2Set,
bool outVector3Set,
bool outVector4Set)
{
P1Float = p1Float;
P2Float = p2Float;
P3Float = p3Float;
P1Vector2 = p1Vector2;
P2Vector2 = p2Vector2;
P3Vector2 = p3Vector2;
P1Vector3 = p1Vector3;
P2Vector3 = p2Vector3;
P3Vector3 = p3Vector3;
P1Vector4 = p1Vector4;
P2Vector4 = p2Vector4;
P3Vector4 = p3Vector4;
P1Matrix = p1Matrix;
P2Matrix = p2Matrix;
OutFloat = outFloat;
OutVector2 = outVector2;
OutVector3 = outVector3;
OutVector4 = outVector4;
OutFloatSet = outFloatSet;
OutVector2Set = outVector2Set;
OutVector3Set = outVector3Set;
OutVector4Set = outVector4Set;
}
private ComputeShaderParameters(
ComputeShaderParameters parameters,
float outFloat,
Vector2 outVector2,
Vector3 outVector3,
Vector4 outVector4,
bool outFloatSet,
bool outVector2Set,
bool outVector3Set,
bool outVector4Set)
{
P1Float = parameters.P1Float;
P2Float = parameters.P2Float;
P3Float = parameters.P3Float;
P1Vector2 = parameters.P1Vector2;
P2Vector2 = parameters.P2Vector2;
P3Vector2 = parameters.P3Vector2;
P1Vector3 = parameters.P1Vector3;
P2Vector3 = parameters.P2Vector3;
P3Vector3 = parameters.P3Vector3;
P1Vector4 = parameters.P1Vector4;
P2Vector4 = parameters.P2Vector4;
P3Vector4 = parameters.P3Vector4;
P1Matrix = parameters.P1Matrix;
P2Matrix = parameters.P2Matrix;
OutFloat = outFloat;
OutVector2 = outVector2;
OutVector3 = outVector3;
OutVector4 = outVector4;
OutFloatSet = outFloatSet;
OutVector2Set = outVector2Set;
OutVector3Set = outVector3Set;
OutVector4Set = outVector4Set;
}
public ComputeShaderParameters Set(float outFloat)
=> new ComputeShaderParameters(this, outFloat, default(Vector2), default(Vector3), default(Vector4),
true, false, false, false);
/*
public ComputeShaderParameters Set(Vector2 outVector2)
=> new ComputeShaderParameters(this, default(float), outVector2, default(Vector3), default(Vector4),
false, true, false, false);
public ComputeShaderParameters Set(Vector3 outVector3)
=> new ComputeShaderParameters(this, default(float), default(Vector2), outVector3, default(Vector4),
false, false, true, false);
public ComputeShaderParameters Set(Vector4 outVector4)
=> new ComputeShaderParameters(this, default(float), default(Vector2), default(Vector3), outVector4,
false, false, false, true);
*/
}
}
Loading

0 comments on commit 124c692

Please sign in to comment.