Begin work on the glsl renderer. Renders blue blocks for chrome-rgba.
This commit is contained in:
@@ -133,7 +133,7 @@ namespace OpenRA.Graphics
|
|||||||
internal static void Initialize( OpenRA.FileFormats.Graphics.WindowMode windowMode )
|
internal static void Initialize( OpenRA.FileFormats.Graphics.WindowMode windowMode )
|
||||||
{
|
{
|
||||||
var resolution = GetResolution( windowMode );
|
var resolution = GetResolution( windowMode );
|
||||||
device = CreateDevice( Assembly.LoadFile( Path.GetFullPath( "OpenRA.Renderer.Cg.dll" ) ), resolution.Width, resolution.Height, windowMode, false );
|
device = CreateDevice( Assembly.LoadFile( Path.GetFullPath( "OpenRA.Renderer.Gl.dll" ) ), resolution.Width, resolution.Height, windowMode, false );
|
||||||
}
|
}
|
||||||
|
|
||||||
static Size GetResolution(WindowMode windowmode)
|
static Size GetResolution(WindowMode windowmode)
|
||||||
|
|||||||
@@ -23,9 +23,6 @@ namespace OpenRA.Renderer.Glsl
|
|||||||
public class GraphicsDevice : IGraphicsDevice
|
public class GraphicsDevice : IGraphicsDevice
|
||||||
{
|
{
|
||||||
Size windowSize;
|
Size windowSize;
|
||||||
internal IntPtr cgContext;
|
|
||||||
internal int vertexProfile, fragmentProfile;
|
|
||||||
|
|
||||||
IntPtr surf;
|
IntPtr surf;
|
||||||
|
|
||||||
public Size WindowSize { get { return windowSize; } }
|
public Size WindowSize { get { return windowSize; } }
|
||||||
@@ -83,18 +80,6 @@ namespace OpenRA.Renderer.Glsl
|
|||||||
|
|
||||||
windowSize = new Size( width, height );
|
windowSize = new Size( width, height );
|
||||||
|
|
||||||
cgContext = Tao.Cg.Cg.cgCreateContext();
|
|
||||||
|
|
||||||
Tao.Cg.Cg.cgSetErrorCallback( CgErrorCallback );
|
|
||||||
|
|
||||||
Tao.Cg.CgGl.cgGLRegisterStates( cgContext );
|
|
||||||
Tao.Cg.CgGl.cgGLSetManageTextureParameters( cgContext, true );
|
|
||||||
vertexProfile = CgGl.cgGLGetLatestProfile( CgGl.CG_GL_VERTEX );
|
|
||||||
fragmentProfile = CgGl.cgGLGetLatestProfile( CgGl.CG_GL_FRAGMENT );
|
|
||||||
|
|
||||||
//Console.WriteLine("VP Profile: " + vertexProfile);
|
|
||||||
//Console.WriteLine("FP Profile: " + fragmentProfile);
|
|
||||||
|
|
||||||
Gl.glEnableClientState( Gl.GL_VERTEX_ARRAY );
|
Gl.glEnableClientState( Gl.GL_VERTEX_ARRAY );
|
||||||
CheckGlError();
|
CheckGlError();
|
||||||
Gl.glEnableClientState( Gl.GL_TEXTURE_COORD_ARRAY );
|
Gl.glEnableClientState( Gl.GL_TEXTURE_COORD_ARRAY );
|
||||||
@@ -103,14 +88,6 @@ namespace OpenRA.Renderer.Glsl
|
|||||||
Sdl.SDL_SetModState( 0 ); // i have had enough.
|
Sdl.SDL_SetModState( 0 ); // i have had enough.
|
||||||
}
|
}
|
||||||
|
|
||||||
static Tao.Cg.Cg.CGerrorCallbackFuncDelegate CgErrorCallback = () =>
|
|
||||||
{
|
|
||||||
var err = Tao.Cg.Cg.cgGetError();
|
|
||||||
var str = Tao.Cg.Cg.cgGetErrorString( err );
|
|
||||||
throw new InvalidOperationException(
|
|
||||||
string.Format( "CG Error: {0}: {1}", err, str ) );
|
|
||||||
};
|
|
||||||
|
|
||||||
public void EnableScissor( int left, int top, int width, int height )
|
public void EnableScissor( int left, int top, int width, int height )
|
||||||
{
|
{
|
||||||
if( width < 0 ) width = 0;
|
if( width < 0 ) width = 0;
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
#region Copyright & License Information
|
#region Copyright & License Information
|
||||||
/*
|
/*
|
||||||
* Copyright 2007-2010 The OpenRA Developers (see AUTHORS)
|
* Copyright 2007-2010 The OpenRA Developers (see AUTHORS)
|
||||||
* This file is part of OpenRA, which is free software. It is made
|
* This file is part of OpenRA, which is free software. It is made
|
||||||
@@ -12,74 +12,86 @@ using System;
|
|||||||
using System.IO;
|
using System.IO;
|
||||||
using OpenRA.FileFormats;
|
using OpenRA.FileFormats;
|
||||||
using OpenRA.FileFormats.Graphics;
|
using OpenRA.FileFormats.Graphics;
|
||||||
using Tao.Cg;
|
using Tao.OpenGl;
|
||||||
|
using System.Text;
|
||||||
|
|
||||||
namespace OpenRA.Renderer.Glsl
|
namespace OpenRA.Renderer.Glsl
|
||||||
{
|
{
|
||||||
public class Shader : IShader
|
public class Shader : IShader
|
||||||
{
|
{
|
||||||
IntPtr effect;
|
int program;
|
||||||
IntPtr technique;
|
string type;
|
||||||
GraphicsDevice dev;
|
public Shader(GraphicsDevice dev, string type)
|
||||||
|
|
||||||
public Shader(GraphicsDevice dev, string name)
|
|
||||||
{
|
{
|
||||||
this.dev = dev;
|
this.type = type;
|
||||||
string code;
|
Console.WriteLine("Loading shader: {0}",type);
|
||||||
using (var file = new StreamReader(FileSystem.Open("cg{0}{1}.fx".F(Path.DirectorySeparatorChar, name))))
|
// Vertex shader
|
||||||
code = file.ReadToEnd();
|
string vertexCode;
|
||||||
effect = Tao.Cg.Cg.cgCreateEffect(dev.cgContext, code, null);
|
using (var file = new StreamReader(FileSystem.Open("glsl{0}{1}.vert".F(Path.DirectorySeparatorChar, type))))
|
||||||
|
vertexCode = file.ReadToEnd();
|
||||||
|
|
||||||
if (effect == IntPtr.Zero)
|
int v = Gl.glCreateShader(Gl.GL_VERTEX_SHADER);
|
||||||
{
|
GraphicsDevice.CheckGlError();
|
||||||
var err = Tao.Cg.Cg.cgGetErrorString(Tao.Cg.Cg.cgGetError());
|
|
||||||
var results = Tao.Cg.Cg.cgGetLastListing(dev.cgContext);
|
|
||||||
throw new InvalidOperationException(
|
|
||||||
string.Format("Cg compile failed ({0}):\n{1}", err, results));
|
|
||||||
}
|
|
||||||
|
|
||||||
technique = Tao.Cg.Cg.cgGetFirstTechnique(effect);
|
Gl.glShaderSource(v,1,new string[]{vertexCode},null);
|
||||||
if (technique == IntPtr.Zero)
|
GraphicsDevice.CheckGlError();
|
||||||
throw new InvalidOperationException("No techniques");
|
Gl.glCompileShader(v);
|
||||||
while (Tao.Cg.Cg.cgValidateTechnique(technique) == 0)
|
GraphicsDevice.CheckGlError();
|
||||||
{
|
|
||||||
technique = Tao.Cg.Cg.cgGetNextTechnique(technique);
|
// Fragment shader
|
||||||
if (technique == IntPtr.Zero)
|
string fragmentCode;
|
||||||
throw new InvalidOperationException("No valid techniques");
|
using (var file = new StreamReader(FileSystem.Open("glsl{0}rgba.frag".F(Path.DirectorySeparatorChar, type))))
|
||||||
}
|
fragmentCode = file.ReadToEnd();
|
||||||
|
int f = Gl.glCreateShader(Gl.GL_FRAGMENT_SHADER);
|
||||||
|
GraphicsDevice.CheckGlError();
|
||||||
|
Gl.glShaderSource(f,1,new string[]{fragmentCode},null);
|
||||||
|
GraphicsDevice.CheckGlError();
|
||||||
|
Gl.glCompileShader(f);
|
||||||
|
GraphicsDevice.CheckGlError();
|
||||||
|
|
||||||
|
// Assemble program
|
||||||
|
program = Gl.glCreateProgram();
|
||||||
|
GraphicsDevice.CheckGlError();
|
||||||
|
Gl.glAttachShader(program,v);
|
||||||
|
GraphicsDevice.CheckGlError();
|
||||||
|
Gl.glAttachShader(program,f);
|
||||||
|
GraphicsDevice.CheckGlError();
|
||||||
|
|
||||||
|
Gl.glLinkProgram(program);
|
||||||
|
StringBuilder foo = new StringBuilder();
|
||||||
|
int[] l = new int[1];
|
||||||
|
System.Text.StringBuilder log = new System.Text.StringBuilder(4024);
|
||||||
|
|
||||||
|
Gl.glGetProgramInfoLog(program,4024,l,log);
|
||||||
|
Console.WriteLine(log.ToString());
|
||||||
|
GraphicsDevice.CheckGlError();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Render(Action a)
|
public void Render(Action a)
|
||||||
{
|
{
|
||||||
Tao.Cg.CgGl.cgGLEnableProfile(dev.vertexProfile);
|
GraphicsDevice.CheckGlError();
|
||||||
Tao.Cg.CgGl.cgGLEnableProfile(dev.fragmentProfile);
|
Gl.glUseProgram(program);
|
||||||
|
GraphicsDevice.CheckGlError();
|
||||||
var pass = Tao.Cg.Cg.cgGetFirstPass(technique);
|
Console.WriteLine("rendering");
|
||||||
while (pass != IntPtr.Zero)
|
|
||||||
{
|
|
||||||
Tao.Cg.Cg.cgSetPassState(pass);
|
|
||||||
a();
|
a();
|
||||||
Tao.Cg.Cg.cgResetPassState(pass);
|
GraphicsDevice.CheckGlError();
|
||||||
pass = Tao.Cg.Cg.cgGetNextPass(pass);
|
Gl.glUseProgram(0);
|
||||||
}
|
GraphicsDevice.CheckGlError();
|
||||||
|
|
||||||
Tao.Cg.CgGl.cgGLDisableProfile(dev.fragmentProfile);
|
|
||||||
Tao.Cg.CgGl.cgGLDisableProfile(dev.vertexProfile);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void SetValue(string name, ITexture t)
|
public void SetValue(string name, ITexture t)
|
||||||
{
|
{
|
||||||
var texture = (Texture)t;
|
|
||||||
var param = Tao.Cg.Cg.cgGetNamedEffectParameter(effect, name);
|
|
||||||
if (param != IntPtr.Zero && texture != null)
|
|
||||||
Tao.Cg.CgGl.cgGLSetupSampler(param, texture.texture);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void SetValue(string name, float x, float y)
|
public void SetValue(string name, float x, float y)
|
||||||
{
|
{
|
||||||
var param = Tao.Cg.Cg.cgGetNamedEffectParameter(effect, name);
|
GraphicsDevice.CheckGlError();
|
||||||
if (param != IntPtr.Zero)
|
Console.WriteLine("setting value {0} to {1},{2} in {3}",name,x,y,type);
|
||||||
Tao.Cg.CgGl.cgGLSetParameter2f(param, x, y);
|
int param = Gl.glGetUniformLocation(program, name);
|
||||||
|
GraphicsDevice.CheckGlError();
|
||||||
|
Gl.glUniform2f(param,x,y);
|
||||||
|
GraphicsDevice.CheckGlError();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Commit() { }
|
public void Commit() { }
|
||||||
|
|||||||
8
glsl/chrome-rgba.vert
Normal file
8
glsl/chrome-rgba.vert
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
uniform vec2 r1;
|
||||||
|
uniform vec2 r2; // matrix elements
|
||||||
|
|
||||||
|
void main()
|
||||||
|
{
|
||||||
|
vec2 p = gl_Vertex.xy*vec2(0.001538462,-0.0025) + vec2(-1,1);
|
||||||
|
gl_Position = vec4(p.x,p.y,0,1);
|
||||||
|
}
|
||||||
8
glsl/chrome-shp.vert
Normal file
8
glsl/chrome-shp.vert
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
uniform vec2 Scroll;
|
||||||
|
uniform vec2 r1;
|
||||||
|
uniform vec2 r2; // matrix elements
|
||||||
|
|
||||||
|
void main()
|
||||||
|
{
|
||||||
|
gl_Position = ftransform();
|
||||||
|
}
|
||||||
4
glsl/rgba.frag
Normal file
4
glsl/rgba.frag
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
void main()
|
||||||
|
{
|
||||||
|
gl_FragColor = vec4(0.4,0.4,0.8,1.0);
|
||||||
|
}
|
||||||
8
glsl/world-line.vert
Normal file
8
glsl/world-line.vert
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
uniform vec2 Scroll;
|
||||||
|
uniform vec2 r1;
|
||||||
|
uniform vec2 r2; // matrix elements
|
||||||
|
|
||||||
|
void main()
|
||||||
|
{
|
||||||
|
gl_Position = ftransform();
|
||||||
|
}
|
||||||
9
glsl/world-shp.vert
Normal file
9
glsl/world-shp.vert
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
uniform vec2 Scroll;
|
||||||
|
uniform vec2 r1;
|
||||||
|
uniform vec2 r2; // matrix elements
|
||||||
|
|
||||||
|
void main()
|
||||||
|
{
|
||||||
|
vec2 p = (gl_Vertex.xy);
|
||||||
|
gl_Position = gl_Vertex;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user