1 /*
2 Copyright (c) 2019-2022 Timur Gafarov
3 
4 Boost Software License - Version 1.0 - August 17th, 2003
5 Permission is hereby granted, free of charge, to any person or organization
6 obtaining a copy of the software and accompanying documentation covered by
7 this license (the "Software") to use, reproduce, display, distribute,
8 execute, and transmit the Software, and to prepare derivative works of the
9 Software, and to permit third-parties to whom the Software is furnished to
10 do so, all subject to the following:
11 
12 The copyright notices in the Software and this entire statement, including
13 the above license grant, this restriction and the following disclaimer,
14 must be included in all copies of the Software, in whole or in part, and
15 all derivative works of the Software, unless such copies or derivative
16 works are solely in the form of machine-executable object code generated by
17 a source language processor.
18 
19 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
22 SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
23 FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
24 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
25 DEALINGS IN THE SOFTWARE.
26 */
27 
28 module dagon.render.framebuffer;
29 
30 import std.stdio;
31 import dlib.core.memory;
32 import dlib.core.ownership;
33 import dlib.image.color;
34 
35 import dagon.core.bindings;
36 
37 enum FrameBufferFormat
38 {
39     R8,
40     R16F,
41     R32F,
42     
43     RG8,
44     RG16F,
45     RG32F,
46     
47     RGB8,
48     RGB16F,
49     RGB32F,
50     
51     RGBA8,
52     RGBA16F,
53     RGBA32F
54 }
55 
56 class Framebuffer: Owner
57 {
58     uint width;
59     uint height;
60     FrameBufferFormat colorFormat;
61     bool hasDepthBuffer;
62     GLuint _colorTexture = 0;
63     GLuint _depthTexture = 0;
64     GLuint framebuffer;
65 
66     this(Owner owner)
67     {
68         super(owner);
69     }
70 
71     this(uint w, uint h, FrameBufferFormat format, bool depth, Owner owner)
72     {
73         super(owner);
74         init(w, h, format, depth);
75     }
76 
77     void init(uint w, uint h, FrameBufferFormat format, bool depth)
78     {
79         width = w;
80         height = h;
81         colorFormat = format;
82         hasDepthBuffer = depth;
83         createFramebuffer();
84     }
85 
86     ~this()
87     {
88         releaseFramebuffer();
89     }
90 
91     protected void createFramebuffer()
92     {
93         releaseFramebuffer();
94 
95         GLint intFormat;
96         GLenum textureFormat;
97         GLenum pixelType;
98 
99         switch (colorFormat)
100         {
101             // R
102             case FrameBufferFormat.R8:      intFormat = GL_R8;      textureFormat = GL_RED;  pixelType = GL_UNSIGNED_BYTE; break;
103             case FrameBufferFormat.R16F:    intFormat = GL_R16F;    textureFormat = GL_RED;  pixelType = GL_FLOAT; break;
104             case FrameBufferFormat.R32F:    intFormat = GL_R32F;    textureFormat = GL_RED;  pixelType = GL_FLOAT; break;
105             
106             // RG
107             case FrameBufferFormat.RG8:     intFormat = GL_RG8;     textureFormat = GL_RG;   pixelType = GL_UNSIGNED_BYTE; break;
108             case FrameBufferFormat.RG16F:   intFormat = GL_RG16F;   textureFormat = GL_RG;   pixelType = GL_FLOAT; break;
109             case FrameBufferFormat.RG32F:   intFormat = GL_RG32F;   textureFormat = GL_RG;   pixelType = GL_FLOAT; break;
110             
111             // RGB
112             case FrameBufferFormat.RGB8:    intFormat = GL_RGB8;    textureFormat = GL_RGB;  pixelType = GL_UNSIGNED_BYTE; break;
113             case FrameBufferFormat.RGB16F:  intFormat = GL_RGB16F;  textureFormat = GL_RGB;  pixelType = GL_FLOAT; break;
114             case FrameBufferFormat.RGB32F:  intFormat = GL_RGB32F;  textureFormat = GL_RGB;  pixelType = GL_FLOAT; break;
115             
116             // RGBA
117             case FrameBufferFormat.RGBA8:   intFormat = GL_RGBA8;   textureFormat = GL_RGBA; pixelType = GL_UNSIGNED_BYTE; break;
118             case FrameBufferFormat.RGBA16F: intFormat = GL_RGBA16F; textureFormat = GL_RGBA; pixelType = GL_FLOAT; break;
119             case FrameBufferFormat.RGBA32F: intFormat = GL_RGBA32F; textureFormat = GL_RGBA; pixelType = GL_FLOAT; break;
120             default: break;
121         }
122 
123         glActiveTexture(GL_TEXTURE0);
124 
125         glGenTextures(1, &_colorTexture);
126         glBindTexture(GL_TEXTURE_2D, _colorTexture);
127         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
128         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
129         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
130         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
131         glTexImage2D(GL_TEXTURE_2D, 0, intFormat, width, height, 0, textureFormat, pixelType, null);
132         glBindTexture(GL_TEXTURE_2D, 0);
133 
134         if (hasDepthBuffer)
135         {
136             glGenTextures(1, &_depthTexture);
137             glBindTexture(GL_TEXTURE_2D, _depthTexture);
138             glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
139             glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
140             glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
141             glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
142             glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH24_STENCIL8, width, height, 0, GL_DEPTH_STENCIL, GL_UNSIGNED_INT_24_8, null);
143             glBindTexture(GL_TEXTURE_2D, 0);
144         }
145 
146         glGenFramebuffers(1, &framebuffer);
147         glBindFramebuffer(GL_FRAMEBUFFER, framebuffer);
148         glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, _colorTexture, 0);
149         if (hasDepthBuffer)
150             glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_TEXTURE_2D, _depthTexture, 0);
151 
152         GLenum[1] drawBuffers = [GL_COLOR_ATTACHMENT0];
153         glDrawBuffers(drawBuffers.length, drawBuffers.ptr);
154 
155         GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
156         if (status != GL_FRAMEBUFFER_COMPLETE)
157             writeln(status);
158 
159         glBindFramebuffer(GL_FRAMEBUFFER, 0);
160     }
161 
162     void releaseFramebuffer()
163     {
164         if (glIsFramebuffer(framebuffer))
165             glDeleteFramebuffers(1, &framebuffer);
166 
167         if (glIsTexture(_colorTexture))
168             glDeleteTextures(1, &_colorTexture);
169 
170         if (glIsTexture(_depthTexture))
171             glDeleteTextures(1, &_depthTexture);
172     }
173 
174     GLuint colorTexture()
175     {
176         return _colorTexture;
177     }
178 
179     GLuint depthTexture()
180     {
181         return _depthTexture;
182     }
183 
184     void bind()
185     {
186         glBindFramebuffer(GL_DRAW_FRAMEBUFFER, framebuffer);
187     }
188 
189     void unbind()
190     {
191         glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);
192     }
193 
194     void resize(uint w, uint h)
195     {
196         width = w;
197         height = h;
198         createFramebuffer();
199     }
200 
201     void blitColorBuffer()
202     {
203         glBindFramebuffer(GL_READ_FRAMEBUFFER, framebuffer);
204         glBlitFramebuffer(0, 0, width, height, 0, 0, width, height, GL_COLOR_BUFFER_BIT, GL_NEAREST);
205         glBindFramebuffer(GL_READ_FRAMEBUFFER, 0);
206     }
207 
208     void blitDepthBuffer()
209     {
210         if (hasDepthBuffer)
211         {
212             glBindFramebuffer(GL_READ_FRAMEBUFFER, framebuffer);
213             glBlitFramebuffer(0, 0, width, height, 0, 0, width, height, GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT, GL_NEAREST);
214             glBindFramebuffer(GL_READ_FRAMEBUFFER, 0);
215         }
216     }
217 }