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
);
75 HeapFree(GetProcessHeap(), 0, loader
);
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
);
103 data
= HeapAlloc(GetProcessHeap(), 0, size
);
107 return E_OUTOFMEMORY
;
110 ret
= ReadFile(file
, data
, size
, &read_len
, NULL
);
114 ERR("Failed to read file contents.\n");
115 HeapFree(GetProcessHeap(), 0, data
);
119 HeapFree(GetProcessHeap(), 0, loader
->data
);
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 HeapFree(GetProcessHeap(), 0, loader
->u
.file
.path
);
148 HeapFree(GetProcessHeap(), 0, loader
->data
);
149 HeapFree(GetProcessHeap(), 0, loader
);
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
);
205 HeapFree(GetProcessHeap(), 0, loader
);
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
);
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
);
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
);
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
)
268 object
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*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
;
281 HRESULT WINAPI
D3DX11CreateAsyncFileLoaderA(const char *filename
, ID3DX11DataLoader
**loader
)
287 TRACE("filename %s, loader %p.\n", debugstr_a(filename
), loader
);
289 if (!filename
|| !loader
)
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
);
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
)
312 object
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*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
);
327 *loader
= &object
->ID3DX11DataLoader_iface
;
332 HRESULT WINAPI
D3DX11CreateAsyncResourceLoaderA(HMODULE module
, const char *resource
, ID3DX11DataLoader
**loader
)
334 struct asyncdataloader
*object
;
337 TRACE("module %p, resource %s, loader %p.\n", module
, debugstr_a(resource
), loader
);
342 object
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*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
;
359 *loader
= &object
->ID3DX11DataLoader_iface
;
364 HRESULT WINAPI
D3DX11CreateAsyncResourceLoaderW(HMODULE module
, const WCHAR
*resource
, ID3DX11DataLoader
**loader
)
366 struct asyncdataloader
*object
;
369 TRACE("module %p, resource %s, loader %p.\n", module
, debugstr_w(resource
), loader
);
374 object
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*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
;
391 *loader
= &object
->ID3DX11DataLoader_iface
;