From: Matteo Bruni <mbruni(a)codeweavers.com>
Signed-off-by: Matteo Bruni <mbruni(a)codeweavers.com>
Signed-off-by: Henri Verbeet <hverbeet(a)codeweavers.com>
---
dlls/d3d9/buffer.c | 24 ++++++-
dlls/d3d9/d3d9_private.h | 4 +-
dlls/d3d9/device.c | 44 +++++++++++--
dlls/d3d9/stateblock.c | 3 +
dlls/d3d9/tests/visual.c | 167 +++++++++++++++++++++++++++++++++++++++++++++++
5 files changed, 234 insertions(+), 8 deletions(-)
diff --git a/dlls/d3d9/buffer.c b/dlls/d3d9/buffer.c
index 15b7e2bdbce..1533a8496d2 100644
--- a/dlls/d3d9/buffer.c
+++ b/dlls/d3d9/buffer.c
@@ -384,6 +384,8 @@ static ULONG WINAPI d3d9_indexbuffer_AddRef(IDirect3DIndexBuffer9 *iface)
IDirect3DDevice9Ex_AddRef(buffer->parent_device);
wined3d_mutex_lock();
wined3d_buffer_incref(buffer->wined3d_buffer);
+ if (buffer->draw_buffer)
+ wined3d_buffer_incref(buffer->draw_buffer);
wined3d_mutex_unlock();
}
@@ -403,6 +405,8 @@ static ULONG WINAPI d3d9_indexbuffer_Release(IDirect3DIndexBuffer9 *iface)
wined3d_mutex_lock();
wined3d_buffer_decref(buffer->wined3d_buffer);
+ if (buffer->draw_buffer)
+ wined3d_buffer_decref(buffer->draw_buffer);
wined3d_mutex_unlock();
/* Release the device last, as it may cause the device to be destroyed. */
@@ -597,6 +601,7 @@ static const struct wined3d_parent_ops d3d9_indexbuffer_wined3d_parent_ops =
HRESULT indexbuffer_init(struct d3d9_indexbuffer *buffer, struct d3d9_device *device,
UINT size, DWORD usage, D3DFORMAT format, D3DPOOL pool)
{
+ const struct wined3d_parent_ops *parent_ops = &d3d9_null_wined3d_parent_ops;
struct wined3d_buffer_desc desc;
HRESULT hr;
@@ -615,19 +620,32 @@ HRESULT indexbuffer_init(struct d3d9_indexbuffer *buffer, struct d3d9_device *de
desc.byte_width = size;
desc.usage = (usage & WINED3DUSAGE_MASK) | WINED3DUSAGE_STATICDECL;
- desc.bind_flags = WINED3D_BIND_INDEX_BUFFER;
+ desc.bind_flags = 0;
desc.access = wined3daccess_from_d3dpool(pool, usage)
| WINED3D_RESOURCE_ACCESS_MAP_R | WINED3D_RESOURCE_ACCESS_MAP_W;
desc.misc_flags = 0;
desc.structure_byte_stride = 0;
+ if (desc.access & WINED3D_RESOURCE_ACCESS_GPU)
+ {
+ desc.bind_flags = WINED3D_BIND_INDEX_BUFFER;
+ parent_ops = &d3d9_indexbuffer_wined3d_parent_ops;
+ }
+
buffer->IDirect3DIndexBuffer9_iface.lpVtbl = &d3d9_indexbuffer_vtbl;
buffer->format = wined3dformat_from_d3dformat(format);
d3d9_resource_init(&buffer->resource);
wined3d_mutex_lock();
- hr = wined3d_buffer_create(device->wined3d_device, &desc, NULL, buffer,
- &d3d9_indexbuffer_wined3d_parent_ops, &buffer->wined3d_buffer);
+ hr = wined3d_buffer_create(device->wined3d_device, &desc, NULL, buffer, parent_ops, &buffer->wined3d_buffer);
+ if (SUCCEEDED(hr) && !(desc.access & WINED3D_RESOURCE_ACCESS_GPU))
+ {
+ desc.bind_flags = WINED3D_BIND_INDEX_BUFFER;
+ desc.access = WINED3D_RESOURCE_ACCESS_GPU;
+ if (FAILED(hr = wined3d_buffer_create(device->wined3d_device, &desc, NULL, buffer,
+ &d3d9_indexbuffer_wined3d_parent_ops, &buffer->draw_buffer)))
+ wined3d_buffer_decref(buffer->wined3d_buffer);
+ }
wined3d_mutex_unlock();
if (FAILED(hr))
{
diff --git a/dlls/d3d9/d3d9_private.h b/dlls/d3d9/d3d9_private.h
index bd6409ffaf5..a4dc3abb7a6 100644
--- a/dlls/d3d9/d3d9_private.h
+++ b/dlls/d3d9/d3d9_private.h
@@ -104,11 +104,12 @@ struct d3d9_device
LONG device_state;
DWORD sysmem_vb : 16; /* D3D9_MAX_STREAMS */
+ DWORD sysmem_ib : 1;
DWORD in_destruction : 1;
DWORD in_scene : 1;
DWORD has_vertex_declaration : 1;
DWORD recording : 1;
- DWORD padding : 12;
+ DWORD padding : 11;
unsigned int max_user_clip_planes;
@@ -199,6 +200,7 @@ struct d3d9_indexbuffer
struct d3d9_resource resource;
struct wined3d_buffer *wined3d_buffer;
IDirect3DDevice9Ex *parent_device;
+ struct wined3d_buffer *draw_buffer;
enum wined3d_format_id format;
};
diff --git a/dlls/d3d9/device.c b/dlls/d3d9/device.c
index ba84db9338f..6844ad62e9f 100644
--- a/dlls/d3d9/device.c
+++ b/dlls/d3d9/device.c
@@ -2718,6 +2718,31 @@ static void d3d9_device_upload_sysmem_vertex_buffers(struct d3d9_device *device,
}
}
+static void d3d9_device_upload_sysmem_index_buffer(struct d3d9_device *device,
+ unsigned int start_idx, unsigned int idx_count)
+{
+ struct wined3d_box box = {0, 0, 0, 1, 0, 1};
+ struct d3d9_vertexbuffer *d3d9_buffer;
+ struct wined3d_buffer *dst_buffer;
+ enum wined3d_format_id format;
+ unsigned int offset, idx_size;
+ HRESULT hr;
+
+ if (!device->sysmem_ib)
+ return;
+
+ if (!(dst_buffer = wined3d_device_get_index_buffer(device->wined3d_device, &format, &offset)))
+ ERR("Failed to get index buffer.\n");
+ d3d9_buffer = wined3d_buffer_get_parent(dst_buffer);
+ idx_size = format == WINED3DFMT_R16_UINT ? 2 : 4;
+ box.left = offset + start_idx * idx_size;
+ box.right = box.left + idx_count * idx_size;
+ if (FAILED(hr = wined3d_device_copy_sub_resource_region(device->wined3d_device,
+ wined3d_buffer_get_resource(dst_buffer), 0, box.left, 0, 0,
+ wined3d_buffer_get_resource(d3d9_buffer->wined3d_buffer), 0, &box, 0)))
+ ERR("Failed to update buffer.\n");
+}
+
static HRESULT WINAPI d3d9_device_DrawPrimitive(IDirect3DDevice9Ex *iface,
D3DPRIMITIVETYPE primitive_type, UINT start_vertex, UINT primitive_count)
{
@@ -2752,6 +2777,7 @@ static HRESULT WINAPI d3d9_device_DrawIndexedPrimitive(IDirect3DDevice9Ex *iface
UINT vertex_count, UINT start_idx, UINT primitive_count)
{
struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
+ unsigned int index_count;
HRESULT hr;
TRACE("iface %p, primitive_type %#x, base_vertex_idx %u, min_vertex_idx %u, "
@@ -2766,12 +2792,13 @@ static HRESULT WINAPI d3d9_device_DrawIndexedPrimitive(IDirect3DDevice9Ex *iface
WARN("Called without a valid vertex declaration set.\n");
return D3DERR_INVALIDCALL;
}
+ index_count = vertex_count_from_primitive_count(primitive_type, primitive_count);
d3d9_device_upload_sysmem_vertex_buffers(device, min_vertex_idx, vertex_count);
+ d3d9_device_upload_sysmem_index_buffer(device, start_idx, index_count);
d3d9_generate_auto_mipmaps(device);
wined3d_device_set_base_vertex_index(device->wined3d_device, base_vertex_idx);
wined3d_device_set_primitive_type(device->wined3d_device, primitive_type, 0);
- hr = wined3d_device_draw_indexed_primitive(device->wined3d_device, start_idx,
- vertex_count_from_primitive_count(primitive_type, primitive_count));
+ hr = wined3d_device_draw_indexed_primitive(device->wined3d_device, start_idx, index_count);
if (SUCCEEDED(hr))
d3d9_rts_flag_auto_gen_mipmap(device);
wined3d_mutex_unlock();
@@ -3512,12 +3539,21 @@ static HRESULT WINAPI d3d9_device_SetIndices(IDirect3DDevice9Ex *iface, IDirect3
{
struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
struct d3d9_indexbuffer *ib = unsafe_impl_from_IDirect3DIndexBuffer9(buffer);
+ struct wined3d_buffer *wined3d_buffer;
TRACE("iface %p, buffer %p.\n", iface, buffer);
+ if (!ib)
+ wined3d_buffer = NULL;
+ else if (ib->draw_buffer)
+ wined3d_buffer = ib->draw_buffer;
+ else
+ wined3d_buffer = ib->wined3d_buffer;
+
wined3d_mutex_lock();
- wined3d_device_set_index_buffer(device->wined3d_device,
- ib ? ib->wined3d_buffer : NULL, ib ? ib->format : WINED3DFMT_UNKNOWN, 0);
+ wined3d_device_set_index_buffer(device->wined3d_device, wined3d_buffer, ib ? ib->format : WINED3DFMT_UNKNOWN, 0);
+ if (!device->recording)
+ device->sysmem_ib = ib && ib->draw_buffer;
wined3d_mutex_unlock();
return D3D_OK;
diff --git a/dlls/d3d9/stateblock.c b/dlls/d3d9/stateblock.c
index 440df0c1e1a..6f39488fc11 100644
--- a/dlls/d3d9/stateblock.c
+++ b/dlls/d3d9/stateblock.c
@@ -111,6 +111,7 @@ static HRESULT WINAPI d3d9_stateblock_Apply(IDirect3DStateBlock9 *iface)
struct wined3d_buffer *wined3d_buffer;
struct d3d9_vertexbuffer *buffer;
unsigned int i, offset, stride;
+ enum wined3d_format_id format;
struct d3d9_device *device;
HRESULT hr;
@@ -130,6 +131,8 @@ static HRESULT WINAPI d3d9_stateblock_Apply(IDirect3DStateBlock9 *iface)
if (buffer->draw_buffer)
device->sysmem_vb |= 1u << i;
}
+ device->sysmem_ib = (wined3d_buffer = wined3d_device_get_index_buffer(device->wined3d_device, &format, &offset))
+ && (buffer = wined3d_buffer_get_parent(wined3d_buffer)) && buffer->draw_buffer;
wined3d_mutex_unlock();
return D3D_OK;
diff --git a/dlls/d3d9/tests/visual.c b/dlls/d3d9/tests/visual.c
index 4f26b0d23f9..392369a3f79 100644
--- a/dlls/d3d9/tests/visual.c
+++ b/dlls/d3d9/tests/visual.c
@@ -24197,6 +24197,172 @@ static void test_color_vertex(void)
DestroyWindow(window);
}
+static void test_sysmem_draw(void)
+{
+ IDirect3DVertexDeclaration9 *vertex_declaration;
+ IDirect3DVertexBuffer9 *vb, *vb_s0, *vb_s1;
+ IDirect3DIndexBuffer9 *ib;
+ IDirect3DDevice9 *device;
+ IDirect3D9 *d3d;
+ D3DCOLOR colour;
+ ULONG refcount;
+ HWND window;
+ HRESULT hr;
+ BYTE *data;
+
+ static const D3DVERTEXELEMENT9 decl_elements[] =
+ {
+ {0, 0, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0},
+ {1, 0, D3DDECLTYPE_D3DCOLOR, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_COLOR, 0},
+ D3DDECL_END()
+ };
+ static const struct
+ {
+ struct vec3 position;
+ DWORD diffuse;
+ }
+ quad[] =
+ {
+ {{-1.0f, -1.0f, 0.0f}, 0xffff0000},
+ {{-1.0f, 1.0f, 0.0f}, 0xff00ff00},
+ {{ 1.0f, -1.0f, 0.0f}, 0xff0000ff},
+ {{ 1.0f, 1.0f, 0.0f}, 0xffffffff},
+ };
+ static const struct vec3 quad_s0[] =
+ {
+ {-1.0f, -1.0f, 0.0f},
+ {-1.0f, 1.0f, 0.0f},
+ { 1.0f, -1.0f, 0.0f},
+ { 1.0f, 1.0f, 0.0f},
+ };
+ static const DWORD quad_s1[] =
+ {
+ 0xffff0000,
+ 0xff00ff00,
+ 0xff0000ff,
+ 0xffffffff,
+ };
+ static const short indices[] = {0, 1, 2, 3};
+
+ window = create_window();
+ ok(!!window, "Failed to create a window.\n");
+
+ d3d = Direct3DCreate9(D3D_SDK_VERSION);
+ ok(!!d3d, "Failed to create a D3D object.\n");
+ if (!(device = create_device(d3d, window, window, TRUE)))
+ {
+ skip("Failed to create a D3D device, skipping tests.\n");
+ IDirect3D9_Release(d3d);
+ DestroyWindow(window);
+ return;
+ }
+
+ hr = IDirect3DDevice9_SetRenderState(device, D3DRS_LIGHTING, FALSE);
+ ok(hr == D3D_OK, "Got unexpected hr %#x.\n", hr);
+
+ hr = IDirect3DDevice9_CreateVertexBuffer(device, sizeof(quad), 0, 0, D3DPOOL_SYSTEMMEM, &vb, NULL);
+ ok(hr == D3D_OK, "Got unexpected hr %#x.\n", hr);
+ hr = IDirect3DVertexBuffer9_Lock(vb, 0, sizeof(quad), (void **)&data, 0);
+ ok(hr == D3D_OK, "Got unexpected hr %#x.\n", hr);
+ memcpy(data, quad, sizeof(quad));
+ hr = IDirect3DVertexBuffer9_Unlock(vb);
+ ok(hr == D3D_OK, "Got unexpected hr %#x.\n", hr);
+
+ hr = IDirect3DDevice9_Clear(device, 0, NULL, D3DCLEAR_TARGET, 0x77777777, 0.0f, 0);
+ ok(SUCCEEDED(hr), "Failed to clear depth/stencil, hr %#x.\n", hr);
+
+ 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_BeginScene(device);
+ ok(hr == D3D_OK, "Got unexpected hr %#x.\n", hr);
+ hr = IDirect3DDevice9_DrawPrimitive(device, D3DPT_TRIANGLESTRIP, 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_CreateIndexBuffer(device, sizeof(indices), 0,
+ D3DFMT_INDEX16, D3DPOOL_SYSTEMMEM, &ib, NULL);
+ ok(hr == D3D_OK, "Got unexpected hr %#x.\n", hr);
+ hr = IDirect3DIndexBuffer9_Lock(ib, 0, sizeof(indices), (void **)&data, 0);
+ ok(hr == D3D_OK, "Got unexpected hr %#x.\n", hr);
+ memcpy(data, indices, sizeof(indices));
+ hr = IDirect3DIndexBuffer9_Unlock(ib);
+ ok(hr == D3D_OK, "Got unexpected hr %#x.\n", hr);
+
+ hr = IDirect3DDevice9_Clear(device, 0, NULL, D3DCLEAR_TARGET, 0x77777777, 0.0f, 0);
+ 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);
+
+ 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_CreateVertexDeclaration(device, decl_elements, &vertex_declaration);
+ ok(hr == D3D_OK, "Got unexpected hr %#x.\n", hr);
+ hr = IDirect3DDevice9_SetVertexDeclaration(device, vertex_declaration);
+ ok(hr == D3D_OK, "Got unexpected hr %#x.\n", hr);
+
+ hr = IDirect3DDevice9_CreateVertexBuffer(device, sizeof(quad_s0), 0, 0, D3DPOOL_SYSTEMMEM, &vb_s0, NULL);
+ ok(hr == D3D_OK, "Got unexpected hr %#x.\n", hr);
+ hr = IDirect3DVertexBuffer9_Lock(vb_s0, 0, sizeof(quad_s0), (void **)&data, 0);
+ ok(hr == D3D_OK, "Got unexpected hr %#x.\n", hr);
+ memcpy(data, quad_s0, sizeof(quad_s0));
+ hr = IDirect3DVertexBuffer9_Unlock(vb_s0);
+ ok(hr == D3D_OK, "Got unexpected hr %#x.\n", hr);
+ hr = IDirect3DDevice9_CreateVertexBuffer(device, sizeof(quad_s1), 0, 0, D3DPOOL_SYSTEMMEM, &vb_s1, NULL);
+ ok(hr == D3D_OK, "Got unexpected hr %#x.\n", hr);
+ hr = IDirect3DVertexBuffer9_Lock(vb_s1, 0, sizeof(quad_s1), (void **)&data, 0);
+ ok(hr == D3D_OK, "Got unexpected hr %#x.\n", hr);
+ memcpy(data, quad_s1, sizeof(quad_s1));
+ hr = IDirect3DVertexBuffer9_Unlock(vb_s1);
+ ok(hr == D3D_OK, "Got unexpected hr %#x.\n", hr);
+
+ hr = IDirect3DDevice9_SetStreamSource(device, 0, vb_s0, 0, sizeof(*quad_s0));
+ ok(hr == D3D_OK, "Got unexpected hr %#x.\n", hr);
+ hr = IDirect3DDevice9_SetStreamSource(device, 1, vb_s1, 0, sizeof(*quad_s1));
+ ok(hr == D3D_OK, "Got unexpected hr %#x.\n", hr);
+
+ 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_Present(device, NULL, NULL, NULL, NULL);
+ ok(hr == D3D_OK, "Got unexpected hr %#x.\n", hr);
+
+ IDirect3DVertexBuffer9_Release(vb_s1);
+ IDirect3DVertexBuffer9_Release(vb_s0);
+ IDirect3DVertexDeclaration9_Release(vertex_declaration);
+ IDirect3DIndexBuffer9_Release(ib);
+ IDirect3DVertexBuffer9_Release(vb);
+ refcount = IDirect3DDevice9_Release(device);
+ ok(!refcount, "Device has %u references left.\n", refcount);
+ IDirect3D9_Release(d3d);
+ DestroyWindow(window);
+}
+
START_TEST(visual)
{
D3DADAPTER_IDENTIFIER9 identifier;
@@ -24335,4 +24501,5 @@ START_TEST(visual)
test_null_format();
test_map_synchronisation();
test_color_vertex();
+ test_sysmem_draw();
}
--
2.11.0