diff --git a/GlitchyEditor/content/Shaders/ClearUInt.hlsl b/GlitchyEditor/content/Shaders/ClearUInt.hlsl index f20eb92..cfe67bf 100644 --- a/GlitchyEditor/content/Shaders/ClearUInt.hlsl +++ b/GlitchyEditor/content/Shaders/ClearUInt.hlsl @@ -13,4 +13,4 @@ uint PS(float4 input : SV_Position) : SV_Target0 return ClearValue; } -#effect[VS = VS, PS = PS] \ No newline at end of file +#pragma Effect[VS = VS; PS = PS] diff --git a/GlitchyEditor/content/Shaders/GammaCorrect.hlsl b/GlitchyEditor/content/Shaders/GammaCorrect.hlsl index f7f6b40..7c19dfd 100644 --- a/GlitchyEditor/content/Shaders/GammaCorrect.hlsl +++ b/GlitchyEditor/content/Shaders/GammaCorrect.hlsl @@ -30,4 +30,4 @@ float4 PS(PS_IN input) : SV_TARGET return float4(pow(color.rgb, 1.0f / 2.2f), color.a); } -#effect[VS = VS, PS = PS] +#pragma Effect[VS = VS; PS = PS] diff --git a/GlitchyEditor/content/Shaders/SimpleTonemapping.hlsl b/GlitchyEditor/content/Shaders/SimpleTonemapping.hlsl index f1236ef..fc44a1f 100644 --- a/GlitchyEditor/content/Shaders/SimpleTonemapping.hlsl +++ b/GlitchyEditor/content/Shaders/SimpleTonemapping.hlsl @@ -32,4 +32,4 @@ float4 PS(PS_IN input) : SV_TARGET return float4(color, 1); } -#effect[VS = VS, PS = PS] +#pragma Effect[VS = VS; PS = PS] diff --git a/GlitchyEditor/content/Shaders/circlebatch.hlsl b/GlitchyEditor/content/Shaders/circlebatch.hlsl index d05cae5..6a51598 100644 --- a/GlitchyEditor/content/Shaders/circlebatch.hlsl +++ b/GlitchyEditor/content/Shaders/circlebatch.hlsl @@ -59,4 +59,4 @@ float4 PS(PS_Input input) : SV_Target0 return color; } -#effect[VS=VS, PS=PS] \ No newline at end of file +#pragma Effect[VS=VS; PS=PS] \ No newline at end of file diff --git a/GlitchyEditor/content/Shaders/lineShader.hlsl b/GlitchyEditor/content/Shaders/lineShader.hlsl index 1353f92..8ab4906 100644 --- a/GlitchyEditor/content/Shaders/lineShader.hlsl +++ b/GlitchyEditor/content/Shaders/lineShader.hlsl @@ -28,4 +28,4 @@ float4 PS(PS_Input input) : SV_Target0 return Color; } -#effect[VS=VS, PS=PS] +#pragma Effect[VS=VS; PS=PS] diff --git a/GlitchyEditor/content/Shaders/msdfShader.hlsl b/GlitchyEditor/content/Shaders/msdfShader.hlsl index 015213e..69207c4 100644 --- a/GlitchyEditor/content/Shaders/msdfShader.hlsl +++ b/GlitchyEditor/content/Shaders/msdfShader.hlsl @@ -90,4 +90,4 @@ float4 PS(PS_Input input) : SV_Target0 } */ -#effect[VS=VS, PS=PS] \ No newline at end of file +#pragma Effect[VS=VS; PS=PS] \ No newline at end of file diff --git a/GlitchyEditor/content/Shaders/myEffect.hlsl b/GlitchyEditor/content/Shaders/myEffect.hlsl index 11524f4..6f0f45c 100644 --- a/GlitchyEditor/content/Shaders/myEffect.hlsl +++ b/GlitchyEditor/content/Shaders/myEffect.hlsl @@ -17,12 +17,14 @@ SamplerState RoughnessSampler : register(s3); // Texture2D AmbientTexture : register(t4); // SamplerState AmbientSampler : register(s4); -cbuffer SceneConstants +#pragma EngineBuffer[ Name = "SceneConstants"; Binding = "Scene" ] +cbuffer SceneConstants : register(b0) { float4x4 ViewProjection; } -cbuffer ObjectConstants +#pragma EngineBuffer[ Name = "ObjectConstants"; Binding = "Object" ] +cbuffer ObjectConstants : register(b1) { float4x4 Transform; /** @@ -35,15 +37,15 @@ cbuffer ObjectConstants #endif } -cbuffer Constants +cbuffer MaterialConstants : register(b2) { - #EditorVariable{ Name = "AlbedoColor"; Preview = "Albedo Color"; Type="Color" } + #pragma EditorVariable[ Name = "AlbedoColor"; Preview = "Albedo Color"; Type="Color" ] float4 AlbedoColor = float4(1.0, 1.0, 1.0, 1.0); - #EditorVariable{ Name = "NormalScaling"; Preview = "Normal Scaling" } + #pragma EditorVariable[ Name = "NormalScaling"; Preview = "Normal Scaling" ] float2 NormalScaling = float2(1.0, 1.0); - #EditorVariable{ Name = "MetallicFactor"; Preview = "Metallic Factor"; Min = 0.0f; Max = 1.0f } + #pragma EditorVariable[ Name = "MetallicFactor"; Preview = "Metallic Factor"; Min = 0.0f; Max = 1.0f ] float MetallicFactor = 1.0; - #EditorVariable{ Name = "RoughnessFactor"; Preview = "Rougness Factor"; Min = 0.0f; Max = 1.0f } + #pragma EditorVariable[ Name = "RoughnessFactor"; Preview = "Rougness Factor"; Min = 0.0f; Max = 1.0f ] float RoughnessFactor = 1.0; // float AmbientFactor = 1.0; } @@ -154,4 +156,4 @@ PS_OUT PS(PS_IN input) return output; } -#effect[VS=VS,PS=PS] +#pragma Effect[VS = VS; PS = PS] diff --git a/GlitchyEditor/content/Shaders/simpleLight.hlsl b/GlitchyEditor/content/Shaders/simpleLight.hlsl index 652ec23..687a1d1 100644 --- a/GlitchyEditor/content/Shaders/simpleLight.hlsl +++ b/GlitchyEditor/content/Shaders/simpleLight.hlsl @@ -123,4 +123,4 @@ float4 PS(PS_IN input) : SV_TARGET return float4(final, 1); } -#effect[VS=VS,PS=PS] +#pragma Effect[VS = VS; PS = PS] diff --git a/GlitchyEditor/content/Shaders/spritebatch.hlsl b/GlitchyEditor/content/Shaders/spritebatch.hlsl index df19e91..dd15a2b 100644 --- a/GlitchyEditor/content/Shaders/spritebatch.hlsl +++ b/GlitchyEditor/content/Shaders/spritebatch.hlsl @@ -71,4 +71,4 @@ PS_Output PS(PS_Input input) return output; } -#effect[VS=VS, PS=PS] \ No newline at end of file +#pragma Effect[VS = VS; PS = PS] diff --git a/GlitchyEditor/content/Shaders/textureViewerShader.hlsl b/GlitchyEditor/content/Shaders/textureViewerShader.hlsl index afd6945..b1d0cf2 100644 --- a/GlitchyEditor/content/Shaders/textureViewerShader.hlsl +++ b/GlitchyEditor/content/Shaders/textureViewerShader.hlsl @@ -49,4 +49,4 @@ float4 PS(PS_Input input) : SV_Target0 return final; } -#effect[VS=VS, PS=PS] +#pragma Effect[VS=VS; PS=PS] diff --git a/GlitchyEngine/src/Platform/DX11/Renderer/Dx11BufferCollection.bf b/GlitchyEngine/src/Platform/DX11/Renderer/Dx11BufferCollection.bf index 9d20794..9bc478e 100644 --- a/GlitchyEngine/src/Platform/DX11/Renderer/Dx11BufferCollection.bf +++ b/GlitchyEngine/src/Platform/DX11/Renderer/Dx11BufferCollection.bf @@ -14,6 +14,9 @@ namespace GlitchyEngine.Renderer { Debug.Profiler.ProfileRendererFunction!(); + // Clear + nativeBuffers = .(); + for(let buffer in _buffers) { nativeBuffers[buffer.Index] = buffer.Buffer.nativeBuffer; diff --git a/GlitchyEngine/src/Platform/DX11/Renderer/Dx11GraphicsContext.bf b/GlitchyEngine/src/Platform/DX11/Renderer/Dx11GraphicsContext.bf index 8850c39..0615144 100644 --- a/GlitchyEngine/src/Platform/DX11/Renderer/Dx11GraphicsContext.bf +++ b/GlitchyEngine/src/Platform/DX11/Renderer/Dx11GraphicsContext.bf @@ -250,7 +250,7 @@ namespace GlitchyEngine.Renderer ID3D11ShaderResourceView*[D3D11_COMMONSHADER_INPUT_RESOURCE_SLOT_COUNT] _textures = .(); ID3D11SamplerState*[D3D11_COMMONSHADER_INPUT_RESOURCE_SLOT_COUNT] _samplers = .(); - for(let entry in shader.Textures) + for (let entry in shader.Textures) { _textures[entry.Index] = entry.BoundTexture._nativeShaderResourceView; _samplers[entry.Index] = entry.BoundTexture._nativeSamplerState; @@ -261,51 +261,53 @@ namespace GlitchyEngine.Renderer _firstTexture = entry.Index; } - if(_textureCount > 0) - { - switch(typeof(TShader)) + shader.Buffers.PlatformFetchNativeBuffers(); + + //if (_textureCount > 0) + //{ + switch (typeof(TShader)) { // TODO: Add remaining shader stages case typeof(PixelShader): // TODO: bind uavs - NativeContext.PixelShader.SetShaderResources(_firstTexture, _textureCount, &_textures[_firstTexture]); - NativeContext.PixelShader.SetSamplers(_firstTexture, _textureCount, &_samplers[_firstTexture]); + if (_textureCount > 0) + { + NativeContext.PixelShader.SetShaderResources(_firstTexture, _textureCount, &_textures[_firstTexture]); + NativeContext.PixelShader.SetSamplers(_firstTexture, _textureCount, &_samplers[_firstTexture]); + } _ps_FirstTexture = _firstTexture; _ps_BoundTextures = _textureCount; + + NativeContext.PixelShader.SetConstantBuffers(0, shader.Buffers.nativeBuffers.Count, &shader.Buffers.nativeBuffers); + + NativeContext.PixelShader.SetShader((ID3D11PixelShader*)shader.nativeShader); + case typeof(VertexShader): - NativeContext.VertexShader.SetShaderResources(_firstTexture, _textureCount, &_textures[_firstTexture]); - NativeContext.VertexShader.SetSamplers(_firstTexture, _textureCount, &_samplers[_firstTexture]); + if (_textureCount > 0) + { + NativeContext.VertexShader.SetShaderResources(_firstTexture, _textureCount, &_textures[_firstTexture]); + NativeContext.VertexShader.SetSamplers(_firstTexture, _textureCount, &_samplers[_firstTexture]); + } + NativeContext.VertexShader.SetConstantBuffers(0, shader.Buffers.nativeBuffers.Count, &shader.Buffers.nativeBuffers); + + NativeContext.VertexShader.SetShader((ID3D11VertexShader*)shader.nativeShader); + _vs_FirstTexture = _firstTexture; _vs_BoundTextures = _textureCount; + + //if (VertexShader vs = shader as VertexShader) + [ConstSkip] + { + SetReference!(_currentVertexShader, shader); + _currentInputLayout?.Release(); + _currentInputLayout = null; + } default: Runtime.FatalError(scope $"Shader stage \"{typeof(TShader)}\" not implemented."); } - } - - shader.Buffers.PlatformFetchNativeBuffers(); - - switch(typeof(TShader)) - { - // TODO: Add remaining shader stages - case typeof(PixelShader): - NativeContext.PixelShader.SetConstantBuffers(0, shader.Buffers.nativeBuffers.Count, &shader.Buffers.nativeBuffers); - NativeContext.PixelShader.SetShader((ID3D11PixelShader*)shader.nativeShader); - case typeof(VertexShader): - NativeContext.VertexShader.SetConstantBuffers(0, shader.Buffers.nativeBuffers.Count, &shader.Buffers.nativeBuffers); - NativeContext.VertexShader.SetShader((ID3D11VertexShader*)shader.nativeShader); - - if (VertexShader vs = shader as VertexShader) - { - SetReference!(_currentVertexShader, vs); - _currentInputLayout?.Release(); - _currentInputLayout = null; - } - - default: - Runtime.FatalError(scope $"Shader stage \"{typeof(TShader)}\" not implemented."); - } + //} } public override void UnbindTextures() diff --git a/GlitchyEngine/src/Renderer/BufferCollection.bf b/GlitchyEngine/src/Renderer/BufferCollection.bf index 1574b76..d3b3f17 100644 --- a/GlitchyEngine/src/Renderer/BufferCollection.bf +++ b/GlitchyEngine/src/Renderer/BufferCollection.bf @@ -1,9 +1,10 @@ using System; using System.Collections; +using GlitchyEngine.Core; namespace GlitchyEngine.Renderer { - public class BufferCollection : IEnumerable<(String Name, int Index, Buffer Buffer)> + public class BufferCollection : RefCounter, IEnumerable<(String Name, int Index, Buffer Buffer)> { public typealias BufferEntry = (String Name, int Index, Buffer Buffer); @@ -124,10 +125,7 @@ namespace GlitchyEngine.Renderer { Log.EngineLogger.AssertDebug(name == bufferEntry.Name); - bufferEntry.Buffer.ReleaseRef(); - - buffer.AddRef(); - bufferEntry.Buffer = buffer; + SetReference!(bufferEntry.Buffer, buffer); return true; } diff --git a/GlitchyEngine/src/Renderer/Effect.bf b/GlitchyEngine/src/Renderer/Effect.bf index 7a3bb63..0cd17c9 100644 --- a/GlitchyEngine/src/Renderer/Effect.bf +++ b/GlitchyEngine/src/Renderer/Effect.bf @@ -120,7 +120,9 @@ namespace GlitchyEngine.Renderer delete _; }; - BufferCollection _bufferCollection ~ delete _; + protected Dictionary _engineBuffers ~ DeleteDictionaryAndKeysAndValues!(_); + + BufferCollection _bufferCollection ~ _.ReleaseRef(); BufferVariableCollection _variables ~ delete _; @@ -132,23 +134,13 @@ namespace GlitchyEngine.Renderer public VertexShader VertexShader { get => _vs; - set - { - _vs?.ReleaseRef(); - _vs = value; - _vs?.AddRef(); - } + private set => SetReference!(_vs, value); } public PixelShader PixelShader { get => _ps; - set - { - _ps?.ReleaseRef(); - _ps = value; - _ps?.AddRef(); - } + private set => SetReference!(_ps, value); } public BufferCollection Buffers => _bufferCollection; @@ -156,28 +148,6 @@ namespace GlitchyEngine.Renderer public String Name => _name; - [Obsolete("Will be removed in the future", false)] - public this() - { - } - - public this(String filename, String vsEntry, String psEntry, String shaderName = null) - { - Debug.Profiler.ProfileResourceFunction!(); - - CompileFromFile(filename, vsEntry, psEntry); - - if(shaderName == null) - { - _name = new String(shaderName); - } - else - { - _name = new String(); - Path.GetFileNameWithoutExtension(filename, _name); - } - } - public this(String filename, String shaderName = null) { Debug.Profiler.ProfileResourceFunction!(); @@ -187,8 +157,9 @@ namespace GlitchyEngine.Renderer String psName = scope String(); _variableDescriptions = new VariableDesc(); + _engineBuffers = new Dictionary(); - ProcessFile(filename, fileContent, vsName, psName, _variableDescriptions); + ProcessFile(filename, fileContent, vsName, psName, _variableDescriptions, _engineBuffers); Compile(fileContent, filename, vsName, psName); @@ -205,15 +176,6 @@ namespace GlitchyEngine.Renderer } } - public this(String shaderName, String vsPath, String vsEntry, String psPath, String psEntry) - { - Debug.Profiler.ProfileResourceFunction!(); - - Compile(vsPath, vsEntry, psPath, psEntry); - - _name = new String(shaderName); - } - public ~this() { Debug.Profiler.ProfileResourceFunction!(); @@ -298,8 +260,6 @@ namespace GlitchyEngine.Renderer context.SetPixelShader(_ps); } - - private void CompileFromFile(String filename, String vsEntry, String psEntry) { Debug.Profiler.ProfileResourceFunction!(); @@ -333,6 +293,81 @@ namespace GlitchyEngine.Renderer code[commentPosition + 1] = '/'; } + private static Result<(int Start, int End)> GetNextPreprocessor(StringView code, int startindex, out StringView name, Dictionary arguments) + { + name = .(); + + int startOfLine; + int endOfLine; + do + { + startOfLine = code.IndexOf("#pragma", startindex); + + if (startOfLine == -1) + return .Err; + + endOfLine = code.IndexOf('\n', startOfLine); + + StringView line = (endOfLine != -1) ? code.Substring(startOfLine, endOfLine - startOfLine) : code.Substring(startOfLine); + + // cut off the #pragma + line = line.Substring(7); + + int lBracketIndex = line.IndexOf('['); + + if (lBracketIndex == -1) + { + name = line..Trim(); + break; + } + + name = line.Substring(0, lBracketIndex); + name.Trim(); + + int rBracketIndex = line.IndexOf(']'); + + if (rBracketIndex == -1) + { + Log.EngineLogger.Error($"Pragma is missing closing Bracket (\"{line}\")"); + rBracketIndex = line.Length; + } + + StringView argumentText = line.Substring(lBracketIndex + 1, rBracketIndex - lBracketIndex - 1); + + for (StringView argument in argumentText.Split(';')) + { + int equalsIndex = argument.IndexOf('='); + + StringView argumentName = .(); + StringView argumentValue = .(); + + if (equalsIndex == -1) + { + argumentName = argument; + argumentName.Trim(); + } + else + { + argumentName = argument.Substring(0, equalsIndex); + argumentName.Trim(); + + argumentValue = argument.Substring(equalsIndex + 1); + argumentValue.Trim(); + } + + if (arguments.ContainsKey(argumentName)) + { + Log.EngineLogger.Error($"Arguments \"{argumentName}\" already exists."); + continue; + } + + arguments.Add(argumentName, argumentValue); + } + } + + return .Ok((startOfLine, endOfLine)); + } + /** * Loads the effect file and extracts the names of the vertex- and pixel-shader. * @param filename The path of the effect file. @@ -341,7 +376,7 @@ namespace GlitchyEngine.Renderer * @param outPsName The string that will receive the pixel shader entry point. * @param outVarDescs The dictionary that will contain the Variable descriptions. */ - private static void ProcessFile(String filename, String fileContent, String outVsName, String outPsName, VariableDesc outVarDescs) + private static void ProcessFile(String filename, String fileContent, String outVsName, String outPsName, VariableDesc outVarDescs, Dictionary outEngineBuffers) { Debug.Profiler.ProfileResourceFunction!(); @@ -349,134 +384,108 @@ namespace GlitchyEngine.Renderer // append line ending just in case the file doesn't end with one. fileContent.Append('\n'); - ProcessEditorVariables(filename, fileContent, outVarDescs); - - int effectIndex = fileContent.IndexOf(effectKeyword, true); - - Log.EngineLogger.Assert(effectIndex >= 0, "Could not find #effect preprocessor directive."); - - int lineEndIndex = fileContent.IndexOf('\n', effectIndex + effectKeyword.Length); - - // String containing the #effect directive - StringView effectDirective = fileContent.Substring(effectIndex, lineEndIndex - effectIndex); - - int paramStartIndex = effectDirective.IndexOf('['); - - Log.EngineLogger.Assert(paramStartIndex >= 0, "Expected '[' after \"#effect\""); - - StringView effectToBracket = effectDirective.Substring(effectKeyword.Length, paramStartIndex - effectKeyword.Length); - - // Make sure there is only whitespace between "#effect" and "[" - Log.EngineLogger.Assert(effectToBracket.IsWhiteSpace, "Expected '[' after \"#effect\""); - - int paramEndIndex = effectDirective.IndexOf(']'); - - Log.EngineLogger.Assert(paramEndIndex >= 0, "Expected ']'"); - - StringView parameters = effectDirective.Substring(paramStartIndex + 1, paramEndIndex - paramStartIndex - 1); - - for(StringView parameter in parameters.Split(',')) { - int indexOfEquals = parameter.IndexOf("="); - - Log.EngineLogger.Assert(indexOfEquals >= 0, "Expected '='"); - - StringView paramName = parameter.Substring(0, indexOfEquals); - paramName.Trim(); - - StringView paramValue = parameter.Substring(indexOfEquals + 1); - paramValue.Trim(); - - switch(paramName) + Dictionary arguments = scope .(); + + int index = 0; + + while (true) { - case "VS", "VertexShader": - outVsName.Append(paramValue); - case "PS", "PixelShader": - outPsName.Append(paramValue); - default: - Log.EngineLogger.Assert(false, scope $"Unknown parameter name \"{paramName}\"."); + Result<(int Start, int End)> result = GetNextPreprocessor(fileContent, index, let name, arguments..Clear()); + + if (result case .Err) + break; + else if (result case .Ok(let value)) + { + index = value.End; + + switch(name) + { + case "Effect": + for (let (argName, argValue) in arguments) + { + switch(argName) + { + case "VS", "VertexShader": + outVsName.Append(argValue); + case "PS", "PixelShader": + outPsName.Append(argValue); + default: + Log.EngineLogger.Assert(false, scope $"Unknown parameter name \"{name}\"."); + } + } + case "EditorVariable": + ProcessEditorVariables(arguments, outVarDescs); + case "EngineBuffer": + ProcessEngineBuffer(arguments, outEngineBuffers); + default: + continue; + } + + CommentLine(fileContent, value.Start); + } } } - - // remove preprocessor directive from string so that the compiler wont try process it - CommentLine(fileContent, effectIndex); } - private static void ProcessEditorVariables(StringView fileName, StringView code, VariableDesc outVarDescs) + private static void ProcessEngineBuffer(Dictionary arguments, Dictionary outEngineBuffers) { - int index = 0; + String nameInEngine = null; + String nameInShader = null; - while(true) + for (var (argName, argValue) in arguments) { - index = code.IndexOf("#EditorVariable", index); - - if (index == -1) - break; - - CommentLine(code, index); - - index = code.IndexOf('{', index); - - Log.EngineLogger.AssertDebug(index != -1, "Missing '{'."); - - index++; - - int endIndex = code.IndexOf('}', index); - - Log.EngineLogger.AssertDebug(endIndex != -1, "Missing '}'."); - - StringView variableInfo = StringView(code, index, endIndex - index); - - Log.ClientLogger.Info($"Found variable: \"{variableInfo}\""); - - Dictionary parameters = new .(); - - String variableName = null; - - for (StringView argument in variableInfo.Split(';', .RemoveEmptyEntries)) + if (argValue.StartsWith('"') && argValue.EndsWith('"')) { - int equalsIndex = argument.IndexOf('='); + argValue = argValue[1...^2]; + } + switch (argName) + { + case "Name": + nameInShader = new String(argValue); + case "Binding": + nameInEngine = new String(argValue); + default: + Log.EngineLogger.Assert(false, scope $"Unknown parameter for EngineBuffer: \"{argName}\"."); + } + } - if (equalsIndex == -1) - { - Log.EngineLogger.Error($"Missing value for Argument \"{argument..Trim()}\"."); - } - else - { - StringView argName = argument.Substring(0, equalsIndex)..Trim(); - StringView argValue = argument.Substring(equalsIndex + 1)..Trim(); + Log.EngineLogger.AssertDebug(nameInEngine != null); + Log.EngineLogger.AssertDebug(nameInShader != null); - if (argValue.StartsWith('"') && argValue.EndsWith('"')) - { - argValue = argValue[1...^2]; - } + outEngineBuffers.Add(nameInEngine, nameInShader); + } - if (argName == "Name") - variableName = new String(argValue); - else - { - Variant value; + private static void ProcessEditorVariables(Dictionary arguments, VariableDesc outVarDescs) + { + String variableName = null; + + Dictionary parameters = new .(); - if (argName == "Min" || argName == "Max") - { - value = ParseVariableValue(argValue); - } - else - { - value = Variant.Create(new String(argValue), true); - } - - parameters.Add(new String(argName), value); - } - } + for (var (name, value) in arguments) + { + if (value.StartsWith('"') && value.EndsWith('"')) + { + value = value[1...^2]; } - Log.EngineLogger.AssertDebug(variableName != null, "Missing argument \"Name\" int variable description."); - - outVarDescs.Add(variableName, parameters); - - index = endIndex; + switch(name) + { + case "Name": + variableName = new String(value); + case "Min", "Max": + Variant paramValue = ParseVariableValue(value); + parameters.Add(new String(name), paramValue); + default: + Variant paramValue = Variant.Create(new String(value), true); + parameters.Add(new String(name), paramValue); + } } + + Log.EngineLogger.AssertDebug(variableName != null, "Missing argument \"Name\" int variable description."); + + outVarDescs.Add(variableName, parameters); + } private static Variant ParseVariableValue(StringView valueString) @@ -613,6 +622,9 @@ namespace GlitchyEngine.Renderer internalIndex++; } + + SetReference!(_vs.[Friend]_buffers, _bufferCollection); + SetReference!(_ps.[Friend]_buffers, _bufferCollection); } private void MergeBufferVariables() @@ -621,8 +633,16 @@ namespace GlitchyEngine.Renderer _variables = new BufferVariableCollection(false); - for(let buffer in _bufferCollection) + outer: for(let buffer in _bufferCollection) { + for (let eb in _engineBuffers) + { + if (eb.value == buffer.Name) + { + continue outer; + } + } + if(let cbuffer = buffer.Buffer as ConstantBuffer) { for(let variable in cbuffer.Variables) diff --git a/GlitchyEngine/src/Renderer/Renderer.bf b/GlitchyEngine/src/Renderer/Renderer.bf index e4b6db1..dc486d1 100644 --- a/GlitchyEngine/src/Renderer/Renderer.bf +++ b/GlitchyEngine/src/Renderer/Renderer.bf @@ -93,6 +93,19 @@ namespace GlitchyEngine.Renderer static BlendState _lightBlend; static DepthStencilState _fullscreenDepthState; + static Buffer _sceneBuffer; + static Buffer _objectBuffer; + + [Ordered] + struct ObjectConstantsBuffer + { + public Matrix Transform; + public Matrix4x3 Transform_InvT; + public uint32 EntityId; + + private Vector3 _padding; + } + public static void Init(GraphicsContext context, EffectLibrary effectLibrary) { Debug.Profiler.ProfileFunction!(); @@ -179,10 +192,19 @@ namespace GlitchyEngine.Renderer DepthStencilStateDescription dsDesc = .Default; dsDesc.DepthEnabled = false; _fullscreenDepthState = new DepthStencilState(dsDesc); + + BufferDescription sceneBufferDesc = .(sizeof(Matrix), .Constant, .Dynamic, .Write); + _sceneBuffer = new Buffer(sceneBufferDesc); + + BufferDescription objectBufferDesc = .(sizeof(ObjectConstantsBuffer), .Constant, .Dynamic, .Write); + _objectBuffer = new Buffer(objectBufferDesc); } static void DeinitDeferredRenderer() { + _objectBuffer.ReleaseRef(); + _sceneBuffer.ReleaseRef(); + _fullscreenDepthState.ReleaseRef(); _lightBlend.ReleaseRef(); _gBufferBlend.ReleaseRef(); @@ -192,6 +214,7 @@ namespace GlitchyEngine.Renderer TestFullscreenEffect.ReleaseRef(); } + // [Obsolete("", false)] public static void BeginScene(OldCamera camera) { Debug.Profiler.ProfileRendererFunction!(); @@ -208,6 +231,8 @@ namespace GlitchyEngine.Renderer _sceneConstants.CameraPosition = transform.Translation; _sceneConstants.CameraTarget = renderTarget; _sceneConstants.CompositionTarget = finalTarget; + + _sceneBuffer.SetData(viewProjection); } public static void BeginScene(EditorCamera camera, RenderTargetGroup finalTarget) @@ -219,6 +244,8 @@ namespace GlitchyEngine.Renderer _sceneConstants.CameraPosition = camera.Position; _sceneConstants.CameraTarget = camera.RenderTarget; _sceneConstants.CompositionTarget = finalTarget; + + _sceneBuffer.SetData(viewProjection, 0, .WriteDiscard); } public static int SortMeshes(SubmittedMesh left, SubmittedMesh right) @@ -282,14 +309,27 @@ namespace GlitchyEngine.Renderer { Debug.Profiler.ProfileRendererScope!("SetVariables"); - entry.Material.SetVariable("ViewProjection", _sceneConstants.ViewProjection); + ObjectConstantsBuffer objectData; + objectData.Transform = entry.Transform; + + Matrix4x3 mat = Matrix4x3((Matrix3x3)(entry.Transform).Invert().Transpose()); + objectData.Transform_InvT = mat; + + objectData.EntityId = entry.EntityId; + + _objectBuffer.SetData(objectData, 0, .WriteDiscard); + + entry.Material.Effect.Buffers.TryReplaceBuffer("SceneConstants", _sceneBuffer); + entry.Material.Effect.Buffers.TryReplaceBuffer("ObjectConstants", _objectBuffer); + + /*entry.Material.SetVariable("ViewProjection", _sceneConstants.ViewProjection); entry.Material.SetVariable("Transform", entry.Transform); entry.Material.SetVariable("EntityId", entry.EntityId); Matrix3x3 mat = (Matrix3x3)(entry.Transform).Invert().Transpose(); - entry.Material.SetVariable("Transform_InvT", mat); + entry.Material.SetVariable("Transform_InvT", mat);*/ } entry.Material.Bind(_context); diff --git a/GlitchyEngine/src/Renderer/Shader.bf b/GlitchyEngine/src/Renderer/Shader.bf index ba5b62c..d8d9552 100644 --- a/GlitchyEngine/src/Renderer/Shader.bf +++ b/GlitchyEngine/src/Renderer/Shader.bf @@ -21,7 +21,7 @@ namespace GlitchyEngine.Renderer public abstract class Shader : RefCounter { - protected BufferCollection _buffers ~ delete _;//:append _; + protected internal BufferCollection _buffers ~ _.ReleaseRef();//:append _; protected ShaderTextureCollection _textures ~ delete _;