Path: blob/master/modules/betsy/image_compress_betsy.h
21648 views
/**************************************************************************/1/* image_compress_betsy.h */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#pragma once3132#include "core/io/image.h"33#include "core/object/worker_thread_pool.h"34#include "core/os/thread.h"35#include "core/templates/command_queue_mt.h"3637#include "servers/rendering/rendering_device_binds.h"38#include "servers/rendering/rendering_server_default.h"3940#if defined(VULKAN_ENABLED)41#include "drivers/vulkan/rendering_context_driver_vulkan.h"42#endif43#if defined(METAL_ENABLED)44#include "drivers/metal/rendering_context_driver_metal.h"45#endif4647enum BetsyFormat {48BETSY_FORMAT_BC1,49BETSY_FORMAT_BC1_DITHER,50BETSY_FORMAT_BC3,51BETSY_FORMAT_BC4_SIGNED,52BETSY_FORMAT_BC4_UNSIGNED,53BETSY_FORMAT_BC5_SIGNED,54BETSY_FORMAT_BC5_UNSIGNED,55BETSY_FORMAT_BC6_SIGNED,56BETSY_FORMAT_BC6_UNSIGNED,57BETSY_FORMAT_MAX,58};5960enum BetsyShaderType {61BETSY_SHADER_BC1_STANDARD,62BETSY_SHADER_BC1_DITHER,63BETSY_SHADER_BC4_SIGNED,64BETSY_SHADER_BC4_UNSIGNED,65BETSY_SHADER_BC6_SIGNED,66BETSY_SHADER_BC6_UNSIGNED,67BETSY_SHADER_ALPHA_STITCH,68BETSY_SHADER_RGB_TO_RGBA_FLOAT,69BETSY_SHADER_RGB_TO_RGBA_HALF,70BETSY_SHADER_RGB_TO_RGBA_UNORM8,71BETSY_SHADER_RGB_TO_RGBA_UNORM16,72BETSY_SHADER_MAX,73};7475struct BC6PushConstant {76float sizeX;77float sizeY;78uint32_t padding[2] = { 0 };79};8081struct BC1PushConstant {82uint32_t num_refines;83uint32_t padding[3] = { 0 };84};8586struct BC4PushConstant {87uint32_t channel_idx;88uint32_t padding[3] = { 0 };89};9091struct RGBToRGBAPushConstant {92uint32_t width;93uint32_t height;94uint32_t padding[2];95};9697void free_device();9899Error _betsy_compress_bptc(Image *r_img, Image::UsedChannels p_channels);100Error _betsy_compress_s3tc(Image *r_img, Image::UsedChannels p_channels);101102class BetsyCompressor : public Object {103GDSOFTCLASS(BetsyCompressor, Object);104105mutable CommandQueueMT command_queue;106bool exit = false;107WorkerThreadPool::TaskID task_id = WorkerThreadPool::INVALID_TASK_ID;108109struct BetsyShader {110RID compiled;111RID pipeline;112};113114// Resources shared by all compression formats.115RenderingDevice *compress_rd = nullptr;116RenderingContextDriver *compress_rcd = nullptr;117BetsyShader cached_shaders[BETSY_SHADER_MAX];118RID src_sampler;119120// Format-specific resources.121RID dxt1_encoding_table_buffer;122123void _init();124void _assign_mt_ids(WorkerThreadPool::TaskID p_pump_task_id);125void _thread_loop();126void _thread_exit();127128Error _get_shader(BetsyFormat p_format, const String &p_version, BetsyShader &r_shader);129Error _compress(BetsyFormat p_format, Image *r_img);130131public:132void init();133void finish();134135Error compress(BetsyFormat p_format, Image *r_img) {136Error err;137command_queue.push_and_ret(this, &BetsyCompressor::_compress, &err, p_format, r_img);138return err;139}140};141142143