using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using ANX.Framework;
using ANX.Framework.Graphics;
using ANX.Framework.NonXNA;
using ANX.Framework.NonXNA.Development;
using ANX.Framework.NonXNA.RenderSystem;
using ANX.Framework.GL3;
using OpenTK;
using OpenTK.Graphics.OpenGL;
// This file is part of the ANX.Framework created by the
// "ANX.Framework developer group" and released under the Ms-PL license.
// For details see: http://anxframework.codeplex.com/license
namespace ANX.RenderSystem.GL3
{
[PercentageComplete(90)]
[TestState(TestStateAttribute.TestState.Untested)]
[Developer("AstrorEnales")]
public class Creator : IRenderSystemCreator
{
#region Public
public string Name
{
get
{
return "OpenGL3";
}
}
public int Priority
{
get
{
return 100;
}
}
public bool IsSupported
{
get
{
//TODO: this is just a very basic version of test for support
PlatformName os = OSInformation.GetName();
return OSInformation.IsWindows ||
os == PlatformName.Linux ||
os == PlatformName.MacOSX;
}
}
public EffectSourceLanguage GetStockShaderSourceLanguage
{
get
{
return EffectSourceLanguage.GLSL_FX;
}
}
#endregion
#region CreateEffect
public INativeEffect CreateEffect(GraphicsDevice graphics, Effect managedEffect,
Stream byteCode)
{
AddInSystemFactory.Instance.PreventSystemChange(
AddInType.RenderSystem);
return new EffectGL3(managedEffect, byteCode);
}
public INativeEffect CreateEffect(GraphicsDevice graphics, Effect managedEffect,
Stream vertexShaderByteCode, Stream pixelShaderByteCode)
{
AddInSystemFactory.Instance.PreventSystemChange(
AddInType.RenderSystem);
return new EffectGL3(managedEffect, vertexShaderByteCode, pixelShaderByteCode);
}
#endregion
#region CreateGraphicsDevice
INativeGraphicsDevice IRenderSystemCreator.CreateGraphicsDevice(PresentationParameters presentationParameters)
{
AddInSystemFactory.Instance.PreventSystemChange(AddInType.RenderSystem);
return new GraphicsDeviceWindowsGL3(presentationParameters);
}
#endregion
#region CreateTexture
///
/// Create a new native texture.
///
/// Graphics device.
/// The format of the texture.
/// The width of the texture.
/// The height of the texture.
/// The number of mipmaps in the texture.
///
public INativeTexture2D CreateTexture(GraphicsDevice graphics,
SurfaceFormat surfaceFormat, int width, int height, int mipCount)
{
AddInSystemFactory.Instance.PreventSystemChange(AddInType.RenderSystem);
return new Texture2DGL3(surfaceFormat, width, height, mipCount);
}
#endregion
#region CreateIndexBuffer
///
/// Create a native index buffer.
///
/// The current graphics device.
/// The size of a single index element.
/// The number of indices stored in the buffer.
///
/// The usage type of the buffer.
/// Native OpenGL index buffer.
public INativeIndexBuffer CreateIndexBuffer(GraphicsDevice graphics,
IndexBuffer managedBuffer, IndexElementSize size, int indexCount,
BufferUsage usage)
{
AddInSystemFactory.Instance.PreventSystemChange(AddInType.RenderSystem);
return new IndexBufferGL3(managedBuffer, size, indexCount, usage);
}
public INativeIndexBuffer CreateDynamicIndexBuffer(GraphicsDevice graphics, IndexBuffer managedBuffer, IndexElementSize size, int indexCount, BufferUsage usage)
{
AddInSystemFactory.Instance.PreventSystemChange(AddInType.RenderSystem);
return new IndexBufferGL3(managedBuffer, size, indexCount, usage);
}
#endregion
#region CreateVertexBuffer
///
/// Create a native vertex buffer.
///
/// The current graphics device.
/// The vertex declaration for the buffer.
/// The number of vertices stored in the buffer.
///
/// The usage type of the buffer.
/// Native OpenGL vertex buffer.
public INativeVertexBuffer CreateVertexBuffer(GraphicsDevice graphics,
VertexBuffer managedBuffer, VertexDeclaration vertexDeclaration,
int vertexCount, BufferUsage usage)
{
AddInSystemFactory.Instance.PreventSystemChange(AddInType.RenderSystem);
return new VertexBufferGL3(managedBuffer, vertexDeclaration, vertexCount,
usage);
}
public INativeVertexBuffer CreateDynamicVertexBuffer(GraphicsDevice graphics, DynamicVertexBuffer managedBuffer, VertexDeclaration vertexDeclaration, int vertexCount, BufferUsage usage)
{
AddInSystemFactory.Instance.PreventSystemChange(AddInType.RenderSystem);
return new VertexBufferGL3(managedBuffer, vertexDeclaration, vertexCount,
usage);
}
#endregion
#if XNAEXT
#region CreateConstantBuffer (TODO)
public INativeConstantBuffer CreateConstantBuffer(GraphicsDevice graphics, ConstantBuffer managedBuffer,
BufferUsage usage)
{
AddInSystemFactory.Instance.PreventSystemChange(AddInType.RenderSystem);
throw new NotImplementedException();
}
#endregion
#endif
#region CreateBlendState
///
/// Create a new native blend state.
///
/// Native Blend State.
public INativeBlendState CreateBlendState()
{
AddInSystemFactory.Instance.PreventSystemChange(AddInType.RenderSystem);
return new BlendStateGL3();
}
#endregion
#region CreateBlendState
///
/// Create a new native rasterizer state.
///
/// Native Rasterizer State.
public INativeRasterizerState CreateRasterizerState()
{
AddInSystemFactory.Instance.PreventSystemChange(AddInType.RenderSystem);
return new RasterizerStateGL3();
}
#endregion
#region CreateDepthStencilState
///
/// Create a new native Depth Stencil State.
///
/// Native Depth Stencil State.
public INativeDepthStencilState CreateDepthStencilState()
{
AddInSystemFactory.Instance.PreventSystemChange(AddInType.RenderSystem);
return new DepthStencilStateGL3();
}
#endregion
#region CreateSamplerState
///
/// Create a new native sampler state.
///
/// Native Sampler State.
public INativeSamplerState CreateSamplerState()
{
AddInSystemFactory.Instance.PreventSystemChange(AddInType.RenderSystem);
return new SamplerStateGL3();
}
#endregion
#region GetShaderByteCode
///
/// Get the byte code of a pre defined shader.
///
/// Pre defined shader type.
/// Byte code of the shader.
public byte[] GetShaderByteCode(PreDefinedShader type)
{
AddInSystemFactory.Instance.PreventSystemChange(AddInType.RenderSystem);
if (type == PreDefinedShader.SpriteBatch)
{
return ShaderByteCode.SpriteBatchByteCode;
}
else if (type == PreDefinedShader.AlphaTestEffect)
{
return ShaderByteCode.AlphaTestEffectByteCode;
}
else if (type == PreDefinedShader.BasicEffect)
{
return ShaderByteCode.BasicEffectByteCode;
}
else if (type == PreDefinedShader.DualTextureEffect)
{
return ShaderByteCode.DualTextureEffectByteCode;
}
else if (type == PreDefinedShader.EnvironmentMapEffect)
{
return ShaderByteCode.EnvironmentMapEffectByteCode;
}
else if (type == PreDefinedShader.SkinnedEffect)
{
return ShaderByteCode.SkinnedEffectByteCode;
}
throw new NotImplementedException("ByteCode for '" + type.ToString() + "' is not yet available");
}
#endregion
#region GetAdapterList (TODO)
///
/// Get a list of available graphics adapter information.
///
/// List of graphics adapters.
public ReadOnlyCollection GetAdapterList()
{
AddInSystemFactory.Instance.PreventSystemChange(AddInType.RenderSystem);
var result = new List();
foreach (DisplayDevice device in DisplayDevice.AvailableDisplays)
{
var resultingModes = new List();
foreach (string format in Enum.GetNames(typeof(SurfaceFormat)))
{
SurfaceFormat surfaceFormat = (SurfaceFormat)Enum.Parse(typeof(SurfaceFormat), format);
// TODO: device.BitsPerPixel
if (surfaceFormat != SurfaceFormat.Color)//adapter.Supports(surfaceFormat) == false)
{
continue;
}
}
var newAdapter = new GraphicsAdapterGL3(device, SurfaceFormat.Color);
result.Add(newAdapter);
}
return new ReadOnlyCollection(result);
}
#endregion
#region CreateRenderTarget
public INativeRenderTarget2D CreateRenderTarget(GraphicsDevice graphics,
int width, int height, bool mipMap, SurfaceFormat preferredFormat,
DepthFormat preferredDepthFormat, int preferredMultiSampleCount,
RenderTargetUsage usage)
{
AddInSystemFactory.Instance.PreventSystemChange(AddInType.RenderSystem);
return new RenderTarget2DGL3(width, height, mipMap, preferredFormat,
preferredDepthFormat, preferredMultiSampleCount, usage);
}
#endregion
#region IsLanguageSupported
public bool IsLanguageSupported(EffectSourceLanguage sourceLanguage)
{
return sourceLanguage == EffectSourceLanguage.GLSL_FX || sourceLanguage == EffectSourceLanguage.GLSL;
}
#endregion
#region CreateOcclusionQuery
public IOcclusionQuery CreateOcclusionQuery()
{
return new OcclusionQueryGL3();
}
#endregion
#region SetTextureSampler (TODO)
public void SetTextureSampler(int index, Texture value)
{
TextureUnit textureUnit = TextureUnit.Texture0 + index;
GL.ActiveTexture(textureUnit);
int handle = (value.NativeTexture as Texture2DGL3).NativeHandle;
GL.BindTexture(TextureTarget.Texture2D, handle);
int unitIndex = (int)(textureUnit - TextureUnit.Texture0);
//GL.Uniform1(UniformIndex, 1, ref unitIndex);
}
#endregion
}
}