Raytracer.cpp 6.21 KB
Newer Older
unknown's avatar
unknown committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include "Raytracer.h"
#include <engine/Messaging.h>
#include <engine/Time.h>
#include <random>

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)
		{
			m_bounces = 8;
			m_render_shader = core::ShaderProgram::makeUnique("/raytracer/raytracer.compute");
			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()
		{

		}

		void Raytracer::reset()
		{
			m_current_sample = 0;
			if (m_color_store)
				gl::clearTextureImage(m_color_store->getID(), 0, gl::TextureFormat::eRGBA, gl::Type::eFloat, glm::value_ptr(glm::vec4(0)));
		}

unknown's avatar
unknown committed
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
	/*	std::unique_ptr<core::Buffer<gl::BufferType::eShaderStorage>> m_debug_buffer;

		struct debug_data_t
		{
			glm::uvec3 data_02;
			glm::uint data_01;
			glm::uvec4 data_03;
			glm::uvec4 data_05;
			glm::vec4 data_04;

			friend std::ostream& operator<< (std::ostream& stream, const debug_data_t& dd) {
				return stream << "data_01: " << dd.data_01 << std::endl
					<< "data_02: " << dd.data_02.x << ", " << dd.data_02.y << ", " << dd.data_02.z << std::endl
					<< "data_03: " << dd.data_03.x << ", " << dd.data_03.y << ", " << dd.data_03.z << std::endl
					<< "data_04: " << dd.data_04.x << ", " << dd.data_04.y << ", " << dd.data_04.z << std::endl
					<< "data_05: " << dd.data_05.x << ", " << dd.data_05.y << ", " << dd.data_05.z << std::endl;
			}
		};
		std::vector<debug_data_t> debug_data(100000, { {0, 0, 0 }, 0 });*/

unknown's avatar
unknown committed
69
70
71
72
73
74
75
76
77
		const core::TextureRGBA_32F &Raytracer::render(uint32_t width, uint32_t height)
		{
			if (width != m_last_width || height != m_last_height) {
				m_last_width = width;
				m_last_height = height;
				m_trace_buffer->setData(sizeof(Trace) * width * height, gl::BufferUsage::eDynamicCopy);
				m_render_target = std::make_unique<core::TextureRGBA_32F>(width, height);
				m_color_store = std::make_unique<core::TextureRGBA_32F>(width, height);
				m_current_sample = 0;
unknown's avatar
unknown committed
78
79
80
81

			/*	if (!m_debug_buffer) {
					m_debug_buffer = std::make_unique<core::Buffer<gl::BufferType::eShaderStorage>>();
				}*/
unknown's avatar
unknown committed
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
			}
			
			for (unsigned sample = 0; sample < m_samples_per_frame; sample++) {

				if (m_current_sample >= m_max_samples)
					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());

				m_render_shader->updateStorageBuffer("material_buffer", m_collector->materialBuffer());
unknown's avatar
unknown committed
101
102
			//	m_debug_buffer->setData(debug_data, gl::BufferUsage::eDynamicCopy);
				//m_render_shader->updateStorageBuffer("debug_data_buffer", m_debug_buffer);
unknown's avatar
unknown committed
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126

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

				static std::default_random_engine generator;
				static std::uniform_int_distribution<int> distribution(0, std::numeric_limits<int>::max());

				m_render_shader->updateUniformInt("random_seed", distribution(generator));
				m_render_shader->updateUniformUInt("u_render_config.current_sample", m_current_sample);
				m_render_shader->updateUniformUInt("u_render_config.max_samples", m_max_samples);
				m_render_shader->updateUniformUInt("u_render_config.num_bounces", m_bounces);

				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);
				}

				core::ClockGL::instance().start();
				m_render_shader->dispatchCompute2D(width, 4, height, 4);
unknown's avatar
unknown committed
127

unknown's avatar
unknown committed
128
129
130
131
132
133
134
135
136
137
138
			//	glFinish();

			//	auto vec = m_debug_buffer->read<debug_data_t>(debug_data.size());

				//Log_Error << "OJUTPUTATOP --------------------------------------------- ";
				//for (auto &&elem : vec) {
				//	if (elem.data_01 > 3839) {
				//		Log_Error << elem;
				//	}
				//}

unknown's avatar
unknown committed
139
140
141
142
143
144
				++m_current_sample;
			}

			return *m_render_target;
		}

unknown's avatar
unknown committed
145
		void Raytracer::saveRenderHDR(fs::path target) const
unknown's avatar
unknown committed
146
147
148
149
		{
			io::ImageUtilities::saveTexture(target, *m_render_target);
		}

unknown's avatar
unknown committed
150
151
152
153
154
		void Raytracer::saveRender(fs::path target) const
		{
			io::ImageUtilities::saveTextureForceUChar(target, *m_render_target);
		}

unknown's avatar
unknown committed
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
		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;
		}
	}
}