mirror of
https://github.com/aharabada/glitchy-engine-beef.git
synced 2026-09-05 21:01:52 +00:00
Added support for Cube Textures, Improved dds import (and textures in general), Texture refactoring and DX11 DeviceContext synchronization
This commit is contained in:
@@ -104,6 +104,18 @@ public class EffectLibrary
|
||||
public bool Exists(String effectName) => _effects.ContainsKey(effectName);
|
||||
}*/
|
||||
|
||||
public enum TextureDimension
|
||||
{
|
||||
Unknown,
|
||||
Texture1D,
|
||||
Texture1DArray,
|
||||
Texture2D,
|
||||
Texture2DArray,
|
||||
Texture3D,
|
||||
TextureCube,
|
||||
TextureCubeArray
|
||||
}
|
||||
|
||||
public class Effect : Asset
|
||||
{
|
||||
internal VertexShader _vs ~ _?.ReleaseRef();
|
||||
@@ -137,14 +149,16 @@ public class Effect : Asset
|
||||
public struct TextureEntry
|
||||
{
|
||||
public TextureViewBinding BoundTexture;
|
||||
public TextureDimension TextureDimension;
|
||||
public ShaderTextureCollection.ResourceEntry* VsSlot;
|
||||
public ShaderTextureCollection.ResourceEntry* PsSlot;
|
||||
|
||||
public this(TextureViewBinding boundTexture, ShaderTextureCollection.ResourceEntry* vsSlot, ShaderTextureCollection.ResourceEntry* psSlot)
|
||||
public this(TextureViewBinding boundTexture, TextureDimension textureDimension, ShaderTextureCollection.ResourceEntry* vsSlot, ShaderTextureCollection.ResourceEntry* psSlot)
|
||||
{
|
||||
BoundTexture = boundTexture;
|
||||
VsSlot = vsSlot;
|
||||
PsSlot = psSlot;
|
||||
TextureDimension = textureDimension;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -741,7 +755,7 @@ public class Effect : Asset
|
||||
// Get existing entry or create new
|
||||
if(!_textures.TryGetValue(shaderEntry.Name, out entry))
|
||||
{
|
||||
entry = .(shaderEntry.BoundTexture, null, null);
|
||||
entry = .(shaderEntry.BoundTexture, shaderEntry.Dimension, null, null);
|
||||
entry.BoundTexture.AddRef();
|
||||
}
|
||||
|
||||
|
||||
@@ -14,7 +14,7 @@ public class Material : Asset
|
||||
|
||||
private uint8[] _rawVariables ~ delete _;
|
||||
|
||||
private Dictionary<String, AssetHandle<Texture>> _textures = new .() ~ delete _;
|
||||
private Dictionary<String, (AssetHandle<Texture> Handle, TextureDimension Dimension)> _textures = new .() ~ delete _;
|
||||
|
||||
private Dictionary<String, (uint32 Offset, BufferVariable Variable)> _variables = new .() ~ delete _;
|
||||
|
||||
@@ -34,7 +34,7 @@ public class Material : Asset
|
||||
// At best whole paths. Shouldn't be that hard to do...
|
||||
/*var texture = entry.BoundTexture;*/
|
||||
|
||||
_textures.Add(name, .Invalid);
|
||||
_textures.Add(name, (AssetHandle<Texture>.Invalid, entry.TextureDimension));
|
||||
}
|
||||
|
||||
InitRawData();
|
||||
@@ -66,7 +66,19 @@ public class Material : Asset
|
||||
|
||||
for(let (name, texture) in _textures)
|
||||
{
|
||||
_effect.SetTexture(name, texture);
|
||||
switch (texture.Dimension)
|
||||
{
|
||||
//case .Texture1D, .Texture1DArray:
|
||||
case .Texture2D, .Texture2DArray:
|
||||
AssetHandle<Texture2D> handle2D = .(texture.Handle);
|
||||
_effect.SetTexture(name, handle2D);
|
||||
case .TextureCube, .TextureCubeArray:
|
||||
AssetHandle<TextureCube> cubeHandle = .(texture.Handle);
|
||||
_effect.SetTexture(name, cubeHandle);
|
||||
//case .Texture3D:
|
||||
default:
|
||||
Log.EngineLogger.Error("Tryied to bind undefined texture dimension!");
|
||||
}
|
||||
}
|
||||
|
||||
for(let (name, variable) in _variables)
|
||||
@@ -86,13 +98,27 @@ public class Material : Asset
|
||||
{
|
||||
if(_textures.TryGetValue(name, var entry))
|
||||
{
|
||||
/*switch (texture.Get().GetType())
|
||||
{
|
||||
//case .Texture1D:
|
||||
// _textures[name].Dimension = .Texture1D;
|
||||
case typeof(Texture2D):
|
||||
_textures[name].Dimension = .Texture2D;
|
||||
case typeof(TextureCube):
|
||||
_textures[name].Dimension = .TextureCube;
|
||||
//case .Texture3D:
|
||||
// _textures[name].Dimension = .Texture3D;
|
||||
default:
|
||||
_textures[name].Dimension = .Unknown;
|
||||
}*/
|
||||
//entry?.ReleaseRef();
|
||||
_textures[name] = texture;
|
||||
_textures[name].Handle = texture;
|
||||
//texture?.AddRef();
|
||||
}
|
||||
else
|
||||
{
|
||||
Log.EngineLogger.Assert(false);
|
||||
Log.EngineLogger.Error($"Material doesn't have the texture slot \"{name}\"");
|
||||
//Log.EngineLogger.Assert(false);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -79,15 +79,6 @@ namespace GlitchyEngine.Renderer
|
||||
|
||||
protected extern TextureViewBinding PlatformGetViewBinding();
|
||||
|
||||
protected internal override void SneakySwappyTexture(Texture otherTexture)
|
||||
{
|
||||
Log.EngineLogger.AssertDebug(otherTexture is RenderTarget2D, "Swapping texture must be a RenderTarget2D!");
|
||||
|
||||
SamplerState = otherTexture.SamplerState;
|
||||
|
||||
PlatformSneakySwappyTexture(otherTexture as RenderTarget2D);
|
||||
}
|
||||
|
||||
protected extern void PlatformSneakySwappyTexture(RenderTarget2D otherTexture);
|
||||
}
|
||||
|
||||
|
||||
@@ -3,9 +3,9 @@ using System.Collections;
|
||||
|
||||
namespace GlitchyEngine.Renderer
|
||||
{
|
||||
public class ShaderTextureCollection : IEnumerable<(String Name, uint32 Index, TextureViewBinding BoundTexture)>
|
||||
public class ShaderTextureCollection : IEnumerable<(String Name, uint32 Index, TextureViewBinding BoundTexture, TextureDimension Dimension)>
|
||||
{
|
||||
public typealias ResourceEntry = (String Name, uint32 Index, TextureViewBinding BoundTexture);
|
||||
public typealias ResourceEntry = (String Name, uint32 Index, TextureViewBinding BoundTexture, TextureDimension Dimension);
|
||||
|
||||
List<ResourceEntry> _textures ~ DeleteTextureEntries!(_);
|
||||
|
||||
@@ -39,14 +39,14 @@ namespace GlitchyEngine.Renderer
|
||||
|
||||
// TODO: finish implementation (like BufferCollection)
|
||||
|
||||
public void Add(String name, uint32 index, TextureViewBinding texture)
|
||||
public void Add(String name, uint32 index, TextureViewBinding texture, TextureDimension dimension)
|
||||
{
|
||||
Add((name, index, texture));
|
||||
Add((name, index, texture, dimension));
|
||||
}
|
||||
|
||||
public void Add(ResourceEntry entry)
|
||||
{
|
||||
ResourceEntry copy = (new String(entry.Name), entry.Index, entry.BoundTexture);
|
||||
ResourceEntry copy = (new String(entry.Name), entry.Index, entry.BoundTexture, entry.Dimension);
|
||||
entry.BoundTexture.AddRef();
|
||||
|
||||
_textures.Add(copy);
|
||||
|
||||
@@ -30,11 +30,6 @@ namespace GlitchyEngine.Renderer
|
||||
public abstract uint32 MipLevels {get;}
|
||||
|
||||
public abstract TextureViewBinding GetViewBinding();
|
||||
|
||||
/// Very dirtily swaps the internals with the given texture.
|
||||
/// TODO: Please do this differently!!!!!!!!!!!!!!!!!!!!!!
|
||||
/// This is for texture hot reloading POC, I know... it's bad...
|
||||
protected internal abstract void SneakySwappyTexture(Texture otherTexture);
|
||||
}
|
||||
|
||||
public struct Texture2DDesc
|
||||
@@ -69,17 +64,6 @@ namespace GlitchyEngine.Renderer
|
||||
//public override extern uint32 ArraySize {get;}
|
||||
//public override extern uint32 MipLevels {get;}
|
||||
|
||||
// TODO: remove
|
||||
private this(Stream data)
|
||||
{
|
||||
LoadDds(data);
|
||||
}
|
||||
|
||||
protected void LoadDds(Stream stream)
|
||||
{
|
||||
LoadDdsPlatform(stream);
|
||||
}
|
||||
|
||||
public this(Texture2DDesc desc)
|
||||
{
|
||||
PrepareTexturePlatform(desc, false);
|
||||
@@ -100,8 +84,6 @@ namespace GlitchyEngine.Renderer
|
||||
uint32 mipLevels = 1, uint32 arraySize = 1, Usage usage = .Default, CPUAccessFlags cpuAccess = .None
|
||||
*/
|
||||
|
||||
protected extern void LoadDdsPlatform(Stream stream);
|
||||
|
||||
protected extern void CreateTexturePlatform(Texture2DDesc desc, bool isRenderTarget, void* data, uint32 linePitch);
|
||||
|
||||
/**
|
||||
@@ -130,17 +112,16 @@ namespace GlitchyEngine.Renderer
|
||||
}
|
||||
|
||||
protected extern TextureViewBinding PlatformGetViewBinding();
|
||||
}
|
||||
|
||||
protected internal override void SneakySwappyTexture(Texture otherTexture)
|
||||
{
|
||||
Log.EngineLogger.AssertDebug(otherTexture is Texture2D, "Swapping texture must be a Texture2D!");
|
||||
|
||||
SamplerState = otherTexture.SamplerState;
|
||||
|
||||
PlatformSneakySwappyTexture(otherTexture as Texture2D);
|
||||
}
|
||||
|
||||
protected extern void PlatformSneakySwappyTexture(Texture2D otherTexture);
|
||||
public enum TextureCubeFace
|
||||
{
|
||||
PositiveX = 0,
|
||||
NegativeX = 1,
|
||||
PositiveY = 2,
|
||||
NegativeY = 3,
|
||||
PositiveZ = 4,
|
||||
NegativeZ = 5,
|
||||
}
|
||||
|
||||
public class TextureCube : Texture
|
||||
@@ -152,24 +133,48 @@ namespace GlitchyEngine.Renderer
|
||||
public override uint32 Depth => 1;
|
||||
// public override extern uint32 ArraySize {get;}
|
||||
// public override extern uint32 MipLevels {get;}
|
||||
|
||||
public this(String path)
|
||||
|
||||
public this(Texture2DDesc desc)
|
||||
{
|
||||
this._path = new String(path);
|
||||
LoadTexture();
|
||||
PrepareTexturePlatform(desc, false);
|
||||
}
|
||||
|
||||
private void LoadTexture()
|
||||
public void SetData(void* data, int elementSize, TextureCubeFace cubeFace, uint32 arraySlice = 0, uint32 mipSlice = 0)
|
||||
{
|
||||
Debug.Profiler.ProfileResourceFunction!();
|
||||
|
||||
Stream data = Application.Get().ContentManager.GetStream(_path);
|
||||
defer delete data;
|
||||
|
||||
LoadTexturePlatform(data);
|
||||
PlatformSetData(data, (.)elementSize, 0, 0, Width, Height, arraySlice * 6 + (uint32)cubeFace, mipSlice, .Write);
|
||||
}
|
||||
|
||||
protected extern void LoadTexturePlatform(Stream stream);
|
||||
public void SetData<T>(T* data, TextureCubeFace cubeFace, uint32 arraySlice = 0, uint32 mipSlice = 0)
|
||||
{
|
||||
PlatformSetData(data, (.)sizeof(T), 0, 0, Width, Height, arraySlice * 6 + (uint32)cubeFace, mipSlice, .Write);
|
||||
}
|
||||
|
||||
public void SetData<T>(T* data, uint32 x, uint32 y, uint32 width, uint32 height, TextureCubeFace cubeFace, uint32 arraySlice = 0, uint32 mipSlice = 0)
|
||||
{
|
||||
PlatformSetData(data, (.)sizeof(T), x, y, width, height, arraySlice * 6 + (uint32)cubeFace, mipSlice, .Write);
|
||||
}
|
||||
|
||||
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.
|
||||
*/
|
||||
protected extern void PrepareTexturePlatform(Texture2DDesc desc, bool isRenderTarget);
|
||||
|
||||
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);
|
||||
|
||||
/**
|
||||
* Copies the texel-data of this texture to the given destination texture.
|
||||
*/
|
||||
public static extern void CopySubresourceRegion(Texture2D source, Texture2D destination,
|
||||
ResourceBox sourceBox = default, uint32 destX = 0, uint32 destY = 0,
|
||||
uint32 srcArraySlice = 0, uint32 srcMipSlice = 0, uint32 destArraySlice = 0, uint32 destMipSlice = 0);
|
||||
|
||||
/**
|
||||
* Copies the data to the given destination.
|
||||
*/
|
||||
//public extern void CopyTo(TextureCube destination);
|
||||
|
||||
public override TextureViewBinding GetViewBinding()
|
||||
{
|
||||
@@ -177,10 +182,5 @@ namespace GlitchyEngine.Renderer
|
||||
}
|
||||
|
||||
protected extern TextureViewBinding PlatformGetViewBinding();
|
||||
|
||||
protected internal override void SneakySwappyTexture(Texture otherTexture)
|
||||
{
|
||||
Runtime.NotImplemented();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user