mirror of
https://github.com/MaSzyna-EU07/maszyna.git
synced 2026-11-19 00:35:59 +01:00
Review of the alpha to coverage change for alpha tested surfaces: - An alpha which is flat over the pixel (fwidth 0) and equal to the opacity threshold got a coverage of 0.5, so e.g. opacity 1 on an opaque or DXT1 texture turned half see-through with multisampling. Without an alpha edge in the pixel the plain test decides now: step(opacity, alpha). - The shaders took the coverage path whenever they were built with multisampling, also where alpha to coverage is off (the reflections pass) or does nothing (the single-sampled model previews), and grew the cut-outs there by about half a pixel. model_ubo gets an alpha_to_coverage flag, set together with the GL state, and the shaders keep the plain alpha test without it. The previews no longer turn alpha to coverage on. The flag takes 4 of the 12 padding bytes after shadow_tone, so the std140 layout and the size of the block stay the same. - The shader validator never set iMultisampling, so it never compiled the alpha to coverage branches. Its "features on" configuration uses the default of 2 now, on desktop GL and GLES.
158 lines
4.2 KiB
C++
158 lines
4.2 KiB
C++
#pragma once
|
|
|
|
#include "object.h"
|
|
#include "bindable.h"
|
|
#include "buffer.h"
|
|
#include <glm/glm.hpp>
|
|
|
|
#define UBS_PAD(x) uint8_t PAD[x]
|
|
|
|
// 12-byte vec3 for structs that must match an exact binary layout (UBOs, vertex data).
|
|
// Stays packed regardless of GLM_FORCE_DEFAULT_ALIGNED_GENTYPES.
|
|
// Guard prevents redefinition when stdafx.h also defines it.
|
|
#ifndef PACKED_VEC3_DEFINED
|
|
#define PACKED_VEC3_DEFINED
|
|
using packed_vec3 = glm::vec<3, float, glm::packed_highp>;
|
|
#endif
|
|
|
|
namespace gl
|
|
{
|
|
class ubo : public buffer
|
|
{
|
|
int index;
|
|
|
|
public:
|
|
ubo(size_t size, int index, GLenum hint = GL_DYNAMIC_DRAW);
|
|
|
|
void bind_uniform();
|
|
|
|
void update(const uint8_t *data, int offset, GLsizeiptr size);
|
|
template <typename T> void update(const T &data, size_t offset = 0)
|
|
{
|
|
update(reinterpret_cast<const uint8_t*>(&data), static_cast<int>(offset), sizeof(data));
|
|
}
|
|
};
|
|
|
|
// layout std140
|
|
// structs must match with GLSL
|
|
// ordered to minimize padding
|
|
|
|
#pragma pack(push, 1)
|
|
|
|
const size_t MAX_TEXTURES = 8;
|
|
const size_t ENVMAP_SIZE = 1024;
|
|
const size_t MAX_CASCADES = 3;
|
|
const size_t HELPER_TEXTURES = 4;
|
|
const size_t SHADOW_TEX = MAX_TEXTURES + 0;
|
|
const size_t ENV_TEX = MAX_TEXTURES + 1;
|
|
const size_t HEADLIGHT_TEX = MAX_TEXTURES + 2;
|
|
|
|
struct scene_ubs
|
|
{
|
|
glm::mat4 projection;
|
|
glm::mat4 inv_view;
|
|
glm::mat4 lightview[MAX_CASCADES];
|
|
packed_vec3 cascade_end;
|
|
float time;
|
|
glm::vec4 rain_params;
|
|
glm::vec4 wiper_pos;
|
|
glm::vec4 wiper_timer_out;
|
|
glm::vec4 wiper_timer_return;
|
|
};
|
|
|
|
static_assert(sizeof(scene_ubs) == 400, "bad size of ubs");
|
|
|
|
const size_t MAX_PARAMS = 4;
|
|
|
|
struct model_ubs
|
|
{
|
|
glm::mat4 modelview;
|
|
glm::mat3x4 modelviewnormal;
|
|
glm::vec4 param[MAX_PARAMS];
|
|
|
|
glm::mat4 future;
|
|
float opacity;
|
|
float emission;
|
|
float fog_density;
|
|
float alpha_mult;
|
|
float shadow_tone;
|
|
// 1.0 while GL_SAMPLE_ALPHA_TO_COVERAGE is on, so the alpha tested shaders write the
|
|
// coverage of their test edge; 0.0 keeps their plain alpha test. set together with
|
|
// the GL state by opengl33_renderer::setup_alpha_to_coverage()
|
|
float alpha_to_coverage;
|
|
UBS_PAD(8);
|
|
|
|
void set_modelview(const glm::mat4 &mv)
|
|
{
|
|
modelview = mv;
|
|
// normal matrix = transpose(inverse(modelview)). The modelview is
|
|
// always affine, so its 3x3 normal matrix depends only on the
|
|
// upper-left 3x3 block; inverting that mat3 directly is markedly
|
|
// cheaper than a full mat4 inverse and yields an identical result
|
|
// (for affine M, mat3(inverse(M)) == inverse(mat3(M))).
|
|
modelviewnormal = glm::mat3x4(glm::transpose(glm::inverse(glm::mat3(mv))));
|
|
}
|
|
};
|
|
|
|
static_assert(sizeof(model_ubs) == 208 + 16 * MAX_PARAMS, "bad size of ubs");
|
|
|
|
// maximum number of instances per single GPU-instanced draw call.
|
|
// 256 mat4 = 16 KiB, the guaranteed minimum UBO size. Bigger batches must split.
|
|
const size_t MAX_INSTANCES_PER_BATCH = 256;
|
|
|
|
struct instance_ubs
|
|
{
|
|
glm::mat4 instance_modelview[MAX_INSTANCES_PER_BATCH];
|
|
};
|
|
|
|
static_assert(sizeof(instance_ubs) == 64 * MAX_INSTANCES_PER_BATCH, "bad size of instance_ubs");
|
|
|
|
struct light_element_ubs
|
|
{
|
|
enum type_e
|
|
{
|
|
SPOT = 0,
|
|
POINT,
|
|
DIR,
|
|
HEADLIGHTS
|
|
};
|
|
|
|
packed_vec3 pos;
|
|
type_e type;
|
|
|
|
packed_vec3 dir;
|
|
float in_cutoff;
|
|
|
|
packed_vec3 color;
|
|
float out_cutoff;
|
|
|
|
float linear;
|
|
float quadratic;
|
|
|
|
float intensity;
|
|
float ambient;
|
|
|
|
glm::mat4 headlight_projection;
|
|
glm::vec4 headlight_weights;
|
|
};
|
|
|
|
static_assert(sizeof(light_element_ubs) == 144, "bad size of ubs");
|
|
|
|
const size_t MAX_LIGHTS = 8;
|
|
|
|
struct light_ubs
|
|
{
|
|
packed_vec3 ambient;
|
|
UBS_PAD(4);
|
|
|
|
packed_vec3 fog_color;
|
|
uint32_t lights_count;
|
|
|
|
light_element_ubs lights[MAX_LIGHTS];
|
|
};
|
|
|
|
static_assert(sizeof(light_ubs) == 32 + sizeof(light_element_ubs) * MAX_LIGHTS, "bad size of ubs");
|
|
|
|
#pragma pack(pop)
|
|
}
|