Skip to content

Commit b81b743

Browse files
committed
prototype host-buffers for openal
#61
1 parent e25fc67 commit b81b743

File tree

9 files changed

+589
-137
lines changed

9 files changed

+589
-137
lines changed

Engine/sound/sound.cpp

+10-3
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ const int32_t Sound::indexTable[] = {
5959
};
6060

6161
Sound::Data::~Data() {
62-
#if 0
62+
#if 1
6363
alDeleteBuffersHost(1, &buffer);
6464
#else
6565
auto ctx = reinterpret_cast<ALCcontext*>(SoundDevice::bufferContextSt());
@@ -68,13 +68,20 @@ Sound::Data::~Data() {
6868
}
6969

7070
uint64_t Sound::Data::timeLength() const {
71-
int size=0, fr=0, bits=0, channels=0;
71+
ALint size=0, fr=0, bits=0, channels=0;
7272

73+
#if 1
74+
alGetBufferiHost(buffer, AL_SIZE, &size);
75+
alGetBufferiHost(buffer, AL_BITS, &bits);
76+
alGetBufferiHost(buffer, AL_CHANNELS, &channels);
77+
alGetBufferiHost(buffer, AL_FREQUENCY, &fr);
78+
#else
7379
auto ctx = reinterpret_cast<ALCcontext*>(SoundDevice::bufferContextSt());
7480
alGetBufferiDirect(ctx, buffer, AL_SIZE, &size);
7581
alGetBufferiDirect(ctx, buffer, AL_BITS, &bits);
7682
alGetBufferiDirect(ctx, buffer, AL_CHANNELS, &channels);
7783
alGetBufferiDirect(ctx, buffer, AL_FREQUENCY, &fr);
84+
#endif
7885

7986
if(channels<=0 || fr<=0)
8087
return 0;
@@ -110,7 +117,7 @@ Sound::Sound(IDevice& f) {
110117
}
111118

112119
void Sound::initData(const char* bytes, int format, size_t size, size_t rate) {
113-
#if 0
120+
#if 1
114121
uint32_t b = 0;
115122
if(alGenBuffersHost(1, &b)!=AL_NO_ERROR) {
116123
throw std::bad_alloc();

Engine/sound/sounddevice.cpp

+59-1
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ struct SoundDevice::Device {
5050
ALCdevice* dev = nullptr;
5151
};
5252

53+
#if 0
5354
struct SoundDevice::BufferContext {
5455
BufferContext(const std::shared_ptr<SoundDevice::Device>& dev):dev(dev) {
5556
ctx = alcCreateContext(dev->dev,nullptr);
@@ -63,18 +64,25 @@ struct SoundDevice::BufferContext {
6364
std::shared_ptr<SoundDevice::Device> dev;
6465
ALCcontext* ctx = nullptr;
6566
};
67+
#endif
6668

6769
struct SoundDevice::PhysicalDeviceList {
6870
std::mutex sync;
6971
std::weak_ptr<SoundDevice::Device> val;
72+
#if 0
7073
BufferContext buf; // dummy context for buffers
74+
#endif
7175

7276
static PhysicalDeviceList& inst() {
7377
static PhysicalDeviceList list;
7478
return list;
7579
}
7680

77-
PhysicalDeviceList():buf(device("")) {
81+
PhysicalDeviceList()
82+
#if 0
83+
: buf(device(""))
84+
#endif
85+
{
7886
}
7987

8088
~PhysicalDeviceList(){}
@@ -113,6 +121,54 @@ SoundDevice::SoundDevice(std::string_view name):data(new Data()) {
113121
}, data.get());
114122
}
115123

124+
{
125+
alcSetThreadContext(data->context);
126+
ALenum e[] = {ALC_EVENT_TYPE_DEVICE_REMOVED_SOFT, ALC_EVENT_TYPE_DEVICE_ADDED_SOFT, ALC_EVENT_TYPE_DEFAULT_DEVICE_CHANGED_SOFT};
127+
alcEventControlSOFT(3, e, true);
128+
alcEventCallbackSOFT([](ALCenum eventType, ALCenum deviceType,
129+
ALCdevice *device, ALCsizei length,
130+
const ALCchar *message, void *userParam) noexcept {
131+
auto& data = *reinterpret_cast<const Data*>(userParam);
132+
if(deviceType!=ALC_PLAYBACK_DEVICE_SOFT)
133+
return;
134+
switch (eventType) {
135+
case ALC_EVENT_TYPE_DEFAULT_DEVICE_CHANGED_SOFT: {
136+
break;
137+
/*
138+
std::thread th([userParam]() {
139+
auto& data = *reinterpret_cast<const Data*>(userParam);
140+
auto s = alcReopenDeviceSOFT(data.dev->dev, nullptr, nullptr);
141+
auto name = alcGetString(data.dev->dev, ALC_ALL_DEVICES_SPECIFIER);
142+
Log::d("reopen sound device: ", name, " ret: ", (s ? "true" : "false"));
143+
});
144+
th.detach();
145+
*/
146+
147+
auto& data = *reinterpret_cast<const Data*>(userParam);
148+
auto s = alcReopenDeviceSOFT(data.dev->dev, nullptr, nullptr);
149+
auto name = alcGetString(data.dev->dev, ALC_ALL_DEVICES_SPECIFIER);
150+
Log::d("reopen sound device: ", name, " ret: ", (s ? "true" : "false"));
151+
152+
//alcReopenDeviceSOFT(data.dev->dev, nullptr, nullptr);
153+
break;
154+
}
155+
case ALC_EVENT_TYPE_DEVICE_ADDED_SOFT:
156+
Log::d("reopen sound device: ");
157+
break;
158+
case ALC_EVENT_TYPE_DEVICE_REMOVED_SOFT:
159+
case AL_EVENT_TYPE_DISCONNECTED_SOFT:
160+
break;
161+
default:
162+
break;
163+
}
164+
/*
165+
auto s = alcReopenDeviceSOFT(data.dev->dev, nullptr, nullptr);
166+
auto name = alcGetString(data.dev->dev, ALC_ALL_DEVICES_SPECIFIER);
167+
Log::d("reopen sound device: ", name, " ret: ", (s ? "true" : "false"));
168+
*/
169+
}, data.get());
170+
alcSetThreadContext(nullptr);
171+
}
116172

117173
alDistanceModelDirect(data->context, AL_LINEAR_DISTANCE);
118174
alDisableDirect(data->context, AL_STOP_SOURCES_ON_DISCONNECT_SOFT);
@@ -194,6 +250,7 @@ void* SoundDevice::context() {
194250
return data->context;
195251
}
196252

253+
#if 0
197254
void* SoundDevice::bufferContext() {
198255
auto& d = PhysicalDeviceList::inst().buf;
199256
return d.ctx;
@@ -203,6 +260,7 @@ void* SoundDevice::bufferContextSt() {
203260
auto& d = PhysicalDeviceList::inst().buf;
204261
return d.ctx;
205262
}
263+
#endif
206264

207265
std::unique_lock<std::mutex> SoundDevice::globalLock() {
208266
static std::mutex gil;

Engine/sound/sounddevice.h

+2
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,10 @@ class SoundDevice final {
5353
std::unique_ptr<Data> data;
5454

5555
void* context();
56+
#if 0
5657
void* bufferContext();
5758
static void* bufferContextSt();
59+
#endif
5860

5961
static std::unique_lock<std::mutex> globalLock();
6062

Engine/sound/soundeffect.cpp

+7-7
Original file line numberDiff line numberDiff line change
@@ -40,25 +40,25 @@ struct SoundEffect::Impl {
4040
ALsizei freq = src.frequency;
4141
ALenum frm = src.channels==2 ? AL_FORMAT_STEREO16 : AL_FORMAT_MONO16;
4242
ALCcontext* ctx = reinterpret_cast<ALCcontext*>(dev.context());
43-
ALCcontext* bufCtx = reinterpret_cast<ALCcontext*>(dev.bufferContext());
4443

4544
auto guard = SoundDevice::globalLock(); // for alGetError
46-
#if 0
45+
#if 1
4746
alGenBuffersHost(1, &stream);
4847
#else
48+
ALCcontext* bufCtx = reinterpret_cast<ALCcontext*>(dev.bufferContext());
4949
alGenBuffersDirect(bufCtx, 1, &stream);
5050
alcSetThreadContext(bufCtx);
5151
alBufferCallbackSOFT(stream, frm, freq, bufferCallback, this);
5252
alcSetThreadContext(nullptr);
5353
#endif
54-
alBufferCallbackDirectSOFT(bufCtx, stream, frm, freq, bufferCallback, this);
55-
if(alGetErrorDirect(bufCtx)!=AL_NO_ERROR) {
54+
alBufferCallbackDirectSOFT(ctx, stream, frm, freq, bufferCallback, this);
55+
if(alGetErrorDirect(ctx)!=AL_NO_ERROR) {
5656
throw std::bad_alloc();
5757
}
5858

5959
alGenSourcesDirect(ctx, 1, &source);
6060
if(alGetErrorDirect(ctx)!=AL_NO_ERROR) {
61-
#if 0
61+
#if 1
6262
alDeleteBuffersHost(1, &stream);
6363
#else
6464
alDeleteBuffersDirect(bufCtx, 1, &stream);
@@ -74,14 +74,14 @@ struct SoundEffect::Impl {
7474
if(source==0)
7575
return;
7676
auto* ctx = reinterpret_cast<ALCcontext*>(dev->context());
77-
auto* bufCtx = reinterpret_cast<ALCcontext*>(dev->bufferContext());
7877

7978
alSourcePausevDirect(ctx, 1, &source);
8079
alDeleteSourcesDirect(ctx, 1, &source);
8180
if(stream!=0) {
82-
#if 0
81+
#if 1
8382
alDeleteBuffersHost(1, &stream);
8483
#else
84+
auto* bufCtx = reinterpret_cast<ALCcontext*>(dev->bufferContext());
8585
alDeleteBuffersDirect(bufCtx, 1, &stream);
8686
#endif
8787
}

Engine/thirdparty/openal-soft/al/auxeffectslot.cpp

+8-11
Original file line numberDiff line numberDiff line change
@@ -124,17 +124,14 @@ inline auto LookupEffect(ALCdevice *device, ALuint id) noexcept -> ALeffect*
124124
return al::to_address(sublist.Effects->begin() + slidx);
125125
}
126126

127-
inline auto LookupBuffer(ALCdevice *device, ALuint id) noexcept -> ALbuffer*
127+
inline auto AquireBufferLock(ALCdevice *device, ALuint id) -> std::unique_lock<std::mutex>
128128
{
129-
const size_t lidx{(id-1) >> 6};
130-
const ALuint slidx{(id-1) & 0x3f};
129+
return ALbuffer::AquireBufferLock(device, id);
130+
};
131131

132-
if(lidx >= device->BufferList.size()) UNLIKELY
133-
return nullptr;
134-
BufferSubList &sublist = device->BufferList[lidx];
135-
if(sublist.FreeMask & (1_u64 << slidx)) UNLIKELY
136-
return nullptr;
137-
return al::to_address(sublist.Buffers->begin() + slidx);
132+
inline auto LookupBuffer(ALCdevice *device, ALuint id) noexcept -> ALbuffer*
133+
{
134+
return ALbuffer::LookupBuffer(device, id);
138135
}
139136

140137

@@ -570,7 +567,7 @@ try {
570567
al::intrusive_ptr<EffectState> state{factory->create()};
571568

572569
ALCdevice *device{context->mALDevice.get()};
573-
auto bufferlock = std::unique_lock{device->BufferLock};
570+
auto bufferlock = AquireBufferLock(device, static_cast<ALuint>(value));
574571
ALbuffer *buffer{};
575572
if(value)
576573
{
@@ -606,7 +603,7 @@ try {
606603
else
607604
{
608605
ALCdevice *device{context->mALDevice.get()};
609-
auto bufferlock = std::unique_lock{device->BufferLock};
606+
auto bufferlock = AquireBufferLock(device, static_cast<ALuint>(value));
610607
ALbuffer *buffer{};
611608
if(value)
612609
{

0 commit comments

Comments
 (0)