rpcrt4: Don't try to bind to a null handle.
[wine] / dlls / d3d9 / shader.c
1 /*
2  * Copyright 2002-2003 Jason Edmeades
3  *                     Raphael Junqueira
4  *
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.
9  *
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.
14  *
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
18  */
19
20 #include "config.h"
21 #include "d3d9_private.h"
22
23 WINE_DEFAULT_DEBUG_CHANNEL(d3d9);
24
25 static HRESULT WINAPI d3d9_vertexshader_QueryInterface(IDirect3DVertexShader9 *iface, REFIID riid, void **object)
26 {
27     TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
28
29     if (IsEqualGUID(riid, &IID_IDirect3DVertexShader9)
30             || IsEqualGUID(riid, &IID_IUnknown))
31     {
32         IDirect3DVertexShader9_AddRef(iface);
33         *object = iface;
34         return S_OK;
35     }
36
37     WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(riid));
38
39     *object = NULL;
40     return E_NOINTERFACE;
41 }
42
43 static ULONG WINAPI d3d9_vertexshader_AddRef(IDirect3DVertexShader9 *iface)
44 {
45     IDirect3DVertexShader9Impl *shader = (IDirect3DVertexShader9Impl *)iface;
46     ULONG refcount = InterlockedIncrement(&shader->ref);
47
48     TRACE("%p increasing refcount to %u.\n", iface, refcount);
49
50     if (refcount == 1)
51     {
52         IDirect3DDevice9Ex_AddRef(shader->parentDevice);
53         wined3d_mutex_lock();
54         wined3d_shader_incref(shader->wined3d_shader);
55         wined3d_mutex_unlock();
56     }
57
58     return refcount;
59 }
60
61 static ULONG WINAPI d3d9_vertexshader_Release(IDirect3DVertexShader9 *iface)
62 {
63     IDirect3DVertexShader9Impl *shader = (IDirect3DVertexShader9Impl *)iface;
64     ULONG refcount = InterlockedDecrement(&shader->ref);
65
66     TRACE("%p decreasing refcount to %u.\n", iface, refcount);
67
68     if (!refcount)
69     {
70         IDirect3DDevice9Ex *device = shader->parentDevice;
71
72         wined3d_mutex_lock();
73         wined3d_shader_decref(shader->wined3d_shader);
74         wined3d_mutex_unlock();
75
76         /* Release the device last, as it may cause the device to be destroyed. */
77         IDirect3DDevice9Ex_Release(device);
78     }
79
80     return refcount;
81 }
82
83 static HRESULT WINAPI d3d9_vertexshader_GetDevice(IDirect3DVertexShader9 *iface, IDirect3DDevice9 **device)
84 {
85     TRACE("iface %p, device %p.\n", iface, device);
86
87     *device = (IDirect3DDevice9 *)((IDirect3DVertexShader9Impl *)iface)->parentDevice;
88     IDirect3DDevice9_AddRef(*device);
89
90     TRACE("Returning device %p.\n", *device);
91
92     return D3D_OK;
93 }
94
95 static HRESULT WINAPI d3d9_vertexshader_GetFunction(IDirect3DVertexShader9 *iface,
96         void *data, UINT *data_size)
97 {
98     HRESULT hr;
99
100     TRACE("iface %p, data %p, data_size %p.\n", iface, data, data_size);
101
102     wined3d_mutex_lock();
103     hr = wined3d_shader_get_byte_code(((IDirect3DVertexShader9Impl *)iface)->wined3d_shader, data, data_size);
104     wined3d_mutex_unlock();
105
106     return hr;
107 }
108
109 static const IDirect3DVertexShader9Vtbl d3d9_vertexshader_vtbl =
110 {
111     /* IUnknown */
112     d3d9_vertexshader_QueryInterface,
113     d3d9_vertexshader_AddRef,
114     d3d9_vertexshader_Release,
115     /* IDirect3DVertexShader9 */
116     d3d9_vertexshader_GetDevice,
117     d3d9_vertexshader_GetFunction,
118 };
119
120 static void STDMETHODCALLTYPE d3d9_vertexshader_wined3d_object_destroyed(void *parent)
121 {
122     HeapFree(GetProcessHeap(), 0, parent);
123 }
124
125 static const struct wined3d_parent_ops d3d9_vertexshader_wined3d_parent_ops =
126 {
127     d3d9_vertexshader_wined3d_object_destroyed,
128 };
129
130 HRESULT vertexshader_init(IDirect3DVertexShader9Impl *shader, IDirect3DDevice9Impl *device, const DWORD *byte_code)
131 {
132     HRESULT hr;
133
134     shader->ref = 1;
135     shader->lpVtbl = &d3d9_vertexshader_vtbl;
136
137     wined3d_mutex_lock();
138     hr = wined3d_shader_create_vs(device->wined3d_device, byte_code, NULL,
139             shader, &d3d9_vertexshader_wined3d_parent_ops, &shader->wined3d_shader);
140     wined3d_mutex_unlock();
141     if (FAILED(hr))
142     {
143         WARN("Failed to create wined3d vertex shader, hr %#x.\n", hr);
144         return hr;
145     }
146
147     shader->parentDevice = &device->IDirect3DDevice9Ex_iface;
148     IDirect3DDevice9Ex_AddRef(shader->parentDevice);
149
150     return D3D_OK;
151 }
152
153 static HRESULT WINAPI d3d9_pixelshader_QueryInterface(IDirect3DPixelShader9 *iface, REFIID riid, void **object)
154 {
155     TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
156
157     if (IsEqualGUID(riid, &IID_IDirect3DPixelShader9)
158             || IsEqualGUID(riid, &IID_IUnknown))
159     {
160         IDirect3DPixelShader9_AddRef(iface);
161         *object = iface;
162         return S_OK;
163     }
164
165     WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(riid));
166
167     *object = NULL;
168     return E_NOINTERFACE;
169 }
170
171 static ULONG WINAPI d3d9_pixelshader_AddRef(IDirect3DPixelShader9 *iface)
172 {
173     IDirect3DPixelShader9Impl *shader = (IDirect3DPixelShader9Impl *)iface;
174     ULONG refcount = InterlockedIncrement(&shader->ref);
175
176     TRACE("%p increasing refcount to %u.\n", iface, refcount);
177
178     if (refcount == 1)
179     {
180         IDirect3DDevice9Ex_AddRef(shader->parentDevice);
181         wined3d_mutex_lock();
182         wined3d_shader_incref(shader->wined3d_shader);
183         wined3d_mutex_unlock();
184     }
185
186     return refcount;
187 }
188
189 static ULONG WINAPI d3d9_pixelshader_Release(IDirect3DPixelShader9 *iface)
190 {
191     IDirect3DPixelShader9Impl *shader = (IDirect3DPixelShader9Impl *)iface;
192     ULONG refcount = InterlockedDecrement(&shader->ref);
193
194     TRACE("%p decreasing refcount to %u.\n", iface, refcount);
195
196     if (!refcount)
197     {
198         IDirect3DDevice9Ex *device = shader->parentDevice;
199
200         wined3d_mutex_lock();
201         wined3d_shader_decref(shader->wined3d_shader);
202         wined3d_mutex_unlock();
203
204         /* Release the device last, as it may cause the device to be destroyed. */
205         IDirect3DDevice9Ex_Release(device);
206     }
207
208     return refcount;
209 }
210
211 static HRESULT WINAPI d3d9_pixelshader_GetDevice(IDirect3DPixelShader9 *iface, IDirect3DDevice9 **device)
212 {
213     TRACE("iface %p, device %p.\n", iface, device);
214
215     *device = (IDirect3DDevice9 *)((IDirect3DPixelShader9Impl *)iface)->parentDevice;
216     IDirect3DDevice9_AddRef(*device);
217
218     TRACE("Returning device %p.\n", *device);
219
220     return D3D_OK;
221 }
222
223 static HRESULT WINAPI d3d9_pixelshader_GetFunction(IDirect3DPixelShader9 *iface, void *data, UINT *data_size)
224 {
225     HRESULT hr;
226
227     TRACE("iface %p, data %p, data_size %p.\n", iface, data, data_size);
228
229     wined3d_mutex_lock();
230     hr = wined3d_shader_get_byte_code(((IDirect3DPixelShader9Impl *)iface)->wined3d_shader, data, data_size);
231     wined3d_mutex_unlock();
232
233     return hr;
234 }
235
236 static const IDirect3DPixelShader9Vtbl d3d9_pixelshader_vtbl =
237 {
238     /* IUnknown */
239     d3d9_pixelshader_QueryInterface,
240     d3d9_pixelshader_AddRef,
241     d3d9_pixelshader_Release,
242     /* IDirect3DPixelShader9 */
243     d3d9_pixelshader_GetDevice,
244     d3d9_pixelshader_GetFunction,
245 };
246
247 static void STDMETHODCALLTYPE d3d9_pixelshader_wined3d_object_destroyed(void *parent)
248 {
249     HeapFree(GetProcessHeap(), 0, parent);
250 }
251
252 static const struct wined3d_parent_ops d3d9_pixelshader_wined3d_parent_ops =
253 {
254     d3d9_pixelshader_wined3d_object_destroyed,
255 };
256
257 HRESULT pixelshader_init(IDirect3DPixelShader9Impl *shader, IDirect3DDevice9Impl *device, const DWORD *byte_code)
258 {
259     HRESULT hr;
260
261     shader->ref = 1;
262     shader->lpVtbl = &d3d9_pixelshader_vtbl;
263
264     wined3d_mutex_lock();
265     hr = wined3d_shader_create_ps(device->wined3d_device, byte_code, NULL, shader,
266             &d3d9_pixelshader_wined3d_parent_ops, &shader->wined3d_shader);
267     wined3d_mutex_unlock();
268     if (FAILED(hr))
269     {
270         WARN("Failed to created wined3d pixel shader, hr %#x.\n", hr);
271         return hr;
272     }
273
274     shader->parentDevice = &device->IDirect3DDevice9Ex_iface;
275     IDirect3DDevice9Ex_AddRef(shader->parentDevice);
276
277     return D3D_OK;
278 }