Change various generated texture names to use the unified extension
[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         textures.reserve(f.size());
19         unsigned samples = f.get_samples();
20         for(FrameAttachment a: f)
21         {
22                 PixelFormat pf = get_attachment_pixelformat(a);
23
24                 if(samples>1)
25                 {
26                         Texture2DMultisample *tex2d_ms = new Texture2DMultisample;
27                         tex2d_ms->storage(pf, width, height, samples);
28                         fbo.attach(a, *tex2d_ms);
29                         textures.push_back(tex2d_ms);
30                 }
31                 else
32                 {
33                         Texture2D *tex2d = new Texture2D;
34                         tex2d->storage(pf, width, height, 1);
35                         fbo.attach(a, *tex2d);
36                         textures.push_back(tex2d);
37                 }
38         }
39 }
40
41 RenderTarget::~RenderTarget()
42 {
43         for(Texture *t: textures)
44                 delete t;
45 }
46
47 const Texture2D &RenderTarget::get_target_texture(unsigned i) const
48 {
49         if(i>=textures.size())
50                 throw out_of_range("RenderTarget::get_target_texture");
51         if(fbo.get_format().get_samples()>1)
52                 throw invalid_operation("RenderTarget::get_target_texture");
53
54         return *static_cast<const Texture2D *>(textures[i]);
55 }
56
57 const Texture2D &RenderTarget::get_target_texture(FrameAttachment fa) const
58 {
59         int index = fbo.get_format().index(fa);
60         if(index<0)
61                 throw key_error(fa);
62
63         return get_target_texture(index);
64 }
65
66 void RenderTarget::set_debug_name(const string &name)
67 {
68 #ifdef DEBUG
69         fbo.set_debug_name(name+" [FBO]");
70         unsigned i = 0;
71         for(FrameAttachment a: fbo.get_format())
72         {
73                 unsigned attach_pt = get_attach_point(a);
74
75                 string tex_name;
76                 if(attach_pt==get_attach_point(DEPTH_ATTACHMENT))
77                         tex_name = name+"/depth";
78                 else if(attach_pt==get_attach_point(STENCIL_ATTACHMENT))
79                         tex_name = name+"/stencil";
80                 else
81                         tex_name = Msp::format("%s/color%d", name, attach_pt);
82
83                 textures[i++]->set_debug_name(tex_name+".tex");
84         }
85 #else
86         (void)name;
87 #endif
88 }
89
90 } // namespace GL
91 } // namespace Msp