Debian Codegraph @

modules/betsy/image_compress_betsy.h

raw symbols SCIP
1 /**************************************************************************/
2 /* image_compress_betsy.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 #pragma once
32
33 #include "core/io/image.h"
34 #include "core/object/worker_thread_pool.h"
35 #include "core/os/thread.h"
36 #include "core/templates/command_queue_mt.h"
37
38 #include "servers/rendering/rendering_device_binds.h"
39 #include "servers/rendering/rendering_server_default.h"
40
41 #if defined(VULKAN_ENABLED)
42 #include "drivers/vulkan/rendering_context_driver_vulkan.h"
43 #endif
44 #if defined(METAL_ENABLED)
45 #include "drivers/metal/rendering_context_driver_metal.h"
46 #endif
47
48 enum BetsyFormat {
49 BETSY_FORMAT_BC1,
50 BETSY_FORMAT_BC1_DITHER,
51 BETSY_FORMAT_BC3,
52 BETSY_FORMAT_BC4_SIGNED,
53 BETSY_FORMAT_BC4_UNSIGNED,
54 BETSY_FORMAT_BC5_SIGNED,
55 BETSY_FORMAT_BC5_UNSIGNED,
56 BETSY_FORMAT_BC6_SIGNED,
57 BETSY_FORMAT_BC6_UNSIGNED,
58 BETSY_FORMAT_MAX,
59 };
60
61 enum BetsyShaderType {
62 BETSY_SHADER_BC1_STANDARD,
63 BETSY_SHADER_BC1_DITHER,
64 BETSY_SHADER_BC4_SIGNED,
65 BETSY_SHADER_BC4_UNSIGNED,
66 BETSY_SHADER_BC6_SIGNED,
67 BETSY_SHADER_BC6_UNSIGNED,
68 BETSY_SHADER_ALPHA_STITCH,
69 BETSY_SHADER_RGB_TO_RGBA_FLOAT,
70 BETSY_SHADER_RGB_TO_RGBA_HALF,
71 BETSY_SHADER_RGB_TO_RGBA_UNORM8,
72 BETSY_SHADER_RGB_TO_RGBA_UNORM16,
73 BETSY_SHADER_MAX,
74 };
75
76 struct BC6PushConstant {
77 float sizeX;
78 float sizeY;
79 uint32_t padding[2] = { 0 };
80 };
81
82 struct BC1PushConstant {
83 uint32_t num_refines;
84 uint32_t padding[3] = { 0 };
85 };
86
87 struct BC4PushConstant {
88 uint32_t channel_idx;
89 uint32_t padding[3] = { 0 };
90 };
91
92 struct RGBToRGBAPushConstant {
93 uint32_t width;
94 uint32_t height;
95 uint32_t padding[2];
96 };
97
98 void free_device();
99
100 Error _betsy_compress_bptc(Image *r_img, Image::UsedChannels p_channels);
101 Error _betsy_compress_s3tc(Image *r_img, Image::UsedChannels p_channels);
102
103 class BetsyCompressor : public Object {
104 GDSOFTCLASS(BetsyCompressor, Object);
105
106 mutable CommandQueueMT command_queue;
107 bool exit = false;
108 WorkerThreadPool::TaskID task_id = WorkerThreadPool::INVALID_TASK_ID;
109
110 struct BetsyShader {
111 RID compiled;
112 RID pipeline;
113 };
114
115 // Resources shared by all compression formats.
116 RenderingDevice *compress_rd = nullptr;
117 RenderingContextDriver *compress_rcd = nullptr;
118 BetsyShader cached_shaders[BETSY_SHADER_MAX];
119 RID src_sampler;
120
121 // Format-specific resources.
122 RID dxt1_encoding_table_buffer;
123
124 void _init();
125 void _assign_mt_ids(WorkerThreadPool::TaskID p_pump_task_id);
126 void _thread_loop();
127 void _thread_exit();
128
129 Error _get_shader(BetsyFormat p_format, const String &p_version, BetsyShader &r_shader);
130 Error _compress(BetsyFormat p_format, Image *r_img);
131
132 public:
133 void init();
134 void finish();
135
136 Error compress(BetsyFormat p_format, Image *r_img) {
137 Error err;
138 command_queue.push_and_ret(this, &BetsyCompressor::_compress, &err, p_format, r_img);
139 return err;
140 }
141 };