Merge pull request #564 from MihailRis/small-optimizations

optimize shadow maps generation a bit
This commit is contained in:
MihailRis 2025-07-25 22:44:17 +03:00 committed by GitHub
commit 8489c36df7
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
31 changed files with 422 additions and 194 deletions

View File

@ -5,6 +5,7 @@
"main", "main",
"lines", "lines",
"entity", "entity",
"translucent",
"background", "background",
"skybox_gen", "skybox_gen",
"shadows" "shadows"

View File

@ -18,6 +18,7 @@ uniform mat4 u_view;
uniform mat4 u_inverseView; uniform mat4 u_inverseView;
uniform vec3 u_sunDir; uniform vec3 u_sunDir;
uniform vec3 u_cameraPos; uniform vec3 u_cameraPos;
uniform float u_gamma;
#include <__effect__> #include <__effect__>

View File

@ -29,6 +29,8 @@ vec4 effect() {
light = max(light, emission); light = max(light, emission);
light = pow(light, u_gamma);
vec3 fogColor = texture(u_skybox, dir).rgb; vec3 fogColor = texture(u_skybox, dir).rgb;
float fog = calc_fog(length(u_view * vec4((modelpos.xyz - u_cameraPos) * FOG_POS_SCALE, 0.0)) / 256.0); float fog = calc_fog(length(u_view * vec4((modelpos.xyz - u_cameraPos) * FOG_POS_SCALE, 0.0)) / 256.0);
return vec4(mix(texture(u_screen, v_uv).rgb * mix(1.0, light, 1.0), fogColor, fog), 1.0); return vec4(mix(texture(u_screen, v_uv).rgb * mix(1.0, light, 1.0), fogColor, fog), 1.0);

View File

@ -3,27 +3,18 @@ layout (location = 1) out vec4 f_position;
layout (location = 2) out vec4 f_normal; layout (location = 2) out vec4 f_normal;
layout (location = 3) out vec4 f_emission; layout (location = 3) out vec4 f_emission;
in float a_distance; #include <world_fragment_header>
in float a_fog;
in vec2 a_texCoord;
in vec3 a_dir;
in vec3 a_normal;
in vec3 a_position;
in vec3 a_realnormal;
in vec4 a_color; in vec4 a_color;
in vec4 a_modelpos;
in float a_emission;
uniform sampler2D u_texture0; uniform sampler2D u_texture0;
uniform samplerCube u_skybox;
uniform vec3 u_fogColor; uniform vec3 u_fogColor;
uniform float u_fogFactor; uniform float u_fogFactor;
uniform float u_fogCurve; uniform float u_fogCurve;
uniform bool u_alphaClip; uniform bool u_alphaClip;
uniform vec3 u_sunDir; uniform vec3 u_sunDir;
#include <shadows>
void main() { void main() {
vec4 texColor = texture(u_texture0, a_texCoord); vec4 texColor = texture(u_texture0, a_texCoord);
float alpha = a_color.a * texColor.a; float alpha = a_color.a * texColor.a;

View File

@ -6,44 +6,23 @@ layout (location = 2) in vec3 v_color;
layout (location = 3) in vec4 v_light; layout (location = 3) in vec4 v_light;
layout (location = 4) in vec4 v_normal; layout (location = 4) in vec4 v_normal;
out float a_distance; #include <world_vertex_header>
out float a_fog;
out vec2 a_texCoord;
out vec3 a_dir;
out vec3 a_normal;
out vec3 a_position;
out vec3 a_realnormal;
out vec4 a_color;
out vec4 a_modelpos;
out float a_emission;
uniform mat4 u_model;
uniform mat4 u_proj;
uniform mat4 u_view;
uniform vec3 u_cameraPos;
uniform float u_gamma;
uniform float u_opacity;
uniform float u_timer;
uniform samplerCube u_skybox;
uniform vec3 u_torchlightColor;
uniform float u_torchlightDistance;
#include <lighting> #include <lighting>
#include <fog> #include <fog>
#include <sky>
out vec4 a_color;
void main() { void main() {
a_modelpos = u_model * vec4(v_position, 1.0); a_modelpos = u_model * vec4(v_position, 1.0);
vec3 pos3d = a_modelpos.xyz - u_cameraPos; vec3 pos3d = a_modelpos.xyz - u_cameraPos;
a_modelpos.xyz = apply_planet_curvature(a_modelpos.xyz, pos3d);
a_realnormal = v_normal.xyz * 2.0 - 1.0; a_realnormal = v_normal.xyz * 2.0 - 1.0;
a_normal = calc_screen_normal(a_realnormal); a_normal = calc_screen_normal(a_realnormal);
vec3 light = v_light.rgb; a_color = vec4(calc_torch_light(
float torchlight = calc_torch_light(a_realnormal, a_modelpos.xyz); v_light.rgb, a_realnormal, a_modelpos.xyz, u_torchlightColor, u_gamma
light += torchlight * u_torchlightColor; ), 1.0);
a_color = vec4(pow(light, vec3(u_gamma)), 1.0f);
a_texCoord = v_texCoord; a_texCoord = v_texCoord;
a_dir = a_modelpos.xyz - u_cameraPos; a_dir = a_modelpos.xyz - u_cameraPos;

View File

@ -1,14 +1,7 @@
#ifndef COMMONS_GLSL_ #ifndef COMMONS_GLSL_
#define COMMONS_GLSL_ #define COMMONS_GLSL_
#include <constants>
vec3 pick_sky_color(samplerCube cubemap) { #include <constants>
vec3 skyLightColor = texture(cubemap, vec3(0.4f, 0.0f, 0.4f)).rgb;
skyLightColor *= SKY_LIGHT_TINT;
skyLightColor = min(vec3(1.0f), skyLightColor * SKY_LIGHT_MUL);
skyLightColor = max(MIN_SKY_LIGHT, skyLightColor);
return skyLightColor;
}
vec3 apply_planet_curvature(vec3 modelPos, vec3 pos3d) { vec3 apply_planet_curvature(vec3 modelPos, vec3 pos3d) {
modelPos.y -= pow(length(pos3d.xz) * CURVATURE_FACTOR, 3.0f); modelPos.y -= pow(length(pos3d.xz) * CURVATURE_FACTOR, 3.0f);

View File

@ -6,6 +6,11 @@ float calc_torch_light(vec3 normal, vec3 modelpos) {
* max(0.0, -dot(normal, normalize(modelpos - u_cameraPos))); * max(0.0, -dot(normal, normalize(modelpos - u_cameraPos)));
} }
vec3 calc_torch_light(vec3 light, vec3 normal, vec3 modelpos, vec3 torchLightColor, float gamma) {
float torchlight = calc_torch_light(normal, modelpos);
return pow(light + torchlight * torchLightColor, vec3(gamma));
}
vec3 calc_screen_normal(vec3 normal) { vec3 calc_screen_normal(vec3 normal) {
return transpose(inverse(mat3(u_view * u_model))) * normal; return transpose(inverse(mat3(u_view * u_model))) * normal;
} }

View File

@ -35,7 +35,7 @@ float calc_shadow(
} }
shadow /= 9.0; shadow /= 9.0;
} else { } else {
shadow = 0.5; shadow = 0.0;
} }
return shadow; return shadow;
} }

14
res/shaders/lib/sky.glsl Normal file
View File

@ -0,0 +1,14 @@
#ifndef COMMONS_SKY_
#define COMMONS_SKY_
#include <constants>
vec3 pick_sky_color(samplerCube cubemap) {
vec3 skyLightColor = texture(cubemap, vec3(0.4f, 0.0f, 0.4f)).rgb;
skyLightColor *= SKY_LIGHT_TINT;
skyLightColor = min(vec3(1.0f), skyLightColor * SKY_LIGHT_MUL);
skyLightColor = max(MIN_SKY_LIGHT, skyLightColor);
return skyLightColor;
}
#endif // COMMONS_SKY_

View File

@ -0,0 +1,17 @@
#ifndef GLSL_WORLD_FRAGMENT_HEADER_
#define GLSL_WORLD_FRAGMENT_HEADER_
in float a_distance;
in float a_fog;
in vec2 a_texCoord;
in vec3 a_dir;
in vec3 a_normal;
in vec3 a_position;
in vec3 a_realnormal;
in vec3 a_skyLight;
in vec4 a_modelpos;
in float a_emission;
#include <world_uniforms>
#endif // GLSL_WORLD_FRAGMENT_HEADER_

View File

@ -0,0 +1,15 @@
#ifndef GLSL_WORLD_UNIFORMS_
#define GLSL_WORLD_UNIFORMS_
uniform mat4 u_model;
uniform mat4 u_proj;
uniform mat4 u_view;
uniform vec3 u_cameraPos;
uniform float u_gamma;
uniform float u_opacity;
uniform float u_timer;
uniform samplerCube u_skybox;
uniform vec3 u_torchlightColor;
uniform float u_torchlightDistance;
#endif // GLSL_WORLD_UNIFORMS_

View File

@ -0,0 +1,17 @@
#ifndef GLSL_WORLD_VERTEX_HEADER_
#define GLSL_WORLD_VERTEX_HEADER_
out float a_distance;
out float a_fog;
out vec2 a_texCoord;
out vec3 a_dir;
out vec3 a_normal;
out vec3 a_position;
out vec3 a_realnormal;
out vec3 a_skyLight;
out vec4 a_modelpos;
out float a_emission;
#include <world_uniforms>
#endif // GLSL_WORLD_VERTEX_HEADER_

View File

@ -3,20 +3,11 @@ layout (location = 1) out vec4 f_position;
layout (location = 2) out vec4 f_normal; layout (location = 2) out vec4 f_normal;
layout (location = 3) out vec4 f_emission; layout (location = 3) out vec4 f_emission;
in float a_distance; #include <world_fragment_header>
in float a_fog;
in vec2 a_texCoord;
in vec3 a_dir;
in vec3 a_normal;
in vec3 a_position;
in vec3 a_realnormal;
in vec3 a_skyLight;
in vec4 a_modelpos;
in vec4 a_torchLight; in vec4 a_torchLight;
in float a_emission;
uniform sampler2D u_texture0; uniform sampler2D u_texture0;
uniform samplerCube u_skybox;
uniform vec3 u_sunDir; uniform vec3 u_sunDir;
// flags // flags
@ -24,8 +15,6 @@ uniform bool u_alphaClip;
uniform bool u_debugLights; uniform bool u_debugLights;
uniform bool u_debugNormals; uniform bool u_debugNormals;
#include <shadows>
void main() { void main() {
vec4 texColor = texture(u_texture0, a_texCoord); vec4 texColor = texture(u_texture0, a_texCoord);
float alpha = texColor.a; float alpha = texColor.a;
@ -39,9 +28,7 @@ void main() {
} }
if (u_debugLights) if (u_debugLights)
texColor.rgb = u_debugNormals ? (a_normal * 0.5 + 0.5) : vec3(1.0); texColor.rgb = u_debugNormals ? (a_normal * 0.5 + 0.5) : vec3(1.0);
else if (u_debugNormals) {
texColor.rgb *= a_normal * 0.5 + 0.5;
}
f_color = texColor; f_color = texColor;
f_color.rgb *= min(vec3(1.0), a_torchLight.rgb + a_skyLight); f_color.rgb *= min(vec3(1.0), a_torchLight.rgb + a_skyLight);

View File

@ -5,44 +5,23 @@ layout (location = 1) in vec2 v_texCoord;
layout (location = 2) in vec4 v_light; layout (location = 2) in vec4 v_light;
layout (location = 3) in vec4 v_normal; layout (location = 3) in vec4 v_normal;
out float a_distance; #include <world_vertex_header>
out float a_fog;
out vec2 a_texCoord;
out vec3 a_dir;
out vec3 a_normal;
out vec3 a_position;
out vec3 a_realnormal;
out vec4 a_torchLight;
out vec3 a_skyLight;
out vec4 a_modelpos;
out float a_emission;
uniform mat4 u_model;
uniform mat4 u_proj;
uniform mat4 u_view;
uniform vec3 u_cameraPos;
uniform float u_gamma;
uniform float u_timer;
uniform samplerCube u_skybox;
uniform vec3 u_torchlightColor;
uniform float u_torchlightDistance;
#include <lighting> #include <lighting>
#include <fog> #include <fog>
#include <sky>
out vec4 a_torchLight;
void main() { void main() {
a_modelpos = u_model * vec4(v_position, 1.0f); a_modelpos = u_model * vec4(v_position, 1.0f);
vec3 pos3d = a_modelpos.xyz - u_cameraPos; vec3 pos3d = a_modelpos.xyz - u_cameraPos;
a_modelpos.xyz = apply_planet_curvature(a_modelpos.xyz, pos3d);
a_realnormal = v_normal.xyz * 2.0 - 1.0; a_realnormal = v_normal.xyz * 2.0 - 1.0;
a_normal = calc_screen_normal(a_realnormal); a_normal = calc_screen_normal(a_realnormal);
vec3 light = v_light.rgb; a_torchLight = vec4(calc_torch_light(
float torchlight = calc_torch_light(a_realnormal, a_modelpos.xyz); v_light.rgb, a_realnormal, a_modelpos.xyz, u_torchlightColor, u_gamma
a_torchLight = vec4(pow(light + torchlight * u_torchlightColor, vec3(u_gamma)), 1.0f); ), 1.0);
a_texCoord = v_texCoord; a_texCoord = v_texCoord;
a_dir = a_modelpos.xyz - u_cameraPos; a_dir = a_modelpos.xyz - u_cameraPos;
@ -51,9 +30,11 @@ void main() {
mat4 viewmodel = u_view * u_model; mat4 viewmodel = u_view * u_model;
a_distance = length(viewmodel * vec4(pos3d, 0.0)); a_distance = length(viewmodel * vec4(pos3d, 0.0));
#ifndef ADVANCED_RENDER #ifndef ADVANCED_RENDER
a_fog = calc_fog(length(viewmodel * vec4(pos3d * FOG_POS_SCALE, 0.0)) / 256.0); a_fog = calc_fog(length(viewmodel * vec4(pos3d * FOG_POS_SCALE, 0.0)) / 256.0);
#endif #endif
a_emission = v_normal.w; a_emission = v_normal.w;
vec4 viewmodelpos = u_view * a_modelpos; vec4 viewmodelpos = u_view * a_modelpos;

View File

@ -0,0 +1,38 @@
layout (location = 0) out vec4 f_color;
#include <world_fragment_header>
in vec4 a_torchLight;
uniform sampler2D u_texture0;
uniform vec3 u_sunDir;
// flags
uniform bool u_alphaClip;
uniform bool u_debugLights;
uniform bool u_debugNormals;
#include <shadows>
void main() {
vec4 texColor = texture(u_texture0, a_texCoord);
float alpha = texColor.a;
if (u_alphaClip) {
if (alpha < 0.2f)
discard;
alpha = 1.0;
} else {
if (alpha < 0.002f)
discard;
}
if (u_debugLights)
texColor.rgb = u_debugNormals ? (a_normal * 0.5 + 0.5) : vec3(1.0);
f_color = texColor;
f_color.rgb *= min(vec3(1.0), a_torchLight.rgb + a_skyLight
* calc_shadow(a_modelpos, a_realnormal, length(a_position)));
vec3 fogColor = texture(u_skybox, a_dir).rgb;
f_color = mix(f_color, vec4(fogColor, 1.0), a_fog);
f_color.a = alpha;
}

View File

@ -0,0 +1,39 @@
#include <commons>
layout (location = 0) in vec3 v_position;
layout (location = 1) in vec2 v_texCoord;
layout (location = 2) in vec4 v_light;
layout (location = 3) in vec4 v_normal;
#include <world_vertex_header>
#include <lighting>
#include <fog>
#include <sky>
out vec4 a_torchLight;
void main() {
a_modelpos = u_model * vec4(v_position, 1.0f);
vec3 pos3d = a_modelpos.xyz - u_cameraPos;
a_realnormal = v_normal.xyz * 2.0 - 1.0;
a_normal = calc_screen_normal(a_realnormal);
a_torchLight = vec4(calc_torch_light(
v_light.rgb, a_realnormal, a_modelpos.xyz, u_torchlightColor, u_gamma
), 1.0);
a_texCoord = v_texCoord;
a_dir = a_modelpos.xyz - u_cameraPos;
vec3 skyLightColor = pick_sky_color(u_skybox);
a_skyLight = skyLightColor.rgb*v_light.a;
mat4 viewmodel = u_view * u_model;
a_distance = length(viewmodel * vec4(pos3d, 0.0));
a_fog = calc_fog(length(viewmodel * vec4(pos3d * FOG_POS_SCALE, 0.0)) / 256.0);
a_emission = v_normal.w;
vec4 viewmodelpos = u_view * a_modelpos;
a_position = viewmodelpos.xyz;
gl_Position = u_proj * viewmodelpos;
}

View File

@ -53,7 +53,7 @@ public:
}); });
} }
const T& get() const { [[nodiscard]] const T& get() const {
return value; return value;
} }

View File

@ -9,9 +9,11 @@
#include "graphics/core/Atlas.hpp" #include "graphics/core/Atlas.hpp"
#include "maths/UVRegion.hpp" #include "maths/UVRegion.hpp"
#include "voxels/Block.hpp" #include "voxels/Block.hpp"
#include "debug/Logger.hpp"
#include "core_defs.hpp" #include "core_defs.hpp"
#include "settings.hpp" #include "settings.hpp"
static debug::Logger logger("content-gfx-cache");
ContentGfxCache::ContentGfxCache( ContentGfxCache::ContentGfxCache(
const Content& content, const Content& content,
@ -22,27 +24,29 @@ ContentGfxCache::ContentGfxCache(
refresh(); refresh();
} }
static void refresh_variant( void ContentGfxCache::refreshVariant(
const Assets& assets,
const Block& def, const Block& def,
const Variant& variant, const Variant& variant,
uint8_t variantIndex, uint8_t variantIndex,
std::unique_ptr<UVRegion[]>& sideregions, const Atlas& atlas
const Atlas& atlas,
const GraphicsSettings& settings,
std::unordered_map<blockid_t, model::Model>& models
) { ) {
bool denseRender = settings.denseRender.get();
for (uint side = 0; side < 6; side++) { for (uint side = 0; side < 6; side++) {
std::string tex = variant.textureFaces[side]; std::string tex = variant.textureFaces[side];
if (variant.culling == CullingMode::OPTIONAL && std::string texOpaque = tex + "_opaque";
!settings.denseRender.get() && atlas.has(tex + "_opaque")) {
tex = tex + "_opaque"; if (!atlas.has(tex)) {
tex = TEXTURE_NOTFOUND;
} }
if (atlas.has(tex)) {
sideregions[(def.rt.id * 6 + side) * MAX_VARIANTS + variantIndex] = atlas.get(tex); if (!atlas.has(texOpaque)) {
} else if (atlas.has(TEXTURE_NOTFOUND)) { texOpaque = tex;
sideregions[(def.rt.id * 6 + side) * MAX_VARIANTS + variantIndex] = atlas.get(TEXTURE_NOTFOUND); } else if (variant.culling == CullingMode::OPTIONAL && !denseRender) {
tex = texOpaque;
} }
size_t index = getRegionIndex(def.rt.id, variantIndex, side, false);
sideregions[index] = atlas.get(tex);
sideregions[index + 1] = atlas.get(texOpaque);
} }
if (variant.model.type == BlockModelType::CUSTOM) { if (variant.model.type == BlockModelType::CUSTOM) {
auto model = assets.require<model::Model>(variant.model.name); auto model = assets.require<model::Model>(variant.model.name);
@ -63,11 +67,11 @@ static void refresh_variant(
} }
void ContentGfxCache::refresh(const Block& def, const Atlas& atlas) { void ContentGfxCache::refresh(const Block& def, const Atlas& atlas) {
refresh_variant(assets, def, def.defaults, 0, sideregions, atlas, settings, models); refreshVariant(def, def.defaults, 0, atlas);
if (def.variants) { if (def.variants) {
const auto& variants = def.variants->variants; const auto& variants = def.variants->variants;
for (int i = 1; i < variants.size() - 1; i++) { for (int i = 1; i < variants.size() - 1; i++) {
refresh_variant(assets, def, variants[i], i, sideregions, atlas, settings, models); refreshVariant(def, variants[i], i, atlas);
} }
def.variants->variants.at(0) = def.defaults; def.variants->variants.at(0) = def.defaults;
} }
@ -75,7 +79,11 @@ void ContentGfxCache::refresh(const Block& def, const Atlas& atlas) {
void ContentGfxCache::refresh() { void ContentGfxCache::refresh() {
auto indices = content.getIndices(); auto indices = content.getIndices();
sideregions = std::make_unique<UVRegion[]>(indices->blocks.count() * 6 * MAX_VARIANTS); size_t size = indices->blocks.count() * GFXC_SIDES * GFXC_MAX_VARIANTS * 2;
logger.info() << "uv cache size is " << (sizeof(UVRegion) * size) << " B";
sideregions = std::make_unique<UVRegion[]>(size);
const auto& atlas = assets.require<Atlas>("blocks"); const auto& atlas = assets.require<Atlas>("blocks");
const auto& blocks = indices->blocks.getIterable(); const auto& blocks = indices->blocks.getIterable();

View File

@ -14,9 +14,11 @@ class Assets;
class Atlas; class Atlas;
class Block; class Block;
struct UVRegion; struct UVRegion;
struct Variant;
struct GraphicsSettings; struct GraphicsSettings;
inline constexpr int MAX_VARIANTS = 16; inline constexpr int GFXC_MAX_VARIANTS = 16;
inline constexpr int GFXC_SIDES = 6;
class ContentGfxCache { class ContentGfxCache {
const Content& content; const Content& content;
@ -26,6 +28,13 @@ class ContentGfxCache {
// array of block sides uv regions (6 per block) // array of block sides uv regions (6 per block)
std::unique_ptr<UVRegion[]> sideregions; std::unique_ptr<UVRegion[]> sideregions;
std::unordered_map<blockid_t, model::Model> models; std::unordered_map<blockid_t, model::Model> models;
void refreshVariant(
const Block& def,
const Variant& variant,
uint8_t variantIndex,
const Atlas& atlas
);
public: public:
ContentGfxCache( ContentGfxCache(
const Content& content, const Content& content,
@ -34,8 +43,14 @@ public:
); );
~ContentGfxCache(); ~ContentGfxCache();
inline const UVRegion& getRegion(blockid_t id, uint8_t variant, int side) const { static inline size_t getRegionIndex(
return sideregions[(id * 6 + side) * MAX_VARIANTS + variant]; blockid_t id, uint8_t variant, int side, bool opaque
) {
return ((id * GFXC_SIDES + side) * GFXC_MAX_VARIANTS + variant) * 2 + opaque;
}
inline const UVRegion& getRegion(blockid_t id, uint8_t variant, int side, bool dense) const {
return sideregions[getRegionIndex(id, variant, side, !dense)];
} }
const model::Model& getModel(blockid_t id) const; const model::Model& getModel(blockid_t id) const;

View File

@ -1,5 +1,7 @@
#pragma once #pragma once
#include <vector>
#include "MeshData.hpp" #include "MeshData.hpp"
@ -8,25 +10,32 @@ struct MeshStats {
static int drawCalls; static int drawCalls;
}; };
struct IndexBufferData {
const uint32_t* indices;
size_t indicesCount;
};
template <typename VertexStructure> template <typename VertexStructure>
class Mesh { class Mesh {
struct IndexBuffer {
unsigned int ibo;
size_t indexCount;
};
unsigned int vao; unsigned int vao;
unsigned int vbo; unsigned int vbo;
unsigned int ibo; std::vector<IndexBuffer> ibos;
size_t vertexCount; size_t vertexCount;
size_t indexCount;
public: public:
explicit Mesh(const MeshData<VertexStructure>& data); explicit Mesh(const MeshData<VertexStructure>& data);
Mesh( Mesh(
const VertexStructure* vertexBuffer, const VertexStructure* vertexBuffer,
size_t vertices, size_t vertices,
const uint32_t* indexBuffer, std::vector<IndexBufferData> indices
size_t indices
); );
Mesh(const VertexStructure* vertexBuffer, size_t vertices) Mesh(const VertexStructure* vertexBuffer, size_t vertices)
: Mesh<VertexStructure>(vertexBuffer, vertices, nullptr, 0) {}; : Mesh<VertexStructure>(vertexBuffer, vertices, {}) {};
~Mesh(); ~Mesh();
@ -34,18 +43,21 @@ public:
/// attributes /// attributes
/// @param vertexBuffer vertex data buffer /// @param vertexBuffer vertex data buffer
/// @param vertexCount number of vertices in new buffer /// @param vertexCount number of vertices in new buffer
/// @param indexBuffer indices buffer /// @param indices indices buffer
/// @param indexCount number of values in indices buffer
void reload( void reload(
const VertexStructure* vertexBuffer, const VertexStructure* vertexBuffer,
size_t vertexCount, size_t vertexCount,
const uint32_t* indexBuffer = nullptr, const std::vector<IndexBufferData>& indices
size_t indexCount = 0
); );
void reload(const VertexStructure* vertexBuffer, size_t vertexCount) {
static const std::vector<IndexBufferData> indices {};
reload(vertexBuffer, vertexCount, indices);
}
/// @brief Draw mesh with specified primitives type /// @brief Draw mesh with specified primitives type
/// @param primitive primitives type /// @param iboIndex index of used element buffer
void draw(unsigned int primitive) const; void draw(unsigned int primitive, int iboIndex = 0) const;
/// @brief Draw mesh as triangles /// @brief Draw mesh as triangles
void draw() const; void draw() const;

View File

@ -11,13 +11,21 @@ inline constexpr size_t calc_size(const VertexAttribute attrs[]) {
return vertexSize; return vertexSize;
} }
template <typename VertexStructure>
inline std::vector<IndexBufferData> convert_to_ibd(const MeshData<VertexStructure>& data) {
std::vector<IndexBufferData> indices;
for (const auto& buffer : data.indices) {
indices.push_back(IndexBufferData {buffer.data(), buffer.size()});
}
return indices;
}
template <typename VertexStructure> template <typename VertexStructure>
Mesh<VertexStructure>::Mesh(const MeshData<VertexStructure>& data) Mesh<VertexStructure>::Mesh(const MeshData<VertexStructure>& data)
: Mesh( : Mesh(
data.vertices.data(), data.vertices.data(),
data.vertices.size(), data.vertices.size(),
data.indices.data(), convert_to_ibd<VertexStructure>(data)
data.indices.size()
) { ) {
} }
@ -25,10 +33,9 @@ template <typename VertexStructure>
Mesh<VertexStructure>::Mesh( Mesh<VertexStructure>::Mesh(
const VertexStructure* vertexBuffer, const VertexStructure* vertexBuffer,
size_t vertices, size_t vertices,
const uint32_t* indexBuffer, std::vector<IndexBufferData> indices
size_t indices
) )
: vao(0), vbo(0), ibo(0), vertexCount(0), indexCount(0) { : vao(0), vbo(0), ibos(), vertexCount(0) {
static_assert( static_assert(
calc_size(VertexStructure::ATTRIBUTES) == sizeof(VertexStructure) calc_size(VertexStructure::ATTRIBUTES) == sizeof(VertexStructure)
); );
@ -39,8 +46,9 @@ Mesh<VertexStructure>::Mesh(
glGenVertexArrays(1, &vao); glGenVertexArrays(1, &vao);
glGenBuffers(1, &vbo); glGenBuffers(1, &vbo);
reload(vertexBuffer, vertices, indexBuffer, indices); reload(vertexBuffer, vertices, std::move(indices));
glBindVertexArray(vao);
// attributes // attributes
int offset = 0; int offset = 0;
for (int i = 0; attrs[i].count; i++) { for (int i = 0; attrs[i].count; i++) {
@ -65,8 +73,8 @@ Mesh<VertexStructure>::~Mesh() {
MeshStats::meshesCount--; MeshStats::meshesCount--;
glDeleteVertexArrays(1, &vao); glDeleteVertexArrays(1, &vao);
glDeleteBuffers(1, &vbo); glDeleteBuffers(1, &vbo);
if (ibo != 0) { for (int i = ibos.size() - 1; i >= 0; i--) {
glDeleteBuffers(1, &ibo); glDeleteBuffers(1, &ibos[i].ibo);
} }
} }
@ -74,11 +82,9 @@ template <typename VertexStructure>
void Mesh<VertexStructure>::reload( void Mesh<VertexStructure>::reload(
const VertexStructure* vertexBuffer, const VertexStructure* vertexBuffer,
size_t vertexCount, size_t vertexCount,
const uint32_t* indexBuffer, const std::vector<IndexBufferData>& indices
size_t indexCount
) { ) {
this->vertexCount = vertexCount; this->vertexCount = vertexCount;
this->indexCount = indexCount;
glBindVertexArray(vao); glBindVertexArray(vao);
glBindBuffer(GL_ARRAY_BUFFER, vbo); glBindBuffer(GL_ARRAY_BUFFER, vbo);
if (vertexBuffer != nullptr && vertexCount != 0) { if (vertexBuffer != nullptr && vertexCount != 0) {
@ -92,30 +98,45 @@ void Mesh<VertexStructure>::reload(
glBufferData(GL_ARRAY_BUFFER, 0, {}, GL_STREAM_DRAW); glBufferData(GL_ARRAY_BUFFER, 0, {}, GL_STREAM_DRAW);
} }
if (indexBuffer != nullptr && indexCount != 0) { for (int i = indices.size(); i < ibos.size(); i++) {
if (ibo == 0) glGenBuffers(1, &ibo); glDeleteBuffers(1, &ibos[i].ibo);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo); }
ibos.clear();
for (int i = 0; i < indices.size(); i++) {
const auto& indexBuffer = indices[i];
ibos.push_back(IndexBuffer {0, 0});
glGenBuffers(1, &ibos[i].ibo);
ibos[i].indexCount = indexBuffer.indicesCount;
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibos[i].ibo);
glBufferData( glBufferData(
GL_ELEMENT_ARRAY_BUFFER, GL_ELEMENT_ARRAY_BUFFER,
sizeof(uint32_t) * indexCount, sizeof(uint32_t) * indexBuffer.indicesCount,
indexBuffer, indexBuffer.indices,
GL_STATIC_DRAW GL_STATIC_DRAW
); );
} else if (ibo != 0) {
glDeleteBuffers(1, &ibo);
} }
glBindVertexArray(0);
} }
template <typename VertexStructure> template <typename VertexStructure>
void Mesh<VertexStructure>::draw(unsigned int primitive) const { void Mesh<VertexStructure>::draw(unsigned int primitive, int iboIndex) const {
MeshStats::drawCalls++; MeshStats::drawCalls++;
glBindVertexArray(vao);
if (ibo != 0) { if (!ibos.empty()) {
glDrawElements(primitive, indexCount, GL_UNSIGNED_INT, nullptr); if (iboIndex < ibos.size()) {
} else { glBindVertexArray(vao);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibos[iboIndex].ibo);
glDrawElements(
primitive, ibos.at(iboIndex).indexCount, GL_UNSIGNED_INT, nullptr
);
glBindVertexArray(0);
}
} else if (vertexCount > 0) {
glBindVertexArray(vao);
glDrawArrays(primitive, 0, vertexCount); glDrawArrays(primitive, 0, vertexCount);
glBindVertexArray(0);
} }
glBindVertexArray(0);
} }
template <typename VertexStructure> template <typename VertexStructure>

View File

@ -1,5 +1,6 @@
#pragma once #pragma once
#include <vector>
#include <stdexcept> #include <stdexcept>
#include "typedefs.hpp" #include "typedefs.hpp"
@ -40,7 +41,7 @@ struct VertexAttribute {
template<typename VertexStructure> template<typename VertexStructure>
struct MeshData { struct MeshData {
util::Buffer<VertexStructure> vertices; util::Buffer<VertexStructure> vertices;
util::Buffer<uint32_t> indices; std::vector<util::Buffer<uint32_t>> indices;
util::Buffer<VertexAttribute> attrs; util::Buffer<VertexAttribute> attrs;
MeshData() = default; MeshData() = default;
@ -50,7 +51,7 @@ struct MeshData {
/// @param attrs vertex attribute sizes (must be null-terminated) /// @param attrs vertex attribute sizes (must be null-terminated)
MeshData( MeshData(
util::Buffer<VertexStructure> vertices, util::Buffer<VertexStructure> vertices,
util::Buffer<uint32_t> indices, std::vector<util::Buffer<uint32_t>> indices,
util::Buffer<VertexAttribute> attrs util::Buffer<VertexAttribute> attrs
) : vertices(std::move(vertices)), ) : vertices(std::move(vertices)),
indices(std::move(indices)), indices(std::move(indices)),

View File

@ -27,9 +27,10 @@ std::unique_ptr<ImageData> BlocksPreview::draw(
){ ){
display::clear(); display::clear();
blockid_t id = def.rt.id; blockid_t id = def.rt.id;
const UVRegion texfaces[6]{cache.getRegion(id, 0, 0), cache.getRegion(id, 0, 1), const UVRegion texfaces[6] {
cache.getRegion(id, 0, 2), cache.getRegion(id, 0, 3), cache.getRegion(id, 0, 0, true), cache.getRegion(id, 0, 1, true),
cache.getRegion(id, 0, 4), cache.getRegion(id, 0, 5)}; cache.getRegion(id, 0, 2, true), cache.getRegion(id, 0, 3, true),
cache.getRegion(id, 0, 4, true), cache.getRegion(id, 0, 5, true)};
glm::vec3 offset(0.1f, 0.5f, 0.1f); glm::vec3 offset(0.1f, 0.5f, 0.1f);
switch (def.defaults.model.type) { switch (def.defaults.model.type) {

View File

@ -20,6 +20,7 @@ BlocksRenderer::BlocksRenderer(
) : content(content), ) : content(content),
vertexBuffer(std::make_unique<ChunkVertex[]>(capacity)), vertexBuffer(std::make_unique<ChunkVertex[]>(capacity)),
indexBuffer(std::make_unique<uint32_t[]>(capacity)), indexBuffer(std::make_unique<uint32_t[]>(capacity)),
denseIndexBuffer(std::make_unique<uint32_t[]>(capacity)),
vertexCount(0), vertexCount(0),
vertexOffset(0), vertexOffset(0),
indexCount(0), indexCount(0),
@ -475,8 +476,10 @@ glm::vec4 BlocksRenderer::pickSoftLight(
} }
void BlocksRenderer::render( void BlocksRenderer::render(
const voxel* voxels, int beginEnds[256][2] const voxel* voxels, const int beginEnds[256][2]
) { ) {
bool denseRender = this->denseRender;
bool densePass = this->densePass;
for (const auto drawGroup : *content.drawGroups) { for (const auto drawGroup : *content.drawGroups) {
int begin = beginEnds[drawGroup][0]; int begin = beginEnds[drawGroup][0];
if (begin == 0) { if (begin == 0) {
@ -493,16 +496,19 @@ void BlocksRenderer::render(
if (id == 0 || variant.drawGroup != drawGroup || state.segment) { if (id == 0 || variant.drawGroup != drawGroup || state.segment) {
continue; continue;
} }
if (denseRender != (variant.culling == CullingMode::OPTIONAL)) {
continue;
}
if (def.translucent) { if (def.translucent) {
continue; continue;
} }
const UVRegion texfaces[6] { const UVRegion texfaces[6] {
cache.getRegion(id, variantId, 0), cache.getRegion(id, variantId, 0, densePass),
cache.getRegion(id, variantId, 1), cache.getRegion(id, variantId, 1, densePass),
cache.getRegion(id, variantId, 2), cache.getRegion(id, variantId, 2, densePass),
cache.getRegion(id, variantId, 3), cache.getRegion(id, variantId, 3, densePass),
cache.getRegion(id, variantId, 4), cache.getRegion(id, variantId, 4, densePass),
cache.getRegion(id, variantId, 5) cache.getRegion(id, variantId, 5, densePass)
}; };
int x = i % CHUNK_W; int x = i % CHUNK_W;
int y = i / (CHUNK_D * CHUNK_W); int y = i / (CHUNK_D * CHUNK_W);
@ -513,16 +519,19 @@ void BlocksRenderer::render(
def.ambientOcclusion); def.ambientOcclusion);
break; break;
case BlockModelType::XSPRITE: { case BlockModelType::XSPRITE: {
if (!denseRender)
blockXSprite(x, y, z, glm::vec3(1.0f), blockXSprite(x, y, z, glm::vec3(1.0f),
texfaces[FACE_MX], texfaces[FACE_MZ], 1.0f); texfaces[FACE_MX], texfaces[FACE_MZ], 1.0f);
break; break;
} }
case BlockModelType::AABB: { case BlockModelType::AABB: {
if (!denseRender)
blockAABB({x, y, z}, texfaces, &def, vox.state.rotation, blockAABB({x, y, z}, texfaces, &def, vox.state.rotation,
!def.shadeless, def.ambientOcclusion); !def.shadeless, def.ambientOcclusion);
break; break;
} }
case BlockModelType::CUSTOM: { case BlockModelType::CUSTOM: {
if (!denseRender)
blockCustomModel( blockCustomModel(
{x, y, z}, {x, y, z},
def, def,
@ -550,6 +559,8 @@ SortingMeshData BlocksRenderer::renderTranslucent(
AABB aabb {}; AABB aabb {};
bool aabbInit = false; bool aabbInit = false;
size_t totalSize = 0; size_t totalSize = 0;
bool densePass = this->densePass;
for (const auto drawGroup : *content.drawGroups) { for (const auto drawGroup : *content.drawGroups) {
int begin = beginEnds[drawGroup][0]; int begin = beginEnds[drawGroup][0];
if (begin == 0) { if (begin == 0) {
@ -570,12 +581,12 @@ SortingMeshData BlocksRenderer::renderTranslucent(
continue; continue;
} }
const UVRegion texfaces[6] { const UVRegion texfaces[6] {
cache.getRegion(id, variantId, 0), cache.getRegion(id, variantId, 0, densePass),
cache.getRegion(id, variantId, 1), cache.getRegion(id, variantId, 1, densePass),
cache.getRegion(id, variantId, 2), cache.getRegion(id, variantId, 2, densePass),
cache.getRegion(id, variantId, 3), cache.getRegion(id, variantId, 3, densePass),
cache.getRegion(id, variantId, 4), cache.getRegion(id, variantId, 4, densePass),
cache.getRegion(id, variantId, 5) cache.getRegion(id, variantId, 5, densePass)
}; };
int x = i % CHUNK_W; int x = i % CHUNK_W;
int y = i / (CHUNK_D * CHUNK_W); int y = i / (CHUNK_D * CHUNK_W);
@ -700,21 +711,45 @@ void BlocksRenderer::build(const Chunk* chunk, const Chunks* chunks) {
vertexCount = 0; vertexCount = 0;
vertexOffset = indexCount = 0; vertexOffset = indexCount = 0;
denseRender = false;
densePass = false;
sortingMesh = renderTranslucent(voxels, beginEnds); sortingMesh = renderTranslucent(voxels, beginEnds);
overflow = false; overflow = false;
vertexCount = 0; vertexCount = 0;
vertexOffset = 0; vertexOffset = 0;
indexCount = 0; indexCount = 0;
denseIndexCount = 0;
denseRender = false; //settings.graphics.denseRender.get();
densePass = false;
render(voxels, beginEnds);
size_t endIndex = indexCount;
denseRender = true;
densePass = true;
render(voxels, beginEnds);
denseIndexCount = indexCount;
for (size_t i = 0; i < denseIndexCount; i++) {
denseIndexBuffer[i] = indexBuffer[i];
}
indexCount = endIndex;
densePass = false;
render(voxels, beginEnds); render(voxels, beginEnds);
} }
ChunkMeshData BlocksRenderer::createMesh() { ChunkMeshData BlocksRenderer::createMesh() {
return ChunkMeshData{ return ChunkMeshData {
MeshData( MeshData(
util::Buffer(vertexBuffer.get(), vertexCount), util::Buffer(vertexBuffer.get(), vertexCount),
util::Buffer(indexBuffer.get(), indexCount), std::vector<util::Buffer<uint32_t>> {
util::Buffer(indexBuffer.get(), indexCount),
util::Buffer(denseIndexBuffer.get(), denseIndexCount),
},
util::Buffer( util::Buffer(
ChunkVertex::ATTRIBUTES, sizeof(ChunkVertex::ATTRIBUTES) / sizeof(VertexAttribute) ChunkVertex::ATTRIBUTES, sizeof(ChunkVertex::ATTRIBUTES) / sizeof(VertexAttribute)
) )
@ -727,10 +762,19 @@ ChunkMesh BlocksRenderer::render(const Chunk *chunk, const Chunks *chunks) {
build(chunk, chunks); build(chunk, chunks);
return ChunkMesh{std::make_unique<Mesh<ChunkVertex>>( return ChunkMesh{std::make_unique<Mesh<ChunkVertex>>(
vertexBuffer.get(), vertexCount, indexBuffer.get(), indexCount vertexBuffer.get(), vertexCount,
std::vector<IndexBufferData> {
IndexBufferData {indexBuffer.get(), indexCount},
IndexBufferData {denseIndexBuffer.get(), denseIndexCount},
}
), std::move(sortingMesh)}; ), std::move(sortingMesh)};
} }
VoxelsVolume* BlocksRenderer::getVoxelsBuffer() const { VoxelsVolume* BlocksRenderer::getVoxelsBuffer() const {
return voxelsBuffer.get(); return voxelsBuffer.get();
} }
size_t BlocksRenderer::getMemoryConsumption() const {
size_t volume = voxelsBuffer->getW() * voxelsBuffer->getH() * voxelsBuffer->getD();
return capacity * (sizeof(ChunkVertex) + sizeof(uint32_t) * 2) + volume * (sizeof(voxel) + sizeof(light_t));
}

View File

@ -26,13 +26,17 @@ class BlocksRenderer {
const Content& content; const Content& content;
std::unique_ptr<ChunkVertex[]> vertexBuffer; std::unique_ptr<ChunkVertex[]> vertexBuffer;
std::unique_ptr<uint32_t[]> indexBuffer; std::unique_ptr<uint32_t[]> indexBuffer;
std::unique_ptr<uint32_t[]> denseIndexBuffer;
size_t vertexCount; size_t vertexCount;
size_t vertexOffset; size_t vertexOffset;
size_t indexCount; size_t indexCount;
size_t denseIndexCount;
size_t capacity; size_t capacity;
int voxelBufferPadding = 2; int voxelBufferPadding = 2;
bool overflow = false; bool overflow = false;
bool cancelled = false; bool cancelled = false;
bool densePass = false;
bool denseRender = false;
const Chunk* chunk = nullptr; const Chunk* chunk = nullptr;
std::unique_ptr<VoxelsVolume> voxelsBuffer; std::unique_ptr<VoxelsVolume> voxelsBuffer;
@ -135,10 +139,12 @@ class BlocksRenderer {
if ((otherDrawGroup && (otherDrawGroup != variant.drawGroup)) || !blockVariant.rt.solid) { if ((otherDrawGroup && (otherDrawGroup != variant.drawGroup)) || !blockVariant.rt.solid) {
return true; return true;
} }
if ((variant.culling == CullingMode::DISABLED || if (densePass) {
(variant.culling == CullingMode::OPTIONAL && return variant.culling == CullingMode::OPTIONAL;
settings.graphics.denseRender.get())) && } else if (variant.culling == CullingMode::OPTIONAL) {
vox.id == def.rt.id) { return false;
}
if (variant.culling == CullingMode::DISABLED && vox.id == def.rt.id) {
return true; return true;
} }
return !vox.id; return !vox.id;
@ -149,7 +155,7 @@ class BlocksRenderer {
glm::vec4 pickSoftLight(const glm::ivec3& coord, const glm::ivec3& right, const glm::ivec3& up) const; glm::vec4 pickSoftLight(const glm::ivec3& coord, const glm::ivec3& right, const glm::ivec3& up) const;
glm::vec4 pickSoftLight(float x, float y, float z, const glm::ivec3& right, const glm::ivec3& up) const; glm::vec4 pickSoftLight(float x, float y, float z, const glm::ivec3& right, const glm::ivec3& up) const;
void render(const voxel* voxels, int beginEnds[256][2]); void render(const voxel* voxels, const int beginEnds[256][2]);
SortingMeshData renderTranslucent(const voxel* voxels, int beginEnds[256][2]); SortingMeshData renderTranslucent(const voxel* voxels, int beginEnds[256][2]);
public: public:
BlocksRenderer( BlocksRenderer(
@ -165,6 +171,8 @@ public:
ChunkMeshData createMesh(); ChunkMeshData createMesh();
VoxelsVolume* getVoxelsBuffer() const; VoxelsVolume* getVoxelsBuffer() const;
size_t getMemoryConsumption() const;
bool isCancelled() const { bool isCancelled() const {
return cancelled; return cancelled;
} }

View File

@ -87,10 +87,12 @@ ChunksRenderer::ChunksRenderer(
level->content, cache, settings level->content, cache, settings
); );
logger.info() << "created " << threadPool.getWorkersCount() << " workers"; logger.info() << "created " << threadPool.getWorkersCount() << " workers";
logger.info() << "memory consumption is "
<< renderer->getMemoryConsumption() * threadPool.getWorkersCount()
<< " B";
} }
ChunksRenderer::~ChunksRenderer() { ChunksRenderer::~ChunksRenderer() = default;
}
const Mesh<ChunkVertex>* ChunksRenderer::render( const Mesh<ChunkVertex>* ChunksRenderer::render(
const std::shared_ptr<Chunk>& chunk, bool important const std::shared_ptr<Chunk>& chunk, bool important
@ -183,29 +185,47 @@ const Mesh<ChunkVertex>* ChunksRenderer::retrieveChunk(
} }
void ChunksRenderer::drawChunksShadowsPass( void ChunksRenderer::drawChunksShadowsPass(
const Camera& camera, Shader& shader const Camera& camera, Shader& shader, const Camera& playerCamera
) { ) {
Frustum frustum;
frustum.update(camera.getProjView());
const auto& atlas = assets.require<Atlas>("blocks"); const auto& atlas = assets.require<Atlas>("blocks");
atlas.getTexture()->bind(); atlas.getTexture()->bind();
auto denseDistance = settings.graphics.denseRenderDistance.get();
auto denseDistance2 = denseDistance * denseDistance;
for (const auto& chunk : chunks.getChunks()) { for (const auto& chunk : chunks.getChunks()) {
if (chunk == nullptr) { if (chunk == nullptr) {
continue; continue;
} }
glm::ivec2 pos {chunk->x, chunk->z};
const auto& found = meshes.find({chunk->x, chunk->z}); const auto& found = meshes.find({chunk->x, chunk->z});
if (found == meshes.end()) { if (found == meshes.end()) {
continue; continue;
} }
auto mesh = found->second.mesh.get();
if (mesh) { glm::vec3 coord(
glm::vec3 coord( pos.x * CHUNK_W + 0.5f, 0.5f, pos.y * CHUNK_D + 0.5f
chunk->x * CHUNK_W + 0.5f, 0.5f, chunk->z * CHUNK_D + 0.5f );
);
glm::mat4 model = glm::translate(glm::mat4(1.0f), coord); glm::vec3 min(chunk->x * CHUNK_W, chunk->bottom, chunk->z * CHUNK_D);
shader.uniformMatrix("u_model", model); glm::vec3 max(
mesh->draw(); chunk->x * CHUNK_W + CHUNK_W,
chunk->top,
chunk->z * CHUNK_D + CHUNK_D
);
if (!frustum.isBoxVisible(min, max)) {
continue;
} }
glm::mat4 model = glm::translate(glm::mat4(1.0f), coord);
shader.uniformMatrix("u_model", model);
found->second.mesh->draw(GL_TRIANGLES,
glm::distance2(playerCamera.position * glm::vec3(1, 0, 1),
(min + max) * 0.5f * glm::vec3(1, 0, 1)) < denseDistance2);
} }
} }
@ -242,6 +262,9 @@ void ChunksRenderer::drawChunks(
visibleChunks = 0; visibleChunks = 0;
shader.uniform1i("u_alphaClip", true); shader.uniform1i("u_alphaClip", true);
auto denseDistance = settings.graphics.denseRenderDistance.get();
auto denseDistance2 = denseDistance * denseDistance;
// TODO: minimize draw calls number // TODO: minimize draw calls number
for (int i = indices.size()-1; i >= 0; i--) { for (int i = indices.size()-1; i >= 0; i--) {
auto& chunk = chunks.getChunks()[indices[i].index]; auto& chunk = chunks.getChunks()[indices[i].index];
@ -253,7 +276,8 @@ void ChunksRenderer::drawChunks(
); );
glm::mat4 model = glm::translate(glm::mat4(1.0f), coord); glm::mat4 model = glm::translate(glm::mat4(1.0f), coord);
shader.uniformMatrix("u_model", model); shader.uniformMatrix("u_model", model);
mesh->draw(); mesh->draw(GL_TRIANGLES, glm::distance2(camera.position * glm::vec3(1, 0, 1),
(coord + glm::vec3(CHUNK_W * 0.5f, 0.0f, CHUNK_D * 0.5f))) < denseDistance2);
visibleChunks++; visibleChunks++;
} }
} }

View File

@ -73,7 +73,9 @@ public:
const std::shared_ptr<Chunk>& chunk, bool important const std::shared_ptr<Chunk>& chunk, bool important
); );
void drawChunksShadowsPass(const Camera& camera, Shader& shader); void drawChunksShadowsPass(
const Camera& camera, Shader& shader, const Camera& playerCamera
);
void drawChunks(const Camera& camera, Shader& shader); void drawChunks(const Camera& camera, Shader& shader);

View File

@ -220,8 +220,6 @@ void WorldRenderer::renderLevel(
if (hudVisible) { if (hudVisible) {
renderLines(camera, linesShader, ctx); renderLines(camera, linesShader, ctx);
} }
shader.use();
chunks->drawSortedMeshes(camera, shader);
if (!pause) { if (!pause) {
scripting::on_frontend_render(); scripting::on_frontend_render();
@ -400,14 +398,13 @@ void WorldRenderer::generateShadowsMap(
shadowCamera.setProjection(glm::ortho(min.x, max.x, min.y, max.y, 0.1f, 1000.0f)); shadowCamera.setProjection(glm::ortho(min.x, max.x, min.y, max.y, 0.1f, 1000.0f));
{ {
frustumCulling->update(shadowCamera.getProjView());
auto sctx = pctx.sub(); auto sctx = pctx.sub();
sctx.setDepthTest(true); sctx.setDepthTest(true);
sctx.setCullFace(true); sctx.setCullFace(true);
sctx.setViewport({resolution, resolution}); sctx.setViewport({resolution, resolution});
shadowMap.bind(); shadowMap.bind();
setupWorldShader(shadowsShader, shadowCamera, settings, 0.0f); setupWorldShader(shadowsShader, shadowCamera, settings, 0.0f);
chunks->drawChunksShadowsPass(shadowCamera, shadowsShader); chunks->drawChunksShadowsPass(shadowCamera, shadowsShader, camera);
shadowMap.unbind(); shadowMap.unbind();
} }
} }
@ -432,6 +429,7 @@ void WorldRenderer::draw(
auto& mainShader = assets.require<Shader>("main"); auto& mainShader = assets.require<Shader>("main");
auto& entityShader = assets.require<Shader>("entity"); auto& entityShader = assets.require<Shader>("entity");
auto& translucentShader = assets.require<Shader>("translucent");
auto& deferredShader = assets.require<PostEffect>("deferred_lighting").getShader(); auto& deferredShader = assets.require<PostEffect>("deferred_lighting").getShader();
const auto& settings = engine.getSettings(); const auto& settings = engine.getSettings();
@ -464,6 +462,7 @@ void WorldRenderer::draw(
mainShader.recompile(); mainShader.recompile();
entityShader.recompile(); entityShader.recompile();
deferredShader.recompile(); deferredShader.recompile();
translucentShader.recompile();
prevCTShaderSettings = currentSettings; prevCTShaderSettings = currentSettings;
} }
@ -526,6 +525,7 @@ void WorldRenderer::draw(
{ {
DrawContext ctx = pctx.sub(); DrawContext ctx = pctx.sub();
ctx.setDepthTest(true); ctx.setDepthTest(true);
if (gbufferPipeline) { if (gbufferPipeline) {
postProcessing.bindDepthBuffer(); postProcessing.bindDepthBuffer();
} else { } else {
@ -534,6 +534,16 @@ void WorldRenderer::draw(
// Drawing background sky plane // Drawing background sky plane
skybox->draw(ctx, camera, assets, worldInfo.daytime, clouds); skybox->draw(ctx, camera, assets, worldInfo.daytime, clouds);
{
auto sctx = ctx.sub();
sctx.setCullFace(true);
skybox->bind();
translucentShader.use();
setupWorldShader(translucentShader, camera, settings, fogFactor);
chunks->drawSortedMeshes(camera, translucentShader);
skybox->unbind();
}
entityShader.use(); entityShader.use();
setupWorldShader(entityShader, camera, settings, fogFactor); setupWorldShader(entityShader, camera, settings, fogFactor);
@ -553,8 +563,7 @@ void WorldRenderer::draw(
glBindFramebuffer(GL_FRAMEBUFFER, 0); glBindFramebuffer(GL_FRAMEBUFFER, 0);
} }
postProcessing.render(pctx, assets, timer, camera); postProcessing.render(pctx, assets, timer, camera);
skybox->unbind();
if (player.currentCamera == player.fpCamera) { if (player.currentCamera == player.fpCamera) {
DrawContext ctx = pctx.sub(); DrawContext ctx = pctx.sub();
ctx.setDepthTest(true); ctx.setDepthTest(true);

View File

@ -47,7 +47,7 @@ struct ChunkMeshData {
}; };
struct ChunkMesh { struct ChunkMesh {
std::unique_ptr<Mesh<ChunkVertex> > mesh; std::unique_ptr<Mesh<ChunkVertex>> mesh;
SortingMeshData sortingMeshData; SortingMeshData sortingMeshData;
std::unique_ptr<Mesh<ChunkVertex> > sortedMesh = nullptr; std::unique_ptr<Mesh<ChunkVertex> > sortedMesh = nullptr;
}; };

View File

@ -77,6 +77,7 @@ SettingsHandler::SettingsHandler(EngineSettings& settings) {
builder.add("advanced-render", &settings.graphics.advancedRender); builder.add("advanced-render", &settings.graphics.advancedRender);
builder.add("ssao", &settings.graphics.ssao); builder.add("ssao", &settings.graphics.ssao);
builder.add("shadows-quality", &settings.graphics.shadowsQuality); builder.add("shadows-quality", &settings.graphics.shadowsQuality);
builder.add("dense-render-distance", &settings.graphics.denseRenderDistance);
builder.section("ui"); builder.section("ui");
builder.add("language", &settings.ui.language); builder.add("language", &settings.ui.language);

View File

@ -81,6 +81,8 @@ struct GraphicsSettings {
FlagSetting ssao {true}; FlagSetting ssao {true};
/// @brief Shadows quality /// @brief Shadows quality
IntegerSetting shadowsQuality {0, 0, 3}; IntegerSetting shadowsQuality {0, 0, 3};
/// @brief Dense render distance
IntegerSetting denseRenderDistance {56, 0, 10'000};
}; };
struct DebugSettings { struct DebugSettings {