Exemplo n.º 1
0
 public static void CacheShaderProgram(string allnames, ShaderProgram compiled)
 {
     lock(Cache)
     {
         if(!Cache.ContainsKey(allnames))Cache.Add(allnames, compiled);
     }
 }
Exemplo n.º 2
0
 public static void MapSimpleLightsToShader(ShaderProgram sp)
 {
     sp.SetUniform("SimpleLightsCount", SimpleLights.Count);
     sp.SetUniformArray("SimpleLightsPos", SimpleLights.Select<SimplePointLight, Vector3>(a => a.Transformation.GetPosition()).ToArray());
     sp.SetUniformArray("SimpleLightsColors", SimpleLights.Select<SimplePointLight, Vector4>(a =>
         new Vector4(a.Color.R / 255.0f, a.Color.G / 255.0f, a.Color.B / 255.0f, a.Color.A / 255.0f)).ToArray());
 }
Exemplo n.º 3
0
        public static ShaderProgram Compile(string vertex, string fragment, string geometry = null, string tesscontrol = null, string tesseval = null)
        {
            string concatedNames = vertex + fragment + geometry + (tesscontrol != null ? tesscontrol : "notess") + (tesseval != null ? tesseval : "notessev");

            var cached = ShaderCache.GetShaderProgramOrNull(concatedNames);
            if(cached != null)
                return cached;
            var output = new ShaderProgram(vertex, fragment, geometry, tesscontrol, tesseval);
            ShaderCache.CacheShaderProgram(concatedNames, output);
            return output;
        }
Exemplo n.º 4
0
 public PassiveVoxelizer()
 {
     VoxelizerShader = ShaderProgram.Compile("Voxelizer.vertex.glsl", "Voxelizer.fragment.glsl");
     BoxesSSBO = new ShaderStorageBuffer();
     DrawingFramebuffer = new Framebuffer(256, 256, false)
     {
         ColorOnly = true,
         ColorInternalFormat = PixelInternalFormat.R8,
         ColorPixelType = PixelType.Byte,
         ColorPixelFormat = PixelFormat.Red
     };
 }
Exemplo n.º 5
0
 public ShaderPack(string fs = null)
 {
     ProgramsList = new List<ShaderProgram>();
     if(Program == null)
         Program = ShaderProgram.Compile("Generic.vertex.glsl",
            fs);
     if(TesselatedProgram == null)
         TesselatedProgram = ShaderProgram.Compile("Generic.vertex.glsl",
             fs, null, "Generic.tesscontrol.glsl", "Generic.tesseval.glsl");
     if(Geometry1iTriangles == null)
         Geometry1iTriangles = ShaderProgram.Compile("Generic.vertex.glsl",
             fs, "Generic.geometry1iTriangles.geometry.glsl");
     if(Geometry96iTriangles == null)
         Geometry96iTriangles = ShaderProgram.Compile("Generic.vertex.glsl",
             fs, "Generic.geometry96iTriangles.geometry.glsl");
     ProgramsList.AddRange(new ShaderProgram[] {
         Program,
         TesselatedProgram,
         Geometry1iTriangles,
         Geometry96iTriangles,
     });
 }
Exemplo n.º 6
0
        public Renderer(int initialWidth, int initialHeight)
        {
            

            CubeMap = new CubeMapTexture(Media.Get("posx.jpg"), Media.Get("posy.jpg"), Media.Get("posz.jpg"),
                Media.Get("negx.jpg"), Media.Get("negy.jpg"), Media.Get("negz.jpg"));

            Width = initialWidth;
            Height = initialHeight;
            //   initialWidth *= 4; initialHeight *= 4;
            MRT = new MRTFramebuffer(initialWidth, initialHeight);
            
            DistanceFramebuffer = new Framebuffer(initialWidth / 1, initialHeight / 1)
            {
                ColorOnly = false,
                ColorInternalFormat = PixelInternalFormat.R32f,
                ColorPixelFormat = PixelFormat.Red,
                ColorPixelType = PixelType.Float
            };

            HDRShader = ShaderProgram.Compile("PostProcess.vertex.glsl", "HDR.fragment.glsl");
            
            PostProcessingMesh = new Object3dInfo(VertexInfo.FromFloatArray(postProcessingPlaneVertices));
        }
Exemplo n.º 7
0
        public bool Use()
        {
            var prg = GetShaderProgram();
            // if(prg.) {
            //GL.Disable(EnableCap.CullFace);
            // }
            if(lastUserProgram == null)
                lastUserProgram = ShaderProgram.Current;
            ShaderProgram.SwitchResult res = prg.Use();
            //  if(res == ShaderProgram.SwitchResult.Locked)
            //  {
            //      prg = ShaderProgram.Current;
            // }
            // if(lastUserProgram == ShaderProgram.Current)
            //      return true;
            //lastUserProgram = ShaderProgram.Current;
            prg.SetUniform("TesselationMultiplier", TesselationMultiplier);
            if(NormalsTexture != null)
            {
                prg.SetUniform("UseNormalsTex", 1);
                prg.SetUniform("InvertNormalMap", InvertNormalMap);
                prg.SetUniform("NormalMapScale", NormalMapScale);
                NormalsTexture.Use(TextureUnit.Texture2);
            }
            else
                prg.SetUniform("UseNormalsTex", 0);

            if(BumpTexture != null)
            {
                prg.SetUniform("UseBumpTex", 1);
                BumpTexture.Use(TextureUnit.Texture3);
            }
            else
                prg.SetUniform("UseBumpTex", 0);
            
            if(RoughnessTexture != null)
            {
                prg.SetUniform("UseRoughnessTex", 1);
                RoughnessTexture.Use(TextureUnit.Texture7);
            }
            else
                prg.SetUniform("UseRoughnessTex", 0);

            if(AlphaTexture != null)
            {
                prg.SetUniform("UseAlphaTex", 1);
                AlphaTexture.Use(TextureUnit.Texture4);
            }
            else
                prg.SetUniform("UseAlphaTex", 0);

            if(DiffuseTexture != null)
            {
                prg.SetUniform("UseDiffuseTex", 1);
                DiffuseTexture.Use(TextureUnit.Texture5);
            }
            else
                prg.SetUniform("UseDiffuseTex", 0);

            if(SpecularTexture != null)
            {
                prg.SetUniform("UseSpecularTex", 1);
                SpecularTexture.Use(TextureUnit.Texture6);
            }
            else
                prg.SetUniform("UseSpecularTex", 0);

            if(Type == MaterialType.Grass)
            {
                GL.Disable(EnableCap.CullFace);
            }
            
            //prg.SetUniform("input_Color", DiffuseColor);
            prg.SetUniform("DrawMode", (int)Mode);
            prg.SetUniform("MaterialType", (int)Type);

            if(Type == MaterialType.DropsSystem && RainsDropSystem != null)
                RainsDropSystem.MapToCurrentShader();


            // pbr stuff
            prg.SetUniform("SpecularColor", SpecularColor);
            prg.SetUniform("DiffuseColor", DiffuseColor);
            prg.SetUniform("Alpha", Alpha);


            prg.SetUniform("Roughness", Roughness);
            prg.SetUniform("ReflectionStrength", ReflectionStrength);
            prg.SetUniform("RefractionStrength", RefractionStrength);
            prg.SetUniform("IgnoreLighting", IgnoreLighting);
            prg.SetUniform("ParallaxHeightMultiplier", ParallaxHeightMultiplier);
            prg.SetUniform("ParallaxInstances", ParallaxInstances);

        //    if(Color.W < 1.0f)
         //       GL.DepthMask(false);
         //   else
        //        GL.DepthMask(true);

            // prg.SetUniform("AORange", AORange);
            // prg.SetUniform("AOStrength", AOStrength);
            // prg.SetUniform("AOAngleCutoff", AOAngleCutoff);
            //  prg.SetUniform("VDAOMultiplier", VDAOMultiplier);
            // prg.SetUniform("VDAOSamplingMultiplier", VDAOSamplingMultiplier);
            // prg.SetUniform("VDAORefreactionMultiplier", VDAORefreactionMultiplier);
            //prg.SetUniform("SubsurfaceScatteringMultiplier", SubsurfaceScatteringMultiplier);
            // prg.SetUniform("FrameINT", (int)PostProcessing.RandomIntFrame);

            // GL.BindImageTexture(22u, (uint)PostProcessing.FullScene3DTexture.Handle, 0, false, 0,
            // TextureAccess.ReadWrite, SizedInternalFormat.R32ui);

            return true;
        }
Exemplo n.º 8
0
 public bool Use()
 {
     if(!Lock)
     {
         //if(Current == this)
         //    return false;
         if(!Compiled)
             Compile();
         GL.UseProgram(Handle);
         Current = this;
         return true;
     }
     return false;
 }
Exemplo n.º 9
0
 public SwitchResult Use()
 {
     if(!Compiled)
         Compile();
     if(Current == this)
         return SwitchResult.AlreadyInUse;
     GL.UseProgram(Handle);
     Current = this;
     return SwitchResult.Switched;
 }
Exemplo n.º 10
0
        public PostProcessing(int initialWidth, int initialHeight)
        {
            //FullScene3DTexture = new Texture3D(new Vector3(64, 64, 64));
            TestBuffer = new ShaderStorageBuffer();
            NumbersTexture = new Texture(Media.Get("numbers.png"));
            CShader = new ComputeShader("Blur.compute.glsl");
            GLThread.Invoke(() =>
            {
                TestBuffer.MapData(new Vector3[4]{
                    new Vector3(1, 0.25f, 1), new Vector3(0, 0.55f, 0.75f),
                    new Vector3(1, 0.25f, 0), new Vector3(0.55f, 0, 0.75f)
                });

            });
            Width = initialWidth;
            Height = initialHeight;
            MSAAResolvingFrameBuffer = new Framebuffer(initialWidth, initialHeight);
            MSAAResolvingFrameBuffer.SetMultiSample(true);

            MRT = new MRTFramebuffer(initialWidth, initialHeight);
            BackMRT = new MRTFramebuffer(initialWidth / 4, initialHeight / 4);

            Pass1FrameBuffer = new Framebuffer(initialWidth, initialHeight);
            Pass2FrameBuffer = new Framebuffer(initialWidth, initialHeight);

            LightPointsFrameBuffer = new Framebuffer(initialWidth / 6, initialHeight / 6);
            BloomFrameBuffer = new Framebuffer(initialWidth / 4, initialHeight / 4);
            FogFramebuffer = new Framebuffer(initialWidth, initialHeight);
            SmallFrameBuffer = new Framebuffer(initialWidth / 10, initialHeight / 10);
            LastWorldPositionFramebuffer = new Framebuffer(initialWidth / 1, initialHeight / 1);
            RSMFramebuffer = new Framebuffer(initialWidth / 1, initialHeight / 1);
            SSReflectionsFramebuffer = new Framebuffer(initialWidth / 1, initialHeight / 1);
            VDAOFramebuffer = new Framebuffer(initialWidth / 1, initialHeight / 1);

            GlobalIlluminationFrameBuffer = new Framebuffer(initialWidth, initialHeight);
            //GlobalIlluminationFrameBuffer.ColorInternalFormat = PixelInternalFormat.R8;
            //GlobalIlluminationFrameBuffer.ColorPixelFormat = PixelFormat.Red;
            //GlobalIlluminationFrameBuffer.ColorPixelType = PixelType.UnsignedByte;
            GlobalIlluminationFrameBuffer.Use();
            //BackDiffuseFrameBuffer = new Framebuffer(initialWidth / 2, initialHeight  / 2);
            //BackNormalsFrameBuffer = new Framebuffer(initialWidth / 2, initialHeight / 2);

            ScreenSpaceNormalsWriterShader = ShaderProgram.Compile("Generic.vertex.glsl", "ScreenSpaceNormalsWriter.fragment.glsl");
            BackDepthWriterShader = ShaderProgram.Compile("Generic.vertex.glsl", "BackDepthWriter.fragment.glsl");

            BloomShader = ShaderProgram.Compile("PostProcess.vertex.glsl", "Bloom.fragment.glsl");
            MSAAShader = ShaderProgram.Compile("PostProcess.vertex.glsl", "MSAA.fragment.glsl");
            SSAOShader = ShaderProgram.Compile("PostProcess.vertex.glsl", "SSAO.fragment.glsl");
            FogShader = ShaderProgram.Compile("PostProcess.vertex.glsl", "Fog.fragment.glsl");
            LightPointsShader = ShaderProgram.Compile("PostProcess.vertex.glsl", "LightPoints.fragment.glsl");
            LensBlurShader = ShaderProgram.Compile("PostProcess.vertex.glsl", "LensBlur.fragment.glsl");
            HDRShader = ShaderProgram.Compile("PostProcess.vertex.glsl", "HDR.fragment.glsl");
            BlitShader = ShaderProgram.Compile("PostProcess.vertex.glsl", "Blit.fragment.glsl");
            DeferredShader = ShaderProgram.Compile("PostProcess.vertex.glsl", "Deferred.fragment.glsl");
            CombinerShader = ShaderProgram.Compile("PostProcess.vertex.glsl", "Combiner.fragment.glsl");
            PathTracerOutputShader = ShaderProgram.Compile("PostProcess.vertex.glsl", "Output.fragment.glsl");
            GlobalIlluminationShaderX = ShaderProgram.Compile("PostProcess.vertex.glsl", "GlobalIllumination.fragment.glsl");
            GlobalIlluminationShaderX.SetGlobal("SEED", "gl_FragCoord.x");
            GlobalIlluminationShaderY = ShaderProgram.Compile("PostProcess.vertex.glsl", "GlobalIllumination.fragment.glsl");
            GlobalIlluminationShaderY.SetGlobal("SEED", "gl_FragCoord.y");
            RSMShader = ShaderProgram.Compile("PostProcess.vertex.glsl", "RSM.fragment.glsl");
            SSReflectionsShader = ShaderProgram.Compile("PostProcess.vertex.glsl", "SSReflections.fragment.glsl");
            VDAOShader = ShaderProgram.Compile("PostProcess.vertex.glsl", "VDAO.fragment.glsl");
            //ReflectShader = ShaderProgram.Compile(Media.ReadAllText("PostProcess.vertex.glsl"), Media.ReadAllText("Reflect.fragment.glsl"));

            Object3dInfo postPlane3dInfo = new Object3dInfo(postProcessingPlaneVertices, postProcessingPlaneIndices);
            PostProcessingMesh = new Mesh3d(postPlane3dInfo, new GenericMaterial(Color.Pink));
        }
Exemplo n.º 11
0
 private void SetLightingUniforms(ShaderProgram shader)
 {
     shader.SetUniformArray("LightsPs", LightPool.GetPMatrices());
     shader.SetUniformArray("LightsVs", LightPool.GetVMatrices());
     shader.SetUniformArray("LightsPos", LightPool.GetPositions());
     shader.SetUniformArray("LightsFarPlane", LightPool.GetFarPlanes());
     shader.SetUniformArray("LightsColors", LightPool.GetColors());
     shader.SetUniformArray("LightsRanges", LightPool.GetRanges());
     shader.SetUniformArray("LightsMixModes", LightPool.GetMixModes());
     shader.SetUniform("LightsCount", LightPool.GetPositions().Length);
 }
Exemplo n.º 12
0
 public void SetUniforms(ShaderProgram program)
 {
     program.SetUniform("HBAOContribution", HBAOContribution);
     program.SetUniform("GIContribution", GIContribution);
     program.SetUniform("MainLightAttentuation", MainLightAttentuation);
     program.SetUniform("SimpleLightAttentuation", SimpleLightAttentuation);
     program.SetUniform("FogContribution", FogContribution);
     program.SetUniform("FogSamples", 1.0f / FogSamples);
     program.SetUniform("GISamples", GISamples);
     program.SetUniform("HBAOSamples", HBAOSamples);
     program.SetUniform("ShadowsBlur", ShadowsBlur);
     program.SetUniform("ShadowsSamples", 1.0f / ShadowsSamples);
     program.SetUniform("LightPointSize", LightPointSize);
     program.SetUniform("SimpleLightPointSize", SimpleLightPointSize);
     program.SetUniform("BloomSamples", 1.0f / BloomSamples);
     program.SetUniform("BloomSize", BloomSize);
     program.SetUniform("BloomContribution", BloomContribution);
     program.SetUniform("GIDiffuseComponent", GIDiffuseComponent);
     program.SetUniform("HBAOStrength", HBAOStrength);
 }
Exemplo n.º 13
0
 void CompileShaders()
 {
     if(Program == null)
         Program = ShaderProgram.Compile("Generic.vertex.glsl",
             "Generic.fragment.glsl");
     if(TesselatedProgram == null)
         TesselatedProgram = ShaderProgram.Compile("Tesselation.vertex.glsl",
             "Generic.fragment.glsl", null, "Generic.tesscontrol.glsl", "Generic.tesseval.glsl");
 }
Exemplo n.º 14
0
        public void SetLightingUniforms(ShaderProgram shader)
        {
            // MEMORY LEAK
            pmats = new List<Matrix4>();
            shadowmaplayers = new List<int>();
            vmats = new List<Matrix4>();
            poss = new List<Vector3>();
            fplanes = new List<float>();
            colors = new List<Vector4>();
            mmodes = new List<int>();
            ipointer = 0;

            foreach(var e in Lights)
                if(e is IShadowMapableLight)
                {
                    var l = e as IShadowMapableLight;
                    var p = e as ILight;
                    pmats.Add(l.GetPMatrix());
                    vmats.Add(l.GetVMatrix());
                    shadowmaplayers.Add((l as ProjectionLight).ShadowMapArrayIndex);
                    poss.Add(p.GetPosition());
                    colors.Add(new Vector4(p.GetColor()));
                    ipointer++;
                }
            Game.ShadowMaps.Bind(0);
            shader.SetUniformArray("LightsPs", pmats.ToArray());
            shader.SetUniformArray("LightsVs", vmats.ToArray());
            shader.SetUniformArray("LightsShadowMapsLayer", shadowmaplayers.ToArray());
            shader.SetUniformArray("LightsPos", poss.ToArray());
            shader.SetUniformArray("LightsFarPlane", fplanes.ToArray());
            shader.SetUniformArray("LightsColors", colors.ToArray());
            shader.SetUniform("LightsCount", pmats.Count);
            pmats = new List<Matrix4>();
            vmats = new List<Matrix4>();
            poss = new List<Vector3>();
            fplanes = new List<float>();
            colors = new List<Vector4>();
            mmodes = new List<int>();
        }
Exemplo n.º 15
0
 public void MapLightsSSBOToShader(ShaderProgram sp)
 {
     SSBO.Use(6);
     ShaderProgram.Current.SetUniform("SimpleLightsCount", SimpleLightsCount);
 }