Skip to content

Commit

Permalink
WIP on native wrapper and generator.
Browse files Browse the repository at this point in the history
  • Loading branch information
tomspilman committed May 25, 2024
1 parent 4305687 commit 37fb5ff
Show file tree
Hide file tree
Showing 17 changed files with 957 additions and 2,064 deletions.
47 changes: 42 additions & 5 deletions MonoGame.Framework.Native.sln
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,68 @@ Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.0.31903.59
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MonoGame.Framework.Native", "MonoGame.Framework\MonoGame.Framework.Native.csproj", "{56BA741D-6AF1-489B-AB00-338DE11B1D32}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MonoGame.Framework.Native", "MonoGame.Framework\MonoGame.Framework.Native.csproj", "{56BA741D-6AF1-489B-AB00-338DE11B1D32}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Tools", "Tools", "{65B3DC17-24BA-4C39-810F-E371AC48199A}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MonoGame.Generator.CTypes", "Tools\MonoGame.Generator.CTypes\MonoGame.Generator.CTypes.csproj", "{74F12E34-D96B-4EC1-A218-BAFC83DC6220}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MonoGame.Generator.CTypes", "Tools\MonoGame.Generator.CTypes\MonoGame.Generator.CTypes.csproj", "{74F12E34-D96B-4EC1-A218-BAFC83DC6220}"
EndProject
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "null_device", "src\monogame\null_device\null_device.vcxproj", "{3482DDFE-A5BE-4261-868F-A269F2B20DAC}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Debug|x64 = Debug|x64
Debug|x86 = Debug|x86
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Release|x64 = Release|x64
Release|x86 = Release|x86
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{56BA741D-6AF1-489B-AB00-338DE11B1D32}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{56BA741D-6AF1-489B-AB00-338DE11B1D32}.Debug|Any CPU.Build.0 = Debug|Any CPU
{56BA741D-6AF1-489B-AB00-338DE11B1D32}.Debug|x64.ActiveCfg = Debug|Any CPU
{56BA741D-6AF1-489B-AB00-338DE11B1D32}.Debug|x64.Build.0 = Debug|Any CPU
{56BA741D-6AF1-489B-AB00-338DE11B1D32}.Debug|x86.ActiveCfg = Debug|Any CPU
{56BA741D-6AF1-489B-AB00-338DE11B1D32}.Debug|x86.Build.0 = Debug|Any CPU
{56BA741D-6AF1-489B-AB00-338DE11B1D32}.Release|Any CPU.ActiveCfg = Release|Any CPU
{56BA741D-6AF1-489B-AB00-338DE11B1D32}.Release|Any CPU.Build.0 = Release|Any CPU
{56BA741D-6AF1-489B-AB00-338DE11B1D32}.Release|x64.ActiveCfg = Release|Any CPU
{56BA741D-6AF1-489B-AB00-338DE11B1D32}.Release|x64.Build.0 = Release|Any CPU
{56BA741D-6AF1-489B-AB00-338DE11B1D32}.Release|x86.ActiveCfg = Release|Any CPU
{56BA741D-6AF1-489B-AB00-338DE11B1D32}.Release|x86.Build.0 = Release|Any CPU
{74F12E34-D96B-4EC1-A218-BAFC83DC6220}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{74F12E34-D96B-4EC1-A218-BAFC83DC6220}.Debug|Any CPU.Build.0 = Debug|Any CPU
{74F12E34-D96B-4EC1-A218-BAFC83DC6220}.Debug|x64.ActiveCfg = Debug|Any CPU
{74F12E34-D96B-4EC1-A218-BAFC83DC6220}.Debug|x64.Build.0 = Debug|Any CPU
{74F12E34-D96B-4EC1-A218-BAFC83DC6220}.Debug|x86.ActiveCfg = Debug|Any CPU
{74F12E34-D96B-4EC1-A218-BAFC83DC6220}.Debug|x86.Build.0 = Debug|Any CPU
{74F12E34-D96B-4EC1-A218-BAFC83DC6220}.Release|Any CPU.ActiveCfg = Release|Any CPU
{74F12E34-D96B-4EC1-A218-BAFC83DC6220}.Release|Any CPU.Build.0 = Release|Any CPU
{74F12E34-D96B-4EC1-A218-BAFC83DC6220}.Release|x64.ActiveCfg = Release|Any CPU
{74F12E34-D96B-4EC1-A218-BAFC83DC6220}.Release|x64.Build.0 = Release|Any CPU
{74F12E34-D96B-4EC1-A218-BAFC83DC6220}.Release|x86.ActiveCfg = Release|Any CPU
{74F12E34-D96B-4EC1-A218-BAFC83DC6220}.Release|x86.Build.0 = Release|Any CPU
{3482DDFE-A5BE-4261-868F-A269F2B20DAC}.Debug|Any CPU.ActiveCfg = Debug|x64
{3482DDFE-A5BE-4261-868F-A269F2B20DAC}.Debug|Any CPU.Build.0 = Debug|x64
{3482DDFE-A5BE-4261-868F-A269F2B20DAC}.Debug|x64.ActiveCfg = Debug|x64
{3482DDFE-A5BE-4261-868F-A269F2B20DAC}.Debug|x64.Build.0 = Debug|x64
{3482DDFE-A5BE-4261-868F-A269F2B20DAC}.Debug|x86.ActiveCfg = Debug|Win32
{3482DDFE-A5BE-4261-868F-A269F2B20DAC}.Debug|x86.Build.0 = Debug|Win32
{3482DDFE-A5BE-4261-868F-A269F2B20DAC}.Release|Any CPU.ActiveCfg = Release|x64
{3482DDFE-A5BE-4261-868F-A269F2B20DAC}.Release|Any CPU.Build.0 = Release|x64
{3482DDFE-A5BE-4261-868F-A269F2B20DAC}.Release|x64.ActiveCfg = Release|x64
{3482DDFE-A5BE-4261-868F-A269F2B20DAC}.Release|x64.Build.0 = Release|x64
{3482DDFE-A5BE-4261-868F-A269F2B20DAC}.Release|x86.ActiveCfg = Release|Win32
{3482DDFE-A5BE-4261-868F-A269F2B20DAC}.Release|x86.Build.0 = Release|Win32
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{74F12E34-D96B-4EC1-A218-BAFC83DC6220} = {65B3DC17-24BA-4C39-810F-E371AC48199A}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {CDA9FB22-5A50-47C6-B732-CE09AC673DCA}
EndGlobalSection
EndGlobal
36 changes: 33 additions & 3 deletions MonoGame.Framework/Platform/Native/ConstantBuffer.Native.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,52 @@
// This file is subject to the terms and conditions defined in
// file 'LICENSE.txt', which is part of this source code package.

using MonoGame.Interop;
using System;
using System.Collections.Generic;
using System.Reflection.Metadata;
using System.Runtime.InteropServices;

namespace Microsoft.Xna.Framework.Graphics;

internal partial class ConstantBuffer
{
private void PlatformInitialize()
{
internal unsafe MGG_Buffer* Handle;

private unsafe void PlatformInitialize()
{
Handle = MGG.Buffer_Create(GraphicsDevice.Handle, _buffer.Length);
}

private void PlatformClear()
{
// TODO: What is this for?
throw new NotImplementedException();
}

internal unsafe void PlatformApply(GraphicsDevice device, ShaderStage stage, int slot)
{
if (Handle == null)
PlatformInitialize();

if (_dirty)
{
fixed (byte* data = &_buffer[0])
MGG.Buffer_SetData(GraphicsDevice.Handle, ref Handle, 0, data, _buffer.Length, true);
_dirty = false;
}

MGG.GraphicsDevice_BindConstantBuffer(GraphicsDevice.Handle, stage, slot, Handle);
}

internal void PlatformApply(GraphicsDevice device, ShaderStage stage, int slot)
protected override unsafe void Dispose(bool disposing)
{
if (disposing && Handle != null)
{
MGG.Buffer_Destroy(GraphicsDevice.Handle, Handle);
Handle = null;
}

base.Dispose(disposing);
}
}
97 changes: 93 additions & 4 deletions MonoGame.Framework/Platform/Native/GraphicsDevice.Native.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,96 @@
// This file is subject to the terms and conditions defined in
// file 'LICENSE.txt', which is part of this source code package.

using MonoGame.Interop;
using System;
using System.Collections.Generic;


namespace Microsoft.Xna.Framework.Graphics;


public partial class GraphicsDevice
{
private void PlatformSetup()
internal unsafe MGG_GraphicsDevice* Handle;

internal Texture2D DefaultTexture;

private int _currentFrame;

private unsafe List<PtrTo<MGG_Texture>> _disposeTexturesNextFrame = new List<PtrTo<MGG_Texture>>();
private unsafe List<PtrTo<MGG_Texture>> _disposeTexturesThisFrame = new List<PtrTo<MGG_Texture>>();

private unsafe void PlatformSetup()
{
Handle = MGG.GraphicsDevice_Create();

// Get the device caps.
MGG_GraphicsDevice_Caps caps;
MGG.GraphicsDevice_GetCaps(Handle, out caps);

MaxTextureSlots = caps.MaxTextureSlots;
MaxVertexTextureSlots = caps.MaxVertexTextureSlots;
_maxVertexBufferSlots = caps.MaxVertexBufferSlots;
}

private void PlatformInitialize()
private unsafe void PlatformInitialize()
{
MGG.GraphicsDevice_ResetBackbuffer(Handle,
PresentationParameters.BackBufferWidth,
PresentationParameters.BackBufferHeight,
PresentationParameters.BackBufferFormat,
PresentationParameters.DepthStencilFormat);

_currentFrame = -1;

// Setup the default texture.
DefaultTexture = new Texture2D(this, 2, 2);
DefaultTexture.SetData(new[] { Color.Black, Color.Black, Color.Black, Color.Black });
}

private void OnPresentationChanged()
private unsafe void OnPresentationChanged()
{
// Finish any frame that is currently rendering.
if (_currentFrame > -1)
MGG.GraphicsDevice_Present(Handle, _currentFrame);

// Now resize the back buffer.
MGG.GraphicsDevice_ResetBackbuffer(
Handle,
PresentationParameters.BackBufferWidth,
PresentationParameters.BackBufferHeight,
PresentationParameters.BackBufferFormat,
PresentationParameters.DepthStencilFormat);

// Begin a new frame it if was previously rendering.
if (_currentFrame > -1)
{
_currentFrame = -1;
BeginFrame();
}
}

private unsafe void BeginFrame()
{
if (_currentFrame > -1)
return;

// Start the command buffer now.
_currentFrame = MGG.GraphicsDevice_BeginFrame(Handle);

// We must reapply all the state on a new command buffer.
_scissorRectangleDirty = true;
_blendFactorDirty = true;
_blendStateDirty = true;
_pixelShaderDirty = true;
_vertexShaderDirty = true;
_depthStencilStateDirty = true;
_indexBufferDirty = true;
_rasterizerStateDirty = true;
_vertexBuffersDirty = true;
//_lastNvnBuffer = IntPtr.Zero;
Textures.Dirty();
SamplerStates.Dirty();
}

private void PlatformClear(ClearOptions options, Vector4 color, float depth, int stencil)
Expand All @@ -30,8 +103,24 @@ private void PlatformDispose()
{
}

private void PlatformPresent()
private unsafe void PlatformPresent()
{
if (_currentFrame < 0)
return;

MGG.GraphicsDevice_Present(Handle, _currentFrame);
_currentFrame = -1;

for (int i = 0; i < _disposeTexturesThisFrame.Count; i++)
{
var texture = _disposeTexturesThisFrame[i].Ptr;
MGG.Texture_Destroy(Handle, texture);
}
_disposeTexturesThisFrame.Clear();

var temp = _disposeTexturesThisFrame;
_disposeTexturesThisFrame = _disposeTexturesNextFrame;
_disposeTexturesNextFrame = temp;
}

private void PlatformSetViewport(ref Viewport value)
Expand Down
83 changes: 80 additions & 3 deletions MonoGame.Framework/Platform/Native/Interop.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,92 @@

using Microsoft.Xna.Framework.Graphics;
using System.Runtime.CompilerServices;
using System;
using System.Runtime.InteropServices;
using System.Runtime.InteropServices.Marshalling;

namespace MonoGame.Interop;

internal readonly struct GamePtr { }
internal readonly struct Game { }

internal readonly struct GameWindowPtr { }
internal readonly struct GameWindow { }

internal static unsafe partial class GameWrapper
{
[LibraryImport("monogame", StringMarshalling = StringMarshalling.Utf8)]
public static partial void MG_GW_SetAllowUserResizing(GamePtr* game, GameWindowPtr* gameWindow, [MarshalAs(UnmanagedType.U1)] bool allowuserresizing);
public static partial void MG_GW_SetAllowUserResizing(Game* game, GameWindow* gameWindow, [MarshalAs(UnmanagedType.U1)] bool allowuserresizing);
}

internal struct PtrTo<T>
{
public unsafe T* Ptr;
}

internal readonly struct MGG_GraphicsDevice { }
internal readonly struct MGG_Buffer { }
internal readonly struct MGG_Texture { }

internal struct MGG_GraphicsDevice_Caps
{
public int MaxTextureSlots;
public int MaxVertexTextureSlots;
public int MaxVertexBufferSlots;
}

internal static unsafe partial class MGG
{
#region GraphicsDevice

[LibraryImport("monogame", EntryPoint = "MGG_GraphicsDevice_Create", StringMarshalling = StringMarshalling.Utf8)]
public static partial MGG_GraphicsDevice* GraphicsDevice_Create();

[LibraryImport("monogame", EntryPoint = "MGG_GraphicsDevice_GetCaps", StringMarshalling = StringMarshalling.Utf8)]
public static partial void GraphicsDevice_GetCaps(MGG_GraphicsDevice* device, out MGG_GraphicsDevice_Caps caps);

[LibraryImport("monogame", EntryPoint = "MGG_GraphicsDevice_ResetBackbuffer", StringMarshalling = StringMarshalling.Utf8)]
public static partial void GraphicsDevice_ResetBackbuffer(MGG_GraphicsDevice* device, int width, int height, SurfaceFormat color, DepthFormat depth);

[LibraryImport("monogame", EntryPoint = "MGG_GraphicsDevice_BeginFrame", StringMarshalling = StringMarshalling.Utf8)]
public static partial int GraphicsDevice_BeginFrame(MGG_GraphicsDevice* device);

[LibraryImport("monogame", EntryPoint = "MGG_GraphicsDevice_Present", StringMarshalling = StringMarshalling.Utf8)]
public static partial void GraphicsDevice_Present(MGG_GraphicsDevice* device, int currentFrame);

[LibraryImport("monogame", EntryPoint = "MGG_GraphicsDevice_BindConstantBuffer", StringMarshalling = StringMarshalling.Utf8)]
public static partial void GraphicsDevice_BindConstantBuffer(MGG_GraphicsDevice* device, ShaderStage stage, int slot, MGG_Buffer* buffer);

#endregion

#region Buffer

[LibraryImport("monogame", EntryPoint = "MGG_Buffer_Create", StringMarshalling = StringMarshalling.Utf8)]
public static partial MGG_Buffer* Buffer_Create(MGG_GraphicsDevice* device, int length);

[LibraryImport("monogame", EntryPoint = "MGG_Buffer_SetData", StringMarshalling = StringMarshalling.Utf8)]
public static partial void Buffer_SetData(
MGG_GraphicsDevice* game,
ref MGG_Buffer* buffer,
int offset,
byte* data,
int length,
[MarshalAs(UnmanagedType.U1)]
bool discard);

[LibraryImport("monogame", EntryPoint = "MGG_Buffer_Destroy", StringMarshalling = StringMarshalling.Utf8)]
public static partial void Buffer_Destroy(MGG_GraphicsDevice* device, MGG_Buffer* buffer);

#endregion

#region Texture

[LibraryImport("monogame", EntryPoint = "MGG_Texture_Create", StringMarshalling = StringMarshalling.Utf8)]
public static partial MGG_Texture* Texture_Create(MGG_GraphicsDevice* device, SurfaceFormat format, int width, int height, int levels);


[LibraryImport("monogame", EntryPoint = "MGG_Texture_Destroy", StringMarshalling = StringMarshalling.Utf8)]
public static partial void Texture_Destroy(MGG_GraphicsDevice* device, MGG_Texture* texture);

#endregion
}


Loading

0 comments on commit 37fb5ff

Please sign in to comment.