Esempio n. 1
0
 public Material(
     string name,
     IProgram program,
     IUniformBlock uniformBlock
     )
 {
     this.name     = name;
     Program       = program;
     uniformBuffer = UniformBufferFactory.Create(uniformBlock);
     uniformBuffer.Sync();
 }
Esempio n. 2
0
        private int modelCount;                         //  How many models in scene

        protected override void OnLoad(System.EventArgs e)
        {
            //  Check GL features
            RenderStack.Graphics.Configuration.Initialize();
            if (RenderStack.Graphics.Configuration.glslVersion < 150)
            {
                throw new System.PlatformNotSupportedException("You need at least GL 3.2");
            }

            AttributeMappings.Global.Add(0, "_position", VertexUsage.Position, 0, 3);
            AttributeMappings.Global.Add(1, "_normal", VertexUsage.Normal, 0, 3);

            //  Build uniform blocks
            {
                System.Text.StringBuilder uniforms = new System.Text.StringBuilder();
                CameraUniformBlock.NearFar             = "near_far";
                CameraUniformBlock.FovXFovYAspect      = "fovx_fovy_aspect";
                CameraUniformBlock.ViewToClip          = "view_to_clip_matrix";
                CameraUniformBlock.ClipToView          = "view_to_clip_matrix";
                CameraUniformBlock.WorldToClip         = "world_to_clip_matrix";
                CameraUniformBlock.ClipToWorld         = "clip_to_world_matrix";
                CameraUniformBlock.WorldToView         = "world_to_view_matrix";
                CameraUniformBlock.ViewToWorld         = "view_to_world_matrix";
                CameraUniformBlock.Viewport            = "viewport";
                CameraUniformBlock.ViewPositionInWorld = "view_position_in_world";
                CameraUniformBlock.Initialize("camera");
                uniforms.Append(CameraUniformBlock.UniformBlockGL.SourceGL);

                ModelsUB = new UniformBlockGL("models");
                ModelsUB.AddMat4("model_to_world_matrix", maxInstanceCount);
                ModelsUB.Seal();
                uniforms.Append(ModelsUB.SourceGL);

                GlobalUB = new UniformBlockGL("global");
                GlobalUB.AddVec4("add_color");
                GlobalUB.AddFloat("alpha");
                GlobalUB.Seal();
                uniforms.Append(GlobalUB.SourceGL);

                MaterialUB = new UniformBlockGL("material");
                MaterialUB.AddVec4("surface_diffuse_reflectance_color");
                MaterialUB.AddVec4("surface_specular_reflectance_color");
                MaterialUB.AddFloat("surface_roughness");
                MaterialUB.Seal();
                uniforms.Append(MaterialUB.SourceGL);

                LightsUB = new UniformBlockGL("lights");
                LightsUB.AddVec4("exposure");
                LightsUB.AddVec4("color", lightCount);
                LightsUB.AddVec4("ambient_light_color");
                LightsUB.AddVec4("direction", lightCount);
                LightsUB.AddMat4("world_to_shadow_matrix", lightCount);
                LightsUB.Seal();
                uniforms.Append(LightsUB.SourceGL);

                ShaderGL3.Replace("UNIFORMS;", uniforms.ToString());
            }

            //  Models
            List <Vector2> positions = UniformPoissonDiskSampler.SampleCircle(Vector2.Zero, poissonRadius, poissonMinDistance);

            modelCount = positions.Count;
            System.Diagnostics.Debug.WriteLine("Model count: " + modelCount);
            modelFrame = new Frame[modelCount];
            for (int i = 0; i < modelCount; ++i)
            {
                modelFrame[i] = new Frame();
                modelFrame[i].LocalToParent.Set(
                    Matrix4.CreateTranslation(positions[i].X, positions[i].Y, -40.0f)
                    );
            }

            //  Setup geometry and mesh
            Geometry geometry = new Sphere(radius, 20, 8);

            //Matrix4 translate = Matrix4.CreateTranslation(0.0f, 0.0f, -4.0f);
            //geometry.Transform(translate);
            mesh = new GeometryMesh(geometry, NormalStyle.PointNormals).GetMesh;
            //  \todo Get rid of this
            //mesh.VertexBufferRange.Buffer.VertexFormat.Seal(AttributeMappings.Global);

            //  This is where we collect models transformations
            models = UniformBufferFactory.Create(ModelsUB);

            global = UniformBufferFactory.Create(GlobalUB);
            global.Floats("alpha").Set(1.0f);
            global.Floats("add_color").Set(0.0f, 0.0f, 0.4f);
            global.Sync();

            //  Setup lights
            lights = UniformBufferFactory.Create(LightsUB);
            lights.Floats("exposure").Set(1.0f);
            lights.Floats("ambient_light_color").Set(1.0f, 1.0f, 1.0f);
            for (int i = 0; i < lightCount; ++i)
            {
                lights.Floats("direction").SetI(i, 0.0f, 1.0f, 0.0f);
                lights.Floats("color").SetI(i, 1.0f, 1.0f, 1.0f, 1.0f / (float)lightCount);
            }
            lights.Sync();

            global.Use();
            models.Use();
            lights.Use();

            //  Setup viewport and camera
            viewport = new Viewport(base.Width, base.Height);
            camera   = new Camera();
            camera.Projection.Near = 0.1f;
            camera.Projection.Far  = 300.0f;
            camera.Frame.LocalToParent.Set(
                Matrix4.CreateLookAt(
                    new Vector3(0.0f, 0.0f, 4.0f),      //  camera location
                    new Vector3(0.0f, 0.0f, 0.0f),      //  aim point
                    new Vector3(0.0f, 1.0f, 0.0f)       //  up vector
                    )
                );

            camera.Projection.FovYRadians    = RenderStack.Math.Conversions.DegreesToRadians(60.0f);
            camera.Projection.ProjectionType = ProjectionType.PerspectiveVertical;
            camera.UniformBufferGL.Use();

            //  Setup shader program
            program = (IProgram) new ProgramGL3(vs, fs);

            //  Setup material
            for (int i = 0; i < Materials.Length; ++i)
            {
                var   material = UniformBufferFactory.Create(MaterialUB);
                float h = (float)(i) / (float)(Materials.Length - 1);
                float r, g, b;
                Conversions.HSVtoRGB(360.0f * h, 1.0f, 1.0f, out r, out g, out b);
                float diffuse   = 0.2f;
                float specular  = 0.8f;
                float roughness = 0.1f;
                material.Floats("surface_diffuse_reflectance_color").Set(diffuse * r, diffuse * g, diffuse * b);
                material.Floats("surface_specular_reflectance_color").Set(specular * r, specular * r, specular * r);
                material.Floats("surface_roughness").Set(roughness);
                material.Sync();
                Materials[i] = material;
            }

            //  Update scene. There is no animation in this scene
            //  so we only need to do it once.
            camera.Frame.UpdateHierarchical(serial);
            foreach (var frame in modelFrame)
            {
                frame.UpdateHierarchical(serial);
            }

            //  Setup initial GL state
            {
                //  Since we only use one program, we don't need to touch this after.
                camera.UpdateFrame();
                camera.UpdateViewport(viewport);
                camera.UniformBufferGL.Sync();

                //  Bind program
                program.Use(0);

                GL.Enable(EnableCap.DepthTest);
                GL.Enable(EnableCap.CullFace);

                float gammaHalf = (float)Math.Pow(0.5, 1.0 / 2.2);
                GL.ClearColor(gammaHalf, gammaHalf, gammaHalf, 1.0f);
                GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
                SwapBuffers();

                Visible = true;

                //  Since we only use one mesh, we don't need to change this after
                SetupAttributeBindings();

                //  The mesh has not yet been uploaded to GL, do it now
                IBufferRange vertexBufferRange = mesh.VertexBufferRange;
                IBufferRange indexBufferRange  = mesh.IndexBufferRange(MeshMode.PolygonFill);
                if (vertexBufferRange.NeedsUploadGL)
                {
                    vertexBufferRange.UpdateGL();
                }
                if (indexBufferRange.NeedsUploadGL)
                {
                    indexBufferRange.UpdateGL();
                }
            }

            Resize += new EventHandler <EventArgs>(Application_Resize);
            Unload += new EventHandler <EventArgs>(Application_Unload);
        }
Esempio n. 3
0
        // \brief Declare uniforms and set Shader replacement strings
        private void InitializeShaderSystem()
        {
            CameraUniformBlock.NearFar             = "near_far";
            CameraUniformBlock.ViewToClip          = "view_to_clip_matrix";
            CameraUniformBlock.ClipToView          = "view_to_clip_matrix";
            CameraUniformBlock.WorldToClip         = "world_to_clip_matrix";
            CameraUniformBlock.ClipToWorld         = "clip_to_world_matrix";
            CameraUniformBlock.WorldToView         = "world_to_view_matrix";
            CameraUniformBlock.ViewToWorld         = "view_to_world_matrix";
            CameraUniformBlock.Viewport            = "viewport";
            CameraUniformBlock.ViewPositionInWorld = "view_position_in_world";
            CameraUniformBlock.Initialize("camera");

            LightsUniforms.spec.Count             = "count";
            LightsUniforms.spec.Exposure          = "exposure";
            LightsUniforms.spec.Bias              = "bias";
            LightsUniforms.spec.AmbientLightColor = "ambient_light_color";
            LightsUniforms.spec.WorldToLight      = "world_to_light_matrix";
            LightsUniforms.spec.WorldToShadow     = "world_to_shadow_matrix";
            LightsUniforms.spec.Direction         = "direction";
            LightsUniforms.spec.Color             = "color";
            LightsUniforms.Initialize("lights", Configuration.maxLightCount);

            System.Text.StringBuilder uniforms = new System.Text.StringBuilder();
            uniforms.Append(CameraUniformBlock.UniformBlockRL.SourceRL);
            uniforms.Append(LightsUniforms.UniformBlockRL.SourceGL);

            modelsUB = new UniformBlockRL("models");
            modelsUB.AddMat4("model_to_world_matrix", Configuration.instanceCount);
            if (RenderStack.Graphics.Configuration.useIntegerPolygonIDs)
            {
                modelsUB.AddUInt("id_offset_uint", Configuration.instanceCount);
                ShaderGL3.Replace("#if USE_INTEGER_POLYGON_ID", "#if 1");
            }
            else
            {
                modelsUB.AddVec4("id_offset_vec3", Configuration.instanceCount);
                ShaderGL3.Replace("#if USE_INTEGER_POLYGON_ID", "#if 0");
            }
            modelsUB.Seal();
            uniforms.Append(modelsUB.ToString());

            globalUB = new UniformBlockRL("global");
            globalUB.AddVec4("add_color");
            globalUB.AddFloat("alpha");
            globalUB.Seal();
            uniforms.Append(globalUB.ToString());

            uniforms.Append(LightsUniforms.UniformBlockRL.SourceRL);
            LightsUniforms.UniformBufferRL.Use();

            ShaderGL3.Replace("MAX_LIGHT_COUNT", Configuration.maxLightCount.ToString());

            models = UniformBufferFactory.Create(modelsUB);
            global = UniformBufferFactory.Create(globalUB);

            global.Floats("alpha").Set(1.0f);
            global.Floats("add_color").Set(0.0f, 0.0f, 0.4f);
            global.Sync();

            LightsUniforms.Exposure.Set(1.0f);
            LightsUniforms.Bias.Set(-0.002f, 0.0f, 0.002f);
            LightsUniforms.AmbientLightColor.Set(0.2f, 0.2f, 0.2f);
            LightsUniforms.UniformBufferRL.Sync();

            models.Use();
            global.Use();

            var nearestClampToEdge = new SamplerRL();

            nearestClampToEdge.MinFilter   = OpenTK.Graphics.OpenGL.TextureMinFilter.Nearest;
            nearestClampToEdge.MagFilter   = OpenTK.Graphics.OpenGL.TextureMagFilter.Nearest;
            nearestClampToEdge.Wrap        = OpenTK.Graphics.OpenGL.TextureWrapMode.ClampToEdge;
            nearestClampToEdge.CompareMode = OpenTK.Graphics.OpenGL.TextureCompareMode.None;

            var bilinearClampToEdge = new SamplerRL();

            bilinearClampToEdge.MinFilter   = OpenTK.Graphics.OpenGL.TextureMinFilter.NearestMipmapLinear;
            bilinearClampToEdge.MagFilter   = OpenTK.Graphics.OpenGL.TextureMagFilter.Linear;
            bilinearClampToEdge.Wrap        = OpenTK.Graphics.OpenGL.TextureWrapMode.ClampToEdge;
            bilinearClampToEdge.CompareMode = OpenTK.Graphics.OpenGL.TextureCompareMode.None;

            Samplers.Global.AddSampler2D("t_font", nearestClampToEdge);
            Samplers.Global.AddSampler2D("t_ninepatch", bilinearClampToEdge).TextureUnitIndex = 1;
            Samplers.Global.AddSampler2D("t_cube", bilinearClampToEdge);
            Samplers.Global.AddSampler2D("t_left", nearestClampToEdge);
            Samplers.Global.AddSampler2D("t_right", nearestClampToEdge).TextureUnitIndex = 1;
            Samplers.Global.AddSampler2D("t_surface_color", bilinearClampToEdge);
            Samplers.Global.AddSampler2D("t_particle", bilinearClampToEdge);
            Samplers.Global.AddSampler2DArray("t_shadowmap_vis", bilinearClampToEdge);
            Samplers.Global.Seal();

            materialUB = new UniformBlockRL("material");
            materialUB.AddVec4("surface_diffuse_reflectance_color");
            materialUB.AddVec4("surface_specular_reflectance_color");
            materialUB.AddFloat("surface_specular_reflectance_exponent");
            materialUB.AddVec2("surface_rim_parameters");
            materialUB.AddVec4("grid_size");
            materialUB.AddFloat("surface_roughness");
            materialUB.AddFloat("surface_isotropy");
            materialUB.AddFloat("contrast").Default   = new Floats(1.0f);
            materialUB.AddFloat("deghost").Default    = new Floats(0.0f);
            materialUB.AddFloat("saturation").Default = new Floats(1.0f);
            materialUB.AddFloat("t");
            materialUB.AddFloat("slider_t");
            materialUB.AddVec4("fill_color");
            materialUB.AddVec4("point_color");
            materialUB.AddFloat("point_z_offset");
            materialUB.AddVec4("line_color");
            materialUB.AddVec2("line_width").Default = new Floats(1.0f, 0.25f); // \todo check how to compute .y
            materialUB.AddFloat("bias_units");
            materialUB.AddFloat("bias_factor");
            materialUB.AddFloat("octaves").Default     = new Floats(4.0f);
            materialUB.AddFloat("offset").Default      = new Floats(0.0f);
            materialUB.AddFloat("frequency").Default   = new Floats(2.2f);
            materialUB.AddFloat("amplitude").Default   = new Floats(0.2f);
            materialUB.AddFloat("lacunarity").Default  = new Floats(3.3f);
            materialUB.AddFloat("persistence").Default = new Floats(0.25f);
            materialUB.Seal();
            uniforms.Append(MaterialUB.ToString());
            uniforms.Append(Samplers.Global.ToString());

            //  Have these last
            ShaderGL3.Replace("UNIFORMS;", uniforms.ToString());
            ShaderGL3.Replace("INSTANCE_COUNT", "50");
        }
Esempio n. 4
0
        private void InitializeShaderSystem()
        {
            //InitializeOpenRL();

            //GL.Enable(EnableCap.Normalize);
            var dualTexture = AttributeMappings.Pool["dualtexture"] = new AttributeMappings();

            dualTexture.Add(0, "_position", VertexUsage.Position, 0, 3);
            dualTexture.Add(1, "_texcoord", VertexUsage.TexCoord, 0, VertexUsage.TexCoord, 0, 2);
            dualTexture.Add(2, "_texcoord", VertexUsage.TexCoord, 0, VertexUsage.TexCoord, 1, 2);
            dualTexture.Add(3, "_color", VertexUsage.Color, 0, 4);

            var idToColor = AttributeMappings.Pool["idToColor"] = new AttributeMappings();

            idToColor.Add(0, "_position", VertexUsage.Position, 0, 3);
            idToColor.Add(1, "_id_vec3", VertexUsage.Id, 0, VertexUsage.Color, 0, 3);

            CameraUniformBlock.NearFar             = "near_far";
            CameraUniformBlock.FovXFovYAspect      = "fovx_fovy_aspect";
            CameraUniformBlock.ViewToClip          = "view_to_clip_matrix";
            CameraUniformBlock.ClipToView          = "view_to_clip_matrix";
            CameraUniformBlock.WorldToClip         = "world_to_clip_matrix";
            CameraUniformBlock.ClipToWorld         = "clip_to_world_matrix";
            CameraUniformBlock.WorldToView         = "world_to_view_matrix";
            CameraUniformBlock.ViewToWorld         = "view_to_world_matrix";
            CameraUniformBlock.Viewport            = "viewport";
            CameraUniformBlock.ViewPositionInWorld = "view_position_in_world";
            CameraUniformBlock.Initialize("camera");

            LightsUniforms.spec.Count             = "count";
            LightsUniforms.spec.Exposure          = "exposure";
            LightsUniforms.spec.Bias              = "bias";
            LightsUniforms.spec.AmbientLightColor = "ambient_light_color";
            LightsUniforms.spec.WorldToLight      = "world_to_light_matrix";
            LightsUniforms.spec.WorldToShadow     = "world_to_shadow_matrix";
            LightsUniforms.spec.Direction         = "direction";
            LightsUniforms.spec.Color             = "color";
            LightsUniforms.Initialize("lights", Configuration.maxLightCount);

            globalUB = new UniformBlockGL("global");
            globalUB.AddVec4("add_color");
            globalUB.AddFloat("alpha");
            globalUB.Seal();

            modelsUB = new UniformBlockGL("models");
            modelsUB.AddMat4("model_to_world_matrix", Configuration.instanceCount);
            modelsUB.AddVec4("id_offset_vec3", Configuration.instanceCount);
            ShaderGL3.Replace("#if USE_INTEGER_POLYGON_ID", "#if 0");
            modelsUB.Seal();

            models = UniformBufferFactory.Create(modelsUB);
            global = UniformBufferFactory.Create(globalUB);
            global.Floats("alpha").Set(1.0f);
            global.Floats("add_color").Set(0.0f, 0.0f, 0.4f);
            global.SyncDelegate = ForceProgramUse;

            models.SyncDelegate = ModelsDirty;
            models.Use();
            global.Use();

            LightsUniforms.Exposure.Set(1.0f);
            LightsUniforms.Bias.Set(-0.002f, 0.0f, 0.002f);
            LightsUniforms.AmbientLightColor.Set(0.2f, 0.2f, 0.2f);
            LightsUniforms.UniformBufferGL.Sync();
            LightsUniforms.UniformBufferGL.Use();

            var nearestClampToEdge = new SamplerGL1();

            nearestClampToEdge.MinFilter   = TextureMinFilter.Nearest;
            nearestClampToEdge.MagFilter   = TextureMagFilter.Nearest;
            nearestClampToEdge.Wrap        = TextureWrapMode.ClampToEdge;
            nearestClampToEdge.CompareMode = TextureCompareMode.None;

            var bilinearClampToEdge = new SamplerGL1();

            bilinearClampToEdge.MinFilter   = TextureMinFilter.NearestMipmapLinear;
            bilinearClampToEdge.MagFilter   = TextureMagFilter.Linear;
            bilinearClampToEdge.Wrap        = TextureWrapMode.ClampToEdge;
            bilinearClampToEdge.CompareMode = TextureCompareMode.None;

            Samplers.Global.AddSampler2D("t_font", nearestClampToEdge);
            Samplers.Global.AddSampler2D("t_ninepatch", bilinearClampToEdge).TextureUnitIndex = 1;
            Samplers.Global.AddSampler2D("t_cube", bilinearClampToEdge);
            Samplers.Global.AddSampler2D("t_left", nearestClampToEdge);
            Samplers.Global.AddSampler2D("t_right", nearestClampToEdge).TextureUnitIndex = 1;
            Samplers.Global.AddSampler2D("t_surface_color", bilinearClampToEdge);
            Samplers.Global.AddSampler2D("t_particle", bilinearClampToEdge);
            Samplers.Global.AddSampler2DArray("t_shadowmap_vis", bilinearClampToEdge);
            Samplers.Global.Seal();

            materialUB = new UniformBlockGL("material");
            materialUB.AddVec4("surface_diffuse_reflectance_color");
            materialUB.AddVec4("surface_specular_reflectance_color");
            materialUB.AddFloat("surface_specular_reflectance_exponent");
            materialUB.AddVec2("surface_rim_parameters");
            materialUB.AddVec4("grid_size");
            materialUB.AddFloat("surface_roughness");
            materialUB.AddFloat("surface_isotropy");
            materialUB.AddFloat("contrast").Default   = new Floats(1.0f);
            materialUB.AddFloat("deghost").Default    = new Floats(0.0f);
            materialUB.AddFloat("saturation").Default = new Floats(1.0f);
            materialUB.AddFloat("t");
            materialUB.AddFloat("slider_t");
            materialUB.AddVec4("fill_color");
            materialUB.AddVec4("point_color");
            materialUB.AddFloat("point_z_offset");
            materialUB.AddVec4("line_color");
            materialUB.AddVec2("line_width").Default = new Floats(1.0f, 0.25f); // \todo check how to compute .y
            materialUB.AddFloat("bias_units");
            materialUB.AddFloat("bias_factor");
            materialUB.AddFloat("octaves").Default     = new Floats(4.0f);
            materialUB.AddFloat("offset").Default      = new Floats(0.0f);
            materialUB.AddFloat("frequency").Default   = new Floats(2.2f);
            materialUB.AddFloat("amplitude").Default   = new Floats(0.2f);
            materialUB.AddFloat("lacunarity").Default  = new Floats(3.3f);
            materialUB.AddFloat("persistence").Default = new Floats(0.25f);
            materialUB.Seal();
            materialUB.ChangeDelegate = ForceProgramUse;
        }
Esempio n. 5
0
        // \brief Declare uniforms and set Shader replacement strings
        private void InitializeShaderSystem()
        {
#if false
            if (RenderStack.Graphics.Configuration.useOpenRL)
            {
                InitializeOpenRL();
            }
#endif

            string fs_shadows = System.IO.File.ReadAllText("res/OpenGL3/_fs_shadows.txt");
            string instancing = System.IO.File.ReadAllText("res/OpenGL3/_instancing.txt");
            string utils      = System.IO.File.ReadAllText("res/OpenGL3/_utils.txt");

            ShaderGL3.Replace("FS_SHADOWS;", fs_shadows);
            ShaderGL3.Replace("INSTANCING;", instancing);
            ShaderGL3.Replace("UTILS;", utils);

            CameraUniformBlock.NearFar             = "near_far";
            CameraUniformBlock.FovXFovYAspect      = "fovx_fovy_aspect";
            CameraUniformBlock.ViewToClip          = "view_to_clip_matrix";
            CameraUniformBlock.ClipToView          = "view_to_clip_matrix";
            CameraUniformBlock.WorldToClip         = "world_to_clip_matrix";
            CameraUniformBlock.ClipToWorld         = "clip_to_world_matrix";
            CameraUniformBlock.WorldToView         = "world_to_view_matrix";
            CameraUniformBlock.ViewToWorld         = "view_to_world_matrix";
            CameraUniformBlock.Viewport            = "viewport";
            CameraUniformBlock.ViewPositionInWorld = "view_position_in_world";
            CameraUniformBlock.Initialize("camera");

            LightsUniforms.spec.Count             = "count";
            LightsUniforms.spec.Exposure          = "exposure";
            LightsUniforms.spec.Bias              = "bias";
            LightsUniforms.spec.AmbientLightColor = "ambient_light_color";
            LightsUniforms.spec.WorldToLight      = "world_to_light_matrix";
            LightsUniforms.spec.WorldToShadow     = "world_to_shadow_matrix";
            LightsUniforms.spec.Direction         = "direction";
            LightsUniforms.spec.Color             = "color";
            LightsUniforms.Initialize("lights", Configuration.maxLightCount);

            System.Text.StringBuilder uniforms = new System.Text.StringBuilder();

            uniforms.Append(CameraUniformBlock.UniformBlockGL.SourceGL);
            uniforms.Append(LightsUniforms.UniformBlockGL.SourceGL);

            modelsUB = new UniformBlockGL("models");
            modelsUB.AddMat4("model_to_world_matrix", Configuration.instanceCount);
            if (RenderStack.Graphics.Configuration.useIntegerPolygonIDs)
            {
                modelsUB.AddUInt("id_offset_uint", Configuration.instanceCount);
                ShaderGL3.Replace("#if USE_INTEGER_POLYGON_ID", "#if 1");
            }
            else
            {
                modelsUB.AddVec4("id_offset_vec3", Configuration.instanceCount);
                ShaderGL3.Replace("#if USE_INTEGER_POLYGON_ID", "#if 0");
            }
            modelsUB.Seal();
            uniforms.Append(modelsUB.SourceGL);

            globalUB = new UniformBlockGL("global");
            globalUB.AddVec4("add_color");
            globalUB.AddFloat("alpha");
            globalUB.Seal();
            uniforms.Append(globalUB.SourceGL);

            LightsUniforms.UniformBufferGL.Use();

            ShaderGL3.Replace("MAX_LIGHT_COUNT", Configuration.maxLightCount.ToString());

            models = UniformBufferFactory.Create(modelsUB);
            global = UniformBufferFactory.Create(globalUB);

            global.Floats("alpha").Set(1.0f);
            global.Floats("add_color").Set(0.0f, 0.0f, 0.4f);
            global.Sync();

            LightsUniforms.Exposure.Set(1.0f);
            LightsUniforms.Bias.Set(-0.002f, 0.0f, 0.002f);
            LightsUniforms.AmbientLightColor.Set(0.2f, 0.2f, 0.2f);
            LightsUniforms.UniformBufferGL.Sync();

            models.Use();
            global.Use();

            //  GL_NEAREST                  - no filtering, no mipmaps
            //  GL_LINEAR                   - filtering, no mipmaps
            //  GL_NEAREST_MIPMAP_NEAREST   - no filtering, sharp switching between mipmaps
            //  GL_NEAREST_MIPMAP_LINEAR    - no filtering, smooth transition between mipmaps
            //  GL_LINEAR_MIPMAP_NEAREST    - filtering, sharp switching between mipmaps
            //  GL_LINEAR_MIPMAP_LINEAR     - filtering, smooth transition between mipmaps
            //
            //  GL_LINEAR                   - bilinear
            //  GL_LINEAR_MIPMAP_NEAREST    - bilinear with mipmaps
            //  GL_LINEAR_MIPMAP_LINEAR     - trilinear

            //  No filtering, no mipmaps
            var nearest = SamplerFactory.Create();
            nearest.MinFilter   = TextureMinFilter.Nearest;
            nearest.MagFilter   = TextureMagFilter.Nearest;
            nearest.Wrap        = TextureWrapMode.ClampToEdge;
            nearest.CompareMode = TextureCompareMode.None;

            var linear = SamplerFactory.Create();
            linear.MinFilter   = TextureMinFilter.Linear;
            linear.MagFilter   = TextureMagFilter.Linear;
            linear.Wrap        = TextureWrapMode.ClampToEdge;
            linear.CompareMode = TextureCompareMode.None;

            var bilinear = SamplerFactory.Create();
            bilinear.MinFilter   = TextureMinFilter.LinearMipmapNearest;
            bilinear.MagFilter   = TextureMagFilter.Linear;
            bilinear.Wrap        = TextureWrapMode.ClampToEdge;
            bilinear.CompareMode = TextureCompareMode.None;

            var trilinear = SamplerFactory.Create();
            trilinear.MinFilter   = TextureMinFilter.LinearMipmapLinear;
            trilinear.MagFilter   = TextureMagFilter.Linear;
            trilinear.Wrap        = TextureWrapMode.ClampToEdge;
            trilinear.CompareMode = TextureCompareMode.None;

            var pcfShadow = SamplerFactory.Create();
            pcfShadow.MinFilter   = TextureMinFilter.Linear;
            pcfShadow.MagFilter   = TextureMagFilter.Linear;
            pcfShadow.Wrap        = TextureWrapMode.ClampToEdge;
            pcfShadow.CompareMode = TextureCompareMode.CompareRefToTexture;

            if (Configuration.hardwareShadowPCF)
            {
                ShaderGL3.Replace("#if HARDWARE_PCF", "#if 1");
                Samplers.Global.AddSampler2DArrayShadow("t_shadowmap", pcfShadow).TextureUnitIndex = 2;
            }
            else
            {
                ShaderGL3.Replace("#if HARDWARE_PCF", "#if 0");
                Samplers.Global.AddSampler2DArray("t_shadowmap", linear).TextureUnitIndex = 2;
            }
            Samplers.Global.AddSampler2D("t_font", nearest).TextureUnitIndex     = 1;
            Samplers.Global.AddSampler2D("t_ninepatch", linear).TextureUnitIndex = 2;
            Samplers.Global.AddSampler2D("t_cube", trilinear);
            Samplers.Global.AddSampler2D("t_left", nearest);
            Samplers.Global.AddSampler2D("t_right", nearest).TextureUnitIndex = 1;
            Samplers.Global.AddSampler2D("t_surface_color", trilinear);
            Samplers.Global.AddSampler2D("t_particle", trilinear);
            Samplers.Global.AddSampler2DArray("t_shadowmap_vis", linear);
            Samplers.Global.Seal();

            materialUB = new UniformBlockGL("material");
            materialUB.AddVec4("surface_diffuse_reflectance_color");
            materialUB.AddVec4("surface_specular_reflectance_color");
            materialUB.AddFloat("surface_specular_reflectance_exponent");
            materialUB.AddVec2("surface_rim_parameters");
            materialUB.AddVec4("grid_size");
            materialUB.AddVec4("main_camera_near_far");
            materialUB.AddFloat("surface_roughness");
            materialUB.AddFloat("surface_isotropy");
            materialUB.AddFloat("contrast").Default   = new Floats(1.0f);
            materialUB.AddFloat("deghost").Default    = new Floats(0.0f);
            materialUB.AddFloat("saturation").Default = new Floats(1.0f);
            materialUB.AddFloat("t");
            materialUB.AddFloat("slider_t");
            materialUB.AddVec4("fill_color");
            materialUB.AddVec4("point_color");
            materialUB.AddFloat("point_z_offset");
            materialUB.AddVec4("line_color");
            materialUB.AddVec2("line_width").Default = new Floats(1.0f, 0.25f); // \todo check how to compute .y
            materialUB.AddFloat("bias_units");
            materialUB.AddFloat("bias_factor");
            materialUB.AddFloat("octaves").Default     = new Floats(4.0f);
            materialUB.AddFloat("offset").Default      = new Floats(0.0f);
            materialUB.AddFloat("frequency").Default   = new Floats(2.2f);
            materialUB.AddFloat("amplitude").Default   = new Floats(0.2f);
            materialUB.AddFloat("lacunarity").Default  = new Floats(3.3f);
            materialUB.AddFloat("persistence").Default = new Floats(0.25f);
            materialUB.Seal();
            uniforms.Append(MaterialUB.SourceGL);
            uniforms.Append(Samplers.Global.ToString());

            //  Have these last
            ShaderGL3.Replace("UNIFORMS;", uniforms.ToString());
            ShaderGL3.Replace("INSTANCE_COUNT", "50");
        }