Skip to content

Commit

Permalink
[2D] - Fixed issue in lighting effect where having 0 lights would ren…
Browse files Browse the repository at this point in the history
…der nothing. Ambient color should be rendered instead.
  • Loading branch information
Tape-Worm committed Oct 26, 2021
1 parent 05477ee commit 5ac7414
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,6 @@ private struct GpuData
private GorgonTexture2DView _displaceTexture;
// The output render target.
private GorgonRenderTarget2DView _output;
// Flag to indicate that the displacement texture has changed.
private bool _displaceTextureUpdated = true;
#endregion

#region Properties.
Expand Down Expand Up @@ -275,7 +273,7 @@ protected override PassContinuationState OnBeforeRenderPass(int passIndex, Gorgo
/// <returns>The 2D batch state.</returns>
protected override Gorgon2DBatchState OnGetBatchState(int passIndex, IGorgon2DEffectBuilders builders, bool statesChanged)
{
if ((statesChanged) || (_displacementState is null) || (_batchState is null) || (_displaceTextureUpdated))
if ((statesChanged) || (_displacementState is null) || (_batchState is null))
{
if (_displacementState is null)
{
Expand All @@ -289,8 +287,6 @@ protected override Gorgon2DBatchState OnGetBatchState(int passIndex, IGorgon2DEf
_batchState = builders.BatchBuilder
.PixelShaderState(_displacementState)
.Build(BatchStateAllocator);

_displaceTextureUpdated = false;
}

return _batchState;
Expand Down Expand Up @@ -338,7 +334,6 @@ public bool BeginDisplacementBatch(GorgonTexture2DView backgroundTexture, Gorgon
{
_displaceTexture = backgroundTexture;
_displacementState = null;
_displaceTextureUpdated = true;
}
_output = output;

Expand Down
19 changes: 11 additions & 8 deletions Gorgon/Gorgon.Renderers/Gorgon2D/Effects/Gorgon2DLightingEffect.cs
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ private void BuildPixelShaderPermutations()
lightShader = Interlocked.Exchange(ref _pixelLitTransformShader, null);
lightShader?.Dispose();

int newLightBufferSize = ((Lights.Count + 7) & ~7).Min(MaxLightCount);
int newLightBufferSize = ((Lights.Count + 7) & ~7).Min(MaxLightCount).Max(8);
var arrayMacro = new GorgonShaderMacro("USE_ARRAY");

Macros.Clear();
Expand Down Expand Up @@ -298,16 +298,11 @@ private void BuildPixelShaderPermutations()
/// <returns>A flag indicate whether to continue rendering or not.</returns>
private PassContinuationState OnBeginRender(GorgonBlendState blendState, GorgonDepthStencilState depthStencilState, GorgonRasterState rasterState, GorgonCameraCommon camera)
{
if (Lights.Count == 0)
{
return PassContinuationState.Stop;
}

BeginRender(_currentRtv, blendState, depthStencilState, rasterState);

int lightCount = Lights.Count.Min(MaxLightCount);

GorgonGpuLightData[] lightData = GorgonArrayPool<GorgonGpuLightData>.SharedTiny.Rent(lightCount);
GorgonGpuLightData[] lightData = GorgonArrayPool<GorgonGpuLightData>.SharedTiny.Rent(lightCount.Max(1));

try
{
Expand All @@ -317,14 +312,22 @@ private PassContinuationState OnBeginRender(GorgonBlendState blendState, GorgonD

if (light is null)
{
lightData[i] = default;
continue;
}

ref readonly GorgonGpuLightData gpuLight = ref light.GetGpuData();
lightData[i] = gpuLight;
}

_lightBuffer.Buffer.SetData<GorgonGpuLightData>(lightData.AsSpan(0, lightCount));
if (lightCount > 0)
{
_lightBuffer.Buffer.SetData<GorgonGpuLightData>(lightData.AsSpan(0, lightCount));
}
else
{
_lightBuffer.Buffer.SetData(lightData[0]);
}

return BeginPass(0, _currentRtv, camera);
}
Expand Down
5 changes: 5 additions & 0 deletions Gorgon/Gorgon.Renderers/Gorgon2D/Resources/Lighting.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,11 @@ float4 GorgonPixelShaderLighting(GorgonSpriteLitVertex vertex) : SV_Target
color = DirectionalLight(vertex, light);
result = float4(result.rgb + color.rgb, color.a);
break;
default:
color = SampleMainTexture(vertex.uv, vertex.color);
REJECT_ALPHA(color.a);
result = float4(result.rgb * _ambientColor.rgb, color.a);
return result;
}
}

Expand Down

0 comments on commit 5ac7414

Please sign in to comment.