Add an example using the debug extensions
[openal-soft.git] / examples / aldebug.cpp
blobf93d88bdf889fb4fdd5df322395e39253e90e871
1 /*
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
22 * THE SOFTWARE.
25 /* This file contains an example for using the debug extension. */
27 #include <algorithm>
28 #include <array>
29 #include <cassert>
30 #include <cstdio>
31 #include <iostream>
32 #include <memory>
33 #include <string>
34 #include <string_view>
35 #include <vector>
37 #include "AL/al.h"
38 #include "AL/alc.h"
39 #include "AL/alext.h"
41 #include "alspan.h"
43 #include "win_main_utf8.h"
45 namespace {
47 struct DeviceCloser {
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*
60 switch(source)
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*
73 switch(type)
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*
90 switch(severity)
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";
118 return 1;
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())};
128 if(!device)
129 std::cerr<< "Failed to open \""<<args[1]<<"\", trying default\n";
130 args = args.subspan(2);
132 if(!device)
133 device = DevicePtr{alcOpenDevice(nullptr)};
134 if(!device)
136 std::cerr<< "Could not open a device!\n";
137 return 1;
140 if(!alcIsExtensionPresent(device.get(), "ALC_EXT_debug"))
142 std::cerr<< "ALC_EXT_debug not supported on device\n";
143 return 1;
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);
158 #undef LOAD_PROC
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,
167 0 /* end-of-list */
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";
173 return 1;
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);
192 fputs("\n", stdout);
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);
201 while(numlogs > 0)
203 auto message = std::vector<char>(static_cast<unsigned int>(maxloglength), '\0');
204 auto source = ALenum{};
205 auto type = ALenum{};
206 auto id = ALuint{};
207 auto severity = ALenum{};
208 auto msglength = ALsizei{};
210 const auto read = alGetDebugMessageLogEXT(1, maxloglength, &source, &type, &id, &severity,
211 &msglength, message.data());
212 if(read != 1)
214 fprintf(stderr, "Read %d debug messages, expected to read 1\n", read);
215 break;
218 printf("Got message log:\n"
219 " Source: %s\n"
220 " Type: %s\n"
221 " ID: %u\n"
222 " Severity: %s\n"
223 " Message: \"%s\"\n", GetDebugSourceName(source), GetDebugTypeName(type), id,
224 GetDebugSeverityName(severity), message.data());
226 --numlogs;
228 fputs("\n", stdout);
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]])
233 noexcept -> void
235 printf("Got message callback:\n"
236 " Source: %s\n"
237 " Type: %s\n"
238 " ID: %u\n"
239 " Severity: %s\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);
246 if(numlogs != 0)
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);
254 fputs("\n", stdout);
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());
262 fputs("\n", stdout);
264 /* Enable notification and low-severity debug messages, which are disabled
265 * by default.
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,
270 nullptr, AL_TRUE);
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();
278 fputs("\n", stdout);
280 /* All done, unset the callback, the context and device will clean
281 * themselves up.
283 alDebugMessageCallbackEXT(nullptr, nullptr);
285 return 0;
288 } // namespace
290 int main(int argc, char **argv)
292 assert(argc >= 0);
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});