mirror of
https://github.com/aharabada/glitchy-engine-beef.git
synced 2026-09-05 13:01:52 +00:00
Moved texture loader from editor to engine
This commit is contained in:
@@ -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;
|
||||
|
||||
@@ -27,8 +27,3 @@ interface IAssetExporter
|
||||
|
||||
Result<void> Export(Stream stream, ProcessedResource processedObject, AssetExporterConfig config);
|
||||
}
|
||||
|
||||
interface IProcessedAssetLoader
|
||||
{
|
||||
Result<Asset> Load(Stream stream);
|
||||
}
|
||||
|
||||
@@ -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<Asset> Load(Stream dataStream)
|
||||
{
|
||||
Dimension dimension = Try!(dataStream.Read<Dimension>());
|
||||
Format pixelFormat = Try!(dataStream.Read<Format>());
|
||||
uint32 width = Try!(dataStream.Read<uint32>());
|
||||
uint32 height = Try!(dataStream.Read<uint32>());
|
||||
uint32 depth = Try!(dataStream.Read<uint32>());
|
||||
uint32 arraySize = Try!(dataStream.Read<uint32>());
|
||||
uint32 mipMapCount = Try!(dataStream.Read<uint32>());
|
||||
bool isCubemap = Try!(dataStream.Read<bool>());
|
||||
|
||||
SamplerStateDescription sampler = Try!(ReadSampler(dataStream));
|
||||
|
||||
List<uint8[]> 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>());
|
||||
uint32 slicePitch = Try!(dataStream.Read<uint32>());
|
||||
uint64 byteCount = Try!(dataStream.Read<uint64>());
|
||||
|
||||
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<SamplerStateDescription> ReadSampler(Stream dataStream)
|
||||
{
|
||||
SamplerStateDescription sampler;
|
||||
|
||||
sampler.MinFilter = Try!(dataStream.Read<FilterFunction>());
|
||||
sampler.MagFilter = Try!(dataStream.Read<FilterFunction>());
|
||||
sampler.MipFilter = Try!(dataStream.Read<FilterFunction>());
|
||||
sampler.FilterMode = Try!(dataStream.Read<FilterMode>());
|
||||
sampler.ComparisonFunction = Try!(dataStream.Read<ComparisonFunction>());
|
||||
sampler.AddressModeU = Try!(dataStream.Read<TextureAddressMode>());
|
||||
sampler.AddressModeV = Try!(dataStream.Read<TextureAddressMode>());
|
||||
sampler.AddressModeW = Try!(dataStream.Read<TextureAddressMode>());
|
||||
sampler.MipLODBias = Try!(dataStream.Read<float>());
|
||||
sampler.MipMinLOD = Try!(dataStream.Read<float>());
|
||||
sampler.MipMaxLOD = Try!(dataStream.Read<float>());
|
||||
sampler.MaxAnisotropy = Try!(dataStream.Read<uint8>());
|
||||
sampler.BorderColor = Try!(dataStream.Read<ColorRGBA>());
|
||||
|
||||
return sampler;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
|
||||
namespace GlitchyEngine.Content;
|
||||
|
||||
interface IProcessedAssetLoader
|
||||
{
|
||||
Result<Asset> Load(Stream stream);
|
||||
}
|
||||
@@ -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<Asset> Load(Stream dataStream)
|
||||
{
|
||||
TextureDimension dimension = Try!(dataStream.Read<TextureDimension>());
|
||||
Format pixelFormat = Try!(dataStream.Read<Format>());
|
||||
uint32 width = Try!(dataStream.Read<uint32>());
|
||||
uint32 height = Try!(dataStream.Read<uint32>());
|
||||
uint32 depth = Try!(dataStream.Read<uint32>());
|
||||
uint32 arraySize = Try!(dataStream.Read<uint32>());
|
||||
uint32 mipMapCount = Try!(dataStream.Read<uint32>());
|
||||
bool isCubemap = Try!(dataStream.Read<bool>());
|
||||
|
||||
SamplerStateDescription sampler = Try!(ReadSampler(dataStream));
|
||||
|
||||
List<uint8[]> 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>());
|
||||
uint32 slicePitch = Try!(dataStream.Read<uint32>());
|
||||
uint64 byteCount = Try!(dataStream.Read<uint64>());
|
||||
|
||||
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<SamplerStateDescription> ReadSampler(Stream dataStream)
|
||||
{
|
||||
SamplerStateDescription sampler;
|
||||
|
||||
sampler.MinFilter = Try!(dataStream.Read<FilterFunction>());
|
||||
sampler.MagFilter = Try!(dataStream.Read<FilterFunction>());
|
||||
sampler.MipFilter = Try!(dataStream.Read<FilterFunction>());
|
||||
sampler.FilterMode = Try!(dataStream.Read<FilterMode>());
|
||||
sampler.ComparisonFunction = Try!(dataStream.Read<ComparisonFunction>());
|
||||
sampler.AddressModeU = Try!(dataStream.Read<TextureAddressMode>());
|
||||
sampler.AddressModeV = Try!(dataStream.Read<TextureAddressMode>());
|
||||
sampler.AddressModeW = Try!(dataStream.Read<TextureAddressMode>());
|
||||
sampler.MipLODBias = Try!(dataStream.Read<float>());
|
||||
sampler.MipMinLOD = Try!(dataStream.Read<float>());
|
||||
sampler.MipMaxLOD = Try!(dataStream.Read<float>());
|
||||
sampler.MaxAnisotropy = Try!(dataStream.Read<uint8>());
|
||||
sampler.BorderColor = Try!(dataStream.Read<ColorRGBA>());
|
||||
|
||||
return sampler;
|
||||
}
|
||||
}
|
||||
@@ -108,11 +108,11 @@ public enum TextureDimension
|
||||
{
|
||||
Unknown,
|
||||
Texture1D,
|
||||
Texture1DArray,
|
||||
Texture2D,
|
||||
Texture2DArray,
|
||||
Texture3D,
|
||||
TextureCube,
|
||||
Texture1DArray,
|
||||
Texture2DArray,
|
||||
TextureCubeArray
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user