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