gbuffer.cpp 5.24 KB
Newer Older
unknown's avatar
unknown committed
1
2
3
4
5
#include "GBuffer.h" 
#include <core/state.h>
#include <core/numeric/flags.h>
#include <util/log.h>
#include <util/files.h>
6
#include <core/objects/light.h>
unknown's avatar
unknown committed
7
8
9
10
11
12
13
14
15
16
17
18
#include <core/rendering/light_manager.h>

namespace glare
{
	namespace core
	{
		GBuffer::GBuffer(unsigned int width, unsigned int height, unsigned samples) : m_width(width), m_height(height)
		{
			const std::vector<std::shared_ptr<glare::core::Shader>> shaders = {
				std::make_shared<glare::core::Shader>(gl::ShaderType::eVertex, files::shader("screenshader/screen_triangle.vert")),
				std::make_shared<glare::core::Shader>(gl::ShaderType::eFragment, files::shader("screenshader/gbuffer/gbuffer.frag"))
			};
Johannes Braun's avatar
Johannes Braun committed
19
			m_texture_renderer = std::make_unique<TextureRenderer>(std::make_shared<Program>(shaders));
unknown's avatar
unknown committed
20
21
22
23
24
25
26
27
28
			
			m_gbuffer_framebuffer = std::make_shared<Framebuffer<TextureMultisampled>>(width, height, samples);
			m_gbuffer_framebuffer->attach(gl::Attachment::eColor0);
			m_gbuffer_framebuffer->attach(gl::Attachment::eColor1);
			m_gbuffer_framebuffer->attach(gl::Attachment::eColor2);
			m_gbuffer_framebuffer->attach(gl::Attachment::eColor3);
			m_gbuffer_framebuffer->attach(gl::Attachment::eColor4);
			m_gbuffer_framebuffer->attach(gl::Attachment::eColor5);
			m_gbuffer_framebuffer->attach(gl::Attachment::eColor6);
29
			m_gbuffer_framebuffer->attach(gl::Attachment::eDepthStencil);
unknown's avatar
unknown committed
30
31
			
			m_lights_buffer = std::make_unique<Buffer<gl::BufferType::eShaderStorage>>();
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46

			//Update all uniforms that won't change anymore...
			m_texture_renderer->shader().uniform("u_gbuffer.depth", m_gbuffer_framebuffer->depthAttachment().makeTextureResident());
			m_texture_renderer->shader().uniform("u_gbuffer.diffuse", m_gbuffer_framebuffer->colorAttachment(gl::Attachment::eColor0).makeTextureResident());
			m_texture_renderer->shader().uniform("u_gbuffer.normals", m_gbuffer_framebuffer->colorAttachment(gl::Attachment::eColor1).makeTextureResident());
			m_texture_renderer->shader().uniform("u_gbuffer.specular", m_gbuffer_framebuffer->colorAttachment(gl::Attachment::eColor2).makeTextureResident());
			m_texture_renderer->shader().uniform("u_gbuffer.id", m_gbuffer_framebuffer->colorAttachment(gl::Attachment::eColor3).makeTextureResident());
			m_texture_renderer->shader().uniform("u_gbuffer.position", m_gbuffer_framebuffer->colorAttachment(gl::Attachment::eColor4).makeTextureResident());
			m_texture_renderer->shader().uniform("u_gbuffer.position_raw", m_gbuffer_framebuffer->colorAttachment(gl::Attachment::eColor5).makeTextureResident());
			m_texture_renderer->shader().uniform("u_gbuffer.lighting", m_gbuffer_framebuffer->colorAttachment(gl::Attachment::eColor6).makeTextureResident());

			// Those will only be later updated at will when resizing.
			m_texture_renderer->shader().uniform("screen.width", m_width);
			m_texture_renderer->shader().uniform("screen.height", m_height);
			m_texture_renderer->shader().uniform("u_samples", m_gbuffer_framebuffer->samples());
47
48
49
50
51

			m_texture_renderer->shader().bindSubroutine(gl::ShaderType::eFragment, "u_light_sample[0]", "samplePoint");
			m_texture_renderer->shader().bindSubroutine(gl::ShaderType::eFragment, "u_light_sample[1]", "sampleSpot");
			m_texture_renderer->shader().bindSubroutine(gl::ShaderType::eFragment, "u_light_sample[2]", "sampleDirectional");
			m_texture_renderer->shader().bindSubroutine(gl::ShaderType::eFragment, "u_light_sample[3]", "sampleAmbient");
unknown's avatar
unknown committed
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
		}

		GBuffer::~GBuffer()
		{
		}

		void GBuffer::record(std::function<void()> render_function) const
		{
			activate();
			render_function();
			deactivate();
		}

		void GBuffer::activate() const
		{
			m_gbuffer_framebuffer->activate();
			gl::clear(gl::ClearBufferBits::eColor | gl::ClearBufferBits::eDepth);
		}

		void GBuffer::deactivate() const
		{
			m_gbuffer_framebuffer->deactivate();
		}

		void GBuffer::draw() const
		{
			gl::setEnabled(gl::EnableParameter::eDepthTest, false);
			m_texture_renderer->shader().use();

			m_lights_buffer->upload(LightManager::getInstance().storage(), gl::BufferUsage::eDynamicCopy);
Johannes Braun's avatar
Johannes Braun committed
82
			m_texture_renderer->shader().storageBuffer("lightsBuffer", *m_lights_buffer);
unknown's avatar
unknown committed
83

84
85
86
87
			LightManager::getInstance().clear();
			m_texture_renderer->shader().uniform("u_view", state::camera->getView());
			m_texture_renderer->shader().uniform("u_projection", state::camera->getProjection());
			m_texture_renderer->shader().uniform("camera_position", glm::vec3(state::camera->getOwner()->absoluteTransform() * glm::vec4(0,0,0,1)));
unknown's avatar
unknown committed
88
89
90
91
92
93
94
			m_texture_renderer->draw();
			gl::setEnabled(gl::EnableParameter::eDepthTest, true);
		}

		void GBuffer::setSkybox(std::shared_ptr<Skybox> skybox)
		{
			m_skybox = skybox;
95
			m_texture_renderer->shader().uniform("environment", m_skybox->makeResident());
unknown's avatar
unknown committed
96
97
98
99
100
101
102
103
		}

		const Framebuffer<TextureMultisampled>& GBuffer::framebuffer() const
		{
			return *m_gbuffer_framebuffer;
		}

		void GBuffer::updateSize(unsigned width, unsigned height)
104
105
106
107
108
		{
			updateSize(width, height, m_gbuffer_framebuffer->samples());
		}

		void GBuffer::updateSize(unsigned width, unsigned height, unsigned samples)
unknown's avatar
unknown committed
109
110
111
		{
			m_width = width;
			m_height = height;
112
113
114
115
			m_gbuffer_framebuffer->resize(width, height, samples);
			m_texture_renderer->shader().uniform("screen.width", m_width);
			m_texture_renderer->shader().uniform("screen.height", m_height);
			m_texture_renderer->shader().uniform("u_samples", samples);
unknown's avatar
unknown committed
116
117
118
		}
	}
}