• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * GStreamer
3  * Copyright (C) 2020 Seungha Yang <seungha@centricular.com>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Library General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Library General Public License for more details.
14  *
15  * You should have received a copy of the GNU Library General Public
16  * License along with this library; if not, write to the
17  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
18  * Boston, MA 02110-1301, USA.
19  */
20 
21 #ifdef HAVE_CONFIG_H
22 #include "config.h"
23 #endif
24 
25 #include "d3d11device.h"
26 #include <d3dcompiler.h>
27 #include <wrl.h>
28 #include <string.h>
29 
30 using namespace Microsoft::WRL;
31 
32 HRESULT
prepare_d3d11_device(ID3D11Device ** d3d11_device,ID3D11DeviceContext ** d3d11_context,IDXGIFactory2 ** dxgi_factory)33 prepare_d3d11_device (ID3D11Device ** d3d11_device,
34     ID3D11DeviceContext ** d3d11_context, IDXGIFactory2 ** dxgi_factory)
35 {
36   HRESULT hr;
37   static const D3D_FEATURE_LEVEL feature_levels[] = {
38     D3D_FEATURE_LEVEL_11_1,
39     D3D_FEATURE_LEVEL_11_0,
40     D3D_FEATURE_LEVEL_10_1,
41     D3D_FEATURE_LEVEL_10_0,
42     D3D_FEATURE_LEVEL_9_3,
43     D3D_FEATURE_LEVEL_9_2,
44     D3D_FEATURE_LEVEL_9_1
45   };
46   D3D_FEATURE_LEVEL selected_level;
47 
48   ComPtr<IDXGIFactory1> factory;
49   hr = CreateDXGIFactory1 (IID_PPV_ARGS (&factory));
50   if (FAILED (hr)) {
51     gst_printerrln ("IDXGIFactory1 is unavailable, hr 0x%x", (guint) hr);
52     return hr;
53   }
54 
55   ComPtr<IDXGIFactory2> factory2;
56   hr = factory.As (&factory2);
57   if (FAILED (hr)) {
58     gst_printerrln ("IDXGIFactory2 is unavailable, hr 0x%x", (guint) hr);
59     return hr;
60   }
61 
62   ComPtr<IDXGIAdapter1> adapter;
63   hr = factory->EnumAdapters1 (0, &adapter);
64   if (FAILED (hr)) {
65     gst_printerrln ("IDXGIAdapter1 is unavailable, hr 0x%x", (guint) hr);
66     return hr;
67   }
68 
69   ComPtr<ID3D11Device> device;
70   ComPtr<ID3D11DeviceContext> context;
71   hr = D3D11CreateDevice (adapter.Get(), D3D_DRIVER_TYPE_UNKNOWN,
72       NULL, D3D11_CREATE_DEVICE_BGRA_SUPPORT, feature_levels,
73       G_N_ELEMENTS (feature_levels), D3D11_SDK_VERSION, &device,
74       &selected_level, &context);
75 
76   /* Try again with excluding D3D_FEATURE_LEVEL_11_1 */
77   if (FAILED (hr)) {
78     hr = D3D11CreateDevice (adapter.Get(), D3D_DRIVER_TYPE_UNKNOWN,
79       NULL, D3D11_CREATE_DEVICE_BGRA_SUPPORT, &feature_levels[1],
80       G_N_ELEMENTS (feature_levels) - 1, D3D11_SDK_VERSION, &device,
81       &selected_level, &context);
82   }
83 
84   if (FAILED (hr)) {
85     gst_printerrln ("ID3D11Device is unavailable, hr 0x%x", (guint) hr);
86     return hr;
87   }
88 
89   if (d3d11_device)
90     *d3d11_device = device.Detach();
91   if (d3d11_context)
92     *d3d11_context = context.Detach();
93   if (dxgi_factory)
94     *dxgi_factory = factory2.Detach();
95 
96   return hr;
97 }
98 
99 HRESULT
prepare_shared_texture(ID3D11Device * d3d11_device,guint width,guint height,DXGI_FORMAT format,UINT misc_flags,ID3D11Texture2D ** texture,ID3D11ShaderResourceView ** srv,IDXGIKeyedMutex ** keyed_mutex,HANDLE * shared_handle)100 prepare_shared_texture (ID3D11Device * d3d11_device, guint width,
101     guint height, DXGI_FORMAT format, UINT misc_flags,
102     ID3D11Texture2D ** texture, ID3D11ShaderResourceView ** srv,
103     IDXGIKeyedMutex ** keyed_mutex, HANDLE * shared_handle)
104 {
105   D3D11_TEXTURE2D_DESC texture_desc = { 0, };
106   HRESULT hr;
107 
108   /* Texture size doesn't need to be identical to that of backbuffer */
109   texture_desc.Width = width;
110   texture_desc.Height = height;
111   texture_desc.MipLevels = 1;
112   texture_desc.Format = format;
113   texture_desc.SampleDesc.Count = 1;
114   texture_desc.ArraySize = 1;
115   texture_desc.Usage = D3D11_USAGE_DEFAULT;
116   texture_desc.BindFlags =
117       D3D11_BIND_RENDER_TARGET | D3D11_BIND_SHADER_RESOURCE;
118   texture_desc.MiscFlags = misc_flags;
119 
120   ComPtr<ID3D11Texture2D> shared_texture;
121   hr = d3d11_device->CreateTexture2D (&texture_desc, nullptr, &shared_texture);
122   if (FAILED (hr)) {
123     gst_printerrln ("Couldn't create ID3D11Texture2D");
124     return hr;
125   }
126 
127   ComPtr<ID3D11ShaderResourceView> shader_resource_view;
128   if (srv) {
129     D3D11_SHADER_RESOURCE_VIEW_DESC srv_desc = { 0, };
130     srv_desc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D;
131     srv_desc.Texture2D.MipLevels = 1;
132     srv_desc.Format = format;
133 
134     hr = d3d11_device->CreateShaderResourceView (shared_texture.Get(), &srv_desc,
135         &shader_resource_view);
136     if (FAILED (hr)) {
137       gst_printerrln ("Couldn't create ID3D11ShaderResourceView");
138       return hr;
139     }
140   }
141 
142   ComPtr<IDXGIKeyedMutex> keyed;
143   if ((misc_flags & D3D11_RESOURCE_MISC_SHARED_KEYEDMUTEX) != 0 && keyed_mutex) {
144     hr = shared_texture.As (&keyed);
145     if (FAILED (hr)) {
146       gst_printerrln ("Couldn't get IDXGIKeyedMutex");
147       return hr;
148     }
149   }
150 
151   ComPtr<IDXGIResource> dxgi_resource;
152   hr = shared_texture.As (&dxgi_resource);
153   if (FAILED (hr)) {
154     gst_printerrln ("Couldn't get IDXGIResource handle");
155     return hr;
156   }
157 
158   HANDLE handle;
159   if ((misc_flags & D3D11_RESOURCE_MISC_SHARED_NTHANDLE) != 0) {
160     ComPtr<IDXGIResource1> dxgi_resource1;
161     hr = dxgi_resource.As (&dxgi_resource1);
162 
163     if (FAILED (hr)) {
164       gst_printerrln ("Couldn't get IDXGIResource1");
165       return hr;
166     }
167 
168     hr = dxgi_resource1->CreateSharedHandle (nullptr,
169       DXGI_SHARED_RESOURCE_READ | DXGI_SHARED_RESOURCE_WRITE, nullptr,
170       &handle);
171   } else {
172     hr = dxgi_resource->GetSharedHandle (&handle);
173   }
174 
175   if (FAILED (hr)) {
176     gst_printerrln ("Couldn't get shared handle from texture");
177     return hr;
178   }
179 
180   *texture = shared_texture.Detach();
181   if (srv)
182     *srv = shader_resource_view.Detach();
183   *shared_handle = handle;
184   if (keyed && keyed_mutex)
185     *keyed_mutex = keyed.Detach();
186 
187   return S_OK;
188 }
189 
190 static HRESULT
d3d_compile(const gchar * source,gboolean is_pixel_shader,ID3DBlob ** code)191 d3d_compile (const gchar * source, gboolean is_pixel_shader, ID3DBlob ** code)
192 {
193   HRESULT hr;
194   const gchar *shader_target = "ps_4_0";
195 
196   if (!is_pixel_shader)
197     shader_target = "vs_4_0";
198 
199   ComPtr<ID3DBlob> blob;
200   ComPtr<ID3DBlob> error;
201   hr = D3DCompile (source, strlen (source), nullptr, nullptr,
202       nullptr, "main", shader_target, 0, 0, &blob, &error);
203 
204   if (FAILED (hr)) {
205     const gchar *err = nullptr;
206     if (error)
207       err = (const gchar *) error->GetBufferPointer ();
208 
209     gst_printerrln ("Couldn't compile pixel shader, error: %s",
210         GST_STR_NULL (err));
211     return hr;
212   }
213 
214   *code = blob.Detach();
215 
216   return S_OK;
217 }
218 
219 HRESULT
prepare_shader(ID3D11Device * d3d11_device,ID3D11DeviceContext * context,ID3D11SamplerState ** sampler,ID3D11PixelShader ** ps,ID3D11VertexShader ** vs,ID3D11InputLayout ** layout,ID3D11Buffer ** vertex,ID3D11Buffer ** index)220 prepare_shader (ID3D11Device * d3d11_device, ID3D11DeviceContext * context,
221     ID3D11SamplerState ** sampler, ID3D11PixelShader ** ps,
222     ID3D11VertexShader ** vs, ID3D11InputLayout ** layout,
223     ID3D11Buffer ** vertex, ID3D11Buffer ** index)
224 {
225   static const gchar ps_code[] =
226     "Texture2D shaderTexture;\n"
227     "SamplerState samplerState;\n"
228     "\n"
229     "struct PS_INPUT\n"
230     "{\n"
231     "  float4 Position: SV_POSITION;\n"
232     "  float3 Texture: TEXCOORD0;\n"
233     "};\n"
234     "\n"
235     "struct PS_OUTPUT\n"
236     "{\n"
237     "  float4 Plane: SV_Target;\n"
238     "};\n"
239     "\n"
240     "PS_OUTPUT main(PS_INPUT input)\n"
241     "{\n"
242     "  PS_OUTPUT output;\n"
243     "  output.Plane = shaderTexture.Sample(samplerState, input.Texture);\n"
244     "  return output;\n"
245     "}\n";
246 
247   static const gchar vs_code[] =
248     "struct VS_INPUT\n"
249     "{\n"
250     "  float4 Position : POSITION;\n"
251     "  float4 Texture : TEXCOORD0;\n"
252     "};\n"
253     "\n"
254     "struct VS_OUTPUT\n"
255     "{\n"
256     "  float4 Position: SV_POSITION;\n"
257     "  float4 Texture: TEXCOORD0;\n"
258     "};\n"
259     "\n"
260     "VS_OUTPUT main(VS_INPUT input)\n"
261     "{\n"
262     "  return input;\n"
263     "}\n";
264 
265   D3D11_SAMPLER_DESC sampler_desc = { 0, };
266   sampler_desc.Filter = D3D11_FILTER_MIN_MAG_LINEAR_MIP_POINT;
267   sampler_desc.AddressU = D3D11_TEXTURE_ADDRESS_CLAMP;
268   sampler_desc.AddressV = D3D11_TEXTURE_ADDRESS_CLAMP;
269   sampler_desc.AddressW = D3D11_TEXTURE_ADDRESS_CLAMP;
270   sampler_desc.ComparisonFunc = D3D11_COMPARISON_ALWAYS;
271   sampler_desc.MinLOD = 0;
272   sampler_desc.MaxLOD = D3D11_FLOAT32_MAX;
273 
274   ComPtr<ID3D11SamplerState> sampler_state;
275   HRESULT hr = d3d11_device->CreateSamplerState (&sampler_desc, &sampler_state);
276   if (FAILED (hr)) {
277     gst_printerrln ("Couldn't create ID3D11SamplerState");
278     return hr;
279   }
280 
281   ComPtr<ID3DBlob> code;
282   hr = d3d_compile (ps_code, TRUE, &code);
283   if (FAILED (hr))
284     return hr;
285 
286   ComPtr<ID3D11PixelShader> pixel_shader;
287   hr = d3d11_device->CreatePixelShader (code->GetBufferPointer(),
288       code->GetBufferSize(), nullptr, &pixel_shader);
289   if (FAILED (hr)) {
290     gst_printerrln ("Couldn't create ID3D11PixelShader");
291     return hr;
292   }
293 
294   hr = d3d_compile (vs_code, FALSE, code.ReleaseAndGetAddressOf());
295   if (FAILED (hr))
296     return hr;
297 
298   ComPtr<ID3D11VertexShader> vertex_shader;
299   hr = d3d11_device->CreateVertexShader (code->GetBufferPointer(),
300       code->GetBufferSize(), nullptr, &vertex_shader);
301   if (FAILED (hr)) {
302     gst_printerrln ("Couldn't create ID3D11VertexShader");
303     return hr;
304   }
305 
306   D3D11_INPUT_ELEMENT_DESC input_desc[] = {
307     { "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0,
308         D3D11_APPEND_ALIGNED_ELEMENT, D3D11_INPUT_PER_VERTEX_DATA, 0 },
309     { "TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT, 0,
310         D3D11_APPEND_ALIGNED_ELEMENT, D3D11_INPUT_PER_VERTEX_DATA, 0 }
311   };
312 
313   ComPtr<ID3D11InputLayout> input_layout;
314   hr = d3d11_device->CreateInputLayout (input_desc, G_N_ELEMENTS (input_desc),
315       code->GetBufferPointer(), code->GetBufferSize(), &input_layout);
316   if (FAILED (hr)) {
317     gst_printerrln ("Couldn't create ID3D11InputLayout");
318     return hr;
319   }
320 
321   D3D11_BUFFER_DESC buffer_desc = { 0, };
322   buffer_desc.Usage = D3D11_USAGE_DYNAMIC;
323   buffer_desc.ByteWidth = sizeof (VertexData) * 4;
324   buffer_desc.BindFlags = D3D11_BIND_VERTEX_BUFFER;
325   buffer_desc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
326 
327   ComPtr<ID3D11Buffer> vertex_buffer;
328   hr = d3d11_device->CreateBuffer (&buffer_desc, nullptr, &vertex_buffer);
329   if (FAILED (hr)) {
330     gst_printerrln ("Couldn't create ID3D11Buffer for vertex buffer");
331     return hr;
332   }
333 
334   D3D11_MAPPED_SUBRESOURCE map;
335   hr = context->Map (vertex_buffer.Get(), 0, D3D11_MAP_WRITE_DISCARD, 0, &map);
336   if (FAILED (hr)) {
337     gst_printerrln ("Couldn't map vertex buffer");
338     return hr;
339   }
340 
341   VertexData *vertex_data = (VertexData *) map.pData;
342   vertex_data[0].position.x = -1.0f;
343   vertex_data[0].position.y = -1.0f;
344   vertex_data[0].position.z = 0.0f;
345   vertex_data[0].texture.x = 0.0f;
346   vertex_data[0].texture.y = 1.0f;
347 
348   vertex_data[1].position.x = -1.0f;
349   vertex_data[1].position.y = 1.0f;
350   vertex_data[1].position.z = 0.0f;
351   vertex_data[1].texture.x = 0.0f;
352   vertex_data[1].texture.y = 0.0f;
353 
354   vertex_data[2].position.x = 1.0f;
355   vertex_data[2].position.y = 1.0f;
356   vertex_data[2].position.z = 0.0f;
357   vertex_data[2].texture.x = 1.0f;
358   vertex_data[2].texture.y = 0.0f;
359 
360   vertex_data[3].position.x = 1.0f;
361   vertex_data[3].position.y = -1.0f;
362   vertex_data[3].position.z = 0.0f;
363   vertex_data[3].texture.x = 1.0f;
364   vertex_data[3].texture.y = 1.0f;
365 
366   context->Unmap (vertex_buffer.Get(), 0);
367 
368   ComPtr<ID3D11Buffer> index_buffer;
369   buffer_desc.ByteWidth = sizeof (WORD) * 2 * 3;
370   buffer_desc.BindFlags = D3D11_BIND_INDEX_BUFFER;
371   hr = d3d11_device->CreateBuffer (&buffer_desc, nullptr, &index_buffer);
372   if (FAILED (hr)) {
373     gst_printerrln ("Couldn't create ID3D11Buffer for index buffer");
374     return hr;
375   }
376 
377   hr = context->Map (index_buffer.Get(), 0, D3D11_MAP_WRITE_DISCARD, 0, &map);
378   if (FAILED (hr)) {
379     gst_printerrln ("Couldn't map index buffer");
380     return hr;
381   }
382 
383   WORD *indices = (WORD *) map.pData;
384   indices[0] = 0;
385   indices[1] = 1;
386   indices[2] = 2;
387 
388   indices[3] = 3;
389   indices[4] = 0;
390   indices[5] = 2;
391 
392   context->Unmap (index_buffer.Get(), 0);
393 
394   *sampler = sampler_state.Detach();
395   *ps = pixel_shader.Detach();
396   *vs = vertex_shader.Detach();
397   *layout = input_layout.Detach();
398   *vertex = vertex_buffer.Detach();
399   *index = index_buffer.Detach();
400 
401   return S_OK;
402 }