kernelbase: Check pointer validity in unsafe_ptr_from_HLOCAL.
[wine.git] / dlls / d3d9 / stateblock.c
blobc797ce54b49a26314f521502099f580d4e18591a
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_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 wined3d_texture *wined3d_texture;
117 struct wined3d_buffer *wined3d_buffer;
118 struct d3d9_vertexbuffer *buffer;
119 struct d3d9_texture *texture;
120 struct d3d9_device *device;
121 unsigned int i;
123 TRACE("iface %p.\n", iface);
125 wined3d_mutex_lock();
126 device = impl_from_IDirect3DDevice9Ex(stateblock->parent_device);
127 if (device->recording)
129 wined3d_mutex_unlock();
130 WARN("Trying to apply stateblock while recording, returning D3DERR_INVALIDCALL.\n");
131 return D3DERR_INVALIDCALL;
133 wined3d_stateblock_apply(stateblock->wined3d_stateblock, device->state);
134 device->sysmem_vb = 0;
135 for (i = 0; i < D3D9_MAX_STREAMS; ++i)
137 if (!(wined3d_buffer = device->stateblock_state->streams[i].buffer))
138 continue;
139 if (!(buffer = wined3d_buffer_get_parent(wined3d_buffer)))
140 continue;
141 if (buffer->draw_buffer)
142 device->sysmem_vb |= 1u << i;
144 device->sysmem_ib = (wined3d_buffer = device->stateblock_state->index_buffer)
145 && (buffer = wined3d_buffer_get_parent(wined3d_buffer)) && buffer->draw_buffer;
146 device->auto_mipmaps = 0;
147 for (i = 0; i < D3D9_MAX_TEXTURE_UNITS; ++i)
149 if ((wined3d_texture = device->stateblock_state->textures[i])
150 && (texture = wined3d_texture_get_parent(wined3d_texture))
151 && texture->usage & D3DUSAGE_AUTOGENMIPMAP)
152 device->auto_mipmaps |= 1u << i;
153 else
154 device->auto_mipmaps &= ~(1u << i);
156 wined3d_mutex_unlock();
158 return D3D_OK;
162 static const struct IDirect3DStateBlock9Vtbl d3d9_stateblock_vtbl =
164 /* IUnknown */
165 d3d9_stateblock_QueryInterface,
166 d3d9_stateblock_AddRef,
167 d3d9_stateblock_Release,
168 /* IDirect3DStateBlock9 */
169 d3d9_stateblock_GetDevice,
170 d3d9_stateblock_Capture,
171 d3d9_stateblock_Apply,
174 HRESULT stateblock_init(struct d3d9_stateblock *stateblock, struct d3d9_device *device,
175 D3DSTATEBLOCKTYPE type, struct wined3d_stateblock *wined3d_stateblock)
177 HRESULT hr;
179 stateblock->IDirect3DStateBlock9_iface.lpVtbl = &d3d9_stateblock_vtbl;
180 stateblock->refcount = 1;
182 if (wined3d_stateblock)
184 stateblock->wined3d_stateblock = wined3d_stateblock;
186 else
188 wined3d_mutex_lock();
189 hr = wined3d_stateblock_create(device->wined3d_device, device->state,
190 (enum wined3d_stateblock_type)type, &stateblock->wined3d_stateblock);
191 wined3d_mutex_unlock();
192 if (FAILED(hr))
194 WARN("Failed to create wined3d stateblock, hr %#x.\n", hr);
195 return hr;
199 stateblock->parent_device = &device->IDirect3DDevice9Ex_iface;
200 IDirect3DDevice9Ex_AddRef(stateblock->parent_device);
202 return D3D_OK;