11 #include <string_view>
12 #include <unordered_map>
20 #include "al/listener.h"
21 #include "althreads.h"
22 #include "core/context.h"
23 #include "intrusive_ptr.h"
24 #include "opthelpers.h"
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"
39 struct EffectSlotSubList
;
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;
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
;
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
)}
68 DebugLogEntry(const DebugLogEntry
&) = default;
69 DebugLogEntry(DebugLogEntry
&&) = default;
73 struct ALCcontext final
: public al::intrusive_ref
<ALCcontext
>, ContextBase
{
74 const al::intrusive_ptr
<ALCdevice
> mALDevice
;
77 bool mPropsDirty
{true};
78 bool mDeferUpdates
{false};
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;
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.
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
148 void processUpdates()
150 if(std::exchange(mDeferUpdates
, false))
155 * Applies all pending updates for the context, listener, effect slots, and
158 void applyAllUpdates();
161 [[gnu::format(__MINGW_PRINTF_FORMAT
, 3, 4)]]
163 [[gnu::format(printf
, 3, 4)]]
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
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
;
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.
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
199 void set(ALCcontext
*ctx
) const noexcept
{ sLocalContext
= ctx
; }
200 /* NOLINTEND(readability-convert-member-functions-to-static) */
202 static thread_local ThreadCtx sThreadContext
;
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
;
212 bool hasEax() const noexcept
{ return mEaxIsInitialized
; }
213 bool eaxIsCapable() const noexcept
;
215 void eaxUninitialize() noexcept
;
218 const GUID
* property_set_id
,
220 ALuint property_source_id
,
221 ALvoid
* property_value
,
222 ALuint property_value_size
);
225 const GUID
* property_set_id
,
227 ALuint property_source_id
,
228 ALvoid
* property_value
,
229 ALuint property_value_size
);
231 void eaxSetLastError() noexcept
;
234 auto eaxGetDistanceFactor() const noexcept
-> float { return mEax
.flDistanceFactor
; }
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
; }
248 void eaxCommitFxSlots()
249 { mEaxFxSlots
.commit(); }
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
;
261 Eax4Props i
; // Immediate.
262 Eax4Props d
; // Deferred.
265 using Eax5Props
= EAX50CONTEXTPROPERTIES
;
268 Eax5Props i
; // Immediate.
269 Eax5Props d
; // Deferred.
272 class ContextException
: public EaxException
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
>(
300 EAXCONTEXT_MINDISTANCEFACTOR
,
301 EAXCONTEXT_MAXDISTANCEFACTOR
);
305 struct Eax4AirAbsorptionHfValidator
{
306 void operator()(float flAirAbsorptionHF
) const
308 eax_validate_range
<ContextException
>(
311 EAXCONTEXT_MINAIRABSORPTIONHF
,
312 EAXCONTEXT_MAXAIRABSORPTIONHF
);
316 struct Eax4HfReferenceValidator
{
317 void operator()(float flHFReference
) const
319 eax_validate_range
<ContextException
>(
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
>(
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
>(
379 EAXCONTEXT_MINEAXSESSION
,
380 EAXCONTEXT_MAXEAXSESSION
);
384 struct Eax5MaxActiveSendsValidator
{
385 void operator()(unsigned long ulMaxActiveSends
) const
387 eax_validate_range
<ContextException
>(
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
>(
409 EAXCONTEXT_MINSPEAKERCONFIG
,
410 EAXCONTEXT_MAXSPEAKERCONFIG
);
414 bool mEaxIsInitialized
{};
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,
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
>();
449 // Gets a new value from EAX call,
451 // updates the deferred value,
452 // updates a dirty flag.
455 EaxDirtyFlags TDirtyBit
,
456 typename TMemberResult
,
459 void eax_defer(const EaxCall
& call
, TState
& state
, TMemberResult
TProps::*member
)
461 const auto& src
= call
.get_value
<ContextException
, const TMemberResult
>();
463 const auto& dst_i
= state
.i
.*member
;
464 auto& dst_d
= state
.d
.*member
;
472 EaxDirtyFlags TDirtyBit
,
473 typename TMemberResult
,
476 void eax_context_commit_property(TState
& state
, EaxDirtyFlags
& dst_df
,
477 TMemberResult
TProps::*member
) noexcept
479 if((mEaxDf
& TDirtyBit
) != EaxDirtyFlags
{})
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();
540 using ContextRef
= al::intrusive_ptr
<ALCcontext
>;
542 ContextRef
GetContextRef() noexcept
;
544 void UpdateContextProps(ALCcontext
*context
);
547 inline bool TrapALError
{false};
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
;
558 #endif /* ALC_CONTEXT_H */