Skip to content

Commit

Permalink
cBlend: More blending modes, implemented alpha blending
Browse files Browse the repository at this point in the history
  • Loading branch information
papadanku committed Oct 26, 2024
1 parent de2ec79 commit f8c09d6
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 38 deletions.
52 changes: 21 additions & 31 deletions shaders/cBlend.fx
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,18 @@
[Shader Options]
*/

uniform int _Blend <
ui_label = "Blend Mode";
#include "shared/cColor.fxh"

uniform int _ColorBlend <
ui_label = "Color Blend Mode";
ui_type = "combo";
ui_items = "Normal\0Multiply\0Screen\0Overlay\0Darken\0Lighten\0Color Dodge\0Color Burn\0Hard Light\0Soft Light\0Difference\0Exclusion\0";
> = 0;

uniform int _AlphaBlend <
ui_label = "Alpha Blend Mode";
ui_type = "combo";
ui_items = "Add\0Subtract\0Multiply\0Min\0Max\0Screen\0";
ui_items = "Normal\0Multiply\0Screen\0Overlay\0Darken\0Lighten\0Color Dodge\0Color Burn\0Hard Light\0Soft Light\0Difference\0Exclusion\0";
> = 0;

uniform float3 _SrcFactor <
Expand Down Expand Up @@ -44,37 +52,19 @@ float4 PS_Copy(CShade_VS2PS_Quad Input) : SV_TARGET0

float4 PS_Blend(CShade_VS2PS_Quad Input) : SV_TARGET0
{
float4 Src = tex2D(SampleSrcTex, Input.Tex0) * float4(_SrcFactor, 1.0);
float4 Dest = tex2D(SampleDestTex, Input.Tex0) * float4(_DestFactor, 1.0);
float4 Src = tex2D(SampleSrcTex, Input.Tex0);
float4 Dest = tex2D(SampleDestTex, Input.Tex0);

float4 OutputColor = 0.0;
Src.rgb *= _SrcFactor;
Dest.rgb *= _DestFactor;

switch(_Blend)
{
case 0: // Add
OutputColor = Src + Dest;
break;
case 1: // Subtract
OutputColor = Src - Dest;
break;
case 2: // Multiply
OutputColor = Src * Dest;
break;
case 3: // Min
OutputColor = min(Src, Dest);
break;
case 4: // Max
OutputColor = max(Src, Dest);
break;
case 5: // Screen
OutputColor = (Src + Dest) - (Src * Dest);
break;
default:
OutputColor = Dest;
break;
}
float3 SrcAlpha = Src.a;
float3 DestAlpha = Dest.a;

float3 ColorBlend = CColor_Blend(Dest.rgb, Src.rgb, _ColorBlend);
float AlphaBlend = CColor_Blend(DestAlpha, SrcAlpha, _AlphaBlend).r;

return OutputColor;
return float4(ColorBlend, AlphaBlend);
}

technique CShade_CopyBuffer < ui_tooltip = "Create CBlend's copy texture"; >
Expand Down
16 changes: 9 additions & 7 deletions shaders/shared/cColor.fxh
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,8 @@
return CColor_BlendDifference(B, S);
case 11: // Exclusion
return CColor_BlendExclusion(B, S);
default:
return CColor_BlendNormal(B, S);
}
}

Expand Down Expand Up @@ -349,22 +351,22 @@

static const float3x3 CColor_OKLABfromRGB_M1 = float3x3
(
float3(0.4122214708, +0.5363325363, +0.0514459929),
float3(0.2119034982, +0.6806995451, +0.1073969566),
float3(0.0883024619, +0.2817188376, +0.6299787005)
float3(+0.4122214708, +0.5363325363, +0.0514459929),
float3(+0.2119034982, +0.6806995451, +0.1073969566),
float3(+0.0883024619, +0.2817188376, +0.6299787005)
);

static const float3x3 CColor_OKLABfromRGB_M2 = float3x3
(
float3(0.2104542553, +0.7936177850f, -0.0040720468),
float3(1.9779984951, -2.4285922050f, +0.4505937099),
float3(0.0259040371, +0.7827717662f, -0.8086757660)
float3(+0.2104542553, +0.7936177850f, -0.0040720468),
float3(+1.9779984951, -2.4285922050f, +0.4505937099),
float3(+0.0259040371, +0.7827717662f, -0.8086757660)
);

float3 CColor_GetOKLABfromRGB(float3 Color)
{
float3 LMS = mul(CColor_OKLABfromRGB_M1, Color);
LMS = pow(LMS, 1.0 / 3.0);
LMS = pow(abs(LMS), 1.0 / 3.0);
LMS = mul(CColor_OKLABfromRGB_M2, LMS);
return LMS;
};
Expand Down

0 comments on commit f8c09d6

Please sign in to comment.