1 #include <msp/core/maputils.h>
3 #include "renderbuffer.h"
4 #include "rendertarget.h"
11 RenderTargetFormat::RenderTargetFormat():
15 RenderTargetFormat::RenderTargetFormat(RenderOutput o):
21 RenderTargetFormat RenderTargetFormat::operator,(RenderOutput o) const
23 if(count>=MAX_OUTPUTS)
24 throw invalid_operation("RenderTargetFormat::operator,");
26 RenderTargetFormat result = *this;
27 result.outputs[result.count++] = o;
32 RenderTargetFormat RenderTargetFormat::operator,(PixelFormat f) const
35 throw invalid_operation("RenderTargetFormat::operator,");
37 PixelComponents comp = get_components(f);
38 unsigned size = get_component_size(f);
39 unsigned char out = outputs[count-1];
40 if(get_output_type(out)>=get_output_type(RENDER_DEPTH))
42 if(comp!=DEPTH_COMPONENT)
43 throw invalid_argument("RenderTargetFormat::operator,");
46 if(get_component_type(f)==UNSIGNED_INT)
51 if(comp!=RED && comp!=RG && comp!=RGB && comp!=RGBA)
52 throw invalid_argument("RenderTargetformat::operator,");
57 out = (out&~15) | (size<<2) | (get_component_count(f)-1);
58 RenderTargetFormat result = *this;
59 result.outputs[result.count-1] = out;
64 int RenderTargetFormat::index(RenderOutput o) const
66 unsigned type = get_output_type(o);
68 for(const unsigned char *j=begin(); j!=end(); ++j, ++i)
69 if(get_output_type(*j)==type)
75 PixelFormat get_output_pixelformat(unsigned char o)
79 if(get_output_type(o)>=get_output_type(RENDER_DEPTH))
81 static DataType types[4] = { UNSIGNED_SHORT, UNSIGNED_SHORT, UNSIGNED_INT, FLOAT };
82 comp = DEPTH_COMPONENT;
83 type = types[(o>>2)&3];
87 static PixelComponents components[4] = { RED, RG, RGB, RGBA };
88 static DataType types[4] = { UNSIGNED_BYTE, UNSIGNED_SHORT, HALF_FLOAT, FLOAT };
89 comp = components[o&3];
90 type = types[(o>>2)&3];
93 return make_pixelformat(comp, type);
97 RenderTarget::RenderTarget(unsigned w, unsigned h, RenderOutput o)
102 RenderTarget::RenderTarget(unsigned w, unsigned h, const RenderTargetFormat &f)
107 RenderTarget::RenderTarget(unsigned w, unsigned h, unsigned s, const RenderTargetFormat &f)
112 void RenderTarget::init(unsigned w, unsigned h, unsigned s, const RenderTargetFormat &f)
119 for(const unsigned char *i=format.begin(); i!=format.end(); ++i)
121 unsigned type = get_output_type(*i);
122 FramebufferAttachment att;
123 if(type>=get_output_type(RENDER_DEPTH))
124 att = DEPTH_ATTACHMENT;
126 att = static_cast<FramebufferAttachment>(COLOR_ATTACHMENT0+type);
128 PixelFormat pf = get_output_pixelformat(*i);
133 tgt.buffer = new Renderbuffer;
134 tgt.buffer->storage_multisample(samples, pf, width, height);
135 fbo.attach(att, *tgt.buffer);
139 tgt.texture = new Texture2D;
140 tgt.texture->storage(pf, width, height, 1);
141 Sampler &sampler = tgt.texture->get_default_sampler();
142 sampler.set_filter(NEAREST);
143 sampler.set_wrap(CLAMP_TO_EDGE);
144 fbo.attach(att, *tgt.texture);
146 buffers.push_back(tgt);
149 fbo.require_complete();
152 RenderTarget::~RenderTarget()
154 for(vector<TargetBuffer>::iterator i=buffers.begin(); i!=buffers.end(); ++i)
163 void RenderTarget::set_texture_filter(TextureFilter filt)
167 for(vector<TargetBuffer>::iterator i=buffers.begin(); i!=buffers.end(); ++i)
168 i->texture->get_default_sampler().set_filter(filt);
172 const Texture2D &RenderTarget::get_target_texture(unsigned i) const
174 if(i>=buffers.size())
175 throw out_of_range("RenderTarget::get_target_texture");
177 throw invalid_operation("RenderTarget::get_target_texture");
179 return *buffers[i].texture;
182 const Texture2D &RenderTarget::get_target_texture(RenderOutput o) const
184 int index = format.index(o);
188 return get_target_texture(index);
191 void RenderTarget::blit_from(const RenderTarget &other)
193 fbo.blit_from(other.fbo, COLOR_BUFFER_BIT|DEPTH_BUFFER_BIT, false);