Skip to content

Commit

Permalink
WIP on native wrapper, generator, and vulkan graphics.
Browse files Browse the repository at this point in the history
  • Loading branch information
tomspilman committed May 26, 2024
1 parent 4305687 commit 93dd444
Show file tree
Hide file tree
Showing 44 changed files with 12,154 additions and 2,094 deletions.
24 changes: 19 additions & 5 deletions MonoGame.Framework.Native.sln
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,21 @@ 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}") = "monogame_null", "src\monogame\null_device\null_device.vcxproj", "{3482DDFE-A5BE-4261-868F-A269F2B20DAC}"
EndProject
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "monogame_windows", "src\monogame\windows\monogame_windows.vcxproj", "{E9536A29-2151-4839-A580-1A03FE787999}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
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
Expand All @@ -26,8 +27,21 @@ Global
{74F12E34-D96B-4EC1-A218-BAFC83DC6220}.Debug|Any CPU.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
{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}.Release|Any CPU.ActiveCfg = Release|x64
{3482DDFE-A5BE-4261-868F-A269F2B20DAC}.Release|Any CPU.Build.0 = Release|x64
{E9536A29-2151-4839-A580-1A03FE787999}.Debug|Any CPU.ActiveCfg = Debug_VK|x64
{E9536A29-2151-4839-A580-1A03FE787999}.Release|Any CPU.ActiveCfg = Release_VK|x64
{E9536A29-2151-4839-A580-1A03FE787999}.Release|Any CPU.Build.0 = Release_VK|x64
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
6 changes: 1 addition & 5 deletions MonoGame.Framework/Graphics/Texture3D.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,7 @@ public void SetData<T> (int level,
{
ValidateParams(level, left, top, right, bottom, front, back, data, startIndex, elementCount);

var width = right - left;
var height = bottom - top;
var depth = back - front;

PlatformSetData(level, left, top, right, bottom, front, back, data, startIndex, elementCount, width, height, depth);
PlatformSetData(level, left, top, right, bottom, front, back, data, startIndex, elementCount);
}

/// <summary>
Expand Down
2 changes: 2 additions & 0 deletions MonoGame.Framework/MonoGame.Framework.Native.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
<PackageId>MonoGame.Framework.Native</PackageId>
<AppendTargetFrameworkToOutputPath>False</AppendTargetFrameworkToOutputPath>
<CopyContentFiles>True</CopyContentFiles>
<Configurations>Debug;Release</Configurations>
<Platforms>AnyCPU</Platforms>
</PropertyGroup>

<ItemGroup>
Expand Down
5 changes: 4 additions & 1 deletion MonoGame.Framework/Platform/Graphics/Texture3D.DirectX.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,11 @@ internal override Resource CreateTexture()

private void PlatformSetData<T>(int level,
int left, int top, int right, int bottom, int front, int back,
T[] data, int startIndex, int elementCount, int width, int height, int depth)
T[] data, int startIndex, int elementCount)
{
var width = right - left;
var height = bottom - top;

var elementSizeInByte = ReflectionHelpers.SizeOf<T>.Get();
var dataHandle = GCHandle.Alloc(data, GCHandleType.Pinned);
try
Expand Down
6 changes: 5 additions & 1 deletion MonoGame.Framework/Platform/Graphics/Texture3D.OpenGL.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,15 @@ private void PlatformConstruct(GraphicsDevice graphicsDevice, int width, int hei
private void PlatformSetData<T>(
int level,
int left, int top, int right, int bottom, int front, int back,
T[] data, int startIndex, int elementCount, int width, int height, int depth)
T[] data, int startIndex, int elementCount)
{
#if GLES
throw new NotSupportedException("OpenGL ES 2.0 doesn't support 3D textures.");
#else
var width = right - left;
var height = bottom - top;
var depth = back - front;

Threading.BlockOnUIThread(() =>
{
var elementSizeInByte = Marshal.SizeOf<T>();
Expand Down
5 changes: 1 addition & 4 deletions MonoGame.Framework/Platform/Graphics/Texture3D.Web.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,7 @@ private void PlatformSetData<T>(
int back,
T[] data,
int startIndex,
int elementCount,
int width,
int height,
int depth)
int elementCount)
{
throw new NotImplementedException();
}
Expand Down
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 (Handle != null)
{
MGG.Buffer_Destroy(GraphicsDevice.Handle, Handle);
Handle = null;
}

base.Dispose(disposing);
}
}
140 changes: 140 additions & 0 deletions MonoGame.Framework/Platform/Native/Graphics.Interop.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
// MonoGame - Copyright (C) The MonoGame Team
// This file is subject to the terms and conditions defined in
// file 'LICENSE.txt', which is part of this source code package.

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

namespace MonoGame.Interop;

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 enum TextureType
{
_2D,
_3D,
Cube,
}

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 sizeInBytes);

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

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

[LibraryImport("monogame", EntryPoint = "MGG_Buffer_GetData", StringMarshalling = StringMarshalling.Utf8)]
public static partial void Buffer_GetData(
MGG_GraphicsDevice* device,
MGG_Buffer* buffer,
int offset,
byte* data,
int dataCount,
int dataBytes,
int dataStride);

#endregion

#region Texture

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

[LibraryImport("monogame", EntryPoint = "MGG_RenderTarget_Create", StringMarshalling = StringMarshalling.Utf8)]
public static partial MGG_Texture* RenderTarget_Create(
MGG_GraphicsDevice* device,
TextureType type,
SurfaceFormat format,
int width,
int height,
int depth,
int mipmaps,
int slices,
DepthFormat depthFormat,
int multiSampleCount,
RenderTargetUsage usage);

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

[LibraryImport("monogame", EntryPoint = "MGG_Texture_SetData", StringMarshalling = StringMarshalling.Utf8)]
public static partial void Texture_SetData(
MGG_GraphicsDevice* device,
MGG_Texture* texture,
int level,
int slice,
int x,
int y,
int z,
int width,
int height,
int depth,
byte* data,
int dataBytes);

[LibraryImport("monogame", EntryPoint = "MGG_Texture_GetData", StringMarshalling = StringMarshalling.Utf8)]
public static partial void Texture_GetData(
MGG_GraphicsDevice* device,
MGG_Texture* texture,
int level,
int slice,
int x,
int y,
int z,
int width,
int height,
int depth,
byte* data,
int dataBytes);

#endregion
}
Loading

0 comments on commit 93dd444

Please sign in to comment.