| 1 | // This file is part of the FidelityFX SDK. |
| 2 | // |
| 3 | // Copyright (c) 2022-2023 Advanced Micro Devices, Inc. All rights reserved. |
| 4 | // |
| 5 | // Permission is hereby granted, free of charge, to any person obtaining a copy |
| 6 | // of this software and associated documentation files (the "Software"), to deal |
| 7 | // in the Software without restriction, including without limitation the rights |
| 8 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 9 | // copies of the Software, and to permit persons to whom the Software is |
| 10 | // furnished to do so, subject to the following conditions: |
| 11 | // The above copyright notice and this permission notice shall be included in |
| 12 | // all copies or substantial portions of the Software. |
| 13 | // |
| 14 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 15 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 16 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 17 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 18 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 19 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 20 | // THE SOFTWARE. |
| 21 | |
| 22 | #include "ffx_assert.h" |
| 23 | #include <stdlib.h> // for malloc() |
| 24 | |
| 25 | #ifdef _WIN32 |
| 26 | #ifndef WIN32_LEAN_AND_MEAN |
| 27 | #define WIN32_LEAN_AND_MEAN |
| 28 | #endif |
| 29 | #include <windows.h> // required for OutputDebugString() |
| 30 | #include <stdio.h> // required for sprintf_s |
| 31 | #endif // #ifndef _WIN32 |
| 32 | |
| 33 | static FfxAssertCallback s_assertCallback; |
| 34 | |
| 35 | // set the printing callback function |
| 36 | void ffxAssertSetPrintingCallback(FfxAssertCallback callback) |
| 37 | { |
| 38 | s_assertCallback = callback; |
| 39 | return; |
| 40 | } |
| 41 | |
| 42 | // implementation of assert reporting |
| 43 | bool ffxAssertReport(const char* file, int32_t line, const char* condition, const char* message) |
| 44 | { |
| 45 | if (!file) { |
| 46 | |
| 47 | return true; |
| 48 | } |
| 49 | |
| 50 | #ifdef _WIN32 |
| 51 | // form the final assertion string and output to the TTY. |
| 52 | const size_t bufferSize = static_cast<size_t>(snprintf(nullptr, 0, "%s(%d): ASSERTION FAILED. %s\n", file, line, message ? message : condition)) + 1; |
| 53 | char* tempBuf = static_cast<char*>(malloc(bufferSize)); |
| 54 | if (!tempBuf) { |
| 55 | |
| 56 | return true; |
| 57 | } |
| 58 | |
| 59 | if (!message) { |
| 60 | sprintf_s(tempBuf, bufferSize, "%s(%d): ASSERTION FAILED. %s\n", file, line, condition); |
| 61 | } else { |
| 62 | sprintf_s(tempBuf, bufferSize, "%s(%d): ASSERTION FAILED. %s\n", file, line, message); |
| 63 | } |
| 64 | |
| 65 | if (!s_assertCallback) { |
| 66 | OutputDebugStringA(tempBuf); |
| 67 | } else { |
| 68 | s_assertCallback(tempBuf); |
| 69 | } |
| 70 | |
| 71 | // free the buffer. |
| 72 | free(tempBuf); |
| 73 | |
| 74 | #else |
| 75 | FFX_UNUSED(line); |
| 76 | FFX_UNUSED(condition); |
| 77 | FFX_UNUSED(message); |
| 78 | #endif |
| 79 | |
| 80 | return true; |
| 81 | } |