Blender V5.3
render_texture_cache.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2026 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#ifdef WITH_CYCLES
10
11# include "render_intern.hh" /* own include */
12
13# include <fmt/format.h>
14# include <mutex>
15# include <string>
16# include <utility>
17
18# include "MEM_guardedalloc.h"
19
20# include "DNA_image_types.h"
21# include "DNA_userdef_types.h"
23
24# include "BLI_fileops.hh"
25# include "BLI_listbase.hh"
26# include "BLI_path_utils.hh"
27# include "BLI_set.hh"
28# include "BLI_string.hh"
29# include "BLI_task.hh"
30# include "BLI_utildefines.hh"
31# include "BLI_vector.hh"
32
33# include "BKE_bpath.hh"
34# include "BKE_context.hh"
35# include "BKE_global.hh"
36# include "BKE_image.hh"
37# include "BKE_lib_query.hh"
38# include "BKE_main.hh"
39# include "BKE_node.hh"
40# include "BKE_report.hh"
41
42# include "BLT_translation.hh"
43
44# include "RNA_access.hh"
45# include "RNA_define.hh"
46
47# include "UI_interface_icons.hh"
48# include "UI_interface_layout.hh"
49# include "UI_resources.hh"
50
51# include "WM_api.hh"
52
53# include "CCL_api.h"
54
55namespace blender {
56
57/* -------------------------------------------------------------------- */
60
61/* Gather all images referenced by shader node trees. */
62static Set<const Image *> gather_cache_images(Main *bmain)
63{
64 Set<const Image *> images;
65
66 FOREACH_NODETREE_BEGIN (bmain, ntree, id) {
67 if (ntree->type != NTREE_SHADER) {
68 continue;
69 }
71 bmain,
72 &ntree->id,
73 [&](LibraryIDLinkCallbackData *cb_data) {
74 ID *image_id = *cb_data->id_pointer;
75 if (image_id && GS(image_id->name) == ID_IM) {
76 Image *image = blender::id_cast<Image *>(image_id);
77 images.add(image);
78 }
79 return IDWALK_RET_NOP;
80 },
81 nullptr,
83 }
85
86 return images;
87}
88
89/* Gather all source image files to the texture cache, expanding UDIM tiles and
90 * optionally image sequences. */
91static Vector<std::pair<const Image *, std::string>> gather_cache_filepaths(
92 Main *bmain, const bool include_sequences)
93{
94 Vector<std::pair<const Image *, std::string>> filepaths;
95
96 for (const Image *image : gather_cache_images(bmain)) {
97 if (ELEM(image->source, IMA_SRC_MOVIE, IMA_SRC_GENERATED, IMA_SRC_VIEWER)) {
98 continue;
99 }
101 continue;
102 }
103 if (image->filepath[0] == '\0') {
104 continue;
105 }
106
107 /* Get regular absolute path. */
108 char filepath[FILE_MAX];
109 STRNCPY(filepath, image->filepath);
111 BLI_path_normalize(filepath);
112
113 /* Handle each UDIM tile. */
114 if (image->source == IMA_SRC_TILED) {
116 char *udim_pattern = BKE_image_get_tile_strformat(filepath, &tile_format);
117
118 if (tile_format != UDIM_TILE_FORMAT_NONE) {
119 for (const ImageTile &tile : image->tiles) {
120 char tile_filepath[FILE_MAX];
122 tile_filepath, udim_pattern, tile_format, tile.tile_number);
123 if (BLI_is_file(tile_filepath)) {
124 filepaths.append({image, tile_filepath});
125 }
126 }
127 MEM_delete(udim_pattern);
128 continue;
129 }
130 }
131
132 /* Handle each image sequence frame. */
133 if (image->source == IMA_SRC_SEQUENCE) {
134 if (include_sequences) {
135 BKE_bpath_sequence_filepaths_foreach(filepath, [&](StringRef frame_filepath) {
136 filepaths.append({image, frame_filepath});
137 });
138 }
139 continue;
140 }
141
142 /* Handle regular image. */
143 if (BLI_is_file(filepath)) {
144 filepaths.append({image, filepath});
145 }
146 }
147
148 return filepaths;
149}
150
152
153/* -------------------------------------------------------------------- */
156
157struct GenerateTextureCacheJob {
158 Main *bmain;
159 bool include_sequences;
160};
161
162struct GenerateTextureCacheUI {
163 int sequence_generate_num = 0;
164};
165
166static void generate_texture_cache(Main *bmain,
167 ReportList *reports,
168 const bool include_sequences,
169 wmJobWorkerStatus *worker_status = nullptr)
170{
171 const Vector<std::pair<const Image *, std::string>> filepaths = gather_cache_filepaths(
172 bmain, include_sequences);
173
174 /* Determine which texture cache files need to be generated. */
175 Set<std::string> found_tx;
176 int uptodate_num = 0;
177 Vector<std::pair<const Image *, std::string>> outdated;
178 for (const auto &item : filepaths) {
179 std::string tx_filepath;
180 const bool up_to_date = CCL_resolve_texture_cache(
181 item.first, item.second.c_str(), U.texture_cachedir, tx_filepath);
182 if (tx_filepath.empty() || !found_tx.add(tx_filepath)) {
183 continue;
184 }
185 if (up_to_date) {
186 uptodate_num++;
187 }
188 else {
189 outdated.append(item);
190 }
191 }
192
193 /* Generate texture cache. */
194 std::atomic<int> completed_num = 0;
195 std::atomic<int> failed_num = 0;
196 std::mutex reports_mutex;
197
198 blender::threading::parallel_for_each(outdated, [&](const auto &item) {
199 if (worker_status) {
200 if (G.is_break || worker_status->stop) {
201 return;
202 }
203 worker_status->progress = (completed_num + failed_num) / float(outdated.size());
204 worker_status->do_update = true;
205 }
206
207 if (CCL_generate_texture_cache(item.first, item.second.c_str(), U.texture_cachedir)) {
208 completed_num++;
209 }
210 else {
211 std::scoped_lock lock(reports_mutex);
213 reports, RPT_ERROR, "Failed to generate texture cache for: %s", item.second.c_str());
214 failed_num++;
215 }
216 });
217
218 /* Report stats. */
219 if (found_tx.is_empty()) {
220 BKE_report(reports, RPT_INFO, "No image files found to generate tx files");
221 }
222 else {
223 BKE_reportf(reports,
224 (failed_num) ? RPT_WARNING : RPT_INFO,
225 "Generated %d tx files, %d failed, %d up to date",
226 completed_num.load(),
227 failed_num.load(),
228 uptodate_num);
229 }
230}
231
232static void generate_texture_cache_startjob(void *customdata, wmJobWorkerStatus *worker_status)
233{
234 GenerateTextureCacheJob *job = static_cast<GenerateTextureCacheJob *>(customdata);
235 generate_texture_cache(
236 job->bmain, worker_status->reports, job->include_sequences, worker_status);
237}
238
239static wmOperatorStatus generate_texture_cache_exec(bContext *C, wmOperator *op)
240{
241 Main *bmain = CTX_data_main(C);
242 const bool include_sequences = RNA_boolean_get(op->ptr, "generate_sequences");
243
244 /* No dialog, execute immediately without job. */
245 if (op->customdata == nullptr) {
246 generate_texture_cache(bmain, op->reports, include_sequences);
247 return OPERATOR_FINISHED;
248 }
249
250 MEM_delete(static_cast<GenerateTextureCacheUI *>(op->customdata));
251 op->customdata = nullptr;
252
253 wmWindowManager *wm = CTX_wm_manager(C);
254 wmJob *wm_job = WM_jobs_get(wm,
256 bmain,
257 RPT_("Generating texture cache..."),
258 WM_JOB_PRIORITY | WM_JOB_PROGRESS,
259 WM_JOB_TYPE_GENERATE_TEXTURE_CACHE);
260
261 GenerateTextureCacheJob *job = MEM_new<GenerateTextureCacheJob>(__func__);
262 job->bmain = bmain;
263 job->include_sequences = include_sequences;
264 WM_jobs_customdata_set(wm_job, job, [](void *customdata) {
265 MEM_delete(static_cast<GenerateTextureCacheJob *>(customdata));
266 });
267
268 WM_jobs_timer(wm_job, 0.2, NC_WM | ND_JOB, 0);
269 WM_jobs_callbacks(wm_job, generate_texture_cache_startjob, nullptr, nullptr, nullptr);
270
271 G.is_break = false;
272 WM_jobs_start(wm, wm_job);
273
274 return OPERATOR_FINISHED;
275}
276
277static wmOperatorStatus generate_texture_cache_invoke(bContext *C,
278 wmOperator *op,
279 const wmEvent * /*event*/)
280{
281 Main *bmain = CTX_data_main(C);
282
283 /* Count files. */
284 Set<std::string> found_tx;
285 int regular_generate_num = 0;
286 int sequence_generate_num = 0;
287 int uptodate_num = 0;
288
289 const bool include_sequences = true;
290 const Vector<std::pair<const Image *, std::string>> filepaths = gather_cache_filepaths(
291 bmain, include_sequences);
292 for (const auto &item : filepaths) {
293 const bool is_sequence = item.first->source == IMA_SRC_SEQUENCE;
294 std::string tx_filepath;
295 const bool up_to_date = CCL_resolve_texture_cache(
296 item.first, item.second.c_str(), U.texture_cachedir, tx_filepath);
297 if (tx_filepath.empty() || !found_tx.add(tx_filepath)) {
298 continue;
299 }
300 if (up_to_date) {
301 uptodate_num++;
302 }
303 else if (is_sequence) {
304 sequence_generate_num++;
305 }
306 else {
307 regular_generate_num++;
308 }
309 }
310
311 /* Nothing to generate, just execute to show the message. */
312 if (regular_generate_num == 0 && sequence_generate_num == 0) {
313 return generate_texture_cache_exec(C, op);
314 }
315
316 /* Show confirmation dialog. */
317 std::string message;
318 if (uptodate_num > 0) {
319 message += fmt::format(fmt::runtime(IFACE_("{} tx files up to date")), uptodate_num);
320 }
321 if (regular_generate_num > 0) {
322 if (!message.empty()) {
323 message += '\n';
324 }
325 message += fmt::format(fmt::runtime(IFACE_("{} tx files to be generated")),
326 regular_generate_num);
327 }
328
329 GenerateTextureCacheUI *data = MEM_new<GenerateTextureCacheUI>(__func__);
330 data->sequence_generate_num = sequence_generate_num;
331 op->customdata = data;
332
334 op,
335 300,
336 IFACE_("Generate Texture Cache?"),
337 IFACE_("Generate"),
338 false,
339 std::move(message),
340 true);
341}
342
343static void generate_texture_cache_ui(bContext * /*C*/, wmOperator *op)
344{
345 ui::Layout &layout = *op->layout;
346 const GenerateTextureCacheUI *data = static_cast<GenerateTextureCacheUI *>(op->customdata);
347
348 if (data) {
349 if (data->sequence_generate_num > 0) {
350 const std::string label = fmt::format(
351 fmt::runtime(IFACE_("Include {} image sequence tx files")), data->sequence_generate_num);
352 layout.prop(op->ptr, "generate_sequences", UI_ITEM_NONE, label, ICON_NONE);
353 }
354 }
355 else {
356 /* Show unconditionally outside the confirmation dialog, like a redo panel. */
357 layout.prop(op->ptr, "generate_sequences", UI_ITEM_NONE, nullptr, ICON_NONE);
358 }
359}
360
361static void generate_texture_cache_cancel(bContext * /*C*/, wmOperator *op)
362{
363 if (op->customdata) {
364 MEM_delete(static_cast<GenerateTextureCacheUI *>(op->customdata));
365 op->customdata = nullptr;
366 }
367}
368
369void RENDER_OT_generate_texture_cache(wmOperatorType *ot)
370{
371 /* identifiers */
372 ot->name = "Generate Texture Cache";
373 ot->idname = "RENDER_OT_generate_texture_cache";
374 ot->description = "Generate Cycles texture cache files for all images used in shader nodes";
375
376 /* API callbacks. */
377 ot->exec = generate_texture_cache_exec;
378 ot->invoke = generate_texture_cache_invoke;
379 ot->ui = generate_texture_cache_ui;
380 ot->cancel = generate_texture_cache_cancel;
381
382 /* flags */
383 ot->flag = OPTYPE_REGISTER;
384
385 /* properties */
386 RNA_def_boolean(ot->srna,
387 "generate_sequences",
388 true,
389 "Image Sequences",
390 "Generate texture cache files for all frames of image sequences");
391}
392
394
395/* -------------------------------------------------------------------- */
398
399static Set<std::string> gather_clear_tx_files(Main *bmain)
400{
401 Set<std::string> tx_files;
402
403 const bool include_sequences = true;
404 const Vector<std::pair<const Image *, std::string>> filepaths = gather_cache_filepaths(
405 bmain, include_sequences);
406
407 Set<std::string> source_filepaths;
408 for (const auto &item : filepaths) {
409 source_filepaths.add(item.second);
410 }
411
412 for (const auto &item : filepaths) {
414 item.second.c_str(), U.texture_cachedir, [&](StringRef cache_filepath) {
415 /* Defensive check just in case a tx file was referenced directly even
416 * though this should not be done. */
417 if (source_filepaths.contains(cache_filepath)) {
418 return;
419 }
420 tx_files.add(cache_filepath);
421 });
422 }
423
424 return tx_files;
425}
426
427static wmOperatorStatus clear_texture_cache_exec(bContext *C, wmOperator *op)
428{
429 Main *bmain = CTX_data_main(C);
430
431 const Set<std::string> tx_files = gather_clear_tx_files(bmain);
432
433 int deleted_num = 0;
434 for (const std::string &tx_filepath : tx_files) {
435 if (BLI_delete(tx_filepath.c_str(), false, false) == 0) {
436 deleted_num++;
437 }
438 else {
439 BKE_reportf(op->reports, RPT_ERROR, "Failed to delete tx file: %s", tx_filepath.c_str());
440 }
441 }
442
443 BKE_reportf(op->reports, RPT_INFO, "Deleted %d tx files", deleted_num);
444 return OPERATOR_FINISHED;
445}
446
447static wmOperatorStatus clear_texture_cache_invoke(bContext *C,
448 wmOperator *op,
449 const wmEvent * /*event*/)
450{
451 Main *bmain = CTX_data_main(C);
452 const int used_num = gather_clear_tx_files(bmain).size();
453
454 if (used_num == 0) {
455 BKE_report(op->reports, RPT_INFO, "No tx files to delete");
456 return OPERATOR_CANCELLED;
457 }
458
459 const std::string message = fmt::format(fmt::runtime(IFACE_("{} tx files to be deleted")),
460 used_num);
461
463 op,
464 IFACE_("Clear Texture Cache?"),
465 message.c_str(),
466 IFACE_("Clear Cache"),
467 ui::AlertIcon::Info,
468 false);
469}
470
471void RENDER_OT_clear_texture_cache(wmOperatorType *ot)
472{
473 /* identifiers */
474 ot->name = "Clear Texture Cache";
475 ot->idname = "RENDER_OT_clear_texture_cache";
476 ot->description = "Delete Cycles texture cache files from disk";
477
478 /* API callbacks. */
479 ot->exec = clear_texture_cache_exec;
480 ot->invoke = clear_texture_cache_invoke;
481
482 /* flags */
483 ot->flag = OPTYPE_REGISTER;
484}
485
487
488} // namespace blender
489
490#endif
#define FOREACH_NODETREE_END
Definition BKE_node.hh:936
#define FOREACH_NODETREE_BEGIN(bmain, _nodetree, _id)
Definition BKE_node.hh:926
File and directory operations.
#define FILE_MAX
#define RPT_(msgid)
#define IFACE_(msgid)
#define ID_BLEND_PATH_FROM_GLOBAL(_id)
Definition DNA_ID.h:691
Read Blender memory allocation and freeing.
#define C
Definition RandGen.cpp:29
#define UI_ITEM_NONE
#define ND_JOB
Definition WM_types.hh:432
#define NC_WM
Definition WM_types.hh:387
volatile int lock
#define U
#define ELEM(...)
#define image(slot, qualifiers, format)
const ccl_global KernelWorkTile * tile
#define G(x, y, z)
void parallel_for_each(Range &&range, const Function &function)
Definition BLI_task.hh:31
BMesh Iterators.
char * BKE_image_get_tile_strformat(const char *filepath, eUDIM_TILE_FORMAT *r_tile_format)
char * STRNCPY(char(&dst)[N], const char *src)
void BKE_image_texture_cache_filepaths_foreach(const char *source_filepath_abs, const char *cache_dir, blender::FunctionRef< void(blender::StringRef cache_filepath)> callback)
void WM_jobs_callbacks(wmJob *wm_job, wm_jobs_start_callback startjob, void(*initjob)(void *), void(*update)(void *), void(*endjob)(void *))
Definition wm_jobs.cc:407
bool CCL_resolve_texture_cache(const Image *image, const char *filepath, const char *texture_cache_directory, std::string &r_tx_filepath)
bool BLI_path_abs(char path[FILE_MAX], const char *basepath) ATTR_NONNULL(1
void RENDER_OT_generate_texture_cache(wmOperatorType *ot)
wmWindow * CTX_wm_window(const bContext *C)
void WM_jobs_customdata_set(wmJob *wm_job, void *customdata, void(*free)(void *customdata))
Definition wm_jobs.cc:377
void WM_jobs_start(wmWindowManager *wm, wmJob *wm_job)
Definition wm_jobs.cc:498
void BKE_image_set_filepath_from_tile_number(char *filepath, const char *pattern, eUDIM_TILE_FORMAT tile_format, int tile_number)
void BKE_report(ReportList *reports, eReportType type, const char *message)
Definition report.cc:162
BMesh const char void * data
wmWindowManager * CTX_wm_manager(const bContext *C)
int BLI_delete(const char *path, bool dir, bool recursive) ATTR_NONNULL()
int BLI_path_normalize(char *path) ATTR_NONNULL(1)
wmOperatorStatus WM_operator_props_dialog_popup(bContext *C, wmOperator *op, int width, std::optional< std::string > title, std::optional< std::string > confirm_text, const bool cancel_default, std::optional< std::string > message, const bool show_icon)
bool RNA_boolean_get(PointerRNA *ptr, const char *name)
wmOperatorStatus WM_operator_confirm_ex(bContext *C, wmOperator *op, const char *title, const char *message, const char *confirm_text, ui::AlertIcon icon, bool cancel_default)
bool CCL_generate_texture_cache(const Image *image, const char *filepath, const char *texture_cache_directory="")
void BKE_bpath_sequence_filepaths_foreach(const char *abs_filepath, blender::FunctionRef< void(blender::StringRef frame_filepath)> callback)
void RENDER_OT_clear_texture_cache(wmOperatorType *ot)
void BKE_reportf(ReportList *reports, eReportType type, const char *format,...) ATTR_PRINTF_FORMAT(3
Definition report.cc:191
Main * CTX_data_main(const bContext *C)
wmJob * WM_jobs_get(wmWindowManager *wm, wmWindow *win, const void *owner, const char *name, const eWM_JobFlag flag, const eWM_JobType job_type)
Definition wm_jobs.cc:228
@ OPTYPE_REGISTER
Definition WM_types.hh:188
bool BKE_image_has_packedfile(const Image *image)
PropertyRNA * RNA_def_boolean(StructOrFunctionRNA *cont_, const char *identifier, const bool default_value, const char *ui_name, const char *ui_description)
bool BLI_is_file(const char *path) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
Definition storage.cc:469
void BKE_library_foreach_ID_link(Main *bmain, ID *id, FunctionRef< LibraryIDLinkCallback > callback, void *user_data, LibraryForeachIDFlag flag)
Definition lib_query.cc:449
void WM_jobs_timer(wmJob *wm_job, double time_step, uint note, uint endnote, void(*timer_step)(void *))
Definition wm_jobs.cc:393
eUDIM_TILE_FORMAT
Definition BKE_image.hh:505
@ UDIM_TILE_FORMAT_NONE
Definition BKE_image.hh:506