Use fmt instead of to_string and concating strings
[openal-soft.git] / al / buffer.cpp
blob83487e2778c59cc65eefa6857868f20ab07046dd
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.
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.
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
21 #include "config.h"
23 #include "buffer.h"
25 #include <algorithm>
26 #include <array>
27 #include <atomic>
28 #include <cassert>
29 #include <cstddef>
30 #include <cstdint>
31 #include <cstring>
32 #include <iterator>
33 #include <limits>
34 #include <memory>
35 #include <mutex>
36 #include <numeric>
37 #include <optional>
38 #include <stdexcept>
39 #include <string>
40 #include <unordered_map>
41 #include <utility>
42 #include <vector>
44 #include "AL/al.h"
45 #include "AL/alc.h"
46 #include "AL/alext.h"
48 #include "albit.h"
49 #include "alc/context.h"
50 #include "alc/device.h"
51 #include "alc/inprogext.h"
52 #include "almalloc.h"
53 #include "alnumeric.h"
54 #include "alspan.h"
55 #include "core/device.h"
56 #include "core/resampler_limits.h"
57 #include "core/voice.h"
58 #include "direct_defs.h"
59 #include "error.h"
60 #include "intrusive_ptr.h"
61 #include "opthelpers.h"
63 #if ALSOFT_EAX
64 #include <unordered_set>
66 #include "eax/globals.h"
67 #include "eax/x_ram.h"
68 #endif // ALSOFT_EAX
71 namespace {
73 using SubListAllocator = al::allocator<std::array<ALbuffer,64>>;
75 constexpr auto AmbiLayoutFromEnum(ALenum layout) noexcept -> std::optional<AmbiLayout>
77 switch(layout)
79 case AL_FUMA_SOFT: return AmbiLayout::FuMa;
80 case AL_ACN_SOFT: return AmbiLayout::ACN;
82 return std::nullopt;
84 constexpr auto EnumFromAmbiLayout(AmbiLayout layout) -> ALenum
86 switch(layout)
88 case AmbiLayout::FuMa: return AL_FUMA_SOFT;
89 case AmbiLayout::ACN: return AL_ACN_SOFT;
91 throw std::runtime_error{"Invalid AmbiLayout: "+std::to_string(int(layout))};
94 constexpr auto AmbiScalingFromEnum(ALenum scale) noexcept -> std::optional<AmbiScaling>
96 switch(scale)
98 case AL_FUMA_SOFT: return AmbiScaling::FuMa;
99 case AL_SN3D_SOFT: return AmbiScaling::SN3D;
100 case AL_N3D_SOFT: return AmbiScaling::N3D;
102 return std::nullopt;
104 constexpr auto EnumFromAmbiScaling(AmbiScaling scale) -> ALenum
106 switch(scale)
108 case AmbiScaling::FuMa: return AL_FUMA_SOFT;
109 case AmbiScaling::SN3D: return AL_SN3D_SOFT;
110 case AmbiScaling::N3D: return AL_N3D_SOFT;
111 case AmbiScaling::UHJ: break;
113 throw std::runtime_error{"Invalid AmbiScaling: "+std::to_string(int(scale))};
116 #if ALSOFT_EAX
117 constexpr auto EaxStorageFromEnum(ALenum scale) noexcept -> std::optional<EaxStorage>
119 switch(scale)
121 case AL_STORAGE_AUTOMATIC: return EaxStorage::Automatic;
122 case AL_STORAGE_ACCESSIBLE: return EaxStorage::Accessible;
123 case AL_STORAGE_HARDWARE: return EaxStorage::Hardware;
125 return std::nullopt;
127 constexpr auto EnumFromEaxStorage(EaxStorage storage) -> ALenum
129 switch(storage)
131 case EaxStorage::Automatic: return AL_STORAGE_AUTOMATIC;
132 case EaxStorage::Accessible: return AL_STORAGE_ACCESSIBLE;
133 case EaxStorage::Hardware: return AL_STORAGE_HARDWARE;
135 throw std::runtime_error{"Invalid EaxStorage: "+std::to_string(int(storage))};
139 auto eax_x_ram_check_availability(const al::Device &device, const ALbuffer &buffer,
140 const ALuint newsize) noexcept -> bool
142 ALuint freemem{device.eax_x_ram_free_size};
143 /* If the buffer is currently in "hardware", add its memory to the free
144 * pool since it'll be "replaced".
146 if(buffer.eax_x_ram_is_hardware)
147 freemem += buffer.OriginalSize;
148 return freemem >= newsize;
151 void eax_x_ram_apply(al::Device &device, ALbuffer &buffer) noexcept
153 if(buffer.eax_x_ram_is_hardware)
154 return;
156 if(device.eax_x_ram_free_size >= buffer.OriginalSize)
158 device.eax_x_ram_free_size -= buffer.OriginalSize;
159 buffer.eax_x_ram_is_hardware = true;
163 void eax_x_ram_clear(al::Device& al_device, ALbuffer& al_buffer) noexcept
165 if(al_buffer.eax_x_ram_is_hardware)
166 al_device.eax_x_ram_free_size += al_buffer.OriginalSize;
167 al_buffer.eax_x_ram_is_hardware = false;
169 #endif // ALSOFT_EAX
172 constexpr ALbitfieldSOFT INVALID_STORAGE_MASK{~unsigned(AL_MAP_READ_BIT_SOFT |
173 AL_MAP_WRITE_BIT_SOFT | AL_MAP_PERSISTENT_BIT_SOFT | AL_PRESERVE_DATA_BIT_SOFT)};
174 constexpr ALbitfieldSOFT MAP_READ_WRITE_FLAGS{AL_MAP_READ_BIT_SOFT | AL_MAP_WRITE_BIT_SOFT};
175 constexpr ALbitfieldSOFT INVALID_MAP_FLAGS{~unsigned(AL_MAP_READ_BIT_SOFT | AL_MAP_WRITE_BIT_SOFT |
176 AL_MAP_PERSISTENT_BIT_SOFT)};
179 auto EnsureBuffers(al::Device *device, size_t needed) noexcept -> bool
180 try {
181 size_t count{std::accumulate(device->BufferList.cbegin(), device->BufferList.cend(), 0_uz,
182 [](size_t cur, const BufferSubList &sublist) noexcept -> size_t
183 { return cur + static_cast<ALuint>(al::popcount(sublist.FreeMask)); })};
185 while(needed > count)
187 if(device->BufferList.size() >= 1<<25) UNLIKELY
188 return false;
190 BufferSubList sublist{};
191 sublist.FreeMask = ~0_u64;
192 sublist.Buffers = SubListAllocator{}.allocate(1);
193 device->BufferList.emplace_back(std::move(sublist));
194 count += std::tuple_size_v<SubListAllocator::value_type>;
196 return true;
198 catch(...) {
199 return false;
202 auto AllocBuffer(al::Device *device) noexcept -> ALbuffer*
204 auto sublist = std::find_if(device->BufferList.begin(), device->BufferList.end(),
205 [](const BufferSubList &entry) noexcept -> bool
206 { return entry.FreeMask != 0; });
207 auto lidx = static_cast<ALuint>(std::distance(device->BufferList.begin(), sublist));
208 auto slidx = static_cast<ALuint>(al::countr_zero(sublist->FreeMask));
209 ASSUME(slidx < 64);
211 ALbuffer *buffer{al::construct_at(al::to_address(sublist->Buffers->begin() + slidx))};
213 /* Add 1 to avoid buffer ID 0. */
214 buffer->id = ((lidx<<6) | slidx) + 1;
216 sublist->FreeMask &= ~(1_u64 << slidx);
218 return buffer;
221 void FreeBuffer(al::Device *device, ALbuffer *buffer)
223 #if ALSOFT_EAX
224 eax_x_ram_clear(*device, *buffer);
225 #endif // ALSOFT_EAX
227 device->mBufferNames.erase(buffer->id);
229 const ALuint id{buffer->id - 1};
230 const size_t lidx{id >> 6};
231 const ALuint slidx{id & 0x3f};
233 std::destroy_at(buffer);
235 device->BufferList[lidx].FreeMask |= 1_u64 << slidx;
238 auto LookupBuffer(al::Device *device, ALuint id) noexcept -> ALbuffer*
240 const size_t lidx{(id-1) >> 6};
241 const ALuint slidx{(id-1) & 0x3f};
243 if(lidx >= device->BufferList.size()) UNLIKELY
244 return nullptr;
245 BufferSubList &sublist = device->BufferList[lidx];
246 if(sublist.FreeMask & (1_u64 << slidx)) UNLIKELY
247 return nullptr;
248 return al::to_address(sublist.Buffers->begin() + slidx);
252 constexpr auto SanitizeAlignment(FmtType type, ALuint align) noexcept -> ALuint
254 if(align == 0)
256 if(type == FmtIMA4)
258 /* Here is where things vary:
259 * nVidia and Apple use 64+1 sample frames per block -> block_size=36 bytes per channel
260 * Most PC sound software uses 2040+1 sample frames per block -> block_size=1024 bytes per channel
262 return 65;
264 if(type == FmtMSADPCM)
265 return 64;
266 return 1;
269 if(type == FmtIMA4)
271 /* IMA4 block alignment must be a multiple of 8, plus 1. */
272 if((align&7) == 1) return align;
273 return 0;
275 if(type == FmtMSADPCM)
277 /* MSADPCM block alignment must be a multiple of 2. */
278 if((align&1) == 0) return align;
279 return 0;
282 return align;
286 /** Loads the specified data into the buffer, using the specified format. */
287 void LoadData(ALCcontext *context [[maybe_unused]], ALbuffer *ALBuf, ALsizei freq, ALuint size,
288 const FmtChannels DstChannels, const FmtType DstType, const al::span<const std::byte> SrcData,
289 ALbitfieldSOFT access)
291 if(ALBuf->ref.load(std::memory_order_relaxed) != 0 || ALBuf->MappedAccess != 0)
292 throw al::context_error{AL_INVALID_OPERATION, "Modifying storage for in-use buffer %u",
293 ALBuf->id};
295 const ALuint unpackalign{ALBuf->UnpackAlign};
296 const ALuint align{SanitizeAlignment(DstType, unpackalign)};
297 if(align < 1)
298 throw al::context_error{AL_INVALID_VALUE, "Invalid unpack alignment %u for %s samples",
299 unpackalign, NameFromFormat(DstType)};
301 const ALuint ambiorder{IsBFormat(DstChannels) ? ALBuf->UnpackAmbiOrder :
302 (IsUHJ(DstChannels) ? 1 : 0)};
304 if((access&AL_PRESERVE_DATA_BIT_SOFT))
306 /* Can only preserve data with the same format and alignment. */
307 if(ALBuf->mChannels != DstChannels || ALBuf->mType != DstType)
308 throw al::context_error{AL_INVALID_VALUE, "Preserving data of mismatched format"};
309 if(ALBuf->mBlockAlign != align)
310 throw al::context_error{AL_INVALID_VALUE, "Preserving data of mismatched alignment"};
311 if(ALBuf->mAmbiOrder != ambiorder)
312 throw al::context_error{AL_INVALID_VALUE, "Preserving data of mismatched order"};
315 /* Convert the size in bytes to blocks using the unpack block alignment. */
316 const ALuint NumChannels{ChannelsFromFmt(DstChannels, ambiorder)};
317 const ALuint BlockSize{NumChannels *
318 ((DstType == FmtIMA4) ? (align-1)/2 + 4 :
319 (DstType == FmtMSADPCM) ? (align-2)/2 + 7 :
320 (align * BytesFromFmt(DstType)))};
321 if((size%BlockSize) != 0)
322 throw al::context_error{AL_INVALID_VALUE,
323 "Data size %d is not a multiple of frame size %d (%d unpack alignment)",
324 size, BlockSize, align};
325 const ALuint blocks{size / BlockSize};
327 if(blocks > std::numeric_limits<ALsizei>::max()/align)
328 throw al::context_error{AL_OUT_OF_MEMORY,
329 "Buffer size overflow, %d blocks x %d samples per block", blocks, align};
330 if(blocks > std::numeric_limits<size_t>::max()/BlockSize)
331 throw al::context_error{AL_OUT_OF_MEMORY,
332 "Buffer size overflow, %d frames x %d bytes per frame", blocks, BlockSize};
334 const size_t newsize{static_cast<size_t>(blocks) * BlockSize};
336 #if ALSOFT_EAX
337 if(ALBuf->eax_x_ram_mode == EaxStorage::Hardware)
339 auto &device = *context->mALDevice;
340 if(!eax_x_ram_check_availability(device, *ALBuf, size))
341 throw al::context_error{AL_OUT_OF_MEMORY,
342 "Out of X-RAM memory (avail: %u, needed: %u)", device.eax_x_ram_free_size, size};
344 #endif
346 /* This could reallocate only when increasing the size or the new size is
347 * less than half the current, but then the buffer's AL_SIZE would not be
348 * very reliable for accounting buffer memory usage, and reporting the real
349 * size could cause problems for apps that use AL_SIZE to try to get the
350 * buffer's play length.
352 if(newsize != ALBuf->mDataStorage.size())
354 auto newdata = decltype(ALBuf->mDataStorage)(newsize, std::byte{});
355 if((access&AL_PRESERVE_DATA_BIT_SOFT))
357 const size_t tocopy{std::min(newdata.size(), ALBuf->mDataStorage.size())};
358 std::copy_n(ALBuf->mDataStorage.begin(), tocopy, newdata.begin());
360 newdata.swap(ALBuf->mDataStorage);
362 ALBuf->mData = ALBuf->mDataStorage;
363 #if ALSOFT_EAX
364 eax_x_ram_clear(*context->mALDevice, *ALBuf);
365 #endif
367 if(!SrcData.empty() && !ALBuf->mData.empty())
368 std::copy_n(SrcData.begin(), blocks*BlockSize, ALBuf->mData.begin());
369 ALBuf->mBlockAlign = (DstType == FmtIMA4 || DstType == FmtMSADPCM) ? align : 1;
371 ALBuf->OriginalSize = size;
373 ALBuf->Access = access;
375 ALBuf->mSampleRate = static_cast<ALuint>(freq);
376 ALBuf->mChannels = DstChannels;
377 ALBuf->mType = DstType;
378 ALBuf->mAmbiOrder = ambiorder;
380 ALBuf->mCallback = nullptr;
381 ALBuf->mUserData = nullptr;
383 ALBuf->mSampleLen = blocks * align;
384 ALBuf->mLoopStart = 0;
385 ALBuf->mLoopEnd = ALBuf->mSampleLen;
387 #if ALSOFT_EAX
388 if(eax_g_is_enabled && ALBuf->eax_x_ram_mode == EaxStorage::Hardware)
389 eax_x_ram_apply(*context->mALDevice, *ALBuf);
390 #endif
393 /** Prepares the buffer to use the specified callback, using the specified format. */
394 void PrepareCallback(ALCcontext *context [[maybe_unused]], ALbuffer *ALBuf, ALsizei freq,
395 const FmtChannels DstChannels, const FmtType DstType, ALBUFFERCALLBACKTYPESOFT callback,
396 void *userptr)
398 if(ALBuf->ref.load(std::memory_order_relaxed) != 0 || ALBuf->MappedAccess != 0)
399 throw al::context_error{AL_INVALID_OPERATION, "Modifying callback for in-use buffer %u",
400 ALBuf->id};
402 const ALuint ambiorder{IsBFormat(DstChannels) ? ALBuf->UnpackAmbiOrder :
403 (IsUHJ(DstChannels) ? 1 : 0)};
405 const ALuint unpackalign{ALBuf->UnpackAlign};
406 const ALuint align{SanitizeAlignment(DstType, unpackalign)};
407 if(align < 1)
408 throw al::context_error{AL_INVALID_VALUE, "Invalid unpack alignment %u for %s samples",
409 unpackalign, NameFromFormat(DstType)};
411 const ALuint BlockSize{ChannelsFromFmt(DstChannels, ambiorder) *
412 ((DstType == FmtIMA4) ? (align-1)/2 + 4 :
413 (DstType == FmtMSADPCM) ? (align-2)/2 + 7 :
414 (align * BytesFromFmt(DstType)))};
416 /* The maximum number of samples a callback buffer may need to store is a
417 * full mixing line * max pitch * channel count, since it may need to hold
418 * a full line's worth of sample frames before downsampling. An additional
419 * MaxResamplerEdge is needed for "future" samples during resampling (the
420 * voice will hold a history for the past samples).
422 static constexpr size_t line_size{DeviceBase::MixerLineSize*MaxPitch + MaxResamplerEdge};
423 const size_t line_blocks{(line_size + align-1) / align};
425 using BufferVectorType = decltype(ALBuf->mDataStorage);
426 BufferVectorType(line_blocks*BlockSize).swap(ALBuf->mDataStorage);
427 ALBuf->mData = ALBuf->mDataStorage;
429 #if ALSOFT_EAX
430 eax_x_ram_clear(*context->mALDevice, *ALBuf);
431 #endif
433 ALBuf->mCallback = callback;
434 ALBuf->mUserData = userptr;
436 ALBuf->OriginalSize = 0;
437 ALBuf->Access = 0;
439 ALBuf->mBlockAlign = (DstType == FmtIMA4 || DstType == FmtMSADPCM) ? align : 1;
440 ALBuf->mSampleRate = static_cast<ALuint>(freq);
441 ALBuf->mChannels = DstChannels;
442 ALBuf->mType = DstType;
443 ALBuf->mAmbiOrder = ambiorder;
445 ALBuf->mSampleLen = 0;
446 ALBuf->mLoopStart = 0;
447 ALBuf->mLoopEnd = ALBuf->mSampleLen;
450 /** Prepares the buffer to use caller-specified storage. */
451 void PrepareUserPtr(ALCcontext *context [[maybe_unused]], ALbuffer *ALBuf, ALsizei freq,
452 const FmtChannels DstChannels, const FmtType DstType, std::byte *sdata, const ALuint sdatalen)
454 if(ALBuf->ref.load(std::memory_order_relaxed) != 0 || ALBuf->MappedAccess != 0)
455 throw al::context_error{AL_INVALID_OPERATION, "Modifying storage for in-use buffer %u",
456 ALBuf->id};
458 const ALuint unpackalign{ALBuf->UnpackAlign};
459 const ALuint align{SanitizeAlignment(DstType, unpackalign)};
460 if(align < 1)
461 throw al::context_error{AL_INVALID_VALUE, "Invalid unpack alignment %u for %s samples",
462 unpackalign, NameFromFormat(DstType)};
464 auto get_type_alignment = [](const FmtType type) noexcept -> ALuint
466 /* NOTE: This only needs to be the required alignment for the CPU to
467 * read/write the given sample type in the mixer.
469 switch(type)
471 case FmtUByte: return alignof(ALubyte);
472 case FmtShort: return alignof(ALshort);
473 case FmtInt: return alignof(ALint);
474 case FmtFloat: return alignof(ALfloat);
475 case FmtDouble: return alignof(ALdouble);
476 case FmtMulaw: return alignof(ALubyte);
477 case FmtAlaw: return alignof(ALubyte);
478 case FmtIMA4: break;
479 case FmtMSADPCM: break;
481 return 1;
483 const auto typealign = get_type_alignment(DstType);
484 if((reinterpret_cast<uintptr_t>(sdata) & (typealign-1)) != 0)
485 throw al::context_error{AL_INVALID_VALUE, "Pointer %p is misaligned for %s samples (%u)",
486 static_cast<void*>(sdata), NameFromFormat(DstType), typealign};
488 const ALuint ambiorder{IsBFormat(DstChannels) ? ALBuf->UnpackAmbiOrder :
489 (IsUHJ(DstChannels) ? 1 : 0)};
491 /* Convert the size in bytes to blocks using the unpack block alignment. */
492 const ALuint NumChannels{ChannelsFromFmt(DstChannels, ambiorder)};
493 const ALuint BlockSize{NumChannels *
494 ((DstType == FmtIMA4) ? (align-1)/2 + 4 :
495 (DstType == FmtMSADPCM) ? (align-2)/2 + 7 :
496 (align * BytesFromFmt(DstType)))};
497 if((sdatalen%BlockSize) != 0)
498 throw al::context_error{AL_INVALID_VALUE,
499 "Data size %u is not a multiple of frame size %u (%u unpack alignment)",
500 sdatalen, BlockSize, align};
501 const ALuint blocks{sdatalen / BlockSize};
503 if(blocks > std::numeric_limits<ALsizei>::max()/align)
504 throw al::context_error{AL_OUT_OF_MEMORY,
505 "Buffer size overflow, %d blocks x %d samples per block", blocks, align};
506 if(blocks > std::numeric_limits<size_t>::max()/BlockSize)
507 throw al::context_error{AL_OUT_OF_MEMORY,
508 "Buffer size overflow, %d frames x %d bytes per frame", blocks, BlockSize};
510 #if ALSOFT_EAX
511 if(ALBuf->eax_x_ram_mode == EaxStorage::Hardware)
513 auto &device = *context->mALDevice;
514 if(!eax_x_ram_check_availability(device, *ALBuf, sdatalen))
515 throw al::context_error{AL_OUT_OF_MEMORY,
516 "Out of X-RAM memory (avail: %u, needed: %u)", device.eax_x_ram_free_size,
517 sdatalen};
519 #endif
521 decltype(ALBuf->mDataStorage){}.swap(ALBuf->mDataStorage);
522 ALBuf->mData = al::span{sdata, sdatalen};
524 #if ALSOFT_EAX
525 eax_x_ram_clear(*context->mALDevice, *ALBuf);
526 #endif
528 ALBuf->mCallback = nullptr;
529 ALBuf->mUserData = nullptr;
531 ALBuf->OriginalSize = sdatalen;
532 ALBuf->Access = 0;
534 ALBuf->mBlockAlign = (DstType == FmtIMA4 || DstType == FmtMSADPCM) ? align : 1;
535 ALBuf->mSampleRate = static_cast<ALuint>(freq);
536 ALBuf->mChannels = DstChannels;
537 ALBuf->mType = DstType;
538 ALBuf->mAmbiOrder = ambiorder;
540 ALBuf->mSampleLen = blocks * align;
541 ALBuf->mLoopStart = 0;
542 ALBuf->mLoopEnd = ALBuf->mSampleLen;
544 #if ALSOFT_EAX
545 if(ALBuf->eax_x_ram_mode == EaxStorage::Hardware)
546 eax_x_ram_apply(*context->mALDevice, *ALBuf);
547 #endif
551 struct DecompResult { FmtChannels channels; FmtType type; };
552 auto DecomposeUserFormat(ALenum format) noexcept -> std::optional<DecompResult>
554 struct FormatMap {
555 ALenum format;
556 DecompResult result;
558 static constexpr std::array UserFmtList{
559 FormatMap{AL_FORMAT_MONO8, {FmtMono, FmtUByte} },
560 FormatMap{AL_FORMAT_MONO16, {FmtMono, FmtShort} },
561 FormatMap{AL_FORMAT_MONO_I32, {FmtMono, FmtInt} },
562 FormatMap{AL_FORMAT_MONO_FLOAT32, {FmtMono, FmtFloat} },
563 FormatMap{AL_FORMAT_MONO_DOUBLE_EXT, {FmtMono, FmtDouble} },
564 FormatMap{AL_FORMAT_MONO_IMA4, {FmtMono, FmtIMA4} },
565 FormatMap{AL_FORMAT_MONO_MSADPCM_SOFT, {FmtMono, FmtMSADPCM}},
566 FormatMap{AL_FORMAT_MONO_MULAW, {FmtMono, FmtMulaw} },
567 FormatMap{AL_FORMAT_MONO_ALAW_EXT, {FmtMono, FmtAlaw} },
569 FormatMap{AL_FORMAT_STEREO8, {FmtStereo, FmtUByte} },
570 FormatMap{AL_FORMAT_STEREO16, {FmtStereo, FmtShort} },
571 FormatMap{AL_FORMAT_STEREO_I32, {FmtStereo, FmtInt} },
572 FormatMap{AL_FORMAT_STEREO_FLOAT32, {FmtStereo, FmtFloat} },
573 FormatMap{AL_FORMAT_STEREO_DOUBLE_EXT, {FmtStereo, FmtDouble} },
574 FormatMap{AL_FORMAT_STEREO_IMA4, {FmtStereo, FmtIMA4} },
575 FormatMap{AL_FORMAT_STEREO_MSADPCM_SOFT, {FmtStereo, FmtMSADPCM}},
576 FormatMap{AL_FORMAT_STEREO_MULAW, {FmtStereo, FmtMulaw} },
577 FormatMap{AL_FORMAT_STEREO_ALAW_EXT, {FmtStereo, FmtAlaw} },
579 FormatMap{AL_FORMAT_REAR8, {FmtRear, FmtUByte}},
580 FormatMap{AL_FORMAT_REAR16, {FmtRear, FmtShort}},
581 FormatMap{AL_FORMAT_REAR32, {FmtRear, FmtFloat}},
582 FormatMap{AL_FORMAT_REAR_I32, {FmtRear, FmtInt} },
583 FormatMap{AL_FORMAT_REAR_FLOAT32, {FmtRear, FmtFloat}},
584 FormatMap{AL_FORMAT_REAR_MULAW, {FmtRear, FmtMulaw}},
586 FormatMap{AL_FORMAT_QUAD8_LOKI, {FmtQuad, FmtUByte}},
587 FormatMap{AL_FORMAT_QUAD16_LOKI, {FmtQuad, FmtShort}},
589 FormatMap{AL_FORMAT_QUAD8, {FmtQuad, FmtUByte}},
590 FormatMap{AL_FORMAT_QUAD16, {FmtQuad, FmtShort}},
591 FormatMap{AL_FORMAT_QUAD32, {FmtQuad, FmtFloat}},
592 FormatMap{AL_FORMAT_QUAD_I32, {FmtQuad, FmtInt} },
593 FormatMap{AL_FORMAT_QUAD_FLOAT32, {FmtQuad, FmtFloat}},
594 FormatMap{AL_FORMAT_QUAD_MULAW, {FmtQuad, FmtMulaw}},
596 FormatMap{AL_FORMAT_51CHN8, {FmtX51, FmtUByte}},
597 FormatMap{AL_FORMAT_51CHN16, {FmtX51, FmtShort}},
598 FormatMap{AL_FORMAT_51CHN32, {FmtX51, FmtFloat}},
599 FormatMap{AL_FORMAT_51CHN_I32, {FmtX51, FmtInt} },
600 FormatMap{AL_FORMAT_51CHN_FLOAT32, {FmtX51, FmtFloat}},
601 FormatMap{AL_FORMAT_51CHN_MULAW, {FmtX51, FmtMulaw}},
603 FormatMap{AL_FORMAT_61CHN8, {FmtX61, FmtUByte}},
604 FormatMap{AL_FORMAT_61CHN16, {FmtX61, FmtShort}},
605 FormatMap{AL_FORMAT_61CHN32, {FmtX61, FmtFloat}},
606 FormatMap{AL_FORMAT_61CHN_I32, {FmtX61, FmtInt} },
607 FormatMap{AL_FORMAT_61CHN_FLOAT32, {FmtX61, FmtFloat}},
608 FormatMap{AL_FORMAT_61CHN_MULAW, {FmtX61, FmtMulaw}},
610 FormatMap{AL_FORMAT_71CHN8, {FmtX71, FmtUByte}},
611 FormatMap{AL_FORMAT_71CHN16, {FmtX71, FmtShort}},
612 FormatMap{AL_FORMAT_71CHN32, {FmtX71, FmtFloat}},
613 FormatMap{AL_FORMAT_71CHN_I32, {FmtX71, FmtInt} },
614 FormatMap{AL_FORMAT_71CHN_FLOAT32, {FmtX71, FmtFloat}},
615 FormatMap{AL_FORMAT_71CHN_MULAW, {FmtX71, FmtMulaw}},
617 FormatMap{AL_FORMAT_BFORMAT2D_8, {FmtBFormat2D, FmtUByte}},
618 FormatMap{AL_FORMAT_BFORMAT2D_16, {FmtBFormat2D, FmtShort}},
619 FormatMap{AL_FORMAT_BFORMAT2D_I32, {FmtBFormat2D, FmtInt} },
620 FormatMap{AL_FORMAT_BFORMAT2D_FLOAT32, {FmtBFormat2D, FmtFloat}},
621 FormatMap{AL_FORMAT_BFORMAT2D_MULAW, {FmtBFormat2D, FmtMulaw}},
623 FormatMap{AL_FORMAT_BFORMAT3D_8, {FmtBFormat3D, FmtUByte}},
624 FormatMap{AL_FORMAT_BFORMAT3D_16, {FmtBFormat3D, FmtShort}},
625 FormatMap{AL_FORMAT_BFORMAT2D_I32, {FmtBFormat3D, FmtInt} },
626 FormatMap{AL_FORMAT_BFORMAT3D_FLOAT32, {FmtBFormat3D, FmtFloat}},
627 FormatMap{AL_FORMAT_BFORMAT3D_MULAW, {FmtBFormat3D, FmtMulaw}},
629 FormatMap{AL_FORMAT_UHJ2CHN8_SOFT, {FmtUHJ2, FmtUByte} },
630 FormatMap{AL_FORMAT_UHJ2CHN16_SOFT, {FmtUHJ2, FmtShort} },
631 FormatMap{AL_FORMAT_UHJ2CHN_I32_SOFT, {FmtUHJ2, FmtInt} },
632 FormatMap{AL_FORMAT_UHJ2CHN_FLOAT32_SOFT, {FmtUHJ2, FmtFloat} },
633 FormatMap{AL_FORMAT_UHJ2CHN_MULAW_SOFT, {FmtUHJ2, FmtMulaw} },
634 FormatMap{AL_FORMAT_UHJ2CHN_ALAW_SOFT, {FmtUHJ2, FmtAlaw} },
635 FormatMap{AL_FORMAT_UHJ2CHN_IMA4_SOFT, {FmtUHJ2, FmtIMA4} },
636 FormatMap{AL_FORMAT_UHJ2CHN_MSADPCM_SOFT, {FmtUHJ2, FmtMSADPCM}},
638 FormatMap{AL_FORMAT_UHJ3CHN8_SOFT, {FmtUHJ3, FmtUByte}},
639 FormatMap{AL_FORMAT_UHJ3CHN16_SOFT, {FmtUHJ3, FmtShort}},
640 FormatMap{AL_FORMAT_UHJ3CHN_I32_SOFT, {FmtUHJ3, FmtInt} },
641 FormatMap{AL_FORMAT_UHJ3CHN_FLOAT32_SOFT, {FmtUHJ3, FmtFloat}},
642 FormatMap{AL_FORMAT_UHJ3CHN_MULAW_SOFT, {FmtUHJ3, FmtMulaw}},
643 FormatMap{AL_FORMAT_UHJ3CHN_ALAW_SOFT, {FmtUHJ3, FmtAlaw} },
645 FormatMap{AL_FORMAT_UHJ4CHN8_SOFT, {FmtUHJ4, FmtUByte}},
646 FormatMap{AL_FORMAT_UHJ4CHN16_SOFT, {FmtUHJ4, FmtShort}},
647 FormatMap{AL_FORMAT_UHJ4CHN_I32_SOFT, {FmtUHJ4, FmtInt} },
648 FormatMap{AL_FORMAT_UHJ4CHN_FLOAT32_SOFT, {FmtUHJ4, FmtFloat}},
649 FormatMap{AL_FORMAT_UHJ4CHN_MULAW_SOFT, {FmtUHJ4, FmtMulaw}},
650 FormatMap{AL_FORMAT_UHJ4CHN_ALAW_SOFT, {FmtUHJ4, FmtAlaw} },
653 auto iter = std::find_if(UserFmtList.cbegin(), UserFmtList.cend(),
654 [format](const FormatMap &fmt) noexcept { return fmt.format == format; });
655 if(iter != UserFmtList.cend())
656 return iter->result;
657 return std::nullopt;
660 } // namespace
663 AL_API DECL_FUNC2(void, alGenBuffers, ALsizei,n, ALuint*,buffers)
664 FORCE_ALIGN void AL_APIENTRY alGenBuffersDirect(ALCcontext *context, ALsizei n, ALuint *buffers) noexcept
665 try {
666 if(n < 0)
667 throw al::context_error{AL_INVALID_VALUE, "Generating %d buffers", n};
668 if(n <= 0) UNLIKELY return;
670 auto *device = context->mALDevice.get();
671 auto buflock = std::lock_guard{device->BufferLock};
673 const al::span bids{buffers, static_cast<ALuint>(n)};
674 if(!EnsureBuffers(device, bids.size()))
675 throw al::context_error{AL_OUT_OF_MEMORY, "Failed to allocate %d buffer%s", n,
676 (n == 1) ? "" : "s"};
678 std::generate(bids.begin(), bids.end(), [device]{ return AllocBuffer(device)->id; });
680 catch(al::context_error& e) {
681 context->setError(e.errorCode(), "%s", e.what());
684 AL_API DECL_FUNC2(void, alDeleteBuffers, ALsizei,n, const ALuint*,buffers)
685 FORCE_ALIGN void AL_APIENTRY alDeleteBuffersDirect(ALCcontext *context, ALsizei n,
686 const ALuint *buffers) noexcept
687 try {
688 if(n < 0)
689 throw al::context_error{AL_INVALID_VALUE, "Deleting %d buffers", n};
690 if(n <= 0) UNLIKELY return;
692 auto *device = context->mALDevice.get();
693 auto buflock = std::lock_guard{device->BufferLock};
695 /* First try to find any buffers that are invalid or in-use. */
696 auto validate_buffer = [device](const ALuint bid)
698 if(!bid) return;
699 ALbuffer *ALBuf{LookupBuffer(device, bid)};
700 if(!ALBuf)
701 throw al::context_error{AL_INVALID_NAME, "Invalid buffer ID %u", bid};
702 if(ALBuf->ref.load(std::memory_order_relaxed) != 0)
703 throw al::context_error{AL_INVALID_OPERATION, "Deleting in-use buffer %u", bid};
706 const al::span bids{buffers, static_cast<ALuint>(n)};
707 std::for_each(bids.begin(), bids.end(), validate_buffer);
709 /* All good. Delete non-0 buffer IDs. */
710 auto delete_buffer = [device](const ALuint bid) -> void
712 if(ALbuffer *buffer{bid ? LookupBuffer(device, bid) : nullptr})
713 FreeBuffer(device, buffer);
715 std::for_each(bids.begin(), bids.end(), delete_buffer);
717 catch(al::context_error& e) {
718 context->setError(e.errorCode(), "%s", e.what());
721 AL_API DECL_FUNC1(ALboolean, alIsBuffer, ALuint,buffer)
722 FORCE_ALIGN ALboolean AL_APIENTRY alIsBufferDirect(ALCcontext *context, ALuint buffer) noexcept
724 auto *device = context->mALDevice.get();
725 auto buflock = std::lock_guard{device->BufferLock};
726 if(!buffer || LookupBuffer(device, buffer))
727 return AL_TRUE;
728 return AL_FALSE;
732 AL_API void AL_APIENTRY alBufferData(ALuint buffer, ALenum format, const ALvoid *data, ALsizei size, ALsizei freq) noexcept
734 auto context = GetContextRef();
735 if(!context) UNLIKELY return;
736 alBufferStorageDirectSOFT(context.get(), buffer, format, data, size, freq, 0);
739 FORCE_ALIGN void AL_APIENTRY alBufferDataDirect(ALCcontext *context, ALuint buffer, ALenum format, const ALvoid *data, ALsizei size, ALsizei freq) noexcept
740 { alBufferStorageDirectSOFT(context, buffer, format, data, size, freq, 0); }
742 AL_API DECL_FUNCEXT6(void, alBufferStorage,SOFT, ALuint,buffer, ALenum,format, const ALvoid*,data, ALsizei,size, ALsizei,freq, ALbitfieldSOFT,flags)
743 FORCE_ALIGN void AL_APIENTRY alBufferStorageDirectSOFT(ALCcontext *context, ALuint buffer,
744 ALenum format, const ALvoid *data, ALsizei size, ALsizei freq, ALbitfieldSOFT flags) noexcept
745 try {
746 auto *device = context->mALDevice.get();
747 auto buflock = std::lock_guard{device->BufferLock};
749 ALbuffer *albuf{LookupBuffer(device, buffer)};
750 if(!albuf)
751 throw al::context_error{AL_INVALID_NAME, "Invalid buffer ID %u", buffer};
752 if(size < 0)
753 throw al::context_error{AL_INVALID_VALUE, "Negative storage size %d", size};
754 if(freq < 1)
755 throw al::context_error{AL_INVALID_VALUE, "Invalid sample rate %d", freq};
756 if((flags&INVALID_STORAGE_MASK) != 0)
757 throw al::context_error{AL_INVALID_VALUE, "Invalid storage flags 0x%x",
758 flags&INVALID_STORAGE_MASK};
759 if((flags&AL_MAP_PERSISTENT_BIT_SOFT) && !(flags&MAP_READ_WRITE_FLAGS))
760 throw al::context_error{AL_INVALID_VALUE,
761 "Declaring persistently mapped storage without read or write access"};
763 auto usrfmt = DecomposeUserFormat(format);
764 if(!usrfmt)
765 throw al::context_error{AL_INVALID_ENUM, "Invalid format 0x%04x", format};
767 auto bdata = static_cast<const std::byte*>(data);
768 LoadData(context, albuf, freq, static_cast<ALuint>(size), usrfmt->channels, usrfmt->type,
769 al::span{bdata, bdata ? static_cast<ALuint>(size) : 0u}, flags);
771 catch(al::context_error& e) {
772 context->setError(e.errorCode(), "%s", e.what());
775 FORCE_ALIGN DECL_FUNC5(void, alBufferDataStatic, ALuint,buffer, ALenum,format, ALvoid*,data, ALsizei,size, ALsizei,freq)
776 FORCE_ALIGN void AL_APIENTRY alBufferDataStaticDirect(ALCcontext *context, const ALuint buffer,
777 ALenum format, ALvoid *data, ALsizei size, ALsizei freq) noexcept
778 try {
779 auto *device = context->mALDevice.get();
780 auto buflock = std::lock_guard{device->BufferLock};
782 ALbuffer *albuf{LookupBuffer(device, buffer)};
783 if(!albuf)
784 throw al::context_error{AL_INVALID_NAME, "Invalid buffer ID %u", buffer};
785 if(size < 0)
786 throw al::context_error{AL_INVALID_VALUE, "Negative storage size %d", size};
787 if(freq < 1)
788 throw al::context_error{AL_INVALID_VALUE, "Invalid sample rate %d", freq};
790 auto usrfmt = DecomposeUserFormat(format);
791 if(!usrfmt)
792 throw al::context_error{AL_INVALID_ENUM, "Invalid format 0x%04x", format};
794 PrepareUserPtr(context, albuf, freq, usrfmt->channels, usrfmt->type,
795 static_cast<std::byte*>(data), static_cast<ALuint>(size));
797 catch(al::context_error& e) {
798 context->setError(e.errorCode(), "%s", e.what());
801 AL_API DECL_FUNCEXT4(void*, alMapBuffer,SOFT, ALuint,buffer, ALsizei,offset, ALsizei,length, ALbitfieldSOFT,access)
802 FORCE_ALIGN void* AL_APIENTRY alMapBufferDirectSOFT(ALCcontext *context, ALuint buffer,
803 ALsizei offset, ALsizei length, ALbitfieldSOFT access) noexcept
804 try {
805 auto *device = context->mALDevice.get();
806 auto buflock = std::lock_guard{device->BufferLock};
808 ALbuffer *albuf{LookupBuffer(device, buffer)};
809 if(!albuf)
810 throw al::context_error{AL_INVALID_NAME, "Invalid buffer ID %u", buffer};
811 if((access&INVALID_MAP_FLAGS) != 0)
812 throw al::context_error{AL_INVALID_VALUE, "Invalid map flags 0x%x",
813 access&INVALID_MAP_FLAGS};
814 if(!(access&MAP_READ_WRITE_FLAGS))
815 throw al::context_error{AL_INVALID_VALUE, "Mapping buffer %u without read or write access",
816 buffer};
818 const ALbitfieldSOFT unavailable{(albuf->Access^access) & access};
819 if(albuf->ref.load(std::memory_order_relaxed) != 0 && !(access&AL_MAP_PERSISTENT_BIT_SOFT))
820 throw al::context_error{AL_INVALID_OPERATION,
821 "Mapping in-use buffer %u without persistent mapping", buffer};
822 if(albuf->MappedAccess != 0)
823 throw al::context_error{AL_INVALID_OPERATION, "Mapping already-mapped buffer %u", buffer};
824 if((unavailable&AL_MAP_READ_BIT_SOFT))
825 throw al::context_error{AL_INVALID_VALUE,
826 "Mapping buffer %u for reading without read access", buffer};
827 if((unavailable&AL_MAP_WRITE_BIT_SOFT))
828 throw al::context_error{AL_INVALID_VALUE,
829 "Mapping buffer %u for writing without write access", buffer};
830 if((unavailable&AL_MAP_PERSISTENT_BIT_SOFT))
831 throw al::context_error{AL_INVALID_VALUE,
832 "Mapping buffer %u persistently without persistent access", buffer};
833 if(offset < 0 || length <= 0 || static_cast<ALuint>(offset) >= albuf->OriginalSize
834 || static_cast<ALuint>(length) > albuf->OriginalSize - static_cast<ALuint>(offset))
835 throw al::context_error{AL_INVALID_VALUE, "Mapping invalid range %d+%d for buffer %u",
836 offset, length, buffer};
838 void *retval{albuf->mData.data() + offset};
839 albuf->MappedAccess = access;
840 albuf->MappedOffset = offset;
841 albuf->MappedSize = length;
842 return retval;
844 catch(al::context_error& e) {
845 context->setError(e.errorCode(), "%s", e.what());
846 return nullptr;
849 AL_API DECL_FUNCEXT1(void, alUnmapBuffer,SOFT, ALuint,buffer)
850 FORCE_ALIGN void AL_APIENTRY alUnmapBufferDirectSOFT(ALCcontext *context, ALuint buffer) noexcept
851 try {
852 auto *device = context->mALDevice.get();
853 auto buflock = std::lock_guard{device->BufferLock};
855 ALbuffer *albuf{LookupBuffer(device, buffer)};
856 if(!albuf)
857 throw al::context_error{AL_INVALID_NAME, "Invalid buffer ID %u", buffer};
858 if(albuf->MappedAccess == 0)
859 throw al::context_error{AL_INVALID_OPERATION, "Unmapping unmapped buffer %u", buffer};
861 albuf->MappedAccess = 0;
862 albuf->MappedOffset = 0;
863 albuf->MappedSize = 0;
865 catch(al::context_error& e) {
866 context->setError(e.errorCode(), "%s", e.what());
869 AL_API DECL_FUNCEXT3(void, alFlushMappedBuffer,SOFT, ALuint,buffer, ALsizei,offset, ALsizei,length)
870 FORCE_ALIGN void AL_APIENTRY alFlushMappedBufferDirectSOFT(ALCcontext *context, ALuint buffer,
871 ALsizei offset, ALsizei length) noexcept
872 try {
873 auto *device = context->mALDevice.get();
874 auto buflock = std::lock_guard{device->BufferLock};
876 ALbuffer *albuf{LookupBuffer(device, buffer)};
877 if(!albuf)
878 throw al::context_error{AL_INVALID_NAME, "Invalid buffer ID %u", buffer};
879 if(!(albuf->MappedAccess&AL_MAP_WRITE_BIT_SOFT))
880 throw al::context_error{AL_INVALID_OPERATION,
881 "Flushing buffer %u while not mapped for writing", buffer};
882 if(offset < albuf->MappedOffset || length <= 0
883 || offset >= albuf->MappedOffset+albuf->MappedSize
884 || length > albuf->MappedOffset+albuf->MappedSize-offset)
885 throw al::context_error{AL_INVALID_VALUE, "Flushing invalid range %d+%d on buffer %u",
886 offset, length, buffer};
888 /* FIXME: Need to use some method of double-buffering for the mixer and app
889 * to hold separate memory, which can be safely transferred asynchronously.
890 * Currently we just say the app shouldn't write where OpenAL's reading,
891 * and hope for the best...
893 std::atomic_thread_fence(std::memory_order_seq_cst);
895 catch(al::context_error& e) {
896 context->setError(e.errorCode(), "%s", e.what());
899 AL_API DECL_FUNCEXT5(void, alBufferSubData,SOFT, ALuint,buffer, ALenum,format, const ALvoid*,data, ALsizei,offset, ALsizei,length)
900 FORCE_ALIGN void AL_APIENTRY alBufferSubDataDirectSOFT(ALCcontext *context, ALuint buffer,
901 ALenum format, const ALvoid *data, ALsizei offset, ALsizei length) noexcept
902 try {
903 auto *device = context->mALDevice.get();
904 auto buflock = std::lock_guard{device->BufferLock};
906 ALbuffer *albuf{LookupBuffer(device, buffer)};
907 if(!albuf)
908 throw al::context_error{AL_INVALID_NAME, "Invalid buffer ID %u", buffer};
910 auto usrfmt = DecomposeUserFormat(format);
911 if(!usrfmt)
912 throw al::context_error{AL_INVALID_ENUM, "Invalid format 0x%04x", format};
914 const ALuint unpack_align{albuf->UnpackAlign};
915 const ALuint align{SanitizeAlignment(usrfmt->type, unpack_align)};
916 if(align < 1)
917 throw al::context_error{AL_INVALID_VALUE, "Invalid unpack alignment %u", unpack_align};
918 if(usrfmt->channels != albuf->mChannels || usrfmt->type != albuf->mType)
919 throw al::context_error{AL_INVALID_ENUM, "Unpacking data with mismatched format"};
920 if(align != albuf->mBlockAlign)
921 throw al::context_error{AL_INVALID_VALUE,
922 "Unpacking data with alignment %u does not match original alignment %u", align,
923 albuf->mBlockAlign};
924 if(albuf->isBFormat() && albuf->UnpackAmbiOrder != albuf->mAmbiOrder)
925 throw al::context_error{AL_INVALID_VALUE,
926 "Unpacking data with mismatched ambisonic order"};
927 if(albuf->MappedAccess != 0)
928 throw al::context_error{AL_INVALID_OPERATION, "Unpacking data into mapped buffer %u",
929 buffer};
931 const ALuint num_chans{albuf->channelsFromFmt()};
932 const ALuint byte_align{
933 (albuf->mType == FmtIMA4) ? ((align-1)/2 + 4) * num_chans :
934 (albuf->mType == FmtMSADPCM) ? ((align-2)/2 + 7) * num_chans :
935 (align * albuf->bytesFromFmt() * num_chans)};
937 if(offset < 0 || length < 0 || static_cast<ALuint>(offset) > albuf->OriginalSize
938 || static_cast<ALuint>(length) > albuf->OriginalSize-static_cast<ALuint>(offset))
939 throw al::context_error{AL_INVALID_VALUE, "Invalid data sub-range %d+%d on buffer %u",
940 offset, length, buffer};
941 if((static_cast<ALuint>(offset)%byte_align) != 0)
942 throw al::context_error{AL_INVALID_VALUE,
943 "Sub-range offset %d is not a multiple of frame size %d (%d unpack alignment)",
944 offset, byte_align, align};
945 if((static_cast<ALuint>(length)%byte_align) != 0)
946 throw al::context_error{AL_INVALID_VALUE,
947 "Sub-range length %d is not a multiple of frame size %d (%d unpack alignment)",
948 length, byte_align, align};
950 std::memcpy(albuf->mData.data()+offset, data, static_cast<ALuint>(length));
952 catch(al::context_error& e) {
953 context->setError(e.errorCode(), "%s", e.what());
957 AL_API DECL_FUNC3(void, alBufferf, ALuint,buffer, ALenum,param, ALfloat,value)
958 FORCE_ALIGN void AL_APIENTRY alBufferfDirect(ALCcontext *context, ALuint buffer, ALenum param,
959 ALfloat value [[maybe_unused]]) noexcept
960 try {
961 auto *device = context->mALDevice.get();
962 auto buflock = std::lock_guard{device->BufferLock};
964 if(LookupBuffer(device, buffer) == nullptr)
965 throw al::context_error{AL_INVALID_NAME, "Invalid buffer ID %u", buffer};
967 switch(param)
970 throw al::context_error{AL_INVALID_ENUM, "Invalid buffer float property 0x%04x", param};
972 catch(al::context_error& e) {
973 context->setError(e.errorCode(), "%s", e.what());
976 AL_API DECL_FUNC5(void, alBuffer3f, ALuint,buffer, ALenum,param, ALfloat,value1, ALfloat,value2, ALfloat,value3)
977 FORCE_ALIGN void AL_APIENTRY alBuffer3fDirect(ALCcontext *context, ALuint buffer, ALenum param,
978 ALfloat value1 [[maybe_unused]], ALfloat value2 [[maybe_unused]],
979 ALfloat value3 [[maybe_unused]]) noexcept
980 try {
981 auto *device = context->mALDevice.get();
982 auto buflock = std::lock_guard{device->BufferLock};
984 if(LookupBuffer(device, buffer) == nullptr)
985 throw al::context_error{AL_INVALID_NAME, "Invalid buffer ID %u", buffer};
987 switch(param)
990 throw al::context_error{AL_INVALID_ENUM, "Invalid buffer 3-float property 0x%04x", param};
992 catch(al::context_error& e) {
993 context->setError(e.errorCode(), "%s", e.what());
996 AL_API DECL_FUNC3(void, alBufferfv, ALuint,buffer, ALenum,param, const ALfloat*,values)
997 FORCE_ALIGN void AL_APIENTRY alBufferfvDirect(ALCcontext *context, ALuint buffer, ALenum param,
998 const ALfloat *values) noexcept
999 try {
1000 auto *device = context->mALDevice.get();
1001 auto buflock = std::lock_guard{device->BufferLock};
1003 if(LookupBuffer(device, buffer) == nullptr)
1004 throw al::context_error{AL_INVALID_NAME, "Invalid buffer ID %u", buffer};
1005 if(!values)
1006 throw al::context_error{AL_INVALID_VALUE, "NULL pointer"};
1008 switch(param)
1011 throw al::context_error{AL_INVALID_ENUM, "Invalid buffer float-vector property 0x%04x", param};
1013 catch(al::context_error& e) {
1014 context->setError(e.errorCode(), "%s", e.what());
1018 AL_API DECL_FUNC3(void, alBufferi, ALuint,buffer, ALenum,param, ALint,value)
1019 FORCE_ALIGN void AL_APIENTRY alBufferiDirect(ALCcontext *context, ALuint buffer, ALenum param,
1020 ALint value) noexcept
1021 try {
1022 auto *device = context->mALDevice.get();
1023 auto buflock = std::lock_guard{device->BufferLock};
1025 ALbuffer *albuf{LookupBuffer(device, buffer)};
1026 if(!albuf)
1027 throw al::context_error{AL_INVALID_NAME, "Invalid buffer ID %u", buffer};
1029 switch(param)
1031 case AL_UNPACK_BLOCK_ALIGNMENT_SOFT:
1032 if(value < 0)
1033 throw al::context_error{AL_INVALID_VALUE, "Invalid unpack block alignment %d", value};
1034 albuf->UnpackAlign = static_cast<ALuint>(value);
1035 return;
1037 case AL_PACK_BLOCK_ALIGNMENT_SOFT:
1038 if(value < 0)
1039 throw al::context_error{AL_INVALID_VALUE, "Invalid pack block alignment %d", value};
1040 albuf->PackAlign = static_cast<ALuint>(value);
1041 return;
1043 case AL_AMBISONIC_LAYOUT_SOFT:
1044 if(albuf->ref.load(std::memory_order_relaxed) != 0)
1045 throw al::context_error{AL_INVALID_OPERATION,
1046 "Modifying in-use buffer %u's ambisonic layout", buffer};
1047 if(const auto layout = AmbiLayoutFromEnum(value))
1049 albuf->mAmbiLayout = layout.value();
1050 return;
1052 throw al::context_error{AL_INVALID_VALUE, "Invalid unpack ambisonic layout %04x", value};
1054 case AL_AMBISONIC_SCALING_SOFT:
1055 if(albuf->ref.load(std::memory_order_relaxed) != 0)
1056 throw al::context_error{AL_INVALID_OPERATION,
1057 "Modifying in-use buffer %u's ambisonic scaling", buffer};
1058 if(const auto scaling = AmbiScalingFromEnum(value))
1060 albuf->mAmbiScaling = scaling.value();
1061 return;
1063 throw al::context_error{AL_INVALID_VALUE, "Invalid unpack ambisonic scaling %04x", value};
1065 case AL_UNPACK_AMBISONIC_ORDER_SOFT:
1066 if(value < 1 || value > 14)
1067 throw al::context_error{AL_INVALID_VALUE, "Invalid unpack ambisonic order %d", value};
1068 albuf->UnpackAmbiOrder = static_cast<ALuint>(value);
1069 return;
1072 throw al::context_error{AL_INVALID_ENUM, "Invalid buffer integer property 0x%04x", param};
1074 catch(al::context_error& e) {
1075 context->setError(e.errorCode(), "%s", e.what());
1078 AL_API DECL_FUNC5(void, alBuffer3i, ALuint,buffer, ALenum,param, ALint,value1, ALint,value2, ALint,value3)
1079 FORCE_ALIGN void AL_APIENTRY alBuffer3iDirect(ALCcontext *context, ALuint buffer, ALenum param,
1080 ALint value1 [[maybe_unused]], ALint value2 [[maybe_unused]], ALint value3 [[maybe_unused]]) noexcept
1081 try {
1082 auto *device = context->mALDevice.get();
1083 auto buflock = std::lock_guard{device->BufferLock};
1085 if(LookupBuffer(device, buffer) == nullptr)
1086 throw al::context_error{AL_INVALID_NAME, "Invalid buffer ID %u", buffer};
1088 switch(param)
1091 throw al::context_error{AL_INVALID_ENUM, "Invalid buffer 3-integer property 0x%04x", param};
1093 catch(al::context_error& e) {
1094 context->setError(e.errorCode(), "%s", e.what());
1097 AL_API DECL_FUNC3(void, alBufferiv, ALuint,buffer, ALenum,param, const ALint*,values)
1098 FORCE_ALIGN void AL_APIENTRY alBufferivDirect(ALCcontext *context, ALuint buffer, ALenum param,
1099 const ALint *values) noexcept
1100 try {
1101 if(!values)
1102 throw al::context_error{AL_INVALID_VALUE, "NULL pointer"};
1104 switch(param)
1106 case AL_UNPACK_BLOCK_ALIGNMENT_SOFT:
1107 case AL_PACK_BLOCK_ALIGNMENT_SOFT:
1108 case AL_AMBISONIC_LAYOUT_SOFT:
1109 case AL_AMBISONIC_SCALING_SOFT:
1110 case AL_UNPACK_AMBISONIC_ORDER_SOFT:
1111 alBufferiDirect(context, buffer, param, *values);
1112 return;
1115 auto *device = context->mALDevice.get();
1116 auto buflock = std::lock_guard{device->BufferLock};
1118 ALbuffer *albuf{LookupBuffer(device, buffer)};
1119 if(!albuf)
1120 throw al::context_error{AL_INVALID_NAME, "Invalid buffer ID %u", buffer};
1122 switch(param)
1124 case AL_LOOP_POINTS_SOFT:
1125 auto vals = al::span{values, 2_uz};
1126 if(albuf->ref.load(std::memory_order_relaxed) != 0)
1127 throw al::context_error{AL_INVALID_OPERATION,
1128 "Modifying in-use buffer %u's loop points", buffer};
1129 if(vals[0] < 0 || vals[0] >= vals[1] || static_cast<ALuint>(vals[1]) > albuf->mSampleLen)
1130 throw al::context_error{AL_INVALID_VALUE,
1131 "Invalid loop point range %d -> %d on buffer %u", vals[0], vals[1], buffer};
1133 albuf->mLoopStart = static_cast<ALuint>(vals[0]);
1134 albuf->mLoopEnd = static_cast<ALuint>(vals[1]);
1135 return;
1138 throw al::context_error{AL_INVALID_ENUM, "Invalid buffer integer-vector property 0x%04x",
1139 param};
1141 catch(al::context_error& e) {
1142 context->setError(e.errorCode(), "%s", e.what());
1146 AL_API DECL_FUNC3(void, alGetBufferf, ALuint,buffer, ALenum,param, ALfloat*,value)
1147 FORCE_ALIGN void AL_APIENTRY alGetBufferfDirect(ALCcontext *context, ALuint buffer, ALenum param,
1148 ALfloat *value) noexcept
1149 try {
1150 auto *device = context->mALDevice.get();
1151 auto buflock = std::lock_guard{device->BufferLock};
1153 ALbuffer *albuf{LookupBuffer(device, buffer)};
1154 if(!albuf)
1155 throw al::context_error{AL_INVALID_NAME, "Invalid buffer ID %u", buffer};
1156 if(!value)
1157 throw al::context_error{AL_INVALID_VALUE, "NULL pointer"};
1159 switch(param)
1161 case AL_SEC_LENGTH_SOFT:
1162 *value = (albuf->mSampleRate < 1) ? 0.0f :
1163 (static_cast<float>(albuf->mSampleLen) / static_cast<float>(albuf->mSampleRate));
1164 return;
1167 throw al::context_error{AL_INVALID_ENUM, "Invalid buffer float property 0x%04x", param};
1169 catch(al::context_error& e) {
1170 context->setError(e.errorCode(), "%s", e.what());
1173 AL_API DECL_FUNC5(void, alGetBuffer3f, ALuint,buffer, ALenum,param, ALfloat*,value1, ALfloat*,value2, ALfloat*,value3)
1174 FORCE_ALIGN void AL_APIENTRY alGetBuffer3fDirect(ALCcontext *context, ALuint buffer, ALenum param,
1175 ALfloat *value1, ALfloat *value2, ALfloat *value3) noexcept
1176 try {
1177 auto *device = context->mALDevice.get();
1178 auto buflock = std::lock_guard{device->BufferLock};
1180 if(LookupBuffer(device, buffer) == nullptr)
1181 throw al::context_error{AL_INVALID_NAME, "Invalid buffer ID %u", buffer};
1182 if(!value1 || !value2 || !value3)
1183 throw al::context_error{AL_INVALID_VALUE, "NULL pointer"};
1185 switch(param)
1188 throw al::context_error{AL_INVALID_ENUM, "Invalid buffer 3-float property 0x%04x", param};
1190 catch(al::context_error& e) {
1191 context->setError(e.errorCode(), "%s", e.what());
1194 AL_API DECL_FUNC3(void, alGetBufferfv, ALuint,buffer, ALenum,param, ALfloat*,values)
1195 FORCE_ALIGN void AL_APIENTRY alGetBufferfvDirect(ALCcontext *context, ALuint buffer, ALenum param,
1196 ALfloat *values) noexcept
1197 try {
1198 switch(param)
1200 case AL_SEC_LENGTH_SOFT:
1201 alGetBufferfDirect(context, buffer, param, values);
1202 return;
1205 auto *device = context->mALDevice.get();
1206 auto buflock = std::lock_guard{device->BufferLock};
1208 if(LookupBuffer(device, buffer) == nullptr)
1209 throw al::context_error{AL_INVALID_NAME, "Invalid buffer ID %u", buffer};
1210 if(!values)
1211 throw al::context_error{AL_INVALID_VALUE, "NULL pointer"};
1213 switch(param)
1216 throw al::context_error{AL_INVALID_ENUM, "Invalid buffer float-vector property 0x%04x", param};
1218 catch(al::context_error& e) {
1219 context->setError(e.errorCode(), "%s", e.what());
1223 AL_API DECL_FUNC3(void, alGetBufferi, ALuint,buffer, ALenum,param, ALint*,value)
1224 FORCE_ALIGN void AL_APIENTRY alGetBufferiDirect(ALCcontext *context, ALuint buffer, ALenum param,
1225 ALint *value) noexcept
1226 try {
1227 auto *device = context->mALDevice.get();
1228 auto buflock = std::lock_guard{device->BufferLock};
1230 ALbuffer *albuf{LookupBuffer(device, buffer)};
1231 if(!albuf)
1232 throw al::context_error{AL_INVALID_NAME, "Invalid buffer ID %u", buffer};
1233 if(!value)
1234 throw al::context_error{AL_INVALID_VALUE, "NULL pointer"};
1236 switch(param)
1238 case AL_FREQUENCY:
1239 *value = static_cast<ALint>(albuf->mSampleRate);
1240 return;
1242 case AL_BITS:
1243 *value = (albuf->mType == FmtIMA4 || albuf->mType == FmtMSADPCM) ? 4
1244 : static_cast<ALint>(albuf->bytesFromFmt() * 8);
1245 return;
1247 case AL_CHANNELS:
1248 *value = static_cast<ALint>(albuf->channelsFromFmt());
1249 return;
1251 case AL_SIZE:
1252 *value = albuf->mCallback ? 0 : static_cast<ALint>(albuf->mData.size());
1253 return;
1255 case AL_BYTE_LENGTH_SOFT:
1256 *value = static_cast<ALint>(albuf->mSampleLen / albuf->mBlockAlign
1257 * albuf->blockSizeFromFmt());
1258 return;
1260 case AL_SAMPLE_LENGTH_SOFT:
1261 *value = static_cast<ALint>(albuf->mSampleLen);
1262 return;
1264 case AL_UNPACK_BLOCK_ALIGNMENT_SOFT:
1265 *value = static_cast<ALint>(albuf->UnpackAlign);
1266 return;
1268 case AL_PACK_BLOCK_ALIGNMENT_SOFT:
1269 *value = static_cast<ALint>(albuf->PackAlign);
1270 return;
1272 case AL_AMBISONIC_LAYOUT_SOFT:
1273 *value = EnumFromAmbiLayout(albuf->mAmbiLayout);
1274 return;
1276 case AL_AMBISONIC_SCALING_SOFT:
1277 *value = EnumFromAmbiScaling(albuf->mAmbiScaling);
1278 return;
1280 case AL_UNPACK_AMBISONIC_ORDER_SOFT:
1281 *value = static_cast<int>(albuf->UnpackAmbiOrder);
1282 return;
1285 throw al::context_error{AL_INVALID_ENUM, "Invalid buffer integer property 0x%04x", param};
1287 catch(al::context_error& e) {
1288 context->setError(e.errorCode(), "%s", e.what());
1291 AL_API DECL_FUNC5(void, alGetBuffer3i, ALuint,buffer, ALenum,param, ALint*,value1, ALint*,value2, ALint*,value3)
1292 FORCE_ALIGN void AL_APIENTRY alGetBuffer3iDirect(ALCcontext *context, ALuint buffer, ALenum param,
1293 ALint *value1, ALint *value2, ALint *value3) noexcept
1294 try {
1295 auto *device = context->mALDevice.get();
1296 auto buflock = std::lock_guard{device->BufferLock};
1298 if(LookupBuffer(device, buffer) == nullptr)
1299 throw al::context_error{AL_INVALID_NAME, "Invalid buffer ID %u", buffer};
1300 if(!value1 || !value2 || !value3)
1301 throw al::context_error{AL_INVALID_VALUE, "NULL pointer"};
1303 switch(param)
1306 throw al::context_error{AL_INVALID_ENUM, "Invalid buffer 3-integer property 0x%04x", param};
1308 catch(al::context_error& e) {
1309 context->setError(e.errorCode(), "%s", e.what());
1312 AL_API DECL_FUNC3(void, alGetBufferiv, ALuint,buffer, ALenum,param, ALint*,values)
1313 FORCE_ALIGN void AL_APIENTRY alGetBufferivDirect(ALCcontext *context, ALuint buffer, ALenum param,
1314 ALint *values) noexcept
1315 try {
1316 switch(param)
1318 case AL_FREQUENCY:
1319 case AL_BITS:
1320 case AL_CHANNELS:
1321 case AL_SIZE:
1322 case AL_INTERNAL_FORMAT_SOFT:
1323 case AL_BYTE_LENGTH_SOFT:
1324 case AL_SAMPLE_LENGTH_SOFT:
1325 case AL_UNPACK_BLOCK_ALIGNMENT_SOFT:
1326 case AL_PACK_BLOCK_ALIGNMENT_SOFT:
1327 case AL_AMBISONIC_LAYOUT_SOFT:
1328 case AL_AMBISONIC_SCALING_SOFT:
1329 case AL_UNPACK_AMBISONIC_ORDER_SOFT:
1330 alGetBufferiDirect(context, buffer, param, values);
1331 return;
1334 auto *device = context->mALDevice.get();
1335 auto buflock = std::lock_guard{device->BufferLock};
1337 ALbuffer *albuf{LookupBuffer(device, buffer)};
1338 if(!albuf)
1339 throw al::context_error{AL_INVALID_NAME, "Invalid buffer ID %u", buffer};
1340 if(!values)
1341 throw al::context_error{AL_INVALID_VALUE, "NULL pointer"};
1343 switch(param)
1345 case AL_LOOP_POINTS_SOFT:
1346 auto vals = al::span{values, 2_uz};
1347 vals[0] = static_cast<ALint>(albuf->mLoopStart);
1348 vals[1] = static_cast<ALint>(albuf->mLoopEnd);
1349 return;
1352 throw al::context_error{AL_INVALID_ENUM, "Invalid buffer integer-vector property 0x%04x",
1353 param};
1355 catch(al::context_error& e) {
1356 context->setError(e.errorCode(), "%s", e.what());
1360 AL_API DECL_FUNCEXT5(void, alBufferCallback,SOFT, ALuint,buffer, ALenum,format, ALsizei,freq, ALBUFFERCALLBACKTYPESOFT,callback, ALvoid*,userptr)
1361 FORCE_ALIGN void AL_APIENTRY alBufferCallbackDirectSOFT(ALCcontext *context, ALuint buffer,
1362 ALenum format, ALsizei freq, ALBUFFERCALLBACKTYPESOFT callback, ALvoid *userptr) noexcept
1363 try {
1364 auto *device = context->mALDevice.get();
1365 auto buflock = std::lock_guard{device->BufferLock};
1367 ALbuffer *albuf{LookupBuffer(device, buffer)};
1368 if(!albuf)
1369 throw al::context_error{AL_INVALID_NAME, "Invalid buffer ID %u", buffer};
1370 if(freq < 1)
1371 throw al::context_error{AL_INVALID_VALUE, "Invalid sample rate %d", freq};
1372 if(callback == nullptr)
1373 throw al::context_error{AL_INVALID_VALUE, "NULL callback"};
1375 auto usrfmt = DecomposeUserFormat(format);
1376 if(!usrfmt)
1377 throw al::context_error{AL_INVALID_ENUM, "Invalid format 0x%04x", format};
1379 PrepareCallback(context, albuf, freq, usrfmt->channels, usrfmt->type, callback, userptr);
1381 catch(al::context_error& e) {
1382 context->setError(e.errorCode(), "%s", e.what());
1385 AL_API DECL_FUNCEXT3(void, alGetBufferPtr,SOFT, ALuint,buffer, ALenum,param, ALvoid**,value)
1386 FORCE_ALIGN void AL_APIENTRY alGetBufferPtrDirectSOFT(ALCcontext *context, ALuint buffer,
1387 ALenum param, ALvoid **value) noexcept
1388 try {
1389 auto *device = context->mALDevice.get();
1390 auto buflock = std::lock_guard{device->BufferLock};
1392 ALbuffer *albuf{LookupBuffer(device, buffer)};
1393 if(!albuf)
1394 throw al::context_error{AL_INVALID_NAME, "Invalid buffer ID %u", buffer};
1395 if(!value)
1396 throw al::context_error{AL_INVALID_VALUE, "NULL pointer"};
1398 switch(param)
1400 case AL_BUFFER_CALLBACK_FUNCTION_SOFT:
1401 *value = reinterpret_cast<void*>(albuf->mCallback);
1402 return;
1403 case AL_BUFFER_CALLBACK_USER_PARAM_SOFT:
1404 *value = albuf->mUserData;
1405 return;
1408 throw al::context_error{AL_INVALID_ENUM, "Invalid buffer pointer property 0x%04x", param};
1410 catch(al::context_error& e) {
1411 context->setError(e.errorCode(), "%s", e.what());
1414 AL_API DECL_FUNCEXT5(void, alGetBuffer3Ptr,SOFT, ALuint,buffer, ALenum,param, ALvoid**,value1, ALvoid**,value2, ALvoid**,value3)
1415 FORCE_ALIGN void AL_APIENTRY alGetBuffer3PtrDirectSOFT(ALCcontext *context, ALuint buffer,
1416 ALenum param, ALvoid **value1, ALvoid **value2, ALvoid **value3) noexcept
1417 try {
1418 auto *device = context->mALDevice.get();
1419 auto buflock = std::lock_guard{device->BufferLock};
1421 if(LookupBuffer(device, buffer) == nullptr)
1422 throw al::context_error{AL_INVALID_NAME, "Invalid buffer ID %u", buffer};
1423 if(!value1 || !value2 || !value3)
1424 throw al::context_error{AL_INVALID_VALUE, "NULL pointer"};
1426 switch(param)
1429 throw al::context_error{AL_INVALID_ENUM, "Invalid buffer 3-pointer property 0x%04x", param};
1431 catch(al::context_error& e) {
1432 context->setError(e.errorCode(), "%s", e.what());
1435 AL_API DECL_FUNCEXT3(void, alGetBufferPtrv,SOFT, ALuint,buffer, ALenum,param, ALvoid**,values)
1436 FORCE_ALIGN void AL_APIENTRY alGetBufferPtrvDirectSOFT(ALCcontext *context, ALuint buffer,
1437 ALenum param, ALvoid **values) noexcept
1438 try {
1439 switch(param)
1441 case AL_BUFFER_CALLBACK_FUNCTION_SOFT:
1442 case AL_BUFFER_CALLBACK_USER_PARAM_SOFT:
1443 alGetBufferPtrDirectSOFT(context, buffer, param, values);
1444 return;
1447 auto *device = context->mALDevice.get();
1448 auto buflock = std::lock_guard{device->BufferLock};
1450 if(LookupBuffer(device, buffer) == nullptr)
1451 throw al::context_error{AL_INVALID_NAME, "Invalid buffer ID %u", buffer};
1452 if(!values)
1453 throw al::context_error{AL_INVALID_VALUE, "NULL pointer"};
1455 switch(param)
1458 throw al::context_error{AL_INVALID_ENUM, "Invalid buffer pointer-vector property 0x%04x",
1459 param};
1461 catch(al::context_error& e) {
1462 context->setError(e.errorCode(), "%s", e.what());
1466 AL_API void AL_APIENTRY alBufferSamplesSOFT(ALuint /*buffer*/, ALuint /*samplerate*/,
1467 ALenum /*internalformat*/, ALsizei /*samples*/, ALenum /*channels*/, ALenum /*type*/,
1468 const ALvoid* /*data*/) noexcept
1470 ContextRef context{GetContextRef()};
1471 if(!context) UNLIKELY return;
1473 context->setError(AL_INVALID_OPERATION, "alBufferSamplesSOFT not supported");
1476 AL_API void AL_APIENTRY alBufferSubSamplesSOFT(ALuint /*buffer*/, ALsizei /*offset*/,
1477 ALsizei /*samples*/, ALenum /*channels*/, ALenum /*type*/, const ALvoid* /*data*/) noexcept
1479 ContextRef context{GetContextRef()};
1480 if(!context) UNLIKELY return;
1482 context->setError(AL_INVALID_OPERATION, "alBufferSubSamplesSOFT not supported");
1485 AL_API void AL_APIENTRY alGetBufferSamplesSOFT(ALuint /*buffer*/, ALsizei /*offset*/,
1486 ALsizei /*samples*/, ALenum /*channels*/, ALenum /*type*/, ALvoid* /*data*/) noexcept
1488 ContextRef context{GetContextRef()};
1489 if(!context) UNLIKELY return;
1491 context->setError(AL_INVALID_OPERATION, "alGetBufferSamplesSOFT not supported");
1494 AL_API ALboolean AL_APIENTRY alIsBufferFormatSupportedSOFT(ALenum /*format*/) noexcept
1496 ContextRef context{GetContextRef()};
1497 if(!context) UNLIKELY return AL_FALSE;
1499 context->setError(AL_INVALID_OPERATION, "alIsBufferFormatSupportedSOFT not supported");
1500 return AL_FALSE;
1504 void ALbuffer::SetName(ALCcontext *context, ALuint id, std::string_view name)
1506 auto *device = context->mALDevice.get();
1507 auto buflock = std::lock_guard{device->BufferLock};
1509 auto buffer = LookupBuffer(device, id);
1510 if(!buffer)
1511 throw al::context_error{AL_INVALID_NAME, "Invalid buffer ID %u", id};
1513 device->mBufferNames.insert_or_assign(id, name);
1517 BufferSubList::~BufferSubList()
1519 if(!Buffers)
1520 return;
1522 uint64_t usemask{~FreeMask};
1523 while(usemask)
1525 const int idx{al::countr_zero(usemask)};
1526 std::destroy_at(al::to_address(Buffers->begin() + idx));
1527 usemask &= ~(1_u64 << idx);
1529 FreeMask = ~usemask;
1530 SubListAllocator{}.deallocate(Buffers, 1);
1531 Buffers = nullptr;
1535 #if ALSOFT_EAX
1536 FORCE_ALIGN DECL_FUNC3(ALboolean, EAXSetBufferMode, ALsizei,n, const ALuint*,buffers, ALint,value)
1537 FORCE_ALIGN ALboolean AL_APIENTRY EAXSetBufferModeDirect(ALCcontext *context, ALsizei n,
1538 const ALuint *buffers, ALint value) noexcept
1539 try {
1540 if(!eax_g_is_enabled)
1541 throw al::context_error{AL_INVALID_OPERATION, "EAX not enabled"};
1543 const auto storage = EaxStorageFromEnum(value);
1544 if(!storage)
1545 throw al::context_error{AL_INVALID_ENUM, "Unsupported X-RAM mode 0x%x", value};
1547 if(n == 0)
1548 return AL_TRUE;
1550 if(n < 0)
1551 throw al::context_error{AL_INVALID_VALUE, "Buffer count %d out of range", n};
1552 if(!buffers)
1553 throw al::context_error{AL_INVALID_VALUE, "Null AL buffers"};
1555 auto device = context->mALDevice.get();
1556 std::lock_guard<std::mutex> devlock{device->BufferLock};
1558 /* Special-case setting a single buffer, to avoid extraneous allocations. */
1559 if(n == 1)
1561 const auto bufid = *buffers;
1562 if(bufid == AL_NONE)
1563 return AL_TRUE;
1565 const auto buffer = LookupBuffer(device, bufid);
1566 if(!buffer)
1567 throw al::context_error{AL_INVALID_NAME, "Invalid buffer ID %u", bufid};
1569 /* TODO: Is the store location allowed to change for in-use buffers, or
1570 * only when not set/queued on a source?
1573 if(*storage == EaxStorage::Hardware)
1575 if(!buffer->eax_x_ram_is_hardware
1576 && buffer->OriginalSize > device->eax_x_ram_free_size)
1577 throw al::context_error{AL_OUT_OF_MEMORY,
1578 "Out of X-RAM memory (need: %u, avail: %u)", buffer->OriginalSize,
1579 device->eax_x_ram_free_size};
1581 eax_x_ram_apply(*device, *buffer);
1583 else
1584 eax_x_ram_clear(*device, *buffer);
1585 buffer->eax_x_ram_mode = *storage;
1586 return AL_TRUE;
1589 /* Validate the buffers. */
1590 std::unordered_set<ALbuffer*> buflist;
1591 for(const ALuint bufid : al::span{buffers, static_cast<ALuint>(n)})
1593 if(bufid == AL_NONE)
1594 continue;
1596 const auto buffer = LookupBuffer(device, bufid);
1597 if(!buffer)
1598 throw al::context_error{AL_INVALID_NAME, "Invalid buffer ID %u", bufid};
1600 /* TODO: Is the store location allowed to change for in-use buffers, or
1601 * only when not set/queued on a source?
1604 buflist.emplace(buffer);
1607 if(*storage == EaxStorage::Hardware)
1609 size_t total_needed{0};
1610 for(ALbuffer *buffer : buflist)
1612 if(!buffer->eax_x_ram_is_hardware)
1614 if(std::numeric_limits<size_t>::max() - buffer->OriginalSize < total_needed)
1615 throw al::context_error{AL_OUT_OF_MEMORY, "Size overflow (%u + %zu)",
1616 buffer->OriginalSize, total_needed};
1618 total_needed += buffer->OriginalSize;
1621 if(total_needed > device->eax_x_ram_free_size)
1622 throw al::context_error{AL_OUT_OF_MEMORY, "Out of X-RAM memory (need: %zu, avail: %u)",
1623 total_needed, device->eax_x_ram_free_size};
1626 /* Update the mode. */
1627 for(ALbuffer *buffer : buflist)
1629 if(*storage == EaxStorage::Hardware)
1630 eax_x_ram_apply(*device, *buffer);
1631 else
1632 eax_x_ram_clear(*device, *buffer);
1633 buffer->eax_x_ram_mode = *storage;
1636 return AL_TRUE;
1638 catch(al::context_error& e) {
1639 context->setError(e.errorCode(), "[EAXSetBufferMode] %s", e.what());
1640 return AL_FALSE;
1643 FORCE_ALIGN DECL_FUNC2(ALenum, EAXGetBufferMode, ALuint,buffer, ALint*,pReserved)
1644 FORCE_ALIGN ALenum AL_APIENTRY EAXGetBufferModeDirect(ALCcontext *context, ALuint buffer,
1645 ALint *pReserved) noexcept
1646 try {
1648 if(!eax_g_is_enabled)
1649 throw al::context_error{AL_INVALID_OPERATION, "EAX not enabled."};
1651 if(pReserved)
1652 throw al::context_error{AL_INVALID_VALUE, "Non-null reserved parameter"};
1654 auto device = context->mALDevice.get();
1655 std::lock_guard<std::mutex> devlock{device->BufferLock};
1657 const auto al_buffer = LookupBuffer(device, buffer);
1658 if(!al_buffer)
1659 throw al::context_error{AL_INVALID_NAME, "Invalid buffer ID %u", buffer};
1661 return EnumFromEaxStorage(al_buffer->eax_x_ram_mode);
1663 catch(al::context_error& e) {
1664 context->setError(e.errorCode(), "[EAXGetBufferMode] %s", e.what());
1665 return AL_NONE;
1668 #endif // ALSOFT_EAX