]> git.tdb.fi Git - libs/gl.git/blob - source/core/bufferable.cpp
Remove RenderBuffer and always use textures as framebuffer attachments
[libs/gl.git] / source / core / bufferable.cpp
1 #include <stdexcept>
2 #include <msp/core/raii.h>
3 #include <msp/gl/extensions/arb_direct_state_access.h>
4 #include "buffer.h"
5 #include "bufferable.h"
6
7 using namespace std;
8
9 namespace Msp {
10 namespace GL {
11
12 Bufferable::Bufferable():
13         buffer(0),
14         offset(0),
15         next_in_buffer(0),
16         prev_in_buffer(0),
17         location_dirty(false),
18         dirty(false)
19 { }
20
21 Bufferable::~Bufferable()
22 {
23         unlink_from_buffer();
24 }
25
26 void Bufferable::use_buffer(Buffer *buf, Bufferable *prev)
27 {
28         if(prev && buf!=prev->buffer)
29                 throw invalid_argument("Bufferable::use_buffer");
30
31         if(buffer)
32                 unlink_from_buffer();
33
34         buffer = buf;
35         if(buffer)
36         {
37                 prev_in_buffer = prev;
38                 if(prev_in_buffer)
39                 {
40                         next_in_buffer = prev_in_buffer->next_in_buffer;
41                         prev_in_buffer->next_in_buffer = this;
42                 }
43         }
44
45         location_dirty = true;
46         dirty = true;
47         update_offset();
48 }
49
50 void Bufferable::change_buffer(Buffer *buf)
51 {
52         for(Bufferable *b=this; b; b=b->next_in_buffer)
53         {
54                 b->buffer = buf;
55                 b->location_dirty = true;
56                 b->dirty = true;
57         }
58         for(Bufferable *b=prev_in_buffer; b; b=b->prev_in_buffer)
59         {
60                 b->buffer = buf;
61                 b->location_dirty = true;
62                 b->dirty = true;
63         }
64 }
65
66 unsigned Bufferable::get_required_buffer_size() const
67 {
68         const Bufferable *last = this;
69         for(; last->next_in_buffer; last=last->next_in_buffer) ;
70         return last->offset+last->get_data_size();
71 }
72
73 Bufferable::AsyncUpdater *Bufferable::refresh_async() const
74 {
75         return dirty ? new AsyncUpdater(*this) : 0;
76 }
77
78 void Bufferable::unlink_from_buffer()
79 {
80         if(prev_in_buffer)
81                 prev_in_buffer->next_in_buffer = next_in_buffer;
82         if(next_in_buffer)
83         {
84                 next_in_buffer->prev_in_buffer = prev_in_buffer;
85                 next_in_buffer->update_offset();
86         }
87         prev_in_buffer = 0;
88         next_in_buffer = 0;
89         buffer = 0;
90         offset = 0;
91 }
92
93 void Bufferable::update_offset()
94 {
95         unsigned new_offset = 0;
96         if(prev_in_buffer)
97                 new_offset = prev_in_buffer->offset+prev_in_buffer->get_data_size();
98
99         unsigned align = get_alignment();
100         new_offset += align-1;
101         new_offset -= new_offset%align;
102         if(new_offset!=offset)
103         {
104                 offset = new_offset;
105                 location_dirty = true;
106                 dirty = true;
107         }
108
109         if(next_in_buffer)
110                 next_in_buffer->update_offset();
111         else if(buffer && offset+get_data_size()>buffer->get_size())
112         {
113                 location_dirty = true;
114                 dirty = true;
115         }
116 }
117
118 void Bufferable::upload_data(char *target) const
119 {
120         unsigned data_size = get_data_size();
121         if(location_dirty)
122         {
123                 buffer->require_size(offset+data_size);
124                 location_changed(buffer, offset, data_size);
125                 location_dirty = false;
126         }
127
128         if(target)
129         {
130                 const char *source = reinterpret_cast<const char *>(get_data_pointer());
131                 copy(source, source+data_size, target);
132         }
133         else
134                 buffer->sub_data(offset, data_size, get_data_pointer());
135         dirty = false;
136 }
137
138
139 Bufferable::AsyncUpdater::AsyncUpdater(const Bufferable &b):
140         bufferable(b)
141 {
142         bufferable.buffer->require_size(bufferable.get_required_buffer_size());
143         mapped_address = reinterpret_cast<char *>(bufferable.buffer->map());
144 }
145
146 Bufferable::AsyncUpdater::~AsyncUpdater()
147 {
148         bufferable.buffer->unmap();
149 }
150
151 void Bufferable::AsyncUpdater::upload_data()
152 {
153         bufferable.upload_data(mapped_address+bufferable.offset);
154         // Update all bufferables in the same buffer at once
155         for(const Bufferable *b=bufferable.prev_in_buffer; b; b=b->prev_in_buffer)
156                 if(b->dirty)
157                         b->upload_data(mapped_address+b->offset);
158         for(const Bufferable *b=bufferable.next_in_buffer; b; b=b->next_in_buffer)
159                 if(b->dirty)
160                         b->upload_data(mapped_address+b->offset);
161 }
162
163 } // namespace GL
164 } // namespace Msp