Working on all OpenGL classes and working on hardcore

error checking integration (later on this will be disabled).
This commit is contained in:
SND\AstrorEnales_cp 2011-11-20 16:17:18 +00:00
parent 26f81c06a7
commit ad1bc47a24
11 changed files with 218 additions and 119 deletions

View File

@ -22,6 +22,7 @@
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<PlatformTarget>x86</PlatformTarget>
<GenerateSerializationAssemblies>Auto</GenerateSerializationAssemblies>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
@ -30,6 +31,7 @@
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<PlatformTarget>x86</PlatformTarget>
</PropertyGroup>
<ItemGroup>
<Reference Include="OpenTK">
@ -47,6 +49,7 @@
<Compile Include="EffectGL3.cs" />
<Compile Include="EffectParameterGL3.cs" />
<Compile Include="EffectTechniqueGL3.cs" />
<Compile Include="ErrorHelper.cs" />
<Compile Include="GraphicsDeviceWindowsGL3.cs" />
<Compile Include="IndexBufferGL3.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />

View File

@ -260,72 +260,6 @@ namespace ANX.Framework.Windows.GL3
}
#endregion
#region VertexElementFormatToVertexPointerType (TODO)
/// <summary>
/// Translate the XNA VertexElementFormat to an OpenGL VertexPointerType.
/// </summary>
/// <param name="format">XNA VertexElementFormat.</param>
/// <param name="size">Returns the size of the vertex element.</param>
/// <returns>Translated VertexPointerType for OpenGL.</returns>
public static VertexPointerType VertexElementFormatToVertexPointerType(
VertexElementFormat format, out int size)
{
switch (format)
{
default:
case VertexElementFormat.Vector2:
size = 2;
return VertexPointerType.Float;
case VertexElementFormat.Vector3:
size = 3;
return VertexPointerType.Float;
case VertexElementFormat.Vector4:
size = 4;
return VertexPointerType.Float;
case VertexElementFormat.Single:
size = 1;
return VertexPointerType.Float;
case VertexElementFormat.HalfVector2:
size = 2;
return VertexPointerType.HalfFloat;
case VertexElementFormat.HalfVector4:
size = 4;
return VertexPointerType.HalfFloat;
// TODO: check difference Short2/NormalizedShort2
case VertexElementFormat.NormalizedShort2:
size = 2;
return VertexPointerType.Short;
// TODO: check difference Short4/NormalizedShort4
case VertexElementFormat.NormalizedShort4:
size = 4;
return VertexPointerType.Short;
// TODO: check difference Short2/NormalizedShort2
case VertexElementFormat.Short2:
size = 2;
return VertexPointerType.Short;
// TODO: check difference Short4/NormalizedShort4
case VertexElementFormat.Short4:
size = 4;
return VertexPointerType.Short;
// TODO: check
case VertexElementFormat.Byte4:
case VertexElementFormat.Color:
size = 1;
return VertexPointerType.Int;
}
}
#endregion
#region Tests
private class Tests
{

View File

@ -71,7 +71,7 @@ namespace ANX.Framework.Windows.GL3
/// <summary>
/// The native shader handle.
/// </summary>
private int programHandle;
internal int programHandle;
#endregion
#region Public
@ -305,8 +305,13 @@ namespace ANX.Framework.Windows.GL3
#region Apply (TODO)
public void Apply(GraphicsDevice graphicsDevice)
{
GL.Enable(EnableCap.Texture2D);
GL.UseProgram(programHandle);
if (GraphicsDeviceWindowsGL3.activeEffect != this)
{
System.Diagnostics.Debug.WriteLine("GL: Shader.Apply");
GL.UseProgram(programHandle);
GraphicsDeviceWindowsGL3.activeEffect = this;
ErrorHelper.Check("UseProgram");
}
}
#endregion

View File

@ -101,22 +101,40 @@ namespace ANX.Framework.Windows.GL3
/// <param name="value">Value for the parameter</param>
public void SetValue(Matrix value)
{
GL.UniformMatrix4(UniformIndex, 16, false, ref value.M11);
GL.UseProgram(parentEffect.programHandle);
System.Diagnostics.Debug.WriteLine("GL: Setting Matrix uniform " + value);
OpenTK.Matrix4 matrix = new OpenTK.Matrix4(
value.M11, value.M12, value.M13, value.M14,
value.M21, value.M22, value.M23, value.M24,
value.M31, value.M32, value.M33, value.M34,
value.M41, value.M42, value.M43, value.M44);
GL.UniformMatrix4(UniformIndex, false, ref matrix);
ErrorHelper.Check("UniformMatrix4");
}
private Texture textureCache = null;
/// <summary>
/// Set a texture value to the effect parameter.
/// </summary>
/// <param name="value">Value for the parameter</param>
public void SetValue(Texture value)
{
// TODO: multiple texture units
TextureUnit textureUnit = TextureUnit.Texture0;
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);
GL.UseProgram(parentEffect.programHandle);
if (textureCache == null ||
textureCache != value)
{
// TODO: multiple texture units
TextureUnit textureUnit = TextureUnit.Texture0;
GL.Enable(EnableCap.Texture2D);
GL.ActiveTexture(textureUnit);
int handle = (value.NativeTexture as Texture2DGL3).NativeHandle;
System.Diagnostics.Debug.WriteLine("GL: Setting Texture uniform " + handle);
GL.BindTexture(TextureTarget.Texture2D, handle);
int unitIndex = (int)(textureUnit - TextureUnit.Texture0);
GL.Uniform1(UniformIndex, 1, ref unitIndex);
ErrorHelper.Check("Uniform1");
}
}
#endregion
}

View File

@ -0,0 +1,34 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using OpenTK.Graphics.OpenGL;
using System.Diagnostics;
using System.Reflection;
namespace ANX.Framework.Windows.GL3
{
internal static class ErrorHelper
{
public static void Check(string extraInformation = "")
{
ErrorCode code = GL.GetError();
if (code != ErrorCode.NoError)
{
string frameInfo = "";
foreach (StackFrame frame in new StackTrace().GetFrames())
{
MethodBase method = frame.GetMethod();
frameInfo += "\n\t" + "at " + method.DeclaringType + "." +
method + ":" + frame.GetFileLineNumber();
}
string message = "OpenGL Error '" + code + "' Checked at: '" +
extraInformation + "'" + frameInfo;
Console.WriteLine(message);
Debug.WriteLine(message);
}
}
}
}

View File

@ -74,6 +74,11 @@ namespace ANX.Framework.Windows.GL3
/// to the graphics device.
/// </summary>
private IWindowInfo nativeWindowInfo;
internal static VertexBufferGL3[] boundVertexBuffers =
new VertexBufferGL3[0];
internal static IndexBufferGL3 boundIndexBuffer;
internal static EffectGL3 activeEffect;
#endregion
#region Constructor
@ -158,6 +163,7 @@ namespace ANX.Framework.Windows.GL3
public void SetViewport(Viewport viewport)
{
GL.Viewport(viewport.X, viewport.Y, viewport.Width, viewport.Height);
ErrorHelper.Check("SetViewport");
}
#endregion
@ -177,6 +183,7 @@ namespace ANX.Framework.Windows.GL3
color.B * ColorMultiplier, color.A * ColorMultiplier);
}
GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
ErrorHelper.Check("Clear");
}
/// <summary>
@ -217,6 +224,7 @@ namespace ANX.Framework.Windows.GL3
GL.ClearDepth(depth);
GL.ClearStencil(stencil);
GL.Clear(mask);
ErrorHelper.Check("Clear");
}
#endregion
@ -234,60 +242,117 @@ namespace ANX.Framework.Windows.GL3
}
#endregion
#region DrawIndexedPrimitives
public void DrawIndexedPrimitives(PrimitiveType primitiveType,
int baseVertex, int minVertexIndex, int numVertices, int startIndex,
int primitiveCount)
{
// TODO: DrawElementsType, baseVertex, minVertexIndex, numVertices, startIndex
GL.DrawElements(DatatypesMapping.PrimitiveTypeToBeginMode(primitiveType),
numVertices, DrawElementsType.UnsignedShort, 0);
}
System.Diagnostics.Debug.WriteLine("GL: DrawIndexedPrimitives");
// TODO: baseVertex, minVertexIndex, numVertices, startIndex, primitiveCount
DrawElementsType elementsType =
boundIndexBuffer.elementSize == IndexElementSize.SixteenBits ?
DrawElementsType.UnsignedShort :
DrawElementsType.UnsignedInt;
GL.DrawElements(
DatatypesMapping.PrimitiveTypeToBeginMode(primitiveType),
numVertices, elementsType, 0);
ErrorHelper.Check("DrawElements");
}
#endregion
#region DrawInstancedPrimitives (TODO)
public void DrawInstancedPrimitives(PrimitiveType primitiveType,
int baseVertex, int minVertexIndex, int numVertices, int startIndex,
int primitiveCount, int instanceCount)
{
//GL.DrawArraysInstanced(
// DatatypesMapping.PrimitiveTypeToBeginMode(primitiveType),
// baseVertex, numVertices, instanceCount);
throw new NotImplementedException();
}
#endregion
#region DrawUserIndexedPrimitives (TODO)
public void DrawUserIndexedPrimitives<T>(PrimitiveType primitiveType,
T[] vertexData, int vertexOffset, int numVertices, Array indexData,
int indexOffset, int primitiveCount, VertexDeclaration vertexDeclaration,
IndexElementSize indexFormat) where T : struct, IVertexType
T[] vertexData, int vertexOffset, int numVertices, Array indexData,
int indexOffset, int primitiveCount, VertexDeclaration vertexDeclaration,
IndexElementSize indexFormat) where T : struct, IVertexType
{
//BeginMode mode = DatatypesMapping.PrimitiveTypeToBeginMode(primitiveType);
//if (indexFormat == IndexElementSize.SixteenBits)
//{
// ushort[] indices = new ushort[indexData.Length];
// indexData.CopyTo(indices, 0);
// GL.DrawElements(mode, numVertices, DrawElementsType.UnsignedShort,
// indices);
//}
//else
//{
// uint[] indices = new uint[indexData.Length];
// indexData.CopyTo(indices, 0);
// GL.DrawElements(mode, numVertices, DrawElementsType.UnsignedInt,
// indices);
//}
throw new NotImplementedException();
}
#endregion
#region DrawUserPrimitives (TODO)
public void DrawUserPrimitives<T>(PrimitiveType primitiveType,
T[] vertexData, int vertexOffset, int primitiveCount,
VertexDeclaration vertexDeclaration) where T : struct, IVertexType
{
throw new NotImplementedException();
}
#endregion
#region DrawPrimitives (TODO: check)
public void DrawPrimitives(PrimitiveType primitiveType, int vertexOffset,
int primitiveCount)
{
// TODO: DrawElementsType
GL.DrawElements(DatatypesMapping.PrimitiveTypeToBeginMode(primitiveType),
primitiveCount, DrawElementsType.UnsignedInt, vertexOffset);
GL.DrawArrays(
DatatypesMapping.PrimitiveTypeToBeginMode(primitiveType),
vertexOffset, primitiveCount);
}
#endregion
#region SetVertexBuffers
public void SetVertexBuffers(VertexBufferBinding[] vertexBuffers)
{
foreach (VertexBufferBinding binding in vertexBuffers)
boundVertexBuffers = new VertexBufferGL3[vertexBuffers.Length];
for (int index = 0; index < vertexBuffers.Length; index++)
{
INativeBuffer vertexBuffer = binding.VertexBuffer.NativeVertexBuffer;
System.Diagnostics.Debug.WriteLine("GL: SetVertexBuffer " + index);
boundVertexBuffers[index] =
(VertexBufferGL3)vertexBuffers[index].VertexBuffer.NativeVertexBuffer;
GL.BindBuffer(BufferTarget.ArrayBuffer,
((VertexBufferGL3)vertexBuffer).BufferHandle);
boundVertexBuffers[index].BufferHandle);
ErrorHelper.Check("BindBuffer");
boundVertexBuffers[index].MapVertexDeclaration(
activeEffect.programHandle);
}
}
#endregion
#region SetIndexBuffer
public void SetIndexBuffer(IndexBuffer indexBuffer)
{
GL.BindBuffer(BufferTarget.ElementArrayBuffer,
((IndexBufferGL3)indexBuffer.NativeIndexBuffer).BufferHandle);
IndexBufferGL3 nativeBuffer =
(IndexBufferGL3)indexBuffer.NativeIndexBuffer;
if (boundIndexBuffer != nativeBuffer)
{
System.Diagnostics.Debug.WriteLine("GL: SetIndexBuffer");
boundIndexBuffer = nativeBuffer;
GL.BindBuffer(BufferTarget.ElementArrayBuffer,
nativeBuffer.BufferHandle);
ErrorHelper.Check("BindBuffer");
}
}
#endregion
public void SetRenderTargets(params RenderTargetBinding[] renderTargets)
{

View File

@ -72,7 +72,7 @@ namespace ANX.Framework.Windows.GL3
private int indexCount;
private IndexElementSize elementSize;
internal IndexElementSize elementSize;
private BufferUsage usage;
@ -97,6 +97,7 @@ namespace ANX.Framework.Windows.GL3
usageHint = BufferUsageHint.DynamicDraw;
GL.GenBuffers(1, out bufferHandle);
ErrorHelper.Check("GenBuffers");
}
#endregion
@ -151,15 +152,18 @@ namespace ANX.Framework.Windows.GL3
2 : 4) * data.Length);
GL.BindBuffer(BufferTarget.ElementArrayBuffer, bufferHandle);
ErrorHelper.Check("BindBuffer");
if (offset != 0)
if (offset == 0)
{
GL.BufferData(BufferTarget.ElementArrayBuffer, size, data, usageHint);
ErrorHelper.Check("BufferData size=" + size);
}
else
{
GL.BufferSubData(BufferTarget.ElementArrayBuffer, (IntPtr)offset,
size, data);
ErrorHelper.Check("BufferSubData offset=" + offset + " size=" + size);
}
}
#endregion
@ -171,6 +175,7 @@ namespace ANX.Framework.Windows.GL3
public void Dispose()
{
GL.DeleteBuffers(1, ref bufferHandle);
ErrorHelper.Check("DeleteBuffers");
}
#endregion
}

View File

@ -174,6 +174,7 @@ namespace ANX.Framework.Windows.GL3
CullFaceMode.Front :
CullFaceMode.Back);
}
ErrorHelper.Check("Set CullMode");
#endregion
GL.PolygonMode(MaterialFace.FrontAndBack,
@ -188,6 +189,7 @@ namespace ANX.Framework.Windows.GL3
{
GL.Disable(EnableCap.ScissorTest);
}
ErrorHelper.Check("Set ScissorTest");
#endregion
#region DepthBias / SlopeScaleDepthBias (TODO: test!)
@ -206,6 +208,7 @@ namespace ANX.Framework.Windows.GL3
{
GL.Disable(EnableCap.PolygonOffsetFill);
}
ErrorHelper.Check("Set DepthBias");
#endregion
#region MultiSampleAntiAlias
@ -217,6 +220,7 @@ namespace ANX.Framework.Windows.GL3
{
GL.Disable(EnableCap.Multisample);
}
ErrorHelper.Check("Set Multisample");
#endregion
}
#endregion

View File

@ -110,10 +110,14 @@ namespace ANX.Framework.Windows.GL3
width = setWidth;
height = setHeight;
numberOfMipMaps = mipCount;
nativeFormat =
DatatypesMapping.SurfaceToPixelInternalFormat(surfaceFormat);
isCompressed = nativeFormat.ToString().StartsWith("Compressed");
NativeHandle = GL.GenTexture();
ErrorHelper.Check("GenTexture");
GL.BindTexture(TextureTarget.Texture2D, NativeHandle);
ErrorHelper.Check("BindTexture");
int wrapMode = (int)All.ClampToEdge;
int filter = (int)(mipCount > 1 ? All.LinearMipmapLinear : All.Linear);
@ -126,9 +130,7 @@ namespace ANX.Framework.Windows.GL3
TextureParameterName.TextureMagFilter, filter);
GL.TexParameter(TextureTarget.Texture2D,
TextureParameterName.TextureMinFilter, filter);
nativeFormat =
DatatypesMapping.SurfaceToPixelInternalFormat(surfaceFormat);
ErrorHelper.Check("TexParameter");
}
#endregion
@ -171,12 +173,14 @@ namespace ANX.Framework.Windows.GL3
{
GL.CompressedTexImage2D(TextureTarget.Texture2D, 0, nativeFormat,
width, height, 0, mipmapByteSize, dataPointer);
ErrorHelper.Check("CompressedTexImage2D Format=" + nativeFormat);
}
else
{
GL.TexImage2D(TextureTarget.Texture2D, 0, nativeFormat,
width, height, 0, (PixelFormat)nativeFormat,
PixelType.UnsignedByte, dataPointer);
ErrorHelper.Check("TexImage2D Format=" + nativeFormat);
}
int mipmapWidth = width;
@ -195,12 +199,14 @@ namespace ANX.Framework.Windows.GL3
GL.CompressedTexImage2D(TextureTarget.Texture2D, index,
nativeFormat, width, height, 0, mipmapByteSize,
dataPointer);
ErrorHelper.Check("CompressedTexImage2D Format=" + nativeFormat);
}
else
{
GL.TexImage2D(TextureTarget.Texture2D, index, nativeFormat,
mipmapWidth, mipmapHeight, 0, (PixelFormat)nativeFormat,
PixelType.UnsignedByte, dataPointer);
ErrorHelper.Check("TexImage2D Format=" + nativeFormat);
}
}
}
@ -218,6 +224,7 @@ namespace ANX.Framework.Windows.GL3
public void Dispose()
{
GL.DeleteTexture(NativeHandle);
ErrorHelper.Check("DeleteTexture");
}
#endregion
}

View File

@ -100,9 +100,12 @@ namespace ANX.Framework.Windows.GL3
usageHint = BufferUsageHint.DynamicDraw;
GL.GenBuffers(1, out bufferHandle);
ErrorHelper.Check("GenBuffers");
GL.BindBuffer(BufferTarget.ArrayBuffer, bufferHandle);
ErrorHelper.Check("BindBuffer");
IntPtr size = (IntPtr)(vertexDeclaration.VertexStride * setVertexCount);
GL.BufferData(BufferTarget.ArrayBuffer, size, IntPtr.Zero, usageHint);
ErrorHelper.Check("BufferData");
}
#endregion
@ -156,42 +159,62 @@ namespace ANX.Framework.Windows.GL3
IntPtr size = (IntPtr)(vertexDeclaration.VertexStride * data.Length);
GL.BindBuffer(BufferTarget.ArrayBuffer, bufferHandle);
// TODO: check the handling with MapBuffer etc. (See link above)
MapVertexDeclaration();
ErrorHelper.Check("BindBuffer");
GL.BufferSubData(BufferTarget.ArrayBuffer, (IntPtr)offset, size, data);
ErrorHelper.Check("BufferSubData");
}
#endregion
#region MapVertexDeclaration
private void MapVertexDeclaration()
internal void MapVertexDeclaration(int programHandle)
{
foreach (VertexElement element in vertexDeclaration.GetVertexElements())
{
int size;
VertexPointerType type =
DatatypesMapping.VertexElementFormatToVertexPointerType(
element.VertexElementFormat, out size);
// TODO: element.UsageIndex?
switch (element.VertexElementUsage)
{
case VertexElementUsage.Position:
GL.EnableClientState(ArrayCap.VertexArray);
int loc = GL.GetAttribLocation(programHandle, "pos");
GL.EnableVertexAttribArray(loc);
GL.VertexAttribPointer(loc, 3, VertexAttribPointerType.Float,
false, vertexDeclaration.VertexStride, element.Offset);
ErrorHelper.Check();
break;
// case VertexElementUsage.Color:
//GL.EnableClientState(ArrayCap.ColorArray);
// break;
// case VertexElementUsage.TextureCoordinate:
//GL.EnableClientState(ArrayCap.TextureCoordArray);
// break;
// TODO more
}
// TODO: usage index?
GL.VertexPointer(size, type, vertexDeclaration.VertexStride,
element.Offset);
case VertexElementUsage.Color:
GL.EnableClientState(ArrayCap.ColorArray);
int col = GL.GetAttribLocation(programHandle, "col");
GL.EnableVertexAttribArray(col);
GL.VertexAttribPointer(col, 4, VertexAttribPointerType.Float,
false, vertexDeclaration.VertexStride, element.Offset);
ErrorHelper.Check();
break;
case VertexElementUsage.TextureCoordinate:
GL.EnableClientState(ArrayCap.TextureCoordArray);
int tex = GL.GetAttribLocation(programHandle, "tex");
GL.EnableVertexAttribArray(tex);
GL.VertexAttribPointer(tex, 2, VertexAttribPointerType.Float,
false, vertexDeclaration.VertexStride, element.Offset);
ErrorHelper.Check();
break;
// TODO
case VertexElementUsage.Binormal:
case VertexElementUsage.BlendIndices:
case VertexElementUsage.BlendWeight:
case VertexElementUsage.Depth:
case VertexElementUsage.Fog:
case VertexElementUsage.Normal:
case VertexElementUsage.PointSize:
case VertexElementUsage.Sample:
case VertexElementUsage.Tangent:
case VertexElementUsage.TessellateFactor:
throw new NotImplementedException();
}
}
}
#endregion
@ -205,5 +228,5 @@ namespace ANX.Framework.Windows.GL3
GL.DeleteBuffers(1, ref bufferHandle);
}
#endregion
}
}
}

View File

@ -46,6 +46,7 @@
//
uniform mat4 MatrixTransform;
attribute vec4 pos;
attribute vec4 col;
attribute vec2 tex;
@ -66,9 +67,9 @@ void main(void)
//
uniform sampler2D Texture;
varying vec4 diffuseColor;
varying vec2 diffuseTexCoord;
void main(void)
{
gl_FragColor = texture2D(Texture, diffuseTexCoord) * diffuseColor;