Commit 75b7ff60 authored by Matteo Bruni's avatar Matteo Bruni Committed by Alexandre Julliard

d3d8: Support drawing from D3DPOOL_SYSTEMMEM vertex buffers.

parent f34a19e6
......@@ -57,6 +57,8 @@ static ULONG WINAPI d3d8_vertexbuffer_AddRef(IDirect3DVertexBuffer8 *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();
}
......@@ -76,6 +78,8 @@ static ULONG WINAPI d3d8_vertexbuffer_Release(IDirect3DVertexBuffer8 *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. */
......@@ -273,6 +277,7 @@ static const struct wined3d_parent_ops d3d8_vertexbuffer_wined3d_parent_ops =
HRESULT vertexbuffer_init(struct d3d8_vertexbuffer *buffer, struct d3d8_device *device,
UINT size, DWORD usage, DWORD fvf, D3DPOOL pool)
{
const struct wined3d_parent_ops *parent_ops = &d3d8_null_wined3d_parent_ops;
struct wined3d_buffer_desc desc;
HRESULT hr;
......@@ -292,15 +297,28 @@ HRESULT vertexbuffer_init(struct d3d8_vertexbuffer *buffer, struct d3d8_device *
desc.byte_width = size;
desc.usage = usage & WINED3DUSAGE_MASK;
desc.bind_flags = WINED3D_BIND_VERTEX_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_VERTEX_BUFFER;
parent_ops = &d3d8_vertexbuffer_wined3d_parent_ops;
}
wined3d_mutex_lock();
hr = wined3d_buffer_create(device->wined3d_device, &desc, NULL, buffer,
&d3d8_vertexbuffer_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_VERTEX_BUFFER;
desc.access = WINED3D_RESOURCE_ACCESS_GPU;
if (FAILED(hr = wined3d_buffer_create(device->wined3d_device, &desc, NULL, buffer,
&d3d8_vertexbuffer_wined3d_parent_ops, &buffer->draw_buffer)))
wined3d_buffer_decref(buffer->wined3d_buffer);
}
wined3d_mutex_unlock();
if (FAILED(hr))
{
......
......@@ -39,9 +39,14 @@
#define D3DPRESENTFLAGS_MASK 0x00000fffu
#define D3D8_MAX_VERTEX_SHADER_CONSTANTF 256
#define D3D8_MAX_STREAMS 16
/* CreateVertexShader can return > 0xFFFF */
#define VS_HIGHESTFIXEDFXF 0xF0000000
extern const struct wined3d_parent_ops d3d8_null_wined3d_parent_ops DECLSPEC_HIDDEN;
void d3dcaps_from_wined3dcaps(D3DCAPS8 *caps, const struct wined3d_caps *wined3d_caps) DECLSPEC_HIDDEN;
struct d3d8
......@@ -118,8 +123,10 @@ struct d3d8_device
UINT index_buffer_pos;
LONG device_state;
/* Avoids recursion with nested ReleaseRef to 0 */
BOOL inDestruction;
DWORD sysmem_vb : 16; /* D3D8_MAX_STREAMS */
DWORD in_destruction : 1;
DWORD recording : 1;
DWORD padding : 14;
/* The d3d8 API supports only one implicit swapchain (no D3DCREATE_ADAPTERGROUP_DEVICE,
* no GetSwapchain, GetBackBuffer doesn't accept a swapchain number). */
......@@ -199,6 +206,7 @@ struct d3d8_vertexbuffer
struct d3d8_resource resource;
struct wined3d_buffer *wined3d_buffer;
IDirect3DDevice8 *parent_device;
struct wined3d_buffer *draw_buffer;
DWORD fvf;
};
......@@ -260,8 +268,6 @@ void d3d8_vertex_shader_destroy(struct d3d8_vertex_shader *shader) DECLSPEC_HIDD
HRESULT d3d8_vertex_shader_init(struct d3d8_vertex_shader *shader, struct d3d8_device *device,
const DWORD *declaration, const DWORD *byte_code, DWORD shader_handle, DWORD usage) DECLSPEC_HIDDEN;
#define D3D8_MAX_VERTEX_SHADER_CONSTANTF 256
struct d3d8_pixel_shader
{
DWORD handle;
......
......@@ -20,6 +20,7 @@
*/
#include "config.h"
#include "wine/port.h"
#include <math.h>
#include <stdarg.h>
......@@ -36,7 +37,7 @@ WINE_DEFAULT_DEBUG_CHANNEL(d3d8);
static void STDMETHODCALLTYPE d3d8_null_wined3d_object_destroyed(void *parent) {}
static const struct wined3d_parent_ops d3d8_null_wined3d_parent_ops =
const struct wined3d_parent_ops d3d8_null_wined3d_parent_ops =
{
d3d8_null_wined3d_object_destroyed,
};
......@@ -564,7 +565,7 @@ static ULONG WINAPI d3d8_device_Release(IDirect3DDevice8 *iface)
struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
ULONG ref;
if (device->inDestruction)
if (device->in_destruction)
return 0;
ref = InterlockedDecrement(&device->ref);
......@@ -580,7 +581,7 @@ static ULONG WINAPI d3d8_device_Release(IDirect3DDevice8 *iface)
wined3d_mutex_lock();
device->inDestruction = TRUE;
device->in_destruction = TRUE;
for (i = 0; i < device->numConvertedDecls; ++i)
{
......@@ -891,6 +892,7 @@ static HRESULT WINAPI d3d8_device_Reset(IDirect3DDevice8 *iface,
if (SUCCEEDED(hr = wined3d_device_reset(device->wined3d_device, &swapchain_desc,
NULL, reset_enum_callback, TRUE)))
{
device->recording = FALSE;
present_parameters->BackBufferCount = swapchain_desc.backbuffer_count;
device->implicit_swapchain->swap_interval
= wined3dswapinterval_from_d3d(present_parameters->FullScreen_PresentationInterval);
......@@ -1845,7 +1847,8 @@ static HRESULT WINAPI d3d8_device_BeginStateBlock(IDirect3DDevice8 *iface)
TRACE("iface %p.\n", iface);
wined3d_mutex_lock();
hr = wined3d_device_begin_stateblock(device->wined3d_device);
if (SUCCEEDED(hr = wined3d_device_begin_stateblock(device->wined3d_device)))
device->recording = TRUE;
wined3d_mutex_unlock();
return hr;
......@@ -1870,6 +1873,7 @@ static HRESULT WINAPI d3d8_device_EndStateBlock(IDirect3DDevice8 *iface, DWORD *
wined3d_mutex_unlock();
return hr;
}
device->recording = FALSE;
*token = d3d8_allocate_handle(&device->handle_table, stateblock, D3D8_HANDLE_SB);
wined3d_mutex_unlock();
......@@ -1893,6 +1897,10 @@ static HRESULT WINAPI d3d8_device_ApplyStateBlock(IDirect3DDevice8 *iface, DWORD
{
struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
struct wined3d_stateblock *stateblock;
struct wined3d_buffer *wined3d_buffer;
struct d3d8_vertexbuffer *buffer;
unsigned int i, offset, stride;
HRESULT hr;
TRACE("iface %p, token %#x.\n", iface, token);
......@@ -1908,6 +1916,17 @@ static HRESULT WINAPI d3d8_device_ApplyStateBlock(IDirect3DDevice8 *iface, DWORD
return D3DERR_INVALIDCALL;
}
wined3d_stateblock_apply(stateblock);
device->sysmem_vb = 0;
for (i = 0; i < D3D8_MAX_STREAMS; ++i)
{
if (FAILED(hr = wined3d_device_get_stream_source(device->wined3d_device,
i, &wined3d_buffer, &offset, &stride)))
continue;
if (!wined3d_buffer || !(buffer = wined3d_buffer_get_parent(wined3d_buffer)))
continue;
if (buffer->draw_buffer)
device->sysmem_vb |= 1u << i;
}
wined3d_mutex_unlock();
return D3D_OK;
......@@ -2229,19 +2248,48 @@ static HRESULT WINAPI d3d8_device_GetCurrentTexturePalette(IDirect3DDevice8 *ifa
return D3DERR_INVALIDCALL;
}
static void d3d8_device_upload_sysmem_vertex_buffers(struct d3d8_device *device,
unsigned int start_vertex, unsigned int vertex_count)
{
struct wined3d_box box = {0, 0, 0, 1, 0, 1};
struct d3d8_vertexbuffer *d3d8_buffer;
unsigned int i, offset, stride, map;
struct wined3d_buffer *dst_buffer;
HRESULT hr;
map = device->sysmem_vb;
while (map)
{
i = ffs(map) - 1;
map ^= 1u << i;
if (FAILED(hr = wined3d_device_get_stream_source(device->wined3d_device, i, &dst_buffer, &offset, &stride)))
ERR("Failed to get stream source.\n");
d3d8_buffer = wined3d_buffer_get_parent(dst_buffer);
box.left = offset + start_vertex * stride;
box.right = box.left + vertex_count * stride;
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)
{
struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
unsigned int vertex_count;
HRESULT hr;
TRACE("iface %p, primitive_type %#x, start_vertex %u, primitive_count %u.\n",
iface, primitive_type, start_vertex, primitive_count);
vertex_count = vertex_count_from_primitive_count(primitive_type, primitive_count);
wined3d_mutex_lock();
d3d8_device_upload_sysmem_vertex_buffers(device, start_vertex, vertex_count);
wined3d_device_set_primitive_type(device->wined3d_device, primitive_type, 0);
hr = wined3d_device_draw_primitive(device->wined3d_device, start_vertex,
vertex_count_from_primitive_count(primitive_type, primitive_count));
hr = wined3d_device_draw_primitive(device->wined3d_device, start_vertex, vertex_count);
wined3d_mutex_unlock();
return hr;
......@@ -2258,6 +2306,7 @@ static HRESULT WINAPI d3d8_device_DrawIndexedPrimitive(IDirect3DDevice8 *iface,
iface, primitive_type, min_vertex_idx, vertex_count, start_idx, primitive_count);
wined3d_mutex_lock();
d3d8_device_upload_sysmem_vertex_buffers(device, min_vertex_idx, vertex_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));
......@@ -3098,6 +3147,7 @@ static HRESULT WINAPI d3d8_device_SetStreamSource(IDirect3DDevice8 *iface,
{
struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
struct d3d8_vertexbuffer *buffer_impl = unsafe_impl_from_IDirect3DVertexBuffer8(buffer);
struct wined3d_buffer *wined3d_buffer;
HRESULT hr;
TRACE("iface %p, stream_idx %u, buffer %p, stride %u.\n",
......@@ -3106,14 +3156,28 @@ static HRESULT WINAPI d3d8_device_SetStreamSource(IDirect3DDevice8 *iface,
wined3d_mutex_lock();
if (!stride)
{
struct wined3d_buffer *wined3d_buffer;
unsigned int cur_offset;
hr = wined3d_device_get_stream_source(device->wined3d_device, stream_idx, &wined3d_buffer,
&cur_offset, &stride);
}
hr = wined3d_device_set_stream_source(device->wined3d_device, stream_idx,
buffer_impl ? buffer_impl->wined3d_buffer : NULL, 0, stride);
if (!buffer_impl)
wined3d_buffer = NULL;
else if (buffer_impl->draw_buffer)
wined3d_buffer = buffer_impl->draw_buffer;
else
wined3d_buffer = buffer_impl->wined3d_buffer;
hr = wined3d_device_set_stream_source(device->wined3d_device, stream_idx, wined3d_buffer, 0, stride);
if (SUCCEEDED(hr) && !device->recording)
{
if (buffer_impl && buffer_impl->draw_buffer)
device->sysmem_vb |= (1u << stream_idx);
else
device->sysmem_vb &= ~(1u << stream_idx);
}
wined3d_mutex_unlock();
return hr;
......
......@@ -10446,6 +10446,82 @@ static void test_color_vertex(void)
DestroyWindow(window);
}
static void test_sysmem_draw(void)
{
IDirect3DVertexBuffer8 *vb;
IDirect3DDevice8 *device;
IDirect3D8 *d3d;
D3DCOLOR colour;
ULONG refcount;
HWND window;
HRESULT hr;
BYTE *data;
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},
};
window = create_window();
ok(!!window, "Failed to create a window.\n");
d3d = Direct3DCreate8(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");
IDirect3D8_Release(d3d);
DestroyWindow(window);
return;
}
hr = IDirect3DDevice8_SetRenderState(device, D3DRS_LIGHTING, FALSE);
ok(hr == D3D_OK, "Got unexpected hr %#x.\n", hr);
hr = IDirect3DDevice8_CreateVertexBuffer(device, sizeof(quad), 0, 0, D3DPOOL_SYSTEMMEM, &vb);
ok(hr == D3D_OK, "Got unexpected hr %#x.\n", hr);
hr = IDirect3DVertexBuffer8_Lock(vb, 0, sizeof(quad), (BYTE **)&data, 0);
ok(hr == D3D_OK, "Got unexpected hr %#x.\n", hr);
memcpy(data, quad, sizeof(quad));
hr = IDirect3DVertexBuffer8_Unlock(vb);
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_SetVertexShader(device, D3DFVF_XYZ | D3DFVF_DIFFUSE);
ok(hr == D3D_OK, "Got unexpected hr %#x.\n", hr);
hr = IDirect3DDevice8_SetStreamSource(device, 0, vb, sizeof(*quad));
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_DrawPrimitive(device, D3DPT_TRIANGLESTRIP, 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);
refcount = IDirect3DDevice8_Release(device);
ok(!refcount, "Device has %u references left.\n", refcount);
IDirect3D8_Release(d3d);
DestroyWindow(window);
}
START_TEST(visual)
{
D3DADAPTER_IDENTIFIER8 identifier;
......@@ -10520,4 +10596,5 @@ START_TEST(visual)
test_map_synchronisation();
test_viewport();
test_color_vertex();
test_sysmem_draw();
}
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