session.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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 __SESSION_H__
  17. #define __SESSION_H__
  18. #include "render/buffers.h"
  19. #include "device/device.h"
  20. #include "render/shader.h"
  21. #include "render/stats.h"
  22. #include "render/tile.h"
  23. #include "util/util_progress.h"
  24. #include "util/util_stats.h"
  25. #include "util/util_thread.h"
  26. #include "util/util_vector.h"
  27. CCL_NAMESPACE_BEGIN
  28. class BufferParams;
  29. class Device;
  30. class DeviceScene;
  31. class DeviceRequestedFeatures;
  32. class DisplayBuffer;
  33. class Progress;
  34. class RenderBuffers;
  35. class Scene;
  36. /* Session Parameters */
  37. class SessionParams {
  38. public:
  39. DeviceInfo device;
  40. bool background;
  41. bool progressive_refine;
  42. bool progressive;
  43. bool experimental;
  44. int samples;
  45. int2 tile_size;
  46. TileOrder tile_order;
  47. int start_resolution;
  48. int pixel_size;
  49. int threads;
  50. bool use_profiling;
  51. bool display_buffer_linear;
  52. bool run_denoising;
  53. bool write_denoising_passes;
  54. bool full_denoising;
  55. DenoiseParams denoising;
  56. double cancel_timeout;
  57. double reset_timeout;
  58. double text_timeout;
  59. double progressive_update_timeout;
  60. ShadingSystem shadingsystem;
  61. function<bool(const uchar *pixels, int width, int height, int channels)> write_render_cb;
  62. SessionParams()
  63. {
  64. background = false;
  65. progressive_refine = false;
  66. progressive = false;
  67. experimental = false;
  68. samples = 1024;
  69. tile_size = make_int2(64, 64);
  70. start_resolution = INT_MAX;
  71. pixel_size = 1;
  72. threads = 0;
  73. use_profiling = false;
  74. run_denoising = false;
  75. write_denoising_passes = false;
  76. full_denoising = false;
  77. display_buffer_linear = false;
  78. cancel_timeout = 0.1;
  79. reset_timeout = 0.1;
  80. text_timeout = 1.0;
  81. progressive_update_timeout = 1.0;
  82. shadingsystem = SHADINGSYSTEM_SVM;
  83. tile_order = TILE_CENTER;
  84. }
  85. bool modified(const SessionParams &params)
  86. {
  87. return !(device == params.device && background == params.background &&
  88. progressive_refine == params.progressive_refine
  89. /* && samples == params.samples */
  90. && progressive == params.progressive && experimental == params.experimental &&
  91. tile_size == params.tile_size && start_resolution == params.start_resolution &&
  92. pixel_size == params.pixel_size && threads == params.threads &&
  93. use_profiling == params.use_profiling &&
  94. display_buffer_linear == params.display_buffer_linear &&
  95. cancel_timeout == params.cancel_timeout && reset_timeout == params.reset_timeout &&
  96. text_timeout == params.text_timeout &&
  97. progressive_update_timeout == params.progressive_update_timeout &&
  98. tile_order == params.tile_order && shadingsystem == params.shadingsystem);
  99. }
  100. };
  101. /* Session
  102. *
  103. * This is the class that contains the session thread, running the render
  104. * control loop and dispatching tasks. */
  105. class Session {
  106. public:
  107. Device *device;
  108. Scene *scene;
  109. RenderBuffers *buffers;
  110. DisplayBuffer *display;
  111. Progress progress;
  112. SessionParams params;
  113. TileManager tile_manager;
  114. Stats stats;
  115. Profiler profiler;
  116. function<void(RenderTile &)> write_render_tile_cb;
  117. function<void(RenderTile &, bool)> update_render_tile_cb;
  118. explicit Session(const SessionParams &params);
  119. ~Session();
  120. void start();
  121. bool draw(BufferParams &params, DeviceDrawParams &draw_params);
  122. void wait();
  123. bool ready_to_reset();
  124. void reset(BufferParams &params, int samples);
  125. void set_samples(int samples);
  126. void set_pause(bool pause);
  127. bool update_scene();
  128. bool load_kernels(bool lock_scene = true);
  129. void device_free();
  130. /* Returns the rendering progress or 0 if no progress can be determined
  131. * (for example, when rendering with unlimited samples). */
  132. float get_progress();
  133. void collect_statistics(RenderStats *stats);
  134. protected:
  135. struct DelayedReset {
  136. thread_mutex mutex;
  137. bool do_reset;
  138. BufferParams params;
  139. int samples;
  140. } delayed_reset;
  141. void run();
  142. void update_status_time(bool show_pause = false, bool show_done = false);
  143. void tonemap(int sample);
  144. void render();
  145. void reset_(BufferParams &params, int samples);
  146. void run_cpu();
  147. bool draw_cpu(BufferParams &params, DeviceDrawParams &draw_params);
  148. void reset_cpu(BufferParams &params, int samples);
  149. void run_gpu();
  150. bool draw_gpu(BufferParams &params, DeviceDrawParams &draw_params);
  151. void reset_gpu(BufferParams &params, int samples);
  152. bool acquire_tile(Device *tile_device, RenderTile &tile);
  153. void update_tile_sample(RenderTile &tile);
  154. void release_tile(RenderTile &tile);
  155. void map_neighbor_tiles(RenderTile *tiles, Device *tile_device);
  156. void unmap_neighbor_tiles(RenderTile *tiles, Device *tile_device);
  157. bool device_use_gl;
  158. thread *session_thread;
  159. volatile bool display_outdated;
  160. volatile bool gpu_draw_ready;
  161. volatile bool gpu_need_tonemap;
  162. thread_condition_variable gpu_need_tonemap_cond;
  163. bool pause;
  164. thread_condition_variable pause_cond;
  165. thread_mutex pause_mutex;
  166. thread_mutex tile_mutex;
  167. thread_mutex buffers_mutex;
  168. thread_mutex display_mutex;
  169. bool kernels_loaded;
  170. DeviceRequestedFeatures loaded_kernel_features;
  171. double reset_time;
  172. /* progressive refine */
  173. double last_update_time;
  174. bool update_progressive_refine(bool cancel);
  175. DeviceRequestedFeatures get_requested_device_features();
  176. /* ** Split kernel routines ** */
  177. /* Maximumnumber of closure during session lifetime. */
  178. int max_closure_global;
  179. /* Get maximum number of closures to be used in kernel. */
  180. int get_max_closure_count();
  181. };
  182. CCL_NAMESPACE_END
  183. #endif /* __SESSION_H__ */