cgv
Loading...
Searching...
No Matches
managed_frame_buffer.cxx
1#include "managed_frame_buffer.h"
2
3namespace cgv {
4namespace render {
5
6managed_frame_buffer::managed_frame_buffer() {
7
8 index_counter = 0;
9 size = ivec2(0);
10}
11
12void managed_frame_buffer::destruct(const context& ctx) {
13
14 fb.destruct(ctx);
15
16 index_counter = 0;
17 for(auto it = attachments.begin(); it != attachments.end(); ++it) {
18 attachment& a = (*it).second;
19 a.tex.destruct(ctx);
20 }
21}
22
23ivec2 managed_frame_buffer::get_size() {
24
25 return ivec2(fb.get_width(), fb.get_height());
26}
27
29
30 this->size = size;
31}
32
33void managed_frame_buffer::add_attachment(const std::string& name, const std::string& format, TextureFilter tf, TextureWrap tw, bool attach) {
34
35 attachment a;
36 a.attach = attach;
37 a.format = format;
38 a.tf = tf;
39 a.tw = tw;
40
41 if(a.is_depth_attachment()) {
42 a.index = 0;
43 } else {
44 a.index = index_counter;
45 ++index_counter;
46 }
47
48 attachments.insert(std::make_pair(name, a));
49}
50
51bool managed_frame_buffer::enable_attachment(context& ctx, const std::string& name, int tex_unit) {
52
53 auto elem = attachments.find(name);
54 if(elem != attachments.end()) {
55 attachment& a = (*elem).second;
56 return a.tex.enable(ctx, tex_unit);
57 }
58
59 return false;
60}
61
62bool managed_frame_buffer::disable_attachment(context& ctx, const std::string& name) {
63
64 auto elem = attachments.find(name);
65 if(elem != attachments.end()) {
66 attachment& a = (*elem).second;
67 return a.tex.disable(ctx);
68 }
69
70 return false;
71}
72
73texture* managed_frame_buffer::attachment_texture_ptr(const std::string& name) {
74
75 auto elem = attachments.find(name);
76 if(elem != attachments.end()) {
77 attachment& a = (*elem).second;
78 return &a.tex;
79 }
80
81 return nullptr;
82}
83
85{
86 int max_size = ctx.get_device_capabilities().max_render_buffer_size;
87 if(max_size > 0 && size.x() > max_size || size.y() > max_size) {
88 std::cerr << "Error: managed_framebuffer::ensure: requested size exceeds maximum supported size" << std::endl;
89 return false;
90 }
91
92 ivec2 actual_size = get_actual_size(ctx);
93 if (!fb.is_created() || fb.get_width() != actual_size.x() || fb.get_height() != actual_size.y())
94 {
95 destruct(ctx);
96 if(!create_and_validate(ctx, actual_size))
97 std::cerr << "Error: managed_framebuffer::ensure: framebuffer not complete" << std::endl;
98 return true;
99 }
100 return false;
101}
102
103bool managed_frame_buffer::enable(context& ctx, bool push_viewport) {
104 if (push_viewport)
105 fb.push_viewport(ctx);
106 bool success = fb.enable(ctx);
107 return success;
108}
109
110bool managed_frame_buffer::disable(context& ctx, bool pop_viewport) {
111 auto result = fb.disable(ctx);
112 if (pop_viewport)
113 fb.pop_viewport(ctx);
114 return result;
115}
116
117bool managed_frame_buffer::create_and_validate(context& ctx, const ivec2& size) {
118
119 fb.create(ctx, size.x(), size.y());
120
121 for(auto it = attachments.begin(); it != attachments.end(); ++it) {
122 attachment& a = (*it).second;
123
124 unsigned filter_specifier = (unsigned)a.tf;
125
126 // even filter specifiers are nearest and odd are linear
127 TextureFilter mag_filter = (filter_specifier & 1) ? TF_LINEAR : TF_NEAREST;
128 // specifiers larger than 1 are using mipmaps
129 bool use_mipmaps = filter_specifier > 1;
130
131 a.tex = texture(a.format, mag_filter, a.tf, a.tw, a.tw);
132 a.tex.create(ctx, TT_2D, size.x(), size.y());
133
134 if(use_mipmaps)
135 a.tex.generate_mipmaps(ctx);
136
137 if(a.is_depth_attachment()) {
138 fb.attach(ctx, a.tex);
139 } else {
140 if(a.attach) {
141 fb.attach(ctx, a.tex, 0, a.index);
142 }
143 }
144 }
145
146 return fb.is_complete(ctx);
147}
148
149ivec2 managed_frame_buffer::get_actual_size(context& ctx) {
150
151 ivec2 actual_size(size);
152 if(size.x() <= 0)
153 actual_size.x() = ctx.get_width();
154 if(size.y() <= 0)
155 actual_size.y() = ctx.get_height();
156 return actual_size;
157}
158
159}
160}
T & y()
second element
Definition fvec.h:160
T & x()
first element
Definition fvec.h:156
base class for all drawables, which is independent of the used rendering API.
Definition context.h:627
bool create(const context &ctx, int _width=-1, int _height=-1)
create framebuffer if extension is supported, otherwise return false.
void push_viewport(context &ctx, const dvec2 &depth_range=dvec2(0, 1))
push a new window transformation to cover the fbo onto the window transformation stack
bool is_complete(const context &ctx) const
check for completeness, if not complete, get the reason in last_error
int get_width() const
return the width
void pop_viewport(context &ctx)
recover the window transformation array active before the last call to push_viewport
void destruct(const context &ctx)
destruct the framebuffer objext
bool attach(const context &ctx, const render_buffer &rb, int i=0)
attach render buffer to depth buffer if it is a depth buffer, to stencil if it is a stencil buffer or...
bool enable(context &ctx, int i0=-1, int i1=-1, int i2=-1, int i3=-1, int i4=-1, int i5=-1, int i6=-1, int i7=-1, int i8=-1, int i9=-1, int i10=-1, int i11=-1, int i12=-1, int i13=-1, int i14=-1, int i15=-1)
enable the framebuffer either with all color attachments if no arguments are given or if arguments ar...
bool disable(context &ctx)
disable the framebuffer object
int get_height() const
return the height
bool ensure(context &ctx)
Ensure the framebuffer is constructed with the specified size.
void set_size(const ivec2 &size)
Set the size of the framebuffer attachments.
virtual bool is_created() const
return whether component has been created
Definition context.cxx:2065
TextureFilter
different texture filter
Definition context.h:198
TextureWrap
different texture wrap modes
Definition context.h:182
the cgv namespace
Definition print.h:11
cgv::math::fvec< int32_t, 2 > ivec2
declare type of 2d 32 bit integer vectors
Definition fvec.h:695