d3d11: Move HLSL compiler to gst-libs

We should move this functionality to gst-libs so that GstD3D11Converter
can be moved to gst-libs.
Another advantage is that applications can call our
HLSL compiler wrapper method without any worry about OS version
dependent system installed HLSL library.
Note that there are multiple HLSL compiler library versions
on Windows and system installed one would be OS version dependent.

Part-of: <https://gitlab.freedesktop.org/gstreamer/gstreamer/-/merge_requests/2760>
This commit is contained in:
Seungha Yang 2022-06-16 03:57:37 +09:00
parent 390518a296
commit 1dd29a2564
14 changed files with 444 additions and 328 deletions

View file

@ -28,9 +28,10 @@
#include <gst/d3d11/gstd3d11config.h>
#include <gst/d3d11/gstd3d11_fwd.h>
#include <gst/d3d11/gstd3d11-enumtypes.h>
#include <gst/d3d11/gstd3d11device.h>
#include <gst/d3d11/gstd3d11memory.h>
#include <gst/d3d11/gstd3d11bufferpool.h>
#include <gst/d3d11/gstd3d11utils.h>
#include <gst/d3d11/gstd3d11compile.h>
#include <gst/d3d11/gstd3d11device.h>
#include <gst/d3d11/gstd3d11format.h>
#include <gst/d3d11/gstd3d11memory.h>
#include <gst/d3d11/gstd3d11utils.h>

View file

@ -0,0 +1,309 @@
/* GStreamer
* Copyright (C) 2022 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 "gstd3d11compile.h"
#include "gstd3d11device.h"
#include "gstd3d11utils.h"
#include <gmodule.h>
#include <wrl.h>
#include <string.h>
/* *INDENT-OFF* */
using namespace Microsoft::WRL;
/* *INDENT-ON* */
#ifndef GST_DISABLE_GST_DEBUG
#define GST_CAT_DEFAULT ensure_debug_category()
static GstDebugCategory *
ensure_debug_category (void)
{
static gsize cat_gonce = 0;
if (g_once_init_enter (&cat_gonce)) {
gsize cat_done;
cat_done = (gsize) _gst_debug_category_new ("d3d11compile", 0,
"d3d11compile");
g_once_init_leave (&cat_gonce, cat_done);
}
return (GstDebugCategory *) cat_gonce;
}
#else
#define ensure_debug_category() /* NOOP */
#endif /* GST_DISABLE_GST_DEBUG */
static GModule *d3d_compiler_module = nullptr;
static pD3DCompile GstD3DCompileFunc = nullptr;
/**
* gst_d3d11_compile_init:
*
* Loads HLSL compiler library
*
* Returns: %TRUE if HLSL compiler library is available
*/
gboolean
gst_d3d11_compile_init (void)
{
static gsize init_once = 0;
if (g_once_init_enter (&init_once)) {
#if GST_D3D11_WINAPI_ONLY_APP
/* Assuming that d3d compiler library is available */
GstD3DCompileFunc = D3DCompile;
#else
static const gchar *d3d_compiler_names[] = {
"d3dcompiler_47.dll",
"d3dcompiler_46.dll",
"d3dcompiler_45.dll",
"d3dcompiler_44.dll",
"d3dcompiler_43.dll",
};
for (guint i = 0; i < G_N_ELEMENTS (d3d_compiler_names); i++) {
d3d_compiler_module =
g_module_open (d3d_compiler_names[i], G_MODULE_BIND_LAZY);
if (d3d_compiler_module) {
GST_INFO ("D3D compiler %s is available", d3d_compiler_names[i]);
if (!g_module_symbol (d3d_compiler_module, "D3DCompile",
(gpointer *) & GstD3DCompileFunc)) {
GST_ERROR ("Cannot load D3DCompile symbol from %s",
d3d_compiler_names[i]);
g_module_close (d3d_compiler_module);
d3d_compiler_module = nullptr;
GstD3DCompileFunc = nullptr;
} else {
break;
}
}
}
if (!GstD3DCompileFunc)
GST_WARNING ("D3D11 compiler library is unavailable");
#endif
g_once_init_leave (&init_once, 1);
}
if (!GstD3DCompileFunc)
return FALSE;
return TRUE;
}
/**
* gst_d3d11_compile:
* @src_data: source data to compile
* @src_data_size: length of src_data
* @source_name: (nullable): used for strings that specify error messages
* @defines: (nullable): null-terminated array of D3D_SHADER_MACRO struct that defines shader macros
* @include: (nullable): a ID3DInclude
* @entry_point: (nullable): the name of entry point function
* @target: a string specifies the shader target
* @flags1: flags defined by D3DCOMPILE constants
* @flags2: flags defined by D3DCOMPILE_EFFECT constants
* @code: (out) (optional): a compiled code
* @error_msgs: (out) (optional) (nullable): compiler error messages
*
* Compiles HLSL code or an effect file into bytecode for a given target
*
* Returns: HRESULT return code
*/
HRESULT
gst_d3d11_compile (LPCVOID src_data, SIZE_T src_data_size, LPCSTR source_name,
CONST D3D_SHADER_MACRO * defines, ID3DInclude * include, LPCSTR entry_point,
LPCSTR target, UINT flags1, UINT flags2, ID3DBlob ** code,
ID3DBlob ** error_msgs)
{
if (!gst_d3d11_compile_init ())
return E_FAIL;
return GstD3DCompileFunc (src_data, src_data_size, source_name, defines,
include, entry_point, target, flags1, flags2, code, error_msgs);
}
/**
* gst_d3d11_create_pixel_shader_simple:
* @device: a #GstD3D11Device
* @source: a pixel shader code to compile
* @entry_point: the name of entry point function
* @shader: (out): a ID3D11PixelShader
* Compiles pixel shader code and creates ID3D11PixelShader
*
* Returns: HRESULT return code
*/
HRESULT
gst_d3d11_create_pixel_shader_simple (GstD3D11Device * device,
const gchar * source, const gchar * entry_point,
ID3D11PixelShader ** shader)
{
ID3D11Device *device_handle;
HRESULT hr;
ComPtr < ID3DBlob > ps_blob;
ComPtr < ID3DBlob > error_msg;
D3D_FEATURE_LEVEL feature_level;
const gchar *target;
g_return_val_if_fail (GST_IS_D3D11_DEVICE (device), E_INVALIDARG);
g_return_val_if_fail (source != nullptr, E_INVALIDARG);
g_return_val_if_fail (entry_point != nullptr, E_INVALIDARG);
g_return_val_if_fail (shader != nullptr, E_INVALIDARG);
device_handle = gst_d3d11_device_get_device_handle (device);
feature_level = device_handle->GetFeatureLevel ();
if (feature_level >= D3D_FEATURE_LEVEL_11_0)
target = "ps_5_0";
else if (feature_level >= D3D_FEATURE_LEVEL_10_0)
target = "ps_4_0";
else if (feature_level >= D3D_FEATURE_LEVEL_9_3)
target = "ps_4_0_level_9_3";
else
target = "ps_4_0_level_9_1";
GST_DEBUG_OBJECT (device, "Compile code\n%s", source);
hr = gst_d3d11_compile (source, strlen (source), nullptr, nullptr, nullptr,
entry_point, target, 0, 0, &ps_blob, &error_msg);
if (!gst_d3d11_result (hr, device)) {
const gchar *err = nullptr;
if (error_msg)
err = (const gchar *) error_msg->GetBufferPointer ();
GST_ERROR_OBJECT (device,
"Couldn't compile code, hr: 0x%x, error detail: %s, source code: \n%s",
(guint) hr, GST_STR_NULL (err), source);
return hr;
}
if (error_msg) {
const gchar *err = (const gchar *) error_msg->GetBufferPointer ();
GST_DEBUG_OBJECT (device, "HLSL compiler warning %s, shader code %s",
GST_STR_NULL (err), source);
}
return device_handle->CreatePixelShader (ps_blob->GetBufferPointer (),
ps_blob->GetBufferSize (), nullptr, shader);
}
/**
* gst_d3d11_create_vertex_shader_simple:
* @device: a #GstD3D11Device
* @source: a vertex shader code to compile
* @entry_point: the name of entry point function
* @input_desc: an array of D3D11_INPUT_ELEMENT_DESC
* @desc_len: length of input_desc
* @shader: (out): a ID3D11VertexShader
* @layout: (out): a ID3D11InputLayout
* Compiles vertex shader code and creates ID3D11VertexShader and
* ID3D11InputLayout
*
* Returns: HRESULT return code
*/
HRESULT
gst_d3d11_create_vertex_shader_simple (GstD3D11Device * device,
const gchar * source, const gchar * entry_point,
const D3D11_INPUT_ELEMENT_DESC * input_desc, guint desc_len,
ID3D11VertexShader ** shader, ID3D11InputLayout ** layout)
{
ID3D11Device *device_handle;
HRESULT hr;
ComPtr < ID3DBlob > vs_blob;
ComPtr < ID3DBlob > error_msg;
ComPtr < ID3D11VertexShader > vs;
ComPtr < ID3D11InputLayout > input_layout;
D3D_FEATURE_LEVEL feature_level;
const gchar *target;
g_return_val_if_fail (GST_IS_D3D11_DEVICE (device), E_INVALIDARG);
g_return_val_if_fail (source != nullptr, E_INVALIDARG);
g_return_val_if_fail (entry_point != nullptr, E_INVALIDARG);
g_return_val_if_fail (input_desc != nullptr, E_INVALIDARG);
g_return_val_if_fail (desc_len > 0, E_INVALIDARG);
g_return_val_if_fail (shader != nullptr, E_INVALIDARG);
g_return_val_if_fail (layout != nullptr, E_INVALIDARG);
device_handle = gst_d3d11_device_get_device_handle (device);
feature_level = device_handle->GetFeatureLevel ();
if (feature_level >= D3D_FEATURE_LEVEL_11_0)
target = "vs_5_0";
else if (feature_level >= D3D_FEATURE_LEVEL_10_0)
target = "vs_4_0";
else if (feature_level >= D3D_FEATURE_LEVEL_9_3)
target = "vs_4_0_level_9_3";
else
target = "vs_4_0_level_9_1";
GST_DEBUG_OBJECT (device, "Compile code\n%s", source);
hr = gst_d3d11_compile (source, strlen (source), nullptr, nullptr, nullptr,
entry_point, target, 0, 0, &vs_blob, &error_msg);
if (!gst_d3d11_result (hr, device)) {
const gchar *err = nullptr;
if (error_msg)
err = (const gchar *) error_msg->GetBufferPointer ();
GST_ERROR_OBJECT (device,
"Couldn't compile code, hr: 0x%x, error detail: %s, source code: \n%s",
(guint) hr, GST_STR_NULL (err), source);
return hr;
}
if (error_msg) {
const gchar *err = (const gchar *) error_msg->GetBufferPointer ();
GST_DEBUG_OBJECT (device, "HLSL compiler warning %s, shader code %s",
GST_STR_NULL (err), source);
}
hr = device_handle->CreateVertexShader (vs_blob->GetBufferPointer (),
vs_blob->GetBufferSize (), nullptr, &vs);
if (!gst_d3d11_result (hr, device)) {
GST_ERROR_OBJECT (device, "Couldn't create vertex shader");
return hr;
}
hr = device_handle->CreateInputLayout (input_desc, desc_len,
vs_blob->GetBufferPointer (), vs_blob->GetBufferSize (), &input_layout);
if (!gst_d3d11_result (hr, device)) {
GST_ERROR_OBJECT (device, "Couldn't create input layout");
return hr;
}
*shader = vs.Detach ();
*layout = input_layout.Detach ();
return hr;
}

View file

@ -0,0 +1,59 @@
/* GStreamer
* Copyright (C) 2022 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/d3d11/gstd3d11_fwd.h>
#include <d3dcompiler.h>
G_BEGIN_DECLS
GST_D3D11_API
gboolean gst_d3d11_compile_init (void);
GST_D3D11_API
HRESULT gst_d3d11_compile (LPCVOID src_data,
SIZE_T src_data_size,
LPCSTR source_name,
CONST D3D_SHADER_MACRO * defines,
ID3DInclude * include,
LPCSTR entry_point,
LPCSTR target,
UINT flags1,
UINT flags2,
ID3DBlob ** code,
ID3DBlob ** error_msgs);
GST_D3D11_API
HRESULT gst_d3d11_create_pixel_shader_simple (GstD3D11Device * device,
const gchar * source,
const gchar * entry_point,
ID3D11PixelShader ** shader);
GST_D3D11_API
HRESULT gst_d3d11_create_vertex_shader_simple (GstD3D11Device * device,
const gchar * source,
const gchar * entry_point,
const D3D11_INPUT_ELEMENT_DESC * input_desc,
guint desc_len,
ID3D11VertexShader ** shader,
ID3D11InputLayout ** layout);
G_END_DECLS

View file

@ -1,5 +1,6 @@
d3d11_sources = [
'gstd3d11bufferpool.cpp',
'gstd3d11compile.cpp',
'gstd3d11device.cpp',
'gstd3d11format.cpp',
'gstd3d11memory.cpp',
@ -51,10 +52,22 @@ dxgi_lib = cc.find_library('dxgi', required : false)
d3dcompiler_lib = cc.find_library('d3dcompiler', required: false)
runtimeobject_lib = cc.find_library('runtimeobject', required : false)
if not d3d11_lib.found() or not dxgi_lib.found() or not cc.has_header('d3d11_4.h') or not cc.has_header('dxgi1_6.h')
if not d3d11_lib.found() or not dxgi_lib.found()
subdir_done()
endif
sdk_headers = [
'd3d11_4.h',
'dxgi1_6.h',
'd3dcompiler.h'
]
foreach h : sdk_headers
if not cc.has_header (h)
subdir_done ()
endif
endforeach
d3d11_winapi_desktop = cxx.compiles('''#include <winapifamily.h>
#if !WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP)
#error "not win32"
@ -83,6 +96,7 @@ if not d3d11_winapi_desktop and not d3d11_winapi_app
subdir_done()
endif
extra_deps = []
d3d11_winapi_only_app = d3d11_winapi_app and not d3d11_winapi_desktop
d3d11_conf.set10('GST_D3D11_WINAPI_ONLY_APP', d3d11_winapi_only_app)
d3d11_conf.set10('GST_D3D11_WINAPI_APP', d3d11_winapi_app)
@ -144,6 +158,10 @@ if have_dxgidebug_h
extra_comm_args += ['-DHAVE_DXGIDEBUG_H']
endif
if d3d11_winapi_only_app
extra_deps += [d3dcompiler_lib, runtimeobject_lib]
endif
# MinGW 32bits compiler seems to be complaining about redundant-decls
# when ComPtr is in use. Let's just disable the warning
if cc.get_id() != 'msvc'
@ -186,7 +204,7 @@ gstd3d11 = library('gstd3d11-' + api_version,
version : libversion,
soversion : soversion,
install : true,
dependencies : [gstbase_dep, gstvideo_dep, gmodule_dep, d3d11_lib, dxgi_lib]
dependencies : [gstbase_dep, gstvideo_dep, gmodule_dep, d3d11_lib, dxgi_lib] + extra_deps
)
pkgconfig.generate(gstd3d11,

View file

@ -44,7 +44,6 @@
#include "gstd3d11compositor.h"
#include "gstd3d11converter.h"
#include "gstd3d11shader.h"
#include "gstd3d11pluginutils.h"
#include <string.h>
#include <wrl.h>
@ -1823,7 +1822,8 @@ gst_d3d11_compositor_create_checker_quad (GstD3D11Compositor * self,
ps_src = checker_ps_src_luma;
}
if (!gst_d3d11_create_pixel_shader (self->device, ps_src, &ps)) {
hr = gst_d3d11_create_pixel_shader_simple (self->device, ps_src, "main", &ps);
if (!gst_d3d11_result (hr, self->device)) {
GST_ERROR_OBJECT (self, "Couldn't setup pixel shader");
return nullptr;
}
@ -1837,8 +1837,9 @@ gst_d3d11_compositor_create_checker_quad (GstD3D11Compositor * self,
input_desc.InputSlotClass = D3D11_INPUT_PER_VERTEX_DATA;
input_desc.InstanceDataStepRate = 0;
if (!gst_d3d11_create_vertex_shader (self->device, checker_vs_src,
&input_desc, 1, &vs, &layout)) {
hr = gst_d3d11_create_vertex_shader_simple (self->device, checker_vs_src,
"main", &input_desc, 1, &vs, &layout);
if (!gst_d3d11_result (hr, self->device)) {
GST_ERROR_OBJECT (self, "Couldn't setup vertex shader");
return nullptr;
}

View file

@ -23,7 +23,6 @@
#endif
#include "gstd3d11converter.h"
#include "gstd3d11shader.h"
#include "gstd3d11pluginutils.h"
#include <wrl.h>
#include <string.h>
@ -1159,7 +1158,6 @@ gst_d3d11_color_convert_setup_shader (GstD3D11Converter * self,
ComPtr < ID3D11Buffer > vertex_buffer;
ComPtr < ID3D11Buffer > index_buffer;
gint i;
gboolean ret;
memset (&sampler_desc, 0, sizeof (sampler_desc));
memset (input_desc, 0, sizeof (input_desc));
@ -1196,9 +1194,10 @@ gst_d3d11_color_convert_setup_shader (GstD3D11Converter * self,
cinfo->build_output_func[i], cinfo->gamma_decode_func,
cinfo->gamma_encode_func, cinfo->XYZ_convert_func);
ret = gst_d3d11_create_pixel_shader (device, shader_code, &ps[i]);
hr = gst_d3d11_create_pixel_shader_simple (device,
shader_code, "main", &ps[i]);
g_free (shader_code);
if (!ret) {
if (!gst_d3d11_result (hr, device)) {
return FALSE;
}
}
@ -1220,8 +1219,9 @@ gst_d3d11_color_convert_setup_shader (GstD3D11Converter * self,
input_desc[1].InputSlotClass = D3D11_INPUT_PER_VERTEX_DATA;
input_desc[1].InstanceDataStepRate = 0;
if (!gst_d3d11_create_vertex_shader (device, templ_vertex_shader,
input_desc, G_N_ELEMENTS (input_desc), &vs, &layout)) {
hr = gst_d3d11_create_vertex_shader_simple (device, templ_vertex_shader,
"main", input_desc, G_N_ELEMENTS (input_desc), &vs, &layout);
if (!gst_d3d11_result (hr, device)) {
GST_ERROR_OBJECT (self, "Couldn't vertex pixel shader");
return FALSE;
}

View file

@ -22,7 +22,6 @@
#endif
#include "gstd3d11overlaycompositor.h"
#include "gstd3d11shader.h"
#include "gstd3d11pluginutils.h"
#include <wrl.h>
@ -381,7 +380,9 @@ gst_d3d11_overlay_compositor_setup_shader (GstD3D11OverlayCompositor * self)
return FALSE;
}
if (!gst_d3d11_create_pixel_shader (device, templ_pixel_shader, &ps)) {
hr = gst_d3d11_create_pixel_shader_simple (device,
templ_pixel_shader, "main", &ps);
if (!gst_d3d11_result (hr, device)) {
GST_ERROR_OBJECT (self, "Couldn't create pixel shader");
return FALSE;
}
@ -402,8 +403,9 @@ gst_d3d11_overlay_compositor_setup_shader (GstD3D11OverlayCompositor * self)
input_desc[1].InputSlotClass = D3D11_INPUT_PER_VERTEX_DATA;
input_desc[1].InstanceDataStepRate = 0;
if (!gst_d3d11_create_vertex_shader (device, templ_vertex_shader,
input_desc, G_N_ELEMENTS (input_desc), &vs, &layout)) {
hr = gst_d3d11_create_vertex_shader_simple (device, templ_vertex_shader,
"main", input_desc, G_N_ELEMENTS (input_desc), &vs, &layout);
if (!gst_d3d11_result (hr, device)) {
GST_ERROR_OBJECT (self, "Couldn't vertex pixel shader");
return FALSE;
}

View file

@ -47,7 +47,6 @@
#endif
#include "gstd3d11screencapture.h"
#include "gstd3d11shader.h"
#include "gstd3d11pluginutils.h"
#include <string.h>
@ -654,14 +653,18 @@ private:
ComPtr<ID3D11VertexShader> vs;
ComPtr<ID3D11InputLayout> layout;
if (!gst_d3d11_create_vertex_shader (device,
vs_str, input_desc, G_N_ELEMENTS (input_desc), &vs, &layout)) {
HRESULT hr;
hr = gst_d3d11_create_vertex_shader_simple (device,
vs_str, "main", input_desc, G_N_ELEMENTS (input_desc), &vs, &layout);
if (!gst_d3d11_result (hr, device)) {
GST_ERROR ("Failed to create vertex shader");
return false;
}
ComPtr<ID3D11PixelShader> ps;
if (!gst_d3d11_create_pixel_shader (device, ps_str, &ps)) {
hr = gst_d3d11_create_pixel_shader_simple (device, ps_str, "main", &ps);
if (!gst_d3d11_result (hr, device)) {
GST_ERROR ("Failed to create pixel shader");
return false;
}
@ -678,7 +681,7 @@ private:
ID3D11Device *device_handle = gst_d3d11_device_get_device_handle (device);
ComPtr<ID3D11SamplerState> sampler;
HRESULT hr = device_handle->CreateSamplerState (&sampler_desc, &sampler);
hr = device_handle->CreateSamplerState (&sampler_desc, &sampler);
if (!gst_d3d11_result (hr, device)) {
GST_ERROR ("Failed to create sampler state, hr 0x%x", (guint) hr);
return false;

View file

@ -39,7 +39,6 @@
#include "gstd3d11screencapturesrc.h"
#include "gstd3d11screencapture.h"
#include "gstd3d11pluginutils.h"
#include "gstd3d11shader.h"
#include <wrl.h>
#include <string.h>
@ -663,13 +662,15 @@ gst_d3d11_screen_capture_prepare_shader (GstD3D11ScreenCaptureSrc * self)
device_handle = gst_d3d11_device_get_device_handle (self->device);
if (!gst_d3d11_create_vertex_shader (self->device,
vs_str, input_desc, G_N_ELEMENTS (input_desc), &vs, &layout)) {
hr = gst_d3d11_create_vertex_shader_simple (self->device,
vs_str, "main", input_desc, G_N_ELEMENTS (input_desc), &vs, &layout);
if (!gst_d3d11_result (hr, self->device)) {
GST_ERROR_OBJECT (self, "Failed to create vertex shader");
return FALSE;
}
if (!gst_d3d11_create_pixel_shader (self->device, ps_str, &ps)) {
hr = gst_d3d11_create_pixel_shader_simple (self->device, ps_str, "main", &ps);
if (!gst_d3d11_result (hr, self->device)) {
GST_ERROR_OBJECT (self, "Failed to create pixel shader");
return FALSE;
}

View file

@ -1,228 +0,0 @@
/* GStreamer
* Copyright (C) <2019> Seungha Yang <seungha.yang@navercorp.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 "gstd3d11shader.h"
#include "gstd3d11pluginutils.h"
#include <gmodule.h>
#include <wrl.h>
/* *INDENT-OFF* */
using namespace Microsoft::WRL;
/* *INDENT-ON* */
GST_DEBUG_CATEGORY_EXTERN (gst_d3d11_shader_debug);
#define GST_CAT_DEFAULT gst_d3d11_shader_debug
static GModule *d3d_compiler_module = NULL;
static pD3DCompile GstD3DCompileFunc = NULL;
gboolean
gst_d3d11_shader_init (void)
{
static gsize _init = 0;
if (g_once_init_enter (&_init)) {
#if GST_D3D11_WINAPI_ONLY_APP
/* Assuming that d3d compiler library is available */
GstD3DCompileFunc = D3DCompile;
#else
static const gchar *d3d_compiler_names[] = {
"d3dcompiler_47.dll",
"d3dcompiler_46.dll",
"d3dcompiler_45.dll",
"d3dcompiler_44.dll",
"d3dcompiler_43.dll",
};
guint i;
for (i = 0; i < G_N_ELEMENTS (d3d_compiler_names); i++) {
d3d_compiler_module =
g_module_open (d3d_compiler_names[i], G_MODULE_BIND_LAZY);
if (d3d_compiler_module) {
GST_INFO ("D3D compiler %s is available", d3d_compiler_names[i]);
if (!g_module_symbol (d3d_compiler_module, "D3DCompile",
(gpointer *) & GstD3DCompileFunc)) {
GST_ERROR ("Cannot load D3DCompile symbol from %s",
d3d_compiler_names[i]);
g_module_close (d3d_compiler_module);
d3d_compiler_module = NULL;
GstD3DCompileFunc = NULL;
} else {
break;
}
}
}
if (!GstD3DCompileFunc)
GST_WARNING ("D3D11 compiler library is unavailable");
#endif
g_once_init_leave (&_init, 1);
}
return !!GstD3DCompileFunc;
}
static gboolean
compile_shader (GstD3D11Device * device, const gchar * shader_source,
gboolean is_pixel_shader, ID3DBlob ** blob)
{
const gchar *shader_target;
D3D_FEATURE_LEVEL feature_level;
HRESULT hr;
ID3D11Device *device_handle;
/* *INDENT-OFF* */
ComPtr<ID3DBlob> ret;
ComPtr<ID3DBlob> error;
/* *INDENT-ON* */
if (!gst_d3d11_shader_init ()) {
GST_ERROR ("D3DCompiler is unavailable");
return FALSE;
}
device_handle = gst_d3d11_device_get_device_handle (device);
feature_level = device_handle->GetFeatureLevel ();
if (is_pixel_shader) {
if (feature_level >= D3D_FEATURE_LEVEL_10_0)
shader_target = "ps_4_0";
else if (feature_level >= D3D_FEATURE_LEVEL_9_3)
shader_target = "ps_4_0_level_9_3";
else
shader_target = "ps_4_0_level_9_1";
} else {
if (feature_level >= D3D_FEATURE_LEVEL_10_0)
shader_target = "vs_4_0";
else if (feature_level >= D3D_FEATURE_LEVEL_9_3)
shader_target = "vs_4_0_level_9_3";
else
shader_target = "vs_4_0_level_9_1";
}
g_assert (GstD3DCompileFunc);
GST_TRACE ("Compile code \n%s", shader_source);
hr = GstD3DCompileFunc (shader_source, strlen (shader_source), NULL, NULL,
NULL, "main", shader_target, 0, 0, &ret, &error);
if (!gst_d3d11_result (hr, device)) {
const gchar *err = NULL;
if (error)
err = (const gchar *) error->GetBufferPointer ();
GST_ERROR ("could not compile source, hr: 0x%x, error detail %s",
(guint) hr, GST_STR_NULL (err));
return FALSE;
}
if (error) {
const gchar *err = (const gchar *) error->GetBufferPointer ();
GST_DEBUG ("HLSL compiler warnings:\n%s\nShader code:\n%s",
GST_STR_NULL (err), GST_STR_NULL (shader_source));
}
*blob = ret.Detach ();
return TRUE;
}
gboolean
gst_d3d11_create_pixel_shader (GstD3D11Device * device,
const gchar * source, ID3D11PixelShader ** shader)
{
ID3D11Device *device_handle;
HRESULT hr;
/* *INDENT-OFF* */
ComPtr<ID3DBlob> ps_blob;
/* *INDENT-ON* */
g_return_val_if_fail (GST_IS_D3D11_DEVICE (device), FALSE);
g_return_val_if_fail (source != NULL, FALSE);
g_return_val_if_fail (shader != NULL, FALSE);
if (!compile_shader (device, source, TRUE, &ps_blob)) {
GST_ERROR ("Failed to compile pixel shader");
return FALSE;
}
device_handle = gst_d3d11_device_get_device_handle (device);
hr = device_handle->CreatePixelShader (ps_blob->GetBufferPointer (),
ps_blob->GetBufferSize (), NULL, shader);
if (!gst_d3d11_result (hr, device)) {
GST_ERROR ("could not create pixel shader, hr: 0x%x", (guint) hr);
return FALSE;
}
return TRUE;
}
gboolean
gst_d3d11_create_vertex_shader (GstD3D11Device * device, const gchar * source,
const D3D11_INPUT_ELEMENT_DESC * input_desc, guint desc_len,
ID3D11VertexShader ** shader, ID3D11InputLayout ** layout)
{
ID3D11Device *device_handle;
HRESULT hr;
/* *INDENT-OFF* */
ComPtr<ID3DBlob> vs_blob;
ComPtr<ID3D11VertexShader> vs;
ComPtr<ID3D11InputLayout> in_layout;
/* *INDENT-ON* */
g_return_val_if_fail (GST_IS_D3D11_DEVICE (device), FALSE);
g_return_val_if_fail (source != NULL, FALSE);
g_return_val_if_fail (input_desc != NULL, FALSE);
g_return_val_if_fail (desc_len > 0, FALSE);
g_return_val_if_fail (shader != NULL, FALSE);
g_return_val_if_fail (layout != NULL, FALSE);
if (!compile_shader (device, source, FALSE, &vs_blob)) {
GST_ERROR ("Failed to compile shader code");
return FALSE;
}
device_handle = gst_d3d11_device_get_device_handle (device);
hr = device_handle->CreateVertexShader (vs_blob->GetBufferPointer (),
vs_blob->GetBufferSize (), NULL, &vs);
if (!gst_d3d11_result (hr, device)) {
GST_ERROR ("could not create vertex shader, hr: 0x%x", (guint) hr);
return FALSE;
}
hr = device_handle->CreateInputLayout (input_desc,
desc_len, vs_blob->GetBufferPointer (),
vs_blob->GetBufferSize (), &in_layout);
if (!gst_d3d11_result (hr, device)) {
GST_ERROR ("could not create input layout shader, hr: 0x%x", (guint) hr);
return FALSE;
}
*shader = vs.Detach ();
*layout = in_layout.Detach ();
return TRUE;
}

View file

@ -1,46 +0,0 @@
/* GStreamer
* Copyright (C) <2019> Seungha Yang <seungha.yang@navercorp.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.
*/
#ifndef __GST_D3D11_SHADER_H__
#define __GST_D3D11_SHADER_H__
#include <gst/gst.h>
#include <gst/video/video.h>
#include <gst/d3d11/gstd3d11.h>
#include <d3dcompiler.h>
G_BEGIN_DECLS
gboolean gst_d3d11_shader_init (void);
gboolean gst_d3d11_create_pixel_shader (GstD3D11Device * device,
const gchar * source,
ID3D11PixelShader ** shader);
gboolean gst_d3d11_create_vertex_shader (GstD3D11Device * device,
const gchar * source,
const D3D11_INPUT_ELEMENT_DESC * input_desc,
guint desc_len,
ID3D11VertexShader ** shader,
ID3D11InputLayout ** layout);
G_END_DECLS
#endif /* __GST_D3D11_SHADER_H__ */

View file

@ -38,7 +38,6 @@
#include "gstd3d11testsrc.h"
#include "gstd3d11pluginutils.h"
#include "gstd3d11shader.h"
#include "gstd3d11converter.h"
#include <wrl.h>
#include <string.h>
@ -324,7 +323,6 @@ setup_snow_render (GstD3D11TestSrc * self, GstD3D11TestSrcRender * render,
guint on_smpte)
{
HRESULT hr;
gboolean ret;
D3D11_INPUT_ELEMENT_DESC input_desc[2];
D3D11_BUFFER_DESC buffer_desc;
D3D11_MAPPED_SUBRESOURCE map;
@ -361,14 +359,16 @@ setup_snow_render (GstD3D11TestSrc * self, GstD3D11TestSrcRender * render,
input_desc[1].InputSlotClass = D3D11_INPUT_PER_VERTEX_DATA;
input_desc[1].InstanceDataStepRate = 0;
if (!gst_d3d11_create_vertex_shader (self->device, templ_vs_coord,
input_desc, G_N_ELEMENTS (input_desc), &vs, &layout)) {
hr = gst_d3d11_create_vertex_shader_simple (self->device, templ_vs_coord,
"main", input_desc, G_N_ELEMENTS (input_desc), &vs, &layout);
if (!gst_d3d11_result (hr, self->device)) {
GST_ERROR_OBJECT (self, "Failed to compile vertext shader");
return FALSE;
}
ret = gst_d3d11_create_pixel_shader (self->device, templ_ps_snow, &ps);
if (!ret) {
hr = gst_d3d11_create_pixel_shader_simple (self->device,
templ_ps_snow, "main", &ps);
if (!gst_d3d11_result (hr, self->device)) {
GST_ERROR_OBJECT (self, "Failed to compile pixel shader");
return FALSE;
}
@ -527,7 +527,6 @@ static gboolean
setup_smpte_render (GstD3D11TestSrc * self, GstD3D11TestSrcRender * render)
{
HRESULT hr;
gboolean ret;
D3D11_INPUT_ELEMENT_DESC input_desc[2];
D3D11_BUFFER_DESC buffer_desc;
D3D11_MAPPED_SUBRESOURCE map;
@ -565,14 +564,17 @@ setup_smpte_render (GstD3D11TestSrc * self, GstD3D11TestSrcRender * render)
input_desc[1].InputSlotClass = D3D11_INPUT_PER_VERTEX_DATA;
input_desc[1].InstanceDataStepRate = 0;
if (!gst_d3d11_create_vertex_shader (self->device, templ_vs_color,
input_desc, G_N_ELEMENTS (input_desc), &vs, &layout)) {
hr = gst_d3d11_create_vertex_shader_simple (self->device, templ_vs_coord,
"main", input_desc, G_N_ELEMENTS (input_desc), &vs, &layout);
if (!gst_d3d11_result (hr, self->device)) {
GST_ERROR_OBJECT (self, "Failed to compile vertext shader");
return FALSE;
}
ret = gst_d3d11_create_pixel_shader (self->device, templ_ps_smpte, &ps);
if (!ret) {
hr = gst_d3d11_create_pixel_shader_simple (self->device,
templ_ps_smpte, "main", &ps);
if (!gst_d3d11_result (hr, self->device)) {
GST_ERROR_OBJECT (self, "Failed to compile pixel shader");
return FALSE;
}
@ -863,7 +865,6 @@ setup_checker_render (GstD3D11TestSrc * self, GstD3D11TestSrcRender * render,
guint checker_size)
{
HRESULT hr;
gboolean ret;
D3D11_INPUT_ELEMENT_DESC input_desc[2];
D3D11_BUFFER_DESC buffer_desc;
D3D11_MAPPED_SUBRESOURCE map;
@ -900,17 +901,18 @@ setup_checker_render (GstD3D11TestSrc * self, GstD3D11TestSrcRender * render,
input_desc[1].InputSlotClass = D3D11_INPUT_PER_VERTEX_DATA;
input_desc[1].InstanceDataStepRate = 0;
if (!gst_d3d11_create_vertex_shader (self->device, templ_vs_coord,
input_desc, G_N_ELEMENTS (input_desc), &vs, &layout)) {
hr = gst_d3d11_create_vertex_shader_simple (self->device, templ_vs_coord,
"main", input_desc, G_N_ELEMENTS (input_desc), &vs, &layout);
if (!gst_d3d11_result (hr, self->device)) {
GST_ERROR_OBJECT (self, "Failed to compile vertext shader");
return FALSE;
}
ps_src = g_strdup_printf (templ_ps_checker,
self->info.width, self->info.height, checker_size);
ret = gst_d3d11_create_pixel_shader (self->device, ps_src, &ps);
hr = gst_d3d11_create_pixel_shader_simple (self->device, ps_src, "main", &ps);
g_free (ps_src);
if (!ret) {
if (!gst_d3d11_result (hr, self->device)) {
GST_ERROR_OBJECT (self, "Failed to compile pixel shader");
return FALSE;
}

View file

@ -12,7 +12,6 @@ d3d11_sources = [
'gstd3d11mpeg2dec.cpp',
'gstd3d11overlaycompositor.cpp',
'gstd3d11pluginutils.cpp',
'gstd3d11shader.cpp',
'gstd3d11testsrc.cpp',
'gstd3d11upload.cpp',
'gstd3d11videosink.cpp',
@ -32,14 +31,13 @@ if host_system != 'windows' or d3d11_option.disabled()
subdir_done()
endif
if not gstd3d11_dep.found() or not cc.has_header('dxva.h') or not cc.has_header('d3d9.h') or not cc.has_header('d3dcompiler.h')
if not gstd3d11_dep.found() or not cc.has_header('dxva.h') or not cc.has_header('d3d9.h')
if d3d11_option.enabled()
error('The d3d11 was enabled explicitly, but required dependencies were not found.')
endif
subdir_done()
endif
d3dcompiler_lib = cc.find_library('d3dcompiler', required: d3d11_option)
runtimeobject_lib = cc.find_library('runtimeobject', required : false)
winmm_lib = cc.find_library('winmm', required: false)
@ -55,7 +53,7 @@ endif
if d3d11_winapi_app
d3d11_sources += ['gstd3d11window_corewindow.cpp',
'gstd3d11window_swapchainpanel.cpp']
extra_dep += [runtimeobject_lib, d3dcompiler_lib]
extra_dep += [runtimeobject_lib]
endif
if d3d11_winapi_desktop

View file

@ -66,7 +66,6 @@
#include "gstd3d11upload.h"
#include "gstd3d11download.h"
#include "gstd3d11convert.h"
#include "gstd3d11shader.h"
#include "gstd3d11compositor.h"
#include "gstd3d11h264dec.h"
#include "gstd3d11h265dec.h"
@ -89,7 +88,6 @@ using namespace Microsoft::WRL;
/* *INDENT-ON* */
GST_DEBUG_CATEGORY (gst_d3d11_debug);
GST_DEBUG_CATEGORY (gst_d3d11_shader_debug);
GST_DEBUG_CATEGORY (gst_d3d11_plugin_utils_debug);
GST_DEBUG_CATEGORY (gst_d3d11_format_debug);
GST_DEBUG_CATEGORY (gst_d3d11_device_debug);
@ -121,8 +119,6 @@ plugin_init (GstPlugin * plugin)
ComPtr < IDXGIFactory1 > factory;
GST_DEBUG_CATEGORY_INIT (gst_d3d11_debug, "d3d11", 0, "direct3d 11 plugin");
GST_DEBUG_CATEGORY_INIT (gst_d3d11_shader_debug,
"d3d11shader", 0, "d3d11shader");
GST_DEBUG_CATEGORY_INIT (gst_d3d11_plugin_utils_debug,
"d3d11pluginutils", 0, "d3d11 plugin utility functions");
GST_DEBUG_CATEGORY_INIT (gst_d3d11_overlay_compositor_debug,
@ -132,8 +128,8 @@ plugin_init (GstPlugin * plugin)
GST_DEBUG_CATEGORY_INIT (gst_d3d11_video_processor_debug,
"d3d11videoprocessor", 0, "d3d11videoprocessor");
if (!gst_d3d11_shader_init ()) {
GST_WARNING ("Cannot initialize d3d11 shader");
if (!gst_d3d11_compile_init ()) {
GST_WARNING ("Cannot initialize d3d11 compiler");
return TRUE;
}