Updated DirectX

This commit is contained in:
Simon Lübeß
2020-11-25 20:50:36 +01:00
parent ff16f6a895
commit 684669321a
2 changed files with 166 additions and 165 deletions
@@ -1,164 +1,165 @@
using System; using System;
using System.Diagnostics; using System.Diagnostics;
using DirectX.Common; using DirectX.Common;
using DirectX.D3D11; using DirectX.D3D11;
using DirectX.D3D11.SDKLayers; using DirectX.D3D11.SDKLayers;
using DirectX.DXGI.DXGI1_2; using DirectX.DXGI.DXGI1_2;
using GlitchyEngine.Renderer; using GlitchyEngine.Renderer;
using GlitchyEngine.Math; using GlitchyEngine.Math;
using GlitchyEngine.Platform.DX11; using GlitchyEngine.Platform.DX11;
using internal GlitchyEngine.Renderer; using internal GlitchyEngine.Renderer;
//using internal GlitchyEngine.Platform.DX11.Renderer; //using internal GlitchyEngine.Platform.DX11.Renderer;
namespace GlitchyEngine.Renderer namespace GlitchyEngine.Renderer
//namespace GlitchyEngine.Platform.DX11.Renderer //namespace GlitchyEngine.Platform.DX11.Renderer
{ {
/// DirectX 11 specific implementation of the GraphicsContext /// DirectX 11 specific implementation of the GraphicsContext
extension GraphicsContext extension GraphicsContext
//public class Dx11Context : GraphicsContext //public class Dx11Context : GraphicsContext
{ {
//private Dx11SwapChain _swapChain; //private Dx11SwapChain _swapChain;
private SwapChain _swapChain; private SwapChain _swapChain;
internal Windows.HWnd nativeWindowHandle; internal Windows.HWnd nativeWindowHandle;
internal ID3D11Device* nativeDevice; internal ID3D11Device* nativeDevice;
internal ID3D11DeviceContext* nativeContext; internal ID3D11DeviceContext* nativeContext;
private ID3D11Debug* _debugDevice; private ID3D11Debug* _debugDevice;
public override SwapChain SwapChain => _swapChain; public override SwapChain SwapChain => _swapChain;
private const uint32 MaxRTVCount = DirectX.D3D11.D3D11_SIMULTANEOUS_RENDER_TARGET_COUNT; private const uint32 MaxRTVCount = DirectX.D3D11.D3D11_SIMULTANEOUS_RENDER_TARGET_COUNT;
//public static override uint32 MaxRenderTargetCount() => MaxRTVCount; //public static override uint32 MaxRenderTargetCount() => MaxRTVCount;
public this(Windows.HWnd windowHandle) public this(Windows.HWnd windowHandle)
{ {
nativeWindowHandle = windowHandle; nativeWindowHandle = windowHandle;
_swapChain = new SwapChain(this); _swapChain = new SwapChain(this);
//_swapChain = new Dx11SwapChain(this); //_swapChain = new Dx11SwapChain(this);
} }
public ~this() public ~this()
{ {
delete _swapChain; delete _swapChain;
nativeDevice?.Release(); nativeDevice?.Release();
nativeContext?.Release(); nativeContext?.Release();
_debugDevice.ReportLiveDeviceObjects(.Detail); _debugDevice.ReportLiveDeviceObjects(.Detail);
_debugDevice?.Release(); _debugDevice?.Release();
} }
public override void Init() public override void Init()
{ {
InitDevice(); InitDevice();
SwapChain.Init(); SwapChain.Init();
} }
/** /**
* Initializes the Device and ImmediateContext. * Initializes the Device and ImmediateContext.
*/ */
void InitDevice() void InitDevice()
{ {
nativeDevice?.Release(); nativeDevice?.Release();
nativeContext?.Release(); nativeContext?.Release();
Log.EngineLogger.Trace("Creating D3D11 Device and Context..."); Log.EngineLogger.Trace("Creating D3D11 Device and Context...");
DeviceCreationFlags deviceFlags = .None; DeviceCreationFlags deviceFlags = .None;
#if DEBUG #if DEBUG
deviceFlags |= .Debug; deviceFlags |= .Debug;
#endif #endif
FeatureLevel[] levels = scope .(.Level_11_0); FeatureLevel[] levels = scope .(.Level_11_0);
var deviceResult = D3D11.CreateDevice(null, .Hardware, 0, deviceFlags, levels, &nativeDevice, let deviceLevel, &nativeContext); FeatureLevel deviceLevel = ?;
Debug.Assert(deviceResult.Succeeded, scope $"Failed to create D3D11 Device. Message(0x{(int32)deviceResult}): {deviceResult}"); var deviceResult = D3D11.CreateDevice(null, .Hardware, 0, deviceFlags, levels, &nativeDevice, &deviceLevel, &nativeContext);
Debug.Assert(deviceResult.Succeeded, scope $"Failed to create D3D11 Device. Message(0x{(int32)deviceResult}): {deviceResult}");
#if DEBUG
if(nativeDevice.QueryInterface<ID3D11Debug>(out _debugDevice).Succeeded) #if DEBUG
{ if(nativeDevice.QueryInterface<ID3D11Debug>(out _debugDevice).Succeeded)
ID3D11InfoQueue* infoQueue; {
if(nativeDevice.QueryInterface<ID3D11InfoQueue>(out infoQueue).Succeeded) ID3D11InfoQueue* infoQueue;
{ if(nativeDevice.QueryInterface<ID3D11InfoQueue>(out infoQueue).Succeeded)
infoQueue.SetBreakOnSeverity(.Corruption, true); {
infoQueue.SetBreakOnSeverity(.Error, true); infoQueue.SetBreakOnSeverity(.Corruption, true);
infoQueue.SetBreakOnSeverity(.Error, true);
infoQueue.Release();
} infoQueue.Release();
} }
#endif }
#endif
Dx11Cheater.Device = nativeDevice;
Dx11Cheater.ImmediateContext = nativeContext; Dx11Cheater.Device = nativeDevice;
Dx11Cheater.ImmediateContext = nativeContext;
Log.EngineLogger.Trace("D3D11 Device and Context created (Feature level: {})", deviceLevel);
} Log.EngineLogger.Trace("D3D11 Device and Context created (Feature level: {})", deviceLevel);
}
private ID3D11RenderTargetView*[MaxRTVCount] _renderTargets;
private ID3D11RenderTargetView*[MaxRTVCount] _renderTargets;
public override void SetRenderTarget(RenderTarget renderTarget, int slot = 0)
{ public override void SetRenderTarget(RenderTarget renderTarget, int slot = 0)
if(renderTarget == null) {
{ if(renderTarget == null)
_renderTargets[slot] = _swapChain.nativeBackBufferTarget; {
//_immediateContext.OutputMerger.SetRenderTargets(1, &_swapChain._backBufferTarget, null); _renderTargets[slot] = _swapChain.nativeBackBufferTarget;
} //_immediateContext.OutputMerger.SetRenderTargets(1, &_swapChain._backBufferTarget, null);
else }
{ else
Runtime.NotImplemented(); {
} Runtime.NotImplemented();
} }
}
public override void BindRenderTargets()
{ public override void BindRenderTargets()
nativeContext.OutputMerger.SetRenderTargets(MaxRTVCount, &_renderTargets, null); {
} nativeContext.OutputMerger.SetRenderTargets(MaxRTVCount, &_renderTargets, null);
}
public override void ClearRenderTarget(RenderTarget renderTarget, ColorRGBA color)
{ public override void ClearRenderTarget(RenderTarget renderTarget, ColorRGBA color)
if(renderTarget == null) {
{ if(renderTarget == null)
nativeContext.ClearRenderTargetView(_swapChain.nativeBackBufferTarget, color); {
} nativeContext.ClearRenderTargetView(_swapChain.nativeBackBufferTarget, color);
else }
{ else
Runtime.NotImplemented(); {
} Runtime.NotImplemented();
} }
}
public override void SetVertexBuffer(uint32 slot, Buffer buffer, uint32 stride, uint32 offset = 0)
{ public override void SetVertexBuffer(uint32 slot, Buffer buffer, uint32 stride, uint32 offset = 0)
// make stride and offset mutable so that we can take their pointers. {
var stride, offset; // make stride and offset mutable so that we can take their pointers.
nativeContext.InputAssembler.SetVertexBuffers(slot, 1, &buffer.nativeBuffer, &stride, &offset); var stride, offset;
} nativeContext.InputAssembler.SetVertexBuffers(slot, 1, &buffer.nativeBuffer, &stride, &offset);
}
public override void Draw(uint32 vertexCount, uint32 startVertexIndex = 0)
{ public override void Draw(uint32 vertexCount, uint32 startVertexIndex = 0)
nativeContext.Draw(vertexCount, startVertexIndex); {
} nativeContext.Draw(vertexCount, startVertexIndex);
}
public override void DrawIndexed(uint32 indexCount, uint32 startIndexLocation = 0, int32 vertexOffset = 0)
{ public override void DrawIndexed(uint32 indexCount, uint32 startIndexLocation = 0, int32 vertexOffset = 0)
nativeContext.DrawIndexed(indexCount, startIndexLocation, vertexOffset); {
} nativeContext.DrawIndexed(indexCount, startIndexLocation, vertexOffset);
}
public override void SetIndexBuffer(Buffer buffer, IndexFormat indexFormat = .Index16Bit, uint32 byteOffset = 0)
{ public override void SetIndexBuffer(Buffer buffer, IndexFormat indexFormat = .Index16Bit, uint32 byteOffset = 0)
nativeContext.InputAssembler.SetIndexBuffer(buffer.nativeBuffer, indexFormat == .Index32Bit ? .R32_UInt : .R16_UInt, byteOffset); {
} nativeContext.InputAssembler.SetIndexBuffer(buffer.nativeBuffer, indexFormat == .Index32Bit ? .R32_UInt : .R16_UInt, byteOffset);
}
protected override void SetRasterizerStateImpl()
{ protected override void SetRasterizerStateImpl()
nativeContext.Rasterizer.SetState(_currentRasterizerState.nativeRasterizerState); {
} nativeContext.Rasterizer.SetState(_currentRasterizerState.nativeRasterizerState);
} }
} }
}