avtGLSLProgram.C 9.85 KB
Newer Older
1 2
/*****************************************************************************
*
brugger's avatar
 
brugger committed
3
* Copyright (c) 2000 - 2011, Lawrence Livermore National Security, LLC
4
* Produced at the Lawrence Livermore National Laboratory
brugger's avatar
 
brugger committed
5
* LLNL-CODE-442911
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
* All rights reserved.
*
* This file is  part of VisIt. For  details, see https://visit.llnl.gov/.  The
* full copyright notice is contained in the file COPYRIGHT located at the root
* of the VisIt distribution or at http://www.llnl.gov/visit/copyright.html.
*
* Redistribution  and  use  in  source  and  binary  forms,  with  or  without
* modification, are permitted provided that the following conditions are met:
*
*  - Redistributions of  source code must  retain the above  copyright notice,
*    this list of conditions and the disclaimer below.
*  - Redistributions in binary form must reproduce the above copyright notice,
*    this  list of  conditions  and  the  disclaimer (as noted below)  in  the
*    documentation and/or other materials provided with the distribution.
*  - Neither the name of  the LLNS/LLNL nor the names of  its contributors may
*    be used to endorse or promote products derived from this software without
*    specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT  HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR  IMPLIED WARRANTIES, INCLUDING,  BUT NOT  LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND  FITNESS FOR A PARTICULAR  PURPOSE
* ARE  DISCLAIMED. IN  NO EVENT  SHALL LAWRENCE  LIVERMORE NATIONAL  SECURITY,
* LLC, THE  U.S.  DEPARTMENT OF  ENERGY  OR  CONTRIBUTORS BE  LIABLE  FOR  ANY
* DIRECT,  INDIRECT,   INCIDENTAL,   SPECIAL,   EXEMPLARY,  OR   CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT  LIMITED TO, PROCUREMENT OF  SUBSTITUTE GOODS OR
* SERVICES; LOSS OF  USE, DATA, OR PROFITS; OR  BUSINESS INTERRUPTION) HOWEVER
* CAUSED  AND  ON  ANY  THEORY  OF  LIABILITY,  WHETHER  IN  CONTRACT,  STRICT
* LIABILITY, OR TORT  (INCLUDING NEGLIGENCE OR OTHERWISE)  ARISING IN ANY  WAY
* OUT OF THE  USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
* DAMAGE.
*
*****************************************************************************/

// ************************************************************************* //
//                              avtGLSLProgram.C                             //
// ************************************************************************* //

#include <avtGLSLProgram.h>
#include <DebugStream.h>

#include <cstring>

// ****************************************************************************
//  Method: avtGLSLProgram constructor
//
//  Programmer: Christoph Garth
//  Creation:   Mon Jan 11 16:25:07 PST 2010 
//
// ****************************************************************************

avtGLSLProgram::avtGLSLProgram( const std::string& _name ) : 
    name(_name), program(0), previous(-1), supported(-1)
{
}

// ****************************************************************************
//  Method: avtGLSLProgram destructor
//
//  Programmer: Christoph Garth
//  Creation:   Mon Jan 11 16:25:07 PST 2010 
//
// ****************************************************************************

avtGLSLProgram::~avtGLSLProgram()
{
    Destroy();
}

// ****************************************************************************
//  Method: avtGLSLProgram::AttachShaderFromString
//
//  Purpose: 
//      Records source code for a new shader, to be compiled and attached on
//      the next call to Enable() or Create().
//     
//  Programmer: Christoph Garth
//  Creation:   Mon Jan 11 16:25:07 PST 2010 
//
// ****************************************************************************

void avtGLSLProgram::AttachShaderFromString( GLenum type, const std::string& source )
{
    shaders.push_back( std::make_pair(type,source) );
}
    
// ****************************************************************************
//  Method: avtGLSLProgram::Enable
//
//  Purpose: 
//      Tell OpenGL to begin using the program. Create() is called to make 
//      sure all outstanding shader sources have been attached. The previously
//      bound shader is stored and reset on the following call of Disable(). 
//      If the program is not supported by the current context or an error
//      occurred during compilation, the fixed function pipeline is activated;
//      this is indicated by a false return value. 
//
//  Returns:
//      True if the shader was activated without error, false otherwise.
//     
//  Programmer: Christoph Garth
//  Creation:   Mon Jan 11 16:26:32 PST 2010 
//
// ****************************************************************************

bool avtGLSLProgram::Enable()
{
    if( !IsSupported() )        
        return false;

    glGetIntegerv( GL_CURRENT_PROGRAM, &previous );

    bool ok = Create();

    if( ok )
        glUseProgram( ok ? program : 0 );
        
    return ok;
}
    
// ****************************************************************************
//  Method: avtGLSLProgram::Disable
//
//  Purpose: 
//      Tell OpenGL to stop using the program. If a program was active at
//      the time of the last Enable(), that program is restored.
//
//  Programmer: Christoph Garth
//  Creation:   Mon Jan 11 16:26:32 PST 2010 
//
// ****************************************************************************
    
void avtGLSLProgram::Disable()
{
    if( !IsSupported() )
        return;

    if( previous >= 0 )
        glUseProgram( previous );
        
    previous = -1;
}                              
  
// ****************************************************************************
//  Method: avtGLSLProgram::Create
//
//  Purpose: 
//      Create the current program, i.e. compile and attach all 
//      previously attached shader sources and link the program.
// 
//  Returns:
//      True upon successful creation, false otherwise.
//
//  Programmer: Christoph Garth
//  Creation:   Mon Jan 11 16:28:51 PST 2010 
//
161 162 163 164 165 166
//  Modifications:
//
//    Tom Fogal, Wed Jan 20 11:48:50 MST 2010
//    Formatting, bail if program creation fails.
//    Delete shader logs.
//
167 168 169 170 171 172 173 174 175 176 177 178
// ****************************************************************************

bool avtGLSLProgram::Create()
{
    if( !IsSupported() )
        return false;

    // Create the program if it's not already there
    if( program == 0 )            
        program = glCreateProgram();

    if( program == 0 )
179 180 181 182 183
    {
        debug1 << "avtGLSLProgram \"" << name << "\": "
                  "GLSL program could not be created\n";
        return false;
    }
184 185 186 187 188

    bool shadersOK = true;

    // Go through the list of shaders, and compile and attach each of them

189 190
    for( std::vector<Shader>::const_iterator si=shaders.begin();
         si!=shaders.end(); ++si )
191 192 193 194
    {
        GLuint shader = glCreateShader( si->first );
        
        if( !shader )
195 196 197 198
        {
            debug1 << "avtGLSLProgram \"" << name << "\": "
                      "GLSL shader could not be created\n";
        }
199 200 201 202 203 204 205 206 207 208 209 210 211 212
        
        const char* source = si->second.c_str();
        
        glShaderSource( shader, 1, &source, NULL );
        glCompileShader( shader );

        GLint length = 0;
        glGetShaderiv( shader, GL_INFO_LOG_LENGTH, &length );
        
        if( length )
        {
            GLchar* log = new GLchar[length];
            glGetShaderInfoLog( shader, length, NULL, log );
            
213 214 215
            debug1 << "avtGLSLProgram \"" << name << "\": "
                   << "GLSL shader compile log:\n" << log << '\n';
            delete[] log;
216 217 218 219 220 221 222
        }
        
        GLint compiled;
        glGetShaderiv( shader, GL_COMPILE_STATUS, &compiled );
        
        if( compiled != GL_TRUE )
        {
223 224
            debug1 << "avtGLSLProgram \"" << name << "\": "
                      "GLSL shader failed to compile\n";
225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246
            shadersOK = false;
        }
            
        glAttachShader( program, shader );
        glDeleteShader( shader );
    }
    
    shaders.clear();
    
    // Link the program
    glLinkProgram( program );
        
    GLint length = 0;
    glGetProgramiv( program, GL_INFO_LOG_LENGTH, &length );
        
    if( length )
    {
        GLchar* log = new GLchar[length];
        glGetProgramInfoLog( program, length, NULL, log );
            
        debug1 << "avtGLSLProgram \"" << name << "\": GLSL program link log:\n"
                << log << '\n';
247
        delete[] log;
248 249 250 251 252 253
    }
        
    GLint linked;
    glGetProgramiv( program, GL_LINK_STATUS, &linked );

    if( linked != GL_TRUE )
254 255
        debug1 << "avtGLSLProgram \""<< name << "\": "
                  "GLSL program failed to link\n";
256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316

    if( shadersOK && linked )
        return true;
        
    glDeleteProgram( program );
    program = 0;
    
    return false;
}
   
  
// ****************************************************************************
//  Method: avtGLSLProgram::Destroy
//
//  Purpose: 
//      Destroy the program and free all resources.
//
//  Programmer: Christoph Garth
//  Creation:   Mon Jan 11 16:29:02 PST 2010 
//
// ****************************************************************************
    
void avtGLSLProgram::Destroy()
{
    if( !IsSupported() )
        return;
        
    if( program )
    {
        glDeleteProgram( program );
        program = 0;
    }
}

// ****************************************************************************
//  Method: avtGLSLProgram::IsSupported
//
//  Purpose: 
//      Determine whether GLSL programs are supported by the OpenGL context.
//
//  Returns:
//      True if all required extensions are present, false otherwise.
//
//  Programmer: Christoph Garth
//  Creation:   Mon Jan 11 16:32:22 PST 2010 
//
// ****************************************************************************
   
bool avtGLSLProgram::IsSupported()
{
    if( supported >= 0 )
        return supported;

    if( !avt::glew::initialize() )
        return supported = 0;
        
    return supported = 
        glewIsSupported( "GL_VERSION_2_0" ) &&
        glewIsSupported( "GL_ARB_shading_language_100" );
}