Added BlendState

This commit is contained in:
Simon Lübeß
2021-02-09 13:37:26 +01:00
parent 7e3bb932fc
commit abe4f66724
8 changed files with 170 additions and 4 deletions
@@ -0,0 +1,41 @@
using DirectX.D3D11;
using DirectX.Common;
using GlitchyEngine.Math;
using internal GlitchyEngine.Renderer;
namespace GlitchyEngine.Renderer
{
extension BlendState
{
internal ID3D11BlendState* nativeBlendState ~ _?.Release();
public override void Bind(ColorRGBA blendFactor)
{
_context.nativeContext.OutputMerger.SetBlendState(nativeBlendState, blendFactor);
}
protected override void PlatformCreateBlendState()
{
BlendDescription nativeDesc = .Default;
nativeDesc.IndependentBlendEnable = _desc.IndependentBlendEnable;
nativeDesc.AlphaToCoverageEnable = _desc.AlphaToCoverageEnable;
for(int i = 0; i < 8; i++)
{
nativeDesc.RenderTarget[i].BlendEnable = _desc.RenderTarget[i].BlendEnable;
nativeDesc.RenderTarget[i].SourceBlend = _desc.RenderTarget[i].SourceBlend;
nativeDesc.RenderTarget[i].DestinationBlend = _desc.RenderTarget[i].DestinationBlend;
nativeDesc.RenderTarget[i].BlendOperation = _desc.RenderTarget[i].BlendOperation;
nativeDesc.RenderTarget[i].SourceBlendAlpha = _desc.RenderTarget[i].SourceBlendAlpha;
nativeDesc.RenderTarget[i].DestinationBlendAlpha = _desc.RenderTarget[i].DestinationBlendAlpha;
nativeDesc.RenderTarget[i].BlendOperationAlpha = _desc.RenderTarget[i].BlendOperationAlpha;
nativeDesc.RenderTarget[i].RenderTargetWriteMask = _desc.RenderTarget[i].RenderTargetWriteMask;
}
HResult result = _context.nativeDevice.CreateBlendState(ref nativeDesc, &nativeBlendState);
Log.EngineLogger.Assert(result.Succeeded, scope $"Failed to create blend state. Error({(int)result}): {result}");
}
}
}
@@ -14,6 +14,11 @@ namespace GlitchyEngine.Renderer
public static override API Api => .D3D11;
public override void Init()
{
}
public override void Clear(RenderTarget renderTarget, ColorRGBA clearColor)
{
_context.ClearRenderTarget(renderTarget, clearColor);
+93
View File
@@ -0,0 +1,93 @@
using System;
using GlitchyEngine.Math;
namespace GlitchyEngine.Renderer
{
public struct BlendStateDescription
{
public typealias Blend = DirectX.D3D11.Blend;
public typealias BlendOperation = DirectX.D3D11.BlendOperation;
public typealias ColorWriteEnable = DirectX.D3D11.ColorWriteEnable;
public struct RenderTargetBlend
{
/**
* Enable (or disable) blending.
*/
public bool BlendEnable;
public Blend SourceBlend;
public Blend DestinationBlend;
public BlendOperation BlendOperation;
public Blend SourceBlendAlpha;
public Blend DestinationBlendAlpha;
public BlendOperation BlendOperationAlpha;
public ColorWriteEnable RenderTargetWriteMask;
public this()
{
this = default;
}
public this(bool blendEnable, Blend sourceBlend, Blend destinationBlend, BlendOperation blendOperation,
Blend sourceBlendAlpha, Blend destinationBlendAlpha, BlendOperation blendOperationAlpha, ColorWriteEnable renderTargetWriteMask)
{
BlendEnable = blendEnable;
SourceBlend = sourceBlend;
DestinationBlend = destinationBlend;
BlendOperation = blendOperation;
SourceBlendAlpha = sourceBlendAlpha;
DestinationBlendAlpha = destinationBlendAlpha;
BlendOperationAlpha = blendOperationAlpha;
RenderTargetWriteMask = renderTargetWriteMask;
}
public static readonly RenderTargetBlend Default = RenderTargetBlend(false, .One, .Zero, .Add, .One, .Zero, .Add, .All);
}
/**
* Specifies whether to enable independent blending in simultaneous render targets. Set to TRUE to enable independent blending.
* If set to FALSE, only the RenderTarget[0] members are used; RenderTarget[1..7] are ignored.
*/
public bool IndependentBlendEnable;
/**
* Specifies whether to use alpha-to-coverage as a multisampling technique when setting a pixel to a render target.
*/
public bool AlphaToCoverageEnable;
public RenderTargetBlend[8] RenderTarget;
public this()
{
this = default;
}
public this(bool independentBlend, bool alphaToCoverage, RenderTargetBlend[8] renderTarget)
{
IndependentBlendEnable = independentBlend;
AlphaToCoverageEnable = alphaToCoverage;
RenderTarget = renderTarget;
}
public static readonly Self Default = .(false, false, .(.Default, .Default, .Default, .Default, .Default, .Default, .Default, .Default));
}
public class BlendState : RefCounted
{
protected GraphicsContext _context ~ _?.ReleaseRef();
protected BlendStateDescription _desc;
public BlendStateDescription Description => _desc;
public this(GraphicsContext context, BlendStateDescription desc)
{
_context = context..AddRef();
_desc = desc;
PlatformCreateBlendState();
}
public extern void Bind(ColorRGBA blendFactor = .White);
protected extern void PlatformCreateBlendState();
}
}
@@ -13,6 +13,12 @@ namespace GlitchyEngine.Renderer
set => _rendererAPI = value;
}
[Inline]
public static void Init()
{
_rendererAPI.Init();
}
[Inline]
public static void Clear(RenderTarget renderTarget, ColorRGBA color)
{
+2
View File
@@ -28,6 +28,8 @@ namespace GlitchyEngine.Renderer
_objectConstants = new Buffer<ObjectConstants>(_context, .(0, .Constant, .Dynamic, .Write));
_objectConstants.Update();
RenderCommand.Init();
}
public static void BeginScene(Camera camera)
@@ -15,6 +15,8 @@ namespace GlitchyEngine.Renderer
*/
public static extern API Api { get; }
public extern void Init();
public extern void Clear(RenderTarget renderTarget, ColorRGBA clearColor);
public extern void DrawIndexed(GeometryBinding geometry);
Binary file not shown.
+18 -1
View File
@@ -69,6 +69,10 @@ namespace Sandbox
GraphicsContext _context ~ _?.ReleaseRef();
Texture2D _texture ~ _?.ReleaseRef();
Texture2D _ge_logo ~ _?.ReleaseRef();
BlendState _alphaBlendState ~ _?.ReleaseRef();
BlendState _opaqueBlendState ~ _?.ReleaseRef();
private Vector3 CircleCoord(float angle)
{
@@ -193,6 +197,7 @@ namespace Sandbox
*/
_texture = new Texture2D(_context, "content/Textures/Checkerboard.dds");
_ge_logo = new Texture2D(_context, "content/Textures/GE_Logo.dds");
let sampler = SamplerStateManager.GetSampler(
SamplerStateDescription()
@@ -201,7 +206,14 @@ namespace Sandbox
});
_texture.SamplerState = sampler;
_ge_logo.SamplerState = sampler;
sampler.ReleaseRef();
BlendStateDescription blendDesc = .();
blendDesc.RenderTarget[0] = .(true, .SourceAlpha, .InvertedSourceAlpha, .Add, .SourceAlpha, .InvertedSourceAlpha, .Add, .All);
_alphaBlendState = new BlendState(_context, blendDesc);
_opaqueBlendState = new BlendState(_context, .Default);
}
public override void Update(GameTime gameTime)
@@ -253,6 +265,8 @@ namespace Sandbox
Renderer.BeginScene(_camera);
_opaqueBlendState.Bind();
for(int x < 20)
for(int y < 20)
{
@@ -271,8 +285,11 @@ namespace Sandbox
_cBuffer.Update();
_texture.Bind();
Renderer.Submit(_quadGeometryBinding, _textureEffect, .Scaling(1.5f));
Renderer.Submit(_geometryBinding, _effect);
_alphaBlendState.Bind();
_ge_logo.Bind();
Renderer.Submit(_quadGeometryBinding, _textureEffect, .Scaling(1.5f));
Renderer.EndScene();