Files
GhostEngine/Ghost.Graphics/DX12/DX12GraphicsDevice.cs
Misaki 4110c166cf Refactor Vector3Field and update project structure
Changed Vector3Field.cs to derive from ValueControl<Vector3> and use NumberBox controls for better UI handling.
Changed EditorControls.xaml to update resource paths for new controls.
Changed InternalControls.xaml to simplify the resource dictionary by removing unnecessary references.
Changed IComponentEditor.cs to reflect updates in the component editor's lifecycle methods.
Changed project files for Ghost.Editor and Ghost.Core to include new dependencies and project references.
Changed FileExtensions.cs and IInspectorService.cs to align with the new namespace structure.
Changed Result.cs to enhance error handling and success checking methods.
Changed TypeHandle.cs to improve type handling compatibility.
Changed AssemblyInfo.cs files to include new assembly visibility attributes for better encapsulation.
Added new graphics-related classes and interfaces in the Ghost.Engine project, including IGraphicsDevice and DX12GraphicsDevice.
Added a new Mesh class to handle 3D mesh data and provide methods for creating geometric shapes.
Added GraphicsPipeline.cs to manage the graphics rendering loop and device initialization.
Added ScenePage.xaml and ScenePage.xaml.cs to create a new page for rendering scenes.
Updated HierarchyPage.xaml.cs and InspectorPage.xaml.cs to use the new service locator pattern for service retrieval.
Updated LandingWindow.xaml.cs and EngineEditorWindow.xaml.cs to utilize the new service locator pattern for better service access.
Updated Logger.cs to enhance logging capabilities with optional stack traces and assertion logging.
Updated QueryFilter.cs and QueryEnumerable.cs to use the new TypeHandle structure for improved efficiency.
Updated WorldNode.cs and WorldNodeSerializer.cs to enhance serialization and management of world nodes.
Updated AssetDatabase and related classes to improve asset management and metadata generation.
Updated Ghost.UnitTest.csproj to include new project references and package dependencies for unit tests.
2025-06-27 20:02:02 +09:00

117 lines
3.0 KiB
C#

using Ghost.Graphics.Contracts;
using Ghost.Graphics.Data;
using Vortice.Direct3D;
using Vortice.Direct3D12;
using Vortice.DXGI;
namespace Ghost.Graphics.DX12;
internal class DX12GraphicsDevice : IGraphicsDevice
{
private readonly IDXGIFactory7 _dxgiFactory;
private readonly ID3D12Device14 _device;
private readonly ID3D12CommandQueue _commandQueue;
private readonly List<IRenderView> _renderViews = new();
#if DEBUG
private readonly IDebugLayer _debugLayer;
#endif
public ID3D12Device14 Device => _device;
public IDXGIFactory7 DXGIFactory => _dxgiFactory;
public ID3D12CommandQueue CommandQueue => _commandQueue;
public static IGraphicsDevice Create() => new DX12GraphicsDevice();
private DX12GraphicsDevice()
{
#if DEBUG
_debugLayer = new DX12DebugLayer();
#endif
InitializeDevice(out _dxgiFactory, out _device);
InitializeCommandQueue(out _commandQueue);
}
private void InitializeDevice(out IDXGIFactory7 factory, out ID3D12Device14 device)
{
#if DEBUG
factory = DXGI.CreateDXGIFactory2<IDXGIFactory7>(true);
#else
factory = DXGI.CreateDXGIFactory2<IDXGIFactory7>(false);
#endif
ID3D12Device14? d3d12Device = default;
for (uint adapterIndex = 0;
factory.EnumAdapters1(adapterIndex, out var adapter).Success;
adapterIndex++)
{
var desc = adapter.Description1;
// Don't select the Basic Render Driver adapter.
if ((desc.Flags & AdapterFlags.Software) != AdapterFlags.None)
{
adapter.Dispose();
continue;
}
if (D3D12.D3D12CreateDevice(adapter, FeatureLevel.Level_11_0, out d3d12Device).Success)
{
adapter.Dispose();
break;
}
}
if (d3d12Device == null)
{
throw new PlatformNotSupportedException("Cannot create ID3D12Device");
}
device = d3d12Device;
}
private void InitializeCommandQueue(out ID3D12CommandQueue queue)
{
var queueDesc = new CommandQueueDescription
{
Type = CommandListType.Direct,
Priority = (int)CommandQueuePriority.High,
Flags = CommandQueueFlags.None,
};
queue = _device.CreateCommandQueue(queueDesc);
}
public IRenderView CreateRenderView(in SwapChainSurface swapChainSurface)
{
var renderView = new DX12RenderView(this, swapChainSurface);
_renderViews.Add(renderView);
return renderView;
}
public void OnRender()
{
foreach (var renderView in _renderViews)
{
renderView.Render();
}
}
public void Dispose()
{
foreach (var renderView in _renderViews)
{
renderView.Dispose();
}
_renderViews.Clear();
_commandQueue?.Dispose();
_device?.Dispose();
_dxgiFactory?.Dispose();
#if DEBUG
_debugLayer.Dispose();
#endif
}
}