1 /*
2 Copyright (c) 2020 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.deferred.forwardpass;
29 
30 import std.stdio;
31 
32 import dlib.core.memory;
33 import dlib.core.ownership;
34 
35 import dagon.core.bindings;
36 import dagon.graphics.entity;
37 import dagon.graphics.shader;
38 import dagon.graphics.terrain;
39 import dagon.graphics.particles;
40 import dagon.render.pipeline;
41 import dagon.render.pass;
42 import dagon.render.framebuffer;
43 import dagon.render.gbuffer;
44 import dagon.render.shaders.forward;
45 
46 class DeferredForwardPass: RenderPass
47 {
48     ForwardShader forwardShader;
49     Framebuffer outputBuffer;
50     GBuffer gbuffer;
51     GLuint framebuffer = 0;
52 
53     this(RenderPipeline pipeline, GBuffer gbuffer, EntityGroup group = null)
54     {
55         super(pipeline, group);
56         forwardShader = New!ForwardShader(this);
57         
58         this.gbuffer = gbuffer;
59     }
60     
61     void prepareFramebuffer()
62     {
63         if (framebuffer)
64             return;
65     
66         glGenFramebuffers(1, &framebuffer);
67         glBindFramebuffer(GL_FRAMEBUFFER, framebuffer);
68         glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, outputBuffer.colorTexture, 0);
69         glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT1, GL_TEXTURE_2D, gbuffer.velocityTexture, 0);
70         glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_TEXTURE_2D, outputBuffer.depthTexture, 0);
71         
72         GLenum[2] drawBuffers = 
73         [
74             GL_COLOR_ATTACHMENT0, 
75             GL_COLOR_ATTACHMENT1
76         ];
77         
78         glDrawBuffers(drawBuffers.length, drawBuffers.ptr);
79 
80         GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
81         if (status != GL_FRAMEBUFFER_COMPLETE)
82             writeln(status);
83 
84         glBindFramebuffer(GL_FRAMEBUFFER, 0);
85     }
86     
87     void resize(uint w, uint h)
88     {
89         if (glIsFramebuffer(framebuffer))
90         {
91             glDeleteFramebuffers(1, &framebuffer);
92             framebuffer = 0;
93         }
94     }
95 
96     override void render()
97     {
98         if (group && outputBuffer)
99         {
100             prepareFramebuffer();
101             
102             glBindFramebuffer(GL_DRAW_FRAMEBUFFER, framebuffer);
103 
104             // TODO: move depth blit to separate stage
105             glBindFramebuffer(GL_READ_FRAMEBUFFER, gbuffer.framebuffer);
106             glBlitFramebuffer(0, 0, gbuffer.width, gbuffer.height, 0, 0, gbuffer.width, gbuffer.height, GL_DEPTH_BUFFER_BIT, GL_NEAREST);
107             glBindFramebuffer(GL_READ_FRAMEBUFFER, 0);
108 
109             glScissor(0, 0, outputBuffer.width, outputBuffer.height);
110             glViewport(0, 0, outputBuffer.width, outputBuffer.height);
111 
112             glEnablei(GL_BLEND, 0);
113             glDisablei(GL_BLEND, 1);
114             glBlendFunci(0, GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
115 
116             foreach(entity; group)
117             {
118                 if (entity.visible && entity.drawable)
119                 {
120                     if (!entityIsTerrain(entity) && !entityIsParticleSystem(entity))
121                     {
122                         Shader shader = forwardShader;
123 
124                         if (entity.material)
125                         {
126                             if (entity.material.shader)
127                                 shader = entity.material.shader;
128                         }
129 
130                         shader.bind();
131                         renderEntity(entity, shader);
132                         shader.unbind();
133                     }
134                 }
135             }
136 
137             glDisablei(GL_BLEND, 0);
138 
139             glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);
140         }
141     }
142 }