13# include <fmt/format.h>
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);
91static Vector<std::pair<const Image *, std::string>> gather_cache_filepaths(
92 Main *bmain,
const bool include_sequences)
94 Vector<std::pair<const Image *, std::string>> filepaths;
96 for (
const Image *
image : gather_cache_images(bmain)) {
97 if (
ELEM(
image->source, IMA_SRC_MOVIE, IMA_SRC_GENERATED, IMA_SRC_VIEWER)) {
103 if (
image->filepath[0] ==
'\0') {
114 if (
image->source == IMA_SRC_TILED) {
118 if (tile_format != UDIM_TILE_FORMAT_NONE) {
119 for (
const ImageTile &
tile :
image->tiles) {
122 tile_filepath, udim_pattern, tile_format,
tile.tile_number);
124 filepaths.append({
image, tile_filepath});
127 MEM_delete(udim_pattern);
133 if (
image->source == IMA_SRC_SEQUENCE) {
134 if (include_sequences) {
136 filepaths.append({
image, frame_filepath});
144 filepaths.append({
image, filepath});
157struct GenerateTextureCacheJob {
159 bool include_sequences;
162struct GenerateTextureCacheUI {
163 int sequence_generate_num = 0;
166static void generate_texture_cache(Main *bmain,
168 const bool include_sequences,
169 wmJobWorkerStatus *worker_status =
nullptr)
171 const Vector<std::pair<const Image *, std::string>> filepaths = gather_cache_filepaths(
172 bmain, include_sequences);
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;
181 item.first, item.second.c_str(),
U.texture_cachedir, tx_filepath);
182 if (tx_filepath.empty() || !found_tx.add(tx_filepath)) {
189 outdated.append(item);
194 std::atomic<int> completed_num = 0;
195 std::atomic<int> failed_num = 0;
196 std::mutex reports_mutex;
200 if (
G.is_break || worker_status->stop) {
203 worker_status->progress = (completed_num + failed_num) /
float(outdated.size());
204 worker_status->do_update =
true;
211 std::scoped_lock
lock(reports_mutex);
213 reports, RPT_ERROR,
"Failed to generate texture cache for: %s", item.second.c_str());
219 if (found_tx.is_empty()) {
220 BKE_report(reports, RPT_INFO,
"No image files found to generate tx files");
224 (failed_num) ? RPT_WARNING : RPT_INFO,
225 "Generated %d tx files, %d failed, %d up to date",
226 completed_num.load(),
232static void generate_texture_cache_startjob(
void *customdata, wmJobWorkerStatus *worker_status)
234 GenerateTextureCacheJob *job =
static_cast<GenerateTextureCacheJob *
>(customdata);
235 generate_texture_cache(
236 job->bmain, worker_status->reports, job->include_sequences, worker_status);
242 const bool include_sequences =
RNA_boolean_get(op->ptr,
"generate_sequences");
245 if (op->customdata ==
nullptr) {
246 generate_texture_cache(bmain, op->reports, include_sequences);
250 MEM_delete(
static_cast<GenerateTextureCacheUI *
>(op->customdata));
251 op->customdata =
nullptr;
257 RPT_(
"Generating texture cache..."),
258 WM_JOB_PRIORITY | WM_JOB_PROGRESS,
259 WM_JOB_TYPE_GENERATE_TEXTURE_CACHE);
261 GenerateTextureCacheJob *job = MEM_new<GenerateTextureCacheJob>(__func__);
263 job->include_sequences = include_sequences;
265 MEM_delete(
static_cast<GenerateTextureCacheJob *
>(customdata));
269 WM_jobs_callbacks(wm_job, generate_texture_cache_startjob,
nullptr,
nullptr,
nullptr);
284 Set<std::string> found_tx;
285 int regular_generate_num = 0;
286 int sequence_generate_num = 0;
287 int uptodate_num = 0;
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) {
294 std::string tx_filepath;
296 item.first, item.second.c_str(),
U.texture_cachedir, tx_filepath);
297 if (tx_filepath.empty() || !found_tx.add(tx_filepath)) {
303 else if (is_sequence) {
304 sequence_generate_num++;
307 regular_generate_num++;
312 if (regular_generate_num == 0 && sequence_generate_num == 0) {
313 return generate_texture_cache_exec(
C, op);
318 if (uptodate_num > 0) {
319 message += fmt::format(fmt::runtime(
IFACE_(
"{} tx files up to date")), uptodate_num);
321 if (regular_generate_num > 0) {
322 if (!message.empty()) {
325 message += fmt::format(fmt::runtime(
IFACE_(
"{} tx files to be generated")),
326 regular_generate_num);
329 GenerateTextureCacheUI *
data = MEM_new<GenerateTextureCacheUI>(__func__);
330 data->sequence_generate_num = sequence_generate_num;
331 op->customdata =
data;
336 IFACE_(
"Generate Texture Cache?"),
343static void generate_texture_cache_ui(bContext * , wmOperator *op)
345 ui::Layout &layout = *op->layout;
346 const GenerateTextureCacheUI *
data =
static_cast<GenerateTextureCacheUI *
>(op->customdata);
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);
357 layout.prop(op->ptr,
"generate_sequences",
UI_ITEM_NONE,
nullptr, ICON_NONE);
361static void generate_texture_cache_cancel(bContext * , wmOperator *op)
363 if (op->customdata) {
364 MEM_delete(
static_cast<GenerateTextureCacheUI *
>(op->customdata));
365 op->customdata =
nullptr;
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";
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;
387 "generate_sequences",
390 "Generate texture cache files for all frames of image sequences");
399static Set<std::string> gather_clear_tx_files(Main *bmain)
401 Set<std::string> tx_files;
403 const bool include_sequences =
true;
404 const Vector<std::pair<const Image *, std::string>> filepaths = gather_cache_filepaths(
405 bmain, include_sequences);
407 Set<std::string> source_filepaths;
408 for (
const auto &item : filepaths) {
409 source_filepaths.add(item.second);
412 for (
const auto &item : filepaths) {
414 item.second.c_str(),
U.texture_cachedir, [&](StringRef cache_filepath) {
417 if (source_filepaths.contains(cache_filepath)) {
420 tx_files.add(cache_filepath);
431 const Set<std::string> tx_files = gather_clear_tx_files(bmain);
434 for (
const std::string &tx_filepath : tx_files) {
435 if (
BLI_delete(tx_filepath.c_str(),
false,
false) == 0) {
439 BKE_reportf(op->reports, RPT_ERROR,
"Failed to delete tx file: %s", tx_filepath.c_str());
443 BKE_reportf(op->reports, RPT_INFO,
"Deleted %d tx files", deleted_num);
452 const int used_num = gather_clear_tx_files(bmain).size();
455 BKE_report(op->reports, RPT_INFO,
"No tx files to delete");
459 const std::string message = fmt::format(fmt::runtime(
IFACE_(
"{} tx files to be deleted")),
464 IFACE_(
"Clear Texture Cache?"),
474 ot->name =
"Clear Texture Cache";
475 ot->idname =
"RENDER_OT_clear_texture_cache";
476 ot->description =
"Delete Cycles texture cache files from disk";
479 ot->exec = clear_texture_cache_exec;
480 ot->invoke = clear_texture_cache_invoke;
#define FOREACH_NODETREE_END
#define FOREACH_NODETREE_BEGIN(bmain, _nodetree, _id)
File and directory operations.
#define ID_BLEND_PATH_FROM_GLOBAL(_id)
Read Blender memory allocation and freeing.
#define image(slot, qualifiers, format)
const ccl_global KernelWorkTile * tile
void parallel_for_each(Range &&range, const Function &function)
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 *))
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))
void WM_jobs_start(wmWindowManager *wm, wmJob *wm_job)
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)
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
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)
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()
void BKE_library_foreach_ID_link(Main *bmain, ID *id, FunctionRef< LibraryIDLinkCallback > callback, void *user_data, LibraryForeachIDFlag flag)
void WM_jobs_timer(wmJob *wm_job, double time_step, uint note, uint endnote, void(*timer_step)(void *))