Blender V5.3
ktx_bridge/intern/encoder.cpp
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2026 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
5#include "encoder.h"
6#include "common.h"
7
8#include <algorithm>
9#include <cstring>
10#include <thread>
11#include <vector>
12
13int encodeToFile(const char *filename,
14 const unsigned char *pixels,
15 unsigned int width,
16 unsigned int height,
17 unsigned int channels,
18 int quality,
19 int is_data,
20 int use_uastc,
21 int compress)
22{
23 if (pixels == nullptr || (channels != 3 && channels != 4)) {
24 return KTX_INVALID_VALUE;
25 }
26
27 // KHR_texture_basisu requires width and height to be multiples of 4
28 if (width % 4 != 0 || height % 4 != 0) {
29 return KTX_INVALID_VALUE;
30 }
31
32 /* KTX2 stores rows top-to-bottom; `pixels` follows Blender's bottom-to-top convention, so
33 * flip while copying into a KTX-ready buffer. */
34 const size_t row_size = size_t(width) * channels;
35 std::vector<unsigned char> flipped(row_size * height);
36 for (unsigned int y = 0; y < height; y++) {
37 memcpy(flipped.data() + size_t(y) * row_size,
38 pixels + size_t(height - 1 - y) * row_size,
39 row_size);
40 }
41
42 ktxTextureCreateInfo create_info = {};
43 if (channels == 3) {
44 create_info.vkFormat = is_data ? VK_FORMAT_R8G8B8_UNORM : VK_FORMAT_R8G8B8_SRGB;
45 }
46 else {
47 create_info.vkFormat = is_data ? VK_FORMAT_R8G8B8A8_UNORM : VK_FORMAT_R8G8B8A8_SRGB;
48 }
49 create_info.baseWidth = width;
50 create_info.baseHeight = height;
51 create_info.baseDepth = 1;
52 create_info.numDimensions = 2;
53 create_info.numLevels = 1;
54 create_info.numLayers = 1;
55 create_info.numFaces = 1;
56 create_info.isArray = KTX_FALSE;
57 create_info.generateMipmaps = KTX_FALSE;
58
59 ktxTexture2 *texture = nullptr;
60 ktx_error_code_e result = ktxTexture2_Create(
61 &create_info, KTX_TEXTURE_CREATE_ALLOC_STORAGE, &texture);
62 if (result != KTX_SUCCESS) {
63 return result;
64 }
65
66 result = ktxTexture_SetImageFromMemory(ktxTexture(texture),
67 /*level*/ 0,
68 /*layer*/ 0,
69 /*faceSlice*/ 0,
70 flipped.data(),
71 flipped.size());
72 if (result != KTX_SUCCESS) {
73 ktxTexture_Destroy(ktxTexture(texture));
74 return result;
75 }
76
77 ktxBasisParams params = {};
78 params.structSize = sizeof(params);
79 params.threadCount = std::max(1u, std::thread::hardware_concurrency());
80
81 if (use_uastc) {
82 /* UASTC: higher quality than ETC1S, larger output.
83 * Map `quality` [0, 100] to UASTC levels [0, KTX_PACK_UASTC_MAX_LEVEL]. */
84 params.uastc = KTX_TRUE;
85 const int level = int(float(quality) / 100.0f * float(KTX_PACK_UASTC_MAX_LEVEL) + 0.5f);
86 params.uastcFlags = ktx_pack_uastc_flags(std::clamp(level, 0, int(KTX_PACK_UASTC_MAX_LEVEL)));
87 }
88 else {
89 /* ETC1S: smaller output, lower quality than UASTC.
90 * Map `quality` [0, 100] to qualityLevel [1, 255]. */
91 params.compressionLevel = KTX_ETC1S_DEFAULT_COMPRESSION_LEVEL;
92 params.qualityLevel = uint32_t(
93 std::clamp(int(float(quality) / 100.0f * 254.0f + 1.5f), 1, 255));
94 }
95
96 result = ktxTexture2_CompressBasisEx(texture, &params);
97 if (result != KTX_SUCCESS) {
98 ktxTexture_Destroy(ktxTexture(texture));
99 return result;
100 }
101
102 /* `ktxTexture2_Create()` always writes BT709 primaries, regardless of the source
103 * VkFormat. KHR_texture_basisu requires UNSPECIFIED primaries for non-color data (normal
104 * maps, etc.), so force it back for `is_data` textures. */
105 if (is_data) {
106 ktxTexture2_SetPrimaries(texture, KHR_DF_PRIMARIES_UNSPECIFIED);
107 }
108
109 /* Zstd supercompression can only be layered on top of UASTC: ETC1S already sets its own
110 * `supercompressionScheme` (KTX_SS_BASIS_LZ), and `ktxTexture2_DeflateZstd()` rejects any
111 * texture whose scheme isn't already KTX_SS_NONE. */
112 if (use_uastc && compress > 0) {
113 const ktx_uint32_t zstd_level = ktx_uint32_t(std::clamp(compress, 1, 22));
114 result = ktxTexture2_DeflateZstd(texture, zstd_level);
115 if (result != KTX_SUCCESS) {
116 ktxTexture_Destroy(ktxTexture(texture));
117 return result;
118 }
119 }
120
121 result = ktxTexture_WriteToNamedFile(ktxTexture(texture), filename);
122 ktxTexture_Destroy(ktxTexture(texture));
123
124 return result;
125}
TEX_TEMPLATE DataVec texture(T, FltCoord, float=0.0f) RET
#define VK_FORMAT_R8G8B8A8_UNORM
#define VK_FORMAT_R8G8B8_SRGB
#define VK_FORMAT_R8G8B8A8_SRGB
#define VK_FORMAT_R8G8B8_UNORM
int encodeToFile(const char *filename, const unsigned char *pixels, unsigned int width, unsigned int height, unsigned int channels, int quality, int is_data, int use_uastc, int compress)