From 30499fac83c98f4a35a8010c5138d7402505bfc9 Mon Sep 17 00:00:00 2001 From: "SND\\AstrorEnales_cp" Date: Sun, 19 Feb 2012 10:32:17 +0000 Subject: [PATCH] - 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. --- ANX.Framework/Graphics/IndexBuffer.cs | 7 +- ANX.Framework/Graphics/VertexBuffer.cs | 7 +- .../RenderSystem/IRenderSystemCreator.cs | 4 +- .../ANX.Framework.Windows.DX10/Creator.cs | 6 +- .../ANX.Framework.Windows.GL3/Creator.cs | 12 ++-- .../IndexBufferGL3.cs | 10 +-- .../SamplerStateGL3.cs | 9 +-- .../ANX.Framework.Windows.GL3/ShaderHelper.cs | 68 +++++++++++++++++-- .../VertexBufferGL3.cs | 41 ++++++----- .../ANX.RenderSystem.Windows.DX11/Creator.cs | 6 +- .../ANX.RenderSystem.Windows.Metro/Creator.cs | 6 +- 11 files changed, 127 insertions(+), 49 deletions(-) diff --git a/ANX.Framework/Graphics/IndexBuffer.cs b/ANX.Framework/Graphics/IndexBuffer.cs index 2bd7d658..b525fa1c 100644 --- a/ANX.Framework/Graphics/IndexBuffer.cs +++ b/ANX.Framework/Graphics/IndexBuffer.cs @@ -71,8 +71,8 @@ namespace ANX.Framework.Graphics this.indexCount = indexCount; this.bufferUsage = usage; - base.GraphicsDevice.ResourceCreated += new EventHandler(GraphicsDevice_ResourceCreated); - base.GraphicsDevice.ResourceDestroyed += new EventHandler(GraphicsDevice_ResourceDestroyed); + base.GraphicsDevice.ResourceCreated += GraphicsDevice_ResourceCreated; + base.GraphicsDevice.ResourceDestroyed += GraphicsDevice_ResourceDestroyed; CreateNativeBuffer(); } @@ -128,7 +128,8 @@ namespace ANX.Framework.Graphics private void CreateNativeBuffer() { - this.nativeIndexBuffer = AddInSystemFactory.Instance.GetDefaultCreator().CreateIndexBuffer(GraphicsDevice, indexElementSize, indexCount, bufferUsage); + this.nativeIndexBuffer = + AddInSystemFactory.Instance.GetDefaultCreator().CreateIndexBuffer(GraphicsDevice, this, indexElementSize, indexCount, bufferUsage); } public void GetData(int offsetInBytes, T[] data, int startIndex, int elementCount) where T : struct diff --git a/ANX.Framework/Graphics/VertexBuffer.cs b/ANX.Framework/Graphics/VertexBuffer.cs index 60bbc411..d67fce1a 100644 --- a/ANX.Framework/Graphics/VertexBuffer.cs +++ b/ANX.Framework/Graphics/VertexBuffer.cs @@ -76,8 +76,8 @@ namespace ANX.Framework.Graphics this.vertexDeclaration = vertexDeclaration; this.bufferUsage = usage; - base.GraphicsDevice.ResourceCreated += new EventHandler(GraphicsDevice_ResourceCreated); - base.GraphicsDevice.ResourceDestroyed += new EventHandler(GraphicsDevice_ResourceDestroyed); + base.GraphicsDevice.ResourceCreated += GraphicsDevice_ResourceCreated; + base.GraphicsDevice.ResourceDestroyed += GraphicsDevice_ResourceDestroyed; CreateNativeBuffer(); } @@ -111,7 +111,8 @@ namespace ANX.Framework.Graphics private void CreateNativeBuffer() { - this.nativeVertexBuffer = AddInSystemFactory.Instance.GetDefaultCreator().CreateVertexBuffer(GraphicsDevice, vertexDeclaration, vertexCount, bufferUsage); + this.nativeVertexBuffer = + AddInSystemFactory.Instance.GetDefaultCreator().CreateVertexBuffer(GraphicsDevice, this, vertexDeclaration, vertexCount, bufferUsage); } public BufferUsage BufferUsage diff --git a/ANX.Framework/NonXNA/RenderSystem/IRenderSystemCreator.cs b/ANX.Framework/NonXNA/RenderSystem/IRenderSystemCreator.cs index cb4e4054..4cc7b416 100644 --- a/ANX.Framework/NonXNA/RenderSystem/IRenderSystemCreator.cs +++ b/ANX.Framework/NonXNA/RenderSystem/IRenderSystemCreator.cs @@ -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); - 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 vertexShaderByteCode, Stream pixelShaderByteCode); diff --git a/RenderSystems/ANX.Framework.Windows.DX10/Creator.cs b/RenderSystems/ANX.Framework.Windows.DX10/Creator.cs index e7b3512e..e745d830 100644 --- a/RenderSystems/ANX.Framework.Windows.DX10/Creator.cs +++ b/RenderSystems/ANX.Framework.Windows.DX10/Creator.cs @@ -96,14 +96,16 @@ namespace ANX.Framework.Windows.DX10 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( AddInType.RenderSystem); 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( AddInType.RenderSystem); diff --git a/RenderSystems/ANX.Framework.Windows.GL3/Creator.cs b/RenderSystems/ANX.Framework.Windows.GL3/Creator.cs index 5aac67e4..aa1651ca 100644 --- a/RenderSystems/ANX.Framework.Windows.GL3/Creator.cs +++ b/RenderSystems/ANX.Framework.Windows.GL3/Creator.cs @@ -173,10 +173,11 @@ namespace ANX.Framework.Windows.GL3 /// The usage type of the buffer. /// Native OpenGL index buffer. public INativeBuffer CreateIndexBuffer(GraphicsDevice graphics, - IndexElementSize size, int indexCount, BufferUsage usage) + IndexBuffer managedBuffer, IndexElementSize size, int indexCount, + BufferUsage usage) { AddInSystemFactory.Instance.PreventSystemChange(AddInType.RenderSystem); - return new IndexBufferGL3(size, indexCount, usage); + return new IndexBufferGL3(managedBuffer, size, indexCount, usage); } #endregion @@ -191,11 +192,12 @@ namespace ANX.Framework.Windows.GL3 /// The usage type of the buffer. /// Native OpenGL vertex buffer. public INativeBuffer CreateVertexBuffer(GraphicsDevice graphics, - VertexDeclaration vertexDeclaration, int vertexCount, - BufferUsage usage) + VertexBuffer managedBuffer, VertexDeclaration vertexDeclaration, + int vertexCount, BufferUsage usage) { AddInSystemFactory.Instance.PreventSystemChange(AddInType.RenderSystem); - return new VertexBufferGL3(vertexDeclaration, vertexCount, usage); + return new VertexBufferGL3(managedBuffer, vertexDeclaration, vertexCount, + usage); } #endregion diff --git a/RenderSystems/ANX.Framework.Windows.GL3/IndexBufferGL3.cs b/RenderSystems/ANX.Framework.Windows.GL3/IndexBufferGL3.cs index 4a7d208c..1e9bc4b2 100644 --- a/RenderSystems/ANX.Framework.Windows.GL3/IndexBufferGL3.cs +++ b/RenderSystems/ANX.Framework.Windows.GL3/IndexBufferGL3.cs @@ -59,6 +59,8 @@ namespace ANX.Framework.Windows.GL3 public class IndexBufferGL3 : INativeBuffer { #region Private + private IndexBuffer managedBuffer; + private int bufferHandle; /// /// Native index buffer handle. @@ -86,17 +88,17 @@ namespace ANX.Framework.Windows.GL3 /// /// Create a new Index Buffer object. /// - internal IndexBufferGL3(IndexElementSize setElementSize, - int setIndexCount, BufferUsage setUsage) + internal IndexBufferGL3(IndexBuffer setManagedBuffer, + IndexElementSize setElementSize, int setIndexCount, BufferUsage setUsage) { GraphicsResourceManager.UpdateResource(this, true); + managedBuffer = setManagedBuffer; indexCount = setIndexCount; elementSize = setElementSize; usage = setUsage; - // TODO: check if dynamic buffer - bool isDynamicBuffer = false; + bool isDynamicBuffer = managedBuffer is DynamicIndexBuffer; usageHint = isDynamicBuffer ? BufferUsageHint.DynamicDraw : diff --git a/RenderSystems/ANX.Framework.Windows.GL3/SamplerStateGL3.cs b/RenderSystems/ANX.Framework.Windows.GL3/SamplerStateGL3.cs index a425d0c5..5ab57413 100644 --- a/RenderSystems/ANX.Framework.Windows.GL3/SamplerStateGL3.cs +++ b/RenderSystems/ANX.Framework.Windows.GL3/SamplerStateGL3.cs @@ -1,7 +1,6 @@ -using System; -using ANX.Framework.Graphics; +using ANX.Framework.Graphics; using ANX.Framework.NonXNA; -using OpenTK.Graphics.OpenGL; +using ANX.Framework.NonXNA.Development; #region License @@ -64,6 +63,8 @@ namespace ANX.Framework.Windows.GL3 /// Info for OGL 3.3 sampler objects (sadly not implemented in OpenTK yet): /// http://www.sinanc.org/blog/?p=215 /// + [PercentageComplete(10)] + [TestState(TestStateAttribute.TestState.Untested)] public class SamplerStateGL3 : INativeSamplerState { #region Public @@ -150,7 +151,7 @@ namespace ANX.Framework.Windows.GL3 /// Apply the sampler state. /// /// Graphics device. - /// + /// The index of which sampler should be modified. public void Apply(GraphicsDevice graphicsDevice, int index) { IsBound = true; diff --git a/RenderSystems/ANX.Framework.Windows.GL3/ShaderHelper.cs b/RenderSystems/ANX.Framework.Windows.GL3/ShaderHelper.cs index 3027aec3..ae117dbd 100644 --- a/RenderSystems/ANX.Framework.Windows.GL3/ShaderHelper.cs +++ b/RenderSystems/ANX.Framework.Windows.GL3/ShaderHelper.cs @@ -115,6 +115,7 @@ namespace ANX.Framework.Windows.GL3 // empty lines, spaces and tabs at beginning and end and also // remove comments. List lines = new List(input.Split('\n')); + input = ""; for (int index = lines.Count - 1; index >= 0; index--) { lines[index] = lines[index].Trim(); @@ -125,15 +126,51 @@ namespace ANX.Framework.Windows.GL3 continue; } - // TODO: add /**/ comment checking and removing. + input = lines[index] + input; } - input = ""; - foreach (string line in lines) + #region Multiline comment removing + 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 string[] minimizables = { @@ -145,8 +182,6 @@ namespace ANX.Framework.Windows.GL3 input = input.Replace(mizable, mizable.Trim()); } - input = input.Replace("\n", ""); - return input; } #endregion @@ -306,6 +341,27 @@ namespace ANX.Framework.Windows.GL3 } #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 public static void TestParseShaderCode() { diff --git a/RenderSystems/ANX.Framework.Windows.GL3/VertexBufferGL3.cs b/RenderSystems/ANX.Framework.Windows.GL3/VertexBufferGL3.cs index e8f5c557..c1eeab32 100644 --- a/RenderSystems/ANX.Framework.Windows.GL3/VertexBufferGL3.cs +++ b/RenderSystems/ANX.Framework.Windows.GL3/VertexBufferGL3.cs @@ -62,6 +62,8 @@ namespace ANX.Framework.Windows.GL3 public class VertexBufferGL3 : INativeBuffer { #region Private + private VertexBuffer managedBuffer; + /// /// Native vertex buffer handle. /// @@ -89,17 +91,18 @@ namespace ANX.Framework.Windows.GL3 /// /// Create a new Vertex Buffer object. /// - internal VertexBufferGL3(VertexDeclaration setVertexDeclaration, - int setVertexCount, BufferUsage setUsage) + internal VertexBufferGL3(VertexBuffer setManagedBuffer, + VertexDeclaration setVertexDeclaration, int setVertexCount, + BufferUsage setUsage) { GraphicsResourceManager.UpdateResource(this, true); + managedBuffer = setManagedBuffer; vertexDeclaration = setVertexDeclaration; usage = setUsage; vertexCount = setVertexCount; - // TODO: check if dynamic buffer - bool isDynamicBuffer = false; + bool isDynamicBuffer = managedBuffer is DynamicVertexBuffer; usageHint = isDynamicBuffer ? BufferUsageHint.DynamicDraw : @@ -224,6 +227,10 @@ namespace ANX.Framework.Windows.GL3 GL.EnableVertexAttribArray(attribute.Location); VertexElement element = elements[(int)attribute.Location]; + int size = 0; + VertexAttribPointerType type = VertexAttribPointerType.Float; + bool normalized = false; + switch (element.VertexElementUsage) { case VertexElementUsage.Binormal: @@ -232,31 +239,33 @@ namespace ANX.Framework.Windows.GL3 case VertexElementUsage.BlendIndices: case VertexElementUsage.BlendWeight: case VertexElementUsage.Position: - GL.VertexAttribPointer((int)attribute.Location, 3, - VertexAttribPointerType.Float, false, - vertexDeclaration.VertexStride, element.Offset); + size = 3; break; case VertexElementUsage.Color: - GL.VertexAttribPointer((int)attribute.Location, 4, - VertexAttribPointerType.UnsignedByte, - true, vertexDeclaration.VertexStride, element.Offset); + size = 4; + type = VertexAttribPointerType.UnsignedByte; + normalized = true; break; case VertexElementUsage.TextureCoordinate: - GL.VertexAttribPointer((int)attribute.Location, 2, - VertexAttribPointerType.Float, false, - vertexDeclaration.VertexStride, element.Offset); + size = 2; + break; + + case VertexElementUsage.Fog: + case VertexElementUsage.PointSize: + case VertexElementUsage.TessellateFactor: + size = 1; break; // TODO case VertexElementUsage.Depth: - case VertexElementUsage.Fog: - case VertexElementUsage.PointSize: case VertexElementUsage.Sample: - case VertexElementUsage.TessellateFactor: throw new NotImplementedException(); } + + GL.VertexAttribPointer((int)attribute.Location, size, type, normalized, + vertexDeclaration.VertexStride, element.Offset); } } #endregion diff --git a/RenderSystems/ANX.RenderSystem.Windows.DX11/Creator.cs b/RenderSystems/ANX.RenderSystem.Windows.DX11/Creator.cs index 44c9d541..f5d94edd 100644 --- a/RenderSystems/ANX.RenderSystem.Windows.DX11/Creator.cs +++ b/RenderSystems/ANX.RenderSystem.Windows.DX11/Creator.cs @@ -97,14 +97,16 @@ namespace ANX.RenderSystem.Windows.DX11 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( AddInType.RenderSystem); 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); return new VertexBuffer_DX11(graphics, vertexDeclaration, vertexCount, usage); diff --git a/RenderSystems/ANX.RenderSystem.Windows.Metro/Creator.cs b/RenderSystems/ANX.RenderSystem.Windows.Metro/Creator.cs index f5f9fa8a..992b6eb7 100644 --- a/RenderSystems/ANX.RenderSystem.Windows.Metro/Creator.cs +++ b/RenderSystems/ANX.RenderSystem.Windows.Metro/Creator.cs @@ -92,12 +92,14 @@ namespace ANX.Framework.Windows.DX10 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); } - 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); }