2 * Copyright 2016 Andrey Gusev
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
22 #include "d3dcompiler.h"
23 #include "dxhelpers.h"
25 #include "wine/debug.h"
27 WINE_DEFAULT_DEBUG_CHANNEL(d3dx
);
29 struct asyncdataloader
31 ID3DX10DataLoader ID3DX10DataLoader_iface
;
49 static inline struct asyncdataloader
*impl_from_ID3DX10DataLoader(ID3DX10DataLoader
*iface
)
51 return CONTAINING_RECORD(iface
, struct asyncdataloader
, ID3DX10DataLoader_iface
);
54 static HRESULT WINAPI
memorydataloader_Load(ID3DX10DataLoader
*iface
)
56 TRACE("iface %p.\n", iface
);
60 static HRESULT WINAPI
memorydataloader_Decompress(ID3DX10DataLoader
*iface
, void **data
, SIZE_T
*size
)
62 struct asyncdataloader
*loader
= impl_from_ID3DX10DataLoader(iface
);
64 TRACE("iface %p, data %p, size %p.\n", iface
, data
, size
);
72 static HRESULT WINAPI
memorydataloader_Destroy(ID3DX10DataLoader
*iface
)
74 struct asyncdataloader
*loader
= impl_from_ID3DX10DataLoader(iface
);
76 TRACE("iface %p.\n", iface
);
82 static const ID3DX10DataLoaderVtbl memorydataloadervtbl
=
84 memorydataloader_Load
,
85 memorydataloader_Decompress
,
86 memorydataloader_Destroy
89 HRESULT
load_file(const WCHAR
*path
, void **data
, DWORD
*size
)
95 file
= CreateFileW(path
, GENERIC_READ
, FILE_SHARE_READ
| FILE_SHARE_WRITE
,
96 NULL
, OPEN_EXISTING
, FILE_ATTRIBUTE_NORMAL
, NULL
);
97 if (file
== INVALID_HANDLE_VALUE
)
98 return D3D10_ERROR_FILE_NOT_FOUND
;
100 *size
= GetFileSize(file
, NULL
);
101 *data
= malloc(*size
);
105 return E_OUTOFMEMORY
;
108 ret
= ReadFile(file
, *data
, *size
, &read_len
, NULL
);
110 if (!ret
|| read_len
!= *size
)
112 WARN("Failed to read file contents.\n");
119 static HRESULT WINAPI
filedataloader_Load(ID3DX10DataLoader
*iface
)
121 struct asyncdataloader
*loader
= impl_from_ID3DX10DataLoader(iface
);
126 TRACE("iface %p.\n", iface
);
128 /* Always buffer file contents, even if Load() was already called. */
129 if (FAILED((hr
= load_file(loader
->u
.file
.path
, &data
, &size
))))
139 static HRESULT WINAPI
filedataloader_Decompress(ID3DX10DataLoader
*iface
, void **data
, SIZE_T
*size
)
141 struct asyncdataloader
*loader
= impl_from_ID3DX10DataLoader(iface
);
143 TRACE("iface %p, data %p, size %p.\n", iface
, data
, size
);
148 *data
= loader
->data
;
149 *size
= loader
->size
;
154 static HRESULT WINAPI
filedataloader_Destroy(ID3DX10DataLoader
*iface
)
156 struct asyncdataloader
*loader
= impl_from_ID3DX10DataLoader(iface
);
158 TRACE("iface %p.\n", iface
);
160 free(loader
->u
.file
.path
);
167 static const ID3DX10DataLoaderVtbl filedataloadervtbl
=
170 filedataloader_Decompress
,
171 filedataloader_Destroy
174 static HRESULT
load_resource_initA(HMODULE module
, const char *resource
, HRSRC
*rsrc
)
176 if (!(*rsrc
= FindResourceA(module
, resource
, (const char *)RT_RCDATA
)))
177 *rsrc
= FindResourceA(module
, resource
, (const char *)RT_BITMAP
);
180 WARN("Failed to find resource.\n");
181 return D3DX10_ERR_INVALID_DATA
;
186 static HRESULT
load_resource_initW(HMODULE module
, const WCHAR
*resource
, HRSRC
*rsrc
)
188 if (!(*rsrc
= FindResourceW(module
, resource
, (const WCHAR
*)RT_RCDATA
)))
189 *rsrc
= FindResourceW(module
, resource
, (const WCHAR
*)RT_BITMAP
);
192 WARN("Failed to find resource.\n");
193 return D3DX10_ERR_INVALID_DATA
;
198 static HRESULT
load_resource(HMODULE module
, HRSRC rsrc
, void **data
, DWORD
*size
)
202 if (!(*size
= SizeofResource(module
, rsrc
)))
203 return D3DX10_ERR_INVALID_DATA
;
204 if (!(hglobal
= LoadResource(module
, rsrc
)))
205 return D3DX10_ERR_INVALID_DATA
;
206 if (!(*data
= LockResource(hglobal
)))
207 return D3DX10_ERR_INVALID_DATA
;
211 HRESULT
load_resourceA(HMODULE module
, const char *resource
, void **data
, DWORD
*size
)
216 if (FAILED((hr
= load_resource_initA(module
, resource
, &rsrc
))))
218 return load_resource(module
, rsrc
, data
, size
);
221 HRESULT
load_resourceW(HMODULE module
, const WCHAR
*resource
, void **data
, DWORD
*size
)
226 if ((FAILED(hr
= load_resource_initW(module
, resource
, &rsrc
))))
228 return load_resource(module
, rsrc
, data
, size
);
231 static HRESULT WINAPI
resourcedataloader_Load(ID3DX10DataLoader
*iface
)
233 struct asyncdataloader
*loader
= impl_from_ID3DX10DataLoader(iface
);
235 TRACE("iface %p.\n", iface
);
240 return load_resource(loader
->u
.resource
.module
, loader
->u
.resource
.rsrc
,
241 &loader
->data
, &loader
->size
);
244 static HRESULT WINAPI
resourcedataloader_Decompress(ID3DX10DataLoader
*iface
, void **data
, SIZE_T
*size
)
246 struct asyncdataloader
*loader
= impl_from_ID3DX10DataLoader(iface
);
248 TRACE("iface %p, data %p, size %p.\n", iface
, data
, size
);
253 *data
= loader
->data
;
254 *size
= loader
->size
;
259 static HRESULT WINAPI
resourcedataloader_Destroy(ID3DX10DataLoader
*iface
)
261 struct asyncdataloader
*loader
= impl_from_ID3DX10DataLoader(iface
);
263 TRACE("iface %p.\n", iface
);
270 static const ID3DX10DataLoaderVtbl resourcedataloadervtbl
=
272 resourcedataloader_Load
,
273 resourcedataloader_Decompress
,
274 resourcedataloader_Destroy
277 struct texture_info_processor
279 ID3DX10DataProcessor ID3DX10DataProcessor_iface
;
280 D3DX10_IMAGE_INFO
*info
;
283 static inline struct texture_info_processor
*impl_from_ID3DX10DataProcessor(ID3DX10DataProcessor
*iface
)
285 return CONTAINING_RECORD(iface
, struct texture_info_processor
, ID3DX10DataProcessor_iface
);
288 static HRESULT WINAPI
texture_info_processor_Process(ID3DX10DataProcessor
*iface
, void *data
, SIZE_T size
)
290 struct texture_info_processor
*processor
= impl_from_ID3DX10DataProcessor(iface
);
292 TRACE("iface %p, data %p, size %Iu.\n", iface
, data
, size
);
293 return get_image_info(data
, size
, processor
->info
);
296 static HRESULT WINAPI
texture_info_processor_CreateDeviceObject(ID3DX10DataProcessor
*iface
, void **object
)
298 TRACE("iface %p, object %p.\n", iface
, object
);
302 static HRESULT WINAPI
texture_info_processor_Destroy(ID3DX10DataProcessor
*iface
)
304 struct texture_info_processor
*processor
= impl_from_ID3DX10DataProcessor(iface
);
306 TRACE("iface %p.\n", iface
);
312 static ID3DX10DataProcessorVtbl texture_info_processor_vtbl
=
314 texture_info_processor_Process
,
315 texture_info_processor_CreateDeviceObject
,
316 texture_info_processor_Destroy
319 struct texture_processor
321 ID3DX10DataProcessor ID3DX10DataProcessor_iface
;
322 ID3D10Device
*device
;
323 D3DX10_IMAGE_LOAD_INFO load_info
;
324 D3D10_SUBRESOURCE_DATA
*resource_data
;
327 static inline struct texture_processor
*texture_processor_from_ID3DX10DataProcessor(ID3DX10DataProcessor
*iface
)
329 return CONTAINING_RECORD(iface
, struct texture_processor
, ID3DX10DataProcessor_iface
);
332 static HRESULT WINAPI
texture_processor_Process(ID3DX10DataProcessor
*iface
, void *data
, SIZE_T size
)
334 struct texture_processor
*processor
= texture_processor_from_ID3DX10DataProcessor(iface
);
336 TRACE("iface %p, data %p, size %Iu.\n", iface
, data
, size
);
338 if (processor
->resource_data
)
340 WARN("Called multiple times.\n");
341 free(processor
->resource_data
);
342 processor
->resource_data
= NULL
;
344 return load_texture_data(data
, size
, &processor
->load_info
, &processor
->resource_data
);
347 static HRESULT WINAPI
texture_processor_CreateDeviceObject(ID3DX10DataProcessor
*iface
, void **object
)
349 struct texture_processor
*processor
= texture_processor_from_ID3DX10DataProcessor(iface
);
351 TRACE("iface %p, object %p.\n", iface
, object
);
353 if (!processor
->resource_data
)
356 return create_d3d_texture(processor
->device
, &processor
->load_info
,
357 processor
->resource_data
, (ID3D10Resource
**)object
);
360 static HRESULT WINAPI
texture_processor_Destroy(ID3DX10DataProcessor
*iface
)
362 struct texture_processor
*processor
= texture_processor_from_ID3DX10DataProcessor(iface
);
364 TRACE("iface %p.\n", iface
);
366 ID3D10Device_Release(processor
->device
);
367 free(processor
->resource_data
);
372 static ID3DX10DataProcessorVtbl texture_processor_vtbl
=
374 texture_processor_Process
,
375 texture_processor_CreateDeviceObject
,
376 texture_processor_Destroy
379 HRESULT WINAPI
D3DX10CompileFromMemory(const char *data
, SIZE_T data_size
, const char *filename
,
380 const D3D10_SHADER_MACRO
*defines
, ID3D10Include
*include
, const char *entry_point
,
381 const char *target
, UINT sflags
, UINT eflags
, ID3DX10ThreadPump
*pump
, ID3D10Blob
**shader
,
382 ID3D10Blob
**error_messages
, HRESULT
*hresult
)
384 TRACE("data %s, data_size %Iu, filename %s, defines %p, include %p, entry_point %s, target %s, "
385 "sflags %#x, eflags %#x, pump %p, shader %p, error_messages %p, hresult %p.\n",
386 debugstr_an(data
, data_size
), data_size
, debugstr_a(filename
), defines
, include
,
387 debugstr_a(entry_point
), debugstr_a(target
), sflags
, eflags
, pump
, shader
,
388 error_messages
, hresult
);
391 FIXME("Unimplemented ID3DX10ThreadPump handling.\n");
393 return D3DCompile(data
, data_size
, filename
, defines
, include
, entry_point
, target
,
394 sflags
, eflags
, shader
, error_messages
);
397 HRESULT WINAPI
D3DX10CreateEffectPoolFromFileA(const char *filename
, const D3D10_SHADER_MACRO
*defines
,
398 ID3D10Include
*include
, const char *profile
, UINT hlslflags
, UINT fxflags
, ID3D10Device
*device
,
399 ID3DX10ThreadPump
*pump
, ID3D10EffectPool
**effectpool
, ID3D10Blob
**errors
, HRESULT
*hresult
)
401 FIXME("filename %s, defines %p, include %p, profile %s, hlslflags %#x, fxflags %#x, device %p, "
402 "pump %p, effectpool %p, errors %p, hresult %p, stub!\n",
403 debugstr_a(filename
), defines
, include
, debugstr_a(profile
), hlslflags
, fxflags
, device
,
404 pump
, effectpool
, errors
, hresult
);
409 HRESULT WINAPI
D3DX10CreateEffectPoolFromFileW(const WCHAR
*filename
, const D3D10_SHADER_MACRO
*defines
,
410 ID3D10Include
*include
, const char *profile
, UINT hlslflags
, UINT fxflags
, ID3D10Device
*device
,
411 ID3DX10ThreadPump
*pump
, ID3D10EffectPool
**effectpool
, ID3D10Blob
**errors
, HRESULT
*hresult
)
413 FIXME("filename %s, defines %p, include %p, profile %s, hlslflags %#x, fxflags %#x, device %p, "
414 "pump %p, effectpool %p, errors %p, hresult %p, stub!\n",
415 debugstr_w(filename
), defines
, include
, debugstr_a(profile
), hlslflags
, fxflags
, device
,
416 pump
, effectpool
, errors
, hresult
);
421 HRESULT WINAPI
D3DX10CreateAsyncMemoryLoader(const void *data
, SIZE_T data_size
, ID3DX10DataLoader
**loader
)
423 struct asyncdataloader
*object
;
425 TRACE("data %p, data_size %Iu, loader %p.\n", data
, data_size
, loader
);
427 if (!data
|| !loader
)
430 object
= calloc(1, sizeof(*object
));
432 return E_OUTOFMEMORY
;
434 object
->ID3DX10DataLoader_iface
.lpVtbl
= &memorydataloadervtbl
;
435 object
->data
= (void *)data
;
436 object
->size
= data_size
;
438 *loader
= &object
->ID3DX10DataLoader_iface
;
443 HRESULT WINAPI
D3DX10CreateAsyncFileLoaderA(const char *filename
, ID3DX10DataLoader
**loader
)
449 TRACE("filename %s, loader %p.\n", debugstr_a(filename
), loader
);
451 if (!filename
|| !loader
)
454 len
= MultiByteToWideChar(CP_ACP
, 0, filename
, -1, NULL
, 0);
455 filename_w
= malloc(len
* sizeof(*filename_w
));
456 MultiByteToWideChar(CP_ACP
, 0, filename
, -1, filename_w
, len
);
458 hr
= D3DX10CreateAsyncFileLoaderW(filename_w
, loader
);
465 HRESULT WINAPI
D3DX10CreateAsyncFileLoaderW(const WCHAR
*filename
, ID3DX10DataLoader
**loader
)
467 struct asyncdataloader
*object
;
469 TRACE("filename %s, loader %p.\n", debugstr_w(filename
), loader
);
471 if (!filename
|| !loader
)
474 object
= calloc(1, sizeof(*object
));
476 return E_OUTOFMEMORY
;
478 object
->ID3DX10DataLoader_iface
.lpVtbl
= &filedataloadervtbl
;
479 object
->u
.file
.path
= malloc((lstrlenW(filename
) + 1) * sizeof(WCHAR
));
480 if (!object
->u
.file
.path
)
483 return E_OUTOFMEMORY
;
485 lstrcpyW(object
->u
.file
.path
, filename
);
489 *loader
= &object
->ID3DX10DataLoader_iface
;
494 HRESULT WINAPI
D3DX10CreateAsyncResourceLoaderA(HMODULE module
, const char *resource
, ID3DX10DataLoader
**loader
)
496 struct asyncdataloader
*object
;
500 TRACE("module %p, resource %s, loader %p.\n", module
, debugstr_a(resource
), loader
);
505 object
= calloc(1, sizeof(*object
));
507 return E_OUTOFMEMORY
;
509 if (FAILED((hr
= load_resource_initA(module
, resource
, &rsrc
))))
515 object
->ID3DX10DataLoader_iface
.lpVtbl
= &resourcedataloadervtbl
;
516 object
->u
.resource
.module
= module
;
517 object
->u
.resource
.rsrc
= rsrc
;
521 *loader
= &object
->ID3DX10DataLoader_iface
;
526 HRESULT WINAPI
D3DX10CreateAsyncResourceLoaderW(HMODULE module
, const WCHAR
*resource
, ID3DX10DataLoader
**loader
)
528 struct asyncdataloader
*object
;
532 TRACE("module %p, resource %s, loader %p.\n", module
, debugstr_w(resource
), loader
);
537 object
= calloc(1, sizeof(*object
));
539 return E_OUTOFMEMORY
;
541 if (FAILED((hr
= load_resource_initW(module
, resource
, &rsrc
))))
547 object
->ID3DX10DataLoader_iface
.lpVtbl
= &resourcedataloadervtbl
;
548 object
->u
.resource
.module
= module
;
549 object
->u
.resource
.rsrc
= rsrc
;
553 *loader
= &object
->ID3DX10DataLoader_iface
;
558 HRESULT WINAPI
D3DX10CreateAsyncTextureInfoProcessor(D3DX10_IMAGE_INFO
*info
, ID3DX10DataProcessor
**processor
)
560 struct texture_info_processor
*object
;
562 TRACE("info %p, processor %p.\n", info
, processor
);
567 object
= malloc(sizeof(*object
));
569 return E_OUTOFMEMORY
;
571 object
->ID3DX10DataProcessor_iface
.lpVtbl
= &texture_info_processor_vtbl
;
574 *processor
= &object
->ID3DX10DataProcessor_iface
;
578 HRESULT WINAPI
D3DX10CreateAsyncTextureProcessor(ID3D10Device
*device
,
579 D3DX10_IMAGE_LOAD_INFO
*load_info
, ID3DX10DataProcessor
**processor
)
581 struct texture_processor
*object
;
583 TRACE("device %p, load_info %p, processor %p.\n", device
, load_info
, processor
);
585 if (!device
|| !processor
)
588 object
= calloc(1, sizeof(*object
));
590 return E_OUTOFMEMORY
;
592 object
->ID3DX10DataProcessor_iface
.lpVtbl
= &texture_processor_vtbl
;
593 object
->device
= device
;
594 ID3D10Device_AddRef(device
);
595 init_load_info(load_info
, &object
->load_info
);
597 *processor
= &object
->ID3DX10DataProcessor_iface
;
601 HRESULT WINAPI
D3DX10PreprocessShaderFromMemory(const char *data
, SIZE_T data_size
, const char *filename
,
602 const D3D10_SHADER_MACRO
*defines
, ID3DInclude
*include
, ID3DX10ThreadPump
*pump
, ID3D10Blob
**shader_text
,
603 ID3D10Blob
**errors
, HRESULT
*hresult
)
605 FIXME("data %s, data_size %Iu, filename %s, defines %p, include %p, pump %p, shader_text %p, "
606 "errors %p, hresult %p stub!\n",
607 debugstr_an(data
, data_size
), data_size
, debugstr_a(filename
), defines
, include
, pump
,
608 shader_text
, errors
, hresult
);