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 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_DEBUG_INFO
:
141 if (tag
== TAG_SDBG
) add
= TRUE
;
145 FIXME("Unhandled D3D_BLOB_PART %s.\n", debug_d3dcompiler_d3d_blob_part(part
));
149 TRACE("%s tag %s\n", add
? "Add" : "Skip", debugstr_an((const char *)&tag
, 4));
154 HRESULT
d3dcompiler_get_blob_part(const void *data
, SIZE_T data_size
, D3D_BLOB_PART part
, UINT flags
, ID3DBlob
**blob
)
156 struct dxbc src_dxbc
, dst_dxbc
;
158 unsigned int i
, count
;
160 if (!data
|| !data_size
|| flags
|| !blob
)
162 WARN("Invalid arguments: data %p, data_size %lu, flags %#x, blob %p\n", data
, data_size
, flags
, blob
);
163 return D3DERR_INVALIDCALL
;
166 if (part
> D3D_BLOB_TEST_COMPILE_PERF
167 || (part
< D3D_BLOB_TEST_ALTERNATE_SHADER
&& part
> D3D_BLOB_XNA_SHADER
))
169 WARN("Invalid D3D_BLOB_PART: part %s\n", debug_d3dcompiler_d3d_blob_part(part
));
170 return D3DERR_INVALIDCALL
;
173 hr
= dxbc_parse(data
, data_size
, &src_dxbc
);
176 WARN("Failed to parse blob part\n");
180 hr
= dxbc_init(&dst_dxbc
, 0);
183 dxbc_destroy(&src_dxbc
);
184 WARN("Failed to init dxbc\n");
188 for (i
= 0; i
< src_dxbc
.count
; ++i
)
190 struct dxbc_section
*section
= &src_dxbc
.sections
[i
];
192 if (check_blob_part(section
->tag
, part
))
194 hr
= dxbc_add_section(&dst_dxbc
, section
->tag
, section
->data
, section
->data_size
);
197 dxbc_destroy(&src_dxbc
);
198 dxbc_destroy(&dst_dxbc
);
199 WARN("Failed to add section to dxbc\n");
205 count
= dst_dxbc
.count
;
209 case D3D_BLOB_INPUT_SIGNATURE_BLOB
:
210 case D3D_BLOB_OUTPUT_SIGNATURE_BLOB
:
211 case D3D_BLOB_DEBUG_INFO
:
212 if (count
!= 1) count
= 0;
215 case D3D_BLOB_INPUT_AND_OUTPUT_SIGNATURE_BLOB
:
216 if (count
!= 2) count
= 0;
220 FIXME("Unhandled D3D_BLOB_PART %s.\n", debug_d3dcompiler_d3d_blob_part(part
));
226 dxbc_destroy(&src_dxbc
);
227 dxbc_destroy(&dst_dxbc
);
228 WARN("Nothing to write into the blob (count = 0)\n");
232 /* some parts aren't full DXBCs, they contain only the data */
233 if (count
== 1 && (part
== D3D_BLOB_DEBUG_INFO
))
235 hr
= D3DCreateBlob(dst_dxbc
.sections
[0].data_size
, blob
);
238 memcpy(ID3D10Blob_GetBufferPointer(*blob
), dst_dxbc
.sections
[0].data
, dst_dxbc
.sections
[0].data_size
);
242 WARN("Could not create blob\n");
247 hr
= dxbc_write_blob(&dst_dxbc
, blob
);
250 WARN("Failed to write blob part\n");
254 dxbc_destroy(&src_dxbc
);
255 dxbc_destroy(&dst_dxbc
);