From ff0ac74d12c6c212d2fa8fa9ec1b731df30ca381 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20L=C3=BCbe=C3=9F?= Date: Mon, 3 Jun 2024 00:59:01 +0200 Subject: [PATCH] Moved texture loader from editor to engine --- .../src/Assets/Importers/DdsImporter.bf | 11 +-- .../src/Assets/Importers/Interfaces.bf | 5 - .../src/Assets/Importers/TextureImporter.bf | 93 +---------------- .../src/Content/IProcessedAssetLoader.bf | 9 ++ GlitchyEngine/src/Content/TextureLoader.bf | 99 +++++++++++++++++++ GlitchyEngine/src/Renderer/Effect.bf | 4 +- 6 files changed, 113 insertions(+), 108 deletions(-) create mode 100644 GlitchyEngine/src/Content/IProcessedAssetLoader.bf create mode 100644 GlitchyEngine/src/Content/TextureLoader.bf diff --git a/GlitchyEditor/src/Assets/Importers/DdsImporter.bf b/GlitchyEditor/src/Assets/Importers/DdsImporter.bf index 53efeed..9235dc4 100644 --- a/GlitchyEditor/src/Assets/Importers/DdsImporter.bf +++ b/GlitchyEditor/src/Assets/Importers/DdsImporter.bf @@ -2,6 +2,7 @@ using System.IO; using System; using GlitchyEngine; using System.Collections; +using GlitchyEngine.Renderer; namespace GlitchyEditor.Assets.Importers; @@ -24,19 +25,11 @@ public struct LoadedSurface public struct LoadedTextureInfo { - public enum Dimension - { - Unknown, - Texture1D, - Texture2D, - Texture3D - } - public uint8[] PixelData = null; public DirectX.DXGI.Format PixelFormat = .Unknown; public int MipMapCount = 0; public int ArraySize = 0; - public Dimension Dimension = .Unknown; + public TextureDimension Dimension = .Unknown; public bool IsCubeMap; public int Width; diff --git a/GlitchyEditor/src/Assets/Importers/Interfaces.bf b/GlitchyEditor/src/Assets/Importers/Interfaces.bf index b82e490..6b9af64 100644 --- a/GlitchyEditor/src/Assets/Importers/Interfaces.bf +++ b/GlitchyEditor/src/Assets/Importers/Interfaces.bf @@ -27,8 +27,3 @@ interface IAssetExporter Result Export(Stream stream, ProcessedResource processedObject, AssetExporterConfig config); } - -interface IProcessedAssetLoader -{ - Result Load(Stream stream); -} diff --git a/GlitchyEditor/src/Assets/Importers/TextureImporter.bf b/GlitchyEditor/src/Assets/Importers/TextureImporter.bf index d3bb422..49ecd44 100644 --- a/GlitchyEditor/src/Assets/Importers/TextureImporter.bf +++ b/GlitchyEditor/src/Assets/Importers/TextureImporter.bf @@ -271,7 +271,7 @@ class ProcessedTexture : ProcessedResource public Format PixelFormat = .Unknown; public int MipMapCount = -1; public int ArraySize = -1; - public Dimension Dimension = .Unknown; + public TextureDimension Dimension = .Unknown; public bool IsCubeMap; public int Width = -1; @@ -661,94 +661,3 @@ class TextureExporter : IAssetExporter } } -class TextureLoader : IProcessedAssetLoader -{ - public Result Load(Stream dataStream) - { - Dimension dimension = Try!(dataStream.Read()); - Format pixelFormat = Try!(dataStream.Read()); - uint32 width = Try!(dataStream.Read()); - uint32 height = Try!(dataStream.Read()); - uint32 depth = Try!(dataStream.Read()); - uint32 arraySize = Try!(dataStream.Read()); - uint32 mipMapCount = Try!(dataStream.Read()); - bool isCubemap = Try!(dataStream.Read()); - - SamplerStateDescription sampler = Try!(ReadSampler(dataStream)); - - List surfaceDatas = scope .(mipMapCount * arraySize); - defer { ClearAndDeleteItems!(surfaceDatas); } - - TextureSliceData[] slices = scope TextureSliceData[mipMapCount * arraySize]; - - int index = 0; - for (int arraySlice < arraySize) - { - for (int mipSlice < mipMapCount) - { - uint32 linePitch = Try!(dataStream.Read()); - uint32 slicePitch = Try!(dataStream.Read()); - uint64 byteCount = Try!(dataStream.Read()); - - uint8[] surfaceData = new uint8[byteCount]; - Try!(dataStream.TryRead(surfaceData)); - slices[index] = .(surfaceData.Ptr, linePitch, slicePitch); - - index++; - - surfaceDatas.Add(surfaceData); - } - } - - Texture result = null; - - switch (dimension) - { - case .Texture2D: - if (isCubemap) - { - Runtime.NotImplemented(); - } - else - { - Texture2DDesc desc = .(width, height, pixelFormat, arraySize, mipMapCount, .Immutable, .None); - - Texture2D texture = new Texture2D(desc); - - texture.SetData(slices); - - result = texture; - } - default: - Runtime.NotImplemented(); - } - - using (SamplerState samplerState = SamplerStateManager.GetSampler(sampler)) - { - result.SamplerState = samplerState; - } - - return result; - } - - private Result ReadSampler(Stream dataStream) - { - SamplerStateDescription sampler; - - sampler.MinFilter = Try!(dataStream.Read()); - sampler.MagFilter = Try!(dataStream.Read()); - sampler.MipFilter = Try!(dataStream.Read()); - sampler.FilterMode = Try!(dataStream.Read()); - sampler.ComparisonFunction = Try!(dataStream.Read()); - sampler.AddressModeU = Try!(dataStream.Read()); - sampler.AddressModeV = Try!(dataStream.Read()); - sampler.AddressModeW = Try!(dataStream.Read()); - sampler.MipLODBias = Try!(dataStream.Read()); - sampler.MipMinLOD = Try!(dataStream.Read()); - sampler.MipMaxLOD = Try!(dataStream.Read()); - sampler.MaxAnisotropy = Try!(dataStream.Read()); - sampler.BorderColor = Try!(dataStream.Read()); - - return sampler; - } -} diff --git a/GlitchyEngine/src/Content/IProcessedAssetLoader.bf b/GlitchyEngine/src/Content/IProcessedAssetLoader.bf new file mode 100644 index 0000000..cb2f168 --- /dev/null +++ b/GlitchyEngine/src/Content/IProcessedAssetLoader.bf @@ -0,0 +1,9 @@ +using System; +using System.IO; + +namespace GlitchyEngine.Content; + +interface IProcessedAssetLoader +{ + Result Load(Stream stream); +} diff --git a/GlitchyEngine/src/Content/TextureLoader.bf b/GlitchyEngine/src/Content/TextureLoader.bf new file mode 100644 index 0000000..afc44b3 --- /dev/null +++ b/GlitchyEngine/src/Content/TextureLoader.bf @@ -0,0 +1,99 @@ +using System; +using System.IO; +using System.Collections; +using GlitchyEngine.Math; +using GlitchyEngine.Renderer; + +namespace GlitchyEngine.Content; + +class TextureLoader : IProcessedAssetLoader +{ + public Result Load(Stream dataStream) + { + TextureDimension dimension = Try!(dataStream.Read()); + Format pixelFormat = Try!(dataStream.Read()); + uint32 width = Try!(dataStream.Read()); + uint32 height = Try!(dataStream.Read()); + uint32 depth = Try!(dataStream.Read()); + uint32 arraySize = Try!(dataStream.Read()); + uint32 mipMapCount = Try!(dataStream.Read()); + bool isCubemap = Try!(dataStream.Read()); + + SamplerStateDescription sampler = Try!(ReadSampler(dataStream)); + + List surfaceDatas = scope .(mipMapCount * arraySize); + defer { ClearAndDeleteItems!(surfaceDatas); } + + TextureSliceData[] slices = scope TextureSliceData[mipMapCount * arraySize]; + + int index = 0; + for (int arraySlice < arraySize) + { + for (int mipSlice < mipMapCount) + { + uint32 linePitch = Try!(dataStream.Read()); + uint32 slicePitch = Try!(dataStream.Read()); + uint64 byteCount = Try!(dataStream.Read()); + + uint8[] surfaceData = new uint8[byteCount]; + Try!(dataStream.TryRead(surfaceData)); + slices[index] = .(surfaceData.Ptr, linePitch, slicePitch); + + index++; + + surfaceDatas.Add(surfaceData); + } + } + + Texture result = null; + + switch (dimension) + { + case .Texture2D: + if (isCubemap) + { + Runtime.NotImplemented(); + } + else + { + Texture2DDesc desc = .(width, height, pixelFormat, arraySize, mipMapCount, .Immutable, .None); + + Texture2D texture = new Texture2D(desc); + + texture.SetData(slices); + + result = texture; + } + default: + Runtime.NotImplemented(); + } + + using (SamplerState samplerState = SamplerStateManager.GetSampler(sampler)) + { + result.SamplerState = samplerState; + } + + return result; + } + + private Result ReadSampler(Stream dataStream) + { + SamplerStateDescription sampler; + + sampler.MinFilter = Try!(dataStream.Read()); + sampler.MagFilter = Try!(dataStream.Read()); + sampler.MipFilter = Try!(dataStream.Read()); + sampler.FilterMode = Try!(dataStream.Read()); + sampler.ComparisonFunction = Try!(dataStream.Read()); + sampler.AddressModeU = Try!(dataStream.Read()); + sampler.AddressModeV = Try!(dataStream.Read()); + sampler.AddressModeW = Try!(dataStream.Read()); + sampler.MipLODBias = Try!(dataStream.Read()); + sampler.MipMinLOD = Try!(dataStream.Read()); + sampler.MipMaxLOD = Try!(dataStream.Read()); + sampler.MaxAnisotropy = Try!(dataStream.Read()); + sampler.BorderColor = Try!(dataStream.Read()); + + return sampler; + } +} diff --git a/GlitchyEngine/src/Renderer/Effect.bf b/GlitchyEngine/src/Renderer/Effect.bf index fea27fb..a4f1748 100644 --- a/GlitchyEngine/src/Renderer/Effect.bf +++ b/GlitchyEngine/src/Renderer/Effect.bf @@ -108,11 +108,11 @@ public enum TextureDimension { Unknown, Texture1D, - Texture1DArray, Texture2D, - Texture2DArray, Texture3D, TextureCube, + Texture1DArray, + Texture2DArray, TextureCubeArray }