I have converted an UYVY image to RGB in pixel shader and the rendered target to texture of RGB bitmap loses exactly one pixel both in width and height.
I have tried if it's an issue of viewport or not. Before rendering the viewport first four floats (TopLeftX, TopLeftY, Width, Height) are (0.0f, 0.0f, 768.0f, 576.0f). I have checked the conversion algorithm from UYVY to RGB in shader. If something wrong with shader algorithm the image will show wrong pixel pixel data which is not. But I am worried about the sampler description I have used. My Sampler Description is following:
const D3D11_SAMPLER_DESC samplerDesc = {
D3D11_FILTER_MIN_MAG_POINT_MIP_LINEAR,
D3D11_TEXTURE_ADDRESS_WRAP,
D3D11_TEXTURE_ADDRESS_WRAP,
D3D11_TEXTURE_ADDRESS_WRAP,
0, 1, D3D11_COMPARISON_NEVER,
{0.0f, 0.0f, 0.0f, 0.0f}, 0, FLT_MAX };
I am losing exactly one pixel width and one pixel height. My converted image target 768 x 576. But the image is showing 767 x 575. The one pixel of black border is showing to the right and bottom in render to image and also in rendering. The one pixel border has black color because I have cleared target view initially in black color.
If anyone can pin point where should I look at to fix this issue it will helpful for me.
Update:
Input layout:
const std::array<D3D11_INPUT_ELEMENT_DESC, 2> PosTexMixVideoLayout =
{
{
{ "POSITION", 0, DXGI_FORMAT_R32G32B32A32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 },
{ "TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT, 0, D3D11_APPEND_ALIGNED_ELEMENT, D3D11_INPUT_PER_VERTEX_DATA, 0 },
}
};
Input structure:
struct VSPosTextOverlay
{
float4 position : POSITION0;
float2 texOverlay : TEXCOORD0;
float2 texImage : TEXCOORD1;
};
struct VSPosVideoTexture
{
float4 position : POSITION0;
float2 texOverlay : TEXCOORD0;
float2 texImage : TEXCOORD1;
};
UYVY to RGB conversion in pixel shader:
/// <summary>Function to convert Uyvy to Rgb.</summary>
/// <param name="texImage">The tex image.</param>
/// <returns>A float4 value of RGB.</returns>
/// <remarks>
/// This function converts the Uyvy data contained in the input X8R8G8B8 texture
/// to Rgb. For even pixels the luminance value .g ist used, for odd pixels .a.
///
/// Formula for color values in the range 0-255:
/// R = 1.164(Y-16) + 1.596(Cr-128)
/// G = 1.164(Y-16) - 0.813(Cr-128) - 0.391(Cb-128)
/// B = 1.164(Y-16) + 2.018(Cb-128)
/// Note that the color values returned from the texture sampler are
/// in the range 0-1.
/// </remarks>
static inline float4 Uyvy2Rgb(in float2 texImage)
{
// check whether we render an odd or even pixel
// odd == 0.0 for even and odd == 1.0 for odd pixels
float odd = step(frac(texImage.x * halfWidth), 0.5f);
float4 uyvyColor = float4(uyvyImageTexture.Sample(uyvyImageSampler, texImage));
float y = lerp(uyvyColor.a, uyvyColor.g, odd);
y = 1.164f * (y - 0.0625f);
uyvyColor.r -= 0.5f;
uyvyColor.b -= 0.5f;
uyvyColor.r *= saturation;
uyvyColor.b *= saturation;
float4 rgbColor;
rgbColor.a = 1.0f;
rgbColor.r = y + 1.596f * uyvyColor.r;
rgbColor.g = y - 0.813f * uyvyColor.r - 0.391f * uyvyColor.b;
rgbColor.b = y + 2.018f * uyvyColor.b;
return rgbColor;
}