[AMSTREAM] We don't need to define WIDL_C_INLINE_WRAPPERS here anymore.
[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 struct wined3d_shader_desc desc;
137 HRESULT hr;
138
139 shader->refcount = 1;
140 shader->IDirect3DVertexShader9_iface.lpVtbl = &d3d9_vertexshader_vtbl;
141
142 desc.byte_code = byte_code;
143 desc.byte_code_size = ~(size_t)0;
144 desc.format = WINED3D_SHADER_BYTE_CODE_FORMAT_SM1;
145 desc.input_signature.element_count = 0;
146 desc.output_signature.element_count = 0;
147 desc.patch_constant_signature.element_count = 0;
148 desc.max_version = 3;
149
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))
155 {
156 WARN("Failed to create wined3d vertex shader, hr %#x.\n", hr);
157 return hr;
158 }
159
160 shader->parent_device = &device->IDirect3DDevice9Ex_iface;
161 IDirect3DDevice9Ex_AddRef(shader->parent_device);
162
163 return D3D_OK;
164 }
165
166 struct d3d9_vertexshader *unsafe_impl_from_IDirect3DVertexShader9(IDirect3DVertexShader9 *iface)
167 {
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);
172
173 return impl_from_IDirect3DVertexShader9(iface);
174 }
175
176 static inline struct d3d9_pixelshader *impl_from_IDirect3DPixelShader9(IDirect3DPixelShader9 *iface)
177 {
178 return CONTAINING_RECORD(iface, struct d3d9_pixelshader, IDirect3DPixelShader9_iface);
179 }
180
181 static HRESULT WINAPI d3d9_pixelshader_QueryInterface(IDirect3DPixelShader9 *iface, REFIID riid, void **out)
182 {
183 TRACE("iface %p, riid %s, out %p.\n", iface, debugstr_guid(riid), out);
184
185 if (IsEqualGUID(riid, &IID_IDirect3DPixelShader9)
186 || IsEqualGUID(riid, &IID_IUnknown))
187 {
188 IDirect3DPixelShader9_AddRef(iface);
189 *out = iface;
190 return S_OK;
191 }
192
193 WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(riid));
194
195 *out = NULL;
196 return E_NOINTERFACE;
197 }
198
199 static ULONG WINAPI d3d9_pixelshader_AddRef(IDirect3DPixelShader9 *iface)
200 {
201 struct d3d9_pixelshader *shader = impl_from_IDirect3DPixelShader9(iface);
202 ULONG refcount = InterlockedIncrement(&shader->refcount);
203
204 TRACE("%p increasing refcount to %u.\n", iface, refcount);
205
206 if (refcount == 1)
207 {
208 IDirect3DDevice9Ex_AddRef(shader->parent_device);
209 wined3d_mutex_lock();
210 wined3d_shader_incref(shader->wined3d_shader);
211 wined3d_mutex_unlock();
212 }
213
214 return refcount;
215 }
216
217 static ULONG WINAPI d3d9_pixelshader_Release(IDirect3DPixelShader9 *iface)
218 {
219 struct d3d9_pixelshader *shader = impl_from_IDirect3DPixelShader9(iface);
220 ULONG refcount = InterlockedDecrement(&shader->refcount);
221
222 TRACE("%p decreasing refcount to %u.\n", iface, refcount);
223
224 if (!refcount)
225 {
226 IDirect3DDevice9Ex *device = shader->parent_device;
227
228 wined3d_mutex_lock();
229 wined3d_shader_decref(shader->wined3d_shader);
230 wined3d_mutex_unlock();
231
232 /* Release the device last, as it may cause the device to be destroyed. */
233 IDirect3DDevice9Ex_Release(device);
234 }
235
236 return refcount;
237 }
238
239 static HRESULT WINAPI d3d9_pixelshader_GetDevice(IDirect3DPixelShader9 *iface, IDirect3DDevice9 **device)
240 {
241 struct d3d9_pixelshader *shader = impl_from_IDirect3DPixelShader9(iface);
242
243 TRACE("iface %p, device %p.\n", iface, device);
244
245 *device = (IDirect3DDevice9 *)shader->parent_device;
246 IDirect3DDevice9_AddRef(*device);
247
248 TRACE("Returning device %p.\n", *device);
249
250 return D3D_OK;
251 }
252
253 static HRESULT WINAPI d3d9_pixelshader_GetFunction(IDirect3DPixelShader9 *iface, void *data, UINT *data_size)
254 {
255 struct d3d9_pixelshader *shader = impl_from_IDirect3DPixelShader9(iface);
256 HRESULT hr;
257
258 TRACE("iface %p, data %p, data_size %p.\n", iface, data, data_size);
259
260 wined3d_mutex_lock();
261 hr = wined3d_shader_get_byte_code(shader->wined3d_shader, data, data_size);
262 wined3d_mutex_unlock();
263
264 return hr;
265 }
266
267 static const IDirect3DPixelShader9Vtbl d3d9_pixelshader_vtbl =
268 {
269 /* IUnknown */
270 d3d9_pixelshader_QueryInterface,
271 d3d9_pixelshader_AddRef,
272 d3d9_pixelshader_Release,
273 /* IDirect3DPixelShader9 */
274 d3d9_pixelshader_GetDevice,
275 d3d9_pixelshader_GetFunction,
276 };
277
278 static void STDMETHODCALLTYPE d3d9_pixelshader_wined3d_object_destroyed(void *parent)
279 {
280 HeapFree(GetProcessHeap(), 0, parent);
281 }
282
283 static const struct wined3d_parent_ops d3d9_pixelshader_wined3d_parent_ops =
284 {
285 d3d9_pixelshader_wined3d_object_destroyed,
286 };
287
288 HRESULT pixelshader_init(struct d3d9_pixelshader *shader, struct d3d9_device *device, const DWORD *byte_code)
289 {
290 struct wined3d_shader_desc desc;
291 HRESULT hr;
292
293 shader->refcount = 1;
294 shader->IDirect3DPixelShader9_iface.lpVtbl = &d3d9_pixelshader_vtbl;
295
296 desc.byte_code = byte_code;
297 desc.byte_code_size = ~(size_t)0;
298 desc.format = WINED3D_SHADER_BYTE_CODE_FORMAT_SM1;
299 desc.input_signature.element_count = 0;
300 desc.output_signature.element_count = 0;
301 desc.patch_constant_signature.element_count = 0;
302 desc.max_version = 3;
303
304 wined3d_mutex_lock();
305 hr = wined3d_shader_create_ps(device->wined3d_device, &desc, shader,
306 &d3d9_pixelshader_wined3d_parent_ops, &shader->wined3d_shader);
307 wined3d_mutex_unlock();
308 if (FAILED(hr))
309 {
310 WARN("Failed to created wined3d pixel shader, hr %#x.\n", hr);
311 return hr;
312 }
313
314 shader->parent_device = &device->IDirect3DDevice9Ex_iface;
315 IDirect3DDevice9Ex_AddRef(shader->parent_device);
316
317 return D3D_OK;
318 }
319
320 struct d3d9_pixelshader *unsafe_impl_from_IDirect3DPixelShader9(IDirect3DPixelShader9 *iface)
321 {
322 if (!iface)
323 return NULL;
324 if (iface->lpVtbl != &d3d9_pixelshader_vtbl)
325 WARN("Pixel shader %p with the wrong vtbl %p\n", iface, iface->lpVtbl);
326
327 return impl_from_IDirect3DPixelShader9(iface);
328 }