usp10/tests: A spelling fix in an ok() message.
[wine.git] / dlls / d3dx11_43 / async.c
blobcde87cec60549cc76aff08de35be65c93ebf4eae
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 HeapFree(GetProcessHeap(), 0, 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 = HeapAlloc(GetProcessHeap(), 0, 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 HeapFree(GetProcessHeap(), 0, data);
116 return E_FAIL;
119 HeapFree(GetProcessHeap(), 0, 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 HeapFree(GetProcessHeap(), 0, loader->u.file.path);
148 HeapFree(GetProcessHeap(), 0, loader->data);
149 HeapFree(GetProcessHeap(), 0, 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 HeapFree(GetProcessHeap(), 0, 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 %lu, 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 FIXME("filename %s, defines %p, include %p, entry_point %s, target %s, sflags %#x, "
240 "eflags %#x, pump %p, shader %p, error_messages %p, hresult %p stub.\n",
241 debugstr_a(filename), defines, include, debugstr_a(entry_point), debugstr_a(target),
242 sflags, eflags, pump, shader, error_messages, hresult);
244 return E_NOTIMPL;
247 HRESULT WINAPI D3DX11CompileFromFileW(const WCHAR *filename, const D3D10_SHADER_MACRO *defines,
248 ID3D10Include *include, const char *entry_point, const char *target, UINT sflags, UINT eflags,
249 ID3DX11ThreadPump *pump, ID3D10Blob **shader, ID3D10Blob **error_messages, HRESULT *hresult)
251 FIXME("filename %s, defines %p, include %p, entry_point %s, target %s, sflags %#x, "
252 "eflags %#x, pump %p, shader %p, error_messages %p, hresult %p stub.\n",
253 debugstr_w(filename), defines, include, debugstr_a(entry_point), debugstr_a(target),
254 sflags, eflags, pump, shader, error_messages, hresult);
256 return E_NOTIMPL;
259 HRESULT WINAPI D3DX11CreateAsyncMemoryLoader(const void *data, SIZE_T data_size, ID3DX11DataLoader **loader)
261 struct asyncdataloader *object;
263 TRACE("data %p, data_size %lu, loader %p.\n", data, data_size, loader);
265 if (!data || !loader)
266 return E_FAIL;
268 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
269 if (!object)
270 return E_OUTOFMEMORY;
272 object->ID3DX11DataLoader_iface.lpVtbl = &memorydataloadervtbl;
273 object->data = (void *)data;
274 object->size = data_size;
276 *loader = &object->ID3DX11DataLoader_iface;
278 return S_OK;
281 HRESULT WINAPI D3DX11CreateAsyncFileLoaderA(const char *filename, ID3DX11DataLoader **loader)
283 WCHAR *filename_w;
284 HRESULT hr;
285 int len;
287 TRACE("filename %s, loader %p.\n", debugstr_a(filename), loader);
289 if (!filename || !loader)
290 return E_FAIL;
292 len = MultiByteToWideChar(CP_ACP, 0, filename, -1, NULL, 0);
293 filename_w = HeapAlloc(GetProcessHeap(), 0, len * sizeof(*filename_w));
294 MultiByteToWideChar(CP_ACP, 0, filename, -1, filename_w, len);
296 hr = D3DX11CreateAsyncFileLoaderW(filename_w, loader);
298 HeapFree(GetProcessHeap(), 0, filename_w);
300 return hr;
303 HRESULT WINAPI D3DX11CreateAsyncFileLoaderW(const WCHAR *filename, ID3DX11DataLoader **loader)
305 struct asyncdataloader *object;
307 TRACE("filename %s, loader %p.\n", debugstr_w(filename), loader);
309 if (!filename || !loader)
310 return E_FAIL;
312 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
313 if (!object)
314 return E_OUTOFMEMORY;
316 object->ID3DX11DataLoader_iface.lpVtbl = &filedataloadervtbl;
317 object->u.file.path = HeapAlloc(GetProcessHeap(), 0, (lstrlenW(filename) + 1) * sizeof(WCHAR));
318 if (!object->u.file.path)
320 HeapFree(GetProcessHeap(), 0, object);
321 return E_OUTOFMEMORY;
323 lstrcpyW(object->u.file.path, filename);
324 object->data = NULL;
325 object->size = 0;
327 *loader = &object->ID3DX11DataLoader_iface;
329 return S_OK;
332 HRESULT WINAPI D3DX11CreateAsyncResourceLoaderA(HMODULE module, const char *resource, ID3DX11DataLoader **loader)
334 struct asyncdataloader *object;
335 HRSRC rsrc;
337 TRACE("module %p, resource %s, loader %p.\n", module, debugstr_a(resource), loader);
339 if (!loader)
340 return E_FAIL;
342 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
343 if (!object)
344 return E_OUTOFMEMORY;
346 if (!(rsrc = FindResourceA(module, resource, (const char *)RT_RCDATA)))
348 WARN("Failed to find resource.\n");
349 HeapFree(GetProcessHeap(), 0, object);
350 return D3DX11_ERR_INVALID_DATA;
353 object->ID3DX11DataLoader_iface.lpVtbl = &resourcedataloadervtbl;
354 object->u.resource.module = module;
355 object->u.resource.rsrc = rsrc;
356 object->data = NULL;
357 object->size = 0;
359 *loader = &object->ID3DX11DataLoader_iface;
361 return S_OK;
364 HRESULT WINAPI D3DX11CreateAsyncResourceLoaderW(HMODULE module, const WCHAR *resource, ID3DX11DataLoader **loader)
366 struct asyncdataloader *object;
367 HRSRC rsrc;
369 TRACE("module %p, resource %s, loader %p.\n", module, debugstr_w(resource), loader);
371 if (!loader)
372 return E_FAIL;
374 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
375 if (!object)
376 return E_OUTOFMEMORY;
378 if (!(rsrc = FindResourceW(module, resource, (const WCHAR *)RT_RCDATA)))
380 WARN("Failed to find resource.\n");
381 HeapFree(GetProcessHeap(), 0, object);
382 return D3DX11_ERR_INVALID_DATA;
385 object->ID3DX11DataLoader_iface.lpVtbl = &resourcedataloadervtbl;
386 object->u.resource.module = module;
387 object->u.resource.rsrc = rsrc;
388 object->data = NULL;
389 object->size = 0;
391 *loader = &object->ID3DX11DataLoader_iface;
393 return S_OK;