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