Path: blob/master/drivers/gles3/storage/utilities.cpp
10033 views
/**************************************************************************/1/* utilities.cpp */2/**************************************************************************/3/* This file is part of: */4/* GODOT ENGINE */5/* https://godotengine.org */6/**************************************************************************/7/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */8/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */9/* */10/* Permission is hereby granted, free of charge, to any person obtaining */11/* a copy of this software and associated documentation files (the */12/* "Software"), to deal in the Software without restriction, including */13/* without limitation the rights to use, copy, modify, merge, publish, */14/* distribute, sublicense, and/or sell copies of the Software, and to */15/* permit persons to whom the Software is furnished to do so, subject to */16/* the following conditions: */17/* */18/* The above copyright notice and this permission notice shall be */19/* included in all copies or substantial portions of the Software. */20/* */21/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */22/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */23/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */24/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */25/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */26/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */27/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */28/**************************************************************************/2930#ifdef GLES3_ENABLED3132#include "utilities.h"3334#include "../rasterizer_gles3.h"35#include "config.h"36#include "light_storage.h"37#include "material_storage.h"38#include "mesh_storage.h"39#include "particles_storage.h"40#include "texture_storage.h"4142using namespace GLES3;4344Utilities *Utilities::singleton = nullptr;4546Utilities::Utilities() {47singleton = this;48frame = 0;49for (int i = 0; i < FRAME_COUNT; i++) {50frames[i].index = 0;51glGenQueries(max_timestamp_query_elements, frames[i].queries);5253frames[i].timestamp_names.resize(max_timestamp_query_elements);54frames[i].timestamp_cpu_values.resize(max_timestamp_query_elements);55frames[i].timestamp_count = 0;5657frames[i].timestamp_result_names.resize(max_timestamp_query_elements);58frames[i].timestamp_cpu_result_values.resize(max_timestamp_query_elements);59frames[i].timestamp_result_values.resize(max_timestamp_query_elements);60frames[i].timestamp_result_count = 0;61}62}6364Utilities::~Utilities() {65singleton = nullptr;66for (int i = 0; i < FRAME_COUNT; i++) {67glDeleteQueries(max_timestamp_query_elements, frames[i].queries);68}6970if (texture_mem_cache) {71uint32_t leaked_data_size = 0;72for (const KeyValue<GLuint, ResourceAllocation> &E : texture_allocs_cache) {73#ifdef DEV_ENABLED74ERR_PRINT(E.value.name + ": leaked " + itos(E.value.size) + " bytes.");75#else76ERR_PRINT("Texture with GL ID of " + itos(E.key) + ": leaked " + itos(E.value.size) + " bytes.");77#endif78leaked_data_size += E.value.size;79}80if (leaked_data_size < texture_mem_cache) {81ERR_PRINT("Texture cache is not empty. There may be an additional texture leak of " + itos(texture_mem_cache - leaked_data_size) + " bytes.");82}83}8485if (render_buffer_mem_cache) {86uint32_t leaked_data_size = 0;87for (const KeyValue<GLuint, ResourceAllocation> &E : render_buffer_allocs_cache) {88#ifdef DEV_ENABLED89ERR_PRINT(E.value.name + ": leaked " + itos(E.value.size) + " bytes.");90#else91ERR_PRINT("Render buffer with GL ID of " + itos(E.key) + ": leaked " + itos(E.value.size) + " bytes.");92#endif93leaked_data_size += E.value.size;94}95if (leaked_data_size < render_buffer_mem_cache) {96ERR_PRINT("Render buffer cache is not empty. There may be an additional render buffer leak of " + itos(render_buffer_mem_cache - leaked_data_size) + " bytes.");97}98}99100if (buffer_mem_cache) {101uint32_t leaked_data_size = 0;102103for (const KeyValue<GLuint, ResourceAllocation> &E : buffer_allocs_cache) {104#ifdef DEV_ENABLED105ERR_PRINT(E.value.name + ": leaked " + itos(E.value.size) + " bytes.");106#else107ERR_PRINT("Buffer with GL ID of " + itos(E.key) + ": leaked " + itos(E.value.size) + " bytes.");108#endif109leaked_data_size += E.value.size;110}111if (leaked_data_size < buffer_mem_cache) {112ERR_PRINT("Buffer cache is not empty. There may be an additional buffer leak of " + itos(buffer_mem_cache - leaked_data_size) + " bytes.");113}114}115}116117Vector<uint8_t> Utilities::buffer_get_data(GLenum p_target, GLuint p_buffer, uint32_t p_buffer_size) {118Vector<uint8_t> ret;119120if (p_buffer_size == 0) {121return ret;122}123124ret.resize(p_buffer_size);125glBindBuffer(p_target, p_buffer);126127#if defined(__EMSCRIPTEN__)128{129uint8_t *w = ret.ptrw();130godot_webgl2_glGetBufferSubData(p_target, 0, p_buffer_size, w);131}132#else133void *data = glMapBufferRange(p_target, 0, p_buffer_size, GL_MAP_READ_BIT);134ERR_FAIL_NULL_V(data, Vector<uint8_t>());135{136uint8_t *w = ret.ptrw();137memcpy(w, data, p_buffer_size);138}139glUnmapBuffer(p_target);140#endif141glBindBuffer(p_target, 0);142return ret;143}144145/* INSTANCES */146147RS::InstanceType Utilities::get_base_type(RID p_rid) const {148if (GLES3::MeshStorage::get_singleton()->owns_mesh(p_rid)) {149return RS::INSTANCE_MESH;150} else if (GLES3::MeshStorage::get_singleton()->owns_multimesh(p_rid)) {151return RS::INSTANCE_MULTIMESH;152} else if (GLES3::LightStorage::get_singleton()->owns_light(p_rid)) {153return RS::INSTANCE_LIGHT;154} else if (GLES3::LightStorage::get_singleton()->owns_lightmap(p_rid)) {155return RS::INSTANCE_LIGHTMAP;156} else if (GLES3::ParticlesStorage::get_singleton()->owns_particles(p_rid)) {157return RS::INSTANCE_PARTICLES;158} else if (GLES3::LightStorage::get_singleton()->owns_reflection_probe(p_rid)) {159return RS::INSTANCE_REFLECTION_PROBE;160} else if (GLES3::ParticlesStorage::get_singleton()->owns_particles_collision(p_rid)) {161return RS::INSTANCE_PARTICLES_COLLISION;162} else if (owns_visibility_notifier(p_rid)) {163return RS::INSTANCE_VISIBLITY_NOTIFIER;164}165return RS::INSTANCE_NONE;166}167168bool Utilities::free(RID p_rid) {169if (GLES3::TextureStorage::get_singleton()->owns_render_target(p_rid)) {170GLES3::TextureStorage::get_singleton()->render_target_free(p_rid);171return true;172} else if (GLES3::TextureStorage::get_singleton()->owns_texture(p_rid)) {173GLES3::TextureStorage::get_singleton()->texture_free(p_rid);174return true;175} else if (GLES3::TextureStorage::get_singleton()->owns_canvas_texture(p_rid)) {176GLES3::TextureStorage::get_singleton()->canvas_texture_free(p_rid);177return true;178} else if (GLES3::MaterialStorage::get_singleton()->owns_shader(p_rid)) {179GLES3::MaterialStorage::get_singleton()->shader_free(p_rid);180return true;181} else if (GLES3::MaterialStorage::get_singleton()->owns_material(p_rid)) {182GLES3::MaterialStorage::get_singleton()->material_free(p_rid);183return true;184} else if (GLES3::MeshStorage::get_singleton()->owns_mesh(p_rid)) {185GLES3::MeshStorage::get_singleton()->mesh_free(p_rid);186return true;187} else if (GLES3::MeshStorage::get_singleton()->owns_multimesh(p_rid)) {188GLES3::MeshStorage::get_singleton()->multimesh_free(p_rid);189return true;190} else if (GLES3::MeshStorage::get_singleton()->owns_mesh_instance(p_rid)) {191GLES3::MeshStorage::get_singleton()->mesh_instance_free(p_rid);192return true;193} else if (GLES3::LightStorage::get_singleton()->owns_light(p_rid)) {194GLES3::LightStorage::get_singleton()->light_free(p_rid);195return true;196} else if (GLES3::LightStorage::get_singleton()->owns_lightmap(p_rid)) {197GLES3::LightStorage::get_singleton()->lightmap_free(p_rid);198return true;199} else if (GLES3::LightStorage::get_singleton()->owns_reflection_probe(p_rid)) {200GLES3::LightStorage::get_singleton()->reflection_probe_free(p_rid);201return true;202} else if (GLES3::LightStorage::get_singleton()->owns_reflection_atlas(p_rid)) {203GLES3::LightStorage::get_singleton()->reflection_atlas_free(p_rid);204return true;205} else if (GLES3::LightStorage::get_singleton()->owns_reflection_probe_instance(p_rid)) {206GLES3::LightStorage::get_singleton()->reflection_probe_instance_free(p_rid);207return true;208} else if (GLES3::ParticlesStorage::get_singleton()->owns_particles(p_rid)) {209GLES3::ParticlesStorage::get_singleton()->particles_free(p_rid);210return true;211} else if (GLES3::ParticlesStorage::get_singleton()->owns_particles_collision(p_rid)) {212GLES3::ParticlesStorage::get_singleton()->particles_collision_free(p_rid);213return true;214} else if (GLES3::ParticlesStorage::get_singleton()->owns_particles_collision_instance(p_rid)) {215GLES3::ParticlesStorage::get_singleton()->particles_collision_instance_free(p_rid);216return true;217} else if (GLES3::MeshStorage::get_singleton()->owns_skeleton(p_rid)) {218GLES3::MeshStorage::get_singleton()->skeleton_free(p_rid);219return true;220} else if (owns_visibility_notifier(p_rid)) {221visibility_notifier_free(p_rid);222return true;223} else {224return false;225}226}227228/* DEPENDENCIES */229230void Utilities::base_update_dependency(RID p_base, DependencyTracker *p_instance) {231if (MeshStorage::get_singleton()->owns_mesh(p_base)) {232Mesh *mesh = MeshStorage::get_singleton()->get_mesh(p_base);233p_instance->update_dependency(&mesh->dependency);234} else if (MeshStorage::get_singleton()->owns_multimesh(p_base)) {235MultiMesh *multimesh = MeshStorage::get_singleton()->get_multimesh(p_base);236p_instance->update_dependency(&multimesh->dependency);237if (multimesh->mesh.is_valid()) {238base_update_dependency(multimesh->mesh, p_instance);239}240} else if (LightStorage::get_singleton()->owns_reflection_probe(p_base)) {241Dependency *dependency = LightStorage::get_singleton()->reflection_probe_get_dependency(p_base);242p_instance->update_dependency(dependency);243} else if (LightStorage::get_singleton()->owns_light(p_base)) {244Light *l = LightStorage::get_singleton()->get_light(p_base);245p_instance->update_dependency(&l->dependency);246} else if (ParticlesStorage::get_singleton()->owns_particles(p_base)) {247Dependency *dependency = ParticlesStorage::get_singleton()->particles_get_dependency(p_base);248p_instance->update_dependency(dependency);249} else if (ParticlesStorage::get_singleton()->owns_particles_collision(p_base)) {250Dependency *dependency = ParticlesStorage::get_singleton()->particles_collision_get_dependency(p_base);251p_instance->update_dependency(dependency);252} else if (owns_visibility_notifier(p_base)) {253VisibilityNotifier *vn = get_visibility_notifier(p_base);254p_instance->update_dependency(&vn->dependency);255}256}257258/* VISIBILITY NOTIFIER */259260RID Utilities::visibility_notifier_allocate() {261return visibility_notifier_owner.allocate_rid();262}263264void Utilities::visibility_notifier_initialize(RID p_notifier) {265visibility_notifier_owner.initialize_rid(p_notifier, VisibilityNotifier());266}267268void Utilities::visibility_notifier_free(RID p_notifier) {269VisibilityNotifier *vn = visibility_notifier_owner.get_or_null(p_notifier);270vn->dependency.deleted_notify(p_notifier);271visibility_notifier_owner.free(p_notifier);272}273274void Utilities::visibility_notifier_set_aabb(RID p_notifier, const AABB &p_aabb) {275VisibilityNotifier *vn = visibility_notifier_owner.get_or_null(p_notifier);276ERR_FAIL_NULL(vn);277vn->aabb = p_aabb;278vn->dependency.changed_notify(Dependency::DEPENDENCY_CHANGED_AABB);279}280281void Utilities::visibility_notifier_set_callbacks(RID p_notifier, const Callable &p_enter_callbable, const Callable &p_exit_callable) {282VisibilityNotifier *vn = visibility_notifier_owner.get_or_null(p_notifier);283ERR_FAIL_NULL(vn);284vn->enter_callback = p_enter_callbable;285vn->exit_callback = p_exit_callable;286}287288AABB Utilities::visibility_notifier_get_aabb(RID p_notifier) const {289const VisibilityNotifier *vn = visibility_notifier_owner.get_or_null(p_notifier);290ERR_FAIL_NULL_V(vn, AABB());291return vn->aabb;292}293294void Utilities::visibility_notifier_call(RID p_notifier, bool p_enter, bool p_deferred) {295VisibilityNotifier *vn = visibility_notifier_owner.get_or_null(p_notifier);296ERR_FAIL_NULL(vn);297298if (p_enter) {299if (vn->enter_callback.is_valid()) {300if (p_deferred) {301vn->enter_callback.call_deferred();302} else {303vn->enter_callback.call();304}305}306} else {307if (vn->exit_callback.is_valid()) {308if (p_deferred) {309vn->exit_callback.call_deferred();310} else {311vn->exit_callback.call();312}313}314}315}316317/* TIMING */318319void Utilities::capture_timestamps_begin() {320capture_timestamp("Frame Begin");321}322323void Utilities::capture_timestamp(const String &p_name) {324ERR_FAIL_COND(frames[frame].timestamp_count >= max_timestamp_query_elements);325326#ifdef GL_API_ENABLED327if (RasterizerGLES3::is_gles_over_gl()) {328glQueryCounter(frames[frame].queries[frames[frame].timestamp_count], GL_TIMESTAMP);329}330#endif // GL_API_ENABLED331332frames[frame].timestamp_names[frames[frame].timestamp_count] = p_name;333frames[frame].timestamp_cpu_values[frames[frame].timestamp_count] = OS::get_singleton()->get_ticks_usec();334frames[frame].timestamp_count++;335}336337void Utilities::_capture_timestamps_begin() {338// frame is incremented at the end of the frame so this gives us the queries for frame - 2. By then they should be ready.339if (frames[frame].timestamp_count) {340#ifdef GL_API_ENABLED341if (RasterizerGLES3::is_gles_over_gl()) {342for (uint32_t i = 0; i < frames[frame].timestamp_count; i++) {343uint64_t temp = 0;344glGetQueryObjectui64v(frames[frame].queries[i], GL_QUERY_RESULT, &temp);345frames[frame].timestamp_result_values[i] = temp;346}347}348#endif // GL_API_ENABLED349SWAP(frames[frame].timestamp_names, frames[frame].timestamp_result_names);350SWAP(frames[frame].timestamp_cpu_values, frames[frame].timestamp_cpu_result_values);351}352353frames[frame].timestamp_result_count = frames[frame].timestamp_count;354frames[frame].timestamp_count = 0;355frames[frame].index = Engine::get_singleton()->get_frames_drawn();356capture_timestamp("Internal Begin");357}358359void Utilities::capture_timestamps_end() {360capture_timestamp("Internal End");361frame = (frame + 1) % FRAME_COUNT;362}363364uint32_t Utilities::get_captured_timestamps_count() const {365return frames[frame].timestamp_result_count;366}367368uint64_t Utilities::get_captured_timestamps_frame() const {369return frames[frame].index;370}371372uint64_t Utilities::get_captured_timestamp_gpu_time(uint32_t p_index) const {373ERR_FAIL_UNSIGNED_INDEX_V(p_index, frames[frame].timestamp_result_count, 0);374return frames[frame].timestamp_result_values[p_index];375}376377uint64_t Utilities::get_captured_timestamp_cpu_time(uint32_t p_index) const {378ERR_FAIL_UNSIGNED_INDEX_V(p_index, frames[frame].timestamp_result_count, 0);379return frames[frame].timestamp_cpu_result_values[p_index];380}381382String Utilities::get_captured_timestamp_name(uint32_t p_index) const {383ERR_FAIL_UNSIGNED_INDEX_V(p_index, frames[frame].timestamp_result_count, String());384return frames[frame].timestamp_result_names[p_index];385}386387/* MISC */388389void Utilities::update_dirty_resources() {390MaterialStorage::get_singleton()->_update_global_shader_uniforms();391MaterialStorage::get_singleton()->_update_queued_materials();392MeshStorage::get_singleton()->_update_dirty_skeletons();393MeshStorage::get_singleton()->_update_dirty_multimeshes();394TextureStorage::get_singleton()->update_texture_atlas();395}396397void Utilities::set_debug_generate_wireframes(bool p_generate) {398Config *config = Config::get_singleton();399config->generate_wireframes = p_generate;400}401402bool Utilities::has_os_feature(const String &p_feature) const {403Config *config = Config::get_singleton();404if (!config) {405return false;406}407408if (p_feature == "rgtc") {409return config->rgtc_supported;410}411if (p_feature == "s3tc") {412return config->s3tc_supported;413}414if (p_feature == "bptc") {415return config->bptc_supported;416}417if (p_feature == "astc") {418return config->astc_supported;419}420if (p_feature == "etc2") {421return config->etc2_supported;422}423if (p_feature == "astc_hdr") {424return config->astc_hdr_supported;425}426427return false;428}429430void Utilities::update_memory_info() {431}432433uint64_t Utilities::get_rendering_info(RS::RenderingInfo p_info) {434if (p_info == RS::RENDERING_INFO_TEXTURE_MEM_USED) {435return texture_mem_cache + render_buffer_mem_cache; // Add render buffer memory to our texture mem.436} else if (p_info == RS::RENDERING_INFO_BUFFER_MEM_USED) {437return buffer_mem_cache;438} else if (p_info == RS::RENDERING_INFO_VIDEO_MEM_USED) {439return texture_mem_cache + buffer_mem_cache + render_buffer_mem_cache;440}441return 0;442}443444String Utilities::get_video_adapter_name() const {445const String rendering_device_name = String::utf8((const char *)glGetString(GL_RENDERER));446// NVIDIA suffixes all GPU model names with "/PCIe/SSE2" in OpenGL (but not Vulkan). This isn't necessary to display nowadays, so it can be trimmed.447return rendering_device_name.trim_suffix("/PCIe/SSE2");448}449450String Utilities::get_video_adapter_vendor() const {451const String rendering_device_vendor = String::utf8((const char *)glGetString(GL_VENDOR));452// NVIDIA suffixes its vendor name with " Corporation". This is neither necessary to process nor display.453return rendering_device_vendor.trim_suffix(" Corporation");454}455456RenderingDevice::DeviceType Utilities::get_video_adapter_type() const {457return RenderingDevice::DeviceType::DEVICE_TYPE_OTHER;458}459460String Utilities::get_video_adapter_api_version() const {461return String::utf8((const char *)glGetString(GL_VERSION));462}463464Size2i Utilities::get_maximum_viewport_size() const {465Config *config = Config::get_singleton();466ERR_FAIL_NULL_V(config, Size2i());467return Size2i(config->max_viewport_size[0], config->max_viewport_size[1]);468}469470uint32_t Utilities::get_maximum_shader_varyings() const {471Config *config = Config::get_singleton();472ERR_FAIL_NULL_V(config, 31);473return config->max_shader_varyings;474}475476uint64_t Utilities::get_maximum_uniform_buffer_size() const {477Config *config = Config::get_singleton();478ERR_FAIL_NULL_V(config, 65536);479return uint64_t(config->max_uniform_buffer_size);480}481482#endif // GLES3_ENABLED483484485