Interact with Materials from scripts

This commit is contained in:
Simon Lübeß
2025-02-14 11:54:07 +01:00
parent e5437c6ff6
commit aaed4199d3
24 changed files with 739 additions and 64 deletions
@@ -109,6 +109,26 @@ namespace GlitchyEngine.Renderer
#endif
}
public enum TypematchResult
{
Ok,
MatrixDimensionMismatch,
ElementTypeMismatch,
}
public TypematchResult CheckTypematch(int rows, int cols, ShaderVariableType type)
{
Debug.Profiler.ProfileRendererFunction!();
if (rows != _rows || cols != _columns)
return .MatrixDimensionMismatch;
if (type != _elementType)
return .ElementTypeMismatch;
return .Ok;
}
public void EnsureTypeMatch<T>()
{
switch(typeof(T))
+62 -25
View File
@@ -196,7 +196,7 @@ public class Material : Asset
}
}
private mixin SetVariable(String name, var value)
private mixin SetVariable(StringView name, var value)
{
if(_variables.TryGetValue(name, let entry))
{
@@ -204,35 +204,72 @@ public class Material : Asset
}
}
public void SetVariable(String name, bool value) => SetVariable!(name, value);
public void SetVariable(String name, bool2 value) => SetVariable!(name, value);
public void SetVariable(String name, bool3 value) => SetVariable!(name, value);
public void SetVariable(String name, bool4 value) => SetVariable!(name, value);
public void SetVariable(String name, int32 value) => SetVariable!(name, value);
public void SetVariable(String name, int2 value) => SetVariable!(name, value);
public void SetVariable(String name, int3 value) => SetVariable!(name, value);
public void SetVariable(String name, int4 value) => SetVariable!(name, value);
public enum SetVariableError
{
VariableNotFound,
ElementTypeMismatch,
MatrixDimensionMismatch,
ProvidedBufferTooShort,
}
public void SetVariable(String name, uint32 value) => SetVariable!(name, value);
public void SetVariable(String name, uint2 value) => SetVariable!(name, value);
public void SetVariable(String name, uint3 value) => SetVariable!(name, value);
public void SetVariable(String name, uint4 value) => SetVariable!(name, value);
internal Result<void, SetVariableError> SetVariableRaw(StringView name, ShaderVariableType elementType, int32 rows, int32 columns, int32 arrayLength, Span<uint8> rawData)
{
if(!_variables.TryGetValue(name, let variable))
{
return .Err(.VariableNotFound);
}
public void SetVariable(String name, float value) => SetVariable!(name, value);
public void SetVariable(String name, float2 value) => SetVariable!(name, value);
public void SetVariable(String name, float3 value) => SetVariable!(name, value);
public void SetVariable(String name, float4 value) => SetVariable!(name, value);
let matchResult = variable.CheckTypematch(rows, columns, elementType);
public void SetVariable(String name, Color value) => SetVariable!(name, value);
public void SetVariable(String name, ColorRGB value) => SetVariable!(name, value);
public void SetVariable(String name, ColorRGBA value) => SetVariable!(name, value);
switch (matchResult)
{
case .Ok:
// Nothing went wrong.
case .ElementTypeMismatch:
return .Err(.ElementTypeMismatch);
case .MatrixDimensionMismatch:
return .Err(.MatrixDimensionMismatch);
}
public void SetVariable(String name, Matrix3x3 value) => SetVariable!(name, value);
public void SetVariable(String name, Matrix4x3 value) => SetVariable!(name, value);
public void SetVariable(String name, Matrix value) => SetVariable!(name, value);
if (rawData.Length < variable._sizeInBytes)
return .Err(.ProvidedBufferTooShort);
public void GetVariable<T>(String name, out T value) where T : struct
// TODO: This method is very stupid atm.
// We could e.g. allow array length mismatches by simply copying the smaller amount of elements.
variable.SetRawData(rawData.Ptr);
return .Ok;
}
public void SetVariable(StringView name, bool value) => SetVariable!(name, value);
public void SetVariable(StringView name, bool2 value) => SetVariable!(name, value);
public void SetVariable(StringView name, bool3 value) => SetVariable!(name, value);
public void SetVariable(StringView name, bool4 value) => SetVariable!(name, value);
public void SetVariable(StringView name, int32 value) => SetVariable!(name, value);
public void SetVariable(StringView name, int2 value) => SetVariable!(name, value);
public void SetVariable(StringView name, int3 value) => SetVariable!(name, value);
public void SetVariable(StringView name, int4 value) => SetVariable!(name, value);
public void SetVariable(StringView name, uint32 value) => SetVariable!(name, value);
public void SetVariable(StringView name, uint2 value) => SetVariable!(name, value);
public void SetVariable(StringView name, uint3 value) => SetVariable!(name, value);
public void SetVariable(StringView name, uint4 value) => SetVariable!(name, value);
public void SetVariable(StringView name, float value) => SetVariable!(name, value);
public void SetVariable(StringView name, float2 value) => SetVariable!(name, value);
public void SetVariable(StringView name, float3 value) => SetVariable!(name, value);
public void SetVariable(StringView name, float4 value) => SetVariable!(name, value);
public void SetVariable(StringView name, Color value) => SetVariable!(name, value);
public void SetVariable(StringView name, ColorRGB value) => SetVariable!(name, value);
public void SetVariable(StringView name, ColorRGBA value) => SetVariable!(name, value);
public void SetVariable(StringView name, Matrix3x3 value) => SetVariable!(name, value);
public void SetVariable(StringView name, Matrix4x3 value) => SetVariable!(name, value);
public void SetVariable(StringView name, Matrix value) => SetVariable!(name, value);
public void GetVariable<T>(StringView name, out T value) where T : struct
{
Debug.Profiler.ProfileRendererFunction!();
+45 -17
View File
@@ -98,6 +98,7 @@ namespace GlitchyEngine.Renderer
static GBuffer _gBuffer;
static AssetHandle<Effect> TestFullscreenEffect;
static Material TestDeferredMaterial ~ _?.ReleaseRef();
static AssetHandle<Effect> s_tonemappingEffect;
static BlendState _gBufferBlend;
@@ -179,6 +180,9 @@ namespace GlitchyEngine.Renderer
static void InitDeferredRenderer()
{
TestFullscreenEffect = Content.LoadAsset("Resources/Shaders/simpleLight.fx", null, true);
TestDeferredMaterial = new Material(TestFullscreenEffect);
s_tonemappingEffect = Content.LoadAsset("Resources/Shaders/SimpleTonemapping.fx", null, true);
_gBuffer = new GBuffer();
@@ -362,32 +366,56 @@ namespace GlitchyEngine.Renderer
{
Debug.Profiler.ProfileRendererScope!("Draw Light");
Effect fsEffect = TestFullscreenEffect.Get();
//Effect fsEffect = TestFullscreenEffect.Get();
if (fsEffect == null)
if (TestDeferredMaterial == null)
continue;
float3 lightDir = -light.Transform.Forward;
fsEffect.SetTexture("GBuffer_Albedo", _gBuffer.Target, 0);
fsEffect.SetTexture("GBuffer_Normal", _gBuffer.Target, 1);
fsEffect.SetTexture("GBuffer_Tangent", _gBuffer.Target, 2);
fsEffect.SetTexture("GBuffer_Position", _gBuffer.Target, 3);
fsEffect.SetTexture("GBuffer_Emissive", _gBuffer.Target, 4);
fsEffect.SetTexture("GBuffer_Material", _gBuffer.Target, 5);
/*TestDeferredMaterial.SetTexture("GBuffer_Albedo", _gBuffer.Target, 0);
TestDeferredMaterial.SetTexture("GBuffer_Normal", _gBuffer.Target, 1);
TestDeferredMaterial.SetTexture("GBuffer_Tangent", _gBuffer.Target, 2);
TestDeferredMaterial.SetTexture("GBuffer_Position", _gBuffer.Target, 3);
TestDeferredMaterial.SetTexture("GBuffer_Emissive", _gBuffer.Target, 4);
TestDeferredMaterial.SetTexture("GBuffer_Material", _gBuffer.Target, 5);*/
fsEffect.Variables["LightColor"].SetData(light.Light.Color);
fsEffect.Variables["Illuminance"].SetData(light.Light.Illuminance);
fsEffect.Variables["LightDir"].SetData(lightDir);
TestDeferredMaterial.SetVariable("LightColor", light.Light.Color);
TestDeferredMaterial.SetVariable("Illuminance", light.Light.Illuminance);
TestDeferredMaterial.SetVariable("LightDir", lightDir);
fsEffect.Variables["CameraPos"].SetData(_sceneConstants.CameraPosition);
TestDeferredMaterial.SetVariable("CameraPos", _sceneConstants.CameraPosition);
fsEffect.Variables["Scaling"].SetData(scaling);
TestDeferredMaterial.SetVariable("Scaling", scaling);
fsEffect.ApplyChanges();
fsEffect.Bind();
//RenderCommand.BindEffect(TestFullscreenEffect);
TestDeferredMaterial.Bind();
// TODO: Don't do that please...
using (let vb = _gBuffer.Target.GetViewBinding(0))
{
RenderCommand.BindTexture(vb, 0, .Pixel);
}
using (let vb = _gBuffer.Target.GetViewBinding(1))
{
RenderCommand.BindTexture(vb, 1, .Pixel);
}
using (let vb = _gBuffer.Target.GetViewBinding(2))
{
RenderCommand.BindTexture(vb, 2, .Pixel);
}
using (let vb = _gBuffer.Target.GetViewBinding(3))
{
RenderCommand.BindTexture(vb, 3, .Pixel);
}
using (let vb = _gBuffer.Target.GetViewBinding(4))
{
RenderCommand.BindTexture(vb, 4, .Pixel);
}
using (let vb = _gBuffer.Target.GetViewBinding(5))
{
RenderCommand.BindTexture(vb, 5, .Pixel);
}
FullscreenQuad.Draw();
}
+5 -5
View File
@@ -640,9 +640,9 @@ namespace GlitchyEngine.Renderer
return;
s_quadInstanceBuffer.SetData<QuadBatchVertex>(s_rawQuadInstances.Ptr, s_setQuadInstances, 0, .WriteDiscard);
//s_currentQuadEffect.ApplyChanges();
//s_currentQuadEffect.Bind();
// TODO: Bind scene buffer
material.SetVariable("ViewProjection", sceneViewProjection);
material.Bind();
using (TextureViewBinding tvb = texture.GetViewBinding())
@@ -1151,8 +1151,8 @@ namespace GlitchyEngine.Renderer
uvTransform = sprite.TextureCoordinates;
}
// TODO: Material for Sprite renderer
DrawQuad(transform, spriteTexture ?? s_whiteTexture, null, spriteRenderer.Color, uvTransform, entityId);
Material material = spriteRenderer.Material.Get();
DrawQuad(transform, spriteTexture ?? s_whiteTexture, material, spriteRenderer.Color, uvTransform, entityId);
}
public static void DrawCircle(Matrix transform, CircleRendererComponent* spriteRenderer, uint32 entityId)