Release 1.1.37.
[wine/gsoc-2012-control.git] / dlls / d3d10core / shader.c
blob256f2811eb66a26001e4c79d7b2074ddc7dcdc7a
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_FAIL;
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 /* IUnknown methods */
137 static HRESULT STDMETHODCALLTYPE d3d10_vertex_shader_QueryInterface(ID3D10VertexShader *iface,
138 REFIID riid, void **object)
140 TRACE("iface %p, riid %s, object %p\n", iface, debugstr_guid(riid), object);
142 if (IsEqualGUID(riid, &IID_ID3D10VertexShader)
143 || IsEqualGUID(riid, &IID_ID3D10DeviceChild)
144 || IsEqualGUID(riid, &IID_IUnknown))
146 IUnknown_AddRef(iface);
147 *object = iface;
148 return S_OK;
151 WARN("%s not implemented, returning E_NOINTERFACE\n", debugstr_guid(riid));
153 *object = NULL;
154 return E_NOINTERFACE;
157 static ULONG STDMETHODCALLTYPE d3d10_vertex_shader_AddRef(ID3D10VertexShader *iface)
159 struct d3d10_vertex_shader *This = (struct d3d10_vertex_shader *)iface;
160 ULONG refcount = InterlockedIncrement(&This->refcount);
162 TRACE("%p increasing refcount to %u\n", This, refcount);
164 if (refcount == 1)
166 IWineD3DVertexShader_AddRef(This->wined3d_shader);
169 return refcount;
172 static ULONG STDMETHODCALLTYPE d3d10_vertex_shader_Release(ID3D10VertexShader *iface)
174 struct d3d10_vertex_shader *This = (struct d3d10_vertex_shader *)iface;
175 ULONG refcount = InterlockedDecrement(&This->refcount);
177 TRACE("%p decreasing refcount to %u\n", This, refcount);
179 if (!refcount)
181 IWineD3DVertexShader_Release(This->wined3d_shader);
184 return refcount;
187 /* ID3D10DeviceChild methods */
189 static void STDMETHODCALLTYPE d3d10_vertex_shader_GetDevice(ID3D10VertexShader *iface, ID3D10Device **device)
191 FIXME("iface %p, device %p stub!\n", iface, device);
194 static HRESULT STDMETHODCALLTYPE d3d10_vertex_shader_GetPrivateData(ID3D10VertexShader *iface,
195 REFGUID guid, UINT *data_size, void *data)
197 FIXME("iface %p, guid %s, data_size %p, data %p stub!\n",
198 iface, debugstr_guid(guid), data_size, data);
200 return E_NOTIMPL;
203 static HRESULT STDMETHODCALLTYPE d3d10_vertex_shader_SetPrivateData(ID3D10VertexShader *iface,
204 REFGUID guid, UINT data_size, const void *data)
206 FIXME("iface %p, guid %s, data_size %u, data %p stub!\n",
207 iface, debugstr_guid(guid), data_size, data);
209 return E_NOTIMPL;
212 static HRESULT STDMETHODCALLTYPE d3d10_vertex_shader_SetPrivateDataInterface(ID3D10VertexShader *iface,
213 REFGUID guid, const IUnknown *data)
215 FIXME("iface %p, guid %s, data %p stub!\n", iface, debugstr_guid(guid), data);
217 return E_NOTIMPL;
220 static const struct ID3D10VertexShaderVtbl d3d10_vertex_shader_vtbl =
222 /* IUnknown methods */
223 d3d10_vertex_shader_QueryInterface,
224 d3d10_vertex_shader_AddRef,
225 d3d10_vertex_shader_Release,
226 /* ID3D10DeviceChild methods */
227 d3d10_vertex_shader_GetDevice,
228 d3d10_vertex_shader_GetPrivateData,
229 d3d10_vertex_shader_SetPrivateData,
230 d3d10_vertex_shader_SetPrivateDataInterface,
233 static void STDMETHODCALLTYPE d3d10_vertex_shader_wined3d_object_destroyed(void *parent)
235 struct d3d10_vertex_shader *shader = parent;
236 shader_free_signature(&shader->output_signature);
237 HeapFree(GetProcessHeap(), 0, shader);
240 static const struct wined3d_parent_ops d3d10_vertex_shader_wined3d_parent_ops =
242 d3d10_vertex_shader_wined3d_object_destroyed,
245 HRESULT d3d10_vertex_shader_init(struct d3d10_vertex_shader *shader, struct d3d10_device *device,
246 const void *byte_code, SIZE_T byte_code_length)
248 struct d3d10_shader_info shader_info;
249 HRESULT hr;
251 shader->vtbl = &d3d10_vertex_shader_vtbl;
252 shader->refcount = 1;
254 shader_info.output_signature = &shader->output_signature;
255 hr = shader_extract_from_dxbc(byte_code, byte_code_length, &shader_info);
256 if (FAILED(hr))
258 ERR("Failed to extract shader, hr %#x.\n", hr);
259 return hr;
262 hr = IWineD3DDevice_CreateVertexShader(device->wined3d_device,
263 shader_info.shader_code, &shader->output_signature, &shader->wined3d_shader,
264 (IUnknown *)shader, &d3d10_vertex_shader_wined3d_parent_ops);
265 if (FAILED(hr))
267 WARN("Failed to create wined3d vertex shader, hr %#x.\n", hr);
268 shader_free_signature(&shader->output_signature);
269 return hr;
272 return S_OK;
275 /* IUnknown methods */
277 static HRESULT STDMETHODCALLTYPE d3d10_geometry_shader_QueryInterface(ID3D10GeometryShader *iface,
278 REFIID riid, void **object)
280 TRACE("iface %p, riid %s, object %p\n", iface, debugstr_guid(riid), object);
282 if (IsEqualGUID(riid, &IID_ID3D10GeometryShader)
283 || IsEqualGUID(riid, &IID_ID3D10DeviceChild)
284 || IsEqualGUID(riid, &IID_IUnknown))
286 IUnknown_AddRef(iface);
287 *object = iface;
288 return S_OK;
291 WARN("%s not implemented, returning E_NOINTERFACE\n", debugstr_guid(riid));
293 *object = NULL;
294 return E_NOINTERFACE;
297 static ULONG STDMETHODCALLTYPE d3d10_geometry_shader_AddRef(ID3D10GeometryShader *iface)
299 struct d3d10_geometry_shader *This = (struct d3d10_geometry_shader *)iface;
300 ULONG refcount = InterlockedIncrement(&This->refcount);
302 TRACE("%p increasing refcount to %u\n", This, refcount);
304 return refcount;
307 static ULONG STDMETHODCALLTYPE d3d10_geometry_shader_Release(ID3D10GeometryShader *iface)
309 struct d3d10_geometry_shader *This = (struct d3d10_geometry_shader *)iface;
310 ULONG refcount = InterlockedDecrement(&This->refcount);
312 TRACE("%p decreasing refcount to %u\n", This, refcount);
314 if (!refcount)
316 IWineD3DGeometryShader_Release(This->wined3d_shader);
319 return refcount;
322 /* ID3D10DeviceChild methods */
324 static void STDMETHODCALLTYPE d3d10_geometry_shader_GetDevice(ID3D10GeometryShader *iface, ID3D10Device **device)
326 FIXME("iface %p, device %p stub!\n", iface, device);
329 static HRESULT STDMETHODCALLTYPE d3d10_geometry_shader_GetPrivateData(ID3D10GeometryShader *iface,
330 REFGUID guid, UINT *data_size, void *data)
332 FIXME("iface %p, guid %s, data_size %p, data %p stub!\n",
333 iface, debugstr_guid(guid), data_size, data);
335 return E_NOTIMPL;
338 static HRESULT STDMETHODCALLTYPE d3d10_geometry_shader_SetPrivateData(ID3D10GeometryShader *iface,
339 REFGUID guid, UINT data_size, const void *data)
341 FIXME("iface %p, guid %s, data_size %u, data %p stub!\n",
342 iface, debugstr_guid(guid), data_size, data);
344 return E_NOTIMPL;
347 static HRESULT STDMETHODCALLTYPE d3d10_geometry_shader_SetPrivateDataInterface(ID3D10GeometryShader *iface,
348 REFGUID guid, const IUnknown *data)
350 FIXME("iface %p, guid %s, data %p stub!\n", iface, debugstr_guid(guid), data);
352 return E_NOTIMPL;
355 static const struct ID3D10GeometryShaderVtbl d3d10_geometry_shader_vtbl =
357 /* IUnknown methods */
358 d3d10_geometry_shader_QueryInterface,
359 d3d10_geometry_shader_AddRef,
360 d3d10_geometry_shader_Release,
361 /* ID3D10DeviceChild methods */
362 d3d10_geometry_shader_GetDevice,
363 d3d10_geometry_shader_GetPrivateData,
364 d3d10_geometry_shader_SetPrivateData,
365 d3d10_geometry_shader_SetPrivateDataInterface,
368 static void STDMETHODCALLTYPE d3d10_geometry_shader_wined3d_object_destroyed(void *parent)
370 struct d3d10_geometry_shader *shader = parent;
371 shader_free_signature(&shader->output_signature);
372 HeapFree(GetProcessHeap(), 0, shader);
375 static const struct wined3d_parent_ops d3d10_geometry_shader_wined3d_parent_ops =
377 d3d10_geometry_shader_wined3d_object_destroyed,
380 HRESULT d3d10_geometry_shader_init(struct d3d10_geometry_shader *shader, struct d3d10_device *device,
381 const void *byte_code, SIZE_T byte_code_length)
383 struct d3d10_shader_info shader_info;
384 HRESULT hr;
386 shader->vtbl = &d3d10_geometry_shader_vtbl;
387 shader->refcount = 1;
389 shader_info.output_signature = &shader->output_signature;
390 hr = shader_extract_from_dxbc(byte_code, byte_code_length, &shader_info);
391 if (FAILED(hr))
393 ERR("Failed to extract shader, hr %#x.\n", hr);
394 return hr;
397 hr = IWineD3DDevice_CreateGeometryShader(device->wined3d_device,
398 shader_info.shader_code, &shader->output_signature, &shader->wined3d_shader,
399 (IUnknown *)shader, &d3d10_geometry_shader_wined3d_parent_ops);
400 if (FAILED(hr))
402 WARN("Failed to create wined3d vertex shader, hr %#x.\n", hr);
403 shader_free_signature(&shader->output_signature);
404 return hr;
407 return S_OK;
410 /* IUnknown methods */
412 static HRESULT STDMETHODCALLTYPE d3d10_pixel_shader_QueryInterface(ID3D10PixelShader *iface,
413 REFIID riid, void **object)
415 TRACE("iface %p, riid %s, object %p\n", iface, debugstr_guid(riid), object);
417 if (IsEqualGUID(riid, &IID_ID3D10PixelShader)
418 || IsEqualGUID(riid, &IID_ID3D10DeviceChild)
419 || IsEqualGUID(riid, &IID_IUnknown))
421 IUnknown_AddRef(iface);
422 *object = iface;
423 return S_OK;
426 WARN("%s not implemented, returning E_NOINTERFACE\n", debugstr_guid(riid));
428 *object = NULL;
429 return E_NOINTERFACE;
432 static ULONG STDMETHODCALLTYPE d3d10_pixel_shader_AddRef(ID3D10PixelShader *iface)
434 struct d3d10_pixel_shader *This = (struct d3d10_pixel_shader *)iface;
435 ULONG refcount = InterlockedIncrement(&This->refcount);
437 TRACE("%p increasing refcount to %u\n", This, refcount);
439 if (refcount == 1)
441 IWineD3DPixelShader_AddRef(This->wined3d_shader);
444 return refcount;
447 static ULONG STDMETHODCALLTYPE d3d10_pixel_shader_Release(ID3D10PixelShader *iface)
449 struct d3d10_pixel_shader *This = (struct d3d10_pixel_shader *)iface;
450 ULONG refcount = InterlockedDecrement(&This->refcount);
452 TRACE("%p decreasing refcount to %u\n", This, refcount);
454 if (!refcount)
456 IWineD3DPixelShader_Release(This->wined3d_shader);
459 return refcount;
462 /* ID3D10DeviceChild methods */
464 static void STDMETHODCALLTYPE d3d10_pixel_shader_GetDevice(ID3D10PixelShader *iface, ID3D10Device **device)
466 FIXME("iface %p, device %p stub!\n", iface, device);
469 static HRESULT STDMETHODCALLTYPE d3d10_pixel_shader_GetPrivateData(ID3D10PixelShader *iface,
470 REFGUID guid, UINT *data_size, void *data)
472 FIXME("iface %p, guid %s, data_size %p, data %p stub!\n",
473 iface, debugstr_guid(guid), data_size, data);
475 return E_NOTIMPL;
478 static HRESULT STDMETHODCALLTYPE d3d10_pixel_shader_SetPrivateData(ID3D10PixelShader *iface,
479 REFGUID guid, UINT data_size, const void *data)
481 FIXME("iface %p, guid %s, data_size %u, data %p stub!\n",
482 iface, debugstr_guid(guid), data_size, data);
484 return E_NOTIMPL;
487 static HRESULT STDMETHODCALLTYPE d3d10_pixel_shader_SetPrivateDataInterface(ID3D10PixelShader *iface,
488 REFGUID guid, const IUnknown *data)
490 FIXME("iface %p, guid %s, data %p stub!\n", iface, debugstr_guid(guid), data);
492 return E_NOTIMPL;
495 static const struct ID3D10PixelShaderVtbl d3d10_pixel_shader_vtbl =
497 /* IUnknown methods */
498 d3d10_pixel_shader_QueryInterface,
499 d3d10_pixel_shader_AddRef,
500 d3d10_pixel_shader_Release,
501 /* ID3D10DeviceChild methods */
502 d3d10_pixel_shader_GetDevice,
503 d3d10_pixel_shader_GetPrivateData,
504 d3d10_pixel_shader_SetPrivateData,
505 d3d10_pixel_shader_SetPrivateDataInterface,
508 static void STDMETHODCALLTYPE d3d10_pixel_shader_wined3d_object_destroyed(void *parent)
510 struct d3d10_pixel_shader *shader = parent;
511 shader_free_signature(&shader->output_signature);
512 HeapFree(GetProcessHeap(), 0, shader);
515 static const struct wined3d_parent_ops d3d10_pixel_shader_wined3d_parent_ops =
517 d3d10_pixel_shader_wined3d_object_destroyed,
520 HRESULT d3d10_pixel_shader_init(struct d3d10_pixel_shader *shader, struct d3d10_device *device,
521 const void *byte_code, SIZE_T byte_code_length)
523 struct d3d10_shader_info shader_info;
524 HRESULT hr;
526 shader->vtbl = &d3d10_pixel_shader_vtbl;
527 shader->refcount = 1;
529 shader_info.output_signature = &shader->output_signature;
530 hr = shader_extract_from_dxbc(byte_code, byte_code_length, &shader_info);
531 if (FAILED(hr))
533 ERR("Failed to extract shader, hr %#x.\n", hr);
534 return hr;
537 hr = IWineD3DDevice_CreatePixelShader(device->wined3d_device,
538 shader_info.shader_code, &shader->output_signature, &shader->wined3d_shader,
539 (IUnknown *)shader, &d3d10_pixel_shader_wined3d_parent_ops);
540 if (FAILED(hr))
542 WARN("Failed to create wined3d pixel shader, hr %#x.\n", hr);
543 shader_free_signature(&shader->output_signature);
544 return hr;
547 return S_OK;