Raytracer.cpp 11 KB
Newer Older
unknown's avatar
unknown committed
1
2
3
4
#include "Raytracer.h"
#include <engine/Messaging.h>
#include <engine/Time.h>
#include <random>
5
#include <pugixml/pugixml.hpp>
6
#include <util/files.h>
unknown's avatar
unknown committed
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29

namespace glare
{
	namespace advanced
	{
		Raytracer::Raytracer()
			: Raytracer(std::make_shared<RayGeneratorDefault>())
		{

		}

		Raytracer::Raytracer(std::shared_ptr<RayGeneratorBase> ray_generator)
			: Raytracer(std::shared_ptr<SceneCollector>(nullptr), ray_generator)
		{
		}

		Raytracer::Raytracer(std::shared_ptr<SceneCollector> collector)
			: Raytracer(collector, std::make_shared<RayGeneratorDefault>())
		{
		}

		Raytracer::Raytracer(std::shared_ptr<SceneCollector> collector, std::shared_ptr<RayGeneratorBase> ray_generator)
		{
30
31
32
33
34
35
36
			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;

37
38
39
40
41
42
43
			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;

44
			m_linespace_config.distance_threshold = 8.f;
45
			m_render_config.bounces = 16;
46
			m_render_shader = core::ShaderProgram::makeUnique(files::shader("/raytracer/pathtracer.compute").string());
unknown's avatar
unknown committed
47
48
49
50
51
52
53
54
55
56
57
58
			m_ray_generator = ray_generator;
			m_trace_buffer = std::make_unique<core::Buffer<gl::BufferType::eShaderStorage>>();
			core::MessageHandler::getInstance().registerReceiver(core::GlobalMessages::c_scene_changed, *this);
			if (collector)
				initialize(collector);
		}

		Raytracer::~Raytracer()
		{

		}

59
60
61
62
63
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
		void Raytracer::loadSettings(const fs::path &xml_file)
		{
			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);
		}

93
94
95
96
97
98
99
100
101
102
		void Raytracer::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;
unknown's avatar
unknown committed
103
104
105
106
107
108
			reset();
		}

		uint8_t Raytracer::getEffectBounces(EffectType type)
		{
			return m_bounce_count_thresholds[unsigned(type)];
109
110
		}

111
112
113
114
115
116
117
118
119
120
		void Raytracer::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;
unknown's avatar
unknown committed
121
122
123
124
125
126
			reset();
		}

		uint8_t Raytracer::getLinespaceBounceThreshold(EffectType type)
		{
			return m_ls_bounce_count_thresholds[unsigned(type)];
127
128
		}

unknown's avatar
unknown committed
129
130
		void Raytracer::reset()
		{
131
			m_render_config.current_sample = 0;
unknown's avatar
unknown committed
132
133
134
135
			if (m_color_store)
				gl::clearTextureImage(m_color_store->getID(), 0, gl::TextureFormat::eRGBA, gl::Type::eFloat, glm::value_ptr(glm::vec4(0)));
		}

136
		const core::TextureRGBA_32F &Raytracer::render(uint32_t width, uint32_t height)
unknown's avatar
unknown committed
137
138
139
140
		{
			if (width != m_last_width || height != m_last_height) {
				m_last_width = width;
				m_last_height = height;
141
142
143

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

144
				m_trace_buffer->reserve<Trace>(width * height, gl::BufferUsage::eDynamicCopy);
unknown's avatar
unknown committed
145
146
				m_render_target = std::make_unique<core::TextureRGBA_32F>(width, height);
				m_color_store = std::make_unique<core::TextureRGBA_32F>(width, height);
147
				m_render_config.current_sample = 0;
unknown's avatar
unknown committed
148
149
			}
			
150
			for (unsigned sample = 0; sample < m_render_config.samples_per_frame; sample++) {
unknown's avatar
unknown committed
151

152
				if (m_render_config.current_sample >= m_render_config.max_samples)
unknown's avatar
unknown committed
153
154
155
156
157
158
159
160
161
162
163
164
					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);

				m_render_shader->updateStorageBuffer("lights_buffer", m_collector->lightBuffer());
				m_render_shader->updateStorageBuffer("trace_buffer", m_trace_buffer);
				m_render_shader->updateStorageBuffer("mesh_buffer", m_collector->meshBuffer());
unknown's avatar
unknown committed
165
166
				m_render_shader->updateStorageBuffer("object_bvh_buffer", m_collector->objectBVHNodeBuffer());
				m_render_shader->updateStorageBuffer("object_bvh_id_buffer", m_collector->objectBVHIDBuffer());
unknown's avatar
unknown committed
167
168
169
170
171

				m_render_shader->updateStorageBuffer("material_buffer", m_collector->materialBuffer());

				m_render_shader->updateUniformStruct("u_camera", m_collector->getActiveCamera());

172
173
174
175
176
				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
177
178
179
180
181
182
183
184
185
186
187
188

				if (m_skybox)
				{
					m_render_shader->updateUniformUInt64("u_environment.cubemap", m_skybox->getBindlessID());
					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);
				}

189
190
				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);
191
				m_render_shader->updateUniformFloat("u_linespace_properties.distance_threshold", m_linespace_config.distance_threshold);
unknown's avatar
unknown committed
192
193
194
195
196
197
				
				unsigned bounce_thresholds  = 0;
				for (int i = 0; i < 8; ++i)
				{
					bounce_thresholds |= (m_bounce_count_thresholds[i] & 0xf) << (4 * i);
				}
198
199
				m_render_shader->updateUniformUInt("u_render_config.bounce_thresholds", bounce_thresholds);

200
				m_linespace_config.bounce_thresholds = 0;
201
202
203
204
205
				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
206

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

209
				++m_render_config.current_sample;
unknown's avatar
unknown committed
210
211
212
213
214
			}

			return *m_render_target;
		}

unknown's avatar
unknown committed
215
216
		void Raytracer::saveRender(fs::path target) const
		{
unknown's avatar
unknown committed
217
			core::textures::saveTexture(target, *m_render_target);
unknown's avatar
unknown committed
218
219
		}

unknown's avatar
unknown committed
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
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
		void Raytracer::setGenerator(std::shared_ptr<RayGeneratorBase> generator)
		{
			m_ray_generator = generator;
			reset();
		}

		void Raytracer::initialize(std::shared_ptr<SceneCollector> collector)
		{
			m_collector = collector;
			reset();
		}

		void Raytracer::handle(core::message_t& message)
		{
			switch (message.name)
			{
			case core::GlobalMessages::c_scene_changed:
				reset();
				break;

			}
		}

		const core::ShaderProgram& Raytracer::getShader() const
		{
			return *m_render_shader;
		}

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

		uint32_t Raytracer::width() const
		{
			return m_last_width;
		}

		uint32_t Raytracer::height() const
		{
			return m_last_height;
		}

		core::Buffer<gl::BufferType::eShaderStorage>& Raytracer::traceBuffer() const
		{
			return *m_trace_buffer;
		}

		const core::TextureRGBA_32F& Raytracer::renderTarget() const
		{
			return *m_render_target;
		}

		std::shared_ptr<SceneCollector> Raytracer::collector() const
		{
			return m_collector;
		}
	}
}