Blender V4.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.h"
9#include "BLI_listbase.h"
10#include "BLI_map.hh"
12#include "BLI_string.h"
13#include "BLI_utildefines.h"
14
15#include "MEM_guardedalloc.h"
16
17#include "IMB_imbuf.hh"
18#include "IMB_imbuf_types.hh"
19
20#include "DNA_scene_types.h"
22
23#include "BKE_image.hh"
24#include "BKE_image_save.hh"
25#include "BKE_report.hh"
26
27#include "RE_pipeline.h"
28
29#include "COM_render_context.hh"
30
32
33/* ------------------------------------------------------------------------------------------------
34 * File Output
35 */
36
37FileOutput::FileOutput(const std::string &path,
39 int2 size,
40 bool save_as_render)
41 : path_(path), format_(format), save_as_render_(save_as_render)
42{
43 render_result_ = MEM_cnew<RenderResult>("Temporary Render Result For File Output");
44
45 render_result_->rectx = size.x;
46 render_result_->recty = size.y;
47
48 /* File outputs are always single layer, as images are actually stored in passes on that single
49 * layer. Create a single unnamed layer to add the passes to. A single unnamed layer is treated
50 * by the EXR writer as a special case where the channel names take the form:
51 * <pass-name>.<view-name>.<channel-id>
52 * Otherwise, the layer name would have preceded in the pass name in yet another section. */
53 RenderLayer *render_layer = MEM_cnew<RenderLayer>("Render Layer For File Output.");
54 BLI_addtail(&render_result_->layers, render_layer);
55 render_layer->name[0] = '\0';
56
57 /* File outputs do not support previews. */
58 format_.flag &= ~R_IMF_FLAG_PREVIEW_JPG;
59}
60
62{
63 RE_FreeRenderResult(render_result_);
64}
65
66void FileOutput::add_view(const char *view_name)
67{
68 /* Empty views can only be added for EXR images. */
70
71 RenderView *render_view = MEM_cnew<RenderView>("Render View For File Output.");
72 BLI_addtail(&render_result_->views, render_view);
73 STRNCPY(render_view->name, view_name);
74}
75
76void FileOutput::add_view(const char *view_name, int channels, float *buffer)
77{
78 RenderView *render_view = MEM_cnew<RenderView>("Render View For File Output.");
79 BLI_addtail(&render_result_->views, render_view);
80 STRNCPY(render_view->name, view_name);
81
82 render_view->ibuf = IMB_allocImBuf(
83 render_result_->rectx, render_result_->recty, channels * 8, 0);
84 render_view->ibuf->channels = channels;
85 IMB_assign_float_buffer(render_view->ibuf, buffer, IB_TAKE_OWNERSHIP);
86}
87
88void FileOutput::add_pass(const char *pass_name,
89 const char *view_name,
90 const char *channels,
91 float *buffer)
92{
93 /* Passes can only be added for EXR images. */
95
96 RenderLayer *render_layer = static_cast<RenderLayer *>(render_result_->layers.first);
97 RenderPass *render_pass = MEM_cnew<RenderPass>("Render Pass For File Output.");
98 BLI_addtail(&render_layer->passes, render_pass);
99 STRNCPY(render_pass->name, pass_name);
100 STRNCPY(render_pass->view, view_name);
101 STRNCPY(render_pass->chan_id, channels);
102
103 const int channels_count = BLI_strnlen(channels, 4);
104 render_pass->rectx = render_result_->rectx;
105 render_pass->recty = render_result_->recty;
106 render_pass->channels = channels_count;
107
108 render_pass->ibuf = IMB_allocImBuf(
109 render_result_->rectx, render_result_->recty, channels_count * 8, 0);
110 render_pass->ibuf->channels = channels_count;
111 IMB_assign_float_buffer(render_pass->ibuf, buffer, IB_TAKE_OWNERSHIP);
112}
113
114void FileOutput::add_meta_data(std::string key, std::string value)
115{
116 meta_data_.add(key, value);
117}
118
120{
121 ReportList reports;
122 BKE_reports_init(&reports, RPT_STORE);
123
124 /* Add scene stamp data as meta data as well as the custom meta data. */
125 BKE_render_result_stamp_info(scene, nullptr, render_result_, false);
126 for (const auto &field : meta_data_.items()) {
127 BKE_render_result_stamp_data(render_result_, field.key.c_str(), field.value.c_str());
128 }
129
131 &reports, render_result_, scene, true, path_.c_str(), &format_, save_as_render_);
132
133 BKE_reports_free(&reports);
134}
135
136/* ------------------------------------------------------------------------------------------------
137 * Render Context
138 */
139
142 int2 size,
143 bool save_as_render)
144{
145 return *file_outputs_.lookup_or_add_cb(
146 path, [&]() { return std::make_unique<FileOutput>(path, format, size, save_as_render); });
147}
148
150{
151 for (std::unique_ptr<FileOutput> &file_output : file_outputs_.values()) {
152 file_output->save(scene);
153 }
154}
155
156} // namespace blender::realtime_compositor
void BKE_render_result_stamp_info(Scene *scene, Object *camera, RenderResult *rr, bool allocate_only)
void BKE_render_result_stamp_data(RenderResult *rr, const char *key, const char *value)
bool BKE_image_render_write(ReportList *reports, RenderResult *rr, const Scene *scene, const bool stamp, const char *filepath_basis, const ImageFormatData *format=nullptr, bool save_as_render=true)
void BKE_reports_free(ReportList *reports)
Definition report.cc:69
void BKE_reports_init(ReportList *reports, int flag)
Definition report.cc:54
#define BLI_assert(a)
Definition BLI_assert.h:50
void BLI_addtail(struct ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition listbase.cc:110
#define STRNCPY(dst, src)
Definition BLI_string.h:593
int char char int int int int size_t BLI_strnlen(const char *str, size_t maxlen) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
Definition string.c:909
#define ELEM(...)
@ R_IMF_IMTYPE_OPENEXR
@ R_IMF_IMTYPE_MULTILAYER
void IMB_assign_float_buffer(ImBuf *ibuf, float *buffer_data, ImBufOwnership ownership)
Contains defines and structs used throughout the imbuf module.
@ IB_TAKE_OWNERSHIP
Read Guarded memory(de)allocation.
Group Output data from inside of a node group A color picker Mix two input colors RGB to Convert a color s luminance to a grayscale value Generate a normal vector and a dot product Brightness Control the brightness and contrast of the input color Vector Map input vector components with curves Camera Retrieve information about the camera and how it relates to the current shading point s position Clamp a value between a minimum and a maximum Vector Perform vector math operation Invert Invert a producing a negative Combine Generate a color from its and blue channels(Deprecated)") DefNode(ShaderNode
bool add(const Key &key, const Value &value)
Definition BLI_map.hh:271
ItemIterator items() const
Definition BLI_map.hh:864
FileOutput(const std::string &path, const ImageFormatData &format, int2 size, bool save_as_render)
void add_view(const char *view_name)
void add_pass(const char *pass_name, const char *view_name, const char *channels, float *buffer)
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)
struct ImBuf * IMB_allocImBuf(unsigned int, unsigned int, unsigned char, unsigned int)
format
void RE_FreeRenderResult(RenderResult *rr)
void * first
ListBase passes
Definition RE_pipeline.h:97
char name[RE_MAXNAME]
Definition RE_pipeline.h:89
struct ImBuf * ibuf
Definition RE_pipeline.h:68
char chan_id[8]
Definition RE_pipeline.h:59
char name[64]
Definition RE_pipeline.h:58
char view[64]
Definition RE_pipeline.h:73
ListBase views
ListBase layers
struct ImBuf * ibuf
Definition RE_pipeline.h:52
char name[64]
Definition RE_pipeline.h:47