Path: blob/master/modules/betsy/image_compress_betsy.h
10277 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_MAX,69};7071struct BC6PushConstant {72float sizeX;73float sizeY;74uint32_t padding[2] = { 0 };75};7677struct BC1PushConstant {78uint32_t num_refines;79uint32_t padding[3] = { 0 };80};8182struct BC4PushConstant {83uint32_t channel_idx;84uint32_t padding[3] = { 0 };85};8687void free_device();8889Error _betsy_compress_bptc(Image *r_img, Image::UsedChannels p_channels);90Error _betsy_compress_s3tc(Image *r_img, Image::UsedChannels p_channels);9192class BetsyCompressor : public Object {93mutable CommandQueueMT command_queue;94bool exit = false;95WorkerThreadPool::TaskID task_id = WorkerThreadPool::INVALID_TASK_ID;9697struct BetsyShader {98RID compiled;99RID pipeline;100};101102// Resources shared by all compression formats.103RenderingDevice *compress_rd = nullptr;104RenderingContextDriver *compress_rcd = nullptr;105BetsyShader cached_shaders[BETSY_SHADER_MAX];106RID src_sampler;107108// Format-specific resources.109RID dxt1_encoding_table_buffer;110111void _init();112void _assign_mt_ids(WorkerThreadPool::TaskID p_pump_task_id);113void _thread_loop();114void _thread_exit();115116Error _get_shader(BetsyFormat p_format, const String &p_version, BetsyShader &r_shader);117Error _compress(BetsyFormat p_format, Image *r_img);118119public:120void init();121void finish();122123Error compress(BetsyFormat p_format, Image *r_img) {124Error err;125command_queue.push_and_ret(this, &BetsyCompressor::_compress, &err, p_format, r_img);126return err;127}128};129130131