Commit 16433fa6 authored by Nikolay Sivov's avatar Nikolay Sivov Committed by Alexandre Julliard

d3dx10: Implement async data loader interfaces.

parent 9c2c9c71
......@@ -24,9 +24,201 @@
#include "d3dcompiler.h"
#include "wine/debug.h"
#include "wine/unicode.h"
WINE_DEFAULT_DEBUG_CHANNEL(d3dx);
struct asyncdataloader
{
ID3DX10DataLoader ID3DX10DataLoader_iface;
union
{
struct
{
WCHAR *path;
} file;
struct
{
HMODULE module;
HRSRC rsrc;
} resource;
} u;
void *data;
SIZE_T size;
};
static inline struct asyncdataloader *impl_from_ID3DX10DataLoader(ID3DX10DataLoader *iface)
{
return CONTAINING_RECORD(iface, struct asyncdataloader, ID3DX10DataLoader_iface);
}
static HRESULT WINAPI memorydataloader_Load(ID3DX10DataLoader *iface)
{
TRACE("iface %p.\n", iface);
return S_OK;
}
static HRESULT WINAPI memorydataloader_Decompress(ID3DX10DataLoader *iface, void **data, SIZE_T *size)
{
struct asyncdataloader *loader = impl_from_ID3DX10DataLoader(iface);
TRACE("iface %p, data %p, size %p.\n", iface, data, size);
*data = loader->data;
*size = loader->size;
return S_OK;
}
static HRESULT WINAPI memorydataloader_Destroy(ID3DX10DataLoader *iface)
{
struct asyncdataloader *loader = impl_from_ID3DX10DataLoader(iface);
TRACE("iface %p.\n", iface);
HeapFree(GetProcessHeap(), 0, loader);
return S_OK;
}
static const ID3DX10DataLoaderVtbl memorydataloadervtbl =
{
memorydataloader_Load,
memorydataloader_Decompress,
memorydataloader_Destroy
};
static HRESULT WINAPI filedataloader_Load(ID3DX10DataLoader *iface)
{
struct asyncdataloader *loader = impl_from_ID3DX10DataLoader(iface);
DWORD size, read_len;
HANDLE file;
void *data;
BOOL ret;
TRACE("iface %p.\n", iface);
/* Always buffer file contents, even if Load() was already called. */
file = CreateFileW(loader->u.file.path, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (file == INVALID_HANDLE_VALUE)
return D3D10_ERROR_FILE_NOT_FOUND;
size = GetFileSize(file, NULL);
data = HeapAlloc(GetProcessHeap(), 0, size);
if (!data)
{
CloseHandle(file);
return E_OUTOFMEMORY;
}
ret = ReadFile(file, data, size, &read_len, NULL);
CloseHandle(file);
if (!ret)
{
ERR("Failed to read file contents.\n");
HeapFree(GetProcessHeap(), 0, data);
return E_FAIL;
}
HeapFree(GetProcessHeap(), 0, loader->data);
loader->data = data;
loader->size = size;
return S_OK;
}
static HRESULT WINAPI filedataloader_Decompress(ID3DX10DataLoader *iface, void **data, SIZE_T *size)
{
struct asyncdataloader *loader = impl_from_ID3DX10DataLoader(iface);
TRACE("iface %p, data %p, size %p.\n", iface, data, size);
if (!loader->data)
return E_FAIL;
*data = loader->data;
*size = loader->size;
return S_OK;
}
static HRESULT WINAPI filedataloader_Destroy(ID3DX10DataLoader *iface)
{
struct asyncdataloader *loader = impl_from_ID3DX10DataLoader(iface);
TRACE("iface %p.\n", iface);
HeapFree(GetProcessHeap(), 0, loader->u.file.path);
HeapFree(GetProcessHeap(), 0, loader->data);
HeapFree(GetProcessHeap(), 0, loader);
return S_OK;
}
static const ID3DX10DataLoaderVtbl filedataloadervtbl =
{
filedataloader_Load,
filedataloader_Decompress,
filedataloader_Destroy
};
static HRESULT WINAPI resourcedataloader_Load(ID3DX10DataLoader *iface)
{
struct asyncdataloader *loader = impl_from_ID3DX10DataLoader(iface);
HGLOBAL hglobal;
TRACE("iface %p.\n", iface);
if (loader->data)
return S_OK;
hglobal = LoadResource(loader->u.resource.module, loader->u.resource.rsrc);
if (!hglobal)
{
ERR("Failed to load resource.\n");
return E_FAIL;
}
loader->data = LockResource(hglobal);
loader->size = SizeofResource(loader->u.resource.module, loader->u.resource.rsrc);
return S_OK;
}
static HRESULT WINAPI resourcedataloader_Decompress(ID3DX10DataLoader *iface, void **data, SIZE_T *size)
{
struct asyncdataloader *loader = impl_from_ID3DX10DataLoader(iface);
TRACE("iface %p, data %p, size %p.\n", iface, data, size);
if (!loader->data)
return E_FAIL;
*data = loader->data;
*size = loader->size;
return S_OK;
}
static HRESULT WINAPI resourcedataloader_Destroy(ID3DX10DataLoader *iface)
{
struct asyncdataloader *loader = impl_from_ID3DX10DataLoader(iface);
TRACE("iface %p.\n", iface);
HeapFree(GetProcessHeap(), 0, loader);
return S_OK;
}
static const ID3DX10DataLoaderVtbl resourcedataloadervtbl =
{
resourcedataloader_Load,
resourcedataloader_Decompress,
resourcedataloader_Destroy
};
HRESULT WINAPI D3DX10CompileFromMemory(const char *data, SIZE_T data_size, const char *filename,
const D3D10_SHADER_MACRO *defines, ID3D10Include *include, const char *entry_point,
const char *target, UINT sflags, UINT eflags, ID3DX10ThreadPump *pump, ID3D10Blob **shader,
......@@ -70,35 +262,137 @@ HRESULT WINAPI D3DX10CreateEffectPoolFromFileW(const WCHAR *filename, const D3D1
HRESULT WINAPI D3DX10CreateAsyncMemoryLoader(const void *data, SIZE_T data_size, ID3DX10DataLoader **loader)
{
FIXME("data %p, data_size %lu, loader %p stub!\n", data, data_size, loader);
struct asyncdataloader *object;
return E_NOTIMPL;
TRACE("data %p, data_size %lu, loader %p.\n", data, data_size, loader);
if (!data || !loader)
return E_FAIL;
object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
if (!object)
return E_OUTOFMEMORY;
object->ID3DX10DataLoader_iface.lpVtbl = &memorydataloadervtbl;
object->data = (void *)data;
object->size = data_size;
*loader = &object->ID3DX10DataLoader_iface;
return S_OK;
}
HRESULT WINAPI D3DX10CreateAsyncFileLoaderA(const char *filename, ID3DX10DataLoader **loader)
{
FIXME("filename %s, loader %p stub!\n", debugstr_a(filename), loader);
WCHAR *filename_w;
HRESULT hr;
int len;
return E_NOTIMPL;
TRACE("filename %s, loader %p.\n", debugstr_a(filename), loader);
if (!filename || !loader)
return E_FAIL;
len = MultiByteToWideChar(CP_ACP, 0, filename, -1, NULL, 0);
filename_w = HeapAlloc(GetProcessHeap(), 0, len * sizeof(*filename_w));
MultiByteToWideChar(CP_ACP, 0, filename, -1, filename_w, len);
hr = D3DX10CreateAsyncFileLoaderW(filename_w, loader);
HeapFree(GetProcessHeap(), 0, filename_w);
return hr;
}
HRESULT WINAPI D3DX10CreateAsyncFileLoaderW(const WCHAR *filename, ID3DX10DataLoader **loader)
{
FIXME("filename %s, loader %p stub!\n", debugstr_w(filename), loader);
struct asyncdataloader *object;
return E_NOTIMPL;
TRACE("filename %s, loader %p.\n", debugstr_w(filename), loader);
if (!filename || !loader)
return E_FAIL;
object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
if (!object)
return E_OUTOFMEMORY;
object->ID3DX10DataLoader_iface.lpVtbl = &filedataloadervtbl;
object->u.file.path = HeapAlloc(GetProcessHeap(), 0, (strlenW(filename) + 1) * sizeof(WCHAR));
if (!object->u.file.path)
{
HeapFree(GetProcessHeap(), 0, object);
return E_OUTOFMEMORY;
}
strcpyW(object->u.file.path, filename);
object->data = NULL;
object->size = 0;
*loader = &object->ID3DX10DataLoader_iface;
return S_OK;
}
HRESULT WINAPI D3DX10CreateAsyncResourceLoaderA(HMODULE module, const char *resource, ID3DX10DataLoader **loader)
{
FIXME("module %p, resource %s, loader %p stub!\n", module, debugstr_a(resource), loader);
struct asyncdataloader *object;
HRSRC rsrc;
return E_NOTIMPL;
TRACE("module %p, resource %s, loader %p.\n", module, debugstr_a(resource), loader);
if (!loader)
return E_FAIL;
object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
if (!object)
return E_OUTOFMEMORY;
if (!(rsrc = FindResourceA(module, resource, (const char *)RT_RCDATA)))
{
ERR("Failed to find resource.\n");
HeapFree(GetProcessHeap(), 0, object);
return D3DX10_ERR_INVALID_DATA;
}
object->ID3DX10DataLoader_iface.lpVtbl = &resourcedataloadervtbl;
object->u.resource.module = module;
object->u.resource.rsrc = rsrc;
object->data = NULL;
object->size = 0;
*loader = &object->ID3DX10DataLoader_iface;
return S_OK;
}
HRESULT WINAPI D3DX10CreateAsyncResourceLoaderW(HMODULE module, const WCHAR *resource, ID3DX10DataLoader **loader)
{
FIXME("module %p, resource %s, loader %p stub!\n", module, debugstr_w(resource), loader);
struct asyncdataloader *object;
HRSRC rsrc;
return E_NOTIMPL;
TRACE("module %p, resource %s, loader %p.\n", module, debugstr_w(resource), loader);
if (!loader)
return E_FAIL;
object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
if (!object)
return E_OUTOFMEMORY;
if (!(rsrc = FindResourceW(module, resource, (const WCHAR *)RT_RCDATA)))
{
ERR("Failed to find resource.\n");
HeapFree(GetProcessHeap(), 0, object);
return D3DX10_ERR_INVALID_DATA;
}
object->ID3DX10DataLoader_iface.lpVtbl = &resourcedataloadervtbl;
object->u.resource.module = module;
object->u.resource.rsrc = rsrc;
object->data = NULL;
object->size = 0;
*loader = &object->ID3DX10DataLoader_iface;
return S_OK;
}
......@@ -614,16 +614,13 @@ static void test_D3DX10CreateAsyncMemoryLoader(void)
void *ptr;
hr = D3DX10CreateAsyncMemoryLoader(NULL, 0, NULL);
todo_wine ok(hr == E_FAIL, "Got unexpected hr %#x.\n", hr);
ok(hr == E_FAIL, "Got unexpected hr %#x.\n", hr);
hr = D3DX10CreateAsyncMemoryLoader(NULL, 0, &loader);
todo_wine ok(hr == E_FAIL, "Got unexpected hr %#x.\n", hr);
ok(hr == E_FAIL, "Got unexpected hr %#x.\n", hr);
hr = D3DX10CreateAsyncMemoryLoader(&data, 0, &loader);
todo_wine ok(hr == S_OK, "Got unexpected hr %#x.\n", hr);
if (FAILED(hr))
return;
ok(hr == S_OK, "Got unexpected hr %#x.\n", hr);
size = 100;
hr = ID3DX10DataLoader_Decompress(loader, &ptr, &size);
......@@ -655,24 +652,45 @@ static void test_D3DX10CreateAsyncMemoryLoader(void)
ok(hr == S_OK, "Got unexpected hr %#x.\n", hr);
}
static void create_testfile(WCHAR *path, const void *data, int data_len)
{
static const WCHAR test_filename[] = {'a','s','y','n','c','l','o','a','d','e','r','.','d','a','t','a',0};
DWORD written;
HANDLE file;
BOOL ret;
GetTempPathW(MAX_PATH, path);
lstrcatW(path, test_filename);
file = CreateFileW(path, GENERIC_READ | GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, 0);
ok(file != INVALID_HANDLE_VALUE, "Test file creation failed, at %s, error %d.\n", wine_dbgstr_w(path),
GetLastError());
ret = WriteFile(file, data, data_len, &written, NULL);
ok(ret, "Write to test file failed.\n");
CloseHandle(file);
}
static void test_D3DX10CreateAsyncFileLoader(void)
{
static const char test_data1[] = "test data";
static const char test_data2[] = "more test data";
ID3DX10DataLoader *loader;
WCHAR path[MAX_PATH];
SIZE_T size;
HRESULT hr;
void *ptr;
BOOL ret;
hr = D3DX10CreateAsyncFileLoaderA(NULL, NULL);
todo_wine ok(hr == E_FAIL, "Got unexpected hr %#x.\n", hr);
ok(hr == E_FAIL, "Got unexpected hr %#x.\n", hr);
hr = D3DX10CreateAsyncFileLoaderA(NULL, &loader);
todo_wine ok(hr == E_FAIL, "Got unexpected hr %#x.\n", hr);
ok(hr == E_FAIL, "Got unexpected hr %#x.\n", hr);
hr = D3DX10CreateAsyncFileLoaderA("nonexistentfilename", &loader);
todo_wine ok(hr == S_OK, "Got unexpected hr %#x.\n", hr);
if (FAILED(hr))
return;
ok(hr == S_OK, "Got unexpected hr %#x.\n", hr);
hr = ID3DX10DataLoader_Decompress(loader, &ptr, &size);
ok(hr == E_FAIL, "Got unexpected hr %#x.\n", hr);
......@@ -685,21 +703,89 @@ static void test_D3DX10CreateAsyncFileLoader(void)
hr = ID3DX10DataLoader_Destroy(loader);
ok(hr == S_OK, "Got unexpected hr %#x.\n", hr);
/* Test file sharing using dummy empty file. */
create_testfile(path, test_data1, sizeof(test_data1));
hr = D3DX10CreateAsyncFileLoaderW(path, &loader);
ok(SUCCEEDED(hr), "Failed to create file loader, hr %#x.\n", hr);
ret = DeleteFileW(path);
ok(ret, "DeleteFile() failed, ret %d, error %d.\n", ret, GetLastError());
/* File was removed before Load(). */
hr = ID3DX10DataLoader_Load(loader);
ok(hr == D3D10_ERROR_FILE_NOT_FOUND, "Load() returned unexpected result, hr %#x.\n", hr);
/* Create it again. */
create_testfile(path, test_data1, sizeof(test_data1));
hr = ID3DX10DataLoader_Load(loader);
ok(SUCCEEDED(hr), "Load() failed, hr %#x.\n", hr);
/* Already loaded. */
hr = ID3DX10DataLoader_Load(loader);
ok(SUCCEEDED(hr), "Load() failed, hr %#x.\n", hr);
ret = DeleteFileW(path);
ok(ret, "DeleteFile() failed, ret %d, error %d.\n", ret, GetLastError());
/* Already loaded, file removed. */
hr = ID3DX10DataLoader_Load(loader);
ok(hr == D3D10_ERROR_FILE_NOT_FOUND, "Load() returned unexpected result, hr %#x.\n", hr);
/* Decompress still works. */
ptr = NULL;
hr = ID3DX10DataLoader_Decompress(loader, &ptr, &size);
ok(SUCCEEDED(hr), "Decompress() failed, hr %#x.\n", hr);
ok(ptr != NULL, "Got unexpected ptr %p.\n", ptr);
ok(size == sizeof(test_data1), "Got unexpected decompressed size.\n");
if (size == sizeof(test_data1))
ok(!memcmp(ptr, test_data1, size), "Got unexpected file data.\n");
/* Create it again, with different data. */
create_testfile(path, test_data2, sizeof(test_data2));
hr = ID3DX10DataLoader_Load(loader);
ok(SUCCEEDED(hr), "Load() failed, hr %#x.\n", hr);
ptr = NULL;
hr = ID3DX10DataLoader_Decompress(loader, &ptr, &size);
ok(SUCCEEDED(hr), "Decompress() failed, hr %#x.\n", hr);
ok(ptr != NULL, "Got unexpected ptr %p.\n", ptr);
ok(size == sizeof(test_data2), "Got unexpected decompressed size.\n");
if (size == sizeof(test_data2))
ok(!memcmp(ptr, test_data2, size), "Got unexpected file data.\n");
hr = ID3DX10DataLoader_Destroy(loader);
ok(SUCCEEDED(hr), "Destroy() failed, hr %#x.\n", hr);
ret = DeleteFileW(path);
ok(ret, "DeleteFile() failed, ret %d, error %d.\n", ret, GetLastError());
}
static void test_D3DX10CreateAsyncResourceLoader(void)
{
static const WCHAR resource_name[] = {'n','o','n','a','m','e',0};
ID3DX10DataLoader *loader;
HRESULT hr;
hr = D3DX10CreateAsyncResourceLoaderA(NULL, NULL, NULL);
todo_wine ok(hr == E_FAIL, "Got unexpected hr %#x.\n", hr);
ok(hr == E_FAIL, "Got unexpected hr %#x.\n", hr);
hr = D3DX10CreateAsyncResourceLoaderA(NULL, NULL, &loader);
todo_wine ok(hr == D3DX10_ERR_INVALID_DATA, "Got unexpected hr %#x.\n", hr);
ok(hr == D3DX10_ERR_INVALID_DATA, "Got unexpected hr %#x.\n", hr);
hr = D3DX10CreateAsyncResourceLoaderA(NULL, "noname", &loader);
ok(hr == D3DX10_ERR_INVALID_DATA, "Got unexpected hr %#x.\n", hr);
hr = D3DX10CreateAsyncResourceLoaderW(NULL, NULL, NULL);
ok(hr == E_FAIL, "Got unexpected hr %#x.\n", hr);
hr = D3DX10CreateAsyncResourceLoaderW(NULL, NULL, &loader);
ok(hr == D3DX10_ERR_INVALID_DATA, "Got unexpected hr %#x.\n", hr);
hr = D3DX10CreateAsyncResourceLoaderA(NULL, "nonexistentresourcename", &loader);
todo_wine ok(hr == D3DX10_ERR_INVALID_DATA, "Got unexpected hr %#x.\n", hr);
hr = D3DX10CreateAsyncResourceLoaderW(NULL, resource_name, &loader);
ok(hr == D3DX10_ERR_INVALID_DATA, "Got unexpected hr %#x.\n", hr);
}
START_TEST(d3dx10)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment