alAuxEffectSlot.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715
  1. /**
  2. * OpenAL cross platform audio library
  3. * Copyright (C) 1999-2007 by authors.
  4. * This library is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Library General Public
  6. * License as published by the Free Software Foundation; either
  7. * version 2 of the License, or (at your option) any later version.
  8. *
  9. * This library is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Library General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Library General Public
  15. * License along with this library; if not, write to the
  16. * Free Software Foundation, Inc.,
  17. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  18. * Or go to http://www.gnu.org/copyleft/lgpl.html
  19. */
  20. #include "config.h"
  21. #include <stdlib.h>
  22. #include <math.h>
  23. #include "AL/al.h"
  24. #include "AL/alc.h"
  25. #include "alMain.h"
  26. #include "alAuxEffectSlot.h"
  27. #include "alThunk.h"
  28. #include "alError.h"
  29. #include "alListener.h"
  30. #include "alSource.h"
  31. #include "almalloc.h"
  32. extern inline void LockEffectSlotsRead(ALCcontext *context);
  33. extern inline void UnlockEffectSlotsRead(ALCcontext *context);
  34. extern inline void LockEffectSlotsWrite(ALCcontext *context);
  35. extern inline void UnlockEffectSlotsWrite(ALCcontext *context);
  36. extern inline struct ALeffectslot *LookupEffectSlot(ALCcontext *context, ALuint id);
  37. extern inline struct ALeffectslot *RemoveEffectSlot(ALCcontext *context, ALuint id);
  38. static void RemoveEffectSlotList(ALCcontext *Context, ALeffectslot *slot);
  39. static UIntMap EffectStateFactoryMap;
  40. static inline ALeffectStateFactory *getFactoryByType(ALenum type)
  41. {
  42. ALeffectStateFactory* (*getFactory)(void) = LookupUIntMapKey(&EffectStateFactoryMap, type);
  43. if(getFactory != NULL)
  44. return getFactory();
  45. return NULL;
  46. }
  47. static void ALeffectState_IncRef(ALeffectState *state);
  48. static void ALeffectState_DecRef(ALeffectState *state);
  49. #define DO_UPDATEPROPS() do { \
  50. if(!ATOMIC_LOAD(&context->DeferUpdates, almemory_order_acquire)) \
  51. UpdateEffectSlotProps(slot); \
  52. else \
  53. slot->NeedsUpdate = AL_TRUE; \
  54. } while(0)
  55. AL_API ALvoid AL_APIENTRY alGenAuxiliaryEffectSlots(ALsizei n, ALuint *effectslots)
  56. {
  57. ALCcontext *context;
  58. ALeffectslot *first, *last;
  59. ALsizei cur;
  60. ALenum err;
  61. context = GetContextRef();
  62. if(!context) return;
  63. if(!(n >= 0))
  64. SET_ERROR_AND_GOTO(context, AL_INVALID_VALUE, done);
  65. first = last = NULL;
  66. for(cur = 0;cur < n;cur++)
  67. {
  68. ALeffectslot *slot = al_calloc(16, sizeof(ALeffectslot));
  69. err = AL_OUT_OF_MEMORY;
  70. if(!slot || (err=InitEffectSlot(slot)) != AL_NO_ERROR)
  71. {
  72. al_free(slot);
  73. alDeleteAuxiliaryEffectSlots(cur, effectslots);
  74. SET_ERROR_AND_GOTO(context, err, done);
  75. }
  76. err = NewThunkEntry(&slot->id);
  77. if(err == AL_NO_ERROR)
  78. err = InsertUIntMapEntry(&context->EffectSlotMap, slot->id, slot);
  79. if(err != AL_NO_ERROR)
  80. {
  81. FreeThunkEntry(slot->id);
  82. ALeffectState_DecRef(slot->Effect.State);
  83. if(slot->Params.EffectState)
  84. ALeffectState_DecRef(slot->Params.EffectState);
  85. al_free(slot);
  86. alDeleteAuxiliaryEffectSlots(cur, effectslots);
  87. SET_ERROR_AND_GOTO(context, err, done);
  88. }
  89. aluInitEffectPanning(slot);
  90. if(!first) first = slot;
  91. if(last) ATOMIC_STORE(&last->next, slot, almemory_order_relaxed);
  92. last = slot;
  93. effectslots[cur] = slot->id;
  94. }
  95. if(last != NULL)
  96. {
  97. ALeffectslot *root = ATOMIC_LOAD(&context->ActiveAuxSlotList);
  98. do {
  99. ATOMIC_STORE(&last->next, root, almemory_order_relaxed);
  100. } while(!ATOMIC_COMPARE_EXCHANGE_WEAK(ALeffectslot*, &context->ActiveAuxSlotList,
  101. &root, first));
  102. }
  103. done:
  104. ALCcontext_DecRef(context);
  105. }
  106. AL_API ALvoid AL_APIENTRY alDeleteAuxiliaryEffectSlots(ALsizei n, const ALuint *effectslots)
  107. {
  108. ALCcontext *context;
  109. ALeffectslot *slot;
  110. ALsizei i;
  111. context = GetContextRef();
  112. if(!context) return;
  113. LockEffectSlotsWrite(context);
  114. if(!(n >= 0))
  115. SET_ERROR_AND_GOTO(context, AL_INVALID_VALUE, done);
  116. for(i = 0;i < n;i++)
  117. {
  118. if((slot=LookupEffectSlot(context, effectslots[i])) == NULL)
  119. SET_ERROR_AND_GOTO(context, AL_INVALID_NAME, done);
  120. if(ReadRef(&slot->ref) != 0)
  121. SET_ERROR_AND_GOTO(context, AL_INVALID_OPERATION, done);
  122. }
  123. // All effectslots are valid
  124. for(i = 0;i < n;i++)
  125. {
  126. if((slot=RemoveEffectSlot(context, effectslots[i])) == NULL)
  127. continue;
  128. FreeThunkEntry(slot->id);
  129. RemoveEffectSlotList(context, slot);
  130. DeinitEffectSlot(slot);
  131. memset(slot, 0, sizeof(*slot));
  132. al_free(slot);
  133. }
  134. done:
  135. UnlockEffectSlotsWrite(context);
  136. ALCcontext_DecRef(context);
  137. }
  138. AL_API ALboolean AL_APIENTRY alIsAuxiliaryEffectSlot(ALuint effectslot)
  139. {
  140. ALCcontext *context;
  141. ALboolean ret;
  142. context = GetContextRef();
  143. if(!context) return AL_FALSE;
  144. LockEffectSlotsRead(context);
  145. ret = (LookupEffectSlot(context, effectslot) ? AL_TRUE : AL_FALSE);
  146. UnlockEffectSlotsRead(context);
  147. ALCcontext_DecRef(context);
  148. return ret;
  149. }
  150. AL_API ALvoid AL_APIENTRY alAuxiliaryEffectSloti(ALuint effectslot, ALenum param, ALint value)
  151. {
  152. ALCdevice *device;
  153. ALCcontext *context;
  154. ALeffectslot *slot;
  155. ALeffect *effect = NULL;
  156. ALenum err;
  157. context = GetContextRef();
  158. if(!context) return;
  159. WriteLock(&context->PropLock);
  160. LockEffectSlotsRead(context);
  161. if((slot=LookupEffectSlot(context, effectslot)) == NULL)
  162. SET_ERROR_AND_GOTO(context, AL_INVALID_NAME, done);
  163. switch(param)
  164. {
  165. case AL_EFFECTSLOT_EFFECT:
  166. device = context->Device;
  167. LockEffectsRead(device);
  168. effect = (value ? LookupEffect(device, value) : NULL);
  169. if(!(value == 0 || effect != NULL))
  170. {
  171. UnlockEffectsRead(device);
  172. SET_ERROR_AND_GOTO(context, AL_INVALID_VALUE, done);
  173. }
  174. err = InitializeEffect(device, slot, effect);
  175. UnlockEffectsRead(device);
  176. if(err != AL_NO_ERROR)
  177. SET_ERROR_AND_GOTO(context, err, done);
  178. break;
  179. case AL_EFFECTSLOT_AUXILIARY_SEND_AUTO:
  180. if(!(value == AL_TRUE || value == AL_FALSE))
  181. SET_ERROR_AND_GOTO(context, AL_INVALID_VALUE, done);
  182. slot->AuxSendAuto = value;
  183. break;
  184. default:
  185. SET_ERROR_AND_GOTO(context, AL_INVALID_ENUM, done);
  186. }
  187. DO_UPDATEPROPS();
  188. done:
  189. UnlockEffectSlotsRead(context);
  190. WriteUnlock(&context->PropLock);
  191. ALCcontext_DecRef(context);
  192. }
  193. AL_API ALvoid AL_APIENTRY alAuxiliaryEffectSlotiv(ALuint effectslot, ALenum param, const ALint *values)
  194. {
  195. ALCcontext *context;
  196. switch(param)
  197. {
  198. case AL_EFFECTSLOT_EFFECT:
  199. case AL_EFFECTSLOT_AUXILIARY_SEND_AUTO:
  200. alAuxiliaryEffectSloti(effectslot, param, values[0]);
  201. return;
  202. }
  203. context = GetContextRef();
  204. if(!context) return;
  205. LockEffectSlotsRead(context);
  206. if(LookupEffectSlot(context, effectslot) == NULL)
  207. SET_ERROR_AND_GOTO(context, AL_INVALID_NAME, done);
  208. switch(param)
  209. {
  210. default:
  211. SET_ERROR_AND_GOTO(context, AL_INVALID_ENUM, done);
  212. }
  213. done:
  214. UnlockEffectSlotsRead(context);
  215. ALCcontext_DecRef(context);
  216. }
  217. AL_API ALvoid AL_APIENTRY alAuxiliaryEffectSlotf(ALuint effectslot, ALenum param, ALfloat value)
  218. {
  219. ALCcontext *context;
  220. ALeffectslot *slot;
  221. context = GetContextRef();
  222. if(!context) return;
  223. WriteLock(&context->PropLock);
  224. LockEffectSlotsRead(context);
  225. if((slot=LookupEffectSlot(context, effectslot)) == NULL)
  226. SET_ERROR_AND_GOTO(context, AL_INVALID_NAME, done);
  227. switch(param)
  228. {
  229. case AL_EFFECTSLOT_GAIN:
  230. if(!(value >= 0.0f && value <= 1.0f))
  231. SET_ERROR_AND_GOTO(context, AL_INVALID_VALUE, done);
  232. slot->Gain = value;
  233. break;
  234. default:
  235. SET_ERROR_AND_GOTO(context, AL_INVALID_ENUM, done);
  236. }
  237. DO_UPDATEPROPS();
  238. done:
  239. UnlockEffectSlotsRead(context);
  240. WriteUnlock(&context->PropLock);
  241. ALCcontext_DecRef(context);
  242. }
  243. AL_API ALvoid AL_APIENTRY alAuxiliaryEffectSlotfv(ALuint effectslot, ALenum param, const ALfloat *values)
  244. {
  245. ALCcontext *context;
  246. switch(param)
  247. {
  248. case AL_EFFECTSLOT_GAIN:
  249. alAuxiliaryEffectSlotf(effectslot, param, values[0]);
  250. return;
  251. }
  252. context = GetContextRef();
  253. if(!context) return;
  254. LockEffectSlotsRead(context);
  255. if(LookupEffectSlot(context, effectslot) == NULL)
  256. SET_ERROR_AND_GOTO(context, AL_INVALID_NAME, done);
  257. switch(param)
  258. {
  259. default:
  260. SET_ERROR_AND_GOTO(context, AL_INVALID_ENUM, done);
  261. }
  262. done:
  263. UnlockEffectSlotsRead(context);
  264. ALCcontext_DecRef(context);
  265. }
  266. AL_API ALvoid AL_APIENTRY alGetAuxiliaryEffectSloti(ALuint effectslot, ALenum param, ALint *value)
  267. {
  268. ALCcontext *context;
  269. ALeffectslot *slot;
  270. context = GetContextRef();
  271. if(!context) return;
  272. LockEffectSlotsRead(context);
  273. if((slot=LookupEffectSlot(context, effectslot)) == NULL)
  274. SET_ERROR_AND_GOTO(context, AL_INVALID_NAME, done);
  275. switch(param)
  276. {
  277. case AL_EFFECTSLOT_AUXILIARY_SEND_AUTO:
  278. *value = slot->AuxSendAuto;
  279. break;
  280. default:
  281. SET_ERROR_AND_GOTO(context, AL_INVALID_ENUM, done);
  282. }
  283. done:
  284. UnlockEffectSlotsRead(context);
  285. ALCcontext_DecRef(context);
  286. }
  287. AL_API ALvoid AL_APIENTRY alGetAuxiliaryEffectSlotiv(ALuint effectslot, ALenum param, ALint *values)
  288. {
  289. ALCcontext *context;
  290. switch(param)
  291. {
  292. case AL_EFFECTSLOT_EFFECT:
  293. case AL_EFFECTSLOT_AUXILIARY_SEND_AUTO:
  294. alGetAuxiliaryEffectSloti(effectslot, param, values);
  295. return;
  296. }
  297. context = GetContextRef();
  298. if(!context) return;
  299. LockEffectSlotsRead(context);
  300. if(LookupEffectSlot(context, effectslot) == NULL)
  301. SET_ERROR_AND_GOTO(context, AL_INVALID_NAME, done);
  302. switch(param)
  303. {
  304. default:
  305. SET_ERROR_AND_GOTO(context, AL_INVALID_ENUM, done);
  306. }
  307. done:
  308. UnlockEffectSlotsRead(context);
  309. ALCcontext_DecRef(context);
  310. }
  311. AL_API ALvoid AL_APIENTRY alGetAuxiliaryEffectSlotf(ALuint effectslot, ALenum param, ALfloat *value)
  312. {
  313. ALCcontext *context;
  314. ALeffectslot *slot;
  315. context = GetContextRef();
  316. if(!context) return;
  317. LockEffectSlotsRead(context);
  318. if((slot=LookupEffectSlot(context, effectslot)) == NULL)
  319. SET_ERROR_AND_GOTO(context, AL_INVALID_NAME, done);
  320. switch(param)
  321. {
  322. case AL_EFFECTSLOT_GAIN:
  323. *value = slot->Gain;
  324. break;
  325. default:
  326. SET_ERROR_AND_GOTO(context, AL_INVALID_ENUM, done);
  327. }
  328. done:
  329. UnlockEffectSlotsRead(context);
  330. ALCcontext_DecRef(context);
  331. }
  332. AL_API ALvoid AL_APIENTRY alGetAuxiliaryEffectSlotfv(ALuint effectslot, ALenum param, ALfloat *values)
  333. {
  334. ALCcontext *context;
  335. switch(param)
  336. {
  337. case AL_EFFECTSLOT_GAIN:
  338. alGetAuxiliaryEffectSlotf(effectslot, param, values);
  339. return;
  340. }
  341. context = GetContextRef();
  342. if(!context) return;
  343. LockEffectSlotsRead(context);
  344. if(LookupEffectSlot(context, effectslot) == NULL)
  345. SET_ERROR_AND_GOTO(context, AL_INVALID_NAME, done);
  346. switch(param)
  347. {
  348. default:
  349. SET_ERROR_AND_GOTO(context, AL_INVALID_ENUM, done);
  350. }
  351. done:
  352. UnlockEffectSlotsRead(context);
  353. ALCcontext_DecRef(context);
  354. }
  355. static void RemoveEffectSlotList(ALCcontext *context, ALeffectslot *slot)
  356. {
  357. ALCdevice *device = context->Device;
  358. ALeffectslot *root, *next;
  359. root = slot;
  360. next = ATOMIC_LOAD(&slot->next);
  361. if(!ATOMIC_COMPARE_EXCHANGE_STRONG(ALeffectslot*, &context->ActiveAuxSlotList, &root, next))
  362. {
  363. ALeffectslot *cur;
  364. do {
  365. cur = root;
  366. root = slot;
  367. } while(!ATOMIC_COMPARE_EXCHANGE_STRONG(ALeffectslot*, &cur->next, &root, next));
  368. }
  369. /* Wait for any mix that may be using these effect slots to finish. */
  370. while((ReadRef(&device->MixCount)&1) != 0)
  371. althrd_yield();
  372. }
  373. void InitEffectFactoryMap(void)
  374. {
  375. InitUIntMap(&EffectStateFactoryMap, ~0);
  376. InsertUIntMapEntry(&EffectStateFactoryMap, AL_EFFECT_NULL, ALnullStateFactory_getFactory);
  377. InsertUIntMapEntry(&EffectStateFactoryMap, AL_EFFECT_EAXREVERB, ALreverbStateFactory_getFactory);
  378. InsertUIntMapEntry(&EffectStateFactoryMap, AL_EFFECT_REVERB, ALreverbStateFactory_getFactory);
  379. InsertUIntMapEntry(&EffectStateFactoryMap, AL_EFFECT_CHORUS, ALchorusStateFactory_getFactory);
  380. InsertUIntMapEntry(&EffectStateFactoryMap, AL_EFFECT_COMPRESSOR, ALcompressorStateFactory_getFactory);
  381. InsertUIntMapEntry(&EffectStateFactoryMap, AL_EFFECT_DISTORTION, ALdistortionStateFactory_getFactory);
  382. InsertUIntMapEntry(&EffectStateFactoryMap, AL_EFFECT_ECHO, ALechoStateFactory_getFactory);
  383. InsertUIntMapEntry(&EffectStateFactoryMap, AL_EFFECT_EQUALIZER, ALequalizerStateFactory_getFactory);
  384. InsertUIntMapEntry(&EffectStateFactoryMap, AL_EFFECT_FLANGER, ALflangerStateFactory_getFactory);
  385. InsertUIntMapEntry(&EffectStateFactoryMap, AL_EFFECT_RING_MODULATOR, ALmodulatorStateFactory_getFactory);
  386. InsertUIntMapEntry(&EffectStateFactoryMap, AL_EFFECT_DEDICATED_DIALOGUE, ALdedicatedStateFactory_getFactory);
  387. InsertUIntMapEntry(&EffectStateFactoryMap, AL_EFFECT_DEDICATED_LOW_FREQUENCY_EFFECT, ALdedicatedStateFactory_getFactory);
  388. }
  389. void DeinitEffectFactoryMap(void)
  390. {
  391. ResetUIntMap(&EffectStateFactoryMap);
  392. }
  393. ALenum InitializeEffect(ALCdevice *Device, ALeffectslot *EffectSlot, ALeffect *effect)
  394. {
  395. ALenum newtype = (effect ? effect->type : AL_EFFECT_NULL);
  396. struct ALeffectslotProps *props;
  397. ALeffectState *State;
  398. if(newtype != EffectSlot->Effect.Type)
  399. {
  400. ALeffectStateFactory *factory;
  401. FPUCtl oldMode;
  402. factory = getFactoryByType(newtype);
  403. if(!factory)
  404. {
  405. ERR("Failed to find factory for effect type 0x%04x\n", newtype);
  406. return AL_INVALID_ENUM;
  407. }
  408. State = V0(factory,create)();
  409. if(!State) return AL_OUT_OF_MEMORY;
  410. SetMixerFPUMode(&oldMode);
  411. almtx_lock(&Device->BackendLock);
  412. State->OutBuffer = Device->Dry.Buffer;
  413. State->OutChannels = Device->Dry.NumChannels;
  414. if(V(State,deviceUpdate)(Device) == AL_FALSE)
  415. {
  416. almtx_unlock(&Device->BackendLock);
  417. RestoreFPUMode(&oldMode);
  418. ALeffectState_DecRef(State);
  419. return AL_OUT_OF_MEMORY;
  420. }
  421. almtx_unlock(&Device->BackendLock);
  422. RestoreFPUMode(&oldMode);
  423. if(!effect)
  424. {
  425. EffectSlot->Effect.Type = AL_EFFECT_NULL;
  426. memset(&EffectSlot->Effect.Props, 0, sizeof(EffectSlot->Effect.Props));
  427. }
  428. else
  429. {
  430. EffectSlot->Effect.Type = effect->type;
  431. EffectSlot->Effect.Props = effect->Props;
  432. }
  433. ALeffectState_DecRef(EffectSlot->Effect.State);
  434. EffectSlot->Effect.State = State;
  435. }
  436. else if(effect)
  437. EffectSlot->Effect.Props = effect->Props;
  438. /* Remove state references from old effect slot property updates. */
  439. props = ATOMIC_LOAD(&EffectSlot->FreeList);
  440. while(props)
  441. {
  442. State = ATOMIC_EXCHANGE(ALeffectState*, &props->State, NULL, almemory_order_relaxed);
  443. if(State) ALeffectState_DecRef(State);
  444. props = ATOMIC_LOAD(&props->next, almemory_order_relaxed);
  445. }
  446. return AL_NO_ERROR;
  447. }
  448. static void ALeffectState_IncRef(ALeffectState *state)
  449. {
  450. uint ref;
  451. ref = IncrementRef(&state->Ref);
  452. TRACEREF("%p increasing refcount to %u\n", state, ref);
  453. }
  454. static void ALeffectState_DecRef(ALeffectState *state)
  455. {
  456. uint ref;
  457. ref = DecrementRef(&state->Ref);
  458. TRACEREF("%p decreasing refcount to %u\n", state, ref);
  459. if(ref == 0) DELETE_OBJ(state);
  460. }
  461. void ALeffectState_Construct(ALeffectState *state)
  462. {
  463. InitRef(&state->Ref, 1);
  464. state->OutBuffer = NULL;
  465. state->OutChannels = 0;
  466. }
  467. void ALeffectState_Destruct(ALeffectState *UNUSED(state))
  468. {
  469. }
  470. ALenum InitEffectSlot(ALeffectslot *slot)
  471. {
  472. ALeffectStateFactory *factory;
  473. slot->Effect.Type = AL_EFFECT_NULL;
  474. factory = getFactoryByType(AL_EFFECT_NULL);
  475. if(!(slot->Effect.State=V0(factory,create)()))
  476. return AL_OUT_OF_MEMORY;
  477. slot->NeedsUpdate = AL_FALSE;
  478. slot->Gain = 1.0;
  479. slot->AuxSendAuto = AL_TRUE;
  480. InitRef(&slot->ref, 0);
  481. ATOMIC_INIT(&slot->Update, NULL);
  482. ATOMIC_INIT(&slot->FreeList, NULL);
  483. slot->Params.Gain = 1.0f;
  484. slot->Params.AuxSendAuto = AL_TRUE;
  485. ALeffectState_IncRef(slot->Effect.State);
  486. slot->Params.EffectState = slot->Effect.State;
  487. slot->Params.RoomRolloff = 0.0f;
  488. slot->Params.DecayTime = 0.0f;
  489. slot->Params.AirAbsorptionGainHF = 1.0f;
  490. ATOMIC_INIT(&slot->next, NULL);
  491. return AL_NO_ERROR;
  492. }
  493. void DeinitEffectSlot(ALeffectslot *slot)
  494. {
  495. struct ALeffectslotProps *props;
  496. ALeffectState *state;
  497. size_t count = 0;
  498. props = ATOMIC_LOAD(&slot->Update);
  499. if(props)
  500. {
  501. state = ATOMIC_LOAD(&props->State, almemory_order_relaxed);
  502. if(state) ALeffectState_DecRef(state);
  503. TRACE("Freed unapplied AuxiliaryEffectSlot update %p\n", props);
  504. al_free(props);
  505. }
  506. props = ATOMIC_LOAD(&slot->FreeList, almemory_order_relaxed);
  507. while(props)
  508. {
  509. struct ALeffectslotProps *next;
  510. state = ATOMIC_LOAD(&props->State, almemory_order_relaxed);
  511. next = ATOMIC_LOAD(&props->next, almemory_order_relaxed);
  512. if(state) ALeffectState_DecRef(state);
  513. al_free(props);
  514. props = next;
  515. ++count;
  516. }
  517. TRACE("Freed "SZFMT" AuxiliaryEffectSlot property object%s\n", count, (count==1)?"":"s");
  518. ALeffectState_DecRef(slot->Effect.State);
  519. if(slot->Params.EffectState)
  520. ALeffectState_DecRef(slot->Params.EffectState);
  521. }
  522. void UpdateEffectSlotProps(ALeffectslot *slot)
  523. {
  524. struct ALeffectslotProps *props;
  525. ALeffectState *oldstate;
  526. /* Get an unused property container, or allocate a new one as needed. */
  527. props = ATOMIC_LOAD(&slot->FreeList, almemory_order_relaxed);
  528. if(!props)
  529. props = al_calloc(16, sizeof(*props));
  530. else
  531. {
  532. struct ALeffectslotProps *next;
  533. do {
  534. next = ATOMIC_LOAD(&props->next, almemory_order_relaxed);
  535. } while(ATOMIC_COMPARE_EXCHANGE_WEAK(struct ALeffectslotProps*,
  536. &slot->FreeList, &props, next, almemory_order_seq_cst,
  537. almemory_order_consume) == 0);
  538. }
  539. /* Copy in current property values. */
  540. ATOMIC_STORE(&props->Gain, slot->Gain, almemory_order_relaxed);
  541. ATOMIC_STORE(&props->AuxSendAuto, slot->AuxSendAuto, almemory_order_relaxed);
  542. ATOMIC_STORE(&props->Type, slot->Effect.Type, almemory_order_relaxed);
  543. props->Props = slot->Effect.Props;
  544. /* Swap out any stale effect state object there may be in the container, to
  545. * delete it.
  546. */
  547. ALeffectState_IncRef(slot->Effect.State);
  548. oldstate = ATOMIC_EXCHANGE(ALeffectState*, &props->State, slot->Effect.State,
  549. almemory_order_relaxed);
  550. /* Set the new container for updating internal parameters. */
  551. props = ATOMIC_EXCHANGE(struct ALeffectslotProps*, &slot->Update, props,
  552. almemory_order_acq_rel);
  553. if(props)
  554. {
  555. /* If there was an unused update container, put it back in the
  556. * freelist.
  557. */
  558. struct ALeffectslotProps *first = ATOMIC_LOAD(&slot->FreeList);
  559. do {
  560. ATOMIC_STORE(&props->next, first, almemory_order_relaxed);
  561. } while(ATOMIC_COMPARE_EXCHANGE_WEAK(struct ALeffectslotProps*,
  562. &slot->FreeList, &first, props) == 0);
  563. }
  564. if(oldstate)
  565. ALeffectState_DecRef(oldstate);
  566. }
  567. void UpdateAllEffectSlotProps(ALCcontext *context)
  568. {
  569. ALeffectslot *slot;
  570. LockEffectSlotsRead(context);
  571. slot = ATOMIC_LOAD(&context->ActiveAuxSlotList);
  572. while(slot)
  573. {
  574. if(slot->NeedsUpdate)
  575. UpdateEffectSlotProps(slot);
  576. slot->NeedsUpdate = AL_FALSE;
  577. slot = ATOMIC_LOAD(&slot->next, almemory_order_relaxed);
  578. }
  579. UnlockEffectSlotsRead(context);
  580. }
  581. ALvoid ReleaseALAuxiliaryEffectSlots(ALCcontext *Context)
  582. {
  583. ALsizei pos;
  584. for(pos = 0;pos < Context->EffectSlotMap.size;pos++)
  585. {
  586. ALeffectslot *temp = Context->EffectSlotMap.values[pos];
  587. Context->EffectSlotMap.values[pos] = NULL;
  588. DeinitEffectSlot(temp);
  589. FreeThunkEntry(temp->id);
  590. memset(temp, 0, sizeof(ALeffectslot));
  591. al_free(temp);
  592. }
  593. }