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
23 #include "wine/port.h"
25 #include "d3dcompiler_private.h"
27 WINE_DEFAULT_DEBUG_CHANNEL(d3dcompiler
);
29 struct d3dcompiler_blob
31 ID3DBlob ID3DBlob_iface
;
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
);
57 WARN("%s not implemented, returning E_NOINTERFACE\n", debugstr_guid(riid
));
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
);
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
);
82 HeapFree(GetProcessHeap(), 0, blob
->data
);
83 HeapFree(GetProcessHeap(), 0, blob
);
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
);
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
);
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
;
124 blob
->size
= data_size
;
126 blob
->data
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, data_size
);
129 ERR("Failed to allocate D3D blob data memory\n");
130 return E_OUTOFMEMORY
;
136 HRESULT WINAPI
D3DCreateBlob(SIZE_T data_size
, ID3DBlob
**blob
)
138 struct d3dcompiler_blob
*object
;
141 TRACE("data_size %lu, blob %p\n", data_size
, blob
);
145 WARN("Invalid blob specified.\n");
146 return D3DERR_INVALIDCALL
;
149 object
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*object
));
151 return E_OUTOFMEMORY
;
153 hr
= d3dcompiler_blob_init(object
, data_size
);
156 WARN("Failed to initialize blob, hr %#x.\n", hr
);
157 HeapFree(GetProcessHeap(), 0, object
);
161 *blob
= &object
->ID3DBlob_iface
;
163 TRACE("Created ID3DBlob %p\n", *blob
);
168 static BOOL
check_blob_part(DWORD tag
, D3D_BLOB_PART part
)
174 case D3D_BLOB_INPUT_SIGNATURE_BLOB
:
175 if (tag
== TAG_ISGN
) add
= TRUE
;
178 case D3D_BLOB_OUTPUT_SIGNATURE_BLOB
:
179 if (tag
== TAG_OSGN
|| tag
== TAG_OSG5
) add
= TRUE
;
182 case D3D_BLOB_INPUT_AND_OUTPUT_SIGNATURE_BLOB
:
183 if (tag
== TAG_ISGN
|| tag
== TAG_OSGN
|| tag
== TAG_OSG5
) add
= TRUE
;
186 case D3D_BLOB_PATCH_CONSTANT_SIGNATURE_BLOB
:
187 if (tag
== TAG_PCSG
) add
= TRUE
;
190 case D3D_BLOB_ALL_SIGNATURE_BLOB
:
191 if (tag
== TAG_ISGN
|| tag
== TAG_OSGN
|| tag
== TAG_OSG5
|| tag
== TAG_PCSG
) add
= TRUE
;
194 case D3D_BLOB_DEBUG_INFO
:
195 if (tag
== TAG_SDBG
) add
= TRUE
;
198 case D3D_BLOB_LEGACY_SHADER
:
199 if (tag
== TAG_Aon9
) add
= TRUE
;
202 case D3D_BLOB_XNA_PREPASS_SHADER
:
203 if (tag
== TAG_XNAP
) add
= TRUE
;
206 case D3D_BLOB_XNA_SHADER
:
207 if (tag
== TAG_XNAS
) add
= TRUE
;
211 FIXME("Unhandled D3D_BLOB_PART %s.\n", debug_d3dcompiler_d3d_blob_part(part
));
215 TRACE("%s tag %s\n", add
? "Add" : "Skip", debugstr_an((const char *)&tag
, 4));
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
;
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
);
242 WARN("Failed to parse blob part\n");
246 hr
= dxbc_init(&dst_dxbc
, 0);
249 dxbc_destroy(&src_dxbc
);
250 WARN("Failed to init dxbc\n");
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
);
263 dxbc_destroy(&src_dxbc
);
264 dxbc_destroy(&dst_dxbc
);
265 WARN("Failed to add section to dxbc\n");
271 count
= dst_dxbc
.count
;
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;
285 case D3D_BLOB_INPUT_AND_OUTPUT_SIGNATURE_BLOB
:
286 if (count
!= 2) count
= 0;
289 case D3D_BLOB_ALL_SIGNATURE_BLOB
:
290 if (count
!= 3) count
= 0;
294 FIXME("Unhandled D3D_BLOB_PART %s.\n", debug_d3dcompiler_d3d_blob_part(part
));
300 dxbc_destroy(&src_dxbc
);
301 dxbc_destroy(&dst_dxbc
);
302 WARN("Nothing to write into the blob (count = 0)\n");
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
);
313 memcpy(ID3D10Blob_GetBufferPointer(*blob
), dst_dxbc
.sections
[0].data
, dst_dxbc
.sections
[0].data_size
);
317 WARN("Could not create blob\n");
322 hr
= dxbc_write_blob(&dst_dxbc
, blob
);
325 WARN("Failed to write blob part\n");
329 dxbc_destroy(&src_dxbc
);
330 dxbc_destroy(&dst_dxbc
);
335 static BOOL
check_blob_strip(DWORD tag
, UINT flags
)
339 if (flags
& D3DCOMPILER_STRIP_TEST_BLOBS
) FIXME("Unhandled flag D3DCOMPILER_STRIP_TEST_BLOBS.\n");
345 if (flags
& D3DCOMPILER_STRIP_REFLECTION_DATA
) add
= FALSE
;
349 if (flags
& D3DCOMPILER_STRIP_DEBUG_INFO
) add
= FALSE
;
356 TRACE("%s tag %s\n", add
? "Add" : "Skip", debugstr_an((const char *)&tag
, 4));
361 static HRESULT
d3dcompiler_strip_shader(const void *data
, SIZE_T data_size
, UINT flags
, ID3DBlob
**blob
)
363 struct dxbc src_dxbc
, dst_dxbc
;
369 WARN("NULL for blob specified\n");
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
);
382 WARN("Failed to parse blob part\n");
386 /* src_dxbc.count >= dst_dxbc.count */
387 hr
= dxbc_init(&dst_dxbc
, src_dxbc
.count
);
390 dxbc_destroy(&src_dxbc
);
391 WARN("Failed to init dxbc\n");
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
);
404 dxbc_destroy(&src_dxbc
);
405 dxbc_destroy(&dst_dxbc
);
406 WARN("Failed to add section to dxbc\n");
412 hr
= dxbc_write_blob(&dst_dxbc
, blob
);
415 WARN("Failed to write blob part\n");
418 dxbc_destroy(&src_dxbc
);
419 dxbc_destroy(&dst_dxbc
);
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
);
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
);