wined3d: Clear the renderbuffer IDs on unload.
[wine.git] / dlls / d3d9 / shader.c
blob9430afd63daa4be4d54dfed87f3b5c1780e09b02
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 struct d3d9_vertexshader *impl_from_IDirect3DVertexShader9(IDirect3DVertexShader9 *iface)
27 return CONTAINING_RECORD(iface, struct d3d9_vertexshader, IDirect3DVertexShader9_iface);
30 static HRESULT WINAPI d3d9_vertexshader_QueryInterface(IDirect3DVertexShader9 *iface, REFIID riid, void **out)
32 TRACE("iface %p, riid %s, out %p.\n", iface, debugstr_guid(riid), out);
34 if (IsEqualGUID(riid, &IID_IDirect3DVertexShader9)
35 || IsEqualGUID(riid, &IID_IUnknown))
37 IDirect3DVertexShader9_AddRef(iface);
38 *out = iface;
39 return S_OK;
42 WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(riid));
44 *out = NULL;
45 return E_NOINTERFACE;
48 static ULONG WINAPI d3d9_vertexshader_AddRef(IDirect3DVertexShader9 *iface)
50 struct d3d9_vertexshader *shader = impl_from_IDirect3DVertexShader9(iface);
51 ULONG refcount = InterlockedIncrement(&shader->refcount);
53 TRACE("%p increasing refcount to %u.\n", iface, refcount);
55 if (refcount == 1)
57 IDirect3DDevice9Ex_AddRef(shader->parent_device);
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 struct d3d9_vertexshader *shader = impl_from_IDirect3DVertexShader9(iface);
69 ULONG refcount = InterlockedDecrement(&shader->refcount);
71 TRACE("%p decreasing refcount to %u.\n", iface, refcount);
73 if (!refcount)
75 IDirect3DDevice9Ex *device = shader->parent_device;
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 struct d3d9_vertexshader *shader = impl_from_IDirect3DVertexShader9(iface);
92 TRACE("iface %p, device %p.\n", iface, device);
94 *device = (IDirect3DDevice9 *)shader->parent_device;
95 IDirect3DDevice9_AddRef(*device);
97 TRACE("Returning device %p.\n", *device);
99 return D3D_OK;
102 static HRESULT WINAPI d3d9_vertexshader_GetFunction(IDirect3DVertexShader9 *iface, void *data, UINT *data_size)
104 struct d3d9_vertexshader *shader = impl_from_IDirect3DVertexShader9(iface);
105 HRESULT hr;
107 TRACE("iface %p, data %p, data_size %p.\n", iface, data, data_size);
109 wined3d_mutex_lock();
110 hr = wined3d_shader_get_byte_code(shader->wined3d_shader, data, data_size);
111 wined3d_mutex_unlock();
113 return hr;
116 static const IDirect3DVertexShader9Vtbl d3d9_vertexshader_vtbl =
118 /* IUnknown */
119 d3d9_vertexshader_QueryInterface,
120 d3d9_vertexshader_AddRef,
121 d3d9_vertexshader_Release,
122 /* IDirect3DVertexShader9 */
123 d3d9_vertexshader_GetDevice,
124 d3d9_vertexshader_GetFunction,
127 static void STDMETHODCALLTYPE d3d9_vertexshader_wined3d_object_destroyed(void *parent)
129 HeapFree(GetProcessHeap(), 0, parent);
132 static const struct wined3d_parent_ops d3d9_vertexshader_wined3d_parent_ops =
134 d3d9_vertexshader_wined3d_object_destroyed,
137 HRESULT vertexshader_init(struct d3d9_vertexshader *shader, struct d3d9_device *device, const DWORD *byte_code)
139 struct wined3d_shader_desc desc;
140 HRESULT hr;
142 shader->refcount = 1;
143 shader->IDirect3DVertexShader9_iface.lpVtbl = &d3d9_vertexshader_vtbl;
145 desc.byte_code = byte_code;
146 desc.input_signature = NULL;
147 desc.output_signature = NULL;
148 desc.max_version = 3;
150 wined3d_mutex_lock();
151 hr = wined3d_shader_create_vs(device->wined3d_device, &desc, shader,
152 &d3d9_vertexshader_wined3d_parent_ops, &shader->wined3d_shader);
153 wined3d_mutex_unlock();
154 if (FAILED(hr))
156 WARN("Failed to create wined3d vertex shader, hr %#x.\n", hr);
157 return hr;
160 shader->parent_device = &device->IDirect3DDevice9Ex_iface;
161 IDirect3DDevice9Ex_AddRef(shader->parent_device);
163 return D3D_OK;
166 struct d3d9_vertexshader *unsafe_impl_from_IDirect3DVertexShader9(IDirect3DVertexShader9 *iface)
168 if (!iface)
169 return NULL;
170 if (iface->lpVtbl != &d3d9_vertexshader_vtbl)
171 WARN("Vertex shader %p with the wrong vtbl %p\n", iface, iface->lpVtbl);
173 return impl_from_IDirect3DVertexShader9(iface);
176 static inline struct d3d9_pixelshader *impl_from_IDirect3DPixelShader9(IDirect3DPixelShader9 *iface)
178 return CONTAINING_RECORD(iface, struct d3d9_pixelshader, IDirect3DPixelShader9_iface);
181 static HRESULT WINAPI d3d9_pixelshader_QueryInterface(IDirect3DPixelShader9 *iface, REFIID riid, void **out)
183 TRACE("iface %p, riid %s, out %p.\n", iface, debugstr_guid(riid), out);
185 if (IsEqualGUID(riid, &IID_IDirect3DPixelShader9)
186 || IsEqualGUID(riid, &IID_IUnknown))
188 IDirect3DPixelShader9_AddRef(iface);
189 *out = iface;
190 return S_OK;
193 WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(riid));
195 *out = NULL;
196 return E_NOINTERFACE;
199 static ULONG WINAPI d3d9_pixelshader_AddRef(IDirect3DPixelShader9 *iface)
201 struct d3d9_pixelshader *shader = impl_from_IDirect3DPixelShader9(iface);
202 ULONG refcount = InterlockedIncrement(&shader->refcount);
204 TRACE("%p increasing refcount to %u.\n", iface, refcount);
206 if (refcount == 1)
208 IDirect3DDevice9Ex_AddRef(shader->parent_device);
209 wined3d_mutex_lock();
210 wined3d_shader_incref(shader->wined3d_shader);
211 wined3d_mutex_unlock();
214 return refcount;
217 static ULONG WINAPI d3d9_pixelshader_Release(IDirect3DPixelShader9 *iface)
219 struct d3d9_pixelshader *shader = impl_from_IDirect3DPixelShader9(iface);
220 ULONG refcount = InterlockedDecrement(&shader->refcount);
222 TRACE("%p decreasing refcount to %u.\n", iface, refcount);
224 if (!refcount)
226 IDirect3DDevice9Ex *device = shader->parent_device;
228 wined3d_mutex_lock();
229 wined3d_shader_decref(shader->wined3d_shader);
230 wined3d_mutex_unlock();
232 /* Release the device last, as it may cause the device to be destroyed. */
233 IDirect3DDevice9Ex_Release(device);
236 return refcount;
239 static HRESULT WINAPI d3d9_pixelshader_GetDevice(IDirect3DPixelShader9 *iface, IDirect3DDevice9 **device)
241 struct d3d9_pixelshader *shader = impl_from_IDirect3DPixelShader9(iface);
243 TRACE("iface %p, device %p.\n", iface, device);
245 *device = (IDirect3DDevice9 *)shader->parent_device;
246 IDirect3DDevice9_AddRef(*device);
248 TRACE("Returning device %p.\n", *device);
250 return D3D_OK;
253 static HRESULT WINAPI d3d9_pixelshader_GetFunction(IDirect3DPixelShader9 *iface, void *data, UINT *data_size)
255 struct d3d9_pixelshader *shader = impl_from_IDirect3DPixelShader9(iface);
256 HRESULT hr;
258 TRACE("iface %p, data %p, data_size %p.\n", iface, data, data_size);
260 wined3d_mutex_lock();
261 hr = wined3d_shader_get_byte_code(shader->wined3d_shader, data, data_size);
262 wined3d_mutex_unlock();
264 return hr;
267 static const IDirect3DPixelShader9Vtbl d3d9_pixelshader_vtbl =
269 /* IUnknown */
270 d3d9_pixelshader_QueryInterface,
271 d3d9_pixelshader_AddRef,
272 d3d9_pixelshader_Release,
273 /* IDirect3DPixelShader9 */
274 d3d9_pixelshader_GetDevice,
275 d3d9_pixelshader_GetFunction,
278 static void STDMETHODCALLTYPE d3d9_pixelshader_wined3d_object_destroyed(void *parent)
280 HeapFree(GetProcessHeap(), 0, parent);
283 static const struct wined3d_parent_ops d3d9_pixelshader_wined3d_parent_ops =
285 d3d9_pixelshader_wined3d_object_destroyed,
288 HRESULT pixelshader_init(struct d3d9_pixelshader *shader, struct d3d9_device *device, const DWORD *byte_code)
290 struct wined3d_shader_desc desc;
291 HRESULT hr;
293 shader->refcount = 1;
294 shader->IDirect3DPixelShader9_iface.lpVtbl = &d3d9_pixelshader_vtbl;
296 desc.byte_code = byte_code;
297 desc.input_signature = NULL;
298 desc.output_signature = NULL;
299 desc.max_version = 3;
301 wined3d_mutex_lock();
302 hr = wined3d_shader_create_ps(device->wined3d_device, &desc, shader,
303 &d3d9_pixelshader_wined3d_parent_ops, &shader->wined3d_shader);
304 wined3d_mutex_unlock();
305 if (FAILED(hr))
307 WARN("Failed to created wined3d pixel shader, hr %#x.\n", hr);
308 return hr;
311 shader->parent_device = &device->IDirect3DDevice9Ex_iface;
312 IDirect3DDevice9Ex_AddRef(shader->parent_device);
314 return D3D_OK;
317 struct d3d9_pixelshader *unsafe_impl_from_IDirect3DPixelShader9(IDirect3DPixelShader9 *iface)
319 if (!iface)
320 return NULL;
321 if (iface->lpVtbl != &d3d9_pixelshader_vtbl)
322 WARN("Pixel shader %p with the wrong vtbl %p\n", iface, iface->lpVtbl);
324 return impl_from_IDirect3DPixelShader9(iface);