]> git.tdb.fi Git - libs/gl.git/blob - source/core/buffer.cpp
Check the flat qualifier from the correct member
[libs/gl.git] / source / core / buffer.cpp
1 #include <stdexcept>
2 #include <msp/strings/format.h>
3 #include "buffer.h"
4 #include "error.h"
5
6 using namespace std;
7
8 namespace Msp {
9 namespace GL {
10
11 void Buffer::storage(size_t sz, BufferUsage u)
12 {
13         if(size>0)
14         {
15                 if(sz!=size)
16                         throw incompatible_data("Buffer::storage");
17                 return;
18         }
19         if(sz==0)
20                 throw invalid_argument("Buffer::storage");
21
22         size = sz;
23         usage = u;
24
25         allocate();
26 }
27
28 void Buffer::data(const void *d)
29 {
30         return sub_data(0, size, d);
31 }
32
33 void Buffer::sub_data(size_t off, size_t sz, const void *d)
34 {
35         check_sub_data(off, sz, "Buffer::sub_data");
36         BufferBackend::sub_data(off, sz, d);
37 }
38
39 Buffer::AsyncTransfer Buffer::sub_data_async(size_t off, size_t sz)
40 {
41         check_sub_data(off, sz, "Buffer::sub_data_async");
42         return AsyncTransfer(*this, off, sz);
43 }
44
45 void Buffer::check_sub_data(size_t off, size_t sz, const char *func)
46 {
47         if(size==0)
48                 throw invalid_operation(func);
49         if(off>get_total_size() || off%size+sz>size)
50                 throw out_of_range(func);
51 }
52
53 void Buffer::require_size(size_t req_sz) const
54 {
55         if(size<req_sz)
56                 throw buffer_too_small(format("buffer has %d bytes; %d required", size, req_sz));
57 }
58
59 void *Buffer::map()
60 {
61         if(size==0 || !can_map() || mapped)
62                 throw invalid_operation("Buffer::map");
63         void *result = BufferBackend::map();
64         mapped = true;
65         return result;
66 }
67
68 bool Buffer::unmap()
69 {
70         if(size==0 || !can_map() || !mapped)
71                 throw invalid_operation("Buffer::unmap");
72         bool result = BufferBackend::unmap();
73         mapped = false;
74         return result;
75 }
76
77
78 Buffer::AsyncTransfer::AsyncTransfer(Buffer &b, size_t o, size_t s):
79         buffer(&b),
80         offset(o),
81         size(s),
82         dest_addr(0)
83 {
84         allocate();
85 }
86
87 Buffer::AsyncTransfer::AsyncTransfer(AsyncTransfer &&other):
88         buffer(other.buffer),
89         offset(other.offset),
90         size(other.size),
91         dest_addr(other.dest_addr)
92 {
93         other.dest_addr = 0;
94 }
95
96 Buffer::AsyncTransfer &Buffer::AsyncTransfer::operator=(AsyncTransfer &&other)
97 {
98         if(dest_addr)
99                 finalize();
100
101         buffer = other.buffer;
102         offset = other.offset;
103         size = other.size;
104         dest_addr = other.dest_addr;
105
106         other.dest_addr = 0;
107
108         return *this;
109 }
110
111 Buffer::AsyncTransfer::~AsyncTransfer()
112 {
113         if(dest_addr)
114                 finalize();
115 }
116
117 } // namespace GL
118 } // namespace Msp