wined3d: Pass a wined3d_device_context to wined3d_cs_emit_blt_sub_resource().
[wine/zf.git] / dlls / d3d9 / stateblock.c
blob3c1e3230095cac57cf8d18f41695ddbd96aa2021
1 /*
2 * IDirect3DStateBlock9 implementation
4 * Copyright 2002-2003 Raphael Junqueira
5 * Copyright 2002-2003 Jason Edmeades
6 * Copyright 2005 Oliver Stieber
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 #include "d3d9_private.h"
25 WINE_DEFAULT_DEBUG_CHANNEL(d3d9);
27 static inline struct d3d9_stateblock *impl_from_IDirect3DStateBlock9(IDirect3DStateBlock9 *iface)
29 return CONTAINING_RECORD(iface, struct d3d9_stateblock, IDirect3DStateBlock9_iface);
32 static HRESULT WINAPI d3d9_stateblock_QueryInterface(IDirect3DStateBlock9 *iface, REFIID riid, void **out)
34 TRACE("iface %p, riid %s, out %p.\n", iface, debugstr_guid(riid), out);
36 if (IsEqualGUID(riid, &IID_IDirect3DStateBlock9)
37 || IsEqualGUID(riid, &IID_IUnknown))
39 IDirect3DStateBlock9_AddRef(iface);
40 *out = iface;
41 return S_OK;
44 WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(riid));
46 *out = NULL;
47 return E_NOINTERFACE;
50 static ULONG WINAPI d3d9_stateblock_AddRef(IDirect3DStateBlock9 *iface)
52 struct d3d9_stateblock *stateblock = impl_from_IDirect3DStateBlock9(iface);
53 ULONG refcount = InterlockedIncrement(&stateblock->refcount);
55 TRACE("%p increasing refcount to %u.\n", iface, refcount);
57 return refcount;
60 static ULONG WINAPI d3d9_stateblock_Release(IDirect3DStateBlock9 *iface)
62 struct d3d9_stateblock *stateblock = impl_from_IDirect3DStateBlock9(iface);
63 ULONG refcount = InterlockedDecrement(&stateblock->refcount);
65 TRACE("%p decreasing refcount to %u.\n", iface, refcount);
67 if (!refcount)
69 wined3d_mutex_lock();
70 wined3d_stateblock_decref(stateblock->wined3d_stateblock);
71 wined3d_mutex_unlock();
73 IDirect3DDevice9Ex_Release(stateblock->parent_device);
74 heap_free(stateblock);
77 return refcount;
80 static HRESULT WINAPI d3d9_stateblock_GetDevice(IDirect3DStateBlock9 *iface, IDirect3DDevice9 **device)
82 struct d3d9_stateblock *stateblock = impl_from_IDirect3DStateBlock9(iface);
84 TRACE("iface %p, device %p.\n", iface, device);
86 *device = (IDirect3DDevice9 *)stateblock->parent_device;
87 IDirect3DDevice9_AddRef(*device);
89 TRACE("Returning device %p.\n", *device);
91 return D3D_OK;
94 static HRESULT WINAPI d3d9_stateblock_Capture(IDirect3DStateBlock9 *iface)
96 struct d3d9_stateblock *stateblock = impl_from_IDirect3DStateBlock9(iface);
97 struct d3d9_device *device;
99 TRACE("iface %p.\n", iface);
101 wined3d_mutex_lock();
102 device = impl_from_IDirect3DDevice9Ex(stateblock->parent_device);
103 if (device->recording)
105 wined3d_mutex_unlock();
106 WARN("Trying to capture stateblock while recording, returning D3DERR_INVALIDCALL.\n");
107 return D3DERR_INVALIDCALL;
109 wined3d_stateblock_capture(stateblock->wined3d_stateblock, device->state);
110 wined3d_mutex_unlock();
112 return D3D_OK;
115 static HRESULT WINAPI d3d9_stateblock_Apply(IDirect3DStateBlock9 *iface)
117 struct d3d9_stateblock *stateblock = impl_from_IDirect3DStateBlock9(iface);
118 struct wined3d_texture *wined3d_texture;
119 struct wined3d_buffer *wined3d_buffer;
120 struct d3d9_vertexbuffer *buffer;
121 struct d3d9_texture *texture;
122 struct d3d9_device *device;
123 unsigned int i;
125 TRACE("iface %p.\n", iface);
127 wined3d_mutex_lock();
128 device = impl_from_IDirect3DDevice9Ex(stateblock->parent_device);
129 if (device->recording)
131 wined3d_mutex_unlock();
132 WARN("Trying to apply stateblock while recording, returning D3DERR_INVALIDCALL.\n");
133 return D3DERR_INVALIDCALL;
135 wined3d_stateblock_apply(stateblock->wined3d_stateblock, device->state);
136 device->sysmem_vb = 0;
137 for (i = 0; i < D3D9_MAX_STREAMS; ++i)
139 if (!(wined3d_buffer = device->stateblock_state->streams[i].buffer))
140 continue;
141 if (!(buffer = wined3d_buffer_get_parent(wined3d_buffer)))
142 continue;
143 if (buffer->draw_buffer)
144 device->sysmem_vb |= 1u << i;
146 device->sysmem_ib = (wined3d_buffer = device->stateblock_state->index_buffer)
147 && (buffer = wined3d_buffer_get_parent(wined3d_buffer)) && buffer->draw_buffer;
148 device->auto_mipmaps = 0;
149 for (i = 0; i < D3D9_MAX_TEXTURE_UNITS; ++i)
151 if ((wined3d_texture = device->stateblock_state->textures[i])
152 && (texture = wined3d_texture_get_parent(wined3d_texture))
153 && texture->usage & D3DUSAGE_AUTOGENMIPMAP)
154 device->auto_mipmaps |= 1u << i;
155 else
156 device->auto_mipmaps &= ~(1u << i);
158 wined3d_mutex_unlock();
160 return D3D_OK;
164 static const struct IDirect3DStateBlock9Vtbl d3d9_stateblock_vtbl =
166 /* IUnknown */
167 d3d9_stateblock_QueryInterface,
168 d3d9_stateblock_AddRef,
169 d3d9_stateblock_Release,
170 /* IDirect3DStateBlock9 */
171 d3d9_stateblock_GetDevice,
172 d3d9_stateblock_Capture,
173 d3d9_stateblock_Apply,
176 HRESULT stateblock_init(struct d3d9_stateblock *stateblock, struct d3d9_device *device,
177 D3DSTATEBLOCKTYPE type, struct wined3d_stateblock *wined3d_stateblock)
179 HRESULT hr;
181 stateblock->IDirect3DStateBlock9_iface.lpVtbl = &d3d9_stateblock_vtbl;
182 stateblock->refcount = 1;
184 if (wined3d_stateblock)
186 stateblock->wined3d_stateblock = wined3d_stateblock;
188 else
190 wined3d_mutex_lock();
191 hr = wined3d_stateblock_create(device->wined3d_device, device->state,
192 (enum wined3d_stateblock_type)type, &stateblock->wined3d_stateblock);
193 wined3d_mutex_unlock();
194 if (FAILED(hr))
196 WARN("Failed to create wined3d stateblock, hr %#x.\n", hr);
197 return hr;
201 stateblock->parent_device = &device->IDirect3DDevice9Ex_iface;
202 IDirect3DDevice9Ex_AddRef(stateblock->parent_device);
204 return D3D_OK;