Allow changing Effect of Materials

+ TextureDimension Property for Texture
This commit is contained in:
Simon Lübeß
2023-08-01 13:28:04 +02:00
parent 5d8c1b111a
commit e8a5594540
7 changed files with 142 additions and 27 deletions
+21 -4
View File
@@ -104,10 +104,27 @@ namespace GlitchyEngine.Renderer
public enum ShaderVariableType
{
Bool,
Float,
Int,
UInt
case Bool;
case Float;
case Int;
case UInt;
// todo
public int ElementSizeInBytes()
{
switch (this)
{
case .Bool:
return 1;
case .Float:
return 4;
case .Int:
return 4;
case .UInt:
return 4;
default:
return 0;
}
}
}
}
+82 -21
View File
@@ -14,30 +14,61 @@ public class Material : Asset
private uint8[] _rawVariables ~ delete _;
private Dictionary<String, (AssetHandle<Texture> Handle, TextureDimension Dimension)> _textures = new .() ~ delete _;
private Dictionary<String, (AssetHandle<Texture> Handle, TextureDimension Dimension)> _textures = new .() ~ DeleteDictionaryAndKeys!(_);
private Dictionary<String, (uint32 Offset, BufferVariable Variable)> _variables = new .() ~ delete _;
private Dictionary<String, (uint32 Offset, BufferVariable Variable)> _variables = new .() ~ DeleteDictionaryAndKeys!(_);
public Effect Effect => _effect;
public Effect Effect
{
get => _effect;
set
{
SetReference!(_effect, value);
if (_effect == null)
return;
// TODO: get variables from effect
decltype(_textures) newTextures = new .();
// Get texture slots from effect
for(let (name, effectTexture) in _effect.Textures)
{
// TODO: We need to be able to define default textures in the shader.
// At least things like "Black", "White", "Normal"
// At best whole paths. Shouldn't be that hard to do...
AssetHandle<Texture> textureHandle = .Invalid;
if (_textures.TryGetValue(name, let oldMaterialTexture))
{
textureHandle = oldMaterialTexture.Handle;
}
if (textureHandle.IsValid)
{
if (textureHandle.Dimension != effectTexture.TextureDimension)
textureHandle = .Invalid;
}
_textures[new String(name)] = (textureHandle, effectTexture.TextureDimension);
}
DeleteDictionaryAndKeys!(_textures);
_textures = newTextures;
InitRawData();
}
}
public this()
{
}
public this(Effect effect)
{
_effect = effect..AddRef();
// TODO: get variables from effect
// Get texture slots from effect
for(let (name, entry) in _effect.Textures)
{
// TODO: We need to be able to define default textures in the shader.
// At least things like "Black", "White", "Normal"
// At best whole paths. Shouldn't be that hard to do...
/*var texture = entry.BoundTexture;*/
_textures.Add(name, (AssetHandle<Texture>.Invalid, entry.TextureDimension));
}
InitRawData();
Effect = effect;
}
@@ -47,14 +78,44 @@ public class Material : Asset
{
uint32 bufferSize = 0;
decltype(_variables) newVariables = new Dictionary<String, (uint32 Offset, BufferVariable Variable)>();
for(let variable in _effect.Variables)
{
_variables.Add(variable.Name, (bufferSize, variable));
newVariables.Add(new .(variable.Name), (bufferSize, variable));
bufferSize += variable._sizeInBytes;
}
_rawVariables = new uint8[bufferSize];
uint8[] newData = new uint8[bufferSize];
for (let (newKey, newValue) in newVariables)
{
if (_variables.TryGetValue(newKey, let oldEntry))
{
if (oldEntry.Variable.Type == newValue.Variable.Type)
{
int elementSize = newValue.Variable.Type.ElementSizeInBytes();
for (int r = 0; r < Math.Min(oldEntry.Variable.Rows, newValue.Variable.Rows); r++)
for (int c = 0; c < Math.Min(oldEntry.Variable.Columns, newValue.Variable.Columns); c++)
{
int oldElementIndex = r * oldEntry.Variable.Columns + c;
int newElementIndex = r * newValue.Variable.Columns + c;
Internal.MemCpy(newData.Ptr + (newValue.Offset + elementSize * oldElementIndex),
_rawVariables.Ptr + (oldEntry.Offset + elementSize * newElementIndex), elementSize);
}
}
// TODO: we could try to convert e.g. Int <-> Float
}
}
delete _rawVariables;
_rawVariables = newData;
DeleteDictionaryAndKeys!(_variables);
_variables = newVariables;
}
/**
+3 -1
View File
@@ -49,7 +49,9 @@ namespace GlitchyEngine.Renderer
public override uint32 ArraySize => _description.ArraySize;
public override uint32 MipLevels => _description.MipLevels;
public override Format Format => _description.PixelFormat;
public override TextureDimension Dimension => .Texture2D;
protected internal DepthStencilTarget _depthStenilTarget ~ _?.ReleaseRef();
// TODO: DepthStencilTarget is just a renderTarget
+6
View File
@@ -31,6 +31,8 @@ namespace GlitchyEngine.Renderer
public abstract Format Format {get;}
public abstract TextureDimension Dimension {get;}
public abstract TextureViewBinding GetViewBinding();
}
@@ -82,6 +84,8 @@ namespace GlitchyEngine.Renderer
//public override extern uint32 ArraySize {get;}
//public override extern uint32 MipLevels {get;}
public override TextureDimension Dimension => .Texture2D;
public this(Texture2DDesc desc)
{
PrepareTexturePlatform(desc, false);
@@ -163,6 +167,8 @@ namespace GlitchyEngine.Renderer
// public override extern uint32 ArraySize {get;}
// public override extern uint32 MipLevels {get;}
public override TextureDimension Dimension => .TextureCube;
public this(Texture2DDesc desc)
{
PrepareTexturePlatform(desc, false);