1 /* 2 Copyright (c) 2018 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.graphics.gbuffer; 29 30 import std.stdio; 31 import std.math; 32 33 import dlib.core.memory; 34 import dlib.math.vector; 35 import dlib.image.color; 36 37 import dagon.core.libs; 38 import dagon.core.ownership; 39 import dagon.graphics.rc; 40 import dagon.graphics.shaders.geometrypass; 41 import dagon.resource.scene; 42 43 class GBuffer: Owner 44 { 45 uint width; 46 uint height; 47 48 GeometryPassShader geometryPassShader; 49 50 GLuint fbo; 51 GLuint depthTexture = 0; 52 GLuint colorTexture = 0; 53 GLuint rmsTexture = 0; 54 GLuint positionTexture = 0; 55 GLuint normalTexture = 0; 56 GLuint velocityTexture = 0; 57 GLuint emissionTexture = 0; 58 59 this(uint w, uint h, Owner o) 60 { 61 super(o); 62 63 width = w; 64 height = h; 65 66 geometryPassShader = New!GeometryPassShader(this); 67 68 glActiveTexture(GL_TEXTURE0); 69 70 glGenTextures(1, &depthTexture); 71 glBindTexture(GL_TEXTURE_2D, depthTexture); 72 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); 73 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); 74 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); 75 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); 76 glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH24_STENCIL8, width, height, 0, GL_DEPTH_STENCIL, GL_UNSIGNED_INT_24_8, null); 77 glBindTexture(GL_TEXTURE_2D, 0); 78 79 glGenTextures(1, &colorTexture); 80 glBindTexture(GL_TEXTURE_2D, colorTexture); 81 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); 82 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); 83 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); 84 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); 85 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, null); 86 glBindTexture(GL_TEXTURE_2D, 0); 87 88 glGenTextures(1, &rmsTexture); 89 glBindTexture(GL_TEXTURE_2D, rmsTexture); 90 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB8, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, null); 91 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); 92 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); 93 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); 94 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); 95 glBindTexture(GL_TEXTURE_2D, 0); 96 97 glGenTextures(1, &positionTexture); 98 glBindTexture(GL_TEXTURE_2D, positionTexture); 99 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA16F, width, height, 0, GL_RGBA, GL_FLOAT, null); 100 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); 101 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); 102 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); 103 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); 104 glBindTexture(GL_TEXTURE_2D, 0); 105 106 glGenTextures(1, &normalTexture); 107 glBindTexture(GL_TEXTURE_2D, normalTexture); 108 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB16F, width, height, 0, GL_RGB, GL_FLOAT, null); 109 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); 110 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); 111 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); 112 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); 113 glBindTexture(GL_TEXTURE_2D, 0); 114 115 glGenTextures(1, &velocityTexture); 116 glBindTexture(GL_TEXTURE_2D, velocityTexture); 117 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA16F, width, height, 0, GL_RGBA, GL_FLOAT, null); 118 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); 119 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); 120 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); 121 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); 122 glBindTexture(GL_TEXTURE_2D, 0); 123 124 glGenTextures(1, &emissionTexture); 125 glBindTexture(GL_TEXTURE_2D, emissionTexture); 126 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB16F, width, height, 0, GL_RGB, GL_FLOAT, null); 127 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); 128 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); 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 glBindTexture(GL_TEXTURE_2D, 0); 132 133 glGenFramebuffers(1, &fbo); 134 glBindFramebuffer(GL_FRAMEBUFFER, fbo); 135 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, colorTexture, 0); 136 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT1, GL_TEXTURE_2D, rmsTexture, 0); 137 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT2, GL_TEXTURE_2D, positionTexture, 0); 138 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT3, GL_TEXTURE_2D, normalTexture, 0); 139 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT4, GL_TEXTURE_2D, velocityTexture, 0); 140 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT5, GL_TEXTURE_2D, emissionTexture, 0); 141 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_TEXTURE_2D, depthTexture, 0); 142 143 GLenum[6] bufs = [GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1, GL_COLOR_ATTACHMENT2, GL_COLOR_ATTACHMENT3, GL_COLOR_ATTACHMENT4, GL_COLOR_ATTACHMENT5]; 144 glDrawBuffers(6, bufs.ptr); 145 146 GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER); 147 if (status != GL_FRAMEBUFFER_COMPLETE) 148 writeln(status); 149 150 glBindFramebuffer(GL_FRAMEBUFFER, 0); 151 } 152 153 ~this() 154 { 155 glBindFramebuffer(GL_FRAMEBUFFER, 0); 156 glDeleteFramebuffers(1, &fbo); 157 158 if (glIsTexture(depthTexture)) 159 glDeleteTextures(1, &depthTexture); 160 if (glIsTexture(colorTexture)) 161 glDeleteTextures(1, &colorTexture); 162 if (glIsTexture(velocityTexture)) 163 glDeleteTextures(1, &velocityTexture); 164 if (glIsTexture(rmsTexture)) 165 glDeleteTextures(1, &rmsTexture); 166 if (glIsTexture(positionTexture)) 167 glDeleteTextures(1, &positionTexture); 168 if (glIsTexture(normalTexture)) 169 glDeleteTextures(1, &normalTexture); 170 if (glIsTexture(emissionTexture)) 171 glDeleteTextures(1, &emissionTexture); 172 } 173 174 void bind() 175 { 176 glBindFramebuffer(GL_FRAMEBUFFER, fbo); 177 } 178 179 void unbind() 180 { 181 glBindFramebuffer(GL_FRAMEBUFFER, 0); 182 } 183 184 void render(Scene scene, RenderingContext* rc) 185 { 186 bind(); 187 188 glViewport(0, 0, width, height); 189 glScissor(0, 0, width, height); 190 clear(); 191 192 glEnable(GL_DEPTH_TEST); 193 194 auto rcLocal = *rc; 195 196 rcLocal.overrideShader = geometryPassShader; 197 198 scene.renderOpaqueEntities3D(&rcLocal); 199 200 unbind(); 201 } 202 203 void clear() 204 { 205 glClear(GL_DEPTH_BUFFER_BIT); 206 Color4f zero = Color4f(0, 0, 0, 0); 207 glClearBufferfv(GL_COLOR, 0, zero.arrayof.ptr); 208 glClearBufferfv(GL_COLOR, 1, zero.arrayof.ptr); 209 glClearBufferfv(GL_COLOR, 2, zero.arrayof.ptr); 210 glClearBufferfv(GL_COLOR, 3, zero.arrayof.ptr); 211 glClearBufferfv(GL_COLOR, 4, zero.arrayof.ptr); 212 glClearBufferfv(GL_COLOR, 5, zero.arrayof.ptr); 213 } 214 }