| 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 | #ifndef SHADER_COMPILER_H |
| 32 | #define SHADER_COMPILER_H |
| 33 | |
| 34 | #include "core/templates/pair.h" |
| 35 | #include "servers/rendering/shader_language.h" |
| 36 | #include "servers/rendering_server.h" |
| 37 | |
| 38 | class ShaderCompiler { |
| 39 | public: |
| 40 | enum Stage { |
| 41 | STAGE_VERTEX, |
| 42 | STAGE_FRAGMENT, |
| 43 | STAGE_COMPUTE, |
| 44 | STAGE_MAX |
| 45 | }; |
| 46 | |
| 47 | struct IdentifierActions { |
| 48 | HashMap<StringName, Stage> entry_point_stages; |
| 49 | |
| 50 | HashMap<StringName, Pair<int *, int>> render_mode_values; |
| 51 | HashMap<StringName, bool *> render_mode_flags; |
| 52 | HashMap<StringName, bool *> usage_flag_pointers; |
| 53 | HashMap<StringName, bool *> write_flag_pointers; |
| 54 | |
| 55 | HashMap<StringName, ShaderLanguage::ShaderNode::Uniform> *uniforms; |
| 56 | }; |
| 57 | |
| 58 | struct GeneratedCode { |
| 59 | Vector<String> defines; |
| 60 | struct Texture { |
| 61 | StringName name; |
| 62 | ShaderLanguage::DataType type; |
| 63 | ShaderLanguage::ShaderNode::Uniform::Hint hint; |
| 64 | bool use_color = false; |
| 65 | ShaderLanguage::TextureFilter filter; |
| 66 | ShaderLanguage::TextureRepeat repeat; |
| 67 | bool global; |
| 68 | int array_size; |
| 69 | }; |
| 70 | |
| 71 | Vector<Texture> texture_uniforms; |
| 72 | |
| 73 | Vector<uint32_t> uniform_offsets; |
| 74 | uint32_t uniform_total_size; |
| 75 | String uniforms; |
| 76 | String stage_globals[STAGE_MAX]; |
| 77 | |
| 78 | HashMap<String, String> code; |
| 79 | |
| 80 | bool uses_global_textures; |
| 81 | bool uses_fragment_time; |
| 82 | bool uses_vertex_time; |
| 83 | bool uses_screen_texture_mipmaps; |
| 84 | bool uses_screen_texture; |
| 85 | bool uses_depth_texture; |
| 86 | bool uses_normal_roughness_texture; |
| 87 | }; |
| 88 | |
| 89 | struct DefaultIdentifierActions { |
| 90 | HashMap<StringName, String> renames; |
| 91 | HashMap<StringName, String> render_mode_defines; |
| 92 | HashMap<StringName, String> usage_defines; |
| 93 | HashMap<StringName, String> custom_samplers; |
| 94 | ShaderLanguage::TextureFilter default_filter; |
| 95 | ShaderLanguage::TextureRepeat default_repeat; |
| 96 | int base_texture_binding_index = 0; |
| 97 | int texture_layout_set = 0; |
| 98 | String base_uniform_string; |
| 99 | String global_buffer_array_variable; |
| 100 | String instance_uniform_index_variable; |
| 101 | uint32_t base_varying_index = 0; |
| 102 | bool apply_luminance_multiplier = false; |
| 103 | bool check_multiview_samplers = false; |
| 104 | }; |
| 105 | |
| 106 | private: |
| 107 | ShaderLanguage parser; |
| 108 | |
| 109 | String _get_sampler_name(ShaderLanguage::TextureFilter p_filter, ShaderLanguage::TextureRepeat p_repeat); |
| 110 | |
| 111 | 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); |
| 112 | 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); |
| 113 | |
| 114 | const ShaderLanguage::ShaderNode *shader = nullptr; |
| 115 | const ShaderLanguage::FunctionNode *function = nullptr; |
| 116 | StringName current_func_name; |
| 117 | StringName time_name; |
| 118 | HashSet<StringName> texture_functions; |
| 119 | |
| 120 | HashSet<StringName> used_name_defines; |
| 121 | HashSet<StringName> used_flag_pointers; |
| 122 | HashSet<StringName> used_rmode_defines; |
| 123 | HashSet<StringName> internal_functions; |
| 124 | HashSet<StringName> fragment_varyings; |
| 125 | |
| 126 | DefaultIdentifierActions actions; |
| 127 | |
| 128 | static ShaderLanguage::DataType _get_global_shader_uniform_type(const StringName &p_name); |
| 129 | |
| 130 | public: |
| 131 | Error compile(RS::ShaderMode p_mode, const String &p_code, IdentifierActions *p_actions, const String &p_path, GeneratedCode &r_gen_code); |
| 132 | |
| 133 | void initialize(DefaultIdentifierActions p_actions); |
| 134 | ShaderCompiler(); |
| 135 | }; |
| 136 | |
| 137 | #endif // SHADER_COMPILER_H |
| 138 | |