Skip to content

Commit

Permalink
Second prototype
Browse files Browse the repository at this point in the history
  • Loading branch information
ns6089 committed Jun 30, 2024
1 parent d43d8a1 commit 3a1115d
Show file tree
Hide file tree
Showing 17 changed files with 118 additions and 179 deletions.
34 changes: 22 additions & 12 deletions src/platform/windows/display_vram.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -425,7 +425,7 @@ namespace platf::dxgi {
return -1;
}

auto draw = [&](auto &input, auto &y_or_yuv_viewports, auto &uv_viewport) {
auto draw = [&](auto &input, auto &y_or_yuv_viewports, auto &uv_viewports) {
device_ctx->PSSetShaderResources(0, 1, &input);

// Draw Y/YUV
Expand All @@ -443,20 +443,22 @@ namespace platf::dxgi {
device_ctx->OMSetRenderTargets(1, &out_UV_rtv, nullptr);
device_ctx->VSSetShader(convert_UV_vs.get(), nullptr, 0);
device_ctx->PSSetShader(img.format == DXGI_FORMAT_R16G16B16A16_FLOAT ? convert_UV_fp16_ps.get() : convert_UV_ps.get(), nullptr, 0);
device_ctx->RSSetViewports(1, &uv_viewport);
device_ctx->Draw(3, 0);
viewport_count = recombine_yuv444_into_yuv420 ? 2 : 1;
assert(viewport_count <= uv_viewports.size());
device_ctx->RSSetViewports(viewport_count, uv_viewports.data());
device_ctx->Draw(3 * viewport_count, 0); // vertex shader will spread vertices across viewports
}
};

// Clear render target view(s) once so that the aspect ratio mismatch "bars" appear black
if (!rtvs_cleared) {
auto black = create_black_texture_for_rtv_clear();
if (black) draw(black, out_Y_or_YUV_viewports_for_clear, out_UV_viewport_for_clear);
if (black) draw(black, out_Y_or_YUV_viewports_for_clear, out_UV_viewports_for_clear);
rtvs_cleared = true;
}

// Draw captured frame
draw(img_ctx.encoder_input_res, out_Y_or_YUV_viewports, out_UV_viewport);
draw(img_ctx.encoder_input_res, out_Y_or_YUV_viewports, out_UV_viewports);

// Release encoder mutex to allow capture code to reuse this image
img_ctx.encoder_mutex->ReleaseSync(0);
Expand Down Expand Up @@ -527,6 +529,7 @@ namespace platf::dxgi {
// VP1-> |UL |VL | <-VP2
// | | |
// +---+---+
/*
out_Y_or_YUV_viewports[0] = { offsetX, offsetY, out_width_f, out_height_f, 0.0f, 1.0f }; // Y plane
out_Y_or_YUV_viewports[1] = out_Y_or_YUV_viewports[0]; // left half of U plane
out_Y_or_YUV_viewports[1].TopLeftY += out_height;
Expand All @@ -540,12 +543,19 @@ namespace platf::dxgi {
out_Y_or_YUV_viewports_for_clear[1].Width /= 2;
out_Y_or_YUV_viewports_for_clear[2] = out_Y_or_YUV_viewports_for_clear[1]; // left half of V plane
out_Y_or_YUV_viewports_for_clear[2].TopLeftX += out_Y_or_YUV_viewports_for_clear[1].Width;
*/

out_UV_viewport = out_Y_or_YUV_viewports[0];
out_UV_viewport.TopLeftX = 0;
out_UV_viewport.Width = out_width_f - out_Y_or_YUV_viewports[1].Width;
out_Y_or_YUV_viewports[0] = { offsetX, offsetY, out_width_f, out_height_f, 0.0f, 1.0f }; // Y plane
out_Y_or_YUV_viewports[1] = out_Y_or_YUV_viewports[0]; // left half of U plane
out_Y_or_YUV_viewports[1].TopLeftY += out_height;

out_UV_viewports[0] = { offsetX / 2, offsetY / 2, out_width_f / 2, out_height_f / 2, 0.0f, 1.0f };
out_UV_viewports[1] = out_UV_viewports[0];
out_UV_viewports[1].TopLeftY += out_height / 2;

out_UV_viewport_for_clear = { 0, 0, (float) out_width / 2, (float) out_height, 0.0f, 1.0f };
// TODO: clear viewports
out_Y_or_YUV_viewports_for_clear = out_Y_or_YUV_viewports;
out_UV_viewports_for_clear = out_UV_viewports;
}
else {
out_Y_or_YUV_viewports[0] = { offsetX, offsetY, out_width_f, out_height_f, 0.0f, 1.0f }; // Y plane
Expand All @@ -560,8 +570,8 @@ namespace platf::dxgi {
out_Y_or_YUV_viewports_for_clear[2] = out_Y_or_YUV_viewports_for_clear[1]; // V plane
out_Y_or_YUV_viewports_for_clear[2].TopLeftY += out_height;

out_UV_viewport = { offsetX / 2, offsetY / 2, out_width_f / 2, out_height_f / 2, 0.0f, 1.0f };
out_UV_viewport_for_clear = { 0, 0, (float) out_width / 2, (float) out_height / 2, 0.0f, 1.0f };
out_UV_viewports[0] = { offsetX / 2, offsetY / 2, out_width_f / 2, out_height_f / 2, 0.0f, 1.0f };
out_UV_viewports_for_clear[0] = { 0, 0, (float) out_width / 2, (float) out_height / 2, 0.0f, 1.0f };
}

float subsample_offset_in[16 / sizeof(float)] { 1.0f / (float) out_width_f, 1.0f / (float) out_height_f }; // aligned to 16-byte
Expand Down Expand Up @@ -1024,7 +1034,7 @@ namespace platf::dxgi {
ps_t convert_UV_fp16_ps;

std::array<D3D11_VIEWPORT, 3> out_Y_or_YUV_viewports, out_Y_or_YUV_viewports_for_clear;
D3D11_VIEWPORT out_UV_viewport, out_UV_viewport_for_clear;
std::array<D3D11_VIEWPORT, 2> out_UV_viewports, out_UV_viewports_for_clear;

DXGI_FORMAT format;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include "include/convert_base.hlsl"

#define PROTOTYPE_UV_SAMPLING
#include "include/convert_yuv444in420_ps_base.hlsl"
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include "include/convert_linear_base.hlsl"

#define PROTOTYPE_UV_SAMPLING
#include "include/convert_yuv444in420_ps_base.hlsl"
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#include "include/convert_base.hlsl"

#define PLANAR_VIEWPORTS
#include "include/convert_yuv444in420_ps_base.hlsl"
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#include "include/convert_linear_base.hlsl"

#define PLANAR_VIEWPORTS
#include "include/convert_yuv444in420_ps_base.hlsl"
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "include/convert_base.hlsl"

#define P010
#define PROTOTYPE_UV_SAMPLING
#include "include/convert_yuv444in420_ps_base.hlsl"
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "include/convert_linear_base.hlsl"

#define P010
#define PROTOTYPE_UV_SAMPLING
#include "include/convert_yuv444in420_ps_base.hlsl"
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "include/convert_perceptual_quantizer_base.hlsl"

#define P010
#define PROTOTYPE_UV_SAMPLING
#include "include/convert_yuv444in420_ps_base.hlsl"
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#include "include/convert_base.hlsl"

#define PLANAR_VIEWPORTS
#define P010
#include "include/convert_yuv444in420_ps_base.hlsl"
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#include "include/convert_linear_base.hlsl"

#define PLANAR_VIEWPORTS
#define P010
#include "include/convert_yuv444in420_ps_base.hlsl"
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#include "include/convert_perceptual_quantizer_base.hlsl"

#define PLANAR_VIEWPORTS
#define P010
#include "include/convert_yuv444in420_ps_base.hlsl"
Original file line number Diff line number Diff line change
@@ -1 +1,29 @@
#include "include/convert_yuv444in420_vs_base.hlsl"
cbuffer subsample_offset_cbuffer : register(b0) {
float2 subsample_offset;
};

cbuffer rotate_texture_steps_cbuffer : register(b1) {
int rotate_texture_steps;
};

cbuffer color_matrix_cbuffer : register(b3) {
float4 color_vec_y;
float4 color_vec_u;
float4 color_vec_v;
float2 range_y;
float2 range_uv;
};

#define PLANAR_VIEWPORTS
#define PROTOTYPE_UV_SAMPLING
#include "include/base_vs.hlsl"

vertex_t main_vs(uint vertex_id : SV_VertexID)
{
vertex_t output = generate_fullscreen_triangle_vertex(vertex_id % 3, subsample_offset, vertex_id / 3, rotate_texture_steps);

output.viewport = vertex_id / 3;
output.color_vec = color_vec_v;

return output;
}
Original file line number Diff line number Diff line change
@@ -1,2 +1,30 @@
cbuffer rotate_texture_steps_cbuffer : register(b1) {
int rotate_texture_steps;
};

cbuffer color_matrix_cbuffer : register(b3) {
float4 color_vec_y;
float4 color_vec_u;
float4 color_vec_v;
float2 range_y;
float2 range_uv;
};

#define PLANAR_VIEWPORTS
#include "include/convert_yuv444in420_vs_base.hlsl"
#include "include/base_vs.hlsl"

vertex_t main_vs(uint vertex_id : SV_VertexID)
{
vertex_t output = generate_fullscreen_triangle_vertex(vertex_id % 3, rotate_texture_steps);

output.viewport = vertex_id / 3;

if (output.viewport == 0) {
output.color_vec = color_vec_y;
}
else {
output.color_vec = color_vec_u;
}

return output;
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
vertex_t generate_fullscreen_triangle_vertex(uint vertex_id, float subsample_offset, int rotate_texture_steps)
#elif defined(TOPLEFT_SUBSAMPLING)
vertex_t generate_fullscreen_triangle_vertex(uint vertex_id, float2 subsample_offset, int rotate_texture_steps)
#elif defined(PROTOTYPE_UV_SAMPLING)
vertex_t generate_fullscreen_triangle_vertex(uint vertex_id, float2 subsample_offset, bool bottom_half, int rotate_texture_steps)
#else
vertex_t generate_fullscreen_triangle_vertex(uint vertex_id, int rotate_texture_steps)
#endif
Expand Down Expand Up @@ -37,6 +39,11 @@ vertex_t generate_fullscreen_triangle_vertex(uint vertex_id, int rotate_texture_
#elif defined (TOPLEFT_SUBSAMPLING)
output.tex_right_left_top = float3(tex_coord.x, tex_coord.x - subsample_offset.x, tex_coord.y - subsample_offset.y);
output.tex_right_left_bottom = float3(tex_coord.x, tex_coord.x - subsample_offset.x, tex_coord.y);
#elif defined(PROTOTYPE_UV_SAMPLING)
output.tex_right_left_center = float3(tex_coord.x + subsample_offset.x / 2,
tex_coord.x - subsample_offset.x / 2,
bottom_half ? tex_coord.y + subsample_offset.y / 2 :
tex_coord.y - subsample_offset.y / 2);
#else
output.tex_coord = tex_coord;
#endif
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
struct vertex_t
{
float4 viewpoint_pos : SV_Position;
#if defined(LEFT_SUBSAMPLING)
#if defined(LEFT_SUBSAMPLING) || defined(PROTOTYPE_UV_SAMPLING)
float3 tex_right_left_center : TEXCOORD;
#elif defined (TOPLEFT_SUBSAMPLING)
float3 tex_right_left_top : TEXCOORD;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,51 +1,42 @@
Texture2D image : register(t0);
SamplerState def_sampler : register(s0);

#ifndef PLANAR_VIEWPORTS
cbuffer color_matrix_cbuffer : register(b0) {
float4 color_vec_y;
float4 color_vec_u;
float4 color_vec_v;
float2 range_y;
float2 range_uv;
};
#endif

#define PLANAR_VIEWPORTS
#include "include/base_vs_types.hlsl"

#ifdef PLANAR_VIEWPORTS
uint main_ps(vertex_t input) : SV_Target
#else
#ifdef PROTOTYPE_UV_SAMPLING
uint2 main_ps(vertex_t input) : SV_Target
#else
uint main_ps(vertex_t input) : SV_Target
#endif
{
// Y U V
// +-------+ +---+ +---+
// | | | | | |
// VP0-> | Y | |UR | |VR |
// | | | | | |
// +---+---+ +---+ +---+
// | | |
// VP1-> |UL |VL | <-VP2
// | | |
// +---+---+

float3 rgb = CONVERT_FUNCTION(image.Sample(def_sampler, input.tex_coord, 0).rgb);

#ifdef PLANAR_VIEWPORTS
float y = dot(input.color_vec.xyz, rgb) + input.color_vec.w;
// Y U V
// +-------+ +---+ +---+
// | | |V0 | |V1 |
// | Y | +---+ +---+
// | | |V2 | |V3 |
// +-------+ +---+ +---+
// | |
// | U |
// | |
// +-------+
#ifdef PROTOTYPE_UV_SAMPLING
float3 rgb_left = CONVERT_FUNCTION(image.Sample(def_sampler, input.tex_right_left_center.yz).rgb);
float3 rgb_right = CONVERT_FUNCTION(image.Sample(def_sampler, input.tex_right_left_center.xz).rgb);
uint2 vv = uint2(dot(input.color_vec.xyz, rgb_left) + input.color_vec.w,
dot(input.color_vec.xyz, rgb_right) + input.color_vec.w);
#ifdef P010
return uint(y) << 6;
return vv << 6;
#else
return uint(y);
return vv;
#endif
#else
float u = dot(color_vec_u.xyz, rgb) + color_vec_u.w;
float v = dot(color_vec_v.xyz, rgb) + color_vec_v.w;
float3 rgb = CONVERT_FUNCTION(image.Sample(def_sampler, input.tex_coord, 0).rgb);
uint yu = dot(input.color_vec.xyz, rgb) + input.color_vec.w;
#ifdef P010
return uint2(u, v) << 6;
return yu << 6;
#else
return uint2(u, v);
return yu;
#endif
#endif
}
Loading

0 comments on commit 3a1115d

Please sign in to comment.