summaryrefslogtreecommitdiffstats
path: root/src/libs/dxvk-native-1.9.2a/tests/dxbc/test_hlsl_compiler.cpp
blob: b16708785a0d4464209493e47e77044d4a8a33b8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#include <cstring>
#include <fstream>
#include <vector>

#include <d3dcompiler.h>

#include <shellapi.h>
#include <windows.h>
#include <windowsx.h>

#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 [--strip] [--text]" << std::endl;
    return 1;
  }

  bool strip = false;
  bool text = false;

  for (int i = 5; i < argc; i++) {
    strip |= str::fromws(argv[i]) == "--strip";
    text  |= str::fromws(argv[i]) == "--text";
  }
  
  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<std::streamsize>::max());
  std::streamsize length = ifile.gcount();
  ifile.clear();
  
  ifile.seekg(0, std::ios_base::beg);
  std::vector<char> hlslCode(length);
  ifile.read(hlslCode.data(), length);
  
  Com<ID3DBlob> binary;
  Com<ID3DBlob> errors;
  
  HRESULT hr = D3DCompile(
    hlslCode.data(),
    hlslCode.size(),
    "Shader", nullptr, nullptr,
    str::fromws(entryPoint).c_str(),
    str::fromws(target).c_str(),
    D3DCOMPILE_OPTIMIZATION_LEVEL3 |
    D3DCOMPILE_ENABLE_UNBOUNDED_DESCRIPTOR_TABLES,
    0, &binary, &errors);

  if (FAILED(hr)) {
    if (errors != nullptr)
      std::cerr << reinterpret_cast<const char*>(errors->GetBufferPointer()) << std::endl;
    return 1;
  }
  
  if (strip) {
    Com<ID3DBlob> strippedBlob;

    hr = D3DStripShader(binary->GetBufferPointer(), binary->GetBufferSize(),
      D3DCOMPILER_STRIP_REFLECTION_DATA | D3DCOMPILER_STRIP_DEBUG_INFO,
      &strippedBlob);

    if (FAILED(hr)) {
      std::cerr << "Failed to strip shader" << std::endl;
      return 1;
    }

    binary = strippedBlob;
  }

  std::ofstream file;

  if (str::fromws(outputFile) != "-")
    file = std::ofstream(str::fromws(outputFile), std::ios::binary | std::ios::trunc);

  std::ostream& outputStream = file.is_open() ? file : std::cout;

  if (text) {
    auto data = reinterpret_cast<const uint32_t*>(binary->GetBufferPointer());
    auto size = binary->GetBufferSize() / sizeof(uint32_t);

    outputStream << std::hex;

    for (uint32_t i = 0; i < size; i++) {
      if (i && !(i & 0x7))
        outputStream << std::endl;
      outputStream << "0x" << std::setfill('0') << std::setw(8) << data[i] << ", ";
    }

    outputStream << std::endl;
  } else {
    outputStream.write(reinterpret_cast<const char*>(binary->GetBufferPointer()), binary->GetBufferSize());
  }

  return 0;
}