start moving common parts of the renderer into OpenRA.Renderer.SdlCommon

This commit is contained in:
Chris Forbes
2011-07-12 21:40:50 +12:00
parent 2ac6ccd3a4
commit df1201b4c2
11 changed files with 321 additions and 94 deletions

View File

@@ -15,6 +15,7 @@ using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using OpenRA.FileFormats.Graphics;
using OpenRA.Renderer.SdlCommon;
using Tao.OpenGl;
using Tao.Sdl;
@@ -37,43 +38,6 @@ namespace OpenRA.Renderer.Glsl
public Size WindowSize { get { return windowSize; } }
public enum GlError
{
GL_NO_ERROR = Gl.GL_NO_ERROR,
GL_INVALID_ENUM = Gl.GL_INVALID_ENUM,
GL_INVALID_VALUE = Gl.GL_INVALID_VALUE,
GL_STACK_OVERFLOW = Gl.GL_STACK_OVERFLOW,
GL_STACK_UNDERFLOW = Gl.GL_STACK_UNDERFLOW,
GL_OUT_OF_MEMORY = Gl.GL_OUT_OF_MEMORY,
GL_TABLE_TOO_LARGE = Gl.GL_TABLE_TOO_LARGE,
GL_INVALID_OPERATION = Gl.GL_INVALID_OPERATION,
}
internal static void CheckGlError()
{
var n = Gl.glGetError();
if( n != Gl.GL_NO_ERROR )
{
var error = "GL Error: {0}\n{1}".F((GlError)n, new StackTrace());
WriteGraphicsLog(error);
throw new InvalidOperationException("OpenGL Error: See graphics.log for details.");
}
}
static void WriteGraphicsLog(string message)
{
Log.AddChannel("graphics", "graphics.log");
Log.Write("graphics", message);
Log.Write("graphics", "");
Log.Write("graphics", "OpenGL Information:");
Log.Write("graphics", "Vendor: {0}", Gl.glGetString(Gl.GL_VENDOR));
Log.Write("graphics", "Renderer: {0}", Gl.glGetString(Gl.GL_RENDERER));
Log.Write("graphics", "GL Version: {0}", Gl.glGetString(Gl.GL_VERSION));
Log.Write("graphics", "Shader Version: {0}", Gl.glGetString(Gl.GL_SHADING_LANGUAGE_VERSION));
Log.Write("graphics", "Available extensions:");
Log.Write("graphics", Gl.glGetString(Gl.GL_EXTENSIONS));
}
public GraphicsDevice( Size size, WindowMode window, bool vsync )
{
Console.WriteLine("Using Gl renderer");
@@ -118,7 +82,7 @@ namespace OpenRA.Renderer.Glsl
Sdl.SDL_EnableUNICODE( 1 );
Sdl.SDL_EnableKeyRepeat( Sdl.SDL_DEFAULT_REPEAT_DELAY, Sdl.SDL_DEFAULT_REPEAT_INTERVAL );
CheckGlError();
ErrorHandler.CheckGlError();
// Test for required extensions
var required = new string[]
@@ -136,20 +100,19 @@ namespace OpenRA.Renderer.Glsl
if (missingExtensions.Any())
{
WriteGraphicsLog("Unsupported GPU: Missing extensions: {0}".F(string.Join(",", missingExtensions)));
ErrorHandler.WriteGraphicsLog("Unsupported GPU: Missing extensions: {0}"
.F(string.Join(",", missingExtensions)));
throw new InvalidProgramException("Unsupported GPU. See graphics.log for details.");
}
windowSize = size;
Gl.glEnableClientState( Gl.GL_VERTEX_ARRAY );
CheckGlError();
ErrorHandler.CheckGlError();
Gl.glEnableClientState( Gl.GL_TEXTURE_COORD_ARRAY );
CheckGlError();
Sdl.SDL_SetModState( 0 );
ErrorHandler.CheckGlError();
Sdl.SDL_SetModState( 0 );
}
public void EnableScissor( int left, int top, int width, int height )
@@ -157,23 +120,23 @@ namespace OpenRA.Renderer.Glsl
if( width < 0 ) width = 0;
if( height < 0 ) height = 0;
Gl.glScissor( left, windowSize.Height - ( top + height ), width, height );
CheckGlError();
ErrorHandler.CheckGlError();
Gl.glEnable( Gl.GL_SCISSOR_TEST );
CheckGlError();
ErrorHandler.CheckGlError();
}
public void DisableScissor()
{
Gl.glDisable( Gl.GL_SCISSOR_TEST );
CheckGlError();
ErrorHandler.CheckGlError();
}
public void Clear( Color c )
{
Gl.glClearColor( 0, 0, 0, 0 );
CheckGlError();
ErrorHandler.CheckGlError();
Gl.glClear( Gl.GL_COLOR_BUFFER_BIT );
CheckGlError();
ErrorHandler.CheckGlError();
}
MouseButton lastButtonBits = (MouseButton)0;
@@ -317,13 +280,13 @@ namespace OpenRA.Renderer.Glsl
pendingMotion = null;
}
CheckGlError();
ErrorHandler.CheckGlError();
}
public void DrawPrimitives( PrimitiveType pt, int firstVertex, int numVertices )
{
Gl.glDrawArrays( ModeFromPrimitiveType( pt ), firstVertex, numVertices );
CheckGlError();
ErrorHandler.CheckGlError();
}
static int ModeFromPrimitiveType( PrimitiveType pt )
@@ -338,9 +301,9 @@ namespace OpenRA.Renderer.Glsl
throw new NotImplementedException();
}
public IVertexBuffer<Vertex> CreateVertexBuffer( int size ) { return new VertexBuffer<Vertex>( this, size ); }
public ITexture CreateTexture() { return new Texture( this ); }
public ITexture CreateTexture( Bitmap bitmap ) { return new Texture( this, bitmap ); }
public IVertexBuffer<Vertex> CreateVertexBuffer( int size ) { return new VertexBuffer<Vertex>( size ); }
public ITexture CreateTexture() { return new Texture(); }
public ITexture CreateTexture( Bitmap bitmap ) { return new Texture( bitmap ); }
public IShader CreateShader( string name ) { return new Shader( this, name ); }
public int GpuMemoryUsed { get; internal set; }

View File

@@ -57,8 +57,6 @@
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="GraphicsDevice.cs" />
<Compile Include="Shader.cs" />
<Compile Include="Texture.cs" />
<Compile Include="VertexBuffer.cs" />
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
<ItemGroup>
@@ -70,5 +68,9 @@
<Project>{0DFB103F-2962-400F-8C6D-E2C28CCBA633}</Project>
<Name>OpenRA.Game</Name>
</ProjectReference>
<ProjectReference Include="..\OpenRA.Renderer.SdlCommon\OpenRA.Renderer.SdlCommon.csproj">
<Project>{52FD9F0B-B209-4ED7-8A32-AC8033363263}</Project>
<Name>OpenRA.Renderer.SdlCommon</Name>
</ProjectReference>
</ItemGroup>
</Project>

View File

@@ -14,6 +14,7 @@ using System.IO;
using System.Text;
using OpenRA.FileFormats;
using OpenRA.FileFormats.Graphics;
using OpenRA.Renderer.SdlCommon;
using Tao.OpenGl;
namespace OpenRA.Renderer.Glsl
@@ -31,15 +32,15 @@ namespace OpenRA.Renderer.Glsl
vertexCode = file.ReadToEnd();
int v = Gl.glCreateShaderObjectARB(Gl.GL_VERTEX_SHADER_ARB);
GraphicsDevice.CheckGlError();
ErrorHandler.CheckGlError();
Gl.glShaderSourceARB(v,1,new string[]{vertexCode},null);
GraphicsDevice.CheckGlError();
ErrorHandler.CheckGlError();
Gl.glCompileShaderARB(v);
GraphicsDevice.CheckGlError();
ErrorHandler.CheckGlError();
int success;
Gl.glGetObjectParameterivARB(v, Gl.GL_OBJECT_COMPILE_STATUS_ARB, out success);
GraphicsDevice.CheckGlError();
ErrorHandler.CheckGlError();
if (success == 0)
throw new InvalidProgramException("Compile error in {0}{1}.vert".F(Path.DirectorySeparatorChar, type));
@@ -48,41 +49,41 @@ namespace OpenRA.Renderer.Glsl
using (var file = new StreamReader(FileSystem.Open("glsl{0}{1}.frag".F(Path.DirectorySeparatorChar, type))))
fragmentCode = file.ReadToEnd();
int f = Gl.glCreateShaderObjectARB(Gl.GL_FRAGMENT_SHADER_ARB);
GraphicsDevice.CheckGlError();
ErrorHandler.CheckGlError();
Gl.glShaderSourceARB(f,1,new string[]{fragmentCode},null);
GraphicsDevice.CheckGlError();
ErrorHandler.CheckGlError();
Gl.glCompileShaderARB(f);
GraphicsDevice.CheckGlError();
ErrorHandler.CheckGlError();
Gl.glGetObjectParameterivARB(f, Gl.GL_OBJECT_COMPILE_STATUS_ARB, out success);
GraphicsDevice.CheckGlError();
ErrorHandler.CheckGlError();
if (success == 0)
throw new InvalidProgramException("Compile error in glsl{0}{1}.frag".F(Path.DirectorySeparatorChar, type));
// Assemble program
program = Gl.glCreateProgramObjectARB();
GraphicsDevice.CheckGlError();
ErrorHandler.CheckGlError();
Gl.glAttachObjectARB(program,v);
GraphicsDevice.CheckGlError();
ErrorHandler.CheckGlError();
Gl.glAttachObjectARB(program,f);
GraphicsDevice.CheckGlError();
ErrorHandler.CheckGlError();
Gl.glLinkProgramARB(program);
GraphicsDevice.CheckGlError();
ErrorHandler.CheckGlError();
Gl.glGetObjectParameterivARB(program, Gl.GL_OBJECT_LINK_STATUS_ARB, out success);
GraphicsDevice.CheckGlError();
ErrorHandler.CheckGlError();
if (success == 0)
throw new InvalidProgramException("Linking error in {0} shader".F(type));
Gl.glUseProgramObjectARB(program);
GraphicsDevice.CheckGlError();
ErrorHandler.CheckGlError();
int numUniforms;
Gl.glGetObjectParameterivARB( program, Gl.GL_ACTIVE_UNIFORMS, out numUniforms );
GraphicsDevice.CheckGlError();
ErrorHandler.CheckGlError();
int nextTexUnit = 1;
for( int i = 0 ; i < numUniforms ; i++ )
@@ -91,14 +92,14 @@ namespace OpenRA.Renderer.Glsl
var sb = new StringBuilder(128);
Gl.glGetActiveUniformARB( program, i, 128, out uLen, out uSize, out uType, sb );
var sampler = sb.ToString();
GraphicsDevice.CheckGlError();
ErrorHandler.CheckGlError();
if( uType == Gl.GL_SAMPLER_2D_ARB )
{
samplers.Add( sampler, nextTexUnit );
loc = Gl.glGetUniformLocationARB(program, sampler);
GraphicsDevice.CheckGlError();
ErrorHandler.CheckGlError();
Gl.glUniform1iARB( loc, nextTexUnit );
GraphicsDevice.CheckGlError();
ErrorHandler.CheckGlError();
++nextTexUnit;
}
}
@@ -107,31 +108,31 @@ namespace OpenRA.Renderer.Glsl
public void Render(Action a)
{
Gl.glUseProgramObjectARB(program);
GraphicsDevice.CheckGlError();
ErrorHandler.CheckGlError();
// Todo: Only enable alpha blending if we need it
Gl.glEnable(Gl.GL_BLEND);
GraphicsDevice.CheckGlError();
ErrorHandler.CheckGlError();
Gl.glBlendFunc(Gl.GL_SRC_ALPHA, Gl.GL_ONE_MINUS_SRC_ALPHA);
GraphicsDevice.CheckGlError();
ErrorHandler.CheckGlError();
a();
GraphicsDevice.CheckGlError();
ErrorHandler.CheckGlError();
Gl.glDisable(Gl.GL_BLEND);
GraphicsDevice.CheckGlError();
ErrorHandler.CheckGlError();
}
public void SetValue(string name, ITexture t)
{
if( t == null ) return;
Gl.glUseProgramObjectARB(program);
GraphicsDevice.CheckGlError();
ErrorHandler.CheckGlError();
var texture = (Texture)t;
int texUnit;
if( samplers.TryGetValue( name, out texUnit ) )
{
Gl.glActiveTextureARB( Gl.GL_TEXTURE0_ARB + texUnit );
GraphicsDevice.CheckGlError();
ErrorHandler.CheckGlError();
Gl.glBindTexture( Gl.GL_TEXTURE_2D, texture.texture );
GraphicsDevice.CheckGlError();
ErrorHandler.CheckGlError();
Gl.glActiveTextureARB( Gl.GL_TEXTURE0_ARB );
}
}
@@ -139,11 +140,11 @@ namespace OpenRA.Renderer.Glsl
public void SetValue(string name, float x, float y)
{
Gl.glUseProgramObjectARB(program);
GraphicsDevice.CheckGlError();
ErrorHandler.CheckGlError();
int param = Gl.glGetUniformLocationARB(program, name);
GraphicsDevice.CheckGlError();
ErrorHandler.CheckGlError();
Gl.glUniform2fARB(param,x,y);
GraphicsDevice.CheckGlError();
ErrorHandler.CheckGlError();
}
}
}