- Added the parent Vertex and IndexBuffer to the Native implementation Create methods.

- OpenGL Vertex and IndexBuffer now decide whether it's a dynamic or a static buffer.
This commit is contained in:
SND\AstrorEnales_cp 2012-02-19 10:32:17 +00:00
parent 772d4be8d3
commit 30499fac83
11 changed files with 127 additions and 49 deletions

View File

@ -71,8 +71,8 @@ namespace ANX.Framework.Graphics
this.indexCount = indexCount; this.indexCount = indexCount;
this.bufferUsage = usage; this.bufferUsage = usage;
base.GraphicsDevice.ResourceCreated += new EventHandler<ResourceCreatedEventArgs>(GraphicsDevice_ResourceCreated); base.GraphicsDevice.ResourceCreated += GraphicsDevice_ResourceCreated;
base.GraphicsDevice.ResourceDestroyed += new EventHandler<ResourceDestroyedEventArgs>(GraphicsDevice_ResourceDestroyed); base.GraphicsDevice.ResourceDestroyed += GraphicsDevice_ResourceDestroyed;
CreateNativeBuffer(); CreateNativeBuffer();
} }
@ -128,7 +128,8 @@ namespace ANX.Framework.Graphics
private void CreateNativeBuffer() private void CreateNativeBuffer()
{ {
this.nativeIndexBuffer = AddInSystemFactory.Instance.GetDefaultCreator<IRenderSystemCreator>().CreateIndexBuffer(GraphicsDevice, indexElementSize, indexCount, bufferUsage); this.nativeIndexBuffer =
AddInSystemFactory.Instance.GetDefaultCreator<IRenderSystemCreator>().CreateIndexBuffer(GraphicsDevice, this, indexElementSize, indexCount, bufferUsage);
} }
public void GetData<T>(int offsetInBytes, T[] data, int startIndex, int elementCount) where T : struct public void GetData<T>(int offsetInBytes, T[] data, int startIndex, int elementCount) where T : struct

View File

@ -76,8 +76,8 @@ namespace ANX.Framework.Graphics
this.vertexDeclaration = vertexDeclaration; this.vertexDeclaration = vertexDeclaration;
this.bufferUsage = usage; this.bufferUsage = usage;
base.GraphicsDevice.ResourceCreated += new EventHandler<ResourceCreatedEventArgs>(GraphicsDevice_ResourceCreated); base.GraphicsDevice.ResourceCreated += GraphicsDevice_ResourceCreated;
base.GraphicsDevice.ResourceDestroyed += new EventHandler<ResourceDestroyedEventArgs>(GraphicsDevice_ResourceDestroyed); base.GraphicsDevice.ResourceDestroyed += GraphicsDevice_ResourceDestroyed;
CreateNativeBuffer(); CreateNativeBuffer();
} }
@ -111,7 +111,8 @@ namespace ANX.Framework.Graphics
private void CreateNativeBuffer() private void CreateNativeBuffer()
{ {
this.nativeVertexBuffer = AddInSystemFactory.Instance.GetDefaultCreator<IRenderSystemCreator>().CreateVertexBuffer(GraphicsDevice, vertexDeclaration, vertexCount, bufferUsage); this.nativeVertexBuffer =
AddInSystemFactory.Instance.GetDefaultCreator<IRenderSystemCreator>().CreateVertexBuffer(GraphicsDevice, this, vertexDeclaration, vertexCount, bufferUsage);
} }
public BufferUsage BufferUsage public BufferUsage BufferUsage

View File

@ -67,9 +67,9 @@ namespace ANX.Framework.NonXNA
INativeRenderTarget2D CreateRenderTarget(GraphicsDevice graphics, int width, int height, bool mipMap, SurfaceFormat preferredFormat, DepthFormat preferredDepthFormat, int preferredMultiSampleCount, RenderTargetUsage usage); INativeRenderTarget2D CreateRenderTarget(GraphicsDevice graphics, int width, int height, bool mipMap, SurfaceFormat preferredFormat, DepthFormat preferredDepthFormat, int preferredMultiSampleCount, RenderTargetUsage usage);
INativeBuffer CreateIndexBuffer(GraphicsDevice graphics, IndexElementSize size, int indexCount, BufferUsage usage); INativeBuffer CreateIndexBuffer(GraphicsDevice graphics, IndexBuffer managedBuffer, IndexElementSize size, int indexCount, BufferUsage usage);
INativeBuffer CreateVertexBuffer(GraphicsDevice graphics, VertexDeclaration vertexDeclaration, int vertexCount, BufferUsage usage); INativeBuffer CreateVertexBuffer(GraphicsDevice graphics, VertexBuffer managedBuffer, VertexDeclaration vertexDeclaration, int vertexCount, BufferUsage usage);
INativeEffect CreateEffect(GraphicsDevice graphics, Effect managedEffect, Stream byteCode); INativeEffect CreateEffect(GraphicsDevice graphics, Effect managedEffect, Stream byteCode);
INativeEffect CreateEffect(GraphicsDevice graphics, Effect managedEffect, Stream vertexShaderByteCode, Stream pixelShaderByteCode); INativeEffect CreateEffect(GraphicsDevice graphics, Effect managedEffect, Stream vertexShaderByteCode, Stream pixelShaderByteCode);

View File

@ -96,14 +96,16 @@ namespace ANX.Framework.Windows.DX10
return new GraphicsDeviceWindowsDX10(presentationParameters); return new GraphicsDeviceWindowsDX10(presentationParameters);
} }
public INativeBuffer CreateIndexBuffer(GraphicsDevice graphics, IndexElementSize size, int indexCount, BufferUsage usage) public INativeBuffer CreateIndexBuffer(GraphicsDevice graphics,
IndexBuffer managedBuffer, IndexElementSize size, int indexCount, BufferUsage usage)
{ {
AddInSystemFactory.Instance.PreventSystemChange( AddInSystemFactory.Instance.PreventSystemChange(
AddInType.RenderSystem); AddInType.RenderSystem);
return new IndexBuffer_DX10(graphics, size, indexCount, usage); return new IndexBuffer_DX10(graphics, size, indexCount, usage);
} }
public INativeBuffer CreateVertexBuffer(GraphicsDevice graphics, VertexDeclaration vertexDeclaration, int vertexCount, BufferUsage usage) public INativeBuffer CreateVertexBuffer(GraphicsDevice graphics,
VertexBuffer managedBuffer, VertexDeclaration vertexDeclaration, int vertexCount, BufferUsage usage)
{ {
AddInSystemFactory.Instance.PreventSystemChange( AddInSystemFactory.Instance.PreventSystemChange(
AddInType.RenderSystem); AddInType.RenderSystem);

View File

@ -173,10 +173,11 @@ namespace ANX.Framework.Windows.GL3
/// <param name="usage">The usage type of the buffer.</param> /// <param name="usage">The usage type of the buffer.</param>
/// <returns>Native OpenGL index buffer.</returns> /// <returns>Native OpenGL index buffer.</returns>
public INativeBuffer CreateIndexBuffer(GraphicsDevice graphics, public INativeBuffer CreateIndexBuffer(GraphicsDevice graphics,
IndexElementSize size, int indexCount, BufferUsage usage) IndexBuffer managedBuffer, IndexElementSize size, int indexCount,
BufferUsage usage)
{ {
AddInSystemFactory.Instance.PreventSystemChange(AddInType.RenderSystem); AddInSystemFactory.Instance.PreventSystemChange(AddInType.RenderSystem);
return new IndexBufferGL3(size, indexCount, usage); return new IndexBufferGL3(managedBuffer, size, indexCount, usage);
} }
#endregion #endregion
@ -191,11 +192,12 @@ namespace ANX.Framework.Windows.GL3
/// <param name="usage">The usage type of the buffer.</param> /// <param name="usage">The usage type of the buffer.</param>
/// <returns>Native OpenGL vertex buffer.</returns> /// <returns>Native OpenGL vertex buffer.</returns>
public INativeBuffer CreateVertexBuffer(GraphicsDevice graphics, public INativeBuffer CreateVertexBuffer(GraphicsDevice graphics,
VertexDeclaration vertexDeclaration, int vertexCount, VertexBuffer managedBuffer, VertexDeclaration vertexDeclaration,
BufferUsage usage) int vertexCount, BufferUsage usage)
{ {
AddInSystemFactory.Instance.PreventSystemChange(AddInType.RenderSystem); AddInSystemFactory.Instance.PreventSystemChange(AddInType.RenderSystem);
return new VertexBufferGL3(vertexDeclaration, vertexCount, usage); return new VertexBufferGL3(managedBuffer, vertexDeclaration, vertexCount,
usage);
} }
#endregion #endregion

View File

@ -59,6 +59,8 @@ namespace ANX.Framework.Windows.GL3
public class IndexBufferGL3 : INativeBuffer public class IndexBufferGL3 : INativeBuffer
{ {
#region Private #region Private
private IndexBuffer managedBuffer;
private int bufferHandle; private int bufferHandle;
/// <summary> /// <summary>
/// Native index buffer handle. /// Native index buffer handle.
@ -86,17 +88,17 @@ namespace ANX.Framework.Windows.GL3
/// <summary> /// <summary>
/// Create a new Index Buffer object. /// Create a new Index Buffer object.
/// </summary> /// </summary>
internal IndexBufferGL3(IndexElementSize setElementSize, internal IndexBufferGL3(IndexBuffer setManagedBuffer,
int setIndexCount, BufferUsage setUsage) IndexElementSize setElementSize, int setIndexCount, BufferUsage setUsage)
{ {
GraphicsResourceManager.UpdateResource(this, true); GraphicsResourceManager.UpdateResource(this, true);
managedBuffer = setManagedBuffer;
indexCount = setIndexCount; indexCount = setIndexCount;
elementSize = setElementSize; elementSize = setElementSize;
usage = setUsage; usage = setUsage;
// TODO: check if dynamic buffer bool isDynamicBuffer = managedBuffer is DynamicIndexBuffer;
bool isDynamicBuffer = false;
usageHint = isDynamicBuffer ? usageHint = isDynamicBuffer ?
BufferUsageHint.DynamicDraw : BufferUsageHint.DynamicDraw :

View File

@ -1,7 +1,6 @@
using System; using ANX.Framework.Graphics;
using ANX.Framework.Graphics;
using ANX.Framework.NonXNA; using ANX.Framework.NonXNA;
using OpenTK.Graphics.OpenGL; using ANX.Framework.NonXNA.Development;
#region License #region License
@ -64,6 +63,8 @@ namespace ANX.Framework.Windows.GL3
/// Info for OGL 3.3 sampler objects (sadly not implemented in OpenTK yet): /// Info for OGL 3.3 sampler objects (sadly not implemented in OpenTK yet):
/// http://www.sinanc.org/blog/?p=215 /// http://www.sinanc.org/blog/?p=215
/// </summary> /// </summary>
[PercentageComplete(10)]
[TestState(TestStateAttribute.TestState.Untested)]
public class SamplerStateGL3 : INativeSamplerState public class SamplerStateGL3 : INativeSamplerState
{ {
#region Public #region Public
@ -150,7 +151,7 @@ namespace ANX.Framework.Windows.GL3
/// Apply the sampler state. /// Apply the sampler state.
/// </summary> /// </summary>
/// <param name="graphicsDevice">Graphics device.</param> /// <param name="graphicsDevice">Graphics device.</param>
/// <param name="index"></param> /// <param name="index">The index of which sampler should be modified.</param>
public void Apply(GraphicsDevice graphicsDevice, int index) public void Apply(GraphicsDevice graphicsDevice, int index)
{ {
IsBound = true; IsBound = true;

View File

@ -115,6 +115,7 @@ namespace ANX.Framework.Windows.GL3
// empty lines, spaces and tabs at beginning and end and also // empty lines, spaces and tabs at beginning and end and also
// remove comments. // remove comments.
List<string> lines = new List<string>(input.Split('\n')); List<string> lines = new List<string>(input.Split('\n'));
input = "";
for (int index = lines.Count - 1; index >= 0; index--) for (int index = lines.Count - 1; index >= 0; index--)
{ {
lines[index] = lines[index].Trim(); lines[index] = lines[index].Trim();
@ -125,14 +126,50 @@ namespace ANX.Framework.Windows.GL3
continue; continue;
} }
// TODO: add /**/ comment checking and removing. input = lines[index] + input;
} }
input = ""; #region Multiline comment removing
foreach (string line in lines) input = input.Replace("/*/*", "/* /*");
input = input.Replace("*/*/", "*/ */");
int length = input.Length;
int foundStartIndex = -1;
int openCommentsCount = 0;
for (int index = 0; index < length - 1; index++)
{ {
input += line + "\n"; if (input[index] == '/' &&
input[index + 1] == '*')
{
if (openCommentsCount == 0)
{
foundStartIndex = index;
} }
openCommentsCount++;
}
if (input[index] == '*' &&
input[index + 1] == '/')
{
openCommentsCount--;
if (openCommentsCount == 0)
{
int commentLength = index - foundStartIndex + 2;
length -= commentLength;
index = foundStartIndex - 1;
input = input.Remove(foundStartIndex, commentLength);
foundStartIndex = -1;
}
}
}
if (openCommentsCount > 0)
{
throw new Exception("Unable to clean the shader code because it seems " +
"some multiline comments interfere with each other or with the code. " +
"Please make sure your shader code and comments are well formatted!");
}
#endregion
// Now to some additional cleanup // Now to some additional cleanup
string[] minimizables = string[] minimizables =
@ -145,8 +182,6 @@ namespace ANX.Framework.Windows.GL3
input = input.Replace(mizable, mizable.Trim()); input = input.Replace(mizable, mizable.Trim());
} }
input = input.Replace("\n", "");
return input; return input;
} }
#endregion #endregion
@ -306,6 +341,27 @@ namespace ANX.Framework.Windows.GL3
} }
#endregion #endregion
#region TestCleanCodeWithExtendedComments
public static void TestCleanCodeWithExtendedComments()
{
string input =
@"// This is a simple comment.
/*Hello
im a multiline comment*/
/* Multiline on a single line */
/* And now the hardcore...a multiline comment
/*in a multiline comment/*in another one
*/*/
Wow...
*/
";
Console.WriteLine(CleanCode(input));
}
#endregion
#region TestParseShaderCode #region TestParseShaderCode
public static void TestParseShaderCode() public static void TestParseShaderCode()
{ {

View File

@ -62,6 +62,8 @@ namespace ANX.Framework.Windows.GL3
public class VertexBufferGL3 : INativeBuffer public class VertexBufferGL3 : INativeBuffer
{ {
#region Private #region Private
private VertexBuffer managedBuffer;
/// <summary> /// <summary>
/// Native vertex buffer handle. /// Native vertex buffer handle.
/// </summary> /// </summary>
@ -89,17 +91,18 @@ namespace ANX.Framework.Windows.GL3
/// <summary> /// <summary>
/// Create a new Vertex Buffer object. /// Create a new Vertex Buffer object.
/// </summary> /// </summary>
internal VertexBufferGL3(VertexDeclaration setVertexDeclaration, internal VertexBufferGL3(VertexBuffer setManagedBuffer,
int setVertexCount, BufferUsage setUsage) VertexDeclaration setVertexDeclaration, int setVertexCount,
BufferUsage setUsage)
{ {
GraphicsResourceManager.UpdateResource(this, true); GraphicsResourceManager.UpdateResource(this, true);
managedBuffer = setManagedBuffer;
vertexDeclaration = setVertexDeclaration; vertexDeclaration = setVertexDeclaration;
usage = setUsage; usage = setUsage;
vertexCount = setVertexCount; vertexCount = setVertexCount;
// TODO: check if dynamic buffer bool isDynamicBuffer = managedBuffer is DynamicVertexBuffer;
bool isDynamicBuffer = false;
usageHint = isDynamicBuffer ? usageHint = isDynamicBuffer ?
BufferUsageHint.DynamicDraw : BufferUsageHint.DynamicDraw :
@ -224,6 +227,10 @@ namespace ANX.Framework.Windows.GL3
GL.EnableVertexAttribArray(attribute.Location); GL.EnableVertexAttribArray(attribute.Location);
VertexElement element = elements[(int)attribute.Location]; VertexElement element = elements[(int)attribute.Location];
int size = 0;
VertexAttribPointerType type = VertexAttribPointerType.Float;
bool normalized = false;
switch (element.VertexElementUsage) switch (element.VertexElementUsage)
{ {
case VertexElementUsage.Binormal: case VertexElementUsage.Binormal:
@ -232,31 +239,33 @@ namespace ANX.Framework.Windows.GL3
case VertexElementUsage.BlendIndices: case VertexElementUsage.BlendIndices:
case VertexElementUsage.BlendWeight: case VertexElementUsage.BlendWeight:
case VertexElementUsage.Position: case VertexElementUsage.Position:
GL.VertexAttribPointer((int)attribute.Location, 3, size = 3;
VertexAttribPointerType.Float, false,
vertexDeclaration.VertexStride, element.Offset);
break; break;
case VertexElementUsage.Color: case VertexElementUsage.Color:
GL.VertexAttribPointer((int)attribute.Location, 4, size = 4;
VertexAttribPointerType.UnsignedByte, type = VertexAttribPointerType.UnsignedByte;
true, vertexDeclaration.VertexStride, element.Offset); normalized = true;
break; break;
case VertexElementUsage.TextureCoordinate: case VertexElementUsage.TextureCoordinate:
GL.VertexAttribPointer((int)attribute.Location, 2, size = 2;
VertexAttribPointerType.Float, false, break;
vertexDeclaration.VertexStride, element.Offset);
case VertexElementUsage.Fog:
case VertexElementUsage.PointSize:
case VertexElementUsage.TessellateFactor:
size = 1;
break; break;
// TODO // TODO
case VertexElementUsage.Depth: case VertexElementUsage.Depth:
case VertexElementUsage.Fog:
case VertexElementUsage.PointSize:
case VertexElementUsage.Sample: case VertexElementUsage.Sample:
case VertexElementUsage.TessellateFactor:
throw new NotImplementedException(); throw new NotImplementedException();
} }
GL.VertexAttribPointer((int)attribute.Location, size, type, normalized,
vertexDeclaration.VertexStride, element.Offset);
} }
} }
#endregion #endregion

View File

@ -97,14 +97,16 @@ namespace ANX.RenderSystem.Windows.DX11
return new GraphicsDeviceWindowsDX11(presentationParameters); return new GraphicsDeviceWindowsDX11(presentationParameters);
} }
public INativeBuffer CreateIndexBuffer(GraphicsDevice graphics, IndexElementSize size, int indexCount, BufferUsage usage) public INativeBuffer CreateIndexBuffer(GraphicsDevice graphics,
IndexBuffer managedBuffer, IndexElementSize size, int indexCount, BufferUsage usage)
{ {
AddInSystemFactory.Instance.PreventSystemChange( AddInSystemFactory.Instance.PreventSystemChange(
AddInType.RenderSystem); AddInType.RenderSystem);
return new IndexBuffer_DX11(graphics, size, indexCount, usage); return new IndexBuffer_DX11(graphics, size, indexCount, usage);
} }
public INativeBuffer CreateVertexBuffer(GraphicsDevice graphics, VertexDeclaration vertexDeclaration, int vertexCount, BufferUsage usage) public INativeBuffer CreateVertexBuffer(GraphicsDevice graphics,
VertexBuffer managedBuffer, VertexDeclaration vertexDeclaration, int vertexCount, BufferUsage usage)
{ {
AddInSystemFactory.Instance.PreventSystemChange(AddInType.RenderSystem); AddInSystemFactory.Instance.PreventSystemChange(AddInType.RenderSystem);
return new VertexBuffer_DX11(graphics, vertexDeclaration, vertexCount, usage); return new VertexBuffer_DX11(graphics, vertexDeclaration, vertexCount, usage);

View File

@ -92,12 +92,14 @@ namespace ANX.Framework.Windows.DX10
return new GraphicsDeviceWindowsDX10(presentationParameters); return new GraphicsDeviceWindowsDX10(presentationParameters);
} }
public INativeBuffer CreateIndexBuffer(GraphicsDevice graphics, IndexElementSize size, int indexCount, BufferUsage usage) public INativeBuffer CreateIndexBuffer(GraphicsDevice graphics,
IndexBuffer managedBuffer, IndexElementSize size, int indexCount, BufferUsage usage)
{ {
return new IndexBuffer_DX10(graphics, size, indexCount, usage); return new IndexBuffer_DX10(graphics, size, indexCount, usage);
} }
public INativeBuffer CreateVertexBuffer(GraphicsDevice graphics, VertexDeclaration vertexDeclaration, int vertexCount, BufferUsage usage) public INativeBuffer CreateVertexBuffer(GraphicsDevice graphics,
VertexBuffer managedBuffer, VertexDeclaration vertexDeclaration, int vertexCount, BufferUsage usage)
{ {
return new VertexBuffer_DX10(graphics, vertexDeclaration, vertexCount, usage); return new VertexBuffer_DX10(graphics, vertexDeclaration, vertexCount, usage);
} }