From 525e3da05137492e632e9ad8fa58238a8e4853bc Mon Sep 17 00:00:00 2001 From: Henri Verbeet Date: Wed, 12 Dec 2012 23:10:42 +0100 Subject: [PATCH] d3d10core: Only create unique blend state objects. --- dlls/d3d10core/d3d10core_private.h | 6 ++++- dlls/d3d10core/device.c | 39 +++++++++++++++++++++++++++++- dlls/d3d10core/state.c | 19 +++++++++++---- 3 files changed, 57 insertions(+), 7 deletions(-) diff --git a/dlls/d3d10core/d3d10core_private.h b/dlls/d3d10core/d3d10core_private.h index 8164e2fe7e3..dd7d8f5c5c9 100644 --- a/dlls/d3d10core/d3d10core_private.h +++ b/dlls/d3d10core/d3d10core_private.h @@ -217,10 +217,13 @@ struct d3d10_blend_state ID3D10BlendState ID3D10BlendState_iface; LONG refcount; + struct d3d10_device *device; D3D10_BLEND_DESC desc; + struct wine_rb_entry entry; }; -HRESULT d3d10_blend_state_init(struct d3d10_blend_state *state, const D3D10_BLEND_DESC *desc) DECLSPEC_HIDDEN; +HRESULT d3d10_blend_state_init(struct d3d10_blend_state *state, struct d3d10_device *device, + const D3D10_BLEND_DESC *desc) DECLSPEC_HIDDEN; struct d3d10_blend_state *unsafe_impl_from_ID3D10BlendState(ID3D10BlendState *iface) DECLSPEC_HIDDEN; /* ID3D10DepthStencilState */ @@ -281,6 +284,7 @@ struct d3d10_device struct wined3d_device_parent device_parent; struct wined3d_device *wined3d_device; + struct wine_rb_tree blend_states; struct wine_rb_tree sampler_states; struct d3d10_blend_state *blend_state; diff --git a/dlls/d3d10core/device.c b/dlls/d3d10core/device.c index 1025b42ae87..3ee13ae0622 100644 --- a/dlls/d3d10core/device.c +++ b/dlls/d3d10core/device.c @@ -82,6 +82,7 @@ static ULONG STDMETHODCALLTYPE d3d10_device_inner_Release(IUnknown *iface) if (device->wined3d_device) wined3d_device_decref(device->wined3d_device); wine_rb_destroy(&device->sampler_states, NULL, NULL); + wine_rb_destroy(&device->blend_states, NULL, NULL); } return refcount; @@ -1385,7 +1386,9 @@ static HRESULT STDMETHODCALLTYPE d3d10_device_CreatePixelShader(ID3D10Device *if static HRESULT STDMETHODCALLTYPE d3d10_device_CreateBlendState(ID3D10Device *iface, const D3D10_BLEND_DESC *desc, ID3D10BlendState **blend_state) { + struct d3d10_device *device = impl_from_ID3D10Device(iface); struct d3d10_blend_state *object; + struct wine_rb_entry *entry; HRESULT hr; TRACE("iface %p, desc %p, blend_state %p.\n", iface, desc, blend_state); @@ -1393,6 +1396,17 @@ static HRESULT STDMETHODCALLTYPE d3d10_device_CreateBlendState(ID3D10Device *ifa if (!desc) return E_INVALIDARG; + if ((entry = wine_rb_get(&device->blend_states, desc))) + { + object = WINE_RB_ENTRY_VALUE(entry, struct d3d10_blend_state, entry); + + TRACE("Returning existing blend state %p.\n", object); + *blend_state = &object->ID3D10BlendState_iface; + ID3D10BlendState_AddRef(*blend_state); + + return S_OK; + } + object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object)); if (!object) { @@ -1400,7 +1414,7 @@ static HRESULT STDMETHODCALLTYPE d3d10_device_CreateBlendState(ID3D10Device *ifa return E_OUTOFMEMORY; } - if (FAILED(hr = d3d10_blend_state_init(object, desc))) + if (FAILED(hr = d3d10_blend_state_init(object, device, desc))) { WARN("Failed to initialize blend state, hr %#x.\n", hr); HeapFree(GetProcessHeap(), 0, object); @@ -1955,6 +1969,22 @@ static const struct wine_rb_functions d3d10_sampler_state_rb_ops = d3d10_sampler_state_compare, }; +static int d3d10_blend_state_compare(const void *key, const struct wine_rb_entry *entry) +{ + const D3D10_BLEND_DESC *ka = key; + const D3D10_BLEND_DESC *kb = &WINE_RB_ENTRY_VALUE(entry, const struct d3d10_blend_state, entry)->desc; + + return memcmp(ka, kb, sizeof(*ka)); +} + +static const struct wine_rb_functions d3d10_blend_state_rb_ops = +{ + d3d10_rb_alloc, + d3d10_rb_realloc, + d3d10_rb_free, + d3d10_blend_state_compare, +}; + HRESULT d3d10_device_init(struct d3d10_device *device, void *outer_unknown) { device->ID3D10Device_iface.lpVtbl = &d3d10_device_vtbl; @@ -1965,9 +1995,16 @@ HRESULT d3d10_device_init(struct d3d10_device *device, void *outer_unknown) /* COM aggregation always takes place */ device->outer_unk = outer_unknown; + if (wine_rb_init(&device->blend_states, &d3d10_blend_state_rb_ops) == -1) + { + WARN("Failed to initialize blend state rbtree.\n"); + return E_FAIL; + } + if (wine_rb_init(&device->sampler_states, &d3d10_sampler_state_rb_ops) == -1) { WARN("Failed to initialize sampler state rbtree.\n"); + wine_rb_destroy(&device->blend_states, NULL, NULL); return E_FAIL; } diff --git a/dlls/d3d10core/state.c b/dlls/d3d10core/state.c index 4092acc6227..099227390bd 100644 --- a/dlls/d3d10core/state.c +++ b/dlls/d3d10core/state.c @@ -63,14 +63,15 @@ static ULONG STDMETHODCALLTYPE d3d10_blend_state_AddRef(ID3D10BlendState *iface) static ULONG STDMETHODCALLTYPE d3d10_blend_state_Release(ID3D10BlendState *iface) { - struct d3d10_blend_state *This = impl_from_ID3D10BlendState(iface); - ULONG refcount = InterlockedDecrement(&This->refcount); + struct d3d10_blend_state *state = impl_from_ID3D10BlendState(iface); + ULONG refcount = InterlockedDecrement(&state->refcount); - TRACE("%p decreasing refcount to %u.\n", This, refcount); + TRACE("%p decreasing refcount to %u.\n", state, refcount); if (!refcount) { - HeapFree(GetProcessHeap(), 0, This); + wine_rb_remove(&state->device->blend_states, &state->desc); + HeapFree(GetProcessHeap(), 0, state); } return refcount; @@ -136,12 +137,20 @@ static const struct ID3D10BlendStateVtbl d3d10_blend_state_vtbl = d3d10_blend_state_GetDesc, }; -HRESULT d3d10_blend_state_init(struct d3d10_blend_state *state, const D3D10_BLEND_DESC *desc) +HRESULT d3d10_blend_state_init(struct d3d10_blend_state *state, struct d3d10_device *device, + const D3D10_BLEND_DESC *desc) { state->ID3D10BlendState_iface.lpVtbl = &d3d10_blend_state_vtbl; state->refcount = 1; + state->device = device; state->desc = *desc; + if (wine_rb_put(&device->blend_states, desc, &state->entry) == -1) + { + ERR("Failed to insert blend state entry.\n"); + return E_FAIL; + } + return S_OK; }