d3d12: Add video sink element

Part-of: <https://gitlab.freedesktop.org/gstreamer/gstreamer/-/merge_requests/5870>
This commit is contained in:
Seungha Yang 2023-12-26 17:12:31 +09:00 committed by GStreamer Marge Bot
parent 660f2d7d27
commit 2f091e7118
12 changed files with 3440 additions and 0 deletions

View file

@ -0,0 +1,769 @@
/* GStreamer
* Copyright (C) 2023 Seungha Yang <seungha@centricular.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
* Boston, MA 02120-1301, USA.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "gstd3d12overlaycompositor.h"
#include <directx/d3dx12.h>
#include <wrl.h>
#include <memory>
#include <vector>
#include <algorithm>
#include "PSMain_sample.h"
#include "PSMain_sample_premul.h"
#include "VSMain_coord.h"
GST_DEBUG_CATEGORY_STATIC (gst_d3d12_overlay_compositor_debug);
#define GST_CAT_DEFAULT gst_d3d12_overlay_compositor_debug
/* *INDENT-OFF* */
using namespace Microsoft::WRL;
struct VertexData
{
struct {
FLOAT x;
FLOAT y;
FLOAT z;
} position;
struct {
FLOAT u;
FLOAT v;
} texture;
};
struct GstD3D12OverlayRect : public GstMiniObject
{
~GstD3D12OverlayRect ()
{
if (overlay_rect)
gst_video_overlay_rectangle_unref (overlay_rect);
gst_clear_d3d12_descriptor (&srv_heap);
}
GstVideoOverlayRectangle *overlay_rect = nullptr;
ComPtr<ID3D12Resource> texture;
ComPtr<ID3D12Resource> staging;
ComPtr<ID3D12Resource> vertex_buf;
GstD3D12Descriptor *srv_heap = nullptr;
D3D12_VERTEX_BUFFER_VIEW vbv;
D3D12_PLACED_SUBRESOURCE_FOOTPRINT layout;
gboolean premul_alpha = FALSE;
gboolean need_upload = TRUE;
};
GST_DEFINE_MINI_OBJECT_TYPE (GstD3D12OverlayRect, gst_d3d12_overlay_rect);
struct GstD3D12OverlayCompositorPrivate
{
~GstD3D12OverlayCompositorPrivate ()
{
if (overlays)
g_list_free_full (overlays, (GDestroyNotify) gst_mini_object_unref);
gst_clear_object (&ca_pool);
gst_clear_object (&srv_heap_pool);
}
GstVideoInfo info;
D3D12_VIEWPORT viewport;
D3D12_RECT scissor_rect;
ComPtr<ID3D12RootSignature> rs;
ComPtr<ID3D12PipelineState> pso;
ComPtr<ID3D12PipelineState> pso_premul;
D3D12_INDEX_BUFFER_VIEW idv;
ComPtr<ID3D12Resource> index_buf;
ComPtr<ID3D12GraphicsCommandList> cl;
GstD3D12CommandAllocatorPool *ca_pool = nullptr;
GstD3D12DescriptorPool *srv_heap_pool = nullptr;
GList *overlays = nullptr;
std::vector<GstVideoOverlayRectangle *> rects_to_upload;
};
/* *INDENT-ON* */
struct _GstD3D12OverlayCompositor
{
GstObject parent;
GstD3D12Device *device;
GstD3D12OverlayCompositorPrivate *priv;
};
static void gst_d3d12_overlay_compositor_finalize (GObject * object);
#define gst_d3d12_overlay_compositor_parent_class parent_class
G_DEFINE_TYPE (GstD3D12OverlayCompositor,
gst_d3d12_overlay_compositor, GST_TYPE_OBJECT);
static void
gst_d3d12_overlay_compositor_class_init (GstD3D12OverlayCompositorClass * klass)
{
GObjectClass *object_class = G_OBJECT_CLASS (klass);
object_class->finalize = gst_d3d12_overlay_compositor_finalize;
GST_DEBUG_CATEGORY_INIT (gst_d3d12_overlay_compositor_debug,
"d3d12overlaycompositor", 0, "d3d12overlaycompositor");
}
static void
gst_d3d12_overlay_compositor_init (GstD3D12OverlayCompositor * self)
{
self->priv = new GstD3D12OverlayCompositorPrivate ();
}
static void
gst_d3d12_overlay_compositor_finalize (GObject * object)
{
GstD3D12OverlayCompositor *self = GST_D3D12_OVERLAY_COMPOSITOR (object);
delete self->priv;
gst_clear_object (&self->device);
G_OBJECT_CLASS (parent_class)->finalize (object);
}
static void
gst_d3d12_overlay_rect_free (GstD3D12OverlayRect * rect)
{
if (rect)
delete rect;
}
static GstD3D12OverlayRect *
gst_d3d12_overlay_rect_new (GstD3D12OverlayCompositor * self,
GstVideoOverlayRectangle * overlay_rect)
{
auto priv = self->priv;
gint x, y;
guint width, height;
VertexData vertex_data[4];
FLOAT x1, y1, x2, y2;
gdouble val;
GstVideoOverlayFormatFlags flags;
gboolean premul_alpha = FALSE;
if (!gst_video_overlay_rectangle_get_render_rectangle (overlay_rect, &x, &y,
&width, &height)) {
GST_ERROR_OBJECT (self, "Failed to get render rectangle");
return nullptr;
}
flags = gst_video_overlay_rectangle_get_flags (overlay_rect);
if ((flags & GST_VIDEO_OVERLAY_FORMAT_FLAG_PREMULTIPLIED_ALPHA) != 0) {
premul_alpha = TRUE;
flags = GST_VIDEO_OVERLAY_FORMAT_FLAG_PREMULTIPLIED_ALPHA;
} else {
flags = GST_VIDEO_OVERLAY_FORMAT_FLAG_NONE;
}
auto buf = gst_video_overlay_rectangle_get_pixels_unscaled_argb (overlay_rect,
flags);
if (!buf) {
GST_ERROR_OBJECT (self, "Failed to get overlay buffer");
return nullptr;
}
auto vmeta = gst_buffer_get_video_meta (buf);
if (!vmeta) {
GST_ERROR_OBJECT (self, "Failed to get video meta");
return nullptr;
}
auto device = gst_d3d12_device_get_device_handle (self->device);
D3D12_HEAP_PROPERTIES heap_prop =
CD3DX12_HEAP_PROPERTIES (D3D12_HEAP_TYPE_DEFAULT);
D3D12_RESOURCE_DESC desc =
CD3DX12_RESOURCE_DESC::Tex2D (DXGI_FORMAT_B8G8R8A8_UNORM, vmeta->width,
vmeta->height, 1, 1);
ComPtr < ID3D12Resource > texture;
auto hr = device->CreateCommittedResource (&heap_prop, D3D12_HEAP_FLAG_NONE,
&desc, D3D12_RESOURCE_STATE_COPY_DEST, nullptr, IID_PPV_ARGS (&texture));
if (!gst_d3d12_result (hr, self->device)) {
GST_ERROR_OBJECT (self, "Couldn't create texture");
return nullptr;
}
D3D12_PLACED_SUBRESOURCE_FOOTPRINT layout;
UINT64 size;
device->GetCopyableFootprints (&desc, 0, 1, 0, &layout, nullptr, nullptr,
&size);
ComPtr < ID3D12Resource > staging;
heap_prop = CD3DX12_HEAP_PROPERTIES (D3D12_HEAP_TYPE_UPLOAD);
desc = CD3DX12_RESOURCE_DESC::Buffer (size);
hr = device->CreateCommittedResource (&heap_prop, D3D12_HEAP_FLAG_NONE,
&desc, D3D12_RESOURCE_STATE_GENERIC_READ, nullptr,
IID_PPV_ARGS (&staging));
if (!gst_d3d12_result (hr, self->device)) {
GST_ERROR_OBJECT (self, "Couldn't create upload buffer");
return nullptr;
}
guint8 *map_data;
hr = staging->Map (0, nullptr, (void **) &map_data);
if (!gst_d3d12_result (hr, self->device)) {
GST_ERROR_OBJECT (self, "Couldn't map staging");
return nullptr;
}
guint8 *data;
gint stride;
GstMapInfo info;
if (!gst_video_meta_map (vmeta,
0, &info, (gpointer *) & data, &stride, GST_MAP_READ)) {
GST_ERROR_OBJECT (self, "Failed to map");
return nullptr;
}
if (layout.Footprint.RowPitch == (UINT) stride) {
memcpy (map_data, data, stride * layout.Footprint.Height);
} else {
guint width_in_bytes = 4 * layout.Footprint.Width;
for (UINT i = 0; i < layout.Footprint.Height; i++) {
memcpy (map_data, data, width_in_bytes);
map_data += layout.Footprint.RowPitch;
data += stride;
}
}
staging->Unmap (0, nullptr);
gst_video_meta_unmap (vmeta, 0, &info);
/* bottom left */
gst_util_fraction_to_double (x, GST_VIDEO_INFO_WIDTH (&priv->info), &val);
x1 = (val * 2.0f) - 1.0f;
gst_util_fraction_to_double (y + height,
GST_VIDEO_INFO_HEIGHT (&priv->info), &val);
y1 = (val * -2.0f) + 1.0f;
/* top right */
gst_util_fraction_to_double (x + width,
GST_VIDEO_INFO_WIDTH (&priv->info), &val);
x2 = (val * 2.0f) - 1.0f;
gst_util_fraction_to_double (y, GST_VIDEO_INFO_HEIGHT (&priv->info), &val);
y2 = (val * -2.0f) + 1.0f;
/* bottom left */
vertex_data[0].position.x = x1;
vertex_data[0].position.y = y1;
vertex_data[0].position.z = 0.0f;
vertex_data[0].texture.u = 0.0f;
vertex_data[0].texture.v = 1.0f;
/* top left */
vertex_data[1].position.x = x1;
vertex_data[1].position.y = y2;
vertex_data[1].position.z = 0.0f;
vertex_data[1].texture.u = 0.0f;
vertex_data[1].texture.v = 0.0f;
/* top right */
vertex_data[2].position.x = x2;
vertex_data[2].position.y = y2;
vertex_data[2].position.z = 0.0f;
vertex_data[2].texture.u = 1.0f;
vertex_data[2].texture.v = 0.0f;
/* bottom right */
vertex_data[3].position.x = x2;
vertex_data[3].position.y = y1;
vertex_data[3].position.z = 0.0f;
vertex_data[3].texture.u = 1.0f;
vertex_data[3].texture.v = 1.0f;
ComPtr < ID3D12Resource > vertex_buf;
heap_prop = CD3DX12_HEAP_PROPERTIES (D3D12_HEAP_TYPE_UPLOAD);
desc = CD3DX12_RESOURCE_DESC::Buffer (sizeof (VertexData) * 4);
hr = device->CreateCommittedResource (&heap_prop, D3D12_HEAP_FLAG_NONE,
&desc, D3D12_RESOURCE_STATE_GENERIC_READ, nullptr,
IID_PPV_ARGS (&vertex_buf));
if (!gst_d3d12_result (hr, self->device)) {
GST_ERROR_OBJECT (self, "Couldn't create vertex buffer");
return nullptr;
}
hr = vertex_buf->Map (0, nullptr, (void **) &map_data);
if (!gst_d3d12_result (hr, self->device)) {
GST_ERROR_OBJECT (self, "Couldn't map vertex buffer");
return nullptr;
}
memcpy (map_data, vertex_data, sizeof (VertexData) * 4);
vertex_buf->Unmap (0, nullptr);
GstD3D12Descriptor *srv_heap;
if (!gst_d3d12_descriptor_pool_acquire (priv->srv_heap_pool, &srv_heap)) {
GST_ERROR_OBJECT (self, "Couldn't acquire command allocator");
return nullptr;
}
ComPtr < ID3D12DescriptorHeap > srv_heap_handle;
gst_d3d12_descriptor_get_handle (srv_heap, &srv_heap_handle);
D3D12_SHADER_RESOURCE_VIEW_DESC srv_desc = { };
srv_desc.ViewDimension = D3D12_SRV_DIMENSION_TEXTURE2D;
srv_desc.Shader4ComponentMapping = D3D12_DEFAULT_SHADER_4_COMPONENT_MAPPING;
srv_desc.Texture2D.MipLevels = 1;
srv_desc.Format = DXGI_FORMAT_B8G8R8A8_UNORM;
device->CreateShaderResourceView (texture.Get (), &srv_desc,
srv_heap_handle->GetCPUDescriptorHandleForHeapStart ());
auto rect = new GstD3D12OverlayRect ();
gst_mini_object_init (rect, 0, gst_d3d12_overlay_rect_get_type (),
nullptr, nullptr,
(GstMiniObjectFreeFunction) gst_d3d12_overlay_rect_free);
rect->overlay_rect = gst_video_overlay_rectangle_ref (overlay_rect);
rect->texture = texture;
rect->staging = staging;
rect->vertex_buf = vertex_buf;
rect->vbv.BufferLocation = vertex_buf->GetGPUVirtualAddress ();
rect->vbv.SizeInBytes = sizeof (VertexData) * 4;
rect->vbv.StrideInBytes = sizeof (VertexData);
rect->layout = layout;
rect->srv_heap = srv_heap;
rect->premul_alpha = premul_alpha;
return rect;
}
static gboolean
gst_d3d12_overlay_compositor_setup_shader (GstD3D12OverlayCompositor * self)
{
auto priv = self->priv;
GstVideoInfo *info = &priv->info;
const WORD indices[6] = { 0, 1, 2, 3, 0, 2 };
const D3D12_ROOT_SIGNATURE_FLAGS rs_flags =
D3D12_ROOT_SIGNATURE_FLAG_ALLOW_INPUT_ASSEMBLER_INPUT_LAYOUT |
D3D12_ROOT_SIGNATURE_FLAG_DENY_HULL_SHADER_ROOT_ACCESS |
D3D12_ROOT_SIGNATURE_FLAG_DENY_DOMAIN_SHADER_ROOT_ACCESS |
D3D12_ROOT_SIGNATURE_FLAG_DENY_GEOMETRY_SHADER_ROOT_ACCESS |
D3D12_ROOT_SIGNATURE_FLAG_DENY_AMPLIFICATION_SHADER_ROOT_ACCESS |
D3D12_ROOT_SIGNATURE_FLAG_DENY_MESH_SHADER_ROOT_ACCESS;
const D3D12_STATIC_SAMPLER_DESC static_sampler_desc = {
D3D12_FILTER_MIN_MAG_LINEAR_MIP_POINT,
D3D12_TEXTURE_ADDRESS_MODE_CLAMP,
D3D12_TEXTURE_ADDRESS_MODE_CLAMP,
D3D12_TEXTURE_ADDRESS_MODE_CLAMP,
0,
1,
D3D12_COMPARISON_FUNC_ALWAYS,
D3D12_STATIC_BORDER_COLOR_OPAQUE_BLACK,
0,
D3D12_FLOAT32_MAX,
0,
0,
D3D12_SHADER_VISIBILITY_PIXEL
};
CD3DX12_ROOT_PARAMETER param;
D3D12_DESCRIPTOR_RANGE range;
std::vector < D3D12_ROOT_PARAMETER > param_list;
range = CD3DX12_DESCRIPTOR_RANGE (D3D12_DESCRIPTOR_RANGE_TYPE_SRV, 1, 0);
param.InitAsDescriptorTable (1, &range, D3D12_SHADER_VISIBILITY_PIXEL);
param_list.push_back (param);
D3D12_VERSIONED_ROOT_SIGNATURE_DESC rs_desc = { };
CD3DX12_VERSIONED_ROOT_SIGNATURE_DESC::Init_1_0 (rs_desc,
param_list.size (), param_list.data (),
1, &static_sampler_desc, rs_flags);
ComPtr < ID3DBlob > rs_blob;
ComPtr < ID3DBlob > error_blob;
auto hr = D3DX12SerializeVersionedRootSignature (&rs_desc,
D3D_ROOT_SIGNATURE_VERSION_1_1, &rs_blob, &error_blob);
if (!gst_d3d12_result (hr, self->device)) {
const gchar *error_msg = nullptr;
if (error_blob)
error_msg = (const gchar *) error_blob->GetBufferPointer ();
GST_ERROR_OBJECT (self, "Couldn't serialize root signature, error: %s",
GST_STR_NULL (error_msg));
return FALSE;
}
GstD3D12Format device_format;
gst_d3d12_device_get_format (self->device, GST_VIDEO_INFO_FORMAT (info),
&device_format);
auto device = gst_d3d12_device_get_device_handle (self->device);
ComPtr < ID3D12RootSignature > rs;
device->CreateRootSignature (0, rs_blob->GetBufferPointer (),
rs_blob->GetBufferSize (), IID_PPV_ARGS (&rs));
D3D12_INPUT_ELEMENT_DESC input_desc[2];
input_desc[0].SemanticName = "POSITION";
input_desc[0].SemanticIndex = 0;
input_desc[0].Format = DXGI_FORMAT_R32G32B32_FLOAT;
input_desc[0].InputSlot = 0;
input_desc[0].AlignedByteOffset = D3D12_APPEND_ALIGNED_ELEMENT;
input_desc[0].InputSlotClass = D3D12_INPUT_CLASSIFICATION_PER_VERTEX_DATA;
input_desc[0].InstanceDataStepRate = 0;
input_desc[1].SemanticName = "TEXCOORD";
input_desc[1].SemanticIndex = 0;
input_desc[1].Format = DXGI_FORMAT_R32G32_FLOAT;
input_desc[1].InputSlot = 0;
input_desc[1].AlignedByteOffset = D3D12_APPEND_ALIGNED_ELEMENT;
input_desc[1].InputSlotClass = D3D12_INPUT_CLASSIFICATION_PER_VERTEX_DATA;
input_desc[1].InstanceDataStepRate = 0;
D3D12_GRAPHICS_PIPELINE_STATE_DESC pso_desc = { };
pso_desc.pRootSignature = rs.Get ();
pso_desc.VS.BytecodeLength = sizeof (g_VSMain_coord);
pso_desc.VS.pShaderBytecode = g_VSMain_coord;
pso_desc.PS.BytecodeLength = sizeof (g_PSMain_sample);
pso_desc.PS.pShaderBytecode = g_PSMain_sample;
pso_desc.BlendState = CD3DX12_BLEND_DESC (D3D12_DEFAULT);
pso_desc.BlendState.RenderTarget[0].BlendEnable = TRUE;
pso_desc.BlendState.RenderTarget[0].LogicOpEnable = FALSE;
pso_desc.BlendState.RenderTarget[0].SrcBlend = D3D12_BLEND_SRC_ALPHA;
pso_desc.BlendState.RenderTarget[0].DestBlend = D3D12_BLEND_INV_SRC_ALPHA;
pso_desc.BlendState.RenderTarget[0].BlendOp = D3D12_BLEND_OP_ADD;
pso_desc.BlendState.RenderTarget[0].SrcBlendAlpha = D3D12_BLEND_ONE;
pso_desc.BlendState.RenderTarget[0].DestBlendAlpha =
D3D12_BLEND_INV_SRC_ALPHA;
pso_desc.BlendState.RenderTarget[0].BlendOpAlpha = D3D12_BLEND_OP_ADD;
pso_desc.BlendState.RenderTarget[0].LogicOp = D3D12_LOGIC_OP_NOOP;
pso_desc.BlendState.RenderTarget[0].RenderTargetWriteMask =
D3D12_COLOR_WRITE_ENABLE_ALL;
pso_desc.SampleMask = UINT_MAX;
pso_desc.RasterizerState = CD3DX12_RASTERIZER_DESC (D3D12_DEFAULT);
pso_desc.RasterizerState.CullMode = D3D12_CULL_MODE_NONE;
pso_desc.DepthStencilState.DepthEnable = FALSE;
pso_desc.DepthStencilState.StencilEnable = FALSE;
pso_desc.InputLayout.pInputElementDescs = input_desc;
pso_desc.InputLayout.NumElements = G_N_ELEMENTS (input_desc);
pso_desc.PrimitiveTopologyType = D3D12_PRIMITIVE_TOPOLOGY_TYPE_TRIANGLE;
pso_desc.NumRenderTargets = 1;
pso_desc.RTVFormats[0] = device_format.resource_format[0];
pso_desc.SampleDesc.Count = 1;
ComPtr < ID3D12PipelineState > pso;
hr = device->CreateGraphicsPipelineState (&pso_desc, IID_PPV_ARGS (&pso));
if (!gst_d3d12_result (hr, self->device)) {
GST_ERROR_OBJECT (self, "Couldn't create pso");
return FALSE;
}
ComPtr < ID3D12PipelineState > pso_premul;
pso_desc.PS.BytecodeLength = sizeof (g_PSMain_sample_premul);
pso_desc.PS.pShaderBytecode = g_PSMain_sample_premul;
hr = device->CreateGraphicsPipelineState (&pso_desc,
IID_PPV_ARGS (&pso_premul));
if (!gst_d3d12_result (hr, self->device)) {
GST_ERROR_OBJECT (self, "Couldn't create pso");
return FALSE;
}
D3D12_HEAP_PROPERTIES heap_prop =
CD3DX12_HEAP_PROPERTIES (D3D12_HEAP_TYPE_UPLOAD);
D3D12_RESOURCE_DESC buffer_desc =
CD3DX12_RESOURCE_DESC::Buffer (sizeof (indices));
ComPtr < ID3D12Resource > index_buf;
hr = device->CreateCommittedResource (&heap_prop, D3D12_HEAP_FLAG_NONE,
&buffer_desc, D3D12_RESOURCE_STATE_GENERIC_READ, nullptr,
IID_PPV_ARGS (&index_buf));
if (!gst_d3d12_result (hr, self->device)) {
GST_ERROR_OBJECT (self, "Couldn't create index buffer");
return FALSE;
}
void *data;
hr = index_buf->Map (0, nullptr, &data);
if (!gst_d3d12_result (hr, self->device)) {
GST_ERROR_OBJECT (self, "Couldn't map index buffer");
return FALSE;
}
memcpy (data, indices, sizeof (indices));
index_buf->Unmap (0, nullptr);
D3D12_DESCRIPTOR_HEAP_DESC heap_desc = { };
heap_desc.Type = D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV;
heap_desc.NumDescriptors = 1;
heap_desc.Flags = D3D12_DESCRIPTOR_HEAP_FLAG_SHADER_VISIBLE;
priv->rs = rs;
priv->pso = pso;
priv->pso_premul = pso_premul;
priv->idv.BufferLocation = index_buf->GetGPUVirtualAddress ();
priv->idv.SizeInBytes = sizeof (indices);
priv->idv.Format = DXGI_FORMAT_R16_UINT;
priv->index_buf = index_buf;
priv->srv_heap_pool = gst_d3d12_descriptor_pool_new (self->device,
&heap_desc);
priv->ca_pool = gst_d3d12_command_allocator_pool_new (self->device,
D3D12_COMMAND_LIST_TYPE_DIRECT);
priv->viewport.TopLeftX = 0;
priv->viewport.TopLeftY = 0;
priv->viewport.Width = GST_VIDEO_INFO_WIDTH (info);
priv->viewport.Height = GST_VIDEO_INFO_HEIGHT (info);
priv->viewport.MinDepth = 0.0f;
priv->viewport.MaxDepth = 1.0f;
priv->scissor_rect.left = 0;
priv->scissor_rect.top = 0;
priv->scissor_rect.right = GST_VIDEO_INFO_WIDTH (info);
priv->scissor_rect.bottom = GST_VIDEO_INFO_HEIGHT (info);
return TRUE;
}
GstD3D12OverlayCompositor *
gst_d3d12_overlay_compositor_new (GstD3D12Device * device,
const GstVideoInfo * info)
{
GstD3D12OverlayCompositor *self = nullptr;
GstD3D12OverlayCompositorPrivate *priv;
g_return_val_if_fail (GST_IS_D3D12_DEVICE (device), nullptr);
g_return_val_if_fail (info != nullptr, nullptr);
self = (GstD3D12OverlayCompositor *)
g_object_new (GST_TYPE_D3D12_OVERLAY_COMPOSITOR, nullptr);
gst_object_ref_sink (self);
priv = self->priv;
self->device = (GstD3D12Device *) gst_object_ref (device);
priv->info = *info;
if (!gst_d3d12_overlay_compositor_setup_shader (self)) {
gst_object_unref (self);
return nullptr;
}
return self;
}
static gboolean
gst_d3d12_overlay_compositor_foreach_meta (GstBuffer * buffer, GstMeta ** meta,
GstD3D12OverlayCompositor * self)
{
auto priv = self->priv;
if ((*meta)->info->api != GST_VIDEO_OVERLAY_COMPOSITION_META_API_TYPE)
return TRUE;
auto cmeta = (GstVideoOverlayCompositionMeta *) (*meta);
if (!cmeta->overlay)
return TRUE;
auto num_rect = gst_video_overlay_composition_n_rectangles (cmeta->overlay);
for (guint i = 0; i < num_rect; i++) {
auto rect = gst_video_overlay_composition_get_rectangle (cmeta->overlay, i);
priv->rects_to_upload.push_back (rect);
}
return TRUE;
}
gboolean
gst_d3d12_overlay_compositor_upload (GstD3D12OverlayCompositor * compositor,
GstBuffer * buf)
{
g_return_val_if_fail (compositor != nullptr, FALSE);
g_return_val_if_fail (GST_IS_BUFFER (buf), FALSE);
auto priv = compositor->priv;
priv->rects_to_upload.clear ();
gst_buffer_foreach_meta (buf,
(GstBufferForeachMetaFunc) gst_d3d12_overlay_compositor_foreach_meta,
compositor);
if (priv->rects_to_upload.empty ()) {
if (priv->overlays)
g_list_free_full (priv->overlays, (GDestroyNotify) gst_mini_object_unref);
priv->overlays = nullptr;
return TRUE;
}
GST_LOG_OBJECT (compositor, "Found %" G_GSIZE_FORMAT
" overlay rectangles", priv->rects_to_upload.size ());
for (size_t i = 0; i < priv->rects_to_upload.size (); i++) {
GList *iter;
bool found = false;
for (iter = priv->overlays; iter; iter = g_list_next (iter)) {
auto rect = (GstD3D12OverlayRect *) iter->data;
if (rect->overlay_rect == priv->rects_to_upload[i]) {
found = true;
break;
}
}
if (!found) {
auto new_rect = gst_d3d12_overlay_rect_new (compositor,
priv->rects_to_upload[i]);
if (new_rect)
priv->overlays = g_list_append (priv->overlays, new_rect);
}
}
/* Remove old overlay */
GList *iter;
GList *next;
for (iter = priv->overlays; iter; iter = next) {
auto rect = (GstD3D12OverlayRect *) iter->data;
next = g_list_next (iter);
if (std::find_if (priv->rects_to_upload.begin (),
priv->rects_to_upload.end (),[&](const auto & overlay)->bool
{
return overlay == rect->overlay_rect;}
) == priv->rects_to_upload.end ()) {
gst_mini_object_unref (rect);
priv->overlays = g_list_delete_link (priv->overlays, iter);
}
}
return TRUE;
}
gboolean
gst_d3d12_overlay_compositor_update_viewport (GstD3D12OverlayCompositor *
compositor, GstVideoRectangle * viewport)
{
g_return_val_if_fail (GST_IS_D3D12_OVERLAY_COMPOSITOR (compositor), FALSE);
g_return_val_if_fail (viewport != nullptr, FALSE);
auto priv = compositor->priv;
priv->viewport.TopLeftX = viewport->x;
priv->viewport.TopLeftY = viewport->y;
priv->viewport.Width = viewport->w;
priv->viewport.Height = viewport->h;
priv->scissor_rect.left = viewport->x;
priv->scissor_rect.top = viewport->y;
priv->scissor_rect.right = viewport->x + viewport->w;
priv->scissor_rect.bottom = viewport->y + viewport->h;
return TRUE;
}
static gboolean
gst_d3d12_overlay_compositor_execute (GstD3D12OverlayCompositor * self,
GstBuffer * buf, GstD3D12FenceData * fence_data,
ID3D12GraphicsCommandList * cl)
{
auto priv = self->priv;
auto mem = (GstD3D12Memory *) gst_buffer_peek_memory (buf, 0);
ComPtr < ID3D12DescriptorHeap > rtv_heap;
if (!gst_d3d12_memory_get_render_target_view_heap (mem, &rtv_heap)) {
GST_ERROR_OBJECT (self, "Couldn't get rtv heap");
return FALSE;
}
GList *iter;
ComPtr < ID3D12PipelineState > prev_pso;
for (iter = priv->overlays; iter; iter = g_list_next (iter)) {
auto rect = (GstD3D12OverlayRect *) iter->data;
if (rect->need_upload) {
D3D12_TEXTURE_COPY_LOCATION src =
CD3DX12_TEXTURE_COPY_LOCATION (rect->staging.Get (), rect->layout);
D3D12_TEXTURE_COPY_LOCATION dst =
CD3DX12_TEXTURE_COPY_LOCATION (rect->texture.Get ());
GST_LOG_OBJECT (self, "First render, uploading texture");
cl->CopyTextureRegion (&dst, 0, 0, 0, &src, nullptr);
D3D12_RESOURCE_BARRIER barrier =
CD3DX12_RESOURCE_BARRIER::Transition (rect->texture.Get (),
D3D12_RESOURCE_STATE_COPY_DEST,
D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE);
cl->ResourceBarrier (1, &barrier);
rect->need_upload = FALSE;
}
cl->SetGraphicsRootSignature (priv->rs.Get ());
ComPtr < ID3D12PipelineState > pso;
if (rect->premul_alpha)
pso = priv->pso;
else
pso = priv->pso_premul;
if (!prev_pso) {
cl->SetPipelineState (pso.Get ());
cl->IASetIndexBuffer (&priv->idv);
cl->IASetPrimitiveTopology (D3D_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
cl->RSSetViewports (1, &priv->viewport);
cl->RSSetScissorRects (1, &priv->scissor_rect);
D3D12_CPU_DESCRIPTOR_HANDLE rtv_heaps[] = {
rtv_heap->GetCPUDescriptorHandleForHeapStart ()
};
cl->OMSetRenderTargets (1, rtv_heaps, FALSE, nullptr);
} else if (pso != prev_pso) {
cl->SetPipelineState (pso.Get ());
}
ComPtr < ID3D12DescriptorHeap > srv_heap;
gst_d3d12_descriptor_get_handle (rect->srv_heap, &srv_heap);
ID3D12DescriptorHeap *heaps[] = { srv_heap.Get () };
cl->SetDescriptorHeaps (1, heaps);
cl->SetGraphicsRootDescriptorTable (0,
srv_heap->GetGPUDescriptorHandleForHeapStart ());
cl->IASetVertexBuffers (0, 1, &rect->vbv);
cl->DrawIndexedInstanced (6, 1, 0, 0, 0);
gst_d3d12_fence_data_add_notify (fence_data, gst_mini_object_ref (rect),
(GDestroyNotify) gst_mini_object_unref);
prev_pso = nullptr;
prev_pso = pso;
}
return TRUE;
}
gboolean
gst_d3d12_overlay_compositor_draw (GstD3D12OverlayCompositor * compositor,
GstBuffer * buf, GstD3D12FenceData * fence_data,
ID3D12GraphicsCommandList * command_list)
{
g_return_val_if_fail (compositor != nullptr, FALSE);
g_return_val_if_fail (GST_IS_BUFFER (buf), FALSE);
g_return_val_if_fail (fence_data, FALSE);
g_return_val_if_fail (command_list, FALSE);
auto priv = compositor->priv;
if (!priv->overlays)
return TRUE;
return gst_d3d12_overlay_compositor_execute (compositor,
buf, fence_data, command_list);
}

View file

@ -0,0 +1,47 @@
/* GStreamer
* Copyright (C) 2023 Seungha Yang <seungha@centricular.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
* Boston, MA 02120-1301, USA.
*/
#pragma once
#include <gst/gst.h>
#include <gst/video/video.h>
#include "gstd3d12.h"
G_BEGIN_DECLS
#define GST_TYPE_D3D12_OVERLAY_COMPOSITOR (gst_d3d12_overlay_compositor_get_type())
G_DECLARE_FINAL_TYPE (GstD3D12OverlayCompositor, gst_d3d12_overlay_compositor,
GST, D3D12_OVERLAY_COMPOSITOR, GstObject)
GstD3D12OverlayCompositor * gst_d3d12_overlay_compositor_new (GstD3D12Device * device,
const GstVideoInfo * info);
gboolean gst_d3d12_overlay_compositor_upload (GstD3D12OverlayCompositor * compositor,
GstBuffer * buf);
gboolean gst_d3d12_overlay_compositor_update_viewport (GstD3D12OverlayCompositor * compositor,
GstVideoRectangle * viewport);
gboolean gst_d3d12_overlay_compositor_draw (GstD3D12OverlayCompositor * compositor,
GstBuffer * buf,
GstD3D12FenceData * fence_data,
ID3D12GraphicsCommandList * command_list);
G_END_DECLS

View file

@ -0,0 +1,958 @@
/* GStreamer
* Copyright (C) 2023 Seungha Yang <seungha@centricular.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "gstd3d12videosink.h"
#include "gstd3d12pluginutils.h"
#include "gstd3d12window.h"
#include <mutex>
#include <atomic>
#include <string>
enum
{
PROP_0,
PROP_ADAPTER,
PROP_FORCE_ASPECT_RATIO,
PROP_ENABLE_NAVIGATION_EVENTS,
PROP_ROTATE_METHOD,
};
#define DEFAULT_ADAPTER -1
#define DEFAULT_FORCE_ASPECT_RATIO TRUE
#define DEFAULT_ENABLE_NAVIGATION_EVENTS TRUE
#define DEFAULT_ROTATE_METHOD GST_VIDEO_ORIENTATION_IDENTITY
static GstStaticPadTemplate sink_template =
GST_STATIC_PAD_TEMPLATE ("sink", GST_PAD_SINK, GST_PAD_ALWAYS,
GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE_WITH_FEATURES
(GST_CAPS_FEATURE_MEMORY_D3D12_MEMORY, GST_D3D12_ALL_FORMATS) "; "
GST_VIDEO_CAPS_MAKE_WITH_FEATURES
(GST_CAPS_FEATURE_MEMORY_D3D12_MEMORY ","
GST_CAPS_FEATURE_META_GST_VIDEO_OVERLAY_COMPOSITION,
GST_D3D12_ALL_FORMATS) ";"
GST_VIDEO_CAPS_MAKE (GST_D3D12_ALL_FORMATS) "; "
GST_VIDEO_CAPS_MAKE_WITH_FEATURES
(GST_CAPS_FEATURE_MEMORY_SYSTEM_MEMORY ","
GST_CAPS_FEATURE_META_GST_VIDEO_OVERLAY_COMPOSITION,
GST_D3D12_ALL_FORMATS)));
GST_DEBUG_CATEGORY_STATIC (gst_d3d12_video_sink_debug);
#define GST_CAT_DEFAULT gst_d3d12_video_sink_debug
/* *INDENT-OFF* */
struct GstD3D12VideoSinkPrivate
{
GstD3D12VideoSinkPrivate ()
{
window = gst_d3d12_window_new ();
}
~GstD3D12VideoSinkPrivate ()
{
gst_clear_caps (&caps);
gst_clear_object (&window);
if (pool) {
gst_buffer_pool_set_active (pool, FALSE);
gst_object_unref (pool);
}
}
GstD3D12Window *window;
guintptr window_handle = 0;
gboolean window_handle_updated = FALSE;
std::recursive_mutex context_lock;
GstVideoInfo info;
GstCaps *caps = nullptr;
gboolean update_window = FALSE;
GstBufferPool *pool = nullptr;
std::recursive_mutex lock;
/* properties */
std::atomic<gint> adapter = { DEFAULT_ADAPTER };
gboolean force_aspect_ratio = DEFAULT_FORCE_ASPECT_RATIO;
gboolean enable_navigation = DEFAULT_ENABLE_NAVIGATION_EVENTS;
GstVideoOrientationMethod orientation = DEFAULT_ROTATE_METHOD;
GstVideoOrientationMethod orientation_from_tag = DEFAULT_ROTATE_METHOD;
GstVideoOrientationMethod orientation_selected = DEFAULT_ROTATE_METHOD;
};
/* *INDENT-ON* */
struct _GstD3D12VideoSink
{
GstVideoSink parent;
GstD3D12Device *device;
GstD3D12VideoSinkPrivate *priv;
};
static void gst_d3d12_videosink_set_property (GObject * object, guint prop_id,
const GValue * value, GParamSpec * pspec);
static void gst_d3d12_videosink_get_property (GObject * object, guint prop_id,
GValue * value, GParamSpec * pspec);
static void gst_d3d12_video_sink_finalize (GObject * object);
static void gst_d3d12_video_sink_set_context (GstElement * element,
GstContext * context);
static gboolean gst_d3d12_video_sink_set_caps (GstBaseSink * sink,
GstCaps * caps);
static gboolean gst_d3d12_video_sink_start (GstBaseSink * sink);
static gboolean gst_d3d12_video_sink_stop (GstBaseSink * sink);
static gboolean gst_d3d12_video_sink_unlock (GstBaseSink * sink);
static gboolean gst_d3d12_video_sink_unlock_stop (GstBaseSink * sink);
static gboolean gst_d3d12_video_sink_propose_allocation (GstBaseSink * sink,
GstQuery * query);
static gboolean gst_d3d12_video_sink_query (GstBaseSink * sink,
GstQuery * query);
static GstFlowReturn gst_d3d12_video_sink_prepare (GstBaseSink * sink,
GstBuffer * buf);
static gboolean gst_d3d12_video_sink_event (GstBaseSink * sink,
GstEvent * event);
static gboolean gst_d3d12_video_sink_set_info (GstVideoSink * sink,
GstCaps * caps, const GstVideoInfo * info);
static GstFlowReturn gst_d3d12_video_sink_show_frame (GstVideoSink * sink,
GstBuffer * buf);
static void gst_d3d12_video_sink_set_orientation (GstD3D12VideoSink * self,
GstVideoOrientationMethod method, gboolean from_tag);
static void gst_d3d12_video_sink_key_event (GstD3D12Window * window,
const gchar * event, const gchar * key, GstD3D12VideoSink * self);
static void gst_d3d12_video_sink_mouse_event (GstD3D12Window * window,
const gchar * event, gint button, gdouble x, gdouble y,
GstD3D12VideoSink * self);
static void
gst_d3d12_video_sink_video_overlay_init (GstVideoOverlayInterface * iface);
static void
gst_d3d12_video_sink_navigation_init (GstNavigationInterface * iface);
#define gst_d3d12_video_sink_parent_class parent_class
G_DEFINE_TYPE_WITH_CODE (GstD3D12VideoSink, gst_d3d12_video_sink,
GST_TYPE_VIDEO_SINK,
G_IMPLEMENT_INTERFACE (GST_TYPE_VIDEO_OVERLAY,
gst_d3d12_video_sink_video_overlay_init);
G_IMPLEMENT_INTERFACE (GST_TYPE_NAVIGATION,
gst_d3d12_video_sink_navigation_init);
GST_DEBUG_CATEGORY_INIT (gst_d3d12_video_sink_debug,
"d3d12videosink", 0, "d3d12videosink"));
static void
gst_d3d12_video_sink_class_init (GstD3D12VideoSinkClass * klass)
{
auto object_class = G_OBJECT_CLASS (klass);
auto element_class = GST_ELEMENT_CLASS (klass);
auto basesink_class = GST_BASE_SINK_CLASS (klass);
auto videosink_class = GST_VIDEO_SINK_CLASS (klass);
object_class->set_property = gst_d3d12_videosink_set_property;
object_class->get_property = gst_d3d12_videosink_get_property;
object_class->finalize = gst_d3d12_video_sink_finalize;
g_object_class_install_property (object_class, PROP_ADAPTER,
g_param_spec_int ("adapter", "Adapter",
"Adapter index for creating device (-1 for default)",
-1, G_MAXINT32, DEFAULT_ADAPTER,
(GParamFlags) (G_PARAM_READWRITE | GST_PARAM_MUTABLE_READY |
G_PARAM_STATIC_STRINGS)));
g_object_class_install_property (object_class, PROP_FORCE_ASPECT_RATIO,
g_param_spec_boolean ("force-aspect-ratio",
"Force aspect ratio",
"When enabled, scaling will respect original aspect ratio",
DEFAULT_FORCE_ASPECT_RATIO,
(GParamFlags) (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
g_object_class_install_property (object_class, PROP_ENABLE_NAVIGATION_EVENTS,
g_param_spec_boolean ("enable-navigation-events",
"Enable navigation events",
"When enabled, navigation events are sent upstream",
DEFAULT_ENABLE_NAVIGATION_EVENTS,
(GParamFlags) (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
g_object_class_install_property (object_class, PROP_ROTATE_METHOD,
g_param_spec_enum ("rotate-method", "Rotate Method",
"Rotate method to use",
GST_TYPE_VIDEO_ORIENTATION_METHOD, GST_VIDEO_ORIENTATION_IDENTITY,
(GParamFlags) (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
element_class->set_context =
GST_DEBUG_FUNCPTR (gst_d3d12_video_sink_set_context);
gst_element_class_set_static_metadata (element_class,
"Direct3D12 Video Sink", "Sink/Video", "A Direct3D12 Video Sink",
"Seungha Yang <seungha@centricular.com>");
gst_element_class_add_static_pad_template (element_class, &sink_template);
basesink_class->start = GST_DEBUG_FUNCPTR (gst_d3d12_video_sink_start);
basesink_class->stop = GST_DEBUG_FUNCPTR (gst_d3d12_video_sink_stop);
basesink_class->unlock = GST_DEBUG_FUNCPTR (gst_d3d12_video_sink_unlock);
basesink_class->unlock_stop =
GST_DEBUG_FUNCPTR (gst_d3d12_video_sink_unlock_stop);
basesink_class->propose_allocation =
GST_DEBUG_FUNCPTR (gst_d3d12_video_sink_propose_allocation);
basesink_class->query = GST_DEBUG_FUNCPTR (gst_d3d12_video_sink_query);
basesink_class->prepare = GST_DEBUG_FUNCPTR (gst_d3d12_video_sink_prepare);
basesink_class->event = GST_DEBUG_FUNCPTR (gst_d3d12_video_sink_event);
videosink_class->set_info = GST_DEBUG_FUNCPTR (gst_d3d12_video_sink_set_info);
videosink_class->show_frame =
GST_DEBUG_FUNCPTR (gst_d3d12_video_sink_show_frame);
}
static void
gst_d3d12_video_sink_init (GstD3D12VideoSink * self)
{
self->priv = new GstD3D12VideoSinkPrivate ();
auto priv = self->priv;
g_signal_connect (priv->window, "key-event",
G_CALLBACK (gst_d3d12_video_sink_key_event), self);
g_signal_connect (priv->window, "mouse-event",
G_CALLBACK (gst_d3d12_video_sink_mouse_event), self);
}
static void
gst_d3d12_video_sink_finalize (GObject * object)
{
auto self = GST_D3D12_VIDEO_SINK (object);
delete self->priv;
gst_clear_object (&self->device);
G_OBJECT_CLASS (parent_class)->finalize (object);
}
static void
gst_d3d12_videosink_set_property (GObject * object, guint prop_id,
const GValue * value, GParamSpec * pspec)
{
auto self = GST_D3D12_VIDEO_SINK (object);
auto priv = self->priv;
std::lock_guard < std::recursive_mutex > lk (priv->lock);
switch (prop_id) {
case PROP_ADAPTER:
priv->adapter = g_value_get_int (value);
break;
case PROP_FORCE_ASPECT_RATIO:
priv->force_aspect_ratio = g_value_get_boolean (value);
gst_d3d12_window_set_force_aspect_ratio (priv->window,
priv->force_aspect_ratio);
break;
case PROP_ENABLE_NAVIGATION_EVENTS:
priv->enable_navigation = g_value_get_boolean (value);
gst_d3d12_window_set_enable_navigation_events (priv->window,
priv->enable_navigation);
break;
case PROP_ROTATE_METHOD:
gst_d3d12_video_sink_set_orientation (self,
(GstVideoOrientationMethod) g_value_get_enum (value), FALSE);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
}
}
static void
gst_d3d12_videosink_get_property (GObject * object, guint prop_id,
GValue * value, GParamSpec * pspec)
{
auto self = GST_D3D12_VIDEO_SINK (object);
auto priv = self->priv;
std::lock_guard < std::recursive_mutex > lk (priv->lock);
switch (prop_id) {
case PROP_ADAPTER:
g_value_set_int (value, priv->adapter);
break;
case PROP_FORCE_ASPECT_RATIO:
g_value_set_boolean (value, priv->force_aspect_ratio);
break;
case PROP_ENABLE_NAVIGATION_EVENTS:
g_value_set_boolean (value, priv->enable_navigation);
break;
case PROP_ROTATE_METHOD:
g_value_set_enum (value, priv->orientation);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
}
}
static void
gst_d3d12_video_sink_set_orientation (GstD3D12VideoSink * self,
GstVideoOrientationMethod orientation, gboolean from_tag)
{
auto priv = self->priv;
if (orientation == GST_VIDEO_ORIENTATION_CUSTOM) {
GST_WARNING_OBJECT (self, "Unsupported custom orientation");
return;
}
if (from_tag)
priv->orientation_from_tag = orientation;
else
priv->orientation = orientation;
if (priv->orientation == GST_VIDEO_ORIENTATION_AUTO)
priv->orientation_selected = priv->orientation_from_tag;
else
priv->orientation_selected = priv->orientation;
gst_d3d12_window_set_orientation (priv->window, priv->orientation_selected);
}
static void
gst_d3d12_video_sink_set_context (GstElement * element, GstContext * context)
{
auto self = GST_D3D12_VIDEO_SINK (element);
auto priv = self->priv;
{
std::lock_guard < std::recursive_mutex > lk (priv->context_lock);
gst_d3d12_handle_set_context (element,
context, priv->adapter, &self->device);
}
GST_ELEMENT_CLASS (parent_class)->set_context (element, context);
}
static gboolean
gst_d3d12_video_sink_event (GstBaseSink * sink, GstEvent * event)
{
auto self = GST_D3D12_VIDEO_SINK (sink);
auto priv = self->priv;
switch (GST_EVENT_TYPE (event)) {
case GST_EVENT_TAG:{
GstTagList *taglist;
gchar *title = nullptr;
gst_event_parse_tag (event, &taglist);
gst_tag_list_get_string (taglist, GST_TAG_TITLE, &title);
std::lock_guard < std::recursive_mutex > lk (priv->lock);
if (title) {
const gchar *app_name = g_get_application_name ();
std::string title_string;
if (app_name) {
title_string = std::string (title) + " : " + std::string (app_name);
} else {
title_string = std::string (title);
}
gst_d3d12_window_set_title (priv->window, title_string.c_str ());
g_free (title);
}
GstVideoOrientationMethod orientation = GST_VIDEO_ORIENTATION_IDENTITY;
if (gst_video_orientation_from_tag (taglist, &orientation))
gst_d3d12_video_sink_set_orientation (self, orientation, TRUE);
break;
}
default:
break;
}
return GST_BASE_SINK_CLASS (parent_class)->event (sink, event);
}
static gboolean
gst_d3d12_video_sink_set_info (GstVideoSink * sink, GstCaps * caps,
const GstVideoInfo * info)
{
auto self = GST_D3D12_VIDEO_SINK (sink);
auto priv = self->priv;
GST_DEBUG_OBJECT (self, "set caps %" GST_PTR_FORMAT, caps);
std::lock_guard < std::recursive_mutex > lk (priv->lock);
gst_caps_replace (&priv->caps, caps);
priv->info = *info;
priv->update_window = TRUE;
return TRUE;
}
static void
gst_d3d12_video_sink_key_event (GstD3D12Window * window, const gchar * event,
const gchar * key, GstD3D12VideoSink * self)
{
GstEvent *key_event;
GST_LOG_OBJECT (self, "send key event %s, key %s", event, key);
if (g_strcmp0 ("key-press", event) == 0) {
key_event = gst_navigation_event_new_key_press (key,
GST_NAVIGATION_MODIFIER_NONE);
} else if (g_strcmp0 ("key-release", event) == 0) {
key_event = gst_navigation_event_new_key_release (key,
GST_NAVIGATION_MODIFIER_NONE);
} else {
return;
}
gst_navigation_send_event_simple (GST_NAVIGATION (self), key_event);
}
static void
gst_d3d12_video_sink_mouse_event (GstD3D12Window * window, const gchar * event,
gint button, gdouble x, gdouble y, GstD3D12VideoSink * self)
{
GstEvent *mouse_event;
GST_LOG_OBJECT (self,
"send mouse event %s, button %d (%.1f, %.1f)", event, button, x, y);
if (g_strcmp0 ("mouse-button-press", event) == 0) {
mouse_event = gst_navigation_event_new_mouse_button_press (button, x, y,
GST_NAVIGATION_MODIFIER_NONE);
} else if (g_strcmp0 ("mouse-button-release", event) == 0) {
mouse_event = gst_navigation_event_new_mouse_button_release (button, x, y,
GST_NAVIGATION_MODIFIER_NONE);
} else if (g_strcmp0 ("mouse-move", event) == 0) {
mouse_event = gst_navigation_event_new_mouse_move (x, y,
GST_NAVIGATION_MODIFIER_NONE);
} else {
return;
}
gst_navigation_send_event_simple (GST_NAVIGATION (self), mouse_event);
}
static GstFlowReturn
gst_d3d12_video_sink_update_window (GstD3D12VideoSink * self)
{
auto priv = self->priv;
auto overlay = GST_VIDEO_OVERLAY (self);
bool notify_window_handle = false;
guintptr window_handle = 0;
auto window_state = gst_d3d12_window_get_state (priv->window);
if (window_state == GST_D3D12_WINDOW_STATE_CLOSED) {
GST_ERROR_OBJECT (self, "Window was closed");
return GST_FLOW_ERROR;
}
{
std::lock_guard < std::recursive_mutex > lk (priv->lock);
if (window_state == GST_D3D12_WINDOW_STATE_OPENED && !priv->update_window)
return GST_FLOW_OK;
GST_DEBUG_OBJECT (self, "Updating window with caps %" GST_PTR_FORMAT,
priv->caps);
if (window_state == GST_D3D12_WINDOW_STATE_INIT) {
if (!priv->window_handle)
gst_video_overlay_prepare_window_handle (overlay);
if (priv->window_handle) {
gst_video_overlay_got_window_handle (overlay, priv->window_handle);
window_handle = priv->window_handle;
} else {
notify_window_handle = true;
}
} else {
window_handle = priv->window_handle;
}
priv->update_window = FALSE;
}
if (priv->pool) {
gst_buffer_pool_set_active (priv->pool, FALSE);
gst_clear_object (&priv->pool);
}
auto video_width = GST_VIDEO_INFO_WIDTH (&priv->info);
auto video_height = GST_VIDEO_INFO_HEIGHT (&priv->info);
auto video_par_n = GST_VIDEO_INFO_PAR_N (&priv->info);
auto video_par_d = GST_VIDEO_INFO_PAR_D (&priv->info);
gint display_par_n = 1;
gint display_par_d = 1;
guint num, den;
if (!gst_video_calculate_display_ratio (&num, &den, video_width,
video_height, video_par_n, video_par_d, display_par_n,
display_par_d)) {
GST_ELEMENT_ERROR (self, CORE, NEGOTIATION, (nullptr),
("Error calculating the output display ratio of the video."));
return GST_FLOW_ERROR;
}
GST_DEBUG_OBJECT (self,
"video width/height: %dx%d, calculated display ratio: %d/%d format: %s",
video_width, video_height, num, den,
gst_video_format_to_string (GST_VIDEO_INFO_FORMAT (&priv->info)));
if (video_height % den == 0) {
GST_DEBUG_OBJECT (self, "keeping video height");
GST_VIDEO_SINK_WIDTH (self) = (guint)
gst_util_uint64_scale_int (video_height, num, den);
GST_VIDEO_SINK_HEIGHT (self) = video_height;
} else if (video_width % num == 0) {
GST_DEBUG_OBJECT (self, "keeping video width");
GST_VIDEO_SINK_WIDTH (self) = video_width;
GST_VIDEO_SINK_HEIGHT (self) = (guint)
gst_util_uint64_scale_int (video_width, den, num);
} else {
GST_DEBUG_OBJECT (self, "approximating while keeping video height");
GST_VIDEO_SINK_WIDTH (self) = (guint)
gst_util_uint64_scale_int (video_height, num, den);
GST_VIDEO_SINK_HEIGHT (self) = video_height;
}
GST_DEBUG_OBJECT (self, "scaling to %dx%d",
GST_VIDEO_SINK_WIDTH (self), GST_VIDEO_SINK_HEIGHT (self));
if (GST_VIDEO_SINK_WIDTH (self) <= 0 || GST_VIDEO_SINK_HEIGHT (self) <= 0) {
GST_ELEMENT_ERROR (self, CORE, NEGOTIATION, (nullptr),
("Error calculating the output display ratio of the video."));
return GST_FLOW_ERROR;
}
auto ret = gst_d3d12_window_prepare (priv->window, self->device,
window_handle, GST_VIDEO_SINK_WIDTH (self),
GST_VIDEO_SINK_HEIGHT (self), priv->caps);
if (ret != GST_FLOW_OK) {
if (ret == GST_FLOW_FLUSHING) {
GST_WARNING_OBJECT (self, "We are flushing");
gst_d3d12_window_unprepare (priv->window);
return ret;
}
GST_ELEMENT_ERROR (self, RESOURCE, FAILED, (nullptr),
("Couldn't setup swapchain"));
return GST_FLOW_ERROR;
}
if (notify_window_handle && !window_handle) {
window_handle = gst_d3d12_window_get_window_handle (priv->window);
gst_video_overlay_got_window_handle (overlay, window_handle);
}
priv->pool = gst_d3d12_buffer_pool_new (self->device);
auto config = gst_buffer_pool_get_config (priv->pool);
gst_buffer_pool_config_set_params (config, priv->caps, priv->info.size, 0, 0);
if (!gst_buffer_pool_set_config (priv->pool, config) ||
!gst_buffer_pool_set_active (priv->pool, TRUE)) {
GST_ELEMENT_ERROR (self, RESOURCE, FAILED, (nullptr),
("Couldn't setup buffer pool"));
return GST_FLOW_ERROR;
}
return GST_FLOW_OK;
}
static gboolean
gst_d3d12_video_sink_start (GstBaseSink * sink)
{
auto self = GST_D3D12_VIDEO_SINK (sink);
auto priv = self->priv;
GST_DEBUG_OBJECT (self, "Start");
if (!gst_d3d12_ensure_element_data (GST_ELEMENT_CAST (self), priv->adapter,
&self->device)) {
GST_ERROR_OBJECT (sink, "Cannot create device");
return FALSE;
}
return TRUE;
}
static gboolean
gst_d3d12_video_sink_stop (GstBaseSink * sink)
{
auto self = GST_D3D12_VIDEO_SINK (sink);
auto priv = self->priv;
GST_DEBUG_OBJECT (self, "Stop");
priv->orientation_from_tag = GST_VIDEO_ORIENTATION_IDENTITY;
if (priv->pool) {
gst_buffer_pool_set_active (priv->pool, FALSE);
gst_clear_object (&priv->pool);
}
gst_d3d12_window_unprepare (priv->window);
gst_clear_object (&self->device);
return TRUE;
}
static gboolean
gst_d3d12_video_sink_unlock (GstBaseSink * sink)
{
auto self = GST_D3D12_VIDEO_SINK (sink);
auto priv = self->priv;
GST_DEBUG_OBJECT (self, "Unlock");
gst_d3d12_window_unlock (priv->window);
return TRUE;
}
static gboolean
gst_d3d12_video_sink_unlock_stop (GstBaseSink * sink)
{
auto self = GST_D3D12_VIDEO_SINK (sink);
auto priv = self->priv;
GST_DEBUG_OBJECT (self, "Unlock stop");
gst_d3d12_window_unlock_stop (priv->window);
return TRUE;
}
static gboolean
gst_d3d12_video_sink_propose_allocation (GstBaseSink * sink, GstQuery * query)
{
auto self = GST_D3D12_VIDEO_SINK (sink);
GstCaps *caps;
GstBufferPool *pool = nullptr;
GstVideoInfo info;
guint size;
gboolean need_pool;
if (!self->device) {
GST_WARNING_OBJECT (self, "No configured device");
return FALSE;
}
gst_query_parse_allocation (query, &caps, &need_pool);
if (!caps) {
GST_WARNING_OBJECT (self, "no caps specified");
return FALSE;
}
if (!gst_video_info_from_caps (&info, caps)) {
GST_ERROR_OBJECT (self, "Invalid caps %" GST_PTR_FORMAT, caps);
return FALSE;
}
size = info.size;
bool is_d3d12 = false;
auto features = gst_caps_get_features (caps, 0);
if (gst_caps_features_contains (features,
GST_CAPS_FEATURE_MEMORY_D3D12_MEMORY)) {
is_d3d12 = true;
GST_DEBUG_OBJECT (self, "upstream support d3d12 memory");
}
if (need_pool) {
if (is_d3d12)
pool = gst_d3d12_buffer_pool_new (self->device);
else
pool = gst_video_buffer_pool_new ();
auto config = gst_buffer_pool_get_config (pool);
gst_buffer_pool_config_add_option (config,
GST_BUFFER_POOL_OPTION_VIDEO_META);
if (!is_d3d12) {
gst_buffer_pool_config_add_option (config,
GST_BUFFER_POOL_OPTION_VIDEO_ALIGNMENT);
}
gst_buffer_pool_config_set_params (config, caps, size, 2, 0);
if (!gst_buffer_pool_set_config (pool, config)) {
GST_ERROR_OBJECT (pool, "Couldn't set config");
gst_object_unref (pool);
return FALSE;
}
if (is_d3d12) {
config = gst_buffer_pool_get_config (pool);
gst_buffer_pool_config_get_params (config, nullptr, &size, nullptr,
nullptr);
gst_structure_free (config);
}
}
gst_query_add_allocation_pool (query, pool, size, 2, 0);
gst_clear_object (&pool);
gst_query_add_allocation_meta (query, GST_VIDEO_META_API_TYPE, nullptr);
gst_query_add_allocation_meta (query,
GST_VIDEO_OVERLAY_COMPOSITION_META_API_TYPE, nullptr);
if (is_d3d12) {
gst_query_add_allocation_meta (query,
GST_VIDEO_CROP_META_API_TYPE, nullptr);
}
return TRUE;
}
static gboolean
gst_d3d12_video_sink_query (GstBaseSink * sink, GstQuery * query)
{
auto self = GST_D3D12_VIDEO_SINK (sink);
auto priv = self->priv;
switch (GST_QUERY_TYPE (query)) {
case GST_QUERY_CONTEXT:
{
std::lock_guard < std::recursive_mutex > lk (priv->context_lock);
if (gst_d3d12_handle_context_query (GST_ELEMENT (self), query,
self->device)) {
return TRUE;
}
break;
}
default:
break;
}
return GST_BASE_SINK_CLASS (parent_class)->query (sink, query);
}
static void
gst_d3d12_video_sink_check_device_update (GstD3D12VideoSink * self,
GstBuffer * buf)
{
auto priv = self->priv;
auto mem = gst_buffer_peek_memory (buf, 0);
if (!gst_is_d3d12_memory (mem))
return;
auto dmem = GST_D3D12_MEMORY_CAST (mem);
if (dmem->device == self->device)
return;
GST_INFO_OBJECT (self, "Updating device %" GST_PTR_FORMAT " -> %"
GST_PTR_FORMAT, self->device, dmem->device);
{
std::lock_guard < std::recursive_mutex > lk (priv->lock);
priv->update_window = TRUE;
}
std::lock_guard < std::recursive_mutex > lk (priv->context_lock);
gst_clear_object (&self->device);
self->device = (GstD3D12Device *) gst_object_ref (dmem->device);
}
static gboolean
gst_d3d12_video_sink_foreach_meta (GstBuffer * buffer, GstMeta ** meta,
GstBuffer * uploaded)
{
if ((*meta)->info->api != GST_VIDEO_OVERLAY_COMPOSITION_META_API_TYPE)
return TRUE;
auto cmeta = (GstVideoOverlayCompositionMeta *) (*meta);
if (!cmeta->overlay)
return TRUE;
if (gst_video_overlay_composition_n_rectangles (cmeta->overlay) == 0)
return TRUE;
gst_buffer_add_video_overlay_composition_meta (uploaded, cmeta->overlay);
return TRUE;
}
static GstFlowReturn
gst_d3d12_video_sink_prepare (GstBaseSink * sink, GstBuffer * buffer)
{
auto self = GST_D3D12_VIDEO_SINK (sink);
auto priv = self->priv;
gst_d3d12_video_sink_check_device_update (self, buffer);
auto ret = gst_d3d12_video_sink_update_window (self);
if (ret != GST_FLOW_OK)
return ret;
GstBuffer *upload = nullptr;
auto mem = gst_buffer_peek_memory (buffer, 0);
if (!gst_is_d3d12_memory (mem)) {
GstBuffer *upload = nullptr;
gst_buffer_pool_acquire_buffer (priv->pool, &upload, nullptr);
if (!upload) {
GST_ERROR_OBJECT (self, "Couldn't allocate upload buffer");
return GST_FLOW_ERROR;
}
GstVideoFrame in_frame, out_frame;
if (!gst_video_frame_map (&in_frame, &priv->info, buffer, GST_MAP_READ)) {
GST_ERROR_OBJECT (self, "Couldn't map input frame");
gst_buffer_unref (upload);
return GST_FLOW_ERROR;
}
if (!gst_video_frame_map (&out_frame, &priv->info, upload, GST_MAP_WRITE)) {
GST_ERROR_OBJECT (self, "Couldn't map upload frame");
gst_video_frame_unmap (&out_frame);
gst_buffer_unref (upload);
return GST_FLOW_ERROR;
}
auto copy_ret = gst_video_frame_copy (&out_frame, &in_frame);
gst_video_frame_unmap (&out_frame);
gst_video_frame_unmap (&in_frame);
if (!copy_ret) {
GST_ERROR_OBJECT (self, "Couldn't copy frame");
gst_buffer_unref (upload);
return GST_FLOW_ERROR;
}
gst_buffer_foreach_meta (buffer,
(GstBufferForeachMetaFunc) gst_d3d12_video_sink_foreach_meta, upload);
buffer = upload;
}
ret = gst_d3d12_window_set_buffer (priv->window, buffer);
if (upload)
gst_buffer_unref (upload);
if (ret == GST_D3D12_WINDOW_FLOW_CLOSED) {
GST_ELEMENT_ERROR (self, RESOURCE, NOT_FOUND,
("Output window was closed"), (nullptr));
return GST_FLOW_ERROR;
}
return ret;
}
static GstFlowReturn
gst_d3d12_video_sink_show_frame (GstVideoSink * sink, GstBuffer * buf)
{
auto self = GST_D3D12_VIDEO_SINK (sink);
auto priv = self->priv;
auto ret = gst_d3d12_window_present (priv->window);
if (ret == GST_D3D12_WINDOW_FLOW_CLOSED) {
GST_ELEMENT_ERROR (self, RESOURCE, NOT_FOUND,
("Output window was closed"), (nullptr));
ret = GST_FLOW_ERROR;
}
return ret;
}
static void
gst_d3d12_video_sink_overlay_expose (GstVideoOverlay * overlay)
{
auto self = GST_D3D12_VIDEO_SINK (overlay);
auto priv = self->priv;
gst_d3d12_window_set_buffer (priv->window, nullptr);
}
static void
gst_d3d12_video_sink_overlay_handle_events (GstVideoOverlay * overlay,
gboolean handle_events)
{
auto self = GST_D3D12_VIDEO_SINK (overlay);
auto priv = self->priv;
std::lock_guard < std::recursive_mutex > lk (priv->lock);
priv->enable_navigation = handle_events;
gst_d3d12_window_set_enable_navigation_events (priv->window, handle_events);
}
static void
gst_d3d12_video_sink_overlay_set_window_handle (GstVideoOverlay * overlay,
guintptr window_handle)
{
auto self = GST_D3D12_VIDEO_SINK (overlay);
auto priv = self->priv;
GST_DEBUG ("set window handle %" G_GUINTPTR_FORMAT, window_handle);
std::lock_guard < std::recursive_mutex > lk (priv->lock);
if (priv->window_handle != window_handle) {
priv->window_handle = window_handle;
priv->update_window = TRUE;
}
}
static void
gst_d3d12_video_sink_overlay_set_render_rectangle (GstVideoOverlay * overlay,
gint x, gint y, gint width, gint height)
{
auto self = GST_D3D12_VIDEO_SINK (overlay);
auto priv = self->priv;
GST_DEBUG_OBJECT (self,
"render rect x: %d, y: %d, width: %d, height %d", x, y, width, height);
GstVideoRectangle rect;
rect.x = x;
rect.y = y;
rect.w = width;
rect.h = height;
gst_d3d12_window_set_render_rect (priv->window, &rect);
}
static void
gst_d3d12_video_sink_video_overlay_init (GstVideoOverlayInterface * iface)
{
iface->expose = gst_d3d12_video_sink_overlay_expose;
iface->handle_events = gst_d3d12_video_sink_overlay_handle_events;
iface->set_window_handle = gst_d3d12_video_sink_overlay_set_window_handle;
iface->set_render_rectangle =
gst_d3d12_video_sink_overlay_set_render_rectangle;
}
static void
gst_d3d12_video_sink_navigation_send_event (GstNavigation * navigation,
GstEvent * event)
{
auto self = GST_D3D12_VIDEO_SINK (navigation);
if (event) {
gboolean handled;
gst_event_ref (event);
handled = gst_pad_push_event (GST_VIDEO_SINK_PAD (self), event);
if (!handled)
gst_element_post_message (GST_ELEMENT_CAST (self),
gst_navigation_message_new_event (GST_OBJECT_CAST (self), event));
gst_event_unref (event);
}
}
static void
gst_d3d12_video_sink_navigation_init (GstNavigationInterface * iface)
{
iface->send_event_simple = gst_d3d12_video_sink_navigation_send_event;
}

View file

@ -0,0 +1,33 @@
/* GStreamer
* Copyright (C) 2023 Seungha Yang <seungha@centricular.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#pragma once
#include <gst/gst.h>
#include <gst/video/video.h>
#include "gstd3d12.h"
G_BEGIN_DECLS
#define GST_TYPE_D3D12_VIDEO_SINK (gst_d3d12_video_sink_get_type())
G_DECLARE_FINAL_TYPE (GstD3D12VideoSink, gst_d3d12_video_sink,
GST, D3D12_VIDEO_SINK, GstVideoSink)
G_END_DECLS

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,81 @@
/* GStreamer
* Copyright (C) 2023 Seungha Yang <seungha@centricular.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#pragma once
#include <gst/gst.h>
#include <gst/video/video.h>
#include "gstd3d12.h"
G_BEGIN_DECLS
#define GST_D3D12_WINDOW_FLOW_CLOSED GST_FLOW_CUSTOM_ERROR
#define GST_TYPE_D3D12_WINDOW (gst_d3d12_window_get_type())
G_DECLARE_FINAL_TYPE (GstD3D12Window, gst_d3d12_window,
GST, D3D12_WINDOW, GstObject)
enum GstD3D12WindowState
{
GST_D3D12_WINDOW_STATE_INIT,
GST_D3D12_WINDOW_STATE_OPENED,
GST_D3D12_WINDOW_STATE_CLOSED,
};
GstD3D12Window * gst_d3d12_window_new (void);
guintptr gst_d3d12_window_get_window_handle (GstD3D12Window * window);
GstD3D12WindowState gst_d3d12_window_get_state (GstD3D12Window * window);
GstFlowReturn gst_d3d12_window_prepare (GstD3D12Window * window,
GstD3D12Device * device,
guintptr window_handle,
guint display_width,
guint display_height,
GstCaps * caps);
void gst_d3d12_window_unprepare (GstD3D12Window * window);
void gst_d3d12_window_unlock (GstD3D12Window * window);
void gst_d3d12_window_unlock_stop (GstD3D12Window * window);
GstFlowReturn gst_d3d12_window_set_buffer (GstD3D12Window * window,
GstBuffer * buffer);
GstFlowReturn gst_d3d12_window_present (GstD3D12Window * window);
void gst_d3d12_window_set_render_rect (GstD3D12Window * window,
const GstVideoRectangle * rect);
void gst_d3d12_window_set_force_aspect_ratio (GstD3D12Window * window,
gboolean force_aspect_ratio);
void gst_d3d12_window_set_enable_navigation_events (GstD3D12Window * window,
gboolean enable);
void gst_d3d12_window_set_orientation (GstD3D12Window * window,
GstVideoOrientationMethod orientation);
void gst_d3d12_window_set_title (GstD3D12Window * window,
const gchar * title);
G_END_DECLS

View file

@ -0,0 +1,32 @@
/* GStreamer
* Copyright (C) 2023 Seungha Yang <seungha@centricular.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
Texture2D shaderTexture : register(t0, space0);
SamplerState samplerState : register(s0, space0);
struct PS_INPUT
{
float4 Position : SV_POSITION;
float2 Texture : TEXCOORD;
};
float4 PSMain_sample (PS_INPUT input): SV_TARGET
{
return shaderTexture.Sample (samplerState, input.Texture);
}

View file

@ -0,0 +1,38 @@
/* GStreamer
* Copyright (C) 2023 Seungha Yang <seungha@centricular.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
Texture2D shaderTexture : register(t0, space0);
SamplerState samplerState : register(s0, space0);
struct PS_INPUT
{
float4 Position : SV_POSITION;
float2 Texture : TEXCOORD;
};
float4 PSMain_sample_premul (PS_INPUT input): SV_TARGET
{
float4 sample = shaderTexture.Sample (samplerState, input.Texture);
float4 premul_sample;
premul_sample.r = saturate (sample.r * sample.a);
premul_sample.g = saturate (sample.g * sample.a);
premul_sample.b = saturate (sample.b * sample.a);
premul_sample.a = sample.a;
return premul_sample;
}

View file

@ -0,0 +1,35 @@
/* GStreamer
* Copyright (C) 2023 Seungha Yang <seungha@centricular.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
struct VS_INPUT
{
float4 Position : POSITION;
float2 Texture : TEXCOORD;
};
struct VS_OUTPUT
{
float4 Position : SV_POSITION;
float2 Texture : TEXCOORD;
};
VS_OUTPUT VSMain_coord (VS_INPUT input)
{
return input;
}

View file

@ -141,6 +141,9 @@ hlsl_precompiled += generated_collection
hlsl_sources = [
['VSMain_converter', 'vs_6_0'],
['PSMain_sample', 'ps_6_0'],
['PSMain_sample_premul', 'ps_6_0'],
['VSMain_coord', 'vs_6_0'],
]
foreach shader : hlsl_sources
@ -157,3 +160,5 @@ foreach shader : hlsl_sources
'@INPUT@'])
hlsl_precompiled += [compiled_shader]
endforeach

View file

@ -17,10 +17,13 @@ d3d12_sources = [
'gstd3d12h264dec.cpp',
'gstd3d12h265dec.cpp',
'gstd3d12memory.cpp',
'gstd3d12overlaycompositor.cpp',
'gstd3d12pluginutils.cpp',
'gstd3d12upload.cpp',
'gstd3d12utils.cpp',
'gstd3d12videosink.cpp',
'gstd3d12vp9dec.cpp',
'gstd3d12window.cpp',
'plugin.cpp',
]

View file

@ -32,6 +32,7 @@
#include "gstd3d12convert.h"
#include "gstd3d12download.h"
#include "gstd3d12upload.h"
#include "gstd3d12videosink.h"
#include "gstd3d12h264dec.h"
#include "gstd3d12h265dec.h"
#include "gstd3d12vp9dec.h"
@ -110,6 +111,8 @@ plugin_init (GstPlugin * plugin)
"d3d12download", GST_RANK_NONE, GST_TYPE_D3D12_DOWNLOAD);
gst_element_register (plugin,
"d3d12upload", GST_RANK_NONE, GST_TYPE_D3D12_UPLOAD);
gst_element_register (plugin,
"d3d12videosink", GST_RANK_NONE, GST_TYPE_D3D12_VIDEO_SINK);
return TRUE;
}