xref: /aosp_15_r20/external/mesa3d/src/vulkan/runtime/vk_texcompress_astc.h (revision 6104692788411f58d303aa86923a9ff6ecaded22)
1 /* Copyright (c) 2017-2023 Hans-Kristian Arntzen
2  *
3  * Permission is hereby granted, free of charge, to any person obtaining
4  * a copy of this software and associated documentation files (the
5  * "Software"), to deal in the Software without restriction, including
6  * without limitation the rights to use, copy, modify, merge, publish,
7  * distribute, sublicense, and/or sell copies of the Software, and to
8  * permit persons to whom the Software is furnished to do so, subject to
9  * the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be
12  * included in all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
17  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
18  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
19  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
20  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21  */
22 #ifndef VK_TEXCOMPRESS_ASTC_H
23 #define VK_TEXCOMPRESS_ASTC_H
24 
25 #include "vk_device.h"
26 
27 /* luts order matching astc glsl shader below,
28  * 0 - color endpoint
29  * 1 - color endpoint unquant
30  * 2 - weights
31  * 3 - weights unquant
32  * 4 - trits quints
33  */
34 #define VK_TEXCOMPRESS_ASTC_NUM_LUTS 5
35 #define VK_TEXCOMPRESS_ASTC_NUM_PARTITION_TABLES 14
36 #define VK_TEXCOMPRESS_ASTC_WRITE_DESC_SET_COUNT 8
37 
38 struct vk_texcompress_astc_state {
39    /* single buffer is allocated for all luts */
40    VkDeviceMemory luts_mem;
41    VkBuffer luts_buf;
42 
43    VkBufferView luts_buf_view[VK_TEXCOMPRESS_ASTC_NUM_LUTS];
44    VkBufferView partition_tbl_buf_view[VK_TEXCOMPRESS_ASTC_NUM_PARTITION_TABLES];
45 
46    simple_mtx_t mutex;
47    VkDescriptorSetLayout ds_layout;
48    VkPipelineLayout p_layout;
49    VkPipeline pipeline[VK_TEXCOMPRESS_ASTC_NUM_PARTITION_TABLES];
50    uint32_t pipeline_mask;
51    VkShaderModule shader_module;
52 };
53 
54 struct vk_texcompress_astc_write_descriptor_set {
55    VkWriteDescriptorSet descriptor_set[VK_TEXCOMPRESS_ASTC_WRITE_DESC_SET_COUNT];
56    VkDescriptorImageInfo dst_desc_image_info;
57    VkDescriptorImageInfo src_desc_image_info;
58 };
59 
60 void
61 vk_texcompress_astc_fill_write_descriptor_sets(struct vk_texcompress_astc_state *astc,
62                                                struct vk_texcompress_astc_write_descriptor_set *set,
63                                                VkImageView src_img_view, VkImageLayout src_img_layout,
64                                                VkImageView dst_img_view,
65                                                VkFormat format);
66 VkPipeline vk_texcompress_astc_get_decode_pipeline(struct vk_device *device,
67                                                    VkAllocationCallbacks *allocator,
68                                                    struct vk_texcompress_astc_state *astc,
69                                                    VkPipelineCache pipeline_cache,
70                                                    VkFormat format);
71 VkResult vk_texcompress_astc_init(struct vk_device *device,
72                                   VkAllocationCallbacks *allocator,
73                                   VkPipelineCache pipeline_cache,
74                                   struct vk_texcompress_astc_state **astc);
75 void vk_texcompress_astc_finish(struct vk_device *device,
76                                 VkAllocationCallbacks *allocator,
77                                 struct vk_texcompress_astc_state *astc);
78 
79 static inline VkFormat
vk_texcompress_astc_emulation_format(VkFormat format)80 vk_texcompress_astc_emulation_format(VkFormat format)
81 {
82    /* TODO: From VK_EXT_astc_Decode_mode spec, VK_FORMAT_R16G16B16A16_SFLOAT is the default
83     * option. VK_FORMAT_R8G8B8A8_UNORM is only acceptable image quality option.
84     */
85    switch (format) {
86    case VK_FORMAT_ASTC_4x4_UNORM_BLOCK:
87    case VK_FORMAT_ASTC_5x4_UNORM_BLOCK:
88    case VK_FORMAT_ASTC_5x5_UNORM_BLOCK:
89    case VK_FORMAT_ASTC_6x5_UNORM_BLOCK:
90    case VK_FORMAT_ASTC_6x6_UNORM_BLOCK:
91    case VK_FORMAT_ASTC_8x5_UNORM_BLOCK:
92    case VK_FORMAT_ASTC_8x6_UNORM_BLOCK:
93    case VK_FORMAT_ASTC_8x8_UNORM_BLOCK:
94    case VK_FORMAT_ASTC_10x5_UNORM_BLOCK:
95    case VK_FORMAT_ASTC_10x6_UNORM_BLOCK:
96    case VK_FORMAT_ASTC_10x8_UNORM_BLOCK:
97    case VK_FORMAT_ASTC_10x10_UNORM_BLOCK:
98    case VK_FORMAT_ASTC_12x10_UNORM_BLOCK:
99    case VK_FORMAT_ASTC_12x12_UNORM_BLOCK:
100       return VK_FORMAT_R8G8B8A8_UNORM;
101    case VK_FORMAT_ASTC_4x4_SRGB_BLOCK:
102    case VK_FORMAT_ASTC_5x4_SRGB_BLOCK:
103    case VK_FORMAT_ASTC_5x5_SRGB_BLOCK:
104    case VK_FORMAT_ASTC_6x5_SRGB_BLOCK:
105    case VK_FORMAT_ASTC_6x6_SRGB_BLOCK:
106    case VK_FORMAT_ASTC_8x5_SRGB_BLOCK:
107    case VK_FORMAT_ASTC_8x6_SRGB_BLOCK:
108    case VK_FORMAT_ASTC_8x8_SRGB_BLOCK:
109    case VK_FORMAT_ASTC_10x5_SRGB_BLOCK:
110    case VK_FORMAT_ASTC_10x6_SRGB_BLOCK:
111    case VK_FORMAT_ASTC_10x8_SRGB_BLOCK:
112    case VK_FORMAT_ASTC_10x10_SRGB_BLOCK:
113    case VK_FORMAT_ASTC_12x10_SRGB_BLOCK:
114    case VK_FORMAT_ASTC_12x12_SRGB_BLOCK:
115       return VK_FORMAT_R8G8B8A8_SRGB;
116    default:
117       return VK_FORMAT_UNDEFINED;
118    }
119 }
120 
121 #endif /* VK_TEXCOMPRESS_ASTC_H */
122