Skip to content

Commit

Permalink
* added component wise vec/vec operator
Browse files Browse the repository at this point in the history
* changed HLSL cross translation system to use VisitorBase
* fixed a HLSL AST type resolver sync
* added HLSL emulation for asinh acosh and atanh variants
  • Loading branch information
WolfgangSt committed Jul 10, 2011
1 parent 6ac7047 commit 1c5b49d
Show file tree
Hide file tree
Showing 11 changed files with 222 additions and 51 deletions.
14 changes: 14 additions & 0 deletions IIS.SLSharp/Annotations/UndefinedBehaviorAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace IIS.SLSharp.Annotations
{
class UndefinedBehaviorAttribute: Attribute
{
public UndefinedBehaviorAttribute(string condition)
{
}
}
}
14 changes: 14 additions & 0 deletions IIS.SLSharp/Annotations/WarningAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace IIS.SLSharp.Annotations
{
internal class WarningAttribute: Attribute
{
public WarningAttribute(string warning)
{
}
}
}
3 changes: 3 additions & 0 deletions IIS.SLSharp/IIS.SLSharp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,14 @@
<Link>Properties\SolutionInfo.cs</Link>
</Compile>
<Compile Include="Annotations\FragmentShaderAttribute.cs" />
<Compile Include="Annotations\UndefinedBehaviorAttribute.cs" />
<Compile Include="Annotations\VertexInAttribute.cs" />
<Compile Include="Annotations\IShaderAttribute.cs" />
<Compile Include="Annotations\FragmentOutAttribute.cs" />
<Compile Include="Annotations\UniformAttribute.cs" />
<Compile Include="Annotations\VaryingAttribute.cs" />
<Compile Include="Annotations\VertexShaderAttribute.cs" />
<Compile Include="Annotations\WarningAttribute.cs" />
<Compile Include="Bindings\Binding.cs" />
<Compile Include="Bindings\IProgram.cs" />
<Compile Include="Bindings\ISLSharpBinding.cs" />
Expand Down Expand Up @@ -84,6 +86,7 @@
<Compile Include="Shaders\ShaderDefinition.SingleVector.cs" />
<Compile Include="ShaderType.cs" />
<Compile Include="SLSharpException.cs" />
<Compile Include="Translation\HLSL\Workarounds\Trigonometric.cs" />
<Compile Include="Translation\VisitorBase.Abstract.cs" />
<Compile Include="Translation\VisitorBase.cs" />
<Compile Include="Translation\VisitorBase.ShaderDef.cs" />
Expand Down
8 changes: 7 additions & 1 deletion IIS.SLSharp/Shaders/ShaderDefinition.SingleVector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ public sealed class vec2

public static vec2 operator /(vec2 v1, float s) { throw _invalidAccess; }

public static vec2 operator /(vec2 v1, vec2 v2) { throw _invalidAccess; }

public static vec2 operator -(vec2 v) { throw _invalidAccess; }

public vec2(float xy) { throw _invalidAccess; }
Expand Down Expand Up @@ -121,6 +123,8 @@ public sealed class vec3

public static vec3 operator /(vec3 v, float d) { throw _invalidAccess; }

public static vec3 operator /(vec3 v, vec3 d) { throw _invalidAccess; }

public static vec3 operator -(vec3 v, float d) { throw _invalidAccess; }

public static vec3 operator +(vec3 v, float d) { throw _invalidAccess; }
Expand Down Expand Up @@ -226,7 +230,9 @@ public sealed class vec4

public static vec4 operator *(mat4 m, vec4 v) { throw _invalidAccess; }

public static vec2 operator /(vec4 v, float s) { throw _invalidAccess; }
public static vec4 operator /(vec4 v, float s) { throw _invalidAccess; }

public static vec4 operator /(vec4 v, vec4 s) { throw _invalidAccess; }

public static vec4 operator +(vec4 v, float s) { throw _invalidAccess; }

Expand Down
12 changes: 11 additions & 1 deletion IIS.SLSharp/Shaders/ShaderDefinition.Trigonometry.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
namespace IIS.SLSharp.Shaders
using IIS.SLSharp.Annotations;

namespace IIS.SLSharp.Shaders
{
public abstract partial class ShaderDefinition
{
Expand Down Expand Up @@ -354,27 +356,31 @@ public abstract partial class ShaderDefinition
/// </summary>
/// <remarks>Results are undefined if x &lt; 1.</remarks>
/// <returns>Returns the arc hyperbolic cosine of x.</returns>
[UndefinedBehavior("x < 1")]
protected internal static float Acosh(float x) { throw _invalidAccess; }

/// <summary>
/// Acosh returns the arc hyperbolic cosine of x; the non-negative inverse of cosh.
/// </summary>
/// <remarks>Results are undefined if x &lt; 1.</remarks>
/// <returns>Returns the arc hyperbolic cosine of x.</returns>
[UndefinedBehavior("x < 1")]
protected internal static vec2 Acosh(vec2 x) { throw _invalidAccess; }

/// <summary>
/// Acosh returns the arc hyperbolic cosine of x; the non-negative inverse of cosh.
/// </summary>
/// <remarks>Results are undefined if x &lt; 1.</remarks>
/// <returns>Returns the arc hyperbolic cosine of x.</returns>
[UndefinedBehavior("x < 1")]
protected internal static vec3 Acosh(vec3 x) { throw _invalidAccess; }

/// <summary>
/// Acosh returns the arc hyperbolic cosine of x; the non-negative inverse of cosh.
/// </summary>
/// <remarks>Results are undefined if x &lt; 1.</remarks>
/// <returns>Returns the arc hyperbolic cosine of x.</returns>
[UndefinedBehavior("x < 1")]
protected internal static vec4 Acosh(vec4 x) { throw _invalidAccess; }

#endregion
Expand All @@ -386,27 +392,31 @@ public abstract partial class ShaderDefinition
/// </summary>
/// <remarks>Results are undefined if |x| &gt;= 1.</remarks>
/// <returns>Returns the arc hyperbolic tangent of x.</returns>
[UndefinedBehavior("x >= 1")]
protected internal static float Atanh(float x) { throw _invalidAccess; }

/// <summary>
/// Atanh returns the arc hyperbolic tangent of x; the inverse of Tanh.
/// </summary>
/// <remarks>Results are undefined if |x| &gt;= 1.</remarks>
/// <returns>Returns the arc hyperbolic tangent of x.</returns>
[UndefinedBehavior("x >= 1")]
protected internal static vec2 Atanh(vec2 x) { throw _invalidAccess; }

/// <summary>
/// Atanh returns the arc hyperbolic tangent of x; the inverse of Tanh.
/// </summary>
/// <remarks>Results are undefined if |x| &gt;= 1.</remarks>
/// <returns>Returns the arc hyperbolic tangent of x.</returns>
[UndefinedBehavior("x >= 1")]
protected internal static vec3 Atanh(vec3 x) { throw _invalidAccess; }

/// <summary>
/// Atanh returns the arc hyperbolic tangent of x; the inverse of Tanh.
/// </summary>
/// <remarks>Results are undefined if |x| &gt;= 1.</remarks>
/// <returns>Returns the arc hyperbolic tangent of x.</returns>
[UndefinedBehavior("x >= 1")]
protected internal static vec4 Atanh(vec4 x) { throw _invalidAccess; }

#endregion
Expand Down
28 changes: 26 additions & 2 deletions IIS.SLSharp/Translation/HLSL/HlslTransform.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,28 @@ public void ResetState()
_functions.Clear();
}

private bool SameMethod(MethodDefinition m, IMethod n, ITypeResolveContext ctx)
{
if (m.Name != n.Name)
return false;

if (n.ReturnType.Resolve(ctx).FullName != m.ReturnType.FullName.Replace('/', '.'))
return false;

if (n.Parameters.Count != m.Parameters.Count)
return false;

for (var i = 0; i < n.Parameters.Count; i++)
{
var pn = n.Parameters[i];
var pm = m.Parameters[i];
if (pn.Type.Resolve(ctx).FullName != pm.ParameterType.FullName.Replace('/', '.'))
return false;
}

return true;
}

/// <summary>
/// Public translation interface.
/// Translates the given method to HLSL
Expand Down Expand Up @@ -67,7 +89,7 @@ public FunctionDescription Transform(TypeDefinition s, MethodDefinition m, Custo
// TODO: need a more sane way to get the correct class + member
var ss = ctx.GetAllClasses().First(c => c.FullName == s.FullName);
resolver.CurrentTypeDefinition = ss;
resolver.CurrentMember = ss.Members.First(n => m.Name == n.Name);
resolver.CurrentMember = ss.Methods.First(n => SameMethod(m, n, ctx));

var rv = new ResolveVisitor(resolver, null, null);

Expand All @@ -89,9 +111,11 @@ public List<string> ForwardDeclare(bool debugInfo)
return _functions.Select(f => f.Item1 + ";" + (debugInfo ? " // " + f.Item2 : string.Empty)).ToList();
}

private Shader[] _workaroundDependencies;

public IEnumerable<Shader> WorkaroundDependencies
{
get { return Enumerable.Empty<Shader>(); }
get { return _workaroundDependencies ?? (_workaroundDependencies = new Shader[] { Shader.CreateInstance<Workarounds.Trigonometric>() }); }
}
}
}
45 changes: 12 additions & 33 deletions IIS.SLSharp/Translation/HLSL/HlslVisitor.ShaderDef.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,20 +83,20 @@ public HlslVisitor()
{ () => ShaderDefinition.Tanh(vec3), ToLower },
{ () => ShaderDefinition.Tanh(vec4), ToLower },

{ () => ShaderDefinition.Asinh(_float), EmulateAsinh },
{ () => ShaderDefinition.Asinh(vec2), EmulateAsinh },
{ () => ShaderDefinition.Asinh(vec3), EmulateAsinh },
{ () => ShaderDefinition.Asinh(vec4), EmulateAsinh },
{ () => ShaderDefinition.Asinh(_float), Redirect<Workarounds.Trigonometric>() },
{ () => ShaderDefinition.Asinh(vec2), Redirect<Workarounds.Trigonometric>() },
{ () => ShaderDefinition.Asinh(vec3), Redirect<Workarounds.Trigonometric>() },
{ () => ShaderDefinition.Asinh(vec4), Redirect<Workarounds.Trigonometric>() },

{ () => ShaderDefinition.Acosh(_float), EmulateAcosh },
{ () => ShaderDefinition.Acosh(vec2), EmulateAcosh },
{ () => ShaderDefinition.Acosh(vec3), EmulateAcosh },
{ () => ShaderDefinition.Acosh(vec4), EmulateAcosh },
{ () => ShaderDefinition.Acosh(_float), Redirect<Workarounds.Trigonometric>() },
{ () => ShaderDefinition.Acosh(vec2), Redirect<Workarounds.Trigonometric>() },
{ () => ShaderDefinition.Acosh(vec3), Redirect<Workarounds.Trigonometric>() },
{ () => ShaderDefinition.Acosh(vec4), Redirect<Workarounds.Trigonometric>() },

{ () => ShaderDefinition.Atanh(_float), EmulateAtanh },
{ () => ShaderDefinition.Atanh(vec2), EmulateAtanh },
{ () => ShaderDefinition.Atanh(vec3), EmulateAtanh },
{ () => ShaderDefinition.Atanh(vec4), EmulateAtanh },
{ () => ShaderDefinition.Atanh(_float), Redirect<Workarounds.Trigonometric>() },
{ () => ShaderDefinition.Atanh(vec2), Redirect<Workarounds.Trigonometric>() },
{ () => ShaderDefinition.Atanh(vec3), Redirect<Workarounds.Trigonometric>() },
{ () => ShaderDefinition.Atanh(vec4), Redirect<Workarounds.Trigonometric>() },

{ () => ShaderDefinition.SinCos(_float, out _float, out _float), ToLower },
{ () => ShaderDefinition.SinCos(vec2, out vec2, out vec2), ToLower },
Expand Down Expand Up @@ -159,26 +159,5 @@ private StringBuilder ModFloat<T>(MethodDefinition m, InvocationExpression i)

return result;
}

private StringBuilder EmulateAsinh(MethodDefinition m, InvocationExpression i)
{
Warn("HLSL does not support a native Asinh, emulating with precision loss.");
// TODO: emulate as ln(x + sqrt(1 + x*x))
throw new NotImplementedException();
}

private StringBuilder EmulateAcosh(MethodDefinition m, InvocationExpression i)
{
Warn("HLSL does not support a native Acosh, emulating with precision loss.");
// TODO: emulate as 2*ln(sqrt((x+1)/2) + sqrt((x-1)/2))
throw new NotImplementedException();
}

private StringBuilder EmulateAtanh(MethodDefinition m, InvocationExpression i)
{
Warn("HLSL does not support a native Atanh, emulating with precision loss.");
// TODO: emulate as (ln(1+x) - ln(1-x))/2
throw new NotImplementedException();
}
}
}
12 changes: 0 additions & 12 deletions IIS.SLSharp/Translation/HLSL/HlslVisitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,10 @@ public IEnumerable<Tuple<string, string>> Functions
get { return _functions; }
}

private static void ValidateComplexType(TypeReference type)
{
var typeDef = type.Resolve();
var declType = typeDef.DeclaringType;

if (declType != null && declType.MetadataToken.ToInt32() == typeof(ShaderDefinition).MetadataToken)
return;

throw new SLSharpException(type.FullName + " is invalid in a shader program.");
}

public static void ValidateType(TypeReference t)
{
t.ToHlsl(); // throws when t is invalid
}


public static string ToHlslParamType(ParameterDefinition p)
{
Expand Down
Loading

0 comments on commit 1c5b49d

Please sign in to comment.