RayGenerator.cpp 4.7 KB
Newer Older
unknown's avatar
unknown committed
1
2
#include "RayGenerator.h"
#include "Raytracer.h"
3
4
#include <engine/rendering/OpenGLState.h>
#include <util/files.h>
unknown's avatar
unknown committed
5
6
7
8
9

namespace glare
{
	namespace advanced
	{
10
11
12
13
14
15
16
17
18
		uint32_t RayGeneratorBase::randomSeed()
		{
			static std::random_device rd;
			static std::mt19937 gen(rd());
			static std::uniform_int_distribution<uint32_t> dist(0, std::numeric_limits<uint32_t>::max());

			return dist(gen);
		}

unknown's avatar
unknown committed
19
20
21
22
23
24
25
26
27
28
29
30
		void RayGeneratorBase::generate(Raytracer &raytracer)
		{
			m_updated_size = false;
			if (raytracer.width() != m_last_width || raytracer.height() != m_last_height) {
				m_last_width = raytracer.width();
				m_last_height = raytracer.height();
				m_updated_size = true;
			}
		}

		RayGeneratorDefault::RayGeneratorDefault()
		{
31
			m_generator_shader_default = core::ShaderProgram::makeUnique(files::shader("/raytracer/generators/bvh/bvh_raygenerator.compute").string());
unknown's avatar
unknown committed
32
33
34
35
36
37
38
39
40
		}

		void RayGeneratorDefault::generate(Raytracer &raytracer)
		{
			if (!m_generator_shader_default)
				return;

			RayGeneratorBase::generate(raytracer);

41
42
43
			m_generator_shader_default->use();

			m_generator_shader_default->updateUniformImageRGBA32F("u_render_target", raytracer.renderTarget(), gl::Access::eReadWrite);
unknown's avatar
unknown committed
44
			m_generator_shader_default->updateStorageBuffer("trace_buffer", raytracer.traceBuffer());
45
			m_generator_shader_default->updateUniformInt("random_seed", randomSeed());
unknown's avatar
unknown committed
46
			m_generator_shader_default->updateUniformStruct("u_camera", raytracer.collector()->getActiveCamera());
47
48

			m_generator_shader_default->updateStorageBuffer("mesh_buffer", raytracer.collector()->meshBuffer());
unknown's avatar
unknown committed
49
50
			m_generator_shader_default->updateStorageBuffer("object_bvh_buffer", raytracer.collector()->objectBVHNodeBuffer());
			m_generator_shader_default->updateStorageBuffer("object_bvh_id_buffer", raytracer.collector()->objectBVHIDBuffer());
unknown's avatar
unknown committed
51
52
53
54
55
56
57

			m_generator_shader_default->dispatchCompute2D(m_last_width, 12, m_last_height, 12);
		}
		
		RayGeneratorGBuffer::RayGeneratorGBuffer()
		{
			std::vector<std::shared_ptr<glare::core::Shader>> shaders = {
58
59
60
				std::make_shared<glare::core::Shader>(gl::ShaderType::eVertex, files::shader("raytracer/generators/gbuffer/gbuffer.vert")),
				std::make_shared<glare::core::Shader>(gl::ShaderType::eGeometry, files::shader("raytracer/generators/gbuffer/gbuffer.geom")),
				std::make_shared<glare::core::Shader>(gl::ShaderType::eFragment, files::shader("raytracer/generators/gbuffer/gbuffer.frag"))
unknown's avatar
unknown committed
61
62
			};
			m_generator_shader_gbuffer = std::make_unique<core::ShaderProgram>(shaders);
63
			m_buffer_depthtest = core::ShaderProgram::makeUnique(files::shader("raytracer/generators/gbuffer/depthtest.compute").string());
unknown's avatar
unknown committed
64
65
66
67
68
69
70
71
			m_vertex_array = std::make_unique<core::VertexArray>();
		}

		void RayGeneratorGBuffer::generate(Raytracer &raytracer)
		{
			if (raytracer.width() != m_last_width || raytracer.height() != m_last_height) {
				m_last_width = raytracer.width();
				m_last_height = raytracer.height();
72
73
74
				m_framebuffer = std::make_unique<core::Framebuffer<core::Texture>>(raytracer.width(), raytracer.height());
				m_framebuffer->attach(gl::Attachment::eColor0);
				m_framebuffer->attach(gl::Attachment::eDepth);
unknown's avatar
unknown committed
75
76
77
78
79
80
81
82
83
84
85
86
87
88
			}

			m_generator_shader_gbuffer->updateStorageBuffer("mesh_buffer", raytracer.collector()->meshBuffer());

			core::OpenGLState::capture();

			m_framebuffer->activate();
			gl::setEnabled(gl::EnableParameter::eBlend, false);
			gl::setEnabled(gl::EnableParameter::eDepthTest, true);
			gl::setEnabled(gl::EnableParameter::eCullFace, false);
			gl::clearColor(0, 0, 0, 0);
			gl::clear(gl::ClearBufferBits::eDepth | gl::ClearBufferBits::eColor);

			for (auto &&pair : raytracer.collector()->meshCollectors()) {
unknown's avatar
unknown committed
89
				auto id = raytracer.collector()->getIdMapping().at(pair.first);
90
				m_generator_shader_gbuffer->updateUniformUInt("u_mesh_id", unsigned(id));
's avatar
committed
91
				m_generator_shader_gbuffer->updateUniformMatrix("u_mvp", core::EngineState::main_camera->getViewProjection() * pair.second->m_transform);
92
93
94
95
96

				m_vertex_array->bind();
				gl::drawArrays(gl::PrimitiveType::ePoints, 0, pair.second->triangles().size());
				m_vertex_array->unbind();

unknown's avatar
unknown committed
97
98
99
100
101
102
103
			}

			gl::memoryBarrier(gl::MemoryBarrierBit::eAll);
			m_framebuffer->deactivate();

			core::OpenGLState::reset();
			
104
			m_buffer_depthtest->updateUniformImageRGBA32F("u_gbuffer_texture_01", m_framebuffer->colorAttachment(gl::Attachment::eColor0), gl::Access::eReadOnly);
unknown's avatar
unknown committed
105
106
107
			m_buffer_depthtest->updateUniformImageRGBA32F("u_render_target", raytracer.renderTarget(), gl::Access::eReadWrite);
			m_buffer_depthtest->updateStorageBuffer("trace_buffer", raytracer.traceBuffer());
			m_buffer_depthtest->updateUniformStruct("u_camera", raytracer.collector()->getActiveCamera());
108
			m_buffer_depthtest->updateUniformInt("random_seed", randomSeed());
unknown's avatar
unknown committed
109
110
111
112
113
			m_buffer_depthtest->dispatchCompute(m_last_width*m_last_height, 1024);
		}

	}
}