<commit_before>/*
   For more information, please see: http://software.sci.utah.edu

   The MIT License

   Copyright (c) 2008 Scientific Computing and Imaging Institute,
   University of Utah.

   
   Permission is hereby granted, free of charge, to any person obtaining a
   copy of this software and associated documentation files (the "Software"),
   to deal in the Software without restriction, including without limitation
   the rights to use, copy, modify, merge, publish, distribute, sublicense,
   and/or sell copies of the Software, and to permit persons to whom the
   Software is furnished to do so, subject to the following conditions:

   The above copyright notice and this permission notice shall be included
   in all copies or substantial portions of the Software.

   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
   OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
   FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
   THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
   LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
   FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
   DEALINGS IN THE SOFTWARE.
*/

/**
  \file    GLSLProgram.cpp
  \author  <NAME>, <NAME>
           SCI Institute, University of Utah
  \date    October 2008
*/

#ifdef _WIN32
  #pragma warning( disable : 4996 ) // disable deprecated warning 
#endif

#include "GLSLProgram.h"
#include <Controller/MasterController.h>

bool GLSLProgram::m_bGlewInitialized=true;    ///< GL Extension Wrangler (glew) is initialized on first instantiation
bool GLSLProgram::m_bGLChecked=false;         ///< GL extension check
bool GLSLProgram::m_bGLUseARB=false;          ///< use pre GL 2.0 syntax

/*
Hack: since the ATI/AMD driver has a bug and the 3rd parameter if glGetActiveUniform must be != 0 we use these two dummy vars in the glGetActiveUniform calls
*/
GLsizei AtiHackLen;
GLchar AtiHackChar;

/**
 * Default Constructor.
 * Initializes glew on first instantiation. 
 * \param void
 * \return void
 * \author <a href="mailto:<EMAIL>"><NAME></a>
 * \date Aug.2004
 * \see Initialize()
 */
GLSLProgram::GLSLProgram(MasterController* pMasterController) :
  m_pMasterController(pMasterController),
  m_bInitialized(false),
  m_bEnabled(false),
  m_hProgram(0)
{
    Initialize();
}

/**
 * Specialized Constructor.
 * Loads any combination of vertex and fragment shader from disk.
 * \param VSFile - name of the file containing the vertex shader
 * \param FSFile - name of the file containing the fragment shader
 * \param src - selects the source of vertex and fragment shader. Can be either GLSLPROGRAM_DISK or GLSLPROGRAM_STRING
 * \return void
 * \author <a href="mailto:<EMAIL>"><NAME></a>
 * \date Aug.2004
 * \see Initialize()
 * \see Load()
 * \see GLSLPROGRAM_SOURCE
 */
GLSLProgram::GLSLProgram(MasterController* pMasterController, const char *VSFile, const char *FSFile,GLSLPROGRAM_SOURCE src) :
  m_pMasterController(pMasterController),
  m_bInitialized(false),
  m_bEnabled(false),
  m_hProgram(0)
{
  if (Initialize())
    Load(VSFile,FSFile,src);
}



/**
 * Standard Destructor.
 * Cleans up the memory automatically.
 * \param void
 * \return void
 * \author <a href="mailto:<EMAIL>"><NAME></a>
 * \date Aug.2004
 */
GLSLProgram::~GLSLProgram() {
  if (IsValid()) { 
    if (m_bGLUseARB) 
      glDeleteObjectARB(m_hProgram);
    else
      glDeleteProgram(m_hProgram);
  }
  m_hProgram=0;
}



/**
 * Returns the handle to this shader.
 * \param void
 * \return a const handle. If this is an invalid shader, the handle will be 0.
 * \author <a href="mailto:<EMAIL>"><NAME></a>
 * \date Feb.2005
 */
GLSLProgram::operator GLuint(void) const {
  return m_hProgram;
}

/**
 * Initializes the class.
 * If GLSLProgram is initialized for the first time, initialize GLEW
 * \param void
 * \return bool 
 * \author <a href="mailto:<EMAIL>"><NAME></a>
 * \date Aug.2004
 * \see m_bGlewInitialized
 */
bool GLSLProgram::Initialize(void) {
  if (!m_bGlewInitialized) {
    if (GLEW_OK!=glewInit()) m_pMasterController->DebugOut()->Error("GLSLProgram::Initialize","GLEW initialization failed!");    
    m_bGlewInitialized=true;
  }
#ifdef GLSL_DEBUG  // just in case someone wants to handle GLEW himself (by setting the static var to true) but failed to do so properly
  else {
    if (glMultiTexCoord2f==NULL) m_pMasterController->DebugOut()->Error("GLSLProgram::Initialize","GLEW must be initialized. Set GLSLProgram::m_bGlewInitialized = false in GLSLProgram.cpp if you want this class to do it for you");
  }
#endif

  if (!m_bGLChecked) {
    if (GLEW_VERSION_2_0) {
      m_pMasterController->DebugOut()->Message("GLSLProgram::Initialize","OpenGL 2.0 supported");
      m_bGLUseARB = false;
    } else { // check for ARB extensions
      if (glewGetExtension("GL_ARB_shader_objects")) 
        m_pMasterController->DebugOut()->Message("GLSLProgram::Initialize","ARB_shader_objects supported.");
      else {
        m_pMasterController->DebugOut()->Error("GLSLProgram::Initialize","ARB_shader_objects not supported!");
        return false;
      }
      if (glewGetExtension("GL_ARB_shading_language_100")) 
        m_pMasterController->DebugOut()->Message("GLSLProgram::Initialize","ARB_shading_language_100 supported.");
      else {
        m_pMasterController->DebugOut()->Message("GLSLProgram::Initialize","ARB_shading_language_100 not supported!");
        return false;
      }

      glUniform1i  = glUniform1iARB;    glUniform2i  = glUniform2iARB;
      glUniform1iv = glUniform1ivARB;   glUniform2iv = glUniform2ivARB;
      glUniform3i  = glUniform3iARB;    glUniform4i  = glUniform4iARB;
      glUniform3iv = glUniform3ivARB;   glUniform4iv = glUniform4ivARB;

      glUniform1f  = glUniform1fARB;    glUniform2f  = glUniform2fARB;
      glUniform1fv = glUniform1fvARB;   glUniform2fv = glUniform2fvARB;
      glUniform3f  = glUniform3fARB;    glUniform4f  = glUniform4fARB;
      glUniform3fv = glUniform3fvARB;   glUniform4fv = glUniform4fvARB;

      glUniformMatrix2fv = glUniformMatrix2fvARB;
      glUniformMatrix3fv = glUniformMatrix3fvARB;
      glUniformMatrix4fv = glUniformMatrix4fvARB;

      m_bGLUseARB = true;
    }
    return false;
  } 
  return true;
}



/**
 * Loads vertex and fragment shader from disk/memory.
 * Loads any combination of vertex and fragment shader from disk or from a memory position.
 * Generates error/information messages to stdout during loading.
 * If nor successful the handle of the shader will be set to 0.
 * \param VSFile - name of the file containing the vertex shader
 * \param FSFile - name of the file containing the fragment shader
 * \param src - selects the source of vertex and fragment shader. Can be either GLSLPROGRAM_DISK or GLSLPROGRAM_STRING
 * \return void
 * \warning Uses glGetError()
 * \author <a href="mailto:<EMAIL>"><NAME></a>
 * \date Aug.2004
 * \see GLSLPROGRAM_SOURCE
 */
void GLSLProgram::Load(const char *VSFile, const char *FSFile, GLSLPROGRAM_SOURCE src) {
  CheckGLError();

  // load<commit_msg>bugfix in the GLSLProgram::Initialize routine<commit_after>/*
   For more information, please see: http://software.sci.utah.edu

   The MIT License

   Copyright (c) 2008 Scientific Computing and Imaging Institute,
   University of Utah.

   
   Permission is hereby granted, free of charge, to any person obtaining a
   copy of this software and associated documentation files (the "Software"),
   to deal in the Software without restriction, including without limitation
   the rights to use, copy, modify, merge, publish, distribute, sublicense,
   and/or sell copies of the Software, and to permit persons to whom the
   Software is furnished to do so, subject to the following conditions:

   The above copyright notice and this permission notice shall be included
   in all copies or substantial portions of the Software.

   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
   OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
   FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
   THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
   LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
   FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
   DEALINGS IN THE SOFTWARE.
*/

/**
  \file    GLSLProgram.cpp
  \author  <NAME>, <NAME>
           SCI Institute, University of Utah
  \date    October 2008
*/

#ifdef _WIN32
  #pragma warning( disable : 4996 ) // disable deprecated warning 
#endif

#include "GLSLProgram.h"
#include <Controller/MasterController.h>

bool GLSLProgram::m_bGlewInitialized=true;    ///< GL Extension Wrangler (glew) is initialized on first instantiation
bool GLSLProgram::m_bGLChecked=false;         ///< GL extension check
bool GLSLProgram::m_bGLUseARB=false;          ///< use pre GL 2.0 syntax

/*
Hack: since the ATI/AMD driver has a bug and the 3rd parameter if glGetActiveUniform must be != 0 we use these two dummy vars in the glGetActiveUniform calls
*/
GLsizei AtiHackLen;
GLchar AtiHackChar;

/**
 * Default Constructor.
 * Initializes glew on first instantiation. 
 * \param void
 * \return void
 * \author <a href="mailto:<EMAIL>"><NAME></a>
 * \date Aug.2004
 * \see Initialize()
 */
GLSLProgram::GLSLProgram(MasterController* pMasterController) :
  m_pMasterController(pMasterController),
  m_bInitialized(false),
  m_bEnabled(false),
  m_hProgram(0)
{
    Initialize();
}

/**
 * Specialized Constructor.
 * Loads any combination of vertex and fragment shader from disk.
 * \param VSFile - name of the file containing the vertex shader
 * \param FSFile - name of the file containing the fragment shader
 * \param src - selects the source of vertex and fragment shader. Can be either GLSLPROGRAM_DISK or GLSLPROGRAM_STRING
 * \return void
 * \author <a href="mailto:<EMAIL>"><NAME></a>
 * \date Aug.2004
 * \see Initialize()
 * \see Load()
 * \see GLSLPROGRAM_SOURCE
 */
GLSLProgram::GLSLProgram(MasterController* pMasterController, const char *VSFile, const char *FSFile,GLSLPROGRAM_SOURCE src) :
  m_pMasterController(pMasterController),
  m_bInitialized(false),
  m_bEnabled(false),
  m_hProgram(0)
{
  if (Initialize())
    Load(VSFile,FSFile,src);
}



/**
 * Standard Destructor.
 * Cleans up the memory automatically.
 * \param void
 * \return void
 * \author <a href="mailto:<EMAIL>"><NAME></a>
 * \date Aug.2004
 */
GLSLProgram::~GLSLProgram() {
  if (IsValid()) { 
    if (m_bGLUseARB) 
      glDeleteObjectARB(m_hProgram);
    else
      glDeleteProgram(m_hProgram);
  }
  m_hProgram=0;
}



/**
 * Returns the handle to this shader.
 * \param void
 * \return a const handle. If this is an invalid shader, the handle will be 0.
 * \author <a href="mailto:<EMAIL>"><NAME></a>
 * \date Feb.2005
 */
GLSLProgram::operator GLuint(void) const {
  return m_hProgram;
}

/**
 * Initializes the class.
 * If GLSLProgram is initialized for the first time, initialize GLEW
 * \param void
 * \return bool 
 * \author <a href="mailto:<EMAIL>"><NAME></a>
 * \date Aug.2004
 * \see m_bGlewInitialized
 */
bool GLSLProgram::Initialize(void) {
  if (!m_bGlewInitialized) {
    if (GLEW_OK!=glewInit()) m_pMasterController->DebugOut()->Error("GLSLProgram::Initialize","GLEW initialization failed!");    
    m_bGlewInitialized=true;
  }
#ifdef GLSL_DEBUG  // just in case someone wants to handle GLEW himself (by setting the static var to true) but failed to do so properly
  else {
    if (glMultiTexCoord2f==NULL) m_pMasterController->DebugOut()->Error("GLSLProgram::Initialize","GLEW must be initialized. Set GLSLProgram::m_bGlewInitialized = false in GLSLProgram.cpp if you want this class to do it for you");
  }
#endif

  if (!m_bGLChecked) {
    if (GLEW_VERSION_2_0) {
      m_pMasterController->DebugOut()->Message("GLSLProgram::Initialize","OpenGL 2.0 supported");
      m_bGLUseARB = false;
    } else { // check for ARB extensions
      if (glewGetExtension("GL_ARB_shader_objects")) 
        m_pMasterController->DebugOut()->Message("GLSLProgram::Initialize","ARB_shader_objects supported.");
      else {
        m_pMasterController->DebugOut()->Error("GLSLProgram::Initialize","Neither OpenGL 2.0 nor ARB_shader_objects not supported!");
        return false;
      }
      if (glewGetExtension("GL_ARB_shading_language_100")) 
        m_pMasterController->DebugOut()->Message("GLSLProgram::Initialize","ARB_shading_language_100 supported.");
      else {
        m_pMasterController->DebugOut()->Message("GLSLProgram::Initialize","Neither OpenGL 2.0 nor ARB_shading_language_100 not supported!");
        return false;
      }

      glUniform1i  = glUniform1iARB;    glUniform2i  = glUniform2iARB;
      glUniform1iv = glUniform1ivARB;   glUniform2iv = glUniform2ivARB;
      glUniform3i  = glUniform3iARB;    glUniform4i  = glUniform4iARB;
      glUniform3iv = glUniform3ivARB;   glUniform4iv = glUniform4ivARB;

      glUniform1f  = glUniform1fARB;    glUniform2f  = glUniform2fARB;
      glUniform1fv = glUniform1fvARB;   glUniform2fv = glUniform2fvARB;
      glUniform3f  = glUniform3fARB;    glUniform4f  = glUniform4fARB;
      glUniform3fv = glUniform3fvARB;   glUniform4fv = glUniform4fvARB;

      glUniformMatrix2fv = glUniformMatrix2fvARB;
      glUniformMatrix3fv = glUniformMatrix3fvARB;
      glUniformMatrix4fv = glUniformMatrix4fvARB;

      m_bGLUseARB = true;
    }
  } 
  return true;
}



/**
 * Loads vertex and fragment shader from disk/memory.
 * Loads any combination of vertex and fragment shader from disk or from a memory position.
 * Generates error/information messages to stdout during loading.
 * If nor successful the handle of the shader will be set to 0.
 * \param VSFile - name of the file containing the vertex shader
 * \param FSFile - name of the file containing the fragment shader
 * \param src - selects the source of vertex and fragment shader. Can be either GLSLPROGRAM_DISK or GLSLPROGRAM_STRING
 * \return void
 * \warning Uses glGetError()
 * \author <a href="mailto:<EMAIL>"><NAME></a>
 * \date Aug.2004
 * \see GLSLPROGRAM_SOURCE
 */
void GLSLProgram::Load(const char *VSFile, const char *FSFile, GLSLPROGRAM_SOURCE src) {
  CheckGLError();

  // load
{'fraction_non_alphanumeric': 0.09519485961726498, 'fraction_numerical': 0.014108115658611538, 'mean_word_length': 3.8987341772151898, 'pattern_counts': {'":': 0, '<': 63, '<?xml version=': 0, '>': 85, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4405513', 'n_tokens_mistral': 4535, 'n_tokens_neox': 4359, 'n_words': 1377}