From 92f250316054e8753a233e2bf40e24d9935aa80d Mon Sep 17 00:00:00 2001 From: Christoph Oelckers Date: Sat, 27 May 2023 09:03:10 +0200 Subject: [PATCH] - did a bit of optimization on sfxinfo_t. Using two bytes of alignment gap to add a userval variable. --- source/common/audio/sound/s_soundinternal.h | 6 +++--- source/core/music/s_advsound.cpp | 14 +++----------- source/games/blood/src/sfx.cpp | 4 ++-- source/games/blood/src/sound.cpp | 8 +++----- source/games/duke/src/sounds.cpp | 19 +++++++++---------- source/games/duke/src/sounds.h | 1 - source/games/sw/src/sounds.cpp | 16 ++++------------ 7 files changed, 24 insertions(+), 44 deletions(-) diff --git a/source/common/audio/sound/s_soundinternal.h b/source/common/audio/sound/s_soundinternal.h index 822623529..dabd8e729 100644 --- a/source/common/audio/sound/s_soundinternal.h +++ b/source/common/audio/sound/s_soundinternal.h @@ -90,6 +90,7 @@ constexpr FSoundID INVALID_SOUND = FSoundID::fromInt(-1); float DefPitchMax = 0.f; // Randomized range with stronger control over pitch itself. int16_t NearLimit = 4; // 0 means unlimited. + int16_t UserVal = 0; // repurpose this gap for something useful uint8_t PitchMask = 0; bool bRandomHeader = false; bool bLoadRAW = false; @@ -99,17 +100,16 @@ constexpr FSoundID INVALID_SOUND = FSoundID::fromInt(-1); bool bTentative = true; bool bExternal = false; - TArray UserData; - int RawRate = 0; // Sample rate to use when bLoadRAW is true int LoopStart = -1; // -1 means no specific loop defined int LoopEnd = -1; // -1 means no specific loop defined + float Attenuation = 1.f; // Multiplies the attenuation passed to S_Sound. FSoundID link = NO_LINK; constexpr static FSoundID NO_LINK = FSoundID::fromInt(-1); + TArray UserData; FRolloffInfo Rolloff{}; - float Attenuation = 1.f; // Multiplies the attenuation passed to S_Sound. }; diff --git a/source/core/music/s_advsound.cpp b/source/core/music/s_advsound.cpp index 208de4875..96fbbca23 100644 --- a/source/core/music/s_advsound.cpp +++ b/source/core/music/s_advsound.cpp @@ -493,7 +493,7 @@ static void S_AddSNDINFO (int lump) sfx->UserData.Resize(Duke3d::kMaxUserData); memset(sfx->UserData.Data(), 0, Duke3d::kMaxUserData * sizeof(int)); } - sfx->UserData[Duke3d::kFlags] = flags; + sfx->UserVal = flags; } else { @@ -523,11 +523,7 @@ static void S_AddSNDINFO (int lump) if (isSWALL()) { auto sfx = soundEngine->GetWritableSfx(sfxid); - if (sfx->UserData.Size() < 1) - { - sfx->UserData.Resize(1); - } - sfx->UserData[0] = flags; + sfx->UserVal = flags; } else { @@ -560,11 +556,7 @@ static void S_AddSNDINFO (int lump) if (isBlood()) { auto sfx = soundEngine->GetWritableSfx(sfxid); - if (sfx->UserData.Size() < 1) - { - sfx->UserData.Resize(1); - } - sfx->UserData[0] = sc.Number; + sfx->UserVal = sc.Number; } else { diff --git a/source/games/blood/src/sfx.cpp b/source/games/blood/src/sfx.cpp index 754f39750..79f9d3e51 100644 --- a/source/games/blood/src/sfx.cpp +++ b/source/games/blood/src/sfx.cpp @@ -149,10 +149,10 @@ void GameInterface::UpdateSounds() FSoundID getSfx(FSoundID soundId, float& attenuation, int& relvol) { - auto udata = soundEngine->GetUserData(soundId); + auto udata = soundEngine->GetSfx(soundId); if (relvol < 0) relvol = 0; - else if (relvol == 0) relvol = udata && udata[0] ? udata[0] : 80; + else if (relvol == 0) relvol = udata && udata->UserVal ? udata->UserVal : 80; if (relvol > 255) relvol = 255; // Limit the attenuation. More than 2.0 is simply too much. attenuation = relvol > 0 ? clamp(80.f / relvol, 0.f, 2.f) : 1.f; diff --git a/source/games/blood/src/sound.cpp b/source/games/blood/src/sound.cpp index 8e1a0f170..e6a214870 100644 --- a/source/games/blood/src/sound.cpp +++ b/source/games/blood/src/sound.cpp @@ -91,7 +91,6 @@ static void S_AddBloodSFX(int lumpnum) { sfxnum = soundEngine->AddSoundLump(FStringf("SfxSound@%04d", resid), rawlump, 0, resid, 6); // use a generic name here in case sound replacements are being used. soundfx = soundEngine->GetWritableSfx(sfxnum); - soundfx->UserData.Resize(1); } if (sfx->format < 5 || sfx->format > 12) { // [0..4] + invalid formats @@ -117,8 +116,7 @@ static void S_AddBloodSFX(int lumpnum) else soundfx->DefPitch = 0; if (sfx->relVol != 80) // 80 is the default { - soundfx->UserData.Resize(1); - soundfx->UserData[0] = sfx->relVol; + soundfx->UserVal = sfx->relVol; } } } @@ -211,8 +209,8 @@ void sndStartSample(unsigned int nSound, int nVolume, int nChannel, bool bLoop, { if (nVolume < 0) { - auto udata = soundEngine->GetUserData(snd); - if (udata) nVolume = min(Scale(udata[0], 255, 80), 255); + auto udata = soundEngine->GetSfx(snd); + if (udata) nVolume = min(Scale(udata->UserVal, 255, 80), 255); else nVolume = 255; } if (bLoop) chanflags |= CHANF_LOOP; diff --git a/source/games/duke/src/sounds.cpp b/source/games/duke/src/sounds.cpp index 2d5c0ccb2..362d77d7a 100644 --- a/source/games/duke/src/sounds.cpp +++ b/source/games/duke/src/sounds.cpp @@ -176,9 +176,9 @@ void S_CacheAllSounds(void) int S_GetUserFlags(FSoundID soundid) { if (!soundEngine->isValidSoundId(soundid)) return 0; - auto const* snd = soundEngine->GetUserData(soundid); + auto const* snd = soundEngine->GetSfx(soundid); if (!snd) return 0; - return snd[kFlags]; + return snd->UserVal; } //========================================================================== @@ -222,15 +222,14 @@ int S_DefineSound(unsigned index, const char *filename, int minpitch, int maxpit auto& sndinf = sfx->UserData; sndinf[kVolAdjust] = 0; sndinf[kWorldTourMapping] = 0; - sndinf[kFlags] = 0; + sfx->UserVal = 0; } } sfx->ResourceId = index; sfx->UserData.Resize(kMaxUserData); sfx->bExternal = true; - auto& sndinf = sfx->UserData; - sndinf[kFlags] = (type & SF_CON_MASK); + sfx->UserVal = (type & SF_CON_MASK); // Take care of backslashes in sound names. Also double backslashes which occur in World Tour. FString fn = filename; @@ -250,8 +249,8 @@ int S_DefineSound(unsigned index, const char *filename, int minpitch, int maxpit sfx->DefPitch = (float)pow(2, clamp(minpitch, INT16_MIN, INT16_MAX) / 1200.); sfx->DefPitchMax = (float)pow(2, clamp(maxpitch, INT16_MIN, INT16_MAX) / 1200.); } - sndinf[kVolAdjust] = clamp(distance, INT16_MIN, INT16_MAX); - sndinf[kWorldTourMapping] = 0; + sfx->UserData[kVolAdjust] = clamp(distance, INT16_MIN, INT16_MAX); + sfx->UserData[kWorldTourMapping] = 0; sfx->Volume = volume; sfx->bTentative = false; return 0; @@ -276,9 +275,9 @@ static int GetPositionInfo(DDukeActor* actor, FSoundID soundid, sectortype* sect // However, ultimately rolloff would also just reposition the sound source so this can remain as it is. int orgsndist = 0, sndist = 0; - auto const* snd = soundEngine->GetUserData(soundid); - int userflags = snd ? snd[kFlags] : 0; - int dist_adjust = snd ? snd[kVolAdjust] : 0; + auto const* sfx = soundEngine->GetSfx(soundid); + int userflags = sfx->UserVal; + int dist_adjust = sfx->UserData.Size() ? sfx->UserData[kVolAdjust] : 0; FVector3 sndorg = GetSoundPos(pos); FVector3 campos = GetSoundPos(cam); diff --git a/source/games/duke/src/sounds.h b/source/games/duke/src/sounds.h index 54d8402f9..81af46f46 100644 --- a/source/games/duke/src/sounds.h +++ b/source/games/duke/src/sounds.h @@ -26,7 +26,6 @@ enum { enum esound_t { kVolAdjust, - kFlags, kWorldTourMapping, kMaxUserData }; diff --git a/source/games/sw/src/sounds.cpp b/source/games/sw/src/sounds.cpp index 4a59ede36..7f0423610 100644 --- a/source/games/sw/src/sounds.cpp +++ b/source/games/sw/src/sounds.cpp @@ -216,7 +216,7 @@ void InitAmbient(int num, DSWActor* actor) amb->vocIndex = vnum; amb->ChanFlags = CHANF_TRANSIENT; if (ambarray[num].ambient_flags & v3df_dontpan) amb->ChanFlags |= EChanFlags::FromInt(CHANEXF_DONTPAN); - if (sfx->UserData[0] & SFLAG_LOOP) amb->ChanFlags |= CHANF_LOOP; + if (sfx->UserVal & SFLAG_LOOP) amb->ChanFlags |= CHANF_LOOP; amb->maxIndex = ambarray[num].maxtics; amb->curIndex = 0; amb->intermit = !!(ambarray[num].ambient_flags & v3df_intermit); @@ -432,14 +432,6 @@ void InitFX(void) { auto &S_sfx = soundEngine->GetSounds(); - for (auto& sfx : S_sfx) - { - if (sfx.UserData.Size() < 1) - { - sfx.UserData.Resize(1); - sfx.UserData[0] = 0; - } - } soundEngine->HashSounds(); for (auto& sfx : S_sfx) @@ -632,7 +624,7 @@ int _PlaySound(const FSoundID sndid, DSWActor* actor, PLAYER* pp, const DVector3 //if (flags & v3df_doppler) cflags |= EChanFlags::FromInt(CHANEXF_NODOPPLER); // intentionally not implemented //if (flags & v3df_dontpan) cflags |= EChanFlags::FromInt(CHANEXF_DONTPAN); // disabled due to poor use - if (sfx->UserData[0] & SFLAG_LOOP) cflags |= CHANF_LOOP; // with the new sound engine these can just be started and don't have to be stopped ever. + if (sfx->UserVal & SFLAG_LOOP) cflags |= CHANF_LOOP; // with the new sound engine these can just be started and don't have to be stopped ever. FVector3 spos = GetSoundPos(pos); auto chan = soundEngine->StartSound(sourcetype, source, &spos, channel, cflags, sndid, 1.f, ATTN_NORM); @@ -772,11 +764,11 @@ int _PlayerSound(int num, PLAYER* pp) // If this is a player voice and he's already yacking, forget it. // Not a player voice, bail. - if (!(sfx->UserData[0] & (SFLAG_PLAYERSPEECH|SFLAG_PLAYERVOICE))) + if (!(sfx->UserVal & (SFLAG_PLAYERSPEECH|SFLAG_PLAYERVOICE))) return 0; // Don't talk if not allowed to. - if ((sfx->UserData[0] & SFLAG_PLAYERSPEECH) && !snd_speech) + if ((sfx->UserVal & SFLAG_PLAYERSPEECH) && !snd_speech) return 0; // The surfacing sound should not block other player speech.