[HLSL Code] ApplyGameCC FO4.ver

share shaders here
Post Reply
  • Author
  • Message
Offline
*blah-blah-blah maniac*
Posts: 565
Joined: 05 Apr 2014, 10:29
Location: Taiwan

[HLSL Code] ApplyGameCC FO4.ver

A detail version of what is happening in APPLYGAMECOLORCORRECTION.

Here is the overall structure

Code: Select all

#ifdef APPLYGAMECOLORCORRECTION
	//fallout4 vanilla post process. Just an example for modders, better not enable without know how to edit and what you need
    
// combines bloom, adaptation     
	float4	r0, r1, r2, r3;
	r0.xyz = color.xyz;
	r1.xy = Params01[4].zw * IN.txcoord0.xy;
	r1.xyz = TextureBloom.Sample(Sampler1, r1.xy).xyz;
	r0.w = TextureAdaptation.Sample(Sampler0, IN.txcoord0.xy).x;
	r1.w = Params01[1].z / (0.001 + r0.w);
	r2.x = r1.w < Params01[1].y;
	r1.w = r2.x ? Params01[1].y : r1.w;
	r2.x = Params01[1].x < r1.w;
	r1.w = r2.x ? Params01[1].x : r1.w;
	r0.xyz = r1.xyz + r0.xyz;
	r0.xyz = r0.xyz * r1.w;
// returns color_adapt
    
// filmic tonemapper    
	r1.xyz = r0.xyz + r0.xyz;
	r2.xyz = r0.xyz * 0.3 + 0.05;
	r3.xy = float2(0.2, 3.333333) * Params01[1].w;
	r2.xyz = r1.xyz * r2.xyz + r3.x;
	r0.xyz = r0.xyz * 0.3 + 0.5;
	r0.xyz = r1.xyz * r0.xyz + 0.06;
	r0.xyz = r2.xyz / r0.xyz;
	r0.xyz = -Params01[1].w * 3.333333 + r0.xyz;
	r1.x = Params01[1].w * 0.2 + 19.376;
	r1.x = r1.x * 0.0408564 - r3.y;
	r1.xyz = r0.xyz / r1.x;
// returns filmic result
    
// post process    
	r0.x = dot(r1.xyz, float3(0.2125, 0.7154, 0.0721));
	r1.xyz = r1.xyz - r0.x;
	r1.xyz = Params01[2].x * r1.xyz + r0.x;
	r2.xyz = r0.x * Params01[3].xyz - r1.xyz;
	r1.xyz = Params01[3].w * r2.xyz + r1.xyz;
	r1.xyz = Params01[2].w * r1.xyz - r0.w;
	r0.xyz = Params01[2].z * r1.xyz + r0.w;
	//last color filter used only for certain conditions, like rifle night scope
	color.xyz = lerp(r0.xyz, Params01[5].xyz, Params01[5].w);

	color.xyz=saturate(color);
	color.xyz = pow(color.xyz, 1.0/2.2);
#endif //APPLYGAMECOLORCORRECTION

And in detail:

the first part is to combine bloom and scale the result with adaption to get Color_adpt for tonemapper.

Code: Select all

#ifdef APPLYGAMECOLORCORRECTION
	//fallout4 vanilla post process. Just an example for modders, better not enable without know how to edit and what you need
    
    float2 bloom_offset = Params01[4].zw;  //bloom coord scaling
    float3 adaptation   = Params01[1].xyz;  //.x = adaption max, .y = min, .z = scale/sensitivity

    float4 color        = TextureColor.Sample(Sampler0, IN.txcoord0.xy);
    float3 bloom        = TextureBloom.Sample(Sampler1, bloom_offset * IN.txcoord0.xy).rgb;
    float  middlegray   = TextureAdaptation.Sample(Sampler0, IN.txcoord0.xy).x;
    
    float  adapt = clamp(adaptation.z / (0.001 + middlegray), adaptation.y, adaptation.x); //auto exposure
    color.rgb    = (color.rgb + bloom.rgb) * adapt;    //bloom in add mode
then do tonemapping. bethesda and filmic operator, what do you know!

Code: Select all

// Filmic operator    
    float A = 0.3;           // Shoulder Strength
    float B = 0.5;           // Linear Strength
    float C = 0.1;           // Linear Angle
    float D = 0.1;           // Toe Strength
    float E = Params01[1].w; // Toe Numerator
    float F = 0.3;           // Toe Denominator
    float W = 5.6;           // LinearWhite, white level

    color.rgb = Tonemap_Filmic(color.rgb, W, A, B, C, D, E, F);
Filmic function:

Code: Select all

float3 Tonemap_Filmic(float3 color, float W, float A, float B, float C, float D, float E, float F)
{
    float4 res = float4(color.rgb, W);
    res        = (res * ( A * res + C * B) + D * E) / (res * ( A * res + B) + D * F);
    res       -= E / F;
    return res.rgb / res.a;
}

The final part is post color correction where the Params can be modded in the future through Creation Kit.

Code: Select all

#define LUM_709  float3(0.2125, 0.7154, 0.0721)
//color corrections   
    float  saturation  = Params01[2].x;   // 0 == gray scale
    float3 tint_color  = Params01[3].rgb; // tint color
    float  tint_weight = Params01[3].w;   // 0 == no tint
    float  contrast    = Params01[2].z;   // 0 == no contrast
    float  brightness  = Params01[2].w;   // intensity
    float3 fade        = Params01[5].xyz; // fade current scene to specified color, mostly used in special effects
    float  fade_weight = Params01[5].w;   // 0 == no fade

    color.a   = dot(color.rgb, LUM_709);                            //get luminance
    color.rgb = lerp(color.a, color.rgb, saturation);               //saturation
    color.rgb = lerp(color.rgb, color.a * tint.rgb, tint.a);        //tint
    color.rgb = lerp(middlegray, brightness * color.rgb, contrast); //contrast & intensity
    color.rgb = lerp(color.rgb, fade, fade_weight);                 //fade current scene to specified color

    color.rgb = saturate(color.rgb);
    color.rgb = pow(color.rgb, 1.0/2.2);
#endif //APPLYGAMECOLORCORRECTION
THE END



Haven't tested ingame though, might have missing something.
⁄(⁄ ⁄•⁄ω⁄•⁄ ⁄)⁄

thanks to roxahris for testing, fix typo in filmic function & constant.
update Oct/30/16 fix a error on float adapt.
Last edited by kingeric1992 on 30 Oct 2016, 10:09, edited 2 times in total.
_________________
Intel Xeon L5639 6C12T @3.96GHz | Gigabyte ga-x58a-ud3r | MSI GTX680 4G | 48G RAM | Intel 760p Nvme w clover bootloader
Flickr
YouTube

Offline
User avatar
*blah-blah-blah maniac*
Posts: 17427
Joined: 27 Dec 2011, 08:53
Location: Rather not to say

Re: [HLSL Code] ApplyGameCC FO4.ver

I edited your post by adding "color.xyz = pow(color.xyz, 1.0/2.2);" lines from latest update. They are required to make proper ldr output instead of linear space.
_________________
i9-9900k, 64Gb RAM, RTX 3060 12Gb, Win7

Offline
User avatar
*master*
Posts: 136
Joined: 08 Nov 2012, 15:24

Re: [HLSL Code] ApplyGameCC FO4.ver

Testing it in-game, it appears the tonemapping is off compared to the original. Other sections are fine, however.
Other sections with original tonemapping code -/- All sections

Offline
*blah-blah-blah maniac*
Posts: 565
Joined: 05 Apr 2014, 10:29
Location: Taiwan

Re: [HLSL Code] ApplyGameCC FO4.ver

roxahris wrote:Testing it in-game, it appears the tonemapping is off compared to the original. Other sections are fine, however.
Other sections with original tonemapping code -/- All sections
found a typo in filmic function & re calculate the W to 5.6. thanks.
_________________
Intel Xeon L5639 6C12T @3.96GHz | Gigabyte ga-x58a-ud3r | MSI GTX680 4G | 48G RAM | Intel 760p Nvme w clover bootloader
Flickr
YouTube

Offline
User avatar
*sensei*
Posts: 341
Joined: 27 Dec 2011, 21:33
Location: Poland, Gdańsk

Re: [HLSL Code] ApplyGameCC FO4.ver

Delete
Last edited by MaxG3D on 28 Mar 2017, 00:25, edited 1 time in total.
_________________
OS: Windows 10
CPU: AMD R5 3600
RAM: Corsair DDR4 16GB 3200MHz Vengeance
GPU: AMD Radeon 5700 XT
Sound Card: X-FI Titanium HD
Mobo: ASRock X370 Pro4
Monitor: M340CLZ 34" 3440x1440 100HZ AMD FREE Sync Curved Monitor

Offline
*blah-blah-blah maniac*
Posts: 565
Joined: 05 Apr 2014, 10:29
Location: Taiwan

Re: [HLSL Code] ApplyGameCC FO4.ver

update Oct/30/16 fix a error on float adapt.
_________________
Intel Xeon L5639 6C12T @3.96GHz | Gigabyte ga-x58a-ud3r | MSI GTX680 4G | 48G RAM | Intel 760p Nvme w clover bootloader
Flickr
YouTube

Offline
Posts: 5
Joined: 02 Nov 2016, 20:28

Re: [HLSL Code] ApplyGameCC FO4.ver

Hi Kingeric :D

Glad to see you're still coding! Didn't think you were still around.

I just now posted something about your amazing ENBlensFX file (in the Effects section) and noticed your recent posts here as well.

Can you please have a look at this if you have time? Thx!
Post Reply