18
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-11-20 08:04:57 +01:00

Merge branch 'master' into feat/renderers-cleanup

This commit is contained in:
2026-11-16 21:44:48 +01:00
264 changed files with 41203 additions and 10041 deletions

View File

@@ -814,8 +814,16 @@ void scene_walk::Render(scene::shape_node const &Shape, bool const Ignorerange)
break;
}
// render
auto const decal{data.decal && (m_renderpass.draw_mode != rendermode::shadows)};
if (decal)
{
walk_decal(true);
}
walk_draw(data.geometry);
if (decal)
{
walk_decal(false);
}
// debug data
++m_renderpass.draw_stats.shapes;
++m_renderpass.draw_stats.drawcalls;

View File

@@ -265,6 +265,9 @@ class scene_walk
// the runs written to the instance buffer of the frame, each as the modelview of its origin times its transforms
virtual void walk_timbers_expand(std::vector<glm::mat4> const &Origins, std::vector<gl::timber_range> const &Ranges) {}
virtual void walk_cull_face(bool const Enabled) = 0;
// a decal (shapenode_data::decal) drawn pulled towards the camera in the depth buffer, over the surface it lies on; on top of the
// bias the pass draws everything with. not in the shadows, which it casts none of
virtual void walk_decal(bool const Enabled) {}
// the glare of a lamp: drawn without the depth test, added to what is there; drawn only where the point of the light was
// seen (walk_light_query() of the last frame, when the backend has it)
virtual void walk_glare(bool const Enabled) = 0;

View File

@@ -11,11 +11,13 @@ http://mozilla.org/MPL/2.0/.
#include "rendering/common/shadersource.h"
#include <algorithm>
#include <cerrno>
#include <cstring>
#include <filesystem>
#include <fstream>
#include <mutex>
#include <sstream>
#include <system_error>
#include "utilities/Logs.h"
@@ -48,7 +50,13 @@ std::string read_file(std::string const &Path)
{
bool found{false};
auto text{read_text(Path, found)};
if ((false == found) || text.empty())
if (false == found)
{
// with the reason the system gives: a file which is there but can't be opened (too many files open) looks
// the same as a missing one otherwise
throw shader_exception("cannot open shader: " + Path + " (" + std::generic_category().message(errno) + ")");
}
if (text.empty())
{
throw shader_exception("cannot read shader: " + Path);
}

View File

@@ -85,6 +85,26 @@ basic_vertex::deserialize_packed( std::istream &s, bool const Tangent ) {
}
}
namespace {
// the tangent of a vertex across its normal, from the direction the texture of its faces runs in
glm::vec3 tangent_across( glm::vec3 const &Normal, glm::vec3 const &Texture ) {
if( auto const tangent { Texture - Normal * glm::dot( Normal, Texture ) };
glm::dot( tangent, tangent ) > 1e-12f ) {
return tangent;
}
// the texture of the faces gives the vertex no direction, as where they all have one spot of it like the cut ends of the rails;
// normalized, nothing would be NaN and the pixels black whatever the light and fog, any direction across the normal does
if( auto const tangent { glm::cross( Normal, ( std::abs( Normal.x ) < 0.9f ? glm::vec3 { 1.f, 0.f, 0.f } : glm::vec3 { 0.f, 1.f, 0.f } ) ) };
glm::dot( tangent, tangent ) > 1e-12f ) {
return tangent;
}
return { 1.f, 0.f, 0.f };
}
} // namespace
// based on
// Lengyel, Eric. “Computing Tangent Space Basis Vectors for an Arbitrary Mesh”.
// Terathon Software, 2001. http://terathon.com/code/tangent.html
@@ -197,7 +217,7 @@ void calculate_tangents(vertex_array &vertices, index_array const &indices, int
vertices[a].tangent =
glm::vec4(
glm::normalize((t - n * glm::dot(n, t))),
glm::normalize( tangent_across( n, t ) ),
(glm::dot(glm::cross(n, t), t2) < 0.0F) ?
-1.0F :
1.0F);

View File

@@ -3526,6 +3526,11 @@ ITexture const &opengl46_renderer::Texture(texture_handle const Texture) const
return m_textures.texture(Texture);
}
void opengl46_renderer::Unload_Texture(texture_handle const Texture)
{
m_textures.unload(Texture);
}
void opengl46_renderer::Update_AnimModel(TAnimModel *model)
{
model->RaAnimate(m_framestamp);
@@ -5042,6 +5047,29 @@ void opengl46_renderer::walk_cull_face(bool const Enabled)
glDisable(GL_CULL_FACE);
}
void opengl46_renderer::walk_decal(bool const Enabled)
{
// the depth is reversed (GL_GEQUAL): a positive offset comes nearer the camera. the depth pass of the scene draws everything a
// little farther (glPolygonOffset(-1, -1), Render_Scene_Depth()), the decal goes as much nearer from there
if (m_scenedepthpass)
{
if (Enabled)
::glPolygonOffset(-1.f + 2.f, -1.f + 8.f);
else
::glPolygonOffset(-1.f, -1.f);
return;
}
if (Enabled)
{
::glEnable(GL_POLYGON_OFFSET_FILL);
::glPolygonOffset(2.f, 8.f);
}
else
{
::glDisable(GL_POLYGON_OFFSET_FILL);
}
}
void opengl46_renderer::walk_glare(bool const Enabled)
{
if (Enabled)

View File

@@ -102,6 +102,8 @@ class opengl46_renderer : public gfx_renderer, public gfx::scene_walk {
UserData( gfx::geometry_handle const &Geometry ) const override;
int
Primitive( gfx::geometry_handle const &Geometry ) const override;
bool
Draws_Timbers() const override { return true; }
// material methods
material_handle
Fetch_Material( std::string const &Filename, bool const Loadnow = true ) override;
@@ -138,6 +140,8 @@ class opengl46_renderer : public gfx_renderer, public gfx::scene_walk {
Texture( texture_handle const Texture ) override;
ITexture const &
Texture( texture_handle const Texture ) const override;
void
Unload_Texture( texture_handle const Texture ) override;
// utility methods
void
Pick_Control_Callback( std::function<void( TSubModel const *, const glm::vec2 )> Callback ) override;
@@ -225,6 +229,7 @@ class opengl46_renderer : public gfx_renderer, public gfx::scene_walk {
std::uint32_t walk_instances_reserve(std::size_t const Count) override;
void walk_timbers_expand(std::vector<glm::mat4> const &Origins, std::vector<gl::timber_range> const &Ranges) override;
void walk_cull_face(bool const Enabled) override;
void walk_decal(bool const Enabled) override;
void walk_glare(bool const Enabled) override;
void walk_glare_draw(TSubModel *Submodel, gfx::geometry_handle const &Geometry) override;
void walk_light_query(TSubModel *Submodel, bool const Begin) override;

View File

@@ -43,6 +43,11 @@ std::unique_ptr<gfx_renderer> gfx_renderer_factory::create(const std::string &ba
return nullptr;
}
bool gfx_renderer_factory::has_backend(const std::string &name) const
{
return backends.contains(name);
}
gfx_renderer_factory *gfx_renderer_factory::get_instance()
{
if (!instance)
@@ -53,6 +58,16 @@ gfx_renderer_factory *gfx_renderer_factory::get_instance()
gfx_renderer_factory *gfx_renderer_factory::instance;
void gfx_renderer::Unload_Material( material_handle const Material )
{
if( Material == null_handle ) {
return;
}
for( auto const texture : this->Material( Material )->GetAllTextures() ) {
Unload_Texture( texture );
}
}
material_handle gfx_renderer::Terrain_Material( material_handle const Reuse, std::vector<gfx::terrain_layer> const &Layers, int const Samples, std::uint8_t const *Weights, glm::vec3 const &Placement, int const HoleSamples, std::uint8_t const *Holes )
{
// without blending the chunk gets the material which covers the most of it

View File

@@ -113,6 +113,8 @@ public:
virtual auto UserData( gfx::geometry_handle const &Geometry ) const ->gfx::userdata_array const & = 0;
// kind of geometry of specified chunk, -1 when the renderer doesn't tell
virtual auto Primitive( gfx::geometry_handle const &Geometry ) const -> int { return -1; }
// whether the 3D sleepers and switch timbers are drawn; where they aren't, the tracks keep their ballast as without them
virtual auto Draws_Timbers() const -> bool { return false; }
// material methods
virtual auto Fetch_Material( std::string const &Filename, bool const Loadnow = true ) -> material_handle = 0;
virtual void Bind_Material( material_handle const Material, TSubModel const *sm = nullptr, lighting_data const *lighting = nullptr ) = 0;
@@ -151,6 +153,11 @@ public:
virtual void Bind_Texture( std::size_t const Unit, texture_handle const Texture ) = 0;
virtual auto Texture( texture_handle const Texture ) -> ITexture & = 0;
virtual auto Texture( texture_handle const Texture ) const -> ITexture const & = 0;
// a texture the scene has never drawn taken out of the gfx card and the memory, read again when it's used: the image of a
// texture picker scrolled past. backends which keep their textures otherwise leave it
virtual void Unload_Texture( texture_handle const Texture ) {}
// the same for all the textures of a material, those of its variants with them
void Unload_Material( material_handle const Material );
// utility methods
virtual void Pick_Control_Callback( std::function<void( TSubModel const *, const glm::vec2 )> Callback ) = 0;
virtual void Pick_Node_Callback( std::function<void( scene::basic_node * )> Callback ) = 0;
@@ -162,7 +169,9 @@ public:
}
virtual auto Mouse_Position() const -> glm::dvec3 = 0;
virtual auto Mouse_Hit() const -> bool { return true; }
// whether the cursor was over something drawn when Mouse_Position() was read; a renderer which doesn't read it says no,
// so a caller never takes a stale or empty position for a hit
virtual auto Mouse_Hit() const -> bool { return false; }
// editor helpers: matrices/position of the most recent color pass camera.
// the view matrix is camera-relative (rotation only, camera at origin), matching the
// camera-relative rendering used by the engine; build object matrices relative to Camera_Position().
@@ -236,6 +245,7 @@ public:
// the description of the backend registered by the name, null if there's none (e.g. not built in)
gfx_backend_desc const *backend(const std::string &name) const;
std::unique_ptr<gfx_renderer> create(const std::string &name);
bool has_backend(const std::string &name) const;
static gfx_renderer_factory* get_instance();

View File

@@ -84,6 +84,8 @@ class vulkan14_renderer : public gfx_renderer, public gfx::scene_walk
gfx::vertex_array const &Vertices(gfx::geometry_handle const &Geometry) const override;
gfx::userdata_array const &UserData(gfx::geometry_handle const &Geometry) const override;
int Primitive(gfx::geometry_handle const &Geometry) const override;
// the sleepers and timbers are drawn by the walk of the scene (gfx::scene_walk), as in the opengl renderer
bool Draws_Timbers() const override { return true; }
// materials
material_handle Fetch_Material(std::string const &Filename, bool const Loadnow = true) override;
material_handle Matte_Material(material_handle const Material) override;
@@ -105,6 +107,7 @@ class vulkan14_renderer : public gfx_renderer, public gfx::scene_walk
void Bind_Texture(std::size_t const Unit, texture_handle const Texture) override;
ITexture &Texture(texture_handle const Texture) override;
ITexture const &Texture(texture_handle const Texture) const override;
void Unload_Texture(texture_handle const Texture) override;
// picking
void Pick_Control_Callback(std::function<void(TSubModel const *, glm::vec2 const)> Callback) override;
void Pick_Node_Callback(std::function<void(scene::basic_node *)> Callback) override;
@@ -191,6 +194,7 @@ class vulkan14_renderer : public gfx_renderer, public gfx::scene_walk
void walk_draw(std::vector<gfx::geometrybank_handle>::iterator First, std::vector<gfx::geometrybank_handle>::iterator Last) override;
std::uint32_t walk_instances(glm::mat4 const *Modelviews, std::size_t const Count) override;
void walk_cull_face(bool const Enabled) override;
void walk_decal(bool const Enabled) override;
void walk_glare(bool const Enabled) override;
void walk_glare_draw(TSubModel *Submodel, gfx::geometry_handle const &Geometry) override;
void walk_light_query(TSubModel *Submodel, bool const Begin) override;

View File

@@ -1769,6 +1769,31 @@ void vulkan14_renderer::walk_cull_face(bool const Enabled)
m_draw->cullface = Enabled;
}
void vulkan14_renderer::walk_decal(bool const Enabled)
{
auto &draw{*m_draw};
if ((false == draw.rendering) || (draw.commands == nullptr) || draw.shadows)
{
return;
}
// the depth is reversed (VK_COMPARE_OP_GREATER_OR_EQUAL): a positive bias comes nearer the camera, as glPolygonOffset(2, 8) of
// the opengl renderer (slope 2, constant 8). the depth pass of the scene draws everything a little farther (-1, -1,
// render_scene_depth()), the decal goes as much nearer from there
if (draw.depthpass)
{
if (Enabled)
draw.commands->set_depth_bias(-1.f + 8.f, 0.f, -1.f + 2.f);
else
draw.commands->set_depth_bias(-1.f, 0.f, -1.f);
return;
}
draw.commands->set_depth_bias_enable(Enabled ? VK_TRUE : VK_FALSE);
if (Enabled)
{
draw.commands->set_depth_bias(8.f, 0.f, 2.f);
}
}
void vulkan14_renderer::walk_glare(bool const Enabled)
{
// without the depth test, added to what is there
@@ -2691,3 +2716,13 @@ ITexture const &vulkan14_renderer::Texture(texture_handle const Texture) const
}
return m_context->textures->texture(Texture);
}
void vulkan14_renderer::Unload_Texture(texture_handle const Texture)
{
if ((Texture == null_handle) || !m_context || !m_context->textures)
{
return;
}
// the image goes once the gpu is done with it (vulkan14_texture::release())
m_context->textures->unload(Texture);
}