4 #include "rendertarget.h" 6 #include "../core/log.h" 8 void RenderTarget::update(
int width,
int height,
int samples) {
9 samples = std::max(1, samples);
10 if (_watcher.changed(width, height, samples)) {
11 std::lock_guard<std::mutex> lock(_mutex);
15 int gl_max_samples = 1;
16 int gl_max_integer_samples = 1;
17 V_GL(glGetIntegerv(GL_MAX_SAMPLES, &gl_max_samples));
18 V_GL(glGetIntegerv(GL_MAX_INTEGER_SAMPLES, &gl_max_integer_samples));
19 samples = std::min(samples, gl_max_samples);
20 samples = std::min(samples, gl_max_integer_samples);
25 _depth_buffer.update(width, height, GL_DEPTH_COMPONENT, samples);
27 _pick_id.update(width, height, GL_R32UI);
28 _pick_depth.update(width, height, GL_DEPTH_COMPONENT);
29 _pick_framebuffer.attach(_pick_id, GL_COLOR_ATTACHMENT0);
30 _pick_framebuffer.attach(_pick_depth, GL_DEPTH_ATTACHMENT);
32 _pick_framebuffer.bind();
33 std::vector<GLenum> buffers = {GL_NONE, GL_NONE, GL_COLOR_ATTACHMENT0};
34 V_GL(glDrawBuffers(3, buffers.data()));
37 _opaque_texture.update(width, height, GL_RGBA16F, samples);
38 _opaque_framebuffer.attach(_depth_buffer, GL_DEPTH_ATTACHMENT);
39 _opaque_framebuffer.attach(_opaque_texture, GL_COLOR_ATTACHMENT0);
41 _transparent_texture_head.update(width, height, GL_RGBA16F, samples);
42 _transparent_framebuffer_head.attach(_depth_buffer, GL_DEPTH_ATTACHMENT);
43 _transparent_framebuffer_head.attach(_transparent_texture_head,
44 GL_COLOR_ATTACHMENT0);
46 _transparent_texture_tail_color.update(width, height, GL_RGBA16F, samples);
47 _transparent_texture_tail_alpha.update(width, height, GL_R16F, samples);
48 _transparent_framebuffer_tail.attach(_transparent_texture_tail_color,
49 GL_COLOR_ATTACHMENT0);
50 _transparent_framebuffer_tail.attach(_transparent_texture_tail_alpha,
51 GL_COLOR_ATTACHMENT1);
52 _transparent_framebuffer_tail.attach(_depth_buffer, GL_DEPTH_ATTACHMENT);
54 _transparent_framebuffer_tail.bind();
55 std::vector<GLenum> buffers = {GL_COLOR_ATTACHMENT0,
56 GL_COLOR_ATTACHMENT1};
57 V_GL(glDrawBuffers(2, buffers.data()));
60 _front_colorbuffer.update(width, height, GL_SRGB8);
69 void RenderTarget::bind() { _opaque_framebuffer.bind(); }
71 void RenderTarget::present(
int target) {
72 std::lock_guard<std::mutex> lock(_mutex);
74 if (!_present_framebuffer.id()) {
75 _present_framebuffer.create();
78 _present_framebuffer.bind();
79 _present_framebuffer.attach(_front_colorbuffer, GL_COLOR_ATTACHMENT0);
81 V_GL(glBindFramebuffer(GL_READ_FRAMEBUFFER, _present_framebuffer.id()));
82 V_GL(glBindFramebuffer(GL_DRAW_FRAMEBUFFER, target));
83 V_GL(glBlitFramebuffer(0, 0, _width, _height, 0, 0, _width, _height,
84 GL_COLOR_BUFFER_BIT, GL_NEAREST));
86 _present_framebuffer.bind();
87 V_GL(glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
90 V_GL(glBindFramebuffer(GL_FRAMEBUFFER, 0));