Skip to content

Commit

Permalink
[WIP] Add tessellation support
Browse files Browse the repository at this point in the history
  • Loading branch information
painfulexistence committed Jul 1, 2024
1 parent 4ab8fb8 commit 8b6cb62
Show file tree
Hide file tree
Showing 12 changed files with 473 additions and 12 deletions.
4 changes: 2 additions & 2 deletions glsl_builders.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,13 +128,13 @@ def build_rd_header(
if header_data.compute_lines:
body_parts = [
"static const char _compute_code[] = {\n%s\n\t\t};" % generate_inline_code(header_data.compute_lines),
f'setup(nullptr, nullptr, _compute_code, "{out_file_class}");',
f'setup(nullptr, nullptr, nullptr, nullptr, _compute_code, "{out_file_class}");',
]
else:
body_parts = [
"static const char _vertex_code[] = {\n%s\n\t\t};" % generate_inline_code(header_data.vertex_lines),
"static const char _fragment_code[] = {\n%s\n\t\t};" % generate_inline_code(header_data.fragment_lines),
f'setup(_vertex_code, _fragment_code, nullptr, "{out_file_class}");',
f'setup(_vertex_code, _fragment_code, nullptr, nullptr, nullptr, "{out_file_class}");',
]

body_content = "\n\t\t".join(body_parts)
Expand Down
2 changes: 1 addition & 1 deletion servers/rendering/renderer_rd/environment/sky.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ void SkyRD::SkyShaderData::set_code(const String &p_code) {
print_line("\n**fragment_globals:\n" + gen_code.stage_globals[ShaderCompiler::STAGE_FRAGMENT]);
#endif

scene_singleton->sky.sky_shader.shader.version_set_code(version, gen_code.code, gen_code.uniforms, gen_code.stage_globals[ShaderCompiler::STAGE_VERTEX], gen_code.stage_globals[ShaderCompiler::STAGE_FRAGMENT], gen_code.defines);
scene_singleton->sky.sky_shader.shader.version_set_code(version, gen_code.code, gen_code.uniforms, gen_code.stage_globals[ShaderCompiler::STAGE_VERTEX], gen_code.stage_globals[ShaderCompiler::STAGE_FRAGMENT], gen_code.stage_globals[ShaderCompiler::STAGE_TESSELLATION_CONTROL], gen_code.stage_globals[ShaderCompiler::STAGE_TESSELLATION_EVALUATION], gen_code.defines);
ERR_FAIL_COND(!scene_singleton->sky.sky_shader.shader.version_is_valid(version));

ubo_size = gen_code.uniform_total_size;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ void SceneShaderForwardClustered::ShaderData::set_code(const String &p_code) {
print_line("\n**vertex_globals:\n" + gen_code.stage_globals[ShaderCompiler::STAGE_VERTEX]);
print_line("\n**fragment_globals:\n" + gen_code.stage_globals[ShaderCompiler::STAGE_FRAGMENT]);
#endif
shader_singleton->shader.version_set_code(version, gen_code.code, gen_code.uniforms, gen_code.stage_globals[ShaderCompiler::STAGE_VERTEX], gen_code.stage_globals[ShaderCompiler::STAGE_FRAGMENT], gen_code.defines);
shader_singleton->shader.version_set_code(version, gen_code.code, gen_code.uniforms, gen_code.stage_globals[ShaderCompiler::STAGE_VERTEX], gen_code.stage_globals[ShaderCompiler::STAGE_FRAGMENT], gen_code.stage_globals[ShaderCompiler::STAGE_TESSELLATION_CONTROL], gen_code.stage_globals[ShaderCompiler::STAGE_TESSELLATION_EVALUATION], gen_code.defines);
ERR_FAIL_COND(!shader_singleton->shader.version_is_valid(version));

ubo_size = gen_code.uniform_total_size;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ void SceneShaderForwardMobile::ShaderData::set_code(const String &p_code) {
print_line("\n**fragment_globals:\n" + gen_code.stage_globals[ShaderCompiler::STAGE_FRAGMENT]);
#endif

shader_singleton->shader.version_set_code(version, gen_code.code, gen_code.uniforms, gen_code.stage_globals[ShaderCompiler::STAGE_VERTEX], gen_code.stage_globals[ShaderCompiler::STAGE_FRAGMENT], gen_code.defines);
shader_singleton->shader.version_set_code(version, gen_code.code, gen_code.uniforms, gen_code.stage_globals[ShaderCompiler::STAGE_VERTEX], gen_code.stage_globals[ShaderCompiler::STAGE_FRAGMENT], gen_code.stage_globals[ShaderCompiler::STAGE_TESSELLATION_CONTROL], gen_code.stage_globals[ShaderCompiler::STAGE_TESSELLATION_EVALUATION], gen_code.defines);
ERR_FAIL_COND(!shader_singleton->shader.version_is_valid(version));

ubo_size = gen_code.uniform_total_size;
Expand Down
4 changes: 3 additions & 1 deletion servers/rendering/renderer_rd/renderer_canvas_render_rd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2184,6 +2184,8 @@ void RendererCanvasRenderRD::CanvasShaderData::set_code(const String &p_code) {
actions.entry_point_stages["vertex"] = ShaderCompiler::STAGE_VERTEX;
actions.entry_point_stages["fragment"] = ShaderCompiler::STAGE_FRAGMENT;
actions.entry_point_stages["light"] = ShaderCompiler::STAGE_FRAGMENT;
actions.entry_point_stages["tesselation_control"] = ShaderCompiler::STAGE_TESSELLATION_CONTROL;
actions.entry_point_stages["tesselation_evaluation"] = ShaderCompiler::STAGE_TESSELLATION_EVALUATION;

actions.render_mode_values["blend_add"] = Pair<int *, int>(&blend_mode, BLEND_MODE_ADD);
actions.render_mode_values["blend_mix"] = Pair<int *, int>(&blend_mode, BLEND_MODE_MIX);
Expand Down Expand Up @@ -2226,7 +2228,7 @@ void RendererCanvasRenderRD::CanvasShaderData::set_code(const String &p_code) {
print_line("\n**vertex_globals:\n" + gen_code.stage_globals[ShaderCompiler::STAGE_VERTEX]);
print_line("\n**fragment_globals:\n" + gen_code.stage_globals[ShaderCompiler::STAGE_FRAGMENT]);
#endif
canvas_singleton->shader.canvas_shader.version_set_code(version, gen_code.code, gen_code.uniforms, gen_code.stage_globals[ShaderCompiler::STAGE_VERTEX], gen_code.stage_globals[ShaderCompiler::STAGE_FRAGMENT], gen_code.defines);
canvas_singleton->shader.canvas_shader.version_set_code(version, gen_code.code, gen_code.uniforms, gen_code.stage_globals[ShaderCompiler::STAGE_VERTEX], gen_code.stage_globals[ShaderCompiler::STAGE_FRAGMENT], gen_code.stage_globals[ShaderCompiler::STAGE_TESSELLATION_CONTROL], gen_code.stage_globals[ShaderCompiler::STAGE_TESSELLATION_EVALUATION], gen_code.defines);
ERR_FAIL_COND(!canvas_singleton->shader.canvas_shader.version_is_valid(version));

ubo_size = gen_code.uniform_total_size;
Expand Down
70 changes: 68 additions & 2 deletions servers/rendering/renderer_rd/shader_rd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,12 @@ void ShaderRD::_add_stage(const char *p_code, StageType p_stage_type) {
case STAGE_TYPE_FRAGMENT:
chunk.type = StageTemplate::Chunk::TYPE_FRAGMENT_GLOBALS;
break;
case STAGE_TYPE_TESSCONTROL:
chunk.type = StageTemplate::Chunk::TYPE_TESSCONTROL_GLOBALS;
break;
case STAGE_TYPE_TESSEVAL:
chunk.type = StageTemplate::Chunk::TYPE_TESSEVAL_GLOBALS;
break;
case STAGE_TYPE_COMPUTE:
chunk.type = StageTemplate::Chunk::TYPE_COMPUTE_GLOBALS;
break;
Expand Down Expand Up @@ -101,7 +107,7 @@ void ShaderRD::_add_stage(const char *p_code, StageType p_stage_type) {
}
}

void ShaderRD::setup(const char *p_vertex_code, const char *p_fragment_code, const char *p_compute_code, const char *p_name) {
void ShaderRD::setup(const char *p_vertex_code, const char *p_fragment_code, const char *p_tesscontrol_code, const char *p_tesseval_code, const char *p_compute_code, const char *p_name) {
name = p_name;

if (p_compute_code) {
Expand All @@ -115,6 +121,12 @@ void ShaderRD::setup(const char *p_vertex_code, const char *p_fragment_code, con
if (p_fragment_code) {
_add_stage(p_fragment_code, STAGE_TYPE_FRAGMENT);
}
if (p_tesscontrol_code) {
_add_stage(p_tesscontrol_code, STAGE_TYPE_TESSCONTROL);
}
if (p_tesseval_code) {
_add_stage(p_tesseval_code, STAGE_TYPE_TESSEVAL);
}
}

StringBuilder tohash;
Expand All @@ -130,6 +142,10 @@ void ShaderRD::setup(const char *p_vertex_code, const char *p_fragment_code, con
tohash.append(p_vertex_code ? p_vertex_code : "");
tohash.append("[Fragment]");
tohash.append(p_fragment_code ? p_fragment_code : "");
tohash.append("[TessellationControl]");
tohash.append(p_tesscontrol_code ? p_tesscontrol_code : "");
tohash.append("[TessellationEvaluation]");
tohash.append(p_tesseval_code ? p_tesseval_code : "");
tohash.append("[Compute]");
tohash.append(p_compute_code ? p_compute_code : "");

Expand Down Expand Up @@ -205,6 +221,12 @@ void ShaderRD::_build_variant_code(StringBuilder &builder, uint32_t p_variant, c
case StageTemplate::Chunk::TYPE_FRAGMENT_GLOBALS: {
builder.append(p_version->fragment_globals.get_data()); // fragment globals
} break;
case StageTemplate::Chunk::TYPE_TESSCONTROL_GLOBALS: {
builder.append(p_version->tesscontrol_globals.get_data()); // tessellation control globals
} break;
case StageTemplate::Chunk::TYPE_TESSEVAL_GLOBALS: {
builder.append(p_version->tesseval_globals.get_data()); // tessellation evaluation globals
} break;
case StageTemplate::Chunk::TYPE_COMPUTE_GLOBALS: {
builder.append(p_version->compute_globals.get_data()); // compute globals
} break;
Expand Down Expand Up @@ -250,7 +272,45 @@ void ShaderRD::_compile_variant(uint32_t p_variant, const CompileData *p_data) {
stages.push_back(stage);
}
}

if (!is_compute && build_ok) {
//tessellation control stage
current_stage = RD::SHADER_STAGE_TESSELATION_CONTROL;
/*
StringBuilder builder;
_build_variant_code(builder, p_variant, p_version, stage_templates[STAGE_TYPE_TESSCONTROL]);
current_source = builder.as_string();
RD::ShaderStageSPIRVData stage;
stage.spir_v = RD::get_singleton()->shader_compile_spirv_from_source(RD::SHADER_STAGE_TESSELATION_CONTROL, current_source, RD::SHADER_LANGUAGE_GLSL, &error);
if (stage.spir_v.size() == 0) {
build_ok = false;
} else {
stage.shader_stage = RD::SHADER_STAGE_TESSELATION_CONTROL;
stages.push_back(stage);
}
*/
}

if (!is_compute && build_ok) {
//tessellation evaluation stage
current_stage = RD::SHADER_STAGE_TESSELATION_EVALUATION;
/*
StringBuilder builder;
_build_variant_code(builder, p_variant, p_version, stage_templates[STAGE_TYPE_TESSEVAL]);
current_source = builder.as_string();
RD::ShaderStageSPIRVData stage;
stage.spir_v = RD::get_singleton()->shader_compile_spirv_from_source(RD::SHADER_STAGE_TESSELATION_EVALUATION, current_source, RD::SHADER_LANGUAGE_GLSL, &error);
if (stage.spir_v.size() == 0) {
build_ok = false;
} else {
stage.shader_stage = RD::SHADER_STAGE_TESSELATION_EVALUATION;
stages.push_back(stage);
}
*/
}

if (!is_compute && build_ok) {
//fragment stage
current_stage = RD::SHADER_STAGE_FRAGMENT;
Expand Down Expand Up @@ -371,6 +431,10 @@ String ShaderRD::_version_get_sha1(Version *p_version) const {
hash_build.append(p_version->vertex_globals.get_data());
hash_build.append("[fragment_globals]");
hash_build.append(p_version->fragment_globals.get_data());
hash_build.append("[tesscontrol_globals]");
hash_build.append(p_version->tesscontrol_globals.get_data());
hash_build.append("[tesseval_globals]");
hash_build.append(p_version->tesseval_globals.get_data());
hash_build.append("[compute_globals]");
hash_build.append(p_version->compute_globals.get_data());

Expand Down Expand Up @@ -568,12 +632,14 @@ void ShaderRD::_compile_version(Version *p_version, int p_group) {
p_version->valid = true;
}

void ShaderRD::version_set_code(RID p_version, const HashMap<String, String> &p_code, const String &p_uniforms, const String &p_vertex_globals, const String &p_fragment_globals, const Vector<String> &p_custom_defines) {
void ShaderRD::version_set_code(RID p_version, const HashMap<String, String> &p_code, const String &p_uniforms, const String &p_vertex_globals, const String &p_fragment_globals, const String &p_tesscontrol_globals, const String &p_tesseval_globals, const Vector<String> &p_custom_defines) {
ERR_FAIL_COND(is_compute);

Version *version = version_owner.get_or_null(p_version);
ERR_FAIL_NULL(version);
version->vertex_globals = p_vertex_globals.utf8();
version->tesscontrol_globals = p_tesscontrol_globals.utf8();
version->tesseval_globals = p_tesseval_globals.utf8();
version->fragment_globals = p_fragment_globals.utf8();
version->uniforms = p_uniforms.utf8();
version->code_sections.clear();
Expand Down
10 changes: 8 additions & 2 deletions servers/rendering/renderer_rd/shader_rd.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ class ShaderRD {
CharString vertex_globals;
CharString compute_globals;
CharString fragment_globals;
CharString tesscontrol_globals;
CharString tesseval_globals;
HashMap<StringName, CharString> code_sections;
Vector<CharString> custom_defines;

Expand Down Expand Up @@ -102,6 +104,8 @@ class ShaderRD {
TYPE_VERTEX_GLOBALS,
TYPE_FRAGMENT_GLOBALS,
TYPE_COMPUTE_GLOBALS,
TYPE_TESSCONTROL_GLOBALS,
TYPE_TESSEVAL_GLOBALS,
TYPE_CODE,
TYPE_TEXT
};
Expand Down Expand Up @@ -132,6 +136,8 @@ class ShaderRD {
enum StageType {
STAGE_TYPE_VERTEX,
STAGE_TYPE_FRAGMENT,
STAGE_TYPE_TESSCONTROL,
STAGE_TYPE_TESSEVAL,
STAGE_TYPE_COMPUTE,
STAGE_TYPE_MAX,
};
Expand All @@ -150,12 +156,12 @@ class ShaderRD {

protected:
ShaderRD();
void setup(const char *p_vertex_code, const char *p_fragment_code, const char *p_compute_code, const char *p_name);
void setup(const char *p_vertex_code, const char *p_fragment_code, const char *p_tesscontrol_code, const char *p_tesseval_code, const char *p_compute_code, const char *p_name);

public:
RID version_create();

void version_set_code(RID p_version, const HashMap<String, String> &p_code, const String &p_uniforms, const String &p_vertex_globals, const String &p_fragment_globals, const Vector<String> &p_custom_defines);
void version_set_code(RID p_version, const HashMap<String, String> &p_code, const String &p_uniforms, const String &p_vertex_globals, const String &p_fragment_globals, const String &p_tesscontrol_globals, const String &p_tesseval_globals, const Vector<String> &p_custom_defines);
void version_set_compute_code(RID p_version, const HashMap<String, String> &p_code, const String &p_uniforms, const String &p_compute_globals, const Vector<String> &p_custom_defines);

_FORCE_INLINE_ RID version_get_shader(RID p_version, int p_variant) {
Expand Down
Loading

0 comments on commit 8b6cb62

Please sign in to comment.