Added RenderTarget2D

This commit is contained in:
Simon Lübeß
2021-09-17 12:58:33 +02:00
parent d051b392d0
commit 266d7a4651
9 changed files with 77 additions and 38 deletions
@@ -102,7 +102,7 @@ namespace GlitchyEngine.Renderer
_depthStencilTarget = target?.nativeView; _depthStencilTarget = target?.nativeView;
} }
public override void SetRenderTarget(RenderTarget renderTarget, int slot = 0) public override void SetRenderTarget(RenderTarget2D renderTarget, int slot = 0)
{ {
if(renderTarget == null) if(renderTarget == null)
{ {
@@ -110,7 +110,7 @@ namespace GlitchyEngine.Renderer
} }
else else
{ {
Runtime.NotImplemented(); _renderTargets[slot] = renderTarget.nativeRenderTargetView;
} }
} }
@@ -119,7 +119,7 @@ namespace GlitchyEngine.Renderer
nativeContext.OutputMerger.SetRenderTargets(MaxRTVCount, &_renderTargets, _depthStencilTarget); nativeContext.OutputMerger.SetRenderTargets(MaxRTVCount, &_renderTargets, _depthStencilTarget);
} }
public override void ClearRenderTarget(RenderTarget renderTarget, ColorRGBA color) public override void ClearRenderTarget(RenderTarget2D renderTarget, ColorRGBA color)
{ {
if(renderTarget == null) if(renderTarget == null)
{ {
@@ -127,7 +127,7 @@ namespace GlitchyEngine.Renderer
} }
else else
{ {
Runtime.NotImplemented(); nativeContext.ClearRenderTargetView(renderTarget.nativeRenderTargetView, color);
} }
} }
@@ -187,7 +187,7 @@ namespace GlitchyEngine.Renderer
for(let entry in shader.Textures) for(let entry in shader.Textures)
{ {
_textures[entry.Index] = entry.Texture?.nativeView; _textures[entry.Index] = entry.Texture?.nativeResourceView;
_samplers[entry.Index] = entry.Texture?.SamplerState?.nativeSamplerState; _samplers[entry.Index] = entry.Texture?.SamplerState?.nativeSamplerState;
if(entry.Index >= _textureCount) if(entry.Index >= _textureCount)
@@ -0,0 +1,22 @@
using DirectX.D3D11;
namespace GlitchyEngine.Renderer
{
using internal GlitchyEngine.Renderer;
extension RenderTarget2D
{
protected internal ID3D11RenderTargetView* nativeRenderTargetView ~ _?.Release();
protected override void CreateRenderTargetPlatform(Texture2DDesc desc)
{
nativeRenderTargetView?.Release();
nativeRenderTargetView = null;
CreateTexturePlatform(desc, true, null, 0);
var result = _context.nativeDevice.CreateRenderTargetView(nativeTexture, null, &nativeRenderTargetView);
Log.EngineLogger.Assert(result.Succeeded, scope $"Failed to create render target view. Error ({result.Underlying}): {result}");
}
}
}
@@ -23,7 +23,7 @@ namespace GlitchyEngine.Renderer
} }
public override void Clear(RenderTarget renderTarget, ColorRGBA clearColor) public override void Clear(RenderTarget2D renderTarget, ColorRGBA clearColor)
{ {
_context.ClearRenderTarget(renderTarget, clearColor); _context.ClearRenderTarget(renderTarget, clearColor);
} }
@@ -13,12 +13,12 @@ namespace GlitchyEngine.Renderer
{ {
extension Texture extension Texture
{ {
protected internal ID3D11ShaderResourceView* nativeView ~ _?.Release(); protected internal ID3D11ShaderResourceView* nativeResourceView ~ _?.Release();
protected override void ImplBind(uint32 slot) protected override void ImplBind(uint32 slot)
{ {
_context.nativeContext.VertexShader.SetShaderResources(slot, 1, &nativeView); _context.nativeContext.VertexShader.SetShaderResources(slot, 1, &nativeResourceView);
_context.nativeContext.PixelShader.SetShaderResources(slot, 1, &nativeView); _context.nativeContext.PixelShader.SetShaderResources(slot, 1, &nativeResourceView);
} }
/** \brief Loads the texture from the specified path. /** \brief Loads the texture from the specified path.
@@ -29,17 +29,17 @@ namespace GlitchyEngine.Renderer
protected bool LoadResourcePlatform<T>(StringView path, ref T* texture) where T : ID3D11Resource protected bool LoadResourcePlatform<T>(StringView path, ref T* texture) where T : ID3D11Resource
{ {
((ID3D11Resource*)texture)?.Release(); ((ID3D11Resource*)texture)?.Release();
nativeView?.Release(); nativeResourceView?.Release();
HResult loadResult = DDSTextureLoader.CreateDDSTextureFromFile(_context.nativeDevice, path.ToScopedNativeWChar!(), HResult loadResult = DDSTextureLoader.CreateDDSTextureFromFile(_context.nativeDevice, path.ToScopedNativeWChar!(),
(.)&texture, &nativeView); (.)&texture, &nativeResourceView);
if(loadResult.Failed) if(loadResult.Failed)
{ {
Log.EngineLogger.Error($"Failed to load texture \"{path}\". Error({(int)loadResult}): {loadResult}"); Log.EngineLogger.Error($"Failed to load texture \"{path}\". Error({(int)loadResult}): {loadResult}");
ReleaseAndNullify!(texture); ReleaseAndNullify!(texture);
ReleaseAndNullify!(nativeView); ReleaseAndNullify!(nativeResourceView);
return false; return false;
} }
@@ -64,6 +64,7 @@ namespace GlitchyEngine.Renderer
desc.SampleDesc = .(1, 0); desc.SampleDesc = .(1, 0);
desc.Usage = (.)Usage; desc.Usage = (.)Usage;
desc.BindFlags = .ShaderResource; desc.BindFlags = .ShaderResource;
desc.CpuAccessFlags = (.)CpuAccess; desc.CpuAccessFlags = (.)CpuAccess;
desc.MiscFlags = .None; desc.MiscFlags = .None;
@@ -93,21 +94,10 @@ namespace GlitchyEngine.Renderer
nativeTexture.GetDescription(out nativeDesc); nativeTexture.GetDescription(out nativeDesc);
} }
protected override void CreateTexturePlatform(Texture2DDesc desc, void* data, uint32 linePitch) protected override void CreateTexturePlatform(Texture2DDesc desc, bool isRenderTarget, void* data, uint32 linePitch)
{ {
nativeTexture?.Release(); PrepareTexturePlatform(desc, isRenderTarget);
nativeView?.Release(); InternalCreateTexture(data, linePitch, 0);
nativeDesc = (NativeTex2DDesc)desc;
SubresourceData resData = .(data, linePitch, 0);
var result = _context.[Friend]nativeDevice.CreateTexture2D(ref nativeDesc, &resData, &nativeTexture);
Log.EngineLogger.Assert(result.Succeeded, scope $"Failed to create texture 2D. Error ({result.Underlying}): {result}");
result = _context.[Friend]nativeDevice.CreateShaderResourceView(nativeTexture, null, &nativeView);
Log.EngineLogger.Assert(result.Succeeded, scope $"Failed to create texture view. Error ({result.Underlying}): {result}");
} }
private void InternalCreateTexture(void* data, uint32 linePitch, uint32 slicePitch) private void InternalCreateTexture(void* data, uint32 linePitch, uint32 slicePitch)
@@ -120,17 +110,21 @@ namespace GlitchyEngine.Renderer
var result = _context.nativeDevice.CreateTexture2D(ref nativeDesc, resDataPtr, &nativeTexture); var result = _context.nativeDevice.CreateTexture2D(ref nativeDesc, resDataPtr, &nativeTexture);
Log.EngineLogger.Assert(result.Succeeded, scope $"Failed to create texture 2D. Error ({result.Underlying}): {result}"); Log.EngineLogger.Assert(result.Succeeded, scope $"Failed to create texture 2D. Error ({result.Underlying}): {result}");
result = _context.[Friend]nativeDevice.CreateShaderResourceView(nativeTexture, null, &nativeView); result = _context.[Friend]nativeDevice.CreateShaderResourceView(nativeTexture, null, &nativeResourceView);
Log.EngineLogger.Assert(result.Succeeded, scope $"Failed to create texture view. Error ({result.Underlying}): {result}"); Log.EngineLogger.Assert(result.Succeeded, scope $"Failed to create texture view. Error ({result.Underlying}): {result}");
} }
protected override void PrepareTexturePlatform(Texture2DDesc desc) protected override void PrepareTexturePlatform(Texture2DDesc desc, bool isRenderTarget)
{ {
nativeTexture?.Release(); nativeTexture?.Release();
nativeTexture = null; nativeTexture = null;
nativeView?.Release(); nativeResourceView?.Release();
nativeResourceView = null;
nativeDesc = (NativeTex2DDesc)desc; nativeDesc = (NativeTex2DDesc)desc;
if(isRenderTarget)
nativeDesc.BindFlags |= .RenderTarget;
} }
// TODO: Update Texture Arrays! // TODO: Update Texture Arrays!
@@ -3,8 +3,6 @@ using System;
namespace GlitchyEngine.Renderer namespace GlitchyEngine.Renderer
{ {
public class RenderTarget;
public class GraphicsContext : RefCounted public class GraphicsContext : RefCounted
{ {
private RasterizerState _currentRasterizerState; private RasterizerState _currentRasterizerState;
@@ -25,7 +23,7 @@ namespace GlitchyEngine.Renderer
* @remarks When @RenderTarget is null the backbuffer will be bound. * @remarks When @RenderTarget is null the backbuffer will be bound.
* @BindRenderTargets has to be called in order to bind the rendertargets. * @BindRenderTargets has to be called in order to bind the rendertargets.
*/ */
public extern void SetRenderTarget(RenderTarget renderTarget, int slot = 0); public extern void SetRenderTarget(RenderTarget2D renderTarget, int slot = 0);
/** /**
* Binds all. * Binds all.
@@ -41,7 +39,7 @@ namespace GlitchyEngine.Renderer
* @param color The color to clear the render target with. * @param color The color to clear the render target with.
* @remarks When @renderTarget is null the backbuffer will be cleared. * @remarks When @renderTarget is null the backbuffer will be cleared.
*/ */
public extern void ClearRenderTarget(RenderTarget renderTarget, ColorRGBA color); public extern void ClearRenderTarget(RenderTarget2D renderTarget, ColorRGBA color);
//public abstract void SetViewport(Viewport viewport); //public abstract void SetViewport(Viewport viewport);
+1 -1
View File
@@ -27,7 +27,7 @@ namespace GlitchyEngine.Renderer
} }
[Inline] [Inline]
public static void Clear(RenderTarget renderTarget, ColorRGBA color) public static void Clear(RenderTarget2D renderTarget, ColorRGBA color)
{ {
_rendererAPI.Clear(renderTarget, color); _rendererAPI.Clear(renderTarget, color);
} }
@@ -0,0 +1,25 @@
using System;
namespace GlitchyEngine.Renderer
{
public class RenderTarget2D : Texture2D
{
public this(GraphicsContext context, uint32 width, uint32 height, Format format, uint32 arraySize = 1, uint32 mipLevels = 1, CPUAccessFlags cpuAccess = .None)
: base(context)
{
Texture2DDesc desc;
desc.Width = width;
desc.Height = height;
desc.Format = format;
desc.ArraySize = arraySize;
desc.MipLevels = mipLevels;
// RenderTargets only can do default (I think)
desc.Usage = .Default;
desc.CpuAccess = cpuAccess;
CreateRenderTargetPlatform(desc);
}
protected extern void CreateRenderTargetPlatform(Texture2DDesc desc);
}
}
+1 -1
View File
@@ -17,7 +17,7 @@ namespace GlitchyEngine.Renderer
public extern void Init(); public extern void Init();
public extern void Clear(RenderTarget renderTarget, ColorRGBA clearColor); public extern void Clear(RenderTarget2D renderTarget, ColorRGBA clearColor);
public extern void Clear(DepthStencilTarget target, float depthValue, uint8 stencilValue, DepthStencilClearFlag clearFlags); public extern void Clear(DepthStencilTarget target, float depthValue, uint8 stencilValue, DepthStencilClearFlag clearFlags);
+3 -3
View File
@@ -78,7 +78,7 @@ namespace GlitchyEngine.Renderer
public this(GraphicsContext context, Texture2DDesc desc) : base(context) public this(GraphicsContext context, Texture2DDesc desc) : base(context)
{ {
PrepareTexturePlatform(desc); PrepareTexturePlatform(desc, false);
} }
public void SetData<T>(T* data, uint32 arraySlice = 0, uint32 mipSlice = 0) public void SetData<T>(T* data, uint32 arraySlice = 0, uint32 mipSlice = 0)
@@ -98,12 +98,12 @@ namespace GlitchyEngine.Renderer
protected extern void LoadTexturePlatform(); protected extern void LoadTexturePlatform();
protected extern void CreateTexturePlatform(Texture2DDesc desc, void* data, uint32 linePitch); protected extern void CreateTexturePlatform(Texture2DDesc desc, bool isRenderTarget, void* data, uint32 linePitch);
/** /**
* Prepares the texture so that a call to SetData can successfully upload the data to the gpu. * Prepares the texture so that a call to SetData can successfully upload the data to the gpu.
*/ */
protected extern void PrepareTexturePlatform(Texture2DDesc desc); protected extern void PrepareTexturePlatform(Texture2DDesc desc, bool isRenderTarget);
protected extern Result<void> PlatformSetData(void* data, uint32 elementSize, uint32 destX, protected extern Result<void> PlatformSetData(void* data, uint32 elementSize, uint32 destX,
uint32 destY, uint32 destWidth, uint32 destHeight, uint32 arraySlice, uint32 mipLevel, GlitchyEngine.Renderer.MapType mapType); uint32 destY, uint32 destWidth, uint32 destHeight, uint32 arraySlice, uint32 mipLevel, GlitchyEngine.Renderer.MapType mapType);