]> git.tdb.fi Git - libs/gl.git/blob - source/core/frameformat.cpp
Remove obsolete includes
[libs/gl.git] / source / core / frameformat.cpp
1 #include "error.h"
2 #include "frameformat.h"
3 #include "gl.h"
4
5 using namespace std;
6
7 namespace Msp {
8 namespace GL {
9
10 FrameFormat::FrameFormat(FrameAttachment fa):
11         count(1),
12         samples(1)
13 {
14         attachments[0] = fa;
15 }
16
17 FrameFormat FrameFormat::operator,(FrameAttachment fa) const
18 {
19         if(count>=MAX_ATTACHMENTS)
20                 throw invalid_operation("FrameFormat::operator,");
21
22         FrameFormat result = *this;
23         result.attachments[result.count++] = fa;
24
25         return result;
26 }
27
28 FrameFormat FrameFormat::operator,(PixelFormat pf) const
29 {
30         if(!count)
31                 throw invalid_operation("FrameFormat::operator,");
32
33         FrameFormat r = *this;
34         FrameAttachment &fa = r.attachments[r.count-1];
35         fa = make_typed_attachment(fa, pf);
36
37         return r;
38 }
39
40 FrameFormat FrameFormat::operator,(unsigned index) const
41 {
42         if(!count)
43                 throw invalid_operation("FrameFormat::operator,");
44
45         FrameFormat r = *this;
46         FrameAttachment &fa = r.attachments[r.count-1];
47         fa = make_indexed_attachment(fa, index);
48
49         return r;
50 }
51
52 FrameFormat &FrameFormat::set_samples(unsigned s)
53 {
54         samples = s;
55         return *this;
56 }
57
58 int FrameFormat::index(FrameAttachment fa) const
59 {
60         for(unsigned i=0; i<count; ++i)
61                 if(get_attach_point(attachments[i])==get_attach_point(fa))
62                         return i;
63         return -1;
64 }
65
66
67 FrameAttachment make_typed_attachment(FrameAttachment fa, PixelFormat pf)
68 {
69         PixelComponents comp = get_components(pf);
70         if(get_attach_point(fa)==get_attach_point(DEPTH_ATTACHMENT))
71         {
72                 if(comp!=DEPTH_COMPONENT)
73                         throw invalid_argument("make_typed_attachment");
74         }
75         else if(get_attach_point(fa)==get_attach_point(STENCIL_ATTACHMENT))
76         {
77                 if(comp!=STENCIL_INDEX)
78                         throw invalid_argument("make_typed_attachment");
79         }
80         else
81         {
82                 if(comp!=RED && comp!=RG && comp!=RGB && comp!=RGBA)
83                         throw invalid_argument("make_typed_attachment");
84         }
85
86         DataType type = get_component_type(pf);
87         return static_cast<FrameAttachment>((fa&0xFC00) | (is_float(type)*0x100) | get_type_size(type)<<4 | get_component_count(comp));
88 }
89
90 FrameAttachment make_indexed_attachment(FrameAttachment fa, unsigned i)
91 {
92         if(get_attach_point(fa)==get_attach_point(COLOR_ATTACHMENT))
93         {
94                 if(i>61)
95                         throw out_of_range("make_indexed_attachment");
96                 return static_cast<FrameAttachment>(fa+(i<<10));
97         }
98         else
99                 throw invalid_argument("make_indexed_attachment");
100 }
101
102 PixelFormat get_attachment_pixelformat(FrameAttachment fa)
103 {
104         PixelComponents comp;
105         if(get_attach_point(fa)==get_attach_point(DEPTH_ATTACHMENT))
106                 comp = DEPTH_COMPONENT;
107         else if(get_attach_point(fa)==get_attach_point(STENCIL_ATTACHMENT))
108                 comp = STENCIL_INDEX;
109         else
110                 comp = static_cast<PixelComponents>(fa&7);
111
112         DataType type;
113         if(fa&0x100)
114                 type = static_cast<DataType>((fa&0x70)>>4 | 0x300);
115         else
116                 type = static_cast<DataType>((fa&0x70)>>4);
117
118         return make_pixelformat(comp, type);
119 }
120
121 } // namespace GL
122 } // namespace Msp