Blend cursor onto the image

This commit is contained in:
loki
2021-05-06 12:36:26 +02:00
parent 0232d8027c
commit 0f661e467e
2 changed files with 41 additions and 21 deletions
+1 -13
View File
@@ -2,25 +2,13 @@ Texture2D image : register(t0);
SamplerState def_sampler : register(s0); SamplerState def_sampler : register(s0);
cbuffer ColorMatrix : register(b0) {
float4 color_vec_y;
float4 color_vec_u;
float4 color_vec_v;
};
struct PS_INPUT struct PS_INPUT
{ {
float4 pos : SV_POSITION; float4 pos : SV_POSITION;
float2 tex : TEXCOORD; float2 tex : TEXCOORD;
}; };
//--------------------------------------------------------------------------------------
// Pixel Shader
//--------------------------------------------------------------------------------------
float4 main_ps(PS_INPUT frag_in) : SV_Target float4 main_ps(PS_INPUT frag_in) : SV_Target
{ {
float4 color = image.Sample(def_sampler, frag_in.tex, 0); return image.Sample(def_sampler, frag_in.tex, 0);
clip(color.a < 0.1f ? -1 : 1);
return color;
} }
+40 -8
View File
@@ -69,13 +69,40 @@ buf_t make_buffer(device_t::pointer device, const T& t) {
buf_t::pointer buf_p; buf_t::pointer buf_p;
auto status = device->CreateBuffer(&buffer_desc, &init_data, &buf_p); auto status = device->CreateBuffer(&buffer_desc, &init_data, &buf_p);
if(status) { if(status) {
BOOST_LOG(error) << "Failed to create buffer"sv; BOOST_LOG(error) << "Failed to create buffer: [0x"sv << util::hex(status).to_string_view() << ']';
return nullptr; return nullptr;
} }
return buf_t { buf_p }; return buf_t { buf_p };
} }
blend_t make_blend(device_t::pointer device, bool enable) {
D3D11_BLEND_DESC bdesc {};
auto &rt = bdesc.RenderTarget[0];
rt.BlendEnable = enable;
rt.RenderTargetWriteMask = D3D11_COLOR_WRITE_ENABLE_ALL;
if(enable) {
rt.BlendOp = D3D11_BLEND_OP_ADD;
rt.BlendOpAlpha = D3D11_BLEND_OP_ADD;
rt.SrcBlend = D3D11_BLEND_SRC_ALPHA;
rt.DestBlend = D3D11_BLEND_INV_SRC_ALPHA;
rt.SrcBlendAlpha = D3D11_BLEND_ZERO;
rt.DestBlendAlpha = D3D11_BLEND_ZERO;
}
blend_t::pointer blend_p;
auto status = device->CreateBlendState(&bdesc, &blend_p);
if(status) {
BOOST_LOG(error) << "Failed to create blend state: [0x"sv << util::hex(status).to_string_view() << ']';
return nullptr;
}
return blend_t { blend_p };
}
blob_t merge_UV_vs_hlsl; blob_t merge_UV_vs_hlsl;
blob_t merge_UV_ps_hlsl; blob_t merge_UV_ps_hlsl;
blob_t merge_Y_vs_hlsl; blob_t merge_Y_vs_hlsl;
@@ -290,12 +317,12 @@ public:
device_ctx_p->PSSetShaderResources(0, 1, &input_res_p); device_ctx_p->PSSetShaderResources(0, 1, &input_res_p);
device_ctx_p->Draw(3, 0); device_ctx_p->Draw(3, 0);
device_ctx_p->Flush();
device_ctx_p->OMSetBlendState(blend_enable.get(), nullptr, 0xFFFFFFFFu);
device_ctx_p->RSSetViewports(1, &cursor_view); device_ctx_p->RSSetViewports(1, &cursor_view);
device_ctx_p->PSSetShaderResources(0, 1, &cursor_res_p); device_ctx_p->PSSetShaderResources(0, 1, &cursor_res_p);
device_ctx_p->Draw(3, 0); device_ctx_p->Draw(3, 0);
device_ctx_p->Flush(); device_ctx_p->OMSetBlendState(blend_disable.get(), nullptr, 0xFFFFFFFFu);
input_res_p = scene_sr.get(); input_res_p = scene_sr.get();
} }
@@ -307,8 +334,6 @@ public:
device_ctx_p->PSSetShaderResources(0, 1, &input_res_p); device_ctx_p->PSSetShaderResources(0, 1, &input_res_p);
device_ctx_p->Draw(3, 0); device_ctx_p->Draw(3, 0);
device_ctx_p->Flush();
_init_view_port(out_width / 2, out_height / 2); _init_view_port(out_width / 2, out_height / 2);
device_ctx_p->OMSetRenderTargets(1, &UV_rt_p, nullptr); device_ctx_p->OMSetRenderTargets(1, &UV_rt_p, nullptr);
device_ctx_p->VSSetShader(merge_UV_vs.get(), nullptr, 0); device_ctx_p->VSSetShader(merge_UV_vs.get(), nullptr, 0);
@@ -374,7 +399,7 @@ public:
vs_t::pointer vs_p; vs_t::pointer vs_p;
status = device_p->CreateVertexShader(merge_Y_vs_hlsl->GetBufferPointer(), merge_Y_vs_hlsl->GetBufferSize(), nullptr, &vs_p); status = device_p->CreateVertexShader(merge_Y_vs_hlsl->GetBufferPointer(), merge_Y_vs_hlsl->GetBufferSize(), nullptr, &vs_p);
if(status) { if(status) {
BOOST_LOG(error) << "Failed to create screen vertex shader [0x"sv << util::hex(status).to_string_view() << ']'; BOOST_LOG(error) << "Failed to create mergeY vertex shader [0x"sv << util::hex(status).to_string_view() << ']';
return -1; return -1;
} }
merge_Y_vs.reset(vs_p); merge_Y_vs.reset(vs_p);
@@ -382,7 +407,7 @@ public:
ps_t::pointer ps_p; ps_t::pointer ps_p;
status = device_p->CreatePixelShader(merge_Y_ps_hlsl->GetBufferPointer(), merge_Y_ps_hlsl->GetBufferSize(), nullptr, &ps_p); status = device_p->CreatePixelShader(merge_Y_ps_hlsl->GetBufferPointer(), merge_Y_ps_hlsl->GetBufferSize(), nullptr, &ps_p);
if(status) { if(status) {
BOOST_LOG(error) << "Failed to create YCrCb pixel shader [0x"sv << util::hex(status).to_string_view() << ']'; BOOST_LOG(error) << "Failed to create mergeY pixel shader [0x"sv << util::hex(status).to_string_view() << ']';
return -1; return -1;
} }
merge_Y_ps.reset(ps_p); merge_Y_ps.reset(ps_p);
@@ -408,6 +433,9 @@ public:
} }
scene_ps.reset(ps_p); scene_ps.reset(ps_p);
blend_disable = make_blend(device_p, false);
blend_enable = make_blend(device_p, true);
if(_init_rt(scene_sr, scene_rt, in_width, in_height, DXGI_FORMAT_B8G8R8A8_UNORM)) { if(_init_rt(scene_sr, scene_rt, in_width, in_height, DXGI_FORMAT_B8G8R8A8_UNORM)) {
return -1; return -1;
} }
@@ -418,7 +446,7 @@ public:
return -1; return -1;
} }
float info_in[16] { 1.0f / (float)out_width }; //aligned to 16-byte float info_in[16 / sizeof(float)] { 1.0f / (float)out_width }; //aligned to 16-byte
info_scene = make_buffer(device_p, info_in); info_scene = make_buffer(device_p, info_in);
if(!info_in) { if(!info_in) {
BOOST_LOG(error) << "Failed to create info scene buffer"sv; BOOST_LOG(error) << "Failed to create info scene buffer"sv;
@@ -502,6 +530,7 @@ public:
auto sampler_linear_p = sampler_linear.get(); auto sampler_linear_p = sampler_linear.get();
auto color_matrix_buf_p = color_matrix.get(); auto color_matrix_buf_p = color_matrix.get();
auto info_buf_p = info_scene.get(); auto info_buf_p = info_scene.get();
device_ctx_p->OMSetBlendState(blend_disable.get(), nullptr, 0xFFFFFFFFu);
device_ctx_p->PSSetSamplers(0, 1, &sampler_linear_p); device_ctx_p->PSSetSamplers(0, 1, &sampler_linear_p);
device_ctx_p->PSSetConstantBuffers(0, 1, &color_matrix_buf_p); device_ctx_p->PSSetConstantBuffers(0, 1, &color_matrix_buf_p);
device_ctx_p->VSSetConstantBuffers(0, 1, &info_buf_p); device_ctx_p->VSSetConstantBuffers(0, 1, &info_buf_p);
@@ -592,6 +621,9 @@ private:
public: public:
color_t *color_p; color_t *color_p;
blend_t blend_enable;
blend_t blend_disable;
buf_t info_scene; buf_t info_scene;
buf_t color_matrix; buf_t color_matrix;