Skip to content

Instantly share code, notes, and snippets.

@RomiTT
Last active January 11, 2024 12:38
Show Gist options
  • Save RomiTT/9c05d36fe339b899793a3252297a5624 to your computer and use it in GitHub Desktop.
Save RomiTT/9c05d36fe339b899793a3252297a5624 to your computer and use it in GitHub Desktop.
YUV to RGB HLSL Shaders - DirectX 11
//-----------------------------------------------------------
// YUV TO RGB BT.601 For SD TV
//-----------------------------------------------------------
cbuffer PS_CONSTANT_BUFFER : register(b0)
{
float Opacity;
float ignoreA;
float ignoreB;
float ignoreC;
};
Texture2D shaderTexture;
SamplerState SampleType;
struct PS_INPUT
{
float4 Position : SV_POSITION;
float2 Texture : TEXCOORD0;
};
float4 PS(PS_INPUT In) : SV_TARGET
{
float4 rgba;
rgba = shaderTexture.Sample(SampleType, In.Texture);
rgba.a = rgba.a * Opacity;
return rgba;
}
struct VS_INPUT
{
float4 Position : POSITION;
float2 Texture : TEXCOORD0;
};
struct VS_OUTPUT
{
float4 Position : SV_POSITION;
float2 Texture : TEXCOORD0;
};
VS_OUTPUT VS(VS_INPUT In)
{
VS_OUTPUT Output;
Output.Position = In.Position;
Output.Texture = In.Texture;
return Output;
}
//-----------------------------------------------------------
// YUV TO RGB BT.2020 For UHD TV
//-----------------------------------------------------------
cbuffer PS_CONSTANT_BUFFER : register(b0)
{
float Opacity;
float ignoreA;
float ignoreB;
float ignoreC;
};
Texture2D shaderTextureY;
Texture2D shaderTextureUV;
SamplerState SampleType;
struct PS_INPUT
{
float4 Position : SV_POSITION;
float2 Texture : TEXCOORD0;
};
float4 PS(PS_INPUT In) : SV_TARGET
{
float3 yuv;
float4 rgba;
yuv.x = shaderTextureY.Sample(SampleType, In.Texture).x;
yuv.yz = shaderTextureUV.Sample(SampleType, In.Texture).xy;
yuv.x = 1.164383561643836 * (yuv.x - 0.0625);
yuv.y = yuv.y - 0.5;
yuv.z = yuv.z - 0.5;
rgba.x = yuv.x + 1.792741071428571 * yuv.z;
rgba.y = yuv.x - 0.532909328559444 * yuv.z - 0.21324861427373 * yuv.y;
rgba.z = yuv.x + 2.112401785714286 * yuv.y;
rgba.x = saturate(1.661 * rgba.x - 0.588 * rgba.y - 0.073 * rgba.z);
rgba.y = saturate(-0.125 * rgba.x + 1.133 * rgba.y - 0.008 * rgba.z);
rgba.z = saturate(-0.018 * rgba.x - 0.101 * rgba.y + 1.119 * rgba.z);
rgba.a = Opacity;
return rgba;
}
//-----------------------------------------------------------
// YUV TO RGB BT.601 For SD TV
//-----------------------------------------------------------
cbuffer PS_CONSTANT_BUFFER : register(b0)
{
float Opacity;
float ignoreA;
float ignoreB;
float ignoreC;
};
Texture2D shaderTextureY;
Texture2D shaderTextureUV;
SamplerState SampleType;
struct PS_INPUT
{
float4 Position : SV_POSITION;
float2 Texture : TEXCOORD0;
};
float4 PS(PS_INPUT In) : SV_TARGET
{
float3 yuv;
float4 rgba;
yuv.x = shaderTextureY.Sample(SampleType, In.Texture).x;
yuv.yz = shaderTextureUV.Sample(SampleType, In.Texture).xy;
yuv.x = 1.164383561643836 * (yuv.x - 0.0625);
yuv.y = yuv.y - 0.5;
yuv.z = yuv.z - 0.5;
rgba.x = saturate(yuv.x + 1.596026785714286 * yuv.z);
rgba.y = saturate(yuv.x - 0.812967647237771 * yuv.z - 0.391762290094914 * yuv.y);
rgba.z = saturate(yuv.x + 2.017232142857142 * yuv.y);
rgba.a = Opacity;
return rgba;
}
//-----------------------------------------------------------
// YUV TO RGB BT.709 For HD TV
//-----------------------------------------------------------
cbuffer PS_CONSTANT_BUFFER : register(b0)
{
float Opacity;
float ignoreA;
float ignoreB;
float ignoreC;
};
Texture2D shaderTextureY;
Texture2D shaderTextureUV;
SamplerState SampleType;
struct PS_INPUT
{
float4 Position : SV_POSITION;
float2 Texture : TEXCOORD0;
};
float4 PS(PS_INPUT input) : SV_Target
{
float3 yuv;
float4 rgba;
yuv.x = shaderTextureY.Sample(SampleType, In.Texture).x;
yuv.yz = shaderTextureUV.Sample(SampleType, In.Texture).xy;
yuv.x = 1.164383561643836 * (yuv.x - 0.0625);
yuv.y = yuv.y - 0.5;
yuv.z = yuv.z - 0.5;
rgba.x = saturate(yuv.x + 1.792741071428571 * yuv.z);
rgba.y = saturate(yuv.x - 0.532909328559444 * yuv.z - 0.21324861427373 * yuv.y);
rgba.z = saturate(yuv.x + 2.112401785714286 * yuv.y);
rgba.a = Opacity;
return rgba;
}
//-----------------------------------------------------------
// YUV TO RGB BT.601 For SD TV
//-----------------------------------------------------------
cbuffer PS_CONSTANT_BUFFER : register(b0)
{
float Opacity;
float ignoreA;
float ignoreB;
float ignoreC;
};
Texture2D shaderTextureYUYV;
SamplerState SampleType;
struct PS_INPUT
{
float4 Position : SV_POSITION;
float2 Texture : TEXCOORD0;
};
float4 PS(PS_INPUT In) : SV_TARGET
{
float3 yuv;
float4 rgba;
yuv.x = shaderTextureYUYV.Sample(SampleType, In.Texture).x;
yuv.y = shaderTextureYUYV.Sample(SampleType, In.Texture).y;
yuv.z = shaderTextureYUYV.Sample(SampleType, In.Texture).a;
yuv.x = 1.164383561643836 * (yuv.x - 0.0625);
yuv.y = yuv.y - 0.5;
yuv.z = yuv.z - 0.5;
rgba.x = saturate(yuv.x + 1.596026785714286 * yuv.z);
rgba.y = saturate(yuv.x - 0.812967647237771 * yuv.z - 0.391762290094914 * yuv.y);
rgba.z = saturate(yuv.x + 2.017232142857142 * yuv.y);
rgba.a = Opacity;
return rgba;
}
//-----------------------------------------------------------
// YUV TO RGB BT.709 For HD TV
//-----------------------------------------------------------
cbuffer PS_CONSTANT_BUFFER : register(b0)
{
float Opacity;
float ignoreA;
float ignoreB;
float ignoreC;
};
Texture2D shaderTextureYUYV;
SamplerState SampleType;
struct PS_INPUT
{
float4 Position : SV_POSITION;
float2 Texture : TEXCOORD0;
};
float4 PS(PS_INPUT In) : SV_TARGET
{
float3 yuv;
float4 rgba;
yuv.x = shaderTextureYUYV.Sample(SampleType, In.Texture).x;
yuv.y = shaderTextureYUYV.Sample(SampleType, In.Texture).y;
yuv.z = shaderTextureYUYV.Sample(SampleType, In.Texture).a;
yuv.x = 1.164383561643836 * (yuv.x - 0.0625);
yuv.y = yuv.y - 0.5;
yuv.z = yuv.z - 0.5;
rgba.x = saturate(yuv.x + 1.792741071428571 * yuv.z);
rgba.y = saturate(yuv.x - 0.532909328559444 * yuv.z - 0.21324861427373 * yuv.y);
rgba.z = saturate(yuv.x + 2.112401785714286 * yuv.y);
rgba.a = Opacity;
return rgba;
}
@RomiTT
Copy link
Author

RomiTT commented Sep 30, 2019

if (height <= 576) {
// SD VIDEO
BT601_SHADER.
}
else if (height <= 1080) {
// HD VIDEO
BT709_SHADER.
}
else {
// UHD VIDEO
BT2020_SHADER.
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment