]> git.tdb.fi Git - libs/gl.git/blob - source/render/rendertarget.cpp
Add support for integer vertex attributes
[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(const UInt16 *i=f.begin(); i!=f.end(); ++i)
21         {
22                 FrameAttachment fa = static_cast<FrameAttachment>(*i);
23                 PixelFormat pf = get_attachment_pixelformat(*i);
24
25                 if(samples>1)
26                 {
27                         Texture2DMultisample *tex2d_ms = new Texture2DMultisample;
28                         tex2d_ms->storage(pf, width, height, samples);
29                         fbo.attach(fa, *tex2d_ms);
30                         textures.push_back(tex2d_ms);
31                 }
32                 else
33                 {
34                         Texture2D *tex2d = new Texture2D;
35                         tex2d->storage(pf, width, height, 1);
36                         fbo.attach(fa, *tex2d);
37                         textures.push_back(tex2d);
38                 }
39         }
40 }
41
42 RenderTarget::~RenderTarget()
43 {
44         for(vector<Texture *>::iterator i=textures.begin(); i!=textures.end(); ++i)
45                 delete *i;
46 }
47
48 const Texture2D &RenderTarget::get_target_texture(unsigned i) const
49 {
50         if(i>=textures.size())
51                 throw out_of_range("RenderTarget::get_target_texture");
52         if(fbo.get_format().get_samples()>1)
53                 throw invalid_operation("RenderTarget::get_target_texture");
54
55         return *static_cast<const Texture2D *>(textures[i]);
56 }
57
58 const Texture2D &RenderTarget::get_target_texture(FrameAttachment fa) const
59 {
60         int index = fbo.get_format().index(fa);
61         if(index<0)
62                 throw key_error(fa);
63
64         return get_target_texture(index);
65 }
66
67 void RenderTarget::set_debug_name(const string &name)
68 {
69 #ifdef DEBUG
70         fbo.set_debug_name(name+" [FBO]");
71         const FrameFormat &fmt = fbo.get_format();
72         unsigned i = 0;
73         for(const UInt16 *j=fmt.begin(); j!=fmt.end(); ++i, ++j)
74         {
75                 unsigned attach_pt = get_attach_point(static_cast<FrameAttachment>(*j));
76
77                 string tex_name;
78                 if(attach_pt==get_attach_point(DEPTH_ATTACHMENT))
79                         tex_name = name+"/depth";
80                 else if(attach_pt==get_attach_point(STENCIL_ATTACHMENT))
81                         tex_name = name+"/stencil";
82                 else
83                         tex_name = Msp::format("%s/color%d", name, attach_pt);
84
85                 textures[i]->set_debug_name(tex_name+".tex2d");
86         }
87 #else
88         (void)name;
89 #endif
90 }
91
92 } // namespace GL
93 } // namespace Msp