]> git.tdb.fi Git - libs/gl.git/blob - source/core/uniformblock.cpp
Change default binding ranges to match lowest OpenGL standard
[libs/gl.git] / source / core / uniformblock.cpp
1 #include <algorithm>
2 #include "deviceinfo.h"
3 #include "uniformblock.h"
4
5 using namespace std;
6
7 namespace Msp {
8 namespace GL {
9
10 UniformBlock::UniformBlock(const ReflectData::UniformBlockInfo &info):
11         UniformBlockBackend(info.bind_point>=0),
12         data(info.data_size)
13 { }
14
15 unsigned UniformBlock::get_alignment() const
16 {
17         return DeviceInfo::get_global().limits.uniform_buffer_alignment;
18 }
19
20 void UniformBlock::store(const ReflectData::UniformInfo &info, unsigned array_size, const void *value)
21 {
22         array_size = min(array_size, max(info.array_size, 1U));
23
24         char *store_ptr;
25         bool packed;
26         if(info.block->bind_point<0)
27         {
28                 if(info.location<0)
29                         return;
30
31                 store_ptr = data.data()+info.location*16;
32                 packed = true;
33         }
34         else
35         {
36                 store_ptr = data.data()+info.offset;
37                 if(array_size!=1 && info.array_stride!=get_type_size(info.type))
38                         packed = false;
39                 else if(is_matrix(info.type))
40                         packed = (info.matrix_stride==get_type_size(get_matrix_column_type(info.type)));
41                 else
42                         packed = true;
43         }
44
45         const char *value_ptr = static_cast<const char *>(value);
46         if(packed)
47         {
48                 const char *data_end = value_ptr+array_size*get_type_size(info.type);
49                 copy(value_ptr, data_end, store_ptr);
50         }
51         else if(is_matrix(info.type))
52         {
53                 unsigned col_size = get_type_size(get_matrix_column_type(info.type));
54                 unsigned cols = get_type_size(info.type)/col_size;
55                 for(unsigned i=0; i<array_size; ++i)
56                 {
57                         char *elem_ptr = store_ptr;
58                         for(unsigned j=0; j<cols; ++j)
59                         {
60                                 copy(value_ptr, value_ptr+col_size, elem_ptr);
61                                 value_ptr += col_size;
62                                 elem_ptr += info.matrix_stride;
63                         }
64                         store_ptr += info.array_stride;
65                 }
66         }
67         else
68         {
69                 unsigned elem_size = get_type_size(info.type);
70                 for(unsigned i=0; i<array_size; ++i)
71                 {
72                         copy(value_ptr, value_ptr+elem_size, store_ptr);
73                         value_ptr += elem_size;
74                         store_ptr += info.array_stride;
75                 }
76         }
77
78         dirty = true;
79 }
80
81 } // namespace GL
82 } // namespace Msp