1 /*************************************************************************/
2 /*  shader_compiler_gles2.h                                              */
3 /*************************************************************************/
4 /*                       This file is part of:                           */
5 /*                           GODOT ENGINE                                */
6 /*                      https://godotengine.org                          */
7 /*************************************************************************/
8 /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur.                 */
9 /* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md).   */
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 SHADERCOMPILERGLES2_H
32 #define SHADERCOMPILERGLES2_H
33 
34 #include "core/pair.h"
35 #include "core/string_builder.h"
36 #include "servers/visual/shader_language.h"
37 #include "servers/visual/shader_types.h"
38 #include "servers/visual_server.h"
39 
40 class ShaderCompilerGLES2 {
41 public:
42 	struct IdentifierActions {
43 
44 		Map<StringName, Pair<int *, int> > render_mode_values;
45 		Map<StringName, bool *> render_mode_flags;
46 		Map<StringName, bool *> usage_flag_pointers;
47 		Map<StringName, bool *> write_flag_pointers;
48 
49 		Map<StringName, ShaderLanguage::ShaderNode::Uniform> *uniforms;
50 	};
51 
52 	struct GeneratedCode {
53 
54 		Vector<CharString> custom_defines;
55 		Vector<StringName> uniforms;
56 		Vector<StringName> texture_uniforms;
57 		Vector<ShaderLanguage::ShaderNode::Uniform::Hint> texture_hints;
58 
59 		String vertex_global;
60 		String vertex;
61 		String fragment_global;
62 		String fragment;
63 		String light;
64 
65 		bool uses_fragment_time;
66 		bool uses_vertex_time;
67 	};
68 
69 private:
70 	ShaderLanguage parser;
71 
72 	struct DefaultIdentifierActions {
73 
74 		Map<StringName, String> renames;
75 		Map<StringName, String> render_mode_defines;
76 		Map<StringName, String> usage_defines;
77 	};
78 
79 	void _dump_function_deps(ShaderLanguage::ShaderNode *p_node, const StringName &p_for_func, const Map<StringName, String> &p_func_code, StringBuilder &r_to_add, Set<StringName> &r_added);
80 	String _dump_node_code(ShaderLanguage::Node *p_node, int p_level, GeneratedCode &r_gen_code, IdentifierActions &p_actions, const DefaultIdentifierActions &p_default_actions, bool p_assigning, bool p_use_scope = true);
81 
82 	StringName current_func_name;
83 	StringName vertex_name;
84 	StringName fragment_name;
85 	StringName light_name;
86 	StringName time_name;
87 
88 	Set<StringName> used_name_defines;
89 	Set<StringName> used_flag_pointers;
90 	Set<StringName> used_rmode_defines;
91 	Set<StringName> internal_functions;
92 
93 	DefaultIdentifierActions actions[VS::SHADER_MAX];
94 
95 public:
96 	Error compile(VS::ShaderMode p_mode, const String &p_code, IdentifierActions *p_actions, const String &p_path, GeneratedCode &r_gen_code);
97 
98 	ShaderCompilerGLES2();
99 };
100 
101 #endif // SHADERCOMPILERGLES3_H
102