d3d9: Get rid of IDirect3DDevice9Impl.
[wine/multimedia.git] / dlls / d3d9 / shader.c
blobb4d8a3d5850587eccdd867f65aeeb9e86cc6ef0e
1 /*
2 * Copyright 2002-2003 Jason Edmeades
3 * Raphael Junqueira
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 #include "config.h"
21 #include "d3d9_private.h"
23 WINE_DEFAULT_DEBUG_CHANNEL(d3d9);
25 static inline IDirect3DVertexShader9Impl *impl_from_IDirect3DVertexShader9(IDirect3DVertexShader9 *iface)
27 return CONTAINING_RECORD(iface, IDirect3DVertexShader9Impl, IDirect3DVertexShader9_iface);
30 static HRESULT WINAPI d3d9_vertexshader_QueryInterface(IDirect3DVertexShader9 *iface, REFIID riid, void **object)
32 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
34 if (IsEqualGUID(riid, &IID_IDirect3DVertexShader9)
35 || IsEqualGUID(riid, &IID_IUnknown))
37 IDirect3DVertexShader9_AddRef(iface);
38 *object = iface;
39 return S_OK;
42 WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(riid));
44 *object = NULL;
45 return E_NOINTERFACE;
48 static ULONG WINAPI d3d9_vertexshader_AddRef(IDirect3DVertexShader9 *iface)
50 IDirect3DVertexShader9Impl *shader = impl_from_IDirect3DVertexShader9(iface);
51 ULONG refcount = InterlockedIncrement(&shader->ref);
53 TRACE("%p increasing refcount to %u.\n", iface, refcount);
55 if (refcount == 1)
57 IDirect3DDevice9Ex_AddRef(shader->parentDevice);
58 wined3d_mutex_lock();
59 wined3d_shader_incref(shader->wined3d_shader);
60 wined3d_mutex_unlock();
63 return refcount;
66 static ULONG WINAPI d3d9_vertexshader_Release(IDirect3DVertexShader9 *iface)
68 IDirect3DVertexShader9Impl *shader = impl_from_IDirect3DVertexShader9(iface);
69 ULONG refcount = InterlockedDecrement(&shader->ref);
71 TRACE("%p decreasing refcount to %u.\n", iface, refcount);
73 if (!refcount)
75 IDirect3DDevice9Ex *device = shader->parentDevice;
77 wined3d_mutex_lock();
78 wined3d_shader_decref(shader->wined3d_shader);
79 wined3d_mutex_unlock();
81 /* Release the device last, as it may cause the device to be destroyed. */
82 IDirect3DDevice9Ex_Release(device);
85 return refcount;
88 static HRESULT WINAPI d3d9_vertexshader_GetDevice(IDirect3DVertexShader9 *iface, IDirect3DDevice9 **device)
90 IDirect3DVertexShader9Impl *shader = impl_from_IDirect3DVertexShader9(iface);
92 TRACE("iface %p, device %p.\n", iface, device);
94 *device = (IDirect3DDevice9 *)shader->parentDevice;
95 IDirect3DDevice9_AddRef(*device);
97 TRACE("Returning device %p.\n", *device);
99 return D3D_OK;
102 static HRESULT WINAPI d3d9_vertexshader_GetFunction(IDirect3DVertexShader9 *iface,
103 void *data, UINT *data_size)
105 IDirect3DVertexShader9Impl *shader = impl_from_IDirect3DVertexShader9(iface);
106 HRESULT hr;
108 TRACE("iface %p, data %p, data_size %p.\n", iface, data, data_size);
110 wined3d_mutex_lock();
111 hr = wined3d_shader_get_byte_code(shader->wined3d_shader, data, data_size);
112 wined3d_mutex_unlock();
114 return hr;
117 static const IDirect3DVertexShader9Vtbl d3d9_vertexshader_vtbl =
119 /* IUnknown */
120 d3d9_vertexshader_QueryInterface,
121 d3d9_vertexshader_AddRef,
122 d3d9_vertexshader_Release,
123 /* IDirect3DVertexShader9 */
124 d3d9_vertexshader_GetDevice,
125 d3d9_vertexshader_GetFunction,
128 static void STDMETHODCALLTYPE d3d9_vertexshader_wined3d_object_destroyed(void *parent)
130 HeapFree(GetProcessHeap(), 0, parent);
133 static const struct wined3d_parent_ops d3d9_vertexshader_wined3d_parent_ops =
135 d3d9_vertexshader_wined3d_object_destroyed,
138 HRESULT vertexshader_init(IDirect3DVertexShader9Impl *shader, struct d3d9_device *device, const DWORD *byte_code)
140 HRESULT hr;
142 shader->ref = 1;
143 shader->IDirect3DVertexShader9_iface.lpVtbl = &d3d9_vertexshader_vtbl;
145 wined3d_mutex_lock();
146 hr = wined3d_shader_create_vs(device->wined3d_device, byte_code, NULL,
147 shader, &d3d9_vertexshader_wined3d_parent_ops, &shader->wined3d_shader, 3);
148 wined3d_mutex_unlock();
149 if (FAILED(hr))
151 WARN("Failed to create wined3d vertex shader, hr %#x.\n", hr);
152 return hr;
155 shader->parentDevice = &device->IDirect3DDevice9Ex_iface;
156 IDirect3DDevice9Ex_AddRef(shader->parentDevice);
158 return D3D_OK;
161 IDirect3DVertexShader9Impl *unsafe_impl_from_IDirect3DVertexShader9(IDirect3DVertexShader9 *iface)
163 if (!iface)
164 return NULL;
165 assert(iface->lpVtbl == &d3d9_vertexshader_vtbl);
167 return impl_from_IDirect3DVertexShader9(iface);
170 static inline IDirect3DPixelShader9Impl *impl_from_IDirect3DPixelShader9(IDirect3DPixelShader9 *iface)
172 return CONTAINING_RECORD(iface, IDirect3DPixelShader9Impl, IDirect3DPixelShader9_iface);
175 static HRESULT WINAPI d3d9_pixelshader_QueryInterface(IDirect3DPixelShader9 *iface, REFIID riid, void **object)
177 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
179 if (IsEqualGUID(riid, &IID_IDirect3DPixelShader9)
180 || IsEqualGUID(riid, &IID_IUnknown))
182 IDirect3DPixelShader9_AddRef(iface);
183 *object = iface;
184 return S_OK;
187 WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(riid));
189 *object = NULL;
190 return E_NOINTERFACE;
193 static ULONG WINAPI d3d9_pixelshader_AddRef(IDirect3DPixelShader9 *iface)
195 IDirect3DPixelShader9Impl *shader = impl_from_IDirect3DPixelShader9(iface);
196 ULONG refcount = InterlockedIncrement(&shader->ref);
198 TRACE("%p increasing refcount to %u.\n", iface, refcount);
200 if (refcount == 1)
202 IDirect3DDevice9Ex_AddRef(shader->parentDevice);
203 wined3d_mutex_lock();
204 wined3d_shader_incref(shader->wined3d_shader);
205 wined3d_mutex_unlock();
208 return refcount;
211 static ULONG WINAPI d3d9_pixelshader_Release(IDirect3DPixelShader9 *iface)
213 IDirect3DPixelShader9Impl *shader = impl_from_IDirect3DPixelShader9(iface);
214 ULONG refcount = InterlockedDecrement(&shader->ref);
216 TRACE("%p decreasing refcount to %u.\n", iface, refcount);
218 if (!refcount)
220 IDirect3DDevice9Ex *device = shader->parentDevice;
222 wined3d_mutex_lock();
223 wined3d_shader_decref(shader->wined3d_shader);
224 wined3d_mutex_unlock();
226 /* Release the device last, as it may cause the device to be destroyed. */
227 IDirect3DDevice9Ex_Release(device);
230 return refcount;
233 static HRESULT WINAPI d3d9_pixelshader_GetDevice(IDirect3DPixelShader9 *iface,
234 IDirect3DDevice9 **device)
236 IDirect3DPixelShader9Impl *shader = impl_from_IDirect3DPixelShader9(iface);
238 TRACE("iface %p, device %p.\n", iface, device);
240 *device = (IDirect3DDevice9 *)shader->parentDevice;
241 IDirect3DDevice9_AddRef(*device);
243 TRACE("Returning device %p.\n", *device);
245 return D3D_OK;
248 static HRESULT WINAPI d3d9_pixelshader_GetFunction(IDirect3DPixelShader9 *iface, void *data,
249 UINT *data_size)
251 IDirect3DPixelShader9Impl *shader = impl_from_IDirect3DPixelShader9(iface);
252 HRESULT hr;
254 TRACE("iface %p, data %p, data_size %p.\n", iface, data, data_size);
256 wined3d_mutex_lock();
257 hr = wined3d_shader_get_byte_code(shader->wined3d_shader, data, data_size);
258 wined3d_mutex_unlock();
260 return hr;
263 static const IDirect3DPixelShader9Vtbl d3d9_pixelshader_vtbl =
265 /* IUnknown */
266 d3d9_pixelshader_QueryInterface,
267 d3d9_pixelshader_AddRef,
268 d3d9_pixelshader_Release,
269 /* IDirect3DPixelShader9 */
270 d3d9_pixelshader_GetDevice,
271 d3d9_pixelshader_GetFunction,
274 static void STDMETHODCALLTYPE d3d9_pixelshader_wined3d_object_destroyed(void *parent)
276 HeapFree(GetProcessHeap(), 0, parent);
279 static const struct wined3d_parent_ops d3d9_pixelshader_wined3d_parent_ops =
281 d3d9_pixelshader_wined3d_object_destroyed,
284 HRESULT pixelshader_init(IDirect3DPixelShader9Impl *shader, struct d3d9_device *device, const DWORD *byte_code)
286 HRESULT hr;
288 shader->ref = 1;
289 shader->IDirect3DPixelShader9_iface.lpVtbl = &d3d9_pixelshader_vtbl;
291 wined3d_mutex_lock();
292 hr = wined3d_shader_create_ps(device->wined3d_device, byte_code, NULL, shader,
293 &d3d9_pixelshader_wined3d_parent_ops, &shader->wined3d_shader, 3);
294 wined3d_mutex_unlock();
295 if (FAILED(hr))
297 WARN("Failed to created wined3d pixel shader, hr %#x.\n", hr);
298 return hr;
301 shader->parentDevice = &device->IDirect3DDevice9Ex_iface;
302 IDirect3DDevice9Ex_AddRef(shader->parentDevice);
304 return D3D_OK;
307 IDirect3DPixelShader9Impl *unsafe_impl_from_IDirect3DPixelShader9(IDirect3DPixelShader9 *iface)
309 if (!iface)
310 return NULL;
311 assert(iface->lpVtbl == &d3d9_pixelshader_vtbl);
313 return impl_from_IDirect3DPixelShader9(iface);