Path: blob/master/servers/rendering/shader_compiler.h
10277 views
/**************************************************************************/1/* shader_compiler.h */2/**************************************************************************/3/* This file is part of: */4/* GODOT ENGINE */5/* https://godotengine.org */6/**************************************************************************/7/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */8/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */9/* */10/* Permission is hereby granted, free of charge, to any person obtaining */11/* a copy of this software and associated documentation files (the */12/* "Software"), to deal in the Software without restriction, including */13/* without limitation the rights to use, copy, modify, merge, publish, */14/* distribute, sublicense, and/or sell copies of the Software, and to */15/* permit persons to whom the Software is furnished to do so, subject to */16/* the following conditions: */17/* */18/* The above copyright notice and this permission notice shall be */19/* included in all copies or substantial portions of the Software. */20/* */21/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */22/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */23/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */24/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */25/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */26/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */27/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */28/**************************************************************************/2930#pragma once3132#include "core/templates/pair.h"33#include "servers/rendering/shader_language.h"34#include "servers/rendering_server.h"3536class ShaderCompiler {37public:38enum Stage {39STAGE_VERTEX,40STAGE_FRAGMENT,41STAGE_COMPUTE,42STAGE_MAX43};4445struct IdentifierActions {46HashMap<StringName, Stage> entry_point_stages;4748HashMap<StringName, Pair<int *, int>> render_mode_values;49HashMap<StringName, bool *> render_mode_flags;50HashMap<StringName, bool *> usage_flag_pointers;51HashMap<StringName, bool *> write_flag_pointers;52HashMap<StringName, Pair<int *, int>> stencil_mode_values;53int *stencil_reference = nullptr;5455HashMap<StringName, ShaderLanguage::ShaderNode::Uniform> *uniforms = nullptr;56};5758struct GeneratedCode {59Vector<String> defines;60struct Texture {61StringName name;62ShaderLanguage::DataType type = ShaderLanguage::DataType::TYPE_VOID;63ShaderLanguage::ShaderNode::Uniform::Hint hint = ShaderLanguage::ShaderNode::Uniform::Hint::HINT_NONE;64bool use_color = false;65ShaderLanguage::TextureFilter filter = ShaderLanguage::TextureFilter::FILTER_DEFAULT;66ShaderLanguage::TextureRepeat repeat = ShaderLanguage::TextureRepeat::REPEAT_DEFAULT;67bool global = false;68int array_size = 0;69};7071Vector<Texture> texture_uniforms;7273Vector<uint32_t> uniform_offsets;74uint32_t uniform_total_size = 0;75String uniforms;76String stage_globals[STAGE_MAX];7778HashMap<String, String> code;7980bool uses_global_textures = false;81bool uses_fragment_time = false;82bool uses_vertex_time = false;83bool uses_screen_texture_mipmaps = false;84bool uses_screen_texture = false;85bool uses_depth_texture = false;86bool uses_normal_roughness_texture = false;87};8889struct DefaultIdentifierActions {90HashMap<StringName, String> renames;91HashMap<StringName, String> render_mode_defines;92HashMap<StringName, String> usage_defines;93HashMap<StringName, String> custom_samplers;94ShaderLanguage::TextureFilter default_filter = ShaderLanguage::TextureFilter::FILTER_NEAREST;95ShaderLanguage::TextureRepeat default_repeat = ShaderLanguage::TextureRepeat::REPEAT_DISABLE;96int base_texture_binding_index = 0;97int texture_layout_set = 0;98String base_uniform_string;99String global_buffer_array_variable;100String instance_uniform_index_variable;101uint32_t base_varying_index = 0;102bool apply_luminance_multiplier = false;103bool check_multiview_samplers = false;104};105106private:107ShaderLanguage parser;108109String _get_sampler_name(ShaderLanguage::TextureFilter p_filter, ShaderLanguage::TextureRepeat p_repeat);110111void _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);112String _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);113114const ShaderLanguage::ShaderNode *shader = nullptr;115const ShaderLanguage::FunctionNode *function = nullptr;116StringName current_func_name;117StringName time_name;118HashSet<StringName> texture_functions;119120HashSet<StringName> used_name_defines;121HashSet<StringName> used_flag_pointers;122HashSet<StringName> used_rmode_defines;123HashSet<StringName> internal_functions;124HashSet<StringName> fragment_varyings;125126DefaultIdentifierActions actions;127128static ShaderLanguage::DataType _get_global_shader_uniform_type(const StringName &p_name);129130public:131Error compile(RS::ShaderMode p_mode, const String &p_code, IdentifierActions *p_actions, const String &p_path, GeneratedCode &r_gen_code);132133void initialize(DefaultIdentifierActions p_actions);134ShaderCompiler();135};136137138