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 "wine/port.h"
22 #include "d3dcompiler.h"
24 #include "wine/debug.h"
25 #include "wine/unicode.h"
27 WINE_DEFAULT_DEBUG_CHANNEL(d3dx
);
29 struct asyncdataloader
31 ID3DX11DataLoader ID3DX11DataLoader_iface
;
49 static inline struct asyncdataloader
*impl_from_ID3DX11DataLoader(ID3DX11DataLoader
*iface
)
51 return CONTAINING_RECORD(iface
, struct asyncdataloader
, ID3DX11DataLoader_iface
);
54 static HRESULT WINAPI
memorydataloader_Load(ID3DX11DataLoader
*iface
)
56 TRACE("iface %p.\n", iface
);
60 static HRESULT WINAPI
memorydataloader_Decompress(ID3DX11DataLoader
*iface
, void **data
, SIZE_T
*size
)
62 struct asyncdataloader
*loader
= impl_from_ID3DX11DataLoader(iface
);
64 TRACE("iface %p, data %p, size %p.\n", iface
, data
, size
);
72 static HRESULT WINAPI
memorydataloader_Destroy(ID3DX11DataLoader
*iface
)
74 struct asyncdataloader
*loader
= impl_from_ID3DX11DataLoader(iface
);
76 TRACE("iface %p.\n", iface
);
78 HeapFree(GetProcessHeap(), 0, loader
);
82 static const ID3DX11DataLoaderVtbl memorydataloadervtbl
=
84 memorydataloader_Load
,
85 memorydataloader_Decompress
,
86 memorydataloader_Destroy
89 static HRESULT WINAPI
filedataloader_Load(ID3DX11DataLoader
*iface
)
91 struct asyncdataloader
*loader
= impl_from_ID3DX11DataLoader(iface
);
97 TRACE("iface %p.\n", iface
);
99 /* Always buffer file contents, even if Load() was already called. */
100 file
= CreateFileW(loader
->u
.file
.path
, GENERIC_READ
, FILE_SHARE_READ
| FILE_SHARE_WRITE
,
101 NULL
, OPEN_EXISTING
, FILE_ATTRIBUTE_NORMAL
, NULL
);
102 if (file
== INVALID_HANDLE_VALUE
)
103 return D3D11_ERROR_FILE_NOT_FOUND
;
105 size
= GetFileSize(file
, NULL
);
106 data
= HeapAlloc(GetProcessHeap(), 0, size
);
110 return E_OUTOFMEMORY
;
113 ret
= ReadFile(file
, data
, size
, &read_len
, NULL
);
117 ERR("Failed to read file contents.\n");
118 HeapFree(GetProcessHeap(), 0, data
);
122 HeapFree(GetProcessHeap(), 0, loader
->data
);
129 static HRESULT WINAPI
filedataloader_Decompress(ID3DX11DataLoader
*iface
, void **data
, SIZE_T
*size
)
131 struct asyncdataloader
*loader
= impl_from_ID3DX11DataLoader(iface
);
133 TRACE("iface %p, data %p, size %p.\n", iface
, data
, size
);
138 *data
= loader
->data
;
139 *size
= loader
->size
;
144 static HRESULT WINAPI
filedataloader_Destroy(ID3DX11DataLoader
*iface
)
146 struct asyncdataloader
*loader
= impl_from_ID3DX11DataLoader(iface
);
148 TRACE("iface %p.\n", iface
);
150 HeapFree(GetProcessHeap(), 0, loader
->u
.file
.path
);
151 HeapFree(GetProcessHeap(), 0, loader
->data
);
152 HeapFree(GetProcessHeap(), 0, loader
);
157 static const ID3DX11DataLoaderVtbl filedataloadervtbl
=
160 filedataloader_Decompress
,
161 filedataloader_Destroy
164 static HRESULT WINAPI
resourcedataloader_Load(ID3DX11DataLoader
*iface
)
166 struct asyncdataloader
*loader
= impl_from_ID3DX11DataLoader(iface
);
169 TRACE("iface %p.\n", iface
);
174 hglobal
= LoadResource(loader
->u
.resource
.module
, loader
->u
.resource
.rsrc
);
177 ERR("Failed to load resource.\n");
181 loader
->data
= LockResource(hglobal
);
182 loader
->size
= SizeofResource(loader
->u
.resource
.module
, loader
->u
.resource
.rsrc
);
187 static HRESULT WINAPI
resourcedataloader_Decompress(ID3DX11DataLoader
*iface
, void **data
, SIZE_T
*size
)
189 struct asyncdataloader
*loader
= impl_from_ID3DX11DataLoader(iface
);
191 TRACE("iface %p, data %p, size %p.\n", iface
, data
, size
);
196 *data
= loader
->data
;
197 *size
= loader
->size
;
202 static HRESULT WINAPI
resourcedataloader_Destroy(ID3DX11DataLoader
*iface
)
204 struct asyncdataloader
*loader
= impl_from_ID3DX11DataLoader(iface
);
206 TRACE("iface %p.\n", iface
);
208 HeapFree(GetProcessHeap(), 0, loader
);
213 static const ID3DX11DataLoaderVtbl resourcedataloadervtbl
=
215 resourcedataloader_Load
,
216 resourcedataloader_Decompress
,
217 resourcedataloader_Destroy
220 HRESULT WINAPI
D3DX11CompileFromMemory(const char *data
, SIZE_T data_size
, const char *filename
,
221 const D3D10_SHADER_MACRO
*defines
, ID3D10Include
*include
, const char *entry_point
,
222 const char *target
, UINT sflags
, UINT eflags
, ID3DX11ThreadPump
*pump
, ID3D10Blob
**shader
,
223 ID3D10Blob
**error_messages
, HRESULT
*hresult
)
225 TRACE("data %s, data_size %lu, filename %s, defines %p, include %p, entry_point %s, target %s, "
226 "sflags %#x, eflags %#x, pump %p, shader %p, error_messages %p, hresult %p.\n",
227 debugstr_an(data
, data_size
), data_size
, debugstr_a(filename
), defines
, include
,
228 debugstr_a(entry_point
), debugstr_a(target
), sflags
, eflags
, pump
, shader
,
229 error_messages
, hresult
);
232 FIXME("Unimplemented ID3DX11ThreadPump handling.\n");
234 return D3DCompile(data
, data_size
, filename
, defines
, include
, entry_point
, target
,
235 sflags
, eflags
, shader
, error_messages
);
238 HRESULT WINAPI
D3DX11CompileFromFileA(const char *filename
, const D3D10_SHADER_MACRO
*defines
,
239 ID3D10Include
*include
, const char *entry_point
, const char *target
, UINT sflags
, UINT eflags
,
240 ID3DX11ThreadPump
*pump
, ID3D10Blob
**shader
, ID3D10Blob
**error_messages
, HRESULT
*hresult
)
242 FIXME("filename %s, defines %p, include %p, entry_point %s, target %s, sflags %#x, "
243 "eflags %#x, pump %p, shader %p, error_messages %p, hresult %p stub.\n",
244 debugstr_a(filename
), defines
, include
, debugstr_a(entry_point
), debugstr_a(target
),
245 sflags
, eflags
, pump
, shader
, error_messages
, hresult
);
250 HRESULT WINAPI
D3DX11CompileFromFileW(const WCHAR
*filename
, const D3D10_SHADER_MACRO
*defines
,
251 ID3D10Include
*include
, const char *entry_point
, const char *target
, UINT sflags
, UINT eflags
,
252 ID3DX11ThreadPump
*pump
, ID3D10Blob
**shader
, ID3D10Blob
**error_messages
, HRESULT
*hresult
)
254 FIXME("filename %s, defines %p, include %p, entry_point %s, target %s, sflags %#x, "
255 "eflags %#x, pump %p, shader %p, error_messages %p, hresult %p stub.\n",
256 debugstr_w(filename
), defines
, include
, debugstr_a(entry_point
), debugstr_a(target
),
257 sflags
, eflags
, pump
, shader
, error_messages
, hresult
);
262 HRESULT WINAPI
D3DX11CreateAsyncMemoryLoader(const void *data
, SIZE_T data_size
, ID3DX11DataLoader
**loader
)
264 struct asyncdataloader
*object
;
266 TRACE("data %p, data_size %lu, loader %p.\n", data
, data_size
, loader
);
268 if (!data
|| !loader
)
271 object
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*object
));
273 return E_OUTOFMEMORY
;
275 object
->ID3DX11DataLoader_iface
.lpVtbl
= &memorydataloadervtbl
;
276 object
->data
= (void *)data
;
277 object
->size
= data_size
;
279 *loader
= &object
->ID3DX11DataLoader_iface
;
284 HRESULT WINAPI
D3DX11CreateAsyncFileLoaderA(const char *filename
, ID3DX11DataLoader
**loader
)
290 TRACE("filename %s, loader %p.\n", debugstr_a(filename
), loader
);
292 if (!filename
|| !loader
)
295 len
= MultiByteToWideChar(CP_ACP
, 0, filename
, -1, NULL
, 0);
296 filename_w
= HeapAlloc(GetProcessHeap(), 0, len
* sizeof(*filename_w
));
297 MultiByteToWideChar(CP_ACP
, 0, filename
, -1, filename_w
, len
);
299 hr
= D3DX11CreateAsyncFileLoaderW(filename_w
, loader
);
301 HeapFree(GetProcessHeap(), 0, filename_w
);
306 HRESULT WINAPI
D3DX11CreateAsyncFileLoaderW(const WCHAR
*filename
, ID3DX11DataLoader
**loader
)
308 struct asyncdataloader
*object
;
310 TRACE("filename %s, loader %p.\n", debugstr_w(filename
), loader
);
312 if (!filename
|| !loader
)
315 object
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*object
));
317 return E_OUTOFMEMORY
;
319 object
->ID3DX11DataLoader_iface
.lpVtbl
= &filedataloadervtbl
;
320 object
->u
.file
.path
= HeapAlloc(GetProcessHeap(), 0, (strlenW(filename
) + 1) * sizeof(WCHAR
));
321 if (!object
->u
.file
.path
)
323 HeapFree(GetProcessHeap(), 0, object
);
324 return E_OUTOFMEMORY
;
326 strcpyW(object
->u
.file
.path
, filename
);
330 *loader
= &object
->ID3DX11DataLoader_iface
;
335 HRESULT WINAPI
D3DX11CreateAsyncResourceLoaderA(HMODULE module
, const char *resource
, ID3DX11DataLoader
**loader
)
337 struct asyncdataloader
*object
;
340 TRACE("module %p, resource %s, loader %p.\n", module
, debugstr_a(resource
), loader
);
345 object
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*object
));
347 return E_OUTOFMEMORY
;
349 if (!(rsrc
= FindResourceA(module
, resource
, (const char *)RT_RCDATA
)))
351 ERR("Failed to find resource.\n");
352 HeapFree(GetProcessHeap(), 0, object
);
353 return D3DX11_ERR_INVALID_DATA
;
356 object
->ID3DX11DataLoader_iface
.lpVtbl
= &resourcedataloadervtbl
;
357 object
->u
.resource
.module
= module
;
358 object
->u
.resource
.rsrc
= rsrc
;
362 *loader
= &object
->ID3DX11DataLoader_iface
;
367 HRESULT WINAPI
D3DX11CreateAsyncResourceLoaderW(HMODULE module
, const WCHAR
*resource
, ID3DX11DataLoader
**loader
)
369 struct asyncdataloader
*object
;
372 TRACE("module %p, resource %s, loader %p.\n", module
, debugstr_w(resource
), loader
);
377 object
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*object
));
379 return E_OUTOFMEMORY
;
381 if (!(rsrc
= FindResourceW(module
, resource
, (const WCHAR
*)RT_RCDATA
)))
383 ERR("Failed to find resource.\n");
384 HeapFree(GetProcessHeap(), 0, object
);
385 return D3DX11_ERR_INVALID_DATA
;
388 object
->ID3DX11DataLoader_iface
.lpVtbl
= &resourcedataloadervtbl
;
389 object
->u
.resource
.module
= module
;
390 object
->u
.resource
.rsrc
= rsrc
;
394 *loader
= &object
->ID3DX11DataLoader_iface
;