Skip to content

Commit 0833166

Browse files
committed
audio HRTF works well and debug switch to turn on CPU only mode vs GPU processing mode.
1 parent 4a3b450 commit 0833166

File tree

3 files changed

+101
-2
lines changed

3 files changed

+101
-2
lines changed

attachments/simple_engine/audio_system.cpp

Lines changed: 59 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -875,6 +875,15 @@ bool AudioSystem::IsHRTFEnabled() const {
875875
return hrtfEnabled;
876876
}
877877

878+
void AudioSystem::SetHRTFCPUOnly(bool cpuOnly) {
879+
hrtfCPUOnly = cpuOnly;
880+
std::cout << "HRTF processing mode set to " << (cpuOnly ? "CPU-only" : "Vulkan shader (when available)") << std::endl;
881+
}
882+
883+
bool AudioSystem::IsHRTFCPUOnly() const {
884+
return hrtfCPUOnly;
885+
}
886+
878887
bool AudioSystem::LoadHRTFData(const std::string& filename) {
879888
if (!filename.empty()) {
880889
std::cout << "Loading HRTF data from: " << filename << std::endl;
@@ -1001,15 +1010,63 @@ bool AudioSystem::LoadHRTFData(const std::string& filename) {
10011010
}
10021011

10031012
bool AudioSystem::ProcessHRTF(const float* inputBuffer, float* outputBuffer, uint32_t sampleCount, const float* sourcePosition) {
1004-
if (!hrtfEnabled || !renderer || !renderer->IsInitialized()) {
1005-
// If HRTF is disabled or renderer is not available, just copy input to output
1013+
if (!hrtfEnabled) {
1014+
// If HRTF is disabled, just copy input to output
10061015
for (uint32_t i = 0; i < sampleCount; i++) {
10071016
outputBuffer[i * 2] = inputBuffer[i]; // Left channel
10081017
outputBuffer[i * 2 + 1] = inputBuffer[i]; // Right channel
10091018
}
10101019
return true;
10111020
}
10121021

1022+
// Check if we should use CPU-only processing or if Vulkan is not available
1023+
if (hrtfCPUOnly || !renderer || !renderer->IsInitialized()) {
1024+
// Use CPU-based HRTF processing (either forced or fallback)
1025+
// Skip Vulkan buffer creation and go directly to CPU processing
1026+
} else {
1027+
// Use Vulkan shader-based HRTF processing
1028+
// Create buffers for HRTF processing if they don't exist or if the sample count has changed
1029+
if (!createHRTFBuffers(sampleCount)) {
1030+
std::cerr << "Failed to create HRTF buffers" << std::endl;
1031+
return false;
1032+
}
1033+
1034+
// Copy input data to input buffer
1035+
void* data = inputBufferMemory.mapMemory(0, sampleCount * sizeof(float));
1036+
memcpy(data, inputBuffer, sampleCount * sizeof(float));
1037+
inputBufferMemory.unmapMemory();
1038+
1039+
// Set up HRTF parameters
1040+
struct HRTFParams {
1041+
float sourcePosition[3];
1042+
float listenerPosition[3];
1043+
float listenerOrientation[6]; // Forward (3) and up (3) vectors
1044+
uint32_t sampleCount;
1045+
uint32_t hrtfSize;
1046+
uint32_t numHrtfPositions;
1047+
float padding; // For alignment
1048+
} params;
1049+
1050+
// Copy source and listener positions
1051+
memcpy(params.sourcePosition, sourcePosition, sizeof(float) * 3);
1052+
memcpy(params.listenerPosition, listenerPosition, sizeof(float) * 3);
1053+
memcpy(params.listenerOrientation, listenerOrientation, sizeof(float) * 6);
1054+
params.sampleCount = sampleCount;
1055+
params.hrtfSize = hrtfSize;
1056+
params.numHrtfPositions = numHrtfPositions;
1057+
params.padding = 0.0f;
1058+
1059+
// Copy parameters to parameter buffer
1060+
data = paramsBufferMemory.mapMemory(0, sizeof(HRTFParams));
1061+
memcpy(data, &params, sizeof(HRTFParams));
1062+
paramsBufferMemory.unmapMemory();
1063+
1064+
// TODO: Add actual Vulkan compute shader dispatch here
1065+
// For now, fall through to CPU processing
1066+
}
1067+
1068+
// CPU-based HRTF processing (used for both CPU-only mode and fallback)
1069+
10131070
// Create buffers for HRTF processing if they don't exist or if the sample count has changed
10141071
if (!createHRTFBuffers(sampleCount)) {
10151072
std::cerr << "Failed to create HRTF buffers" << std::endl;

attachments/simple_engine/audio_system.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,18 @@ class AudioSystem {
232232
*/
233233
bool IsHRTFEnabled() const;
234234

235+
/**
236+
* @brief Set whether to force CPU-only HRTF processing.
237+
* @param cpuOnly Whether to force CPU-only processing (true) or allow Vulkan shader processing (false).
238+
*/
239+
void SetHRTFCPUOnly(bool cpuOnly);
240+
241+
/**
242+
* @brief Check if HRTF processing is set to CPU-only mode.
243+
* @return True if CPU-only mode is enabled, false if Vulkan shader processing is allowed.
244+
*/
245+
bool IsHRTFCPUOnly() const;
246+
235247
/**
236248
* @brief Load HRTF data from a file.
237249
* @param filename The path to the HRTF data file.
@@ -277,6 +289,7 @@ class AudioSystem {
277289

278290
// HRTF processing
279291
bool hrtfEnabled = false;
292+
bool hrtfCPUOnly = false;
280293
std::vector<float> hrtfData;
281294
uint32_t hrtfSize = 0;
282295
uint32_t numHrtfPositions = 0;

attachments/simple_engine/imgui_system.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,35 @@ void ImGuiSystem::NewFrame() {
210210
ImGui::Text("HRTF Processing: ENABLED");
211211
ImGui::Text("Use directional buttons to move the audio source in 3D space");
212212
ImGui::Text("You should hear the audio move around you!");
213+
214+
// HRTF Processing Mode Selection
215+
ImGui::Separator();
216+
ImGui::Text("HRTF Processing Mode:");
217+
218+
static bool cpuOnlyMode = false;
219+
static bool initialized = false;
220+
221+
// Initialize checkbox state to match audio system state
222+
if (!initialized && audioSystem) {
223+
cpuOnlyMode = audioSystem->IsHRTFCPUOnly();
224+
initialized = true;
225+
}
226+
227+
if (ImGui::Checkbox("Force CPU-only HRTF processing", &cpuOnlyMode)) {
228+
if (audioSystem) {
229+
audioSystem->SetHRTFCPUOnly(cpuOnlyMode);
230+
std::cout << "HRTF processing mode changed to: " << (cpuOnlyMode ? "CPU-only" : "Vulkan shader (when available)") << std::endl;
231+
}
232+
}
233+
234+
// Display current processing mode
235+
if (audioSystem) {
236+
if (audioSystem->IsHRTFCPUOnly()) {
237+
ImGui::Text("Current Mode: CPU-only processing");
238+
} else {
239+
ImGui::Text("Current Mode: Vulkan shader processing (when available)");
240+
}
241+
}
213242
} else {
214243
ImGui::Text("HRTF Processing: DISABLED");
215244
}

0 commit comments

Comments
 (0)