]> git.tdb.fi Git - libs/gl.git/blob - source/core/vertexsetup.cpp
4fd589a9698bfe8a1616a4d097902a984e4f9c49
[libs/gl.git] / source / core / vertexsetup.cpp
1 #include "deviceinfo.h"
2 #include "error.h"
3 #include "vertexarray.h"
4 #include "vertexsetup.h"
5
6 using namespace std;
7
8 namespace Msp {
9 namespace GL {
10
11 VertexSetup::VertexSetup():
12         dirty(0),
13         vertex_array(0),
14         inst_array(0),
15         index_buffer(0),
16         index_type(UNSIGNED_SHORT)
17 { }
18
19 void VertexSetup::set_format(const VertexFormat &vfmt)
20 {
21         if(!verify_format(vfmt))
22                 throw invalid_argument("VertexSetup::set_format");
23         if(!vertex_format.empty())
24                 throw invalid_operation("VertexSetup::set_format");
25
26         require_format(vfmt, false);
27
28         vertex_format = vfmt;
29 }
30
31 void VertexSetup::set_format_instanced(const VertexFormat &vfmt, const VertexFormat &ifmt)
32 {
33         if(!verify_format(vfmt) || !verify_format(ifmt))
34                 throw invalid_argument("VertexSetup::set_format");
35         if(!vertex_format.empty())
36                 throw invalid_operation("VertexSetup::set_format");
37
38         require_format(vfmt, false);
39         require_format(ifmt, true);
40
41         vertex_format = vfmt;
42         inst_format = ifmt;
43 }
44
45 void VertexSetup::set_vertex_array(const VertexArray &a)
46 {
47         if(vertex_format.empty())
48                 throw invalid_operation("VertexSetup::set_vertex_array");
49         if(a.get_format()!=vertex_format)
50                 throw incompatible_data("VertexSetup::set_vertex_array");
51         if(!a.get_buffer())
52                 throw invalid_argument("VertexSetup::set_vertex_array");
53
54         vertex_array = &a;
55         dirty |= VERTEX_ARRAY;
56 }
57
58 void VertexSetup::set_instance_array(const VertexArray &a)
59 {
60         if(inst_format.empty())
61                 throw invalid_operation("VertexSetup::set_instance_array");
62         if(a.get_format()!=inst_format)
63                 throw incompatible_data("VertexSetup::set_instance_array");
64         if(!a.get_buffer())
65                 throw invalid_argument("VertexSetup::set_instance_array");
66
67         inst_array = &a;
68         dirty |= INSTANCE_ARRAY;
69 }
70
71 void VertexSetup::set_index_buffer(const Buffer &ibuf, DataType itype)
72 {
73         index_buffer = &ibuf;
74         index_type = itype;
75         dirty |= INDEX_BUFFER;
76 }
77
78 bool VertexSetup::verify_format(const VertexFormat &fmt)
79 {
80         if(fmt.empty())
81                 return false;
82
83         static unsigned max_attribs = DeviceInfo::get_global().limits.max_vertex_attributes;
84         return all_of(fmt.begin(), fmt.end(), [](VertexAttribute a){ return get_attribute_semantic(a)<max_attribs; });
85 }
86
87 void VertexSetup::update() const
88 {
89         VertexSetupBackend::update(dirty);
90         dirty = 0;
91 }
92
93 void VertexSetup::unload()
94 {
95         VertexSetupBackend::unload();
96
97         vertex_array = 0;
98         vertex_format = VertexFormat();
99         inst_array = 0;
100         inst_format = VertexFormat();
101         index_buffer = 0;
102 }
103
104 } // namespace GL
105 } // namespace Msp