| 1 | /**************************************************************************/ |
| 2 | /* rendering_context_driver.cpp */ |
| 3 | /**************************************************************************/ |
| 4 | /* This file is part of: */ |
| 5 | /* GODOT ENGINE */ |
| 6 | /* https://godotengine.org */ |
| 7 | /**************************************************************************/ |
| 8 | /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ |
| 9 | /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */ |
| 10 | /* */ |
| 11 | /* Permission is hereby granted, free of charge, to any person obtaining */ |
| 12 | /* a copy of this software and associated documentation files (the */ |
| 13 | /* "Software"), to deal in the Software without restriction, including */ |
| 14 | /* without limitation the rights to use, copy, modify, merge, publish, */ |
| 15 | /* distribute, sublicense, and/or sell copies of the Software, and to */ |
| 16 | /* permit persons to whom the Software is furnished to do so, subject to */ |
| 17 | /* the following conditions: */ |
| 18 | /* */ |
| 19 | /* The above copyright notice and this permission notice shall be */ |
| 20 | /* included in all copies or substantial portions of the Software. */ |
| 21 | /* */ |
| 22 | /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ |
| 23 | /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ |
| 24 | /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */ |
| 25 | /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ |
| 26 | /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ |
| 27 | /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ |
| 28 | /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ |
| 29 | /**************************************************************************/ |
| 30 | |
| 31 | #include "rendering_context_driver.h" |
| 32 | |
| 33 | RenderingContextDriver::~RenderingContextDriver() { |
| 34 | } |
| 35 | |
| 36 | RenderingContextDriver::SurfaceID RenderingContextDriver::surface_get_from_window(DisplayServer::WindowID p_window) const { |
| 37 | HashMap<DisplayServer::WindowID, SurfaceID>::ConstIterator it = window_surface_map.find(p_window); |
| 38 | if (it != window_surface_map.end()) { |
| 39 | return it->value; |
| 40 | } else { |
| 41 | return SurfaceID(); |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | Error RenderingContextDriver::window_create(DisplayServer::WindowID p_window, const void *p_platform_data) { |
| 46 | SurfaceID surface = surface_create(p_platform_data); |
| 47 | if (surface != 0) { |
| 48 | window_surface_map[p_window] = surface; |
| 49 | return OK; |
| 50 | } else { |
| 51 | return ERR_CANT_CREATE; |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | void RenderingContextDriver::window_set_size(DisplayServer::WindowID p_window, uint32_t p_width, uint32_t p_height) { |
| 56 | SurfaceID surface = surface_get_from_window(p_window); |
| 57 | if (surface) { |
| 58 | surface_set_size(surface, p_width, p_height); |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | void RenderingContextDriver::window_set_vsync_mode(DisplayServer::WindowID p_window, DisplayServer::VSyncMode p_vsync_mode) { |
| 63 | SurfaceID surface = surface_get_from_window(p_window); |
| 64 | if (surface) { |
| 65 | surface_set_vsync_mode(surface, p_vsync_mode); |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | DisplayServer::VSyncMode RenderingContextDriver::window_get_vsync_mode(DisplayServer::WindowID p_window) const { |
| 70 | SurfaceID surface = surface_get_from_window(p_window); |
| 71 | if (surface) { |
| 72 | return surface_get_vsync_mode(surface); |
| 73 | } else { |
| 74 | return DisplayServer::VSYNC_DISABLED; |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | void RenderingContextDriver::window_destroy(DisplayServer::WindowID p_window) { |
| 79 | SurfaceID surface = surface_get_from_window(p_window); |
| 80 | if (surface) { |
| 81 | surface_destroy(surface); |
| 82 | } |
| 83 | |
| 84 | window_surface_map.erase(p_window); |
| 85 | } |
| 86 | |
| 87 | String RenderingContextDriver::get_driver_and_device_memory_report() const { |
| 88 | String report; |
| 89 | |
| 90 | const uint32_t num_tracked_obj_types = static_cast<uint32_t>(get_tracked_object_type_count()); |
| 91 | |
| 92 | report += "=== Driver Memory Report ==="; |
| 93 | |
| 94 | report += "\nLaunch with --extra-gpu-memory-tracking and build with " |
| 95 | "DEBUG_ENABLED for this functionality to work."; |
| 96 | report += "\nDevice memory may be unavailable if the API does not support it" |
| 97 | "(e.g. VK_EXT_device_memory_report is unsupported)."; |
| 98 | report += "\n"; |
| 99 | |
| 100 | report += "\nTotal Driver Memory:"; |
| 101 | report += String::num_real(double(get_driver_total_memory()) / (1024.0 * 1024.0)); |
| 102 | report += " MB"; |
| 103 | report += "\nTotal Driver Num Allocations: "; |
| 104 | report += String::num_uint64(get_driver_allocation_count()); |
| 105 | |
| 106 | report += "\nTotal Device Memory:"; |
| 107 | report += String::num_real(double(get_device_total_memory()) / (1024.0 * 1024.0)); |
| 108 | report += " MB"; |
| 109 | report += "\nTotal Device Num Allocations: "; |
| 110 | report += String::num_uint64(get_device_allocation_count()); |
| 111 | |
| 112 | report += "\n\nMemory use by object type (CSV format):"; |
| 113 | report += "\n\nCategory; Driver memory in MB; Driver Allocation Count; " |
| 114 | "Device memory in MB; Device Allocation Count"; |
| 115 | |
| 116 | for (uint32_t i = 0u; i < num_tracked_obj_types; ++i) { |
| 117 | report += "\n"; |
| 118 | report += get_tracked_object_name(i); |
| 119 | report += ";"; |
| 120 | report += String::num_real(double(get_driver_memory_by_object_type(i)) / (1024.0 * 1024.0)); |
| 121 | report += ";"; |
| 122 | report += String::num_uint64(get_driver_allocs_by_object_type(i)); |
| 123 | report += ";"; |
| 124 | report += String::num_real(double(get_device_memory_by_object_type(i)) / (1024.0 * 1024.0)); |
| 125 | report += ";"; |
| 126 | report += String::num_uint64(get_device_allocs_by_object_type(i)); |
| 127 | } |
| 128 | |
| 129 | return report; |
| 130 | } |
| 131 | |
| 132 | const char *RenderingContextDriver::get_tracked_object_name(uint32_t p_type_index) const { |
| 133 | return "Tracking Unsupported by API"; |
| 134 | } |
| 135 | |
| 136 | uint64_t RenderingContextDriver::get_tracked_object_type_count() const { |
| 137 | return 0; |
| 138 | } |
| 139 | |
| 140 | uint64_t RenderingContextDriver::get_driver_total_memory() const { |
| 141 | return 0; |
| 142 | } |
| 143 | |
| 144 | uint64_t RenderingContextDriver::get_driver_allocation_count() const { |
| 145 | return 0; |
| 146 | } |
| 147 | |
| 148 | uint64_t RenderingContextDriver::get_driver_memory_by_object_type(uint32_t) const { |
| 149 | return 0; |
| 150 | } |
| 151 | |
| 152 | uint64_t RenderingContextDriver::get_driver_allocs_by_object_type(uint32_t) const { |
| 153 | return 0; |
| 154 | } |
| 155 | |
| 156 | uint64_t RenderingContextDriver::get_device_total_memory() const { |
| 157 | return 0; |
| 158 | } |
| 159 | |
| 160 | uint64_t RenderingContextDriver::get_device_allocation_count() const { |
| 161 | return 0; |
| 162 | } |
| 163 | |
| 164 | uint64_t RenderingContextDriver::get_device_memory_by_object_type(uint32_t) const { |
| 165 | return 0; |
| 166 | } |
| 167 | |
| 168 | uint64_t RenderingContextDriver::get_device_allocs_by_object_type(uint32_t) const { |
| 169 | return 0; |
| 170 | } |