| 1 | /**************************************************************************/ |
| 2 | /* utilities.h */ |
| 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 | #ifndef UTILITIES_GLES3_H |
| 32 | #define UTILITIES_GLES3_H |
| 33 | |
| 34 | #ifdef GLES3_ENABLED |
| 35 | |
| 36 | #include "servers/rendering/storage/utilities.h" |
| 37 | |
| 38 | #include "platform_config.h" |
| 39 | #ifndef OPENGL_INCLUDE_H |
| 40 | #include <GLES3/gl3.h> |
| 41 | #else |
| 42 | #include OPENGL_INCLUDE_H |
| 43 | #endif |
| 44 | |
| 45 | namespace GLES3 { |
| 46 | |
| 47 | class Utilities : public RendererUtilities { |
| 48 | private: |
| 49 | static Utilities *singleton; |
| 50 | |
| 51 | struct ResourceAllocation { |
| 52 | #ifdef DEV_ENABLED |
| 53 | String name; |
| 54 | #endif |
| 55 | uint32_t size = 0; |
| 56 | }; |
| 57 | HashMap<GLuint, ResourceAllocation> buffer_allocs_cache; |
| 58 | HashMap<GLuint, ResourceAllocation> texture_allocs_cache; |
| 59 | |
| 60 | uint64_t buffer_mem_cache = 0; |
| 61 | uint64_t texture_mem_cache = 0; |
| 62 | |
| 63 | public: |
| 64 | static Utilities *get_singleton() { return singleton; } |
| 65 | |
| 66 | Utilities(); |
| 67 | ~Utilities(); |
| 68 | |
| 69 | // Buffer size is specified in bytes |
| 70 | static Vector<uint8_t> buffer_get_data(GLenum p_target, GLuint p_buffer, uint32_t p_buffer_size); |
| 71 | |
| 72 | // Allocate memory with glBufferData. Does not handle resizing. |
| 73 | _FORCE_INLINE_ void buffer_allocate_data(GLenum p_target, GLuint p_id, uint32_t p_size, const void *p_data, GLenum p_usage, String p_name = "" ) { |
| 74 | glBufferData(p_target, p_size, p_data, p_usage); |
| 75 | buffer_mem_cache += p_size; |
| 76 | |
| 77 | #ifdef DEV_ENABLED |
| 78 | ERR_FAIL_COND_MSG(buffer_allocs_cache.has(p_id), "trying to allocate buffer with name " + p_name + " but ID already used by " + buffer_allocs_cache[p_id].name); |
| 79 | #endif |
| 80 | |
| 81 | ResourceAllocation resource_allocation; |
| 82 | resource_allocation.size = p_size; |
| 83 | #ifdef DEV_ENABLED |
| 84 | resource_allocation.name = p_name + ": " + itos((uint64_t)p_id); |
| 85 | #endif |
| 86 | buffer_allocs_cache[p_id] = resource_allocation; |
| 87 | } |
| 88 | |
| 89 | _FORCE_INLINE_ void buffer_free_data(GLuint p_id) { |
| 90 | ERR_FAIL_COND(!buffer_allocs_cache.has(p_id)); |
| 91 | glDeleteBuffers(1, &p_id); |
| 92 | buffer_mem_cache -= buffer_allocs_cache[p_id].size; |
| 93 | buffer_allocs_cache.erase(p_id); |
| 94 | } |
| 95 | |
| 96 | // Records that data was allocated for state tracking purposes. |
| 97 | _FORCE_INLINE_ void texture_allocated_data(GLuint p_id, uint32_t p_size, String p_name = "" ) { |
| 98 | texture_mem_cache += p_size; |
| 99 | #ifdef DEV_ENABLED |
| 100 | ERR_FAIL_COND_MSG(texture_allocs_cache.has(p_id), "trying to allocate texture with name " + p_name + " but ID already used by " + texture_allocs_cache[p_id].name); |
| 101 | #endif |
| 102 | ResourceAllocation resource_allocation; |
| 103 | resource_allocation.size = p_size; |
| 104 | #ifdef DEV_ENABLED |
| 105 | resource_allocation.name = p_name + ": " + itos((uint64_t)p_id); |
| 106 | #endif |
| 107 | texture_allocs_cache[p_id] = resource_allocation; |
| 108 | } |
| 109 | |
| 110 | _FORCE_INLINE_ void texture_free_data(GLuint p_id) { |
| 111 | ERR_FAIL_COND(!texture_allocs_cache.has(p_id)); |
| 112 | glDeleteTextures(1, &p_id); |
| 113 | texture_mem_cache -= texture_allocs_cache[p_id].size; |
| 114 | texture_allocs_cache.erase(p_id); |
| 115 | } |
| 116 | |
| 117 | _FORCE_INLINE_ void texture_resize_data(GLuint p_id, uint32_t p_size) { |
| 118 | ERR_FAIL_COND(!texture_allocs_cache.has(p_id)); |
| 119 | texture_mem_cache -= texture_allocs_cache[p_id].size; |
| 120 | texture_mem_cache += p_size; |
| 121 | texture_allocs_cache[p_id].size = p_size; |
| 122 | } |
| 123 | |
| 124 | /* INSTANCES */ |
| 125 | |
| 126 | virtual RS::InstanceType get_base_type(RID p_rid) const override; |
| 127 | virtual bool free(RID p_rid) override; |
| 128 | |
| 129 | /* DEPENDENCIES */ |
| 130 | |
| 131 | virtual void base_update_dependency(RID p_base, DependencyTracker *p_instance) override; |
| 132 | |
| 133 | /* VISIBILITY NOTIFIER */ |
| 134 | virtual RID visibility_notifier_allocate() override; |
| 135 | virtual void visibility_notifier_initialize(RID p_notifier) override; |
| 136 | virtual void visibility_notifier_free(RID p_notifier) override; |
| 137 | |
| 138 | virtual void visibility_notifier_set_aabb(RID p_notifier, const AABB &p_aabb) override; |
| 139 | virtual void visibility_notifier_set_callbacks(RID p_notifier, const Callable &p_enter_callbable, const Callable &p_exit_callable) override; |
| 140 | |
| 141 | virtual AABB visibility_notifier_get_aabb(RID p_notifier) const override; |
| 142 | virtual void visibility_notifier_call(RID p_notifier, bool p_enter, bool p_deferred) override; |
| 143 | |
| 144 | /* TIMING */ |
| 145 | |
| 146 | #define MAX_QUERIES 256 |
| 147 | #define FRAME_COUNT 3 |
| 148 | |
| 149 | struct Frame { |
| 150 | GLuint queries[MAX_QUERIES]; |
| 151 | TightLocalVector<String> timestamp_names; |
| 152 | TightLocalVector<uint64_t> timestamp_cpu_values; |
| 153 | uint32_t timestamp_count = 0; |
| 154 | TightLocalVector<String> timestamp_result_names; |
| 155 | TightLocalVector<uint64_t> timestamp_cpu_result_values; |
| 156 | TightLocalVector<uint64_t> timestamp_result_values; |
| 157 | uint32_t timestamp_result_count = 0; |
| 158 | uint64_t index = 0; |
| 159 | }; |
| 160 | |
| 161 | const uint32_t max_timestamp_query_elements = MAX_QUERIES; |
| 162 | |
| 163 | Frame frames[FRAME_COUNT]; // Frames for capturing timestamps. We use 3 so we don't need to wait for commands to complete |
| 164 | uint32_t frame = 0; |
| 165 | |
| 166 | virtual void capture_timestamps_begin() override; |
| 167 | virtual void capture_timestamp(const String &p_name) override; |
| 168 | virtual uint32_t get_captured_timestamps_count() const override; |
| 169 | virtual uint64_t get_captured_timestamps_frame() const override; |
| 170 | virtual uint64_t get_captured_timestamp_gpu_time(uint32_t p_index) const override; |
| 171 | virtual uint64_t get_captured_timestamp_cpu_time(uint32_t p_index) const override; |
| 172 | virtual String get_captured_timestamp_name(uint32_t p_index) const override; |
| 173 | void _capture_timestamps_begin(); |
| 174 | void capture_timestamps_end(); |
| 175 | |
| 176 | /* MISC */ |
| 177 | |
| 178 | virtual void update_dirty_resources() override; |
| 179 | virtual void set_debug_generate_wireframes(bool p_generate) override; |
| 180 | |
| 181 | virtual bool has_os_feature(const String &p_feature) const override; |
| 182 | |
| 183 | virtual void update_memory_info() override; |
| 184 | |
| 185 | virtual uint64_t get_rendering_info(RS::RenderingInfo p_info) override; |
| 186 | virtual String get_video_adapter_name() const override; |
| 187 | virtual String get_video_adapter_vendor() const override; |
| 188 | virtual RenderingDevice::DeviceType get_video_adapter_type() const override; |
| 189 | virtual String get_video_adapter_api_version() const override; |
| 190 | |
| 191 | virtual Size2i get_maximum_viewport_size() const override; |
| 192 | }; |
| 193 | |
| 194 | } // namespace GLES3 |
| 195 | |
| 196 | #endif // GLES3_ENABLED |
| 197 | |
| 198 | #endif // UTILITIES_GLES3_H |
| 199 | |