bcrypt: Implement BCryptGenerateSymmetricKey and BCryptDestroyKey.
[wine.git] / dlls / d3dcompiler_43 / blob.c
blobf22dc7183d57f6cf7e2615c134b8bc2339916c16
1 /*
2 * Direct3D blob file
4 * Copyright 2010 Rico Schüller
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 #include "config.h"
23 #include "wine/port.h"
25 #include "d3dcompiler_private.h"
27 WINE_DEFAULT_DEBUG_CHANNEL(d3dcompiler);
29 struct d3dcompiler_blob
31 ID3DBlob ID3DBlob_iface;
32 LONG refcount;
34 SIZE_T size;
35 void *data;
38 static inline struct d3dcompiler_blob *impl_from_ID3DBlob(ID3DBlob *iface)
40 return CONTAINING_RECORD(iface, struct d3dcompiler_blob, ID3DBlob_iface);
43 /* IUnknown methods */
45 static HRESULT STDMETHODCALLTYPE d3dcompiler_blob_QueryInterface(ID3DBlob *iface, REFIID riid, void **object)
47 TRACE("iface %p, riid %s, object %p\n", iface, debugstr_guid(riid), object);
49 if (IsEqualGUID(riid, &IID_ID3D10Blob)
50 || IsEqualGUID(riid, &IID_IUnknown))
52 IUnknown_AddRef(iface);
53 *object = iface;
54 return S_OK;
57 WARN("%s not implemented, returning E_NOINTERFACE\n", debugstr_guid(riid));
59 *object = NULL;
60 return E_NOINTERFACE;
63 static ULONG STDMETHODCALLTYPE d3dcompiler_blob_AddRef(ID3DBlob *iface)
65 struct d3dcompiler_blob *blob = impl_from_ID3DBlob(iface);
66 ULONG refcount = InterlockedIncrement(&blob->refcount);
68 TRACE("%p increasing refcount to %u\n", blob, refcount);
70 return refcount;
73 static ULONG STDMETHODCALLTYPE d3dcompiler_blob_Release(ID3DBlob *iface)
75 struct d3dcompiler_blob *blob = impl_from_ID3DBlob(iface);
76 ULONG refcount = InterlockedDecrement(&blob->refcount);
78 TRACE("%p decreasing refcount to %u\n", blob, refcount);
80 if (!refcount)
82 HeapFree(GetProcessHeap(), 0, blob->data);
83 HeapFree(GetProcessHeap(), 0, blob);
86 return refcount;
89 /* ID3DBlob methods */
91 static void * STDMETHODCALLTYPE d3dcompiler_blob_GetBufferPointer(ID3DBlob *iface)
93 struct d3dcompiler_blob *blob = impl_from_ID3DBlob(iface);
95 TRACE("iface %p\n", iface);
97 return blob->data;
100 static SIZE_T STDMETHODCALLTYPE d3dcompiler_blob_GetBufferSize(ID3DBlob *iface)
102 struct d3dcompiler_blob *blob = impl_from_ID3DBlob(iface);
104 TRACE("iface %p\n", iface);
106 return blob->size;
109 static const struct ID3D10BlobVtbl d3dcompiler_blob_vtbl =
111 /* IUnknown methods */
112 d3dcompiler_blob_QueryInterface,
113 d3dcompiler_blob_AddRef,
114 d3dcompiler_blob_Release,
115 /* ID3DBlob methods */
116 d3dcompiler_blob_GetBufferPointer,
117 d3dcompiler_blob_GetBufferSize,
120 static HRESULT d3dcompiler_blob_init(struct d3dcompiler_blob *blob, SIZE_T data_size)
122 blob->ID3DBlob_iface.lpVtbl = &d3dcompiler_blob_vtbl;
123 blob->refcount = 1;
124 blob->size = data_size;
126 blob->data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, data_size);
127 if (!blob->data)
129 ERR("Failed to allocate D3D blob data memory\n");
130 return E_OUTOFMEMORY;
133 return S_OK;
136 HRESULT WINAPI D3DCreateBlob(SIZE_T data_size, ID3DBlob **blob)
138 struct d3dcompiler_blob *object;
139 HRESULT hr;
141 TRACE("data_size %lu, blob %p\n", data_size, blob);
143 if (!blob)
145 WARN("Invalid blob specified.\n");
146 return D3DERR_INVALIDCALL;
149 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
150 if (!object)
151 return E_OUTOFMEMORY;
153 hr = d3dcompiler_blob_init(object, data_size);
154 if (FAILED(hr))
156 WARN("Failed to initialize blob, hr %#x.\n", hr);
157 HeapFree(GetProcessHeap(), 0, object);
158 return hr;
161 *blob = &object->ID3DBlob_iface;
163 TRACE("Created ID3DBlob %p\n", *blob);
165 return S_OK;
168 static BOOL check_blob_part(DWORD tag, D3D_BLOB_PART part)
170 BOOL add = FALSE;
172 switch(part)
174 case D3D_BLOB_INPUT_SIGNATURE_BLOB:
175 if (tag == TAG_ISGN) add = TRUE;
176 break;
178 case D3D_BLOB_OUTPUT_SIGNATURE_BLOB:
179 if (tag == TAG_OSGN || tag == TAG_OSG5) add = TRUE;
180 break;
182 case D3D_BLOB_INPUT_AND_OUTPUT_SIGNATURE_BLOB:
183 if (tag == TAG_ISGN || tag == TAG_OSGN || tag == TAG_OSG5) add = TRUE;
184 break;
186 case D3D_BLOB_PATCH_CONSTANT_SIGNATURE_BLOB:
187 if (tag == TAG_PCSG) add = TRUE;
188 break;
190 case D3D_BLOB_ALL_SIGNATURE_BLOB:
191 if (tag == TAG_ISGN || tag == TAG_OSGN || tag == TAG_OSG5 || tag == TAG_PCSG) add = TRUE;
192 break;
194 case D3D_BLOB_DEBUG_INFO:
195 if (tag == TAG_SDBG) add = TRUE;
196 break;
198 case D3D_BLOB_LEGACY_SHADER:
199 if (tag == TAG_Aon9) add = TRUE;
200 break;
202 case D3D_BLOB_XNA_PREPASS_SHADER:
203 if (tag == TAG_XNAP) add = TRUE;
204 break;
206 case D3D_BLOB_XNA_SHADER:
207 if (tag == TAG_XNAS) add = TRUE;
208 break;
210 default:
211 FIXME("Unhandled D3D_BLOB_PART %s.\n", debug_d3dcompiler_d3d_blob_part(part));
212 break;
215 TRACE("%s tag %s\n", add ? "Add" : "Skip", debugstr_an((const char *)&tag, 4));
217 return add;
220 static HRESULT d3dcompiler_get_blob_part(const void *data, SIZE_T data_size, D3D_BLOB_PART part, UINT flags, ID3DBlob **blob)
222 struct dxbc src_dxbc, dst_dxbc;
223 HRESULT hr;
224 unsigned int i, count;
226 if (!data || !data_size || flags || !blob)
228 WARN("Invalid arguments: data %p, data_size %lu, flags %#x, blob %p\n", data, data_size, flags, blob);
229 return D3DERR_INVALIDCALL;
232 if (part > D3D_BLOB_TEST_COMPILE_PERF
233 || (part < D3D_BLOB_TEST_ALTERNATE_SHADER && part > D3D_BLOB_XNA_SHADER))
235 WARN("Invalid D3D_BLOB_PART: part %s\n", debug_d3dcompiler_d3d_blob_part(part));
236 return D3DERR_INVALIDCALL;
239 hr = dxbc_parse(data, data_size, &src_dxbc);
240 if (FAILED(hr))
242 WARN("Failed to parse blob part\n");
243 return hr;
246 hr = dxbc_init(&dst_dxbc, 0);
247 if (FAILED(hr))
249 dxbc_destroy(&src_dxbc);
250 WARN("Failed to init dxbc\n");
251 return hr;
254 for (i = 0; i < src_dxbc.count; ++i)
256 struct dxbc_section *section = &src_dxbc.sections[i];
258 if (check_blob_part(section->tag, part))
260 hr = dxbc_add_section(&dst_dxbc, section->tag, section->data, section->data_size);
261 if (FAILED(hr))
263 dxbc_destroy(&src_dxbc);
264 dxbc_destroy(&dst_dxbc);
265 WARN("Failed to add section to dxbc\n");
266 return hr;
271 count = dst_dxbc.count;
273 switch(part)
275 case D3D_BLOB_INPUT_SIGNATURE_BLOB:
276 case D3D_BLOB_OUTPUT_SIGNATURE_BLOB:
277 case D3D_BLOB_PATCH_CONSTANT_SIGNATURE_BLOB:
278 case D3D_BLOB_DEBUG_INFO:
279 case D3D_BLOB_LEGACY_SHADER:
280 case D3D_BLOB_XNA_PREPASS_SHADER:
281 case D3D_BLOB_XNA_SHADER:
282 if (count != 1) count = 0;
283 break;
285 case D3D_BLOB_INPUT_AND_OUTPUT_SIGNATURE_BLOB:
286 if (count != 2) count = 0;
287 break;
289 case D3D_BLOB_ALL_SIGNATURE_BLOB:
290 if (count != 3) count = 0;
291 break;
293 default:
294 FIXME("Unhandled D3D_BLOB_PART %s.\n", debug_d3dcompiler_d3d_blob_part(part));
295 break;
298 if (count == 0)
300 dxbc_destroy(&src_dxbc);
301 dxbc_destroy(&dst_dxbc);
302 WARN("Nothing to write into the blob (count = 0)\n");
303 return E_FAIL;
306 /* some parts aren't full DXBCs, they contain only the data */
307 if (count == 1 && (part == D3D_BLOB_DEBUG_INFO || part == D3D_BLOB_LEGACY_SHADER || part == D3D_BLOB_XNA_PREPASS_SHADER
308 || part == D3D_BLOB_XNA_SHADER))
310 hr = D3DCreateBlob(dst_dxbc.sections[0].data_size, blob);
311 if (SUCCEEDED(hr))
313 memcpy(ID3D10Blob_GetBufferPointer(*blob), dst_dxbc.sections[0].data, dst_dxbc.sections[0].data_size);
315 else
317 WARN("Could not create blob\n");
320 else
322 hr = dxbc_write_blob(&dst_dxbc, blob);
323 if (FAILED(hr))
325 WARN("Failed to write blob part\n");
329 dxbc_destroy(&src_dxbc);
330 dxbc_destroy(&dst_dxbc);
332 return hr;
335 static BOOL check_blob_strip(DWORD tag, UINT flags)
337 BOOL add = TRUE;
339 if (flags & D3DCOMPILER_STRIP_TEST_BLOBS) FIXME("Unhandled flag D3DCOMPILER_STRIP_TEST_BLOBS.\n");
341 switch(tag)
343 case TAG_RDEF:
344 case TAG_STAT:
345 if (flags & D3DCOMPILER_STRIP_REFLECTION_DATA) add = FALSE;
346 break;
348 case TAG_SDBG:
349 if (flags & D3DCOMPILER_STRIP_DEBUG_INFO) add = FALSE;
350 break;
352 default:
353 break;
356 TRACE("%s tag %s\n", add ? "Add" : "Skip", debugstr_an((const char *)&tag, 4));
358 return add;
361 static HRESULT d3dcompiler_strip_shader(const void *data, SIZE_T data_size, UINT flags, ID3DBlob **blob)
363 struct dxbc src_dxbc, dst_dxbc;
364 HRESULT hr;
365 unsigned int i;
367 if (!blob)
369 WARN("NULL for blob specified\n");
370 return E_FAIL;
373 if (!data || !data_size)
375 WARN("Invalid arguments: data %p, data_size %lu\n", data, data_size);
376 return D3DERR_INVALIDCALL;
379 hr = dxbc_parse(data, data_size, &src_dxbc);
380 if (FAILED(hr))
382 WARN("Failed to parse blob part\n");
383 return hr;
386 /* src_dxbc.count >= dst_dxbc.count */
387 hr = dxbc_init(&dst_dxbc, src_dxbc.count);
388 if (FAILED(hr))
390 dxbc_destroy(&src_dxbc);
391 WARN("Failed to init dxbc\n");
392 return hr;
395 for (i = 0; i < src_dxbc.count; ++i)
397 struct dxbc_section *section = &src_dxbc.sections[i];
399 if (check_blob_strip(section->tag, flags))
401 hr = dxbc_add_section(&dst_dxbc, section->tag, section->data, section->data_size);
402 if (FAILED(hr))
404 dxbc_destroy(&src_dxbc);
405 dxbc_destroy(&dst_dxbc);
406 WARN("Failed to add section to dxbc\n");
407 return hr;
412 hr = dxbc_write_blob(&dst_dxbc, blob);
413 if (FAILED(hr))
415 WARN("Failed to write blob part\n");
418 dxbc_destroy(&src_dxbc);
419 dxbc_destroy(&dst_dxbc);
421 return hr;
424 HRESULT WINAPI D3DGetBlobPart(const void *data, SIZE_T data_size, D3D_BLOB_PART part, UINT flags, ID3DBlob **blob)
426 TRACE("data %p, data_size %lu, part %s, flags %#x, blob %p\n", data,
427 data_size, debug_d3dcompiler_d3d_blob_part(part), flags, blob);
429 return d3dcompiler_get_blob_part(data, data_size, part, flags, blob);
432 HRESULT WINAPI D3DGetInputSignatureBlob(const void *data, SIZE_T data_size, ID3DBlob **blob)
434 TRACE("data %p, data_size %lu, blob %p\n", data, data_size, blob);
436 return d3dcompiler_get_blob_part(data, data_size, D3D_BLOB_INPUT_SIGNATURE_BLOB, 0, blob);
439 HRESULT WINAPI D3DGetOutputSignatureBlob(const void *data, SIZE_T data_size, ID3DBlob **blob)
441 TRACE("data %p, data_size %lu, blob %p\n", data, data_size, blob);
443 return d3dcompiler_get_blob_part(data, data_size, D3D_BLOB_OUTPUT_SIGNATURE_BLOB, 0, blob);
446 HRESULT WINAPI D3DGetInputAndOutputSignatureBlob(const void *data, SIZE_T data_size, ID3DBlob **blob)
448 TRACE("data %p, data_size %lu, blob %p\n", data, data_size, blob);
450 return d3dcompiler_get_blob_part(data, data_size, D3D_BLOB_INPUT_AND_OUTPUT_SIGNATURE_BLOB, 0, blob);
453 HRESULT WINAPI D3DGetDebugInfo(const void *data, SIZE_T data_size, ID3DBlob **blob)
455 TRACE("data %p, data_size %lu, blob %p\n", data, data_size, blob);
457 return d3dcompiler_get_blob_part(data, data_size, D3D_BLOB_DEBUG_INFO, 0, blob);
460 HRESULT WINAPI D3DStripShader(const void *data, SIZE_T data_size, UINT flags, ID3D10Blob **blob)
462 TRACE("data %p, data_size %lu, flags %#x, blob %p\n", data, data_size, flags, blob);
464 return d3dcompiler_strip_shader(data, data_size, flags, blob);
467 HRESULT WINAPI D3DReadFileToBlob(const WCHAR *filename, ID3DBlob **contents)
469 FIXME("filename %s, contents %p\n", debugstr_w(filename), contents);
471 return E_NOTIMPL;
474 HRESULT WINAPI D3DWriteBlobToFile(ID3DBlob* blob, const WCHAR *filename, BOOL overwrite)
476 FIXME("blob %p, filename %s, overwrite %d\n", blob, debugstr_w(filename), overwrite);
478 return E_NOTIMPL;