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