2 * Copyright 2016 Matteo Bruni for CodeWeavers
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 #include "d3dcompiler.h"
22 #include "wine/debug.h"
24 WINE_DEFAULT_DEBUG_CHANNEL(d3dx
);
26 struct asyncdataloader
28 ID3DX11DataLoader ID3DX11DataLoader_iface
;
46 static inline struct asyncdataloader
*impl_from_ID3DX11DataLoader(ID3DX11DataLoader
*iface
)
48 return CONTAINING_RECORD(iface
, struct asyncdataloader
, ID3DX11DataLoader_iface
);
51 static HRESULT WINAPI
memorydataloader_Load(ID3DX11DataLoader
*iface
)
53 TRACE("iface %p.\n", iface
);
57 static HRESULT WINAPI
memorydataloader_Decompress(ID3DX11DataLoader
*iface
, void **data
, SIZE_T
*size
)
59 struct asyncdataloader
*loader
= impl_from_ID3DX11DataLoader(iface
);
61 TRACE("iface %p, data %p, size %p.\n", iface
, data
, size
);
69 static HRESULT WINAPI
memorydataloader_Destroy(ID3DX11DataLoader
*iface
)
71 struct asyncdataloader
*loader
= impl_from_ID3DX11DataLoader(iface
);
73 TRACE("iface %p.\n", iface
);
79 static const ID3DX11DataLoaderVtbl memorydataloadervtbl
=
81 memorydataloader_Load
,
82 memorydataloader_Decompress
,
83 memorydataloader_Destroy
86 static HRESULT WINAPI
filedataloader_Load(ID3DX11DataLoader
*iface
)
88 struct asyncdataloader
*loader
= impl_from_ID3DX11DataLoader(iface
);
94 TRACE("iface %p.\n", iface
);
96 /* Always buffer file contents, even if Load() was already called. */
97 file
= CreateFileW(loader
->u
.file
.path
, GENERIC_READ
, FILE_SHARE_READ
| FILE_SHARE_WRITE
,
98 NULL
, OPEN_EXISTING
, FILE_ATTRIBUTE_NORMAL
, NULL
);
99 if (file
== INVALID_HANDLE_VALUE
)
100 return D3D11_ERROR_FILE_NOT_FOUND
;
102 size
= GetFileSize(file
, NULL
);
107 return E_OUTOFMEMORY
;
110 ret
= ReadFile(file
, data
, size
, &read_len
, NULL
);
114 ERR("Failed to read file contents.\n");
126 static HRESULT WINAPI
filedataloader_Decompress(ID3DX11DataLoader
*iface
, void **data
, SIZE_T
*size
)
128 struct asyncdataloader
*loader
= impl_from_ID3DX11DataLoader(iface
);
130 TRACE("iface %p, data %p, size %p.\n", iface
, data
, size
);
135 *data
= loader
->data
;
136 *size
= loader
->size
;
141 static HRESULT WINAPI
filedataloader_Destroy(ID3DX11DataLoader
*iface
)
143 struct asyncdataloader
*loader
= impl_from_ID3DX11DataLoader(iface
);
145 TRACE("iface %p.\n", iface
);
147 free(loader
->u
.file
.path
);
154 static const ID3DX11DataLoaderVtbl filedataloadervtbl
=
157 filedataloader_Decompress
,
158 filedataloader_Destroy
161 static HRESULT WINAPI
resourcedataloader_Load(ID3DX11DataLoader
*iface
)
163 struct asyncdataloader
*loader
= impl_from_ID3DX11DataLoader(iface
);
166 TRACE("iface %p.\n", iface
);
171 hglobal
= LoadResource(loader
->u
.resource
.module
, loader
->u
.resource
.rsrc
);
174 ERR("Failed to load resource.\n");
178 loader
->data
= LockResource(hglobal
);
179 loader
->size
= SizeofResource(loader
->u
.resource
.module
, loader
->u
.resource
.rsrc
);
184 static HRESULT WINAPI
resourcedataloader_Decompress(ID3DX11DataLoader
*iface
, void **data
, SIZE_T
*size
)
186 struct asyncdataloader
*loader
= impl_from_ID3DX11DataLoader(iface
);
188 TRACE("iface %p, data %p, size %p.\n", iface
, data
, size
);
193 *data
= loader
->data
;
194 *size
= loader
->size
;
199 static HRESULT WINAPI
resourcedataloader_Destroy(ID3DX11DataLoader
*iface
)
201 struct asyncdataloader
*loader
= impl_from_ID3DX11DataLoader(iface
);
203 TRACE("iface %p.\n", iface
);
210 static const ID3DX11DataLoaderVtbl resourcedataloadervtbl
=
212 resourcedataloader_Load
,
213 resourcedataloader_Decompress
,
214 resourcedataloader_Destroy
217 HRESULT WINAPI
D3DX11CompileFromMemory(const char *data
, SIZE_T data_size
, const char *filename
,
218 const D3D10_SHADER_MACRO
*defines
, ID3D10Include
*include
, const char *entry_point
,
219 const char *target
, UINT sflags
, UINT eflags
, ID3DX11ThreadPump
*pump
, ID3D10Blob
**shader
,
220 ID3D10Blob
**error_messages
, HRESULT
*hresult
)
222 TRACE("data %s, data_size %Iu, filename %s, defines %p, include %p, entry_point %s, target %s, "
223 "sflags %#x, eflags %#x, pump %p, shader %p, error_messages %p, hresult %p.\n",
224 debugstr_an(data
, data_size
), data_size
, debugstr_a(filename
), defines
, include
,
225 debugstr_a(entry_point
), debugstr_a(target
), sflags
, eflags
, pump
, shader
,
226 error_messages
, hresult
);
229 FIXME("Unimplemented ID3DX11ThreadPump handling.\n");
231 return D3DCompile(data
, data_size
, filename
, defines
, include
, entry_point
, target
,
232 sflags
, eflags
, shader
, error_messages
);
235 HRESULT WINAPI
D3DX11CompileFromFileA(const char *filename
, const D3D10_SHADER_MACRO
*defines
,
236 ID3D10Include
*include
, const char *entry_point
, const char *target
, UINT sflags
, UINT eflags
,
237 ID3DX11ThreadPump
*pump
, ID3D10Blob
**shader
, ID3D10Blob
**error_messages
, HRESULT
*hresult
)
239 WCHAR filename_w
[MAX_PATH
];
241 TRACE("filename %s, defines %p, include %p, entry_point %s, target %s, sflags %#x, "
242 "eflags %#x, pump %p, shader %p, error_messages %p, hresult %p.\n",
243 debugstr_a(filename
), defines
, include
, debugstr_a(entry_point
), debugstr_a(target
),
244 sflags
, eflags
, pump
, shader
, error_messages
, hresult
);
246 MultiByteToWideChar(CP_ACP
, 0, filename
, -1, filename_w
, ARRAY_SIZE(filename_w
));
248 return D3DX11CompileFromFileW(filename_w
, defines
, include
, entry_point
, target
,
249 sflags
, eflags
, pump
, shader
, error_messages
, hresult
);
252 HRESULT WINAPI
D3DX11CompileFromFileW(const WCHAR
*filename
, const D3D10_SHADER_MACRO
*defines
,
253 ID3D10Include
*include
, const char *entry_point
, const char *target
, UINT sflags
, UINT eflags
,
254 ID3DX11ThreadPump
*pump
, ID3D10Blob
**shader
, ID3D10Blob
**error_messages
, HRESULT
*hresult
)
258 TRACE("filename %s, defines %p, include %p, entry_point %s, target %s, sflags %#x, "
259 "eflags %#x, pump %p, shader %p, error_messages %p, hresult %p.\n",
260 debugstr_w(filename
), defines
, include
, debugstr_a(entry_point
), debugstr_a(target
),
261 sflags
, eflags
, pump
, shader
, error_messages
, hresult
);
264 FIXME("Unimplemented ID3DX11ThreadPump handling.\n");
267 include
= D3D_COMPILE_STANDARD_FILE_INCLUDE
;
269 hr
= D3DCompileFromFile(filename
, defines
, include
, entry_point
, target
, sflags
, eflags
, shader
, error_messages
);
276 HRESULT WINAPI
D3DX11CreateAsyncMemoryLoader(const void *data
, SIZE_T data_size
, ID3DX11DataLoader
**loader
)
278 struct asyncdataloader
*object
;
280 TRACE("data %p, data_size %Iu, loader %p.\n", data
, data_size
, loader
);
282 if (!data
|| !loader
)
285 object
= calloc(1, sizeof(*object
));
287 return E_OUTOFMEMORY
;
289 object
->ID3DX11DataLoader_iface
.lpVtbl
= &memorydataloadervtbl
;
290 object
->data
= (void *)data
;
291 object
->size
= data_size
;
293 *loader
= &object
->ID3DX11DataLoader_iface
;
298 HRESULT WINAPI
D3DX11CreateAsyncFileLoaderA(const char *filename
, ID3DX11DataLoader
**loader
)
304 TRACE("filename %s, loader %p.\n", debugstr_a(filename
), loader
);
306 if (!filename
|| !loader
)
309 len
= MultiByteToWideChar(CP_ACP
, 0, filename
, -1, NULL
, 0);
310 filename_w
= malloc(len
* sizeof(*filename_w
));
311 MultiByteToWideChar(CP_ACP
, 0, filename
, -1, filename_w
, len
);
313 hr
= D3DX11CreateAsyncFileLoaderW(filename_w
, loader
);
320 HRESULT WINAPI
D3DX11CreateAsyncFileLoaderW(const WCHAR
*filename
, ID3DX11DataLoader
**loader
)
322 struct asyncdataloader
*object
;
324 TRACE("filename %s, loader %p.\n", debugstr_w(filename
), loader
);
326 if (!filename
|| !loader
)
329 object
= calloc(1, sizeof(*object
));
331 return E_OUTOFMEMORY
;
333 object
->ID3DX11DataLoader_iface
.lpVtbl
= &filedataloadervtbl
;
334 object
->u
.file
.path
= malloc((lstrlenW(filename
) + 1) * sizeof(WCHAR
));
335 if (!object
->u
.file
.path
)
338 return E_OUTOFMEMORY
;
340 lstrcpyW(object
->u
.file
.path
, filename
);
344 *loader
= &object
->ID3DX11DataLoader_iface
;
349 HRESULT WINAPI
D3DX11CreateAsyncResourceLoaderA(HMODULE module
, const char *resource
, ID3DX11DataLoader
**loader
)
351 struct asyncdataloader
*object
;
354 TRACE("module %p, resource %s, loader %p.\n", module
, debugstr_a(resource
), loader
);
359 object
= calloc(1, sizeof(*object
));
361 return E_OUTOFMEMORY
;
363 if (!(rsrc
= FindResourceA(module
, resource
, (const char *)RT_RCDATA
)))
365 WARN("Failed to find resource.\n");
367 return D3DX11_ERR_INVALID_DATA
;
370 object
->ID3DX11DataLoader_iface
.lpVtbl
= &resourcedataloadervtbl
;
371 object
->u
.resource
.module
= module
;
372 object
->u
.resource
.rsrc
= rsrc
;
376 *loader
= &object
->ID3DX11DataLoader_iface
;
381 HRESULT WINAPI
D3DX11CreateAsyncResourceLoaderW(HMODULE module
, const WCHAR
*resource
, ID3DX11DataLoader
**loader
)
383 struct asyncdataloader
*object
;
386 TRACE("module %p, resource %s, loader %p.\n", module
, debugstr_w(resource
), loader
);
391 object
= calloc(1, sizeof(*object
));
393 return E_OUTOFMEMORY
;
395 if (!(rsrc
= FindResourceW(module
, resource
, (const WCHAR
*)RT_RCDATA
)))
397 WARN("Failed to find resource.\n");
399 return D3DX11_ERR_INVALID_DATA
;
402 object
->ID3DX11DataLoader_iface
.lpVtbl
= &resourcedataloadervtbl
;
403 object
->u
.resource
.module
= module
;
404 object
->u
.resource
.rsrc
= rsrc
;
408 *loader
= &object
->ID3DX11DataLoader_iface
;