context.cpp 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059
  1. #include "config.h"
  2. #include "context.h"
  3. #include <algorithm>
  4. #include <array>
  5. #include <cstddef>
  6. #include <cstring>
  7. #include <functional>
  8. #include <limits>
  9. #include <numeric>
  10. #include <stdexcept>
  11. #include <string_view>
  12. #include <utility>
  13. #include "AL/efx.h"
  14. #include "al/auxeffectslot.h"
  15. #include "al/debug.h"
  16. #include "al/source.h"
  17. #include "al/effect.h"
  18. #include "al/event.h"
  19. #include "al/listener.h"
  20. #include "albit.h"
  21. #include "alc/alu.h"
  22. #include "alc/backends/base.h"
  23. #include "alspan.h"
  24. #include "core/async_event.h"
  25. #include "core/device.h"
  26. #include "core/effectslot.h"
  27. #include "core/logging.h"
  28. #include "core/voice.h"
  29. #include "core/voice_change.h"
  30. #include "device.h"
  31. #include "flexarray.h"
  32. #include "ringbuffer.h"
  33. #include "vecmat.h"
  34. #ifdef ALSOFT_EAX
  35. #include "al/eax/globals.h"
  36. #endif // ALSOFT_EAX
  37. namespace {
  38. using namespace std::string_view_literals;
  39. using voidp = void*;
  40. /* Default context extensions */
  41. std::vector<std::string_view> getContextExtensions() noexcept
  42. {
  43. return std::vector<std::string_view>{
  44. "AL_EXT_ALAW"sv,
  45. "AL_EXT_BFORMAT"sv,
  46. "AL_EXT_debug"sv,
  47. "AL_EXT_direct_context"sv,
  48. "AL_EXT_DOUBLE"sv,
  49. "AL_EXT_EXPONENT_DISTANCE"sv,
  50. "AL_EXT_FLOAT32"sv,
  51. "AL_EXT_IMA4"sv,
  52. "AL_EXT_LINEAR_DISTANCE"sv,
  53. "AL_EXT_MCFORMATS"sv,
  54. "AL_EXT_MULAW"sv,
  55. "AL_EXT_MULAW_BFORMAT"sv,
  56. "AL_EXT_MULAW_MCFORMATS"sv,
  57. "AL_EXT_OFFSET"sv,
  58. "AL_EXT_source_distance_model"sv,
  59. "AL_EXT_SOURCE_RADIUS"sv,
  60. "AL_EXT_STATIC_BUFFER"sv,
  61. "AL_EXT_STEREO_ANGLES"sv,
  62. "AL_LOKI_quadriphonic"sv,
  63. "AL_SOFT_bformat_ex"sv,
  64. "AL_SOFTX_bformat_hoa"sv,
  65. "AL_SOFT_block_alignment"sv,
  66. "AL_SOFT_buffer_length_query"sv,
  67. "AL_SOFT_callback_buffer"sv,
  68. "AL_SOFTX_convolution_effect"sv,
  69. "AL_SOFT_deferred_updates"sv,
  70. "AL_SOFT_direct_channels"sv,
  71. "AL_SOFT_direct_channels_remix"sv,
  72. "AL_SOFT_effect_target"sv,
  73. "AL_SOFT_events"sv,
  74. "AL_SOFT_gain_clamp_ex"sv,
  75. "AL_SOFTX_hold_on_disconnect"sv,
  76. "AL_SOFT_loop_points"sv,
  77. "AL_SOFTX_map_buffer"sv,
  78. "AL_SOFT_MSADPCM"sv,
  79. "AL_SOFT_source_latency"sv,
  80. "AL_SOFT_source_length"sv,
  81. "AL_SOFTX_source_panning"sv,
  82. "AL_SOFT_source_resampler"sv,
  83. "AL_SOFT_source_spatialize"sv,
  84. "AL_SOFT_source_start_delay"sv,
  85. "AL_SOFT_UHJ"sv,
  86. "AL_SOFT_UHJ_ex"sv,
  87. };
  88. }
  89. } // namespace
  90. std::atomic<bool> ALCcontext::sGlobalContextLock{false};
  91. std::atomic<ALCcontext*> ALCcontext::sGlobalContext{nullptr};
  92. ALCcontext::ThreadCtx::~ThreadCtx()
  93. {
  94. if(ALCcontext *ctx{std::exchange(ALCcontext::sLocalContext, nullptr)})
  95. {
  96. const bool result{ctx->releaseIfNoDelete()};
  97. ERR("Context %p current for thread being destroyed%s!\n", voidp{ctx},
  98. result ? "" : ", leak detected");
  99. }
  100. }
  101. thread_local ALCcontext::ThreadCtx ALCcontext::sThreadContext;
  102. ALeffect ALCcontext::sDefaultEffect;
  103. ALCcontext::ALCcontext(al::intrusive_ptr<ALCdevice> device, ContextFlagBitset flags)
  104. : ContextBase{device.get()}, mALDevice{std::move(device)}, mContextFlags{flags}
  105. {
  106. mDebugGroups.emplace_back(DebugSource::Other, 0, std::string{});
  107. mDebugEnabled.store(mContextFlags.test(ContextFlags::DebugBit), std::memory_order_relaxed);
  108. }
  109. ALCcontext::~ALCcontext()
  110. {
  111. TRACE("Freeing context %p\n", voidp{this});
  112. size_t count{std::accumulate(mSourceList.cbegin(), mSourceList.cend(), 0_uz,
  113. [](size_t cur, const SourceSubList &sublist) noexcept -> size_t
  114. { return cur + static_cast<uint>(al::popcount(~sublist.FreeMask)); })};
  115. if(count > 0)
  116. WARN("%zu Source%s not deleted\n", count, (count==1)?"":"s");
  117. mSourceList.clear();
  118. mNumSources = 0;
  119. #ifdef ALSOFT_EAX
  120. eaxUninitialize();
  121. #endif // ALSOFT_EAX
  122. mDefaultSlot = nullptr;
  123. count = std::accumulate(mEffectSlotList.cbegin(), mEffectSlotList.cend(), 0_uz,
  124. [](size_t cur, const EffectSlotSubList &sublist) noexcept -> size_t
  125. { return cur + static_cast<uint>(al::popcount(~sublist.FreeMask)); });
  126. if(count > 0)
  127. WARN("%zu AuxiliaryEffectSlot%s not deleted\n", count, (count==1)?"":"s");
  128. mEffectSlotList.clear();
  129. mNumEffectSlots = 0;
  130. }
  131. void ALCcontext::init()
  132. {
  133. if(sDefaultEffect.type != AL_EFFECT_NULL && mDevice->Type == DeviceType::Playback)
  134. {
  135. mDefaultSlot = std::make_unique<ALeffectslot>(this);
  136. aluInitEffectPanning(mDefaultSlot->mSlot, this);
  137. }
  138. std::unique_ptr<EffectSlotArray> auxslots;
  139. if(!mDefaultSlot)
  140. auxslots = EffectSlot::CreatePtrArray(0);
  141. else
  142. {
  143. auxslots = EffectSlot::CreatePtrArray(2);
  144. (*auxslots)[0] = mDefaultSlot->mSlot;
  145. (*auxslots)[1] = mDefaultSlot->mSlot;
  146. mDefaultSlot->mState = SlotState::Playing;
  147. }
  148. mActiveAuxSlots.store(std::move(auxslots), std::memory_order_relaxed);
  149. allocVoiceChanges();
  150. {
  151. VoiceChange *cur{mVoiceChangeTail};
  152. while(VoiceChange *next{cur->mNext.load(std::memory_order_relaxed)})
  153. cur = next;
  154. mCurrentVoiceChange.store(cur, std::memory_order_relaxed);
  155. }
  156. mExtensions = getContextExtensions();
  157. if(sBufferSubDataCompat)
  158. {
  159. auto iter = std::find(mExtensions.begin(), mExtensions.end(), "AL_EXT_SOURCE_RADIUS"sv);
  160. if(iter != mExtensions.end()) mExtensions.erase(iter);
  161. /* TODO: Would be nice to sort this alphabetically. Needs case-
  162. * insensitive searching.
  163. */
  164. mExtensions.emplace_back("AL_SOFT_buffer_sub_data"sv);
  165. }
  166. #ifdef ALSOFT_EAX
  167. eax_initialize_extensions();
  168. #endif // ALSOFT_EAX
  169. if(!mExtensions.empty())
  170. {
  171. const size_t len{std::accumulate(mExtensions.cbegin()+1, mExtensions.cend(),
  172. mExtensions.front().length(),
  173. [](size_t current, std::string_view ext) noexcept
  174. { return current + ext.length() + 1; })};
  175. std::string extensions;
  176. extensions.reserve(len);
  177. extensions += mExtensions.front();
  178. for(std::string_view ext : al::span{mExtensions}.subspan<1>())
  179. {
  180. extensions += ' ';
  181. extensions += ext;
  182. }
  183. mExtensionsString = std::move(extensions);
  184. }
  185. mParams.Position = alu::Vector{0.0f, 0.0f, 0.0f, 1.0f};
  186. mParams.Matrix = alu::Matrix::Identity();
  187. mParams.Velocity = alu::Vector{};
  188. mParams.Gain = mListener.Gain;
  189. mParams.MetersPerUnit = mListener.mMetersPerUnit;
  190. mParams.AirAbsorptionGainHF = mAirAbsorptionGainHF;
  191. mParams.DopplerFactor = mDopplerFactor;
  192. mParams.SpeedOfSound = mSpeedOfSound * mDopplerVelocity;
  193. mParams.SourceDistanceModel = mSourceDistanceModel;
  194. mParams.mDistanceModel = mDistanceModel;
  195. mAsyncEvents = RingBuffer::Create(1024, sizeof(AsyncEvent), false);
  196. StartEventThrd(this);
  197. allocVoices(256);
  198. mActiveVoiceCount.store(64, std::memory_order_relaxed);
  199. }
  200. void ALCcontext::deinit()
  201. {
  202. if(sLocalContext == this)
  203. {
  204. WARN("%p released while current on thread\n", voidp{this});
  205. sThreadContext.set(nullptr);
  206. dec_ref();
  207. }
  208. ALCcontext *origctx{this};
  209. if(sGlobalContext.compare_exchange_strong(origctx, nullptr))
  210. {
  211. while(sGlobalContextLock.load()) {
  212. /* Wait to make sure another thread didn't get the context and is
  213. * trying to increment its refcount.
  214. */
  215. }
  216. dec_ref();
  217. }
  218. bool stopPlayback{};
  219. /* First make sure this context exists in the device's list. */
  220. auto *oldarray = mDevice->mContexts.load(std::memory_order_acquire);
  221. if(auto toremove = static_cast<size_t>(std::count(oldarray->begin(), oldarray->end(), this)))
  222. {
  223. using ContextArray = al::FlexArray<ContextBase*>;
  224. const size_t newsize{oldarray->size() - toremove};
  225. auto newarray = ContextArray::Create(newsize);
  226. /* Copy the current/old context handles to the new array, excluding the
  227. * given context.
  228. */
  229. std::copy_if(oldarray->begin(), oldarray->end(), newarray->begin(),
  230. [this](ContextBase *ctx) { return ctx != this; });
  231. /* Store the new context array in the device. Wait for any current mix
  232. * to finish before deleting the old array.
  233. */
  234. auto prevarray = mDevice->mContexts.exchange(std::move(newarray));
  235. std::ignore = mDevice->waitForMix();
  236. stopPlayback = (newsize == 0);
  237. }
  238. else
  239. stopPlayback = oldarray->empty();
  240. StopEventThrd(this);
  241. if(stopPlayback && mALDevice->mDeviceState == DeviceState::Playing)
  242. {
  243. mALDevice->Backend->stop();
  244. mALDevice->mDeviceState = DeviceState::Configured;
  245. }
  246. }
  247. void ALCcontext::applyAllUpdates()
  248. {
  249. /* Tell the mixer to stop applying updates, then wait for any active
  250. * updating to finish, before providing updates.
  251. */
  252. mHoldUpdates.store(true, std::memory_order_release);
  253. while((mUpdateCount.load(std::memory_order_acquire)&1) != 0) {
  254. /* busy-wait */
  255. }
  256. #ifdef ALSOFT_EAX
  257. if(mEaxNeedsCommit)
  258. eaxCommit();
  259. #endif
  260. if(std::exchange(mPropsDirty, false))
  261. UpdateContextProps(this);
  262. UpdateAllEffectSlotProps(this);
  263. UpdateAllSourceProps(this);
  264. /* Now with all updates declared, let the mixer continue applying them so
  265. * they all happen at once.
  266. */
  267. mHoldUpdates.store(false, std::memory_order_release);
  268. }
  269. #ifdef ALSOFT_EAX
  270. namespace {
  271. template<typename F>
  272. void ForEachSource(ALCcontext *context, F func)
  273. {
  274. for(auto &sublist : context->mSourceList)
  275. {
  276. uint64_t usemask{~sublist.FreeMask};
  277. while(usemask)
  278. {
  279. const auto idx = static_cast<uint>(al::countr_zero(usemask));
  280. usemask &= ~(1_u64 << idx);
  281. func((*sublist.Sources)[idx]);
  282. }
  283. }
  284. }
  285. } // namespace
  286. bool ALCcontext::eaxIsCapable() const noexcept
  287. {
  288. return eax_has_enough_aux_sends();
  289. }
  290. void ALCcontext::eaxUninitialize() noexcept
  291. {
  292. if(!mEaxIsInitialized)
  293. return;
  294. mEaxIsInitialized = false;
  295. mEaxIsTried = false;
  296. mEaxFxSlots.uninitialize();
  297. }
  298. ALenum ALCcontext::eax_eax_set(
  299. const GUID* property_set_id,
  300. ALuint property_id,
  301. ALuint property_source_id,
  302. ALvoid* property_value,
  303. ALuint property_value_size)
  304. {
  305. const auto call = create_eax_call(
  306. EaxCallType::set,
  307. property_set_id,
  308. property_id,
  309. property_source_id,
  310. property_value,
  311. property_value_size);
  312. eax_initialize();
  313. switch(call.get_property_set_id())
  314. {
  315. case EaxCallPropertySetId::context:
  316. eax_set(call);
  317. break;
  318. case EaxCallPropertySetId::fx_slot:
  319. case EaxCallPropertySetId::fx_slot_effect:
  320. eax_dispatch_fx_slot(call);
  321. break;
  322. case EaxCallPropertySetId::source:
  323. eax_dispatch_source(call);
  324. break;
  325. default:
  326. eax_fail_unknown_property_set_id();
  327. }
  328. mEaxNeedsCommit = true;
  329. if(!call.is_deferred())
  330. {
  331. eaxCommit();
  332. if(!mDeferUpdates)
  333. applyAllUpdates();
  334. }
  335. return AL_NO_ERROR;
  336. }
  337. ALenum ALCcontext::eax_eax_get(
  338. const GUID* property_set_id,
  339. ALuint property_id,
  340. ALuint property_source_id,
  341. ALvoid* property_value,
  342. ALuint property_value_size)
  343. {
  344. const auto call = create_eax_call(
  345. EaxCallType::get,
  346. property_set_id,
  347. property_id,
  348. property_source_id,
  349. property_value,
  350. property_value_size);
  351. eax_initialize();
  352. switch(call.get_property_set_id())
  353. {
  354. case EaxCallPropertySetId::context:
  355. eax_get(call);
  356. break;
  357. case EaxCallPropertySetId::fx_slot:
  358. case EaxCallPropertySetId::fx_slot_effect:
  359. eax_dispatch_fx_slot(call);
  360. break;
  361. case EaxCallPropertySetId::source:
  362. eax_dispatch_source(call);
  363. break;
  364. default:
  365. eax_fail_unknown_property_set_id();
  366. }
  367. return AL_NO_ERROR;
  368. }
  369. void ALCcontext::eaxSetLastError() noexcept
  370. {
  371. mEaxLastError = EAXERR_INVALID_OPERATION;
  372. }
  373. [[noreturn]] void ALCcontext::eax_fail(const char* message)
  374. {
  375. throw ContextException{message};
  376. }
  377. [[noreturn]] void ALCcontext::eax_fail_unknown_property_set_id()
  378. {
  379. eax_fail("Unknown property ID.");
  380. }
  381. [[noreturn]] void ALCcontext::eax_fail_unknown_primary_fx_slot_id()
  382. {
  383. eax_fail("Unknown primary FX Slot ID.");
  384. }
  385. [[noreturn]] void ALCcontext::eax_fail_unknown_property_id()
  386. {
  387. eax_fail("Unknown property ID.");
  388. }
  389. [[noreturn]] void ALCcontext::eax_fail_unknown_version()
  390. {
  391. eax_fail("Unknown version.");
  392. }
  393. void ALCcontext::eax_initialize_extensions()
  394. {
  395. if(!eax_g_is_enabled)
  396. return;
  397. mExtensions.emplace(mExtensions.begin(), "EAX-RAM"sv);
  398. if(eaxIsCapable())
  399. {
  400. mExtensions.emplace(mExtensions.begin(), "EAX5.0"sv);
  401. mExtensions.emplace(mExtensions.begin(), "EAX4.0"sv);
  402. mExtensions.emplace(mExtensions.begin(), "EAX3.0"sv);
  403. mExtensions.emplace(mExtensions.begin(), "EAX2.0"sv);
  404. mExtensions.emplace(mExtensions.begin(), "EAX"sv);
  405. }
  406. }
  407. void ALCcontext::eax_initialize()
  408. {
  409. if(mEaxIsInitialized)
  410. return;
  411. if(mEaxIsTried)
  412. eax_fail("No EAX.");
  413. mEaxIsTried = true;
  414. if(!eax_g_is_enabled)
  415. eax_fail("EAX disabled by a configuration.");
  416. eax_ensure_compatibility();
  417. eax_set_defaults();
  418. eax_context_commit_air_absorbtion_hf();
  419. eax_update_speaker_configuration();
  420. eax_initialize_fx_slots();
  421. mEaxIsInitialized = true;
  422. }
  423. bool ALCcontext::eax_has_no_default_effect_slot() const noexcept
  424. {
  425. return mDefaultSlot == nullptr;
  426. }
  427. void ALCcontext::eax_ensure_no_default_effect_slot() const
  428. {
  429. if(!eax_has_no_default_effect_slot())
  430. eax_fail("There is a default effect slot in the context.");
  431. }
  432. bool ALCcontext::eax_has_enough_aux_sends() const noexcept
  433. {
  434. return mALDevice->NumAuxSends >= EAX_MAX_FXSLOTS;
  435. }
  436. void ALCcontext::eax_ensure_enough_aux_sends() const
  437. {
  438. if(!eax_has_enough_aux_sends())
  439. eax_fail("Not enough aux sends.");
  440. }
  441. void ALCcontext::eax_ensure_compatibility()
  442. {
  443. eax_ensure_enough_aux_sends();
  444. }
  445. unsigned long ALCcontext::eax_detect_speaker_configuration() const
  446. {
  447. #define EAX_PREFIX "[EAX_DETECT_SPEAKER_CONFIG]"
  448. switch(mDevice->FmtChans)
  449. {
  450. case DevFmtMono: return SPEAKERS_2;
  451. case DevFmtStereo:
  452. /* Pretend 7.1 if using UHJ output, since they both provide full
  453. * horizontal surround.
  454. */
  455. if(mDevice->mUhjEncoder)
  456. return SPEAKERS_7;
  457. if(mDevice->Flags.test(DirectEar))
  458. return HEADPHONES;
  459. return SPEAKERS_2;
  460. case DevFmtQuad: return SPEAKERS_4;
  461. case DevFmtX51: return SPEAKERS_5;
  462. case DevFmtX61: return SPEAKERS_6;
  463. case DevFmtX71: return SPEAKERS_7;
  464. /* 7.1.4(.4) is compatible with 7.1. This could instead be HEADPHONES to
  465. * suggest with-height surround sound (like HRTF).
  466. */
  467. case DevFmtX714: return SPEAKERS_7;
  468. case DevFmtX7144: return SPEAKERS_7;
  469. /* 3D7.1 is only compatible with 5.1. This could instead be HEADPHONES to
  470. * suggest full-sphere surround sound (like HRTF).
  471. */
  472. case DevFmtX3D71: return SPEAKERS_5;
  473. /* This could also be HEADPHONES, since headphones-based HRTF and Ambi3D
  474. * provide full-sphere surround sound. Depends if apps are more likely to
  475. * consider headphones or 7.1 for surround sound support.
  476. */
  477. case DevFmtAmbi3D: return SPEAKERS_7;
  478. }
  479. ERR(EAX_PREFIX "Unexpected device channel format 0x%x.\n", mDevice->FmtChans);
  480. return HEADPHONES;
  481. #undef EAX_PREFIX
  482. }
  483. void ALCcontext::eax_update_speaker_configuration()
  484. {
  485. mEaxSpeakerConfig = eax_detect_speaker_configuration();
  486. }
  487. void ALCcontext::eax_set_last_error_defaults() noexcept
  488. {
  489. mEaxLastError = EAXCONTEXT_DEFAULTLASTERROR;
  490. }
  491. void ALCcontext::eax_session_set_defaults() noexcept
  492. {
  493. mEaxSession.ulEAXVersion = EAXCONTEXT_DEFAULTEAXSESSION;
  494. mEaxSession.ulMaxActiveSends = EAXCONTEXT_DEFAULTMAXACTIVESENDS;
  495. }
  496. void ALCcontext::eax4_context_set_defaults(Eax4Props& props) noexcept
  497. {
  498. props.guidPrimaryFXSlotID = EAX40CONTEXT_DEFAULTPRIMARYFXSLOTID;
  499. props.flDistanceFactor = EAXCONTEXT_DEFAULTDISTANCEFACTOR;
  500. props.flAirAbsorptionHF = EAXCONTEXT_DEFAULTAIRABSORPTIONHF;
  501. props.flHFReference = EAXCONTEXT_DEFAULTHFREFERENCE;
  502. }
  503. void ALCcontext::eax4_context_set_defaults(Eax4State& state) noexcept
  504. {
  505. eax4_context_set_defaults(state.i);
  506. state.d = state.i;
  507. }
  508. void ALCcontext::eax5_context_set_defaults(Eax5Props& props) noexcept
  509. {
  510. props.guidPrimaryFXSlotID = EAX50CONTEXT_DEFAULTPRIMARYFXSLOTID;
  511. props.flDistanceFactor = EAXCONTEXT_DEFAULTDISTANCEFACTOR;
  512. props.flAirAbsorptionHF = EAXCONTEXT_DEFAULTAIRABSORPTIONHF;
  513. props.flHFReference = EAXCONTEXT_DEFAULTHFREFERENCE;
  514. props.flMacroFXFactor = EAXCONTEXT_DEFAULTMACROFXFACTOR;
  515. }
  516. void ALCcontext::eax5_context_set_defaults(Eax5State& state) noexcept
  517. {
  518. eax5_context_set_defaults(state.i);
  519. state.d = state.i;
  520. }
  521. void ALCcontext::eax_context_set_defaults()
  522. {
  523. eax5_context_set_defaults(mEax123);
  524. eax4_context_set_defaults(mEax4);
  525. eax5_context_set_defaults(mEax5);
  526. mEax = mEax5.i;
  527. mEaxVersion = 5;
  528. mEaxDf = EaxDirtyFlags{};
  529. }
  530. void ALCcontext::eax_set_defaults()
  531. {
  532. eax_set_last_error_defaults();
  533. eax_session_set_defaults();
  534. eax_context_set_defaults();
  535. }
  536. void ALCcontext::eax_dispatch_fx_slot(const EaxCall& call)
  537. {
  538. const auto fx_slot_index = call.get_fx_slot_index();
  539. if(!fx_slot_index.has_value())
  540. eax_fail("Invalid fx slot index.");
  541. auto& fx_slot = eaxGetFxSlot(*fx_slot_index);
  542. if(fx_slot.eax_dispatch(call))
  543. {
  544. std::lock_guard<std::mutex> source_lock{mSourceLock};
  545. ForEachSource(this, std::mem_fn(&ALsource::eaxMarkAsChanged));
  546. }
  547. }
  548. void ALCcontext::eax_dispatch_source(const EaxCall& call)
  549. {
  550. const auto source_id = call.get_property_al_name();
  551. std::lock_guard<std::mutex> source_lock{mSourceLock};
  552. const auto source = ALsource::EaxLookupSource(*this, source_id);
  553. if (source == nullptr)
  554. eax_fail("Source not found.");
  555. source->eaxDispatch(call);
  556. }
  557. void ALCcontext::eax_get_misc(const EaxCall& call)
  558. {
  559. switch(call.get_property_id())
  560. {
  561. case EAXCONTEXT_NONE:
  562. break;
  563. case EAXCONTEXT_LASTERROR:
  564. call.set_value<ContextException>(mEaxLastError);
  565. mEaxLastError = EAX_OK;
  566. break;
  567. case EAXCONTEXT_SPEAKERCONFIG:
  568. call.set_value<ContextException>(mEaxSpeakerConfig);
  569. break;
  570. case EAXCONTEXT_EAXSESSION:
  571. call.set_value<ContextException>(mEaxSession);
  572. break;
  573. default:
  574. eax_fail_unknown_property_id();
  575. }
  576. }
  577. void ALCcontext::eax4_get(const EaxCall& call, const Eax4Props& props)
  578. {
  579. switch(call.get_property_id())
  580. {
  581. case EAXCONTEXT_ALLPARAMETERS:
  582. call.set_value<ContextException>(props);
  583. break;
  584. case EAXCONTEXT_PRIMARYFXSLOTID:
  585. call.set_value<ContextException>(props.guidPrimaryFXSlotID);
  586. break;
  587. case EAXCONTEXT_DISTANCEFACTOR:
  588. call.set_value<ContextException>(props.flDistanceFactor);
  589. break;
  590. case EAXCONTEXT_AIRABSORPTIONHF:
  591. call.set_value<ContextException>(props.flAirAbsorptionHF);
  592. break;
  593. case EAXCONTEXT_HFREFERENCE:
  594. call.set_value<ContextException>(props.flHFReference);
  595. break;
  596. default:
  597. eax_get_misc(call);
  598. break;
  599. }
  600. }
  601. void ALCcontext::eax5_get(const EaxCall& call, const Eax5Props& props)
  602. {
  603. switch(call.get_property_id())
  604. {
  605. case EAXCONTEXT_ALLPARAMETERS:
  606. call.set_value<ContextException>(props);
  607. break;
  608. case EAXCONTEXT_PRIMARYFXSLOTID:
  609. call.set_value<ContextException>(props.guidPrimaryFXSlotID);
  610. break;
  611. case EAXCONTEXT_DISTANCEFACTOR:
  612. call.set_value<ContextException>(props.flDistanceFactor);
  613. break;
  614. case EAXCONTEXT_AIRABSORPTIONHF:
  615. call.set_value<ContextException>(props.flAirAbsorptionHF);
  616. break;
  617. case EAXCONTEXT_HFREFERENCE:
  618. call.set_value<ContextException>(props.flHFReference);
  619. break;
  620. case EAXCONTEXT_MACROFXFACTOR:
  621. call.set_value<ContextException>(props.flMacroFXFactor);
  622. break;
  623. default:
  624. eax_get_misc(call);
  625. break;
  626. }
  627. }
  628. void ALCcontext::eax_get(const EaxCall& call)
  629. {
  630. switch(call.get_version())
  631. {
  632. case 4: eax4_get(call, mEax4.i); break;
  633. case 5: eax5_get(call, mEax5.i); break;
  634. default: eax_fail_unknown_version();
  635. }
  636. }
  637. void ALCcontext::eax_context_commit_primary_fx_slot_id()
  638. {
  639. mEaxPrimaryFxSlotIndex = mEax.guidPrimaryFXSlotID;
  640. }
  641. void ALCcontext::eax_context_commit_distance_factor()
  642. {
  643. if(mListener.mMetersPerUnit == mEax.flDistanceFactor)
  644. return;
  645. mListener.mMetersPerUnit = mEax.flDistanceFactor;
  646. mPropsDirty = true;
  647. }
  648. void ALCcontext::eax_context_commit_air_absorbtion_hf()
  649. {
  650. const auto new_value = level_mb_to_gain(mEax.flAirAbsorptionHF);
  651. if(mAirAbsorptionGainHF == new_value)
  652. return;
  653. mAirAbsorptionGainHF = new_value;
  654. mPropsDirty = true;
  655. }
  656. void ALCcontext::eax_context_commit_hf_reference()
  657. {
  658. // TODO
  659. }
  660. void ALCcontext::eax_context_commit_macro_fx_factor()
  661. {
  662. // TODO
  663. }
  664. void ALCcontext::eax_initialize_fx_slots()
  665. {
  666. mEaxFxSlots.initialize(*this);
  667. mEaxPrimaryFxSlotIndex = mEax.guidPrimaryFXSlotID;
  668. }
  669. void ALCcontext::eax_update_sources()
  670. {
  671. std::unique_lock<std::mutex> source_lock{mSourceLock};
  672. auto update_source = [](ALsource &source)
  673. { source.eaxCommit(); };
  674. ForEachSource(this, update_source);
  675. }
  676. void ALCcontext::eax_set_misc(const EaxCall& call)
  677. {
  678. switch(call.get_property_id())
  679. {
  680. case EAXCONTEXT_NONE:
  681. break;
  682. case EAXCONTEXT_SPEAKERCONFIG:
  683. eax_set<Eax5SpeakerConfigValidator>(call, mEaxSpeakerConfig);
  684. break;
  685. case EAXCONTEXT_EAXSESSION:
  686. eax_set<Eax5SessionAllValidator>(call, mEaxSession);
  687. break;
  688. default:
  689. eax_fail_unknown_property_id();
  690. }
  691. }
  692. void ALCcontext::eax4_defer_all(const EaxCall& call, Eax4State& state)
  693. {
  694. const auto& src = call.get_value<ContextException, const EAX40CONTEXTPROPERTIES>();
  695. Eax4AllValidator{}(src);
  696. const auto& dst_i = state.i;
  697. auto& dst_d = state.d;
  698. dst_d = src;
  699. if(dst_i.guidPrimaryFXSlotID != dst_d.guidPrimaryFXSlotID)
  700. mEaxDf |= eax_primary_fx_slot_id_dirty_bit;
  701. if(dst_i.flDistanceFactor != dst_d.flDistanceFactor)
  702. mEaxDf |= eax_distance_factor_dirty_bit;
  703. if(dst_i.flAirAbsorptionHF != dst_d.flAirAbsorptionHF)
  704. mEaxDf |= eax_air_absorption_hf_dirty_bit;
  705. if(dst_i.flHFReference != dst_d.flHFReference)
  706. mEaxDf |= eax_hf_reference_dirty_bit;
  707. }
  708. void ALCcontext::eax4_defer(const EaxCall& call, Eax4State& state)
  709. {
  710. switch(call.get_property_id())
  711. {
  712. case EAXCONTEXT_ALLPARAMETERS:
  713. eax4_defer_all(call, state);
  714. break;
  715. case EAXCONTEXT_PRIMARYFXSLOTID:
  716. eax_defer<Eax4PrimaryFxSlotIdValidator, eax_primary_fx_slot_id_dirty_bit>(
  717. call, state, &EAX40CONTEXTPROPERTIES::guidPrimaryFXSlotID);
  718. break;
  719. case EAXCONTEXT_DISTANCEFACTOR:
  720. eax_defer<Eax4DistanceFactorValidator, eax_distance_factor_dirty_bit>(
  721. call, state, &EAX40CONTEXTPROPERTIES::flDistanceFactor);
  722. break;
  723. case EAXCONTEXT_AIRABSORPTIONHF:
  724. eax_defer<Eax4AirAbsorptionHfValidator, eax_air_absorption_hf_dirty_bit>(
  725. call, state, &EAX40CONTEXTPROPERTIES::flAirAbsorptionHF);
  726. break;
  727. case EAXCONTEXT_HFREFERENCE:
  728. eax_defer<Eax4HfReferenceValidator, eax_hf_reference_dirty_bit>(
  729. call, state, &EAX40CONTEXTPROPERTIES::flHFReference);
  730. break;
  731. default:
  732. eax_set_misc(call);
  733. break;
  734. }
  735. }
  736. void ALCcontext::eax5_defer_all(const EaxCall& call, Eax5State& state)
  737. {
  738. const auto& src = call.get_value<ContextException, const EAX50CONTEXTPROPERTIES>();
  739. Eax4AllValidator{}(src);
  740. const auto& dst_i = state.i;
  741. auto& dst_d = state.d;
  742. dst_d = src;
  743. if(dst_i.guidPrimaryFXSlotID != dst_d.guidPrimaryFXSlotID)
  744. mEaxDf |= eax_primary_fx_slot_id_dirty_bit;
  745. if(dst_i.flDistanceFactor != dst_d.flDistanceFactor)
  746. mEaxDf |= eax_distance_factor_dirty_bit;
  747. if(dst_i.flAirAbsorptionHF != dst_d.flAirAbsorptionHF)
  748. mEaxDf |= eax_air_absorption_hf_dirty_bit;
  749. if(dst_i.flHFReference != dst_d.flHFReference)
  750. mEaxDf |= eax_hf_reference_dirty_bit;
  751. if(dst_i.flMacroFXFactor != dst_d.flMacroFXFactor)
  752. mEaxDf |= eax_macro_fx_factor_dirty_bit;
  753. }
  754. void ALCcontext::eax5_defer(const EaxCall& call, Eax5State& state)
  755. {
  756. switch(call.get_property_id())
  757. {
  758. case EAXCONTEXT_ALLPARAMETERS:
  759. eax5_defer_all(call, state);
  760. break;
  761. case EAXCONTEXT_PRIMARYFXSLOTID:
  762. eax_defer<Eax5PrimaryFxSlotIdValidator, eax_primary_fx_slot_id_dirty_bit>(
  763. call, state, &EAX50CONTEXTPROPERTIES::guidPrimaryFXSlotID);
  764. break;
  765. case EAXCONTEXT_DISTANCEFACTOR:
  766. eax_defer<Eax4DistanceFactorValidator, eax_distance_factor_dirty_bit>(
  767. call, state, &EAX50CONTEXTPROPERTIES::flDistanceFactor);
  768. break;
  769. case EAXCONTEXT_AIRABSORPTIONHF:
  770. eax_defer<Eax4AirAbsorptionHfValidator, eax_air_absorption_hf_dirty_bit>(
  771. call, state, &EAX50CONTEXTPROPERTIES::flAirAbsorptionHF);
  772. break;
  773. case EAXCONTEXT_HFREFERENCE:
  774. eax_defer<Eax4HfReferenceValidator, eax_hf_reference_dirty_bit>(
  775. call, state, &EAX50CONTEXTPROPERTIES::flHFReference);
  776. break;
  777. case EAXCONTEXT_MACROFXFACTOR:
  778. eax_defer<Eax5MacroFxFactorValidator, eax_macro_fx_factor_dirty_bit>(
  779. call, state, &EAX50CONTEXTPROPERTIES::flMacroFXFactor);
  780. break;
  781. default:
  782. eax_set_misc(call);
  783. break;
  784. }
  785. }
  786. void ALCcontext::eax_set(const EaxCall& call)
  787. {
  788. const auto version = call.get_version();
  789. switch(version)
  790. {
  791. case 4: eax4_defer(call, mEax4); break;
  792. case 5: eax5_defer(call, mEax5); break;
  793. default: eax_fail_unknown_version();
  794. }
  795. if(version != mEaxVersion)
  796. mEaxDf = ~EaxDirtyFlags();
  797. mEaxVersion = version;
  798. }
  799. void ALCcontext::eax4_context_commit(Eax4State& state, EaxDirtyFlags& dst_df)
  800. {
  801. if(mEaxDf == EaxDirtyFlags{})
  802. return;
  803. eax_context_commit_property<eax_primary_fx_slot_id_dirty_bit>(
  804. state, dst_df, &EAX40CONTEXTPROPERTIES::guidPrimaryFXSlotID);
  805. eax_context_commit_property<eax_distance_factor_dirty_bit>(
  806. state, dst_df, &EAX40CONTEXTPROPERTIES::flDistanceFactor);
  807. eax_context_commit_property<eax_air_absorption_hf_dirty_bit>(
  808. state, dst_df, &EAX40CONTEXTPROPERTIES::flAirAbsorptionHF);
  809. eax_context_commit_property<eax_hf_reference_dirty_bit>(
  810. state, dst_df, &EAX40CONTEXTPROPERTIES::flHFReference);
  811. mEaxDf = EaxDirtyFlags{};
  812. }
  813. void ALCcontext::eax5_context_commit(Eax5State& state, EaxDirtyFlags& dst_df)
  814. {
  815. if(mEaxDf == EaxDirtyFlags{})
  816. return;
  817. eax_context_commit_property<eax_primary_fx_slot_id_dirty_bit>(
  818. state, dst_df, &EAX50CONTEXTPROPERTIES::guidPrimaryFXSlotID);
  819. eax_context_commit_property<eax_distance_factor_dirty_bit>(
  820. state, dst_df, &EAX50CONTEXTPROPERTIES::flDistanceFactor);
  821. eax_context_commit_property<eax_air_absorption_hf_dirty_bit>(
  822. state, dst_df, &EAX50CONTEXTPROPERTIES::flAirAbsorptionHF);
  823. eax_context_commit_property<eax_hf_reference_dirty_bit>(
  824. state, dst_df, &EAX50CONTEXTPROPERTIES::flHFReference);
  825. eax_context_commit_property<eax_macro_fx_factor_dirty_bit>(
  826. state, dst_df, &EAX50CONTEXTPROPERTIES::flMacroFXFactor);
  827. mEaxDf = EaxDirtyFlags{};
  828. }
  829. void ALCcontext::eax_context_commit()
  830. {
  831. auto dst_df = EaxDirtyFlags{};
  832. switch(mEaxVersion)
  833. {
  834. case 1:
  835. case 2:
  836. case 3:
  837. eax5_context_commit(mEax123, dst_df);
  838. break;
  839. case 4:
  840. eax4_context_commit(mEax4, dst_df);
  841. break;
  842. case 5:
  843. eax5_context_commit(mEax5, dst_df);
  844. break;
  845. }
  846. if(dst_df == EaxDirtyFlags{})
  847. return;
  848. if((dst_df & eax_primary_fx_slot_id_dirty_bit) != EaxDirtyFlags{})
  849. eax_context_commit_primary_fx_slot_id();
  850. if((dst_df & eax_distance_factor_dirty_bit) != EaxDirtyFlags{})
  851. eax_context_commit_distance_factor();
  852. if((dst_df & eax_air_absorption_hf_dirty_bit) != EaxDirtyFlags{})
  853. eax_context_commit_air_absorbtion_hf();
  854. if((dst_df & eax_hf_reference_dirty_bit) != EaxDirtyFlags{})
  855. eax_context_commit_hf_reference();
  856. if((dst_df & eax_macro_fx_factor_dirty_bit) != EaxDirtyFlags{})
  857. eax_context_commit_macro_fx_factor();
  858. if((dst_df & eax_primary_fx_slot_id_dirty_bit) != EaxDirtyFlags{})
  859. eax_update_sources();
  860. }
  861. void ALCcontext::eaxCommit()
  862. {
  863. mEaxNeedsCommit = false;
  864. eax_context_commit();
  865. eaxCommitFxSlots();
  866. eax_update_sources();
  867. }
  868. FORCE_ALIGN auto AL_APIENTRY EAXSet(const GUID *property_set_id, ALuint property_id,
  869. ALuint source_id, ALvoid *value, ALuint value_size) noexcept -> ALenum
  870. {
  871. auto context = GetContextRef();
  872. if(!context) UNLIKELY return AL_INVALID_OPERATION;
  873. return EAXSetDirect(context.get(), property_set_id, property_id, source_id, value, value_size);
  874. }
  875. FORCE_ALIGN auto AL_APIENTRY EAXSetDirect(ALCcontext *context, const GUID *property_set_id,
  876. ALuint property_id, ALuint source_id, ALvoid *value, ALuint value_size) noexcept -> ALenum
  877. try
  878. {
  879. std::lock_guard<std::mutex> prop_lock{context->mPropLock};
  880. return context->eax_eax_set(property_set_id, property_id, source_id, value, value_size);
  881. }
  882. catch(...)
  883. {
  884. context->eaxSetLastError();
  885. eax_log_exception(std::data(__func__));
  886. return AL_INVALID_OPERATION;
  887. }
  888. FORCE_ALIGN auto AL_APIENTRY EAXGet(const GUID *property_set_id, ALuint property_id,
  889. ALuint source_id, ALvoid *value, ALuint value_size) noexcept -> ALenum
  890. {
  891. auto context = GetContextRef();
  892. if(!context) UNLIKELY return AL_INVALID_OPERATION;
  893. return EAXGetDirect(context.get(), property_set_id, property_id, source_id, value, value_size);
  894. }
  895. FORCE_ALIGN auto AL_APIENTRY EAXGetDirect(ALCcontext *context, const GUID *property_set_id,
  896. ALuint property_id, ALuint source_id, ALvoid *value, ALuint value_size) noexcept -> ALenum
  897. try
  898. {
  899. std::lock_guard<std::mutex> prop_lock{context->mPropLock};
  900. return context->eax_eax_get(property_set_id, property_id, source_id, value, value_size);
  901. }
  902. catch(...)
  903. {
  904. context->eaxSetLastError();
  905. eax_log_exception(std::data(__func__));
  906. return AL_INVALID_OPERATION;
  907. }
  908. #endif // ALSOFT_EAX