From d66593fab5430f3c082f5ed283b64258cd6727b6 Mon Sep 17 00:00:00 2001 From: Philip Rebohle Date: Tue, 26 Dec 2017 01:04:03 +0100 Subject: [PATCH] [tests] Added simple app that compiles HLSL shaders --- tests/dxbc/meson.build | 3 +- tests/dxbc/test_hlsl_compiler.cpp | 61 +++++++++++++++++++++++++++++++ 2 files changed, 63 insertions(+), 1 deletion(-) create mode 100644 tests/dxbc/test_hlsl_compiler.cpp diff --git a/tests/dxbc/meson.build b/tests/dxbc/meson.build index 01c71fb0..0ed7e9c9 100644 --- a/tests/dxbc/meson.build +++ b/tests/dxbc/meson.build @@ -1,4 +1,5 @@ test_dxbc_deps = [ dxbc_dep, dxvk_dep ] executable('dxbc-compiler', files('test_dxbc_compiler.cpp'), dependencies : test_dxbc_deps, install : true) -executable('dxbc-disasm', files('test_dxbc_disasm.cpp'), dependencies : [ test_dxbc_deps, lib_d3dcompiler_47 ], install : true) \ No newline at end of file +executable('dxbc-disasm', files('test_dxbc_disasm.cpp'), dependencies : [ test_dxbc_deps, lib_d3dcompiler_47 ], install : true) +executable('hlsl-compiler', files('test_hlsl_compiler.cpp'), dependencies : [ test_dxbc_deps, lib_d3dcompiler_47 ], install : true) diff --git a/tests/dxbc/test_hlsl_compiler.cpp b/tests/dxbc/test_hlsl_compiler.cpp new file mode 100644 index 00000000..38b56f3e --- /dev/null +++ b/tests/dxbc/test_hlsl_compiler.cpp @@ -0,0 +1,61 @@ +#include +#include + +#include + +#include +#include +#include + +#include "../test_utils.h" + +using namespace dxvk; + +int WINAPI WinMain(HINSTANCE hInstance, + HINSTANCE hPrevInstance, + LPSTR lpCmdLine, + int nCmdShow) { + int argc = 0; + LPWSTR* argv = CommandLineToArgvW( + GetCommandLineW(), &argc); + + if (argc < 5) { + std::cerr << "Usage: hlsl-compiler target entrypoint input.hlsl output.dxbc" << std::endl; + return 1; + } + + const LPWSTR target = argv[1]; + const LPWSTR entryPoint = argv[2]; + const LPWSTR inputFile = argv[3]; + const LPWSTR outputFile = argv[4]; + + std::ifstream ifile(str::fromws(inputFile), std::ios::binary); + ifile.ignore(std::numeric_limits::max()); + std::streamsize length = ifile.gcount(); + ifile.clear(); + + ifile.seekg(0, std::ios_base::beg); + std::vector hlslCode(length); + ifile.read(hlslCode.data(), length); + + Com binary; + Com errors; + + HRESULT hr = D3DCompile( + hlslCode.data(), + hlslCode.size(), + "Shader", nullptr, nullptr, + str::fromws(entryPoint).c_str(), + str::fromws(target).c_str(), + 0, 0, &binary, &errors); + + if (FAILED(hr)) { + if (errors != nullptr) + std::cerr << reinterpret_cast(errors->GetBufferPointer()) << std::endl; + return 1; + } + + std::ofstream outputStream(str::fromws(outputFile), std::ios::binary | std::ios::trunc); + outputStream.write(reinterpret_cast(binary->GetBufferPointer()), binary->GetBufferSize()); + return 0; +}