pathtracer.h 4.72 KB
Newer Older
1
2
#ifndef INCLUDE_PATHTRACER_H
#define INCLUDE_PATHTRACER_H
unknown's avatar
unknown committed
3

unknown's avatar
unknown committed
4
#include <array>
5
#include <filesystem>
unknown's avatar
unknown committed
6

Johannes Braun's avatar
Johannes Braun committed
7
#include <core/state.h>
8
#include <core/base/program.h>
Johannes Braun's avatar
Johannes Braun committed
9
#include <core/graph/scene_node.h>
Johannes Braun's avatar
Johannes Braun committed
10
#include <util/messages.h>
unknown's avatar
unknown committed
11

12
#include <raytrace/data/global_collector.h>
13
#include "raygenerator.h"
unknown's avatar
unknown committed
14

15
namespace glare::raytrace
unknown's avatar
unknown committed
16
{
17
18
	namespace fs = std::experimental::filesystem;

19
	enum class Effect
unknown's avatar
unknown committed
20
	{
21
		eDiffusion = 0,
22
23
24
		eReflection,
		eTransmission,
		eEmission
25
	};
unknown's avatar
unknown committed
26

Johannes Braun's avatar
Johannes Braun committed
27
	class Pathtracer : public msg::Observer
28
29
	{
	public:
30
31
32
		explicit Pathtracer(std::shared_ptr<core::SceneNode> graph_root = nullptr, RayGenType ray_generator = RayGenType::eTrace);
		explicit Pathtracer(RayGenType ray_generator);
		explicit Pathtracer(std::shared_ptr<SceneCollector> collector, RayGenType ray_generator = RayGenType::eTrace);
33
34
35
36
		~Pathtracer();

		void loadSettings(const fs::path &xml_file);

Johannes Braun's avatar
Johannes Braun committed
37
		const core::Texture &render(unsigned width, unsigned height);
38
		void draw(std::shared_ptr<core::SceneNode> node = core::Context::current().graph());
39
		void saveRender(const fs::path& target) const;
40
41

		void initialize(std::shared_ptr<SceneCollector> collector);
Johannes Braun's avatar
Johannes Braun committed
42
		void reload(std::shared_ptr<core::SceneNode> graph_root);
43

Johannes Braun's avatar
Johannes Braun committed
44
		void onNotify(msg::id_type id, msg::message_type message) override;
45
46
47

		void reset();

48
49
		unsigned width() const;
		unsigned height() const;
50
		const core::Program& shader() const;
Johannes Braun's avatar
Johannes Braun committed
51
		core::Buffer& traceBuffer();
Johannes Braun's avatar
Johannes Braun committed
52
		const core::Texture& renderTarget() const;
53
		std::shared_ptr<SceneCollector> collector() const;
54
55
		
		void setBounceCount(unsigned count) { m_bounce_count = count; reset(); }
Johannes Braun's avatar
Johannes Braun committed
56
		void setBounceThreshold(Effect effect, int count) { m_bounce_thresholds[effect] = count; reset(); }
57
58
59
60
61
62
		void setClampDirect(float clamp) { m_clamp_direct = clamp; reset(); }
		void setClampIndirect(float clamp) { m_clamp_indirect = clamp; reset(); }
		void setDebugTimes(bool show) { m_debug_times = show; reset(); }
		void setLinespaceAccuracy(float accuracy) { m_linespace_accuracy = accuracy; reset(); }
		void setLinespaceShadow(float shadow) { m_linespace_shadow = shadow; reset(); }
		void setLinespaceDistance(float distance) { m_linespace_distance = distance; reset(); }
Johannes Braun's avatar
Johannes Braun committed
63
		void setLinespaceBounces(Effect effect, int count) { m_linespace_bounces[effect] = count; reset(); }
Johannes Braun's avatar
Johannes Braun committed
64
		void setLinespaceBias(float bias) { m_linespace_bias = bias; reset(); }
Johannes Braun's avatar
Johannes Braun committed
65
		void setSamplesMax(unsigned max) { m_samples_max = max; }
66
67
68
69
70
71
72
73
74
75
76
		void setSamplesPerFrame(unsigned spframe) { m_samples_per_frame = spframe; }
		void setRayGenerator(RayGenType generator) { m_ray_generator = generator; reset(); }

		unsigned getBounceCount() const { return m_bounce_count; }
		uint8_t getBounceThreshold(Effect effect) const { return m_bounce_thresholds[effect]; }
		float getClampDirect() const { return m_clamp_direct; }
		float getClampIndirect() const { return m_clamp_indirect; }
		float getLinespaceAccuracy() const { return m_linespace_accuracy; }
		float getLinespaceShadow() const { return m_linespace_shadow; }
		float getLinespaceDistance() const { return m_linespace_distance; }
		uint8_t getLinespaceBounces(Effect effect) const { return m_linespace_bounces[effect]; }
Johannes Braun's avatar
Johannes Braun committed
77
		float getLinespaceBias() const { return m_linespace_bias; }
78
79
80
81
		unsigned getSamplesMax() const { return m_samples_max; }
		unsigned getSamplesPerFrame() const { return m_samples_per_frame; }
		unsigned getSamplesCurrent() const { return m_samples_current; }
		RayGenType getRayGenerator() const { return m_ray_generator; }
Johannes Braun's avatar
Johannes Braun committed
82

83
84
	private:
		struct LogTime
unknown's avatar
unknown committed
85
		{
86
87
			explicit LogTime(std::string label, bool enabled);
			~LogTime();
88

89
90
91
92
		private:
			bool m_enabled;
			std::string m_label;
		};
93

94
95
96
		struct EffectArray
		{
			template<typename... Args> EffectArray(Args&&... args);
97
98
99
100
			int& operator[](Effect effect);
			const int& operator[](Effect effect) const;
			int& operator[](int effect);
			const int& operator[](int effect) const;
101
102

		private:
103
			std::array<int, 4> m_data;
104
105
		};

106
		int	m_bounce_count			= 16;
107
108
109
110
111
112
113
114
		EffectArray	m_bounce_thresholds		= { 4, 6, 12, 1 };
		float		m_clamp_direct			= 10.f;
		float		m_clamp_indirect		= 10.f;
		bool		m_debug_times			= false;
		float		m_linespace_accuracy	= 0.5f;
		float		m_linespace_shadow		= 0.5f;
		float		m_linespace_distance	= 10.f;
		EffectArray	m_linespace_bounces		= { 3, 3, 3, 1 };
Johannes Braun's avatar
Johannes Braun committed
115
		float		m_linespace_bias		= 0.08f;
116
117
118
		int	m_samples_current		= 0;
		int	m_samples_max			= 80'000;
		int	m_samples_per_frame		= 1;
119
120
121
122
123
124
		RayGenType	m_ray_generator			= RayGenType::eTrace;

		unsigned m_width = 0;
		unsigned m_height = 0;
		
		std::unique_ptr<core::Program> m_render_shader;
Johannes Braun's avatar
Johannes Braun committed
125
		std::unique_ptr<core::Texture> m_render_target;
Johannes Braun's avatar
Johannes Braun committed
126
		core::Buffer m_trace_buffer;
127

128
		std::shared_ptr<SceneCollector> m_collector;
129
	};
130
131
132

	template <typename ... Args>
	Pathtracer::EffectArray::EffectArray(Args&&... args) : m_data{ static_cast<uint8_t>(args)... } {}
unknown's avatar
unknown committed
133
134
}

135
#endif //INCLUDE_PATHTRACER_H