[PATCH v2 1/5] d3d9: Don't upload unused system memory vertex buffers.

Matteo Bruni mbruni at codeweavers.com
Mon Feb 25 12:00:41 CST 2019


Signed-off-by: Matteo Bruni <mbruni at codeweavers.com>
---
v2: Add a couple of comments.

 dlls/d3d9/d3d9_private.h      |  2 ++
 dlls/d3d9/device.c            | 26 ++++++++++++++++++++++++--
 dlls/d3d9/tests/visual.c      | 23 +++++++++++++++++++++++
 dlls/d3d9/vertexdeclaration.c |  2 +-
 4 files changed, 50 insertions(+), 3 deletions(-)

diff --git a/dlls/d3d9/d3d9_private.h b/dlls/d3d9/d3d9_private.h
index 3fe0376e5c1..50b18b1ed36 100644
--- a/dlls/d3d9/d3d9_private.h
+++ b/dlls/d3d9/d3d9_private.h
@@ -256,6 +256,8 @@ struct d3d9_vertex_declaration
     IDirect3DDevice9Ex *parent_device;
 };
 
+HRESULT convert_to_wined3d_declaration(const D3DVERTEXELEMENT9 *d3d9_elements,
+        struct wined3d_vertex_element **wined3d_elements, UINT *element_count) DECLSPEC_HIDDEN;
 HRESULT d3d9_vertex_declaration_create(struct d3d9_device *device,
         const D3DVERTEXELEMENT9 *elements, struct d3d9_vertex_declaration **declaration) DECLSPEC_HIDDEN;
 struct d3d9_vertex_declaration *unsafe_impl_from_IDirect3DVertexDeclaration9(
diff --git a/dlls/d3d9/device.c b/dlls/d3d9/device.c
index 6cc3f180b09..ef73adcc789 100644
--- a/dlls/d3d9/device.c
+++ b/dlls/d3d9/device.c
@@ -2730,23 +2730,45 @@ static void d3d9_generate_auto_mipmaps(struct d3d9_device *device)
 static void d3d9_device_upload_sysmem_vertex_buffers(struct d3d9_device *device,
         int base_vertex, unsigned int start_vertex, unsigned int vertex_count)
 {
+    struct wined3d_vertex_declaration *wined3d_decl;
     struct wined3d_box box = {0, 0, 0, 1, 0, 1};
+    unsigned int i, offset, stride, map, count;
+    struct wined3d_vertex_element *elements;
     struct d3d9_vertexbuffer *d3d9_buffer;
     struct wined3d_resource *dst_resource;
-    unsigned int i, offset, stride, map;
+    struct d3d9_vertex_declaration *decl;
     struct wined3d_buffer *dst_buffer;
     struct wined3d_resource_desc desc;
     HRESULT hr;
 
     if (!device->sysmem_vb)
         return;
+    wined3d_decl = wined3d_device_get_vertex_declaration(device->wined3d_device);
+    if (!wined3d_decl)
+        return;
 
     if (base_vertex >= 0 || start_vertex >= -base_vertex)
         start_vertex += base_vertex;
     else
         FIXME("System memory vertex data offset is negative.\n");
 
-    map = device->sysmem_vb;
+    /* Make sure to only upload buffers that are used by the current draw:
+     * released buffers might still be bound as stream sources. */
+    decl = wined3d_vertex_declaration_get_parent(wined3d_decl);
+    if (!decl->elements)
+    {
+        map = 1;
+    }
+    else
+    {
+        map = 0;
+        if (FAILED(convert_to_wined3d_declaration(decl->elements, &elements, &count)))
+            return;
+        for (i = 0; i < count; ++i)
+            map |= 1u << elements[i].input_slot;
+        heap_free(elements);
+    }
+    map &= device->sysmem_vb;
     while (map)
     {
         i = wined3d_bit_scan(&map);
diff --git a/dlls/d3d9/tests/visual.c b/dlls/d3d9/tests/visual.c
index 52d67c81991..0e572984784 100644
--- a/dlls/d3d9/tests/visual.c
+++ b/dlls/d3d9/tests/visual.c
@@ -24713,6 +24713,29 @@ static void test_sysmem_draw(void)
     colour = getPixelColor(device, 320, 240);
     ok(color_match(colour, 0x00443322, 1), "Got unexpected colour 0x%08x.\n", colour);
 
+    /* Test that releasing but not unbinding a vertex buffer doesn't break. */
+    hr = IDirect3DDevice9_SetFVF(device, D3DFVF_XYZ | D3DFVF_DIFFUSE);
+    ok(hr == D3D_OK, "Got unexpected hr %#x.\n", hr);
+    hr = IDirect3DDevice9_SetStreamSource(device, 0, vb, 0, sizeof(*quad));
+    ok(hr == D3D_OK, "Got unexpected hr %#x.\n", hr);
+    hr = IDirect3DDevice9_SetIndices(device, ib);
+    ok(hr == D3D_OK, "Got unexpected hr %#x.\n", hr);
+
+    IDirect3DVertexBuffer9_Release(vb_s1);
+
+    hr = IDirect3DDevice9_Clear(device, 0, NULL, D3DCLEAR_TARGET, 0x77777777, 0.0f, 0);
+    ok(hr == D3D_OK, "Got unexpected hr %#x.\n", hr);
+
+    hr = IDirect3DDevice9_BeginScene(device);
+    ok(hr == D3D_OK, "Got unexpected hr %#x.\n", hr);
+    hr = IDirect3DDevice9_DrawIndexedPrimitive(device, D3DPT_TRIANGLESTRIP, 0, 0, 4, 0, 2);
+    ok(hr == D3D_OK, "Got unexpected hr %#x.\n", hr);
+    hr = IDirect3DDevice9_EndScene(device);
+    ok(hr == D3D_OK, "Got unexpected hr %#x.\n", hr);
+
+    colour = getPixelColor(device, 320, 240);
+    ok(color_match(colour, 0x00007f7f, 1), "Got unexpected colour 0x%08x.\n", colour);
+
     hr = IDirect3DDevice9_CreateTexture(device, 2, 2, 1, 0, D3DFMT_A8R8G8B8, D3DPOOL_SYSTEMMEM, &texture, NULL);
     ok(hr == D3D_OK, "Got unexpected hr %#x.\n", hr);
     memset(&lr, 0, sizeof(lr));
diff --git a/dlls/d3d9/vertexdeclaration.c b/dlls/d3d9/vertexdeclaration.c
index b6445255238..5f07207a482 100644
--- a/dlls/d3d9/vertexdeclaration.c
+++ b/dlls/d3d9/vertexdeclaration.c
@@ -320,7 +320,7 @@ static const struct wined3d_parent_ops d3d9_vertexdeclaration_wined3d_parent_ops
     d3d9_vertexdeclaration_wined3d_object_destroyed,
 };
 
-static HRESULT convert_to_wined3d_declaration(const D3DVERTEXELEMENT9 *d3d9_elements,
+HRESULT convert_to_wined3d_declaration(const D3DVERTEXELEMENT9 *d3d9_elements,
         struct wined3d_vertex_element **wined3d_elements, UINT *element_count)
 {
     const D3DVERTEXELEMENT9* element;
-- 
2.19.2




More information about the wine-devel mailing list