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; 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 override void render() 88 { 89 if (group && outputBuffer) 90 { 91 prepareFramebuffer(); 92 93 glBindFramebuffer(GL_DRAW_FRAMEBUFFER, framebuffer); 94 95 // TODO: move depth blit to separate stage 96 glBindFramebuffer(GL_READ_FRAMEBUFFER, gbuffer.framebuffer); 97 glBlitFramebuffer(0, 0, gbuffer.width, gbuffer.height, 0, 0, gbuffer.width, gbuffer.height, GL_DEPTH_BUFFER_BIT, GL_NEAREST); 98 glBindFramebuffer(GL_READ_FRAMEBUFFER, 0); 99 100 glScissor(0, 0, outputBuffer.width, outputBuffer.height); 101 glViewport(0, 0, outputBuffer.width, outputBuffer.height); 102 103 glEnablei(GL_BLEND, 0); 104 glDisablei(GL_BLEND, 1); 105 glBlendFunci(0, GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); 106 107 foreach(entity; group) 108 { 109 if (entity.visible && entity.drawable) 110 { 111 if (!entityIsTerrain(entity) && !entityIsParticleSystem(entity)) 112 { 113 Shader shader = forwardShader; 114 115 if (entity.material) 116 { 117 if (entity.material.shader) 118 shader = entity.material.shader; 119 } 120 121 shader.bind(); 122 renderEntity(entity, shader); 123 shader.unbind(); 124 } 125 } 126 } 127 128 glDisablei(GL_BLEND, 0); 129 130 glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0); 131 } 132 } 133 }