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.shaders.environmentpass; 29 30 import std.stdio; 31 import std.math; 32 33 import dlib.core.memory; 34 import dlib.math.vector; 35 import dlib.math.matrix; 36 import dlib.image.color; 37 38 import dagon.core.libs; 39 import dagon.core.ownership; 40 import dagon.graphics.rc; 41 import dagon.graphics.gbuffer; 42 import dagon.graphics.shadow; 43 import dagon.graphics.shader; 44 import dagon.graphics.framebuffer; 45 46 class EnvironmentPassShader: Shader 47 { 48 string vs = import("EnvironmentPass.vs"); 49 string fs = import("EnvironmentPass.fs"); 50 51 GBuffer gbuffer; 52 CascadedShadowMap shadowMap; 53 54 Matrix4x4f defaultShadowMatrix; 55 56 bool enableSSAO = false; 57 int ssaoSamples = 16; 58 float ssaoRadius = 0.2f; 59 float ssaoPower = 4.0f; 60 61 this(GBuffer gbuffer, CascadedShadowMap shadowMap, Owner o) 62 { 63 auto myProgram = New!ShaderProgram(vs, fs, this); 64 super(myProgram, o); 65 this.gbuffer = gbuffer; 66 this.shadowMap = shadowMap; 67 this.defaultShadowMatrix = Matrix4x4f.identity; 68 } 69 70 void bind(RenderingContext* rc2d, RenderingContext* rc3d) 71 { 72 setParameter("modelViewMatrix", rc2d.modelViewMatrix); 73 setParameter("projectionMatrix", rc2d.projectionMatrix); 74 75 setParameter("camProjectionMatrix", rc3d.projectionMatrix); 76 setParameter("camViewMatrix", rc3d.viewMatrix); 77 setParameter("camInvViewMatrix", rc3d.invViewMatrix); 78 79 setParameter("viewSize", Vector2f(gbuffer.width, gbuffer.height)); 80 81 setParameter("sunDirection", rc3d.environment.sunDirectionEye(rc3d.viewMatrix)); 82 setParameter("sunColor", rc3d.environment.sunColor); 83 setParameter("sunEnergy", rc3d.environment.sunEnergy); 84 85 // Texture 0 - color buffer 86 glActiveTexture(GL_TEXTURE0); 87 glBindTexture(GL_TEXTURE_2D, gbuffer.colorTexture); 88 setParameter("colorBuffer", 0); 89 90 // Texture 1 - roughness-metallic-specularity buffer 91 glActiveTexture(GL_TEXTURE1); 92 glBindTexture(GL_TEXTURE_2D, gbuffer.rmsTexture); 93 setParameter("rmsBuffer", 1); 94 95 // Texture 2 - position buffer 96 glActiveTexture(GL_TEXTURE2); 97 glBindTexture(GL_TEXTURE_2D, gbuffer.positionTexture); 98 setParameter("positionBuffer", 2); 99 100 // Texture 3 - normal buffer 101 glActiveTexture(GL_TEXTURE3); 102 glBindTexture(GL_TEXTURE_2D, gbuffer.normalTexture); 103 setParameter("normalBuffer", 3); 104 105 // Texture 4 - environment 106 if (rc3d.environment.environmentMap) 107 { 108 glActiveTexture(GL_TEXTURE4); 109 rc3d.environment.environmentMap.bind(); 110 setParameter("envTexture", 4); 111 setParameterSubroutine("environment", ShaderType.Fragment, "environmentTexture"); 112 } 113 else 114 { 115 setParameter("skyZenithColor", rc3d.environment.skyZenithColor); 116 setParameter("skyHorizonColor", rc3d.environment.skyHorizonColor); 117 setParameter("groundColor", rc3d.environment.groundColor); 118 setParameter("skyEnergy", rc3d.environment.skyEnergy); 119 setParameter("groundEnergy", rc3d.environment.groundEnergy); 120 setParameterSubroutine("environment", ShaderType.Fragment, "environmentSky"); 121 } 122 123 // Texture 5 - emission buffer 124 glActiveTexture(GL_TEXTURE5); 125 glBindTexture(GL_TEXTURE_2D, gbuffer.emissionTexture); 126 setParameter("emissionBuffer", 5); 127 128 // Texture 6 - shadow map cascades (3 layer texture array) 129 if (shadowMap) 130 { 131 glActiveTexture(GL_TEXTURE6); 132 glBindTexture(GL_TEXTURE_2D_ARRAY, shadowMap.depthTexture); 133 setParameter("shadowTextureArray", 6); 134 setParameter("shadowTextureSize", cast(float)shadowMap.size); 135 setParameter("shadowMatrix1", shadowMap.area1.shadowMatrix); 136 setParameter("shadowMatrix2", shadowMap.area2.shadowMatrix); 137 setParameter("shadowMatrix3", shadowMap.area3.shadowMatrix); 138 } 139 else 140 { 141 setParameter("shadowMatrix1", defaultShadowMatrix); 142 setParameter("shadowMatrix2", defaultShadowMatrix); 143 setParameter("shadowMatrix3", defaultShadowMatrix); 144 } 145 146 // SSAO 147 setParameter("enableSSAO", enableSSAO); 148 setParameter("ssaoSamples", ssaoSamples); 149 setParameter("ssaoRadius", ssaoRadius); 150 setParameter("ssaoPower", ssaoPower); 151 152 // Fog 153 setParameter("fogColor", rc3d.environment.fogColor); 154 setParameter("fogStart", rc3d.environment.fogStart); 155 setParameter("fogEnd", rc3d.environment.fogEnd); 156 157 glActiveTexture(GL_TEXTURE0); 158 159 super.bind(rc2d); 160 } 161 162 void unbind(RenderingContext* rc2d, RenderingContext* rc3d) 163 { 164 super.unbind(rc2d); 165 166 glActiveTexture(GL_TEXTURE0); 167 glBindTexture(GL_TEXTURE_2D, 0); 168 169 glActiveTexture(GL_TEXTURE1); 170 glBindTexture(GL_TEXTURE_2D, 0); 171 172 glActiveTexture(GL_TEXTURE2); 173 glBindTexture(GL_TEXTURE_2D, 0); 174 175 glActiveTexture(GL_TEXTURE3); 176 glBindTexture(GL_TEXTURE_2D, 0); 177 178 glActiveTexture(GL_TEXTURE4); 179 glBindTexture(GL_TEXTURE_2D, 0); 180 181 glActiveTexture(GL_TEXTURE5); 182 glBindTexture(GL_TEXTURE_2D, 0); 183 184 glActiveTexture(GL_TEXTURE6); 185 glBindTexture(GL_TEXTURE_2D_ARRAY, 0); 186 187 glActiveTexture(GL_TEXTURE7); 188 glBindTexture(GL_TEXTURE_2D, 0); 189 190 glActiveTexture(GL_TEXTURE0); 191 } 192 }