Rename some cmake target names to avoid conflicts
[openal-soft.git] / alc / context.h
blob3dd9853dc523603963420560940039d625d877d8
1 #ifndef ALC_CONTEXT_H
2 #define ALC_CONTEXT_H
4 #include <atomic>
5 #include <bitset>
6 #include <cstdint>
7 #include <deque>
8 #include <memory>
9 #include <mutex>
10 #include <string>
11 #include <string_view>
12 #include <unordered_map>
13 #include <utility>
14 #include <vector>
16 #include "AL/al.h"
17 #include "AL/alc.h"
18 #include "AL/alext.h"
20 #include "al/listener.h"
21 #include "althreads.h"
22 #include "core/context.h"
23 #include "intrusive_ptr.h"
24 #include "opthelpers.h"
26 #ifdef ALSOFT_EAX
27 #include "al/eax/api.h"
28 #include "al/eax/exception.h"
29 #include "al/eax/fx_slot_index.h"
30 #include "al/eax/fx_slots.h"
31 #include "al/eax/utils.h"
33 class EaxCall;
34 #endif // ALSOFT_EAX
36 struct ALeffect;
37 struct ALeffectslot;
38 struct DebugGroup;
39 struct EffectSlotSubList;
40 struct SourceSubList;
42 enum class DebugSource : std::uint8_t;
43 enum class DebugType : std::uint8_t;
44 enum class DebugSeverity : std::uint8_t;
46 using uint = unsigned int;
49 enum ContextFlags {
50 DebugBit = 0, /* ALC_CONTEXT_DEBUG_BIT_EXT */
52 using ContextFlagBitset = std::bitset<sizeof(ALuint)*8>;
55 struct DebugLogEntry {
56 const DebugSource mSource;
57 const DebugType mType;
58 const DebugSeverity mSeverity;
59 const uint mId;
61 std::string mMessage;
63 template<typename T>
64 DebugLogEntry(DebugSource source, DebugType type, uint id, DebugSeverity severity, T&& message)
65 : mSource{source}, mType{type}, mSeverity{severity}, mId{id}
66 , mMessage{std::forward<T>(message)}
67 { }
68 DebugLogEntry(const DebugLogEntry&) = default;
69 DebugLogEntry(DebugLogEntry&&) = default;
73 struct ALCcontext : public al::intrusive_ref<ALCcontext>, ContextBase {
74 const al::intrusive_ptr<ALCdevice> mALDevice;
77 bool mPropsDirty{true};
78 bool mDeferUpdates{false};
80 std::mutex mPropLock;
82 al::tss<ALenum> mLastThreadError{AL_NO_ERROR};
84 const ContextFlagBitset mContextFlags;
85 std::atomic<bool> mDebugEnabled{false};
87 DistanceModel mDistanceModel{DistanceModel::Default};
88 bool mSourceDistanceModel{false};
90 float mDopplerFactor{1.0f};
91 float mDopplerVelocity{1.0f};
92 float mSpeedOfSound{SpeedOfSoundMetersPerSec};
93 float mAirAbsorptionGainHF{AirAbsorbGainHF};
95 std::mutex mEventCbLock;
96 ALEVENTPROCSOFT mEventCb{};
97 void *mEventParam{nullptr};
99 std::mutex mDebugCbLock;
100 ALDEBUGPROCEXT mDebugCb{};
101 void *mDebugParam{nullptr};
102 std::vector<DebugGroup> mDebugGroups;
103 std::deque<DebugLogEntry> mDebugLog;
105 ALlistener mListener{};
107 std::vector<SourceSubList> mSourceList;
108 ALuint mNumSources{0};
109 std::mutex mSourceLock;
111 std::vector<EffectSlotSubList> mEffectSlotList;
112 ALuint mNumEffectSlots{0u};
113 std::mutex mEffectSlotLock;
115 /* Default effect slot */
116 std::unique_ptr<ALeffectslot> mDefaultSlot;
118 std::vector<std::string_view> mExtensions;
119 std::string mExtensionsString{};
121 std::unordered_map<ALuint,std::string> mSourceNames;
122 std::unordered_map<ALuint,std::string> mEffectSlotNames;
124 ALCcontext(al::intrusive_ptr<ALCdevice> device, ContextFlagBitset flags);
125 ALCcontext(const ALCcontext&) = delete;
126 ALCcontext& operator=(const ALCcontext&) = delete;
127 ~ALCcontext();
129 void init();
131 * Removes the context from its device and removes it from being current on
132 * the running thread or globally. Stops device playback if this was the
133 * last context on its device.
135 void deinit();
138 * Defers/suspends updates for the given context's listener and sources.
139 * This does *NOT* stop mixing, but rather prevents certain property
140 * changes from taking effect. mPropLock must be held when called.
142 void deferUpdates() noexcept { mDeferUpdates = true; }
145 * Resumes update processing after being deferred. mPropLock must be held
146 * when called.
148 void processUpdates()
150 if(std::exchange(mDeferUpdates, false))
151 applyAllUpdates();
155 * Applies all pending updates for the context, listener, effect slots, and
156 * sources.
158 void applyAllUpdates();
160 #ifdef __MINGW32__
161 [[gnu::format(__MINGW_PRINTF_FORMAT, 3, 4)]]
162 #else
163 [[gnu::format(printf, 3, 4)]]
164 #endif
165 void setError(ALenum errorCode, const char *msg, ...);
167 void sendDebugMessage(std::unique_lock<std::mutex> &debuglock, DebugSource source,
168 DebugType type, ALuint id, DebugSeverity severity, std::string_view message);
170 void debugMessage(DebugSource source, DebugType type, ALuint id, DebugSeverity severity,
171 std::string_view message)
173 if(!mDebugEnabled.load(std::memory_order_relaxed)) LIKELY
174 return;
175 std::unique_lock<std::mutex> debuglock{mDebugCbLock};
176 sendDebugMessage(debuglock, source, type, id, severity, message);
179 /* Process-wide current context */
180 static std::atomic<bool> sGlobalContextLock;
181 static std::atomic<ALCcontext*> sGlobalContext;
183 private:
184 /* Thread-local current context. */
185 static inline thread_local ALCcontext *sLocalContext{};
187 /* Thread-local context handling. This handles attempting to release the
188 * context which may have been left current when the thread is destroyed.
190 class ThreadCtx {
191 public:
192 ~ThreadCtx();
193 /* NOLINTBEGIN(readability-convert-member-functions-to-static)
194 * This should be non-static to invoke construction of the thread-local
195 * sThreadContext, so that it's destructor gets run at thread exit to
196 * clear sLocalContext (which isn't a member variable to make read
197 * access efficient).
199 void set(ALCcontext *ctx) const noexcept { sLocalContext = ctx; }
200 /* NOLINTEND(readability-convert-member-functions-to-static) */
202 static thread_local ThreadCtx sThreadContext;
204 public:
205 static ALCcontext *getThreadContext() noexcept { return sLocalContext; }
206 static void setThreadContext(ALCcontext *context) noexcept { sThreadContext.set(context); }
208 /* Default effect that applies to sources that don't have an effect on send 0. */
209 static ALeffect sDefaultEffect;
211 #ifdef ALSOFT_EAX
212 bool hasEax() const noexcept { return mEaxIsInitialized; }
213 bool eaxIsCapable() const noexcept;
215 void eaxUninitialize() noexcept;
217 ALenum eax_eax_set(
218 const GUID* property_set_id,
219 ALuint property_id,
220 ALuint property_source_id,
221 ALvoid* property_value,
222 ALuint property_value_size);
224 ALenum eax_eax_get(
225 const GUID* property_set_id,
226 ALuint property_id,
227 ALuint property_source_id,
228 ALvoid* property_value,
229 ALuint property_value_size);
231 void eaxSetLastError() noexcept;
233 [[nodiscard]]
234 auto eaxGetDistanceFactor() const noexcept -> float { return mEax.flDistanceFactor; }
236 [[nodiscard]]
237 auto eaxGetPrimaryFxSlotIndex() const noexcept -> EaxFxSlotIndex
238 { return mEaxPrimaryFxSlotIndex; }
240 const ALeffectslot& eaxGetFxSlot(EaxFxSlotIndexValue fx_slot_index) const
241 { return mEaxFxSlots.get(fx_slot_index); }
242 ALeffectslot& eaxGetFxSlot(EaxFxSlotIndexValue fx_slot_index)
243 { return mEaxFxSlots.get(fx_slot_index); }
245 bool eaxNeedsCommit() const noexcept { return mEaxNeedsCommit; }
246 void eaxCommit();
248 void eaxCommitFxSlots()
249 { mEaxFxSlots.commit(); }
251 private:
252 static constexpr auto eax_primary_fx_slot_id_dirty_bit = EaxDirtyFlags{1} << 0;
253 static constexpr auto eax_distance_factor_dirty_bit = EaxDirtyFlags{1} << 1;
254 static constexpr auto eax_air_absorption_hf_dirty_bit = EaxDirtyFlags{1} << 2;
255 static constexpr auto eax_hf_reference_dirty_bit = EaxDirtyFlags{1} << 3;
256 static constexpr auto eax_macro_fx_factor_dirty_bit = EaxDirtyFlags{1} << 4;
258 using Eax4Props = EAX40CONTEXTPROPERTIES;
260 struct Eax4State {
261 Eax4Props i; // Immediate.
262 Eax4Props d; // Deferred.
265 using Eax5Props = EAX50CONTEXTPROPERTIES;
267 struct Eax5State {
268 Eax5Props i; // Immediate.
269 Eax5Props d; // Deferred.
272 class ContextException : public EaxException
274 public:
275 explicit ContextException(const char* message)
276 : EaxException{"EAX_CONTEXT", message}
280 struct Eax4PrimaryFxSlotIdValidator {
281 void operator()(const GUID& guidPrimaryFXSlotID) const
283 if(guidPrimaryFXSlotID != EAX_NULL_GUID &&
284 guidPrimaryFXSlotID != EAXPROPERTYID_EAX40_FXSlot0 &&
285 guidPrimaryFXSlotID != EAXPROPERTYID_EAX40_FXSlot1 &&
286 guidPrimaryFXSlotID != EAXPROPERTYID_EAX40_FXSlot2 &&
287 guidPrimaryFXSlotID != EAXPROPERTYID_EAX40_FXSlot3)
289 eax_fail_unknown_primary_fx_slot_id();
294 struct Eax4DistanceFactorValidator {
295 void operator()(float flDistanceFactor) const
297 eax_validate_range<ContextException>(
298 "Distance Factor",
299 flDistanceFactor,
300 EAXCONTEXT_MINDISTANCEFACTOR,
301 EAXCONTEXT_MAXDISTANCEFACTOR);
305 struct Eax4AirAbsorptionHfValidator {
306 void operator()(float flAirAbsorptionHF) const
308 eax_validate_range<ContextException>(
309 "Air Absorption HF",
310 flAirAbsorptionHF,
311 EAXCONTEXT_MINAIRABSORPTIONHF,
312 EAXCONTEXT_MAXAIRABSORPTIONHF);
316 struct Eax4HfReferenceValidator {
317 void operator()(float flHFReference) const
319 eax_validate_range<ContextException>(
320 "HF Reference",
321 flHFReference,
322 EAXCONTEXT_MINHFREFERENCE,
323 EAXCONTEXT_MAXHFREFERENCE);
327 struct Eax4AllValidator {
328 void operator()(const EAX40CONTEXTPROPERTIES& all) const
330 Eax4PrimaryFxSlotIdValidator{}(all.guidPrimaryFXSlotID);
331 Eax4DistanceFactorValidator{}(all.flDistanceFactor);
332 Eax4AirAbsorptionHfValidator{}(all.flAirAbsorptionHF);
333 Eax4HfReferenceValidator{}(all.flHFReference);
337 struct Eax5PrimaryFxSlotIdValidator {
338 void operator()(const GUID& guidPrimaryFXSlotID) const
340 if(guidPrimaryFXSlotID != EAX_NULL_GUID &&
341 guidPrimaryFXSlotID != EAXPROPERTYID_EAX50_FXSlot0 &&
342 guidPrimaryFXSlotID != EAXPROPERTYID_EAX50_FXSlot1 &&
343 guidPrimaryFXSlotID != EAXPROPERTYID_EAX50_FXSlot2 &&
344 guidPrimaryFXSlotID != EAXPROPERTYID_EAX50_FXSlot3)
346 eax_fail_unknown_primary_fx_slot_id();
351 struct Eax5MacroFxFactorValidator {
352 void operator()(float flMacroFXFactor) const
354 eax_validate_range<ContextException>(
355 "Macro FX Factor",
356 flMacroFXFactor,
357 EAXCONTEXT_MINMACROFXFACTOR,
358 EAXCONTEXT_MAXMACROFXFACTOR);
362 struct Eax5AllValidator {
363 void operator()(const EAX50CONTEXTPROPERTIES& all) const
365 Eax5PrimaryFxSlotIdValidator{}(all.guidPrimaryFXSlotID);
366 Eax4DistanceFactorValidator{}(all.flDistanceFactor);
367 Eax4AirAbsorptionHfValidator{}(all.flAirAbsorptionHF);
368 Eax4HfReferenceValidator{}(all.flHFReference);
369 Eax5MacroFxFactorValidator{}(all.flMacroFXFactor);
373 struct Eax5EaxVersionValidator {
374 void operator()(unsigned long ulEAXVersion) const
376 eax_validate_range<ContextException>(
377 "EAX version",
378 ulEAXVersion,
379 EAXCONTEXT_MINEAXSESSION,
380 EAXCONTEXT_MAXEAXSESSION);
384 struct Eax5MaxActiveSendsValidator {
385 void operator()(unsigned long ulMaxActiveSends) const
387 eax_validate_range<ContextException>(
388 "Max Active Sends",
389 ulMaxActiveSends,
390 EAXCONTEXT_MINMAXACTIVESENDS,
391 EAXCONTEXT_MAXMAXACTIVESENDS);
395 struct Eax5SessionAllValidator {
396 void operator()(const EAXSESSIONPROPERTIES& all) const
398 Eax5EaxVersionValidator{}(all.ulEAXVersion);
399 Eax5MaxActiveSendsValidator{}(all.ulMaxActiveSends);
403 struct Eax5SpeakerConfigValidator {
404 void operator()(unsigned long ulSpeakerConfig) const
406 eax_validate_range<ContextException>(
407 "Speaker Config",
408 ulSpeakerConfig,
409 EAXCONTEXT_MINSPEAKERCONFIG,
410 EAXCONTEXT_MAXSPEAKERCONFIG);
414 bool mEaxIsInitialized{};
415 bool mEaxIsTried{};
417 long mEaxLastError{};
418 unsigned long mEaxSpeakerConfig{};
420 EaxFxSlotIndex mEaxPrimaryFxSlotIndex{};
421 EaxFxSlots mEaxFxSlots{};
423 int mEaxVersion{}; // Current EAX version.
424 bool mEaxNeedsCommit{};
425 EaxDirtyFlags mEaxDf{}; // Dirty flags for the current EAX version.
426 Eax5State mEax123{}; // EAX1/EAX2/EAX3 state.
427 Eax4State mEax4{}; // EAX4 state.
428 Eax5State mEax5{}; // EAX5 state.
429 Eax5Props mEax{}; // Current EAX state.
430 EAXSESSIONPROPERTIES mEaxSession{};
432 [[noreturn]] static void eax_fail(const char* message);
433 [[noreturn]] static void eax_fail_unknown_property_set_id();
434 [[noreturn]] static void eax_fail_unknown_primary_fx_slot_id();
435 [[noreturn]] static void eax_fail_unknown_property_id();
436 [[noreturn]] static void eax_fail_unknown_version();
438 // Gets a value from EAX call,
439 // validates it,
440 // and updates the current value.
441 template<typename TValidator, typename TProperty>
442 static void eax_set(const EaxCall& call, TProperty& property)
444 const auto& value = call.get_value<ContextException, const TProperty>();
445 TValidator{}(value);
446 property = value;
449 // Gets a new value from EAX call,
450 // validates it,
451 // updates the deferred value,
452 // updates a dirty flag.
453 template<
454 typename TValidator,
455 EaxDirtyFlags TDirtyBit,
456 typename TMemberResult,
457 typename TProps,
458 typename TState>
459 void eax_defer(const EaxCall& call, TState& state, TMemberResult TProps::*member)
461 const auto& src = call.get_value<ContextException, const TMemberResult>();
462 TValidator{}(src);
463 const auto& dst_i = state.i.*member;
464 auto& dst_d = state.d.*member;
465 dst_d = src;
467 if(dst_i != dst_d)
468 mEaxDf |= TDirtyBit;
471 template<
472 EaxDirtyFlags TDirtyBit,
473 typename TMemberResult,
474 typename TProps,
475 typename TState>
476 void eax_context_commit_property(TState& state, EaxDirtyFlags& dst_df,
477 TMemberResult TProps::*member) noexcept
479 if((mEaxDf & TDirtyBit) != EaxDirtyFlags{})
481 dst_df |= TDirtyBit;
482 const auto& src_d = state.d.*member;
483 state.i.*member = src_d;
484 mEax.*member = src_d;
488 void eax_initialize_extensions();
489 void eax_initialize();
491 bool eax_has_no_default_effect_slot() const noexcept;
492 void eax_ensure_no_default_effect_slot() const;
493 bool eax_has_enough_aux_sends() const noexcept;
494 void eax_ensure_enough_aux_sends() const;
495 void eax_ensure_compatibility();
497 unsigned long eax_detect_speaker_configuration() const;
498 void eax_update_speaker_configuration();
500 void eax_set_last_error_defaults() noexcept;
501 void eax_session_set_defaults() noexcept;
502 static void eax4_context_set_defaults(Eax4Props& props) noexcept;
503 static void eax4_context_set_defaults(Eax4State& state) noexcept;
504 static void eax5_context_set_defaults(Eax5Props& props) noexcept;
505 static void eax5_context_set_defaults(Eax5State& state) noexcept;
506 void eax_context_set_defaults();
507 void eax_set_defaults();
509 void eax_dispatch_fx_slot(const EaxCall& call);
510 void eax_dispatch_source(const EaxCall& call);
512 void eax_get_misc(const EaxCall& call);
513 void eax4_get(const EaxCall& call, const Eax4Props& props);
514 void eax5_get(const EaxCall& call, const Eax5Props& props);
515 void eax_get(const EaxCall& call);
517 void eax_context_commit_primary_fx_slot_id();
518 void eax_context_commit_distance_factor();
519 void eax_context_commit_air_absorbtion_hf();
520 void eax_context_commit_hf_reference();
521 void eax_context_commit_macro_fx_factor();
523 void eax_initialize_fx_slots();
525 void eax_update_sources();
527 void eax_set_misc(const EaxCall& call);
528 void eax4_defer_all(const EaxCall& call, Eax4State& state);
529 void eax4_defer(const EaxCall& call, Eax4State& state);
530 void eax5_defer_all(const EaxCall& call, Eax5State& state);
531 void eax5_defer(const EaxCall& call, Eax5State& state);
532 void eax_set(const EaxCall& call);
534 void eax4_context_commit(Eax4State& state, EaxDirtyFlags& dst_df);
535 void eax5_context_commit(Eax5State& state, EaxDirtyFlags& dst_df);
536 void eax_context_commit();
537 #endif // ALSOFT_EAX
540 using ContextRef = al::intrusive_ptr<ALCcontext>;
542 ContextRef GetContextRef() noexcept;
544 void UpdateContextProps(ALCcontext *context);
547 inline bool TrapALError{false};
550 #ifdef ALSOFT_EAX
551 auto AL_APIENTRY EAXSet(const GUID *property_set_id, ALuint property_id,
552 ALuint source_id, ALvoid *value, ALuint value_size) noexcept -> ALenum;
554 auto AL_APIENTRY EAXGet(const GUID *property_set_id, ALuint property_id,
555 ALuint source_id, ALvoid *value, ALuint value_size) noexcept -> ALenum;
556 #endif // ALSOFT_EAX
558 #endif /* ALC_CONTEXT_H */