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