17
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-11-02 01:09:13 +01:00

feat(vulkan): vulkan 1.4 core with the user interface and the launcher, shaders split into ogl46/vk14 with SPIR-V binaries loaded first

This commit is contained in:
2026-10-24 05:47:11 +02:00
parent 68fa5dc2da
commit bd589f272d
235 changed files with 9397 additions and 3975 deletions

View File

@@ -1,19 +1,24 @@
#include "stdafx.h"
#include "glsl_common.h"
#include "rendering/common/shadersource.h"
std::string gl::glsl_common;
void gl::glsl_common_setup()
{
// the values shaders/ogl46/common.glsl takes instead of its own (MASZYNA_CONSTANTS). the settings of the game aren't here,
// they are specialization constants of the binaries (glsl_settings_defines(), glsl_settings_values())
glsl_common =
"#define SHADOWMAP_ENABLED " + std::to_string((int)Global.gfx_shadowmap_enabled) + "\n" +
"#define ENVMAP_ENABLED " + std::to_string((int)Global.gfx_envmap_enabled) + "\n" +
"#define MOTIONBLUR_ENABLED " + std::to_string((int)Global.gfx_postfx_motionblur_enabled) + "\n" +
"#define POSTFX_ENABLED " + std::to_string((int)!Global.gfx_skippipeline) + "\n" +
"#define EXTRAEFFECTS_ENABLED " + std::to_string((int)Global.gfx_extraeffects) + "\n" +
"#define MASZYNA_CONSTANTS 1\n"
"#define MAX_LIGHTS " + std::to_string(MAX_LIGHTS) + "U\n" +
"#define MAX_CASCADES " + std::to_string(MAX_CASCADES) + "U\n" +
"#define MAX_PARAMS " + std::to_string(MAX_PARAMS) + "U\n" +
"#define SHADOW_TEX " + std::to_string(SHADOW_TEX) + "\n" +
"#define ENV_TEX " + std::to_string(ENV_TEX) + "\n" +
"#define HEADLIGHT_TEX " + std::to_string(HEADLIGHT_TEX) + "\n" +
"#define ENV_GGX_TEX " + std::to_string(ENV_GGX_TEX) + "\n" +
"#define SCENE_UBO_BINDING " + std::to_string(SCENE_UBO_BINDING) + "\n" +
"#define MODEL_UBO_BINDING " + std::to_string(MODEL_UBO_BINDING) + "\n" +
"#define INSTANCE_SSBO_BINDING " + std::to_string(INSTANCE_SSBO_BINDING) + "\n" +
"#define INSTANCE_RECORD_SSBO_BINDING " + std::to_string(INSTANCE_RECORD_SSBO_BINDING) + "\n" +
"#define INSTANCE_JOB_SSBO_BINDING " + std::to_string(INSTANCE_JOB_SSBO_BINDING) + "\n" +
@@ -86,223 +91,34 @@ void gl::glsl_common_setup()
"#define MAX_CLOUD_LAYERS " + std::to_string(MAX_CLOUD_LAYERS) + "\n" +
"#define FOG_HORIZON_SAMPLES " + std::to_string(FOG_HORIZON_SAMPLES) + "\n" +
"#define WEATHER_GROUP_SIZE " + std::to_string(WEATHER_GROUP_SIZE) + "\n" +
"#define FOG_VOLUME_BIAS " + std::to_string(FOG_VOLUME_BIAS) + "\n" +
R"STRING(
const uint LIGHT_SPOT = 0U;
const uint LIGHT_POINT = 1U;
const uint LIGHT_DIR = 2U;
const uint LIGHT_HEADLIGHTS = 3U;
struct light_s
{
vec3 pos;
uint type;
vec3 dir;
float in_cutoff;
vec3 color;
float out_cutoff;
float linear;
float quadratic;
float intensity;
float ambient;
mat4 headlight_projection;
vec4 headlight_weights;
};
// storage blocks are guaranteed only in fragment and compute shaders (GL 4.6 minimum for the other stages is 0)
#if defined(STAGE_FRAGMENT) || defined(STAGE_COMPUTE)
layout(std430, binding = LIGHT_SSBO_BINDING) readonly buffer light_ssbo
{
vec3 ambient;
// lights stored in lights[], what the cluster culling goes through
uint lights_total;
vec3 fog_color;
// lights the shading loop goes through when the lights are not clustered
uint lights_count;
// 1 if the shading takes the lights from the list of the cluster of a fragment, see cluster_ubo
uint lights_clustered;
// scale of the directional part of the sky ambient light, see sky_ssbo
float sky_ambient_factor;
// 1 if rough reflections come from the prefiltered environment map
uint env_ggx;
light_s lights[];
};
// directional part of the sky ambient light: real spherical harmonics (bands 1 and 2, band 0 is ambient) of the light
// a surface facing a world space direction gets, in units of ambient. see gl::sky_data
layout(std430, binding = SKY_SSBO_BINDING) readonly buffer sky_ssbo
{
vec4 sky_ambient_sh[9];
};
#endif
// light clusters, see gl/cluster_grid.h
struct cluster_aabb_s
{
vec4 lower;
vec4 upper;
};
struct cluster_range_s
{
uint offset;
uint count;
};
#if defined(STAGE_FRAGMENT) || defined(STAGE_COMPUTE)
// parameters of the grid for the pass being drawn
layout(std140, binding = CLUSTER_UBO_BINDING) uniform cluster_ubo
{
mat4 cluster_projection;
float cluster_znear;
float cluster_zfar;
float cluster_slice_scale;
float cluster_slice_bias;
vec2 cluster_viewport_size;
uint cluster_valid;
};
// the compute passes fill the cluster buffers, shading only reads them
#ifdef STAGE_COMPUTE
#define CLUSTER_SSBO_ACCESS
layout(std430, binding = CLUSTER_AABB_SSBO_BINDING) buffer cluster_aabb_ssbo
{
cluster_aabb_s cluster_aabbs[];
};
layout(std430, binding = CLUSTER_COUNTER_SSBO_BINDING) buffer cluster_counter_ssbo
{
uint cluster_index_count;
uint cluster_overflow_count;
};
#else
#define CLUSTER_SSBO_ACCESS readonly
#endif
layout(std430, binding = CLUSTER_RANGE_SSBO_BINDING) CLUSTER_SSBO_ACCESS buffer cluster_range_ssbo
{
cluster_range_s cluster_ranges[];
};
layout(std430, binding = CLUSTER_INDEX_SSBO_BINDING) CLUSTER_SSBO_ACCESS buffer cluster_index_ssbo
{
uint cluster_light_indices[];
};
#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;
mat3 modelviewnormal;
vec4 param[MAX_PARAMS];
mat4 future;
float opacity;
float emission;
float fog_density;
float alpha_mult;
float shadow_tone;
};
#endif
layout (std140) uniform scene_ubo
{
mat4 projection;
mat4 inv_view;
mat4 lightview[MAX_CASCADES];
vec3 cascade_end;
float time;
vec4 rain_params;
vec4 wiper_pos;
vec4 wiper_timer_out;
vec4 wiper_timer_return;
// shadow maps of the headlights, see gl::scene_ubs
mat4 headlight_shadow[MAX_HEADLIGHT_SHADOWS];
vec4 headlight_shadow_params[MAX_HEADLIGHT_SHADOWS];
// contact shadows, see gl::scene_ubs
vec4 contact_shadow_params;
vec4 contact_shadow_lamps;
// cascades of the sun shadows fitted to the depth of the view, see gl::scene_ubs
vec4 shadow_fit;
};
#ifdef STAGE_VERTEX
// Per-instance modelview matrices for GPU-instanced draws, element 0 is identity.
// Draws which aren't instanced have gl_BaseInstance == 0 and gl_InstanceID == 0,
// so effective_modelview() reduces to model_ubo.modelview. For instanced draws
// the C++ side uploads camera-space root transforms of the instances and draws
// them from base instance onwards, with model_ubo.modelview set to the submodel-local
// chain (computed from identity, NOT from the camera/instance), so the product
// gives the correct final transform per instance per submodel.
layout(std430, binding = INSTANCE_SSBO_BINDING) readonly buffer instance_ssbo
{
mat4 instance_modelview[];
};
mat4 effective_modelview() {
return instance_modelview[gl_BaseInstance + gl_InstanceID] * modelview;
}
mat3 effective_modelviewnormal() {
// instance_modelview can include per-instance scale (uniform or per-axis),
// so its upper-3x3 is NOT its own inverse-transpose in the non-uniform
// case. Compute the correct normal matrix via transpose(inverse(...)).
// For pure rotation+translation this still produces the rotation matrix
// (one extra mat3 inverse per vertex — modern GPUs handle it cheaply).
mat3 instance_mv3 = mat3(instance_modelview[gl_BaseInstance + gl_InstanceID]);
mat3 instance_normal = transpose(inverse(instance_mv3));
return instance_normal * modelviewnormal;
}
#endif
)STRING";
"#define FOG_VOLUME_BIAS " + std::to_string(FOG_VOLUME_BIAS) + "\n";
}
std::string gl::glsl_settings_defines()
{
auto const value = [](bool const Value) { return std::string(Value ? "true" : "false"); };
return
"#define SHADOWMAP_ENABLED_VALUE " + value(Global.gfx_shadowmap_enabled) + "\n"
"#define ENVMAP_ENABLED_VALUE " + value(Global.gfx_envmap_enabled) + "\n"
"#define MOTIONBLUR_ENABLED_VALUE " + value(Global.gfx_postfx_motionblur_enabled) + "\n"
"#define POSTFX_ENABLED_VALUE " + value(!Global.gfx_skippipeline) + "\n"
"#define EXTRAEFFECTS_ENABLED_VALUE " + value(Global.gfx_extraeffects) + "\n"
"#define OUTPUT_GAMMA_VALUE " + value(Global.gfx_shadergamma) + "\n";
}
std::array<GLuint, 6> gl::glsl_settings_values()
{
// in the order of gl::shader_setting
return {
static_cast<GLuint>(Global.gfx_shadowmap_enabled),
static_cast<GLuint>(Global.gfx_envmap_enabled),
static_cast<GLuint>(Global.gfx_postfx_motionblur_enabled),
static_cast<GLuint>(!Global.gfx_skippipeline),
static_cast<GLuint>(Global.gfx_extraeffects),
static_cast<GLuint>(Global.gfx_shadergamma) };
}
bool gl::glsl_common_matches(std::string const &Basedir, std::vector<std::string> &Differences)
{
return gfx::shader_constants_match(gfx::shader_api::opengl46, "common.glsl", glsl_common, Basedir, Differences);
}

View File

@@ -1,10 +1,22 @@
#pragma once
#include <array>
#include <string>
#include <vector>
#include "ubo.h"
#include "utilities/Globals.h"
namespace gl
{
// the constants of the game the shaders take (shaders/ogl46/common.glsl), as #define lines put before the sources
extern std::string glsl_common;
void glsl_common_setup();
// the settings of the game the shaders take (shaders/ogl46/settings.glsl): #define lines of their values for the sources,
// and the values of the specialization constants of the binaries, in the order of gl::shader_setting
std::string glsl_settings_defines();
std::array<GLuint, 6> glsl_settings_values();
// whether the constants written in common.glsl in Basedir (shaders/ogl46/ when empty) are those of the game: the SPIR-V
// binaries are compiled with the ones of the file. Differences: what isn't the same
bool glsl_common_matches(std::string const &Basedir, std::vector<std::string> &Differences);
}

View File

@@ -1,292 +1,232 @@
#include "stdafx.h"
#include <fstream>
#include <sstream>
#include "shader.h"
#include "glsl_common.h"
#include "utilities/Logs.h"
inline bool strcend(std::string_view value, std::string_view ending)
#include <cstring>
// the values of the components are those of opengl (material textures take them as the hint of their format)
static_assert(static_cast<GLint>(gfx::shader_components::R) == GL_RED);
static_assert(static_cast<GLint>(gfx::shader_components::RG) == GL_RG);
static_assert(static_cast<GLint>(gfx::shader_components::RGB) == GL_RGB);
static_assert(static_cast<GLint>(gfx::shader_components::RGBA) == GL_RGBA);
static_assert(static_cast<GLint>(gfx::shader_components::sRGB) == GL_SRGB);
static_assert(static_cast<GLint>(gfx::shader_components::sRGB_A) == GL_SRGB_ALPHA);
static_assert(gfx::MAX_SHADER_TEXTURES == gl::MAX_TEXTURES);
static_assert(gfx::MAX_SHADER_PARAMS == gl::MAX_PARAMS);
namespace
{
if (ending.size() > value.size())
GLenum shader_type(gfx::shader_stage const Stage)
{
switch (Stage)
{
case gfx::shader_stage::vertex:
return GL_VERTEX_SHADER;
case gfx::shader_stage::fragment:
return GL_FRAGMENT_SHADER;
case gfx::shader_stage::geometry:
return GL_GEOMETRY_SHADER;
case gfx::shader_stage::compute:
return GL_COMPUTE_SHADER;
}
return GL_VERTEX_SHADER;
}
gfx::shader_source preprocess(std::string const &Name, std::string const &Defines, bool const Allowmissing)
{
return gfx::preprocess_shader(gfx::shader_api::opengl46, Name, Defines, gl::glsl_common + gl::glsl_settings_defines(), Allowmissing);
}
// the files of the sources named by their numbers, for the messages of the compiler (#line <line> <number>)
std::string files_of(gfx::shader_source const &Source)
{
std::string result;
for (std::size_t i = 0; i < Source.files.size(); ++i)
result += (i > 0 ? ", " : "") + std::to_string(i) + ": " + Source.files[i];
return result;
}
std::string shader_log(GLuint const Shader)
{
GLint length{0};
glGetShaderiv(Shader, GL_INFO_LOG_LENGTH, &length);
std::string info(static_cast<std::size_t>(std::max(length, 1)), '\0');
glGetShaderInfoLog(Shader, static_cast<GLsizei>(info.size()), nullptr, info.data());
info.resize(std::strlen(info.c_str()));
return info;
}
std::string program_log(GLuint const Program)
{
GLint length{0};
glGetProgramiv(Program, GL_INFO_LOG_LENGTH, &length);
std::string info(static_cast<std::size_t>(std::max(length, 1)), '\0');
glGetProgramInfoLog(Program, static_cast<GLsizei>(info.size()), nullptr, info.data());
info.resize(std::strlen(info.c_str()));
return info;
}
} // namespace
bool gl::shader_binaries_enabled()
{
// decided once, at the first shader: the settings and the driver don't change while the renderer runs
static bool const enabled{[]() {
if (false == Global.gfx_shader_binaries)
{
WriteLog("shaders: compiled from the sources (gfx.shaders.binaries no)", logtype::shader);
return false;
}
if ((false == GLAD_GL_VERSION_4_6) || (glSpecializeShader == nullptr))
{
WriteLog("shaders: the driver can't load SPIR-V, compiled from the sources", logtype::shader);
return false;
}
std::vector<std::string> differences;
if (false == glsl_common_matches({}, differences))
{
std::string text;
for (auto const &difference : differences)
text += "\n " + difference;
ErrorLog("shaders: the constants in " + gfx::shader_source_dir(gfx::shader_api::opengl46) + "common.glsl aren't those of the game, so the SPIR-V binaries of " +
gfx::shader_binary_dir(gfx::shader_api::opengl46) + " can't be used; the shaders are compiled from the sources:" + text,
logtype::shader);
return false;
}
WriteLog("shaders: the SPIR-V binaries of " + gfx::shader_binary_dir(gfx::shader_api::opengl46) + " are loaded where there are up to date ones", logtype::shader);
return true;
}()};
return enabled;
}
gl::shader::shader(const std::string &filename, const std::string &defines) : name(filename), m_defines(defines)
{
auto const source{preprocess(filename, defines, true)};
texture_conf = source.texture_conf;
param_conf = source.param_conf;
m_type = shader_type(source.stage);
if (shader_binaries_enabled() && load_binary(source))
{
if (false == source.has_source)
ErrorLog("shader " + filename + ": the SPIR-V binary is used without its source, so the textures and the parameters of the materials (#pragma texture, #pragma param) aren't known", logtype::shader);
return;
}
if (false == source.has_source)
throw shader_exception("cannot read shader: " + gfx::shader_source_dir(gfx::shader_api::opengl46) + filename);
**this = compile_source(source);
}
bool gl::shader::load_binary(gfx::shader_source const &Source)
{
std::string reason;
auto const words{gfx::load_shader_binary(Source, reason)};
if (false == reason.empty())
WriteLog("shader " + name + (Source.variant.empty() ? "" : " (" + Source.variant + ")") + ": " + reason + ", compiled from the source", logtype::shader);
if (words.empty())
return false;
return std::equal(ending.rbegin(), ending.rend(), value.rbegin());
}
std::string gl::shader::read_file(const std::string &filename) const
{
std::stringstream stream;
std::ifstream f;
f.open(filename);
stream << f.rdbuf();
f.close();
std::string str = stream.str();
if (str.empty())
throw shader_exception("cannot read shader: " + filename);
return str;
}
void gl::shader::expand_includes(std::string &str, const std::string &basedir)
{
size_t start_pos = 0;
std::string magic = "#include";
while ((start_pos = str.find(magic, start_pos)) != std::string::npos)
auto const id{glCreateShader(m_type)};
glShaderBinary(1, &id, GL_SHADER_BINARY_FORMAT_SPIR_V, words.data(), static_cast<GLsizei>(words.size() * sizeof(std::uint32_t)));
// the settings the module has, the others aren't given
auto const values{glsl_settings_values()};
std::vector<GLuint> indices;
std::vector<GLuint> settings;
for (auto const id : gfx::spirv_specialization_ids(words))
{
size_t fp = str.find('<', start_pos);
size_t fe = str.find('>', start_pos);
if (fp == std::string::npos || fe == std::string::npos)
return;
std::string filename = str.substr(fp + 1, fe - fp - 1);
std::string content;
if (filename != "common")
content = read_file(basedir + filename);
else
content = glsl_common;
str.replace(start_pos, fe - start_pos + 1, content);
if (id < values.size())
{
indices.push_back(id);
settings.push_back(values[id]);
}
}
}
glSpecializeShader(id, "main", static_cast<GLuint>(indices.size()), indices.data(), settings.data());
std::unordered_map<std::string, gl::shader::components_e> gl::shader::components_mapping =
{
{ "R", components_e::R },
{ "RG", components_e::RG },
{ "RGB", components_e::RGB },
{ "RGBA", components_e::RGBA },
{ "sRGB", components_e::sRGB },
{ "sRGB_A", components_e::sRGB_A }
};
std::unordered_map<std::string, gl::shader::defaultparam_e> gl::shader::defaultparams_mapping =
{
{ "required", defaultparam_e::required },
{ "nan", defaultparam_e::nan },
{ "zero", defaultparam_e::zero },
{ "one", defaultparam_e::one },
{ "ambient", defaultparam_e::ambient },
{ "diffuse", defaultparam_e::diffuse },
{ "specular", defaultparam_e::specular },
{ "glossiness", defaultparam_e::glossiness }
};
std::pair<GLuint, std::string> gl::shader::process_source(const std::string &filename, const std::string &basedir, const std::string &defines)
{
std::string str;
GLuint type;
if (strcend(filename, ".vert"))
type = GL_VERTEX_SHADER;
else if (strcend(filename, ".frag"))
type = GL_FRAGMENT_SHADER;
else if (strcend(filename, ".geom"))
type = GL_GEOMETRY_SHADER;
else if (strcend(filename, ".comp"))
type = GL_COMPUTE_SHADER;
else
throw shader_exception("unknown shader " + filename);
str += "#version 460 core\n";
// expose the shader stage to glsl_common so vertex-only built-ins
// (gl_InstanceID, effective_modelview, etc.) can be guarded out of
// fragment/geometry compilations.
if (type == GL_VERTEX_SHADER) str += "#define STAGE_VERTEX 1\n";
else if (type == GL_FRAGMENT_SHADER) str += "#define STAGE_FRAGMENT 1\n";
else if (type == GL_GEOMETRY_SHADER) str += "#define STAGE_GEOMETRY 1\n";
else if (type == GL_COMPUTE_SHADER) str += "#define STAGE_COMPUTE 1\n";
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);
expand_includes(str, basedir);
parse_texture_entries(str);
parse_param_entries(str);
return std::make_pair(type, str);
}
void gl::shader::parse_texture_entries(std::string &str)
{
size_t start_pos = 0;
std::string magic = "#texture";
while ((start_pos = str.find(magic, start_pos)) != std::string::npos)
GLint status{GL_FALSE};
glGetShaderiv(id, GL_COMPILE_STATUS, &status);
if (status == GL_FALSE)
{
size_t fp = str.find('(', start_pos);
size_t fe = str.find(')', start_pos);
if (fp == std::string::npos || fe == std::string::npos)
return;
std::istringstream ss(str.substr(fp + 1, fe - fp - 1));
std::string token;
std::string uniformname;
texture_entry conf;
size_t arg = 0;
while (std::getline(ss, token, ','))
{
std::istringstream token_ss(token);
if (arg == 0)
token_ss >> uniformname;
else if (arg == 1)
token_ss >> conf.id;
else if (arg == 2)
{
std::string comp;
token_ss >> comp;
if (!components_mapping.contains(comp))
log_error("unknown components: " + comp);
else
conf.components = components_mapping[comp];
}
arg++;
}
if (arg == 3)
{
if (uniformname.empty())
log_error("empty name");
else if (conf.id >= gl::MAX_TEXTURES)
log_error("invalid texture binding: " + std::to_string(conf.id));
else
texture_conf.try_emplace(uniformname, conf);
}
else
log_error("invalid argument count to #texture");
str.erase(start_pos, fe - start_pos + 1);
WriteLog("shader " + name + ": the SPIR-V binary can't be loaded (" + shader_log(id) + "), compiled from the source", logtype::shader);
glDeleteShader(id);
return false;
}
**this = id;
m_binary = true;
return true;
}
void gl::shader::parse_param_entries(std::string &str)
GLuint gl::shader::compile_source(gfx::shader_source const &Source) const
{
size_t start_pos = 0;
const GLchar *cstr = Source.text.c_str();
std::string magic = "#param";
while ((start_pos = str.find(magic, start_pos)) != std::string::npos)
{
size_t fp = str.find('(', start_pos);
size_t fe = str.find(')', start_pos);
if (fp == std::string::npos || fe == std::string::npos)
return;
std::istringstream ss(str.substr(fp + 1, fe - fp - 1));
std::string token;
std::string uniformname;
param_entry conf;
size_t arg = 0;
while (std::getline(ss, token, ','))
{
std::istringstream token_ss(token);
if (arg == 0)
token_ss >> uniformname;
else if (arg == 1)
token_ss >> conf.location;
else if (arg == 2)
token_ss >> conf.offset;
else if (arg == 3)
token_ss >> conf.size;
else if (arg == 4)
{
std::string tok;
token_ss >> tok;
if (!defaultparams_mapping.contains(tok))
log_error("unknown param default: " + tok);
conf.defaultparam = defaultparams_mapping[tok];
}
arg++;
}
if (arg == 5)
{
if (uniformname.empty())
log_error("empty name");
else if (conf.location >= gl::MAX_PARAMS)
log_error("invalid param binding: " + std::to_string(conf.location));
else if (conf.offset > 3)
log_error("invalid offset: " + std::to_string(conf.offset));
else if (conf.offset + conf.size > 4)
log_error("invalid size: " + std::to_string(conf.size));
else
param_conf.try_emplace(uniformname, conf);
}
else
log_error("invalid argument count to #param");
str.erase(start_pos, fe - start_pos + 1);
}
}
void gl::shader::log_error(const std::string &str) const
{
ErrorLog("bad shader: " + name + ": " + str, logtype::shader);
}
gl::shader::shader(const std::string &filename, const std::string &defines) : name(filename)
{
auto [shadertype, shadersource] = process_source(filename, "shaders/", defines);
const GLchar *cstr = shadersource.c_str();
**this = glCreateShader(shadertype);
glShaderSource(*this, 1, &cstr, nullptr);
glCompileShader(*this);
auto const id{glCreateShader(m_type)};
glShaderSource(id, 1, &cstr, nullptr);
glCompileShader(id);
GLint status;
glGetShaderiv(*this, GL_COMPILE_STATUS, &status);
glGetShaderiv(id, GL_COMPILE_STATUS, &status);
if (!status)
{
std::array<GLchar, 512> info{};
glGetShaderInfoLog(*this, static_cast<GLsizei>(info.size()), nullptr, info.data());
log_error(std::string(info.data()));
throw shader_exception("failed to compile " + filename + ": " + std::string(info.data()));
auto const info{shader_log(id)};
glDeleteShader(id);
ErrorLog("bad shader: " + name + ": " + info + "(source strings: " + files_of(Source) + ")", logtype::shader);
throw shader_exception("failed to compile " + name + ": " + info);
}
return id;
}
GLuint gl::shader::source_object() const
{
if (false == m_binary)
return *this;
if (m_source_object == 0)
m_source_object = compile_source(preprocess(name, m_defines, false));
return m_source_object;
}
gl::shader::~shader()
{
#ifndef SHADERVALIDATOR_STANDALONE
if (m_source_object != 0)
glDeleteShader(m_source_object);
glDeleteShader(*this);
#endif
}
void gl::program::init()
{
bind();
// the samplers and the blocks have their bindings in the shaders; the sources which don't say them (older ones) are given
// them here. a program linked from SPIR-V doesn't know the names, nothing is found then
for (auto const &[texturename, textureentry] : texture_conf)
{
shader::texture_entry const &e = textureentry;
GLuint loc = glGetUniformLocation(*this, texturename.c_str());
glUniform1i(loc, static_cast<GLint>(e.id));
GLint loc = glGetUniformLocation(*this, texturename.c_str());
if (loc >= 0)
glUniform1i(loc, static_cast<GLint>(textureentry.id));
}
glUniform1i(glGetUniformLocation(*this, "shadowmap"), gl::SHADOW_TEX);
glUniform1i(glGetUniformLocation(*this, "envmap"), gl::ENV_TEX);
glUniform1i(glGetUniformLocation(*this, "headlightmap"), gl::HEADLIGHT_TEX);
auto const unit = [this](char const *Name, std::size_t const Unit) {
GLint loc = glGetUniformLocation(*this, Name);
if (loc >= 0)
glUniform1i(loc, static_cast<GLint>(Unit));
};
unit("shadowmap", gl::SHADOW_TEX);
unit("envmap", gl::ENV_TEX);
unit("headlightmap", gl::HEADLIGHT_TEX);
// the prefiltered environment map (envmapping.glsl); without a unit of its own the cube sampler would share unit 0 with
// the 2d diffuse texture of the materials, which makes their draws fail (GL_INVALID_OPERATION)
glUniform1i(glGetUniformLocation(*this, "envmap_ggx"), gl::ENV_GGX_TEX);
unit("envmap_ggx", gl::ENV_GGX_TEX);
GLuint index;
if ((index = glGetUniformBlockIndex(*this, "scene_ubo")) != GL_INVALID_INDEX)
glUniformBlockBinding(*this, index, 0);
glUniformBlockBinding(*this, index, gl::SCENE_UBO_BINDING);
if ((index = glGetUniformBlockIndex(*this, "model_ubo")) != GL_INVALID_INDEX)
glUniformBlockBinding(*this, index, 1);
glUniformBlockBinding(*this, index, gl::MODEL_UBO_BINDING);
}
gl::program::program()
@@ -307,21 +247,51 @@ void gl::program::attach(const gl::shader &s)
texture_conf.try_emplace(texturename, std::move(textureentry));
for (auto [paramname, paramentry] : s.param_conf)
param_conf.try_emplace(paramname, std::move(paramentry));
glAttachShader(*this, *s);
m_attached.push_back(&s);
}
void gl::program::link()
{
glLinkProgram(*this);
GLint status;
glGetProgramiv(*this, GL_LINK_STATUS, &status);
if (!status)
// a program is linked either from SPIR-V binaries or from sources only: when one of the shaders has no binary, the others are
// compiled from the source too
auto const allbinary{std::all_of(m_attached.begin(), m_attached.end(), [](shader const *Shader) { return Shader->is_binary(); })};
GLint status{GL_FALSE};
if (allbinary && (false == m_attached.empty()))
{
std::array<GLchar, 512> info{};
glGetProgramInfoLog(*this, static_cast<GLsizei>(info.size()), nullptr, info.data());
throw shader_exception("failed to link program: " + std::string(info.data()));
for (auto const *s : m_attached)
glAttachShader(*this, *s);
glLinkProgram(*this);
glGetProgramiv(*this, GL_LINK_STATUS, &status);
for (auto const *s : m_attached)
glDetachShader(*this, *s);
if (status == GL_TRUE)
{
m_binary = true;
}
else
{
std::string names;
for (auto const *s : m_attached)
names += (names.empty() ? "" : ", ") + s->name;
WriteLog("program of " + names + ": the SPIR-V binaries can't be linked (" + program_log(*this) + "), compiled from the sources", logtype::shader);
}
}
if (status == GL_FALSE)
{
std::vector<GLuint> objects;
for (auto const *s : m_attached)
objects.push_back(s->source_object());
for (auto const object : objects)
glAttachShader(*this, object);
glLinkProgram(*this);
glGetProgramiv(*this, GL_LINK_STATUS, &status);
for (auto const object : objects)
glDetachShader(*this, object);
m_binary = false;
}
m_attached.clear();
if (status == GL_FALSE)
throw shader_exception("failed to link program: " + program_log(*this));
init();
}

View File

@@ -1,5 +1,6 @@
#pragma once
#include <array>
#include <string>
#include <vector>
#include <functional>
@@ -7,82 +8,63 @@
#include "object.h"
#include "bindable.h"
#include "rendering/common/shadersource.h"
namespace gl
{
class shader_exception : public std::runtime_error
using shader_exception = gfx::shader_exception;
// the settings of the game the shaders take (shaders/ogl46/settings.glsl): the ids of the specialization constants of the
// SPIR-V binaries; compiled from the source, the shaders get them as constants (gl::glsl_settings_defines())
enum class shader_setting : GLuint
{
using runtime_error::runtime_error;
shadowmap = 0,
envmap = 1,
motionblur = 2,
postfx = 3,
extraeffects = 4,
output_gamma = 5,
count = 6
};
// a shader of shaders/ogl46/: the SPIR-V binary of shaders/ogl46bin/ when there's an up to date one (gfx.shaders.binaries),
// compiled from the source otherwise
class shader : public object
{
public:
#ifdef SHADERVALIDATOR_STANDALONE
shader() = default;
#endif
// 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
{
R = GL_RED,
RG = GL_RG,
RGB = GL_RGB,
RGBA = GL_RGBA,
sRGB = GL_SRGB,
sRGB_A = GL_SRGB_ALPHA
};
using components_e = gfx::shader_components;
using texture_entry = gfx::shader_texture_entry;
using defaultparam_e = gfx::shader_param_default;
using param_entry = gfx::shader_param_entry;
struct texture_entry
{
size_t id;
components_e components;
};
enum class defaultparam_e
{
required,
nan,
zero,
one,
ambient,
diffuse,
specular,
glossiness
};
struct param_entry
{
size_t location;
size_t offset;
size_t size;
defaultparam_e defaultparam;
};
std::unordered_map<std::string, texture_entry> texture_conf;
std::unordered_map<std::string, param_entry> param_conf;
gfx::shader_texture_conf texture_conf;
gfx::shader_param_conf param_conf;
std::string name;
#ifndef SHADERVALIDATOR_STANDALONE
// whether the shader was loaded from a SPIR-V binary
bool is_binary() const
{
return m_binary;
}
// the shader compiled from the source: the shader itself, or for a binary one the same shader compiled from the source
// (a program can't be linked from binaries and sources together). throws shader_exception
GLuint source_object() const;
private:
#endif
std::pair<GLuint, std::string> process_source(const std::string &filename, const std::string &basedir, const std::string &defines = {});
bool load_binary(gfx::shader_source const &Source);
GLuint compile_source(gfx::shader_source const &Source) const;
void expand_includes(std::string &str, const std::string &basedir);
void parse_texture_entries(std::string &str);
void parse_param_entries(std::string &str);
std::string read_file(const std::string &filename) const;
static std::unordered_map<std::string, components_e> components_mapping;
static std::unordered_map<std::string, defaultparam_e> defaultparams_mapping;
void log_error(const std::string &str) const;
GLenum m_type{0};
std::string m_defines;
bool m_binary{false};
mutable GLuint m_source_object{0};
};
class program : public object, public bindable<program>
class program : public gfx::shader_program, public object, public bindable<program>
{
public:
program();
@@ -92,16 +74,27 @@ namespace gl
using bindable::bind;
static void bind(GLuint i);
// the shaders are linked by link(), which has to come while they are there
void attach(const shader &);
void link();
// for a program linked from a compute shader only: binds it and runs the given number of work groups
void dispatch(GLuint groups_x, GLuint groups_y = 1, GLuint groups_z = 1);
std::unordered_map<std::string, shader::texture_entry> texture_conf;
std::unordered_map<std::string, shader::param_entry> param_conf;
// whether the program was linked from SPIR-V binaries
bool is_binary() const
{
return m_binary;
}
private:
void init();
std::vector<shader const *> m_attached;
bool m_binary{false};
};
// whether the shaders are loaded from the SPIR-V binaries where there are some: gfx.shaders.binaries, an opengl 4.6 driver,
// and the constants of the game the same as those the binaries were compiled with (gl::glsl_common_matches())
bool shader_binaries_enabled();
}

View File

@@ -247,7 +247,11 @@ namespace gl
const GLuint CLUSTER_BUILD_GROUP_SIZE = 64;
const GLuint CLUSTER_CULL_GROUP_SIZE = 64;
// binding points of the cluster buffers: layout(binding = ...) in glsl_common.
// binding points of the blocks of the scene and of the model being drawn (common.glsl)
const GLuint SCENE_UBO_BINDING = 0;
const GLuint MODEL_UBO_BINDING = 1;
// binding points of the cluster buffers: layout(binding = ...) in common.glsl.
// the uniform block numbers go together with scene (0), model (1) and instance (3) blocks
const GLuint CLUSTER_UBO_BINDING = 2;
const GLuint CLUSTER_AABB_SSBO_BINDING = 1;