pathtracer.cpp 12.8 KB
Newer Older
1
2
3
4
#include "pathtracer.h"

#include <random>

5
6
#include <core/message_tags.h>
#include <core/time.h>
7
#include <pugixml/pugixml.hpp>
8
#include <util/files.h>
unknown's avatar
unknown committed
9
10
11

namespace glare
{
12
	namespace raytrace
unknown's avatar
unknown committed
13
	{
14
15
		Pathtracer::Pathtracer()
			: Pathtracer(std::make_shared<RayGeneratorDefault>())
unknown's avatar
unknown committed
16
17
18
19
		{

		}

20
21
		Pathtracer::Pathtracer(std::shared_ptr<RayGeneratorBase> ray_generator)
			: Pathtracer(std::shared_ptr<SceneCollector>(nullptr), ray_generator)
unknown's avatar
unknown committed
22
23
24
		{
		}

25
26
		Pathtracer::Pathtracer(std::shared_ptr<SceneCollector> collector)
			: Pathtracer(collector, std::make_shared<RayGeneratorDefault>())
unknown's avatar
unknown committed
27
28
29
		{
		}

30
		Pathtracer::Pathtracer(std::shared_ptr<SceneCollector> collector, std::shared_ptr<RayGeneratorBase> ray_generator)
unknown's avatar
unknown committed
31
		{
32
33
34
35
36
37
38
			m_bounce_count_thresholds[unsigned(EffectType::eDiffuse)] = 4;
			m_bounce_count_thresholds[unsigned(EffectType::eTranslucent)] = 6;
			m_bounce_count_thresholds[unsigned(EffectType::eReflection)] = 6;
			m_bounce_count_thresholds[unsigned(EffectType::eRefraction)] = 12;
			m_bounce_count_thresholds[unsigned(EffectType::eTransparent)] = 12;
			m_bounce_count_thresholds[unsigned(EffectType::eEmissive)] = 0;

39
40
41
42
43
44
45
			m_ls_bounce_count_thresholds[unsigned(EffectType::eDiffuse)] = 1;
			m_ls_bounce_count_thresholds[unsigned(EffectType::eTranslucent)] = 3;
			m_ls_bounce_count_thresholds[unsigned(EffectType::eReflection)] = 3;
			m_ls_bounce_count_thresholds[unsigned(EffectType::eRefraction)] = 6;
			m_ls_bounce_count_thresholds[unsigned(EffectType::eTransparent)] = 8;
			m_ls_bounce_count_thresholds[unsigned(EffectType::eEmissive)] = 0;

46
			m_linespace_config.distance_threshold = 8.f;
47
			m_render_config.bounces = 16;
48
			m_render_shader = core::ShaderProgram::makeUnique(files::shader("/raytracer/pathtracer.compute").string());
unknown's avatar
unknown committed
49
50
			m_ray_generator = ray_generator;
			m_trace_buffer = std::make_unique<core::Buffer<gl::BufferType::eShaderStorage>>();
51
52
53

			registerForMessage(tags::scene);

unknown's avatar
unknown committed
54
55
56
57
			if (collector)
				initialize(collector);
		}

58
		Pathtracer::~Pathtracer()
unknown's avatar
unknown committed
59
		{
60
			unregisterForMessage(tags::scene);
unknown's avatar
unknown committed
61
62
		}

63
		void Pathtracer::loadSettings(const fs::path &xml_file)
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
		{
			pugi::xml_document preferences_file;
			Log_Info << "Pathtracer settings file load status: " << preferences_file.load_file(xml_file.c_str()).description();

			//Pathtracer
			const auto pt_child = preferences_file.child("pathtracer");

			//bounces
			const auto bounces_child = pt_child.find_child_by_attribute("group", "name", "bounces");
			m_bounce_count_thresholds[unsigned(EffectType::eDiffuse)] = bounces_child.find_child_by_attribute("item", "name", "diffuse").attribute("value").as_uint(2);
			m_bounce_count_thresholds[unsigned(EffectType::eTranslucent)] = bounces_child.find_child_by_attribute("item", "name", "translucent").attribute("value").as_uint(4);
			m_bounce_count_thresholds[unsigned(EffectType::eReflection)] = bounces_child.find_child_by_attribute("item", "name", "reflect").attribute("value").as_uint(4);
			m_bounce_count_thresholds[unsigned(EffectType::eRefraction)] = bounces_child.find_child_by_attribute("item", "name", "refract").attribute("value").as_uint(8);
			m_bounce_count_thresholds[unsigned(EffectType::eTransparent)] = bounces_child.find_child_by_attribute("item", "name", "transparent").attribute("value").as_uint(8);
			m_bounce_count_thresholds[unsigned(EffectType::eEmissive)] = bounces_child.find_child_by_attribute("item", "name", "emissive").attribute("value").as_uint(1);

			//ls thresholds
			const auto ls_thresholds_child = pt_child.find_child_by_attribute("group", "name", "ls-thresholds");
			m_ls_bounce_count_thresholds[unsigned(EffectType::eDiffuse)] = ls_thresholds_child.find_child_by_attribute("item", "name", "diffuse").attribute("value").as_uint(2);
			m_ls_bounce_count_thresholds[unsigned(EffectType::eTranslucent)] = ls_thresholds_child.find_child_by_attribute("item", "name", "translucent").attribute("value").as_uint(4);
			m_ls_bounce_count_thresholds[unsigned(EffectType::eReflection)] = ls_thresholds_child.find_child_by_attribute("item", "name", "reflect").attribute("value").as_uint(4);
			m_ls_bounce_count_thresholds[unsigned(EffectType::eRefraction)] = ls_thresholds_child.find_child_by_attribute("item", "name", "refract").attribute("value").as_uint(8);
			m_ls_bounce_count_thresholds[unsigned(EffectType::eTransparent)] = ls_thresholds_child.find_child_by_attribute("item", "name", "transparent").attribute("value").as_uint(8);
			m_ls_bounce_count_thresholds[unsigned(EffectType::eEmissive)] = ls_thresholds_child.find_child_by_attribute("item", "name", "emissive").attribute("value").as_uint(1);

			m_linespace_config.accuracy_threshold = ls_thresholds_child.find_child_by_attribute("item", "name", "accuracy").attribute("value").as_float(0.5f);
			m_linespace_config.shadow_threshold = ls_thresholds_child.find_child_by_attribute("item", "name", "shadow").attribute("value").as_float(0.5f);
			m_linespace_config.distance_threshold = ls_thresholds_child.find_child_by_attribute("item", "name", "distance").attribute("value").as_float(10.f);

			m_render_config.clamp_direct = pt_child.find_child_by_attribute("item", "name", "clamp-direct").attribute("value").as_float(8);
			m_render_config.clamp_indirect = pt_child.find_child_by_attribute("item", "name", "clamp-indirect").attribute("value").as_float(8);
		}

97
		void Pathtracer::reset()
unknown's avatar
unknown committed
98
		{
99
			m_render_config.current_sample = 0;
unknown's avatar
unknown committed
100
101
102
103
			if (m_color_store)
				gl::clearTextureImage(m_color_store->getID(), 0, gl::TextureFormat::eRGBA, gl::Type::eFloat, glm::value_ptr(glm::vec4(0)));
		}

104
		const core::TextureRGBA_32F &Pathtracer::render(uint32_t width, uint32_t height)
unknown's avatar
unknown committed
105
106
107
108
		{
			if (width != m_last_width || height != m_last_height) {
				m_last_width = width;
				m_last_height = height;
109
110
111

				Log_Info << "RESIZED: " << width << ", " << height;

112
				m_trace_buffer->reserve<Trace>(width * height, gl::BufferUsage::eDynamicCopy);
unknown's avatar
unknown committed
113
114
				m_render_target = std::make_unique<core::TextureRGBA_32F>(width, height);
				m_color_store = std::make_unique<core::TextureRGBA_32F>(width, height);
115
				m_render_config.current_sample = 0;
unknown's avatar
unknown committed
116
			}
117

118
			for (unsigned sample = 0; sample < m_render_config.samples_per_frame; sample++) {
unknown's avatar
unknown committed
119

120
				if (m_render_config.current_sample >= m_render_config.max_samples)
unknown's avatar
unknown committed
121
122
123
124
125
126
127
128
129
					return *m_render_target;

				m_ray_generator->generate(*this);

				m_render_shader->use();

				m_render_shader->updateUniformImageRGBA32F("u_render_target", *m_render_target, gl::Access::eReadWrite);
				m_render_shader->updateUniformImageRGBA32F("u_color_store", *m_color_store, gl::Access::eReadWrite);

130
				//m_render_shader->updateStorageBuffer("lights_buffer", m_collector->lightBuffer());
unknown's avatar
unknown committed
131
				m_render_shader->updateStorageBuffer("trace_buffer", m_trace_buffer);
132
133
134

				m_collector->apply(*m_render_shader);
				/*m_render_shader->updateStorageBuffer("mesh_buffer", m_collector->meshBuffer());
unknown's avatar
unknown committed
135
136
				m_render_shader->updateStorageBuffer("object_bvh_buffer", m_collector->objectBVHNodeBuffer());
				m_render_shader->updateStorageBuffer("object_bvh_id_buffer", m_collector->objectBVHIDBuffer());
137
				m_render_shader->updateStorageBuffer("material_buffer", m_collector->materialBuffer());*/
unknown's avatar
unknown committed
138

139
140
141
142
143
				m_render_shader->updateUniformUInt("u_render_config.current_sample", m_render_config.current_sample);
				m_render_shader->updateUniformUInt("u_render_config.max_samples", m_render_config.max_samples);
				m_render_shader->updateUniformUInt("u_render_config.num_bounces", m_render_config.bounces);
				m_render_shader->updateUniformFloat("u_render_config.clamp_direct", m_render_config.clamp_direct);
				m_render_shader->updateUniformFloat("u_render_config.clamp_indirect", m_render_config.clamp_indirect);
unknown's avatar
unknown committed
144
145
146

				if (m_skybox)
				{
147
					m_render_shader->updateUniformPointer("u_environment.cubemap", m_skybox->makeResident());
unknown's avatar
unknown committed
148
149
150
151
152
153
154
155
					m_render_shader->updateUniformInt("u_environment.has", 1);
				}
				else
				{
					m_render_shader->updateUniformVec4("u_environment.color", glm::vec4(0.7f, 0.9f, 0.97f, 1));
					m_render_shader->updateUniformInt("u_environment.has", 0);
				}

156
157
				m_render_shader->updateUniformFloat("u_linespace_properties.accuracy_quality", m_linespace_config.accuracy_threshold);
				m_render_shader->updateUniformFloat("u_linespace_properties.shadow_quality", m_linespace_config.shadow_threshold);
158
				m_render_shader->updateUniformFloat("u_linespace_properties.distance_threshold", m_linespace_config.distance_threshold);
159
160

				unsigned bounce_thresholds = 0;
unknown's avatar
unknown committed
161
162
163
164
				for (int i = 0; i < 8; ++i)
				{
					bounce_thresholds |= (m_bounce_count_thresholds[i] & 0xf) << (4 * i);
				}
165
166
				m_render_shader->updateUniformUInt("u_render_config.bounce_thresholds", bounce_thresholds);

167
				m_linespace_config.bounce_thresholds = 0;
168
169
170
171
172
				for (int i = 0; i < 8; ++i)
				{
					m_linespace_config.bounce_thresholds |= (m_ls_bounce_count_thresholds[i] & 0xf) << (4 * i);
				}
				m_render_shader->updateUniformUInt("u_linespace_properties.bounce_thresholds", m_linespace_config.bounce_thresholds);
unknown's avatar
unknown committed
173

unknown's avatar
unknown committed
174
				m_render_shader->dispatchCompute2D(width, 4, height, 4);
unknown's avatar
unknown committed
175

176
				++m_render_config.current_sample;
unknown's avatar
unknown committed
177
178
179
180
181
			}

			return *m_render_target;
		}

182
		void Pathtracer::saveRender(fs::path target) const
unknown's avatar
unknown committed
183
		{
unknown's avatar
unknown committed
184
			core::textures::saveTexture(target, *m_render_target);
unknown's avatar
unknown committed
185
186
		}

187
		void Pathtracer::setGenerator(std::shared_ptr<RayGeneratorBase> generator)
unknown's avatar
unknown committed
188
189
190
191
192
		{
			m_ray_generator = generator;
			reset();
		}

193
		void Pathtracer::initialize(std::shared_ptr<SceneCollector> collector)
unknown's avatar
unknown committed
194
195
196
197
198
		{
			m_collector = collector;
			reset();
		}

199
		void Pathtracer::handle(messaging::message_t& message)
unknown's avatar
unknown committed
200
201
202
		{
			switch (message.name)
			{
203
			case tags::scene:
unknown's avatar
unknown committed
204
205
206
207
208
209
				reset();
				break;

			}
		}

210
		const core::ShaderProgram& Pathtracer::shader() const
unknown's avatar
unknown committed
211
212
213
214
		{
			return *m_render_shader;
		}

215
		uint32_t Pathtracer::width() const
unknown's avatar
unknown committed
216
217
218
219
		{
			return m_last_width;
		}

220
		uint32_t Pathtracer::height() const
unknown's avatar
unknown committed
221
222
223
224
		{
			return m_last_height;
		}

225
		core::Buffer<gl::BufferType::eShaderStorage>& Pathtracer::traceBuffer() const
unknown's avatar
unknown committed
226
227
228
229
		{
			return *m_trace_buffer;
		}

230
		const core::TextureRGBA_32F& Pathtracer::renderTarget() const
unknown's avatar
unknown committed
231
232
233
234
		{
			return *m_render_target;
		}

235
		std::shared_ptr<SceneCollector> Pathtracer::collector() const
unknown's avatar
unknown committed
236
237
238
		{
			return m_collector;
		}
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371

		unsigned Pathtracer::currentSample() const
		{
			return m_render_config.current_sample;
		}

		void Pathtracer::setEffectBounces(EffectType type, uint8_t bounces)
		{
			uint8_t clamped = bounces & 0xf;

			if (clamped != bounces)
			{
				Log_Warn << bounces << " - value larger than 15 (0xf). Result of " << clamped << " may be unintended.";
			}

			m_bounce_count_thresholds[unsigned(type)] = clamped;
			reset();
		}

		uint8_t Pathtracer::getEffectBounces(EffectType type)
		{
			return m_bounce_count_thresholds[unsigned(type)];
		}

		void Pathtracer::setLinespaceBounceThresholds(EffectType type, uint8_t bounces)
		{
			uint8_t clamped = bounces & 0xf;

			if (clamped != bounces)
			{
				Log_Warn << bounces << " - value larger than 15 (0xf). Result of " << clamped << " may be unintended.";
			}

			m_ls_bounce_count_thresholds[unsigned(type)] = clamped;
			reset();
		}

		uint8_t Pathtracer::getLinespaceBounceThreshold(EffectType type)
		{
			return m_ls_bounce_count_thresholds[unsigned(type)];
		}

		void Pathtracer::setSkybox(std::shared_ptr<core::Skybox> skybox)
		{
			m_skybox = skybox;
			reset();
		}

		std::shared_ptr<core::Skybox> Pathtracer::getSkybox() const
		{
			return m_skybox;
		}

		void Pathtracer::setBounces(unsigned count)
		{
			reset();
			m_render_config.bounces = count;
		}

		unsigned Pathtracer::getBounces() const
		{
			return m_render_config.bounces;
		}

		void Pathtracer::setMaxSamples(unsigned count)
		{
			m_render_config.max_samples = count;
		}

		unsigned Pathtracer::getMaxSamples() const
		{
			return m_render_config.max_samples;
		}

		void Pathtracer::setSamplesPerFrame(unsigned count)
		{
			m_render_config.samples_per_frame = count;
		}

		unsigned Pathtracer::getSamplesPerFrame() const
		{
			return m_render_config.samples_per_frame;
		}

		void Pathtracer::setClampDirect(float clamp_direct)
		{
			m_render_config.clamp_direct = clamp_direct; reset();
		}

		float Pathtracer::getClampDirect() const
		{ 
			return m_render_config.clamp_direct; 
		}

		void Pathtracer::setClampIndirect(float clamp_indirect)
		{
			m_render_config.clamp_indirect = clamp_indirect; reset();
		}

		float Pathtracer::getClampIndirect() const
		{ 
			return m_render_config.clamp_indirect; 
		}

		void Pathtracer::setAccuracyThreshold(float accuracy_threshold)
		{
			m_linespace_config.accuracy_threshold = accuracy_threshold; reset();
		}

		float Pathtracer::getAccuracyThreshold() const
		{ 
			return m_linespace_config.accuracy_threshold; 
		}

		void Pathtracer::setShadowThreshold(float shadow_threshold)
		{
			m_linespace_config.shadow_threshold = shadow_threshold; reset();
		}

		float Pathtracer::getShadowThreshold() const
		{ 
			return m_linespace_config.shadow_threshold; 
		}

		void Pathtracer::setDistanceThreshold(float distance_threshold)
		{
			m_linespace_config.distance_threshold = distance_threshold; reset();
		}

		float Pathtracer::getDistanceThreshold() const
		{
			return m_linespace_config.distance_threshold; 
		}
unknown's avatar
unknown committed
372
373
	}
}