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