Skip to content

Commit 19de0bf

Browse files
committed
cCamera: Make flexible Camera struct
1 parent 633eef1 commit 19de0bf

File tree

3 files changed

+31
-12
lines changed

3 files changed

+31
-12
lines changed

shaders/cAutoExposure.fx

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,9 +98,13 @@ float4 PS_Blit(VS2PS_Quad Input) : SV_TARGET0
9898

9999
float3 PS_Exposure(VS2PS_Quad Input) : SV_TARGET0
100100
{
101+
// Get textures
101102
float Luma = tex2Dlod(SampleLumaTex, float4(Input.Tex0, 0.0, 99.0)).r;
102103
float4 NonExposedColor = tex2D(CShade_SampleColorTex, Input.Tex0);
103-
float3 ExposedColor = ApplyAutoExposure(NonExposedColor.rgb, Luma);
104+
105+
// Get exposure data
106+
Exposure ExposureData = GetExposureData(Luma);
107+
float3 ExposedColor = ApplyAutoExposure(NonExposedColor.rgb, ExposureData);
104108

105109
float2 UNormPos = (Input.Tex0 * 2.0) - 1.0;
106110
float3 Output = ApplyOutputTonemap(ExposedColor.rgb);
@@ -137,7 +141,8 @@ float3 PS_Exposure(VS2PS_Quad Input) : SV_TARGET0
137141

138142
if (_DisplayAverageLuma)
139143
{
140-
float2 LumaTex = UNormPos + float2(0.0, -0.5);
144+
// The offset goes from [-0.5, 0.5], hence the -0.5 subtraction.
145+
float2 LumaTex = UNormPos + float2(0.0, 0.5);
141146

142147
// Shrink the UV so [-1, 1] fills a square
143148
#if BUFFER_WIDTH > BUFFER_HEIGHT
@@ -154,7 +159,7 @@ float3 PS_Exposure(VS2PS_Quad Input) : SV_TARGET0
154159
// Create LumaIcon through alpha compositing
155160
float4 LumaIcon = 0.0;
156161
float4 Shadow = float4(0.0, 0.0, 0.0, 1.0);
157-
float4 ExpLuma = float4((float3)exp(Luma), 1.0);
162+
float4 ExpLuma = float4((float3)ExposureData.ExpLuma, 1.0);
158163

159164
LumaIcon = lerp(LumaIcon, Shadow, ShadowMask);
160165
LumaIcon = lerp(ExpLuma, LumaIcon, LumaTexMask);

shaders/cBloom.fx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,8 @@ float4 PS_Prefilter(VS2PS_Quad Input) : SV_TARGET0
174174
#if USE_AUTOEXPOSURE
175175
// Apply auto-exposure here
176176
float Luma = tex2D(SampleExposureTex, Input.Tex0).r;
177-
Color = ApplyAutoExposure(Color.rgb, Luma);
177+
Exposure ExposureData = GetExposureData(Luma);
178+
Color = ApplyAutoExposure(Color.rgb, ExposureData);
178179
#endif
179180

180181
// Store log luminance in alpha channel

shaders/shared/cCamera.fxh

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -43,18 +43,31 @@
4343
ui_label = "Exposure Compensation Range";
4444
ui_type = "slider";
4545
ui_step = 0.001;
46-
ui_min = 0.0;
46+
ui_min = 1.0;
4747
ui_max = 4.0;
4848
> = 1.0;
4949

50-
float3 ApplyAutoExposure(float3 Color, float Luma)
50+
struct Exposure
5151
{
52-
float LumaAverage = exp(Luma);
53-
float Ev100 = log2(LumaAverage * 100.0 / 12.5);
54-
Ev100 -= _CShadeExposureBias; // optional manual bias
55-
Ev100 = clamp(Ev100, -_CShadeExposureRange, _CShadeExposureRange);
56-
float Exposure = 1.0 / (1.2 * exp2(Ev100));
57-
return Color * Exposure;
52+
float ExpLuma;
53+
float Ev100;
54+
float Value;
55+
};
56+
57+
Exposure GetExposureData(float LumaTex)
58+
{
59+
Exposure Output;
60+
Output.ExpLuma = exp(LumaTex);
61+
Output.Ev100 = log2(Output.ExpLuma * 100.0 / 12.5);
62+
Output.Ev100 -= _CShadeExposureBias; // optional manual bias
63+
Output.Ev100 = clamp(Output.Ev100, -_CShadeExposureRange, _CShadeExposureRange);
64+
Output.Value = 1.0 / (1.2 * exp2(Output.Ev100));
65+
return Output;
66+
}
67+
68+
float3 ApplyAutoExposure(float3 Color, Exposure Input)
69+
{
70+
return Color * Input.Value;
5871
}
5972
#endif
6073

0 commit comments

Comments
 (0)