예제 #1
0
        //This method attaches UBOs to shader binding points
        public static void attachUBOToShaderBindingPoint(GLSLShaderConfig shader_conf, string var_name, int binding_point)
        {
            int shdr_program_id = shader_conf.program_id;
            int ubo_index       = GL.GetUniformBlockIndex(shdr_program_id, var_name);

            GL.UniformBlockBinding(shdr_program_id, ubo_index, binding_point);
        }
예제 #2
0
        static private void loadActiveUniforms(GLSLShaderConfig shader_conf)
        {
            int active_uniforms_count;

            GL.GetProgram(shader_conf.program_id, GetProgramParameterName.ActiveUniforms, out active_uniforms_count);

            shader_conf.uniformLocations.Clear(); //Reset locataions
            shader_conf.log += "Active Uniforms: " + active_uniforms_count.ToString() + "\n";
            for (int i = 0; i < active_uniforms_count; i++)
            {
                int               size;
                int               bufSize = 64;
                int               length;
                string            name;
                ActiveUniformType type;
                int               loc;

                GL.GetActiveUniform(shader_conf.program_id, i, bufSize, out size, out length, out type, out name);
                loc = GL.GetUniformLocation(shader_conf.program_id, name);
                shader_conf.uniformLocations[name] = loc; //Store location

                if (RenderState.enableShaderCompilationLog)
                {
                    string info_string = String.Format("Uniform # {0} Location: {1} Type: {2} Name: {3} Length: {4} Size: {5}",
                                                       i, loc, type.ToString(), name, length, size);
                    shader_conf.log += info_string + "\n";
                }
            }
        }
예제 #3
0
 public static void compileShader(GLSLShaderConfig config)
 {
     if (config.program_id != -1)
     {
         GL.DeleteProgram(config.program_id);
     }
     CreateShaders(config);
 }
예제 #4
0
        public static void attachSSBOToShaderBindingPoint(GLSLShaderConfig shader_conf, string var_name, int binding_point)
        {
            //Binding Position 0 - Matrices UBO
            int shdr_program_id = shader_conf.program_id;
            int ssbo_index      = GL.GetProgramResourceIndex(shdr_program_id, ProgramInterface.ShaderStorageBlock, var_name);

            GL.ShaderStorageBlockBinding(shader_conf.program_id, ssbo_index, binding_point);
        }
예제 #5
0
        //GLPreparation
        public static GLSLShaderConfig compileShader(GLSLShaderText vs, GLSLShaderText fs, GLSLShaderText gs, GLSLShaderText tes, GLSLShaderText tcs, SHADER_TYPE type, ref string log)
        {
            GLSLShaderConfig shader_conf = new GLSLShaderConfig(vs, fs, gs, tcs, tes, type);

            //Set modify Shader delegate
            shader_conf.modifyShader = issuemodifyShaderRequest;

            compileShader(shader_conf);
            log += shader_conf.log; //Append log

            return(shader_conf);
        }
예제 #6
0
        //Shader Compilation

        public static void issuemodifyShaderRequest(GLSLShaderConfig config, GLSLShaderText shaderText)
        {
            Console.WriteLine("Sending Shader Modification Request");
            ThreadRequest req = new ThreadRequest();

            req.type = THREAD_REQUEST_TYPE.GL_MODIFY_SHADER_REQUEST;
            req.arguments.Add(config);
            req.arguments.Add(shaderText);

            //Send request
            CallBacks.issueRequestToGLControl(ref req);
        }
예제 #7
0
        static public void modifyShader(GLSLShaderConfig shader_conf, GLSLShaderText shaderText)
        {
            Console.WriteLine("Actually Modifying Shader");

            int[] attached_shaders = new int[20];
            int   count;
            int   status_code;

            GL.GetAttachedShaders(shader_conf.program_id, 20, out count, attached_shaders);

            for (int i = 0; i < count; i++)
            {
                int[] shader_params = new int[10];
                GL.GetShader(attached_shaders[i], OpenTK.Graphics.OpenGL4.ShaderParameter.ShaderType, shader_params);

                if (shader_params[0] == (int)shaderText.shader_type)
                {
                    Console.WriteLine("Found modified shader");

                    //Trying to compile shader
                    shaderText.compile();

                    //Attach new shader back to program
                    GL.DetachShader(shader_conf.program_id, attached_shaders[i]);
                    GL.AttachShader(shader_conf.program_id, shaderText.shader_object_id);
                    GL.LinkProgram(shader_conf.program_id);

                    GL.GetProgram(shader_conf.program_id, GetProgramParameterName.LinkStatus, out status_code);
                    if (status_code != 1)
                    {
                        Console.WriteLine("Unable to link the new shader. Reverting to the old shader");
                        return;
                    }

                    //Delete old shader and reload uniforms
                    loadActiveUniforms(shader_conf); //Re-load active uniforms
                    Console.WriteLine("Shader was modified successfully");
                    break;
                }
            }
            Console.WriteLine("Shader was not found...");
        }
예제 #8
0
파일: Mesh.cs 프로젝트: LongJohnCoder/NMSMV
        public void renderBSphere(GLSLHelper.GLSLShaderConfig shader)
        {
            for (int i = 0; i < instance_count; i++)
            {
                GLVao bsh_Vao = setupBSphere(i);

                //Rendering

                GL.UseProgram(shader.program_id);

                //Step 2 Bind & Render Vao
                //Render Bounding Sphere
                GL.BindVertexArray(bsh_Vao.vao_id);
                GL.PolygonMode(MaterialFace.FrontAndBack, PolygonMode.Line);
                GL.DrawElements(PrimitiveType.Triangles, 600, DrawElementsType.UnsignedInt, (IntPtr)0);

                GL.BindVertexArray(0);
                bsh_Vao.Dispose();
            }
        }
예제 #9
0
        public static GLSLShaderConfig compileShader(string vs_path, string fs_path, string gs_path, string tcs_path, string tes_path,
                                                     List <string> directives, List <string> includes, SHADER_TYPE type, ref string log)
        {
            List <string> defines = new List <string>();

            //General Directives are provided here
            foreach (string d in directives)
            {
                defines.Add(d);
            }

            //Material Flags are provided here
            foreach (string f in includes)
            {
                defines.Add("_" + f);
            }

            //Main Object Shader - Deferred Shading
            GLSLShaderText main_deferred_shader_vs = new GLSLShaderText(ShaderType.VertexShader);
            GLSLShaderText main_deferred_shader_fs = new GLSLShaderText(ShaderType.FragmentShader);

            foreach (string s in defines)
            {
                main_deferred_shader_vs.addString("#define " + s + "\n");
            }
            main_deferred_shader_vs.addStringFromFile(vs_path);
            foreach (string s in defines)
            {
                main_deferred_shader_fs.addString("#define " + s + "\n");
            }
            main_deferred_shader_fs.addStringFromFile(fs_path);

            GLSLShaderConfig conf = compileShader(main_deferred_shader_vs, main_deferred_shader_fs, null, null, null,
                                                  type, ref log);

            conf.shaderHash = calculateShaderHash(includes);

            return(conf);
        }
예제 #10
0
        //Shader Creation
        static public void CreateShaders(GLSLShaderConfig config)
        {
            int    status_code;
            string info;
            bool   gsflag = false;
            bool   tsflag = false;

            if (!(config.gs_text == null))
            {
                gsflag = true;
            }
            if (!((config.tcs_text == null) & (config.tes_text == null)))
            {
                tsflag = true;
            }

            //Compile vertex shader

            if (config.vs_text != null)
            {
                config.vs_text.compile();
                if (RenderState.enableShaderCompilationLog)
                {
                    config.log += NumberLines(config.vs_text.resolved_text) + "\n";
                }
            }

            if (config.fs_text != null)
            {
                config.fs_text.compile();
                if (RenderState.enableShaderCompilationLog)
                {
                    config.log += NumberLines(config.fs_text.resolved_text) + "\n";
                }
            }

            if (config.gs_text != null)
            {
                config.gs_text.compile();
                if (RenderState.enableShaderCompilationLog)
                {
                    config.log += NumberLines(config.gs_text.resolved_text) + "\n";
                }
            }

            if (config.tes_text != null)
            {
                config.tes_text.compile();
                if (RenderState.enableShaderCompilationLog)
                {
                    config.log += NumberLines(config.tes_text.resolved_text) + "\n";
                }
            }

            if (config.tcs_text != null)
            {
                config.tcs_text.compile();
                if (RenderState.enableShaderCompilationLog)
                {
                    config.log += NumberLines(config.tcs_text.resolved_text) + "\n";
                }
            }

            //Create new program
            config.program_id = GL.CreateProgram();

            //Attach shaders to program
            GL.AttachShader(config.program_id, config.vs_text.shader_object_id);
            GL.AttachShader(config.program_id, config.fs_text.shader_object_id);

            if (gsflag)
            {
                GL.AttachShader(config.program_id, config.gs_text.shader_object_id);
            }

            if (tsflag)
            {
                GL.AttachShader(config.program_id, config.tcs_text.shader_object_id);
                GL.AttachShader(config.program_id, config.tes_text.shader_object_id);
            }

            GL.LinkProgram(config.program_id);

            //Check Linking
            if (RenderState.enableShaderCompilationLog)
            {
                GL.GetProgramInfoLog(config.program_id, out info);
                config.log += info + "\n";
            }

            GL.GetProgram(config.program_id, GetProgramParameterName.LinkStatus, out status_code);
            if (status_code != 1)
            {
                throwCompilationError(config.log);
            }

            loadActiveUniforms(config);
        }
예제 #11
0
        public static void reportUBOs(GLSLShaderConfig shader_conf)
        {
            //Print Debug Information for the UBO
            // Get named blocks info
            int count, info, length;
            int test_program = shader_conf.program_id;

            GL.GetProgram(test_program, GetProgramParameterName.ActiveUniformBlocks, out count);

            for (int i = 0; i < count; ++i)
            {
                // Get blocks name
                string block_name;
                int    block_size, block_bind_index;
                GL.GetActiveUniformBlockName(test_program, i, 256, out length, out block_name);
                GL.GetActiveUniformBlock(test_program, i, ActiveUniformBlockParameter.UniformBlockDataSize, out block_size);
                Console.WriteLine("Block {0} Data Size {1}", block_name, block_size);

                GL.GetActiveUniformBlock(test_program, i, ActiveUniformBlockParameter.UniformBlockBinding, out block_bind_index);
                Console.WriteLine("    Block Binding Point {0}", block_bind_index);

                GL.GetInteger(GetIndexedPName.UniformBufferBinding, block_bind_index, out info);
                Console.WriteLine("    Block Bound to Binding Point: {0} {{", info);

                int block_active_uniforms;
                GL.GetActiveUniformBlock(test_program, i, ActiveUniformBlockParameter.UniformBlockActiveUniforms, out block_active_uniforms);
                int[] uniform_indices = new int[block_active_uniforms];
                GL.GetActiveUniformBlock(test_program, i, ActiveUniformBlockParameter.UniformBlockActiveUniformIndices, uniform_indices);


                int[] uniform_types   = new int[block_active_uniforms];
                int[] uniform_offsets = new int[block_active_uniforms];
                int[] uniform_sizes   = new int[block_active_uniforms];

                //Fetch Parameters for all active Uniforms
                GL.GetActiveUniforms(test_program, block_active_uniforms, uniform_indices, ActiveUniformParameter.UniformType, uniform_types);
                GL.GetActiveUniforms(test_program, block_active_uniforms, uniform_indices, ActiveUniformParameter.UniformOffset, uniform_offsets);
                GL.GetActiveUniforms(test_program, block_active_uniforms, uniform_indices, ActiveUniformParameter.UniformSize, uniform_sizes);

                for (int k = 0; k < block_active_uniforms; ++k)
                {
                    int    actual_name_length;
                    string name;

                    GL.GetActiveUniformName(test_program, uniform_indices[k], 256, out actual_name_length, out name);
                    Console.WriteLine("\t{0}", name);

                    Console.WriteLine("\t\t    type: {0}", uniform_types[k]);
                    Console.WriteLine("\t\t    offset: {0}", uniform_offsets[k]);
                    Console.WriteLine("\t\t    size: {0}", uniform_sizes[k]);

                    /*
                     * GL.GetActiveUniforms(test_program, i, ref uniform_indices[k], ActiveUniformParameter.UniformArrayStride, out uniArrayStride);
                     * Console.WriteLine("\t\t    array stride: {0}", uniArrayStride);
                     *
                     * GL.GetActiveUniforms(test_program, i, ref uniform_indices[k], ActiveUniformParameter.UniformMatrixStride, out uniMatStride);
                     * Console.WriteLine("\t\t    matrix stride: {0}", uniMatStride);
                     */
                }
                Console.WriteLine("}}");
            }
        }