diff --git a/src/d3d9/d3d9_common_texture.cpp b/src/d3d9/d3d9_common_texture.cpp index c3db7fec..679bd69e 100644 --- a/src/d3d9/d3d9_common_texture.cpp +++ b/src/d3d9/d3d9_common_texture.cpp @@ -190,7 +190,10 @@ namespace dxvk { const VkExtent3D blockCount = util::computeBlockCount( mipExtent, formatInfo.blockSize); + const uint32_t planeCount = m_mapping.ConversionFormatInfo.MacroPixelSize.depth; + return formatInfo.elementSize + * planeCount * blockCount.width * blockCount.height * blockCount.depth; diff --git a/src/d3d9/d3d9_format.cpp b/src/d3d9/d3d9_format.cpp index 7e34badb..9a52b414 100644 --- a/src/d3d9/d3d9_format.cpp +++ b/src/d3d9/d3d9_format.cpp @@ -406,6 +406,15 @@ namespace dxvk { { VK_COMPONENT_SWIZZLE_R, VK_COMPONENT_SWIZZLE_R, VK_COMPONENT_SWIZZLE_R, VK_COMPONENT_SWIZZLE_R }}; + case D3D9Format::NV12: return { + VK_FORMAT_R8_UNORM, + VK_FORMAT_UNDEFINED, + VK_IMAGE_ASPECT_COLOR_BIT, + { VK_COMPONENT_SWIZZLE_IDENTITY, VK_COMPONENT_SWIZZLE_IDENTITY, + VK_COMPONENT_SWIZZLE_IDENTITY, VK_COMPONENT_SWIZZLE_IDENTITY }, + { D3D9ConversionFormat_NV12, { 1u, 1u, 2u }, VK_FORMAT_B8G8R8A8_UNORM } + }; + case D3D9Format::RAWZ: return {}; // Unsupported default: diff --git a/src/d3d9/d3d9_format.h b/src/d3d9/d3d9_format.h index 4aa87f58..ab722134 100644 --- a/src/d3d9/d3d9_format.h +++ b/src/d3d9/d3d9_format.h @@ -134,12 +134,13 @@ namespace dxvk { D3D9ConversionFormat_L6V5U5, D3D9ConversionFormat_X8L8V8U8, D3D9ConversionFormat_A2W10V10U10, + D3D9ConversionFormat_NV12, D3D9ConversionFormat_Count }; struct D3D9_CONVERSION_FORMAT_INFO { D3D9ConversionFormat FormatType = D3D9ConversionFormat_None; - VkExtent2D MacroPixelSize = { 1u, 1u }; + VkExtent3D MacroPixelSize = { 1u, 1u, 1u }; VkFormat FormatColor = VK_FORMAT_UNDEFINED; VkFormat FormatSrgb = VK_FORMAT_UNDEFINED; }; diff --git a/src/d3d9/d3d9_format_helpers.cpp b/src/d3d9/d3d9_format_helpers.cpp index 7bd3b2dd..564f30eb 100644 --- a/src/d3d9/d3d9_format_helpers.cpp +++ b/src/d3d9/d3d9_format_helpers.cpp @@ -4,6 +4,7 @@ #include #include #include +#include namespace dxvk { @@ -35,6 +36,10 @@ namespace dxvk { break; } + case D3D9ConversionFormat_NV12: + ConvertGenericFormat(conversionFormat, dstImage, dstSubresource, srcBuffer, VK_FORMAT_R8_UINT, 0); + break; + case D3D9ConversionFormat_L6V5U5: ConvertGenericFormat(conversionFormat, dstImage, dstSubresource, srcBuffer, VK_FORMAT_R16_UINT, 0); break; @@ -108,6 +113,7 @@ namespace dxvk { m_shaders[D3D9ConversionFormat_L6V5U5] = InitShader(d3d9_convert_l6v5u5); m_shaders[D3D9ConversionFormat_X8L8V8U8] = InitShader(d3d9_convert_x8l8v8u8); m_shaders[D3D9ConversionFormat_A2W10V10U10] = InitShader(d3d9_convert_a2w10v10u10); + m_shaders[D3D9ConversionFormat_NV12] = InitShader(d3d9_convert_nv12); } diff --git a/src/d3d9/meson.build b/src/d3d9/meson.build index 2c28d03a..eb08e457 100644 --- a/src/d3d9/meson.build +++ b/src/d3d9/meson.build @@ -6,7 +6,8 @@ d3d9_shaders = files([ 'shaders/d3d9_convert_yuy2_uyvy.comp', 'shaders/d3d9_convert_l6v5u5.comp', 'shaders/d3d9_convert_x8l8v8u8.comp', - 'shaders/d3d9_convert_a2w10v10u10.comp' + 'shaders/d3d9_convert_a2w10v10u10.comp', + 'shaders/d3d9_convert_nv12.comp' ]) d3d9_src = [ diff --git a/src/d3d9/shaders/d3d9_convert_common.h b/src/d3d9/shaders/d3d9_convert_common.h index 30f045f0..6ee55e44 100644 --- a/src/d3d9/shaders/d3d9_convert_common.h +++ b/src/d3d9/shaders/d3d9_convert_common.h @@ -26,4 +26,14 @@ vec4 convertYUV(vec3 yuv) { vec3 value = vec4(yuv, 1 / 255.0) * g_yuv_to_rgb; return vec4(clamp(value, 0, 1), 1); -} \ No newline at end of file +} + +mat3x3 g_bt703_to_rgb = { + { 1.164, 0, 1.793 }, + { 1.164, -0.213, -0.533 }, + { 1.164, 2.112, 0 } +}; + +vec4 convertBT_703(vec3 cde) { + return vec4(clamp(cde * g_bt703_to_rgb, 0, 1), 1); +} diff --git a/src/d3d9/shaders/d3d9_convert_nv12.comp b/src/d3d9/shaders/d3d9_convert_nv12.comp new file mode 100644 index 00000000..2cd85266 --- /dev/null +++ b/src/d3d9/shaders/d3d9_convert_nv12.comp @@ -0,0 +1,56 @@ +#version 450 +#extension GL_GOOGLE_include_directive : enable + +#include "d3d9_convert_common.h" + +layout( + local_size_x = 8, + local_size_y = 8, + local_size_z = 1) in; + +layout(binding = 0) +writeonly uniform image2D dst; + +layout(binding = 1) uniform usamplerBuffer src; + +layout(push_constant) +uniform u_info_t { + uvec2 extent; +} u_info; + +float fetchUnorm(usamplerBuffer source, uint offset) { + return unpackUnorm(texelFetch(src, int(offset)).r); +} + +void main() { + ivec3 thread_id = ivec3(gl_GlobalInvocationID); + + if (all(lessThan(thread_id.xy, u_info.extent))) { + uvec2 pitch = uvec2(u_info.extent.x, u_info.extent.y); + + // Format is: + // YYYYYYYYYYYYYYY... + // UVUVUVUVUVUVUVU... + uint offset = thread_id.x + + thread_id.y * pitch.x; + + float c0 = fetchUnorm(src, offset) - (16 / 255.0); + + // Floor .x to the nearest 2, because + // UV data is in WORDs, and we want to get the color + // for this pixel. + // Then divide thread_id.y by 2 because the macropixel + // layout for chroma data is [2, 2]. + offset = (thread_id.x / 2) * 2 + + thread_id.y / 2 * pitch.x + + pitch.x * pitch.y; + + float u = fetchUnorm(src, offset) - (128 / 255.0); + float v = fetchUnorm(src, offset + 1) - (128 / 255.0); + + // The NV12 format seems to use the BT.703 color space. + vec4 color0 = convertBT_703(vec3(c0, u, v)); + + imageStore(dst, thread_id.xy, color0); + } +} \ No newline at end of file