Commit 2e94258c authored by Zebediah Figura's avatar Zebediah Figura Committed by Alexandre Julliard

d3d9: Use the wined3d_streaming_buffer helpers to manage the streaming vertex buffer.

parent e8b7200f
......@@ -100,9 +100,8 @@ struct d3d9_device
struct fvf_declaration *fvf_decls;
UINT fvf_decl_count, fvf_decl_size;
struct wined3d_buffer *vertex_buffer;
UINT vertex_buffer_size;
UINT vertex_buffer_pos;
struct wined3d_streaming_buffer vertex_buffer;
struct wined3d_buffer *index_buffer;
UINT index_buffer_size;
UINT index_buffer_pos;
......
......@@ -663,8 +663,7 @@ static ULONG WINAPI DECLSPEC_HOTPATCH d3d9_device_Release(IDirect3DDevice9Ex *if
}
heap_free(device->fvf_decls);
if (device->vertex_buffer)
wined3d_buffer_decref(device->vertex_buffer);
wined3d_streaming_buffer_cleanup(&device->vertex_buffer);
if (device->index_buffer)
wined3d_buffer_decref(device->index_buffer);
......@@ -1079,12 +1078,7 @@ static HRESULT d3d9_device_reset(struct d3d9_device *device,
wined3d_mutex_lock();
if (device->vertex_buffer)
{
wined3d_buffer_decref(device->vertex_buffer);
device->vertex_buffer = NULL;
device->vertex_buffer_size = 0;
}
wined3d_streaming_buffer_cleanup(&device->vertex_buffer);
if (device->index_buffer)
{
......@@ -3080,54 +3074,14 @@ static HRESULT WINAPI d3d9_device_DrawIndexedPrimitive(IDirect3DDevice9Ex *iface
return D3D_OK;
}
/* The caller is responsible for wined3d locking */
static HRESULT d3d9_device_prepare_vertex_buffer(struct d3d9_device *device, UINT min_size)
{
HRESULT hr;
if (device->vertex_buffer_size < min_size || !device->vertex_buffer)
{
UINT size = max(device->vertex_buffer_size * 2, min_size);
struct wined3d_buffer_desc desc;
struct wined3d_buffer *buffer;
TRACE("Growing vertex buffer to %u bytes.\n", size);
desc.byte_width = size;
desc.usage = WINED3DUSAGE_DYNAMIC;
desc.bind_flags = WINED3D_BIND_VERTEX_BUFFER;
desc.access = WINED3D_RESOURCE_ACCESS_GPU | WINED3D_RESOURCE_ACCESS_MAP_W;
desc.misc_flags = 0;
desc.structure_byte_stride = 0;
if (FAILED(hr = wined3d_buffer_create(device->wined3d_device, &desc,
NULL, NULL, &d3d9_null_wined3d_parent_ops, &buffer)))
{
ERR("Failed to create vertex buffer, hr %#x.\n", hr);
return hr;
}
if (device->vertex_buffer)
wined3d_buffer_decref(device->vertex_buffer);
device->vertex_buffer = buffer;
device->vertex_buffer_size = size;
device->vertex_buffer_pos = 0;
}
return D3D_OK;
}
static HRESULT WINAPI d3d9_device_DrawPrimitiveUP(IDirect3DDevice9Ex *iface,
D3DPRIMITIVETYPE primitive_type, UINT primitive_count, const void *data, UINT stride)
{
struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(iface);
HRESULT hr;
UINT vtx_count = vertex_count_from_primitive_count(primitive_type, primitive_count);
struct wined3d_map_desc wined3d_map_desc;
struct wined3d_box wined3d_box = {0};
UINT size = vtx_count * stride;
struct wined3d_resource *vb;
UINT vb_pos, align;
unsigned int vb_pos;
HRESULT hr;
TRACE("iface %p, primitive_type %#x, primitive_count %u, data %p, stride %u.\n",
iface, primitive_type, primitive_count, data, stride);
......@@ -3152,29 +3106,11 @@ static HRESULT WINAPI d3d9_device_DrawPrimitiveUP(IDirect3DDevice9Ex *iface,
return D3DERR_INVALIDCALL;
}
hr = d3d9_device_prepare_vertex_buffer(device, size);
if (FAILED(hr))
if (FAILED(hr = wined3d_streaming_buffer_upload(device->wined3d_device,
&device->vertex_buffer, data, size, stride, &vb_pos)))
goto done;
vb_pos = device->vertex_buffer_pos;
align = vb_pos % stride;
if (align) align = stride - align;
if (vb_pos + size + align > device->vertex_buffer_size)
vb_pos = 0;
else
vb_pos += align;
wined3d_box.left = vb_pos;
wined3d_box.right = vb_pos + size;
vb = wined3d_buffer_get_resource(device->vertex_buffer);
if (FAILED(hr = wined3d_resource_map(vb, 0, &wined3d_map_desc, &wined3d_box,
WINED3D_MAP_WRITE | (vb_pos ? WINED3D_MAP_NOOVERWRITE : WINED3D_MAP_DISCARD))))
goto done;
memcpy(wined3d_map_desc.data, data, size);
wined3d_resource_unmap(vb, 0);
device->vertex_buffer_pos = vb_pos + size;
hr = wined3d_stateblock_set_stream_source(device->state, 0, device->vertex_buffer, 0, stride);
hr = wined3d_stateblock_set_stream_source(device->state, 0, device->vertex_buffer.buffer, 0, stride);
if (FAILED(hr))
goto done;
......@@ -3240,7 +3176,7 @@ static HRESULT WINAPI d3d9_device_DrawIndexedPrimitiveUP(IDirect3DDevice9Ex *ifa
UINT idx_size = idx_count * idx_fmt_size;
struct wined3d_map_desc wined3d_map_desc;
struct wined3d_box wined3d_box = {0};
struct wined3d_resource *ib, *vb;
struct wined3d_resource *ib;
UINT vb_pos, ib_pos, align;
HRESULT hr;
......@@ -3269,27 +3205,9 @@ static HRESULT WINAPI d3d9_device_DrawIndexedPrimitiveUP(IDirect3DDevice9Ex *ifa
return D3DERR_INVALIDCALL;
}
hr = d3d9_device_prepare_vertex_buffer(device, vtx_size);
if (FAILED(hr))
goto done;
vb_pos = device->vertex_buffer_pos;
align = vb_pos % vertex_stride;
if (align) align = vertex_stride - align;
if (vb_pos + vtx_size + align > device->vertex_buffer_size)
vb_pos = 0;
else
vb_pos += align;
wined3d_box.left = vb_pos;
wined3d_box.right = vb_pos + vtx_size;
vb = wined3d_buffer_get_resource(device->vertex_buffer);
if (FAILED(hr = wined3d_resource_map(vb, 0, &wined3d_map_desc, &wined3d_box,
WINED3D_MAP_WRITE | (vb_pos ? WINED3D_MAP_NOOVERWRITE : WINED3D_MAP_DISCARD))))
if (FAILED(hr = wined3d_streaming_buffer_upload(device->wined3d_device, &device->vertex_buffer,
(char *)vertex_data + min_vertex_idx * vertex_stride, vtx_size, vertex_stride, &vb_pos)))
goto done;
memcpy(wined3d_map_desc.data, (char *)vertex_data + min_vertex_idx * vertex_stride, vtx_size);
wined3d_resource_unmap(vb, 0);
device->vertex_buffer_pos = vb_pos + vtx_size;
hr = d3d9_device_prepare_index_buffer(device, idx_size);
if (FAILED(hr))
......@@ -3313,7 +3231,7 @@ static HRESULT WINAPI d3d9_device_DrawIndexedPrimitiveUP(IDirect3DDevice9Ex *ifa
wined3d_resource_unmap(ib, 0);
device->index_buffer_pos = ib_pos + idx_size;
hr = wined3d_stateblock_set_stream_source(device->state, 0, device->vertex_buffer, 0, vertex_stride);
hr = wined3d_stateblock_set_stream_source(device->state, 0, device->vertex_buffer.buffer, 0, vertex_stride);
if (FAILED(hr))
goto done;
......@@ -4754,6 +4672,8 @@ HRESULT device_init(struct d3d9_device *device, struct d3d9 *parent, struct wine
device->stateblock_state = wined3d_stateblock_get_state(device->state);
device->update_state = device->state;
wined3d_streaming_buffer_init(&device->vertex_buffer, WINED3D_BIND_VERTEX_BUFFER);
if (flags & D3DCREATE_MULTITHREADED)
wined3d_device_set_multithreaded(device->wined3d_device);
......
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