public void Compile(OpenGL gl, string fragmentShader, int raysPerPixel) { _RaysPerPixel = raysPerPixel; _FramesRendered = 0; _Rays = 0; _StopWatch = new Stopwatch(); _Program = fragmentShader; const uint positionAttribute = 0; var attributeLocations = new Dictionary <uint, string> { { positionAttribute, "Position" } }; try { if (_BurnVariables) { ShaderVariables shaderVars = GetShaderVars(); shaderVars.BurnVariables(ref fragmentShader); } DateTime start = DateTime.Now; shaderRayMarch = new ShaderProgram(); shaderRayMarch.Create(gl, ManifestResourceLoader.LoadTextFile(@"Shaders\RayMarchProgressive.vert"), fragmentShader, attributeLocations); DateTime end = DateTime.Now; TimeSpan duration = end - start; Console.WriteLine(duration.ToString()); } catch (ShaderCompilationException exception) { _ShaderError = true; MessageBox.Show(exception.Message + "\r\n" + exception.CompilerOutput); } }
/// <summary> /// Initialises the Scene. /// </summary> /// <param name="gl">The OpenGL instance.</param> public void Initialise(OpenGL gl, int width, int height, mat4 viewMatrix, vec3 position, bool burnVariables) { Logger.Log("ShaderRenderer.Initialise Started"); _GL = gl; if (_Initialised) { Destroy(gl); _Initialised = true; } _ViewMatrix = viewMatrix; _Position = position; _TargetWidth = width; _TargetHeight = height; _ProgressiveSteps = 1; _ProgressiveIndex = 0; _BurnVariables = burnVariables; // We're going to specify the attribute locations for the position and normal, // so that we can force both shaders to explicitly have the same locations. const uint positionAttribute = 0; var attributeLocations = new Dictionary <uint, string> { { positionAttribute, "Position" } }; Logger.Log("ShaderRenderer.Initialise Loading shaders from manifest"); // Create the raymarch shader shaderRayMarch = new ShaderProgram(); if (_Program == null) { shaderRayMarch.Create(gl, ManifestResourceLoader.LoadTextFile(@"Shaders\RayMarchProgressive.vert"), ManifestResourceLoader.LoadTextFile(@"Shaders\RayMarch.frag"), attributeLocations); } else { if (_BurnVariables) { ShaderVariables shaderVars = GetShaderVars(); shaderVars.BurnVariables(ref _Program); } _ShaderError = false; try { shaderRayMarch.Create(gl, ManifestResourceLoader.LoadTextFile(@"Shaders\RayMarchProgressive.vert"), _Program, attributeLocations); } catch (ShaderCompilationException exception) { _ShaderError = true; MessageBox.Show(exception.Message + "\r\n" + exception.CompilerOutput); } } // Create the transfer shader string fragShader = @" #version 130 in vec2 texCoord; out vec4 FragColor; uniform float mode; // 0=ramp, 1=exposure, 2=standard uniform float toneFactor; uniform float gammaFactor; uniform float gammaContrast; uniform sampler2D renderedTexture; vec3 filmic(vec3 value) { float A=0.22; float B=0.30; float C=0.1; float D=0.2; float E=0.01; float F=0.3; return ((value*(A*value+C*B)+D*E)/(value*(A*value+B)+D*F)) - E/F; } void main() { vec4 rgb = texture(renderedTexture, vec2((texCoord.x+1)*0.5, (texCoord.y+1)*0.5)); FragColor=rgb; // FragColor.rgb /= FragColor.a; // brightness/contrast float luminance = dot(FragColor.rgb, vec3(0.2126,0.7152,0.0722)); float luminanceOut = gammaFactor * pow(luminance, gammaContrast); float multiplier = (max(0, luminance) * luminanceOut) / (luminance * luminance); FragColor.rgb *= multiplier; if (mode>2.9 && mode<3.1) { //filmic https://www.slideshare.net/ozlael/hable-john-uncharted2-hdr-lighting FragColor.rgb = filmic(FragColor.rgb)/filmic(vec3(toneFactor)); } else if (mode>1.9 && mode<2.1) { //reinhard https://imdoingitwrong.wordpress.com/2010/08/19/why-reinhard-desaturates-my-blacks-3/ float nL = luminance * (1+luminance/(toneFactor*toneFactor)) / (1+luminance); FragColor.rgb *= nL; } else if (mode>0.9 && mode<1.1) { //exposure originally Matt Fairclough FragColor.rgb = 1 - exp(-FragColor.rgb * toneFactor); } else { FragColor.rgb /= toneFactor; } } "; shaderTransfer = new ShaderProgram(); shaderTransfer.Create(gl, ManifestResourceLoader.LoadTextFile(@"Shaders\RayMarch.vert"), fragShader, attributeLocations); CheckForError(gl); fragShader = @" #version 130 in vec2 texCoord; out vec4 FragColor; void main() { FragColor=vec4(0,0,0,0); } "; shaderClean = new ShaderProgram(); shaderClean.Create(gl, ManifestResourceLoader.LoadTextFile(@"Shaders\RayMarch.vert"), fragShader, attributeLocations); CheckForError(gl); // Create the transfer shader string fragShaderIntTransfer = @" #version 130 in vec2 texCoord; out vec4 FragColor; uniform sampler2D renderedTexture; void main() { vec4 rgb = texture(renderedTexture, vec2((texCoord.x+1)*0.5, (texCoord.y+1)*0.5)); FragColor=rgb; } "; shaderIntTransfer = new ShaderProgram(); shaderIntTransfer.Create(gl, ManifestResourceLoader.LoadTextFile(@"Shaders\RayMarch.vert"), fragShaderIntTransfer, attributeLocations); CheckForError(gl); Logger.Log("ShaderRenderer.Initialise Loading random numbers"); LoadRandomNumbers(gl); Logger.Log("ShaderRenderer.Initialise Finished loading random numbers"); float[] viewport = new float[4]; gl.GetFloat(OpenGL.GL_VIEWPORT, viewport); gl.GenFramebuffersEXT(2, _FrameBuffer); CheckForError(gl); gl.GenTextures(2, _RaytracerBuffer); CheckForError(gl); for (int i = 0; i < 2; i++) { gl.BindFramebufferEXT(OpenGL.GL_FRAMEBUFFER_EXT, _FrameBuffer[i]); gl.BindTexture(OpenGL.GL_TEXTURE_2D, _RaytracerBuffer[i]); gl.TexParameter(OpenGL.GL_TEXTURE_2D, OpenGL.GL_TEXTURE_MAG_FILTER, OpenGL.GL_LINEAR); gl.TexParameter(OpenGL.GL_TEXTURE_2D, OpenGL.GL_TEXTURE_MIN_FILTER, OpenGL.GL_LINEAR); gl.TexParameter(OpenGL.GL_TEXTURE_2D, OpenGL.GL_TEXTURE_WRAP_S, OpenGL.GL_CLAMP_TO_EDGE); gl.TexParameter(OpenGL.GL_TEXTURE_2D, OpenGL.GL_TEXTURE_WRAP_T, OpenGL.GL_CLAMP_TO_EDGE); gl.TexParameter(OpenGL.GL_TEXTURE_2D, OpenGL.GL_GENERATE_MIPMAP_SGIS, OpenGL.GL_FALSE); // automatic mipmap // gl.TexImage2D(OpenGL.GL_TEXTURE_2D, 0, OpenGL.GL_RGBA, (int)viewport[2], (int)viewport[3], 0, // OpenGL.GL_RGBA, OpenGL.GL_FLOAT, null); gl.TexImage2D(OpenGL.GL_TEXTURE_2D, 0, OpenGL.GL_RGBA32F, _TargetWidth, _TargetHeight, 0, OpenGL.GL_RGBA, OpenGL.GL_FLOAT, null); CheckForError(gl); gl.FramebufferTexture2DEXT(OpenGL.GL_FRAMEBUFFER_EXT, OpenGL.GL_COLOR_ATTACHMENT0_EXT, OpenGL.GL_TEXTURE_2D, _RaytracerBuffer[i], 0); gl.FramebufferRenderbufferEXT(OpenGL.GL_FRAMEBUFFER_EXT, OpenGL.GL_DEPTH_ATTACHMENT_EXT, OpenGL.GL_RENDERBUFFER_EXT, 0); } gl.BindTexture(OpenGL.GL_TEXTURE_2D, 0); gl.GenFramebuffersEXT(2, _EffectFrameBuffer); CheckForError(gl); gl.GenTextures(2, _EffectRaytracerBuffer); CheckForError(gl); for (int i = 0; i < 2; i++) { gl.BindFramebufferEXT(OpenGL.GL_FRAMEBUFFER_EXT, _EffectFrameBuffer[i]); gl.BindTexture(OpenGL.GL_TEXTURE_2D, _EffectRaytracerBuffer[i]); gl.TexParameter(OpenGL.GL_TEXTURE_2D, OpenGL.GL_TEXTURE_MAG_FILTER, OpenGL.GL_LINEAR); gl.TexParameter(OpenGL.GL_TEXTURE_2D, OpenGL.GL_TEXTURE_MIN_FILTER, OpenGL.GL_LINEAR); gl.TexParameter(OpenGL.GL_TEXTURE_2D, OpenGL.GL_TEXTURE_WRAP_S, OpenGL.GL_CLAMP_TO_EDGE); gl.TexParameter(OpenGL.GL_TEXTURE_2D, OpenGL.GL_TEXTURE_WRAP_T, OpenGL.GL_CLAMP_TO_EDGE); gl.TexParameter(OpenGL.GL_TEXTURE_2D, OpenGL.GL_GENERATE_MIPMAP_SGIS, OpenGL.GL_FALSE); // automatic mipmap gl.TexImage2D(OpenGL.GL_TEXTURE_2D, 0, OpenGL.GL_RGBA32F, _TargetWidth, _TargetHeight, 0, OpenGL.GL_RGBA, OpenGL.GL_FLOAT, null); CheckForError(gl); gl.FramebufferTexture2DEXT(OpenGL.GL_FRAMEBUFFER_EXT, OpenGL.GL_COLOR_ATTACHMENT0_EXT, OpenGL.GL_TEXTURE_2D, _EffectRaytracerBuffer[i], 0); gl.FramebufferRenderbufferEXT(OpenGL.GL_FRAMEBUFFER_EXT, OpenGL.GL_DEPTH_ATTACHMENT_EXT, OpenGL.GL_RENDERBUFFER_EXT, 0); } gl.BindTexture(OpenGL.GL_TEXTURE_2D, 0); // and now initialise the integer framebuffer gl.GenFramebuffersEXT(1, _IntFrameBuffer); CheckForError(gl); gl.GenTextures(1, _PostprocessBuffer); CheckForError(gl); gl.BindFramebufferEXT(OpenGL.GL_FRAMEBUFFER_EXT, _IntFrameBuffer[0]); gl.BindTexture(OpenGL.GL_TEXTURE_2D, _PostprocessBuffer[0]); gl.TexParameter(OpenGL.GL_TEXTURE_2D, OpenGL.GL_TEXTURE_MAG_FILTER, OpenGL.GL_LINEAR); gl.TexParameter(OpenGL.GL_TEXTURE_2D, OpenGL.GL_TEXTURE_MIN_FILTER, OpenGL.GL_LINEAR); gl.TexParameter(OpenGL.GL_TEXTURE_2D, OpenGL.GL_TEXTURE_WRAP_S, OpenGL.GL_CLAMP_TO_EDGE); gl.TexParameter(OpenGL.GL_TEXTURE_2D, OpenGL.GL_TEXTURE_WRAP_T, OpenGL.GL_CLAMP_TO_EDGE); gl.TexParameter(OpenGL.GL_TEXTURE_2D, OpenGL.GL_GENERATE_MIPMAP_SGIS, OpenGL.GL_FALSE); // automatic mipmap gl.TexImage2D(OpenGL.GL_TEXTURE_2D, 0, OpenGL.GL_RGBA, _TargetWidth, _TargetHeight, 0, OpenGL.GL_RGBA, OpenGL.GL_UNSIGNED_BYTE, null); CheckForError(gl); gl.FramebufferTexture2DEXT(OpenGL.GL_FRAMEBUFFER_EXT, OpenGL.GL_COLOR_ATTACHMENT0_EXT, OpenGL.GL_TEXTURE_2D, _PostprocessBuffer[0], 0); gl.FramebufferRenderbufferEXT(OpenGL.GL_FRAMEBUFFER_EXT, OpenGL.GL_DEPTH_ATTACHMENT_EXT, OpenGL.GL_RENDERBUFFER_EXT, 0); gl.BindTexture(OpenGL.GL_TEXTURE_2D, 0); _PostProcess = new PostProcess(); _PostProcess.Initialise(gl); gl.GenFramebuffersEXT(1, _DepthFrameBuffer); gl.GenTextures(1, _DepthCalcBuffer); gl.BindFramebufferEXT(OpenGL.GL_FRAMEBUFFER_EXT, _DepthFrameBuffer[0]); gl.BindTexture(OpenGL.GL_TEXTURE_2D, _DepthCalcBuffer[0]); gl.TexParameter(OpenGL.GL_TEXTURE_2D, OpenGL.GL_TEXTURE_MAG_FILTER, OpenGL.GL_LINEAR); gl.TexParameter(OpenGL.GL_TEXTURE_2D, OpenGL.GL_TEXTURE_MIN_FILTER, OpenGL.GL_LINEAR_MIPMAP_LINEAR); gl.TexParameter(OpenGL.GL_TEXTURE_2D, OpenGL.GL_TEXTURE_WRAP_S, OpenGL.GL_CLAMP_TO_EDGE); gl.TexParameter(OpenGL.GL_TEXTURE_2D, OpenGL.GL_TEXTURE_WRAP_T, OpenGL.GL_CLAMP_TO_EDGE); gl.TexParameter(OpenGL.GL_TEXTURE_2D, OpenGL.GL_GENERATE_MIPMAP_SGIS, OpenGL.GL_TRUE); gl.TexImage2D(OpenGL.GL_TEXTURE_2D, 0, OpenGL.GL_RGBA32F, 1, 1, 0, OpenGL.GL_RGBA, OpenGL.GL_FLOAT, null); gl.FramebufferTexture2DEXT(OpenGL.GL_FRAMEBUFFER_EXT, OpenGL.GL_COLOR_ATTACHMENT0_EXT, OpenGL.GL_TEXTURE_2D, _DepthCalcBuffer[0], 0); gl.FramebufferRenderbufferEXT(OpenGL.GL_FRAMEBUFFER_EXT, OpenGL.GL_DEPTH_ATTACHMENT_EXT, OpenGL.GL_RENDERBUFFER_EXT, 0); _Initialised = true; /* * gl.GenRenderbuffersEXT(2, _RaytracerBuffer); * gl.BindRenderbufferEXT(OpenGL.GL_RENDERBUFFER_EXT, _RaytracerBuffer[0]); * gl.RenderbufferStorageEXT(OpenGL.GL_RENDERBUFFER_EXT, OpenGL.GL_RGBA32F, (int)viewport[2], (int)viewport[3]); * gl.BindRenderbufferEXT(OpenGL.GL_RENDERBUFFER_EXT, _RaytracerBuffer[1]); * gl.RenderbufferStorageEXT(OpenGL.GL_RENDERBUFFER_EXT, OpenGL.GL_RGBA32F, (int)viewport[2], (int)viewport[3]); */ // gl.GenRenderbuffersEXT(1, _RenderBuffer); //gl.BindRenderbufferEXT(OpenGL.GL_RENDERBUFFER_EXT, _RenderBuffer[0]); //gl.RenderbufferStorageEXT(OpenGL.GL_RENDERBUFFER_EXT, OpenGL.GL_RGBA, (int)viewport[2], (int)viewport[3]); Logger.Log("ShaderRenderer.Initialise Finished"); }
public void Initialise(OpenGL gl) { const uint positionAttribute = 0; var attributeLocations = new Dictionary <uint, string> { { positionAttribute, "Position" } }; string fragShaderHighlights = @" #version 130 in vec2 texCoord; out vec4 FragColor; uniform sampler2D renderedTexture; uniform float exponent; void main() { vec4 rgb = texture(renderedTexture, vec2((texCoord.x+1)*0.5, (texCoord.y+1)*0.5)); rgb = pow(rgb, vec4(exponent)); FragColor=rgb; } "; shaderHighlights = new ShaderProgram(); shaderHighlights.Create(gl, ManifestResourceLoader.LoadTextFile(@"Shaders\RayMarch.vert"), fragShaderHighlights, attributeLocations); PostProcess.GaussianShader gaussianShader = new PostProcess.GaussianShader(); shaderGaussianX = new ShaderProgram(); shaderGaussianX.Create(gl, ManifestResourceLoader.LoadTextFile(@"Shaders\RayMarch.vert"), gaussianShader.GetShader((float)_GaussianSD, 0, 0.005f), attributeLocations); shaderGaussianY = new ShaderProgram(); shaderGaussianY.Create(gl, ManifestResourceLoader.LoadTextFile(@"Shaders\RayMarch.vert"), gaussianShader.GetShader((float)_GaussianSD, (float)Math.PI / 2, 0.005f), attributeLocations); string fragShaderBlend = @" #version 130 in vec2 texCoord; out vec4 FragColor; uniform sampler2D renderedTexture1; uniform sampler2D renderedTexture2; uniform float factor1; uniform float factor2; void main() { vec4 rgb1 = texture(renderedTexture1, vec2((texCoord.x+1)*0.5, (texCoord.y+1)*0.5)); rgb1.rgb /= rgb1.a; vec4 rgb2 = texture(renderedTexture2, vec2((texCoord.x+1)*0.5, (texCoord.y+1)*0.5)); FragColor=rgb1*factor1 + rgb2*factor2; } "; shaderBlend = new ShaderProgram(); shaderBlend.Create(gl, ManifestResourceLoader.LoadTextFile(@"Shaders\RayMarch.vert"), fragShaderBlend, attributeLocations); // Create the transfer shader string fragShaderRGBATransfer = @" #version 130 in vec2 texCoord; out vec4 FragColor; uniform sampler2D renderedTexture; void main() { FragColor = texture(renderedTexture, vec2((texCoord.x+1)*0.5, (texCoord.y+1)*0.5)); FragColor.rgb /= FragColor.a; } "; shaderRGBA = new ShaderProgram(); shaderRGBA.Create(gl, ManifestResourceLoader.LoadTextFile(@"Shaders\RayMarch.vert"), fragShaderRGBATransfer, attributeLocations); }