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
));
152 ERR("Failed to allocate D3D blob object memory\n");
153 return E_OUTOFMEMORY
;
156 hr
= d3dcompiler_blob_init(object
, data_size
);
159 WARN("Failed to initialize blob, hr %#x.\n", hr
);
160 HeapFree(GetProcessHeap(), 0, object
);
164 *blob
= &object
->ID3DBlob_iface
;
166 TRACE("Created ID3DBlob %p\n", *blob
);
171 static BOOL
check_blob_part(DWORD tag
, D3D_BLOB_PART part
)
177 case D3D_BLOB_INPUT_SIGNATURE_BLOB
:
178 if (tag
== TAG_ISGN
) add
= TRUE
;
181 case D3D_BLOB_OUTPUT_SIGNATURE_BLOB
:
182 if (tag
== TAG_OSGN
|| tag
== TAG_OSG5
) add
= TRUE
;
185 case D3D_BLOB_INPUT_AND_OUTPUT_SIGNATURE_BLOB
:
186 if (tag
== TAG_ISGN
|| tag
== TAG_OSGN
|| tag
== TAG_OSG5
) add
= TRUE
;
189 case D3D_BLOB_PATCH_CONSTANT_SIGNATURE_BLOB
:
190 if (tag
== TAG_PCSG
) add
= TRUE
;
193 case D3D_BLOB_ALL_SIGNATURE_BLOB
:
194 if (tag
== TAG_ISGN
|| tag
== TAG_OSGN
|| tag
== TAG_OSG5
|| tag
== TAG_PCSG
) add
= TRUE
;
197 case D3D_BLOB_DEBUG_INFO
:
198 if (tag
== TAG_SDBG
) add
= TRUE
;
201 case D3D_BLOB_LEGACY_SHADER
:
202 if (tag
== TAG_Aon9
) add
= TRUE
;
205 case D3D_BLOB_XNA_PREPASS_SHADER
:
206 if (tag
== TAG_XNAP
) add
= TRUE
;
209 case D3D_BLOB_XNA_SHADER
:
210 if (tag
== TAG_XNAS
) add
= TRUE
;
214 FIXME("Unhandled D3D_BLOB_PART %s.\n", debug_d3dcompiler_d3d_blob_part(part
));
218 TRACE("%s tag %s\n", add
? "Add" : "Skip", debugstr_an((const char *)&tag
, 4));
223 static HRESULT
d3dcompiler_get_blob_part(const void *data
, SIZE_T data_size
, D3D_BLOB_PART part
, UINT flags
, ID3DBlob
**blob
)
225 struct dxbc src_dxbc
, dst_dxbc
;
227 unsigned int i
, count
;
229 if (!data
|| !data_size
|| flags
|| !blob
)
231 WARN("Invalid arguments: data %p, data_size %lu, flags %#x, blob %p\n", data
, data_size
, flags
, blob
);
232 return D3DERR_INVALIDCALL
;
235 if (part
> D3D_BLOB_TEST_COMPILE_PERF
236 || (part
< D3D_BLOB_TEST_ALTERNATE_SHADER
&& part
> D3D_BLOB_XNA_SHADER
))
238 WARN("Invalid D3D_BLOB_PART: part %s\n", debug_d3dcompiler_d3d_blob_part(part
));
239 return D3DERR_INVALIDCALL
;
242 hr
= dxbc_parse(data
, data_size
, &src_dxbc
);
245 WARN("Failed to parse blob part\n");
249 hr
= dxbc_init(&dst_dxbc
, 0);
252 dxbc_destroy(&src_dxbc
);
253 WARN("Failed to init dxbc\n");
257 for (i
= 0; i
< src_dxbc
.count
; ++i
)
259 struct dxbc_section
*section
= &src_dxbc
.sections
[i
];
261 if (check_blob_part(section
->tag
, part
))
263 hr
= dxbc_add_section(&dst_dxbc
, section
->tag
, section
->data
, section
->data_size
);
266 dxbc_destroy(&src_dxbc
);
267 dxbc_destroy(&dst_dxbc
);
268 WARN("Failed to add section to dxbc\n");
274 count
= dst_dxbc
.count
;
278 case D3D_BLOB_INPUT_SIGNATURE_BLOB
:
279 case D3D_BLOB_OUTPUT_SIGNATURE_BLOB
:
280 case D3D_BLOB_PATCH_CONSTANT_SIGNATURE_BLOB
:
281 case D3D_BLOB_DEBUG_INFO
:
282 case D3D_BLOB_LEGACY_SHADER
:
283 case D3D_BLOB_XNA_PREPASS_SHADER
:
284 case D3D_BLOB_XNA_SHADER
:
285 if (count
!= 1) count
= 0;
288 case D3D_BLOB_INPUT_AND_OUTPUT_SIGNATURE_BLOB
:
289 if (count
!= 2) count
= 0;
292 case D3D_BLOB_ALL_SIGNATURE_BLOB
:
293 if (count
!= 3) count
= 0;
297 FIXME("Unhandled D3D_BLOB_PART %s.\n", debug_d3dcompiler_d3d_blob_part(part
));
303 dxbc_destroy(&src_dxbc
);
304 dxbc_destroy(&dst_dxbc
);
305 WARN("Nothing to write into the blob (count = 0)\n");
309 /* some parts aren't full DXBCs, they contain only the data */
310 if (count
== 1 && (part
== D3D_BLOB_DEBUG_INFO
|| part
== D3D_BLOB_LEGACY_SHADER
|| part
== D3D_BLOB_XNA_PREPASS_SHADER
311 || part
== D3D_BLOB_XNA_SHADER
))
313 hr
= D3DCreateBlob(dst_dxbc
.sections
[0].data_size
, blob
);
316 memcpy(ID3D10Blob_GetBufferPointer(*blob
), dst_dxbc
.sections
[0].data
, dst_dxbc
.sections
[0].data_size
);
320 WARN("Could not create blob\n");
325 hr
= dxbc_write_blob(&dst_dxbc
, blob
);
328 WARN("Failed to write blob part\n");
332 dxbc_destroy(&src_dxbc
);
333 dxbc_destroy(&dst_dxbc
);
338 static BOOL
check_blob_strip(DWORD tag
, UINT flags
)
342 if (flags
& D3DCOMPILER_STRIP_TEST_BLOBS
) FIXME("Unhandled flag D3DCOMPILER_STRIP_TEST_BLOBS.\n");
348 if (flags
& D3DCOMPILER_STRIP_REFLECTION_DATA
) add
= FALSE
;
352 if (flags
& D3DCOMPILER_STRIP_DEBUG_INFO
) add
= FALSE
;
359 TRACE("%s tag %s\n", add
? "Add" : "Skip", debugstr_an((const char *)&tag
, 4));
364 static HRESULT
d3dcompiler_strip_shader(const void *data
, SIZE_T data_size
, UINT flags
, ID3DBlob
**blob
)
366 struct dxbc src_dxbc
, dst_dxbc
;
372 WARN("NULL for blob specified\n");
376 if (!data
|| !data_size
)
378 WARN("Invalid arguments: data %p, data_size %lu\n", data
, data_size
);
379 return D3DERR_INVALIDCALL
;
382 hr
= dxbc_parse(data
, data_size
, &src_dxbc
);
385 WARN("Failed to parse blob part\n");
389 /* src_dxbc.count >= dst_dxbc.count */
390 hr
= dxbc_init(&dst_dxbc
, src_dxbc
.count
);
393 dxbc_destroy(&src_dxbc
);
394 WARN("Failed to init dxbc\n");
398 for (i
= 0; i
< src_dxbc
.count
; ++i
)
400 struct dxbc_section
*section
= &src_dxbc
.sections
[i
];
402 if (check_blob_strip(section
->tag
, flags
))
404 hr
= dxbc_add_section(&dst_dxbc
, section
->tag
, section
->data
, section
->data_size
);
407 dxbc_destroy(&src_dxbc
);
408 dxbc_destroy(&dst_dxbc
);
409 WARN("Failed to add section to dxbc\n");
415 hr
= dxbc_write_blob(&dst_dxbc
, blob
);
418 WARN("Failed to write blob part\n");
421 dxbc_destroy(&src_dxbc
);
422 dxbc_destroy(&dst_dxbc
);
427 HRESULT WINAPI
D3DGetBlobPart(const void *data
, SIZE_T data_size
, D3D_BLOB_PART part
, UINT flags
, ID3DBlob
**blob
)
429 TRACE("data %p, data_size %lu, part %s, flags %#x, blob %p\n", data
,
430 data_size
, debug_d3dcompiler_d3d_blob_part(part
), flags
, blob
);
432 return d3dcompiler_get_blob_part(data
, data_size
, part
, flags
, blob
);
435 HRESULT WINAPI
D3DGetInputSignatureBlob(const void *data
, SIZE_T data_size
, ID3DBlob
**blob
)
437 TRACE("data %p, data_size %lu, blob %p\n", data
, data_size
, blob
);
439 return d3dcompiler_get_blob_part(data
, data_size
, D3D_BLOB_INPUT_SIGNATURE_BLOB
, 0, blob
);
442 HRESULT WINAPI
D3DGetOutputSignatureBlob(const void *data
, SIZE_T data_size
, ID3DBlob
**blob
)
444 TRACE("data %p, data_size %lu, blob %p\n", data
, data_size
, blob
);
446 return d3dcompiler_get_blob_part(data
, data_size
, D3D_BLOB_OUTPUT_SIGNATURE_BLOB
, 0, blob
);
449 HRESULT WINAPI
D3DGetInputAndOutputSignatureBlob(const void *data
, SIZE_T data_size
, ID3DBlob
**blob
)
451 TRACE("data %p, data_size %lu, blob %p\n", data
, data_size
, blob
);
453 return d3dcompiler_get_blob_part(data
, data_size
, D3D_BLOB_INPUT_AND_OUTPUT_SIGNATURE_BLOB
, 0, blob
);
456 HRESULT WINAPI
D3DGetDebugInfo(const void *data
, SIZE_T data_size
, ID3DBlob
**blob
)
458 TRACE("data %p, data_size %lu, blob %p\n", data
, data_size
, blob
);
460 return d3dcompiler_get_blob_part(data
, data_size
, D3D_BLOB_DEBUG_INFO
, 0, blob
);
463 HRESULT WINAPI
D3DStripShader(const void *data
, SIZE_T data_size
, UINT flags
, ID3D10Blob
**blob
)
465 TRACE("data %p, data_size %lu, flags %#x, blob %p\n", data
, data_size
, flags
, blob
);
467 return d3dcompiler_strip_shader(data
, data_size
, flags
, blob
);