summaryrefslogtreecommitdiff
path: root/Source/Core/VideoBackends/D3D12/D3DShader.cpp
blob: d99a02c5ac0e93f768c7a02e5d62a40e4b45b290 (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
// Copyright 2010 Dolphin Emulator Project
// Licensed under GPLv2+
// Refer to the license.txt file included.

#include <fstream>
#include <string>

#include "Common/FileUtil.h"
#include "Common/MsgHandler.h"
#include "Common/StringUtil.h"
#include "Common/Logging/Log.h"
#include "VideoBackends/D3D12/D3DBase.h"
#include "VideoBackends/D3D12/D3DShader.h"
#include "VideoCommon/VideoConfig.h"

namespace DX12
{

namespace D3D
{

bool CompileShader(const std::string& code, ID3DBlob** blob, const D3D_SHADER_MACRO* defines, const std::string& shader_version_string)
{
	ID3D10Blob* shader_buffer = nullptr;
	ID3D10Blob* error_buffer = nullptr;

#if defined(_DEBUG) || defined(DEBUGFAST)
	UINT flags = D3DCOMPILE_ENABLE_BACKWARDS_COMPATIBILITY | D3DCOMPILE_DEBUG;
#else
	UINT flags = D3DCOMPILE_ENABLE_BACKWARDS_COMPATIBILITY | D3DCOMPILE_OPTIMIZATION_LEVEL3 | D3DCOMPILE_SKIP_VALIDATION;
#endif
	HRESULT hr = d3d_compile(code.c_str(), code.length(), nullptr, defines, nullptr, "main", shader_version_string.data(),
		flags, 0, &shader_buffer, &error_buffer);

	if (error_buffer)
	{
		WARN_LOG(VIDEO, "Warning generated when compiling %s shader:\n%s\n",
			shader_version_string.c_str(),
			static_cast<const char*>(error_buffer->GetBufferPointer()));
	}

	if (FAILED(hr))
	{
		static int num_failures = 0;
		std::string filename = StringFromFormat("%sbad_%s_%04i.txt", File::GetUserPath(D_DUMP_IDX).c_str(), shader_version_string.c_str(), num_failures++);
		std::ofstream file;
		OpenFStream(file, filename, std::ios_base::out);
		file << code;
		file << std::endl << "Errors:" << std::endl;
		file << static_cast<const char*>(error_buffer->GetBufferPointer());
		file.close();

		PanicAlert("Failed to compile shader: %s\nDebug info (%s):\n%s",
			filename.c_str(),
			shader_version_string.c_str(),
			static_cast<const char*>(error_buffer->GetBufferPointer()));

		*blob = nullptr;
		error_buffer->Release();
	}
	else
	{
		*blob = shader_buffer;
	}

	return SUCCEEDED(hr);
}

// code->bytecode
bool CompileVertexShader(const std::string& code, ID3DBlob** blob)
{
	return CompileShader(code, blob, nullptr, D3D::VertexShaderVersionString());
}

// code->bytecode
bool CompileGeometryShader(const std::string& code, ID3DBlob** blob, const D3D_SHADER_MACRO* defines)
{
	return CompileShader(code, blob, defines, D3D::GeometryShaderVersionString());
}

// code->bytecode
bool CompilePixelShader(const std::string& code, ID3DBlob** blob, const D3D_SHADER_MACRO* defines)
{
	return CompileShader(code, blob, defines, D3D::PixelShaderVersionString());
}

}  // namespace

}  // namespace DX12