Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/modules/betsy/image_compress_betsy.h
10277 views
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_MAX,
70
};
71
72
struct BC6PushConstant {
73
float sizeX;
74
float sizeY;
75
uint32_t padding[2] = { 0 };
76
};
77
78
struct BC1PushConstant {
79
uint32_t num_refines;
80
uint32_t padding[3] = { 0 };
81
};
82
83
struct BC4PushConstant {
84
uint32_t channel_idx;
85
uint32_t padding[3] = { 0 };
86
};
87
88
void free_device();
89
90
Error _betsy_compress_bptc(Image *r_img, Image::UsedChannels p_channels);
91
Error _betsy_compress_s3tc(Image *r_img, Image::UsedChannels p_channels);
92
93
class BetsyCompressor : public Object {
94
mutable CommandQueueMT command_queue;
95
bool exit = false;
96
WorkerThreadPool::TaskID task_id = WorkerThreadPool::INVALID_TASK_ID;
97
98
struct BetsyShader {
99
RID compiled;
100
RID pipeline;
101
};
102
103
// Resources shared by all compression formats.
104
RenderingDevice *compress_rd = nullptr;
105
RenderingContextDriver *compress_rcd = nullptr;
106
BetsyShader cached_shaders[BETSY_SHADER_MAX];
107
RID src_sampler;
108
109
// Format-specific resources.
110
RID dxt1_encoding_table_buffer;
111
112
void _init();
113
void _assign_mt_ids(WorkerThreadPool::TaskID p_pump_task_id);
114
void _thread_loop();
115
void _thread_exit();
116
117
Error _get_shader(BetsyFormat p_format, const String &p_version, BetsyShader &r_shader);
118
Error _compress(BetsyFormat p_format, Image *r_img);
119
120
public:
121
void init();
122
void finish();
123
124
Error compress(BetsyFormat p_format, Image *r_img) {
125
Error err;
126
command_queue.push_and_ret(this, &BetsyCompressor::_compress, &err, p_format, r_img);
127
return err;
128
}
129
};
130
131