Skip to content

Commit

Permalink
Add GPU acceleration for most of ApiHawk's GuiApi (gui.* lua APIs), r…
Browse files Browse the repository at this point in the history
…efactor ApiHawk surfaces
  • Loading branch information
CasualPokePlayer committed May 24, 2024
1 parent e1fe18b commit 476ac94
Show file tree
Hide file tree
Showing 38 changed files with 1,283 additions and 607 deletions.
Binary file modified Assets/dll/SDL2.dll
Binary file not shown.
Binary file added Assets/dll/cimgui.dll
Binary file not shown.
Binary file added Assets/dll/cimgui.so
Binary file not shown.
Binary file modified Assets/dll/libSDL2.so
Binary file not shown.
1 change: 1 addition & 0 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
<PackageVersion Include="Cyotek.Drawing.BitmapFont" Version="2.0.4" />
<PackageVersion Include="DotNetAnalyzers.DocumentationAnalyzers" Version="1.0.0-beta.59" />
<PackageVersion Include="Google.FlatBuffers" Version="23.5.26" /> <!-- last version with .NET Standard 2.0 support -->
<PackageVersion Include="ImGui.NET" Version="1.90.6.1" />
<PackageVersion Include="JunitXml.TestLogger" Version="3.0.124" />
<PackageVersion Include="Magick.NET-Q8-AnyCPU" Version="13.6.0" />
<PackageVersion Include="Menees.Analyzers" Version="3.0.10" />
Expand Down
3 changes: 2 additions & 1 deletion ExternalProjects/SDL2/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ project(SDL2-BizHawk C CXX)
# set(SDL_ATOMIC ON) # Used in HIDAPI (and disabling it just makes it emulate atomics rather than use intrinsics)
set(SDL_AUDIO OFF)
set(SDL_VIDEO ON) # Used in mupen64plus + OpenGL/D3D9 display methods
set(SDL_RENDER OFF)
set(SDL_RENDER ON) # We use the software renderer for GDI+ImGui 2D rendering
set(SDL_EVENTS ON) # Implied by SDL_VIDEO and SDL_JOYSTICK
set(SDL_JOYSTICK ON) # Used for our SDL2 input adapter
set(SDL_HAPTIC OFF)
Expand Down Expand Up @@ -69,6 +69,7 @@ set(SDL_DIRECTFB OFF)
set(SDL_DUMMYVIDEO OFF)
set(SDL_RPI OFF)
set(SDL_RENDER_D3D OFF)
set(SDL_RENDER_METAL OFF)
set(SDL_VIVANTE OFF)
set(SDL_VULKAN OFF)
set(SDL_KMSDRM OFF)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ protected override CreateParams CreateParams
protected override void OnHandleCreated(EventArgs e)
{
base.OnHandleCreated(e);
Context = new(Handle, 3, 0, true, false);
Context = new(Handle, 3, 2, true, false);
}

protected override void OnHandleDestroyed(EventArgs e)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Cyotek.Drawing.BitmapFont" />
<PackageReference Include="ImGui.NET" ExcludeAssets="native" />
<PackageReference Include="System.Drawing.Common" />
<PackageReference Include="Silk.NET.OpenGL" />
<PackageReference Include="Silk.NET.WGL.Extensions.NV" />
Expand Down
69 changes: 68 additions & 1 deletion src/BizHawk.Bizware.Graphics/D3D11/D3D11Pipeline.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ internal class D3D11Pipeline : IPipeline
public ID3D11Buffer VertexBuffer;
public int VertexBufferCount;

public ID3D11Buffer IndexBuffer;
public int IndexBufferCount;

private readonly record struct D3D11Uniform(IntPtr VariablePointer, int VariableSize, D3D11PendingBuffer PB);

private readonly Dictionary<string, D3D11Uniform> _vsUniforms = new();
Expand Down Expand Up @@ -99,7 +102,7 @@ public D3D11Pipeline(D3D11Resources resources, PipelineCompileArgs compileArgs)
1 => Format.R32_Float,
2 => Format.R32G32_Float,
3 => Format.R32G32B32_Float,
4 => Format.R32G32B32A32_Float,
4 => item.Integer ? Format.B8G8R8A8_UNorm : Format.R32G32B32A32_Float,
_ => throw new InvalidOperationException()
};

Expand Down Expand Up @@ -253,6 +256,9 @@ public void DestroyPipeline()
VertexBuffer?.Dispose();
VertexBuffer = null;
VertexBufferCount = 0;
IndexBuffer?.Dispose();
IndexBuffer = null;
IndexBufferCount = 0;

for (var i = 0; i < ID3D11DeviceContext.CommonShaderConstantBufferSlotCount; i++)
{
Expand Down Expand Up @@ -283,6 +289,62 @@ public void DestroyPendingBuffers()
}
}

public void SetVertexData(IntPtr data, int count)
{
if (VertexBufferCount < count)
{
VertexBuffer?.Dispose();
var bd = new BufferDescription( count * VertexStride, BindFlags.VertexBuffer, ResourceUsage.Dynamic, CpuAccessFlags.Write);
VertexBuffer = Device.CreateBuffer(in bd, data);
VertexBufferCount = count;
Context.IASetVertexBuffer(0, VertexBuffer, VertexStride);
}
else
{
var mappedVb = Context.Map(VertexBuffer, MapMode.WriteDiscard);
try
{
unsafe
{
Buffer.MemoryCopy((void*)data, (void*)mappedVb.DataPointer,
VertexBufferCount * VertexStride, count * VertexStride);
}
}
finally
{
Context.Unmap(VertexBuffer);
}
}
}

public void SetIndexData(IntPtr data, int count)
{
if (IndexBufferCount < count)
{
IndexBuffer?.Dispose();
var bd = new BufferDescription(count * 2, BindFlags.IndexBuffer, ResourceUsage.Dynamic, CpuAccessFlags.Write);
IndexBuffer = Device.CreateBuffer(in bd, data);
IndexBufferCount = count;
Context.IASetIndexBuffer(IndexBuffer, Format.R16_UInt, 0);
}
else
{
var mappedIb = Context.Map(IndexBuffer, MapMode.WriteDiscard);
try
{
unsafe
{
Buffer.MemoryCopy((void*)data, (void*)mappedIb.DataPointer,
IndexBufferCount * 2, count * 2);
}
}
finally
{
Context.Unmap(IndexBuffer);
}
}
}

public bool HasUniformSampler(string name)
{
return _vsSamplers.ContainsKey(name)
Expand All @@ -299,6 +361,11 @@ public string GetUniformSamplerName(int index)

public void SetUniformSampler(string name, ITexture2D tex)
{
if (tex == null)
{
return;
}

var d3d11Tex = (D3D11Texture2D)tex;
var sampler = d3d11Tex.LinearFiltering ? LinearSamplerState : PointSamplerState;

Expand Down
4 changes: 1 addition & 3 deletions src/BizHawk.Bizware.Graphics/D3D11/D3D11Resources.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public void CreateResources()
bd.RenderTarget[0].DestinationBlend = Blend.InverseSourceAlpha;
bd.RenderTarget[0].BlendOperation = BlendOperation.Add;
bd.RenderTarget[0].SourceBlendAlpha = Blend.One;
bd.RenderTarget[0].DestinationBlendAlpha = Blend.Zero;
bd.RenderTarget[0].DestinationBlendAlpha = Blend.InverseSourceAlpha;
bd.RenderTarget[0].BlendOperationAlpha = BlendOperation.Add;
bd.RenderTarget[0].RenderTargetWriteMask = ColorWriteEnable.All;
BlendEnableState = Device.CreateBlendState(bd);
Expand All @@ -106,8 +106,6 @@ public void CreateResources()

RasterizerState = Device.CreateRasterizerState(rd);

Context.IASetPrimitiveTopology(PrimitiveTopology.TriangleStrip);

foreach (var tex2d in Textures)
{
tex2d.CreateTexture();
Expand Down
81 changes: 33 additions & 48 deletions src/BizHawk.Bizware.Graphics/D3D11/IGL_D3D11.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@
using System.Drawing;
using System.Numerics;

using BizHawk.Common;

using Vortice.Direct3D;
using Vortice.Direct3D11;
using Vortice.DXGI;

using BizHawk.Common;

namespace BizHawk.Bizware.Graphics
{
/// <summary>
Expand Down Expand Up @@ -216,6 +217,7 @@ public void BindPipeline(IPipeline pipeline)

Context.IASetInputLayout(d3d11Pipeline.VertexInputLayout);
Context.IASetVertexBuffer(0, d3d11Pipeline.VertexBuffer, d3d11Pipeline.VertexStride);
Context.IASetIndexBuffer(d3d11Pipeline.IndexBuffer, Format.R16_UInt, 0);

// not sure if this applies to the current pipeline or all pipelines
// just set it every time to be safe
Expand Down Expand Up @@ -262,63 +264,46 @@ public void BindDefaultRenderTarget()
Context.OMSetRenderTargets(_controlSwapChain.RTV);
}

public void Draw(IntPtr data, int count)
private unsafe void UpdateConstantBuffers()
{
var pipeline = CurPipeline;
var stride = pipeline.VertexStride;

if (pipeline.VertexBufferCount < count)
{
pipeline.VertexBuffer?.Dispose();
var bd = new BufferDescription(stride * count, BindFlags.VertexBuffer, ResourceUsage.Dynamic, CpuAccessFlags.Write);
pipeline.VertexBuffer = Device.CreateBuffer(in bd, data);
pipeline.VertexBufferCount = count;
}
else
for (var i = 0; i < ID3D11DeviceContext.CommonShaderConstantBufferSlotCount; i++)
{
var mappedVb = Context.Map(pipeline.VertexBuffer, MapMode.WriteDiscard);
try
var pb = CurPipeline.PendingBuffers[i];
if (pb == null)
{
unsafe
{
Buffer.MemoryCopy((void*)data, (void*)mappedVb.DataPointer, stride * pipeline.VertexBufferCount, stride * count);
}
break;
}
finally

if (pb.VSBufferDirty)
{
Context.Unmap(pipeline.VertexBuffer);
var vsCb = Context.Map(CurPipeline.VSConstantBuffers[i], MapMode.WriteDiscard);
Buffer.MemoryCopy((void*)pb.VSPendingBuffer, (void*)vsCb.DataPointer, pb.VSBufferSize, pb.VSBufferSize);
Context.Unmap(CurPipeline.VSConstantBuffers[i]);
pb.VSBufferDirty = false;
}
}

unsafe
{
for (var i = 0; i < ID3D11DeviceContext.CommonShaderConstantBufferSlotCount; i++)
if (pb.PSBufferDirty)
{
var pb = pipeline.PendingBuffers[i];
if (pb == null)
{
break;
}

if (pb.VSBufferDirty)
{
var vsCb = Context.Map(pipeline.VSConstantBuffers[i], MapMode.WriteDiscard);
Buffer.MemoryCopy((void*)pb.VSPendingBuffer, (void*)vsCb.DataPointer, pb.VSBufferSize, pb.VSBufferSize);
Context.Unmap(pipeline.VSConstantBuffers[i]);
pb.VSBufferDirty = false;
}

if (pb.PSBufferDirty)
{
var psCb = Context.Map(pipeline.PSConstantBuffers[i], MapMode.WriteDiscard);
Buffer.MemoryCopy((void*)pb.PSPendingBuffer, (void*)psCb.DataPointer, pb.PSBufferSize, pb.PSBufferSize);
Context.Unmap(pipeline.PSConstantBuffers[i]);
pb.PSBufferDirty = false;
}
var psCb = Context.Map(CurPipeline.PSConstantBuffers[i], MapMode.WriteDiscard);
Buffer.MemoryCopy((void*)pb.PSPendingBuffer, (void*)psCb.DataPointer, pb.PSBufferSize, pb.PSBufferSize);
Context.Unmap(CurPipeline.PSConstantBuffers[i]);
pb.PSBufferDirty = false;
}
}
}

public void Draw(int vertexCount)
{
UpdateConstantBuffers();
Context.IASetPrimitiveTopology(PrimitiveTopology.TriangleStrip);
Context.Draw(vertexCount, 0);
}

Context.Draw(count, 0);
public void DrawIndexed(int indexCount, int indexStart, int vertexStart)
{
UpdateConstantBuffers();
Context.IASetPrimitiveTopology(PrimitiveTopology.TriangleList);
Context.DrawIndexed(indexCount, indexStart, vertexStart);
}
}
}
4 changes: 4 additions & 0 deletions src/BizHawk.Bizware.Graphics/Extensions/DrawingExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Drawing;
using System.Numerics;

namespace BizHawk.Bizware.Graphics
{
Expand All @@ -9,5 +10,8 @@ public static void Deconstruct(this Size size, out int width, out int height)
width = size.Width;
height = size.Height;
}

public static Vector2 ToVector(this Point point)
=> new(point.X, point.Y);
}
}
5 changes: 5 additions & 0 deletions src/BizHawk.Bizware.Graphics/Extensions/IGLExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ public static IGuiRenderer CreateGuiRenderer(this IGL gl)
? new GDIPlusGuiRenderer(gdipImpl)
: new GuiRenderer(gl);

public static I2DRenderer Create2DRenderer(this IGL gl, ImGuiResourceCache resourceCache)
=> gl is IGL_GDIPlus gdipImpl
? new SDLImGui2DRenderer(gdipImpl, resourceCache)
: new ImGui2DRenderer(gl, resourceCache);

/// <summary>
/// Loads a texture from disk
/// </summary>
Expand Down
6 changes: 5 additions & 1 deletion src/BizHawk.Bizware.Graphics/GDIPlus/IGL_GDIPlus.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,11 @@ public void BindPipeline(IPipeline pipeline)
{
}

public void Draw(IntPtr data, int count)
public void Draw(int vertexCount)
{
}

public void DrawIndexed(int indexCount, int indexStart, int vertexStart)
{
}

Expand Down
Loading

0 comments on commit 476ac94

Please sign in to comment.