Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/servers/rendering/shader_compiler.h
10277 views
1
/**************************************************************************/
2
/* shader_compiler.h */
3
/**************************************************************************/
4
/* This file is part of: */
5
/* GODOT ENGINE */
6
/* https://godotengine.org */
7
/**************************************************************************/
8
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10
/* */
11
/* Permission is hereby granted, free of charge, to any person obtaining */
12
/* a copy of this software and associated documentation files (the */
13
/* "Software"), to deal in the Software without restriction, including */
14
/* without limitation the rights to use, copy, modify, merge, publish, */
15
/* distribute, sublicense, and/or sell copies of the Software, and to */
16
/* permit persons to whom the Software is furnished to do so, subject to */
17
/* the following conditions: */
18
/* */
19
/* The above copyright notice and this permission notice shall be */
20
/* included in all copies or substantial portions of the Software. */
21
/* */
22
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29
/**************************************************************************/
30
31
#pragma once
32
33
#include "core/templates/pair.h"
34
#include "servers/rendering/shader_language.h"
35
#include "servers/rendering_server.h"
36
37
class ShaderCompiler {
38
public:
39
enum Stage {
40
STAGE_VERTEX,
41
STAGE_FRAGMENT,
42
STAGE_COMPUTE,
43
STAGE_MAX
44
};
45
46
struct IdentifierActions {
47
HashMap<StringName, Stage> entry_point_stages;
48
49
HashMap<StringName, Pair<int *, int>> render_mode_values;
50
HashMap<StringName, bool *> render_mode_flags;
51
HashMap<StringName, bool *> usage_flag_pointers;
52
HashMap<StringName, bool *> write_flag_pointers;
53
HashMap<StringName, Pair<int *, int>> stencil_mode_values;
54
int *stencil_reference = nullptr;
55
56
HashMap<StringName, ShaderLanguage::ShaderNode::Uniform> *uniforms = nullptr;
57
};
58
59
struct GeneratedCode {
60
Vector<String> defines;
61
struct Texture {
62
StringName name;
63
ShaderLanguage::DataType type = ShaderLanguage::DataType::TYPE_VOID;
64
ShaderLanguage::ShaderNode::Uniform::Hint hint = ShaderLanguage::ShaderNode::Uniform::Hint::HINT_NONE;
65
bool use_color = false;
66
ShaderLanguage::TextureFilter filter = ShaderLanguage::TextureFilter::FILTER_DEFAULT;
67
ShaderLanguage::TextureRepeat repeat = ShaderLanguage::TextureRepeat::REPEAT_DEFAULT;
68
bool global = false;
69
int array_size = 0;
70
};
71
72
Vector<Texture> texture_uniforms;
73
74
Vector<uint32_t> uniform_offsets;
75
uint32_t uniform_total_size = 0;
76
String uniforms;
77
String stage_globals[STAGE_MAX];
78
79
HashMap<String, String> code;
80
81
bool uses_global_textures = false;
82
bool uses_fragment_time = false;
83
bool uses_vertex_time = false;
84
bool uses_screen_texture_mipmaps = false;
85
bool uses_screen_texture = false;
86
bool uses_depth_texture = false;
87
bool uses_normal_roughness_texture = false;
88
};
89
90
struct DefaultIdentifierActions {
91
HashMap<StringName, String> renames;
92
HashMap<StringName, String> render_mode_defines;
93
HashMap<StringName, String> usage_defines;
94
HashMap<StringName, String> custom_samplers;
95
ShaderLanguage::TextureFilter default_filter = ShaderLanguage::TextureFilter::FILTER_NEAREST;
96
ShaderLanguage::TextureRepeat default_repeat = ShaderLanguage::TextureRepeat::REPEAT_DISABLE;
97
int base_texture_binding_index = 0;
98
int texture_layout_set = 0;
99
String base_uniform_string;
100
String global_buffer_array_variable;
101
String instance_uniform_index_variable;
102
uint32_t base_varying_index = 0;
103
bool apply_luminance_multiplier = false;
104
bool check_multiview_samplers = false;
105
};
106
107
private:
108
ShaderLanguage parser;
109
110
String _get_sampler_name(ShaderLanguage::TextureFilter p_filter, ShaderLanguage::TextureRepeat p_repeat);
111
112
void _dump_function_deps(const ShaderLanguage::ShaderNode *p_node, const StringName &p_for_func, const HashMap<StringName, String> &p_func_code, String &r_to_add, HashSet<StringName> &added);
113
String _dump_node_code(const ShaderLanguage::Node *p_node, int p_level, GeneratedCode &r_gen_code, IdentifierActions &p_actions, const DefaultIdentifierActions &p_default_actions, bool p_assigning, bool p_scope = true);
114
115
const ShaderLanguage::ShaderNode *shader = nullptr;
116
const ShaderLanguage::FunctionNode *function = nullptr;
117
StringName current_func_name;
118
StringName time_name;
119
HashSet<StringName> texture_functions;
120
121
HashSet<StringName> used_name_defines;
122
HashSet<StringName> used_flag_pointers;
123
HashSet<StringName> used_rmode_defines;
124
HashSet<StringName> internal_functions;
125
HashSet<StringName> fragment_varyings;
126
127
DefaultIdentifierActions actions;
128
129
static ShaderLanguage::DataType _get_global_shader_uniform_type(const StringName &p_name);
130
131
public:
132
Error compile(RS::ShaderMode p_mode, const String &p_code, IdentifierActions *p_actions, const String &p_path, GeneratedCode &r_gen_code);
133
134
void initialize(DefaultIdentifierActions p_actions);
135
ShaderCompiler();
136
};
137
138