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
@@ -0,0 +1 @@
{_recentScenes=["Assets\\Scenes\\physics2D.scene"]}
@@ -60,8 +60,37 @@ class MaterialAssetPropertiesEditor : AssetPropertiesEditor
if (material == null)
return;
ImGui.TextUnformatted("Effect: ");
ImGui.SameLine();
Effect effect = material?.Effect;
StringView effectName = (effect?.Identifier ?? "(None)");
ImGui.Button(effectName.ToScopeCStr!());
ImGui.AttachTooltip(effectName);
// Effect drop target
if (ImGui.BeginDragDropTarget())
{
ImGui.Payload* payload = ImGui.AcceptDragDropPayload(.ContentBrowserItem);
if (payload != null)
{
StringView path = .((char8*)payload.Data, (int)payload.DataSize);
AssetHandle<Effect> newEffect = Content.LoadAsset(path);
material.Effect = newEffect;
//newTexture.Get().SamplerState = SamplerStateManager.AnisotropicWrap;
//material.SetTexture(texture.key, newTexture.Cast<Texture>());
}
ImGui.EndDragDropTarget();
}
if (effect == null)
return;
-1
View File
@@ -70,7 +70,6 @@ class ProjectUserSettings
public void RestoreDefaults()
{
//_lastOpenedScene?.Clear();
if (_recentScenes != null)
ClearAndDeleteItems!(_recentScenes);
+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;
}
}
}
}
+72 -11
View File
@@ -14,31 +14,62 @@ 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 this(Effect effect)
public Effect Effect
{
_effect = effect..AddRef();
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, entry) in _effect.Textures)
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...
/*var texture = entry.BoundTexture;*/
_textures.Add(name, (AssetHandle<Texture>.Invalid, entry.TextureDimension));
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;
}
/** @brief Initializes the raw data array for the variables.
@@ -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;
}
/**
@@ -50,6 +50,8 @@ namespace GlitchyEngine.Renderer
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);