start moving common parts of the renderer into OpenRA.Renderer.SdlCommon
This commit is contained in:
22
Makefile
22
Makefile
@@ -37,30 +37,38 @@ game: $(game_TARGET)
|
||||
#
|
||||
# Renderer dlls
|
||||
#
|
||||
rsdl_SRCS := $(shell find OpenRA.Renderer.SdlCommon/ -iname '*.cs')
|
||||
rsdl_TARGET = OpenRA.Renderer.SdlCommon.dll
|
||||
rsdl_KIND = library
|
||||
rsdl_DEPS = $(fileformats_TARGET) $(game_TARGET)
|
||||
rsdl_LIBS = $(COMMON_LIBS) System.Windows.Forms.dll \
|
||||
thirdparty/Tao/Tao.OpenGl.dll thirdparty/Tao/Tao.Sdl.dll \
|
||||
$(rsdl_DEPS)
|
||||
|
||||
rcg_SRCS := $(shell find OpenRA.Renderer.Cg/ -iname '*.cs')
|
||||
rcg_TARGET = OpenRA.Renderer.Cg.dll
|
||||
rcg_KIND = library
|
||||
rcg_DEPS = $(fileformats_TARGET) $(game_TARGET)
|
||||
rcg_DEPS = $(fileformats_TARGET) $(game_TARGET) $(rsdl_TARGET)
|
||||
rcg_LIBS = $(COMMON_LIBS) System.Windows.Forms.dll \
|
||||
thirdparty/Tao/Tao.Cg.dll thirdparty/Tao/Tao.OpenGl.dll thirdparty/Tao/Tao.Sdl.dll \
|
||||
$(rcg_DEPS) $(game_TARGET)
|
||||
$(rcg_DEPS)
|
||||
|
||||
rgl_SRCS := $(shell find OpenRA.Renderer.Gl/ -iname '*.cs')
|
||||
rgl_TARGET = OpenRA.Renderer.Gl.dll
|
||||
rgl_KIND = library
|
||||
rgl_DEPS = $(fileformats_TARGET) $(game_TARGET)
|
||||
rgl_DEPS = $(fileformats_TARGET) $(game_TARGET) $(rsdl_TARGET)
|
||||
rgl_LIBS = $(COMMON_LIBS) System.Windows.Forms.dll \
|
||||
thirdparty/Tao/Tao.Cg.dll thirdparty/Tao/Tao.OpenGl.dll thirdparty/Tao/Tao.Sdl.dll \
|
||||
$(rgl_DEPS) $(game_TARGET)
|
||||
$(rgl_DEPS)
|
||||
|
||||
rnull_SRCS := $(shell find OpenRA.Renderer.Null/ -iname '*.cs')
|
||||
rnull_TARGET = OpenRA.Renderer.Null.dll
|
||||
rnull_KIND = library
|
||||
rnull_DEPS = $(fileformats_TARGET) $(game_TARGET)
|
||||
rnull_LIBS = $(COMMON_LIBS) System.Windows.Forms.dll \
|
||||
$(rnull_DEPS) $(game_TARGET)
|
||||
PROGRAMS += rcg rgl rnull
|
||||
renderers: $(rcg_TARGET) $(rgl_TARGET) $(rnull_TARGET)
|
||||
$(rnull_DEPS)
|
||||
PROGRAMS += rcg rgl rnull rsdl
|
||||
renderers: $(rcg_TARGET) $(rgl_TARGET) $(rnull_TARGET) $(rsdl_TARGET)
|
||||
|
||||
#
|
||||
# Official Mods
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Project ToolsVersion="3.5" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||
<PropertyGroup>
|
||||
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Project ToolsVersion="3.5" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||
<PropertyGroup>
|
||||
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
|
||||
@@ -70,6 +70,10 @@
|
||||
<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>
|
||||
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
|
||||
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
|
||||
|
||||
@@ -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();
|
||||
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; }
|
||||
|
||||
@@ -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>
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
58
OpenRA.Renderer.SdlCommon/ErrorHandler.cs
Normal file
58
OpenRA.Renderer.SdlCommon/ErrorHandler.cs
Normal 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));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
122
OpenRA.Renderer.SdlCommon/Texture.cs
Normal file
122
OpenRA.Renderer.SdlCommon/Texture.cs
Normal 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
58
OpenRA.Renderer.SdlCommon/VertexBuffer.cs
Normal file
58
OpenRA.Renderer.SdlCommon/VertexBuffer.cs
Normal 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 ); }
|
||||
}
|
||||
}
|
||||
10
OpenRA.sln
10
OpenRA.sln
@@ -33,6 +33,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "OpenRA.Renderer.Null", "Ope
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "OpenRA.Renderer.Gl", "OpenRA.Renderer.Gl\OpenRA.Renderer.Gl.csproj", "{E9C01A96-C3E2-4772-825B-A740AC513D31}"
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "OpenRA.Renderer.SdlCommon", "OpenRA.Renderer.SdlCommon\OpenRA.Renderer.SdlCommon.csproj", "{52FD9F0B-B209-4ED7-8A32-AC8033363263}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
Debug|Any CPU = Debug|Any CPU
|
||||
@@ -96,6 +98,14 @@ Global
|
||||
{4A8A43B5-A9EF-4ED0-99DD-4BAB10A0DB6E}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{4A8A43B5-A9EF-4ED0-99DD-4BAB10A0DB6E}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU
|
||||
{4A8A43B5-A9EF-4ED0-99DD-4BAB10A0DB6E}.Release|Mixed Platforms.Build.0 = Release|Any CPU
|
||||
{52FD9F0B-B209-4ED7-8A32-AC8033363263}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{52FD9F0B-B209-4ED7-8A32-AC8033363263}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{52FD9F0B-B209-4ED7-8A32-AC8033363263}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU
|
||||
{52FD9F0B-B209-4ED7-8A32-AC8033363263}.Debug|Mixed Platforms.Build.0 = Debug|Any CPU
|
||||
{52FD9F0B-B209-4ED7-8A32-AC8033363263}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{52FD9F0B-B209-4ED7-8A32-AC8033363263}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{52FD9F0B-B209-4ED7-8A32-AC8033363263}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU
|
||||
{52FD9F0B-B209-4ED7-8A32-AC8033363263}.Release|Mixed Platforms.Build.0 = Release|Any CPU
|
||||
{56B1073B-AE14-499A-BB98-43A8DE9A39CA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{56B1073B-AE14-499A-BB98-43A8DE9A39CA}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{56B1073B-AE14-499A-BB98-43A8DE9A39CA}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU
|
||||
|
||||
Reference in New Issue
Block a user