aldebug.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. /*
  2. * OpenAL Debug Context Example
  3. *
  4. * Copyright (c) 2024 by Chris Robinson <[email protected]>
  5. *
  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:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  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.
  23. */
  24. /* This file contains an example for using the debug extension. */
  25. #include <algorithm>
  26. #include <array>
  27. #include <cassert>
  28. #include <cstdio>
  29. #include <memory>
  30. #include <string>
  31. #include <string_view>
  32. #include <vector>
  33. #include "AL/al.h"
  34. #include "AL/alc.h"
  35. #include "AL/alext.h"
  36. #include "alnumeric.h"
  37. #include "alspan.h"
  38. #include "fmt/core.h"
  39. #include "win_main_utf8.h"
  40. namespace {
  41. using namespace std::string_view_literals;
  42. struct DeviceCloser {
  43. void operator()(ALCdevice *device) const noexcept { alcCloseDevice(device); }
  44. };
  45. using DevicePtr = std::unique_ptr<ALCdevice,DeviceCloser>;
  46. struct ContextDestroyer {
  47. void operator()(ALCcontext *context) const noexcept { alcDestroyContext(context); }
  48. };
  49. using ContextPtr = std::unique_ptr<ALCcontext,ContextDestroyer>;
  50. constexpr auto GetDebugSourceName(ALenum source) noexcept -> std::string_view
  51. {
  52. switch(source)
  53. {
  54. case AL_DEBUG_SOURCE_API_EXT: return "API"sv;
  55. case AL_DEBUG_SOURCE_AUDIO_SYSTEM_EXT: return "Audio System"sv;
  56. case AL_DEBUG_SOURCE_THIRD_PARTY_EXT: return "Third Party"sv;
  57. case AL_DEBUG_SOURCE_APPLICATION_EXT: return "Application"sv;
  58. case AL_DEBUG_SOURCE_OTHER_EXT: return "Other"sv;
  59. }
  60. return "<invalid source>"sv;
  61. }
  62. constexpr auto GetDebugTypeName(ALenum type) noexcept -> std::string_view
  63. {
  64. switch(type)
  65. {
  66. case AL_DEBUG_TYPE_ERROR_EXT: return "Error"sv;
  67. case AL_DEBUG_TYPE_DEPRECATED_BEHAVIOR_EXT: return "Deprecated Behavior"sv;
  68. case AL_DEBUG_TYPE_UNDEFINED_BEHAVIOR_EXT: return "Undefined Behavior"sv;
  69. case AL_DEBUG_TYPE_PORTABILITY_EXT: return "Portability"sv;
  70. case AL_DEBUG_TYPE_PERFORMANCE_EXT: return "Performance"sv;
  71. case AL_DEBUG_TYPE_MARKER_EXT: return "Marker"sv;
  72. case AL_DEBUG_TYPE_PUSH_GROUP_EXT: return "Push Group"sv;
  73. case AL_DEBUG_TYPE_POP_GROUP_EXT: return "Pop Group"sv;
  74. case AL_DEBUG_TYPE_OTHER_EXT: return "Other"sv;
  75. }
  76. return "<invalid type>"sv;
  77. }
  78. constexpr auto GetDebugSeverityName(ALenum severity) noexcept -> std::string_view
  79. {
  80. switch(severity)
  81. {
  82. case AL_DEBUG_SEVERITY_HIGH_EXT: return "High"sv;
  83. case AL_DEBUG_SEVERITY_MEDIUM_EXT: return "Medium"sv;
  84. case AL_DEBUG_SEVERITY_LOW_EXT: return "Low"sv;
  85. case AL_DEBUG_SEVERITY_NOTIFICATION_EXT: return "Notification"sv;
  86. }
  87. return "<invalid severity>"sv;
  88. }
  89. auto alDebugMessageCallbackEXT = LPALDEBUGMESSAGECALLBACKEXT{};
  90. auto alDebugMessageInsertEXT = LPALDEBUGMESSAGEINSERTEXT{};
  91. auto alDebugMessageControlEXT = LPALDEBUGMESSAGECONTROLEXT{};
  92. auto alPushDebugGroupEXT = LPALPUSHDEBUGGROUPEXT{};
  93. auto alPopDebugGroupEXT = LPALPOPDEBUGGROUPEXT{};
  94. auto alGetDebugMessageLogEXT = LPALGETDEBUGMESSAGELOGEXT{};
  95. auto alObjectLabelEXT = LPALOBJECTLABELEXT{};
  96. auto alGetObjectLabelEXT = LPALGETOBJECTLABELEXT{};
  97. auto alGetPointerEXT = LPALGETPOINTEREXT{};
  98. auto alGetPointervEXT = LPALGETPOINTERVEXT{};
  99. int main(al::span<std::string_view> args)
  100. {
  101. /* Print out usage if -h was specified */
  102. if(args.size() > 1 && (args[1] == "-h" || args[1] == "--help"))
  103. {
  104. fmt::println(stderr, "Usage: {} [-device <name>] [-nodebug]", args[0]);
  105. return 1;
  106. }
  107. /* Initialize OpenAL. */
  108. args = args.subspan(1);
  109. auto device = DevicePtr{};
  110. if(args.size() > 1 && args[0] == "-device")
  111. {
  112. device = DevicePtr{alcOpenDevice(std::string{args[1]}.c_str())};
  113. if(!device)
  114. fmt::println(stderr, "Failed to open \"{}\", trying default", args[1]);
  115. args = args.subspan(2);
  116. }
  117. if(!device)
  118. device = DevicePtr{alcOpenDevice(nullptr)};
  119. if(!device)
  120. {
  121. fmt::println(stderr, "Could not open a device!");
  122. return 1;
  123. }
  124. if(!alcIsExtensionPresent(device.get(), "ALC_EXT_debug"))
  125. {
  126. fmt::println(stderr, "ALC_EXT_debug not supported on device");
  127. return 1;
  128. }
  129. /* Load the Debug API functions we're using. */
  130. #define LOAD_PROC(N) N = reinterpret_cast<decltype(N)>(alcGetProcAddress(device.get(), #N))
  131. LOAD_PROC(alDebugMessageCallbackEXT);
  132. LOAD_PROC(alDebugMessageInsertEXT);
  133. LOAD_PROC(alDebugMessageControlEXT);
  134. LOAD_PROC(alPushDebugGroupEXT);
  135. LOAD_PROC(alPopDebugGroupEXT);
  136. LOAD_PROC(alGetDebugMessageLogEXT);
  137. LOAD_PROC(alObjectLabelEXT);
  138. LOAD_PROC(alGetObjectLabelEXT);
  139. LOAD_PROC(alGetPointerEXT);
  140. LOAD_PROC(alGetPointervEXT);
  141. #undef LOAD_PROC
  142. /* Create a debug context and set it as current. If -nodebug was specified,
  143. * create a non-debug context (to see how debug messages react).
  144. */
  145. auto flags = ALCint{ALC_CONTEXT_DEBUG_BIT_EXT};
  146. if(!args.empty() && args[0] == "-nodebug")
  147. flags &= ~ALC_CONTEXT_DEBUG_BIT_EXT;
  148. const auto attribs = std::array<ALCint,3>{{
  149. ALC_CONTEXT_FLAGS_EXT, flags,
  150. 0 /* end-of-list */
  151. }};
  152. auto context = ContextPtr{alcCreateContext(device.get(), attribs.data())};
  153. if(!context || alcMakeContextCurrent(context.get()) == ALC_FALSE)
  154. {
  155. fmt::println(stderr, "Could not create and set a context!");
  156. return 1;
  157. }
  158. /* Enable low-severity debug messages, which are disabled by default. */
  159. alDebugMessageControlEXT(AL_DONT_CARE_EXT, AL_DONT_CARE_EXT, AL_DEBUG_SEVERITY_LOW_EXT, 0,
  160. nullptr, AL_TRUE);
  161. fmt::println("Context flags: {:#010x}", as_unsigned(alGetInteger(AL_CONTEXT_FLAGS_EXT)));
  162. /* A debug context has debug output enabled by default. But in case this
  163. * isn't a debug context, explicitly enable it (probably won't get much, if
  164. * anything, in that case).
  165. */
  166. fmt::println("Default debug state is: {}",
  167. alIsEnabled(AL_DEBUG_OUTPUT_EXT) ? "enabled"sv : "disabled"sv);
  168. alEnable(AL_DEBUG_OUTPUT_EXT);
  169. /* The max debug message length property will allow us to define message
  170. * storage of sufficient length. This includes space for the null
  171. * terminator.
  172. */
  173. const auto maxloglength = alGetInteger(AL_MAX_DEBUG_MESSAGE_LENGTH_EXT);
  174. fmt::println("Max debug message length: {}", maxloglength);
  175. fmt::println("");
  176. /* Doppler Velocity is deprecated since AL 1.1, so this should generate a
  177. * deprecation debug message. We'll first handle debug messages through the
  178. * message log, meaning we'll query for and read it afterward.
  179. */
  180. fmt::println("Calling alDopplerVelocity(0.5f)...");
  181. alDopplerVelocity(0.5f);
  182. for(auto numlogs = alGetInteger(AL_DEBUG_LOGGED_MESSAGES_EXT);numlogs > 0;--numlogs)
  183. {
  184. auto message = std::vector<char>(static_cast<ALuint>(maxloglength), '\0');
  185. auto source = ALenum{};
  186. auto type = ALenum{};
  187. auto id = ALuint{};
  188. auto severity = ALenum{};
  189. auto msglength = ALsizei{};
  190. /* Getting the message removes it from the log. */
  191. const auto read = alGetDebugMessageLogEXT(1, maxloglength, &source, &type, &id, &severity,
  192. &msglength, message.data());
  193. if(read != 1)
  194. {
  195. fmt::println(stderr, "Read {} debug messages, expected to read 1", read);
  196. break;
  197. }
  198. /* The message lengths returned by alGetDebugMessageLogEXT include the
  199. * null terminator, so subtract one for the string_view length. If we
  200. * read more than one message at a time, the length could be used as
  201. * the offset to the next message.
  202. */
  203. const auto msgstr = std::string_view{message.data(),
  204. static_cast<ALuint>(msglength ? msglength-1 : 0)};
  205. fmt::println("Got message from log:\n"
  206. " Source: {}\n"
  207. " Type: {}\n"
  208. " ID: {}\n"
  209. " Severity: {}\n"
  210. " Message: \"{}\"", GetDebugSourceName(source), GetDebugTypeName(type), id,
  211. GetDebugSeverityName(severity), msgstr);
  212. }
  213. fmt::println("");
  214. /* Now set up a callback function. This lets us print the debug messages as
  215. * they happen without having to explicitly query and get them.
  216. */
  217. static constexpr auto debug_callback = [](ALenum source, ALenum type, ALuint id,
  218. ALenum severity, ALsizei length, const ALchar *message, void *userParam [[maybe_unused]])
  219. noexcept -> void
  220. {
  221. /* The message length provided to the callback does not include the
  222. * null terminator.
  223. */
  224. const auto msgstr = std::string_view{message, static_cast<ALuint>(length)};
  225. fmt::println("Got message from callback:\n"
  226. " Source: {}\n"
  227. " Type: {}\n"
  228. " ID: {}\n"
  229. " Severity: {}\n"
  230. " Message: \"{}\"", GetDebugSourceName(source), GetDebugTypeName(type), id,
  231. GetDebugSeverityName(severity), msgstr);
  232. };
  233. alDebugMessageCallbackEXT(debug_callback, nullptr);
  234. if(const auto numlogs = alGetInteger(AL_DEBUG_LOGGED_MESSAGES_EXT))
  235. fmt::println(stderr, "{} left over logged message{}!", numlogs, (numlogs==1)?"":"s");
  236. /* This should also generate a deprecation debug message, which will now go
  237. * through the callback.
  238. */
  239. fmt::println("Calling alGetInteger(AL_DOPPLER_VELOCITY)...");
  240. auto dv [[maybe_unused]] = alGetInteger(AL_DOPPLER_VELOCITY);
  241. fmt::println("");
  242. /* These functions are notoriously unreliable for their behavior, they will
  243. * likely generate portability debug messages.
  244. */
  245. fmt::println("Calling alcSuspendContext and alcProcessContext...");
  246. alcSuspendContext(context.get());
  247. alcProcessContext(context.get());
  248. fputs("\n", stdout);
  249. fmt::println("Pushing a debug group, making some invalid calls, and popping the debug group...");
  250. alPushDebugGroupEXT(AL_DEBUG_SOURCE_APPLICATION_EXT, 0, -1, "Error test group");
  251. alSpeedOfSound(0.0f);
  252. /* Can't set the label of the null buffer. */
  253. alObjectLabelEXT(AL_BUFFER, 0, -1, "The null buffer");
  254. alPopDebugGroupEXT();
  255. fmt::println("");
  256. /* All done, insert a custom message and unset the callback. The context
  257. * and device will clean themselves up.
  258. */
  259. alDebugMessageInsertEXT(AL_DEBUG_SOURCE_APPLICATION_EXT, AL_DEBUG_TYPE_MARKER_EXT, 0,
  260. AL_DEBUG_SEVERITY_NOTIFICATION_EXT, -1, "End of run, cleaning up");
  261. alDebugMessageCallbackEXT(nullptr, nullptr);
  262. return 0;
  263. }
  264. } // namespace
  265. int main(int argc, char **argv)
  266. {
  267. assert(argc >= 0);
  268. auto args = std::vector<std::string_view>(static_cast<unsigned int>(argc));
  269. std::copy_n(argv, args.size(), args.begin());
  270. return main(al::span{args});
  271. }