]> git.tdb.fi Git - libs/gl.git/blob - source/core/buffer.cpp
Fix reflection of image types from Spir-V modules
[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         if(size==0)
36                 throw invalid_operation("Buffer::sub_data");
37         if(off>size || off+sz>size)
38                 throw out_of_range("Buffer::sub_data");
39
40         BufferBackend::sub_data(off, sz, d);
41 }
42
43 void Buffer::require_size(size_t req_sz) const
44 {
45         if(size<req_sz)
46                 throw buffer_too_small(format("buffer has %d bytes; %d required", size, req_sz));
47 }
48
49 void *Buffer::map()
50 {
51         if(!can_map() || mapped)
52                 throw invalid_operation("Buffer::map");
53         void *result = BufferBackend::map();
54         mapped = true;
55         return result;
56 }
57
58 bool Buffer::unmap()
59 {
60         if(!can_map() || !mapped)
61                 throw invalid_operation("Buffer::map");
62         bool result = BufferBackend::unmap();
63         mapped = false;
64         return result;
65 }
66
67 } // namespace GL
68 } // namespace Msp