Signed-off-by: Matteo Bruni mbruni@codeweavers.com --- dlls/d3d9/buffer.c | 24 +++++++++++++++++--- dlls/d3d9/d3d9_private.h | 1 + dlls/d3d9/device.c | 47 +++++++++++++++++++++++++++++++++++++--- dlls/d3d9/stateblock.c | 4 ++++ 4 files changed, 70 insertions(+), 6 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 8647aac4af1..6f3c71e2910 100644 --- a/dlls/d3d9/d3d9_private.h +++ b/dlls/d3d9/d3d9_private.h @@ -198,6 +198,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 7c488bfd601..dcd6100026d 100644 --- a/dlls/d3d9/device.c +++ b/dlls/d3d9/device.c @@ -2700,7 +2700,7 @@ static void d3d9_device_upload_sysmem_buffers(struct d3d9_device *device, struct wined3d_buffer *dst_buffer; HRESULT hr;
- map = device->upload_map; + map = device->upload_map & ((1u << D3D9_MAX_STREAMS) - 1); while (map) { i = ffs(map) - 1; @@ -2718,6 +2718,31 @@ static void d3d9_device_upload_sysmem_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->upload_map & (1u << D3D9_MAX_STREAMS)) + 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) { @@ -2767,6 +2792,8 @@ static HRESULT WINAPI d3d9_device_DrawIndexedPrimitive(IDirect3DDevice9Ex *iface return D3DERR_INVALIDCALL; } d3d9_device_upload_sysmem_buffers(device, min_vertex_idx, vertex_count); + d3d9_device_upload_sysmem_index_buffer(device, start_idx, + vertex_count_from_primitive_count(primitive_type, primitive_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); @@ -3512,12 +3539,26 @@ 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) + { + if (ib && ib->draw_buffer) + device->upload_map |= (1u << D3D9_MAX_STREAMS); + else + device->upload_map &= ~(1u << D3D9_MAX_STREAMS); + } wined3d_mutex_unlock();
return D3D_OK; diff --git a/dlls/d3d9/stateblock.c b/dlls/d3d9/stateblock.c index c5306fd80b3..876e66b74c9 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,9 @@ static HRESULT WINAPI d3d9_stateblock_Apply(IDirect3DStateBlock9 *iface) if (buffer->draw_buffer) device->upload_map |= 1u << i; } + if ((wined3d_buffer = wined3d_device_get_index_buffer(device->wined3d_device, &format, &offset)) + && (buffer = wined3d_buffer_get_parent(wined3d_buffer)) && buffer->draw_buffer) + device->upload_map |= 1u << D3D9_MAX_STREAMS; wined3d_mutex_unlock();
return D3D_OK;