You've already forked godot
mirror of
https://github.com/godotengine/godot.git
synced 2025-12-02 16:48:55 +00:00
Add optimizing AT_LIGHT_PASS builtin to canvas shaders
This one allows for complex shaders paired with a simple lighting shader to skip code that would otherwise be pointlessly (and wastefully) run during the light pass. You can use `if (AT_LIGHT_PASS) , negated or not, and that will be converted to a preprocessed #if when the shader is compiled. Depending on your game (number of items and lights), this can be a *significant* performance gain, or at least avoids relying on the driver's optimizing abilities.
This commit is contained in:
@@ -502,16 +502,31 @@ String ShaderCompilerGLES2::dump_node_code(SL::Node *p_node, int p_level, bool p
|
||||
SL::ControlFlowNode *cfnode = (SL::ControlFlowNode *)p_node;
|
||||
if (cfnode->flow_op == SL::FLOW_OP_IF) {
|
||||
|
||||
code += "if (" + dump_node_code(cfnode->statements[0], p_level) + ") {" ENDL;
|
||||
code += dump_node_code(cfnode->statements[1], p_level + 1);
|
||||
if (cfnode->statements.size() == 3) {
|
||||
// Help lazy drivers
|
||||
if (!_is_condition_preprocessable(cfnode->statements[0])) {
|
||||
|
||||
code += "} else {" ENDL;
|
||||
code += dump_node_code(cfnode->statements[2], p_level + 1);
|
||||
code += "if (" + dump_node_code(cfnode->statements[0], p_level) + ") {" ENDL;
|
||||
code += dump_node_code(cfnode->statements[1], p_level + 1);
|
||||
if (cfnode->statements.size() == 3) {
|
||||
|
||||
code += "} else {" ENDL;
|
||||
code += dump_node_code(cfnode->statements[2], p_level + 1);
|
||||
}
|
||||
|
||||
code += "}" ENDL;
|
||||
} else {
|
||||
|
||||
code += "#if (" + dump_node_code(cfnode->statements[0], p_level) + ")" ENDL;
|
||||
code += dump_node_code(cfnode->statements[1], p_level);
|
||||
if (cfnode->statements.size() == 3) {
|
||||
|
||||
code += "#else" ENDL;
|
||||
code += dump_node_code(cfnode->statements[2], p_level);
|
||||
}
|
||||
|
||||
code += "#endif" ENDL;
|
||||
}
|
||||
|
||||
code += "}" ENDL;
|
||||
|
||||
} else if (cfnode->flow_op == SL::FLOW_OP_RETURN) {
|
||||
|
||||
if (cfnode->statements.size()) {
|
||||
@@ -649,6 +664,33 @@ String ShaderCompilerGLES2::replace_string(const StringName &p_string) {
|
||||
return "_" + p_string.operator String();
|
||||
}
|
||||
|
||||
bool ShaderCompilerGLES2::_is_condition_preprocessable(ShaderLanguage::Node *p_condition) const {
|
||||
|
||||
// Check if this is a "(!)variable" expression
|
||||
|
||||
SL::Node *maybe_variable;
|
||||
if (p_condition->type == SL::Node::TYPE_OPERATOR) {
|
||||
SL::OperatorNode *op = (SL::OperatorNode *)p_condition;
|
||||
if (op->op != SL::OP_NOT)
|
||||
return false;
|
||||
maybe_variable = op->arguments[0];
|
||||
} else {
|
||||
maybe_variable = p_condition;
|
||||
}
|
||||
|
||||
if (maybe_variable->type != SL::Node::TYPE_VARIABLE)
|
||||
return false;
|
||||
|
||||
SL::VariableNode *variable = (SL::VariableNode *)maybe_variable;
|
||||
// Hardcoding since it's currently the only;
|
||||
// if more were added, they would be better characterized
|
||||
// in the various ShaderLanguage::BuiltinsDef
|
||||
if (variable->name != String("AT_LIGHT_PASS"))
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
Error ShaderCompilerGLES2::compile(const String &p_code, ShaderLanguage::ShaderType p_type, String &r_code_line, String &r_globals_line, Flags &r_flags, Map<StringName, ShaderLanguage::Uniform> *r_uniforms) {
|
||||
|
||||
uses_texscreen = false;
|
||||
@@ -873,6 +915,7 @@ ShaderCompilerGLES2::ShaderCompilerGLES2() {
|
||||
mode_replace_table[ShaderLanguage::SHADER_CANVAS_ITEM_VERTEX]["PROJECTION_MATRIX"] = "projection_matrix";
|
||||
mode_replace_table[ShaderLanguage::SHADER_CANVAS_ITEM_VERTEX]["EXTRA_MATRIX"] = "extra_matrix";
|
||||
mode_replace_table[ShaderLanguage::SHADER_CANVAS_ITEM_VERTEX]["TIME"] = "time";
|
||||
mode_replace_table[ShaderLanguage::SHADER_CANVAS_ITEM_VERTEX]["AT_LIGHT_PASS"] = "at_light_pass";
|
||||
|
||||
mode_replace_table[ShaderLanguage::SHADER_CANVAS_ITEM_FRAGMENT]["POSITION"] = "(gl_FragCoord.xy)";
|
||||
mode_replace_table[ShaderLanguage::SHADER_CANVAS_ITEM_FRAGMENT]["NORMAL"] = "normal";
|
||||
@@ -888,6 +931,7 @@ ShaderCompilerGLES2::ShaderCompilerGLES2() {
|
||||
mode_replace_table[ShaderLanguage::SHADER_CANVAS_ITEM_FRAGMENT]["SCREEN_UV"] = "screen_uv";
|
||||
mode_replace_table[ShaderLanguage::SHADER_CANVAS_ITEM_FRAGMENT]["POINT_COORD"] = "gl_PointCoord";
|
||||
mode_replace_table[ShaderLanguage::SHADER_CANVAS_ITEM_FRAGMENT]["TIME"] = "time";
|
||||
mode_replace_table[ShaderLanguage::SHADER_CANVAS_ITEM_FRAGMENT]["AT_LIGHT_PASS"] = "at_light_pass";
|
||||
|
||||
mode_replace_table[ShaderLanguage::SHADER_CANVAS_ITEM_LIGHT]["POSITION"] = "(gl_FragCoord.xy)";
|
||||
mode_replace_table[ShaderLanguage::SHADER_CANVAS_ITEM_LIGHT]["NORMAL"] = "normal";
|
||||
|
||||
Reference in New Issue
Block a user