Blender V5.3
eevee_sampling.hh
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2021 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
10
11#pragma once
12
13#include "BLI_vector.hh"
14#include "DNA_scene_types.h"
15
18
19namespace blender::eevee {
20
21class Instance;
22
24
25class Sampling {
26 private:
27 Instance &inst_;
28
29 /* Number of samples in the first ring of jittered depth of field. */
30 static constexpr uint64_t dof_web_density_ = 6;
31 /* High number of sample for viewport infinite rendering. */
32 static constexpr uint64_t infinite_sample_count_ = 0xFFFFFFu;
33 /* During interactive rendering, loop over the first few samples. */
34 static constexpr uint64_t interactive_sample_aa_ = 8;
35 static constexpr uint64_t interactive_sample_raytrace_ = 32;
36 static constexpr uint64_t interactive_sample_volume_ = 32;
37 static constexpr uint64_t interactive_sample_max_ = interactive_sample_aa_ *
38 interactive_sample_raytrace_ *
39 interactive_sample_volume_;
40 static constexpr float unclamped_max = 1e20;
41
43 uint64_t sample_ = 0;
45 uint64_t sample_count_ = 64;
47 uint64_t dof_ring_count_ = 0;
49 uint64_t dof_sample_count_ = 1;
51 uint64_t motion_blur_steps_ = 1;
53 int64_t viewport_sample_ = 0;
55 bool reset_ = false;
60 bool interactive_mode_ = false;
66 static constexpr int interactive_mode_threshold = 3;
70 float time_limit_ = 0.0f;
71 float start_render_time_ = 0.0f;
72 float current_time_ = 0.0f;
76 bool use_custom_pixel_jitter_sample_ = false;
77 float2 custom_pixel_jitter_sample_ = {};
78
79 SamplingDataBuf data_ = {"SamplingDataBuf"};
80
81 ClampData &clamp_data_;
82
83 bool check_time_limit_reached() const;
84
85 public:
86 Sampling(Instance &inst, ClampData &clamp_data) : inst_(inst), clamp_data_(clamp_data) {};
88
89 void init(const Scene *scene);
90 void init(const Object &probe_object);
91 void end_sync();
92 void step();
93
94 /* Viewport Only: Function to call to notify something in the scene changed.
95 * This will reset accumulation. Do not call after end_sync() or during sample rendering. */
96 void reset();
97
98 /* Viewport Only: true if an update happened in the scene and accumulation needs reset. */
99 bool is_reset() const;
100
101 template<typename PassType> void bind_resources(PassType &pass)
102 {
103 pass.bind_ssbo(SAMPLING_BUF_SLOT, &data_);
104 }
105
106 /* Returns a pseudo random number in [0..1] range. Each dimension are de-correlated.
107 * WARNING: Don't use during init or sync,
108 * results are only valid during render, after step() has been called. */
109 float rng_get(eSamplingDimension dimension) const
110 {
111 return data_.dimensions[dimension];
112 }
113
114 /* Returns a pseudo random number in [0..1] range. Each dimension are de-correlated.
115 * WARNING: Don't use during init or sync,
116 * results are only valid during render, after step() has been called. */
117 float2 rng_2d_get(eSamplingDimension starting_dimension) const
118 {
119 return *reinterpret_cast<const float2 *>(&data_.dimensions[starting_dimension]);
120 }
121
122 /* Returns a pseudo random number in [0..1] range. Each dimension are de-correlated.
123 * WARNING: Don't use during init or sync,
124 * results are only valid during render, after step() has been called. */
125 float3 rng_3d_get(eSamplingDimension starting_dimension) const
126 {
127 return *reinterpret_cast<const float3 *>(&data_.dimensions[starting_dimension]);
128 }
129
130 /* Returns true if rendering has finished. */
131 bool finished() const
132 {
133 return (sample_ >= sample_count_) || check_time_limit_reached();
134 }
135
136 /* Returns true if viewport smoothing and sampling has finished. */
137 bool finished_viewport() const
138 {
139 return ((viewport_sample_ >= sample_count_) && !interactive_mode_) ||
140 check_time_limit_reached();
141 }
142
143 /* Returns true if viewport renderer is in interactive mode and should use TAA. */
144 bool interactive_mode() const
145 {
146 return interactive_mode_;
147 }
148
149 /* Target sample count. */
151 {
152 return sample_count_;
153 }
154
155 /* 0 based current sample. Might not increase sequentially in viewport. */
157 {
158 return sample_;
159 }
160
161 /* Updates the current progress towards the time limit. Must be called after a sample has been
162 * rendered and before `finished` or `finished_viewport` are called. */
163 void update_time();
164
165 /* Returns true if a custom pixel jitter sample position is set. */
167 {
168 return use_custom_pixel_jitter_sample_;
169 }
170
171 bool use_clamp_direct() const
172 {
173 return clamp_data_.surface_direct != unclamped_max;
174 }
175
177 {
178 return clamp_data_.surface_indirect != unclamped_max;
179 }
180
181 bool use_direct_scale() const
182 {
183 return clamp_data_.direct_scale != 1.0f;
184 }
185
186 float direct_scale() const
187 {
188 return clamp_data_.direct_scale;
189 }
190
192 {
193 return clamp_data_.indirect_scale != 1.0f;
194 }
195
196 float indirect_scale() const
197 {
198 return clamp_data_.indirect_scale;
199 }
200
201 /* Return true if we are starting a new motion blur step. We need to run sync again since
202 * depsgraph was updated by MotionBlur::step(). */
203 bool do_render_sync() const
204 {
205 return ((sample_ % (sample_count_ / motion_blur_steps_)) == 0);
206 }
207
216 static float3 sample_ball(const float3 &rand);
217
223 static float2 sample_disk(const float2 &rand);
224
230 static float3 sample_hemisphere(const float2 &rand);
231
237 static float3 sample_sphere(const float2 &rand);
238
244 static float2 sample_spiral(const float2 &rand);
245
250 void dof_disk_sample_get(float *r_radius, float *r_theta) const;
251
256 {
257 return dof_ring_count_;
258 }
259
264 {
265 return dof_sample_count_;
266 }
267
268 /* Cumulative Distribution Function Utils. */
269
274 static void cdf_from_curvemapping(const CurveMapping &curve, Vector<float> &cdf);
279 static void cdf_invert(Vector<float> &cdf, Vector<float> &inverted_cdf);
280};
281
282} // namespace blender::eevee
void reset()
A running instance of the engine.
uint64_t sample_index() const
bool use_custom_pixel_jitter_sample() const
float rng_get(eSamplingDimension dimension) const
void bind_resources(PassType &pass)
float3 rng_3d_get(eSamplingDimension starting_dimension) const
static void cdf_invert(Vector< float > &cdf, Vector< float > &inverted_cdf)
float2 rng_2d_get(eSamplingDimension starting_dimension) const
uint64_t dof_ring_count_get() const
void dof_disk_sample_get(float *r_radius, float *r_theta) const
uint64_t sample_count() const
uint64_t dof_sample_count_get() const
Sampling(Instance &inst, ClampData &clamp_data)
static float2 sample_spiral(const float2 &rand)
static void cdf_from_curvemapping(const CurveMapping &curve, Vector< float > &cdf)
#define SAMPLING_BUF_SLOT
float3 sample_ball(float3 rand)
float2 sample_disk(float2 rand)
float3 sample_sphere(float2 rand)
float3 sample_hemisphere(float2 rand)
VecBase< float, D > step(VecOp< float, D >, VecOp< float, D >) RET
PassType
draw::StorageBuffer< SamplingData > SamplingDataBuf
unsigned __int64 uint64_t
Definition BKE_text.h:91
__int64 int64_t
Definition BKE_text.h:90
VecBase< float, 2 > float2
VecBase< float, 3 > float3