Permalink
Browse files

Removed definition gl_Vertex

New syntax is [Varying(UsageSementic.Position0)] vec4 outName

Updated all samples.
Updates unit test framework.
  • Loading branch information...
1 parent b0aa7e9 commit e67b68cbfdd857cf4d67b4ddfaeea81b88ba68dc @WolfgangSt WolfgangSt committed Oct 19, 2011
@@ -44,6 +44,9 @@ public abstract class ClipmapShader : Shader
[Varying]
public vec2 _uv;
+ [Varying(UsageSemantic.Position0)]
+ private vec4 _position;
+
[Varying]
public float _z;
@@ -84,7 +87,7 @@ public void ClipmapVertexMain()
var worldPosFinal = new vec4(worldPos, _z * 0.3f, 1.0f);
_finalPos = ModelViewProjectionMatrix * worldPosFinal;
- gl_Position = _finalPos;
+ _position = _finalPos;
// just for testing, derive normal using interpolation over heights
@@ -24,6 +24,9 @@ public abstract class SimpleShader : Shader
[Varying]
private vec2 _uv;
+ [Varying(UsageSemantic.Position0)]
+ private vec4 _position;
+
[VertexIn(UsageSemantic.Position0)]
public vec4 Vertex;
@@ -44,7 +47,7 @@ protected void FragmentMain()
public void VertexMain()
{
_uv = Texcoord;
- gl_Position = ModelviewProjection * Vertex;
+ _position = ModelviewProjection * Vertex;
}
// resource setup code
@@ -21,6 +21,9 @@ public abstract class BackdropShader : Shader
[Varying]
private vec2 _uv;
+ [Varying(UsageSemantic.Position0)]
+ private vec4 _position;
+
[FragmentOut(UsageSemantic.Color0)]
protected vec4 Color;
@@ -40,7 +43,7 @@ protected void FragmentMain()
public void VertexMain()
{
_uv = GlVertex.xy * 0.5f * Scale * MaxScale;
- gl_Position = GlVertex;
+ _position = GlVertex;
}
// resource setup code
@@ -8,6 +8,9 @@ public abstract class CubeShader : Shader
[Varying]
private vec3 _uvw;
+ [Varying(UsageSemantic.Position0)]
+ private vec4 _position;
+
[VertexIn(UsageSemantic.Position0)]
public vec4 Vertex;
@@ -31,7 +34,7 @@ protected void FragmentMain()
public void VertexMain()
{
_uvw = (Vertex.xyz + new vec3(1.0f)) * 0.5f;
- gl_Position = ModelViewProjectionMatrix * Vertex;
+ _position = ModelViewProjectionMatrix * Vertex;
}
// resource setup code
@@ -46,6 +46,9 @@ public abstract class ClipmapShader : Shader
[FragmentOut(UsageSemantic.Color0)]
public vec4 FragColor;
+ [Varying(UsageSemantic.Position0)]
+ private vec4 _position;
+
[VertexShader(true)]
public void ClipmapVertexMain()
{
@@ -74,7 +77,7 @@ public void ClipmapVertexMain()
var worldPosFinal = new vec4(worldPos, _z * 0.3f, 1.0f);
_finalPos = ModelViewProjectionMatrix * worldPosFinal;
- gl_Position = _finalPos;
+ _position = _finalPos;
// just for testing, derive normal using interpolation over heights
@@ -38,6 +38,9 @@ public Texture2D HeightmapTex
[Varying]
public vec2 _uv;
+ [Varying(UsageSemantic.Position0)]
+ private vec4 _position;
+
[FragmentShader(true)]
public void DebugFragmentMain()
{
@@ -67,7 +70,7 @@ public void DebugFragmentMain()
[VertexShader(true)]
public void FragmentMain()
{
- gl_Position = Vertex;
+ _position = Vertex;
_uv = Vertex.xy * 0.5f + new vec2(0.5f);
}
@@ -45,6 +45,9 @@ public abstract class ClipmapShader : Shader
[Varying]
public vec2 _uv;
+ [Varying(UsageSemantic.Position0)]
+ private vec4 _position;
+
[Varying]
public float _z;
@@ -85,7 +88,7 @@ public void ClipmapVertexMain()
var worldPosFinal = new vec4(worldPos, _z * 0.3f, 1.0f);
_finalPos = ModelViewProjectionMatrix * worldPosFinal;
- gl_Position = _finalPos;
+ _position = _finalPos;
// just for testing, derive normal using interpolation over heights
@@ -24,6 +24,9 @@ public abstract class SimpleShader : Shader
[Varying]
private vec2 _uv;
+ [Varying(UsageSemantic.Position0)]
+ private vec4 _position;
+
[VertexIn(UsageSemantic.Position0)]
public vec4 Vertex;
@@ -44,7 +47,7 @@ protected void FragmentMain()
public void VertexMain()
{
_uv = Texcoord;
- gl_Position = ModelviewProjection * Vertex;
+ _position = ModelviewProjection * Vertex;
}
// resource setup code
@@ -18,6 +18,9 @@ public abstract class MyShader : Shader
[FragmentOut(UsageSemantic.Color0)]
public vec4 Color;
+ [Varying(UsageSemantic.Position0)]
+ public vec4 Position;
+
// demonstrate using a shared library
public InvertShader Invert { get; private set; }
@@ -32,7 +35,7 @@ protected void FragmentMain()
public void VertexMain()
{
_uv = (Vertex.xy + new vec2(1.0f)) * 0.5f;
- gl_Position = Vertex;
+ Position = Vertex;
}
// resource setup code
@@ -15,6 +15,9 @@ public abstract class SimpleShader : Shader
[Varying]
private vec2 _fragNormal;
+ [Varying(UsageSemantic.Position0)]
+ private vec4 _position;
+
[VertexIn(UsageSemantic.Position0)]
public vec4 Vertex;
@@ -34,7 +37,7 @@ protected void FragmentMain()
public void VertexMain()
{
_fragNormal = Normal;
- gl_Position = ModelviewProjection * Vertex;
+ _position = ModelviewProjection * Vertex;
}
// resource setup code
@@ -20,10 +20,13 @@ internal sealed class ShaderBuilder
private FieldBuilder _output;
+ private FieldBuilder _position;
+
public ShaderBuilder(TypeBuilder type)
{
_type = type;
+ DefinePosition();
DefineConstructor();
DefineVertexMain();
DefineFragmentMain();
@@ -101,6 +104,18 @@ private void DefineOutput()
#endregion
+ #region DefinePosition
+
+ private void DefinePosition()
+ {
+ var ctor = typeof(VaryingAttribute).GetConstructor(new[] { typeof(UsageSemantic) });
+ var positionOutAttrib = new CustomAttributeBuilder(ctor, new object[] { UsageSemantic.Position0 });
+ _position = _type.DefineField("position", typeof(ShaderDefinition.vec4), FieldAttributes.Public);
+ _position.SetCustomAttribute(positionOutAttrib);
+ }
+
+ #endregion
+
#region DefineInput
private PropertyInfo DefineInput(Expression input, string name)
@@ -135,7 +150,7 @@ public void DefineVertexBody()
_vertexBody.Emit(OpCodes.Ldarg_0);
_vertexBody.Emit(OpCodes.Ldc_R4, 0.0f);
_vertexBody.Emit(OpCodes.Newobj, typeof(ShaderDefinition.vec4).GetConstructor(new[] { typeof(float) }));
- _vertexBody.Emit(OpCodes.Stfld, typeof(Shader).GetField("gl_Position", BindingFlags.Instance | BindingFlags.NonPublic));
+ _vertexBody.Emit(OpCodes.Stfld, _position);
_vertexBody.Emit(OpCodes.Ret);
}
@@ -5,5 +5,12 @@ namespace IIS.SLSharp.Annotations
[AttributeUsage(AttributeTargets.Field, AllowMultiple = false)]
public sealed class VaryingAttribute : ShaderVariableAttribute
{
+ public VaryingAttribute()
+ {
+ }
+
+ public VaryingAttribute(UsageSemantic hint)
+ {
+ }
}
}
@@ -99,7 +99,8 @@ private List<VariableDescription> CollectVaryings()
let type = TypeMap[field.FieldType.Resolve().MetadataToken.ToInt32()].Type
let name = GetVaryingName(field)
let comment = DebugMode ? " // " + field.DeclaringType.FullName + "." + field.Name : string.Empty
- select new VariableDescription(type, name, UsageSemantic.Unknown, comment)).ToList();
+ let semantic = attr.HasConstructorArguments ? (UsageSemantic)attr.ConstructorArguments[0].Value : UsageSemantic.Unknown
+ select new VariableDescription(type, name, semantic, comment)).ToList();
}
/// <summary>
@@ -8,13 +8,5 @@ namespace IIS.SLSharp.Shaders
public abstract partial class ShaderDefinition
{
private static readonly Exception _invalidAccess = new InvalidOperationException("Invalid usage");
-
- // ReSharper disable InconsistentNaming
- // ReSharper disable UnusedParameter.Local
-
- protected vec4 gl_Position;
-
- // ReSharper restore InconsistentNaming
- // ReSharper restore UnusedParameter.Local
}
}
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Text;
+using System.Linq;
using IIS.SLSharp.Annotations;
using IIS.SLSharp.Descriptions;
using IIS.SLSharp.Shaders;
@@ -113,18 +114,7 @@ public static string ToGlsl(this SourceDescription desc, ShaderType type)
if (type == ShaderType.FragmentShader)
desc.FragmentOuts.ForEach(
- v =>
- {
- if (v.Semantic == UsageSemantic.Depth)
- {
- // validate type and let SL# throw rather than generate invalid GL code
- if (v.Type != typeof(float))
- throw new SLSharpException(v.Name + "'s must be float, as it is marked as fragment depth!");
- s.AppendFormat("#define {0} gl_FragDepth {1}", v.Name, v.Comment).Append(Environment.NewLine);
- return;
- }
- s.AppendFormat("out {0} {1};{2}", v.Type.ToGlsl(), v.Name, v.Comment).Append(Environment.NewLine);
- });
+ v => s.AppendFormat("out {0} {1};{2}", v.Type.ToGlsl(), v.Name, v.Comment).Append(Environment.NewLine));
s.AppendLine();
desc.ForwardDecl.ForEach(v => s.AppendLine(v));
@@ -133,6 +123,38 @@ public static string ToGlsl(this SourceDescription desc, ShaderType type)
desc.Functions.ForEach(v => s.AppendLine(v.Body));
+ var entry = desc.Functions.FirstOrDefault(x => x.Type == type && x.EntryPoint);
+ if (entry != null)
+ {
+ s.AppendLine("void main()").AppendLine("{");
+ s.AppendLine(" " + entry.Name + "();");
+ if (type == ShaderType.VertexShader)
+ {
+ var glPosition = desc.Varyings.FirstOrDefault(x => x.Semantic == UsageSemantic.Position0);
+ // glPosition write is mandatory in a vs.
+ // In theory a lib could supply this, we dont support this with the new syntax anymore at the moment tho.
+ // in order for support we'd need to scan over all dependency varyings as well.
+ if (glPosition == null)
+ throw new SLSharpException("Vertexshader must output a Position");
+ s.AppendLine(" gl_Position = " + glPosition.Name + ";");
+ }
+
+ if (type == ShaderType.FragmentShader)
+ {
+ var glFragDepth = desc.FragmentOuts.FirstOrDefault(x => x.Semantic == UsageSemantic.Depth);
+
+ if (glFragDepth != null)
+ {
+ // validate type and let SL# throw rather than generate invalid GL code
+ if (glFragDepth.Type != typeof(float))
+ throw new SLSharpException(glFragDepth.Name + "'s must be float, as it is marked as fragment depth!");
+ s.AppendLine(" gl_FragDepth = " + glFragDepth.Name + ";");
+ }
+ }
+
+ s.AppendLine("}");
+ }
+
var src = s.ToString();
Console.WriteLine(src);
@@ -56,11 +56,10 @@ public void ResetState()
_functions.UnionWith(glsl.Functions);
+ var sig = GlslVisitor.GetSignature(m);
var entry = (bool)attr.ConstructorArguments.FirstOrDefault().Value;
- var sig = entry ? "void main()" : GlslVisitor.GetSignature(m);
-
var code = glsl.Result;
- var desc = new FunctionDescription(entry ? "main" : Shader.GetMethodName(m), sig + code, entry, type);
+ var desc = new FunctionDescription(Shader.GetMethodName(m), sig + code, entry, type);
_dependencies.UnionWith(glsl.Dependencies);
Oops, something went wrong.

0 comments on commit e67b68c

Please sign in to comment.