1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
|
/**
* OpenAL cross platform audio library
* Copyright (C) 1999-2007 by authors.
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
* Or go to http://www.gnu.org/copyleft/lgpl.html
*/
#include "config.h"
#include "buffer.h"
#include <algorithm>
#include <array>
#include <atomic>
#include <cassert>
#include <cstddef>
#include <cstdint>
#include <cstdlib>
#include <cstring>
#include <iterator>
#include <limits>
#include <memory>
#include <mutex>
#include <new>
#include <numeric>
#include <optional>
#include <stdexcept>
#include <utility>
#include <vector>
#include "AL/al.h"
#include "AL/alc.h"
#include "AL/alext.h"
#include "albit.h"
#include "alc/context.h"
#include "alc/device.h"
#include "alc/inprogext.h"
#include "almalloc.h"
#include "alnumeric.h"
#include "atomic.h"
#include "core/except.h"
#include "core/logging.h"
#include "core/voice.h"
#include "direct_defs.h"
#include "opthelpers.h"
#ifdef ALSOFT_EAX
#include <unordered_set>
#include "eax/globals.h"
#include "eax/x_ram.h"
#endif // ALSOFT_EAX
namespace {
using SubListAllocator = typename al::allocator<std::array<ALbuffer,64>>;
std::optional<AmbiLayout> AmbiLayoutFromEnum(ALenum layout)
{
switch(layout)
{
case AL_FUMA_SOFT: return AmbiLayout::FuMa;
case AL_ACN_SOFT: return AmbiLayout::ACN;
}
return std::nullopt;
}
ALenum EnumFromAmbiLayout(AmbiLayout layout)
{
switch(layout)
{
case AmbiLayout::FuMa: return AL_FUMA_SOFT;
case AmbiLayout::ACN: return AL_ACN_SOFT;
}
throw std::runtime_error{"Invalid AmbiLayout: "+std::to_string(int(layout))};
}
std::optional<AmbiScaling> AmbiScalingFromEnum(ALenum scale)
{
switch(scale)
{
case AL_FUMA_SOFT: return AmbiScaling::FuMa;
case AL_SN3D_SOFT: return AmbiScaling::SN3D;
case AL_N3D_SOFT: return AmbiScaling::N3D;
}
return std::nullopt;
}
ALenum EnumFromAmbiScaling(AmbiScaling scale)
{
switch(scale)
{
case AmbiScaling::FuMa: return AL_FUMA_SOFT;
case AmbiScaling::SN3D: return AL_SN3D_SOFT;
case AmbiScaling::N3D: return AL_N3D_SOFT;
case AmbiScaling::UHJ: break;
}
throw std::runtime_error{"Invalid AmbiScaling: "+std::to_string(int(scale))};
}
#ifdef ALSOFT_EAX
std::optional<EaxStorage> EaxStorageFromEnum(ALenum scale)
{
switch(scale)
{
case AL_STORAGE_AUTOMATIC: return EaxStorage::Automatic;
case AL_STORAGE_ACCESSIBLE: return EaxStorage::Accessible;
case AL_STORAGE_HARDWARE: return EaxStorage::Hardware;
}
return std::nullopt;
}
ALenum EnumFromEaxStorage(EaxStorage storage)
{
switch(storage)
{
case EaxStorage::Automatic: return AL_STORAGE_AUTOMATIC;
case EaxStorage::Accessible: return AL_STORAGE_ACCESSIBLE;
case EaxStorage::Hardware: return AL_STORAGE_HARDWARE;
}
throw std::runtime_error{"Invalid EaxStorage: "+std::to_string(int(storage))};
}
bool eax_x_ram_check_availability(const ALCdevice &device, const ALbuffer &buffer,
const ALuint newsize) noexcept
{
ALuint freemem{device.eax_x_ram_free_size};
/* If the buffer is currently in "hardware", add its memory to the free
* pool since it'll be "replaced".
*/
if(buffer.eax_x_ram_is_hardware)
freemem += buffer.OriginalSize;
return freemem >= newsize;
}
void eax_x_ram_apply(ALCdevice &device, ALbuffer &buffer) noexcept
{
if(buffer.eax_x_ram_is_hardware)
return;
if(device.eax_x_ram_free_size >= buffer.OriginalSize)
{
device.eax_x_ram_free_size -= buffer.OriginalSize;
buffer.eax_x_ram_is_hardware = true;
}
}
void eax_x_ram_clear(ALCdevice& al_device, ALbuffer& al_buffer)
{
if(al_buffer.eax_x_ram_is_hardware)
al_device.eax_x_ram_free_size += al_buffer.OriginalSize;
al_buffer.eax_x_ram_is_hardware = false;
}
#endif // ALSOFT_EAX
constexpr ALbitfieldSOFT INVALID_STORAGE_MASK{~unsigned(AL_MAP_READ_BIT_SOFT |
AL_MAP_WRITE_BIT_SOFT | AL_MAP_PERSISTENT_BIT_SOFT | AL_PRESERVE_DATA_BIT_SOFT)};
constexpr ALbitfieldSOFT MAP_READ_WRITE_FLAGS{AL_MAP_READ_BIT_SOFT | AL_MAP_WRITE_BIT_SOFT};
constexpr ALbitfieldSOFT INVALID_MAP_FLAGS{~unsigned(AL_MAP_READ_BIT_SOFT | AL_MAP_WRITE_BIT_SOFT |
AL_MAP_PERSISTENT_BIT_SOFT)};
bool EnsureBuffers(ALCdevice *device, size_t needed)
{
size_t count{std::accumulate(device->BufferList.cbegin(), device->BufferList.cend(), 0_uz,
[](size_t cur, const BufferSubList &sublist) noexcept -> size_t
{ return cur + static_cast<ALuint>(al::popcount(sublist.FreeMask)); })};
try {
while(needed > count)
{
if(device->BufferList.size() >= 1<<25) UNLIKELY
return false;
BufferSubList sublist{};
sublist.FreeMask = ~0_u64;
sublist.Buffers = SubListAllocator{}.allocate(1);
device->BufferList.emplace_back(std::move(sublist));
count += 64;
}
}
catch(...) {
return false;
}
return true;
}
ALbuffer *AllocBuffer(ALCdevice *device)
{
auto sublist = std::find_if(device->BufferList.begin(), device->BufferList.end(),
[](const BufferSubList &entry) noexcept -> bool
{ return entry.FreeMask != 0; });
auto lidx = static_cast<ALuint>(std::distance(device->BufferList.begin(), sublist));
auto slidx = static_cast<ALuint>(al::countr_zero(sublist->FreeMask));
ASSUME(slidx < 64);
ALbuffer *buffer{al::construct_at(al::to_address(sublist->Buffers->begin() + slidx))};
/* Add 1 to avoid buffer ID 0. */
buffer->id = ((lidx<<6) | slidx) + 1;
sublist->FreeMask &= ~(1_u64 << slidx);
return buffer;
}
void FreeBuffer(ALCdevice *device, ALbuffer *buffer)
{
#ifdef ALSOFT_EAX
eax_x_ram_clear(*device, *buffer);
#endif // ALSOFT_EAX
device->mBufferNames.erase(buffer->id);
const ALuint id{buffer->id - 1};
const size_t lidx{id >> 6};
const ALuint slidx{id & 0x3f};
std::destroy_at(buffer);
device->BufferList[lidx].FreeMask |= 1_u64 << slidx;
}
inline ALbuffer *LookupBuffer(ALCdevice *device, ALuint id)
{
const size_t lidx{(id-1) >> 6};
const ALuint slidx{(id-1) & 0x3f};
if(lidx >= device->BufferList.size()) UNLIKELY
return nullptr;
BufferSubList &sublist = device->BufferList[lidx];
if(sublist.FreeMask & (1_u64 << slidx)) UNLIKELY
return nullptr;
return al::to_address(sublist.Buffers->begin() + slidx);
}
ALuint SanitizeAlignment(FmtType type, ALuint align)
{
if(align == 0)
{
if(type == FmtIMA4)
{
/* Here is where things vary:
* nVidia and Apple use 64+1 sample frames per block -> block_size=36 bytes per channel
* Most PC sound software uses 2040+1 sample frames per block -> block_size=1024 bytes per channel
*/
return 65;
}
if(type == FmtMSADPCM)
return 64;
return 1;
}
if(type == FmtIMA4)
{
/* IMA4 block alignment must be a multiple of 8, plus 1. */
if((align&7) == 1) return static_cast<ALuint>(align);
return 0;
}
if(type == FmtMSADPCM)
{
/* MSADPCM block alignment must be a multiple of 2. */
if((align&1) == 0) return static_cast<ALuint>(align);
return 0;
}
return static_cast<ALuint>(align);
}
/** Loads the specified data into the buffer, using the specified format. */
void LoadData(ALCcontext *context, ALbuffer *ALBuf, ALsizei freq, ALuint size,
const FmtChannels DstChannels, const FmtType DstType, const std::byte *SrcData,
ALbitfieldSOFT access)
{
if(ALBuf->ref.load(std::memory_order_relaxed) != 0 || ALBuf->MappedAccess != 0) UNLIKELY
return context->setError(AL_INVALID_OPERATION, "Modifying storage for in-use buffer %u",
ALBuf->id);
const ALuint unpackalign{ALBuf->UnpackAlign};
const ALuint align{SanitizeAlignment(DstType, unpackalign)};
if(align < 1) UNLIKELY
return context->setError(AL_INVALID_VALUE, "Invalid unpack alignment %u for %s samples",
unpackalign, NameFromFormat(DstType));
const ALuint ambiorder{IsBFormat(DstChannels) ? ALBuf->UnpackAmbiOrder :
(IsUHJ(DstChannels) ? 1 : 0)};
if((access&AL_PRESERVE_DATA_BIT_SOFT))
{
/* Can only preserve data with the same format and alignment. */
if(ALBuf->mChannels != DstChannels || ALBuf->mType != DstType) UNLIKELY
return context->setError(AL_INVALID_VALUE, "Preserving data of mismatched format");
if(ALBuf->mBlockAlign != align) UNLIKELY
return context->setError(AL_INVALID_VALUE, "Preserving data of mismatched alignment");
if(ALBuf->mAmbiOrder != ambiorder) UNLIKELY
return context->setError(AL_INVALID_VALUE, "Preserving data of mismatched order");
}
/* Convert the size in bytes to blocks using the unpack block alignment. */
const ALuint NumChannels{ChannelsFromFmt(DstChannels, ambiorder)};
const ALuint BlockSize{NumChannels *
((DstType == FmtIMA4) ? (align-1)/2 + 4 :
(DstType == FmtMSADPCM) ? (align-2)/2 + 7 :
(align * BytesFromFmt(DstType)))};
if((size%BlockSize) != 0) UNLIKELY
return context->setError(AL_INVALID_VALUE,
"Data size %d is not a multiple of frame size %d (%d unpack alignment)",
size, BlockSize, align);
const ALuint blocks{size / BlockSize};
if(blocks > std::numeric_limits<ALsizei>::max()/align) UNLIKELY
return context->setError(AL_OUT_OF_MEMORY,
"Buffer size overflow, %d blocks x %d samples per block", blocks, align);
if(blocks > std::numeric_limits<size_t>::max()/BlockSize) UNLIKELY
return context->setError(AL_OUT_OF_MEMORY,
"Buffer size overflow, %d frames x %d bytes per frame", blocks, BlockSize);
const size_t newsize{static_cast<size_t>(blocks) * BlockSize};
#ifdef ALSOFT_EAX
if(ALBuf->eax_x_ram_mode == EaxStorage::Hardware)
{
ALCdevice &device = *context->mALDevice;
if(!eax_x_ram_check_availability(device, *ALBuf, size))
return context->setError(AL_OUT_OF_MEMORY,
"Out of X-RAM memory (avail: %u, needed: %u)", device.eax_x_ram_free_size, size);
}
#endif
/* This could reallocate only when increasing the size or the new size is
* less than half the current, but then the buffer's AL_SIZE would not be
* very reliable for accounting buffer memory usage, and reporting the real
* size could cause problems for apps that use AL_SIZE to try to get the
* buffer's play length.
*/
if(newsize != ALBuf->mDataStorage.size())
{
auto newdata = decltype(ALBuf->mDataStorage)(newsize, std::byte{});
if((access&AL_PRESERVE_DATA_BIT_SOFT))
{
const size_t tocopy{minz(newdata.size(), ALBuf->mDataStorage.size())};
std::copy_n(ALBuf->mDataStorage.begin(), tocopy, newdata.begin());
}
newdata.swap(ALBuf->mDataStorage);
}
ALBuf->mData = ALBuf->mDataStorage;
#ifdef ALSOFT_EAX
eax_x_ram_clear(*context->mALDevice, *ALBuf);
#endif
if(SrcData != nullptr && !ALBuf->mData.empty())
std::copy_n(SrcData, blocks*BlockSize, ALBuf->mData.begin());
ALBuf->mBlockAlign = (DstType == FmtIMA4 || DstType == FmtMSADPCM) ? align : 1;
ALBuf->OriginalSize = size;
ALBuf->Access = access;
ALBuf->mSampleRate = static_cast<ALuint>(freq);
ALBuf->mChannels = DstChannels;
ALBuf->mType = DstType;
ALBuf->mAmbiOrder = ambiorder;
ALBuf->mCallback = nullptr;
ALBuf->mUserData = nullptr;
ALBuf->mSampleLen = blocks * align;
ALBuf->mLoopStart = 0;
ALBuf->mLoopEnd = ALBuf->mSampleLen;
#ifdef ALSOFT_EAX
if(eax_g_is_enabled && ALBuf->eax_x_ram_mode == EaxStorage::Hardware)
eax_x_ram_apply(*context->mALDevice, *ALBuf);
#endif
}
/** Prepares the buffer to use the specified callback, using the specified format. */
void PrepareCallback(ALCcontext *context, ALbuffer *ALBuf, ALsizei freq,
const FmtChannels DstChannels, const FmtType DstType, ALBUFFERCALLBACKTYPESOFT callback,
void *userptr)
{
if(ALBuf->ref.load(std::memory_order_relaxed) != 0 || ALBuf->MappedAccess != 0) UNLIKELY
return context->setError(AL_INVALID_OPERATION, "Modifying callback for in-use buffer %u",
ALBuf->id);
const ALuint ambiorder{IsBFormat(DstChannels) ? ALBuf->UnpackAmbiOrder :
(IsUHJ(DstChannels) ? 1 : 0)};
const ALuint unpackalign{ALBuf->UnpackAlign};
const ALuint align{SanitizeAlignment(DstType, unpackalign)};
if(align < 1) UNLIKELY
return context->setError(AL_INVALID_VALUE, "Invalid unpack alignment %u for %s samples",
unpackalign, NameFromFormat(DstType));
const ALuint BlockSize{ChannelsFromFmt(DstChannels, ambiorder) *
((DstType == FmtIMA4) ? (align-1)/2 + 4 :
(DstType == FmtMSADPCM) ? (align-2)/2 + 7 :
(align * BytesFromFmt(DstType)))};
/* The maximum number of samples a callback buffer may need to store is a
* full mixing line * max pitch * channel count, since it may need to hold
* a full line's worth of sample frames before downsampling. An additional
* MaxResamplerEdge is needed for "future" samples during resampling (the
* voice will hold a history for the past samples).
*/
static constexpr size_t line_size{DeviceBase::MixerLineSize*MaxPitch + MaxResamplerEdge};
const size_t line_blocks{(line_size + align-1) / align};
using BufferVectorType = decltype(ALBuf->mDataStorage);
BufferVectorType(line_blocks*BlockSize).swap(ALBuf->mDataStorage);
ALBuf->mData = ALBuf->mDataStorage;
#ifdef ALSOFT_EAX
eax_x_ram_clear(*context->mALDevice, *ALBuf);
#endif
ALBuf->mCallback = callback;
ALBuf->mUserData = userptr;
ALBuf->OriginalSize = 0;
ALBuf->Access = 0;
ALBuf->mBlockAlign = (DstType == FmtIMA4 || DstType == FmtMSADPCM) ? align : 1;
ALBuf->mSampleRate = static_cast<ALuint>(freq);
ALBuf->mChannels = DstChannels;
ALBuf->mType = DstType;
ALBuf->mAmbiOrder = ambiorder;
ALBuf->mSampleLen = 0;
ALBuf->mLoopStart = 0;
ALBuf->mLoopEnd = ALBuf->mSampleLen;
}
/** Prepares the buffer to use caller-specified storage. */
void PrepareUserPtr(ALCcontext *context, ALbuffer *ALBuf, ALsizei freq,
const FmtChannels DstChannels, const FmtType DstType, std::byte *sdata, const ALuint sdatalen)
{
if(ALBuf->ref.load(std::memory_order_relaxed) != 0 || ALBuf->MappedAccess != 0) UNLIKELY
return context->setError(AL_INVALID_OPERATION, "Modifying storage for in-use buffer %u",
ALBuf->id);
const ALuint unpackalign{ALBuf->UnpackAlign};
const ALuint align{SanitizeAlignment(DstType, unpackalign)};
if(align < 1) UNLIKELY
return context->setError(AL_INVALID_VALUE, "Invalid unpack alignment %u for %s samples",
unpackalign, NameFromFormat(DstType));
auto get_type_alignment = [](const FmtType type) noexcept -> ALuint
{
/* NOTE: This only needs to be the required alignment for the CPU to
* read/write the given sample type in the mixer.
*/
switch(type)
{
case FmtUByte: return alignof(ALubyte);
case FmtShort: return alignof(ALshort);
case FmtInt: return alignof(ALint);
case FmtFloat: return alignof(ALfloat);
case FmtDouble: return alignof(ALdouble);
case FmtMulaw: return alignof(ALubyte);
case FmtAlaw: return alignof(ALubyte);
case FmtIMA4: break;
case FmtMSADPCM: break;
}
return 1;
};
const auto typealign = get_type_alignment(DstType);
if((reinterpret_cast<uintptr_t>(sdata) & (typealign-1)) != 0)
return context->setError(AL_INVALID_VALUE, "Pointer %p is misaligned for %s samples (%u)",
static_cast<void*>(sdata), NameFromFormat(DstType), typealign);
const ALuint ambiorder{IsBFormat(DstChannels) ? ALBuf->UnpackAmbiOrder :
(IsUHJ(DstChannels) ? 1 : 0)};
/* Convert the size in bytes to blocks using the unpack block alignment. */
const ALuint NumChannels{ChannelsFromFmt(DstChannels, ambiorder)};
const ALuint BlockSize{NumChannels *
((DstType == FmtIMA4) ? (align-1)/2 + 4 :
(DstType == FmtMSADPCM) ? (align-2)/2 + 7 :
(align * BytesFromFmt(DstType)))};
if((sdatalen%BlockSize) != 0) UNLIKELY
return context->setError(AL_INVALID_VALUE,
"Data size %u is not a multiple of frame size %u (%u unpack alignment)",
sdatalen, BlockSize, align);
const ALuint blocks{sdatalen / BlockSize};
if(blocks > std::numeric_limits<ALsizei>::max()/align) UNLIKELY
return context->setError(AL_OUT_OF_MEMORY,
"Buffer size overflow, %d blocks x %d samples per block", blocks, align);
if(blocks > std::numeric_limits<size_t>::max()/BlockSize) UNLIKELY
return context->setError(AL_OUT_OF_MEMORY,
"Buffer size overflow, %d frames x %d bytes per frame", blocks, BlockSize);
#ifdef ALSOFT_EAX
if(ALBuf->eax_x_ram_mode == EaxStorage::Hardware)
{
ALCdevice &device = *context->mALDevice;
if(!eax_x_ram_check_availability(device, *ALBuf, sdatalen))
return context->setError(AL_OUT_OF_MEMORY,
"Out of X-RAM memory (avail: %u, needed: %u)", device.eax_x_ram_free_size,
sdatalen);
}
#endif
decltype(ALBuf->mDataStorage){}.swap(ALBuf->mDataStorage);
ALBuf->mData = {static_cast<std::byte*>(sdata), sdatalen};
#ifdef ALSOFT_EAX
eax_x_ram_clear(*context->mALDevice, *ALBuf);
#endif
ALBuf->mCallback = nullptr;
ALBuf->mUserData = nullptr;
ALBuf->OriginalSize = sdatalen;
ALBuf->Access = 0;
ALBuf->mBlockAlign = (DstType == FmtIMA4 || DstType == FmtMSADPCM) ? align : 1;
ALBuf->mSampleRate = static_cast<ALuint>(freq);
ALBuf->mChannels = DstChannels;
ALBuf->mType = DstType;
ALBuf->mAmbiOrder = ambiorder;
ALBuf->mSampleLen = blocks * align;
ALBuf->mLoopStart = 0;
ALBuf->mLoopEnd = ALBuf->mSampleLen;
#ifdef ALSOFT_EAX
if(ALBuf->eax_x_ram_mode == EaxStorage::Hardware)
eax_x_ram_apply(*context->mALDevice, *ALBuf);
#endif
}
struct DecompResult { FmtChannels channels; FmtType type; };
std::optional<DecompResult> DecomposeUserFormat(ALenum format)
{
struct FormatMap {
ALenum format;
FmtChannels channels;
FmtType type;
};
static constexpr std::array UserFmtList{
FormatMap{AL_FORMAT_MONO8, FmtMono, FmtUByte },
FormatMap{AL_FORMAT_MONO16, FmtMono, FmtShort },
FormatMap{AL_FORMAT_MONO_I32, FmtMono, FmtInt },
FormatMap{AL_FORMAT_MONO_FLOAT32, FmtMono, FmtFloat },
FormatMap{AL_FORMAT_MONO_DOUBLE_EXT, FmtMono, FmtDouble },
FormatMap{AL_FORMAT_MONO_IMA4, FmtMono, FmtIMA4 },
FormatMap{AL_FORMAT_MONO_MSADPCM_SOFT, FmtMono, FmtMSADPCM},
FormatMap{AL_FORMAT_MONO_MULAW, FmtMono, FmtMulaw },
FormatMap{AL_FORMAT_MONO_ALAW_EXT, FmtMono, FmtAlaw },
FormatMap{AL_FORMAT_STEREO8, FmtStereo, FmtUByte },
FormatMap{AL_FORMAT_STEREO16, FmtStereo, FmtShort },
FormatMap{AL_FORMAT_STEREO_I32, FmtStereo, FmtInt },
FormatMap{AL_FORMAT_STEREO_FLOAT32, FmtStereo, FmtFloat },
FormatMap{AL_FORMAT_STEREO_DOUBLE_EXT, FmtStereo, FmtDouble },
FormatMap{AL_FORMAT_STEREO_IMA4, FmtStereo, FmtIMA4 },
FormatMap{AL_FORMAT_STEREO_MSADPCM_SOFT, FmtStereo, FmtMSADPCM},
FormatMap{AL_FORMAT_STEREO_MULAW, FmtStereo, FmtMulaw },
FormatMap{AL_FORMAT_STEREO_ALAW_EXT, FmtStereo, FmtAlaw },
FormatMap{AL_FORMAT_REAR8, FmtRear, FmtUByte},
FormatMap{AL_FORMAT_REAR16, FmtRear, FmtShort},
FormatMap{AL_FORMAT_REAR32, FmtRear, FmtFloat},
FormatMap{AL_FORMAT_REAR_I32, FmtRear, FmtInt },
FormatMap{AL_FORMAT_REAR_FLOAT32, FmtRear, FmtFloat},
FormatMap{AL_FORMAT_REAR_MULAW, FmtRear, FmtMulaw},
FormatMap{AL_FORMAT_QUAD8_LOKI, FmtQuad, FmtUByte},
FormatMap{AL_FORMAT_QUAD16_LOKI, FmtQuad, FmtShort},
FormatMap{AL_FORMAT_QUAD8, FmtQuad, FmtUByte},
FormatMap{AL_FORMAT_QUAD16, FmtQuad, FmtShort},
FormatMap{AL_FORMAT_QUAD32, FmtQuad, FmtFloat},
FormatMap{AL_FORMAT_QUAD_I32, FmtQuad, FmtInt },
FormatMap{AL_FORMAT_QUAD_FLOAT32, FmtQuad, FmtFloat},
FormatMap{AL_FORMAT_QUAD_MULAW, FmtQuad, FmtMulaw},
FormatMap{AL_FORMAT_51CHN8, FmtX51, FmtUByte},
FormatMap{AL_FORMAT_51CHN16, FmtX51, FmtShort},
FormatMap{AL_FORMAT_51CHN32, FmtX51, FmtFloat},
FormatMap{AL_FORMAT_51CHN_I32, FmtX51, FmtInt },
FormatMap{AL_FORMAT_51CHN_FLOAT32, FmtX51, FmtFloat},
FormatMap{AL_FORMAT_51CHN_MULAW, FmtX51, FmtMulaw},
FormatMap{AL_FORMAT_61CHN8, FmtX61, FmtUByte},
FormatMap{AL_FORMAT_61CHN16, FmtX61, FmtShort},
FormatMap{AL_FORMAT_61CHN32, FmtX61, FmtFloat},
FormatMap{AL_FORMAT_61CHN_I32, FmtX61, FmtInt },
FormatMap{AL_FORMAT_61CHN_FLOAT32, FmtX61, FmtFloat},
FormatMap{AL_FORMAT_61CHN_MULAW, FmtX61, FmtMulaw},
FormatMap{AL_FORMAT_71CHN8, FmtX71, FmtUByte},
FormatMap{AL_FORMAT_71CHN16, FmtX71, FmtShort},
FormatMap{AL_FORMAT_71CHN32, FmtX71, FmtFloat},
FormatMap{AL_FORMAT_71CHN_I32, FmtX71, FmtInt },
FormatMap{AL_FORMAT_71CHN_FLOAT32, FmtX71, FmtFloat},
FormatMap{AL_FORMAT_71CHN_MULAW, FmtX71, FmtMulaw},
FormatMap{AL_FORMAT_BFORMAT2D_8, FmtBFormat2D, FmtUByte},
FormatMap{AL_FORMAT_BFORMAT2D_16, FmtBFormat2D, FmtShort},
FormatMap{AL_FORMAT_BFORMAT2D_FLOAT32, FmtBFormat2D, FmtFloat},
FormatMap{AL_FORMAT_BFORMAT2D_MULAW, FmtBFormat2D, FmtMulaw},
FormatMap{AL_FORMAT_BFORMAT3D_8, FmtBFormat3D, FmtUByte},
FormatMap{AL_FORMAT_BFORMAT3D_16, FmtBFormat3D, FmtShort},
FormatMap{AL_FORMAT_BFORMAT3D_FLOAT32, FmtBFormat3D, FmtFloat},
FormatMap{AL_FORMAT_BFORMAT3D_MULAW, FmtBFormat3D, FmtMulaw},
FormatMap{AL_FORMAT_UHJ2CHN8_SOFT, FmtUHJ2, FmtUByte },
FormatMap{AL_FORMAT_UHJ2CHN16_SOFT, FmtUHJ2, FmtShort },
FormatMap{AL_FORMAT_UHJ2CHN_I32, FmtUHJ2, FmtInt },
FormatMap{AL_FORMAT_UHJ2CHN_FLOAT32_SOFT, FmtUHJ2, FmtFloat },
FormatMap{AL_FORMAT_UHJ2CHN_MULAW_SOFT, FmtUHJ2, FmtMulaw },
FormatMap{AL_FORMAT_UHJ2CHN_ALAW_SOFT, FmtUHJ2, FmtAlaw },
FormatMap{AL_FORMAT_UHJ2CHN_IMA4_SOFT, FmtUHJ2, FmtIMA4 },
FormatMap{AL_FORMAT_UHJ2CHN_MSADPCM_SOFT, FmtUHJ2, FmtMSADPCM},
FormatMap{AL_FORMAT_UHJ3CHN8_SOFT, FmtUHJ3, FmtUByte},
FormatMap{AL_FORMAT_UHJ3CHN16_SOFT, FmtUHJ3, FmtShort},
FormatMap{AL_FORMAT_UHJ3CHN_I32, FmtUHJ3, FmtInt },
FormatMap{AL_FORMAT_UHJ3CHN_FLOAT32_SOFT, FmtUHJ3, FmtFloat},
FormatMap{AL_FORMAT_UHJ3CHN_MULAW_SOFT, FmtUHJ3, FmtMulaw},
FormatMap{AL_FORMAT_UHJ3CHN_ALAW_SOFT, FmtUHJ3, FmtAlaw },
FormatMap{AL_FORMAT_UHJ4CHN8_SOFT, FmtUHJ4, FmtUByte},
FormatMap{AL_FORMAT_UHJ4CHN16_SOFT, FmtUHJ4, FmtShort},
FormatMap{AL_FORMAT_UHJ4CHN_I32, FmtUHJ4, FmtInt },
FormatMap{AL_FORMAT_UHJ4CHN_FLOAT32_SOFT, FmtUHJ4, FmtFloat},
FormatMap{AL_FORMAT_UHJ4CHN_MULAW_SOFT, FmtUHJ4, FmtMulaw},
FormatMap{AL_FORMAT_UHJ4CHN_ALAW_SOFT, FmtUHJ4, FmtAlaw },
};
for(const auto &fmt : UserFmtList)
{
if(fmt.format == format)
return DecompResult{fmt.channels, fmt.type};
}
return std::nullopt;
}
} // namespace
AL_API DECL_FUNC2(void, alGenBuffers, ALsizei, ALuint*)
FORCE_ALIGN void AL_APIENTRY alGenBuffersDirect(ALCcontext *context, ALsizei n, ALuint *buffers) noexcept
{
if(n < 0) UNLIKELY
context->setError(AL_INVALID_VALUE, "Generating %d buffers", n);
if(n <= 0) UNLIKELY return;
ALCdevice *device{context->mALDevice.get()};
std::lock_guard<std::mutex> _{device->BufferLock};
if(!EnsureBuffers(device, static_cast<ALuint>(n)))
{
context->setError(AL_OUT_OF_MEMORY, "Failed to allocate %d buffer%s", n, (n==1)?"":"s");
return;
}
if(n == 1) LIKELY
{
/* Special handling for the easy and normal case. */
ALbuffer *buffer{AllocBuffer(device)};
buffers[0] = buffer->id;
}
else
{
/* Store the allocated buffer IDs in a separate local list, to avoid
* modifying the user storage in case of failure.
*/
std::vector<ALuint> ids;
ids.reserve(static_cast<ALuint>(n));
do {
ALbuffer *buffer{AllocBuffer(device)};
ids.emplace_back(buffer->id);
} while(--n);
std::copy(ids.begin(), ids.end(), buffers);
}
}
AL_API DECL_FUNC2(void, alDeleteBuffers, ALsizei, const ALuint*)
FORCE_ALIGN void AL_APIENTRY alDeleteBuffersDirect(ALCcontext *context, ALsizei n,
const ALuint *buffers) noexcept
{
if(n < 0) UNLIKELY
context->setError(AL_INVALID_VALUE, "Deleting %d buffers", n);
if(n <= 0) UNLIKELY return;
ALCdevice *device{context->mALDevice.get()};
std::lock_guard<std::mutex> _{device->BufferLock};
/* First try to find any buffers that are invalid or in-use. */
auto validate_buffer = [device, &context](const ALuint bid) -> bool
{
if(!bid) return true;
ALbuffer *ALBuf{LookupBuffer(device, bid)};
if(!ALBuf) UNLIKELY
{
context->setError(AL_INVALID_NAME, "Invalid buffer ID %u", bid);
return false;
}
if(ALBuf->ref.load(std::memory_order_relaxed) != 0) UNLIKELY
{
context->setError(AL_INVALID_OPERATION, "Deleting in-use buffer %u", bid);
return false;
}
return true;
};
const ALuint *buffers_end = buffers + n;
auto invbuf = std::find_if_not(buffers, buffers_end, validate_buffer);
if(invbuf != buffers_end) UNLIKELY return;
/* All good. Delete non-0 buffer IDs. */
auto delete_buffer = [device](const ALuint bid) -> void
{
ALbuffer *buffer{bid ? LookupBuffer(device, bid) : nullptr};
if(buffer) FreeBuffer(device, buffer);
};
std::for_each(buffers, buffers_end, delete_buffer);
}
AL_API DECL_FUNC1(ALboolean, alIsBuffer, ALuint)
FORCE_ALIGN ALboolean AL_APIENTRY alIsBufferDirect(ALCcontext *context, ALuint buffer) noexcept
{
ALCdevice *device{context->mALDevice.get()};
std::lock_guard<std::mutex> _{device->BufferLock};
if(!buffer || LookupBuffer(device, buffer))
return AL_TRUE;
return AL_FALSE;
}
AL_API void AL_APIENTRY alBufferData(ALuint buffer, ALenum format, const ALvoid *data, ALsizei size, ALsizei freq) noexcept
{
auto context = GetContextRef();
if(!context) UNLIKELY return;
alBufferStorageDirectSOFT(context.get(), buffer, format, data, size, freq, 0);
}
FORCE_ALIGN void AL_APIENTRY alBufferDataDirect(ALCcontext *context, ALuint buffer, ALenum format, const ALvoid *data, ALsizei size, ALsizei freq) noexcept
{ alBufferStorageDirectSOFT(context, buffer, format, data, size, freq, 0); }
AL_API DECL_FUNCEXT6(void, alBufferStorage,SOFT, ALuint, ALenum, const ALvoid*, ALsizei, ALsizei, ALbitfieldSOFT)
FORCE_ALIGN void AL_APIENTRY alBufferStorageDirectSOFT(ALCcontext *context, ALuint buffer,
ALenum format, const ALvoid *data, ALsizei size, ALsizei freq, ALbitfieldSOFT flags) noexcept
{
ALCdevice *device{context->mALDevice.get()};
std::lock_guard<std::mutex> _{device->BufferLock};
ALbuffer *albuf = LookupBuffer(device, buffer);
if(!albuf) UNLIKELY
context->setError(AL_INVALID_NAME, "Invalid buffer ID %u", buffer);
else if(size < 0) UNLIKELY
context->setError(AL_INVALID_VALUE, "Negative storage size %d", size);
else if(freq < 1) UNLIKELY
context->setError(AL_INVALID_VALUE, "Invalid sample rate %d", freq);
else if((flags&INVALID_STORAGE_MASK) != 0) UNLIKELY
context->setError(AL_INVALID_VALUE, "Invalid storage flags 0x%x",
flags&INVALID_STORAGE_MASK);
else if((flags&AL_MAP_PERSISTENT_BIT_SOFT) && !(flags&MAP_READ_WRITE_FLAGS)) UNLIKELY
context->setError(AL_INVALID_VALUE,
"Declaring persistently mapped storage without read or write access");
else
{
auto usrfmt = DecomposeUserFormat(format);
if(!usrfmt) UNLIKELY
context->setError(AL_INVALID_ENUM, "Invalid format 0x%04x", format);
else
{
LoadData(context, albuf, freq, static_cast<ALuint>(size), usrfmt->channels,
usrfmt->type, static_cast<const std::byte*>(data), flags);
}
}
}
DECL_FUNC5(void, alBufferDataStatic, ALuint, ALenum, ALvoid*, ALsizei, ALsizei)
FORCE_ALIGN void AL_APIENTRY alBufferDataStaticDirect(ALCcontext *context, const ALuint buffer,
ALenum format, ALvoid *data, ALsizei size, ALsizei freq) noexcept
{
ALCdevice *device{context->mALDevice.get()};
std::lock_guard<std::mutex> _{device->BufferLock};
ALbuffer *albuf = LookupBuffer(device, buffer);
if(!albuf) UNLIKELY
return context->setError(AL_INVALID_NAME, "Invalid buffer ID %u", buffer);
if(size < 0) UNLIKELY
return context->setError(AL_INVALID_VALUE, "Negative storage size %d", size);
if(freq < 1) UNLIKELY
return context->setError(AL_INVALID_VALUE, "Invalid sample rate %d", freq);
auto usrfmt = DecomposeUserFormat(format);
if(!usrfmt) UNLIKELY
return context->setError(AL_INVALID_ENUM, "Invalid format 0x%04x", format);
PrepareUserPtr(context, albuf, freq, usrfmt->channels, usrfmt->type,
static_cast<std::byte*>(data), static_cast<ALuint>(size));
}
AL_API DECL_FUNCEXT4(void*, alMapBuffer,SOFT, ALuint, ALsizei, ALsizei, ALbitfieldSOFT)
FORCE_ALIGN void* AL_APIENTRY alMapBufferDirectSOFT(ALCcontext *context, ALuint buffer,
ALsizei offset, ALsizei length, ALbitfieldSOFT access) noexcept
{
ALCdevice *device{context->mALDevice.get()};
std::lock_guard<std::mutex> _{device->BufferLock};
ALbuffer *albuf = LookupBuffer(device, buffer);
if(!albuf) UNLIKELY
context->setError(AL_INVALID_NAME, "Invalid buffer ID %u", buffer);
else if((access&INVALID_MAP_FLAGS) != 0) UNLIKELY
context->setError(AL_INVALID_VALUE, "Invalid map flags 0x%x", access&INVALID_MAP_FLAGS);
else if(!(access&MAP_READ_WRITE_FLAGS)) UNLIKELY
context->setError(AL_INVALID_VALUE, "Mapping buffer %u without read or write access",
buffer);
else
{
ALbitfieldSOFT unavailable = (albuf->Access^access) & access;
if(albuf->ref.load(std::memory_order_relaxed) != 0
&& !(access&AL_MAP_PERSISTENT_BIT_SOFT)) UNLIKELY
context->setError(AL_INVALID_OPERATION,
"Mapping in-use buffer %u without persistent mapping", buffer);
else if(albuf->MappedAccess != 0) UNLIKELY
context->setError(AL_INVALID_OPERATION, "Mapping already-mapped buffer %u", buffer);
else if((unavailable&AL_MAP_READ_BIT_SOFT)) UNLIKELY
context->setError(AL_INVALID_VALUE,
"Mapping buffer %u for reading without read access", buffer);
else if((unavailable&AL_MAP_WRITE_BIT_SOFT)) UNLIKELY
context->setError(AL_INVALID_VALUE,
"Mapping buffer %u for writing without write access", buffer);
else if((unavailable&AL_MAP_PERSISTENT_BIT_SOFT)) UNLIKELY
context->setError(AL_INVALID_VALUE,
"Mapping buffer %u persistently without persistent access", buffer);
else if(offset < 0 || length <= 0
|| static_cast<ALuint>(offset) >= albuf->OriginalSize
|| static_cast<ALuint>(length) > albuf->OriginalSize - static_cast<ALuint>(offset))
UNLIKELY
context->setError(AL_INVALID_VALUE, "Mapping invalid range %d+%d for buffer %u",
offset, length, buffer);
else
{
void *retval{albuf->mData.data() + offset};
albuf->MappedAccess = access;
albuf->MappedOffset = offset;
albuf->MappedSize = length;
return retval;
}
}
return nullptr;
}
AL_API DECL_FUNCEXT1(void, alUnmapBuffer,SOFT, ALuint)
FORCE_ALIGN void AL_APIENTRY alUnmapBufferDirectSOFT(ALCcontext *context, ALuint buffer) noexcept
{
ALCdevice *device{context->mALDevice.get()};
std::lock_guard<std::mutex> _{device->BufferLock};
ALbuffer *albuf = LookupBuffer(device, buffer);
if(!albuf) UNLIKELY
context->setError(AL_INVALID_NAME, "Invalid buffer ID %u", buffer);
else if(albuf->MappedAccess == 0) UNLIKELY
context->setError(AL_INVALID_OPERATION, "Unmapping unmapped buffer %u", buffer);
else
{
albuf->MappedAccess = 0;
albuf->MappedOffset = 0;
albuf->MappedSize = 0;
}
}
AL_API DECL_FUNCEXT3(void, alFlushMappedBuffer,SOFT, ALuint, ALsizei, ALsizei)
FORCE_ALIGN void AL_APIENTRY alFlushMappedBufferDirectSOFT(ALCcontext *context, ALuint buffer,
ALsizei offset, ALsizei length) noexcept
{
ALCdevice *device{context->mALDevice.get()};
std::lock_guard<std::mutex> _{device->BufferLock};
ALbuffer *albuf = LookupBuffer(device, buffer);
if(!albuf) UNLIKELY
context->setError(AL_INVALID_NAME, "Invalid buffer ID %u", buffer);
else if(!(albuf->MappedAccess&AL_MAP_WRITE_BIT_SOFT)) UNLIKELY
context->setError(AL_INVALID_OPERATION, "Flushing buffer %u while not mapped for writing",
buffer);
else if(offset < albuf->MappedOffset || length <= 0
|| offset >= albuf->MappedOffset+albuf->MappedSize
|| length > albuf->MappedOffset+albuf->MappedSize-offset) UNLIKELY
context->setError(AL_INVALID_VALUE, "Flushing invalid range %d+%d on buffer %u", offset,
length, buffer);
else
{
/* FIXME: Need to use some method of double-buffering for the mixer and
* app to hold separate memory, which can be safely transferred
* asynchronously. Currently we just say the app shouldn't write where
* OpenAL's reading, and hope for the best...
*/
std::atomic_thread_fence(std::memory_order_seq_cst);
}
}
AL_API DECL_FUNCEXT5(void, alBufferSubData,SOFT, ALuint, ALenum, const ALvoid*, ALsizei, ALsizei)
FORCE_ALIGN void AL_APIENTRY alBufferSubDataDirectSOFT(ALCcontext *context, ALuint buffer,
ALenum format, const ALvoid *data, ALsizei offset, ALsizei length) noexcept
{
ALCdevice *device{context->mALDevice.get()};
std::lock_guard<std::mutex> _{device->BufferLock};
ALbuffer *albuf = LookupBuffer(device, buffer);
if(!albuf) UNLIKELY
return context->setError(AL_INVALID_NAME, "Invalid buffer ID %u", buffer);
auto usrfmt = DecomposeUserFormat(format);
if(!usrfmt) UNLIKELY
return context->setError(AL_INVALID_ENUM, "Invalid format 0x%04x", format);
const ALuint unpack_align{albuf->UnpackAlign};
const ALuint align{SanitizeAlignment(usrfmt->type, unpack_align)};
if(align < 1) UNLIKELY
return context->setError(AL_INVALID_VALUE, "Invalid unpack alignment %u", unpack_align);
if(usrfmt->channels != albuf->mChannels || usrfmt->type != albuf->mType) UNLIKELY
return context->setError(AL_INVALID_ENUM, "Unpacking data with mismatched format");
if(align != albuf->mBlockAlign) UNLIKELY
return context->setError(AL_INVALID_VALUE,
"Unpacking data with alignment %u does not match original alignment %u", align,
albuf->mBlockAlign);
if(albuf->isBFormat() && albuf->UnpackAmbiOrder != albuf->mAmbiOrder) UNLIKELY
return context->setError(AL_INVALID_VALUE,
"Unpacking data with mismatched ambisonic order");
if(albuf->MappedAccess != 0) UNLIKELY
return context->setError(AL_INVALID_OPERATION, "Unpacking data into mapped buffer %u",
buffer);
const ALuint num_chans{albuf->channelsFromFmt()};
const ALuint byte_align{
(albuf->mType == FmtIMA4) ? ((align-1)/2 + 4) * num_chans :
(albuf->mType == FmtMSADPCM) ? ((align-2)/2 + 7) * num_chans :
(align * albuf->bytesFromFmt() * num_chans)};
if(offset < 0 || length < 0 || static_cast<ALuint>(offset) > albuf->OriginalSize
|| static_cast<ALuint>(length) > albuf->OriginalSize-static_cast<ALuint>(offset))
UNLIKELY
return context->setError(AL_INVALID_VALUE, "Invalid data sub-range %d+%d on buffer %u",
offset, length, buffer);
if((static_cast<ALuint>(offset)%byte_align) != 0) UNLIKELY
return context->setError(AL_INVALID_VALUE,
"Sub-range offset %d is not a multiple of frame size %d (%d unpack alignment)",
offset, byte_align, align);
if((static_cast<ALuint>(length)%byte_align) != 0) UNLIKELY
return context->setError(AL_INVALID_VALUE,
"Sub-range length %d is not a multiple of frame size %d (%d unpack alignment)",
length, byte_align, align);
assert(al::to_underlying(usrfmt->type) == al::to_underlying(albuf->mType));
memcpy(albuf->mData.data()+offset, data, static_cast<ALuint>(length));
}
AL_API DECL_FUNC3(void, alBufferf, ALuint, ALenum, ALfloat)
FORCE_ALIGN void AL_APIENTRY alBufferfDirect(ALCcontext *context, ALuint buffer, ALenum param,
ALfloat /*value*/) noexcept
{
ALCdevice *device{context->mALDevice.get()};
std::lock_guard<std::mutex> _{device->BufferLock};
if(LookupBuffer(device, buffer) == nullptr) UNLIKELY
context->setError(AL_INVALID_NAME, "Invalid buffer ID %u", buffer);
else switch(param)
{
default:
context->setError(AL_INVALID_ENUM, "Invalid buffer float property 0x%04x", param);
}
}
AL_API DECL_FUNC5(void, alBuffer3f, ALuint, ALenum, ALfloat, ALfloat, ALfloat)
FORCE_ALIGN void AL_APIENTRY alBuffer3fDirect(ALCcontext *context, ALuint buffer, ALenum param,
ALfloat /*value1*/, ALfloat /*value2*/, ALfloat /*value3*/) noexcept
{
ALCdevice *device{context->mALDevice.get()};
std::lock_guard<std::mutex> _{device->BufferLock};
if(LookupBuffer(device, buffer) == nullptr) UNLIKELY
context->setError(AL_INVALID_NAME, "Invalid buffer ID %u", buffer);
else switch(param)
{
default:
context->setError(AL_INVALID_ENUM, "Invalid buffer 3-float property 0x%04x", param);
}
}
AL_API DECL_FUNC3(void, alBufferfv, ALuint, ALenum, const ALfloat*)
FORCE_ALIGN void AL_APIENTRY alBufferfvDirect(ALCcontext *context, ALuint buffer, ALenum param,
const ALfloat *values) noexcept
{
ALCdevice *device{context->mALDevice.get()};
std::lock_guard<std::mutex> _{device->BufferLock};
if(LookupBuffer(device, buffer) == nullptr) UNLIKELY
context->setError(AL_INVALID_NAME, "Invalid buffer ID %u", buffer);
else if(!values) UNLIKELY
context->setError(AL_INVALID_VALUE, "NULL pointer");
else switch(param)
{
default:
context->setError(AL_INVALID_ENUM, "Invalid buffer float-vector property 0x%04x", param);
}
}
AL_API DECL_FUNC3(void, alBufferi, ALuint, ALenum, ALint)
FORCE_ALIGN void AL_APIENTRY alBufferiDirect(ALCcontext *context, ALuint buffer, ALenum param,
ALint value) noexcept
{
ALCdevice *device{context->mALDevice.get()};
std::lock_guard<std::mutex> _{device->BufferLock};
ALbuffer *albuf = LookupBuffer(device, buffer);
if(!albuf) UNLIKELY
context->setError(AL_INVALID_NAME, "Invalid buffer ID %u", buffer);
else switch(param)
{
case AL_UNPACK_BLOCK_ALIGNMENT_SOFT:
if(value < 0) UNLIKELY
context->setError(AL_INVALID_VALUE, "Invalid unpack block alignment %d", value);
else
albuf->UnpackAlign = static_cast<ALuint>(value);
break;
case AL_PACK_BLOCK_ALIGNMENT_SOFT:
if(value < 0) UNLIKELY
context->setError(AL_INVALID_VALUE, "Invalid pack block alignment %d", value);
else
albuf->PackAlign = static_cast<ALuint>(value);
break;
case AL_AMBISONIC_LAYOUT_SOFT:
if(albuf->ref.load(std::memory_order_relaxed) != 0) UNLIKELY
context->setError(AL_INVALID_OPERATION, "Modifying in-use buffer %u's ambisonic layout",
buffer);
else if(const auto layout = AmbiLayoutFromEnum(value))
albuf->mAmbiLayout = layout.value();
else UNLIKELY
context->setError(AL_INVALID_VALUE, "Invalid unpack ambisonic layout %04x", value);
break;
case AL_AMBISONIC_SCALING_SOFT:
if(albuf->ref.load(std::memory_order_relaxed) != 0) UNLIKELY
context->setError(AL_INVALID_OPERATION, "Modifying in-use buffer %u's ambisonic scaling",
buffer);
else if(const auto scaling = AmbiScalingFromEnum(value))
albuf->mAmbiScaling = scaling.value();
else UNLIKELY
context->setError(AL_INVALID_VALUE, "Invalid unpack ambisonic scaling %04x", value);
break;
case AL_UNPACK_AMBISONIC_ORDER_SOFT:
if(value < 1 || value > 14) UNLIKELY
context->setError(AL_INVALID_VALUE, "Invalid unpack ambisonic order %d", value);
else
albuf->UnpackAmbiOrder = static_cast<ALuint>(value);
break;
default:
context->setError(AL_INVALID_ENUM, "Invalid buffer integer property 0x%04x", param);
}
}
AL_API DECL_FUNC5(void, alBuffer3i, ALuint, ALenum, ALint, ALint, ALint)
FORCE_ALIGN void AL_APIENTRY alBuffer3iDirect(ALCcontext *context, ALuint buffer, ALenum param,
ALint /*value1*/, ALint /*value2*/, ALint /*value3*/) noexcept
{
ALCdevice *device{context->mALDevice.get()};
std::lock_guard<std::mutex> _{device->BufferLock};
if(LookupBuffer(device, buffer) == nullptr) UNLIKELY
context->setError(AL_INVALID_NAME, "Invalid buffer ID %u", buffer);
else switch(param)
{
default:
context->setError(AL_INVALID_ENUM, "Invalid buffer 3-integer property 0x%04x", param);
}
}
AL_API DECL_FUNC3(void, alBufferiv, ALuint, ALenum, const ALint*)
FORCE_ALIGN void AL_APIENTRY alBufferivDirect(ALCcontext *context, ALuint buffer, ALenum param,
const ALint *values) noexcept
{
if(!values) UNLIKELY
return context->setError(AL_INVALID_VALUE, "NULL pointer");
switch(param)
{
case AL_UNPACK_BLOCK_ALIGNMENT_SOFT:
case AL_PACK_BLOCK_ALIGNMENT_SOFT:
case AL_AMBISONIC_LAYOUT_SOFT:
case AL_AMBISONIC_SCALING_SOFT:
case AL_UNPACK_AMBISONIC_ORDER_SOFT:
alBufferiDirect(context, buffer, param, values[0]);
return;
}
ALCdevice *device{context->mALDevice.get()};
std::lock_guard<std::mutex> _{device->BufferLock};
ALbuffer *albuf = LookupBuffer(device, buffer);
if(!albuf) UNLIKELY
context->setError(AL_INVALID_NAME, "Invalid buffer ID %u", buffer);
else switch(param)
{
case AL_LOOP_POINTS_SOFT:
if(albuf->ref.load(std::memory_order_relaxed) != 0) UNLIKELY
context->setError(AL_INVALID_OPERATION, "Modifying in-use buffer %u's loop points",
buffer);
else if(values[0] < 0 || values[0] >= values[1]
|| static_cast<ALuint>(values[1]) > albuf->mSampleLen) UNLIKELY
context->setError(AL_INVALID_VALUE, "Invalid loop point range %d -> %d on buffer %u",
values[0], values[1], buffer);
else
{
albuf->mLoopStart = static_cast<ALuint>(values[0]);
albuf->mLoopEnd = static_cast<ALuint>(values[1]);
}
break;
default:
context->setError(AL_INVALID_ENUM, "Invalid buffer integer-vector property 0x%04x", param);
}
}
AL_API DECL_FUNC3(void, alGetBufferf, ALuint, ALenum, ALfloat*)
FORCE_ALIGN void AL_APIENTRY alGetBufferfDirect(ALCcontext *context, ALuint buffer, ALenum param,
ALfloat *value) noexcept
{
ALCdevice *device{context->mALDevice.get()};
std::lock_guard<std::mutex> _{device->BufferLock};
ALbuffer *albuf = LookupBuffer(device, buffer);
if(!albuf) UNLIKELY
context->setError(AL_INVALID_NAME, "Invalid buffer ID %u", buffer);
else if(!value) UNLIKELY
context->setError(AL_INVALID_VALUE, "NULL pointer");
else switch(param)
{
case AL_SEC_LENGTH_SOFT:
*value = (albuf->mSampleRate < 1) ? 0.0f :
(static_cast<float>(albuf->mSampleLen) / static_cast<float>(albuf->mSampleRate));
break;
default:
context->setError(AL_INVALID_ENUM, "Invalid buffer float property 0x%04x", param);
}
}
AL_API DECL_FUNC5(void, alGetBuffer3f, ALuint, ALenum, ALfloat*, ALfloat*, ALfloat*)
FORCE_ALIGN void AL_APIENTRY alGetBuffer3fDirect(ALCcontext *context, ALuint buffer, ALenum param,
ALfloat *value1, ALfloat *value2, ALfloat *value3) noexcept
{
ALCdevice *device{context->mALDevice.get()};
std::lock_guard<std::mutex> _{device->BufferLock};
if(LookupBuffer(device, buffer) == nullptr) UNLIKELY
context->setError(AL_INVALID_NAME, "Invalid buffer ID %u", buffer);
else if(!value1 || !value2 || !value3) UNLIKELY
context->setError(AL_INVALID_VALUE, "NULL pointer");
else switch(param)
{
default:
context->setError(AL_INVALID_ENUM, "Invalid buffer 3-float property 0x%04x", param);
}
}
AL_API DECL_FUNC3(void, alGetBufferfv, ALuint, ALenum, ALfloat*)
FORCE_ALIGN void AL_APIENTRY alGetBufferfvDirect(ALCcontext *context, ALuint buffer, ALenum param,
ALfloat *values) noexcept
{
switch(param)
{
case AL_SEC_LENGTH_SOFT:
alGetBufferfDirect(context, buffer, param, values);
return;
}
ALCdevice *device{context->mALDevice.get()};
std::lock_guard<std::mutex> _{device->BufferLock};
if(LookupBuffer(device, buffer) == nullptr) UNLIKELY
context->setError(AL_INVALID_NAME, "Invalid buffer ID %u", buffer);
else if(!values) UNLIKELY
context->setError(AL_INVALID_VALUE, "NULL pointer");
else switch(param)
{
default:
context->setError(AL_INVALID_ENUM, "Invalid buffer float-vector property 0x%04x", param);
}
}
AL_API DECL_FUNC3(void, alGetBufferi, ALuint, ALenum, ALint*)
FORCE_ALIGN void AL_APIENTRY alGetBufferiDirect(ALCcontext *context, ALuint buffer, ALenum param,
ALint *value) noexcept
{
ALCdevice *device{context->mALDevice.get()};
std::lock_guard<std::mutex> _{device->BufferLock};
ALbuffer *albuf = LookupBuffer(device, buffer);
if(!albuf) UNLIKELY
context->setError(AL_INVALID_NAME, "Invalid buffer ID %u", buffer);
else if(!value) UNLIKELY
context->setError(AL_INVALID_VALUE, "NULL pointer");
else switch(param)
{
case AL_FREQUENCY:
*value = static_cast<ALint>(albuf->mSampleRate);
break;
case AL_BITS:
*value = (albuf->mType == FmtIMA4 || albuf->mType == FmtMSADPCM) ? 4
: static_cast<ALint>(albuf->bytesFromFmt() * 8);
break;
case AL_CHANNELS:
*value = static_cast<ALint>(albuf->channelsFromFmt());
break;
case AL_SIZE:
*value = albuf->mCallback ? 0 : static_cast<ALint>(albuf->mData.size());
break;
case AL_BYTE_LENGTH_SOFT:
*value = static_cast<ALint>(albuf->mSampleLen / albuf->mBlockAlign
* albuf->blockSizeFromFmt());
break;
case AL_SAMPLE_LENGTH_SOFT:
*value = static_cast<ALint>(albuf->mSampleLen);
break;
case AL_UNPACK_BLOCK_ALIGNMENT_SOFT:
*value = static_cast<ALint>(albuf->UnpackAlign);
break;
case AL_PACK_BLOCK_ALIGNMENT_SOFT:
*value = static_cast<ALint>(albuf->PackAlign);
break;
case AL_AMBISONIC_LAYOUT_SOFT:
*value = EnumFromAmbiLayout(albuf->mAmbiLayout);
break;
case AL_AMBISONIC_SCALING_SOFT:
*value = EnumFromAmbiScaling(albuf->mAmbiScaling);
break;
case AL_UNPACK_AMBISONIC_ORDER_SOFT:
*value = static_cast<int>(albuf->UnpackAmbiOrder);
break;
default:
context->setError(AL_INVALID_ENUM, "Invalid buffer integer property 0x%04x", param);
}
}
AL_API DECL_FUNC5(void, alGetBuffer3i, ALuint, ALenum, ALint*, ALint*, ALint*)
FORCE_ALIGN void AL_APIENTRY alGetBuffer3iDirect(ALCcontext *context, ALuint buffer, ALenum param,
ALint *value1, ALint *value2, ALint *value3) noexcept
{
ALCdevice *device{context->mALDevice.get()};
std::lock_guard<std::mutex> _{device->BufferLock};
if(LookupBuffer(device, buffer) == nullptr) UNLIKELY
context->setError(AL_INVALID_NAME, "Invalid buffer ID %u", buffer);
else if(!value1 || !value2 || !value3) UNLIKELY
context->setError(AL_INVALID_VALUE, "NULL pointer");
else switch(param)
{
default:
context->setError(AL_INVALID_ENUM, "Invalid buffer 3-integer property 0x%04x", param);
}
}
AL_API DECL_FUNC3(void, alGetBufferiv, ALuint, ALenum, ALint*)
FORCE_ALIGN void AL_APIENTRY alGetBufferivDirect(ALCcontext *context, ALuint buffer, ALenum param,
ALint *values) noexcept
{
switch(param)
{
case AL_FREQUENCY:
case AL_BITS:
case AL_CHANNELS:
case AL_SIZE:
case AL_INTERNAL_FORMAT_SOFT:
case AL_BYTE_LENGTH_SOFT:
case AL_SAMPLE_LENGTH_SOFT:
case AL_UNPACK_BLOCK_ALIGNMENT_SOFT:
case AL_PACK_BLOCK_ALIGNMENT_SOFT:
case AL_AMBISONIC_LAYOUT_SOFT:
case AL_AMBISONIC_SCALING_SOFT:
case AL_UNPACK_AMBISONIC_ORDER_SOFT:
alGetBufferiDirect(context, buffer, param, values);
return;
}
ALCdevice *device{context->mALDevice.get()};
std::lock_guard<std::mutex> _{device->BufferLock};
ALbuffer *albuf = LookupBuffer(device, buffer);
if(!albuf) UNLIKELY
context->setError(AL_INVALID_NAME, "Invalid buffer ID %u", buffer);
else if(!values) UNLIKELY
context->setError(AL_INVALID_VALUE, "NULL pointer");
else switch(param)
{
case AL_LOOP_POINTS_SOFT:
values[0] = static_cast<ALint>(albuf->mLoopStart);
values[1] = static_cast<ALint>(albuf->mLoopEnd);
break;
default:
context->setError(AL_INVALID_ENUM, "Invalid buffer integer-vector property 0x%04x", param);
}
}
AL_API DECL_FUNCEXT5(void, alBufferCallback,SOFT, ALuint, ALenum, ALsizei, ALBUFFERCALLBACKTYPESOFT, ALvoid*)
FORCE_ALIGN void AL_APIENTRY alBufferCallbackDirectSOFT(ALCcontext *context, ALuint buffer,
ALenum format, ALsizei freq, ALBUFFERCALLBACKTYPESOFT callback, ALvoid *userptr) noexcept
{
ALCdevice *device{context->mALDevice.get()};
std::lock_guard<std::mutex> _{device->BufferLock};
ALbuffer *albuf = LookupBuffer(device, buffer);
if(!albuf) UNLIKELY
context->setError(AL_INVALID_NAME, "Invalid buffer ID %u", buffer);
else if(freq < 1) UNLIKELY
context->setError(AL_INVALID_VALUE, "Invalid sample rate %d", freq);
else if(callback == nullptr) UNLIKELY
context->setError(AL_INVALID_VALUE, "NULL callback");
else
{
auto usrfmt = DecomposeUserFormat(format);
if(!usrfmt) UNLIKELY
context->setError(AL_INVALID_ENUM, "Invalid format 0x%04x", format);
else
PrepareCallback(context, albuf, freq, usrfmt->channels, usrfmt->type, callback,
userptr);
}
}
AL_API DECL_FUNCEXT3(void, alGetBufferPtr,SOFT, ALuint, ALenum, ALvoid**)
FORCE_ALIGN void AL_APIENTRY alGetBufferPtrDirectSOFT(ALCcontext *context, ALuint buffer,
ALenum param, ALvoid **value) noexcept
{
ALCdevice *device{context->mALDevice.get()};
std::lock_guard<std::mutex> _{device->BufferLock};
ALbuffer *albuf = LookupBuffer(device, buffer);
if(!albuf) UNLIKELY
context->setError(AL_INVALID_NAME, "Invalid buffer ID %u", buffer);
else if(!value) UNLIKELY
context->setError(AL_INVALID_VALUE, "NULL pointer");
else switch(param)
{
case AL_BUFFER_CALLBACK_FUNCTION_SOFT:
*value = reinterpret_cast<void*>(albuf->mCallback);
break;
case AL_BUFFER_CALLBACK_USER_PARAM_SOFT:
*value = albuf->mUserData;
break;
default:
context->setError(AL_INVALID_ENUM, "Invalid buffer pointer property 0x%04x", param);
}
}
AL_API DECL_FUNCEXT5(void, alGetBuffer3Ptr,SOFT, ALuint, ALenum, ALvoid**, ALvoid**, ALvoid**)
FORCE_ALIGN void AL_APIENTRY alGetBuffer3PtrDirectSOFT(ALCcontext *context, ALuint buffer,
ALenum param, ALvoid **value1, ALvoid **value2, ALvoid **value3) noexcept
{
ALCdevice *device{context->mALDevice.get()};
std::lock_guard<std::mutex> _{device->BufferLock};
if(LookupBuffer(device, buffer) == nullptr) UNLIKELY
context->setError(AL_INVALID_NAME, "Invalid buffer ID %u", buffer);
else if(!value1 || !value2 || !value3) UNLIKELY
context->setError(AL_INVALID_VALUE, "NULL pointer");
else switch(param)
{
default:
context->setError(AL_INVALID_ENUM, "Invalid buffer 3-pointer property 0x%04x", param);
}
}
AL_API DECL_FUNCEXT3(void, alGetBufferPtrv,SOFT, ALuint, ALenum, ALvoid**)
FORCE_ALIGN void AL_APIENTRY alGetBufferPtrvDirectSOFT(ALCcontext *context, ALuint buffer,
ALenum param, ALvoid **values) noexcept
{
switch(param)
{
case AL_BUFFER_CALLBACK_FUNCTION_SOFT:
case AL_BUFFER_CALLBACK_USER_PARAM_SOFT:
alGetBufferPtrDirectSOFT(context, buffer, param, values);
return;
}
ALCdevice *device{context->mALDevice.get()};
std::lock_guard<std::mutex> _{device->BufferLock};
if(LookupBuffer(device, buffer) == nullptr) UNLIKELY
context->setError(AL_INVALID_NAME, "Invalid buffer ID %u", buffer);
else if(!values) UNLIKELY
context->setError(AL_INVALID_VALUE, "NULL pointer");
else switch(param)
{
default:
context->setError(AL_INVALID_ENUM, "Invalid buffer pointer-vector property 0x%04x", param);
}
}
AL_API void AL_APIENTRY alBufferSamplesSOFT(ALuint /*buffer*/, ALuint /*samplerate*/,
ALenum /*internalformat*/, ALsizei /*samples*/, ALenum /*channels*/, ALenum /*type*/,
const ALvoid* /*data*/) noexcept
{
ContextRef context{GetContextRef()};
if(!context) UNLIKELY return;
context->setError(AL_INVALID_OPERATION, "alBufferSamplesSOFT not supported");
}
AL_API void AL_APIENTRY alBufferSubSamplesSOFT(ALuint /*buffer*/, ALsizei /*offset*/,
ALsizei /*samples*/, ALenum /*channels*/, ALenum /*type*/, const ALvoid* /*data*/) noexcept
{
ContextRef context{GetContextRef()};
if(!context) UNLIKELY return;
context->setError(AL_INVALID_OPERATION, "alBufferSubSamplesSOFT not supported");
}
AL_API void AL_APIENTRY alGetBufferSamplesSOFT(ALuint /*buffer*/, ALsizei /*offset*/,
ALsizei /*samples*/, ALenum /*channels*/, ALenum /*type*/, ALvoid* /*data*/) noexcept
{
ContextRef context{GetContextRef()};
if(!context) UNLIKELY return;
context->setError(AL_INVALID_OPERATION, "alGetBufferSamplesSOFT not supported");
}
AL_API ALboolean AL_APIENTRY alIsBufferFormatSupportedSOFT(ALenum /*format*/) noexcept
{
ContextRef context{GetContextRef()};
if(!context) UNLIKELY return AL_FALSE;
context->setError(AL_INVALID_OPERATION, "alIsBufferFormatSupportedSOFT not supported");
return AL_FALSE;
}
void ALbuffer::SetName(ALCcontext *context, ALuint id, std::string_view name)
{
ALCdevice *device{context->mALDevice.get()};
std::lock_guard<std::mutex> _{device->BufferLock};
auto buffer = LookupBuffer(device, id);
if(!buffer) UNLIKELY
return context->setError(AL_INVALID_NAME, "Invalid buffer ID %u", id);
device->mBufferNames.insert_or_assign(id, name);
}
BufferSubList::~BufferSubList()
{
if(!Buffers)
return;
uint64_t usemask{~FreeMask};
while(usemask)
{
const int idx{al::countr_zero(usemask)};
std::destroy_at(al::to_address(Buffers->begin() + idx));
usemask &= ~(1_u64 << idx);
}
FreeMask = ~usemask;
SubListAllocator{}.deallocate(Buffers, 1);
Buffers = nullptr;
}
#ifdef ALSOFT_EAX
FORCE_ALIGN DECL_FUNC3(ALboolean, EAXSetBufferMode, ALsizei, const ALuint*, ALint)
FORCE_ALIGN ALboolean AL_APIENTRY EAXSetBufferModeDirect(ALCcontext *context, ALsizei n,
const ALuint *buffers, ALint value) noexcept
{
#define EAX_PREFIX "[EAXSetBufferMode] "
if(!eax_g_is_enabled)
{
context->setError(AL_INVALID_OPERATION, EAX_PREFIX "%s", "EAX not enabled.");
return AL_FALSE;
}
const auto storage = EaxStorageFromEnum(value);
if(!storage)
{
context->setError(AL_INVALID_ENUM, EAX_PREFIX "Unsupported X-RAM mode 0x%x", value);
return AL_FALSE;
}
if(n == 0)
return AL_TRUE;
if(n < 0)
{
context->setError(AL_INVALID_VALUE, EAX_PREFIX "Buffer count %d out of range", n);
return AL_FALSE;
}
if(!buffers)
{
context->setError(AL_INVALID_VALUE, EAX_PREFIX "%s", "Null AL buffers");
return AL_FALSE;
}
auto device = context->mALDevice.get();
std::lock_guard<std::mutex> device_lock{device->BufferLock};
/* Special-case setting a single buffer, to avoid extraneous allocations. */
if(n == 1)
{
const auto bufid = buffers[0];
if(bufid == AL_NONE)
return AL_TRUE;
const auto buffer = LookupBuffer(device, bufid);
if(!buffer) UNLIKELY
{
ERR(EAX_PREFIX "Invalid buffer ID %u.\n", bufid);
return AL_FALSE;
}
/* TODO: Is the store location allowed to change for in-use buffers, or
* only when not set/queued on a source?
*/
if(*storage == EaxStorage::Hardware)
{
if(!buffer->eax_x_ram_is_hardware
&& buffer->OriginalSize > device->eax_x_ram_free_size) UNLIKELY
{
context->setError(AL_OUT_OF_MEMORY,
EAX_PREFIX "Out of X-RAM memory (need: %u, avail: %u)", buffer->OriginalSize,
device->eax_x_ram_free_size);
return AL_FALSE;
}
eax_x_ram_apply(*device, *buffer);
}
else
eax_x_ram_clear(*device, *buffer);
buffer->eax_x_ram_mode = *storage;
return AL_TRUE;
}
/* Validate the buffers. */
std::unordered_set<ALbuffer*> buflist;
for(auto i = 0;i < n;++i)
{
const auto bufid = buffers[i];
if(bufid == AL_NONE)
continue;
const auto buffer = LookupBuffer(device, bufid);
if(!buffer) UNLIKELY
{
ERR(EAX_PREFIX "Invalid buffer ID %u.\n", bufid);
return AL_FALSE;
}
/* TODO: Is the store location allowed to change for in-use buffers, or
* only when not set/queued on a source?
*/
buflist.emplace(buffer);
}
if(*storage == EaxStorage::Hardware)
{
size_t total_needed{0};
for(ALbuffer *buffer : buflist)
{
if(!buffer->eax_x_ram_is_hardware)
{
if(std::numeric_limits<size_t>::max()-buffer->OriginalSize < total_needed) UNLIKELY
{
context->setError(AL_OUT_OF_MEMORY, EAX_PREFIX "Size overflow (%u + %zu)\n",
buffer->OriginalSize, total_needed);
return AL_FALSE;
}
total_needed += buffer->OriginalSize;
}
}
if(total_needed > device->eax_x_ram_free_size)
{
context->setError(AL_OUT_OF_MEMORY,
EAX_PREFIX "Out of X-RAM memory (need: %zu, avail: %u)", total_needed,
device->eax_x_ram_free_size);
return AL_FALSE;
}
}
/* Update the mode. */
for(ALbuffer *buffer : buflist)
{
if(*storage == EaxStorage::Hardware)
eax_x_ram_apply(*device, *buffer);
else
eax_x_ram_clear(*device, *buffer);
buffer->eax_x_ram_mode = *storage;
}
return AL_TRUE;
#undef EAX_PREFIX
}
FORCE_ALIGN DECL_FUNC2(ALenum, EAXGetBufferMode, ALuint, ALint*)
FORCE_ALIGN ALenum AL_APIENTRY EAXGetBufferModeDirect(ALCcontext *context, ALuint buffer,
ALint *pReserved) noexcept
{
#define EAX_PREFIX "[EAXGetBufferMode] "
if(!eax_g_is_enabled)
{
context->setError(AL_INVALID_OPERATION, EAX_PREFIX "%s", "EAX not enabled.");
return AL_NONE;
}
if(pReserved)
{
context->setError(AL_INVALID_VALUE, EAX_PREFIX "%s", "Non-null reserved parameter");
return AL_NONE;
}
auto device = context->mALDevice.get();
std::lock_guard<std::mutex> device_lock{device->BufferLock};
const auto al_buffer = LookupBuffer(device, buffer);
if(!al_buffer)
{
context->setError(AL_INVALID_NAME, EAX_PREFIX "Invalid buffer ID %u", buffer);
return AL_NONE;
}
return EnumFromEaxStorage(al_buffer->eax_x_ram_mode);
#undef EAX_PREFIX
}
#endif // ALSOFT_EAX
|