2 * OpenAL Debug Context Example
4 * Copyright (c) 2024 by Chris Robinson <chris.kcat@gmail.com>
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 /* This file contains an example for using the debug extension. */
34 #include <string_view>
43 #include "win_main_utf8.h"
48 void operator()(ALCdevice
*device
) const noexcept
{ alcCloseDevice(device
); }
50 using DevicePtr
= std::unique_ptr
<ALCdevice
,DeviceCloser
>;
52 struct ContextDestroyer
{
53 void operator()(ALCcontext
*context
) const noexcept
{ alcDestroyContext(context
); }
55 using ContextPtr
= std::unique_ptr
<ALCcontext
,ContextDestroyer
>;
58 constexpr auto GetDebugSourceName(ALenum source
) noexcept
-> const char*
62 case AL_DEBUG_SOURCE_API_EXT
: return "API";
63 case AL_DEBUG_SOURCE_AUDIO_SYSTEM_EXT
: return "Audio System";
64 case AL_DEBUG_SOURCE_THIRD_PARTY_EXT
: return "Third Party";
65 case AL_DEBUG_SOURCE_APPLICATION_EXT
: return "Application";
66 case AL_DEBUG_SOURCE_OTHER_EXT
: return "Other";
68 return "<invalid source>";
71 constexpr auto GetDebugTypeName(ALenum type
) noexcept
-> const char*
75 case AL_DEBUG_TYPE_ERROR_EXT
: return "Error";
76 case AL_DEBUG_TYPE_DEPRECATED_BEHAVIOR_EXT
: return "Deprecated Behavior";
77 case AL_DEBUG_TYPE_UNDEFINED_BEHAVIOR_EXT
: return "Undefined Behavior";
78 case AL_DEBUG_TYPE_PORTABILITY_EXT
: return "Portability";
79 case AL_DEBUG_TYPE_PERFORMANCE_EXT
: return "Performance";
80 case AL_DEBUG_TYPE_MARKER_EXT
: return "Marker";
81 case AL_DEBUG_TYPE_PUSH_GROUP_EXT
: return "Push Group";
82 case AL_DEBUG_TYPE_POP_GROUP_EXT
: return "Pop Group";
83 case AL_DEBUG_TYPE_OTHER_EXT
: return "Other";
85 return "<invalid type>";
88 constexpr auto GetDebugSeverityName(ALenum severity
) noexcept
-> const char*
92 case AL_DEBUG_SEVERITY_HIGH_EXT
: return "High";
93 case AL_DEBUG_SEVERITY_MEDIUM_EXT
: return "Medium";
94 case AL_DEBUG_SEVERITY_LOW_EXT
: return "Low";
95 case AL_DEBUG_SEVERITY_NOTIFICATION_EXT
: return "Notification";
97 return "<invalid severity>";
100 auto alDebugMessageCallbackEXT
= LPALDEBUGMESSAGECALLBACKEXT
{};
101 auto alDebugMessageInsertEXT
= LPALDEBUGMESSAGEINSERTEXT
{};
102 auto alDebugMessageControlEXT
= LPALDEBUGMESSAGECONTROLEXT
{};
103 auto alPushDebugGroupEXT
= LPALPUSHDEBUGGROUPEXT
{};
104 auto alPopDebugGroupEXT
= LPALPOPDEBUGGROUPEXT
{};
105 auto alGetDebugMessageLogEXT
= LPALGETDEBUGMESSAGELOGEXT
{};
106 auto alObjectLabelEXT
= LPALOBJECTLABELEXT
{};
107 auto alGetObjectLabelEXT
= LPALGETOBJECTLABELEXT
{};
108 auto alGetPointerEXT
= LPALGETPOINTEREXT
{};
109 auto alGetPointervEXT
= LPALGETPOINTERVEXT
{};
112 int main(al::span
<std::string_view
> args
)
114 /* Print out usage if -h was specified */
115 if(args
.size() > 1 && (args
[1] == "-h" || args
[1] == "--help"))
117 std::cerr
<< "Usage: "<<args
[0]<<" [-device <name>]\n";
121 /* Initialize OpenAL. */
122 args
= args
.subspan(1);
124 auto device
= DevicePtr
{};
125 if(args
.size() > 1 && args
[0] == "-device")
127 device
= DevicePtr
{alcOpenDevice(std::string
{args
[1]}.c_str())};
129 std::cerr
<< "Failed to open \""<<args
[1]<<"\", trying default\n";
130 args
= args
.subspan(2);
133 device
= DevicePtr
{alcOpenDevice(nullptr)};
136 std::cerr
<< "Could not open a device!\n";
140 if(!alcIsExtensionPresent(device
.get(), "ALC_EXT_debug"))
142 std::cerr
<< "ALC_EXT_debug not supported on device\n";
146 /* Load the Debug API functions we're using. */
147 #define LOAD_PROC(N) N = reinterpret_cast<decltype(N)>(alcGetProcAddress(device.get(), #N))
148 LOAD_PROC(alDebugMessageCallbackEXT
);
149 LOAD_PROC(alDebugMessageInsertEXT
);
150 LOAD_PROC(alDebugMessageControlEXT
);
151 LOAD_PROC(alPushDebugGroupEXT
);
152 LOAD_PROC(alPopDebugGroupEXT
);
153 LOAD_PROC(alGetDebugMessageLogEXT
);
154 LOAD_PROC(alObjectLabelEXT
);
155 LOAD_PROC(alGetObjectLabelEXT
);
156 LOAD_PROC(alGetPointerEXT
);
157 LOAD_PROC(alGetPointervEXT
);
160 auto flags
= ALCint
{ALC_CONTEXT_DEBUG_BIT_EXT
};
161 if(!args
.empty() && args
[0] == "-nodebug")
162 flags
&= ~ALC_CONTEXT_DEBUG_BIT_EXT
;
164 /* Create a debug context and set it as current. */
165 const auto attribs
= std::array
<ALCint
,3>{{
166 ALC_CONTEXT_FLAGS_EXT
, flags
,
169 auto context
= ContextPtr
{alcCreateContext(device
.get(), attribs
.data())};
170 if(!context
|| alcMakeContextCurrent(context
.get()) == ALC_FALSE
)
172 std::cerr
<< "Could not create and set a context!\n";
176 printf("Context flags: 0x%08x\n", alGetInteger(AL_CONTEXT_FLAGS_EXT
));
178 /* A debug context has debug output enabled by default. But in case this
179 * isn't a debug context, explicitly enable it (probably won't get much, if
180 * anything, in that case).
182 printf("Default debug state is: %s\n",
183 alIsEnabled(AL_DEBUG_OUTPUT_EXT
) ? "enabled" : "disabled");
184 alEnable(AL_DEBUG_OUTPUT_EXT
);
186 /* The max debug message length property will allow us to define message
187 * storage of sufficient length.
189 const auto maxloglength
= alGetInteger(AL_MAX_DEBUG_MESSAGE_LENGTH_EXT
);
190 printf("Max debug message length: %d\n", maxloglength
);
194 /* Doppler Velocity is deprecated since AL 1.1, so this should generate a
195 * deprecation debug message.
197 printf("Calling alDopplerVelocity(0.5f)...\n");
198 alDopplerVelocity(0.5f
);
200 auto numlogs
= alGetInteger(AL_DEBUG_LOGGED_MESSAGES_EXT
);
203 auto message
= std::vector
<char>(static_cast<unsigned int>(maxloglength
), '\0');
204 auto source
= ALenum
{};
205 auto type
= ALenum
{};
207 auto severity
= ALenum
{};
208 auto msglength
= ALsizei
{};
210 const auto read
= alGetDebugMessageLogEXT(1, maxloglength
, &source
, &type
, &id
, &severity
,
211 &msglength
, message
.data());
214 fprintf(stderr
, "Read %d debug messages, expected to read 1\n", read
);
218 printf("Got message log:\n"
223 " Message: \"%s\"\n", GetDebugSourceName(source
), GetDebugTypeName(type
), id
,
224 GetDebugSeverityName(severity
), message
.data());
230 /* Now set up a callback. */
231 auto debug_callback
= [](ALenum source
, ALenum type
, ALuint id
, ALenum severity
,
232 ALsizei length
[[maybe_unused
]], const ALchar
*message
, void *userParam
[[maybe_unused
]])
235 printf("Got message callback:\n"
240 " Message: \"%s\"\n", GetDebugSourceName(source
), GetDebugTypeName(type
), id
,
241 GetDebugSeverityName(severity
), message
);
243 alDebugMessageCallbackEXT(debug_callback
, nullptr);
245 numlogs
= alGetInteger(AL_DEBUG_LOGGED_MESSAGES_EXT
);
247 fprintf(stderr
, "%d left over logged message%s!\n", numlogs
, (numlogs
==1)?"":"s");
249 /* This should also generate a deprecation debug message, which will now go
250 * through the callback.
252 printf("Calling alGetInteger(AL_DOPPLER_VELOCITY)...\n");
253 auto dv
[[maybe_unused
]] = alGetInteger(AL_DOPPLER_VELOCITY
);
256 /* These functions are notoriously unreliable for their behavior, they will
257 * likely generate debug messages.
259 printf("Calling alcSuspendContext and alcProcessContext...\n");
260 alcSuspendContext(context
.get());
261 alcProcessContext(context
.get());
264 /* Enable notification and low-severity debug messages, which are disabled
267 alDebugMessageControlEXT(AL_DONT_CARE_EXT
, AL_DONT_CARE_EXT
,
268 AL_DEBUG_SEVERITY_NOTIFICATION_EXT
, 0, nullptr, AL_TRUE
);
269 alDebugMessageControlEXT(AL_DONT_CARE_EXT
, AL_DONT_CARE_EXT
, AL_DEBUG_SEVERITY_LOW_EXT
, 0,
272 printf("Pushing a debug group, making some invalid calls, and popping the debug group...\n");
273 alPushDebugGroupEXT(AL_DEBUG_SOURCE_APPLICATION_EXT
, 0, -1, "Error test group");
274 alSpeedOfSound(0.0f
);
275 /* Can't set the label of the null buffer. */
276 alObjectLabelEXT(AL_BUFFER
, 0, -1, "The null buffer");
277 alPopDebugGroupEXT();
280 /* All done, unset the callback, the context and device will clean
283 alDebugMessageCallbackEXT(nullptr, nullptr);
290 int main(int argc
, char **argv
)
293 auto args
= std::vector
<std::string_view
>(static_cast<unsigned int>(argc
));
294 std::copy_n(argv
, args
.size(), args
.begin());
295 return main(al::span
{args
});