Skip to content

Commit

Permalink
Implement multi-pass shader directive.l
Browse files Browse the repository at this point in the history
  • Loading branch information
RadiantUwU committed Dec 5, 2024
1 parent d09d82d commit 257e84a
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 7 deletions.
4 changes: 2 additions & 2 deletions editor/plugins/text_shader_editor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,7 @@ void ShaderTextEditor::_code_complete_script(const String &p_code, List<ScriptLa
if (!complete_from_path.ends_with("/")) {
complete_from_path += "/";
}
preprocessor.preprocess(p_code, resource_path, code, nullptr, nullptr, nullptr, nullptr, &pp_options, &pp_defines, _complete_include_paths);
preprocessor.preprocess(p_code, resource_path, code, nullptr, nullptr, nullptr, nullptr, nullptr, &pp_options, &pp_defines, _complete_include_paths);
complete_from_path = String();
if (pp_options.size()) {
for (const ScriptLanguage::CodeCompletionOption &E : pp_options) {
Expand Down Expand Up @@ -465,7 +465,7 @@ void ShaderTextEditor::_validate_script() {
} else if (shader_inc.is_valid()) {
filename = shader_inc->get_path();
}
last_compile_result = preprocessor.preprocess(code, filename, code_pp, &error_pp, &err_positions, &regions);
last_compile_result = preprocessor.preprocess(code, filename, code_pp, nullptr, &error_pp, &err_positions, &regions);

for (int i = 0; i < get_text_editor()->get_line_count(); i++) {
get_text_editor()->set_line_background_color(i, Color(0, 0, 0, 0));
Expand Down
2 changes: 1 addition & 1 deletion editor/plugins/visual_shader_editor_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6241,7 +6241,7 @@ void VisualShaderEditor::_update_preview() {
String error_pp;
List<ShaderPreprocessor::FilePosition> err_positions;
ShaderPreprocessor preprocessor;
Error err = preprocessor.preprocess(code, path, preprocessed_code, &error_pp, &err_positions);
Error err = preprocessor.preprocess(code, path, preprocessed_code, nullptr, &error_pp, &err_positions);
if (err != OK) {
ERR_FAIL_COND(err_positions.is_empty());

Expand Down
2 changes: 1 addition & 1 deletion scene/resources/shader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ void Shader::set_code(const String &p_code) {
// 2) Server does not do interaction with Resource filetypes, this is a scene level feature.
HashSet<Ref<ShaderInclude>> new_include_dependencies;
ShaderPreprocessor preprocessor;
Error result = preprocessor.preprocess(p_code, path, preprocessed_code, nullptr, nullptr, nullptr, &new_include_dependencies);
Error result = preprocessor.preprocess(p_code, path, preprocessed_code, nullptr, nullptr, nullptr, nullptr, &new_include_dependencies);
if (result == OK) {
// This ensures previous include resources are not freed and then re-loaded during parse (which would make compiling slower)
include_dependencies = new_include_dependencies;
Expand Down
2 changes: 1 addition & 1 deletion scene/resources/shader_include.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ void ShaderInclude::set_code(const String &p_code) {
String pp_code;
HashSet<Ref<ShaderInclude>> new_dependencies;
ShaderPreprocessor preprocessor;
Error result = preprocessor.preprocess(p_code, path, pp_code, nullptr, nullptr, nullptr, &new_dependencies);
Error result = preprocessor.preprocess(p_code, path, pp_code, nullptr, nullptr, nullptr, nullptr, &new_dependencies);
if (result == OK) {
// This ensures previous include resources are not freed and then re-loaded during parse (which would make compiling slower)
dependencies = new_dependencies;
Expand Down
25 changes: 24 additions & 1 deletion servers/rendering/shader_preprocessor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -826,6 +826,20 @@ void ShaderPreprocessor::process_undef(Tokenizer *p_tokenizer) {
}
}

void ShaderPreprocessor::process_pass(Tokenizer *p_tokenizer) {
const int line = p_tokenizer->get_line();
const String label = p_tokenizer->get_identifier();
if (label.is_empty() || !p_tokenizer->consume_empty_line()) {
set_error(vformat(RTR("Invalid '%s' directive."), "pass"), line);
return;
}

if (state->defines.has(label)) {
set_error(vformat(RTR("Cannot use pass directive '%s' if it's been already defined previously."), label), line);
return;
}
}

void ShaderPreprocessor::add_region(int p_line, bool p_enabled, Region *p_parent_region) {
Region region;
region.file = state->current_filename;
Expand Down Expand Up @@ -1323,7 +1337,7 @@ Error ShaderPreprocessor::preprocess(State *p_state, const String &p_code, Strin
return OK;
}

Error ShaderPreprocessor::preprocess(const String &p_code, const String &p_filename, String &r_result, String *r_error_text, List<FilePosition> *r_error_position, List<Region> *r_regions, HashSet<Ref<ShaderInclude>> *r_includes, List<ScriptLanguage::CodeCompletionOption> *r_completion_options, List<ScriptLanguage::CodeCompletionOption> *r_completion_defines, IncludeCompletionFunction p_include_completion_func) {
Error ShaderPreprocessor::preprocess(const String &p_code, const String &p_filename, String &r_result, String *p_pass, String *r_error_text, List<FilePosition> *r_error_position, List<Region> *r_regions, HashSet<Ref<ShaderInclude>> *r_includes, List<ScriptLanguage::CodeCompletionOption> *r_completion_options, List<ScriptLanguage::CodeCompletionOption> *r_completion_defines, IncludeCompletionFunction p_include_completion_func, Vector<String> *r_passes) {
State pp_state;
if (!p_filename.is_empty()) {
pp_state.current_filename = p_filename;
Expand Down Expand Up @@ -1356,6 +1370,12 @@ Error ShaderPreprocessor::preprocess(const String &p_code, const String &p_filen
define->body = E.value;
pp_state.defines[E.key] = define;
}

if (p_pass) {
Define *define = memnew(Define);
define->is_builtin = true;
pp_state.defines[*p_pass] = define;
}
}

Error err = preprocess(&pp_state, p_code, r_result);
Expand All @@ -1367,6 +1387,9 @@ Error ShaderPreprocessor::preprocess(const String &p_code, const String &p_filen
*r_error_position = pp_state.include_positions;
}
}
if (r_passes) {
*r_passes = pp_state.passes;
}
if (r_regions) {
*r_regions = pp_state.regions[p_filename];
}
Expand Down
4 changes: 3 additions & 1 deletion servers/rendering/shader_preprocessor.h
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ class ShaderPreprocessor {
bool disabled = false;
CompletionType completion_type = COMPLETION_TYPE_NONE;
HashSet<Ref<ShaderInclude>> shader_includes;
Vector<String> passes;
};

private:
Expand Down Expand Up @@ -199,6 +200,7 @@ class ShaderPreprocessor {
void process_include(Tokenizer *p_tokenizer);
void process_pragma(Tokenizer *p_tokenizer);
void process_undef(Tokenizer *p_tokenizer);
void process_pass(Tokenizer *p_tokenizer);

void add_region(int p_line, bool p_enabled, Region *p_parent_region);
void start_branch_condition(Tokenizer *p_tokenizer, bool p_success, bool p_continue = false);
Expand All @@ -223,7 +225,7 @@ class ShaderPreprocessor {
public:
typedef void (*IncludeCompletionFunction)(List<ScriptLanguage::CodeCompletionOption> *);

Error preprocess(const String &p_code, const String &p_filename, String &r_result, String *r_error_text = nullptr, List<FilePosition> *r_error_position = nullptr, List<Region> *r_regions = nullptr, HashSet<Ref<ShaderInclude>> *r_includes = nullptr, List<ScriptLanguage::CodeCompletionOption> *r_completion_options = nullptr, List<ScriptLanguage::CodeCompletionOption> *r_completion_defines = nullptr, IncludeCompletionFunction p_include_completion_func = nullptr);
Error preprocess(const String &p_code, const String &p_filename, String &r_result, String *p_pass = nullptr, String *r_error_text = nullptr, List<FilePosition> *r_error_position = nullptr, List<Region> *r_regions = nullptr, HashSet<Ref<ShaderInclude>> *r_includes = nullptr, List<ScriptLanguage::CodeCompletionOption> *r_completion_options = nullptr, List<ScriptLanguage::CodeCompletionOption> *r_completion_defines = nullptr, IncludeCompletionFunction p_include_completion_func = nullptr, Vector<String> *r_passes = nullptr);

static void get_keyword_list(List<String> *r_keywords, bool p_include_shader_keywords, bool p_ignore_context_keywords = false);
static void get_pragma_list(List<String> *r_pragmas);
Expand Down

0 comments on commit 257e84a

Please sign in to comment.