]> git.tdb.fi Git - libs/gl.git/blob - source/core/batch.h
Adapt to changes in mspmath
[libs/gl.git] / source / core / batch.h
1 #ifndef MSP_GL_BATCH_H_
2 #define MSP_GL_BATCH_H_
3
4 #include <vector>
5 #include <msp/datafile/objectloader.h>
6 #include "batch_backend.h"
7 #include "bufferable.h"
8 #include "datatype.h"
9 #include "primitivetype.h"
10
11 namespace Msp {
12 namespace GL {
13
14 class Buffer;
15
16 /**
17 Stores primitive type and element indices for a single draw call.
18
19 Data type for indices is automatically chosen to accommodate the largest
20 index, but can also be manually overridden.
21
22 Batches are normally contained in a Mesh.
23 */
24 class Batch: public BatchBackend, public Bufferable
25 {
26 public:
27         class Loader: public DataFile::ObjectLoader<Batch>
28         {
29         public:
30                 Loader(Batch &);
31         private:
32                 void indices(const std::vector<unsigned> &);
33         };
34
35 private:
36         PrimitiveType prim_type;
37         DataType index_type;
38         std::vector<std::uint8_t> data;
39         unsigned max_index;
40
41 public:
42         Batch(PrimitiveType);
43
44         PrimitiveType get_type() const { return prim_type; }
45
46         /** Sets the data type for indices.  Allowed types are UNSIGNED_SHORT and
47         UNSIGNED_INT.  It's an error to specify a type which can't hold the current
48         range of index values. */
49         void set_index_type(DataType);
50
51         DataType get_index_type() const { return index_type; }
52
53         /** Appends a single index.  The data type is automatically adjusted if the
54         index is too large for the current data type. */
55         Batch &append(unsigned);
56
57         Batch &append(const std::vector<unsigned> &);
58
59         /** Checks if it's possible to append another Batch with a given primitive
60         type. */
61         bool can_append(PrimitiveType);
62
63         /** Appends another batch.  Additional indices may be inserted in order to
64         join the primitives. */
65         Batch &append(const Batch &);
66
67 private:
68         void append_index(unsigned);
69         virtual std::size_t get_data_size() const { return data.size(); }
70         virtual const void *get_data_pointer() const { return &data[0]; }
71         virtual std::size_t get_alignment() const { return get_index_size(); }
72 public:
73         std::size_t get_index_size() const { return get_type_size(index_type); }
74         std::size_t size() const { return data.size()/get_index_size(); }
75
76         unsigned get_index(std::size_t) const;
77 };
78
79 } // namespace GL
80 } // namespace Msp
81
82 #endif