- simplify vertex buffer and vertex format

This commit is contained in:
Magnus Norddahl 2019-08-16 13:07:57 +02:00
parent d701987fb4
commit 9b53939f0c
22 changed files with 202 additions and 471 deletions

View file

@ -92,7 +92,7 @@ namespace CodeImp.DoomBuilder.Actions
{ {
Handle = RawMouse_New(control.Handle); Handle = RawMouse_New(control.Handle);
if (Handle == IntPtr.Zero) if (Handle == IntPtr.Zero)
throw new Exception("VertexDeclaration_New failed"); throw new Exception("RawMouse_New failed");
} }
~RawMouse() ~RawMouse()

View file

@ -232,7 +232,6 @@
<Compile Include="Rendering\Vector3.cs" /> <Compile Include="Rendering\Vector3.cs" />
<Compile Include="Rendering\Vector4.cs" /> <Compile Include="Rendering\Vector4.cs" />
<Compile Include="Rendering\VertexBuffer.cs" /> <Compile Include="Rendering\VertexBuffer.cs" />
<Compile Include="Rendering\VertexDeclaration.cs" />
<Compile Include="Windows\ThingStatisticsForm.cs"> <Compile Include="Windows\ThingStatisticsForm.cs">
<SubType>Form</SubType> <SubType>Form</SubType>
</Compile> </Compile>
@ -573,7 +572,6 @@
<Compile Include="Rendering\Renderer.cs" /> <Compile Include="Rendering\Renderer.cs" />
<Compile Include="Rendering\Renderer2D.cs" /> <Compile Include="Rendering\Renderer2D.cs" />
<Compile Include="Rendering\Renderer3D.cs" /> <Compile Include="Rendering\Renderer3D.cs" />
<Compile Include="Rendering\ShaderManager.cs" />
<Compile Include="VisualModes\VisualSector.cs" /> <Compile Include="VisualModes\VisualSector.cs" />
<Compile Include="Rendering\WorldVertex.cs" /> <Compile Include="Rendering\WorldVertex.cs" />
<Compile Include="ZDoom\DecorateActorStructure.cs" /> <Compile Include="ZDoom\DecorateActorStructure.cs" />

View file

@ -3465,7 +3465,6 @@ namespace CodeImp.DoomBuilder.Data
General.Map.Graphics.SetUniform(UniformName.campos, new Vector4()); General.Map.Graphics.SetUniform(UniformName.campos, new Vector4());
// Begin fullbright shaderpass // Begin fullbright shaderpass
General.Map.Graphics.SetVertexDeclaration(General.Map.Graphics.Shaders.WorldVertexDecl);
General.Map.Graphics.SetShader(ShaderName.world3d_fullbright); General.Map.Graphics.SetShader(ShaderName.world3d_fullbright);
// Render to the six faces of the cube map // Render to the six faces of the cube map

View file

@ -22,7 +22,7 @@ namespace CodeImp.DoomBuilder.Rendering
internal void Draw(RenderDevice device) internal void Draw(RenderDevice device)
{ {
device.SetVertexBuffer(0, Vertices, 0, WorldVertex.Stride); device.SetVertexBuffer(Vertices);
device.SetIndexBuffer(Indices); device.SetIndexBuffer(Indices);
device.DrawIndexed(PrimitiveType.TriangleList, 0, Count / 3); device.DrawIndexed(PrimitiveType.TriangleList, 0, Count / 3);
} }

View file

@ -37,7 +37,7 @@ namespace CodeImp.DoomBuilder.Rendering
throw new Exception("RenderDevice_New failed"); throw new Exception("RenderDevice_New failed");
RenderTarget = rendertarget; RenderTarget = rendertarget;
Shaders = new ShaderManager(this);
SetupSettings(); SetupSettings();
} }
@ -102,9 +102,9 @@ namespace CodeImp.DoomBuilder.Rendering
}, 16); }, 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) public void SetIndexBuffer(IndexBuffer buffer)
@ -236,12 +236,7 @@ namespace CodeImp.DoomBuilder.Rendering
public void Draw(PrimitiveType type, int startIndex, int primitiveCount, FlatVertex[] data) public void Draw(PrimitiveType type, int startIndex, int primitiveCount, FlatVertex[] data)
{ {
RenderDevice_DrawData(Handle, type, startIndex, primitiveCount, data, Marshal.SizeOf<FlatVertex>()); RenderDevice_DrawData(Handle, type, startIndex, primitiveCount, data);
}
public void SetVertexDeclaration(VertexDeclaration decl)
{
RenderDevice_SetVertexDeclaration(Handle, decl != null ? decl.Handle : IntPtr.Zero);
} }
public void StartRendering(bool clear, Color4 backcolor) 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<int>()); RenderDevice_SetIndexBufferData(Handle, buffer.Handle, data, data.Length * Marshal.SizeOf<int>());
} }
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) public void SetBufferData(VertexBuffer buffer, FlatVertex[] data)
{ {
RenderDevice_SetVertexBufferData(Handle, buffer.Handle, data, data.Length * Marshal.SizeOf<FlatVertex>()); RenderDevice_SetVertexBufferData(Handle, buffer.Handle, data, data.Length * Marshal.SizeOf<FlatVertex>(), VertexFormat.Flat);
} }
public void SetBufferData(VertexBuffer buffer, WorldVertex[] data) public void SetBufferData(VertexBuffer buffer, WorldVertex[] data)
{ {
RenderDevice_SetVertexBufferData(Handle, buffer.Handle, data, data.Length * Marshal.SizeOf<WorldVertex>()); RenderDevice_SetVertexBufferData(Handle, buffer.Handle, data, data.Length * Marshal.SizeOf<WorldVertex>(), VertexFormat.World);
} }
public void SetBufferSubdata(VertexBuffer buffer, long destOffset, FlatVertex[] data) public void SetBufferSubdata(VertexBuffer buffer, long destOffset, FlatVertex[] data)
@ -379,8 +374,13 @@ namespace CodeImp.DoomBuilder.Rendering
// Texture addressing // Texture addressing
SetSamplerState(0, TextureAddress.Wrap); SetSamplerState(0, TextureAddress.Wrap);
// Shader settings //mxd. It's still nice to have anisotropic filtering when texture filtering is disabled
Shaders.SetWorld3DConstants(General.Settings.VisualBilinear, General.Settings.FilterAnisotropy); 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 // Initialize presentations
Presentation.Initialize(); Presentation.Initialize();
@ -401,7 +401,7 @@ namespace CodeImp.DoomBuilder.Rendering
static extern IntPtr RenderDevice_SetUniform(IntPtr hwnd, UniformName name, float[] data, int count); static extern IntPtr RenderDevice_SetUniform(IntPtr hwnd, UniformName name, float[] data, int count);
[DllImport("BuilderNative.dll", CallingConvention = CallingConvention.Cdecl)] [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)] [DllImport("BuilderNative.dll", CallingConvention = CallingConvention.Cdecl)]
static extern void RenderDevice_SetIndexBuffer(IntPtr handle, IntPtr buffer); 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); static extern void RenderDevice_DrawIndexed(IntPtr handle, PrimitiveType type, int startIndex, int primitiveCount);
[DllImport("BuilderNative.dll", CallingConvention = CallingConvention.Cdecl)] [DllImport("BuilderNative.dll", CallingConvention = CallingConvention.Cdecl)]
static extern void RenderDevice_DrawData(IntPtr handle, PrimitiveType type, int startIndex, int primitiveCount, FlatVertex[] data, int stride); 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_SetVertexDeclaration(IntPtr handle, IntPtr decl);
[DllImport("BuilderNative.dll", CallingConvention = CallingConvention.Cdecl)] [DllImport("BuilderNative.dll", CallingConvention = CallingConvention.Cdecl)]
static extern void RenderDevice_StartRendering(IntPtr handle, bool clear, int backcolor); 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); static extern void RenderDevice_SetIndexBufferData(IntPtr handle, IntPtr buffer, int[] data, long size);
[DllImport("BuilderNative.dll", CallingConvention = CallingConvention.Cdecl)] [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)] [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)] [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)] [DllImport("BuilderNative.dll", CallingConvention = CallingConvention.Cdecl)]
static extern void RenderDevice_SetVertexBufferSubdata(IntPtr handle, IntPtr buffer, long destOffset, FlatVertex[] data, long sizeInBytes); 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<int> AA_STEPS = new List<int> { 0, 2, 4, 8 }; public static readonly List<int> AA_STEPS = new List<int> { 0, 2, 4, 8 };
internal RenderTargetControl RenderTarget { get; private set; } internal RenderTargetControl RenderTarget { get; private set; }
internal ShaderManager Shaders { get; private set; }
// Make a color from ARGB // Make a color from ARGB
public static int ARGB(float a, float r, float g, float b) public static int ARGB(float a, float r, float g, float b)
@ -600,6 +596,7 @@ namespace CodeImp.DoomBuilder.Rendering
campos campos
} }
public enum VertexFormat : int { Flat, World }
public enum Cull : int { None, Clockwise } public enum Cull : int { None, Clockwise }
public enum Blend : int { InverseSourceAlpha, SourceAlpha, One, BlendFactor } public enum Blend : int { InverseSourceAlpha, SourceAlpha, One, BlendFactor }
public enum BlendOperation : int { Add, ReverseSubtract } public enum BlendOperation : int { Add, ReverseSubtract }

View file

@ -184,9 +184,8 @@ namespace CodeImp.DoomBuilder.Rendering
graphics.SetCullMode(Cull.None); graphics.SetCullMode(Cull.None);
graphics.SetZEnable(false); graphics.SetZEnable(false);
graphics.SetFogEnable(false); graphics.SetFogEnable(false);
graphics.SetVertexBuffer(0, screenverts, 0, FlatVertex.Stride); graphics.SetVertexBuffer(screenverts);
graphics.SetTransform(TransformState.World, Matrix.Identity); graphics.SetTransform(TransformState.World, Matrix.Identity);
graphics.SetVertexDeclaration(graphics.Shaders.FlatVertexDecl);
// Go for all layers // Go for all layers
foreach(PresentLayer layer in present.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; if((backimageverts == null) || (General.Map.Grid.Background.Texture == null)) break;
graphics.SetShader(aapass); graphics.SetShader(aapass);
graphics.SetTexture(0, General.Map.Grid.Background.Texture); 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.Draw(PrimitiveType.TriangleStrip, 0, 2, backimageverts);
graphics.SetVertexBuffer(0, screenverts, 0, FlatVertex.Stride); graphics.SetVertexBuffer(screenverts);
break; break;
// GRID // GRID
case RendererLayer.Grid: case RendererLayer.Grid:
graphics.SetShader(aapass); graphics.SetShader(aapass);
graphics.SetTexture(0, backtex); 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); graphics.Draw(PrimitiveType.TriangleStrip, 0, 2);
break; break;
@ -253,7 +252,7 @@ namespace CodeImp.DoomBuilder.Rendering
case RendererLayer.Geometry: case RendererLayer.Geometry:
graphics.SetShader(aapass); graphics.SetShader(aapass);
graphics.SetTexture(0, plottertex); 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); graphics.Draw(PrimitiveType.TriangleStrip, 0, 2);
break; break;
@ -261,7 +260,7 @@ namespace CodeImp.DoomBuilder.Rendering
case RendererLayer.Things: case RendererLayer.Things:
graphics.SetShader(aapass); graphics.SetShader(aapass);
graphics.SetTexture(0, thingstex); 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); graphics.Draw(PrimitiveType.TriangleStrip, 0, 2);
break; break;
@ -269,7 +268,7 @@ namespace CodeImp.DoomBuilder.Rendering
case RendererLayer.Overlay: case RendererLayer.Overlay:
graphics.SetShader(aapass); graphics.SetShader(aapass);
graphics.SetTexture(0, overlaytex); 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); graphics.Draw(PrimitiveType.TriangleStrip, 0, 2);
break; break;
@ -277,7 +276,7 @@ namespace CodeImp.DoomBuilder.Rendering
case RendererLayer.Surface: case RendererLayer.Surface:
graphics.SetShader(aapass); graphics.SetShader(aapass);
graphics.SetTexture(0, surfacetex); 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); graphics.Draw(PrimitiveType.TriangleStrip, 0, 2);
break; break;
} }
@ -289,7 +288,7 @@ namespace CodeImp.DoomBuilder.Rendering
// Release binds // Release binds
graphics.SetTexture(0, null); graphics.SetTexture(0, null);
graphics.SetVertexBuffer(0, null, 0, 0); graphics.SetVertexBuffer(null);
} }
#endregion #endregion
@ -382,7 +381,7 @@ namespace CodeImp.DoomBuilder.Rendering
// Create vertex buffers // Create vertex buffers
screenverts = new VertexBuffer(); screenverts = new VertexBuffer();
thingsvertices = 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 // Make screen vertices
FlatVertex[] verts = CreateScreenVerts(structsize); FlatVertex[] verts = CreateScreenVerts(structsize);
@ -492,6 +491,32 @@ namespace CodeImp.DoomBuilder.Rendering
} }
} }
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);
}
/// <summary> /// <summary>
/// This unprojects display coordinates (screen space) to map coordinates /// This unprojects display coordinates (screen space) to map coordinates
/// </summary> /// </summary>
@ -1207,14 +1232,13 @@ namespace CodeImp.DoomBuilder.Rendering
graphics.SetAlphaTestEnable(false); graphics.SetAlphaTestEnable(false);
graphics.SetFogEnable(false); graphics.SetFogEnable(false);
graphics.SetTextureFactor(alphacolor.ToArgb()); graphics.SetTextureFactor(alphacolor.ToArgb());
graphics.SetVertexBuffer(0, thingsvertices, 0, FlatVertex.Stride); graphics.SetVertexBuffer(thingsvertices);
// Set things texture // Set things texture
graphics.SetTexture(0, General.Map.Data.ThingTexture.Texture); //mxd graphics.SetTexture(0, General.Map.Data.ThingTexture.Texture); //mxd
SetWorldTransformation(false); SetWorldTransformation(false);
graphics.SetVertexDeclaration(graphics.Shaders.FlatVertexDecl);
graphics.SetShader(ShaderName.things2d_thing); graphics.SetShader(ShaderName.things2d_thing);
graphics.Shaders.SetThings2DSettings(alpha); SetThings2DSettings(alpha);
// Determine next lock size // Determine next lock size
int locksize = (things.Count > THING_BUFFER_SIZE) ? THING_BUFFER_SIZE : things.Count; 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 position = Matrix.Translation(screenpos.x, screenpos.y, 0.0f);
Matrix world = General.Map.Data.ModeldefEntries[t.Type].Transform * modelscale * rotation * viewscale * position; Matrix world = General.Map.Data.ModeldefEntries[t.Type].Transform * modelscale * rotation * viewscale * position;
graphics.Shaders.SetThings2DTransformSettings(world); SetThings2DTransformSettings(world);
// Draw // Draw
graphics.SetVertexDeclaration(graphics.Shaders.WorldVertexDecl);
foreach(Mesh mesh in mde.Model.Meshes) mesh.Draw(graphics); foreach(Mesh mesh in mde.Model.Meshes) mesh.Draw(graphics);
} }
} }
@ -1565,7 +1588,7 @@ namespace CodeImp.DoomBuilder.Rendering
graphics.SetTextureFactor(-1); graphics.SetTextureFactor(-1);
graphics.SetFogEnable(false); graphics.SetFogEnable(false);
SetWorldTransformation(true); SetWorldTransformation(true);
graphics.Shaders.SetDisplay2DSettings(1f, 1f, 0f, 1f, General.Settings.ClassicBilinear); SetDisplay2DSettings(1f, 1f, 0f, 1f, General.Settings.ClassicBilinear);
// Prepare for rendering // Prepare for rendering
switch(viewmode) switch(viewmode)
@ -1622,11 +1645,10 @@ namespace CodeImp.DoomBuilder.Rendering
graphics.SetAlphaTestEnable(false); graphics.SetAlphaTestEnable(false);
graphics.SetTextureFactor(-1); graphics.SetTextureFactor(-1);
graphics.SetFogEnable(false); graphics.SetFogEnable(false);
graphics.SetVertexDeclaration(graphics.Shaders.FlatVertexDecl);
graphics.SetShader(ShaderName.display2d_normal); graphics.SetShader(ShaderName.display2d_normal);
graphics.SetTexture(0, t); graphics.SetTexture(0, t);
SetWorldTransformation(transformcoords); SetWorldTransformation(transformcoords);
graphics.Shaders.SetDisplay2DSettings(1f, 1f, 0f, 1f, General.Settings.ClassicBilinear); SetDisplay2DSettings(1f, 1f, 0f, 1f, General.Settings.ClassicBilinear);
// Draw // Draw
graphics.Draw(PrimitiveType.TriangleList, 0, vertices.Length / 3, vertices); graphics.Draw(PrimitiveType.TriangleList, 0, vertices.Length / 3, vertices);
@ -1648,10 +1670,9 @@ namespace CodeImp.DoomBuilder.Rendering
SetWorldTransformation(true); SetWorldTransformation(true);
graphics.SetUniform(UniformName.FillColor, new Color4(color)); graphics.SetUniform(UniformName.FillColor, new Color4(color));
graphics.Shaders.SetThings2DSettings(1.0f); SetThings2DSettings(1.0f);
// Draw // Draw
graphics.SetVertexDeclaration(graphics.Shaders.FlatVertexDecl);
graphics.SetShader(ShaderName.things2d_fill); graphics.SetShader(ShaderName.things2d_fill);
graphics.Draw(PrimitiveType.TriangleList, 0, vertices.Length / 3, vertices); graphics.Draw(PrimitiveType.TriangleList, 0, vertices.Length / 3, vertices);
} }
@ -1674,12 +1695,11 @@ namespace CodeImp.DoomBuilder.Rendering
graphics.SetAlphaTestEnable(false); graphics.SetAlphaTestEnable(false);
graphics.SetTextureFactor(-1); graphics.SetTextureFactor(-1);
graphics.SetFogEnable(false); graphics.SetFogEnable(false);
graphics.SetVertexDeclaration(graphics.Shaders.FlatVertexDecl);
graphics.SetShader(ShaderName.display2d_normal); graphics.SetShader(ShaderName.display2d_normal);
graphics.SetTexture(0, label.Texture); graphics.SetTexture(0, label.Texture);
SetWorldTransformation(false); SetWorldTransformation(false);
graphics.Shaders.SetDisplay2DSettings(1f, 1f, 0f, 1f, false); SetDisplay2DSettings(1f, 1f, 0f, 1f, false);
graphics.SetVertexBuffer(0, label.VertexBuffer, 0, FlatVertex.Stride); graphics.SetVertexBuffer(label.VertexBuffer);
// Draw // Draw
graphics.Draw(PrimitiveType.TriangleStrip, 0, 2); graphics.Draw(PrimitiveType.TriangleStrip, 0, 2);
@ -1707,9 +1727,8 @@ namespace CodeImp.DoomBuilder.Rendering
graphics.SetTextureFactor(-1); graphics.SetTextureFactor(-1);
graphics.SetFogEnable(false); graphics.SetFogEnable(false);
SetWorldTransformation(false); SetWorldTransformation(false);
graphics.SetVertexDeclaration(graphics.Shaders.FlatVertexDecl);
graphics.SetShader(ShaderName.display2d_normal); graphics.SetShader(ShaderName.display2d_normal);
graphics.Shaders.SetDisplay2DSettings(1f, 1f, 0f, 1f, false); SetDisplay2DSettings(1f, 1f, 0f, 1f, false);
foreach(ITextLabel label in labels) foreach(ITextLabel label in labels)
{ {
@ -1717,7 +1736,7 @@ namespace CodeImp.DoomBuilder.Rendering
if(!label.SkipRendering) if(!label.SkipRendering)
{ {
graphics.SetTexture(0, label.Texture); graphics.SetTexture(0, label.Texture);
graphics.SetVertexBuffer(0, label.VertexBuffer, 0, FlatVertex.Stride); graphics.SetVertexBuffer(label.VertexBuffer);
// Draw // Draw
graphics.Draw(PrimitiveType.TriangleStrip, 0, 2); graphics.Draw(PrimitiveType.TriangleStrip, 0, 2);
@ -1775,10 +1794,9 @@ namespace CodeImp.DoomBuilder.Rendering
graphics.SetTextureFactor(-1); graphics.SetTextureFactor(-1);
graphics.SetFogEnable(false); graphics.SetFogEnable(false);
SetWorldTransformation(false); SetWorldTransformation(false);
graphics.SetVertexDeclaration(graphics.Shaders.FlatVertexDecl);
graphics.SetShader(ShaderName.display2d_normal); graphics.SetShader(ShaderName.display2d_normal);
graphics.SetTexture(0, General.Map.Data.WhiteTexture.Texture); 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 // Draw
quads[0].Render(graphics); quads[0].Render(graphics);
@ -1811,10 +1829,9 @@ namespace CodeImp.DoomBuilder.Rendering
graphics.SetTextureFactor(-1); graphics.SetTextureFactor(-1);
graphics.SetFogEnable(false); graphics.SetFogEnable(false);
SetWorldTransformation(false); SetWorldTransformation(false);
graphics.SetVertexDeclaration(graphics.Shaders.FlatVertexDecl);
graphics.SetShader(ShaderName.display2d_normal); graphics.SetShader(ShaderName.display2d_normal);
graphics.SetTexture(0, General.Map.Data.WhiteTexture.Texture); 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 // Draw
quad.Render(graphics); quad.Render(graphics);
@ -1844,10 +1861,9 @@ namespace CodeImp.DoomBuilder.Rendering
graphics.SetTextureFactor(-1); graphics.SetTextureFactor(-1);
graphics.SetFogEnable(false); graphics.SetFogEnable(false);
SetWorldTransformation(false); SetWorldTransformation(false);
graphics.SetVertexDeclaration(graphics.Shaders.FlatVertexDecl);
graphics.SetShader(ShaderName.display2d_normal); graphics.SetShader(ShaderName.display2d_normal);
graphics.SetTexture(0, texture.Texture); graphics.SetTexture(0, texture.Texture);
graphics.Shaders.SetDisplay2DSettings(1f, 1f, 0f, 1f, General.Settings.ClassicBilinear); SetDisplay2DSettings(1f, 1f, 0f, 1f, General.Settings.ClassicBilinear);
// Draw // Draw
quad.Render(graphics); quad.Render(graphics);
@ -1944,13 +1960,12 @@ namespace CodeImp.DoomBuilder.Rendering
graphics.SetTextureFactor(-1); graphics.SetTextureFactor(-1);
graphics.SetFogEnable(false); graphics.SetFogEnable(false);
SetWorldTransformation(false); SetWorldTransformation(false);
graphics.SetVertexDeclaration(graphics.Shaders.FlatVertexDecl);
graphics.SetShader(ShaderName.display2d_normal); graphics.SetShader(ShaderName.display2d_normal);
graphics.SetTexture(0, General.Map.Data.WhiteTexture.Texture); 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 // Draw
graphics.SetVertexBuffer(0, vb, 0, FlatVertex.Stride); graphics.SetVertexBuffer(vb);
graphics.Draw(PrimitiveType.LineList, 0, pointscount / 2); graphics.Draw(PrimitiveType.LineList, 0, pointscount / 2);
vb.Dispose(); vb.Dispose();
} }
@ -1997,10 +2012,9 @@ namespace CodeImp.DoomBuilder.Rendering
graphics.SetTextureFactor(-1); graphics.SetTextureFactor(-1);
graphics.SetFogEnable(false); graphics.SetFogEnable(false);
SetWorldTransformation(false); SetWorldTransformation(false);
graphics.SetVertexDeclaration(graphics.Shaders.FlatVertexDecl);
graphics.SetShader(ShaderName.display2d_normal); graphics.SetShader(ShaderName.display2d_normal);
graphics.SetTexture(0, General.Map.Data.WhiteTexture.Texture); 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 // Draw
graphics.Draw(PrimitiveType.TriangleStrip, 0, 2, verts); graphics.Draw(PrimitiveType.TriangleStrip, 0, 2, verts);

View file

@ -382,7 +382,6 @@ namespace CodeImp.DoomBuilder.Rendering
graphics.SetAlphaBlendEnable(false); graphics.SetAlphaBlendEnable(false);
graphics.SetAlphaTestEnable(false); graphics.SetAlphaTestEnable(false);
graphics.SetTextureFactor(-1); graphics.SetTextureFactor(-1);
graphics.SetVertexDeclaration(graphics.Shaders.WorldVertexDecl);
//mxd. SKY PASS //mxd. SKY PASS
if(skygeo.Count > 0) if(skygeo.Count > 0)
@ -604,7 +603,7 @@ namespace CodeImp.DoomBuilder.Rendering
graphics.SetUniform(UniformName.vertexColor, thingcolor); graphics.SetUniform(UniformName.vertexColor, thingcolor);
//Render cage //Render cage
graphics.SetVertexBuffer(0, t.CageBuffer, 0, WorldVertex.Stride); graphics.SetVertexBuffer(t.CageBuffer);
graphics.Draw(PrimitiveType.LineList, 0, t.CageLength); graphics.Draw(PrimitiveType.LineList, 0, t.CageLength);
} }
@ -644,7 +643,7 @@ namespace CodeImp.DoomBuilder.Rendering
graphics.SetUniform(UniformName.vertexColor, color); graphics.SetUniform(UniformName.vertexColor, color);
//Commence drawing!!11 //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); graphics.Draw(PrimitiveType.LineList, 0, 8);
} }
@ -723,7 +722,7 @@ namespace CodeImp.DoomBuilder.Rendering
ApplyMatrices3D(); ApplyMatrices3D();
//render //render
graphics.SetVertexBuffer(0, vb, 0, WorldVertex.Stride); graphics.SetVertexBuffer(vb);
graphics.Draw(PrimitiveType.LineList, 0, pointscount / 2); graphics.Draw(PrimitiveType.LineList, 0, pointscount / 2);
// Done // Done
@ -781,7 +780,7 @@ namespace CodeImp.DoomBuilder.Rendering
sector = g.Sector; sector = g.Sector;
// Set stream source // Set stream source
graphics.SetVertexBuffer(0, sector.GeometryBuffer, 0, WorldVertex.Stride); graphics.SetVertexBuffer(sector.GeometryBuffer);
} }
else else
{ {
@ -935,7 +934,7 @@ namespace CodeImp.DoomBuilder.Rendering
ApplyMatrices3D(); ApplyMatrices3D();
// Apply buffer // Apply buffer
graphics.SetVertexBuffer(0, t.GeometryBuffer, 0, WorldVertex.Stride); graphics.SetVertexBuffer(t.GeometryBuffer);
// Render! // Render!
graphics.Draw(PrimitiveType.TriangleList, 0, t.Triangles); graphics.Draw(PrimitiveType.TriangleList, 0, t.Triangles);
@ -1055,7 +1054,7 @@ namespace CodeImp.DoomBuilder.Rendering
sector = g.Sector; sector = g.Sector;
// Set stream source // Set stream source
graphics.SetVertexBuffer(0, sector.GeometryBuffer, 0, WorldVertex.Stride); graphics.SetVertexBuffer(sector.GeometryBuffer);
} }
else else
{ {
@ -1243,7 +1242,7 @@ namespace CodeImp.DoomBuilder.Rendering
ApplyMatrices3D(); ApplyMatrices3D();
// Apply buffer // Apply buffer
graphics.SetVertexBuffer(0, t.GeometryBuffer, 0, WorldVertex.Stride); graphics.SetVertexBuffer(t.GeometryBuffer);
// Render! // Render!
graphics.Draw(PrimitiveType.TriangleList, 0, t.Triangles); graphics.Draw(PrimitiveType.TriangleList, 0, t.Triangles);
@ -1316,7 +1315,7 @@ namespace CodeImp.DoomBuilder.Rendering
sector = g.Sector; sector = g.Sector;
// Set stream source // Set stream source
graphics.SetVertexBuffer(0, sector.GeometryBuffer, 0, WorldVertex.Stride); graphics.SetVertexBuffer(sector.GeometryBuffer);
} }
else else
{ {
@ -1773,7 +1772,7 @@ namespace CodeImp.DoomBuilder.Rendering
sector = g.Sector; sector = g.Sector;
// Set stream source // Set stream source
graphics.SetVertexBuffer(0, sector.GeometryBuffer, 0, WorldVertex.Stride); graphics.SetVertexBuffer(sector.GeometryBuffer);
} }
else else
{ {
@ -2016,7 +2015,6 @@ namespace CodeImp.DoomBuilder.Rendering
graphics.SetTransform(TransformState.Projection, Matrix.Identity); graphics.SetTransform(TransformState.Projection, Matrix.Identity);
ApplyMatrices2D(); ApplyMatrices2D();
graphics.SetVertexDeclaration(graphics.Shaders.FlatVertexDecl);
graphics.SetShader(ShaderName.display2d_normal); graphics.SetShader(ShaderName.display2d_normal);
// Texture // Texture
@ -2032,10 +2030,20 @@ namespace CodeImp.DoomBuilder.Rendering
} }
// Draw // 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); graphics.Draw(PrimitiveType.TriangleStrip, 0, 2, crosshairverts);
} }
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 // This switches fog on and off
public void SetFogMode(bool usefog) public void SetFogMode(bool usefog)
{ {

View file

@ -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);
}
}
}

View file

@ -140,7 +140,7 @@ namespace CodeImp.DoomBuilder.Rendering
{ {
// Make the new buffer! // Make the new buffer!
VertexBuffer b = new VertexBuffer(); 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 // Start refilling the buffer with sector geometry
foreach (SurfaceEntry e in set.Value.entries) foreach (SurfaceEntry e in set.Value.entries)
@ -273,7 +273,7 @@ namespace CodeImp.DoomBuilder.Rendering
{ {
// Make the new buffer! // Make the new buffer!
vb = new VertexBuffer(); vb = new VertexBuffer();
General.Map.Graphics.SetBufferData(vb, FlatVertex.Stride * buffernumvertices); General.Map.Graphics.SetBufferData(vb, buffernumvertices, VertexFormat.Flat);
// Add it. // Add it.
set.buffers.Add(vb); set.buffers.Add(vb);
@ -316,7 +316,7 @@ namespace CodeImp.DoomBuilder.Rendering
{ {
// Make the new buffer and lock it // Make the new buffer and lock it
vb = new VertexBuffer(); 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 // Start refilling the buffer with sector geometry
@ -611,7 +611,6 @@ namespace CodeImp.DoomBuilder.Rendering
if(!resourcesunloaded) if(!resourcesunloaded)
{ {
ShaderName pass = Renderer.FullBrightness ? ShaderName.display2d_fullbright : ShaderName.display2d_normal; //mxd ShaderName pass = Renderer.FullBrightness ? ShaderName.display2d_fullbright : ShaderName.display2d_normal; //mxd
graphics.SetVertexDeclaration(graphics.Shaders.FlatVertexDecl);
foreach(KeyValuePair<ImageData, List<SurfaceEntry>> imgsurfaces in surfaces) foreach(KeyValuePair<ImageData, List<SurfaceEntry>> imgsurfaces in surfaces)
{ {
graphics.SetShader(pass); graphics.SetShader(pass);
@ -628,7 +627,7 @@ namespace CodeImp.DoomBuilder.Rendering
if(set.buffers[entry.bufferindex] != lastbuffer) if(set.buffers[entry.bufferindex] != lastbuffer)
{ {
lastbuffer = set.buffers[entry.bufferindex]; lastbuffer = set.buffers[entry.bufferindex];
graphics.SetVertexBuffer(0, lastbuffer, 0, FlatVertex.Stride); graphics.SetVertexBuffer(lastbuffer);
} }
// Draw // Draw

View file

@ -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 }
}

View file

@ -138,7 +138,7 @@ namespace CodeImp.DoomBuilder.VisualModes
{ {
// Make a new buffer // Make a new buffer
geobuffer = new VertexBuffer(); geobuffer = new VertexBuffer();
graphics.SetBufferData(geobuffer, WorldVertex.Stride * numverts); graphics.SetBufferData(geobuffer, numverts, VertexFormat.World);
// Fill the buffer // Fill the buffer
foreach(VisualGeometry g in allgeometry) foreach(VisualGeometry g in allgeometry)

View file

@ -212,7 +212,6 @@
<ClCompile Include="ShaderManager.cpp" /> <ClCompile Include="ShaderManager.cpp" />
<ClCompile Include="Texture.cpp" /> <ClCompile Include="Texture.cpp" />
<ClCompile Include="VertexBuffer.cpp" /> <ClCompile Include="VertexBuffer.cpp" />
<ClCompile Include="VertexDeclaration.cpp" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClInclude Include="gl_load\gl_load.h" /> <ClInclude Include="gl_load\gl_load.h" />
@ -229,7 +228,6 @@
<ClInclude Include="ShaderWorld3D.h" /> <ClInclude Include="ShaderWorld3D.h" />
<ClInclude Include="Texture.h" /> <ClInclude Include="Texture.h" />
<ClInclude Include="VertexBuffer.h" /> <ClInclude Include="VertexBuffer.h" />
<ClInclude Include="VertexDeclaration.h" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Text Include="gl_load\gl_extlist.txt" /> <Text Include="gl_load\gl_extlist.txt" />

View file

@ -5,7 +5,6 @@
<ClCompile Include="RenderDevice.cpp" /> <ClCompile Include="RenderDevice.cpp" />
<ClCompile Include="Texture.cpp" /> <ClCompile Include="Texture.cpp" />
<ClCompile Include="VertexBuffer.cpp" /> <ClCompile Include="VertexBuffer.cpp" />
<ClCompile Include="VertexDeclaration.cpp" />
<ClCompile Include="gl_load\gl_load.c"> <ClCompile Include="gl_load\gl_load.c">
<Filter>gl_load</Filter> <Filter>gl_load</Filter>
</ClCompile> </ClCompile>
@ -20,7 +19,6 @@
<ClInclude Include="RenderDevice.h" /> <ClInclude Include="RenderDevice.h" />
<ClInclude Include="Texture.h" /> <ClInclude Include="Texture.h" />
<ClInclude Include="VertexBuffer.h" /> <ClInclude Include="VertexBuffer.h" />
<ClInclude Include="VertexDeclaration.h" />
<ClInclude Include="gl_load\gl_load.h"> <ClInclude Include="gl_load\gl_load.h">
<Filter>gl_load</Filter> <Filter>gl_load</Filter>
</ClInclude> </ClInclude>

View file

@ -3,7 +3,6 @@
#include "RenderDevice.h" #include "RenderDevice.h"
#include "VertexBuffer.h" #include "VertexBuffer.h"
#include "IndexBuffer.h" #include "IndexBuffer.h"
#include "VertexDeclaration.h"
#include "Texture.h" #include "Texture.h"
#include "ShaderManager.h" #include "ShaderManager.h"
#include <stdexcept> #include <stdexcept>
@ -15,8 +14,17 @@ RenderDevice::RenderDevice(HWND hwnd) : Context(hwnd)
if (Context) if (Context)
{ {
Context.Begin(); Context.Begin();
glGenVertexArrays(1, &mStreamVAO);
glGenBuffers(1, &mStreamVertexBuffer); glGenBuffers(1, &mStreamVertexBuffer);
glBindVertexArray(mStreamVAO);
glBindBuffer(GL_ARRAY_BUFFER, mStreamVertexBuffer);
VertexBuffer::SetupFlatVAO();
glBindBuffer(GL_ARRAY_BUFFER, 0);
mShaderManager = std::make_unique<ShaderManager>(); mShaderManager = std::make_unique<ShaderManager>();
CheckError();
Context.End(); Context.End();
} }
} }
@ -27,14 +35,15 @@ RenderDevice::~RenderDevice()
{ {
Context.Begin(); Context.Begin();
glDeleteBuffers(1, &mStreamVertexBuffer); glDeleteBuffers(1, &mStreamVertexBuffer);
glDeleteVertexArrays(1, &mStreamVAO);
mShaderManager->ReleaseResources(); mShaderManager->ReleaseResources();
Context.End(); 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; mNeedApply = true;
} }
@ -173,7 +182,7 @@ void RenderDevice::DrawIndexed(PrimitiveType type, int startIndex, int primitive
Context.End(); 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 modes[] = { GL_LINES, GL_TRIANGLES, GL_TRIANGLE_STRIP };
static const int toVertexCount[] = { 2, 3, 1 }; 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]; int vertcount = toVertexStart[(int)type] + primitiveCount * toVertexCount[(int)type];
Context.Begin(); Context.Begin();
mStreamBufferStride = stride; if (mNeedApply) ApplyChanges();
glBindBuffer(GL_ARRAY_BUFFER, mStreamVertexBuffer);
glBufferData(GL_ARRAY_BUFFER, vertcount * (size_t)stride, static_cast<const uint8_t*>(data) + startIndex * (size_t)stride, GL_STREAM_DRAW);
ApplyChanges();
glDrawArrays(modes[(int)type], 0, vertcount);
mStreamBufferStride = 0;
Context.End();
}
void RenderDevice::SetVertexDeclaration(VertexDeclaration* decl) glBindBuffer(GL_ARRAY_BUFFER, mStreamVertexBuffer);
{ glBufferData(GL_ARRAY_BUFFER, vertcount * (size_t)VertexBuffer::FlatStride, static_cast<const uint8_t*>(data) + startIndex * (size_t)VertexBuffer::FlatStride, GL_STREAM_DRAW);
mVertexDeclaration = decl; glBindVertexArray(mStreamVAO);
mNeedApply = true; glDrawArrays(modes[(int)type], 0, vertcount);
ApplyVertexBuffer();
Context.End();
} }
void RenderDevice::StartRendering(bool clear, int backcolor, Texture* target, bool usedepthbuffer) 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); 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(); Context.Begin();
buffer->Format = format;
GLint oldbinding = 0; GLint oldbinding = 0;
glGetIntegerv(GL_ARRAY_BUFFER_BINDING, &oldbinding); glGetIntegerv(GL_ARRAY_BUFFER_BINDING, &oldbinding);
glBindBuffer(GL_ARRAY_BUFFER, buffer->GetBuffer()); glBindBuffer(GL_ARRAY_BUFFER, buffer->GetBuffer());
@ -359,7 +364,7 @@ Shader* RenderDevice::GetActiveShader()
void RenderDevice::ApplyChanges() void RenderDevice::ApplyChanges()
{ {
ApplyShader(); ApplyShader();
ApplyVertexBuffers(); ApplyVertexBuffer();
ApplyIndexBuffer(); ApplyIndexBuffer();
ApplyUniforms(); ApplyUniforms();
ApplyTextures(); ApplyTextures();
@ -435,67 +440,10 @@ void RenderDevice::ApplyIndexBuffer()
} }
} }
void RenderDevice::ApplyVertexBuffers() void RenderDevice::ApplyVertexBuffer()
{ {
static const int typeSize[] = { 2, 3, GL_BGRA }; if (mVertexBuffer)
static const int type[] = { GL_FLOAT, GL_FLOAT, GL_UNSIGNED_BYTE }; glBindVertexArray(mVertexBuffer->GetVAO());
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;
}
}
} }
void RenderDevice::SetShader(ShaderName name) void RenderDevice::SetShader(ShaderName name)
@ -617,9 +565,9 @@ void RenderDevice_SetUniform(RenderDevice* device, UniformName name, const void*
device->SetUniform(name, values, count); 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) 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); 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); device->DrawData(type, startIndex, primitiveCount, data);
}
void RenderDevice_SetVertexDeclaration(RenderDevice* device, VertexDeclaration* decl)
{
device->SetVertexDeclaration(decl);
} }
void RenderDevice_StartRendering(RenderDevice* device, bool clear, int backcolor, Texture* target, bool usedepthbuffer) 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); 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) void RenderDevice_SetVertexBufferSubdata(RenderDevice* device, VertexBuffer* buffer, int64_t destOffset, void* data, int64_t size)

View file

@ -4,11 +4,11 @@
class VertexBuffer; class VertexBuffer;
class IndexBuffer; class IndexBuffer;
class VertexDeclaration;
class Texture; class Texture;
class ShaderManager; class ShaderManager;
class Shader; class Shader;
enum class CubeMapFace; enum class CubeMapFace;
enum class VertexFormat;
enum class Cull : int { None, Clockwise }; enum class Cull : int { None, Clockwise };
enum class Blend : int { InverseSourceAlpha, SourceAlpha, One, BlendFactor }; enum class Blend : int { InverseSourceAlpha, SourceAlpha, One, BlendFactor };
@ -79,7 +79,7 @@ public:
void SetShader(ShaderName name); void SetShader(ShaderName name);
void SetUniform(UniformName name, const void* values, int count); 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 SetIndexBuffer(IndexBuffer* buffer);
void SetAlphaBlendEnable(bool value); void SetAlphaBlendEnable(bool value);
void SetAlphaTestEnable(bool value); void SetAlphaTestEnable(bool value);
@ -96,15 +96,14 @@ public:
void SetSamplerState(int unit, TextureAddress addressU, TextureAddress addressV, TextureAddress addressW); void SetSamplerState(int unit, TextureAddress addressU, TextureAddress addressV, TextureAddress addressW);
void Draw(PrimitiveType type, int startIndex, int primitiveCount); void Draw(PrimitiveType type, int startIndex, int primitiveCount);
void DrawIndexed(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 DrawData(PrimitiveType type, int startIndex, int primitiveCount, const void* data);
void SetVertexDeclaration(VertexDeclaration* decl);
void StartRendering(bool clear, int backcolor, Texture* target, bool usedepthbuffer); void StartRendering(bool clear, int backcolor, Texture* target, bool usedepthbuffer);
void FinishRendering(); void FinishRendering();
void Present(); void Present();
void ClearTexture(int backcolor, Texture* texture); void ClearTexture(int backcolor, Texture* texture);
void CopyTexture(Texture* src, Texture* dst, CubeMapFace face); 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 SetVertexBufferSubdata(VertexBuffer* buffer, int64_t destOffset, void* data, int64_t size);
void SetIndexBufferData(IndexBuffer* buffer, void* data, int64_t size); void SetIndexBufferData(IndexBuffer* buffer, void* data, int64_t size);
@ -116,7 +115,7 @@ public:
void InvalidateTexture(Texture* texture); void InvalidateTexture(Texture* texture);
void ApplyChanges(); void ApplyChanges();
void ApplyVertexBuffers(); void ApplyVertexBuffer();
void ApplyIndexBuffer(); void ApplyIndexBuffer();
void ApplyShader(); void ApplyShader();
void ApplyUniforms(); void ApplyUniforms();
@ -133,16 +132,6 @@ public:
OpenGLContext Context; 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 struct TextureUnit
{ {
Texture* Tex = nullptr; Texture* Tex = nullptr;
@ -156,13 +145,9 @@ public:
enum { NumSlots = 16 }; enum { NumSlots = 16 };
VertexDeclaration *mVertexDeclaration = nullptr;
GLuint mVAO = 0;
int mEnabledVertexAttributes[NumSlots] = { 0 };
VertexBinding mVertexBindings[NumSlots];
TextureUnit mTextureUnits[NumSlots]; TextureUnit mTextureUnits[NumSlots];
VertexBuffer* mVertexBuffer = nullptr;
IndexBuffer* mIndexBuffer = nullptr; IndexBuffer* mIndexBuffer = nullptr;
std::unique_ptr<ShaderManager> mShaderManager; std::unique_ptr<ShaderManager> mShaderManager;
@ -177,7 +162,7 @@ public:
UniformEntry mUniforms[4 * 16 + 12 * 4]; UniformEntry mUniforms[4 * 16 + 12 * 4];
GLuint mStreamVertexBuffer = 0; GLuint mStreamVertexBuffer = 0;
int mStreamBufferStride = 0; GLuint mStreamVAO = 0;
Cull mCullMode = Cull::None; Cull mCullMode = Cull::None;
FillMode mFillMode = FillMode::Solid; FillMode mFillMode = FillMode::Solid;

View file

@ -1,7 +1,6 @@
#include "Precomp.h" #include "Precomp.h"
#include "Shader.h" #include "Shader.h"
#include "VertexDeclaration.h"
#include "RenderDevice.h" #include "RenderDevice.h"
#include <stdexcept> #include <stdexcept>

View file

@ -3,6 +3,8 @@
#include <string> #include <string>
#include "RenderDevice.h" #include "RenderDevice.h"
enum class DeclarationUsage : int32_t { Position, Color, TextureCoordinate, Normal };
class Shader class Shader
{ {
public: public:

View file

@ -1,6 +1,7 @@
#include "Precomp.h" #include "Precomp.h"
#include "VertexBuffer.h" #include "VertexBuffer.h"
#include "Shader.h"
VertexBuffer::VertexBuffer() VertexBuffer::VertexBuffer()
{ {
@ -18,6 +19,44 @@ GLuint VertexBuffer::GetBuffer()
return mBuffer; 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() VertexBuffer* VertexBuffer_New()

View file

@ -1,5 +1,7 @@
#pragma once #pragma once
enum class VertexFormat : int32_t { Flat, World };
class VertexBuffer class VertexBuffer
{ {
public: public:
@ -7,7 +9,17 @@ public:
~VertexBuffer(); ~VertexBuffer();
GLuint GetBuffer(); 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: private:
GLuint mBuffer = 0; GLuint mBuffer = 0;
GLuint mVAO = 0;
}; };

View file

@ -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;
}

View file

@ -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<VertexElement> Elements;
};

View file

@ -23,7 +23,6 @@ EXPORTS
RenderDevice_Draw RenderDevice_Draw
RenderDevice_DrawIndexed RenderDevice_DrawIndexed
RenderDevice_DrawData RenderDevice_DrawData
RenderDevice_SetVertexDeclaration
RenderDevice_StartRendering RenderDevice_StartRendering
RenderDevice_FinishRendering RenderDevice_FinishRendering
RenderDevice_Present RenderDevice_Present
@ -38,8 +37,6 @@ EXPORTS
RenderDevice_UnlockTexture RenderDevice_UnlockTexture
VertexBuffer_New VertexBuffer_New
VertexBuffer_Delete VertexBuffer_Delete
VertexDeclaration_New
VertexDeclaration_Delete
IndexBuffer_New IndexBuffer_New
IndexBuffer_Delete IndexBuffer_Delete
Texture_New Texture_New