// Copyright (c) 2016- PPSSPP Project.12// This program is free software: you can redistribute it and/or modify3// it under the terms of the GNU General Public License as published by4// the Free Software Foundation, version 2.0 or later versions.56// This program is distributed in the hope that it will be useful,7// but WITHOUT ANY WARRANTY; without even the implied warranty of8// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the9// GNU General Public License 2.0 for more details.1011// A copy of the GPL 2.0 should have been included with the program.12// If not, see http://www.gnu.org/licenses/1314// Official git repository and contact information can be found at15// https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/.1617#include <string>18#include <sstream>19#include <map>20#include <mutex>2122#include "Common/Log.h"23#include "Common/System/System.h"24#include "Common/GPU/Vulkan/VulkanContext.h"25#include "Common/GPU/Vulkan/VulkanDebug.h"2627const int MAX_SAME_ERROR_COUNT = 10;2829// Used to stop outputting the same message over and over.30static std::map<int, int> g_errorCount;31std::mutex g_errorCountMutex;3233// TODO: Call this when launching games in some clean way.34void VulkanClearValidationErrorCounts() {35std::lock_guard<std::mutex> lock(g_errorCountMutex);36g_errorCount.clear();37}3839VKAPI_ATTR VkBool32 VKAPI_CALL VulkanDebugUtilsCallback(40VkDebugUtilsMessageSeverityFlagBitsEXT messageSeverity,41VkDebugUtilsMessageTypeFlagsEXT messageType,42const VkDebugUtilsMessengerCallbackDataEXT *pCallbackData,43void *pUserData) {44std::ostringstream message;4546const char *pMessage = pCallbackData->pMessage;4748int messageCode = pCallbackData->messageIdNumber;49switch (messageCode) {50case 101294395:51// UNASSIGNED-CoreValidation-Shader-OutputNotConsumed - benign perf warning52return false;53case 1303270965:54// Benign perf warning, image blit using GENERAL layout.55// TODO: Oops, turns out we filtered out a bit too much here!56// We really need that performance flag check to sort out the stuff that matters.57// Will enable it soon, but it'll take some fixing.58//59if (messageType & VK_DEBUG_UTILS_MESSAGE_TYPE_PERFORMANCE_BIT_EXT)60return false;61break;6263case -375211665:64// VUID-vkAllocateMemory-pAllocateInfo-0171365// Can happen when VMA aggressively tries to allocate aperture memory for upload. It gracefully66// falls back to regular video memory, so we just ignore this. I'd argue this is a VMA bug, actually.67return false;68case 657182421:69// Extended validation (ARM best practices)70// Non-fifo validation not recommended71return false;72case 672904502:73// ARM best practices: index buffer usage warning (too few indices used compared to value range).74// This should be looked into more - happens even in moppi-flower which is weird.75return false;76case 227275665:77// PowerVR (and all other) best practices: LOAD_OP_LOAD used in render pass.78return false;79case 1835555994: // [AMD] [NVIDIA] Performance warning : Pipeline VkPipeline 0xa808d50000000033[global_texcolor] was bound twice in the frame.80// Benign perf warnings.81return false;8283case 1243445977:84// Clear value but no LOAD_OP_CLEAR. Not worth fixing right now.85return false;8687case 1544472022:88// MSAA depth resolve write-after-write??89return false;9091case -1306653903:92// ARM complaint about non-fifo swapchain93return false;9495default:96break;97}9899/*100// Can be used to temporarily turn errors into info for easier debugging.101switch (messageCode) {102case 1544472022:103if (messageSeverity & VK_DEBUG_UTILS_MESSAGE_SEVERITY_ERROR_BIT_EXT) {104messageSeverity = (VkDebugUtilsMessageSeverityFlagBitsEXT)((messageSeverity & ~VK_DEBUG_UTILS_MESSAGE_SEVERITY_ERROR_BIT_EXT) | VK_DEBUG_UTILS_MESSAGE_SEVERITY_INFO_BIT_EXT);105}106break;107default:108break;109}110*/111112int count;113{114std::lock_guard<std::mutex> lock(g_errorCountMutex);115count = g_errorCount[messageCode]++;116}117if (count == MAX_SAME_ERROR_COUNT) {118WARN_LOG(Log::G3D, "Too many validation messages with message %d, stopping", messageCode);119}120if (count >= MAX_SAME_ERROR_COUNT) {121return false;122}123124if (messageSeverity & VK_DEBUG_UTILS_MESSAGE_SEVERITY_ERROR_BIT_EXT) {125message << "ERROR(";126} else if (messageSeverity & VK_DEBUG_UTILS_MESSAGE_SEVERITY_WARNING_BIT_EXT) {127message << "WARNING(";128} else if (messageSeverity & VK_DEBUG_UTILS_MESSAGE_SEVERITY_INFO_BIT_EXT) {129message << "INFO(";130} else if (messageSeverity & VK_DEBUG_UTILS_MESSAGE_SEVERITY_VERBOSE_BIT_EXT) {131message << "VERBOSE(";132}133134if (messageType & VK_DEBUG_UTILS_MESSAGE_TYPE_PERFORMANCE_BIT_EXT) {135message << "perf";136} else if (messageType & VK_DEBUG_UTILS_MESSAGE_TYPE_GENERAL_BIT_EXT) {137message << "general";138} else if (messageType & VK_DEBUG_UTILS_MESSAGE_TYPE_VALIDATION_BIT_EXT) {139message << "validation";140}141message << ":" << messageCode << ") " << pMessage << "\n";142143std::string msg = message.str();144145#ifdef _WIN32146const VulkanLogOptions *options = (const VulkanLogOptions *)pUserData;147OutputDebugStringA(msg.c_str());148if (messageSeverity & VK_DEBUG_UTILS_MESSAGE_SEVERITY_ERROR_BIT_EXT) {149if (options->breakOnError && System_GetPropertyBool(SYSPROP_DEBUGGER_PRESENT)) {150DebugBreak();151}152if (options->msgBoxOnError) {153MessageBoxA(NULL, pMessage, "Alert", MB_OK);154}155} else if (messageSeverity & VK_DEBUG_UTILS_MESSAGE_SEVERITY_WARNING_BIT_EXT) {156// Don't break on perf warnings for now, even with a debugger. We log them at least.157if (options->breakOnWarning && System_GetPropertyBool(SYSPROP_DEBUGGER_PRESENT) && 0 == (messageType & VK_DEBUG_UTILS_MESSAGE_TYPE_PERFORMANCE_BIT_EXT)) {158DebugBreak();159}160}161#endif162163if (messageSeverity & VK_DEBUG_UTILS_MESSAGE_SEVERITY_ERROR_BIT_EXT) {164ERROR_LOG(Log::G3D, "VKDEBUG: %s", msg.c_str());165} else {166WARN_LOG(Log::G3D, "VKDEBUG: %s", msg.c_str());167}168169// false indicates that layer should not bail-out of an170// API call that had validation failures. This may mean that the171// app dies inside the driver due to invalid parameter(s).172// That's what would happen without validation layers, so we'll173// keep that behavior here.174return false;175}176177178