1 /*
2  * This file is part of the Colobot: Gold Edition source code
3  * Copyright (C) 2001-2020, Daniel Roux, EPSITEC SA & TerranovaTeam
4  * http://epsitec.ch; http://colobot.info; http://github.com/colobot
5  *
6  * This program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14  * See the GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program. If not, see http://gnu.org/licenses
18  */
19 
20 /**
21 * \file graphics/core/framebuffer.h
22 * \brief Abstract representation of framebuffer and offscreen buffers
23 */
24 
25 #pragma once
26 
27 namespace Gfx
28 {
29 
30 /**
31  * \struct FramebufferParams
32  * \brief Contains parameters for new framebuffer
33  */
34 struct FramebufferParams
35 {
36     //! Requested width of buffers
37     int width = 1024;
38     //! Requested height of buffers
39     int height = 1024;
40     //! Requested depth buffer
41     int depth = 16;
42     //! Requested number of samples for multisampling
43     int samples = 1;
44 
45     enum class AttachmentType
46     {
47         Texture,
48         Renderbuffer,
49         None,
50     };
51     AttachmentType colorAttachment = AttachmentType::Renderbuffer;
52     AttachmentType depthAttachment = AttachmentType::Renderbuffer;
53 
54     //! Loads default values
LoadDefaultFramebufferParams55     void LoadDefault()
56     {
57         *this = FramebufferParams();
58     }
59 };
60 
61 /**
62  * \class CFramebuffer
63  * \brief Abstract interface of default framebuffer and offscreen framebuffers
64  *
65  * This code encapsulates basics of default framebuffer and offscreen buffers
66  * and allows offscreen rendering in generic way. CDevice may or may not implement
67  * offscreen buffers depending on available hardware but is required to provide
68  * default framebuffer implementation. Because of some hardware restrictions
69  * and in order to simplify interface, you can't bind/unbind textures from
70  * offscreen buffers and you can't change it's parameters.
71  */
72 class CFramebuffer
73 {
74 public:
~CFramebuffer()75     virtual ~CFramebuffer() {}
76 
77     //! Creates this framebuffer
78     virtual bool Create() = 0;
79 
80     //! Destroys this framebuffer
81     virtual void Destroy() = 0;
82 
83     //! Returns true if this is default framebuffer
84     virtual bool IsDefault() = 0;
85 
86     //! Returns width of buffers in this framebuffer
87     virtual int GetWidth() = 0;
88 
89     //! Returns height of buffers in this framebuffer
90     virtual int GetHeight() = 0;
91 
92     //! Returns depth size in bits
93     virtual int GetDepth() = 0;
94 
95     //! Returns number of samples or 1 if multisampling is not supported
96     virtual int GetSamples() = 0;
97 
98     //! Returns texture that contains color buffer or 0 if not available
99     virtual int GetColorTexture() = 0;
100 
101     //! Returns texture that contains depth buffer or 0 if not available
102     virtual int GetDepthTexture() = 0;
103 
104     //! Binds this framebuffer to context
105     virtual void Bind() = 0;
106 
107     //! Unbinds this framebuffer from context
108     virtual void Unbind() = 0;
109 
110     //! Copies content of color buffer to screen
111     virtual void CopyToScreen(int fromX, int fromY, int fromWidth, int fromHeight, int toX, int toY, int toWidth, int toHeight) = 0;
112 };
113 
114 
115 /**
116 * \class CDefaultFramebuffer
117 * \brief Concrete implementation of default framebuffer.
118 *
119 * This class represents default framebuffer implementation.
120 */
121 class CDefaultFramebuffer : public CFramebuffer
122 {
123 private:
124     int m_width, m_height, m_depth;
125 
126 public:
127     explicit CDefaultFramebuffer(const FramebufferParams &params);
128 
129     //! Creates default framebuffer
130     bool Create() override;
131 
132     //! Destroys default framebuffer
133     void Destroy() override;
134 
135     //! Returns true
136     bool IsDefault() override;
137 
138     //! Returns width of buffers in this framebuffer
139     int GetWidth() override;
140 
141     //! Returns height of buffers in this framebuffer
142     int GetHeight() override;
143 
144     //! Returns depth size in bits
145     int GetDepth() override;
146 
147     //! Returns number of samples or 1 if multisampling is not supported
148     int GetSamples() override;
149 
150     //! Returns texture that contains color buffer or 0 if not available
151     int GetColorTexture() override;
152 
153     //! Returns texture that contains depth buffer or 0 if not available
154     int GetDepthTexture() override;
155 
156     //! Binds this framebuffer to context
157     void Bind() override;
158 
159     //! Unbinds this framebuffer from context
160     void Unbind() override;
161 
162     //! Copies content of color buffer to screen
163     void CopyToScreen(int fromX, int fromY, int fromWidth, int fromHeight, int toX, int toY, int toWidth, int toHeight) override;
164 };
165 
166 } // end of Gfx
167