]> git.tdb.fi Git - libs/gl.git/blob - source/render/rendertarget.cpp
Rework multisample resolve to use resolve attachments
[libs/gl.git] / source / render / rendertarget.cpp
1 #include <msp/core/maputils.h>
2 #include <msp/strings/format.h>
3 #include "error.h"
4 #include "rendertarget.h"
5 #include "texture2d.h"
6 #include "texture2dmultisample.h"
7
8 using namespace std;
9
10 namespace Msp {
11 namespace GL {
12
13 RenderTarget::RenderTarget(unsigned w, unsigned h, const FrameFormat &f):
14         width(w),
15         height(h),
16         fbo(f)
17 {
18         bool multisample = (f.get_samples()>1);
19         textures.reserve(f.size()*(1+multisample));
20         unsigned samples = f.get_samples();
21         for(FrameAttachment a: f)
22         {
23                 PixelFormat pf = get_attachment_pixelformat(a);
24
25                 Texture2D *tex2d = new Texture2D;
26                 tex2d->storage(pf, width, height, 1);
27
28                 if(multisample)
29                 {
30                         Texture2DMultisample *tex2d_ms = new Texture2DMultisample;
31                         tex2d_ms->storage(pf, width, height, samples);
32                         fbo.attach(a, *tex2d_ms, tex2d);
33                         textures.push_back(tex2d_ms);
34                         textures.push_back(tex2d);
35                 }
36                 else
37                 {
38                         fbo.attach(a, *tex2d);
39                         textures.push_back(tex2d);
40                 }
41         }
42 }
43
44 RenderTarget::~RenderTarget()
45 {
46         for(Texture *t: textures)
47                 delete t;
48 }
49
50 const Texture2D &RenderTarget::get_target_texture(unsigned i) const
51 {
52         if(i>=textures.size())
53                 throw out_of_range("RenderTarget::get_target_texture");
54         if(fbo.get_format().get_samples()>1)
55                 i = i*2+1;
56
57         return *static_cast<const Texture2D *>(textures[i]);
58 }
59
60 const Texture2D &RenderTarget::get_target_texture(FrameAttachment fa) const
61 {
62         int index = fbo.get_format().index(fa);
63         if(index<0)
64                 throw key_error(fa);
65
66         return get_target_texture(index);
67 }
68
69 void RenderTarget::set_debug_name(const string &name)
70 {
71 #ifdef DEBUG
72         fbo.set_debug_name(name+" [FBO]");
73         bool multisample = (fbo.get_format().get_samples()>1);
74         unsigned i = 0;
75         for(FrameAttachment a: fbo.get_format())
76         {
77                 unsigned attach_pt = get_attach_point(a);
78
79                 string tex_name;
80                 if(attach_pt==get_attach_point(DEPTH_ATTACHMENT))
81                         tex_name = name+"/depth";
82                 else if(attach_pt==get_attach_point(STENCIL_ATTACHMENT))
83                         tex_name = name+"/stencil";
84                 else
85                         tex_name = Msp::format("%s/color%d", name, attach_pt);
86
87                 textures[i++]->set_debug_name(tex_name+".tex");
88                 if(multisample)
89                         textures[i++]->set_debug_name(tex_name+"_resolve.tex");
90         }
91 #else
92         (void)name;
93 #endif
94 }
95
96 } // namespace GL
97 } // namespace Msp