d3dcompiler: Add D3D_BLOB_XNA_SHADER to D3DGetBlobPart().
[wine.git] / dlls / d3dcompiler_43 / blob.c
blobdc0a65f0fa9807d49055f3f6bbed1d380672c8fa
1 /*
2 * Direct3D blob file
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
22 #include "config.h"
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);
39 *object = iface;
40 return S_OK;
43 WARN("%s not implemented, returning E_NOINTERFACE\n", debugstr_guid(riid));
45 *object = NULL;
46 return E_NOINTERFACE;
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);
56 return 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);
66 if (!refcount)
68 HeapFree(GetProcessHeap(), 0, blob->data);
69 HeapFree(GetProcessHeap(), 0, blob);
72 return refcount;
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);
83 return blob->data;
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);
92 return blob->size;
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;
109 blob->refcount = 1;
110 blob->size = data_size;
112 blob->data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, data_size);
113 if (!blob->data)
115 ERR("Failed to allocate D3D blob data memory\n");
116 return E_OUTOFMEMORY;
119 return S_OK;
122 static BOOL check_blob_part(DWORD tag, D3D_BLOB_PART part)
124 BOOL add = FALSE;
126 switch(part)
128 case D3D_BLOB_INPUT_SIGNATURE_BLOB:
129 if (tag == TAG_ISGN) add = TRUE;
130 break;
132 case D3D_BLOB_OUTPUT_SIGNATURE_BLOB:
133 if (tag == TAG_OSGN) add = TRUE;
134 break;
136 case D3D_BLOB_INPUT_AND_OUTPUT_SIGNATURE_BLOB:
137 if (tag == TAG_ISGN || tag == TAG_OSGN) add = TRUE;
138 break;
140 case D3D_BLOB_PATCH_CONSTANT_SIGNATURE_BLOB:
141 if (tag == TAG_PCSG) add = TRUE;
142 break;
144 case D3D_BLOB_ALL_SIGNATURE_BLOB:
145 if (tag == TAG_ISGN || tag == TAG_OSGN || tag == TAG_PCSG) add = TRUE;
146 break;
148 case D3D_BLOB_DEBUG_INFO:
149 if (tag == TAG_SDBG) add = TRUE;
150 break;
152 case D3D_BLOB_LEGACY_SHADER:
153 if (tag == TAG_Aon9) add = TRUE;
154 break;
156 case D3D_BLOB_XNA_PREPASS_SHADER:
157 if (tag == TAG_XNAP) add = TRUE;
158 break;
160 case D3D_BLOB_XNA_SHADER:
161 if (tag == TAG_XNAS) add = TRUE;
162 break;
164 default:
165 FIXME("Unhandled D3D_BLOB_PART %s.\n", debug_d3dcompiler_d3d_blob_part(part));
166 break;
169 TRACE("%s tag %s\n", add ? "Add" : "Skip", debugstr_an((const char *)&tag, 4));
171 return add;
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;
177 HRESULT hr;
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);
194 if (FAILED(hr))
196 WARN("Failed to parse blob part\n");
197 return hr;
200 hr = dxbc_init(&dst_dxbc, 0);
201 if (FAILED(hr))
203 dxbc_destroy(&src_dxbc);
204 WARN("Failed to init dxbc\n");
205 return hr;
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);
215 if (FAILED(hr))
217 dxbc_destroy(&src_dxbc);
218 dxbc_destroy(&dst_dxbc);
219 WARN("Failed to add section to dxbc\n");
220 return hr;
225 count = dst_dxbc.count;
227 switch(part)
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;
237 break;
239 case D3D_BLOB_INPUT_AND_OUTPUT_SIGNATURE_BLOB:
240 if (count != 2) count = 0;
241 break;
243 case D3D_BLOB_ALL_SIGNATURE_BLOB:
244 if (count != 3) count = 0;
245 break;
247 default:
248 FIXME("Unhandled D3D_BLOB_PART %s.\n", debug_d3dcompiler_d3d_blob_part(part));
249 break;
252 if (count == 0)
254 dxbc_destroy(&src_dxbc);
255 dxbc_destroy(&dst_dxbc);
256 WARN("Nothing to write into the blob (count = 0)\n");
257 return E_FAIL;
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);
265 if (SUCCEEDED(hr))
267 memcpy(ID3D10Blob_GetBufferPointer(*blob), dst_dxbc.sections[0].data, dst_dxbc.sections[0].data_size);
269 else
271 WARN("Could not create blob\n");
274 else
276 hr = dxbc_write_blob(&dst_dxbc, blob);
277 if (FAILED(hr))
279 WARN("Failed to write blob part\n");
283 dxbc_destroy(&src_dxbc);
284 dxbc_destroy(&dst_dxbc);
286 return hr;