Lower final color into final output stage

This commit is contained in:
2026-04-06 16:15:19 +08:00
parent 6645d507d0
commit 33bb84f650
17 changed files with 1149 additions and 42 deletions

View File

@@ -0,0 +1,65 @@
// XC_BUILTIN_FINAL_COLOR_OPENGL_PS
#version 430
layout(binding = 0) uniform sampler2D uSourceColorTexture;
layout(std140, binding = 0) uniform FinalColorConstants {
vec4 gColorScale;
vec4 gFinalColorParams;
};
in vec2 vTexCoord;
layout(location = 0) out vec4 fragColor;
float SRGBEncodeChannel(float value) {
float linear = max(value, 0.0);
return linear <= 0.0031308
? linear * 12.92
: 1.055 * pow(linear, 1.0 / 2.4) - 0.055;
}
vec3 ApplyLinearToSRGB(vec3 color) {
return vec3(
SRGBEncodeChannel(color.r),
SRGBEncodeChannel(color.g),
SRGBEncodeChannel(color.b));
}
vec3 ApplyNeutralToneMapping(vec3 color) {
return color / (color + vec3(1.0));
}
vec3 ApplyAcesToneMapping(vec3 color) {
const float a = 2.51;
const float b = 0.03;
const float c = 2.43;
const float d = 0.59;
const float e = 0.14;
return clamp((color * (a * color + b)) / (color * (c * color + d) + e), 0.0, 1.0);
}
vec3 ApplyToneMapping(vec3 color, float toneMappingMode) {
if (toneMappingMode > 1.5) {
return ApplyAcesToneMapping(color);
}
if (toneMappingMode > 0.5) {
return ApplyNeutralToneMapping(color);
}
return color;
}
void main() {
vec4 color = texture(uSourceColorTexture, vTexCoord);
color.rgb *= max(gFinalColorParams.x, 0.0);
color *= gColorScale;
color.rgb = ApplyToneMapping(color.rgb, gFinalColorParams.z);
if (gFinalColorParams.y > 0.5) {
color.rgb = ApplyLinearToSRGB(color.rgb);
}
fragColor = color;
}

View File

@@ -0,0 +1,66 @@
// XC_BUILTIN_FINAL_COLOR_VULKAN_PS
#version 450
layout(set = 0, binding = 0, std140) uniform FinalColorConstants {
vec4 gColorScale;
vec4 gFinalColorParams;
};
layout(set = 1, binding = 0) uniform texture2D uSourceColorTexture;
layout(set = 2, binding = 0) uniform sampler uLinearClampSampler;
layout(location = 0) in vec2 vTexCoord;
layout(location = 0) out vec4 fragColor;
float SRGBEncodeChannel(float value) {
float linear = max(value, 0.0);
return linear <= 0.0031308
? linear * 12.92
: 1.055 * pow(linear, 1.0 / 2.4) - 0.055;
}
vec3 ApplyLinearToSRGB(vec3 color) {
return vec3(
SRGBEncodeChannel(color.r),
SRGBEncodeChannel(color.g),
SRGBEncodeChannel(color.b));
}
vec3 ApplyNeutralToneMapping(vec3 color) {
return color / (color + vec3(1.0));
}
vec3 ApplyAcesToneMapping(vec3 color) {
const float a = 2.51;
const float b = 0.03;
const float c = 2.43;
const float d = 0.59;
const float e = 0.14;
return clamp((color * (a * color + b)) / (color * (c * color + d) + e), 0.0, 1.0);
}
vec3 ApplyToneMapping(vec3 color, float toneMappingMode) {
if (toneMappingMode > 1.5) {
return ApplyAcesToneMapping(color);
}
if (toneMappingMode > 0.5) {
return ApplyNeutralToneMapping(color);
}
return color;
}
void main() {
vec4 color = texture(sampler2D(uSourceColorTexture, uLinearClampSampler), vTexCoord);
color.rgb *= max(gFinalColorParams.x, 0.0);
color *= gColorScale;
color.rgb = ApplyToneMapping(color.rgb, gFinalColorParams.z);
if (gFinalColorParams.y > 0.5) {
color.rgb = ApplyLinearToSRGB(color.rgb);
}
fragColor = color;
}

View File

@@ -0,0 +1,65 @@
// XC_BUILTIN_FINAL_COLOR_D3D12_PS
Texture2D gSourceColorTexture : register(t0);
SamplerState gLinearClampSampler : register(s0);
cbuffer FinalColorConstants : register(b0) {
float4 gColorScale;
float4 gFinalColorParams;
};
struct PSInput {
float4 position : SV_POSITION;
float2 texcoord : TEXCOORD0;
};
float SRGBEncodeChannel(float value) {
const float linear = max(value, 0.0f);
return linear <= 0.0031308f
? linear * 12.92f
: 1.055f * pow(linear, 1.0f / 2.4f) - 0.055f;
}
float3 ApplyLinearToSRGB(float3 color) {
return float3(
SRGBEncodeChannel(color.r),
SRGBEncodeChannel(color.g),
SRGBEncodeChannel(color.b));
}
float3 ApplyNeutralToneMapping(float3 color) {
return color / (color + 1.0f);
}
float3 ApplyAcesToneMapping(float3 color) {
const float a = 2.51f;
const float b = 0.03f;
const float c = 2.43f;
const float d = 0.59f;
const float e = 0.14f;
return saturate((color * (a * color + b)) / (color * (c * color + d) + e));
}
float3 ApplyToneMapping(float3 color, float toneMappingMode) {
if (toneMappingMode > 1.5f) {
return ApplyAcesToneMapping(color);
}
if (toneMappingMode > 0.5f) {
return ApplyNeutralToneMapping(color);
}
return color;
}
float4 MainPS(PSInput input) : SV_TARGET {
float4 color = gSourceColorTexture.Sample(gLinearClampSampler, input.texcoord);
color.rgb *= max(gFinalColorParams.x, 0.0f);
color *= gColorScale;
color.rgb = ApplyToneMapping(color.rgb, gFinalColorParams.z);
if (gFinalColorParams.y > 0.5f) {
color.rgb = ApplyLinearToSRGB(color.rgb);
}
return color;
}

View File

@@ -0,0 +1,31 @@
Shader "Builtin Final Color"
{
Properties
{
_ColorScale ("Color Scale", Color) = (1.0,1.0,1.0,1.0)
_Exposure ("Exposure", Float) = 1.0
_OutputTransferMode ("Output Transfer Mode", Float) = 0.0
_ToneMappingMode ("Tone Mapping Mode", Float) = 0.0
}
SubShader
{
Pass
{
Name "FinalColor"
Tags { "LightMode" = "FinalOutput" }
Resources
{
FinalColorConstants (ConstantBuffer, 0, 0)
SourceColorTexture (Texture2D, 1, 0)
LinearClampSampler (Sampler, 2, 0)
}
HLSLPROGRAM
#pragma vertex MainVS
#pragma fragment MainPS
#pragma backend D3D12 HLSL "final-color.vs.hlsl" "final-color.ps.hlsl" vs_5_0 ps_5_0
#pragma backend OpenGL GLSL "final-color.vert.glsl" "final-color.frag.glsl"
#pragma backend Vulkan GLSL "final-color.vert.vk.glsl" "final-color.frag.vk.glsl"
ENDHLSL
}
}
}

View File

@@ -0,0 +1,16 @@
// XC_BUILTIN_FINAL_COLOR_OPENGL_VS
#version 430
out vec2 vTexCoord;
void main() {
const vec2 positions[3] = vec2[3](
vec2(-1.0, -1.0),
vec2(-1.0, 3.0),
vec2( 3.0, -1.0)
);
vec2 position = positions[gl_VertexID];
gl_Position = vec4(position, 1.0, 1.0);
vTexCoord = position * 0.5 + 0.5;
}

View File

@@ -0,0 +1,18 @@
// XC_BUILTIN_FINAL_COLOR_VULKAN_VS
#version 450
layout(location = 0) out vec2 vTexCoord;
void main() {
const vec2 positions[3] = vec2[3](
vec2(-1.0, -1.0),
vec2(-1.0, 3.0),
vec2( 3.0, -1.0)
);
vec2 position = positions[gl_VertexIndex];
gl_Position = vec4(position, 1.0, 1.0);
vTexCoord = vec2(
position.x * 0.5 + 0.5,
1.0 - (position.y * 0.5 + 0.5));
}

View File

@@ -0,0 +1,22 @@
// XC_BUILTIN_FINAL_COLOR_D3D12_VS
struct VSOutput {
float4 position : SV_POSITION;
float2 texcoord : TEXCOORD0;
};
VSOutput MainVS(uint vertexId : SV_VertexID) {
const float2 positions[3] = {
float2(-1.0f, -1.0f),
float2(-1.0f, 3.0f),
float2( 3.0f, -1.0f)
};
const float2 position = positions[vertexId];
VSOutput output;
output.position = float4(position, 1.0f, 1.0f);
output.texcoord = float2(
position.x * 0.5f + 0.5f,
1.0f - (position.y * 0.5f + 0.5f));
return output;
}