diff options
author | Chris Robinson <[email protected]> | 2020-08-28 00:09:46 -0700 |
---|---|---|
committer | Chris Robinson <[email protected]> | 2020-08-28 00:09:46 -0700 |
commit | 1f486f820e97fd5ce1da40a87aa3b743800fb5b0 (patch) | |
tree | 04e70d2410fcaad3167899b81b2661b57c0488fe /al | |
parent | ecf30de36f6487c1f8a19ae0d03ba810078706f4 (diff) |
Use a separate structure for buffer storage
Diffstat (limited to 'al')
-rw-r--r-- | al/auxeffectslot.cpp | 12 | ||||
-rw-r--r-- | al/buffer.cpp | 92 | ||||
-rw-r--r-- | al/buffer.h | 22 | ||||
-rw-r--r-- | al/source.cpp | 43 |
4 files changed, 79 insertions, 90 deletions
diff --git a/al/auxeffectslot.cpp b/al/auxeffectslot.cpp index 5b2a7f44..fb2d2233 100644 --- a/al/auxeffectslot.cpp +++ b/al/auxeffectslot.cpp @@ -453,7 +453,7 @@ START_API_FUNC { buffer = LookupBuffer(device, static_cast<ALuint>(value)); if(!buffer) SETERR_RETURN(context, AL_INVALID_VALUE,, "Invalid buffer ID"); - if(buffer->Callback) + if(buffer->mBuffer.mCallback) SETERR_RETURN(context, AL_INVALID_OPERATION,, "Callback buffer not valid for effects"); @@ -469,8 +469,9 @@ START_API_FUNC { FPUCtl mixer_mode{}; auto *state = slot->Effect.State.get(); - slot->Effect.Buffer.reset(state->createBuffer(device, buffer->mData.data(), - buffer->Frequency, buffer->mFmtType, buffer->mFmtChannels, buffer->SampleLen)); + slot->Effect.Buffer.reset(state->createBuffer(device, buffer->mBuffer.mData.data(), + buffer->mBuffer.mSampleRate, buffer->mBuffer.mType, buffer->mBuffer.mChannels, + buffer->mBuffer.mSampleLen)); } } break; @@ -745,8 +746,9 @@ ALenum ALeffectslot::initEffect(ALeffect *effect, ALCcontext *context) State->deviceUpdate(Device); Effect.Buffer = nullptr; if(Buffer) - Effect.Buffer.reset(State->createBuffer(Device, Buffer->mData.data(), - Buffer->Frequency, Buffer->mFmtType, Buffer->mFmtChannels, Buffer->SampleLen)); + Effect.Buffer.reset(State->createBuffer(Device, Buffer->mBuffer.mData.data(), + Buffer->mBuffer.mSampleRate, Buffer->mBuffer.mType, Buffer->mBuffer.mChannels, + Buffer->mBuffer.mSampleLen)); } if(!effect) diff --git a/al/buffer.cpp b/al/buffer.cpp index b795964a..f43c756f 100644 --- a/al/buffer.cpp +++ b/al/buffer.cpp @@ -466,11 +466,11 @@ void LoadData(ALCcontext *context, ALbuffer *ALBuf, ALsizei freq, ALuint size, if((access&AL_PRESERVE_DATA_BIT_SOFT)) { /* Can only preserve data with the same format and alignment. */ - if UNLIKELY(ALBuf->mFmtChannels != DstChannels || ALBuf->OriginalType != SrcType) + if UNLIKELY(ALBuf->mBuffer.mChannels != DstChannels || ALBuf->OriginalType != SrcType) SETERR_RETURN(context, AL_INVALID_VALUE,, "Preserving data of mismatched format"); if UNLIKELY(ALBuf->OriginalAlign != align) SETERR_RETURN(context, AL_INVALID_VALUE,, "Preserving data of mismatched alignment"); - if(ALBuf->AmbiOrder != ambiorder) + if(ALBuf->mBuffer.mAmbiOrder != ambiorder) SETERR_RETURN(context, AL_INVALID_VALUE,, "Preserving data of mismatched order"); } @@ -508,55 +508,55 @@ void LoadData(ALCcontext *context, ALbuffer *ALBuf, ALsizei freq, ALuint size, * use AL_SIZE to try to get the buffer's play length. */ newsize = RoundUp(newsize, 16); - if(newsize != ALBuf->mData.size()) + if(newsize != ALBuf->mBuffer.mData.size()) { auto newdata = al::vector<al::byte,16>(newsize, al::byte{}); if((access&AL_PRESERVE_DATA_BIT_SOFT)) { - const size_t tocopy{minz(newdata.size(), ALBuf->mData.size())}; - std::copy_n(ALBuf->mData.begin(), tocopy, newdata.begin()); + const size_t tocopy{minz(newdata.size(), ALBuf->mBuffer.mData.size())}; + std::copy_n(ALBuf->mBuffer.mData.begin(), tocopy, newdata.begin()); } - newdata.swap(ALBuf->mData); + newdata.swap(ALBuf->mBuffer.mData); } if(SrcType == UserFmtIMA4) { assert(DstType == FmtShort); - if(SrcData != nullptr && !ALBuf->mData.empty()) - Convert_int16_ima4(reinterpret_cast<int16_t*>(ALBuf->mData.data()), SrcData, + if(SrcData != nullptr && !ALBuf->mBuffer.mData.empty()) + Convert_int16_ima4(reinterpret_cast<int16_t*>(ALBuf->mBuffer.mData.data()), SrcData, NumChannels, frames, align); ALBuf->OriginalAlign = align; } else if(SrcType == UserFmtMSADPCM) { assert(DstType == FmtShort); - if(SrcData != nullptr && !ALBuf->mData.empty()) - Convert_int16_msadpcm(reinterpret_cast<int16_t*>(ALBuf->mData.data()), SrcData, + if(SrcData != nullptr && !ALBuf->mBuffer.mData.empty()) + Convert_int16_msadpcm(reinterpret_cast<int16_t*>(ALBuf->mBuffer.mData.data()), SrcData, NumChannels, frames, align); ALBuf->OriginalAlign = align; } else { assert(static_cast<long>(SrcType) == static_cast<long>(DstType)); - if(SrcData != nullptr && !ALBuf->mData.empty()) - std::copy_n(SrcData, frames*FrameSize, ALBuf->mData.begin()); + if(SrcData != nullptr && !ALBuf->mBuffer.mData.empty()) + std::copy_n(SrcData, frames*FrameSize, ALBuf->mBuffer.mData.begin()); ALBuf->OriginalAlign = 1; } ALBuf->OriginalSize = size; ALBuf->OriginalType = SrcType; - ALBuf->Frequency = static_cast<ALuint>(freq); - ALBuf->mFmtChannels = DstChannels; - ALBuf->mFmtType = DstType; + ALBuf->mBuffer.mSampleRate = static_cast<ALuint>(freq); + ALBuf->mBuffer.mChannels = DstChannels; + ALBuf->mBuffer.mType = DstType; ALBuf->Access = access; - ALBuf->AmbiOrder = ambiorder; + ALBuf->mBuffer.mAmbiOrder = ambiorder; - ALBuf->Callback = nullptr; - ALBuf->UserData = nullptr; + ALBuf->mBuffer.mCallback = nullptr; + ALBuf->mBuffer.mUserData = nullptr; - ALBuf->SampleLen = frames; + ALBuf->mBuffer.mSampleLen = frames; ALBuf->LoopStart = 0; - ALBuf->LoopEnd = ALBuf->SampleLen; + ALBuf->LoopEnd = ALBuf->mBuffer.mSampleLen; } /** Prepares the buffer to use the specified callback, using the specified format. */ @@ -605,24 +605,24 @@ void PrepareCallback(ALCcontext *context, ALbuffer *ALBuf, ALsizei freq, ALBuf->UnpackAmbiOrder : 0}; al::vector<al::byte,16>(FrameSizeFromFmt(DstChannels, DstType, ambiorder) * - size_t{BUFFERSIZE + (MAX_RESAMPLER_PADDING>>1)}).swap(ALBuf->mData); + size_t{BUFFERSIZE + (MAX_RESAMPLER_PADDING>>1)}).swap(ALBuf->mBuffer.mData); - ALBuf->Callback = callback; - ALBuf->UserData = userptr; + ALBuf->mBuffer.mCallback = callback; + ALBuf->mBuffer.mUserData = userptr; ALBuf->OriginalType = SrcType; ALBuf->OriginalSize = 0; ALBuf->OriginalAlign = 1; - ALBuf->Frequency = static_cast<ALuint>(freq); - ALBuf->mFmtChannels = DstChannels; - ALBuf->mFmtType = DstType; + ALBuf->mBuffer.mSampleRate = static_cast<ALuint>(freq); + ALBuf->mBuffer.mChannels = DstChannels; + ALBuf->mBuffer.mType = DstType; ALBuf->Access = 0; - ALBuf->AmbiOrder = ambiorder; + ALBuf->mBuffer.mAmbiOrder = ambiorder; - ALBuf->SampleLen = 0; + ALBuf->mBuffer.mSampleLen = 0; ALBuf->LoopStart = 0; - ALBuf->LoopEnd = ALBuf->SampleLen; + ALBuf->LoopEnd = ALBuf->mBuffer.mSampleLen; } @@ -883,7 +883,7 @@ START_API_FUNC offset, length, buffer); else { - void *retval = albuf->mData.data() + offset; + void *retval{albuf->mBuffer.mData.data() + offset}; albuf->MappedAccess = access; albuf->MappedOffset = offset; albuf->MappedSize = length; @@ -977,15 +977,15 @@ START_API_FUNC ALuint align{SanitizeAlignment(usrfmt->type, unpack_align)}; if UNLIKELY(align < 1) context->setError(AL_INVALID_VALUE, "Invalid unpack alignment %u", unpack_align); - else if UNLIKELY(long{usrfmt->channels} != long{albuf->mFmtChannels} + else if UNLIKELY(long{usrfmt->channels} != long{albuf->mBuffer.mChannels} || usrfmt->type != albuf->OriginalType) context->setError(AL_INVALID_ENUM, "Unpacking data with mismatched format"); else if UNLIKELY(align != albuf->OriginalAlign) context->setError(AL_INVALID_VALUE, "Unpacking data with alignment %u does not match original alignment %u", align, albuf->OriginalAlign); - else if UNLIKELY((albuf->mFmtChannels == FmtBFormat2D || albuf->mFmtChannels == FmtBFormat3D) - && albuf->UnpackAmbiOrder != albuf->AmbiOrder) + else if UNLIKELY(albuf->mBuffer.isBFormat() + && albuf->UnpackAmbiOrder != albuf->mBuffer.mAmbiOrder) context->setError(AL_INVALID_VALUE, "Unpacking data with mismatched ambisonic order"); else if UNLIKELY(albuf->MappedAccess != 0) context->setError(AL_INVALID_OPERATION, "Unpacking data into mapped buffer %u", buffer); @@ -1017,16 +1017,16 @@ START_API_FUNC size_t byteoff{static_cast<ALuint>(offset)/byte_align * align * frame_size}; size_t samplen{static_cast<ALuint>(length)/byte_align * align}; - void *dst = albuf->mData.data() + byteoff; - if(usrfmt->type == UserFmtIMA4 && albuf->mFmtType == FmtShort) + void *dst = albuf->mBuffer.mData.data() + byteoff; + if(usrfmt->type == UserFmtIMA4 && albuf->mBuffer.mType == FmtShort) Convert_int16_ima4(static_cast<int16_t*>(dst), static_cast<const al::byte*>(data), num_chans, samplen, align); - else if(usrfmt->type == UserFmtMSADPCM && albuf->mFmtType == FmtShort) + else if(usrfmt->type == UserFmtMSADPCM && albuf->mBuffer.mType == FmtShort) Convert_int16_msadpcm(static_cast<int16_t*>(dst), static_cast<const al::byte*>(data), num_chans, samplen, align); else { - assert(long{usrfmt->type} == long{albuf->mFmtType}); + assert(long{usrfmt->type} == long{albuf->mBuffer.mType}); memcpy(dst, data, size_t{samplen} * frame_size); } } @@ -1177,7 +1177,7 @@ START_API_FUNC else if UNLIKELY(value != AL_FUMA_SOFT && value != AL_ACN_SOFT) context->setError(AL_INVALID_VALUE, "Invalid unpack ambisonic layout %04x", value); else - albuf->AmbiLayout = value; + albuf->mBuffer.mAmbiLayout = static_cast<AmbiLayout>(value); break; case AL_AMBISONIC_SCALING_SOFT: @@ -1187,7 +1187,7 @@ START_API_FUNC else if UNLIKELY(value != AL_FUMA_SOFT && value != AL_SN3D_SOFT && value != AL_N3D_SOFT) context->setError(AL_INVALID_VALUE, "Invalid unpack ambisonic scaling %04x", value); else - albuf->AmbiScaling = value; + albuf->mBuffer.mAmbiScaling = static_cast<AmbiScaling>(value); break; case AL_UNPACK_AMBISONIC_ORDER_SOFT: @@ -1258,7 +1258,7 @@ START_API_FUNC context->setError(AL_INVALID_OPERATION, "Modifying in-use buffer %u's loop points", buffer); else if UNLIKELY(values[0] < 0 || values[0] >= values[1] - || static_cast<ALuint>(values[1]) > albuf->SampleLen) + || static_cast<ALuint>(values[1]) > albuf->mBuffer.mSampleLen) context->setError(AL_INVALID_VALUE, "Invalid loop point range %d -> %d on buffer %u", values[0], values[1], buffer); else @@ -1363,7 +1363,7 @@ START_API_FUNC else switch(param) { case AL_FREQUENCY: - *value = static_cast<ALint>(albuf->Frequency); + *value = static_cast<ALint>(albuf->mBuffer.mSampleRate); break; case AL_BITS: @@ -1375,7 +1375,7 @@ START_API_FUNC break; case AL_SIZE: - *value = static_cast<ALint>(albuf->SampleLen * albuf->frameSizeFromFmt()); + *value = static_cast<ALint>(albuf->mBuffer.mSampleLen * albuf->frameSizeFromFmt()); break; case AL_UNPACK_BLOCK_ALIGNMENT_SOFT: @@ -1387,11 +1387,11 @@ START_API_FUNC break; case AL_AMBISONIC_LAYOUT_SOFT: - *value = albuf->AmbiLayout; + *value = static_cast<int>(albuf->mBuffer.mAmbiLayout); break; case AL_AMBISONIC_SCALING_SOFT: - *value = albuf->AmbiScaling; + *value = static_cast<int>(albuf->mBuffer.mAmbiScaling); break; case AL_UNPACK_AMBISONIC_ORDER_SOFT: @@ -1516,10 +1516,10 @@ START_API_FUNC else switch(param) { case AL_BUFFER_CALLBACK_FUNCTION_SOFT: - *value = reinterpret_cast<void*>(albuf->Callback); + *value = reinterpret_cast<void*>(albuf->mBuffer.mCallback); break; case AL_BUFFER_CALLBACK_USER_PARAM_SOFT: - *value = albuf->UserData; + *value = albuf->mBuffer.mUserData; break; default: diff --git a/al/buffer.h b/al/buffer.h index 6b1d45f5..bdc87e07 100644 --- a/al/buffer.h +++ b/al/buffer.h @@ -39,27 +39,14 @@ enum UserFmtChannels : unsigned char { struct ALbuffer { - al::vector<al::byte,16> mData; + BufferStorage mBuffer; - ALuint Frequency{0u}; ALbitfieldSOFT Access{0u}; - ALuint SampleLen{0u}; - - FmtChannels mFmtChannels{}; - FmtType mFmtType{}; UserFmtType OriginalType{}; ALuint OriginalSize{0}; ALuint OriginalAlign{0}; - ALenum AmbiLayout{AL_FUMA_SOFT}; - ALenum AmbiScaling{AL_FUMA_SOFT}; - /* AmbiOrder is only updated when loading new data. */ - ALuint AmbiOrder{0}; - - LPALBUFFERCALLBACKTYPESOFT Callback{nullptr}; - void *UserData{nullptr}; - ALuint LoopStart{0u}; ALuint LoopEnd{0u}; @@ -77,10 +64,9 @@ struct ALbuffer { /* Self ID */ ALuint id{0}; - inline ALuint bytesFromFmt() const noexcept { return BytesFromFmt(mFmtType); } - inline ALuint channelsFromFmt() const noexcept - { return ChannelsFromFmt(mFmtChannels, AmbiOrder); } - inline ALuint frameSizeFromFmt() const noexcept { return channelsFromFmt() * bytesFromFmt(); } + inline ALuint bytesFromFmt() const noexcept { return mBuffer.bytesFromFmt(); } + inline ALuint channelsFromFmt() const noexcept { return mBuffer.channelsFromFmt(); } + inline ALuint frameSizeFromFmt() const noexcept { return mBuffer.frameSizeFromFmt(); } DISABLE_ALLOC() }; diff --git a/al/source.cpp b/al/source.cpp index a4c96deb..9771fec7 100644 --- a/al/source.cpp +++ b/al/source.cpp @@ -257,7 +257,7 @@ double GetSourceSecOffset(ALsource *Source, ALCcontext *context, nanoseconds *cl } assert(BufferFmt != nullptr); - return static_cast<double>(readPos) / double{FRACTIONONE} / BufferFmt->Frequency; + return static_cast<double>(readPos) / double{FRACTIONONE} / BufferFmt->mBuffer.mSampleRate; } /* GetSourceOffset @@ -313,7 +313,8 @@ double GetSourceOffset(ALsource *Source, ALenum name, ALCcontext *context) switch(name) { case AL_SEC_OFFSET: - offset = (readPos + static_cast<double>(readPosFrac)/FRACTIONONE) / BufferFmt->Frequency; + offset = (readPos + static_cast<double>(readPosFrac)/FRACTIONONE) / + BufferFmt->mBuffer.mSampleRate; break; case AL_SAMPLE_OFFSET: @@ -409,7 +410,7 @@ al::optional<VoicePos> GetSampleOffset(ALbufferlistitem *BufferList, ALenum Offs break; case AL_SEC_OFFSET: - dblfrac = std::modf(Offset*BufferFmt->Frequency, &dbloff); + dblfrac = std::modf(Offset*BufferFmt->mBuffer.mSampleRate, &dbloff); offset = static_cast<ALuint>(mind(dbloff, std::numeric_limits<ALuint>::max())); frac = static_cast<ALuint>(mind(dblfrac*FRACTIONONE, FRACTIONONE-1.0)); break; @@ -442,14 +443,14 @@ void InitVoice(Voice *voice, ALsource *source, ALbufferlistitem *BufferList, ALC ALbuffer *buffer{BufferList->mBuffer}; ALuint num_channels{buffer->channelsFromFmt()}; - voice->mFrequency = buffer->Frequency; - voice->mFmtChannels = buffer->mFmtChannels; + voice->mFrequency = buffer->mBuffer.mSampleRate; + voice->mFmtChannels = buffer->mBuffer.mChannels; voice->mSampleSize = buffer->bytesFromFmt(); - voice->mAmbiLayout = static_cast<AmbiLayout>(buffer->AmbiLayout); - voice->mAmbiScaling = static_cast<AmbiNorm>(buffer->AmbiScaling); - voice->mAmbiOrder = buffer->AmbiOrder; + voice->mAmbiLayout = buffer->mBuffer.mAmbiLayout; + voice->mAmbiScaling = buffer->mBuffer.mAmbiScaling; + voice->mAmbiOrder = buffer->mBuffer.mAmbiOrder; - if(buffer->Callback) voice->mFlags |= VOICE_IS_CALLBACK; + if(buffer->mBuffer.mCallback) voice->mFlags |= VOICE_IS_CALLBACK; else if(source->SourceType == AL_STATIC) voice->mFlags |= VOICE_IS_STATIC; voice->mNumCallbackSamples = 0; @@ -1311,7 +1312,7 @@ bool SetSourceiv(ALsource *Source, ALCcontext *Context, SourceProp prop, const a if(buffer && buffer->MappedAccess && !(buffer->MappedAccess&AL_MAP_PERSISTENT_BIT_SOFT)) SETERR_RETURN(Context, AL_INVALID_OPERATION, false, "Setting non-persistently mapped buffer %u", buffer->id); - else if(buffer && buffer->Callback && ReadRef(buffer->ref) != 0) + else if(buffer && buffer->mBuffer.mCallback && ReadRef(buffer->ref) != 0) SETERR_RETURN(Context, AL_INVALID_OPERATION, false, "Setting already-set callback buffer %u", buffer->id); else @@ -1327,7 +1328,7 @@ bool SetSourceiv(ALsource *Source, ALCcontext *Context, SourceProp prop, const a { /* Add the selected buffer to a one-item queue */ auto newlist = new ALbufferlistitem{}; - newlist->mSampleLen = buffer->SampleLen; + newlist->mSampleLen = buffer->mBuffer.mSampleLen; newlist->mBuffer = buffer; IncrementRef(buffer->ref); @@ -2858,8 +2859,8 @@ START_API_FUNC while(BufferList && BufferList->mSampleLen == 0) { ALbuffer *buffer{BufferList->mBuffer}; - if(buffer && buffer->Callback) break; - + if(buffer && buffer->mBuffer.mCallback) + break; BufferList = BufferList->mNext.load(std::memory_order_relaxed); } @@ -3209,7 +3210,7 @@ START_API_FUNC context->setError(AL_INVALID_NAME, "Queueing invalid buffer ID %u", buffers[i]); goto buffer_error; } - if(buffer && buffer->Callback) + if(buffer && buffer->mBuffer.mCallback) { context->setError(AL_INVALID_OPERATION, "Queueing callback buffer %u", buffers[i]); goto buffer_error; @@ -3227,7 +3228,7 @@ START_API_FUNC BufferList = item; } BufferList->mNext.store(nullptr, std::memory_order_relaxed); - BufferList->mSampleLen = buffer ? buffer->SampleLen : 0; + BufferList->mSampleLen = buffer ? buffer->mBuffer.mSampleLen : 0; BufferList->mBuffer = buffer; if(!buffer) continue; @@ -3244,14 +3245,14 @@ START_API_FUNC BufferFmt = buffer; else { - fmt_mismatch |= BufferFmt->Frequency != buffer->Frequency; - fmt_mismatch |= BufferFmt->mFmtChannels != buffer->mFmtChannels; - if(BufferFmt->mFmtChannels == FmtBFormat2D || BufferFmt->mFmtChannels == FmtBFormat3D) + fmt_mismatch |= BufferFmt->mBuffer.mSampleRate != buffer->mBuffer.mSampleRate; + fmt_mismatch |= BufferFmt->mBuffer.mChannels != buffer->mBuffer.mChannels; + if(BufferFmt->mBuffer.isBFormat()) { - fmt_mismatch |= BufferFmt->AmbiLayout != buffer->AmbiLayout; - fmt_mismatch |= BufferFmt->AmbiScaling != buffer->AmbiScaling; + fmt_mismatch |= BufferFmt->mBuffer.mAmbiLayout != buffer->mBuffer.mAmbiLayout; + fmt_mismatch |= BufferFmt->mBuffer.mAmbiScaling != buffer->mBuffer.mAmbiScaling; } - fmt_mismatch |= BufferFmt->AmbiOrder != buffer->AmbiOrder; + fmt_mismatch |= BufferFmt->mBuffer.mAmbiOrder != buffer->mBuffer.mAmbiOrder; fmt_mismatch |= BufferFmt->OriginalType != buffer->OriginalType; } if(fmt_mismatch) |