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