tile.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /*
  2. * Copyright 2011-2013 Blender Foundation
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #ifndef __TILE_H__
  17. #define __TILE_H__
  18. #include <limits.h>
  19. #include "render/buffers.h"
  20. #include "util/util_list.h"
  21. CCL_NAMESPACE_BEGIN
  22. /* Tile */
  23. class Tile {
  24. public:
  25. int index;
  26. int x, y, w, h;
  27. int device;
  28. /* RENDER: The tile has to be rendered.
  29. * RENDERED: The tile has been rendered, but can't be denoised yet (waiting for neighbors).
  30. * DENOISE: The tile can be denoised now.
  31. * DENOISED: The tile has been denoised, but can't be freed yet (waiting for neighbors).
  32. * DONE: The tile is finished and has been freed. */
  33. typedef enum { RENDER = 0, RENDERED, DENOISE, DENOISED, DONE } State;
  34. State state;
  35. RenderBuffers *buffers;
  36. Tile()
  37. {
  38. }
  39. Tile(int index_, int x_, int y_, int w_, int h_, int device_, State state_ = RENDER)
  40. : index(index_), x(x_), y(y_), w(w_), h(h_), device(device_), state(state_), buffers(NULL)
  41. {
  42. }
  43. };
  44. /* Tile order */
  45. /* Note: this should match enum_tile_order in properties.py */
  46. enum TileOrder {
  47. TILE_CENTER = 0,
  48. TILE_RIGHT_TO_LEFT = 1,
  49. TILE_LEFT_TO_RIGHT = 2,
  50. TILE_TOP_TO_BOTTOM = 3,
  51. TILE_BOTTOM_TO_TOP = 4,
  52. TILE_HILBERT_SPIRAL = 5,
  53. };
  54. /* Tile Manager */
  55. class TileManager {
  56. public:
  57. BufferParams params;
  58. struct State {
  59. vector<Tile> tiles;
  60. int tile_stride;
  61. BufferParams buffer;
  62. int sample;
  63. int num_samples;
  64. int resolution_divider;
  65. int num_tiles;
  66. /* Total samples over all pixels: Generally num_samples*num_pixels,
  67. * but can be higher due to the initial resolution division for previews. */
  68. uint64_t total_pixel_samples;
  69. /* These lists contain the indices of the tiles to be rendered/denoised and are used
  70. * when acquiring a new tile for the device.
  71. * Each list in each vector is for one logical device. */
  72. vector<list<int>> render_tiles;
  73. vector<list<int>> denoising_tiles;
  74. } state;
  75. int num_samples;
  76. TileManager(bool progressive,
  77. int num_samples,
  78. int2 tile_size,
  79. int start_resolution,
  80. bool preserve_tile_device,
  81. bool background,
  82. TileOrder tile_order,
  83. int num_devices = 1,
  84. int pixel_size = 1);
  85. ~TileManager();
  86. void device_free();
  87. void reset(BufferParams &params, int num_samples);
  88. void set_samples(int num_samples);
  89. bool next();
  90. bool next_tile(Tile *&tile, int device = 0);
  91. bool finish_tile(int index, bool &delete_tile);
  92. bool done();
  93. void set_tile_order(TileOrder tile_order_)
  94. {
  95. tile_order = tile_order_;
  96. }
  97. /* ** Sample range rendering. ** */
  98. /* Start sample in the range. */
  99. int range_start_sample;
  100. /* Number to samples in the rendering range. */
  101. int range_num_samples;
  102. /* Get number of actual samples to render. */
  103. int get_num_effective_samples();
  104. /* Schedule tiles for denoising after they've been rendered. */
  105. bool schedule_denoising;
  106. protected:
  107. void set_tiles();
  108. bool progressive;
  109. int2 tile_size;
  110. TileOrder tile_order;
  111. int start_resolution;
  112. int pixel_size;
  113. int num_devices;
  114. /* in some cases it is important that the same tile will be returned for the same
  115. * device it was originally generated for (i.e. viewport rendering when buffer is
  116. * allocating once for tile and then always used by it)
  117. *
  118. * in other cases any tile could be handled by any device (i.e. final rendering
  119. * without progressive refine)
  120. */
  121. bool preserve_tile_device;
  122. /* for background render tiles should exactly match render parts generated from
  123. * blender side, which means image first gets split into tiles and then tiles are
  124. * assigning to render devices
  125. *
  126. * however viewport rendering expects tiles to be allocated in a special way,
  127. * meaning image is being sliced horizontally first and every device handles
  128. * it's own slice
  129. */
  130. bool background;
  131. /* Generate tile list, return number of tiles. */
  132. int gen_tiles(bool sliced);
  133. void gen_render_tiles();
  134. int get_neighbor_index(int index, int neighbor);
  135. bool check_neighbor_state(int index, Tile::State state);
  136. };
  137. CCL_NAMESPACE_END
  138. #endif /* __TILE_H__ */