From 9b53939f0c0b68fe854b16e9d631468f1caec307 Mon Sep 17 00:00:00 2001 From: Magnus Norddahl Date: Fri, 16 Aug 2019 13:07:57 +0200 Subject: [PATCH] - simplify vertex buffer and vertex format --- Source/Core/Actions/MouseInput.cs | 2 +- Source/Core/Builder.csproj | 2 - Source/Core/Data/DataManager.cs | 1 - Source/Core/Rendering/Mesh.cs | 2 +- Source/Core/Rendering/RenderDevice.cs | 51 +++---- Source/Core/Rendering/Renderer2D.cs | 100 +++++++------ Source/Core/Rendering/Renderer3D.cs | 36 +++-- Source/Core/Rendering/ShaderManager.cs | 157 -------------------- Source/Core/Rendering/SurfaceManager.cs | 9 +- Source/Core/Rendering/VertexDeclaration.cs | 63 -------- Source/Core/VisualModes/VisualSector.cs | 2 +- Source/Native/BuilderNative.vcxproj | 2 - Source/Native/BuilderNative.vcxproj.filters | 2 - Source/Native/RenderDevice.cpp | 121 ++++----------- Source/Native/RenderDevice.h | 29 +--- Source/Native/Shader.cpp | 1 - Source/Native/Shader.h | 2 + Source/Native/VertexBuffer.cpp | 39 +++++ Source/Native/VertexBuffer.h | 12 ++ Source/Native/VertexDeclaration.cpp | 17 --- Source/Native/VertexDeclaration.h | 20 --- Source/Native/exports.def | 3 - 22 files changed, 202 insertions(+), 471 deletions(-) delete mode 100755 Source/Core/Rendering/ShaderManager.cs delete mode 100644 Source/Core/Rendering/VertexDeclaration.cs delete mode 100644 Source/Native/VertexDeclaration.cpp delete mode 100644 Source/Native/VertexDeclaration.h diff --git a/Source/Core/Actions/MouseInput.cs b/Source/Core/Actions/MouseInput.cs index 9c3f78cb..499efb9e 100755 --- a/Source/Core/Actions/MouseInput.cs +++ b/Source/Core/Actions/MouseInput.cs @@ -92,7 +92,7 @@ namespace CodeImp.DoomBuilder.Actions { Handle = RawMouse_New(control.Handle); if (Handle == IntPtr.Zero) - throw new Exception("VertexDeclaration_New failed"); + throw new Exception("RawMouse_New failed"); } ~RawMouse() diff --git a/Source/Core/Builder.csproj b/Source/Core/Builder.csproj index 74c80cae..3fa27e16 100644 --- a/Source/Core/Builder.csproj +++ b/Source/Core/Builder.csproj @@ -232,7 +232,6 @@ - Form @@ -573,7 +572,6 @@ - diff --git a/Source/Core/Data/DataManager.cs b/Source/Core/Data/DataManager.cs index 88ad4c84..9992d933 100755 --- a/Source/Core/Data/DataManager.cs +++ b/Source/Core/Data/DataManager.cs @@ -3465,7 +3465,6 @@ namespace CodeImp.DoomBuilder.Data General.Map.Graphics.SetUniform(UniformName.campos, new Vector4()); // Begin fullbright shaderpass - General.Map.Graphics.SetVertexDeclaration(General.Map.Graphics.Shaders.WorldVertexDecl); General.Map.Graphics.SetShader(ShaderName.world3d_fullbright); // Render to the six faces of the cube map diff --git a/Source/Core/Rendering/Mesh.cs b/Source/Core/Rendering/Mesh.cs index 4a09b30b..7f5d7376 100644 --- a/Source/Core/Rendering/Mesh.cs +++ b/Source/Core/Rendering/Mesh.cs @@ -22,7 +22,7 @@ namespace CodeImp.DoomBuilder.Rendering internal void Draw(RenderDevice device) { - device.SetVertexBuffer(0, Vertices, 0, WorldVertex.Stride); + device.SetVertexBuffer(Vertices); device.SetIndexBuffer(Indices); device.DrawIndexed(PrimitiveType.TriangleList, 0, Count / 3); } diff --git a/Source/Core/Rendering/RenderDevice.cs b/Source/Core/Rendering/RenderDevice.cs index 54b0cb03..f6dcf3a1 100755 --- a/Source/Core/Rendering/RenderDevice.cs +++ b/Source/Core/Rendering/RenderDevice.cs @@ -37,7 +37,7 @@ namespace CodeImp.DoomBuilder.Rendering throw new Exception("RenderDevice_New failed"); RenderTarget = rendertarget; - Shaders = new ShaderManager(this); + SetupSettings(); } @@ -102,9 +102,9 @@ namespace CodeImp.DoomBuilder.Rendering }, 16); } - public void SetVertexBuffer(int index, VertexBuffer buffer, long offset, long stride) + public void SetVertexBuffer(VertexBuffer buffer) { - RenderDevice_SetVertexBuffer(Handle, index, buffer != null ? buffer.Handle : IntPtr.Zero, offset, stride); + RenderDevice_SetVertexBuffer(Handle, buffer != null ? buffer.Handle : IntPtr.Zero); } public void SetIndexBuffer(IndexBuffer buffer) @@ -236,12 +236,7 @@ namespace CodeImp.DoomBuilder.Rendering public void Draw(PrimitiveType type, int startIndex, int primitiveCount, FlatVertex[] data) { - RenderDevice_DrawData(Handle, type, startIndex, primitiveCount, data, Marshal.SizeOf()); - } - - public void SetVertexDeclaration(VertexDeclaration decl) - { - RenderDevice_SetVertexDeclaration(Handle, decl != null ? decl.Handle : IntPtr.Zero); + RenderDevice_DrawData(Handle, type, startIndex, primitiveCount, data); } public void StartRendering(bool clear, Color4 backcolor) @@ -279,19 +274,19 @@ namespace CodeImp.DoomBuilder.Rendering RenderDevice_SetIndexBufferData(Handle, buffer.Handle, data, data.Length * Marshal.SizeOf()); } - public void SetBufferData(VertexBuffer buffer, int size) + public void SetBufferData(VertexBuffer buffer, int length, VertexFormat format) { - RenderDevice_SetVertexBufferData(Handle, buffer.Handle, IntPtr.Zero, size); + RenderDevice_SetVertexBufferData(Handle, buffer.Handle, IntPtr.Zero, length * (format == VertexFormat.Flat ? FlatVertex.Stride : WorldVertex.Stride), format); } public void SetBufferData(VertexBuffer buffer, FlatVertex[] data) { - RenderDevice_SetVertexBufferData(Handle, buffer.Handle, data, data.Length * Marshal.SizeOf()); + RenderDevice_SetVertexBufferData(Handle, buffer.Handle, data, data.Length * Marshal.SizeOf(), VertexFormat.Flat); } public void SetBufferData(VertexBuffer buffer, WorldVertex[] data) { - RenderDevice_SetVertexBufferData(Handle, buffer.Handle, data, data.Length * Marshal.SizeOf()); + RenderDevice_SetVertexBufferData(Handle, buffer.Handle, data, data.Length * Marshal.SizeOf(), VertexFormat.World); } public void SetBufferSubdata(VertexBuffer buffer, long destOffset, FlatVertex[] data) @@ -379,11 +374,16 @@ namespace CodeImp.DoomBuilder.Rendering // Texture addressing SetSamplerState(0, TextureAddress.Wrap); - // Shader settings - Shaders.SetWorld3DConstants(General.Settings.VisualBilinear, General.Settings.FilterAnisotropy); - - // Initialize presentations - Presentation.Initialize(); + //mxd. It's still nice to have anisotropic filtering when texture filtering is disabled + TextureFilter magminfilter = (General.Settings.VisualBilinear ? TextureFilter.Linear : TextureFilter.Point); + SetSamplerFilter(0, + General.Settings.FilterAnisotropy > 1.0f ? TextureFilter.Anisotropic : magminfilter, + magminfilter, + General.Settings.VisualBilinear ? TextureFilter.Linear : TextureFilter.None, // [SB] use None, otherwise textures are still filtered + General.Settings.FilterAnisotropy); + + // Initialize presentations + Presentation.Initialize(); } IntPtr Handle; @@ -401,7 +401,7 @@ namespace CodeImp.DoomBuilder.Rendering static extern IntPtr RenderDevice_SetUniform(IntPtr hwnd, UniformName name, float[] data, int count); [DllImport("BuilderNative.dll", CallingConvention = CallingConvention.Cdecl)] - static extern void RenderDevice_SetVertexBuffer(IntPtr handle, int index, IntPtr buffer, long offset, long stride); + static extern void RenderDevice_SetVertexBuffer(IntPtr handle, IntPtr buffer); [DllImport("BuilderNative.dll", CallingConvention = CallingConvention.Cdecl)] static extern void RenderDevice_SetIndexBuffer(IntPtr handle, IntPtr buffer); @@ -452,10 +452,7 @@ namespace CodeImp.DoomBuilder.Rendering static extern void RenderDevice_DrawIndexed(IntPtr handle, PrimitiveType type, int startIndex, int primitiveCount); [DllImport("BuilderNative.dll", CallingConvention = CallingConvention.Cdecl)] - static extern void RenderDevice_DrawData(IntPtr handle, PrimitiveType type, int startIndex, int primitiveCount, FlatVertex[] data, int stride); - - [DllImport("BuilderNative.dll", CallingConvention = CallingConvention.Cdecl)] - static extern void RenderDevice_SetVertexDeclaration(IntPtr handle, IntPtr decl); + static extern void RenderDevice_DrawData(IntPtr handle, PrimitiveType type, int startIndex, int primitiveCount, FlatVertex[] data); [DllImport("BuilderNative.dll", CallingConvention = CallingConvention.Cdecl)] static extern void RenderDevice_StartRendering(IntPtr handle, bool clear, int backcolor); @@ -479,13 +476,13 @@ namespace CodeImp.DoomBuilder.Rendering static extern void RenderDevice_SetIndexBufferData(IntPtr handle, IntPtr buffer, int[] data, long size); [DllImport("BuilderNative.dll", CallingConvention = CallingConvention.Cdecl)] - static extern void RenderDevice_SetVertexBufferData(IntPtr handle, IntPtr buffer, IntPtr data, long size); + static extern void RenderDevice_SetVertexBufferData(IntPtr handle, IntPtr buffer, IntPtr data, long size, VertexFormat format); [DllImport("BuilderNative.dll", CallingConvention = CallingConvention.Cdecl)] - static extern void RenderDevice_SetVertexBufferData(IntPtr handle, IntPtr buffer, FlatVertex[] data, long size); + static extern void RenderDevice_SetVertexBufferData(IntPtr handle, IntPtr buffer, FlatVertex[] data, long size, VertexFormat format); [DllImport("BuilderNative.dll", CallingConvention = CallingConvention.Cdecl)] - static extern void RenderDevice_SetVertexBufferData(IntPtr handle, IntPtr buffer, WorldVertex[] data, long size); + static extern void RenderDevice_SetVertexBufferData(IntPtr handle, IntPtr buffer, WorldVertex[] data, long size, VertexFormat format); [DllImport("BuilderNative.dll", CallingConvention = CallingConvention.Cdecl)] static extern void RenderDevice_SetVertexBufferSubdata(IntPtr handle, IntPtr buffer, long destOffset, FlatVertex[] data, long sizeInBytes); @@ -512,7 +509,6 @@ namespace CodeImp.DoomBuilder.Rendering public static readonly List AA_STEPS = new List { 0, 2, 4, 8 }; internal RenderTargetControl RenderTarget { get; private set; } - internal ShaderManager Shaders { get; private set; } // Make a color from ARGB public static int ARGB(float a, float r, float g, float b) @@ -600,6 +596,7 @@ namespace CodeImp.DoomBuilder.Rendering campos } + public enum VertexFormat : int { Flat, World } public enum Cull : int { None, Clockwise } public enum Blend : int { InverseSourceAlpha, SourceAlpha, One, BlendFactor } public enum BlendOperation : int { Add, ReverseSubtract } diff --git a/Source/Core/Rendering/Renderer2D.cs b/Source/Core/Rendering/Renderer2D.cs index 480acbd7..96bb8065 100755 --- a/Source/Core/Rendering/Renderer2D.cs +++ b/Source/Core/Rendering/Renderer2D.cs @@ -184,9 +184,8 @@ namespace CodeImp.DoomBuilder.Rendering graphics.SetCullMode(Cull.None); graphics.SetZEnable(false); graphics.SetFogEnable(false); - graphics.SetVertexBuffer(0, screenverts, 0, FlatVertex.Stride); + graphics.SetVertexBuffer(screenverts); graphics.SetTransform(TransformState.World, Matrix.Identity); - graphics.SetVertexDeclaration(graphics.Shaders.FlatVertexDecl); // Go for all layers foreach(PresentLayer layer in present.layers) @@ -236,16 +235,16 @@ namespace CodeImp.DoomBuilder.Rendering if((backimageverts == null) || (General.Map.Grid.Background.Texture == null)) break; graphics.SetShader(aapass); graphics.SetTexture(0, General.Map.Grid.Background.Texture); - graphics.Shaders.SetDisplay2DSettings(1f / windowsize.Width, 1f / windowsize.Height, FSAA_FACTOR, layer.alpha, false); + SetDisplay2DSettings(1f / windowsize.Width, 1f / windowsize.Height, FSAA_FACTOR, layer.alpha, false); graphics.Draw(PrimitiveType.TriangleStrip, 0, 2, backimageverts); - graphics.SetVertexBuffer(0, screenverts, 0, FlatVertex.Stride); + graphics.SetVertexBuffer(screenverts); break; // GRID case RendererLayer.Grid: graphics.SetShader(aapass); graphics.SetTexture(0, backtex); - graphics.Shaders.SetDisplay2DSettings(1f / backsize.Width, 1f / backsize.Height, FSAA_FACTOR, layer.alpha, false); + SetDisplay2DSettings(1f / backsize.Width, 1f / backsize.Height, FSAA_FACTOR, layer.alpha, false); graphics.Draw(PrimitiveType.TriangleStrip, 0, 2); break; @@ -253,7 +252,7 @@ namespace CodeImp.DoomBuilder.Rendering case RendererLayer.Geometry: graphics.SetShader(aapass); graphics.SetTexture(0, plottertex); - graphics.Shaders.SetDisplay2DSettings(1f / structsize.Width, 1f / structsize.Height, FSAA_FACTOR, layer.alpha, false); + SetDisplay2DSettings(1f / structsize.Width, 1f / structsize.Height, FSAA_FACTOR, layer.alpha, false); graphics.Draw(PrimitiveType.TriangleStrip, 0, 2); break; @@ -261,7 +260,7 @@ namespace CodeImp.DoomBuilder.Rendering case RendererLayer.Things: graphics.SetShader(aapass); graphics.SetTexture(0, thingstex); - graphics.Shaders.SetDisplay2DSettings(1f / thingssize.Width, 1f / thingssize.Height, FSAA_FACTOR, layer.alpha, false); + SetDisplay2DSettings(1f / thingssize.Width, 1f / thingssize.Height, FSAA_FACTOR, layer.alpha, false); graphics.Draw(PrimitiveType.TriangleStrip, 0, 2); break; @@ -269,7 +268,7 @@ namespace CodeImp.DoomBuilder.Rendering case RendererLayer.Overlay: graphics.SetShader(aapass); graphics.SetTexture(0, overlaytex); - graphics.Shaders.SetDisplay2DSettings(1f / overlaysize.Width, 1f / overlaysize.Height, FSAA_FACTOR, layer.alpha, false); + SetDisplay2DSettings(1f / overlaysize.Width, 1f / overlaysize.Height, FSAA_FACTOR, layer.alpha, false); graphics.Draw(PrimitiveType.TriangleStrip, 0, 2); break; @@ -277,7 +276,7 @@ namespace CodeImp.DoomBuilder.Rendering case RendererLayer.Surface: graphics.SetShader(aapass); graphics.SetTexture(0, surfacetex); - graphics.Shaders.SetDisplay2DSettings(1f / overlaysize.Width, 1f / overlaysize.Height, FSAA_FACTOR, layer.alpha, false); + SetDisplay2DSettings(1f / overlaysize.Width, 1f / overlaysize.Height, FSAA_FACTOR, layer.alpha, false); graphics.Draw(PrimitiveType.TriangleStrip, 0, 2); break; } @@ -289,7 +288,7 @@ namespace CodeImp.DoomBuilder.Rendering // Release binds graphics.SetTexture(0, null); - graphics.SetVertexBuffer(0, null, 0, 0); + graphics.SetVertexBuffer(null); } #endregion @@ -382,7 +381,7 @@ namespace CodeImp.DoomBuilder.Rendering // Create vertex buffers screenverts = new VertexBuffer(); thingsvertices = new VertexBuffer(); - graphics.SetBufferData(thingsvertices, THING_BUFFER_SIZE * 12 * FlatVertex.Stride); + graphics.SetBufferData(thingsvertices, THING_BUFFER_SIZE * 12, VertexFormat.Flat); // Make screen vertices FlatVertex[] verts = CreateScreenVerts(structsize); @@ -491,11 +490,37 @@ namespace CodeImp.DoomBuilder.Rendering graphics.SetTransform(TransformState.World, Matrix.Identity); } } - - /// - /// This unprojects display coordinates (screen space) to map coordinates - /// - public Vector2D DisplayToMap(Vector2D mousepos) + + private void SetDisplay2DSettings(float texelx, float texely, float fsaafactor, float alpha, bool bilinear) + { + Vector4 values = new Vector4(texelx, texely, fsaafactor, alpha); + graphics.SetUniform(UniformName.rendersettings, values); + Matrix world = graphics.GetTransform(TransformState.World); + Matrix view = graphics.GetTransform(TransformState.View); + graphics.SetUniform(UniformName.transformsettings, world * view); + graphics.SetSamplerFilter(0, bilinear ? TextureFilter.Linear : TextureFilter.Point); + } + + private void SetThings2DSettings(float alpha) + { + Vector4 values = new Vector4(0.0f, 0.0f, 1.0f, alpha); + graphics.SetUniform(UniformName.rendersettings, values); + Matrix world = graphics.GetTransform(TransformState.World); + Matrix view = graphics.GetTransform(TransformState.View); + graphics.SetUniform(UniformName.transformsettings, world * view); + } + + //mxd. Used to render models + private void SetThings2DTransformSettings(Matrix world) + { + Matrix view = graphics.GetTransform(TransformState.View); + graphics.SetUniform(UniformName.transformsettings, world * view); + } + + /// + /// This unprojects display coordinates (screen space) to map coordinates + /// + public Vector2D DisplayToMap(Vector2D mousepos) { return mousepos.GetInvTransformed(-translatex, -translatey, scaleinv, -scaleinv); } @@ -1207,14 +1232,13 @@ namespace CodeImp.DoomBuilder.Rendering graphics.SetAlphaTestEnable(false); graphics.SetFogEnable(false); graphics.SetTextureFactor(alphacolor.ToArgb()); - graphics.SetVertexBuffer(0, thingsvertices, 0, FlatVertex.Stride); + graphics.SetVertexBuffer(thingsvertices); // Set things texture graphics.SetTexture(0, General.Map.Data.ThingTexture.Texture); //mxd SetWorldTransformation(false); - graphics.SetVertexDeclaration(graphics.Shaders.FlatVertexDecl); graphics.SetShader(ShaderName.things2d_thing); - graphics.Shaders.SetThings2DSettings(alpha); + SetThings2DSettings(alpha); // Determine next lock size int locksize = (things.Count > THING_BUFFER_SIZE) ? THING_BUFFER_SIZE : things.Count; @@ -1503,10 +1527,9 @@ namespace CodeImp.DoomBuilder.Rendering Matrix position = Matrix.Translation(screenpos.x, screenpos.y, 0.0f); Matrix world = General.Map.Data.ModeldefEntries[t.Type].Transform * modelscale * rotation * viewscale * position; - graphics.Shaders.SetThings2DTransformSettings(world); + SetThings2DTransformSettings(world); // Draw - graphics.SetVertexDeclaration(graphics.Shaders.WorldVertexDecl); foreach(Mesh mesh in mde.Model.Meshes) mesh.Draw(graphics); } } @@ -1565,7 +1588,7 @@ namespace CodeImp.DoomBuilder.Rendering graphics.SetTextureFactor(-1); graphics.SetFogEnable(false); SetWorldTransformation(true); - graphics.Shaders.SetDisplay2DSettings(1f, 1f, 0f, 1f, General.Settings.ClassicBilinear); + SetDisplay2DSettings(1f, 1f, 0f, 1f, General.Settings.ClassicBilinear); // Prepare for rendering switch(viewmode) @@ -1622,11 +1645,10 @@ namespace CodeImp.DoomBuilder.Rendering graphics.SetAlphaTestEnable(false); graphics.SetTextureFactor(-1); graphics.SetFogEnable(false); - graphics.SetVertexDeclaration(graphics.Shaders.FlatVertexDecl); graphics.SetShader(ShaderName.display2d_normal); graphics.SetTexture(0, t); SetWorldTransformation(transformcoords); - graphics.Shaders.SetDisplay2DSettings(1f, 1f, 0f, 1f, General.Settings.ClassicBilinear); + SetDisplay2DSettings(1f, 1f, 0f, 1f, General.Settings.ClassicBilinear); // Draw graphics.Draw(PrimitiveType.TriangleList, 0, vertices.Length / 3, vertices); @@ -1648,10 +1670,9 @@ namespace CodeImp.DoomBuilder.Rendering SetWorldTransformation(true); graphics.SetUniform(UniformName.FillColor, new Color4(color)); - graphics.Shaders.SetThings2DSettings(1.0f); + SetThings2DSettings(1.0f); // Draw - graphics.SetVertexDeclaration(graphics.Shaders.FlatVertexDecl); graphics.SetShader(ShaderName.things2d_fill); graphics.Draw(PrimitiveType.TriangleList, 0, vertices.Length / 3, vertices); } @@ -1674,12 +1695,11 @@ namespace CodeImp.DoomBuilder.Rendering graphics.SetAlphaTestEnable(false); graphics.SetTextureFactor(-1); graphics.SetFogEnable(false); - graphics.SetVertexDeclaration(graphics.Shaders.FlatVertexDecl); graphics.SetShader(ShaderName.display2d_normal); graphics.SetTexture(0, label.Texture); SetWorldTransformation(false); - graphics.Shaders.SetDisplay2DSettings(1f, 1f, 0f, 1f, false); - graphics.SetVertexBuffer(0, label.VertexBuffer, 0, FlatVertex.Stride); + SetDisplay2DSettings(1f, 1f, 0f, 1f, false); + graphics.SetVertexBuffer(label.VertexBuffer); // Draw graphics.Draw(PrimitiveType.TriangleStrip, 0, 2); @@ -1707,9 +1727,8 @@ namespace CodeImp.DoomBuilder.Rendering graphics.SetTextureFactor(-1); graphics.SetFogEnable(false); SetWorldTransformation(false); - graphics.SetVertexDeclaration(graphics.Shaders.FlatVertexDecl); graphics.SetShader(ShaderName.display2d_normal); - graphics.Shaders.SetDisplay2DSettings(1f, 1f, 0f, 1f, false); + SetDisplay2DSettings(1f, 1f, 0f, 1f, false); foreach(ITextLabel label in labels) { @@ -1717,7 +1736,7 @@ namespace CodeImp.DoomBuilder.Rendering if(!label.SkipRendering) { graphics.SetTexture(0, label.Texture); - graphics.SetVertexBuffer(0, label.VertexBuffer, 0, FlatVertex.Stride); + graphics.SetVertexBuffer(label.VertexBuffer); // Draw graphics.Draw(PrimitiveType.TriangleStrip, 0, 2); @@ -1775,10 +1794,9 @@ namespace CodeImp.DoomBuilder.Rendering graphics.SetTextureFactor(-1); graphics.SetFogEnable(false); SetWorldTransformation(false); - graphics.SetVertexDeclaration(graphics.Shaders.FlatVertexDecl); graphics.SetShader(ShaderName.display2d_normal); graphics.SetTexture(0, General.Map.Data.WhiteTexture.Texture); - graphics.Shaders.SetDisplay2DSettings(1f, 1f, 0f, 1f, General.Settings.ClassicBilinear); + SetDisplay2DSettings(1f, 1f, 0f, 1f, General.Settings.ClassicBilinear); // Draw quads[0].Render(graphics); @@ -1811,10 +1829,9 @@ namespace CodeImp.DoomBuilder.Rendering graphics.SetTextureFactor(-1); graphics.SetFogEnable(false); SetWorldTransformation(false); - graphics.SetVertexDeclaration(graphics.Shaders.FlatVertexDecl); graphics.SetShader(ShaderName.display2d_normal); graphics.SetTexture(0, General.Map.Data.WhiteTexture.Texture); - graphics.Shaders.SetDisplay2DSettings(1f, 1f, 0f, 1f, General.Settings.ClassicBilinear); + SetDisplay2DSettings(1f, 1f, 0f, 1f, General.Settings.ClassicBilinear); // Draw quad.Render(graphics); @@ -1844,10 +1861,9 @@ namespace CodeImp.DoomBuilder.Rendering graphics.SetTextureFactor(-1); graphics.SetFogEnable(false); SetWorldTransformation(false); - graphics.SetVertexDeclaration(graphics.Shaders.FlatVertexDecl); graphics.SetShader(ShaderName.display2d_normal); graphics.SetTexture(0, texture.Texture); - graphics.Shaders.SetDisplay2DSettings(1f, 1f, 0f, 1f, General.Settings.ClassicBilinear); + SetDisplay2DSettings(1f, 1f, 0f, 1f, General.Settings.ClassicBilinear); // Draw quad.Render(graphics); @@ -1944,13 +1960,12 @@ namespace CodeImp.DoomBuilder.Rendering graphics.SetTextureFactor(-1); graphics.SetFogEnable(false); SetWorldTransformation(false); - graphics.SetVertexDeclaration(graphics.Shaders.FlatVertexDecl); graphics.SetShader(ShaderName.display2d_normal); graphics.SetTexture(0, General.Map.Data.WhiteTexture.Texture); - graphics.Shaders.SetDisplay2DSettings(1f, 1f, 0f, 1f, General.Settings.ClassicBilinear); + SetDisplay2DSettings(1f, 1f, 0f, 1f, General.Settings.ClassicBilinear); // Draw - graphics.SetVertexBuffer(0, vb, 0, FlatVertex.Stride); + graphics.SetVertexBuffer(vb); graphics.Draw(PrimitiveType.LineList, 0, pointscount / 2); vb.Dispose(); } @@ -1997,10 +2012,9 @@ namespace CodeImp.DoomBuilder.Rendering graphics.SetTextureFactor(-1); graphics.SetFogEnable(false); SetWorldTransformation(false); - graphics.SetVertexDeclaration(graphics.Shaders.FlatVertexDecl); graphics.SetShader(ShaderName.display2d_normal); graphics.SetTexture(0, General.Map.Data.WhiteTexture.Texture); - graphics.Shaders.SetDisplay2DSettings(1f, 1f, 0f, 1f, General.Settings.ClassicBilinear); + SetDisplay2DSettings(1f, 1f, 0f, 1f, General.Settings.ClassicBilinear); // Draw graphics.Draw(PrimitiveType.TriangleStrip, 0, 2, verts); diff --git a/Source/Core/Rendering/Renderer3D.cs b/Source/Core/Rendering/Renderer3D.cs index bd0b747d..72fe814e 100755 --- a/Source/Core/Rendering/Renderer3D.cs +++ b/Source/Core/Rendering/Renderer3D.cs @@ -382,7 +382,6 @@ namespace CodeImp.DoomBuilder.Rendering graphics.SetAlphaBlendEnable(false); graphics.SetAlphaTestEnable(false); graphics.SetTextureFactor(-1); - graphics.SetVertexDeclaration(graphics.Shaders.WorldVertexDecl); //mxd. SKY PASS if(skygeo.Count > 0) @@ -604,7 +603,7 @@ namespace CodeImp.DoomBuilder.Rendering graphics.SetUniform(UniformName.vertexColor, thingcolor); //Render cage - graphics.SetVertexBuffer(0, t.CageBuffer, 0, WorldVertex.Stride); + graphics.SetVertexBuffer(t.CageBuffer); graphics.Draw(PrimitiveType.LineList, 0, t.CageLength); } @@ -644,7 +643,7 @@ namespace CodeImp.DoomBuilder.Rendering graphics.SetUniform(UniformName.vertexColor, color); //Commence drawing!!11 - graphics.SetVertexBuffer(0, v.CeilingVertex ? vertexhandle.Upper : vertexhandle.Lower, 0, WorldVertex.Stride); + graphics.SetVertexBuffer(v.CeilingVertex ? vertexhandle.Upper : vertexhandle.Lower); graphics.Draw(PrimitiveType.LineList, 0, 8); } @@ -723,7 +722,7 @@ namespace CodeImp.DoomBuilder.Rendering ApplyMatrices3D(); //render - graphics.SetVertexBuffer(0, vb, 0, WorldVertex.Stride); + graphics.SetVertexBuffer(vb); graphics.Draw(PrimitiveType.LineList, 0, pointscount / 2); // Done @@ -781,7 +780,7 @@ namespace CodeImp.DoomBuilder.Rendering sector = g.Sector; // Set stream source - graphics.SetVertexBuffer(0, sector.GeometryBuffer, 0, WorldVertex.Stride); + graphics.SetVertexBuffer(sector.GeometryBuffer); } else { @@ -935,7 +934,7 @@ namespace CodeImp.DoomBuilder.Rendering ApplyMatrices3D(); // Apply buffer - graphics.SetVertexBuffer(0, t.GeometryBuffer, 0, WorldVertex.Stride); + graphics.SetVertexBuffer(t.GeometryBuffer); // Render! graphics.Draw(PrimitiveType.TriangleList, 0, t.Triangles); @@ -1055,7 +1054,7 @@ namespace CodeImp.DoomBuilder.Rendering sector = g.Sector; // Set stream source - graphics.SetVertexBuffer(0, sector.GeometryBuffer, 0, WorldVertex.Stride); + graphics.SetVertexBuffer(sector.GeometryBuffer); } else { @@ -1243,7 +1242,7 @@ namespace CodeImp.DoomBuilder.Rendering ApplyMatrices3D(); // Apply buffer - graphics.SetVertexBuffer(0, t.GeometryBuffer, 0, WorldVertex.Stride); + graphics.SetVertexBuffer(t.GeometryBuffer); // Render! graphics.Draw(PrimitiveType.TriangleList, 0, t.Triangles); @@ -1316,7 +1315,7 @@ namespace CodeImp.DoomBuilder.Rendering sector = g.Sector; // Set stream source - graphics.SetVertexBuffer(0, sector.GeometryBuffer, 0, WorldVertex.Stride); + graphics.SetVertexBuffer(sector.GeometryBuffer); } else { @@ -1773,7 +1772,7 @@ namespace CodeImp.DoomBuilder.Rendering sector = g.Sector; // Set stream source - graphics.SetVertexBuffer(0, sector.GeometryBuffer, 0, WorldVertex.Stride); + graphics.SetVertexBuffer(sector.GeometryBuffer); } else { @@ -2016,7 +2015,6 @@ namespace CodeImp.DoomBuilder.Rendering graphics.SetTransform(TransformState.Projection, Matrix.Identity); ApplyMatrices2D(); - graphics.SetVertexDeclaration(graphics.Shaders.FlatVertexDecl); graphics.SetShader(ShaderName.display2d_normal); // Texture @@ -2032,12 +2030,22 @@ namespace CodeImp.DoomBuilder.Rendering } // Draw - graphics.Shaders.SetDisplay2DSettings(1.0f, 1.0f, 0.0f, 1.0f, true); + SetDisplay2DSettings(1.0f, 1.0f, 0.0f, 1.0f, true); graphics.Draw(PrimitiveType.TriangleStrip, 0, 2, crosshairverts); } - // This switches fog on and off - public void SetFogMode(bool usefog) + private void SetDisplay2DSettings(float texelx, float texely, float fsaafactor, float alpha, bool bilinear) + { + Vector4 values = new Vector4(texelx, texely, fsaafactor, alpha); + graphics.SetUniform(UniformName.rendersettings, values); + Matrix world = graphics.GetTransform(TransformState.World); + Matrix view = graphics.GetTransform(TransformState.View); + graphics.SetUniform(UniformName.transformsettings, world * view); + graphics.SetSamplerFilter(0, bilinear ? TextureFilter.Linear : TextureFilter.Point); + } + + // This switches fog on and off + public void SetFogMode(bool usefog) { graphics.SetFogEnable(usefog); } diff --git a/Source/Core/Rendering/ShaderManager.cs b/Source/Core/Rendering/ShaderManager.cs deleted file mode 100755 index 2b51c091..00000000 --- a/Source/Core/Rendering/ShaderManager.cs +++ /dev/null @@ -1,157 +0,0 @@ - -#region ================== Copyright (c) 2007 Pascal vd Heiden - -/* - * Copyright (c) 2007 Pascal vd Heiden, www.codeimp.com - * This program is released under GNU General Public License - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - */ - -#endregion - -#region ================== Namespaces - -using System; - -#endregion - -namespace CodeImp.DoomBuilder.Rendering -{ - internal class ShaderManager : IRenderResource, IDisposable - { - #region ================== Constants - - #endregion - - #region ================== Variables - - // Shaders - private VertexDeclaration flatvertexdecl; - private VertexDeclaration worldvertexdecl; - - // Device - private RenderDevice device; - - // Disposing - private bool isdisposed; - - #endregion - - #region ================== Properties - - public VertexDeclaration FlatVertexDecl { get { return flatvertexdecl; } } - public VertexDeclaration WorldVertexDecl { get { return worldvertexdecl; } } - public bool IsDisposed { get { return isdisposed; } } - internal RenderDevice D3DDevice { get { return device; } } - - #endregion - - #region ================== Constructor / Disposer - - // Constructor - public ShaderManager(RenderDevice device) - { - // Initialize - this.device = device; - - // Load - ReloadResource(); - - // Register as resource - device.RegisterResource(this); - - // We have no destructor - GC.SuppressFinalize(this); - } - - // Disposer - public void Dispose() - { - // Not already disposed? - if(!isdisposed) - { - // Clean up - UnloadResource(); - - // Unregister as resource - device.UnregisterResource(this); - - // Done - device = null; - isdisposed = true; - } - } - - #endregion - - #region ================== Resources - - // Clean up resources - public void UnloadResource() - { - flatvertexdecl.Dispose(); - worldvertexdecl.Dispose(); - } - - // Load resources - public void ReloadResource() - { - flatvertexdecl = new VertexDeclaration(new VertexElement[] { - new VertexElement(0, 0, DeclarationType.Float3, DeclarationUsage.Position), - new VertexElement(0, 12, DeclarationType.Color, DeclarationUsage.Color), - new VertexElement(0, 16, DeclarationType.Float2, DeclarationUsage.TextureCoordinate) - }); - - worldvertexdecl = new VertexDeclaration(new VertexElement[] { - new VertexElement(0, 0, DeclarationType.Float3, DeclarationUsage.Position), - new VertexElement(0, 12, DeclarationType.Color, DeclarationUsage.Color), - new VertexElement(0, 16, DeclarationType.Float2, DeclarationUsage.TextureCoordinate), - new VertexElement(0, 24, DeclarationType.Float3, DeclarationUsage.Normal) - }); - } - - #endregion - - public void SetDisplay2DSettings(float texelx, float texely, float fsaafactor, float alpha, bool bilinear) - { - Vector4 values = new Vector4(texelx, texely, fsaafactor, alpha); - D3DDevice.SetUniform(UniformName.rendersettings, values); - Matrix world = D3DDevice.GetTransform(TransformState.World); - Matrix view = D3DDevice.GetTransform(TransformState.View); - D3DDevice.SetUniform(UniformName.transformsettings, world * view); - D3DDevice.SetSamplerFilter(0, bilinear ? TextureFilter.Linear : TextureFilter.Point); - } - - public void SetThings2DSettings(float alpha) - { - Vector4 values = new Vector4(0.0f, 0.0f, 1.0f, alpha); - D3DDevice.SetUniform(UniformName.rendersettings, values); - Matrix world = D3DDevice.GetTransform(TransformState.World); - Matrix view = D3DDevice.GetTransform(TransformState.View); - D3DDevice.SetUniform(UniformName.transformsettings, world * view); - } - - //mxd. Used to render models - public void SetThings2DTransformSettings(Matrix world) - { - Matrix view = D3DDevice.GetTransform(TransformState.View); - D3DDevice.SetUniform(UniformName.transformsettings, world * view); - } - - public void SetWorld3DConstants(bool bilinear, float maxanisotropy) - { - //mxd. It's still nice to have anisotropic filtering when texture filtering is disabled - TextureFilter magminfilter = (bilinear ? TextureFilter.Linear : TextureFilter.Point); - D3DDevice.SetSamplerFilter(0, - maxanisotropy > 1.0f ? TextureFilter.Anisotropic : magminfilter, - magminfilter, - bilinear ? TextureFilter.Linear : TextureFilter.None, // [SB] use None, otherwise textures are still filtered - maxanisotropy); - } - } -} diff --git a/Source/Core/Rendering/SurfaceManager.cs b/Source/Core/Rendering/SurfaceManager.cs index 02222d79..5177dad0 100755 --- a/Source/Core/Rendering/SurfaceManager.cs +++ b/Source/Core/Rendering/SurfaceManager.cs @@ -140,7 +140,7 @@ namespace CodeImp.DoomBuilder.Rendering { // Make the new buffer! VertexBuffer b = new VertexBuffer(); - General.Map.Graphics.SetBufferData(b, FlatVertex.Stride * set.Value.buffersizes[i]); + General.Map.Graphics.SetBufferData(b, set.Value.buffersizes[i], VertexFormat.Flat); // Start refilling the buffer with sector geometry foreach (SurfaceEntry e in set.Value.entries) @@ -273,7 +273,7 @@ namespace CodeImp.DoomBuilder.Rendering { // Make the new buffer! vb = new VertexBuffer(); - General.Map.Graphics.SetBufferData(vb, FlatVertex.Stride * buffernumvertices); + General.Map.Graphics.SetBufferData(vb, buffernumvertices, VertexFormat.Flat); // Add it. set.buffers.Add(vb); @@ -316,7 +316,7 @@ namespace CodeImp.DoomBuilder.Rendering { // Make the new buffer and lock it vb = new VertexBuffer(); - General.Map.Graphics.SetBufferData(vb, FlatVertex.Stride * buffernumvertices); + General.Map.Graphics.SetBufferData(vb, buffernumvertices, VertexFormat.Flat); } // Start refilling the buffer with sector geometry @@ -611,7 +611,6 @@ namespace CodeImp.DoomBuilder.Rendering if(!resourcesunloaded) { ShaderName pass = Renderer.FullBrightness ? ShaderName.display2d_fullbright : ShaderName.display2d_normal; //mxd - graphics.SetVertexDeclaration(graphics.Shaders.FlatVertexDecl); foreach(KeyValuePair> imgsurfaces in surfaces) { graphics.SetShader(pass); @@ -628,7 +627,7 @@ namespace CodeImp.DoomBuilder.Rendering if(set.buffers[entry.bufferindex] != lastbuffer) { lastbuffer = set.buffers[entry.bufferindex]; - graphics.SetVertexBuffer(0, lastbuffer, 0, FlatVertex.Stride); + graphics.SetVertexBuffer(lastbuffer); } // Draw diff --git a/Source/Core/Rendering/VertexDeclaration.cs b/Source/Core/Rendering/VertexDeclaration.cs deleted file mode 100644 index 8ec87338..00000000 --- a/Source/Core/Rendering/VertexDeclaration.cs +++ /dev/null @@ -1,63 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using System.Runtime.InteropServices; - -namespace CodeImp.DoomBuilder.Rendering -{ - public class VertexDeclaration : IDisposable - { - public VertexDeclaration(VertexElement[] elements) - { - Handle = VertexDeclaration_New(elements, elements.Length); - if (Handle == IntPtr.Zero) - throw new Exception("VertexDeclaration_New failed"); - } - - ~VertexDeclaration() - { - Dispose(); - } - - public bool Disposed { get { return Handle == IntPtr.Zero; } } - - public void Dispose() - { - if (!Disposed) - { - VertexDeclaration_Delete(Handle); - Handle = IntPtr.Zero; - } - } - - internal IntPtr Handle; - - [DllImport("BuilderNative.dll", CallingConvention = CallingConvention.Cdecl)] - static extern IntPtr VertexDeclaration_New(VertexElement[] elements, int count); - - [DllImport("BuilderNative.dll", CallingConvention = CallingConvention.Cdecl)] - static extern void VertexDeclaration_Delete(IntPtr handle); - } - - [StructLayout(LayoutKind.Sequential)] - public struct VertexElement - { - public VertexElement(short stream, short offset, DeclarationType type, DeclarationUsage usage) - { - Stream = stream; - Offset = offset; - Type = type; - Usage = usage; - } - - public short Stream; - public short Offset; - public DeclarationType Type; - public DeclarationUsage Usage; - } - - public enum DeclarationType : int { Float2, Float3, Color } - public enum DeclarationUsage : int { Position, Color, TextureCoordinate, Normal } -} diff --git a/Source/Core/VisualModes/VisualSector.cs b/Source/Core/VisualModes/VisualSector.cs index d2a80fc5..3dd6c3d8 100755 --- a/Source/Core/VisualModes/VisualSector.cs +++ b/Source/Core/VisualModes/VisualSector.cs @@ -138,7 +138,7 @@ namespace CodeImp.DoomBuilder.VisualModes { // Make a new buffer geobuffer = new VertexBuffer(); - graphics.SetBufferData(geobuffer, WorldVertex.Stride * numverts); + graphics.SetBufferData(geobuffer, numverts, VertexFormat.World); // Fill the buffer foreach(VisualGeometry g in allgeometry) diff --git a/Source/Native/BuilderNative.vcxproj b/Source/Native/BuilderNative.vcxproj index d50c030a..c86f39db 100644 --- a/Source/Native/BuilderNative.vcxproj +++ b/Source/Native/BuilderNative.vcxproj @@ -212,7 +212,6 @@ - @@ -229,7 +228,6 @@ - diff --git a/Source/Native/BuilderNative.vcxproj.filters b/Source/Native/BuilderNative.vcxproj.filters index e2a130ea..4fc6c5dc 100644 --- a/Source/Native/BuilderNative.vcxproj.filters +++ b/Source/Native/BuilderNative.vcxproj.filters @@ -5,7 +5,6 @@ - gl_load @@ -20,7 +19,6 @@ - gl_load diff --git a/Source/Native/RenderDevice.cpp b/Source/Native/RenderDevice.cpp index db668ed4..7db17a55 100644 --- a/Source/Native/RenderDevice.cpp +++ b/Source/Native/RenderDevice.cpp @@ -3,7 +3,6 @@ #include "RenderDevice.h" #include "VertexBuffer.h" #include "IndexBuffer.h" -#include "VertexDeclaration.h" #include "Texture.h" #include "ShaderManager.h" #include @@ -15,8 +14,17 @@ RenderDevice::RenderDevice(HWND hwnd) : Context(hwnd) if (Context) { Context.Begin(); + + glGenVertexArrays(1, &mStreamVAO); glGenBuffers(1, &mStreamVertexBuffer); + glBindVertexArray(mStreamVAO); + glBindBuffer(GL_ARRAY_BUFFER, mStreamVertexBuffer); + VertexBuffer::SetupFlatVAO(); + glBindBuffer(GL_ARRAY_BUFFER, 0); + mShaderManager = std::make_unique(); + + CheckError(); Context.End(); } } @@ -27,14 +35,15 @@ RenderDevice::~RenderDevice() { Context.Begin(); glDeleteBuffers(1, &mStreamVertexBuffer); + glDeleteVertexArrays(1, &mStreamVAO); mShaderManager->ReleaseResources(); Context.End(); } } -void RenderDevice::SetVertexBuffer(int index, VertexBuffer* buffer, long offset, long stride) +void RenderDevice::SetVertexBuffer(VertexBuffer* buffer) { - mVertexBindings[index] = { buffer, offset, stride }; + mVertexBuffer = buffer; mNeedApply = true; } @@ -173,7 +182,7 @@ void RenderDevice::DrawIndexed(PrimitiveType type, int startIndex, int primitive Context.End(); } -void RenderDevice::DrawData(PrimitiveType type, int startIndex, int primitiveCount, const void* data, int stride) +void RenderDevice::DrawData(PrimitiveType type, int startIndex, int primitiveCount, const void* data) { static const int modes[] = { GL_LINES, GL_TRIANGLES, GL_TRIANGLE_STRIP }; static const int toVertexCount[] = { 2, 3, 1 }; @@ -182,19 +191,14 @@ void RenderDevice::DrawData(PrimitiveType type, int startIndex, int primitiveCou int vertcount = toVertexStart[(int)type] + primitiveCount * toVertexCount[(int)type]; Context.Begin(); - mStreamBufferStride = stride; - glBindBuffer(GL_ARRAY_BUFFER, mStreamVertexBuffer); - glBufferData(GL_ARRAY_BUFFER, vertcount * (size_t)stride, static_cast(data) + startIndex * (size_t)stride, GL_STREAM_DRAW); - ApplyChanges(); - glDrawArrays(modes[(int)type], 0, vertcount); - mStreamBufferStride = 0; - Context.End(); -} + if (mNeedApply) ApplyChanges(); -void RenderDevice::SetVertexDeclaration(VertexDeclaration* decl) -{ - mVertexDeclaration = decl; - mNeedApply = true; + glBindBuffer(GL_ARRAY_BUFFER, mStreamVertexBuffer); + glBufferData(GL_ARRAY_BUFFER, vertcount * (size_t)VertexBuffer::FlatStride, static_cast(data) + startIndex * (size_t)VertexBuffer::FlatStride, GL_STREAM_DRAW); + glBindVertexArray(mStreamVAO); + glDrawArrays(modes[(int)type], 0, vertcount); + ApplyVertexBuffer(); + Context.End(); } void RenderDevice::StartRendering(bool clear, int backcolor, Texture* target, bool usedepthbuffer) @@ -274,9 +278,10 @@ void RenderDevice::CopyTexture(Texture* src, Texture* dst, CubeMapFace face) glBindFramebuffer(GL_READ_FRAMEBUFFER, oldFramebuffer); } -void RenderDevice::SetVertexBufferData(VertexBuffer* buffer, void* data, int64_t size) +void RenderDevice::SetVertexBufferData(VertexBuffer* buffer, void* data, int64_t size, VertexFormat format) { Context.Begin(); + buffer->Format = format; GLint oldbinding = 0; glGetIntegerv(GL_ARRAY_BUFFER_BINDING, &oldbinding); glBindBuffer(GL_ARRAY_BUFFER, buffer->GetBuffer()); @@ -359,7 +364,7 @@ Shader* RenderDevice::GetActiveShader() void RenderDevice::ApplyChanges() { ApplyShader(); - ApplyVertexBuffers(); + ApplyVertexBuffer(); ApplyIndexBuffer(); ApplyUniforms(); ApplyTextures(); @@ -435,67 +440,10 @@ void RenderDevice::ApplyIndexBuffer() } } -void RenderDevice::ApplyVertexBuffers() +void RenderDevice::ApplyVertexBuffer() { - static const int typeSize[] = { 2, 3, GL_BGRA }; - static const int type[] = { GL_FLOAT, GL_FLOAT, GL_UNSIGNED_BYTE }; - static const int typeNormalized[] = { GL_FALSE, GL_FALSE, GL_TRUE }; - - if (mVertexDeclaration) - { - if (!mVAO) - { - glGenVertexArrays(1, &mVAO); - glBindVertexArray(mVAO); - } - - if (mStreamBufferStride) - { - glBindBuffer(GL_ARRAY_BUFFER, mStreamVertexBuffer); - for (size_t i = 0; i < mVertexDeclaration->Elements.size(); i++) - { - const auto& element = mVertexDeclaration->Elements[i]; - GLuint location = (int)element.Usage; - - glEnableVertexAttribArray(location); - glVertexAttribPointer(location, typeSize[(int)element.Type], type[(int)element.Type], typeNormalized[(int)element.Type], mStreamBufferStride, (const void*)element.Offset); - - mEnabledVertexAttributes[location] = 2; - } - } - else - { - for (size_t i = 0; i < mVertexDeclaration->Elements.size(); i++) - { - const auto& element = mVertexDeclaration->Elements[i]; - auto& vertBinding = mVertexBindings[element.Stream]; - GLuint location = (int)element.Usage; - if (vertBinding.Buffer) - { - GLuint vertexbuffer = vertBinding.Buffer->GetBuffer(); - glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer); - glEnableVertexAttribArray(location); - glVertexAttribPointer(location, typeSize[(int)element.Type], type[(int)element.Type], typeNormalized[(int)element.Type], vertBinding.Stride, (const void*)(element.Offset + (ptrdiff_t)vertBinding.Offset)); - - mEnabledVertexAttributes[location] = 2; - } - } - } - glBindBuffer(GL_ARRAY_BUFFER, 0); - } - - for (size_t i = 0; i < NumSlots; i++) - { - if (mEnabledVertexAttributes[i] == 2) - { - mEnabledVertexAttributes[i] = 1; - } - else if (mEnabledVertexAttributes[i] == 1) - { - glDisableVertexAttribArray((GLuint)i); - mEnabledVertexAttributes[i] = 0; - } - } + if (mVertexBuffer) + glBindVertexArray(mVertexBuffer->GetVAO()); } void RenderDevice::SetShader(ShaderName name) @@ -617,9 +565,9 @@ void RenderDevice_SetUniform(RenderDevice* device, UniformName name, const void* device->SetUniform(name, values, count); } -void RenderDevice_SetVertexBuffer(RenderDevice* device, int index, VertexBuffer* buffer, long offset, long stride) +void RenderDevice_SetVertexBuffer(RenderDevice* device, VertexBuffer* buffer) { - device->SetVertexBuffer(index, buffer, offset, stride); + device->SetVertexBuffer(buffer); } void RenderDevice_SetIndexBuffer(RenderDevice* device, IndexBuffer* buffer) @@ -702,14 +650,9 @@ void RenderDevice_DrawIndexed(RenderDevice* device, PrimitiveType type, int star device->DrawIndexed(type, startIndex, primitiveCount); } -void RenderDevice_DrawData(RenderDevice* device, PrimitiveType type, int startIndex, int primitiveCount, const void* data, int stride) +void RenderDevice_DrawData(RenderDevice* device, PrimitiveType type, int startIndex, int primitiveCount, const void* data) { - device->DrawData(type, startIndex, primitiveCount, data, stride); -} - -void RenderDevice_SetVertexDeclaration(RenderDevice* device, VertexDeclaration* decl) -{ - device->SetVertexDeclaration(decl); + device->DrawData(type, startIndex, primitiveCount, data); } void RenderDevice_StartRendering(RenderDevice* device, bool clear, int backcolor, Texture* target, bool usedepthbuffer) @@ -737,9 +680,9 @@ void RenderDevice_CopyTexture(RenderDevice* device, Texture* src, Texture* dst, device->CopyTexture(src, dst, face); } -void RenderDevice_SetVertexBufferData(RenderDevice* device, VertexBuffer* buffer, void* data, int64_t size) +void RenderDevice_SetVertexBufferData(RenderDevice* device, VertexBuffer* buffer, void* data, int64_t size, VertexFormat format) { - device->SetVertexBufferData(buffer, data, size); + device->SetVertexBufferData(buffer, data, size, format); } void RenderDevice_SetVertexBufferSubdata(RenderDevice* device, VertexBuffer* buffer, int64_t destOffset, void* data, int64_t size) diff --git a/Source/Native/RenderDevice.h b/Source/Native/RenderDevice.h index 21d87fc9..ca734ba9 100644 --- a/Source/Native/RenderDevice.h +++ b/Source/Native/RenderDevice.h @@ -4,11 +4,11 @@ class VertexBuffer; class IndexBuffer; -class VertexDeclaration; class Texture; class ShaderManager; class Shader; enum class CubeMapFace; +enum class VertexFormat; enum class Cull : int { None, Clockwise }; enum class Blend : int { InverseSourceAlpha, SourceAlpha, One, BlendFactor }; @@ -79,7 +79,7 @@ public: void SetShader(ShaderName name); void SetUniform(UniformName name, const void* values, int count); - void SetVertexBuffer(int index, VertexBuffer* buffer, long offset, long stride); + void SetVertexBuffer(VertexBuffer* buffer); void SetIndexBuffer(IndexBuffer* buffer); void SetAlphaBlendEnable(bool value); void SetAlphaTestEnable(bool value); @@ -96,15 +96,14 @@ public: void SetSamplerState(int unit, TextureAddress addressU, TextureAddress addressV, TextureAddress addressW); void Draw(PrimitiveType type, int startIndex, int primitiveCount); void DrawIndexed(PrimitiveType type, int startIndex, int primitiveCount); - void DrawData(PrimitiveType type, int startIndex, int primitiveCount, const void* data, int stride); - void SetVertexDeclaration(VertexDeclaration* decl); + void DrawData(PrimitiveType type, int startIndex, int primitiveCount, const void* data); void StartRendering(bool clear, int backcolor, Texture* target, bool usedepthbuffer); void FinishRendering(); void Present(); void ClearTexture(int backcolor, Texture* texture); void CopyTexture(Texture* src, Texture* dst, CubeMapFace face); - void SetVertexBufferData(VertexBuffer* buffer, void* data, int64_t size); + void SetVertexBufferData(VertexBuffer* buffer, void* data, int64_t size, VertexFormat format); void SetVertexBufferSubdata(VertexBuffer* buffer, int64_t destOffset, void* data, int64_t size); void SetIndexBufferData(IndexBuffer* buffer, void* data, int64_t size); @@ -116,7 +115,7 @@ public: void InvalidateTexture(Texture* texture); void ApplyChanges(); - void ApplyVertexBuffers(); + void ApplyVertexBuffer(); void ApplyIndexBuffer(); void ApplyShader(); void ApplyUniforms(); @@ -133,16 +132,6 @@ public: OpenGLContext Context; - struct VertexBinding - { - VertexBinding() = default; - VertexBinding(VertexBuffer* buffer, long offset, long stride) : Buffer(buffer), Offset(offset), Stride(stride) { } - - VertexBuffer* Buffer = nullptr; - long Offset = 0; - long Stride = 0; - }; - struct TextureUnit { Texture* Tex = nullptr; @@ -156,13 +145,9 @@ public: enum { NumSlots = 16 }; - VertexDeclaration *mVertexDeclaration = nullptr; - GLuint mVAO = 0; - int mEnabledVertexAttributes[NumSlots] = { 0 }; - VertexBinding mVertexBindings[NumSlots]; - TextureUnit mTextureUnits[NumSlots]; + VertexBuffer* mVertexBuffer = nullptr; IndexBuffer* mIndexBuffer = nullptr; std::unique_ptr mShaderManager; @@ -177,7 +162,7 @@ public: UniformEntry mUniforms[4 * 16 + 12 * 4]; GLuint mStreamVertexBuffer = 0; - int mStreamBufferStride = 0; + GLuint mStreamVAO = 0; Cull mCullMode = Cull::None; FillMode mFillMode = FillMode::Solid; diff --git a/Source/Native/Shader.cpp b/Source/Native/Shader.cpp index 14744165..e591cecb 100644 --- a/Source/Native/Shader.cpp +++ b/Source/Native/Shader.cpp @@ -1,7 +1,6 @@ #include "Precomp.h" #include "Shader.h" -#include "VertexDeclaration.h" #include "RenderDevice.h" #include diff --git a/Source/Native/Shader.h b/Source/Native/Shader.h index 81c64eb9..4a372c22 100644 --- a/Source/Native/Shader.h +++ b/Source/Native/Shader.h @@ -3,6 +3,8 @@ #include #include "RenderDevice.h" +enum class DeclarationUsage : int32_t { Position, Color, TextureCoordinate, Normal }; + class Shader { public: diff --git a/Source/Native/VertexBuffer.cpp b/Source/Native/VertexBuffer.cpp index 00cb87d1..dfad2f9f 100644 --- a/Source/Native/VertexBuffer.cpp +++ b/Source/Native/VertexBuffer.cpp @@ -1,6 +1,7 @@ #include "Precomp.h" #include "VertexBuffer.h" +#include "Shader.h" VertexBuffer::VertexBuffer() { @@ -18,6 +19,44 @@ GLuint VertexBuffer::GetBuffer() return mBuffer; } +GLuint VertexBuffer::GetVAO() +{ + if (!mVAO) + { + glGenVertexArrays(1, &mVAO); + glBindVertexArray(mVAO); + glBindBuffer(GL_ARRAY_BUFFER, GetBuffer()); + if (Format == VertexFormat::Flat) + SetupFlatVAO(); + else + SetupWorldVAO(); + glBindBuffer(GL_ARRAY_BUFFER, 0); + } + return mVAO; +} + +void VertexBuffer::SetupFlatVAO() +{ + glEnableVertexAttribArray((int)DeclarationUsage::Position); + glEnableVertexAttribArray((int)DeclarationUsage::Color); + glEnableVertexAttribArray((int)DeclarationUsage::TextureCoordinate); + glVertexAttribPointer((int)DeclarationUsage::Position, 3, GL_FLOAT, GL_FALSE, FlatStride, (const void*)0); + glVertexAttribPointer((int)DeclarationUsage::Color, GL_BGRA, GL_UNSIGNED_BYTE, GL_TRUE, FlatStride, (const void*)12); + glVertexAttribPointer((int)DeclarationUsage::TextureCoordinate, 2, GL_FLOAT, GL_FALSE, FlatStride, (const void*)16); +} + +void VertexBuffer::SetupWorldVAO() +{ + glEnableVertexAttribArray((int)DeclarationUsage::Position); + glEnableVertexAttribArray((int)DeclarationUsage::Color); + glEnableVertexAttribArray((int)DeclarationUsage::TextureCoordinate); + glEnableVertexAttribArray((int)DeclarationUsage::Normal); + glVertexAttribPointer((int)DeclarationUsage::Position, 3, GL_FLOAT, GL_FALSE, WorldStride, (const void*)0); + glVertexAttribPointer((int)DeclarationUsage::Color, GL_BGRA, GL_UNSIGNED_BYTE, GL_TRUE, WorldStride, (const void*)12); + glVertexAttribPointer((int)DeclarationUsage::TextureCoordinate, 2, GL_FLOAT, GL_FALSE, WorldStride, (const void*)16); + glVertexAttribPointer((int)DeclarationUsage::Normal, 3, GL_FLOAT, GL_FALSE, WorldStride, (const void*)24); +} + ///////////////////////////////////////////////////////////////////////////// VertexBuffer* VertexBuffer_New() diff --git a/Source/Native/VertexBuffer.h b/Source/Native/VertexBuffer.h index 084b927d..003ecae8 100644 --- a/Source/Native/VertexBuffer.h +++ b/Source/Native/VertexBuffer.h @@ -1,5 +1,7 @@ #pragma once +enum class VertexFormat : int32_t { Flat, World }; + class VertexBuffer { public: @@ -7,7 +9,17 @@ public: ~VertexBuffer(); GLuint GetBuffer(); + GLuint GetVAO(); + + VertexFormat Format = VertexFormat::Flat; + + static const int FlatStride = 24; + static const int WorldStride = 36; + + static void SetupFlatVAO(); + static void SetupWorldVAO(); private: GLuint mBuffer = 0; + GLuint mVAO = 0; }; diff --git a/Source/Native/VertexDeclaration.cpp b/Source/Native/VertexDeclaration.cpp deleted file mode 100644 index 649f1128..00000000 --- a/Source/Native/VertexDeclaration.cpp +++ /dev/null @@ -1,17 +0,0 @@ - -#include "Precomp.h" -#include "VertexDeclaration.h" - -VertexDeclaration::VertexDeclaration(const VertexElement* elements, int count) : Elements(elements, elements + count) -{ -} - -VertexDeclaration* VertexDeclaration_New(const VertexElement* elements, int count) -{ - return new VertexDeclaration(elements, count); -} - -void VertexDeclaration_Delete(VertexDeclaration* decl) -{ - delete decl; -} diff --git a/Source/Native/VertexDeclaration.h b/Source/Native/VertexDeclaration.h deleted file mode 100644 index 4f6015c2..00000000 --- a/Source/Native/VertexDeclaration.h +++ /dev/null @@ -1,20 +0,0 @@ -#pragma once - -enum class DeclarationType : int32_t { Float2, Float3, Color }; -enum class DeclarationUsage : int32_t { Position, Color, TextureCoordinate, Normal }; - -struct VertexElement -{ - int16_t Stream; - int16_t Offset; - DeclarationType Type; - DeclarationUsage Usage; -}; - -class VertexDeclaration -{ -public: - VertexDeclaration(const VertexElement* elements, int count); - - std::vector Elements; -}; diff --git a/Source/Native/exports.def b/Source/Native/exports.def index dbabc667..0f5da2f2 100644 --- a/Source/Native/exports.def +++ b/Source/Native/exports.def @@ -23,7 +23,6 @@ EXPORTS RenderDevice_Draw RenderDevice_DrawIndexed RenderDevice_DrawData - RenderDevice_SetVertexDeclaration RenderDevice_StartRendering RenderDevice_FinishRendering RenderDevice_Present @@ -38,8 +37,6 @@ EXPORTS RenderDevice_UnlockTexture VertexBuffer_New VertexBuffer_Delete - VertexDeclaration_New - VertexDeclaration_Delete IndexBuffer_New IndexBuffer_Delete Texture_New