dmusic: Avoid swallowing collection Load failures.
[wine.git] / dlls / d3dx11_43 / async.c
blob074a8529e4ef00a09dbd889d301b6a9468861bf8
1 /*
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
19 #include "d3dx11.h"
20 #include "d3dcompiler.h"
22 #include "wine/debug.h"
24 WINE_DEFAULT_DEBUG_CHANNEL(d3dx);
26 struct asyncdataloader
28 ID3DX11DataLoader ID3DX11DataLoader_iface;
30 union
32 struct
34 WCHAR *path;
35 } file;
36 struct
38 HMODULE module;
39 HRSRC rsrc;
40 } resource;
41 } u;
42 void *data;
43 SIZE_T size;
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);
54 return S_OK;
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);
63 *data = loader->data;
64 *size = loader->size;
66 return S_OK;
69 static HRESULT WINAPI memorydataloader_Destroy(ID3DX11DataLoader *iface)
71 struct asyncdataloader *loader = impl_from_ID3DX11DataLoader(iface);
73 TRACE("iface %p.\n", iface);
75 free(loader);
76 return S_OK;
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);
89 DWORD size, read_len;
90 HANDLE file;
91 void *data;
92 BOOL ret;
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);
103 data = malloc(size);
104 if (!data)
106 CloseHandle(file);
107 return E_OUTOFMEMORY;
110 ret = ReadFile(file, data, size, &read_len, NULL);
111 CloseHandle(file);
112 if (!ret)
114 ERR("Failed to read file contents.\n");
115 free(data);
116 return E_FAIL;
119 free(loader->data);
120 loader->data = data;
121 loader->size = size;
123 return S_OK;
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);
132 if (!loader->data)
133 return E_FAIL;
135 *data = loader->data;
136 *size = loader->size;
138 return S_OK;
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);
148 free(loader->data);
149 free(loader);
151 return S_OK;
154 static const ID3DX11DataLoaderVtbl filedataloadervtbl =
156 filedataloader_Load,
157 filedataloader_Decompress,
158 filedataloader_Destroy
161 static HRESULT WINAPI resourcedataloader_Load(ID3DX11DataLoader *iface)
163 struct asyncdataloader *loader = impl_from_ID3DX11DataLoader(iface);
164 HGLOBAL hglobal;
166 TRACE("iface %p.\n", iface);
168 if (loader->data)
169 return S_OK;
171 hglobal = LoadResource(loader->u.resource.module, loader->u.resource.rsrc);
172 if (!hglobal)
174 ERR("Failed to load resource.\n");
175 return E_FAIL;
178 loader->data = LockResource(hglobal);
179 loader->size = SizeofResource(loader->u.resource.module, loader->u.resource.rsrc);
181 return S_OK;
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);
190 if (!loader->data)
191 return E_FAIL;
193 *data = loader->data;
194 *size = loader->size;
196 return S_OK;
199 static HRESULT WINAPI resourcedataloader_Destroy(ID3DX11DataLoader *iface)
201 struct asyncdataloader *loader = impl_from_ID3DX11DataLoader(iface);
203 TRACE("iface %p.\n", iface);
205 free(loader);
207 return S_OK;
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);
228 if (pump)
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)
256 HRESULT hr;
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);
263 if (pump)
264 FIXME("Unimplemented ID3DX11ThreadPump handling.\n");
266 if (!include)
267 include = D3D_COMPILE_STANDARD_FILE_INCLUDE;
269 hr = D3DCompileFromFile(filename, defines, include, entry_point, target, sflags, eflags, shader, error_messages);
270 if (hresult)
271 *hresult = hr;
273 return hr;
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)
283 return E_FAIL;
285 object = calloc(1, sizeof(*object));
286 if (!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;
295 return S_OK;
298 HRESULT WINAPI D3DX11CreateAsyncFileLoaderA(const char *filename, ID3DX11DataLoader **loader)
300 WCHAR *filename_w;
301 HRESULT hr;
302 int len;
304 TRACE("filename %s, loader %p.\n", debugstr_a(filename), loader);
306 if (!filename || !loader)
307 return E_FAIL;
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);
315 free(filename_w);
317 return hr;
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)
327 return E_FAIL;
329 object = calloc(1, sizeof(*object));
330 if (!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)
337 free(object);
338 return E_OUTOFMEMORY;
340 lstrcpyW(object->u.file.path, filename);
341 object->data = NULL;
342 object->size = 0;
344 *loader = &object->ID3DX11DataLoader_iface;
346 return S_OK;
349 HRESULT WINAPI D3DX11CreateAsyncResourceLoaderA(HMODULE module, const char *resource, ID3DX11DataLoader **loader)
351 struct asyncdataloader *object;
352 HRSRC rsrc;
354 TRACE("module %p, resource %s, loader %p.\n", module, debugstr_a(resource), loader);
356 if (!loader)
357 return E_FAIL;
359 object = calloc(1, sizeof(*object));
360 if (!object)
361 return E_OUTOFMEMORY;
363 if (!(rsrc = FindResourceA(module, resource, (const char *)RT_RCDATA)))
365 WARN("Failed to find resource.\n");
366 free(object);
367 return D3DX11_ERR_INVALID_DATA;
370 object->ID3DX11DataLoader_iface.lpVtbl = &resourcedataloadervtbl;
371 object->u.resource.module = module;
372 object->u.resource.rsrc = rsrc;
373 object->data = NULL;
374 object->size = 0;
376 *loader = &object->ID3DX11DataLoader_iface;
378 return S_OK;
381 HRESULT WINAPI D3DX11CreateAsyncResourceLoaderW(HMODULE module, const WCHAR *resource, ID3DX11DataLoader **loader)
383 struct asyncdataloader *object;
384 HRSRC rsrc;
386 TRACE("module %p, resource %s, loader %p.\n", module, debugstr_w(resource), loader);
388 if (!loader)
389 return E_FAIL;
391 object = calloc(1, sizeof(*object));
392 if (!object)
393 return E_OUTOFMEMORY;
395 if (!(rsrc = FindResourceW(module, resource, (const WCHAR *)RT_RCDATA)))
397 WARN("Failed to find resource.\n");
398 free(object);
399 return D3DX11_ERR_INVALID_DATA;
402 object->ID3DX11DataLoader_iface.lpVtbl = &resourcedataloadervtbl;
403 object->u.resource.module = module;
404 object->u.resource.rsrc = rsrc;
405 object->data = NULL;
406 object->size = 0;
408 *loader = &object->ID3DX11DataLoader_iface;
410 return S_OK;