1 /* Copyright (c) 2017-2022 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 23 #pragma once 24 25 #include <mutex> 26 #include <unordered_map> 27 #include <vector> 28 29 namespace Granite 30 { 31 32 struct ASTCQuantizationMode 33 { 34 uint8_t bits, trits, quints; 35 }; 36 37 // In order to decode color endpoints, we need to convert available bits and number of values 38 // into a format of (bits, trits, quints). A simple LUT texture is a reasonable approach for this. 39 // Decoders are expected to have some form of LUT to deal with this ... 40 static const ASTCQuantizationMode astc_quantization_modes[] = { 41 { 8, 0, 0 }, 42 { 6, 1, 0 }, 43 { 5, 0, 1 }, 44 { 7, 0, 0 }, 45 { 5, 1, 0 }, 46 { 4, 0, 1 }, 47 { 6, 0, 0 }, 48 { 4, 1, 0 }, 49 { 3, 0, 1 }, 50 { 5, 0, 0 }, 51 { 3, 1, 0 }, 52 { 2, 0, 1 }, 53 { 4, 0, 0 }, 54 { 2, 1, 0 }, 55 { 1, 0, 1 }, 56 { 3, 0, 0 }, 57 { 1, 1, 0 }, 58 }; 59 60 constexpr size_t astc_num_quantization_modes = sizeof(astc_quantization_modes) / sizeof(astc_quantization_modes[0]); 61 62 static const ASTCQuantizationMode astc_weight_modes[] = { 63 { 0, 0, 0 }, // Invalid 64 { 0, 0, 0 }, // Invalid 65 { 1, 0, 0 }, 66 { 0, 1, 0 }, 67 { 2, 0, 0 }, 68 { 0, 0, 1 }, 69 { 1, 1, 0 }, 70 { 3, 0, 0 }, 71 { 0, 0, 0 }, // Invalid 72 { 0, 0, 0 }, // Invalid 73 { 1, 0, 1 }, 74 { 2, 1, 0 }, 75 { 4, 0, 0 }, 76 { 2, 0, 1 }, 77 { 3, 1, 0 }, 78 { 5, 0, 0 }, 79 }; 80 81 constexpr size_t astc_num_weight_modes = sizeof(astc_weight_modes) / sizeof(astc_weight_modes[0]); 82 83 struct ASTCLutHolder 84 { 85 ASTCLutHolder(); 86 87 void init_color_endpoint(); 88 void init_weight_luts(); 89 void init_trits_quints(); 90 91 struct 92 { 93 size_t unquant_offset = 0; 94 uint8_t unquant_lut[2048]; 95 uint16_t lut[9][128][4]; 96 size_t unquant_lut_offsets[astc_num_quantization_modes]; 97 } color_endpoint; 98 99 struct 100 { 101 size_t unquant_offset = 0; 102 uint8_t unquant_lut[2048]; 103 uint8_t lut[astc_num_weight_modes][4]; 104 } weights; 105 106 struct 107 { 108 uint16_t trits_quints[256 + 128]; 109 } integer; 110 111 struct PartitionTable 112 { 113 PartitionTable() = default; 114 PartitionTable(unsigned width, unsigned height); 115 std::vector<uint8_t> lut_buffer; 116 unsigned lut_width = 0; 117 unsigned lut_height = 0; 118 }; 119 120 std::mutex table_lock; 121 std::unordered_map<unsigned, PartitionTable> tables; 122 123 PartitionTable &get_partition_table(unsigned width, unsigned height); 124 }; 125 126 ASTCLutHolder &get_astc_luts(); 127 } 128