Rendertarget inherits from texture

This commit is contained in:
Simon Lübeß
2022-05-04 14:53:31 +02:00
parent 9c93ccee47
commit 805423a64e
6 changed files with 25 additions and 39 deletions
@@ -14,7 +14,7 @@ namespace ImGui
public static override void Image(Texture2D texture, Vec2 size, Vec2 uv0 = Vec2.Zero, Vec2 uv1 = Vec2.Ones, Vec4 tint_col = Vec4.Ones, Vec4 border_col = Vec4.Zero) public static override void Image(Texture2D texture, Vec2 size, Vec2 uv0 = Vec2.Zero, Vec2 uv1 = Vec2.Ones, Vec4 tint_col = Vec4.Ones, Vec4 border_col = Vec4.Zero)
{ {
var view = texture.nativeResourceView..AddRef(); var view = texture._nativeResourceView..AddRef();
_resourceViews.Add(view); _resourceViews.Add(view);
ImGui.Image(view, size, uv0, uv1, tint_col, border_col); ImGui.Image(view, size, uv0, uv1, tint_col, border_col);
@@ -14,7 +14,6 @@ namespace GlitchyEngine.Renderer
extension RenderTarget2D extension RenderTarget2D
{ {
protected internal ID3D11Texture2D* _nativeTexture ~ _?.Release(); protected internal ID3D11Texture2D* _nativeTexture ~ _?.Release();
protected internal ID3D11ShaderResourceView* _nativeResourceView ~ _?.Release();
protected internal ID3D11RenderTargetView* _nativeRenderTargetView ~ _?.Release(); protected internal ID3D11RenderTargetView* _nativeRenderTargetView ~ _?.Release();
private void ReleaseAndNullify() private void ReleaseAndNullify()
@@ -18,7 +18,7 @@ namespace GlitchyEngine.Renderer
extension Texture extension Texture
{ {
protected internal ID3D11ShaderResourceView* nativeResourceView ~ _?.Release(); protected internal ID3D11ShaderResourceView* _nativeResourceView ~ _?.Release();
/** \brief Loads the texture from the specified path. /** \brief Loads the texture from the specified path.
* @param path The path of the texture to load. * @param path The path of the texture to load.
@@ -39,17 +39,17 @@ namespace GlitchyEngine.Renderer
} }
((ID3D11Resource*)texture)?.Release(); ((ID3D11Resource*)texture)?.Release();
nativeResourceView?.Release(); _nativeResourceView?.Release();
HResult loadResult = DDSTextureLoader.CreateDDSTextureFromMemory(NativeDevice, HResult loadResult = DDSTextureLoader.CreateDDSTextureFromMemory(NativeDevice,
ddsData.Ptr, (uint)ddsData.Count, (.)&texture, &nativeResourceView); ddsData.Ptr, (uint)ddsData.Count, (.)&texture, &_nativeResourceView);
if(loadResult.Failed) if(loadResult.Failed)
{ {
Log.EngineLogger.Error($"Failed to load texture. Error({(int)loadResult}): {loadResult}"); Log.EngineLogger.Error($"Failed to load texture. Error({(int)loadResult}): {loadResult}");
ReleaseAndNullify!(texture); ReleaseAndNullify!(texture);
ReleaseAndNullify!(nativeResourceView); ReleaseAndNullify!(_nativeResourceView);
return false; return false;
} }
@@ -126,7 +126,7 @@ namespace GlitchyEngine.Renderer
var result = NativeDevice.CreateTexture2D(ref nativeDesc, resDataPtr, &nativeTexture); var result = 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 = NativeDevice.CreateShaderResourceView(nativeTexture, null, &nativeResourceView); result = 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}");
} }
@@ -136,8 +136,8 @@ namespace GlitchyEngine.Renderer
nativeTexture?.Release(); nativeTexture?.Release();
nativeTexture = null; nativeTexture = null;
nativeResourceView?.Release(); _nativeResourceView?.Release();
nativeResourceView = null; _nativeResourceView = null;
nativeDesc = (NativeTex2DDesc)desc; nativeDesc = (NativeTex2DDesc)desc;
+6 -17
View File
@@ -33,34 +33,23 @@ namespace GlitchyEngine.Renderer
} }
} }
public class RenderTarget2D : RefCounter public class RenderTarget2D : Texture
{ {
private RenderTarget2DDescription _description; private RenderTarget2DDescription _description;
public RenderTarget2DDescription Description => _description; public RenderTarget2DDescription Description => _description;
public uint32 Width => _description.Width; public override uint32 Width => _description.Width;
public uint32 Height => _description.Height; public override uint32 Height => _description.Height;
public override uint32 Depth => 1;
public override uint32 ArraySize => _description.ArraySize;
public override uint32 MipLevels => _description.MipLevels;
protected internal DepthStencilTarget _depthStenilTarget ~ _?.ReleaseRef(); protected internal DepthStencilTarget _depthStenilTarget ~ _?.ReleaseRef();
// TODO: DepthStencilTarget is just a renderTarget // TODO: DepthStencilTarget is just a renderTarget
public DepthStencilTarget DepthStencilTarget => _depthStenilTarget; public DepthStencilTarget DepthStencilTarget => _depthStenilTarget;
protected SamplerState _samplerState ~ _?.ReleaseRef();
public SamplerState SamplerState
{
get => _samplerState;
set
{
if(_samplerState == value)
return;
SetReference!(_samplerState, value);
}
}
public this(RenderTarget2DDescription description) public this(RenderTarget2DDescription description)
{ {
Debug.Profiler.ProfileResourceFunction!(); Debug.Profiler.ProfileResourceFunction!();
+8 -8
View File
@@ -91,13 +91,13 @@ namespace GlitchyEngine.Renderer
FrontToBack FrontToBack
} }
struct QueueQuad: this(Matrix Transform, ColorRGBA Color, Texture2D Texture, float Depth, Vector4 uvTransform) { } struct QueueQuad: this(Matrix Transform, ColorRGBA Color, Texture Texture, float Depth, Vector4 uvTransform) { }
struct QueueCircle : QueueQuad struct QueueCircle : QueueQuad
{ {
public float InnerRadius; public float InnerRadius;
public this(Matrix Transform, ColorRGBA Color, Texture2D Texture, float Depth, Vector4 uvTransform, float innerRadius) public this(Matrix Transform, ColorRGBA Color, Texture Texture, float Depth, Vector4 uvTransform, float innerRadius)
: base(Transform, Color, Texture, Depth, uvTransform) : base(Transform, Color, Texture, Depth, uvTransform)
{ {
InnerRadius = innerRadius; InnerRadius = innerRadius;
@@ -472,7 +472,7 @@ namespace GlitchyEngine.Renderer
/// Adds a quad instance to the instance queue. /// Adds a quad instance to the instance queue.
[Inline] [Inline]
private static void QueueQuadInstance(Matrix transform, ColorRGBA color, Texture2D texture, float depth, Vector4 uvTransform) private static void QueueQuadInstance(Matrix transform, ColorRGBA color, Texture texture, float depth, Vector4 uvTransform)
{ {
s_QuadinstanceQueue.Add(QueueQuad(transform, color, texture ?? s_whiteTexture, depth, uvTransform)); s_QuadinstanceQueue.Add(QueueQuad(transform, color, texture ?? s_whiteTexture, depth, uvTransform));
s_statistics.QuadCount++; s_statistics.QuadCount++;
@@ -593,7 +593,7 @@ namespace GlitchyEngine.Renderer
if(s_QuadinstanceQueue.IsEmpty) if(s_QuadinstanceQueue.IsEmpty)
return; return;
Texture2D texture = s_QuadinstanceQueue[0].Texture; Texture texture = s_QuadinstanceQueue[0].Texture;
s_currentEffect.SetTexture("Texture", texture); s_currentEffect.SetTexture("Texture", texture);
s_setInstances = 0; s_setInstances = 0;
@@ -631,7 +631,7 @@ namespace GlitchyEngine.Renderer
if(s_circleInstanceQueue.IsEmpty) if(s_circleInstanceQueue.IsEmpty)
return; return;
Texture2D texture = s_circleInstanceQueue[0].Texture; Texture texture = s_circleInstanceQueue[0].Texture;
s_currentCircleEffect.SetTexture("Texture", texture); s_currentCircleEffect.SetTexture("Texture", texture);
s_setInstances = 0; s_setInstances = 0;
@@ -764,19 +764,19 @@ namespace GlitchyEngine.Renderer
// Textured Quad // Textured Quad
public static void DrawQuad(Vector2 position, Vector2 size, float rotation, Texture2D texture, ColorRGBA color = .White, Vector4 uvTransform = .(0, 0, 1, 1)) public static void DrawQuad(Vector2 position, Vector2 size, float rotation, Texture texture, ColorRGBA color = .White, Vector4 uvTransform = .(0, 0, 1, 1))
{ {
DrawQuad(Vector3(position, 0.0f), size, rotation, texture, color, uvTransform); DrawQuad(Vector3(position, 0.0f), size, rotation, texture, color, uvTransform);
} }
public static void DrawQuad(Vector3 position, Vector2 size, float rotation, Texture2D texture, ColorRGBA color = .White, Vector4 uvTransform = .(0, 0, 1, 1)) public static void DrawQuad(Vector3 position, Vector2 size, float rotation, Texture texture, ColorRGBA color = .White, Vector4 uvTransform = .(0, 0, 1, 1))
{ {
Matrix transform = Calculate2DTransform(position, size, rotation); Matrix transform = Calculate2DTransform(position, size, rotation);
DrawQuad(transform, texture, color, uvTransform); DrawQuad(transform, texture, color, uvTransform);
} }
public static void DrawQuad(Matrix transform, Texture2D texture, ColorRGBA color = .White, Vector4 uvTransform = .(0, 0, 1, 1)) public static void DrawQuad(Matrix transform, Texture texture, ColorRGBA color = .White, Vector4 uvTransform = .(0, 0, 1, 1))
{ {
Debug.Profiler.ProfileRendererFunction!(); Debug.Profiler.ProfileRendererFunction!();
+1 -3
View File
@@ -18,9 +18,7 @@ namespace GlitchyEngine.Renderer
if(_samplerState == value) if(_samplerState == value)
return; return;
_samplerState?.ReleaseRef(); SetReference!(_samplerState, value);
_samplerState = value;
_samplerState?.AddRef();
} }
} }