Skip to content

Commit

Permalink
various updates to Axiom binding + sample app => we finally have some…
Browse files Browse the repository at this point in the history
… graphicaloutput.

It's still quite hacky tho
  • Loading branch information
WolfgangSt committed Apr 28, 2011
1 parent fe234d5 commit 3480480
Show file tree
Hide file tree
Showing 7 changed files with 103 additions and 30 deletions.
49 changes: 42 additions & 7 deletions IIS.SLSharp.Bindings.Axiom/GLSL/Bindings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using Axiom.Core;
using Axiom.Graphics;
using Axiom.RenderSystems.OpenGL.GLSL;
using IIS.SLSharp.Annotations;
using IIS.SLSharp.Descriptions;
using IIS.SLSharp.Reflection;
using Axiom.Math;
Expand Down Expand Up @@ -59,7 +60,8 @@ public object Compile(Shader shader, ShaderType type, SourceDescription source)

unit.Source = source.ToGlsl(type);

return new System.Tuple<ShaderType, GLSLProgram>(type, (GLSLProgram)unit);
return new System.Tuple<ShaderType, GLSLProgram, SourceDescription>(
type, (GLSLProgram)unit, source);
}


Expand All @@ -68,11 +70,14 @@ public IProgram Link(Shader shader, IEnumerable<object> u)
GLSLProgram vs;
GLSLProgram ps;

var units = u.Cast<System.Tuple<ShaderType, GLSLProgram>>();
var units = u.Cast<System.Tuple<ShaderType, GLSLProgram, SourceDescription>>();
var name = shader.GetType().FullName;

const string group = ResourceGroupManager.DefaultResourceGroupName;


var vertexIns = units.Where(v => v.Item1 == ShaderType.VertexShader).Aggregate(
SourceDescription.Empty, (current, vu) => current.Merge(vu.Item3)).VertexIns;

var verts = units.Where(v => v.Item1 == ShaderType.VertexShader).Select(v => v.Item2);
if (verts.Count() > 1)
{
Expand Down Expand Up @@ -100,7 +105,7 @@ public IProgram Link(Shader shader, IEnumerable<object> u)
else
ps = frags.FirstOrDefault();

return new Program(vs, ps);
return new Program(vs, ps, vertexIns);
}

public void Initialize()
Expand Down Expand Up @@ -135,8 +140,12 @@ public class Program : IProgram

private readonly GLSLLinkProgram.UniformReferenceList _uniforms;

public Program(GLSLProgram vs, GLSLProgram ps)
private readonly List<VariableDescription> _vertexIns;

public Program(GLSLProgram vs, GLSLProgram ps, List<VariableDescription> vertexIns)
{
_vertexIns = vertexIns;

VertexShader = vs;
PixelShader = ps;

Expand All @@ -157,6 +166,32 @@ public Program(GLSLProgram vs, GLSLProgram ps)

public void Activate()
{
// HACK: assign attribute semantics
// axiom doesnt know about attributes yet :(

foreach (var v in _vertexIns)
{
//var id = Gl.glGetAttribLocationARB(Prog.GLHandle, v.Name);
int id;
switch (v.Semantic)
{
case UsageSemantic.Position0: id = 0; break;
case UsageSemantic.Normal0: id = 2; break;
case UsageSemantic.Color0: id = 3; break;
case UsageSemantic.Color1: id = 4; break;
case UsageSemantic.Texcoord0: id = 8; break;
case UsageSemantic.Texcoord1: id = 9; break;
case UsageSemantic.Texcoord2: id = 10; break;
case UsageSemantic.Texcoord3: id = 11; break;
case UsageSemantic.Texcoord4: id = 12; break;
case UsageSemantic.Texcoord5: id = 13; break;
case UsageSemantic.Texcoord6: id = 14; break;
case UsageSemantic.Texcoord7: id = 15; break;
default:
throw new SLSharpException("Axiom does not support " + v.Semantic + " yet for OpenGL.");
}
Gl.glBindAttribLocation(Prog.GLHandle, id, v.Name);
}
CurrentProgram = this;
}

Expand Down Expand Up @@ -284,7 +319,7 @@ public static int GetLocation(int program, string name)

public static void Uniform1F(int location, float value)
{
throw new NotImplementedException();
Tao.OpenGl.Gl.glUniform1f(location, value);
//GL.Uniform1(location, value);
}

Expand Down Expand Up @@ -346,7 +381,7 @@ public unsafe static void UniformMatrix4X4(int location)
// HACK: Axiom doesnt know about glUniformMatrix* -_-

fixed (Matrix4* v = &_storage.F4X4)
Tao.OpenGl.Gl.glUniformMatrix4fv(location, 1, 0, new IntPtr(v));
Tao.OpenGl.Gl.glUniformMatrix4fv(location, 1, 1, new IntPtr(v));
//Tao.OpenGl.Gl.glUniformMatrix4fv(location, 1, false, storage.F4X4);
}

Expand Down
5 changes: 3 additions & 2 deletions IIS.SLSharp.Bindings.OpenTK/Bindings.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Linq;
using IIS.SLSharp.Bindings.OpenTK.Textures;
using IIS.SLSharp.Descriptions;
using IIS.SLSharp.Reflection;
Expand Down Expand Up @@ -83,12 +84,12 @@ public object Compile(Shader s, ShaderType type, SourceDescription source)
if (info != string.Empty)
Console.WriteLine(info);

return shader;
return new Tuple<int, SourceDescription>(shader, source);
}

public IProgram Link(Shader shader, IEnumerable<object> units)
{
return new Program(units);
return new Program(units.Cast<Tuple<int, SourceDescription>>());
}

public void Initialize()
Expand Down
16 changes: 13 additions & 3 deletions IIS.SLSharp.Bindings.OpenTK/Program.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using IIS.SLSharp.Descriptions;
using OpenTK.Graphics.OpenGL;

namespace IIS.SLSharp.Bindings.OpenTK
Expand All @@ -7,12 +9,20 @@ sealed class Program: IProgram
{
private int _name;

public Program(IEnumerable<object> units)
public List<VariableDescription> VertexIns { get; private set; }

public Program(IEnumerable<Tuple<int, SourceDescription>> units)
{
_name = GL.CreateProgram();

var merged = SourceDescription.Empty;
foreach (var unit in units)
GL.AttachShader(_name, (int)unit);
{
GL.AttachShader(_name, unit.Item1);
merged = merged.Merge(unit.Item2);
}
VertexIns = merged.VertexIns;

GL.LinkProgram(_name);
Utilities.CheckGL();
}
Expand Down
39 changes: 24 additions & 15 deletions IIS.SLSharp.Examples.Axiom/DemoWindow.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
using System.IO;
using System;
using System.IO;
using System.Linq;
using Axiom.Core;
using Axiom.Graphics;
using Axiom.Math;
using IIS.SLSharp.Bindings.Axiom.GLSL;
using IIS.SLSharp.Examples.Axiom.Shaders;
using IIS.SLSharp.Shaders;

Expand Down Expand Up @@ -32,6 +34,8 @@ public DemoWindow(Root root)
public void OnLoad()
{
Bindings.Axiom.GLSL.SLSharp.Init();
Shader.DebugMode = true;

_shader = Shader.CreateSharedShader<SimpleShader>();

// Create patch with positions, normals, and 1 set of texcoords
Expand Down Expand Up @@ -140,11 +144,16 @@ public void OnLoad()
pass.LightingEnabled = false;
//pass.CreateTextureUnitState("test.png");

var prog = (Bindings.Axiom.GLSL.Program) _shader.Program;

var prog = (Bindings.Axiom.GLSL.Program)_shader.Program;
pass.SetVertexProgram(prog.VertexShader.Name);
pass.SetFragmentProgram(prog.PixelShader.Name);
pass.VertexProgramParameters.SetAutoConstant(1, GpuProgramParameters.AutoConstantType.WorldViewProjMatrix);

// var mvp = Shader.UniformName(() => _shader.ModelviewProjection);
// var idx = pass.VertexProgramParameters.GetParamIndex(mvp);

var mvpIdx = Shader.UniformLocation(_shader, () => _shader.ModelviewProjection);
pass.VertexProgramParameters.SetAutoConstant(mvpIdx, GpuProgramParameters.AutoConstantType.WorldViewProjMatrix);

//pass.VertexProgramParameters.SetAutoConstant(0, GpuProgramParameters.AutoConstantType.VertexWinding);

Expand All @@ -171,8 +180,9 @@ public void OnLoad()

_scene.RootSceneNode.AttachObject(_patchEntity);

_camera.Position = new Vector3(500, 500, 1500);
_camera.LookAt(new Vector3(0, 200, -300));
//_camera.Position = new Vector3(500, 500, 1500);
//_camera.LookAt(new Vector3(0, 200, -300));
_camera.LookAt(Vector3.Zero);
_camera.Near = 5;
_camera.AutoAspectRatio = true;

Expand All @@ -188,19 +198,18 @@ public void OnUnload()

public void OnRenderFrame(object s, FrameEventArgs e)
{
var angle = Utility.DegreesToRadians((DateTime.Now.Millisecond / 1000.0f + DateTime.Now.Second) * 6 * 4);
var cam = new Vector3((float)Math.Sin(angle), 0.0f, (float)Math.Cos(angle));
var look = new Vector3(0, 300, 0);

var mvp = _camera.ViewMatrix*_camera.ProjectionMatrix;

/*
var vname = Shader.AttributeName(() => _shader.Vertex);
int prog;
Tao.OpenGl.Gl.glGetIntegerv(Tao.OpenGl.Gl.GL_CURRENT_PROGRAM, out prog);
Tao.OpenGl.Gl.glBindAttribLocation(prog, 0, vname);
cam *= 1600.0f;
cam.y += 100.0f;
_camera.LookAt(look);
_camera.Position = cam + look;

_shader.Begin();
var mvp = _camera.ProjectionMatrix * _camera.ViewMatrix;
_shader.ModelviewProjection = mvp.ToMatrix4F();
_shader.End();
*/
_shader.Blue = (float)Math.Sin(angle * 8.0f);
}

}
Expand Down
2 changes: 1 addition & 1 deletion IIS.SLSharp.Examples.Axiom/Shaders/SimpleShader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ protected void FragmentMain()
[VertexShader(true)]
public void VertexMain()
{
_uv = (Vertex.xy + new vec2(1.0f)) * 0.5f;
_uv = (Vertex.xy + new vec2(1.0f)) * 0.5f * 0.005f;
gl_Position = ModelviewProjection * Vertex;
}

Expand Down
6 changes: 6 additions & 0 deletions IIS.SLSharp/Descriptions/SourceDescription.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ public class SourceDescription

public readonly List<string> ForwardDecl;

public static readonly SourceDescription Empty = new SourceDescription(
new List<FunctionDescription>(), new List<VariableDescription>(),
new List<VariableDescription>(), new List<VariableDescription>(),
new List<VariableDescription>(), new List<VariableDescription>(),
new List<string>());

public SourceDescription(List<FunctionDescription> functions,
List<VariableDescription> uniforms, List<VariableDescription> attributes,
List<VariableDescription> varyings, List<VariableDescription> vertexIns,
Expand Down
16 changes: 14 additions & 2 deletions IIS.SLSharp/Shaders/Shader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -529,7 +529,7 @@ protected void Activate()
/// shader that is going to be accessed.
/// </summary>
/// <param name="main"></param>
protected void BeginLibrary(Shader main)
public void BeginLibrary(Shader main)
{
Program = main.Program;
CacheUniforms();
Expand Down Expand Up @@ -772,13 +772,25 @@ public static string AttributeName<T>(Expression<Func<T>> expr)
var body = ((MemberExpression)expr.Body);
return GetVaryingName(body.Member as FieldInfo);
}

public static int AttributeLocation<T>(Shader shader, Expression<Func<T>> expr)
{
var loc = shader.Program.GetAttributeIndex(AttributeName(expr));
return loc;
}

public static string UniformName<T>(Expression<Func<T>> expr)
{
var body = ((MemberExpression)expr.Body);
return GetUniformName(body.Member as PropertyInfo);
}

public static int UniformLocation<T>(Shader shader, Expression<Func<T>> expr)
{
var loc = shader.Program.GetUniformIndex(UniformName(expr));
return loc;
}

private static void RefShaders()
{
if (_refCount == 0)
Expand Down

0 comments on commit 3480480

Please sign in to comment.