Skip to content

Commit

Permalink
Moved forward lighting stuff from rendering engine to light renderer
Browse files Browse the repository at this point in the history
  • Loading branch information
softwareantics committed Jan 11, 2024
1 parent a0677f3 commit 7fc8435
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 52 deletions.
2 changes: 2 additions & 0 deletions FinalEngine.Examples.Sponza/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,8 @@ private static void Main()

renderingEngine.Render(camera);

renderingEngine.SetSkybox(null);

ImGui.End();

controller.Render();
Expand Down
4 changes: 4 additions & 0 deletions FinalEngine.Rendering/Renderers/ILightRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,9 @@ namespace FinalEngine.Rendering.Renderers;

public interface ILightRenderer
{
void Conclude();

void Prepare();

void Render(Light light);
}
68 changes: 50 additions & 18 deletions FinalEngine.Rendering/Renderers/LightRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace FinalEngine.Rendering.Renderers;

public sealed class LightRenderer : ILightRenderer
{
private readonly IPipeline pipeline;
private readonly IRenderDevice renderDevice;

private IShaderProgram? ambientProgram;

Expand All @@ -21,9 +21,9 @@ public sealed class LightRenderer : ILightRenderer

private IShaderProgram? spotProgram;

public LightRenderer(IPipeline pipeline)
public LightRenderer(IRenderDevice renderDevice)
{
this.pipeline = pipeline ?? throw new ArgumentNullException(nameof(pipeline));
this.renderDevice = renderDevice ?? throw new ArgumentNullException(nameof(renderDevice));
}

private IShaderProgram AmbientProgram
Expand All @@ -46,6 +46,38 @@ private IShaderProgram SpotProgram
get { return this.spotProgram ??= ResourceManager.Instance.LoadResource<IShaderProgram>("Resources\\Shaders\\Lighting\\lighting-spot.fesp"); }
}

public void Conclude()
{
this.renderDevice.OutputMerger.SetBlendState(new BlendStateDescription()
{
Enabled = false,
});

this.renderDevice.OutputMerger.SetDepthState(new DepthStateDescription()
{
ReadEnabled = true,
ComparisonMode = ComparisonMode.Less,
WriteEnabled = true,
});
}

public void Prepare()
{
this.renderDevice.OutputMerger.SetBlendState(new BlendStateDescription()
{
Enabled = true,
SourceMode = BlendMode.One,
DestinationMode = BlendMode.One,
});

this.renderDevice.OutputMerger.SetDepthState(new DepthStateDescription()
{
ReadEnabled = true,
WriteEnabled = false,
ComparisonMode = ComparisonMode.Equal,
});
}

public void Render(Light light)
{
ArgumentNullException.ThrowIfNull(light, nameof(light));
Expand All @@ -72,45 +104,45 @@ public void Render(Light light)
throw new NotSupportedException($"The specified {nameof(light)} is not supported by the {nameof(LightRenderer)}.");
}

this.pipeline.SetUniform("u_light.base.color", light.Color);
this.pipeline.SetUniform("u_light.base.intensity", light.Intensity);
this.renderDevice.Pipeline.SetUniform("u_light.base.color", light.Color);
this.renderDevice.Pipeline.SetUniform("u_light.base.intensity", light.Intensity);
}

private void RenderAmbientLight()
{
this.pipeline.SetShaderProgram(this.AmbientProgram);
this.renderDevice.Pipeline.SetShaderProgram(this.AmbientProgram);
}

private void RenderAttenuation(Light light)
{
this.pipeline.SetUniform("u_light.attenuation.constant", light.Attenuation.Constant);
this.pipeline.SetUniform("u_light.attenuation.linear", light.Attenuation.Linear);
this.pipeline.SetUniform("u_light.attenuation.quadratic", light.Attenuation.Quadratic);
this.renderDevice.Pipeline.SetUniform("u_light.attenuation.constant", light.Attenuation.Constant);
this.renderDevice.Pipeline.SetUniform("u_light.attenuation.linear", light.Attenuation.Linear);
this.renderDevice.Pipeline.SetUniform("u_light.attenuation.quadratic", light.Attenuation.Quadratic);
}

private void RenderDirectionalLight(Light light)
{
this.pipeline.SetShaderProgram(this.DirectionalProgram);
this.pipeline.SetUniform("u_light.direction", light.Transform.Forward);
this.renderDevice.Pipeline.SetShaderProgram(this.DirectionalProgram);
this.renderDevice.Pipeline.SetUniform("u_light.direction", light.Transform.Forward);
}

private void RenderPointLight(Light light)
{
this.pipeline.SetShaderProgram(this.PointProgram);
this.renderDevice.Pipeline.SetShaderProgram(this.PointProgram);

this.RenderAttenuation(light);
this.pipeline.SetUniform("u_light.position", light.Transform.Position);
this.renderDevice.Pipeline.SetUniform("u_light.position", light.Transform.Position);
}

private void RenderSpotLight(Light light)
{
this.pipeline.SetShaderProgram(this.SpotProgram);
this.renderDevice.Pipeline.SetShaderProgram(this.SpotProgram);

this.RenderAttenuation(light);

this.pipeline.SetUniform("u_light.position", light.Transform.Position);
this.pipeline.SetUniform("u_light.direction", light.Transform.Forward);
this.pipeline.SetUniform("u_light.radius", light.Radius);
this.pipeline.SetUniform("u_light.outerRadius", light.OuterRadius);
this.renderDevice.Pipeline.SetUniform("u_light.position", light.Transform.Position);
this.renderDevice.Pipeline.SetUniform("u_light.direction", light.Transform.Forward);
this.renderDevice.Pipeline.SetUniform("u_light.radius", light.Radius);
this.renderDevice.Pipeline.SetUniform("u_light.outerRadius", light.OuterRadius);
}
}
36 changes: 2 additions & 34 deletions FinalEngine.Rendering/RenderingEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -128,10 +128,10 @@ public void Render(ICamera camera)
continue;
}

this.PrepareLightingPass();
this.lightRenderer.Prepare();
this.lightRenderer.Render(light);
this.RenderScene(camera);
this.FinalizeLightingPass();
this.lightRenderer.Conclude();
}
}
}
Expand All @@ -156,38 +156,6 @@ public void SetSkybox(ITextureCube? skyboxTexture)
this.skyboxTexture = skyboxTexture;
}

private void FinalizeLightingPass()
{
this.renderDevice.OutputMerger.SetBlendState(new BlendStateDescription()
{
Enabled = false,
});

this.renderDevice.OutputMerger.SetDepthState(new DepthStateDescription()
{
ReadEnabled = true,
ComparisonMode = ComparisonMode.Less,
WriteEnabled = true,
});
}

private void PrepareLightingPass()
{
this.renderDevice.OutputMerger.SetBlendState(new BlendStateDescription()
{
Enabled = true,
SourceMode = BlendMode.One,
DestinationMode = BlendMode.One,
});

this.renderDevice.OutputMerger.SetDepthState(new DepthStateDescription()
{
ReadEnabled = true,
WriteEnabled = false,
ComparisonMode = ComparisonMode.Equal,
});
}

private void RenderScene(ICamera camera)
{
this.UpdateCamera(camera);
Expand Down

0 comments on commit 7fc8435

Please sign in to comment.