Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions tools/clang/unittests/HLSLExec/ExecHLSLTests.rc
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include <windows.h>

ShaderOpArithTable.xml DATASOURCE_XML "ShaderOpArithTable.xml"
LongVectorOp DATASOURCE_XML "LongVectorOp.xml"
76 changes: 62 additions & 14 deletions tools/clang/unittests/HLSLExec/HlslExecTestUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,20 @@ static UINT getD3D12SDKVersion(std::wstring SDKPath) {
return SDKVersion;
}

// Simple wrapper to free the loaded module on scope exit.
struct DllWrapper {
HMODULE Module = NULL; // NOLINT

~DllWrapper() { Close(); }

void Close() {
if (Module) {
FreeLibrary(Module);
Module = NULL;
}
}
};

static bool createDevice(
ID3D12Device **D3DDevice, D3D_SHADER_MODEL TestModel, bool SkipUnsupported,
std::function<HRESULT(IUnknown *, D3D_FEATURE_LEVEL, REFIID, void **)>
Expand Down Expand Up @@ -108,20 +122,7 @@ static bool createDevice(
// load. To force this to be used, we make sure that this DLL is loaded
// before attempting to create the device.

struct WarpDll {
HMODULE Module = NULL; // NOLINT

~WarpDll() { Close(); }

void Close() {
if (Module) {
FreeLibrary(Module);
Module = NULL;
}
}
};

WarpDll ExplicitlyLoadedWarpDll;
DllWrapper ExplicitlyLoadedWarpDll;
WEX::Common::String WarpDllPath;
if (SUCCEEDED(WEX::TestExecution::RuntimeParameters::TryGetValue(
L"WARP_DLL", WarpDllPath))) {
Expand Down Expand Up @@ -212,6 +213,53 @@ static bool createDevice(
return true;
}

// Read a resource embedded into a dll via an .rc file and wrap it in a DXC
// read-only stream
void readEmbeddedHlslDataIntoNewStream(
LPCWSTR ResourceName, // Resource name in rc file. e.g. L"LongVectorOp"
IStream **TestXML, dxc::SpecificDllLoader &Support) {

DllWrapper Dll;
Dll.Module = LoadLibraryEx(TEXT("ExecHLSLTests.dll"), nullptr,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@damyanp You mentioned the hard coded module name would be a problem here. I think we can change to using the name from GetModuleFileName to handle the tests have a different dll name?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What HMODULE would you pass to GetModuleFilename?

LOAD_LIBRARY_AS_DATAFILE);

// 1. Locate the resource
HRSRC ResInfo = FindResourceW(Dll.Module, ResourceName, L"DATASOURCE_XML");
if (!ResInfo)
VERIFY_SUCCEEDED(HRESULT_FROM_WIN32(::GetLastError()));

// 2. Load the resource
HGLOBAL ResData = LoadResource(Dll.Module, ResInfo);
if (!ResData)
VERIFY_SUCCEEDED(HRESULT_FROM_WIN32(::GetLastError()));
VERIFY_SUCCEEDED(HRESULT_FROM_WIN32(::GetLastError()));

// 3. Access the resource bytes
const void *Data = LockResource(ResData);
VERIFY_IS_NOT_NULL(Data);

// Sanity
const DWORD Size = SizeofResource(Dll.Module, ResInfo);
VERIFY_IS_FALSE(0 == Size);

VERIFY_SUCCEEDED(
Support.InitializeForDll(dxc::kDxCompilerLib, "DxcCreateInstance"));

CComPtr<IDxcLibrary> Library;
VERIFY_SUCCEEDED(Support.CreateInstance(CLSID_DxcLibrary, &Library));

// 4. Create a DXC blob from the resource data
CComPtr<IDxcBlobEncoding> Blob;
VERIFY_SUCCEEDED(
Library->CreateBlobWithEncodingFromPinned(Data, Size, CP_UTF8, &Blob));

// 5. Create a read-only stream from the DXC blob
CComPtr<IStream> Stream;
VERIFY_SUCCEEDED(Library->CreateStreamFromBlobReadOnly(Blob, &Stream));

*TestXML = Stream.Detach();
}

void readHlslDataIntoNewStream(LPCWSTR RelativePath, IStream **Stream,
dxc::SpecificDllLoader &Support) {
VERIFY_SUCCEEDED(
Expand Down
2 changes: 2 additions & 0 deletions tools/clang/unittests/HLSLExec/HlslExecTestUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ class D3D12SDKSelector {
bool SkipUnsupported = true);
};

void readEmbeddedHlslDataIntoNewStream(LPCWSTR ResourceName, IStream **Stream,
dxc::SpecificDllLoader &Support);
void readHlslDataIntoNewStream(LPCWSTR RelativePath, IStream **Stream,
dxc::SpecificDllLoader &Support);

Expand Down
Loading