d3d11: Reuse sampler object

The linear sampler object can be reused

Part-of: <https://gitlab.freedesktop.org/gstreamer/gstreamer/-/merge_requests/5457>
This commit is contained in:
Seungha Yang 2023-10-10 20:01:57 +09:00 committed by GStreamer Marge Bot
parent 92cc5722c8
commit 83a576e854
5 changed files with 50 additions and 35 deletions

View file

@ -53,5 +53,10 @@ HRESULT gst_d3d11_device_get_vertex_shader (GstD3D11Device * device,
ID3D11VertexShader ** vs, ID3D11VertexShader ** vs,
ID3D11InputLayout ** layout); ID3D11InputLayout ** layout);
GST_D3D11_API
HRESULT gst_d3d11_device_get_sampler (GstD3D11Device * device,
D3D11_FILTER filter,
ID3D11SamplerState ** sampler);
G_END_DECLS G_END_DECLS

View file

@ -39,6 +39,7 @@
#include <utility> #include <utility>
#include <atomic> #include <atomic>
#include <mutex> #include <mutex>
#include <string.h>
/** /**
* SECTION:gstd3d11device * SECTION:gstd3d11device
@ -130,6 +131,7 @@ struct _GstD3D11DevicePrivate
std::map <gint64, ComPtr<ID3D11PixelShader>> ps_cache; std::map <gint64, ComPtr<ID3D11PixelShader>> ps_cache;
std::map <gint64, std::map <gint64,
std::pair<ComPtr<ID3D11VertexShader>, ComPtr<ID3D11InputLayout>>> vs_cache; std::pair<ComPtr<ID3D11VertexShader>, ComPtr<ID3D11InputLayout>>> vs_cache;
std::map <D3D11_FILTER, ComPtr<ID3D11SamplerState>> sampler_cache;
#if HAVE_D3D11SDKLAYERS_H #if HAVE_D3D11SDKLAYERS_H
ID3D11Debug *d3d11_debug = nullptr; ID3D11Debug *d3d11_debug = nullptr;
@ -745,6 +747,7 @@ gst_d3d11_device_dispose (GObject * object)
priv->ps_cache.clear (); priv->ps_cache.clear ();
priv->vs_cache.clear (); priv->vs_cache.clear ();
priv->sampler_cache.clear ();
GST_D3D11_CLEAR_COM (priv->device5); GST_D3D11_CLEAR_COM (priv->device5);
GST_D3D11_CLEAR_COM (priv->device_context4); GST_D3D11_CLEAR_COM (priv->device_context4);
@ -1843,3 +1846,39 @@ gst_d3d11_device_get_vertex_shader (GstD3D11Device * device, gint64 token,
return S_OK; return S_OK;
} }
HRESULT
gst_d3d11_device_get_sampler (GstD3D11Device * device, D3D11_FILTER filter,
ID3D11SamplerState ** sampler)
{
GstD3D11DevicePrivate *priv = device->priv;
ComPtr < ID3D11SamplerState > state;
D3D11_SAMPLER_DESC desc;
HRESULT hr;
std::lock_guard < std::mutex > lk (priv->resource_lock);
auto cached = priv->sampler_cache.find (filter);
if (cached != priv->sampler_cache.end ()) {
*sampler = cached->second.Get ();
(*sampler)->AddRef ();
return S_OK;
}
memset (&desc, 0, sizeof (D3D11_SAMPLER_DESC));
desc.Filter = filter;
desc.AddressU = D3D11_TEXTURE_ADDRESS_CLAMP;
desc.AddressV = D3D11_TEXTURE_ADDRESS_CLAMP;
desc.AddressW = D3D11_TEXTURE_ADDRESS_CLAMP;
desc.ComparisonFunc = D3D11_COMPARISON_ALWAYS;
desc.MaxLOD = D3D11_FLOAT32_MAX;
hr = priv->device->CreateSamplerState (&desc, &state);
if (!gst_d3d11_result (hr, device))
return hr;
priv->sampler_cache[filter] = state;
*sampler = state.Detach ();
return S_OK;
}

View file

@ -641,19 +641,9 @@ private:
return false; return false;
} }
D3D11_SAMPLER_DESC sampler_desc;
memset (&sampler_desc, 0, sizeof (D3D11_SAMPLER_DESC));
sampler_desc.Filter = D3D11_FILTER_MIN_MAG_MIP_LINEAR;
sampler_desc.AddressU = D3D11_TEXTURE_ADDRESS_CLAMP;
sampler_desc.AddressV = D3D11_TEXTURE_ADDRESS_CLAMP;
sampler_desc.AddressW = D3D11_TEXTURE_ADDRESS_CLAMP;
sampler_desc.ComparisonFunc = D3D11_COMPARISON_NEVER;
sampler_desc.MinLOD = 0;
sampler_desc.MaxLOD = D3D11_FLOAT32_MAX;
ID3D11Device *device_handle = gst_d3d11_device_get_device_handle (device);
ComPtr<ID3D11SamplerState> sampler; ComPtr<ID3D11SamplerState> sampler;
hr = device_handle->CreateSamplerState (&sampler_desc, &sampler); hr = gst_d3d11_device_get_sampler (device, D3D11_FILTER_MIN_MAG_MIP_LINEAR,
&sampler);
if (!gst_d3d11_result (hr, device)) { if (!gst_d3d11_result (hr, device)) {
GST_ERROR ("Failed to create sampler state, hr 0x%x", (guint) hr); GST_ERROR ("Failed to create sampler state, hr 0x%x", (guint) hr);
return false; return false;

View file

@ -327,7 +327,6 @@ gst_d3d11_overlay_compositor_setup_shader (GstD3D11OverlayCompositor * self)
GstVideoInfo *info = &priv->info; GstVideoInfo *info = &priv->info;
GstD3D11Device *device = self->device; GstD3D11Device *device = self->device;
HRESULT hr; HRESULT hr;
D3D11_SAMPLER_DESC sampler_desc;
D3D11_BUFFER_DESC buffer_desc; D3D11_BUFFER_DESC buffer_desc;
D3D11_BLEND_DESC blend_desc; D3D11_BLEND_DESC blend_desc;
D3D11_MAPPED_SUBRESOURCE map; D3D11_MAPPED_SUBRESOURCE map;
@ -342,23 +341,14 @@ gst_d3d11_overlay_compositor_setup_shader (GstD3D11OverlayCompositor * self)
ComPtr < ID3D11BlendState > blend; ComPtr < ID3D11BlendState > blend;
ComPtr < ID3D11Buffer > index_buffer; ComPtr < ID3D11Buffer > index_buffer;
memset (&sampler_desc, 0, sizeof (sampler_desc));
memset (&buffer_desc, 0, sizeof (buffer_desc)); memset (&buffer_desc, 0, sizeof (buffer_desc));
memset (&blend_desc, 0, sizeof (blend_desc)); memset (&blend_desc, 0, sizeof (blend_desc));
device_handle = gst_d3d11_device_get_device_handle (device); device_handle = gst_d3d11_device_get_device_handle (device);
context_handle = gst_d3d11_device_get_device_context_handle (device); context_handle = gst_d3d11_device_get_device_context_handle (device);
/* bilinear filtering */ hr = gst_d3d11_device_get_sampler (device,
sampler_desc.Filter = D3D11_FILTER_MIN_MAG_LINEAR_MIP_POINT; D3D11_FILTER_MIN_MAG_LINEAR_MIP_POINT, &sampler);
sampler_desc.AddressU = D3D11_TEXTURE_ADDRESS_CLAMP;
sampler_desc.AddressV = D3D11_TEXTURE_ADDRESS_CLAMP;
sampler_desc.AddressW = D3D11_TEXTURE_ADDRESS_CLAMP;
sampler_desc.ComparisonFunc = D3D11_COMPARISON_ALWAYS;
sampler_desc.MinLOD = 0;
sampler_desc.MaxLOD = D3D11_FLOAT32_MAX;
hr = device_handle->CreateSamplerState (&sampler_desc, &sampler);
if (!gst_d3d11_result (hr, device)) { if (!gst_d3d11_result (hr, device)) {
GST_ERROR_OBJECT (self, "Couldn't create sampler state, hr: 0x%x", GST_ERROR_OBJECT (self, "Couldn't create sampler state, hr: 0x%x",
(guint) hr); (guint) hr);

View file

@ -875,7 +875,6 @@ gst_d3d11_screen_capture_prepare_shader (GstD3D11ScreenCaptureSrc * self)
ComPtr < ID3D11PixelShader > ps; ComPtr < ID3D11PixelShader > ps;
ComPtr < ID3D11SamplerState > sampler; ComPtr < ID3D11SamplerState > sampler;
ComPtr < ID3D11BlendState > blend; ComPtr < ID3D11BlendState > blend;
D3D11_SAMPLER_DESC sampler_desc;
D3D11_BLEND_DESC blend_desc; D3D11_BLEND_DESC blend_desc;
ID3D11Device *device_handle; ID3D11Device *device_handle;
HRESULT hr; HRESULT hr;
@ -894,16 +893,8 @@ gst_d3d11_screen_capture_prepare_shader (GstD3D11ScreenCaptureSrc * self)
return FALSE; return FALSE;
} }
memset (&sampler_desc, 0, sizeof (D3D11_SAMPLER_DESC)); hr = gst_d3d11_device_get_sampler (self->device,
sampler_desc.Filter = D3D11_FILTER_MIN_MAG_MIP_LINEAR; D3D11_FILTER_MIN_MAG_MIP_LINEAR, &sampler);
sampler_desc.AddressU = D3D11_TEXTURE_ADDRESS_CLAMP;
sampler_desc.AddressV = D3D11_TEXTURE_ADDRESS_CLAMP;
sampler_desc.AddressW = D3D11_TEXTURE_ADDRESS_CLAMP;
sampler_desc.ComparisonFunc = D3D11_COMPARISON_NEVER;
sampler_desc.MinLOD = 0;
sampler_desc.MaxLOD = D3D11_FLOAT32_MAX;
hr = device_handle->CreateSamplerState (&sampler_desc, &sampler);
if (!gst_d3d11_result (hr, self->device)) { if (!gst_d3d11_result (hr, self->device)) {
GST_ERROR_OBJECT (self, GST_ERROR_OBJECT (self,
"Failed to create sampler state, hr 0x%x", (guint) hr); "Failed to create sampler state, hr 0x%x", (guint) hr);