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 static inline struct d3dcompiler_blob
*impl_from_ID3DBlob(ID3DBlob
*iface
)
31 return CONTAINING_RECORD(iface
, struct d3dcompiler_blob
, ID3DBlob_iface
);
34 /* IUnknown methods */
36 static HRESULT STDMETHODCALLTYPE
d3dcompiler_blob_QueryInterface(ID3DBlob
*iface
, REFIID riid
, void **object
)
38 TRACE("iface %p, riid %s, object %p\n", iface
, debugstr_guid(riid
), object
);
40 if (IsEqualGUID(riid
, &IID_ID3D10Blob
)
41 || IsEqualGUID(riid
, &IID_IUnknown
))
43 IUnknown_AddRef(iface
);
48 WARN("%s not implemented, returning E_NOINTERFACE\n", debugstr_guid(riid
));
54 static ULONG STDMETHODCALLTYPE
d3dcompiler_blob_AddRef(ID3DBlob
*iface
)
56 struct d3dcompiler_blob
*blob
= impl_from_ID3DBlob(iface
);
57 ULONG refcount
= InterlockedIncrement(&blob
->refcount
);
59 TRACE("%p increasing refcount to %u\n", blob
, refcount
);
64 static ULONG STDMETHODCALLTYPE
d3dcompiler_blob_Release(ID3DBlob
*iface
)
66 struct d3dcompiler_blob
*blob
= impl_from_ID3DBlob(iface
);
67 ULONG refcount
= InterlockedDecrement(&blob
->refcount
);
69 TRACE("%p decreasing refcount to %u\n", blob
, refcount
);
73 HeapFree(GetProcessHeap(), 0, blob
->data
);
74 HeapFree(GetProcessHeap(), 0, blob
);
80 /* ID3DBlob methods */
82 static void * STDMETHODCALLTYPE
d3dcompiler_blob_GetBufferPointer(ID3DBlob
*iface
)
84 struct d3dcompiler_blob
*blob
= impl_from_ID3DBlob(iface
);
86 TRACE("iface %p\n", iface
);
91 static SIZE_T STDMETHODCALLTYPE
d3dcompiler_blob_GetBufferSize(ID3DBlob
*iface
)
93 struct d3dcompiler_blob
*blob
= impl_from_ID3DBlob(iface
);
95 TRACE("iface %p\n", iface
);
100 static const struct ID3D10BlobVtbl d3dcompiler_blob_vtbl
=
102 /* IUnknown methods */
103 d3dcompiler_blob_QueryInterface
,
104 d3dcompiler_blob_AddRef
,
105 d3dcompiler_blob_Release
,
106 /* ID3DBlob methods */
107 d3dcompiler_blob_GetBufferPointer
,
108 d3dcompiler_blob_GetBufferSize
,
111 HRESULT
d3dcompiler_blob_init(struct d3dcompiler_blob
*blob
, SIZE_T data_size
)
113 blob
->ID3DBlob_iface
.lpVtbl
= &d3dcompiler_blob_vtbl
;
115 blob
->size
= data_size
;
117 blob
->data
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, data_size
);
120 ERR("Failed to allocate D3D blob data memory\n");
121 return E_OUTOFMEMORY
;
127 static BOOL
check_blob_part(DWORD tag
, D3D_BLOB_PART part
)
133 case D3D_BLOB_INPUT_SIGNATURE_BLOB
:
134 if (tag
== TAG_ISGN
) add
= TRUE
;
137 case D3D_BLOB_OUTPUT_SIGNATURE_BLOB
:
138 if (tag
== TAG_OSGN
|| tag
== TAG_OSG5
) add
= TRUE
;
141 case D3D_BLOB_INPUT_AND_OUTPUT_SIGNATURE_BLOB
:
142 if (tag
== TAG_ISGN
|| tag
== TAG_OSGN
|| tag
== TAG_OSG5
) add
= TRUE
;
145 case D3D_BLOB_PATCH_CONSTANT_SIGNATURE_BLOB
:
146 if (tag
== TAG_PCSG
) add
= TRUE
;
149 case D3D_BLOB_ALL_SIGNATURE_BLOB
:
150 if (tag
== TAG_ISGN
|| tag
== TAG_OSGN
|| tag
== TAG_OSG5
|| tag
== TAG_PCSG
) add
= TRUE
;
153 case D3D_BLOB_DEBUG_INFO
:
154 if (tag
== TAG_SDBG
) add
= TRUE
;
157 case D3D_BLOB_LEGACY_SHADER
:
158 if (tag
== TAG_Aon9
) add
= TRUE
;
161 case D3D_BLOB_XNA_PREPASS_SHADER
:
162 if (tag
== TAG_XNAP
) add
= TRUE
;
165 case D3D_BLOB_XNA_SHADER
:
166 if (tag
== TAG_XNAS
) add
= TRUE
;
170 FIXME("Unhandled D3D_BLOB_PART %s.\n", debug_d3dcompiler_d3d_blob_part(part
));
174 TRACE("%s tag %s\n", add
? "Add" : "Skip", debugstr_an((const char *)&tag
, 4));
179 HRESULT
d3dcompiler_get_blob_part(const void *data
, SIZE_T data_size
, D3D_BLOB_PART part
, UINT flags
, ID3DBlob
**blob
)
181 struct dxbc src_dxbc
, dst_dxbc
;
183 unsigned int i
, count
;
185 if (!data
|| !data_size
|| flags
|| !blob
)
187 WARN("Invalid arguments: data %p, data_size %lu, flags %#x, blob %p\n", data
, data_size
, flags
, blob
);
188 return D3DERR_INVALIDCALL
;
191 if (part
> D3D_BLOB_TEST_COMPILE_PERF
192 || (part
< D3D_BLOB_TEST_ALTERNATE_SHADER
&& part
> D3D_BLOB_XNA_SHADER
))
194 WARN("Invalid D3D_BLOB_PART: part %s\n", debug_d3dcompiler_d3d_blob_part(part
));
195 return D3DERR_INVALIDCALL
;
198 hr
= dxbc_parse(data
, data_size
, &src_dxbc
);
201 WARN("Failed to parse blob part\n");
205 hr
= dxbc_init(&dst_dxbc
, 0);
208 dxbc_destroy(&src_dxbc
);
209 WARN("Failed to init dxbc\n");
213 for (i
= 0; i
< src_dxbc
.count
; ++i
)
215 struct dxbc_section
*section
= &src_dxbc
.sections
[i
];
217 if (check_blob_part(section
->tag
, part
))
219 hr
= dxbc_add_section(&dst_dxbc
, section
->tag
, section
->data
, section
->data_size
);
222 dxbc_destroy(&src_dxbc
);
223 dxbc_destroy(&dst_dxbc
);
224 WARN("Failed to add section to dxbc\n");
230 count
= dst_dxbc
.count
;
234 case D3D_BLOB_INPUT_SIGNATURE_BLOB
:
235 case D3D_BLOB_OUTPUT_SIGNATURE_BLOB
:
236 case D3D_BLOB_PATCH_CONSTANT_SIGNATURE_BLOB
:
237 case D3D_BLOB_DEBUG_INFO
:
238 case D3D_BLOB_LEGACY_SHADER
:
239 case D3D_BLOB_XNA_PREPASS_SHADER
:
240 case D3D_BLOB_XNA_SHADER
:
241 if (count
!= 1) count
= 0;
244 case D3D_BLOB_INPUT_AND_OUTPUT_SIGNATURE_BLOB
:
245 if (count
!= 2) count
= 0;
248 case D3D_BLOB_ALL_SIGNATURE_BLOB
:
249 if (count
!= 3) count
= 0;
253 FIXME("Unhandled D3D_BLOB_PART %s.\n", debug_d3dcompiler_d3d_blob_part(part
));
259 dxbc_destroy(&src_dxbc
);
260 dxbc_destroy(&dst_dxbc
);
261 WARN("Nothing to write into the blob (count = 0)\n");
265 /* some parts aren't full DXBCs, they contain only the data */
266 if (count
== 1 && (part
== D3D_BLOB_DEBUG_INFO
|| part
== D3D_BLOB_LEGACY_SHADER
|| part
== D3D_BLOB_XNA_PREPASS_SHADER
267 || part
== D3D_BLOB_XNA_SHADER
))
269 hr
= D3DCreateBlob(dst_dxbc
.sections
[0].data_size
, blob
);
272 memcpy(ID3D10Blob_GetBufferPointer(*blob
), dst_dxbc
.sections
[0].data
, dst_dxbc
.sections
[0].data_size
);
276 WARN("Could not create blob\n");
281 hr
= dxbc_write_blob(&dst_dxbc
, blob
);
284 WARN("Failed to write blob part\n");
288 dxbc_destroy(&src_dxbc
);
289 dxbc_destroy(&dst_dxbc
);
294 static BOOL
check_blob_strip(DWORD tag
, UINT flags
)
298 if (flags
& D3DCOMPILER_STRIP_TEST_BLOBS
) FIXME("Unhandled flag D3DCOMPILER_STRIP_TEST_BLOBS.\n");
304 if (flags
& D3DCOMPILER_STRIP_REFLECTION_DATA
) add
= FALSE
;
308 if (flags
& D3DCOMPILER_STRIP_DEBUG_INFO
) add
= FALSE
;
315 TRACE("%s tag %s\n", add
? "Add" : "Skip", debugstr_an((const char *)&tag
, 4));
320 HRESULT
d3dcompiler_strip_shader(const void *data
, SIZE_T data_size
, UINT flags
, ID3DBlob
**blob
)
322 struct dxbc src_dxbc
, dst_dxbc
;
328 WARN("NULL for blob specified\n");
332 if (!data
|| !data_size
)
334 WARN("Invalid arguments: data %p, data_size %lu\n", data
, data_size
);
335 return D3DERR_INVALIDCALL
;
338 hr
= dxbc_parse(data
, data_size
, &src_dxbc
);
341 WARN("Failed to parse blob part\n");
345 /* src_dxbc.count >= dst_dxbc.count */
346 hr
= dxbc_init(&dst_dxbc
, src_dxbc
.count
);
349 dxbc_destroy(&src_dxbc
);
350 WARN("Failed to init dxbc\n");
354 for (i
= 0; i
< src_dxbc
.count
; ++i
)
356 struct dxbc_section
*section
= &src_dxbc
.sections
[i
];
358 if (check_blob_strip(section
->tag
, flags
))
360 hr
= dxbc_add_section(&dst_dxbc
, section
->tag
, section
->data
, section
->data_size
);
363 dxbc_destroy(&src_dxbc
);
364 dxbc_destroy(&dst_dxbc
);
365 WARN("Failed to add section to dxbc\n");
371 hr
= dxbc_write_blob(&dst_dxbc
, blob
);
374 WARN("Failed to write blob part\n");
377 dxbc_destroy(&src_dxbc
);
378 dxbc_destroy(&dst_dxbc
);