Added RasterizerState

This commit is contained in:
Simon Lübeß
2020-11-25 20:04:51 +01:00
parent 5954fbc411
commit ff16f6a895
5 changed files with 155 additions and 15 deletions
+5 -15
View File
@@ -72,12 +72,12 @@ namespace GlitchyEngine
VertexBuffer<VertexColor> _vertexBuffer ~ delete _;
IndexBuffer _indexBuffer ~ delete _;
RasterizerState _rasterizerState ~ delete _;
ID3D11VertexShader* _vertexShader ~ _?.Release();
ID3D11PixelShader* _pixelShader ~ _?.Release();
ID3D11InputLayout* _inputLayout ~ _?.Release();
ID3D11RasterizerState* _rasterizerState ~ _?.Release();
private Vector3 CircleCoord(float angle)
{
return .(Math.Cos(angle), Math.Sin(angle), 0);
@@ -179,17 +179,8 @@ namespace GlitchyEngine
}
// Create rasterizer state
RasterizerStateDescription rsDesc = .();
rsDesc.CullMode = .Back;
rsDesc.FillMode = .Solid;
rsDesc.FrontCounterClockwise = true;
result = Dx11Cheater.Device.CreateRasterizerState(ref rsDesc, &_rasterizerState);
if(result.Failed)
{
Debug.Write("ERROR: Failed to create Rasterizer State: {}", result);
Runtime.FatalError("Failed to create Rasterizer State");
}
GlitchyEngine.Renderer.RasterizerStateDescription rsDesc = .(.Solid, .Back, true);
_rasterizerState = new RasterizerState(Window.Context, rsDesc);
}
public void OnEvent(Event e)
@@ -233,8 +224,7 @@ namespace GlitchyEngine
_immediateContext.VertexShader.SetShader(_vertexShader, null, 0);
_immediateContext.Rasterizer.SetState(_rasterizerState);
Window.Context.SetRasterizerState(_rasterizerState);
_immediateContext.Rasterizer.SetViewports(1, &Dx11Cheater.BackbufferViewport);
@@ -155,5 +155,10 @@ namespace GlitchyEngine.Renderer
{
nativeContext.InputAssembler.SetIndexBuffer(buffer.nativeBuffer, indexFormat == .Index32Bit ? .R32_UInt : .R16_UInt, byteOffset);
}
protected override void SetRasterizerStateImpl()
{
nativeContext.Rasterizer.SetState(_currentRasterizerState.nativeRasterizerState);
}
}
}
@@ -0,0 +1,63 @@
using DirectX.D3D11;
using internal GlitchyEngine.Renderer;
namespace GlitchyEngine.Renderer
{
extension FillMode
{
public static explicit operator DirectX.D3D11.FillMode(Self fm) => fm == .Solid ? .Solid : .Wireframe;
}
extension CullMode
{
public static explicit operator DirectX.D3D11.CullMode(Self cm)
{
if(cm == .None)
return .None;
if(cm == .Front)
return .Front;
return .Back;
}
}
extension RasterizerStateDescription
{
public static explicit operator DirectX.D3D11.RasterizerStateDescription(Self description)
{
DirectX.D3D11.RasterizerStateDescription result = .();
result.FillMode = (.)description.FillMode;
result.CullMode = (.)description.CullMode;
result.FrontCounterClockwise = description.FrontCounterClockwise;
result.DepthBias = description.DepthBias;
result.DepthBiasClamp = description.DepthBiasClamp;
result.SlopeScaledDepthBias = description.SlopeScaledDepthBias;
result.DepthClipEnable = description.DepthClipEnabled;
result.ScissorEnable = description.ScissorEnabled;
result.MultisampleEnable = description.MultisampleEnabled;
result.AntialiasedLineEnable = description.AntialiasedLineEnabled;
return result;
}
}
extension RasterizerState
{
internal DirectX.D3D11.RasterizerStateDescription nativeDescription;
internal ID3D11RasterizerState* nativeRasterizerState ~ _?.Release();
public override this(GraphicsContext context, GlitchyEngine.Renderer.RasterizerStateDescription description) : this(context)
{
_description = description;
nativeDescription = (.)_description;
var result = context.nativeDevice.CreateRasterizerState(ref nativeDescription, &nativeRasterizerState);
if(result.Failed)
{
Log.EngineLogger.Error("Failed to create rasterizer state. Message({}): {}", (int)result, result);
}
}
}
}
@@ -7,6 +7,8 @@ namespace GlitchyEngine.Renderer
//public abstract class GraphicsContext
public class GraphicsContext
{
private RasterizerState _currentRasterizerState;
//public abstract SwapChain SwapChain {get;}
public extern SwapChain SwapChain {get;}
@@ -76,5 +78,18 @@ namespace GlitchyEngine.Renderer
}
public extern void SetIndexBuffer(Buffer buffer, IndexFormat indexFormat = .Index16Bit, uint32 byteOffset = 0);
public void SetRasterizerState(RasterizerState rasterizerState)
{
_currentRasterizerState = rasterizerState;
SetRasterizerStateImpl();
}
protected extern void SetRasterizerStateImpl();
public RasterizerState GetRasterizerState()
{
return _currentRasterizerState;
}
}
}
@@ -0,0 +1,67 @@
namespace GlitchyEngine.Renderer
{
public enum FillMode
{
Wireframe,
Solid
}
public enum CullMode
{
None,
Front,
Back
}
public struct RasterizerStateDescription
{
public FillMode FillMode;
public CullMode CullMode;
public bool FrontCounterClockwise;
public int32 DepthBias;
public float DepthBiasClamp;
public float SlopeScaledDepthBias;
public bool DepthClipEnabled;
public bool ScissorEnabled;
public bool MultisampleEnabled;
public bool AntialiasedLineEnabled;
public this() => this = default;
/**
Initializes the RasterizerStateDescription with the specified values
*/
public this(FillMode fillMode, CullMode cullMode = .Back, bool frontCounterClockwise = false, int32 depthBias = 0, float depthBiasClamp = 0.0f,
float slopeScaledDepthBias = 0.0f, bool depthClipEnabled = true, bool scissorEnabled = false, bool multisampleEnabled = false, bool antialiasedLineEnabled = false)
{
FillMode = fillMode;
CullMode = cullMode;
FrontCounterClockwise = frontCounterClockwise;
DepthBias = depthBias;
DepthBiasClamp = depthBiasClamp;
SlopeScaledDepthBias = slopeScaledDepthBias;
DepthClipEnabled = depthClipEnabled;
ScissorEnabled = scissorEnabled;
MultisampleEnabled = multisampleEnabled;
AntialiasedLineEnabled = antialiasedLineEnabled;
}
public static readonly RasterizerStateDescription Default = .(.Solid, .Back, false, 0, 0f, 0f, true, false, false, false);
}
public class RasterizerState
{
internal GraphicsContext _context;
private RasterizerStateDescription _description;
public GraphicsContext Context => _context;
public RasterizerStateDescription Description => _description;
protected this(GraphicsContext context)
{
_context = context;
}
public extern this(GraphicsContext context, RasterizerStateDescription description);
}
}