Fix aspect fit ratio for video

This commit is contained in:
2025-10-08 00:23:26 +09:00
parent e0aa81ed72
commit 8b6e8943de

View File

@@ -307,22 +307,7 @@ HRESULT YUV420PUploadBackend::CreateGraphicsResources() {
HRESULT YUV420PUploadBackend::CompileGraphicsShaders() {
const char* vsSource = R"(
struct VSOutput {
float4 pos : SV_POSITION;
float2 uv : TEXCOORD0;
};
VSOutput main(uint id : SV_VertexID) {
VSOutput o;
o.uv = float2((id << 1) & 2, id & 2);
o.pos = float4(o.uv * float2(2, -2) + float2(-1, 1), 0, 1);
return o;
})";
const char* psSource = R"(
Texture2D t : register(t0);
SamplerState s : register(s0);
cbuffer C : register(b0) {
cbuffer AspectFitBuffer : register(b0) {
float videoAspectRatio;
float containerAspectRatio;
float uvScaleX;
@@ -332,11 +317,36 @@ cbuffer C : register(b0) {
float2 padding;
};
struct VSOutput {
float4 pos : SV_POSITION;
float2 uv : TEXCOORD0;
};
VSOutput main(uint id : SV_VertexID) {
VSOutput o;
// Generate fullscreen triangle UVs (0-1 range)
o.uv = float2((id << 1) & 2, id & 2);
// Apply AspectFit transformation to vertex positions
float2 scale = float2(uvScaleX, uvScaleY);
float2 offset = float2(uvOffsetX, uvOffsetY);
// Transform NDC coordinates for AspectFit
float2 ndcPos = o.uv * 2.0 - 1.0; // Convert to -1 to 1
ndcPos *= scale; // Apply scale
ndcPos.y *= -1.0; // Flip Y for D3D
o.pos = float4(ndcPos, 0, 1);
return o;
})";
const char* psSource = R"(
Texture2D t : register(t0);
SamplerState s : register(s0);
float4 main(float4 pos : SV_POSITION, float2 uv : TEXCOORD0) : SV_TARGET {
// Apply AspectFit transformation
float2 transformedUV = uv * float2(uvScaleX, uvScaleY) + float2(uvOffsetX, uvOffsetY);
float4 color = t.Sample(s, transformedUV);
// RGB texture (RGBA format) to BGRA backbuffer - no swizzle needed
// Sample texture directly - AspectFit is handled in vertex shader
float4 color = t.Sample(s, uv);
return color;
})";
@@ -350,8 +360,8 @@ float4 main(float4 pos : SV_POSITION, float2 uv : TEXCOORD0) : SV_TARGET {
HRESULT YUV420PUploadBackend::CreateGraphicsRootSignature() {
CD3DX12_DESCRIPTOR_RANGE srvRange; srvRange.Init(D3D12_DESCRIPTOR_RANGE_TYPE_SRV, 1, 0);
CD3DX12_ROOT_PARAMETER rootParams[2];
rootParams[0].InitAsDescriptorTable(1, &srvRange);
rootParams[1].InitAsConstantBufferView(0);
rootParams[0].InitAsDescriptorTable(1, &srvRange, D3D12_SHADER_VISIBILITY_PIXEL);
rootParams[1].InitAsConstantBufferView(0, 0, D3D12_SHADER_VISIBILITY_VERTEX); // Bind to vertex shader
D3D12_STATIC_SAMPLER_DESC samplerDesc = {};
samplerDesc.Filter = D3D12_FILTER_MIN_MAG_MIP_LINEAR;