summaryrefslogtreecommitdiff
path: root/servers/rendering/shader_warnings.cpp
blob: dce8f6cff11adbbcf865f65b80fcfc027226f155 (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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/**************************************************************************/
/*  shader_warnings.cpp                                                   */
/**************************************************************************/
/*                         This file is part of:                          */
/*                             GODOT ENGINE                               */
/*                        https://godotengine.org                         */
/**************************************************************************/
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur.                  */
/*                                                                        */
/* Permission is hereby granted, free of charge, to any person obtaining  */
/* a copy of this software and associated documentation files (the        */
/* "Software"), to deal in the Software without restriction, including    */
/* without limitation the rights to use, copy, modify, merge, publish,    */
/* distribute, sublicense, and/or sell copies of the Software, and to     */
/* permit persons to whom the Software is furnished to do so, subject to  */
/* the following conditions:                                              */
/*                                                                        */
/* The above copyright notice and this permission notice shall be         */
/* included in all copies or substantial portions of the Software.        */
/*                                                                        */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,        */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF     */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY   */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,   */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE      */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.                 */
/**************************************************************************/

#include "shader_warnings.h"
#include "core/variant/variant.h"

#ifdef DEBUG_ENABLED

ShaderWarning::Code ShaderWarning::get_code() const {
	return code;
}

int ShaderWarning::get_line() const {
	return line;
}

const StringName &ShaderWarning::get_subject() const {
	return subject;
}

String ShaderWarning::get_message() const {
	switch (code) {
		case FLOAT_COMPARISON:
			return vformat(RTR("Direct floating-point comparison (this may not evaluate to `true` as you expect). Instead, use `abs(a - b) < 0.0001` for an approximate but predictable comparison."));
		case UNUSED_CONSTANT:
			return vformat(RTR("The const '%s' is declared but never used."), subject);
		case UNUSED_FUNCTION:
			return vformat(RTR("The function '%s' is declared but never used."), subject);
		case UNUSED_STRUCT:
			return vformat(RTR("The struct '%s' is declared but never used."), subject);
		case UNUSED_UNIFORM:
			return vformat(RTR("The uniform '%s' is declared but never used."), subject);
		case UNUSED_VARYING:
			return vformat(RTR("The varying '%s' is declared but never used."), subject);
		case UNUSED_LOCAL_VARIABLE:
			return vformat(RTR("The local variable '%s' is declared but never used."), subject);
		case FORMATTING_ERROR:
			return subject;
		case DEVICE_LIMIT_EXCEEDED:
			return vformat(RTR("The total size of the %s for this shader on this device has been exceeded (%d/%d). The shader may not work correctly."), subject, (int)extra_args[0], (int)extra_args[1]);
		default:
			break;
	}
	return String();
}

String ShaderWarning::get_name() const {
	return get_name_from_code(code);
}

Vector<Variant> ShaderWarning::get_extra_args() const {
	return extra_args;
}

String ShaderWarning::get_name_from_code(Code p_code) {
	ERR_FAIL_INDEX_V(p_code, WARNING_MAX, String());

	static const char *names[] = {
		"FLOAT_COMPARISON",
		"UNUSED_CONSTANT",
		"UNUSED_FUNCTION",
		"UNUSED_STRUCT",
		"UNUSED_UNIFORM",
		"UNUSED_VARYING",
		"UNUSED_LOCAL_VARIABLE",
		"FORMATTING_ERROR",
		"DEVICE_LIMIT_EXCEEDED",
	};

	static_assert((sizeof(names) / sizeof(*names)) == WARNING_MAX, "Amount of warning types don't match the amount of warning names.");

	return names[(int)p_code];
}

ShaderWarning::Code ShaderWarning::get_code_from_name(const String &p_name) {
	for (int i = 0; i < WARNING_MAX; i++) {
		if (get_name_from_code((Code)i) == p_name) {
			return (Code)i;
		}
	}

	ERR_FAIL_V_MSG(WARNING_MAX, "Invalid shader warning name: " + p_name);
}

static HashMap<int, uint32_t> *code_to_flags_map = nullptr;

static void init_code_to_flags_map() {
	code_to_flags_map = memnew((HashMap<int, uint32_t>));
	code_to_flags_map->insert(ShaderWarning::FLOAT_COMPARISON, ShaderWarning::FLOAT_COMPARISON_FLAG);
	code_to_flags_map->insert(ShaderWarning::UNUSED_CONSTANT, ShaderWarning::UNUSED_CONSTANT_FLAG);
	code_to_flags_map->insert(ShaderWarning::UNUSED_FUNCTION, ShaderWarning::UNUSED_FUNCTION_FLAG);
	code_to_flags_map->insert(ShaderWarning::UNUSED_STRUCT, ShaderWarning::UNUSED_STRUCT_FLAG);
	code_to_flags_map->insert(ShaderWarning::UNUSED_UNIFORM, ShaderWarning::UNUSED_UNIFORM_FLAG);
	code_to_flags_map->insert(ShaderWarning::UNUSED_VARYING, ShaderWarning::UNUSED_VARYING_FLAG);
	code_to_flags_map->insert(ShaderWarning::UNUSED_LOCAL_VARIABLE, ShaderWarning::UNUSED_LOCAL_VARIABLE_FLAG);
	code_to_flags_map->insert(ShaderWarning::FORMATTING_ERROR, ShaderWarning::FORMATTING_ERROR_FLAG);
	code_to_flags_map->insert(ShaderWarning::DEVICE_LIMIT_EXCEEDED, ShaderWarning::DEVICE_LIMIT_EXCEEDED_FLAG);
}

ShaderWarning::CodeFlags ShaderWarning::get_flags_from_codemap(const HashMap<Code, bool> &p_map) {
	uint32_t result = 0U;

	if (code_to_flags_map == nullptr) {
		init_code_to_flags_map();
	}

	for (const KeyValue<Code, bool> &E : p_map) {
		if (E.value) {
			ERR_FAIL_COND_V(!code_to_flags_map->has((int)E.key), ShaderWarning::NONE_FLAG);
			result |= (*code_to_flags_map)[(int)E.key];
		}
	}
	return (CodeFlags)result;
}

ShaderWarning::ShaderWarning(Code p_code, int p_line, const StringName &p_subject, const Vector<Variant> &p_extra_args) :
		code(p_code), line(p_line), subject(p_subject), extra_args(p_extra_args) {
}

#endif // DEBUG_ENABLED