Blender  V3.3
session/tile.h
Go to the documentation of this file.
1 /* SPDX-License-Identifier: Apache-2.0
2  * Copyright 2011-2022 Blender Foundation */
3 
4 #pragma once
5 
6 #include "session/buffers.h"
7 #include "util/image.h"
8 #include "util/string.h"
9 #include "util/unique_ptr.h"
10 
12 
13 class DenoiseParams;
14 class Scene;
15 
16 /* --------------------------------------------------------------------
17  * Tile.
18  */
19 
20 class Tile {
21  public:
22  int x = 0, y = 0;
23  int width = 0, height = 0;
24 
25  int window_x = 0, window_y = 0;
27 
28  Tile()
29  {
30  }
31 };
32 
33 /* --------------------------------------------------------------------
34  * Tile Manager.
35  */
36 
37 class TileManager {
38  public:
39  /* This callback is invoked by whenever on-dist tiles storage file is closed after writing. */
40  function<void(string_view)> full_buffer_written_cb;
41 
42  TileManager();
43  ~TileManager();
44 
45  TileManager(const TileManager &other) = delete;
46  TileManager(TileManager &&other) noexcept = delete;
47  TileManager &operator=(const TileManager &other) = delete;
48  TileManager &operator=(TileManager &&other) = delete;
49 
50  /* Reset current progress and start new rendering of the full-frame parameters in tiles of the
51  * given size.
52  * Only touches scheduling-related state of the tile manager. */
53  /* TODO(sergey): Consider using tile area instead of exact size to help dealing with extreme
54  * cases of stretched renders. */
55  void reset_scheduling(const BufferParams &params, int2 tile_size);
56 
57  /* Update for the known buffer passes and scene parameters.
58  * Will store all parameters needed for buffers access outside of the scene graph. */
59  void update(const BufferParams &params, const Scene *scene);
60 
61  void set_temp_dir(const string &temp_dir);
62 
63  inline int get_num_tiles() const
64  {
65  return tile_state_.num_tiles;
66  }
67 
68  inline bool has_multiple_tiles() const
69  {
70  return tile_state_.num_tiles > 1;
71  }
72 
73  inline int get_tile_overscan() const
74  {
75  return overscan_;
76  }
77 
78  bool next();
79  bool done();
80 
81  const Tile &get_current_tile() const;
82  const int2 get_size() const;
83 
84  /* Write render buffer of a tile to a file on disk.
85  *
86  * Opens file for write when first tile is written.
87  *
88  * Returns true on success. */
89  bool write_tile(const RenderBuffers &tile_buffers);
90 
91  /* Inform the tile manager that no more tiles will be written to disk.
92  * The file will be considered final, all handles to it will be closed. */
93  void finish_write_tiles();
94 
95  /* Check whether any tile has been written to disk. */
96  inline bool has_written_tiles() const
97  {
98  return write_state_.num_tiles_written != 0;
99  }
100 
101  /* Read full frame render buffer from tiles file on disk.
102  *
103  * Returns true on success. */
104  bool read_full_buffer_from_disk(string_view filename,
105  RenderBuffers *buffers,
106  DenoiseParams *denoise_params);
107 
108  /* Compute valid tile size compatible with image saving. */
109  int compute_render_tile_size(const int suggested_tile_size) const;
110 
111  /* Tile size in the image file. */
112  static const int IMAGE_TILE_SIZE = 128;
113 
114  /* Maximum supported tile size.
115  * Needs to be safe from allocation on a GPU point of view: the display driver needs to be able
116  * to allocate texture with the side size of this value.
117  * Use conservative value which is safe for most of OpenGL drivers and GPUs. */
118  static const int MAX_TILE_SIZE = 8192;
119 
120  protected:
121  /* Get tile configuration for its index.
122  * The tile index must be within [0, state_.tile_state_). */
123  Tile get_tile_for_index(int index) const;
124 
125  bool open_tile_output();
126  bool close_tile_output();
127 
128  string temp_dir_;
129 
130  /* Part of an on-disk tile file name which avoids conflicts between several Cycles instances or
131  * several sessions. */
133 
135 
136  /* Number of extra pixels around the actual tile to render. */
137  int overscan_ = 0;
138 
140 
141  /* Tile scheduling state. */
142  struct {
143  int num_tiles_x = 0;
144  int num_tiles_y = 0;
145  int num_tiles = 0;
146 
148 
151 
152  /* State of tiles writing to a file on disk. */
153  struct {
154  /* Index of a tile file used during the current session.
155  * This number is used for the file name construction, making it possible to render several
156  * scenes throughout duration of the session and keep all results available for later read
157  * access. */
159 
160  string filename;
161 
162  /* Specification of the tile image which corresponds to the buffer parameters.
163  * Contains channels configured according to the passes configuration in the path traces.
164  *
165  * Output images are saved using this specification, input images are expected to have matched
166  * specification. */
167  ImageSpec image_spec;
168 
169  /* Output handle for the tile file.
170  *
171  * This file can not be closed until all tiles has been provided, so the handle is stored in
172  * the state and is created whenever writing is requested. */
173  unique_ptr<ImageOutput> tile_out;
174 
177 };
178 
BufferParams buffer_params_
Definition: session/tile.h:139
void set_temp_dir(const string &temp_dir)
function< void(string_view)> full_buffer_written_cb
Definition: session/tile.h:40
string temp_dir_
Definition: session/tile.h:128
int num_tiles_written
Definition: session/tile.h:175
bool close_tile_output()
void reset_scheduling(const BufferParams &params, int2 tile_size)
int compute_render_tile_size(const int suggested_tile_size) const
bool has_multiple_tiles() const
Definition: session/tile.h:68
struct TileManager::@1269 write_state_
TileManager(TileManager &&other) noexcept=delete
Tile get_tile_for_index(int index) const
bool has_written_tiles() const
Definition: session/tile.h:96
void update(const BufferParams &params, const Scene *scene)
int tile_file_index
Definition: session/tile.h:158
const int2 get_size() const
const Tile & get_current_tile() const
int next_tile_index
Definition: session/tile.h:147
string filename
Definition: session/tile.h:160
int get_num_tiles() const
Definition: session/tile.h:63
struct TileManager::@1268 tile_state_
unique_ptr< ImageOutput > tile_out
Definition: session/tile.h:173
bool open_tile_output()
bool write_tile(const RenderBuffers &tile_buffers)
Tile current_tile
Definition: session/tile.h:149
TileManager & operator=(const TileManager &other)=delete
bool read_full_buffer_from_disk(string_view filename, RenderBuffers *buffers, DenoiseParams *denoise_params)
ImageSpec image_spec
Definition: session/tile.h:167
TileManager(const TileManager &other)=delete
static const int MAX_TILE_SIZE
Definition: session/tile.h:118
string tile_file_unique_part_
Definition: session/tile.h:132
void finish_write_tiles()
TileManager & operator=(TileManager &&other)=delete
int get_tile_overscan() const
Definition: session/tile.h:73
static const int IMAGE_TILE_SIZE
Definition: session/tile.h:112
int height
Definition: session/tile.h:23
int window_y
Definition: session/tile.h:25
int y
Definition: session/tile.h:22
int window_height
Definition: session/tile.h:26
int x
Definition: session/tile.h:22
int window_x
Definition: session/tile.h:25
int window_width
Definition: session/tile.h:26
int width
Definition: session/tile.h:23
Tile()
Definition: session/tile.h:28
#define CCL_NAMESPACE_END
Definition: cuda/compat.h:9
Scene scene
SyclQueue void void size_t num_bytes void
uiWidgetBaseParameters params[MAX_WIDGET_BASE_BATCH]
#define make_int2(x, y)
Definition: metal/compat.h:206