]> git.tdb.fi Git - libs/gl.git/blob - source/core/framebuffer.h
Fix reflection of image types from Spir-V modules
[libs/gl.git] / source / core / framebuffer.h
1 #ifndef MSP_GL_FRAMEBUFFER_H_
2 #define MSP_GL_FRAMEBUFFER_H_
3
4 #include <vector>
5 #include "color.h"
6 #include "framebuffer_backend.h"
7 #include "frameformat.h"
8 #include "texturecube.h"
9
10 namespace Msp {
11 namespace GL {
12
13 class Texture2D;
14 class Texture2DMultisample;
15 class Texture3D;
16 class WindowView;
17
18 class framebuffer_incomplete: public std::runtime_error
19 {
20 public:
21         framebuffer_incomplete(const std::string &);
22         virtual ~framebuffer_incomplete() throw() { }
23 };
24
25 /**
26 Uses one or more textures as buffers to draw into.  Framebuffers can contain
27 multiple color buffers to match multiple outputs from a fragment shader, but
28 only one depth and stencil buffer.
29
30 RenderTarget provides a higher-level interface which manages the textures as
31 well as the framebuffer itself.
32 */
33 class Framebuffer: public FramebufferBackend
34 {
35         friend FramebufferBackend;
36
37 protected:
38         struct Attachment
39         {
40                 Texture *tex = 0;
41                 unsigned level = 0;
42                 int layer = 0;
43
44                 void set(Texture &, unsigned, int);
45                 void clear();
46         };
47
48         FrameFormat format;
49         std::vector<Attachment> attachments;
50         unsigned width = 0;
51         unsigned height = 0;
52         mutable unsigned dirty = 0;
53
54         Framebuffer(bool);
55 public:
56         /** Creates an empty framebuffer.  Format must be set before textures can
57         be attached. */
58         Framebuffer();
59
60         /** Creates a framebuffer and sets its format to a single attachment. */
61         Framebuffer(FrameAttachment);
62
63         /** Creates a framebuffer and sets its format. */
64         Framebuffer(const FrameFormat &);
65
66         /** Sets the format of the framebuffer.  Once the format is set, it can't
67         be changed. */
68         void set_format(const FrameFormat &);
69
70         const FrameFormat &get_format() const { return format; }
71
72         unsigned get_width() const { return width; }
73         unsigned get_height() const { return height; }
74
75 protected:
76         void update() const;
77         void check_size();
78         void set_attachment(FrameAttachment, Texture &, unsigned, int, unsigned);
79
80 public:
81         /** Attaches a texture to the framebuffer.  Only the attachment point
82         portion of attch is considered; pixel format is ignored.  The framebuffer
83         must have a format and the format of the texture must match that defined
84         in the framebuffer for this attachment point. */
85         void attach(FrameAttachment attch, Texture2D &, unsigned level = 0);
86
87         void attach(FrameAttachment attch, Texture2DMultisample &);
88
89         /** Attaches a single layer from a 3-dimensional texture to the
90         framebuffer. */
91         void attach(FrameAttachment attch, Texture3D &, unsigned layer, unsigned level = 0);
92
93         void attach(FrameAttachment attch, TextureCube &, TextureCubeFace face, unsigned level = 0);
94
95         /** Attaches a layered texture to the framebuffer.  Shaders can direct
96         output to a particular layer. */
97         void attach_layered(FrameAttachment attch, Texture3D &, unsigned level = 0);
98
99         void attach_layered(FrameAttachment attch, TextureCube &, unsigned level = 0);
100         void detach(FrameAttachment attch);
101
102         /** Ensures that the framebuffer is complete, throwing an exception if it
103         isn't. */
104         void require_complete() const;
105
106         void refresh() const { if(dirty) update(); }
107
108         using FramebufferBackend::set_debug_name;
109 };
110
111
112 union ClearValue
113 {
114         Color color;
115         struct
116         {
117                 float depth;
118                 int stencil;
119         } depth_stencil;
120
121         ClearValue(): color(0.0f, 0.0f, 0.0f, 0.0f) { }
122 };
123
124 } // namespace GL
125 } // namespace Msp
126
127 #endif