@@ -25,6 +25,19 @@ ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
2525DEALINGS IN THE SOFTWARE.
2626*/
2727
28+ /**
29+ * G-buffer abstraction for deferred rendering.
30+ *
31+ * Description:
32+ * This module provides a lightweight wrapper around OpenGL framebuffer and
33+ * texture attachments used for deferred shading. The GBuffer class owns
34+ * render targets for color, depth, normal, material (PBR), emission, velocity,
35+ * and radiance output.
36+ *
37+ * Copyright: Timur Gafarov 2019-2026
38+ * License: $(LINK2 https://boost.org/LICENSE_1_0.txt, Boost License 1.0).
39+ * Authors: Timur Gafarov
40+ */
2841module dagon.render.deferred.gbuffer ;
2942
3043import std.stdio ;
@@ -37,20 +50,53 @@ import dagon.core.bindings;
3750import dagon.core.logger ;
3851import dagon.render.framebuffer;
3952
53+ /**
54+ * Manages the G-buffer attachments.
55+ *
56+ * The G-Buffer (Geometry buffer) is a set of fragment attribute buffers
57+ * used to store screen-space geometry data in a deferred renderer.
58+ */
4059class GBuffer : Owner
4160{
61+ // / Current framebuffer width.
4262 uint width;
63+
64+ // / Current framebuffer height.
4365 uint height;
4466
67+ // / OpenGL framebuffer object handle.
4568 GLuint framebuffer;
69+
70+ // / Color render target.
4671 GLuint colorTexture = 0 ;
72+
73+ // / Depth-stencil render target.
4774 GLuint depthTexture = 0 ;
75+
76+ // / Eye-space normal render target.
4877 GLuint normalTexture = 0 ;
78+
79+ // / Roughness-Metallic render target.
4980 GLuint pbrTexture = 0 ;
81+
82+ // / Emission render target.
5083 GLuint emissionTexture = 0 ;
84+
85+ // / Screen-space velocity render target.
5186 GLuint velocityTexture = 0 ;
87+
88+ // / Radiance framebuffer that is also attached to this G-buffer.
5289 Framebuffer radiance;
5390
91+ /**
92+ * Creates a new GBuffer for the given size and radiance target.
93+ *
94+ * Params:
95+ * w = Width of the G-buffer.
96+ * h = Height of the G-buffer.
97+ * radiance = Framebuffer used for radiance output.
98+ * owner = Owner object.
99+ */
54100 this (uint w, uint h, Framebuffer radiance, Owner owner)
55101 {
56102 super (owner);
@@ -61,6 +107,12 @@ class GBuffer: Owner
61107 createFramebuffer();
62108 }
63109
110+ /**
111+ * Initializes and configures the framebuffer and its texture attachments.
112+ *
113+ * Recreates the G-buffer textures and rebinds them to a new
114+ * framebuffer object. It also validates framebuffer completeness.
115+ */
64116 void createFramebuffer ()
65117 {
66118 releaseFramebuffer();
@@ -155,6 +207,7 @@ class GBuffer: Owner
155207 glBindFramebuffer(GL_FRAMEBUFFER , 0 );
156208 }
157209
210+ // / Releases the framebuffer and texture resources owned by this GBuffer.
158211 void releaseFramebuffer ()
159212 {
160213 if (glIsFramebuffer(framebuffer))
@@ -179,21 +232,25 @@ class GBuffer: Owner
179232 glDeleteTextures(1 , &velocityTexture);
180233 }
181234
235+ // / Destructor that ensures OpenGL resources are cleaned up.
182236 ~this ()
183237 {
184238 releaseFramebuffer();
185239 }
186240
241+ // / Binds the G-buffer for drawing.
187242 void bind ()
188243 {
189244 glBindFramebuffer(GL_DRAW_FRAMEBUFFER , framebuffer);
190245 }
191246
247+ // / Unbinds any draw framebuffer and returns to the default framebuffer.
192248 void unbind ()
193249 {
194250 glBindFramebuffer(GL_DRAW_FRAMEBUFFER , 0 );
195251 }
196252
253+ // / Clears all G-buffer attachments.
197254 void clear ()
198255 {
199256 glBindFramebuffer(GL_DRAW_FRAMEBUFFER , framebuffer);
@@ -214,6 +271,7 @@ class GBuffer: Owner
214271 glBindFramebuffer(GL_DRAW_FRAMEBUFFER , 0 );
215272 }
216273
274+ // / Resizes the G-buffer and recreates the framebuffer attachments.
217275 void resize (uint w, uint h)
218276 {
219277 width = w;
0 commit comments