winepulse: Use GetModuleFileName() instead of hardcoded module filename for registry...
[wine.git] / dlls / d3d9 / stateblock.c
blob76299877b89b27c6f05413bbe80a1478fb1dd4a6
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 %lu.\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 %lu.\n", iface, refcount);
67 if (!refcount)
69 wined3d_stateblock_decref(stateblock->wined3d_stateblock);
71 IDirect3DDevice9Ex_Release(stateblock->parent_device);
72 heap_free(stateblock);
75 return refcount;
78 static HRESULT WINAPI d3d9_stateblock_GetDevice(IDirect3DStateBlock9 *iface, IDirect3DDevice9 **device)
80 struct d3d9_stateblock *stateblock = impl_from_IDirect3DStateBlock9(iface);
82 TRACE("iface %p, device %p.\n", iface, device);
84 *device = (IDirect3DDevice9 *)stateblock->parent_device;
85 IDirect3DDevice9_AddRef(*device);
87 TRACE("Returning device %p.\n", *device);
89 return D3D_OK;
92 static HRESULT WINAPI d3d9_stateblock_Capture(IDirect3DStateBlock9 *iface)
94 struct d3d9_stateblock *stateblock = impl_from_IDirect3DStateBlock9(iface);
95 struct d3d9_device *device;
97 TRACE("iface %p.\n", iface);
99 wined3d_mutex_lock();
100 device = impl_from_IDirect3DDevice9Ex(stateblock->parent_device);
101 if (device->recording)
103 wined3d_mutex_unlock();
104 WARN("Trying to capture stateblock while recording, returning D3DERR_INVALIDCALL.\n");
105 return D3DERR_INVALIDCALL;
107 wined3d_stateblock_capture(stateblock->wined3d_stateblock, device->state);
108 wined3d_mutex_unlock();
110 return D3D_OK;
113 static HRESULT WINAPI d3d9_stateblock_Apply(IDirect3DStateBlock9 *iface)
115 struct d3d9_stateblock *stateblock = impl_from_IDirect3DStateBlock9(iface);
116 struct d3d9_vertexbuffer *vertex_buffer;
117 struct wined3d_texture *wined3d_texture;
118 struct d3d9_indexbuffer *index_buffer;
119 struct wined3d_buffer *wined3d_buffer;
120 struct d3d9_texture *texture;
121 struct d3d9_device *device;
122 unsigned int i;
124 TRACE("iface %p.\n", iface);
126 wined3d_mutex_lock();
127 device = impl_from_IDirect3DDevice9Ex(stateblock->parent_device);
128 if (device->recording)
130 wined3d_mutex_unlock();
131 WARN("Trying to apply stateblock while recording, returning D3DERR_INVALIDCALL.\n");
132 return D3DERR_INVALIDCALL;
134 wined3d_stateblock_apply(stateblock->wined3d_stateblock, device->state);
135 device->sysmem_vb = 0;
136 for (i = 0; i < D3D9_MAX_STREAMS; ++i)
138 if (!(wined3d_buffer = device->stateblock_state->streams[i].buffer))
139 continue;
140 if (!(vertex_buffer = wined3d_buffer_get_parent(wined3d_buffer)))
141 continue;
142 if (vertex_buffer->draw_buffer)
143 device->sysmem_vb |= 1u << i;
145 device->sysmem_ib = (wined3d_buffer = device->stateblock_state->index_buffer)
146 && (index_buffer = wined3d_buffer_get_parent(wined3d_buffer)) && index_buffer->sysmem;
147 device->auto_mipmaps = 0;
148 for (i = 0; i < D3D9_MAX_TEXTURE_UNITS; ++i)
150 if ((wined3d_texture = device->stateblock_state->textures[i])
151 && (texture = wined3d_texture_get_parent(wined3d_texture))
152 && texture->usage & D3DUSAGE_AUTOGENMIPMAP)
153 device->auto_mipmaps |= 1u << i;
154 else
155 device->auto_mipmaps &= ~(1u << i);
157 wined3d_mutex_unlock();
159 return D3D_OK;
163 static const struct IDirect3DStateBlock9Vtbl d3d9_stateblock_vtbl =
165 /* IUnknown */
166 d3d9_stateblock_QueryInterface,
167 d3d9_stateblock_AddRef,
168 d3d9_stateblock_Release,
169 /* IDirect3DStateBlock9 */
170 d3d9_stateblock_GetDevice,
171 d3d9_stateblock_Capture,
172 d3d9_stateblock_Apply,
175 HRESULT stateblock_init(struct d3d9_stateblock *stateblock, struct d3d9_device *device,
176 D3DSTATEBLOCKTYPE type, struct wined3d_stateblock *wined3d_stateblock)
178 HRESULT hr;
180 stateblock->IDirect3DStateBlock9_iface.lpVtbl = &d3d9_stateblock_vtbl;
181 stateblock->refcount = 1;
183 if (wined3d_stateblock)
185 stateblock->wined3d_stateblock = wined3d_stateblock;
187 else
189 wined3d_mutex_lock();
190 hr = wined3d_stateblock_create(device->wined3d_device, device->state,
191 (enum wined3d_stateblock_type)type, &stateblock->wined3d_stateblock);
192 wined3d_mutex_unlock();
193 if (FAILED(hr))
195 WARN("Failed to create wined3d stateblock, hr %#lx.\n", hr);
196 return hr;
200 stateblock->parent_device = &device->IDirect3DDevice9Ex_iface;
201 IDirect3DDevice9Ex_AddRef(stateblock->parent_device);
203 return D3D_OK;