diff --git a/GlitchyEditor/content/Shaders/GlitchyEngine.hlsl b/GlitchyEditor/content/Shaders/GlitchyEngine.hlsl new file mode 100644 index 0000000..40461cb --- /dev/null +++ b/GlitchyEditor/content/Shaders/GlitchyEngine.hlsl @@ -0,0 +1,59 @@ +#pragma EngineBuffer[ Name = "SceneConstants"; Binding = "Scene" ] +cbuffer SceneConstants : register(b0) +{ + float4x4 ViewProjection; +} + +#pragma EngineBuffer[ Name = "ObjectConstants"; Binding = "Object" ] +cbuffer ObjectConstants : register(b1) +{ + float4x4 Transform; + /** + * \brief Inverted and transposed transform matrix. + * \remarks This matrix is used in order to correctly transform normal vectors. + */ + float4x3 Transform_InvT; +#ifdef OutputEntityId + uint EntityId; +#endif +} + +/** + * \brief Pixel Shader output for lit surfaces. + */ +struct PS_OUT_Lit +{ + /** RGB: Albedo A: Transparency */ + float4 Albedo : SV_TARGET0; + /** RG: TextureNormal.XY | BA: GeoNrm.XY */ + float4 Normal : SV_TARGET1; + /** R: GeoNrm.Z | GBA: GeoTan.XYZ */ + float4 Tangent : SV_TARGET2; + /** RGB: world position XYZ | A: 1.0 */ + float4 Position : SV_TARGET3; + /** RGB: emissive light and color | A: unused */ + float4 Emissive : SV_TARGET4; + /** R: Metallicity | G: Roughness | B: Ambient | A: Unused */ + float4 Material : SV_TARGET5; +#ifdef OutputEntityId + /** Needed for editor picking. Simply pass through the EntityId. */ + uint EntityId : SV_TARGET6; +#endif +}; + +/** + * \brief Pixel Shader output for unlit surfaces. + */ +struct PS_OUT_Unlit +{ + /** RGB: Color A: Transparency */ + float4 Color : SV_TARGET0; + /** RGB: emissive light and color | A: unused */ + float4 Emissive : SV_TARGET1; + /** RGB: world position XYZ | A: 1.0 */ + float4 Position : SV_TARGET2; + +#ifdef OutputEntityId + uint EntityId : SV_TARGET5; +#endif +}; diff --git a/GlitchyEditor/content/Shaders/GlitchyEngine.hlsl.ass b/GlitchyEditor/content/Shaders/GlitchyEngine.hlsl.ass new file mode 100644 index 0000000..ccd76fa --- /dev/null +++ b/GlitchyEditor/content/Shaders/GlitchyEngine.hlsl.ass @@ -0,0 +1,4 @@ +{ + AssetLoader = "EffectAssetLoader", + Config = (GlitchyEditor.Assets.EffectAssetLoaderConfig){}/* No reflection data for GlitchyEditor.Assets.EffectAssetLoaderConfig. Add [BonTarget] or force it */ +} \ No newline at end of file diff --git a/GlitchyEditor/content/Shaders/myEffect.hlsl b/GlitchyEditor/content/Shaders/myEffect.hlsl index afa9e70..480204c 100644 --- a/GlitchyEditor/content/Shaders/myEffect.hlsl +++ b/GlitchyEditor/content/Shaders/myEffect.hlsl @@ -2,6 +2,8 @@ #include "ShaderHelpers.hlsl" +#include "GlitchyEngine.hlsl" + Texture2D AlbedoTexture : register(t0); SamplerState AlbedoSampler : register(s0); @@ -14,28 +16,11 @@ SamplerState MetallicSampler : register(s2); Texture2D RoughnessTexture : register(t3); SamplerState RoughnessSampler : register(s3); -// Texture2D AmbientTexture : register(t4); -// SamplerState AmbientSampler : register(s4); +Texture2D EmissiveTexture : register(t4); +SamplerState EmissiveSampler : register(s4); -#pragma EngineBuffer[ Name = "SceneConstants"; Binding = "Scene" ] -cbuffer SceneConstants : register(b0) -{ - float4x4 ViewProjection; -} - -#pragma EngineBuffer[ Name = "ObjectConstants"; Binding = "Object" ] -cbuffer ObjectConstants : register(b1) -{ - float4x4 Transform; - /** - * \brief Inverted and transposed transform matrix. - * \remarks This matrix is used in order to correctly transform normal vectors. - */ - float4x3 Transform_InvT; -#ifdef OutputEntityId - uint EntityId; -#endif -} +// Texture2D AmbientTexture : register(t5); +// SamplerState AmbientSampler : register(s5); cbuffer MaterialConstants : register(b2) { @@ -44,9 +29,12 @@ cbuffer MaterialConstants : register(b2) #pragma EditorVariable[ Name = "NormalScaling"; Preview = "Normal Scaling" ] float2 NormalScaling = float2(1.0, 1.0); #pragma EditorVariable[ Name = "MetallicFactor"; Preview = "Metallic Factor"; Min = 0.0f; Max = 1.0f ] - float MetallicFactor = 1.0; + float MetallicFactor = 0.0; #pragma EditorVariable[ Name = "RoughnessFactor"; Preview = "Rougness Factor"; Min = 0.0f; Max = 1.0f ] - float RoughnessFactor = 1.0; + float RoughnessFactor = 0.6; + #pragma EditorVariable[ Name = "EmissiveColor"; Preview = "Emissive Factor"; Type="ColorHDR" ] + float3 EmissiveColor = float3(0.0, 0.0, 0.0); + // # pra gma Editor Variable[ Name = "AmbientFactor"; Preview = "Ambient Factor" ] // float AmbientFactor = 1.0; } @@ -59,7 +47,7 @@ struct VS_IN float2 TexCoord : TEXCOORD; }; -struct PS_IN +typedef struct PS_IN { float4 Position : SV_POSITION; float3 WorldPosition : WORLDPOSITION; @@ -70,11 +58,11 @@ struct PS_IN #ifdef OutputEntityId nointerpolation uint EntityId : ENTITYID; #endif -}; +} VS_OUT; -PS_IN VS(VS_IN input) +VS_OUT VS(VS_IN input) { - PS_IN output; + VS_OUT output; float4 worldPosition = mul(Transform, float4(input.Position, 1)); @@ -92,22 +80,22 @@ PS_IN VS(VS_IN input) return output; } -struct PS_OUT -{ - float4 Albedo : SV_TARGET0; - // RG: TextureNormal.XY BA: GeoNrm.XY - float4 Normal : SV_TARGET1; - // R: GeoNrm.Z GBA: GeoTan.XYZ - float4 Tangent : SV_TARGET2; - float4 Position : SV_TARGET3; - // R: Metallicity G: Roughness B: Ambient - float4 Material : SV_TARGET4; -#ifdef OutputEntityId - uint EntityId : SV_TARGET5; -#endif -}; +//struct PS_OUT +//{ +// float4 Albedo : SV_TARGET0; +// // RG: TextureNormal.XY BA: GeoNrm.XY +// float4 Normal : SV_TARGET1; +// // R: GeoNrm.Z GBA: GeoTan.XYZ +// float4 Tangent : SV_TARGET2; +// float4 Position : SV_TARGET3; +// // R: Metallicity G: Roughness B: Ambient +// float4 Material : SV_TARGET4; +//#ifdef OutputEntityId +// uint EntityId : SV_TARGET5; +//#endif +//}; -PS_OUT PS(PS_IN input) +PS_OUT_Lit PS(PS_IN input) { // Build tangent space float3 normal = normalize(input.Normal); @@ -124,6 +112,9 @@ PS_OUT PS(PS_IN input) float texMetallic = MetallicTexture.Sample(MetallicSampler, input.TexCoord); float texRoughness = RoughnessTexture.Sample(RoughnessSampler, input.TexCoord); + + float3 texEmissive = EmissiveTexture.Sample(EmissiveSampler, input.TexCoord); + //float3 objectNormal = mul(tangentTransform, texNormal); //float3 worldNormal = mul(objectNormal, (float3x3)Transform); @@ -131,6 +122,7 @@ PS_OUT PS(PS_IN input) float3 finalNormal = ScaleNormal(texNormal, NormalScaling); float finalMetallic = texMetallic * MetallicFactor; float finalRoughness = texRoughness * RoughnessFactor; + float3 finalEmissive = texEmissive * EmissiveColor; /////////////TODO: REMOVEME @@ -141,12 +133,13 @@ PS_OUT PS(PS_IN input) /////////////TODO: END_REMOVEME - PS_OUT output; + PS_OUT_Lit output; output.Albedo = finalAlbedo; //output.Normal = float4(objectNormal, 1.0); output.Normal = float4(finalNormal.xy, normal.xy); output.Tangent = float4(normal.z, tangent.xyz); - output.Position = float4(input.WorldPosition, 1.0); + output.Position = float4(input.WorldPosition, 1.0); + output.Emissive = float4(finalEmissive, 0.0); output.Material = float4(finalMetallic, finalRoughness, 1.0, 0); #ifdef OutputEntityId diff --git a/GlitchyEditor/content/Shaders/simpleLight.hlsl b/GlitchyEditor/content/Shaders/simpleLight.hlsl index 687a1d1..3659c0d 100644 --- a/GlitchyEditor/content/Shaders/simpleLight.hlsl +++ b/GlitchyEditor/content/Shaders/simpleLight.hlsl @@ -15,7 +15,8 @@ Texture2D GBuffer_Albedo : register(t0); Texture2D GBuffer_Normal : register(t1); Texture2D GBuffer_Tangent : register(t2); Texture2D GBuffer_Position : register(t3); -Texture2D GBuffer_Material : register(t4); +Texture2D GBuffer_Emissive : register(t4); +Texture2D GBuffer_Material : register(t5); cbuffer Constants { @@ -58,7 +59,8 @@ float4 PS(PS_IN input) : SV_TARGET float4 rawAlbedo = GBuffer_Albedo.Sample(Sampler, input.TexCoord); float4 rawNormal = GBuffer_Normal.Sample(Sampler, input.TexCoord); float4 rawTangent = GBuffer_Tangent.Sample(Sampler, input.TexCoord); - float4 rawPosition = GBuffer_Position.Sample(Sampler, input.TexCoord); + float4 rawPosition = GBuffer_Position.Sample(Sampler, input.TexCoord); + float4 rawEmissive = GBuffer_Emissive.Sample(Sampler, input.TexCoord); float4 rawMaterial = GBuffer_Material.Sample(Sampler, input.TexCoord); // Extract data from GBuffer @@ -108,7 +110,7 @@ float4 PS(PS_IN input) : SV_TARGET float3 luminanceColor = LightColor * Illuminance; - float3 final = (kd * diffuse + specular) * luminanceColor * n_dot_l; + float3 final = (kd * diffuse + specular) * luminanceColor * n_dot_l + rawEmissive; #if OUTPUT == Inspect_NormalDistribution final = max(final - 10000000, nrmDist.xxx); diff --git a/GlitchyEditor/content/Textures/TestMaterial.mat b/GlitchyEditor/content/Textures/TestMaterial.mat index 07cce57..90ebaf9 100644 --- a/GlitchyEditor/content/Textures/TestMaterial.mat +++ b/GlitchyEditor/content/Textures/TestMaterial.mat @@ -4,7 +4,8 @@ "AlbedoTexture": "Textures/TestMat/rustediron2_albedo.png", "NormalTexture": "Textures/TestMat/rustediron2_normal.png", "MetallicTexture": "Textures/TestMat/rustediron2_metallic.png", - "RoughnessTexture": "Textures/TestMat/rustediron2_roughness.png" + "RoughnessTexture": "Textures/TestMat/rustediron2_roughness.png", + "EmissiveTexture": "Textures/rocket.png" ], Variables = [ "AlbedoColor": .ColorRGBA{ @@ -26,6 +27,13 @@ }, "RoughnessFactor": .Float{ Value = 1 + }, + "EmissiveColor": .ColorRGB{ + Value = { + R = 1, + G = 0, + B = 0 + } } ] } \ No newline at end of file diff --git a/GlitchyEditor/src/Assets/MaterialAssetLoader.bf b/GlitchyEditor/src/Assets/MaterialAssetLoader.bf index 4450acf..90b24fa 100644 --- a/GlitchyEditor/src/Assets/MaterialAssetLoader.bf +++ b/GlitchyEditor/src/Assets/MaterialAssetLoader.bf @@ -99,7 +99,8 @@ class MaterialAssetPropertiesEditor : AssetPropertiesEditor { for (let (name, arguments) in effect.[Friend]_variableDescriptions) { - let variable = effect.Variables[name]; + if (!effect.Variables.TryGetVariable(name, let variable)) + continue; bool hasPreviewName = TryGetValue(arguments, "Preview", var previewName); @@ -107,35 +108,69 @@ class MaterialAssetPropertiesEditor : AssetPropertiesEditor bool hasPreviewType = TryGetValue(arguments, "Type", var previewType); - if (hasPreviewType && previewType.Get() == "Color") + if (hasPreviewType && (previewType.Get() == "Color" || previewType.Get() == "ColorHDR")) { - Log.EngineLogger.AssertDebug(variable.Type == .Float && variable.Rows == 1); - - if (variable.Columns == 3) + if (previewType.Get().Equals("Color", .InvariantCultureIgnoreCase)) { - material.GetVariable(variable.Name, var value); + Log.EngineLogger.AssertDebug(variable.Type == .Float && variable.Rows == 1); - value = (float3)ColorRGB.LinearToSRGB((ColorRGB)value); - - if (ImGui.ColorEdit3(displayName.Ptr, *(float[3]*)&value)) + if (variable.Columns == 3) { - value = (float3)ColorRGB.SRgbToLinear((ColorRGB)value); - material.SetVariable(variable.Name, value); + material.GetVariable(variable.Name, var value); + + value = (float3)ColorRGB.LinearToSRGB((ColorRGB)value); + + if (ImGui.ColorEdit3(displayName.Ptr, *(float[3]*)&value)) + { + value = (float3)ColorRGB.SRgbToLinear((ColorRGB)value); + material.SetVariable(variable.Name, value); + } + } + else if (variable.Columns == 4) + { + material.GetVariable(variable.Name, var value); + + value = (float4)ColorRGBA.LinearToSRGB((ColorRGBA)value); + + if (ImGui.ColorEdit4(displayName.Ptr, *(float[4]*)&value)) + { + value = (float4)ColorRGBA.SRgbToLinear((ColorRGBA)value); + material.SetVariable(variable.Name, value); + } } } - else if (variable.Columns == 4) + else if (previewType.Get().Equals("ColorHDR", .InvariantCultureIgnoreCase)) { - material.GetVariable(variable.Name, var value); - - value = (float4)ColorRGBA.LinearToSRGB((ColorRGBA)value); + Log.EngineLogger.AssertDebug(variable.Type == .Float && variable.Rows == 1); - if (ImGui.ColorEdit4(displayName.Ptr, *(float[4]*)&value)) + if (variable.Columns == 3) { - value = (float4)ColorRGBA.SRgbToLinear((ColorRGBA)value); - material.SetVariable(variable.Name, value); + material.GetVariable(variable.Name, var value); + + value = (float3)ColorRGB.LinearToSRGB((ColorRGB)value); + + if (ImGui.ColorEdit3(displayName.Ptr, *(float[3]*)&value, .HDR | .Float)) + { + value = (float3)ColorRGB.SRgbToLinear((ColorRGB)value); + material.SetVariable(variable.Name, value); + } + } + else if (variable.Columns == 4) + { + material.GetVariable(variable.Name, var value); + + value = (float4)ColorRGBA.LinearToSRGB((ColorRGBA)value); + + if (ImGui.ColorEdit4(displayName.Ptr, *(float[4]*)&value, .HDR | .Float)) + { + value = (float4)ColorRGBA.SRgbToLinear((ColorRGBA)value); + material.SetVariable(variable.Name, value); + } } } } + //ImGui.ColorEdit3("", null, .HDR | .Float) + else if (variable.Type == .Float && variable.Rows == 1) { bool hasMin = TryGetValue(arguments, "Min", var min); diff --git a/GlitchyEditor/src/EditorLayer.bf b/GlitchyEditor/src/EditorLayer.bf index 98cdb6b..a7e0f1a 100644 --- a/GlitchyEditor/src/EditorLayer.bf +++ b/GlitchyEditor/src/EditorLayer.bf @@ -93,7 +93,7 @@ namespace GlitchyEditor public this(EditorContentManager contentManager) : base("Editor") { - Application.Get().Window.IsVSync = false; + Application.Get().Window.IsVSync = true; _contentManager = contentManager; diff --git a/GlitchyEngine/src/Renderer/ConstantBuffer.bf b/GlitchyEngine/src/Renderer/ConstantBuffer.bf index 0c2b995..2434f98 100644 --- a/GlitchyEngine/src/Renderer/ConstantBuffer.bf +++ b/GlitchyEngine/src/Renderer/ConstantBuffer.bf @@ -45,6 +45,11 @@ namespace GlitchyEngine.Renderer } } + public bool TryGetVariable(String name, out BufferVariable variable) + { + return _nameToVariable.TryGetValue(name, out variable); + } + public BufferVariable this[String name] => _nameToVariable[name]; public List.Enumerator GetEnumerator() diff --git a/GlitchyEngine/src/Renderer/Renderer.bf b/GlitchyEngine/src/Renderer/Renderer.bf index a8e43c4..9add510 100644 --- a/GlitchyEngine/src/Renderer/Renderer.bf +++ b/GlitchyEngine/src/Renderer/Renderer.bf @@ -40,21 +40,29 @@ namespace GlitchyEngine.Renderer if (_width == 0 || _height == 0) { - SamplerStateDescription desc = .(); - desc.MinFilter = .Point; - desc.MagFilter = .Point; + SamplerStateDescription samplerDesc = .(); + samplerDesc.MinFilter = .Point; + samplerDesc.MagFilter = .Point; RenderTargetGroupDescription targetDesc = .(width, height, TargetDescription[]( - .(RenderTargetFormat.R8G8B8A8_UNorm){SamplerDescription = desc}, - .(RenderTargetFormat.R16G16B16A16_SNorm){SamplerDescription = desc}, - .(RenderTargetFormat.R16G16B16A16_SNorm){SamplerDescription = desc}, - .(RenderTargetFormat.R32G32B32A32_Float){SamplerDescription = desc}, - .(RenderTargetFormat.R8G8B8A8_UNorm){SamplerDescription = desc}, - .(RenderTargetFormat.R32_UInt){SamplerDescription = desc, ClearColor = .UInt(uint32.MaxValue)}, + // RGB: Albedo A: Transparency + .(RenderTargetFormat.R8G8B8A8_UNorm){SamplerDescription = samplerDesc}, + // RG: TextureNormal.XY | BA: GeoNrm.XY + .(RenderTargetFormat.R16G16B16A16_SNorm){SamplerDescription = samplerDesc}, + // R: GeoNrm.Z | GBA: GeoTan.XYZ + .(RenderTargetFormat.R16G16B16A16_SNorm){SamplerDescription = samplerDesc}, + // RGB: world position XYZ | A: 1.0 + .(RenderTargetFormat.R32G32B32A32_Float){SamplerDescription = samplerDesc}, + // RGB: emissive light and color | A: unused + .(RenderTargetFormat.R16G16B16A16_Float){SamplerDescription = samplerDesc}, + // R: Metallicity | G: Roughness | B: Ambient | A: Unused + .(RenderTargetFormat.R8G8B8A8_UNorm){SamplerDescription = samplerDesc}, + // EntityId : Needed for editor picking. Simply pass through the EntityId. + .(RenderTargetFormat.R32_UInt){SamplerDescription = samplerDesc, ClearColor = .UInt(uint32.MaxValue)}, ), TargetDescription(.D24_UNorm_S8_UInt){ - SamplerDescription = desc, + SamplerDescription = samplerDesc, ClearColor = .DepthStencil(1.0f, 0) }); Target = new RenderTargetGroup(targetDesc); @@ -348,7 +356,8 @@ namespace GlitchyEngine.Renderer fsEffect.SetTexture("GBuffer_Normal", _gBuffer.Target, 1); fsEffect.SetTexture("GBuffer_Tangent", _gBuffer.Target, 2); fsEffect.SetTexture("GBuffer_Position", _gBuffer.Target, 3); - fsEffect.SetTexture("GBuffer_Material", _gBuffer.Target, 4); + fsEffect.SetTexture("GBuffer_Emissive", _gBuffer.Target, 4); + fsEffect.SetTexture("GBuffer_Material", _gBuffer.Target, 5); fsEffect.Variables["LightColor"].SetData(light.Light.Color); fsEffect.Variables["Illuminance"].SetData(light.Light.Illuminance); @@ -369,7 +378,7 @@ namespace GlitchyEngine.Renderer _lights.Clear(); // Copy EntityIDs to compositionTarget - _gBuffer.Target.CopyTo(_sceneConstants.CompositionTarget, 1, Int2.Zero, Int2(_sceneConstants.CameraTarget.Width, _sceneConstants.CameraTarget.Height), Int2.Zero, 5); + _gBuffer.Target.CopyTo(_sceneConstants.CompositionTarget, 1, Int2.Zero, Int2(_sceneConstants.CameraTarget.Width, _sceneConstants.CameraTarget.Height), Int2.Zero, 6); RenderCommand.SetBlendState(_gBufferBlend);