Begin work on the glsl renderer. Renders blue blocks for chrome-rgba.

This commit is contained in:
Paul Chote
2010-11-10 22:00:58 +13:00
parent a058eb06b2
commit f2a20a182e
8 changed files with 103 additions and 77 deletions

View File

@@ -23,9 +23,6 @@ namespace OpenRA.Renderer.Glsl
public class GraphicsDevice : IGraphicsDevice
{
Size windowSize;
internal IntPtr cgContext;
internal int vertexProfile, fragmentProfile;
IntPtr surf;
public Size WindowSize { get { return windowSize; } }
@@ -83,18 +80,6 @@ namespace OpenRA.Renderer.Glsl
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 );
CheckGlError();
Gl.glEnableClientState( Gl.GL_TEXTURE_COORD_ARRAY );
@@ -103,14 +88,6 @@ namespace OpenRA.Renderer.Glsl
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 )
{
if( width < 0 ) width = 0;

View File

@@ -1,4 +1,4 @@
#region Copyright & License Information
#region Copyright & License Information
/*
* Copyright 2007-2010 The OpenRA Developers (see AUTHORS)
* This file is part of OpenRA, which is free software. It is made
@@ -12,74 +12,86 @@ using System;
using System.IO;
using OpenRA.FileFormats;
using OpenRA.FileFormats.Graphics;
using Tao.Cg;
using Tao.OpenGl;
using System.Text;
namespace OpenRA.Renderer.Glsl
{
public class Shader : IShader
{
IntPtr effect;
IntPtr technique;
GraphicsDevice dev;
public Shader(GraphicsDevice dev, string name)
int program;
string type;
public Shader(GraphicsDevice dev, string type)
{
this.dev = dev;
string code;
using (var file = new StreamReader(FileSystem.Open("cg{0}{1}.fx".F(Path.DirectorySeparatorChar, name))))
code = file.ReadToEnd();
effect = Tao.Cg.Cg.cgCreateEffect(dev.cgContext, code, null);
if (effect == IntPtr.Zero)
{
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);
if (technique == IntPtr.Zero)
throw new InvalidOperationException("No techniques");
while (Tao.Cg.Cg.cgValidateTechnique(technique) == 0)
{
technique = Tao.Cg.Cg.cgGetNextTechnique(technique);
if (technique == IntPtr.Zero)
throw new InvalidOperationException("No valid techniques");
}
this.type = type;
Console.WriteLine("Loading shader: {0}",type);
// Vertex shader
string vertexCode;
using (var file = new StreamReader(FileSystem.Open("glsl{0}{1}.vert".F(Path.DirectorySeparatorChar, type))))
vertexCode = file.ReadToEnd();
int v = Gl.glCreateShader(Gl.GL_VERTEX_SHADER);
GraphicsDevice.CheckGlError();
Gl.glShaderSource(v,1,new string[]{vertexCode},null);
GraphicsDevice.CheckGlError();
Gl.glCompileShader(v);
GraphicsDevice.CheckGlError();
// Fragment shader
string fragmentCode;
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)
{
Tao.Cg.CgGl.cgGLEnableProfile(dev.vertexProfile);
Tao.Cg.CgGl.cgGLEnableProfile(dev.fragmentProfile);
var pass = Tao.Cg.Cg.cgGetFirstPass(technique);
while (pass != IntPtr.Zero)
{
Tao.Cg.Cg.cgSetPassState(pass);
a();
Tao.Cg.Cg.cgResetPassState(pass);
pass = Tao.Cg.Cg.cgGetNextPass(pass);
}
Tao.Cg.CgGl.cgGLDisableProfile(dev.fragmentProfile);
Tao.Cg.CgGl.cgGLDisableProfile(dev.vertexProfile);
GraphicsDevice.CheckGlError();
Gl.glUseProgram(program);
GraphicsDevice.CheckGlError();
Console.WriteLine("rendering");
a();
GraphicsDevice.CheckGlError();
Gl.glUseProgram(0);
GraphicsDevice.CheckGlError();
}
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)
{
var param = Tao.Cg.Cg.cgGetNamedEffectParameter(effect, name);
if (param != IntPtr.Zero)
Tao.Cg.CgGl.cgGLSetParameter2f(param, x, y);
GraphicsDevice.CheckGlError();
Console.WriteLine("setting value {0} to {1},{2} in {3}",name,x,y,type);
int param = Gl.glGetUniformLocation(program, name);
GraphicsDevice.CheckGlError();
Gl.glUniform2f(param,x,y);
GraphicsDevice.CheckGlError();
}
public void Commit() { }