From 90284739c0dec966d11f28fbbaf48c394fc91ecb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20L=C3=BCbe=C3=9F?= Date: Sun, 19 Sep 2021 14:22:16 +0200 Subject: [PATCH] Added support for int shader variables --- .../DX11/Renderer/Dx11ConstantBuffer.bf | 2 + GlitchyEngine/src/Renderer/BufferVariable.bf | 105 ++++++------------ GlitchyEngine/src/Renderer/ConstantBuffer.bf | 1 + GlitchyEngine/src/Renderer/Material.bf | 5 + 4 files changed, 44 insertions(+), 69 deletions(-) diff --git a/GlitchyEngine/src/Platform/DX11/Renderer/Dx11ConstantBuffer.bf b/GlitchyEngine/src/Platform/DX11/Renderer/Dx11ConstantBuffer.bf index e9a196f..824865d 100644 --- a/GlitchyEngine/src/Platform/DX11/Renderer/Dx11ConstantBuffer.bf +++ b/GlitchyEngine/src/Platform/DX11/Renderer/Dx11ConstantBuffer.bf @@ -31,6 +31,8 @@ namespace GlitchyEngine.Renderer _type = .Bool; case .Float: _type = .Float; + case .Int: + _type = .Int; default: Log.EngineLogger.Assert(false, scope $"Unhandled shader variable type: {shaderTypeDescription.Type}"); } diff --git a/GlitchyEngine/src/Renderer/BufferVariable.bf b/GlitchyEngine/src/Renderer/BufferVariable.bf index 6a822b4..aa0e7fa 100644 --- a/GlitchyEngine/src/Renderer/BufferVariable.bf +++ b/GlitchyEngine/src/Renderer/BufferVariable.bf @@ -79,6 +79,15 @@ namespace GlitchyEngine.Renderer EnsureTypeMatch(1, 3, .Float); case typeof(Vector4): EnsureTypeMatch(1, 4, .Float); + + case typeof(int32): + EnsureTypeMatch(1, 1, .Int); + case typeof(Int32_2): + EnsureTypeMatch(1, 2, .Int); + case typeof(Int32_3): + EnsureTypeMatch(1, 3, .Int); + case typeof(Int32_4): + EnsureTypeMatch(1, 4, .Int); case typeof(Matrix4x3): EnsureTypeMatch(4, 3, .Float); @@ -95,53 +104,37 @@ namespace GlitchyEngine.Renderer EnsureTypeMatch(1, 4, .Float); } } + + [Inline] + private void SetData(T value) + { + EnsureTypeMatch(); + + *(T*)firstByte = value; + } - public void SetData(bool value) - { - EnsureTypeMatch(1, 1, .Bool); + public void SetData(bool value) => SetData(value); - *(bool*)firstByte = value; - } + public void SetData(float value) => SetData(value); + public void SetData(Vector2 value) => SetData(value); + public void SetData(Vector3 value) => SetData(value); + public void SetData(Vector4 value) => SetData(value); - public void SetData(float value) - { - EnsureTypeMatch(1, 1, .Float); + public void SetData(int32 value) => SetData(value); + public void SetData(Int32_2 value) => SetData(value); + public void SetData(Int32_3 value) => SetData(value); + public void SetData(Int32_4 value) => SetData(value); + + public void SetData(ColorRGB value) => SetData(value); + public void SetData(ColorRGBA value) => SetData(value); + public void SetData(Color value) => SetData((ColorRGBA)value); - *(float*)firstByte = value; - } - public void SetData(Vector2 value) - { - EnsureTypeMatch(1, 2, .Float); - - *(Vector2*)firstByte = value; - } - - public void SetData(Vector3 value) - { - EnsureTypeMatch(1, 3, .Float); - - *(Vector3*)firstByte = value; - } - - public void SetData(Vector4 value) - { - EnsureTypeMatch(1, 4, .Float); - - *(Vector4*)firstByte = value; - } - - public void SetData(Matrix4x3 value) - { - // I think this is right - EnsureTypeMatch(4, 3, .Float); - - *(Matrix4x3*)firstByte = value; - } + public void SetData(Matrix4x3 value) => SetData(value); public void SetData(Matrix4x3[] value) { - EnsureTypeMatch(4, 3, .Float); + EnsureTypeMatch(); // TODO: assert length @@ -150,7 +143,7 @@ namespace GlitchyEngine.Renderer public void SetData(Matrix3x3 value) { - EnsureTypeMatch(3, 3, .Float); + EnsureTypeMatch(); *(Matrix4x3*)firstByte = Matrix4x3(value); @@ -159,7 +152,7 @@ namespace GlitchyEngine.Renderer public void SetData(Matrix3x3[] value) { - EnsureTypeMatch(3, 3, .Float); + EnsureTypeMatch(); // TODO: assert length @@ -169,42 +162,16 @@ namespace GlitchyEngine.Renderer } } - public void SetData(Matrix value) - { - EnsureTypeMatch(4, 4, .Float); - - *(Matrix*)firstByte = value; - } + public void SetData(Matrix value) => SetData(value); public void SetData(Matrix[] value) { - EnsureTypeMatch(4, 4, .Float); + EnsureTypeMatch(); // TODO: assert length Internal.MemCpy(firstByte, value.Ptr, sizeof(Matrix) * Math.Min(value.Count, _elements)); } - - public void SetData(ColorRGB value) - { - EnsureTypeMatch(1, 3, .Float); - - *(ColorRGB*)firstByte = value; - } - - public void SetData(ColorRGBA value) - { - EnsureTypeMatch(1, 4, .Float); - - *(ColorRGBA*)firstByte = value; - } - - public void SetData(Color value) - { - EnsureTypeMatch(1, 4, .Float); - - *(ColorRGBA*)firstByte = (ColorRGBA)value; - } // Todo: add all the other SetData-Methods diff --git a/GlitchyEngine/src/Renderer/ConstantBuffer.bf b/GlitchyEngine/src/Renderer/ConstantBuffer.bf index ac7a6e3..a7562fa 100644 --- a/GlitchyEngine/src/Renderer/ConstantBuffer.bf +++ b/GlitchyEngine/src/Renderer/ConstantBuffer.bf @@ -101,6 +101,7 @@ namespace GlitchyEngine.Renderer { Bool, Float, + Int, // todo } } diff --git a/GlitchyEngine/src/Renderer/Material.bf b/GlitchyEngine/src/Renderer/Material.bf index ece1c45..e13fa96 100644 --- a/GlitchyEngine/src/Renderer/Material.bf +++ b/GlitchyEngine/src/Renderer/Material.bf @@ -122,6 +122,11 @@ namespace GlitchyEngine.Renderer public void SetVariable(String name, Vector3 value) => SetVariable(name, value); public void SetVariable(String name, Vector4 value) => SetVariable(name, value); + public void SetVariable(String name, int32 value) => SetVariable(name, value); + public void SetVariable(String name, Int32_2 value) => SetVariable(name, value); + public void SetVariable(String name, Int32_3 value) => SetVariable(name, value); + public void SetVariable(String name, Int32_4 value) => SetVariable(name, value); + 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);