mirror of
https://github.com/MaSzyna-EU07/maszyna.git
synced 2026-11-27 21:45:16 +01:00
* fix(gl46): the loading screen stays until the shaders are compiled, no black window at the first start of a new version At the first start of a new build (the driver keeps the compiled shaders of each executable apart, and every build has a name of its own) the window went black and didn't answer for about half a minute after the scenery was loaded. Measured on calkowo_osobowy with a copy of the executable under a new name: 33 of those seconds were the first step of the water on the windscreens, which compiled its nine programs (water_*.comp) in the first frame of the simulation. - After the scenery is loaded the loader keeps its screen, with the bar going and the note on preparing the graphics, until the renderer says the programs are ready (gfx_renderer::Prewarm_Shaders(), about 50 ms a frame): the programs of the materials are drawn with once each into a small target of the formats of the colour pass, as the opaque things and as the translucent ones, then the programs of the windscreen water are compiled one a frame (opengl46_windscreen_water::prepare_next()), which gpu_init() then keeps. The window answers in between. - The Vulkan renderer, which has its pipeline cache, says it's all done at once (the default of the interface). Checked with a copy of the executable under a new name (nothing kept by the driver): the loading screen with the note from the end of the loading (19 s) to the first frame (58 s), the window answering all the time; the first frame no longer stalls (the water 12 ms instead of 33 s). With the shaders kept by the driver the preparing takes no time. No asset changes. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * fix(gl46): the missing program of the windscreen water found by find_if, which GCC 13 and MSVC 14.4x compile Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * fix(loading): the notice that the graphics are being prepared at the top of the screen The notice stood at the bottom left, beside where the loading icon goes, over the trivia and the texts some of the backgrounds carry at the bottom. It is now centred at the top of the screen on a dark plate, so it reads on any background. The text and its translation are the same. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * feat(vk14): the loading screen stays until the pipelines of the scene are made, as with OpenGL 4.6 vulkan14_renderer::Prewarm_Shaders() lists the pipelines the loaded scenery draws with most: those of the programs of its materials for the colour pass of the main view, opaque and blended, and those of the shadows. With the background threads (gfx.vulkan.asyncresources) they are handed to them, made ahead and not counted as made late; without them they are made on the main thread for about 50 ms a frame. The loading screen, with its notice and progress bar, waits for them and for the pipelines of the last session the background threads make ahead. Before, a pipeline first asked for by a draw either left the draw out for a few frames or held the frame while it was made. The images of a viewport are made by make_viewport_targets(), taken out of render_scene() unchanged, so the main view's formats are known before the first frame. The key and the settings of a pipeline come from the same helpers bind_scene_pipeline() uses. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * fix(gl46, vk14): the review of #476: the multi draw variants prewarmed with a model block, the last session's list in the progress OpenGL 4.6: with gfx.gpucull 1 and the multi draws on, Prewarm_Shaders() also draws the multi draw variants of the material programs. Their shaders read the model block from the storage buffer at MODEL_DRAW_SSBO_BINDING, which nothing bound before the first frame, so the draw was undefined. The prewarm now binds a buffer of one model block there. With the default gfx.gpucull 0 there are no such variants and nothing changes. Vulkan: the loading screen waits for the background threads, and so for the list of the last session, up to 4096 pipelines. With a cold cache the bar stood at n/(n+1) all that time and looked stuck. The pipelines of that list are now counted in the progress as they are done with, made or not. The wait still ends when the background threads are idle, as before. make_viewport_targets() returns false without a context, which Sonar reported as a null pointer dereference. Its callers checked the context already. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01QRgQ1LqB4YXWorLDBBL6hD --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
301 lines
18 KiB
C++
301 lines
18 KiB
C++
/*
|
|
This Source Code Form is subject to the
|
|
terms of the Mozilla Public License, v.
|
|
2.0. If a copy of the MPL was not
|
|
distributed with this file, You can
|
|
obtain one at
|
|
http://mozilla.org/MPL/2.0/.
|
|
*/
|
|
|
|
#pragma once
|
|
|
|
#include <array>
|
|
#include <cstddef>
|
|
#include <cstdint>
|
|
#include <utility>
|
|
|
|
#include "rendering/geometrybank.h"
|
|
#include "interfaces/IMaterial.h"
|
|
#include "interfaces/ITexture.h"
|
|
#include "utilities/Globals.h"
|
|
|
|
struct lighting_data;
|
|
|
|
namespace gfx
|
|
{
|
|
class shader_program;
|
|
}
|
|
|
|
namespace script_screens
|
|
{
|
|
class post_processor;
|
|
}
|
|
|
|
namespace gfx
|
|
{
|
|
// material painted over the heightmap terrain, and the size (metres) its textures are repeated at; 0: the size given by the material, or 8 m
|
|
struct terrain_layer
|
|
{
|
|
material_handle material{null_handle};
|
|
float size{0.f};
|
|
float rotation{0.f}; // degrees the textures are turned by
|
|
};
|
|
|
|
// kind of grass grown over the heightmap terrain (heightmap::grass_def), colours in linear rgb
|
|
struct grass_kind
|
|
{
|
|
glm::vec3 base{0.f}; // at the root of the blades
|
|
glm::vec3 tip{0.f};
|
|
float height{0.3f}; // metres
|
|
float variation{0.4f}; // share of the height the blades differ by, either way
|
|
float width{0.012f}; // metres, at the root
|
|
float density{60.f}; // blades per square metre where painted fully
|
|
float bend{0.3f}; // share of the height the tips lean over by
|
|
glm::vec3 dry{0.f}; // the tips dried out, in autumn and winter
|
|
float seasonal{1.f}; // how much the kind follows the seasons, 0..1
|
|
};
|
|
|
|
// a chunk of the heightmap terrain the grass grows on, see gfx_renderer::Terrain_Grass()
|
|
struct grass_chunk
|
|
{
|
|
std::uint64_t id{0}; // the same for the same chunk, as long as it's in memory
|
|
std::uint64_t version{0}; // changes with every change of what the grass is made of
|
|
glm::dvec2 corner{0.0}; // x, z
|
|
float size{0.f}; // metres along a side
|
|
float low{0.f}, high{0.f}; // the lowest and the highest point
|
|
int cells{0}; // quads of the grid along a side
|
|
float const *heights{nullptr}; // (cells + 1)^2, rows of growing z
|
|
int samples{0}; // grass samples along a side less one
|
|
std::uint8_t const *density{nullptr}; // (samples + 1)^2 per kind, planar
|
|
std::array<int, 4> kinds{-1, -1, -1, -1}; // indices of the kinds of the samples, -1 unused
|
|
int holes{0}; // hole samples along a side less one, 0: none
|
|
std::uint8_t const *hole_mask{nullptr}; // (holes + 1)^2, 255: cut
|
|
};
|
|
} // namespace gfx
|
|
|
|
class gfx_renderer {
|
|
|
|
public:
|
|
// types
|
|
// constructors
|
|
// destructor
|
|
virtual ~gfx_renderer() {}
|
|
// methods
|
|
virtual auto Init( GLFWwindow *Window ) -> bool = 0;
|
|
virtual bool AddViewport(const global_settings::extraviewport_config &conf) = 0;
|
|
virtual void Shutdown() = 0;
|
|
// main draw call. returns false on error
|
|
virtual auto Render() -> bool = 0;
|
|
// after the scenery is loaded, while the loading screen is shown: the programs of the materials drawn with once each, for
|
|
// about Milliseconds, so the driver compiles them now and not in the first frame of the simulation (a black window that
|
|
// doesn't answer for half a minute at the first start of a new version). returns how many are done and how many there are;
|
|
// a renderer which needs none of it says it's all done
|
|
virtual std::pair<std::size_t, std::size_t> Prewarm_Shaders( double const Milliseconds ) { return { 0, 0 }; }
|
|
virtual void SwapBuffers() = 0;
|
|
virtual auto Framerate() -> float = 0;
|
|
// geometry methods
|
|
// NOTE: hands-on geometry management is exposed as a temporary measure; ultimately all visualization data should be generated/handled automatically by the renderer itself
|
|
// creates a new geometry bank. returns: handle to the bank or NULL
|
|
virtual auto Create_Bank() -> gfx::geometrybank_handle = 0;
|
|
// creates a new indexed geometry chunk of specified type from supplied data, in specified bank. returns: handle to the chunk or NULL
|
|
virtual auto Insert(gfx::index_array &Indices, gfx::vertex_array &Vertices, gfx::userdata_array &Userdata, gfx::geometrybank_handle const &Geometry, int const Type) -> gfx::geometry_handle = 0;
|
|
// creates a new geometry chunk of specified type from supplied data, in specified bank. returns: handle to the chunk or NULL
|
|
virtual auto Insert(gfx::vertex_array &Vertices, gfx::userdata_array &Userdata, gfx::geometrybank_handle const &Geometry, int const Type) -> gfx::geometry_handle = 0;
|
|
// replaces data of specified chunk with the supplied vertex data, starting from specified offset
|
|
virtual auto Replace(gfx::vertex_array &Vertices, gfx::userdata_array &Userdata, gfx::geometry_handle const &Geometry, int const Type, const std::size_t Offset = 0) -> bool = 0;
|
|
// replaces the index and the vertex data of specified indexed chunk with the supplied data of the same counts.
|
|
// returns: false if it wasn't replaced (the counts differ, or the backend can't)
|
|
virtual auto Replace_Indexed( gfx::index_array &Indices, gfx::vertex_array &Vertices, gfx::userdata_array &Userdata, gfx::geometry_handle const &Geometry ) -> bool { return false; }
|
|
// adds supplied vertex data at the end of specified chunk
|
|
virtual auto Append(gfx::vertex_array &Vertices, gfx::userdata_array &Userdata, gfx::geometry_handle const &Geometry, int const Type) -> bool = 0;
|
|
// empties specified chunk, its handle stays valid to be filled again
|
|
virtual void Erase( gfx::geometry_handle const & /*Geometry*/ ) {
|
|
// a renderer which keeps no chunks of its own has none to empty
|
|
}
|
|
// provides direct access to index data of specfied chunk
|
|
virtual auto Indices( gfx::geometry_handle const &Geometry ) const->gfx::index_array const & = 0;
|
|
// provides direct access to vertex data of specfied chunk
|
|
virtual auto Vertices( gfx::geometry_handle const &Geometry ) const ->gfx::vertex_array const & = 0;
|
|
// provides direct access to vertex user data of specfied chunk
|
|
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;
|
|
virtual auto Material( material_handle const Material ) const -> IMaterial const * = 0;
|
|
// the same material with its highlights damped, for faces which aren't polished like the surfaces they are cut from. backends which can't make one give back the material itself
|
|
virtual auto Matte_Material( material_handle const Material ) -> material_handle { return Material; }
|
|
// the same material with its texture tinted by the colour, as far as the userdata of the vertices says. backends which can't make one give back the material itself
|
|
virtual auto Tinted_Material( material_handle const Material, glm::vec3 const &Colour ) -> material_handle { return Material; }
|
|
// the material of a track blending its texture with those of up to three others (null: none) by the weights in y, z and w of the user data
|
|
// of the vertices (world/TrackBlend.h). backends which can't make one give back the material itself
|
|
virtual auto Blend_Material( material_handle const Material, std::array<material_handle, 3> const &Others ) -> material_handle { return Material; }
|
|
// whether a material can be blended so
|
|
virtual auto Blendable_Material( material_handle const Material ) -> bool { return false; }
|
|
// heightmap terrain: material of a chunk blending the materials of up to 8 layers by their weights.
|
|
// Weights: (Samples + 1)^2 values per layer, planar, rows of growing z, the weights of a sample adding up to 255; null with a single layer.
|
|
// Placement: corner of the chunk (x, z) in the texture coordinates of its mesh, and the size of the chunk.
|
|
// Reuse: a material made by this call before, to be filled anew, or null_handle.
|
|
// Holes: (HoleSamples + 1)^2 values (255: the ground is cut there), rows like the weights; null without holes. the cells
|
|
// cut out entirely are left out of the geometry, the material cuts the rest of the openings.
|
|
// returns: the material. backends which can't blend give back the material of the layer covering the most
|
|
virtual auto 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 = 0, std::uint8_t const *Holes = nullptr ) -> material_handle;
|
|
// gives back a material made by Terrain_Material, once its chunk is gone
|
|
virtual void Terrain_Release( material_handle const Material ) {}
|
|
// heightmap terrain: the grass to draw, the kinds of the grass palette and the chunks near the camera which have grass.
|
|
// the data of the chunks is read during the call only; a chunk the renderer has with the same id and version isn't read
|
|
// again. chunks left out are dropped. backends which can't draw grass ignore it
|
|
virtual void Terrain_Grass( std::vector<gfx::grass_kind> const &Kinds, std::vector<gfx::grass_chunk> const &Chunks ) {}
|
|
// shader methods
|
|
virtual auto Fetch_Shader( std::string const &name ) -> std::shared_ptr<gfx::shader_program> = 0;
|
|
// the name the program was fetched by, empty where it wasn't fetched by Fetch_Shader()
|
|
virtual auto Shader_Name( std::shared_ptr<gfx::shader_program> const &Program ) const -> std::string { return {}; }
|
|
// texture methods
|
|
virtual auto Fetch_Texture( std::string const &Filename, bool const Loadnow = true, GLint format_hint = GL_SRGB_ALPHA ) -> texture_handle = 0;
|
|
virtual void Bind_Texture( texture_handle const Texture ) = 0;
|
|
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;
|
|
virtual auto Pick_Control() const -> TSubModel const * = 0;
|
|
virtual auto Pick_Node() const -> scene::basic_node const * = 0;
|
|
// a node deleted from the scene is no longer given as picked
|
|
virtual void Forget_Node( scene::basic_node const * /*Node*/ ) {
|
|
// a renderer which keeps no node picked has none to forget
|
|
}
|
|
|
|
virtual auto Mouse_Position() const -> glm::dvec3 = 0;
|
|
// 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().
|
|
// default implementations return identity so backends without a usable camera still compile.
|
|
virtual auto Camera_View_Matrix() const -> glm::mat4 { return glm::mat4( 1.f ); }
|
|
virtual auto Camera_Projection_Matrix() const -> glm::mat4 { return glm::mat4( 1.f ); }
|
|
virtual auto Camera_Position() const -> glm::dvec3 { return glm::dvec3( 0.0 ); }
|
|
// maintenance methods
|
|
virtual void Update( double const Deltatime ) = 0;
|
|
// the main loop begins a frame, before it reads the input: a renderer keeping the latency low waits here (reflex of vulkan)
|
|
virtual void Frame_Start() {}
|
|
virtual void Update_Pick_Control() = 0;
|
|
virtual void Update_Pick_Node() = 0;
|
|
virtual auto Update_Mouse_Position() -> glm::dvec3 = 0;
|
|
virtual bool Debug_Ui_State(std::optional<bool>) = 0;
|
|
// debug methods
|
|
virtual auto info_times() const -> std::string const & = 0;
|
|
virtual auto info_stats() const -> std::string const & = 0;
|
|
// the same data in sections, for the debug panel to show each under a header of its own, the ones marked open unfolded at
|
|
// first. none: the panel shows info_times() and info_stats() as they are
|
|
struct info_section {
|
|
std::string title;
|
|
std::string text;
|
|
bool open { false };
|
|
};
|
|
virtual auto info_sections() const -> std::vector<info_section> const & {
|
|
static std::vector<info_section> const none;
|
|
return none; }
|
|
// imgui renderer
|
|
virtual class imgui_renderer *GetImguiRenderer() = 0;
|
|
virtual void MakeScreenshot() = 0;
|
|
// draws the model of a scenery instance on its own into a square image of Size pixels on white background, the model
|
|
// seen in perspective from the front, side and above and filling the image less Margin pixels at each edge.
|
|
// Image receives rgb rows, top row first. returns false if the backend can't make previews or there's nothing to draw
|
|
virtual auto Render_Preview( TAnimModel *Instance, int const Size, int const Margin, bool const Shadows, std::vector<std::uint8_t> &Image ) -> bool { return false; }
|
|
// runs the post-processing of the script screens (image:led_post, image:resize) on the gpu. nullptr when the backend
|
|
// can't; the screens then do it on the cpu
|
|
virtual auto Screen_post_processor() -> script_screens::post_processor * { return nullptr; }
|
|
};
|
|
|
|
// what the application has to know about a backend before its renderer exists: whether it can run here at all, and how its
|
|
// windows are made. a window made for opengl can't be drawn to with vulkan and the other way round, so the application asks
|
|
// the backend picked by gfxrenderer before the main window is made, and falls back to opengl when it can't run
|
|
struct gfx_backend_desc
|
|
{
|
|
using create_method = std::unique_ptr<gfx_renderer>(*)();
|
|
|
|
std::string name; // for the log
|
|
create_method create { nullptr };
|
|
// whether the backend can run on this machine. false: Reason tells why. null: it always can
|
|
bool (*probe)( std::string &Reason ) { nullptr };
|
|
// glfw hints of the windows of the backend, set over the defaults before the main window is made. they stay in effect for
|
|
// the windows made after it
|
|
void (*window_hints)() { nullptr };
|
|
// after the main window was made, before the renderer is. false: the backend can't use the window
|
|
bool (*after_window)( GLFWwindow *Window ) { nullptr };
|
|
// the main window couldn't be made; Reason is the error of glfw. tells the user what is missing
|
|
void (*window_failed)( std::string const &Reason ) { nullptr };
|
|
// the windows have an opengl context, which the windows made later can share
|
|
bool gl_context { true };
|
|
};
|
|
|
|
class gfx_renderer_factory
|
|
{
|
|
public:
|
|
using create_method = gfx_backend_desc::create_method;
|
|
|
|
// a backend with no window of its own to care about (e.g. "null")
|
|
bool register_backend(const std::string &backend, create_method func);
|
|
bool register_backend(const std::string &backend, gfx_backend_desc const &desc);
|
|
// 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();
|
|
|
|
private:
|
|
std::unordered_map<std::string, gfx_backend_desc> backends;
|
|
static gfx_renderer_factory *instance;
|
|
};
|
|
|
|
class imgui_renderer
|
|
{
|
|
public:
|
|
// how an image of the user interface is sampled
|
|
struct image_options
|
|
{
|
|
bool srgb{true}; // the colours are srgb, made linear by the sampling; plain values otherwise
|
|
bool nearest{false}; // the nearest texel instead of the linear filtering
|
|
bool mipmaps{false}; // mipmaps made of the image, for an image drawn smaller than it is
|
|
};
|
|
|
|
virtual bool Init() = 0;
|
|
virtual void Shutdown() = 0;
|
|
virtual void BeginFrame() = 0;
|
|
virtual void Render() = 0;
|
|
// images of the user interface itself (e.g. the previews of the node bank), apart from the textures of the scene: rgba, rows
|
|
// from the top. returns: id for ImGui::Image, 0 if the renderer can't make them
|
|
virtual std::uint64_t Create_Image(std::uint8_t const *Rgba, int const Width, int const Height, image_options const &Options)
|
|
{
|
|
return 0;
|
|
}
|
|
std::uint64_t Create_Image(std::uint8_t const *Rgba, int const Width, int const Height)
|
|
{
|
|
return Create_Image(Rgba, Width, Height, image_options{});
|
|
}
|
|
// the image given other content, of the same or another size (Image 0: a new one). returns: its id, which can be a new one
|
|
virtual std::uint64_t Update_Image(std::uint64_t const Image, std::uint8_t const *Rgba, int const Width, int const Height, image_options const &Options)
|
|
{
|
|
Release_Image(Image);
|
|
return Create_Image(Rgba, Width, Height, Options);
|
|
}
|
|
virtual void Release_Image(std::uint64_t const Image) {}
|
|
};
|
|
|
|
extern std::unique_ptr<gfx_renderer> GfxRenderer;
|
|
|
|
//---------------------------------------------------------------------------
|