Blender V5.3
render_context.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
5#include <memory>
6#include <string>
7
8#include "BLI_assert.hh"
9#include "BLI_listbase.hh"
10#include "BLI_map.hh"
11#include "BLI_math_vector_c.hh"
13#include "BLI_string.hh"
14#include "BLI_string_utf8.hh"
15#include "BLI_utildefines.hh"
16
17#include "MEM_guardedalloc.h"
18
19#include "IMB_imbuf.hh"
20#include "IMB_imbuf_types.hh"
21
22#include "DNA_scene_types.h"
24
25#include "BKE_image.hh"
26#include "BKE_image_save.hh"
27#include "BKE_report.hh"
28#include "BKE_scene.hh"
29
30#include "RE_pipeline.h"
31
32#include "COM_render_context.hh"
33#include "COM_result.hh"
34
35namespace blender::compositor {
36
37/* ------------------------------------------------------------------------------------------------
38 * File Output
39 */
40
41FileOutput::FileOutput(const std::string &path,
42 const ImageFormatData &format,
43 int2 size,
44 bool save_as_render)
45 : path_(path), format_(format), save_as_render_(save_as_render)
46{
47 render_result_ = MEM_new<RenderResult>("Temporary Render Result For File Output");
48
49 render_result_->rectx = size.x;
50 render_result_->recty = size.y;
51
52 /* NOTE: set dummy values which will won't be used unless overwritten.
53 * When `save_as_render` is set, this is overwritten by the scenes PPM setting.
54 * We *could* support setting the DPI in the file output node too. */
55 render_result_->ppm[0] = 0.0;
56 render_result_->ppm[1] = 0.0;
57
58 /* File outputs are always single layer, as images are actually stored in passes on that single
59 * layer. Create a single unnamed layer to add the passes to. A single unnamed layer is treated
60 * by the EXR writer as a special case where the channel names take the form:
61 * <pass-name>.<view-name>.<channel-id>
62 * Otherwise, the layer name would have preceded in the pass name in yet another section. */
63 RenderLayer *render_layer = MEM_new<RenderLayer>("Render Layer For File Output.");
64 BLI_addtail(&render_result_->layers, render_layer);
65 render_layer->name[0] = '\0';
66
67 /* File outputs do not support previews. */
68 format_.flag &= ~R_IMF_FLAG_PREVIEW_JPG;
69}
70
72{
73 RE_FreeRenderResult(render_result_);
74}
75
76void FileOutput::add_view(const char *view_name)
77{
78 /* Empty views can only be added for EXR images. */
80
81 RenderView *render_view = MEM_new<RenderView>("Render View For File Output.");
82 BLI_addtail(&render_result_->views, render_view);
83 STRNCPY_UTF8(render_view->name, view_name);
84}
85
86void FileOutput::add_view(const char *view_name, const Result &data)
87{
88 RenderView *render_view = MEM_new<RenderView>("Render View For File Output.");
89 BLI_addtail(&render_result_->views, render_view);
90 STRNCPY_UTF8(render_view->name, view_name);
91
92 ImColorMode color_mode = ImColorMode::RGBA;
93 if (data.channels_count() == 1) {
94 color_mode = ImColorMode::BW;
95 }
96 else if (data.channels_count() == 3) {
97 color_mode = ImColorMode::RGB;
98 }
99 render_view->ibuf = IMB_allocImBuf(UNPACK2(data.domain().data_size), ImBufFlags::Zero);
100 render_view->ibuf->color_mode = color_mode;
101 if (data.sharing_info()) {
102 render_view->ibuf->channels = data.channels_count();
103 render_view->ibuf->float_buffer = ImBufFloatBuffer{
104 .data = static_cast<const float *>(data.cpu_data().data()),
105 .sharing_info = data.sharing_info(),
106 .colorspace = nullptr};
107 }
108 else if (data.cpu_data().data() != render_view->ibuf->float_data()) {
109 IMB_alloc_float_pixels(render_view->ibuf, data.channels_count(), false);
110 std::memcpy(
111 render_view->ibuf->float_data_for_write(), data.cpu_data().data(), data.size_in_bytes());
112 }
113}
114
115void FileOutput::add_pass(const char *pass_name,
116 const char *view_name,
117 const char *channels,
118 const Result &data)
119{
120 /* Passes can only be added for EXR images. */
122
123 RenderLayer *render_layer = render_result_->layers.first();
124 RenderPass *render_pass = MEM_new<RenderPass>("Render Pass For File Output.");
125 BLI_addtail(&render_layer->passes, render_pass);
126 STRNCPY(render_pass->name, pass_name);
127 STRNCPY(render_pass->view, view_name);
128 STRNCPY(render_pass->chan_id, channels);
129
130 render_pass->rectx = data.domain().data_size.x;
131 render_pass->recty = data.domain().data_size.y;
132 render_pass->channels = data.channels_count();
133
134 ImColorMode color_mode = ImColorMode::RGBA;
135 if (render_pass->channels == 1) {
136 color_mode = ImColorMode::BW;
137 }
138 else if (render_pass->channels == 3) {
139 color_mode = ImColorMode::RGB;
140 }
141 render_pass->ibuf = IMB_allocImBuf(UNPACK2(data.domain().data_size), ImBufFlags::Zero);
142 render_pass->ibuf->color_mode = color_mode;
143 if (data.sharing_info()) {
144 render_pass->ibuf->channels = data.channels_count();
145 render_pass->ibuf->float_buffer = ImBufFloatBuffer{
146 .data = static_cast<const float *>(data.cpu_data().data()),
147 .sharing_info = data.sharing_info(),
148 .colorspace = nullptr};
149 }
150 else if (data.cpu_data().data() != render_pass->ibuf->float_data()) {
151 IMB_alloc_float_pixels(render_pass->ibuf, data.channels_count(), false);
152 std::memcpy(
153 render_pass->ibuf->float_data_for_write(), data.cpu_data().data(), data.size_in_bytes());
154 }
155 copy_v2_v2_db(render_pass->ibuf->ppm, render_result_->ppm);
156}
157
158void FileOutput::add_meta_data(std::string key, std::string value)
159{
160 meta_data_.add(key, value);
161}
162
164{
165 ReportList reports;
166 BKE_reports_init(&reports, RPT_STORE);
167
168 /* Add scene stamp data as meta data as well as the custom meta data. */
169 BKE_render_result_stamp_info(scene, nullptr, render_result_, false);
170 for (const auto &field : meta_data_.items()) {
171 BKE_render_result_stamp_data(render_result_, field.key.c_str(), field.value.c_str());
172 }
173
174 /* NOTE: without this the file will be written without any density information.
175 * So always write this. */
176 if (save_as_render_ || true) {
177 BKE_scene_ppm_get(&scene->r, render_result_->ppm);
178 }
179
181 &reports, render_result_, scene, true, path_.c_str(), &format_, save_as_render_);
182
183 BKE_reports_free(&reports);
184}
185
186/* ------------------------------------------------------------------------------------------------
187 * Render Context
188 */
189
191 ImageFormatData format,
192 int2 size,
193 bool save_as_render)
194{
195 return *file_outputs_.lookup_or_add_cb(
196 path, [&]() { return std::make_unique<FileOutput>(path, format, size, save_as_render); });
197}
198
200{
201 for (std::unique_ptr<FileOutput> &file_output : file_outputs_.values()) {
202 file_output->save(scene);
203 }
204}
205
206} // namespace blender::compositor
#define STRNCPY_UTF8(dst, src)
Read Blender memory allocation and freeing.
#define UNPACK2(a)
#define ELEM(...)
void add_view(const char *view_name)
FileOutput(const std::string &path, const ImageFormatData &format, int2 size, bool save_as_render)
void add_pass(const char *pass_name, const char *view_name, const char *channels, const Result &data)
void add_meta_data(std::string key, std::string value)
FileOutput & get_file_output(std::string path, ImageFormatData format, int2 size, bool save_as_render)
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
char * STRNCPY(char(&dst)[N], const char *src)
bool BKE_image_render_write(ReportList *reports, RenderResult *rr, const Scene *scene, bool stamp, const char *filepath_basis, const ImageFormatData *format=nullptr, bool save_as_render=true)
void BKE_reports_free(ReportList *reports)
Definition report.cc:106
void BKE_render_result_stamp_info(Scene *scene, Object *camera, RenderResult *rr, bool allocate_only)
void BKE_reports_init(ReportList *reports, int flag)
Definition report.cc:91
void BKE_render_result_stamp_data(RenderResult *rr, const char *key, const char *value)
@ R_IMF_FLAG_PREVIEW_JPG
void RE_FreeRenderResult(RenderResult *rr)
void BKE_scene_ppm_get(const RenderData *rd, double r_ppm[2])
Definition scene.cc:3487
@ R_IMF_IMTYPE_MULTILAYER
BMesh const char void * data
MINLINE void copy_v2_v2_db(double r[2], const double a[2])
ImBuf * IMB_allocImBuf(unsigned int x, unsigned int y, ImBufFlags flags)
void BLI_addtail(ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition listbase.cc:118
bool IMB_alloc_float_pixels(ImBuf *ibuf, unsigned int channels, bool initialize_pixels=true)
ImBufFloatBuffer float_buffer
const float * float_data() const
ImColorMode color_mode
float * float_data_for_write()
Definition allocimbuf.cc:79
ListBaseT< RenderPass > passes
Definition RE_pipeline.h:99
char name[RE_MAXNAME]
Definition RE_pipeline.h:94
struct ImBuf * ibuf
Definition RE_pipeline.h:72
struct ImBuf * ibuf
Definition RE_pipeline.h:54
struct RenderData r