- 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);
if (Handle == IntPtr.Zero)
throw new Exception("VertexDeclaration_New failed");
throw new Exception("RawMouse_New failed");
}
~RawMouse()

View file

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

View file

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

View file

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

View file

@ -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<FlatVertex>());
}
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<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)
{
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)
{
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)
@ -379,11 +374,16 @@ namespace CodeImp.DoomBuilder.Rendering
// Texture addressing
SetSamplerState(0, TextureAddress.Wrap);
// Shader settings
Shaders.SetWorld3DConstants(General.Settings.VisualBilinear, General.Settings.FilterAnisotropy);
//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();
// 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<int> AA_STEPS = new List<int> { 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 }

View file

@ -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);
@ -492,10 +491,36 @@ namespace CodeImp.DoomBuilder.Rendering
}
}
/// <summary>
/// This unprojects display coordinates (screen space) to map coordinates
/// </summary>
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);
}
/// <summary>
/// This unprojects display coordinates (screen space) to map coordinates
/// </summary>
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);

View file

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

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!
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<ImageData, List<SurfaceEntry>> 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

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
geobuffer = new VertexBuffer();
graphics.SetBufferData(geobuffer, WorldVertex.Stride * numverts);
graphics.SetBufferData(geobuffer, numverts, VertexFormat.World);
// Fill the buffer
foreach(VisualGeometry g in allgeometry)

View file

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

View file

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

View file

@ -3,7 +3,6 @@
#include "RenderDevice.h"
#include "VertexBuffer.h"
#include "IndexBuffer.h"
#include "VertexDeclaration.h"
#include "Texture.h"
#include "ShaderManager.h"
#include <stdexcept>
@ -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<ShaderManager>();
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<const uint8_t*>(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<const uint8_t*>(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)

View file

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

View file

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

View file

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

View file

@ -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()

View file

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

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_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