Skip to content

Commit

Permalink
Add a renderer using very basic dx12
Browse files Browse the repository at this point in the history
  • Loading branch information
FaberSanZ committed Jul 6, 2024
1 parent 34cdb19 commit 16eec3e
Show file tree
Hide file tree
Showing 36 changed files with 4,199 additions and 22 deletions.
113 changes: 101 additions & 12 deletions Src/Games/FPSTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,115 @@
using System.Text;
using System.Threading.Tasks;
using Xultaik.Desktop;
using Xultaik.Graphics;

namespace Games
{
public class FPSTest
public class FPSTest :IDisposable
{
public Window Window { get; set; }

public RenderDescriptor Parameters { get; set; }

public GraphicsAdapter Adapter { get; set; }

public GraphicsDevice Device { get; set; }

public SwapChain SwapChain { get; set; }

public CommandList CommandList { get; set; }





public FPSTest()
{
Window window = new(new WindowSettings()
Window = new Window(WindowSettings.Default);



Parameters = new RenderDescriptor()
{
Border = WindowBorder.Hidden,
Position = Point.Empty,
Size = new Size(800, 600),
State = WindowState.Normal,
CursorMode = CursorMode.Normal,
Title = "Test",
UpdateFrequency = null,
Icon = WindowResourcePtr.LoadIcon("Content\\win.ico"),
BackBufferWidth = Window.ClientSize.Width,
BackBufferHeight = Window.ClientSize.Height,
DeviceHandle = Window.WindowHandler.Value,
Settings = new Settings()
{
Fullscreen = false,
VSync = false,
},
};
}

public void Initialize()
{

Adapter = new GraphicsAdapter();

Device = new GraphicsDevice(Adapter, Parameters);

SwapChain = new SwapChain(Device);

CommandList = new CommandList(Device);
}


public void Run()
{
Initialize();

BeginRun();

Window?.Show();

Tick();
}

public void Tick()
{

Window.RenderLoop(() =>
{
Update();
Draw();
});
window.Show();
window.RenderLoop(() => { });
}



public void BeginRun()
{
foreach (var Description in Device.NativeAdapter.Description)
Console.WriteLine(Description);

foreach (var VendorId in Device.NativeAdapter.VendorId)
Console.WriteLine(VendorId);

}

public void Update()
{

}

public void Draw()
{
CommandList.Reset();

CommandList.SetViewport(0, 0, 800, 600);
CommandList.SetScissor(0, 0, 800, 600);
CommandList.ClearTargetColor(SwapChain.BackBuffer, 0.0f, 0.2f, 0.4f, 1.0f);

CommandList.ExecuteCommandList();

SwapChain.Present();

}

public void Dispose()
{
//Device.Dispose();
}
}
}
3 changes: 2 additions & 1 deletion Src/Games/Program.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using Games;

new FPSTest();
using (var App = new FPSTest())
App.Run();
4 changes: 2 additions & 2 deletions Src/Xultaik.Desktop/WindowSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ public struct WindowSettings
{
public static WindowSettings Default => new WindowSettings
{
Title = "Default",
Size = new(600, 400),
Title = "Xultaik",
Size = new(800, 600),
Position = new(0, 0),
State = WindowState.Normal,
Border = WindowBorder.Resizable,
Expand Down
178 changes: 178 additions & 0 deletions Src/Xultaik.Graphics/Buffer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
// Copyright (c) Faber Leonardo. All Rights Reserved. https://github.com/FaberSanZ
// This code is licensed under the MIT license (MIT) (http://opensource.org/licenses/MIT)




using System;
using System.Collections.Generic;
using System.Text;
using Vortice.Direct3D12;
using Vortice.DXGI;

namespace Xultaik.Graphics
{
public unsafe class Buffer : GraphicsResource
{
public BufferDescription Description { get; private set; }

public int SizeInBytes => Description.SizeInBytes;

//public BufferFlags Flags => BufferFlags. Description.Flags;

public HeapType HeapType => Description.HeapType;

public int StructureByteStride => Description.StructureByteStride;


internal ID3D12Resource NativeResource;
internal CpuDescriptorHandle? constantBufferView;
internal long GPUVirtualAddress;



public Buffer(GraphicsDevice device, BufferDescription description) : base(device)
{
InitializeFrom(description);
}

public void SetData<T>(T[] data, int offsetInBytes = 0) where T : unmanaged
{

if (HeapType == HeapType.Upload)
{
IntPtr mappedResource = IntPtr.Zero;
NativeResource.Map(0, null, mappedResource.ToPointer());

Interop.MemoryHelper.Write(mappedResource, data);

NativeResource.Unmap(0);
}
else
{

//IntPtr mappedResource = NativeResource.Map(0);

//Interop.MemoryHelper.Write(mappedResource, data);

//NativeResource.Unmap(0);
//mappedResource = IntPtr.Zero;


//CommandList CommandList = new CommandList(GraphicsDevice);
//CommandList.Reset();
//var commandList = CommandList.nativeCommandList;

////commandList.Reset();
//// Copy from upload heap to actual resource
//commandList.CopyBufferRegion(NativeResource, 0, uploadResource, uploadOffset, SizeInBytes);

//// Switch resource to proper read state
//commandList.ResourceBarrierTransition(NativeResource, 0, ResourceStates.CopyDestination, NativeResourceState);

//commandList.Close();

//GraphicsDevice.WaitCopyQueue();
}
}


public void InitializeFrom(BufferDescription description)
{
Description = description;

ResourceStates resourceStates = ResourceStates.Common;



if (description.HeapType == HeapType.Upload)
resourceStates |= ResourceStates.GenericRead;


else if (description.HeapType == HeapType.Readback)
resourceStates |= ResourceStates.CopyDest;


if ((description.Flags & BufferFlags.ConstantBuffer) != 0)
constantBufferView = CreateConstantBufferView();






ResourceDescription ResourceDesc = new ResourceDescription()
{
Width = (ulong)SizeInBytes,
Height = 1,
DepthOrArraySize = 1,
Dimension = ResourceDimension.Buffer,
Alignment = 65536,
Layout = TextureLayout.RowMajor,
Flags = ResourceFlags.None,
MipLevels = 1,
Format = Format.Unknown,
SampleDescription = new SampleDescription()
{
Count = 1,
Quality = 0
}
};




HeapProperties heapProp = new HeapProperties()
{
Type = (Vortice.Direct3D12.HeapType)description.HeapType,
CreationNodeMask = 1,
VisibleNodeMask = 1,
CPUPageProperty = CpuPageProperty.Unknown,
MemoryPoolPreference = MemoryPool.Unknown,

};

NativeResource = GraphicsDevice.NativeDevice.CreateCommittedResource<ID3D12Resource>(heapProp, HeapFlags.None, ResourceDesc, resourceStates);







GPUVirtualAddress = (long)NativeResource.GPUVirtualAddress;


//return InitializeFrom(resource, description);
}


internal Buffer InitializeFrom(ID3D12Resource resource, BufferDescription description, int firstElement = 0, int elementCount = 0)
{


return this;
}


internal CpuDescriptorHandle CreateConstantBufferView()
{
CpuDescriptorHandle cpuHandle = GraphicsDevice.ShaderResourceViewAllocator.Allocate(1);

int constantBufferSize = (SizeInBytes + 255) & ~255;

ConstantBufferViewDescription cbvDescription = new ConstantBufferViewDescription()
{
BufferLocation = NativeResource!.GPUVirtualAddress,
SizeInBytes = constantBufferSize
};

GraphicsDevice.NativeDevice.CreateConstantBufferView(cbvDescription, cpuHandle);

return cpuHandle;
}

}


}
32 changes: 32 additions & 0 deletions Src/Xultaik.Graphics/BufferDescription.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Copyright (c) Faber Leonardo. All Rights Reserved. https://github.com/FaberSanZ
// This code is licensed under the MIT license (MIT) (http://opensource.org/licenses/MIT)


using System;
using System.Collections.Generic;
using System.Text;

namespace Xultaik.Graphics
{
public struct BufferDescription
{
public int SizeInBytes;

public int StructureByteStride;

public HeapType HeapType;

public BufferFlags Flags;



public BufferDescription(int sizeInBytes, BufferFlags bufferFlags, HeapType heapType, int structureByteStride = 0)
{
SizeInBytes = sizeInBytes;
Flags = bufferFlags;
HeapType = heapType;
StructureByteStride = structureByteStride;
}

}
}
Loading

0 comments on commit 16eec3e

Please sign in to comment.