/// Compiles an OpenGL glsl shader from source code and returns the shader id.
///
/// This is mostly used as a helper for 'program_from_*' which can be used
/// to build an OpenGL shader program.
/// The source should not contain any null bytes or it will result in an
/// error while converting the source into a CString.
Compiles an OpenGL glsl shader from source code and returns the shader id.
This is mostly used as a helper for 'program_from_*' which can be used
to build an OpenGL shader program.
The source should not contain any null bytes or it will result in an
error while converting the source into a CString.
Compiles an OpenGL glsl shader from source code and returns the shader id . This is mostly used as a helper for ' program_from_ * ' which can be used to build an OpenGL shader program . The source should not contain any null bytes or it will result in an error while converting the source into a CString .
pub fn compile_source(source: String, shader_type: ShaderType)
        -> Result<GLuint, EngineError> {
    //println!("================={:?}================\n{}", shader_type, source);
    // Virtually all this code is unsafe as it deals with CStrings and
    // OpenGL calls
    let shader: GLuint;
    unsafe {
        // Convert source to CString
        let csource = match CString::new(source) {
            Ok(cstring) => cstring,
            Err(err) => {
                return Err(EngineError::ShaderCompile("Failed to build CString".into()));
            }
        };
        shader = gl::CreateShader(shader_type.clone() as u32);
        gl::ShaderSource(shader, 1, &csource.as_ptr(), std::ptr::null());
        gl::CompileShader(shader);
        // Check for compilation success
        let mut compile_success: GLint = 1;
        gl::GetShaderiv(shader, gl::COMPILE_STATUS, &mut compile_success);
        // On fail, retrieve the error
        if compile_success == 0 {
            let mut log_len: GLint = 0;
            gl::GetShaderiv(shader, gl::INFO_LOG_LENGTH, &mut log_len);
            let mut log_buffer: Vec<u8> = Vec::with_capacity(log_len as usize + 1);
            // Fill the buffer with spaces
            log_buffer.extend([b' '].iter().cycle().take(log_len as usize));
            let error_log: CString = CString::from_vec_unchecked(log_buffer);
            // Load the log into CString
            gl::GetShaderInfoLog(shader, 
                                log_len, 
                                std::ptr::null_mut(),
                                error_log.as_ptr() as *mut GLchar);
            // Convert CString to String
            let error = error_log.to_string_lossy().into_owned();
            // Return as compile error
            println!("========= {:?} Compile Error =========\n{}\n==========================", 
                     &shader_type, error);
            return Err(EngineError::ShaderCompile(error));
        }
    }
    // Return the shader id that OpenGL provided
    Ok(shader)
}
pub fn compile_source ( source : String , shader_type : ShaderType ) -> Result < GLuint , EngineError > { //println!("================={:?}================\n{}", shader_type, source); // Virtually all this code is unsafe as it deals with CStrings and // OpenGL calls let shader : GLuint ; unsafe { // Convert source to CString let csource = match CString :: new ( source ) { Ok ( cstring ) => cstring , Err ( err ) => { return Err ( EngineError :: ShaderCompile ( " " . into ( ) ) ) ; } } ; shader = gl :: CreateShader ( shader_type . clone ( ) as u32 ) ; gl :: ShaderSource ( shader , 1 , & csource . as_ptr ( ) , std :: ptr :: null ( ) ) ; gl :: CompileShader ( shader ) ; // Check for compilation success let mut compile_success : GLint = 1 ; gl :: GetShaderiv ( shader , gl :: COMPILE_STATUS , & mut compile_success ) ; // On fail, retrieve the error if compile_success == 0 { let mut log_len : GLint = 0 ; gl :: GetShaderiv ( shader , gl :: INFO_LOG_LENGTH , & mut log_len ) ; let mut log_buffer : Vec < u8 > = Vec :: with_capacity ( log_len as usize + 1 ) ; // Fill the buffer with spaces log_buffer . extend ( [ b' ' ] . iter ( ) . cycle ( ) . take ( log_len as usize ) ) ; let error_log : CString = CString :: from_vec_unchecked ( log_buffer ) ; // Load the log into CString gl :: GetShaderInfoLog ( shader , log_len , std :: ptr :: null_mut ( ) , error_log . as_ptr ( ) as * mut GLchar ) ; // Convert CString to String let error = error_log . to_string_lossy ( ) . into_owned ( ) ; // Return as compile error println ! ( " \n \n " shader_type error ) ; return Err ( EngineError :: ShaderCompile ( error ) ) ; } } // Return the shader id that OpenGL provided Ok ( shader ) }
Compiles an OpenGL glsl shader from source code and returns the shader id.
Compiles an OpenGL glsl shader from source code and returns the shader id .
source
String
shader_type
ShaderType