quartz: Handle a crash and race condition that occurs on directsound renderer input...
[wine/multimedia.git] / dlls / d3d10core / shader.c
blobc6803ed375221629338439ddc788fa8cee15eaa9
1 /*
2 * Copyright 2009 Henri Verbeet for CodeWeavers
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 #include "config.h"
21 #include "wine/port.h"
23 #include "d3d10core_private.h"
25 WINE_DEFAULT_DEBUG_CHANNEL(d3d10core);
27 static HRESULT shdr_handler(const char *data, DWORD data_size, DWORD tag, void *ctx)
29 struct d3d10_shader_info *shader_info = ctx;
30 HRESULT hr;
32 switch(tag)
34 case TAG_OSGN:
35 hr = shader_parse_signature(data, data_size, shader_info->output_signature);
36 if (FAILED(hr)) return hr;
37 break;
39 case TAG_SHDR:
40 shader_info->shader_code = (const DWORD *)data;
41 break;
43 default:
44 FIXME("Unhandled chunk %s\n", debugstr_an((const char *)&tag, 4));
45 break;
48 return S_OK;
51 static HRESULT shader_extract_from_dxbc(const void *dxbc, SIZE_T dxbc_length, struct d3d10_shader_info *shader_info)
53 HRESULT hr;
55 shader_info->shader_code = NULL;
56 memset(shader_info->output_signature, 0, sizeof(*shader_info->output_signature));
58 hr = parse_dxbc(dxbc, dxbc_length, shdr_handler, shader_info);
59 if (!shader_info->shader_code) hr = E_INVALIDARG;
61 if (FAILED(hr))
63 ERR("Failed to parse shader, hr %#x\n", hr);
64 shader_free_signature(shader_info->output_signature);
67 return hr;
70 HRESULT shader_parse_signature(const char *data, DWORD data_size, struct wined3d_shader_signature *s)
72 struct wined3d_shader_signature_element *e;
73 unsigned int string_data_offset;
74 unsigned int string_data_size;
75 const char *ptr = data;
76 char *string_data;
77 unsigned int i;
78 DWORD count;
80 read_dword(&ptr, &count);
81 TRACE("%u elements\n", count);
83 skip_dword_unknown(&ptr, 1);
85 e = HeapAlloc(GetProcessHeap(), 0, count * sizeof(*e));
86 if (!e)
88 ERR("Failed to allocate input signature memory.\n");
89 return E_OUTOFMEMORY;
92 /* 2 DWORDs for the header, 6 for each element. */
93 string_data_offset = 2 * sizeof(DWORD) + count * 6 * sizeof(DWORD);
94 string_data_size = data_size - string_data_offset;
95 string_data = HeapAlloc(GetProcessHeap(), 0, string_data_size);
96 if (!string_data)
98 ERR("Failed to allocate string data memory.\n");
99 HeapFree(GetProcessHeap(), 0, e);
100 return E_OUTOFMEMORY;
102 memcpy(string_data, data + string_data_offset, string_data_size);
104 for (i = 0; i < count; ++i)
106 UINT name_offset;
108 read_dword(&ptr, &name_offset);
109 e[i].semantic_name = string_data + (name_offset - string_data_offset);
110 read_dword(&ptr, &e[i].semantic_idx);
111 read_dword(&ptr, &e[i].sysval_semantic);
112 read_dword(&ptr, &e[i].component_type);
113 read_dword(&ptr, &e[i].register_idx);
114 read_dword(&ptr, &e[i].mask);
116 TRACE("semantic: %s, semantic idx: %u, sysval_semantic %#x, "
117 "type %u, register idx: %u, use_mask %#x, input_mask %#x\n",
118 debugstr_a(e[i].semantic_name), e[i].semantic_idx, e[i].sysval_semantic,
119 e[i].component_type, e[i].register_idx, (e[i].mask >> 8) & 0xff, e[i].mask & 0xff);
122 s->elements = e;
123 s->element_count = count;
124 s->string_data = string_data;
126 return S_OK;
129 void shader_free_signature(struct wined3d_shader_signature *s)
131 HeapFree(GetProcessHeap(), 0, s->string_data);
132 HeapFree(GetProcessHeap(), 0, s->elements);
135 static inline struct d3d10_vertex_shader *impl_from_ID3D10VertexShader(ID3D10VertexShader *iface)
137 return CONTAINING_RECORD(iface, struct d3d10_vertex_shader, ID3D10VertexShader_iface);
140 /* IUnknown methods */
142 static HRESULT STDMETHODCALLTYPE d3d10_vertex_shader_QueryInterface(ID3D10VertexShader *iface,
143 REFIID riid, void **object)
145 TRACE("iface %p, riid %s, object %p\n", iface, debugstr_guid(riid), object);
147 if (IsEqualGUID(riid, &IID_ID3D10VertexShader)
148 || IsEqualGUID(riid, &IID_ID3D10DeviceChild)
149 || IsEqualGUID(riid, &IID_IUnknown))
151 IUnknown_AddRef(iface);
152 *object = iface;
153 return S_OK;
156 WARN("%s not implemented, returning E_NOINTERFACE\n", debugstr_guid(riid));
158 *object = NULL;
159 return E_NOINTERFACE;
162 static ULONG STDMETHODCALLTYPE d3d10_vertex_shader_AddRef(ID3D10VertexShader *iface)
164 struct d3d10_vertex_shader *This = impl_from_ID3D10VertexShader(iface);
165 ULONG refcount = InterlockedIncrement(&This->refcount);
167 TRACE("%p increasing refcount to %u\n", This, refcount);
169 if (refcount == 1)
170 wined3d_shader_incref(This->wined3d_shader);
172 return refcount;
175 static ULONG STDMETHODCALLTYPE d3d10_vertex_shader_Release(ID3D10VertexShader *iface)
177 struct d3d10_vertex_shader *This = impl_from_ID3D10VertexShader(iface);
178 ULONG refcount = InterlockedDecrement(&This->refcount);
180 TRACE("%p decreasing refcount to %u\n", This, refcount);
182 if (!refcount)
183 wined3d_shader_decref(This->wined3d_shader);
185 return refcount;
188 /* ID3D10DeviceChild methods */
190 static void STDMETHODCALLTYPE d3d10_vertex_shader_GetDevice(ID3D10VertexShader *iface, ID3D10Device **device)
192 FIXME("iface %p, device %p stub!\n", iface, device);
195 static HRESULT STDMETHODCALLTYPE d3d10_vertex_shader_GetPrivateData(ID3D10VertexShader *iface,
196 REFGUID guid, UINT *data_size, void *data)
198 FIXME("iface %p, guid %s, data_size %p, data %p stub!\n",
199 iface, debugstr_guid(guid), data_size, data);
201 return E_NOTIMPL;
204 static HRESULT STDMETHODCALLTYPE d3d10_vertex_shader_SetPrivateData(ID3D10VertexShader *iface,
205 REFGUID guid, UINT data_size, const void *data)
207 FIXME("iface %p, guid %s, data_size %u, data %p stub!\n",
208 iface, debugstr_guid(guid), data_size, data);
210 return E_NOTIMPL;
213 static HRESULT STDMETHODCALLTYPE d3d10_vertex_shader_SetPrivateDataInterface(ID3D10VertexShader *iface,
214 REFGUID guid, const IUnknown *data)
216 FIXME("iface %p, guid %s, data %p stub!\n", iface, debugstr_guid(guid), data);
218 return E_NOTIMPL;
221 static const struct ID3D10VertexShaderVtbl d3d10_vertex_shader_vtbl =
223 /* IUnknown methods */
224 d3d10_vertex_shader_QueryInterface,
225 d3d10_vertex_shader_AddRef,
226 d3d10_vertex_shader_Release,
227 /* ID3D10DeviceChild methods */
228 d3d10_vertex_shader_GetDevice,
229 d3d10_vertex_shader_GetPrivateData,
230 d3d10_vertex_shader_SetPrivateData,
231 d3d10_vertex_shader_SetPrivateDataInterface,
234 static void STDMETHODCALLTYPE d3d10_vertex_shader_wined3d_object_destroyed(void *parent)
236 struct d3d10_vertex_shader *shader = parent;
237 shader_free_signature(&shader->output_signature);
238 HeapFree(GetProcessHeap(), 0, shader);
241 static const struct wined3d_parent_ops d3d10_vertex_shader_wined3d_parent_ops =
243 d3d10_vertex_shader_wined3d_object_destroyed,
246 HRESULT d3d10_vertex_shader_init(struct d3d10_vertex_shader *shader, struct d3d10_device *device,
247 const void *byte_code, SIZE_T byte_code_length)
249 struct d3d10_shader_info shader_info;
250 HRESULT hr;
252 shader->ID3D10VertexShader_iface.lpVtbl = &d3d10_vertex_shader_vtbl;
253 shader->refcount = 1;
255 shader_info.output_signature = &shader->output_signature;
256 hr = shader_extract_from_dxbc(byte_code, byte_code_length, &shader_info);
257 if (FAILED(hr))
259 ERR("Failed to extract shader, hr %#x.\n", hr);
260 return hr;
263 hr = wined3d_shader_create_vs(device->wined3d_device, shader_info.shader_code,
264 &shader->output_signature, shader, &d3d10_vertex_shader_wined3d_parent_ops, &shader->wined3d_shader, 4);
265 if (FAILED(hr))
267 WARN("Failed to create wined3d vertex shader, hr %#x.\n", hr);
268 shader_free_signature(&shader->output_signature);
269 hr = E_INVALIDARG;
270 return hr;
273 return S_OK;
276 struct d3d10_vertex_shader *unsafe_impl_from_ID3D10VertexShader(ID3D10VertexShader *iface)
278 if (!iface)
279 return NULL;
280 assert(iface->lpVtbl == &d3d10_vertex_shader_vtbl);
282 return impl_from_ID3D10VertexShader(iface);
285 static inline struct d3d10_geometry_shader *impl_from_ID3D10GeometryShader(ID3D10GeometryShader *iface)
287 return CONTAINING_RECORD(iface, struct d3d10_geometry_shader, ID3D10GeometryShader_iface);
290 /* IUnknown methods */
292 static HRESULT STDMETHODCALLTYPE d3d10_geometry_shader_QueryInterface(ID3D10GeometryShader *iface,
293 REFIID riid, void **object)
295 TRACE("iface %p, riid %s, object %p\n", iface, debugstr_guid(riid), object);
297 if (IsEqualGUID(riid, &IID_ID3D10GeometryShader)
298 || IsEqualGUID(riid, &IID_ID3D10DeviceChild)
299 || IsEqualGUID(riid, &IID_IUnknown))
301 IUnknown_AddRef(iface);
302 *object = iface;
303 return S_OK;
306 WARN("%s not implemented, returning E_NOINTERFACE\n", debugstr_guid(riid));
308 *object = NULL;
309 return E_NOINTERFACE;
312 static ULONG STDMETHODCALLTYPE d3d10_geometry_shader_AddRef(ID3D10GeometryShader *iface)
314 struct d3d10_geometry_shader *This = impl_from_ID3D10GeometryShader(iface);
315 ULONG refcount = InterlockedIncrement(&This->refcount);
317 TRACE("%p increasing refcount to %u\n", This, refcount);
319 return refcount;
322 static ULONG STDMETHODCALLTYPE d3d10_geometry_shader_Release(ID3D10GeometryShader *iface)
324 struct d3d10_geometry_shader *This = impl_from_ID3D10GeometryShader(iface);
325 ULONG refcount = InterlockedDecrement(&This->refcount);
327 TRACE("%p decreasing refcount to %u\n", This, refcount);
329 if (!refcount)
330 wined3d_shader_decref(This->wined3d_shader);
332 return refcount;
335 /* ID3D10DeviceChild methods */
337 static void STDMETHODCALLTYPE d3d10_geometry_shader_GetDevice(ID3D10GeometryShader *iface, ID3D10Device **device)
339 FIXME("iface %p, device %p stub!\n", iface, device);
342 static HRESULT STDMETHODCALLTYPE d3d10_geometry_shader_GetPrivateData(ID3D10GeometryShader *iface,
343 REFGUID guid, UINT *data_size, void *data)
345 FIXME("iface %p, guid %s, data_size %p, data %p stub!\n",
346 iface, debugstr_guid(guid), data_size, data);
348 return E_NOTIMPL;
351 static HRESULT STDMETHODCALLTYPE d3d10_geometry_shader_SetPrivateData(ID3D10GeometryShader *iface,
352 REFGUID guid, UINT data_size, const void *data)
354 FIXME("iface %p, guid %s, data_size %u, data %p stub!\n",
355 iface, debugstr_guid(guid), data_size, data);
357 return E_NOTIMPL;
360 static HRESULT STDMETHODCALLTYPE d3d10_geometry_shader_SetPrivateDataInterface(ID3D10GeometryShader *iface,
361 REFGUID guid, const IUnknown *data)
363 FIXME("iface %p, guid %s, data %p stub!\n", iface, debugstr_guid(guid), data);
365 return E_NOTIMPL;
368 static const struct ID3D10GeometryShaderVtbl d3d10_geometry_shader_vtbl =
370 /* IUnknown methods */
371 d3d10_geometry_shader_QueryInterface,
372 d3d10_geometry_shader_AddRef,
373 d3d10_geometry_shader_Release,
374 /* ID3D10DeviceChild methods */
375 d3d10_geometry_shader_GetDevice,
376 d3d10_geometry_shader_GetPrivateData,
377 d3d10_geometry_shader_SetPrivateData,
378 d3d10_geometry_shader_SetPrivateDataInterface,
381 static void STDMETHODCALLTYPE d3d10_geometry_shader_wined3d_object_destroyed(void *parent)
383 struct d3d10_geometry_shader *shader = parent;
384 shader_free_signature(&shader->output_signature);
385 HeapFree(GetProcessHeap(), 0, shader);
388 static const struct wined3d_parent_ops d3d10_geometry_shader_wined3d_parent_ops =
390 d3d10_geometry_shader_wined3d_object_destroyed,
393 HRESULT d3d10_geometry_shader_init(struct d3d10_geometry_shader *shader, struct d3d10_device *device,
394 const void *byte_code, SIZE_T byte_code_length)
396 struct d3d10_shader_info shader_info;
397 HRESULT hr;
399 shader->ID3D10GeometryShader_iface.lpVtbl = &d3d10_geometry_shader_vtbl;
400 shader->refcount = 1;
402 shader_info.output_signature = &shader->output_signature;
403 hr = shader_extract_from_dxbc(byte_code, byte_code_length, &shader_info);
404 if (FAILED(hr))
406 ERR("Failed to extract shader, hr %#x.\n", hr);
407 return hr;
410 hr = wined3d_shader_create_gs(device->wined3d_device, shader_info.shader_code,
411 &shader->output_signature, shader, &d3d10_geometry_shader_wined3d_parent_ops, &shader->wined3d_shader, 4);
412 if (FAILED(hr))
414 WARN("Failed to create wined3d geometry shader, hr %#x.\n", hr);
415 shader_free_signature(&shader->output_signature);
416 hr = E_INVALIDARG;
417 return hr;
420 return S_OK;
423 static inline struct d3d10_pixel_shader *impl_from_ID3D10PixelShader(ID3D10PixelShader *iface)
425 return CONTAINING_RECORD(iface, struct d3d10_pixel_shader, ID3D10PixelShader_iface);
428 /* IUnknown methods */
430 static HRESULT STDMETHODCALLTYPE d3d10_pixel_shader_QueryInterface(ID3D10PixelShader *iface,
431 REFIID riid, void **object)
433 TRACE("iface %p, riid %s, object %p\n", iface, debugstr_guid(riid), object);
435 if (IsEqualGUID(riid, &IID_ID3D10PixelShader)
436 || IsEqualGUID(riid, &IID_ID3D10DeviceChild)
437 || IsEqualGUID(riid, &IID_IUnknown))
439 IUnknown_AddRef(iface);
440 *object = iface;
441 return S_OK;
444 WARN("%s not implemented, returning E_NOINTERFACE\n", debugstr_guid(riid));
446 *object = NULL;
447 return E_NOINTERFACE;
450 static ULONG STDMETHODCALLTYPE d3d10_pixel_shader_AddRef(ID3D10PixelShader *iface)
452 struct d3d10_pixel_shader *This = impl_from_ID3D10PixelShader(iface);
453 ULONG refcount = InterlockedIncrement(&This->refcount);
455 TRACE("%p increasing refcount to %u\n", This, refcount);
457 if (refcount == 1)
458 wined3d_shader_incref(This->wined3d_shader);
460 return refcount;
463 static ULONG STDMETHODCALLTYPE d3d10_pixel_shader_Release(ID3D10PixelShader *iface)
465 struct d3d10_pixel_shader *This = impl_from_ID3D10PixelShader(iface);
466 ULONG refcount = InterlockedDecrement(&This->refcount);
468 TRACE("%p decreasing refcount to %u\n", This, refcount);
470 if (!refcount)
471 wined3d_shader_decref(This->wined3d_shader);
473 return refcount;
476 /* ID3D10DeviceChild methods */
478 static void STDMETHODCALLTYPE d3d10_pixel_shader_GetDevice(ID3D10PixelShader *iface, ID3D10Device **device)
480 FIXME("iface %p, device %p stub!\n", iface, device);
483 static HRESULT STDMETHODCALLTYPE d3d10_pixel_shader_GetPrivateData(ID3D10PixelShader *iface,
484 REFGUID guid, UINT *data_size, void *data)
486 FIXME("iface %p, guid %s, data_size %p, data %p stub!\n",
487 iface, debugstr_guid(guid), data_size, data);
489 return E_NOTIMPL;
492 static HRESULT STDMETHODCALLTYPE d3d10_pixel_shader_SetPrivateData(ID3D10PixelShader *iface,
493 REFGUID guid, UINT data_size, const void *data)
495 FIXME("iface %p, guid %s, data_size %u, data %p stub!\n",
496 iface, debugstr_guid(guid), data_size, data);
498 return E_NOTIMPL;
501 static HRESULT STDMETHODCALLTYPE d3d10_pixel_shader_SetPrivateDataInterface(ID3D10PixelShader *iface,
502 REFGUID guid, const IUnknown *data)
504 FIXME("iface %p, guid %s, data %p stub!\n", iface, debugstr_guid(guid), data);
506 return E_NOTIMPL;
509 static const struct ID3D10PixelShaderVtbl d3d10_pixel_shader_vtbl =
511 /* IUnknown methods */
512 d3d10_pixel_shader_QueryInterface,
513 d3d10_pixel_shader_AddRef,
514 d3d10_pixel_shader_Release,
515 /* ID3D10DeviceChild methods */
516 d3d10_pixel_shader_GetDevice,
517 d3d10_pixel_shader_GetPrivateData,
518 d3d10_pixel_shader_SetPrivateData,
519 d3d10_pixel_shader_SetPrivateDataInterface,
522 static void STDMETHODCALLTYPE d3d10_pixel_shader_wined3d_object_destroyed(void *parent)
524 struct d3d10_pixel_shader *shader = parent;
525 shader_free_signature(&shader->output_signature);
526 HeapFree(GetProcessHeap(), 0, shader);
529 static const struct wined3d_parent_ops d3d10_pixel_shader_wined3d_parent_ops =
531 d3d10_pixel_shader_wined3d_object_destroyed,
534 HRESULT d3d10_pixel_shader_init(struct d3d10_pixel_shader *shader, struct d3d10_device *device,
535 const void *byte_code, SIZE_T byte_code_length)
537 struct d3d10_shader_info shader_info;
538 HRESULT hr;
540 shader->ID3D10PixelShader_iface.lpVtbl = &d3d10_pixel_shader_vtbl;
541 shader->refcount = 1;
543 shader_info.output_signature = &shader->output_signature;
544 hr = shader_extract_from_dxbc(byte_code, byte_code_length, &shader_info);
545 if (FAILED(hr))
547 ERR("Failed to extract shader, hr %#x.\n", hr);
548 return hr;
551 hr = wined3d_shader_create_ps(device->wined3d_device, shader_info.shader_code,
552 &shader->output_signature, shader, &d3d10_pixel_shader_wined3d_parent_ops, &shader->wined3d_shader, 4);
553 if (FAILED(hr))
555 WARN("Failed to create wined3d pixel shader, hr %#x.\n", hr);
556 shader_free_signature(&shader->output_signature);
557 hr = E_INVALIDARG;
558 return hr;
561 return S_OK;
564 struct d3d10_pixel_shader *unsafe_impl_from_ID3D10PixelShader(ID3D10PixelShader *iface)
566 if (!iface)
567 return NULL;
568 assert(iface->lpVtbl == &d3d10_pixel_shader_vtbl);
570 return impl_from_ID3D10PixelShader(iface);