Skip to content

Commit

Permalink
Simplify {vulkan,dx12,xaudio}_check macro
Browse files Browse the repository at this point in the history
This changes `vulkan_check` and other macros to directly take the call
to be executed. It will execute it and check the result, if it's not
successful, it will log an error with the function name.

It also returns the result code, allowing further processing.
  • Loading branch information
brakhane committed Jan 13, 2025
1 parent 4660a68 commit cc01bd1
Show file tree
Hide file tree
Showing 7 changed files with 363 additions and 576 deletions.
123 changes: 57 additions & 66 deletions WickedEngine/wiAudio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ static constexpr T AlignTo(T value, T alignment)
#define fourccXWMA 'AMWX'
#define fourccDPDS 'sdpd'

#define xaudio_check(hr) wilog_assert(SUCCEEDED(hr), "XAudio2 error: %s, line %d, hr = %s", relative_path(__FILE__), __LINE__, wi::helper::GetPlatformErrorString(hr).c_str())
#define xaudio_assert(cond, fname) { wilog_assert(cond, "XAudio2 error: %s failed with %s (%s:%d)", fname, wi::helper::GetPlatformErrorString(hr), relative_path(__FILE__), __LINE__); }

#define xaudio_check(call) [&]() { HRESULT hr = call; char buf[256]; xaudio_assert(SUCCEEDED(hr), wi::backlog::internal::extract_function_name(buf, #call)); return hr; }()

namespace wi::audio
{
Expand Down Expand Up @@ -87,17 +89,15 @@ namespace wi::audio
wi::Timer timer;

HRESULT hr;
hr = CoInitializeEx(NULL, COINIT_MULTITHREADED);
hr = xaudio_check(CoInitializeEx(NULL, COINIT_MULTITHREADED));
if (!SUCCEEDED(hr))
{
wilog_error("XAudio2: CoInitializeEx returned error: %s", wi::helper::GetPlatformErrorString(hr).c_str());
return;
}

hr = XAudio2Create(&audioEngine, 0, XAUDIO2_USE_DEFAULT_PROCESSOR);
hr = xaudio_check(XAudio2Create(&audioEngine, 0, XAUDIO2_USE_DEFAULT_PROCESSOR));
if (!SUCCEEDED(hr))
{
wilog_error("XAudio2: XAudio2Create returned error: %s", wi::helper::GetPlatformErrorString(hr).c_str());
return;
}

Expand All @@ -108,10 +108,9 @@ namespace wi::audio
audioEngine->SetDebugConfiguration(&debugConfig);
#endif // _DEBUG

hr = audioEngine->CreateMasteringVoice(&masteringVoice);
hr = xaudio_check(audioEngine->CreateMasteringVoice(&masteringVoice));
if (!SUCCEEDED(hr))
{
wilog_error("XAudio2: CreateMasteringVoice returned error: %s", wi::helper::GetPlatformErrorString(hr).c_str());
return;
}

Expand All @@ -123,64 +122,59 @@ namespace wi::audio

for (int i = 0; i < SUBMIX_TYPE_COUNT; ++i)
{
hr = audioEngine->CreateSubmixVoice(
hr = xaudio_check(audioEngine->CreateSubmixVoice(
&submixVoices[i],
masteringVoiceDetails.InputChannels,
masteringVoiceDetails.InputSampleRate,
0,
0,
0,
0
);
));

if (!SUCCEEDED(hr))
{
wilog_error("XAudio2: CreateSubmixVoice returned error: %s", wi::helper::GetPlatformErrorString(hr).c_str());
return;
}
}

DWORD channelMask;
masteringVoice->GetChannelMask(&channelMask);
hr = X3DAudioInitialize(channelMask, X3DAUDIO_SPEED_OF_SOUND, audio3D);
hr = xaudio_check(X3DAudioInitialize(channelMask, X3DAUDIO_SPEED_OF_SOUND, audio3D));
if (!SUCCEEDED(hr))
{
wilog_error("XAudio2: X3DAudioInitialize returned error: %s", wi::helper::GetPlatformErrorString(hr).c_str());
return;
}

// Reverb setup:
{
hr = XAudio2CreateReverb(&reverbEffect);
hr = xaudio_check(XAudio2CreateReverb(&reverbEffect));
if (!SUCCEEDED(hr))
{
wilog_error("XAudio2: XAudio2CreateReverb returned error: %s", wi::helper::GetPlatformErrorString(hr).c_str());
return;
}

XAUDIO2_EFFECT_DESCRIPTOR effects[] = { { reverbEffect.Get(), TRUE, 1 } };
XAUDIO2_EFFECT_CHAIN effectChain = { arraysize(effects), effects };
hr = audioEngine->CreateSubmixVoice(
hr = xaudio_check(audioEngine->CreateSubmixVoice(
&reverbSubmix,
1, // reverb is mono
masteringVoiceDetails.InputSampleRate,
0,
0,
nullptr,
&effectChain
);
));
if (!SUCCEEDED(hr))
{
wilog_error("XAudio2: CreateSubmixVoice returned error: %s", wi::helper::GetPlatformErrorString(hr).c_str());
return;
}

XAUDIO2FX_REVERB_PARAMETERS native;
ReverbConvertI3DL2ToNative(&reverbPresets[REVERB_PRESET_DEFAULT], &native);
HRESULT hr = reverbSubmix->SetEffectParameters(0, &native, sizeof(native));
HRESULT hr = xaudio_check(reverbSubmix->SetEffectParameters(0, &native, sizeof(native)));
if (!SUCCEEDED(hr))
{
wilog_error("XAudio2: SetEffectParameters returned error: %s", wi::helper::GetPlatformErrorString(hr).c_str());
return;
}
}
Expand Down Expand Up @@ -443,18 +437,18 @@ namespace wi::audio
instanceinternal->audio = audio_internal;
instanceinternal->soundinternal = soundinternal;

XAUDIO2_SEND_DESCRIPTOR SFXSend[] = {
XAUDIO2_SEND_DESCRIPTOR SFXSend[] = {
{ XAUDIO2_SEND_USEFILTER, instanceinternal->audio->submixVoices[instance->type] },
{ XAUDIO2_SEND_USEFILTER, instanceinternal->audio->reverbSubmix }, // this should be last to enable/disable reverb simply
};
XAUDIO2_VOICE_SENDS SFXSendList = {
XAUDIO2_VOICE_SENDS SFXSendList = {
(instance->IsEnableReverb() && instanceinternal->audio->reverbSubmix != nullptr) ? (uint32_t)arraysize(SFXSend) : 1,
SFXSend
SFXSend
};

hr = instanceinternal->audio->audioEngine->CreateSourceVoice(&instanceinternal->sourceVoice, &soundinternal->wfx,
0, XAUDIO2_DEFAULT_FREQ_RATIO, instanceinternal.get(), &SFXSendList, NULL);
xaudio_check(hr);
hr = xaudio_check(instanceinternal->audio->audioEngine->CreateSourceVoice(&instanceinternal->sourceVoice, &soundinternal->wfx,
0, XAUDIO2_DEFAULT_FREQ_RATIO, instanceinternal.get(), &SFXSendList, NULL));

if (FAILED(hr))
{
return false;
Expand Down Expand Up @@ -494,8 +488,8 @@ namespace wi::audio
instanceinternal->buffer.Flags = XAUDIO2_END_OF_STREAM;
instanceinternal->buffer.LoopCount = instance->IsLooped() ? XAUDIO2_LOOP_INFINITE : 0;

hr = instanceinternal->sourceVoice->SubmitSourceBuffer(&instanceinternal->buffer);
xaudio_check(hr);
hr = xaudio_check(instanceinternal->sourceVoice->SubmitSourceBuffer(&instanceinternal->buffer));

if (FAILED(hr))
{
return false;
Expand All @@ -508,49 +502,49 @@ namespace wi::audio
if (instance != nullptr && instance->IsValid())
{
auto instanceinternal = to_internal(instance);
HRESULT hr = instanceinternal->sourceVoice->Start();
xaudio_check(hr);
xaudio_check(instanceinternal->sourceVoice->Start());

}
}
void Pause(SoundInstance* instance)
{
if (instance != nullptr && instance->IsValid())
{
auto instanceinternal = to_internal(instance);
HRESULT hr = instanceinternal->sourceVoice->Stop(); // preserves cursor position
xaudio_check(hr);
xaudio_check(instanceinternal->sourceVoice->Stop()); // preserves cursor position

}
}
void Stop(SoundInstance* instance)
{
if (instance != nullptr && instance->IsValid())
{
auto instanceinternal = to_internal(instance);
HRESULT hr = instanceinternal->sourceVoice->Stop(); // preserves cursor position
xaudio_check(hr);
hr = instanceinternal->sourceVoice->FlushSourceBuffers(); // reset submitted audio buffer
xaudio_check(hr);
xaudio_check(instanceinternal->sourceVoice->Stop()); // preserves cursor position

xaudio_check(instanceinternal->sourceVoice->FlushSourceBuffers()); // reset submitted audio buffer

if (!instanceinternal->ended) // if already ended, don't submit end again, it can cause high pitched jerky sound
{
hr = instanceinternal->sourceVoice->SubmitSourceBuffer(&audio_internal->termination_mark); // mark this as terminated, this resets XAUDIO2_VOICE_STATE::SamplesPlayed to zero
xaudio_check(hr);
xaudio_check(instanceinternal->sourceVoice->SubmitSourceBuffer(&audio_internal->termination_mark)); // mark this as terminated, this resets XAUDIO2_VOICE_STATE::SamplesPlayed to zero

}
hr = instanceinternal->sourceVoice->SubmitSourceBuffer(&instanceinternal->buffer); // resubmit
xaudio_check(hr);
xaudio_check(instanceinternal->sourceVoice->SubmitSourceBuffer(&instanceinternal->buffer)); // resubmit

}
}
void SetVolume(float volume, SoundInstance* instance)
{
if (instance == nullptr || !instance->IsValid())
{
HRESULT hr = audio_internal->masteringVoice->SetVolume(volume);
xaudio_check(hr);
xaudio_check(audio_internal->masteringVoice->SetVolume(volume));

}
else
{
auto instanceinternal = to_internal(instance);
HRESULT hr = instanceinternal->sourceVoice->SetVolume(volume);
xaudio_check(hr);
xaudio_check(instanceinternal->sourceVoice->SetVolume(volume));

}
}
float GetVolume(const SoundInstance* instance)
Expand All @@ -574,13 +568,13 @@ namespace wi::audio
auto instanceinternal = to_internal(instance);
if (instanceinternal->buffer.LoopCount == 0)
return;
HRESULT hr = instanceinternal->sourceVoice->ExitLoop();
xaudio_check(hr);
xaudio_check(instanceinternal->sourceVoice->ExitLoop());

if (instanceinternal->ended)
{
instanceinternal->buffer.LoopCount = 0;
hr = instanceinternal->sourceVoice->SubmitSourceBuffer(&instanceinternal->buffer);
xaudio_check(hr);
xaudio_check(instanceinternal->sourceVoice->SubmitSourceBuffer(&instanceinternal->buffer));

}
}
}
Expand Down Expand Up @@ -621,8 +615,8 @@ namespace wi::audio

void SetSubmixVolume(SUBMIX_TYPE type, float volume)
{
HRESULT hr = audio_internal->submixVoices[type]->SetVolume(volume);
xaudio_check(hr);
xaudio_check(audio_internal->submixVoices[type]->SetVolume(volume));

}
float GetSubmixVolume(SUBMIX_TYPE type)
{
Expand Down Expand Up @@ -674,30 +668,27 @@ namespace wi::audio

X3DAudioCalculate(instanceinternal->audio->audio3D, &listener, &emitter, flags, &settings);

HRESULT hr;

hr = instanceinternal->sourceVoice->SetFrequencyRatio(settings.DopplerFactor);
xaudio_check(hr);
xaudio_check(instanceinternal->sourceVoice->SetFrequencyRatio(settings.DopplerFactor));

hr = instanceinternal->sourceVoice->SetOutputMatrix(
xaudio_check(instanceinternal->sourceVoice->SetOutputMatrix(
instanceinternal->audio->submixVoices[instance->type],
settings.SrcChannelCount,
settings.DstChannelCount,
settings.SrcChannelCount,
settings.DstChannelCount,
settings.pMatrixCoefficients
);
xaudio_check(hr);
));


XAUDIO2_FILTER_PARAMETERS FilterParametersDirect = { LowPassFilter, 2.0f * sinf(X3DAUDIO_PI / 6.0f * settings.LPFDirectCoefficient), 1.0f };
hr = instanceinternal->sourceVoice->SetOutputFilterParameters(instanceinternal->audio->submixVoices[instance->type], &FilterParametersDirect);
xaudio_check(hr);
xaudio_check(instanceinternal->sourceVoice->SetOutputFilterParameters(instanceinternal->audio->submixVoices[instance->type], &FilterParametersDirect));


if (instance->IsEnableReverb() && instanceinternal->audio->reverbSubmix != nullptr)
{
hr = instanceinternal->sourceVoice->SetOutputMatrix(instanceinternal->audio->reverbSubmix, settings.SrcChannelCount, 1, &settings.ReverbLevel);
xaudio_check(hr);
xaudio_check(instanceinternal->sourceVoice->SetOutputMatrix(instanceinternal->audio->reverbSubmix, settings.SrcChannelCount, 1, &settings.ReverbLevel));

XAUDIO2_FILTER_PARAMETERS FilterParametersReverb = { LowPassFilter, 2.0f * sinf(X3DAUDIO_PI / 6.0f * settings.LPFReverbCoefficient), 1.0f };
hr = instanceinternal->sourceVoice->SetOutputFilterParameters(instanceinternal->audio->reverbSubmix, &FilterParametersReverb);
xaudio_check(hr);
xaudio_check(instanceinternal->sourceVoice->SetOutputFilterParameters(instanceinternal->audio->reverbSubmix, &FilterParametersReverb));

}
}
}
Expand All @@ -706,8 +697,8 @@ namespace wi::audio
{
XAUDIO2FX_REVERB_PARAMETERS native;
ReverbConvertI3DL2ToNative(&reverbPresets[preset], &native);
HRESULT hr = audio_internal->reverbSubmix->SetEffectParameters(0, &native, sizeof(native));
xaudio_check(hr);
xaudio_check(audio_internal->reverbSubmix->SetEffectParameters(0, &native, sizeof(native)));

}
}

Expand Down
10 changes: 10 additions & 0 deletions WickedEngine/wiBacklog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,16 @@ using namespace std::chrono_literals;

namespace wi::backlog
{
namespace internal {
char* extract_function_name(char* dst, const char* src)
{
int i = 0;
while (src[i] != '(') i++;
memcpy(dst, src, i);
dst[i] = 0;
return dst;
}
}
bool enabled = false;
bool was_ever_enabled = enabled;
struct LogEntry
Expand Down
5 changes: 5 additions & 0 deletions WickedEngine/wiBacklog.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@

namespace wi::backlog
{
namespace internal {
// Used by various *_check macros
char* extract_function_name(char* dst, const char* src);
}

// Do not modify the order, as this is exposed to LUA scripts as int!
enum class LogLevel
{
Expand Down
Loading

0 comments on commit cc01bd1

Please sign in to comment.