17
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-10-28 18:07:29 +01:00

Draw the instances from the GPU with multi draws, the model block of each draw read by its number

This commit is contained in:
2026-10-18 00:47:05 +02:00
parent 7221c153dd
commit 494304903f
18 changed files with 609 additions and 64 deletions

View File

@@ -22,6 +22,7 @@ void gl::glsl_common_setup()
"#define INSTANCE_COUNTER_SSBO_BINDING " + std::to_string(INSTANCE_COUNTER_SSBO_BINDING) + "\n" +
"#define INSTANCE_OUTPUT_SSBO_BINDING " + std::to_string(INSTANCE_OUTPUT_SSBO_BINDING) + "\n" +
"#define INSTANCE_COMMAND_SSBO_BINDING " + std::to_string(INSTANCE_COMMAND_SSBO_BINDING) + "\n" +
"#define MODEL_DRAW_SSBO_BINDING " + std::to_string(MODEL_DRAW_SSBO_BINDING) + "\n" +
"#define INSTANCE_CULL_GROUP_SIZE " + std::to_string(INSTANCE_CULL_GROUP_SIZE) + "\n" +
"#define INSTANCE_OFFSETS_GROUP_SIZE " + std::to_string(INSTANCE_OFFSETS_GROUP_SIZE) + "\n" +
"#define INSTANCE_COMMAND_GROUP_SIZE " + std::to_string(INSTANCE_COMMAND_GROUP_SIZE) + "\n" +
@@ -187,6 +188,49 @@ void gl::glsl_common_setup()
};
#endif
#ifdef MODEL_DRAWS
// programs of the multi draws of the instances drawn from the gpu culling: the model block of every draw comes from a
// storage buffer, picked by the number of the draw in the multi draw. the fragment stage gets the number from the vertex
// stage (which writes f_model_draw), and the names of the uniform block below stand for the fields of the draw
struct model_draw_s
{
mat4 modelview;
mat3 modelviewnormal;
vec4 param[MAX_PARAMS];
mat4 future;
float opacity;
float emission;
float fog_density;
float alpha_mult;
float shadow_tone;
};
layout(std430, binding = MODEL_DRAW_SSBO_BINDING) readonly buffer model_draw_ssbo
{
model_draw_s model_draws[];
};
#if defined(STAGE_VERTEX)
flat out uint f_model_draw;
#define MODEL_DRAW gl_DrawID
#elif defined(STAGE_FRAGMENT)
flat in uint f_model_draw;
#define MODEL_DRAW f_model_draw
#else
#define MODEL_DRAW 0
#endif
#define modelview model_draws[MODEL_DRAW].modelview
#define modelviewnormal model_draws[MODEL_DRAW].modelviewnormal
#define param model_draws[MODEL_DRAW].param
#define future model_draws[MODEL_DRAW].future
#define opacity model_draws[MODEL_DRAW].opacity
#define emission model_draws[MODEL_DRAW].emission
#define fog_density model_draws[MODEL_DRAW].fog_density
#define alpha_mult model_draws[MODEL_DRAW].alpha_mult
#define shadow_tone model_draws[MODEL_DRAW].shadow_tone
#else
layout (std140) uniform model_ubo
{
mat4 modelview;
@@ -200,6 +244,7 @@ void gl::glsl_common_setup()
float alpha_mult;
float shadow_tone;
};
#endif
layout (std140) uniform scene_ubo
{

View File

@@ -74,7 +74,7 @@ std::unordered_map<std::string, gl::shader::defaultparam_e> gl::shader::defaultp
{ "glossiness", defaultparam_e::glossiness }
};
std::pair<GLuint, std::string> gl::shader::process_source(const std::string &filename, const std::string &basedir)
std::pair<GLuint, std::string> gl::shader::process_source(const std::string &filename, const std::string &basedir, const std::string &defines)
{
std::string str;
@@ -101,6 +101,7 @@ std::pair<GLuint, std::string> gl::shader::process_source(const std::string &fil
str += "vec4 FBOUT(vec4 x) { return " + (Global.gfx_shadergamma ? std::string("vec4(pow(x.rgb, vec3(1.0 / 2.2)), x.a)") : std::string("x")) + "; }\n";
// whether the colour written to the window is encoded by the shader (FBOUT) or left to the sRGB framebuffer (dither.glsl)
str += std::string("#define OUTPUT_GAMMA ") + (Global.gfx_shadergamma ? "1" : "0") + "\n";
str += defines;
str += read_file(basedir + filename);
@@ -231,10 +232,10 @@ void gl::shader::log_error(const std::string &str) const
ErrorLog("bad shader: " + name + ": " + str, logtype::shader);
}
gl::shader::shader(const std::string &filename) : name(filename)
gl::shader::shader(const std::string &filename, const std::string &defines) : name(filename)
{
auto [shadertype, shadersource] = process_source(filename, "shaders/");
auto [shadertype, shadersource] = process_source(filename, "shaders/", defines);
const GLchar *cstr = shadersource.c_str();

View File

@@ -21,7 +21,8 @@ namespace gl
#ifdef SHADERVALIDATOR_STANDALONE
shader() = default;
#endif
explicit shader(const std::string &filename);
// Defines: lines put before the source (after the version), e.g. "#define MODEL_DRAWS 1\n" for a variant of the shader
explicit shader(const std::string &filename, const std::string &defines = {});
~shader();
enum class components_e
@@ -67,7 +68,7 @@ namespace gl
#ifndef SHADERVALIDATOR_STANDALONE
private:
#endif
std::pair<GLuint, std::string> process_source(const std::string &filename, const std::string &basedir);
std::pair<GLuint, std::string> process_source(const std::string &filename, const std::string &basedir, const std::string &defines = {});
void expand_includes(std::string &str, const std::string &basedir);
void parse_texture_entries(std::string &str);

View File

@@ -472,6 +472,10 @@ namespace gl
const GLuint INSTANCE_COUNTER_SSBO_BINDING = 13; // header, then count[entries], cursor[entries] and first[entries]
const GLuint INSTANCE_OUTPUT_SSBO_BINDING = 14; // modelview matrices of the instances which passed, by entry
const GLuint INSTANCE_COMMAND_SSBO_BINDING = 15; // indirect draw commands of the entries (instance_cull_commands.comp)
// the model blocks of the draws of a multi draw of the instances drawn from the gpu culling, one gl::model_ubs per draw
// (std430, the same layout), read by gl_DrawID in programs built with MODEL_DRAWS. the point is the one of the jobs of the
// instance culling, which binds its buffers before every use; the draws bind theirs before every multi draw
const GLuint MODEL_DRAW_SSBO_BINDING = 10;
const GLuint INSTANCE_CULL_GROUP_SIZE = 64;
const GLuint INSTANCE_OFFSETS_GROUP_SIZE = 1024;
const GLuint INSTANCE_COMMAND_GROUP_SIZE = 64;