Commit 695dbca9 authored by Matteo Bruni's avatar Matteo Bruni Committed by Alexandre Julliard

d3d8: Support drawing from D3DPOOL_SYSTEMMEM index buffers.

parent 75b7ff60
......@@ -377,6 +377,8 @@ static ULONG WINAPI d3d8_indexbuffer_AddRef(IDirect3DIndexBuffer8 *iface)
IDirect3DDevice8_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();
}
......@@ -396,6 +398,8 @@ static ULONG WINAPI d3d8_indexbuffer_Release(IDirect3DIndexBuffer8 *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. */
......@@ -592,6 +596,7 @@ static const struct wined3d_parent_ops d3d8_indexbuffer_wined3d_parent_ops =
HRESULT indexbuffer_init(struct d3d8_indexbuffer *buffer, struct d3d8_device *device,
UINT size, DWORD usage, D3DFORMAT format, D3DPOOL pool)
{
const struct wined3d_parent_ops *parent_ops = &d3d8_null_wined3d_parent_ops;
struct wined3d_buffer_desc desc;
HRESULT hr;
......@@ -604,19 +609,32 @@ HRESULT indexbuffer_init(struct d3d8_indexbuffer *buffer, struct d3d8_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 = &d3d8_indexbuffer_wined3d_parent_ops;
}
buffer->IDirect3DIndexBuffer8_iface.lpVtbl = &d3d8_indexbuffer_vtbl;
d3d8_resource_init(&buffer->resource);
buffer->format = wined3dformat_from_d3dformat(format);
wined3d_mutex_lock();
hr = wined3d_buffer_create(device->wined3d_device, &desc, NULL, buffer,
&d3d8_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,
&d3d8_indexbuffer_wined3d_parent_ops, &buffer->draw_buffer)))
wined3d_buffer_decref(buffer->wined3d_buffer);
}
wined3d_mutex_unlock();
if (FAILED(hr))
{
......
......@@ -124,9 +124,10 @@ struct d3d8_device
LONG device_state;
DWORD sysmem_vb : 16; /* D3D8_MAX_STREAMS */
DWORD sysmem_ib : 1;
DWORD in_destruction : 1;
DWORD recording : 1;
DWORD padding : 14;
DWORD padding : 13;
/* The d3d8 API supports only one implicit swapchain (no D3DCREATE_ADAPTERGROUP_DEVICE,
* no GetSwapchain, GetBackBuffer doesn't accept a swapchain number). */
......@@ -220,6 +221,7 @@ struct d3d8_indexbuffer
struct d3d8_resource resource;
struct wined3d_buffer *wined3d_buffer;
IDirect3DDevice8 *parent_device;
struct wined3d_buffer *draw_buffer;
enum wined3d_format_id format;
};
......
......@@ -1900,6 +1900,7 @@ static HRESULT WINAPI d3d8_device_ApplyStateBlock(IDirect3DDevice8 *iface, DWORD
struct wined3d_buffer *wined3d_buffer;
struct d3d8_vertexbuffer *buffer;
unsigned int i, offset, stride;
enum wined3d_format_id format;
HRESULT hr;
TRACE("iface %p, token %#x.\n", iface, token);
......@@ -1927,6 +1928,8 @@ static HRESULT WINAPI d3d8_device_ApplyStateBlock(IDirect3DDevice8 *iface, DWORD
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;
......@@ -2275,6 +2278,31 @@ static void d3d8_device_upload_sysmem_vertex_buffers(struct d3d8_device *device,
}
}
static void d3d8_device_upload_sysmem_index_buffer(struct d3d8_device *device,
unsigned int start_idx, unsigned int idx_count)
{
struct wined3d_box box = {0, 0, 0, 1, 0, 1};
struct d3d8_vertexbuffer *d3d8_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");
d3d8_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(d3d8_buffer->wined3d_buffer), 0, &box, 0)))
ERR("Failed to update buffer.\n");
}
static HRESULT WINAPI d3d8_device_DrawPrimitive(IDirect3DDevice8 *iface,
D3DPRIMITIVETYPE primitive_type, UINT start_vertex, UINT primitive_count)
{
......@@ -2300,16 +2328,18 @@ static HRESULT WINAPI d3d8_device_DrawIndexedPrimitive(IDirect3DDevice8 *iface,
UINT start_idx, UINT primitive_count)
{
struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
unsigned int index_count;
HRESULT hr;
TRACE("iface %p, primitive_type %#x, min_vertex_idx %u, vertex_count %u, start_idx %u, primitive_count %u.\n",
iface, primitive_type, min_vertex_idx, vertex_count, start_idx, primitive_count);
index_count = vertex_count_from_primitive_count(primitive_type, primitive_count);
wined3d_mutex_lock();
d3d8_device_upload_sysmem_vertex_buffers(device, min_vertex_idx, vertex_count);
d3d8_device_upload_sysmem_index_buffer(device, start_idx, index_count);
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);
wined3d_mutex_unlock();
return hr;
......@@ -2883,9 +2913,17 @@ static HRESULT WINAPI d3d8_device_SetIndices(IDirect3DDevice8 *iface,
{
struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
struct d3d8_indexbuffer *ib = unsafe_impl_from_IDirect3DIndexBuffer8(buffer);
struct wined3d_buffer *wined3d_buffer;
TRACE("iface %p, buffer %p, base_vertex_idx %u.\n", iface, buffer, base_vertex_idx);
if (!ib)
wined3d_buffer = NULL;
else if (ib->draw_buffer)
wined3d_buffer = ib->draw_buffer;
else
wined3d_buffer = ib->wined3d_buffer;
/* WineD3D takes an INT(due to d3d9), but d3d8 uses UINTs. Do I have to add a check here that
* the UINT doesn't cause an overflow in the INT? It seems rather unlikely because such large
* vertex buffers can't be created to address them with an index that requires the 32nd bit
......@@ -2894,8 +2932,9 @@ static HRESULT WINAPI d3d8_device_SetIndices(IDirect3DDevice8 *iface,
*/
wined3d_mutex_lock();
wined3d_device_set_base_vertex_index(device->wined3d_device, base_vertex_idx);
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;
......
......@@ -10448,7 +10448,8 @@ static void test_color_vertex(void)
static void test_sysmem_draw(void)
{
IDirect3DVertexBuffer8 *vb;
IDirect3DVertexBuffer8 *vb, *vb_s0, *vb_s1;
IDirect3DIndexBuffer8 *ib;
IDirect3DDevice8 *device;
IDirect3D8 *d3d;
D3DCOLOR colour;
......@@ -10456,7 +10457,16 @@ static void test_sysmem_draw(void)
HWND window;
HRESULT hr;
BYTE *data;
DWORD vs;
static const DWORD decl[] =
{
D3DVSD_STREAM(0),
D3DVSD_REG(D3DVSDE_POSITION, D3DVSDT_FLOAT3),
D3DVSD_STREAM(1),
D3DVSD_REG(D3DVSDE_DIFFUSE, D3DVSDT_D3DCOLOR),
D3DVSD_END()
};
static const struct
{
struct vec3 position;
......@@ -10469,6 +10479,21 @@ static void test_sysmem_draw(void)
{{ 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");
......@@ -10512,9 +10537,76 @@ static void test_sysmem_draw(void)
colour = getPixelColor(device, 320, 240);
ok(color_match(colour, 0x00007f7f, 1), "Got unexpected colour 0x%08x.\n", colour);
hr = IDirect3DDevice8_CreateIndexBuffer(device, sizeof(indices), 0,
D3DFMT_INDEX16, D3DPOOL_SYSTEMMEM, &ib);
ok(hr == D3D_OK, "Got unexpected hr %#x.\n", hr);
hr = IDirect3DIndexBuffer8_Lock(ib, 0, sizeof(indices), (BYTE **)&data, 0);
ok(hr == D3D_OK, "Got unexpected hr %#x.\n", hr);
memcpy(data, indices, sizeof(indices));
hr = IDirect3DIndexBuffer8_Unlock(ib);
ok(hr == D3D_OK, "Got unexpected hr %#x.\n", hr);
hr = IDirect3DDevice8_Clear(device, 0, NULL, D3DCLEAR_TARGET, 0x77777777, 0.0f, 0);
ok(hr == D3D_OK, "Got unexpected hr %#x.\n", hr);
hr = IDirect3DDevice8_SetIndices(device, ib, 0);
ok(hr == D3D_OK, "Got unexpected hr %#x.\n", hr);
hr = IDirect3DDevice8_BeginScene(device);
ok(hr == D3D_OK, "Got unexpected hr %#x.\n", hr);
hr = IDirect3DDevice8_DrawIndexedPrimitive(device, D3DPT_TRIANGLESTRIP, 0, 4, 0, 2);
ok(hr == D3D_OK, "Got unexpected hr %#x.\n", hr);
hr = IDirect3DDevice8_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 = IDirect3DDevice8_CreateVertexShader(device, decl, NULL, &vs, 0);
ok(hr == D3D_OK, "Got unexpected hr %#x.\n", hr);
hr = IDirect3DDevice8_SetVertexShader(device, vs);
ok(hr == D3D_OK, "Got unexpected hr %#x.\n", hr);
hr = IDirect3DDevice8_CreateVertexBuffer(device, sizeof(quad_s0), 0, 0, D3DPOOL_SYSTEMMEM, &vb_s0);
ok(hr == D3D_OK, "Got unexpected hr %#x.\n", hr);
hr = IDirect3DVertexBuffer8_Lock(vb_s0, 0, sizeof(quad_s0), (BYTE **)&data, 0);
ok(hr == D3D_OK, "Got unexpected hr %#x.\n", hr);
memcpy(data, quad_s0, sizeof(quad_s0));
hr = IDirect3DVertexBuffer8_Unlock(vb_s0);
ok(hr == D3D_OK, "Got unexpected hr %#x.\n", hr);
hr = IDirect3DDevice8_CreateVertexBuffer(device, sizeof(quad_s1), 0, 0, D3DPOOL_SYSTEMMEM, &vb_s1);
ok(hr == D3D_OK, "Got unexpected hr %#x.\n", hr);
hr = IDirect3DVertexBuffer8_Lock(vb_s1, 0, sizeof(quad_s1), (BYTE **)&data, 0);
ok(hr == D3D_OK, "Got unexpected hr %#x.\n", hr);
memcpy(data, quad_s1, sizeof(quad_s1));
hr = IDirect3DVertexBuffer8_Unlock(vb_s1);
ok(hr == D3D_OK, "Got unexpected hr %#x.\n", hr);
hr = IDirect3DDevice8_SetStreamSource(device, 0, vb_s0, sizeof(*quad_s0));
ok(hr == D3D_OK, "Got unexpected hr %#x.\n", hr);
hr = IDirect3DDevice8_SetStreamSource(device, 1, vb_s1, sizeof(*quad_s1));
ok(hr == D3D_OK, "Got unexpected hr %#x.\n", hr);
hr = IDirect3DDevice8_Clear(device, 0, NULL, D3DCLEAR_TARGET, 0x77777777, 0.0f, 0);
ok(hr == D3D_OK, "Got unexpected hr %#x.\n", hr);
hr = IDirect3DDevice8_BeginScene(device);
ok(hr == D3D_OK, "Got unexpected hr %#x.\n", hr);
hr = IDirect3DDevice8_DrawIndexedPrimitive(device, D3DPT_TRIANGLESTRIP, 0, 4, 0, 2);
ok(hr == D3D_OK, "Got unexpected hr %#x.\n", hr);
hr = IDirect3DDevice8_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 = IDirect3DDevice8_Present(device, NULL, NULL, NULL, NULL);
ok(hr == D3D_OK, "Got unexpected hr %#x.\n", hr);
IDirect3DVertexBuffer8_Release(vb_s1);
IDirect3DVertexBuffer8_Release(vb_s0);
IDirect3DDevice8_DeleteVertexShader(device, vs);
IDirect3DIndexBuffer8_Release(ib);
IDirect3DVertexBuffer8_Release(vb);
refcount = IDirect3DDevice8_Release(device);
ok(!refcount, "Device has %u references left.\n", refcount);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment