18
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-11-18 22:16:02 +01:00
Files
maszyna/gl/shader.h
jerrrrycho aecc73e893 fix(render): antialias alpha tested edges with multisampling (eu07/maszyna#449)
The sides of the trackbed (and grass, fences, foliage) are cut out of their
textures by the alpha test, which keeps or drops whole pixels, so their edges
stayed jagged at any multisampling level; only geometry edges got smoothed.

With multisampling on, the alpha tested materials of the opaque colour pass
now draw with GL_SAMPLE_ALPHA_TO_COVERAGE. Their shaders do the alpha test
with alpha_test_fail() of glsl_common and write as alpha the coverage of the
test edge: the alpha sharpened around the opacity threshold to go from 0 to 1
over about a pixel, so the edge keeps the place it had and gets the samples
it covers. The renderer turns alpha to coverage on per material, only for
shaders which use the helper and only in the opaque pass with a positive
opacity, so translucent, water, terrain and the other shaders are unchanged.
Without multisampling the shaders work as before.
2026-11-08 01:34:35 +01:00

108 lines
2.7 KiB
C++

#pragma once
#include <string>
#include <vector>
#include <functional>
#include <unordered_map>
#include "object.h"
#include "bindable.h"
namespace gl
{
class shader_exception : public std::runtime_error
{
using runtime_error::runtime_error;
};
class shader : public object
{
public:
#ifdef SHADERVALIDATOR_STANDALONE
shader() = default;
#endif
explicit shader(const std::string &filename);
~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
};
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;
std::string name;
// the shader does its alpha test with alpha_test_fail() of glsl_common, so its alpha
// output can drive GL_SAMPLE_ALPHA_TO_COVERAGE in the opaque pass
bool alpha_to_coverage{ false };
#ifndef SHADERVALIDATOR_STANDALONE
private:
#endif
std::pair<GLuint, std::string> process_source(const std::string &filename, const std::string &basedir);
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;
};
class program : public object, public bindable<program>
{
public:
program();
explicit program(std::vector<std::reference_wrapper<const gl::shader>> const &);
~program();
using bindable::bind;
static void bind(GLuint i);
void attach(const shader &);
void link();
std::unordered_map<std::string, shader::texture_entry> texture_conf;
std::unordered_map<std::string, shader::param_entry> param_conf;
bool alpha_to_coverage{ false };
private:
void init();
};
}