Skip to content

Commit

Permalink
Decode video frame on gpu via pixel shader
Browse files Browse the repository at this point in the history
  • Loading branch information
SaiyansKing committed Oct 3, 2023
1 parent 2435f19 commit a00243e
Show file tree
Hide file tree
Showing 5 changed files with 233 additions and 247 deletions.
3 changes: 3 additions & 0 deletions D3D11Engine/D3D11ShaderManager.cpp
Expand Up @@ -320,6 +320,9 @@ XRESULT D3D11ShaderManager::Init() {
Shaders.push_back( ShaderInfo( "PS_FixedFunctionPipe", "PS_FixedFunctionPipe.hlsl", "p" ) );
Shaders.back().cBufferSizes.push_back( sizeof( GothicGraphicsState ) );

Shaders.push_back( ShaderInfo( "PS_Video", "PS_Video.hlsl", "p" ) );
Shaders.back().cBufferSizes.push_back( sizeof( GothicGraphicsState ) );

Shaders.push_back( ShaderInfo( "PS_DS_PointLight", "PS_DS_PointLight.hlsl", "p" ) );
Shaders.back().cBufferSizes.push_back( sizeof( DS_PointLightConstantBuffer ) );

Expand Down
8 changes: 6 additions & 2 deletions D3D11Engine/D3D11Texture.cpp
Expand Up @@ -144,7 +144,9 @@ UINT D3D11Texture::GetRowPitchBytes( int mip ) {
int px = (TextureSize.x >> mip);
//int py = (TextureSize.y >> mip);

if ( TextureFormat == DXGI_FORMAT_BC1_UNORM || TextureFormat == DXGI_FORMAT_BC2_UNORM ||
if ( TextureFormat == DXGI_FORMAT_R8_UNORM ) {
return px;
} else if ( TextureFormat == DXGI_FORMAT_BC1_UNORM || TextureFormat == DXGI_FORMAT_BC2_UNORM ||
TextureFormat == DXGI_FORMAT_BC3_UNORM ) {
return Toolbox::GetDDSRowPitchSize( px, TextureFormat == DXGI_FORMAT_BC1_UNORM );
} else { // Use B8G8R8A8
Expand All @@ -157,7 +159,9 @@ UINT D3D11Texture::GetSizeInBytes( int mip ) {
int px = (TextureSize.x >> mip);
int py = (TextureSize.y >> mip);

if ( TextureFormat == DXGI_FORMAT_BC1_UNORM || TextureFormat == DXGI_FORMAT_BC2_UNORM ||
if ( TextureFormat == DXGI_FORMAT_R8_UNORM ) {
return px * py;
} else if ( TextureFormat == DXGI_FORMAT_BC1_UNORM || TextureFormat == DXGI_FORMAT_BC2_UNORM ||
TextureFormat == DXGI_FORMAT_BC3_UNORM ) {
return Toolbox::GetDDSStorageRequirements( px, py, TextureFormat == DXGI_FORMAT_BC1_UNORM );
} else { // Use B8G8R8A8
Expand Down
1 change: 1 addition & 0 deletions D3D11Engine/D3D11Texture.h
Expand Up @@ -8,6 +8,7 @@ class D3D11Texture {

/** Layec out for DXGI */
enum ETextureFormat {
TF_R8 = DXGI_FORMAT_R8_UNORM,
TF_B8G8R8A8 = DXGI_FORMAT_B8G8R8A8_UNORM,
TF_DXT1 = DXGI_FORMAT_BC1_UNORM,
TF_DXT3 = DXGI_FORMAT_BC2_UNORM,
Expand Down
47 changes: 47 additions & 0 deletions D3D11Engine/Shaders/PS_Video.hlsl
@@ -0,0 +1,47 @@
//--------------------------------------------------------------------------------------
// Textures and Samplers
//--------------------------------------------------------------------------------------
SamplerState SS_Linear : register( s0 );
Texture2D TX_TextureY : register( t0 );
Texture2D TX_TextureU : register( t1 );
Texture2D TX_TextureV : register( t2 );

//--------------------------------------------------------------------------------------
// Input / Output structures
//--------------------------------------------------------------------------------------
struct PS_INPUT
{
float2 vTexcoord : TEXCOORD0;
float2 vTexcoord2 : TEXCOORD1;
float4 vDiffuse : TEXCOORD2;
float3 vNormalVS : TEXCOORD4;
float3 vViewPosition : TEXCOORD5;
float4 vPosition : SV_POSITION;
};

//--------------------------------------------------------------------------------------
// Pixel Shader
//--------------------------------------------------------------------------------------
float4 PSMain( PS_INPUT Input ) : SV_TARGET
{
const float3 offset = {-0.0627451017, -0.501960814, -0.501960814};
const float3 Rcoeff = {1.1644, 0.0000, 1.7927};
const float3 Gcoeff = {1.1644, -0.2132, -0.5329};
const float3 Bcoeff = {1.1644, 2.1124, 0.0000};

float4 color;

float3 yuv;
yuv.x = TX_TextureY.Sample(SS_Linear, Input.vTexcoord).r;
yuv.y = TX_TextureU.Sample(SS_Linear, Input.vTexcoord).r;
yuv.z = TX_TextureV.Sample(SS_Linear, Input.vTexcoord).r;

yuv += offset;
color.r = saturate(dot(yuv, Rcoeff));
color.g = saturate(dot(yuv, Gcoeff));
color.b = saturate(dot(yuv, Bcoeff));
color.a = 1.0f;

return color;
}

0 comments on commit a00243e

Please sign in to comment.