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 /* IUnknown methods */
31 static HRESULT STDMETHODCALLTYPE d3dcompiler_blob_QueryInterface(ID3DBlob *iface, REFIID riid, void **object)
33 TRACE("iface %p, riid %s, object %p\n", iface, debugstr_guid(riid), object);
35 if (IsEqualGUID(riid, &IID_ID3D10Blob)
36 || IsEqualGUID(riid, &IID_IUnknown))
38 IUnknown_AddRef(iface);
43 WARN("%s not implemented, returning E_NOINTERFACE\n", debugstr_guid(riid));
49 static ULONG STDMETHODCALLTYPE d3dcompiler_blob_AddRef(ID3DBlob *iface)
51 struct d3dcompiler_blob *blob = (struct d3dcompiler_blob *)iface;
52 ULONG refcount = InterlockedIncrement(&blob->refcount);
54 TRACE("%p increasing refcount to %u\n", blob, refcount);
59 static ULONG STDMETHODCALLTYPE d3dcompiler_blob_Release(ID3DBlob *iface)
61 struct d3dcompiler_blob *blob = (struct d3dcompiler_blob *)iface;
62 ULONG refcount = InterlockedDecrement(&blob->refcount);
64 TRACE("%p decreasing refcount to %u\n", blob, refcount);
68 HeapFree(GetProcessHeap(), 0, blob->data);
69 HeapFree(GetProcessHeap(), 0, blob);
75 /* ID3DBlob methods */
77 static void * STDMETHODCALLTYPE d3dcompiler_blob_GetBufferPointer(ID3DBlob *iface)
79 struct d3dcompiler_blob *blob = (struct d3dcompiler_blob *)iface;
81 TRACE("iface %p\n", iface);
86 static SIZE_T STDMETHODCALLTYPE d3dcompiler_blob_GetBufferSize(ID3DBlob *iface)
88 struct d3dcompiler_blob *blob = (struct d3dcompiler_blob *)iface;
90 TRACE("iface %p\n", iface);
95 static const struct ID3D10BlobVtbl d3dcompiler_blob_vtbl =
97 /* IUnknown methods */
98 d3dcompiler_blob_QueryInterface,
99 d3dcompiler_blob_AddRef,
100 d3dcompiler_blob_Release,
101 /* ID3DBlob methods */
102 d3dcompiler_blob_GetBufferPointer,
103 d3dcompiler_blob_GetBufferSize,
106 HRESULT d3dcompiler_blob_init(struct d3dcompiler_blob *blob, SIZE_T data_size)
108 blob->vtbl = &d3dcompiler_blob_vtbl;
110 blob->size = data_size;
112 blob->data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, data_size);
115 ERR("Failed to allocate D3D blob data memory\n");
116 return E_OUTOFMEMORY;
122 static BOOL check_blob_part(DWORD tag, D3D_BLOB_PART part)
128 case D3D_BLOB_INPUT_SIGNATURE_BLOB:
129 if (tag == TAG_ISGN) add = TRUE;
132 case D3D_BLOB_OUTPUT_SIGNATURE_BLOB:
133 if (tag == TAG_OSGN) add = TRUE;
136 case D3D_BLOB_INPUT_AND_OUTPUT_SIGNATURE_BLOB:
137 if (tag == TAG_ISGN || tag == TAG_OSGN) add = TRUE;
140 case D3D_BLOB_PATCH_CONSTANT_SIGNATURE_BLOB:
141 if (tag == TAG_PCSG) add = TRUE;
144 case D3D_BLOB_ALL_SIGNATURE_BLOB:
145 if (tag == TAG_ISGN || tag == TAG_OSGN || tag == TAG_PCSG) add = TRUE;
148 case D3D_BLOB_DEBUG_INFO:
149 if (tag == TAG_SDBG) add = TRUE;
152 case D3D_BLOB_LEGACY_SHADER:
153 if (tag == TAG_Aon9) add = TRUE;
156 case D3D_BLOB_XNA_PREPASS_SHADER:
157 if (tag == TAG_XNAP) add = TRUE;
160 case D3D_BLOB_XNA_SHADER:
161 if (tag == TAG_XNAS) add = TRUE;
165 FIXME("Unhandled D3D_BLOB_PART %s.\n", debug_d3dcompiler_d3d_blob_part(part));
169 TRACE("%s tag %s\n", add ? "Add" : "Skip", debugstr_an((const char *)&tag, 4));
174 HRESULT d3dcompiler_get_blob_part(const void *data, SIZE_T data_size, D3D_BLOB_PART part, UINT flags, ID3DBlob **blob)
176 struct dxbc src_dxbc, dst_dxbc;
178 unsigned int i, count;
180 if (!data || !data_size || flags || !blob)
182 WARN("Invalid arguments: data %p, data_size %lu, flags %#x, blob %p\n", data, data_size, flags, blob);
183 return D3DERR_INVALIDCALL;
186 if (part > D3D_BLOB_TEST_COMPILE_PERF
187 || (part < D3D_BLOB_TEST_ALTERNATE_SHADER && part > D3D_BLOB_XNA_SHADER))
189 WARN("Invalid D3D_BLOB_PART: part %s\n", debug_d3dcompiler_d3d_blob_part(part));
190 return D3DERR_INVALIDCALL;
193 hr = dxbc_parse(data, data_size, &src_dxbc);
196 WARN("Failed to parse blob part\n");
200 hr = dxbc_init(&dst_dxbc, 0);
203 dxbc_destroy(&src_dxbc);
204 WARN("Failed to init dxbc\n");
208 for (i = 0; i < src_dxbc.count; ++i)
210 struct dxbc_section *section = &src_dxbc.sections[i];
212 if (check_blob_part(section->tag, part))
214 hr = dxbc_add_section(&dst_dxbc, section->tag, section->data, section->data_size);
217 dxbc_destroy(&src_dxbc);
218 dxbc_destroy(&dst_dxbc);
219 WARN("Failed to add section to dxbc\n");
225 count = dst_dxbc.count;
229 case D3D_BLOB_INPUT_SIGNATURE_BLOB:
230 case D3D_BLOB_OUTPUT_SIGNATURE_BLOB:
231 case D3D_BLOB_PATCH_CONSTANT_SIGNATURE_BLOB:
232 case D3D_BLOB_DEBUG_INFO:
233 case D3D_BLOB_LEGACY_SHADER:
234 case D3D_BLOB_XNA_PREPASS_SHADER:
235 case D3D_BLOB_XNA_SHADER:
236 if (count != 1) count = 0;
239 case D3D_BLOB_INPUT_AND_OUTPUT_SIGNATURE_BLOB:
240 if (count != 2) count = 0;
243 case D3D_BLOB_ALL_SIGNATURE_BLOB:
244 if (count != 3) count = 0;
248 FIXME("Unhandled D3D_BLOB_PART %s.\n", debug_d3dcompiler_d3d_blob_part(part));
254 dxbc_destroy(&src_dxbc);
255 dxbc_destroy(&dst_dxbc);
256 WARN("Nothing to write into the blob (count = 0)\n");
260 /* some parts aren't full DXBCs, they contain only the data */
261 if (count == 1 && (part == D3D_BLOB_DEBUG_INFO || part == D3D_BLOB_LEGACY_SHADER || part == D3D_BLOB_XNA_PREPASS_SHADER
262 || part == D3D_BLOB_XNA_SHADER))
264 hr = D3DCreateBlob(dst_dxbc.sections[0].data_size, blob);
267 memcpy(ID3D10Blob_GetBufferPointer(*blob), dst_dxbc.sections[0].data, dst_dxbc.sections[0].data_size);
271 WARN("Could not create blob\n");
276 hr = dxbc_write_blob(&dst_dxbc, blob);
279 WARN("Failed to write blob part\n");
283 dxbc_destroy(&src_dxbc);
284 dxbc_destroy(&dst_dxbc);
289 static BOOL check_blob_strip(DWORD tag, UINT flags)
293 if (flags & D3DCOMPILER_STRIP_TEST_BLOBS) FIXME("Unhandled flag D3DCOMPILER_STRIP_TEST_BLOBS.\n");
299 if (flags & D3DCOMPILER_STRIP_REFLECTION_DATA) add = FALSE;
303 if (flags & D3DCOMPILER_STRIP_DEBUG_INFO) add = FALSE;
310 TRACE("%s tag %s\n", add ? "Add" : "Skip", debugstr_an((const char *)&tag, 4));
315 HRESULT d3dcompiler_strip_shader(const void *data, SIZE_T data_size, UINT flags, ID3DBlob **blob)
317 struct dxbc src_dxbc, dst_dxbc;
323 WARN("NULL for blob specified\n");
327 if (!data || !data_size)
329 WARN("Invalid arguments: data %p, data_size %lu\n", data, data_size);
330 return D3DERR_INVALIDCALL;
333 hr = dxbc_parse(data, data_size, &src_dxbc);
336 WARN("Failed to parse blob part\n");
340 /* src_dxbc.count >= dst_dxbc.count */
341 hr = dxbc_init(&dst_dxbc, src_dxbc.count);
344 dxbc_destroy(&src_dxbc);
345 WARN("Failed to init dxbc\n");
349 for (i = 0; i < src_dxbc.count; ++i)
351 struct dxbc_section *section = &src_dxbc.sections[i];
353 if (check_blob_strip(section->tag, flags))
355 hr = dxbc_add_section(&dst_dxbc, section->tag, section->data, section->data_size);
358 dxbc_destroy(&src_dxbc);
359 dxbc_destroy(&dst_dxbc);
360 WARN("Failed to add section to dxbc\n");
366 hr = dxbc_write_blob(&dst_dxbc, blob);
369 WARN("Failed to write blob part\n");
372 dxbc_destroy(&src_dxbc);
373 dxbc_destroy(&dst_dxbc);