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

@@ -0,0 +1,58 @@
#region Copyright & License Information
/*
* Copyright 2007-2011 The OpenRA Developers (see AUTHORS)
* This file is part of OpenRA, which is free software. It is made
* available to you under the terms of the GNU General Public License
* as published by the Free Software Foundation. For more information,
* see COPYING.
*/
#endregion
using System;
using System.Diagnostics;
using Tao.OpenGl;
using OpenRA.FileFormats;
namespace OpenRA.Renderer.SdlCommon
{
public static class ErrorHandler
{
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,
}
public 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.");
}
}
public 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));
}
}
}

View File

@@ -0,0 +1 @@
<?xml version="1.0" encoding="utf-8"?>

View File

@@ -0,0 +1,122 @@
#region Copyright & License Information
/*
* Copyright 2007-2011 The OpenRA Developers (see AUTHORS)
* This file is part of OpenRA, which is free software. It is made
* available to you under the terms of the GNU General Public License
* as published by the Free Software Foundation. For more information,
* see COPYING.
*/
#endregion
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using OpenRA.FileFormats;
using OpenRA.FileFormats.Graphics;
using Tao.OpenGl;
namespace OpenRA.Renderer.SdlCommon
{
public class Texture : ITexture
{
public int texture; /* temp: can be internal again once shaders are in shared code */
public Texture()
{
Gl.glGenTextures(1, out texture);
ErrorHandler.CheckGlError();
}
public Texture(Bitmap bitmap)
{
Gl.glGenTextures(1, out texture);
ErrorHandler.CheckGlError();
SetData(bitmap);
}
void FinalizeInner() { Gl.glDeleteTextures(1, ref texture); }
~Texture() { Game.RunAfterTick(FinalizeInner); }
void PrepareTexture()
{
ErrorHandler.CheckGlError();
Gl.glBindTexture(Gl.GL_TEXTURE_2D, texture);
ErrorHandler.CheckGlError();
Gl.glTexParameteri(Gl.GL_TEXTURE_2D, Gl.GL_TEXTURE_MAG_FILTER, Gl.GL_NEAREST);
ErrorHandler.CheckGlError();
Gl.glTexParameteri(Gl.GL_TEXTURE_2D, Gl.GL_TEXTURE_MIN_FILTER, Gl.GL_NEAREST);
ErrorHandler.CheckGlError();
Gl.glTexParameteri(Gl.GL_TEXTURE_2D, Gl.GL_TEXTURE_BASE_LEVEL, 0);
ErrorHandler.CheckGlError();
Gl.glTexParameteri(Gl.GL_TEXTURE_2D, Gl.GL_TEXTURE_MAX_LEVEL, 0);
ErrorHandler.CheckGlError();
}
public void SetData(byte[] colors, int width, int height)
{
if (!IsPowerOf2(width) || !IsPowerOf2(height))
throw new InvalidDataException("Non-power-of-two array {0}x{1}".F(width, height));
unsafe
{
fixed (byte* ptr = &colors[0])
{
IntPtr intPtr = new IntPtr((void*)ptr);
PrepareTexture();
Gl.glTexImage2D(Gl.GL_TEXTURE_2D, 0, Gl.GL_RGBA8, width, height,
0, Gl.GL_BGRA, Gl.GL_UNSIGNED_BYTE, intPtr);
ErrorHandler.CheckGlError();
}
}
}
// An array of RGBA
public void SetData(uint[,] colors)
{
int width = colors.GetUpperBound(1) + 1;
int height = colors.GetUpperBound(0) + 1;
if (!IsPowerOf2(width) || !IsPowerOf2(height))
throw new InvalidDataException("Non-power-of-two array {0}x{1}".F(width,height));
unsafe
{
fixed (uint* ptr = &colors[0,0])
{
IntPtr intPtr = new IntPtr((void *) ptr);
PrepareTexture();
Gl.glTexImage2D(Gl.GL_TEXTURE_2D, 0, Gl.GL_RGBA8, width, height,
0, Gl.GL_BGRA, Gl.GL_UNSIGNED_BYTE, intPtr);
ErrorHandler.CheckGlError();
}
}
}
public void SetData(Bitmap bitmap)
{
if (!IsPowerOf2(bitmap.Width) || !IsPowerOf2(bitmap.Height))
{
//throw new InvalidOperationException( "non-power-of-2-texture" );
bitmap = new Bitmap(bitmap, new Size(Exts.NextPowerOf2(bitmap.Width), Exts.NextPowerOf2(bitmap.Height)));
}
var bits = bitmap.LockBits(
new Rectangle(0, 0, bitmap.Width, bitmap.Height),
ImageLockMode.ReadOnly,
PixelFormat.Format32bppArgb);
PrepareTexture();
Gl.glTexImage2D(Gl.GL_TEXTURE_2D, 0, Gl.GL_RGBA8, bits.Width, bits.Height,
0, Gl.GL_BGRA, Gl.GL_UNSIGNED_BYTE, bits.Scan0); // todo: weird strides
ErrorHandler.CheckGlError();
bitmap.UnlockBits(bits);
}
bool IsPowerOf2(int v)
{
return (v & (v - 1)) == 0;
}
}
}

View File

@@ -0,0 +1,58 @@
#region Copyright & License Information
/*
* Copyright 2007-2011 The OpenRA Developers (see AUTHORS)
* This file is part of OpenRA, which is free software. It is made
* available to you under the terms of the GNU General Public License
* as published by the Free Software Foundation. For more information,
* see COPYING.
*/
#endregion
using System;
using System.Runtime.InteropServices;
using OpenRA.FileFormats.Graphics;
using Tao.OpenGl;
namespace OpenRA.Renderer.SdlCommon
{
public class VertexBuffer<T> : IVertexBuffer<T>
where T : struct
{
int buffer;
public VertexBuffer(int size)
{
Gl.glGenBuffers(1, out buffer);
ErrorHandler.CheckGlError();
Bind();
Gl.glBufferData(Gl.GL_ARRAY_BUFFER,
new IntPtr(Marshal.SizeOf(typeof(T)) * size),
new T[ size ],
Gl.GL_DYNAMIC_DRAW);
ErrorHandler.CheckGlError();
}
public void SetData(T[] data, int length)
{
Bind();
Gl.glBufferSubData(Gl.GL_ARRAY_BUFFER,
IntPtr.Zero,
new IntPtr(Marshal.SizeOf(typeof(T)) * length),
data);
ErrorHandler.CheckGlError();
}
public void Bind()
{
Gl.glBindBuffer(Gl.GL_ARRAY_BUFFER, buffer);
ErrorHandler.CheckGlError();
Gl.glVertexPointer(3, Gl.GL_FLOAT, Marshal.SizeOf(typeof(T)), IntPtr.Zero);
ErrorHandler.CheckGlError();
Gl.glTexCoordPointer(4, Gl.GL_FLOAT, Marshal.SizeOf(typeof(T)), new IntPtr(12));
ErrorHandler.CheckGlError();
}
void FinalizeInner() { Gl.glDeleteBuffers( 1, ref buffer ); }
~VertexBuffer() { Game.RunAfterTick( FinalizeInner ); }
}
}