]> git.tdb.fi Git - libs/gl.git/blob - source/core/batch.h
Move swizzling modes to pixelformat.h
[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         ~Batch();
44
45         PrimitiveType get_type() const { return prim_type; }
46
47         /** Sets the data type for indices.  Allowed types are UNSIGNED_SHORT and
48         UNSIGNED_INT.  It's an error to specify a type which can't hold the current
49         range of index values. */
50         void set_index_type(DataType);
51
52         DataType get_index_type() const { return index_type; }
53
54         /** Appends a single index.  The data type is automatically adjusted if the
55         index is too large for the current data type. */
56         Batch &append(unsigned);
57
58         Batch &append(const std::vector<unsigned> &);
59
60         /** Checks if it's possible to append another Batch with a given primitive
61         type. */
62         bool can_append(PrimitiveType);
63
64         /** Appends another batch.  Additional indices may be inserted in order to
65         join the primitives. */
66         Batch &append(const Batch &);
67
68 private:
69         void append_index(unsigned);
70         virtual std::size_t get_data_size() const { return data.size(); }
71         virtual const void *get_data_pointer() const { return &data[0]; }
72         virtual std::size_t get_alignment() const { return get_index_size(); }
73         std::size_t get_index_size() const;
74 public:
75         std::size_t size() const { return data.size()/get_index_size(); }
76
77         unsigned get_index(std::size_t) const;
78 };
79
80 } // namespace GL
81 } // namespace Msp
82
83 #endif