18
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-11-29 05:26:17 +01:00
Files
maszyna/audio/audiorenderer.h
jerrrrycho 3eb2fdf462 Merge staging into feat/gl46-migration
origin/staging 7ac2472c3 into feat/gl46-migration db26a33ed, so that #468
merges into staging. Merge base 037158104. Staging carries master up to
571487612 (squashed in #514); this branch had merged master up to 7c12b83ba.

Conflicts, file by file:
- README.md: WITH_BETTER_RENDERER row dropped (gl46), USE_SYSTEM_JSON row kept (staging)
- CMakeLists.txt: gl46 lists, plus staging's new core sources (hardwareinfo, sn_utils, regionfile, inionlysettings, fieldshunting, gaugewrap, drivingaid, signalpassing, brakereleaser, crew, brakecharge); none listed twice
- tests/unit/CMakeLists.txt: both (texturealpha, freecameramotion; editorSwitchDrives, Camera)
- vehicle/brakingcurve.{h,cpp}, tests/unit/test_braking_curve.cpp (add/add): staging (#479 brake_acting on the same #409 code)
- editor/editorHttp.cpp: staging (it has gl46's #432 and certificate fixes, plus #439 libcurl loaded at run time)
- tests/unit/test_editor_http.cpp (add/add): staging (adds the available() case of #439)
- utilities/Globals.h: gl46 crashdump_full (staging's CrashDumpFull is #425, replaced here by utilities/crashdump.cpp); fmradio and staging's OpenAL options both; gfx_headlights_beamwidth (#481) not taken
- utilities/Globals.cpp: both (odometer.expiry, OpenAL keys, fmradio, gl46 gfx keys); gfx.headlights.beamwidth (#481) not taken
- utilities/Logs.cpp: staging's log_file_name() for both files, with gl46's crashdump_set_log_file() for both
- utilities/utilities.h: gl46 set_thread_random_engine(); Random(int, int) gone on both sides
- tests/unit/test_utilities.cpp: staging's includes
- simulation/simulationtest.cpp: gl46 (no NvRenderer, LegacyRenderer, BasicRenderer here)
- ci_shadervalidator/* (incl. add/add utilities/Logs.h): gl46's validator of shaders/ogl46 and shaders/vk14; staging's #437 fixed the GL 3.3 one
- gl/shader.cpp, gl/glsl_common.cpp: gl46 (shaders come from rendering/common/shadersource.cpp); HEADLIGHT_BEAM_WIDTH (#481) not taken
- shaders/mat_wire.frag: gl46. Rename detection paired staging's mat_colored.frag (#481) with it
- lang/editor_pl.po: union; the 12 camera speed / orthophoto entries staging adds are here already (master #312)
- audio/audiorenderer.{h,cpp}: both (fmradio; startsample and the low-pass filter); the destructor releases the radio, then the filter
- application/editormode.h, application/editormodeswitchdrive.cpp: staging (#509 on #330, which is all this side had)
- application/application.cpp: gl46's start sequence (no init_ogl, ANGLE or NvRenderer; crash window after init_ui) with staging's hardware::log_system() (#389)
- EU07.cpp: gl46 (crash reports of utilities/crashdump.cpp); staging's side was #425's handler, replaced here
- model/Texture.{h,cpp}: gl46 (texture_source); staging's side is master #399/#411/#362/#386, merged here before
- world/Track.h, world/TrackSleepers.h: both (TrackBlend, lay(Fastened)/fasten; TrackBuckle, pack_models)
- world/Segment.cpp: staging's track buckling on the split stray factors both sides have
- vehicle/Driver.h: staging's additions (#444, #494, #473, #346, timetable flags)
- vehicle/Driver.cpp: staging's file, with this branch's f4f8c75e4 (road orders measured along the lane) and 8fbf05674 (a holding road order blocks the ones past it) applied again; #409's SeenAheadTolerance gives way to #473
- vehicle/DynObj.h: friend opengl46_renderer and friend class vehicle_table
- vehicle/DynObj.cpp: gl46's vehicle_table::update() with staging's update_off_track_contacts() and update_brake_pipes()
- McZapkie/Mover.cpp: staging's function splits (#239) and hose exchange (#388); every line this branch added to Mover.cpp/MOVER.h is kept
- .github/workflows/build_bin.yml: staging's apt-install step (#497) and pinned download-artifact (#331)

Modify/delete, kept deleted:
- rendering/opengl33renderer.{cpp,h}, rendering/openglparticles.cpp, rendering/openglrenderer.{cpp,h}: staging's versions are master's, and every master change to them is in this branch's history already
- shaders/light_common.glsl, shaders/mat_sunlessnormalmap_specgloss.frag: see below

Carried into this branch's code:
- the hose exchange after each sub-step (#387/#388) also in update_steps_parallel(), per group of vehicles, as update() does it single-threaded
- physics.pipewake (pipe_wakes(), wake_cause::scndpipe) in staging's GetScndDVc(), where the main reservoir hose exchange went
- hardware::log_gl_context() (#389) in after_window() of the GL 4.6 backend, init_ogl() being gone

Taken back out of the auto-merge (renderers this branch removed):
- shaders/vk14/tonemapping.glsl: #481's switch to ACES, applied there by rename detection
- environment/skydome.cpp, simulation/simulationenvironment.cpp: #415, the sky of the legacy renderer
- application/application.cpp: #384's fallback of an unknown gfxrenderer, which this branch had dropped

Not carried from staging (renderer files removed here):
- master #481 (571487612): old assets drawn as in 26.05 (metal diffuse, sun highlight tint, ACES, SUN_DIFFUSE_SCALE 2.5, untinted Diffuse, mat_colored base) and gfx.headlights.beamwidth. Made for the GL 3.3 shaders; the PBR shaders of shaders/ogl46 and vk14 (and their SPIR-V binaries) are the author's call
- #437: the unique temporary file of the shader validator (Sonar S5443); gl46's validator still writes /tmp/shader*

Unit tests: 1133 of 1137 cases pass. Four staging tests fail (derailment: a
repaired vehicle is back on the rails; three pullee cases): the vehicle
stands, and with physics.sleepheld (on by default here) its physics sleeps
with the Fb / FStand of before. With physics.sleepheld off they pass. Left
to the author.
2026-11-27 05:26:13 +01:00

211 lines
7.5 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 "audio/audio.h"
#include "audio/fmradio.h"
#include "audio/startsample.h"
#include "model/ResourceManager.h"
#include "application/uitranscripts.h"
// Dodaj brakujące includy
#include <list>
#include <stack>
#include <cstdint>
#include <atomic>
// Definicje dla OpenAL 1.25 jeśli brakuje
#ifndef ALC_CONNECTED
#define ALC_CONNECTED 0x313
#endif
#ifndef ALC_DEFAULT_ALL_DEVICES_SPECIFIER
#define ALC_DEFAULT_ALL_DEVICES_SPECIFIER 0x1012
#endif
#define EU07_SOUND_PROOFINGUSESRANGE
class sound_source;
using uint32_sequence = std::vector<std::uint32_t>;
enum class sound_category : unsigned int {
unknown = 0,
vehicle,
local,
ambient,
};
struct sound_properties {
glm::dvec3 location;
float pitch { 1.f };
sound_category category { sound_category::unknown };
float gain { 1.f };
float soundproofing { 1.f };
// the soundproofing of what stands between the sound and the listener, without the volume trim of the sounds placed
// on top of the listener: what the low-pass filter muffles the sound by (sound.efx.lowpass)
float soundproofing_hf { 1.f };
std::uintptr_t soundproofing_stamp { ~std::uintptr_t{0} };
};
enum class sync_state {
good,
bad_distance,
bad_resource
};
namespace audio {
struct openal_source {
friend class openal_renderer;
using buffer_sequence = std::vector<audio::buffer_handle>;
ALuint id { audio::null_resource };
ALuint last_buffer { audio::null_resource }; // the buffer queued last, the one left playing once those before it are done
sound_source *controller { nullptr };
uint32_sequence sounds;
int sound_index { 0 };
bool sound_change { false };
bool is_playing { false };
bool is_looping { false };
sound_properties properties;
sync_state sync { sync_state::good };
openal_source() = default;
template <class Iterator_>
openal_source &bind( sound_source *Controller, uint32_sequence Sounds, Iterator_ First, Iterator_ Last );
void play();
void update( double const Deltatime, glm::vec3 const &Listenervelocity );
void sync_with( sound_properties const &State );
void stop();
void loop( bool const State );
void range( float const Range );
void pitch( float const Pitch );
void clear();
private:
double update_deltatime { 0.0 };
float pitch_variation { 1.f };
float sound_range { 50.f };
glm::vec3 sound_distance { 0.f };
glm::vec3 sound_velocity { 0.f };
bool is_in_range { false };
bool is_multipart { false };
};
class openal_renderer {
public:
openal_renderer() = default;
~openal_renderer();
audio::buffer_handle fetch_buffer( std::string const &Filename );
audio::openal_buffer const &buffer( audio::buffer_handle const Buffer ) const;
// makes buffers of all sound files still decoded in the background (sound.asyncload), waiting for them
void complete_buffers();
// the sound buffers and the bytes of their data, for the debug panel
memory_count buffers_memory() const { return m_buffers.memory(); }
bool init();
template <class Iterator_>
void insert( Iterator_ First, Iterator_ Last, sound_source *Controller, uint32_sequence Sounds ) {
m_sources.emplace_back( fetch_source().bind( Controller, Sounds, First, Last ) );
}
void erase( sound_source const *Controller );
void update( double const Deltatime );
// provides access to the broadcast radio receiver
audio::fm_radio &fmradio() { return m_fmradio; }
// choices the library offers, for the settings
std::vector<std::string> output_devices() const;
std::vector<std::string> hrtf_profiles() const;
std::vector<std::string> resamplers() const;
// what the device ended up with: device, mixing rate, output mode, HRTF, resampler
std::string device_status() const;
// applies changed device options (sound.openal.renderer, outputmode, hrtf, limiter, frequency)
// without a restart. returns: false if the library can't do it
bool apply_device_settings();
// applies changed source options (sound.resampler, doppler, efx.*) to every source
void apply_source_settings();
// sets the EFX low-pass filter of a source for given soundproofing (sound.efx.lowpass)
void soundproofing_filter( ALuint const Source, float const Soundproofing ) const;
glm::dvec3 cached_camerapos;
private:
using source_list = std::list<audio::openal_source>;
using source_sequence = std::stack<ALuint>;
bool init_caps();
void init_extensions();
std::vector<ALCint> device_attributes() const;
int hrtf_index( std::string const &Name ) const;
ALint resampler_index() const;
void configure_source( ALuint const Source ) const;
audio::openal_source fetch_source();
ALCdevice *m_device { nullptr };
ALCcontext *m_context { nullptr };
bool m_ready { false };
glm::vec3 m_listenervelocity;
bool m_freeflymode { true };
bool m_windowopen { true };
int m_activecab { 0 };
buffer_manager m_buffers;
source_list m_sources;
source_sequence m_sourcespares;
audio::fm_radio m_fmradio; // streamed sound, maintained separately from regular emitters
// OpenAL Soft extension entry points, resolved at runtime via al(c)GetProcAddress
LPALDEFERUPDATESSOFT m_alDeferUpdatesSOFT { nullptr };
LPALPROCESSUPDATESSOFT m_alProcessUpdatesSOFT { nullptr };
LPALCDEVICEPAUSESOFT m_alcDevicePauseSOFT { nullptr };
LPALCDEVICERESUMESOFT m_alcDeviceResumeSOFT { nullptr };
LPALCREOPENDEVICESOFT m_alcReopenDeviceSOFT { nullptr };
LPALCRESETDEVICESOFT m_alcResetDeviceSOFT { nullptr };
LPALCGETSTRINGISOFT m_alcGetStringiSOFT { nullptr };
LPALGETSTRINGISOFT m_alGetStringiSOFT { nullptr };
LPALGENFILTERS m_alGenFilters { nullptr };
LPALDELETEFILTERS m_alDeleteFilters { nullptr };
LPALFILTERI m_alFilteri { nullptr };
LPALFILTERF m_alFilterf { nullptr };
bool m_hasoutputmode { false }; // ALC_SOFT_output_mode
bool m_haslimiter { false }; // ALC_SOFT_output_limiter
bool m_hasefx { false }; // ALC_EXT_EFX, with its filter entry points
ALuint m_lowpassfilter { 0 }; // shared low-pass filter, its parameters are copied to a source when attached
ALint m_resampler { -1 }; // index of the resampler set on the sources, or -1 to leave the library's default
bool m_candetectdisconnect { false };
std::vector<ALCint> m_contextattributes; // cached for device reopen
double m_devicechecktime { 0.0 };
bool m_reopenfailed { false }; // a failed reopen is retried each second, but logged once
// ALC_SOFT_system_events: event-driven following of output-device / default-output changes.
// Preferred over polling because it reliably catches both device removal and default changes
// (e.g. re-plugging headphones), which alcGetString(DEFAULT_ALL_DEVICES) does not report live.
LPALCEVENTCONTROLSOFT m_alcEventControlSOFT { nullptr };
LPALCEVENTCALLBACKSOFT m_alcEventCallbackSOFT { nullptr };
bool m_usedeviceevents { false };
std::atomic<bool> m_outputchanged { false }; // set from the (possibly off-thread) event callback
static void ALC_APIENTRY device_event_callback( ALCenum eventtype, ALCenum devicetype, ALCdevice *device, ALCsizei length, ALCchar const *message, void *userparam ) noexcept;
};
extern openal_renderer renderer;
extern bool event_volume_change;
} // namespace audio