From 92a3eaa4266c9cac4bcd4d44da17e5bc1dc01c6e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20L=C3=BCbe=C3=9F?= Date: Thu, 25 Mar 2021 16:31:03 +0100 Subject: [PATCH] Added protected function to create Texture2D from raw data --- .../src/Platform/DX11/Renderer/Dx11Texture.bf | 48 ++++++++++++++++++- GlitchyEngine/src/Renderer/Texture.bf | 13 +++++ 2 files changed, 59 insertions(+), 2 deletions(-) diff --git a/GlitchyEngine/src/Platform/DX11/Renderer/Dx11Texture.bf b/GlitchyEngine/src/Platform/DX11/Renderer/Dx11Texture.bf index 86dbfc6..f23f355 100644 --- a/GlitchyEngine/src/Platform/DX11/Renderer/Dx11Texture.bf +++ b/GlitchyEngine/src/Platform/DX11/Renderer/Dx11Texture.bf @@ -5,6 +5,8 @@ using DirectXTK; using internal GlitchyEngine.Renderer; +typealias NativeTex2DDesc = DirectX.D3D11.Texture2DDescription; + namespace GlitchyEngine.Renderer { extension Texture @@ -18,10 +20,35 @@ namespace GlitchyEngine.Renderer } } + extension Texture2DDesc + { + public NativeTex2DDesc ToNative() + { + NativeTex2DDesc desc; + + desc.Width = Width; + desc.Height = Height; + desc.MipLevels = MipLevels; + desc.ArraySize = ArraySize; + desc.Format = Format; + + // TODO: missing options + desc.SampleDesc = .(1, 0); + desc.Usage = .Immutable; + desc.BindFlags = .ShaderResource; + desc.CpuAccessFlags = .None; + desc.MiscFlags = .None; + + return desc; + } + + public static implicit operator NativeTex2DDesc(Self desc) => desc.ToNative(); + } + extension Texture2D { - internal ID3D11Texture2D* nativeTexture ~ _?.Release(); - internal Texture2DDescription nativeDesc; + protected internal ID3D11Texture2D* nativeTexture ~ _?.Release(); + protected internal NativeTex2DDesc nativeDesc; public override uint32 Width => nativeDesc.Width; public override uint32 Height => nativeDesc.Height; @@ -48,5 +75,22 @@ namespace GlitchyEngine.Renderer nativeTexture.GetDescription(out nativeDesc); } + + protected override void CreateTexturePlatform(Texture2DDesc desc, void* data, uint32 linePitch) + { + nativeTexture?.Release(); + nativeView?.Release(); + + nativeDesc = 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}"); + } } } diff --git a/GlitchyEngine/src/Renderer/Texture.bf b/GlitchyEngine/src/Renderer/Texture.bf index 1c3f649..11cf489 100644 --- a/GlitchyEngine/src/Renderer/Texture.bf +++ b/GlitchyEngine/src/Renderer/Texture.bf @@ -46,6 +46,15 @@ namespace GlitchyEngine.Renderer } } + public struct Texture2DDesc + { + public uint32 Width; + public uint32 Height; + public uint32 ArraySize; + public uint32 MipLevels; + public Format Format; + } + public class Texture2D : Texture { protected String _path ~ delete _; @@ -55,6 +64,8 @@ namespace GlitchyEngine.Renderer public override uint32 Depth => 1; public override extern uint32 ArraySize {get;} public override extern uint32 MipLevels {get;} + + protected this(GraphicsContext context) : base(context) {} public this(GraphicsContext context, String path) : base(context) { @@ -63,5 +74,7 @@ namespace GlitchyEngine.Renderer } protected extern void LoadTexturePlatform(); + + protected extern void CreateTexturePlatform(Texture2DDesc desc, void* data, uint32 linePitch); } }