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