d3d11: Avoid ID3D11DeviceContext::Map if possible

Allocate resource with initial date instead of calling Map/Unmap
after allocation

Part-of: <https://gitlab.freedesktop.org/gstreamer/gstreamer/-/merge_requests/5660>
This commit is contained in:
Seungha Yang 2023-11-15 00:38:14 +09:00
parent ac11ccd4ff
commit cb3c1390f2
4 changed files with 161 additions and 295 deletions

View file

@ -768,7 +768,7 @@ gst_d3d11_color_convert_setup_shader (GstD3D11Converter * self,
HRESULT hr; HRESULT hr;
D3D11_BUFFER_DESC buffer_desc; D3D11_BUFFER_DESC buffer_desc;
VertexData vertex_data[4]; VertexData vertex_data[4];
WORD indices[6]; const WORD indices[6] = { 0, 1, 2, 3, 0, 2 };
ID3D11Device *device_handle; ID3D11Device *device_handle;
ComPtr < ID3D11VertexShader > vs; ComPtr < ID3D11VertexShader > vs;
ComPtr < ID3D11InputLayout > layout; ComPtr < ID3D11InputLayout > layout;
@ -920,15 +920,6 @@ gst_d3d11_color_convert_setup_shader (GstD3D11Converter * self,
return FALSE; return FALSE;
} }
/* clockwise indexing */
indices[0] = 0; /* bottom left */
indices[1] = 1; /* top left */
indices[2] = 2; /* top right */
indices[3] = 3; /* bottom right */
indices[4] = 0; /* bottom left */
indices[5] = 2; /* top right */
buffer_desc.Usage = D3D11_USAGE_DYNAMIC; buffer_desc.Usage = D3D11_USAGE_DYNAMIC;
buffer_desc.ByteWidth = sizeof (WORD) * 6; buffer_desc.ByteWidth = sizeof (WORD) * 6;
buffer_desc.BindFlags = D3D11_BIND_INDEX_BUFFER; buffer_desc.BindFlags = D3D11_BIND_INDEX_BUFFER;

View file

@ -1827,12 +1827,11 @@ gst_d3d11_compositor_create_checker_quad (GstD3D11Compositor * self,
const GstVideoInfo * info) const GstVideoInfo * info)
{ {
GstD3D11CompositorQuad *quad = nullptr; GstD3D11CompositorQuad *quad = nullptr;
VertexData *vertex_data; VertexData vertex_data[4];
WORD *indices; const WORD indices[6] = { 0, 1, 2, 3, 0, 2 };
ID3D11Device *device_handle; ID3D11Device *device_handle;
ID3D11DeviceContext *context_handle;
D3D11_MAPPED_SUBRESOURCE map;
D3D11_BUFFER_DESC buffer_desc; D3D11_BUFFER_DESC buffer_desc;
D3D11_SUBRESOURCE_DATA subresource;
ComPtr < ID3D11Buffer > vertex_buffer; ComPtr < ID3D11Buffer > vertex_buffer;
ComPtr < ID3D11Buffer > index_buffer; ComPtr < ID3D11Buffer > index_buffer;
ComPtr < ID3D11PixelShader > ps; ComPtr < ID3D11PixelShader > ps;
@ -1842,7 +1841,6 @@ gst_d3d11_compositor_create_checker_quad (GstD3D11Compositor * self,
HRESULT hr; HRESULT hr;
device_handle = gst_d3d11_device_get_device_handle (self->device); device_handle = gst_d3d11_device_get_device_handle (self->device);
context_handle = gst_d3d11_device_get_device_context_handle (self->device);
if (GST_VIDEO_INFO_IS_RGB (info)) { if (GST_VIDEO_INFO_IS_RGB (info)) {
hr = gst_d3d11_get_pixel_shader_checker_rgb (self->device, &ps); hr = gst_d3d11_get_pixel_shader_checker_rgb (self->device, &ps);
@ -1869,28 +1867,6 @@ gst_d3d11_compositor_create_checker_quad (GstD3D11Compositor * self,
return nullptr; return nullptr;
} }
memset (&buffer_desc, 0, sizeof (D3D11_BUFFER_DESC));
buffer_desc.Usage = D3D11_USAGE_DYNAMIC;
buffer_desc.ByteWidth = sizeof (VertexData) * 4;
buffer_desc.BindFlags = D3D11_BIND_VERTEX_BUFFER;
buffer_desc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
hr = device_handle->CreateBuffer (&buffer_desc, nullptr, &vertex_buffer);
if (!gst_d3d11_result (hr, self->device)) {
GST_ERROR_OBJECT (self,
"Couldn't create vertex buffer, hr: 0x%x", (guint) hr);
return nullptr;
}
hr = context_handle->Map (vertex_buffer.Get (),
0, D3D11_MAP_WRITE_DISCARD, 0, &map);
if (!gst_d3d11_result (hr, self->device)) {
GST_ERROR_OBJECT (self, "Couldn't map vertex buffer, hr: 0x%x", (guint) hr);
return nullptr;
}
vertex_data = (VertexData *) map.pData;
/* bottom left */ /* bottom left */
vertex_data[0].position.x = -1.0f; vertex_data[0].position.x = -1.0f;
vertex_data[0].position.y = -1.0f; vertex_data[0].position.y = -1.0f;
@ -1919,40 +1895,39 @@ gst_d3d11_compositor_create_checker_quad (GstD3D11Compositor * self,
vertex_data[3].texture.u = 1.0f; vertex_data[3].texture.u = 1.0f;
vertex_data[3].texture.v = 1.0f; vertex_data[3].texture.v = 1.0f;
context_handle->Unmap (vertex_buffer.Get (), 0); memset (&subresource, 0, sizeof (D3D11_SUBRESOURCE_DATA));
memset (&buffer_desc, 0, sizeof (D3D11_BUFFER_DESC));
buffer_desc.Usage = D3D11_USAGE_DYNAMIC;
buffer_desc.ByteWidth = sizeof (VertexData) * 4;
buffer_desc.BindFlags = D3D11_BIND_VERTEX_BUFFER;
buffer_desc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
subresource.pSysMem = vertex_data;
subresource.SysMemPitch = sizeof (VertexData) * 4;
hr = device_handle->CreateBuffer (&buffer_desc, &subresource, &vertex_buffer);
if (!gst_d3d11_result (hr, self->device)) {
GST_ERROR_OBJECT (self,
"Couldn't create vertex buffer, hr: 0x%x", (guint) hr);
return nullptr;
}
buffer_desc.Usage = D3D11_USAGE_DYNAMIC; buffer_desc.Usage = D3D11_USAGE_DYNAMIC;
buffer_desc.ByteWidth = sizeof (WORD) * 6; buffer_desc.ByteWidth = sizeof (WORD) * 6;
buffer_desc.BindFlags = D3D11_BIND_INDEX_BUFFER; buffer_desc.BindFlags = D3D11_BIND_INDEX_BUFFER;
buffer_desc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE; buffer_desc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
hr = device_handle->CreateBuffer (&buffer_desc, nullptr, &index_buffer); subresource.pSysMem = indices;
subresource.SysMemPitch = sizeof (WORD) * 6;
hr = device_handle->CreateBuffer (&buffer_desc, &subresource, &index_buffer);
if (!gst_d3d11_result (hr, self->device)) { if (!gst_d3d11_result (hr, self->device)) {
GST_ERROR_OBJECT (self, GST_ERROR_OBJECT (self,
"Couldn't create index buffer, hr: 0x%x", (guint) hr); "Couldn't create index buffer, hr: 0x%x", (guint) hr);
return nullptr; return nullptr;
} }
hr = context_handle->Map (index_buffer.Get (),
0, D3D11_MAP_WRITE_DISCARD, 0, &map);
if (!gst_d3d11_result (hr, self->device)) {
GST_ERROR_OBJECT (self, "Couldn't map index buffer, hr: 0x%x", (guint) hr);
return nullptr;
}
indices = (WORD *) map.pData;
/* clockwise indexing */
indices[0] = 0; /* bottom left */
indices[1] = 1; /* top left */
indices[2] = 2; /* top right */
indices[3] = 3; /* bottom right */
indices[4] = 0; /* bottom left */
indices[5] = 2; /* top right */
context_handle->Unmap (index_buffer.Get (), 0);
quad = g_new0 (GstD3D11CompositorQuad, 1); quad = g_new0 (GstD3D11CompositorQuad, 1);
quad->ps = ps.Detach (); quad->ps = ps.Detach ();
quad->vs = vs.Detach (); quad->vs = vs.Detach ();

View file

@ -126,12 +126,11 @@ gst_d3d11_composition_overlay_new (GstD3D11OverlayCompositor * self,
GstD3D11OverlayCompositorPrivate *priv = self->priv; GstD3D11OverlayCompositorPrivate *priv = self->priv;
gint x, y; gint x, y;
guint width, height; guint width, height;
D3D11_SUBRESOURCE_DATA subresource_data; D3D11_SUBRESOURCE_DATA subresource;
D3D11_TEXTURE2D_DESC texture_desc; D3D11_TEXTURE2D_DESC texture_desc;
D3D11_SHADER_RESOURCE_VIEW_DESC srv_desc; D3D11_SHADER_RESOURCE_VIEW_DESC srv_desc;
D3D11_BUFFER_DESC buffer_desc; D3D11_BUFFER_DESC buffer_desc;
D3D11_MAPPED_SUBRESOURCE map; VertexData vertex_data[4];
VertexData *vertex_data;
GstBuffer *buf; GstBuffer *buf;
GstVideoMeta *vmeta; GstVideoMeta *vmeta;
GstMapInfo info; GstMapInfo info;
@ -139,7 +138,6 @@ gst_d3d11_composition_overlay_new (GstD3D11OverlayCompositor * self,
gint stride; gint stride;
HRESULT hr; HRESULT hr;
ID3D11Device *device_handle; ID3D11Device *device_handle;
ID3D11DeviceContext *context_handle;
GstD3D11Device *device = self->device; GstD3D11Device *device = self->device;
FLOAT x1, y1, x2, y2; FLOAT x1, y1, x2, y2;
gdouble val; gdouble val;
@ -151,13 +149,12 @@ gst_d3d11_composition_overlay_new (GstD3D11OverlayCompositor * self,
GstMemory *mem; GstMemory *mem;
gboolean is_d3d11 = FALSE; gboolean is_d3d11 = FALSE;
memset (&subresource_data, 0, sizeof (subresource_data)); memset (&subresource, 0, sizeof (subresource));
memset (&texture_desc, 0, sizeof (texture_desc)); memset (&texture_desc, 0, sizeof (texture_desc));
memset (&srv_desc, 0, sizeof (srv_desc)); memset (&srv_desc, 0, sizeof (srv_desc));
memset (&buffer_desc, 0, sizeof (buffer_desc)); memset (&buffer_desc, 0, sizeof (buffer_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);
if (!gst_video_overlay_rectangle_get_render_rectangle (overlay_rect, &x, &y, if (!gst_video_overlay_rectangle_get_render_rectangle (overlay_rect, &x, &y,
&width, &height)) { &width, &height)) {
@ -207,9 +204,8 @@ gst_d3d11_composition_overlay_new (GstD3D11OverlayCompositor * self,
} }
/* Do create texture and upload data at once, for create immutable texture */ /* Do create texture and upload data at once, for create immutable texture */
subresource_data.pSysMem = data; subresource.pSysMem = data;
subresource_data.SysMemPitch = stride; subresource.SysMemPitch = stride;
subresource_data.SysMemSlicePitch = 0;
texture_desc.Width = vmeta->width; texture_desc.Width = vmeta->width;
texture_desc.Height = vmeta->height; texture_desc.Height = vmeta->height;
@ -222,8 +218,7 @@ gst_d3d11_composition_overlay_new (GstD3D11OverlayCompositor * self,
texture_desc.BindFlags = D3D11_BIND_SHADER_RESOURCE; texture_desc.BindFlags = D3D11_BIND_SHADER_RESOURCE;
texture_desc.CPUAccessFlags = 0; texture_desc.CPUAccessFlags = 0;
hr = device_handle->CreateTexture2D (&texture_desc, hr = device_handle->CreateTexture2D (&texture_desc, &subresource, &texture);
&subresource_data, &texture);
gst_video_meta_unmap (vmeta, 0, &info); gst_video_meta_unmap (vmeta, 0, &info);
if (!gst_d3d11_result (hr, device)) { if (!gst_d3d11_result (hr, device)) {
@ -242,28 +237,6 @@ gst_d3d11_composition_overlay_new (GstD3D11OverlayCompositor * self,
} }
} }
buffer_desc.Usage = D3D11_USAGE_DYNAMIC;
buffer_desc.ByteWidth = sizeof (VertexData) * 4;
buffer_desc.BindFlags = D3D11_BIND_VERTEX_BUFFER;
buffer_desc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
hr = device_handle->CreateBuffer (&buffer_desc, nullptr, &vertex_buffer);
if (!gst_d3d11_result (hr, device)) {
GST_ERROR_OBJECT (self,
"Couldn't create vertex buffer, hr: 0x%x", (guint) hr);
return nullptr;
}
GstD3D11DeviceLockGuard lk (device);
hr = context_handle->Map (vertex_buffer.Get (),
0, D3D11_MAP_WRITE_DISCARD, 0, &map);
if (!gst_d3d11_result (hr, device)) {
GST_ERROR_OBJECT (self, "Couldn't map vertex buffer, hr: 0x%x", (guint) hr);
return nullptr;
}
vertex_data = (VertexData *) map.pData;
/* bottom left */ /* bottom left */
gst_util_fraction_to_double (x, GST_VIDEO_INFO_WIDTH (&priv->info), &val); gst_util_fraction_to_double (x, GST_VIDEO_INFO_WIDTH (&priv->info), &val);
x1 = (val * 2.0f) - 1.0f; x1 = (val * 2.0f) - 1.0f;
@ -308,7 +281,20 @@ gst_d3d11_composition_overlay_new (GstD3D11OverlayCompositor * self,
vertex_data[3].texture.u = 1.0f; vertex_data[3].texture.u = 1.0f;
vertex_data[3].texture.v = 1.0f; vertex_data[3].texture.v = 1.0f;
context_handle->Unmap (vertex_buffer.Get (), 0); buffer_desc.Usage = D3D11_USAGE_DYNAMIC;
buffer_desc.ByteWidth = sizeof (VertexData) * 4;
buffer_desc.BindFlags = D3D11_BIND_VERTEX_BUFFER;
buffer_desc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
subresource.pSysMem = vertex_data;
subresource.SysMemPitch = sizeof (VertexData) * 4;
hr = device_handle->CreateBuffer (&buffer_desc, &subresource, &vertex_buffer);
if (!gst_d3d11_result (hr, device)) {
GST_ERROR_OBJECT (self,
"Couldn't create vertex buffer, hr: 0x%x", (guint) hr);
return nullptr;
}
auto overlay = std::make_shared < GstD3D11CompositionOverlay > (); auto overlay = std::make_shared < GstD3D11CompositionOverlay > ();
overlay->overlay_rect = gst_video_overlay_rectangle_ref (overlay_rect); overlay->overlay_rect = gst_video_overlay_rectangle_ref (overlay_rect);
@ -331,10 +317,9 @@ gst_d3d11_overlay_compositor_setup_shader (GstD3D11OverlayCompositor * self)
HRESULT hr; HRESULT hr;
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_SUBRESOURCE_DATA subresource;
WORD *indices; const WORD indices[6] = { 0, 1, 2, 3, 0, 2 };
ID3D11Device *device_handle; ID3D11Device *device_handle;
ID3D11DeviceContext *context_handle;
ComPtr < ID3D11PixelShader > ps; ComPtr < ID3D11PixelShader > ps;
ComPtr < ID3D11PixelShader > premul_ps; ComPtr < ID3D11PixelShader > premul_ps;
ComPtr < ID3D11VertexShader > vs; ComPtr < ID3D11VertexShader > vs;
@ -346,9 +331,9 @@ gst_d3d11_overlay_compositor_setup_shader (GstD3D11OverlayCompositor * self)
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));
memset (&subresource, 0, sizeof (subresource));
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);
hr = gst_d3d11_device_get_sampler (device, hr = gst_d3d11_device_get_sampler (device,
D3D11_FILTER_MIN_MAG_LINEAR_MIP_POINT, &sampler); D3D11_FILTER_MIN_MAG_LINEAR_MIP_POINT, &sampler);
@ -406,35 +391,16 @@ gst_d3d11_overlay_compositor_setup_shader (GstD3D11OverlayCompositor * self)
buffer_desc.BindFlags = D3D11_BIND_INDEX_BUFFER; buffer_desc.BindFlags = D3D11_BIND_INDEX_BUFFER;
buffer_desc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE; buffer_desc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
hr = device_handle->CreateBuffer (&buffer_desc, nullptr, &index_buffer); subresource.pSysMem = indices;
subresource.SysMemPitch = sizeof (WORD) * 6;
hr = device_handle->CreateBuffer (&buffer_desc, &subresource, &index_buffer);
if (!gst_d3d11_result (hr, device)) { if (!gst_d3d11_result (hr, device)) {
GST_ERROR_OBJECT (self, GST_ERROR_OBJECT (self,
"Couldn't create index buffer, hr: 0x%x", (guint) hr); "Couldn't create index buffer, hr: 0x%x", (guint) hr);
return FALSE; return FALSE;
} }
GstD3D11DeviceLockGuard lk (device);
hr = context_handle->Map (index_buffer.Get (),
0, D3D11_MAP_WRITE_DISCARD, 0, &map);
if (!gst_d3d11_result (hr, device)) {
GST_ERROR_OBJECT (self, "Couldn't map index buffer, hr: 0x%x", (guint) hr);
return FALSE;
}
indices = (WORD *) map.pData;
/* clockwise indexing */
indices[0] = 0; /* bottom left */
indices[1] = 1; /* top left */
indices[2] = 2; /* top right */
indices[3] = 3; /* bottom right */
indices[4] = 0; /* bottom left */
indices[5] = 2; /* top right */
context_handle->Unmap (index_buffer.Get (), 0);
priv->ps = ps; priv->ps = ps;
priv->premul_ps = premul_ps; priv->premul_ps = premul_ps;
priv->vs = vs; priv->vs = vs;

View file

@ -302,13 +302,11 @@ setup_snow_render (GstD3D11TestSrc * self, GstD3D11TestSrcRender * render,
{ {
HRESULT hr; HRESULT hr;
D3D11_BUFFER_DESC buffer_desc; D3D11_BUFFER_DESC buffer_desc;
D3D11_MAPPED_SUBRESOURCE map; D3D11_SUBRESOURCE_DATA subresource;
UvVertexData *vertex_data; UvVertexData vertex_data[4];
WORD *indices; const WORD indices[6] = { 0, 1, 2, 3, 0, 2 };
ID3D11Device *device_handle = ID3D11Device *device_handle =
gst_d3d11_device_get_device_handle (self->device); gst_d3d11_device_get_device_handle (self->device);
ID3D11DeviceContext *context_handle =
gst_d3d11_device_get_device_context_handle (self->device);
ComPtr < ID3D11PixelShader > ps; ComPtr < ID3D11PixelShader > ps;
ComPtr < ID3D11VertexShader > vs; ComPtr < ID3D11VertexShader > vs;
ComPtr < ID3D11InputLayout > layout; ComPtr < ID3D11InputLayout > layout;
@ -317,6 +315,7 @@ setup_snow_render (GstD3D11TestSrc * self, GstD3D11TestSrcRender * render,
ComPtr < ID3D11Buffer > const_buffer; ComPtr < ID3D11Buffer > const_buffer;
memset (&buffer_desc, 0, sizeof (buffer_desc)); memset (&buffer_desc, 0, sizeof (buffer_desc));
memset (&subresource, 0, sizeof (subresource));
hr = gst_d3d11_get_vertex_shader_coord (self->device, &vs, &layout); hr = gst_d3d11_get_vertex_shader_coord (self->device, &vs, &layout);
if (!gst_d3d11_result (hr, self->device)) { if (!gst_d3d11_result (hr, self->device)) {
@ -330,56 +329,6 @@ setup_snow_render (GstD3D11TestSrc * self, GstD3D11TestSrcRender * render,
return FALSE; return FALSE;
} }
buffer_desc.Usage = D3D11_USAGE_DYNAMIC;
buffer_desc.ByteWidth = sizeof (UvVertexData) * 4;
buffer_desc.BindFlags = D3D11_BIND_VERTEX_BUFFER;
buffer_desc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
hr = device_handle->CreateBuffer (&buffer_desc, nullptr, &vertex_buffer);
if (!gst_d3d11_result (hr, self->device)) {
GST_ERROR_OBJECT (self, "Failed to create vertex buffer");
return FALSE;
}
buffer_desc.Usage = D3D11_USAGE_DYNAMIC;
buffer_desc.ByteWidth = sizeof (WORD) * 6;
buffer_desc.BindFlags = D3D11_BIND_INDEX_BUFFER;
buffer_desc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
hr = device_handle->CreateBuffer (&buffer_desc, nullptr, &index_buffer);
if (!gst_d3d11_result (hr, self->device)) {
GST_ERROR_OBJECT (self, "Failed to create index buffer");
return FALSE;
}
buffer_desc.Usage = D3D11_USAGE_DYNAMIC;
buffer_desc.ByteWidth = sizeof (SnowConstBuffer);
buffer_desc.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
buffer_desc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
hr = device_handle->CreateBuffer (&buffer_desc, nullptr, &const_buffer);
if (!gst_d3d11_result (hr, self->device)) {
GST_ERROR_OBJECT (self, "Failed to create constant buffer");
return FALSE;
}
GstD3D11DeviceLockGuard lk (self->device);
hr = context_handle->Map (vertex_buffer.Get (), 0, D3D11_MAP_WRITE_DISCARD, 0,
&map);
if (!gst_d3d11_result (hr, self->device)) {
GST_ERROR_OBJECT (self, "Failed to map vertex buffer");
return FALSE;
}
vertex_data = (UvVertexData *) map.pData;
hr = context_handle->Map (index_buffer.Get (), 0, D3D11_MAP_WRITE_DISCARD, 0,
&map);
if (!gst_d3d11_result (hr, self->device)) {
GST_ERROR_OBJECT (self, "Failed to map index buffer");
context_handle->Unmap (vertex_buffer.Get (), 0);
return FALSE;
}
indices = (WORD *) map.pData;
if (on_smpte) { if (on_smpte) {
FLOAT left, right, top, bottom; FLOAT left, right, top, bottom;
FLOAT left_u, right_u, top_v, bottom_v; FLOAT left_u, right_u, top_v, bottom_v;
@ -450,17 +399,43 @@ setup_snow_render (GstD3D11TestSrc * self, GstD3D11TestSrcRender * render,
vertex_data[3].texture.v = 1.0f; vertex_data[3].texture.v = 1.0f;
} }
/* clockwise indexing */ buffer_desc.Usage = D3D11_USAGE_DYNAMIC;
indices[0] = 0; /* bottom left */ buffer_desc.ByteWidth = sizeof (UvVertexData) * 4;
indices[1] = 1; /* top left */ buffer_desc.BindFlags = D3D11_BIND_VERTEX_BUFFER;
indices[2] = 2; /* top right */ buffer_desc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
indices[3] = 3; /* bottom right */ subresource.pSysMem = vertex_data;
indices[4] = 0; /* bottom left */ subresource.SysMemPitch = sizeof (UvVertexData) * 4;
indices[5] = 2; /* top right */
context_handle->Unmap (vertex_buffer.Get (), 0); hr = device_handle->CreateBuffer (&buffer_desc, &subresource, &vertex_buffer);
context_handle->Unmap (index_buffer.Get (), 0); if (!gst_d3d11_result (hr, self->device)) {
GST_ERROR_OBJECT (self, "Failed to create vertex buffer");
return FALSE;
}
buffer_desc.Usage = D3D11_USAGE_DYNAMIC;
buffer_desc.ByteWidth = sizeof (WORD) * 6;
buffer_desc.BindFlags = D3D11_BIND_INDEX_BUFFER;
buffer_desc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
subresource.pSysMem = indices;
subresource.SysMemPitch = sizeof (WORD) * 6;
hr = device_handle->CreateBuffer (&buffer_desc, &subresource, &index_buffer);
if (!gst_d3d11_result (hr, self->device)) {
GST_ERROR_OBJECT (self, "Failed to create index buffer");
return FALSE;
}
buffer_desc.Usage = D3D11_USAGE_DYNAMIC;
buffer_desc.ByteWidth = sizeof (SnowConstBuffer);
buffer_desc.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
buffer_desc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
hr = device_handle->CreateBuffer (&buffer_desc, nullptr, &const_buffer);
if (!gst_d3d11_result (hr, self->device)) {
GST_ERROR_OBJECT (self, "Failed to create constant buffer");
return FALSE;
}
auto quad = std::make_shared < GstD3D11TestSrcQuad > (); auto quad = std::make_shared < GstD3D11TestSrcQuad > ();
@ -486,13 +461,11 @@ setup_smpte_render (GstD3D11TestSrc * self, GstD3D11TestSrcRender * render)
{ {
HRESULT hr; HRESULT hr;
D3D11_BUFFER_DESC buffer_desc; D3D11_BUFFER_DESC buffer_desc;
D3D11_MAPPED_SUBRESOURCE map; D3D11_SUBRESOURCE_DATA subresource;
ColorVertexData *vertex_data; ColorVertexData vertex_data[4 * 20];
WORD *indices; WORD indices[6 * 20];
ID3D11Device *device_handle = ID3D11Device *device_handle =
gst_d3d11_device_get_device_handle (self->device); gst_d3d11_device_get_device_handle (self->device);
ID3D11DeviceContext *context_handle =
gst_d3d11_device_get_device_context_handle (self->device);
ComPtr < ID3D11PixelShader > ps; ComPtr < ID3D11PixelShader > ps;
ComPtr < ID3D11VertexShader > vs; ComPtr < ID3D11VertexShader > vs;
ComPtr < ID3D11InputLayout > layout; ComPtr < ID3D11InputLayout > layout;
@ -502,6 +475,7 @@ setup_smpte_render (GstD3D11TestSrc * self, GstD3D11TestSrcRender * render)
guint num_index = 0; guint num_index = 0;
memset (&buffer_desc, 0, sizeof (buffer_desc)); memset (&buffer_desc, 0, sizeof (buffer_desc));
memset (&subresource, 0, sizeof (subresource));
hr = gst_d3d11_get_vertex_shader_color (self->device, &vs, &layout); hr = gst_d3d11_get_vertex_shader_color (self->device, &vs, &layout);
if (!gst_d3d11_result (hr, self->device)) { if (!gst_d3d11_result (hr, self->device)) {
@ -515,46 +489,6 @@ setup_smpte_render (GstD3D11TestSrc * self, GstD3D11TestSrcRender * render)
return FALSE; return FALSE;
} }
buffer_desc.Usage = D3D11_USAGE_DYNAMIC;
buffer_desc.ByteWidth = sizeof (ColorVertexData) * 4 * 20;
buffer_desc.BindFlags = D3D11_BIND_VERTEX_BUFFER;
buffer_desc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
hr = device_handle->CreateBuffer (&buffer_desc, nullptr, &vertex_buffer);
if (!gst_d3d11_result (hr, self->device)) {
GST_ERROR_OBJECT (self, "Failed to create vertex buffer");
return FALSE;
}
buffer_desc.Usage = D3D11_USAGE_DYNAMIC;
buffer_desc.ByteWidth = sizeof (WORD) * 6 * 20;
buffer_desc.BindFlags = D3D11_BIND_INDEX_BUFFER;
buffer_desc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
hr = device_handle->CreateBuffer (&buffer_desc, nullptr, &index_buffer);
if (!gst_d3d11_result (hr, self->device)) {
GST_ERROR_OBJECT (self, "Failed to create index buffer");
return FALSE;
}
GstD3D11DeviceLockGuard lk (self->device);
hr = context_handle->Map (vertex_buffer.Get (), 0, D3D11_MAP_WRITE_DISCARD, 0,
&map);
if (!gst_d3d11_result (hr, self->device)) {
GST_ERROR_OBJECT (self, "Failed to map vertex buffer");
return FALSE;
}
vertex_data = (ColorVertexData *) map.pData;
hr = context_handle->Map (index_buffer.Get (), 0, D3D11_MAP_WRITE_DISCARD, 0,
&map);
if (!gst_d3d11_result (hr, self->device)) {
GST_ERROR_OBJECT (self, "Failed to map index buffer");
context_handle->Unmap (vertex_buffer.Get (), 0);
return FALSE;
}
indices = (WORD *) map.pData;
/* top row */ /* top row */
for (guint i = 0; i < 7; i++) { for (guint i = 0; i < 7; i++) {
FLOAT left, right, top, bottom; FLOAT left, right, top, bottom;
@ -792,8 +726,33 @@ setup_smpte_render (GstD3D11TestSrc * self, GstD3D11TestSrcRender * render)
indices[idx_base + 5] = base + 2; /* top right */ indices[idx_base + 5] = base + 2; /* top right */
} }
context_handle->Unmap (vertex_buffer.Get (), 0); buffer_desc.Usage = D3D11_USAGE_DYNAMIC;
context_handle->Unmap (index_buffer.Get (), 0); buffer_desc.ByteWidth = sizeof (ColorVertexData) * 4 * 20;
buffer_desc.BindFlags = D3D11_BIND_VERTEX_BUFFER;
buffer_desc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
subresource.pSysMem = vertex_data;
subresource.SysMemPitch = sizeof (ColorVertexData) * 4 * 20;
hr = device_handle->CreateBuffer (&buffer_desc, &subresource, &vertex_buffer);
if (!gst_d3d11_result (hr, self->device)) {
GST_ERROR_OBJECT (self, "Failed to create vertex buffer");
return FALSE;
}
buffer_desc.Usage = D3D11_USAGE_DYNAMIC;
buffer_desc.ByteWidth = sizeof (WORD) * 6 * 20;
buffer_desc.BindFlags = D3D11_BIND_INDEX_BUFFER;
buffer_desc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
subresource.pSysMem = indices;
subresource.SysMemPitch = sizeof (WORD) * 6 * 20;
hr = device_handle->CreateBuffer (&buffer_desc, &subresource, &index_buffer);
if (!gst_d3d11_result (hr, self->device)) {
GST_ERROR_OBJECT (self, "Failed to create index buffer");
return FALSE;
}
auto quad = std::make_shared < GstD3D11TestSrcQuad > (); auto quad = std::make_shared < GstD3D11TestSrcQuad > ();
@ -816,13 +775,11 @@ setup_checker_render (GstD3D11TestSrc * self, GstD3D11TestSrcRender * render,
{ {
HRESULT hr; HRESULT hr;
D3D11_BUFFER_DESC buffer_desc; D3D11_BUFFER_DESC buffer_desc;
D3D11_MAPPED_SUBRESOURCE map; D3D11_SUBRESOURCE_DATA subresource;
UvVertexData *vertex_data; UvVertexData vertex_data[4];
WORD *indices; const WORD indices[6] = { 0, 1, 2, 3, 0, 2 };
ID3D11Device *device_handle = ID3D11Device *device_handle =
gst_d3d11_device_get_device_handle (self->device); gst_d3d11_device_get_device_handle (self->device);
ID3D11DeviceContext *context_handle =
gst_d3d11_device_get_device_context_handle (self->device);
ComPtr < ID3D11PixelShader > ps; ComPtr < ID3D11PixelShader > ps;
ComPtr < ID3D11VertexShader > vs; ComPtr < ID3D11VertexShader > vs;
ComPtr < ID3D11InputLayout > layout; ComPtr < ID3D11InputLayout > layout;
@ -831,6 +788,7 @@ setup_checker_render (GstD3D11TestSrc * self, GstD3D11TestSrcRender * render,
ComPtr < ID3D11Buffer > const_buffer; ComPtr < ID3D11Buffer > const_buffer;
memset (&buffer_desc, 0, sizeof (buffer_desc)); memset (&buffer_desc, 0, sizeof (buffer_desc));
memset (&subresource, 0, sizeof (subresource));
hr = gst_d3d11_get_vertex_shader_coord (self->device, &vs, &layout); hr = gst_d3d11_get_vertex_shader_coord (self->device, &vs, &layout);
if (!gst_d3d11_result (hr, self->device)) { if (!gst_d3d11_result (hr, self->device)) {
@ -844,56 +802,6 @@ setup_checker_render (GstD3D11TestSrc * self, GstD3D11TestSrcRender * render,
return FALSE; return FALSE;
} }
buffer_desc.Usage = D3D11_USAGE_DYNAMIC;
buffer_desc.ByteWidth = sizeof (UvVertexData) * 4;
buffer_desc.BindFlags = D3D11_BIND_VERTEX_BUFFER;
buffer_desc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
hr = device_handle->CreateBuffer (&buffer_desc, nullptr, &vertex_buffer);
if (!gst_d3d11_result (hr, self->device)) {
GST_ERROR_OBJECT (self, "Failed to create vertex buffer");
return FALSE;
}
buffer_desc.Usage = D3D11_USAGE_DYNAMIC;
buffer_desc.ByteWidth = sizeof (WORD) * 6;
buffer_desc.BindFlags = D3D11_BIND_INDEX_BUFFER;
buffer_desc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
hr = device_handle->CreateBuffer (&buffer_desc, nullptr, &index_buffer);
if (!gst_d3d11_result (hr, self->device)) {
GST_ERROR_OBJECT (self, "Failed to create index buffer");
return FALSE;
}
buffer_desc.Usage = D3D11_USAGE_DYNAMIC;
buffer_desc.ByteWidth = sizeof (CheckerConstBuffer);
buffer_desc.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
buffer_desc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
hr = device_handle->CreateBuffer (&buffer_desc, nullptr, &const_buffer);
if (!gst_d3d11_result (hr, self->device)) {
GST_ERROR_OBJECT (self, "Failed to create constant buffer");
return FALSE;
}
GstD3D11DeviceLockGuard lk (self->device);
hr = context_handle->Map (vertex_buffer.Get (), 0, D3D11_MAP_WRITE_DISCARD, 0,
&map);
if (!gst_d3d11_result (hr, self->device)) {
GST_ERROR_OBJECT (self, "Failed to map vertex buffer");
return FALSE;
}
vertex_data = (UvVertexData *) map.pData;
hr = context_handle->Map (index_buffer.Get (), 0, D3D11_MAP_WRITE_DISCARD, 0,
&map);
if (!gst_d3d11_result (hr, self->device)) {
GST_ERROR_OBJECT (self, "Failed to map index buffer");
context_handle->Unmap (vertex_buffer.Get (), 0);
return FALSE;
}
indices = (WORD *) map.pData;
/* bottom left */ /* bottom left */
vertex_data[0].position.x = -1.0f; vertex_data[0].position.x = -1.0f;
vertex_data[0].position.y = -1.0f; vertex_data[0].position.y = -1.0f;
@ -921,17 +829,43 @@ setup_checker_render (GstD3D11TestSrc * self, GstD3D11TestSrcRender * render,
vertex_data[3].texture.u = 1.0f; vertex_data[3].texture.u = 1.0f;
vertex_data[3].texture.v = 1.0f; vertex_data[3].texture.v = 1.0f;
/* clockwise indexing */ buffer_desc.Usage = D3D11_USAGE_DYNAMIC;
indices[0] = 0; /* bottom left */ buffer_desc.ByteWidth = sizeof (UvVertexData) * 4;
indices[1] = 1; /* top left */ buffer_desc.BindFlags = D3D11_BIND_VERTEX_BUFFER;
indices[2] = 2; /* top right */ buffer_desc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
indices[3] = 3; /* bottom right */ subresource.pSysMem = vertex_data;
indices[4] = 0; /* bottom left */ subresource.SysMemPitch = sizeof (UvVertexData) * 4;
indices[5] = 2; /* top right */
context_handle->Unmap (vertex_buffer.Get (), 0); hr = device_handle->CreateBuffer (&buffer_desc, &subresource, &vertex_buffer);
context_handle->Unmap (index_buffer.Get (), 0); if (!gst_d3d11_result (hr, self->device)) {
GST_ERROR_OBJECT (self, "Failed to create vertex buffer");
return FALSE;
}
buffer_desc.Usage = D3D11_USAGE_DYNAMIC;
buffer_desc.ByteWidth = sizeof (WORD) * 6;
buffer_desc.BindFlags = D3D11_BIND_INDEX_BUFFER;
buffer_desc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
subresource.pSysMem = indices;
subresource.SysMemPitch = sizeof (WORD) * 6;
hr = device_handle->CreateBuffer (&buffer_desc, &subresource, &index_buffer);
if (!gst_d3d11_result (hr, self->device)) {
GST_ERROR_OBJECT (self, "Failed to create index buffer");
return FALSE;
}
buffer_desc.Usage = D3D11_USAGE_DYNAMIC;
buffer_desc.ByteWidth = sizeof (CheckerConstBuffer);
buffer_desc.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
buffer_desc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
hr = device_handle->CreateBuffer (&buffer_desc, nullptr, &const_buffer);
if (!gst_d3d11_result (hr, self->device)) {
GST_ERROR_OBJECT (self, "Failed to create constant buffer");
return FALSE;
}
auto quad = std::make_shared < GstD3D11TestSrcQuad > (); auto quad = std::make_shared < GstD3D11TestSrcQuad > ();