Skip to content
0.2.0-dev Download ZIP
181 lines ·5.4 KB Raw
1// basisu_uastc_hdr_4x4_enc.h
2#pragma once
3#include "basisu_enc.h"
4#include "basisu_gpu_texture.h"
5#include "../transcoder/basisu_astc_helpers.h"
6#include "../transcoder/basisu_astc_hdr_core.h"
7#include "basisu_astc_hdr_common.h"
8
9namespace basisu
10{
11 struct uastc_hdr_4x4_codec_options : astc_hdr_codec_base_options
12 {
13 float m_bc6h_err_weight;
14
15 bool m_use_solid;
16
17 bool m_use_mode11_part1;
18 bool m_mode11_uber_mode;
19 uint32_t m_first_mode11_weight_ise_range;
20 uint32_t m_last_mode11_weight_ise_range;
21 bool m_mode11_direct_only;
22 int32_t m_first_mode11_submode;
23 int32_t m_last_mode11_submode;
24
25 bool m_use_mode7_part1;
26 uint32_t m_first_mode7_part1_weight_ise_range;
27 uint32_t m_last_mode7_part1_weight_ise_range;
28
29 bool m_use_mode7_part2;
30 uint32_t m_mode7_part2_part_masks;
31 uint32_t m_first_mode7_part2_weight_ise_range;
32 uint32_t m_last_mode7_part2_weight_ise_range;
33
34 bool m_use_mode11_part2;
35 uint32_t m_mode11_part2_part_masks;
36 uint32_t m_first_mode11_part2_weight_ise_range;
37 uint32_t m_last_mode11_part2_weight_ise_range;
38
39 bool m_refine_weights;
40
41 uint32_t m_level;
42
43 bool m_use_estimated_partitions;
44 uint32_t m_max_estimated_partitions;
45
46 uastc_hdr_4x4_codec_options();
47
48 void init();
49
50 // TODO: set_quality_level() is preferred to configure the codec for transcoding purposes.
51 static const int cMinLevel = 0;
52 static const int cMaxLevel = 4;
53 static const int cDefaultLevel = 1;
54 void set_quality_level(int level);
55
56 private:
57 void set_quality_best();
58 void set_quality_normal();
59 void set_quality_fastest();
60 };
61
62 struct astc_hdr_4x4_pack_results
63 {
64 double m_best_block_error;
65 double m_bc6h_block_error; // note this is not used/set by the encoder, here for convienance
66
67 // Encoder results (logical ASTC block)
68 astc_helpers::log_astc_block m_best_blk;
69
70 // For statistical use
71 uint32_t m_best_submodes[2];
72 uint32_t m_best_pat_index;
73 bool m_constrained_weights;
74
75 bool m_improved_via_refinement_flag;
76
77 // Only valid if the block is solid
78 basist::astc_blk m_solid_blk;
79
80 // The BC6H transcoded block
81 basist::bc6h_block m_bc6h_block;
82
83 // Solid color/void extent flag
84 bool m_is_solid;
85
86 void clear()
87 {
88 m_best_block_error = 1e+30f;
89 m_bc6h_block_error = 1e+30f;
90
91 m_best_blk.clear();
92 m_best_blk.m_grid_width = 4;
93 m_best_blk.m_grid_height = 4;
94 m_best_blk.m_endpoint_ise_range = 20; // 0-255
95
96 clear_obj(m_best_submodes);
97
98 m_best_pat_index = 0;
99 m_constrained_weights = false;
100
101 clear_obj(m_bc6h_block);
102
103 m_is_solid = false;
104 m_improved_via_refinement_flag = false;
105 }
106 };
107
108 // Encodes a 4x4 ASTC HDR block given a 4x4 array of source block pixels/texels.
109 // Supports solid color blocks, mode 11 (all submodes), mode 7/1 partition (all submodes),
110 // and mode 7/2 partitions (all submodes) - 30 patterns, only the ones also in common with the BC6H format.
111 // The packed ASTC weight grid dimensions are currently always 4x4 texels, but may be also 3x3 in the future.
112 // This function is thread safe, i.e. it may be called from multiple encoding threads simultanously with different blocks.
113 //
114 // Parameters:
115 // pRGBPixels - An array of 48 (16 RGB) floats: the 4x4 block to pack
116 // pPacked_block - A pointer to the packed ASTC HDR block
117 // coptions - Codec options
118 // pInternal_results - An optional pointer to details about how the block was packed, for statistics/debugging purposes. May be nullptr.
119 //
120 // Requirements:
121 // astc_hdr_enc_init() MUST have been called first to initialized the codec.
122 // Input pixels are checked and cannot be NaN's, Inf's, signed, or too large (greater than MAX_HALF_FLOAT, or 65504).
123 // Normal values and denormals are okay.
124 bool astc_hdr_4x4_enc_block(
125 const float* pRGBPixels, const basist::half_float *pRGBPixelsHalf,
126 const uastc_hdr_4x4_codec_options& coptions,
127 basisu::vector<astc_hdr_4x4_pack_results> &all_results);
128
129 bool astc_hdr_4x4_pack_results_to_block(basist::astc_blk& dst_blk, const astc_hdr_4x4_pack_results& results);
130
131 bool astc_hdr_4x4_refine_weights(const basist::half_float* pSource_block, astc_hdr_4x4_pack_results& cur_results, const uastc_hdr_4x4_codec_options& coptions, float bc6h_weight, bool* pImproved_flag);
132
133 struct astc_hdr_4x4_block_stats
134 {
135 std::mutex m_mutex;
136
137 uint32_t m_total_blocks;
138 uint32_t m_total_2part, m_total_solid;
139 uint32_t m_total_mode7_1part, m_total_mode7_2part;
140 uint32_t m_total_mode11_1part, m_total_mode11_2part;
141 uint32_t m_total_mode11_1part_constrained_weights;
142
143 uint32_t m_weight_range_hist_7[11];
144 uint32_t m_weight_range_hist_7_2part[11];
145 uint32_t m_mode7_submode_hist[6];
146
147 uint32_t m_weight_range_hist_11[11];
148 uint32_t m_weight_range_hist_11_2part[11];
149 uint32_t m_mode11_submode_hist[9];
150
151 uint32_t m_part_hist[32];
152
153 uint32_t m_total_refined;
154
155 astc_hdr_4x4_block_stats() { clear(); }
156
157 void clear()
158 {
159 std::lock_guard<std::mutex> lck(m_mutex);
160
161 m_total_blocks = 0;
162 m_total_mode7_1part = 0, m_total_mode7_2part = 0, m_total_mode11_1part = 0, m_total_2part = 0, m_total_solid = 0, m_total_mode11_2part = 0;
163 m_total_mode11_1part_constrained_weights = 0;
164 m_total_refined = 0;
165
166 clear_obj(m_weight_range_hist_11);
167 clear_obj(m_weight_range_hist_11_2part);
168 clear_obj(m_weight_range_hist_7);
169 clear_obj(m_weight_range_hist_7_2part);
170 clear_obj(m_mode7_submode_hist);
171 clear_obj(m_mode11_submode_hist);
172 clear_obj(m_part_hist);
173 }
174
175 void update(const astc_hdr_4x4_pack_results& log_blk);
176
177 void print();
178 };
179
180} // namespace basisu
181