<commit_before>/*
-----------------------------------------------------------------------------
This source file is part of OGRE
    (Object-oriented Graphics Rendering Engine)
For the latest info, see http://www.ogre3d.org/

Copyright (c) 2000-2014 Tor<NAME> Ltd

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.
-----------------------------------------------------------------------------
*/

#include "OgreGLSLExtSupport.h"
#include "OgreGLSLLinkProgram.h"
#include "OgreStringConverter.h"
#include "OgreGLSLProgram.h"
#include "OgreGLSLLinkProgramManager.h"
#include "OgreException.h"
#include "OgreGpuProgramManager.h"
#include "OgreGLSLProgramCommon.h"

namespace Ogre {
    namespace GLSL {

    static GLint getGLGeometryInputPrimitiveType(RenderOperation::OperationType operationType)
    {
        switch (operationType)
        {
        case RenderOperation::OT_POINT_LIST:
            return GL_POINTS;
        case RenderOperation::OT_LINE_LIST:
        case RenderOperation::OT_LINE_STRIP:
			return GL_LINES;
        case RenderOperation::OT_LINE_LIST_ADJ:
        case RenderOperation::OT_LINE_STRIP_ADJ:
			return GL_LINES_ADJACENCY_EXT;
        case RenderOperation::OT_TRIANGLE_LIST_ADJ:
        case RenderOperation::OT_TRIANGLE_STRIP_ADJ:
            return GL_TRIANGLES_ADJACENCY_EXT;
        default:
        case RenderOperation::OT_TRIANGLE_LIST:
        case RenderOperation::OT_TRIANGLE_STRIP:
        case RenderOperation::OT_TRIANGLE_FAN:
            return GL_TRIANGLES;
		}
    }
    //-----------------------------------------------------------------------
    static GLint getGLGeometryOutputPrimitiveType(RenderOperation::OperationType operationType)
    {
        switch (operationType)
        {
        case RenderOperation::OT_POINT_LIST:
            return GL_POINTS;
        case RenderOperation::OT_LINE_STRIP:
            return GL_LINE_STRIP;
        default:
        case RenderOperation::OT_TRIANGLE_STRIP:
            return GL_TRIANGLE_STRIP;
        }
    }

    //-----------------------------------------------------------------------
    GLSLLinkProgram::GLSLLinkProgram(const GLShaderList& shaders) : GLSLProgramCommon(shaders)
    {
        // Initialise uniform cache
        mUniformCache = new GLUniformCache();
    }

    //-----------------------------------------------------------------------
    GLSLLinkProgram::~GLSLLinkProgram(void)
    {
        glDeleteObjectARB(mGLProgramHandle);

        delete mUniformCache;
        mUniformCache = 0;
    }

    //-----------------------------------------------------------------------
    void GLSLLinkProgram::activate(void)
    {
        if (!mLinked)
        {           
            glGetError(); //Clean up the error. Otherwise will flood log.

            mGLProgramHandle = glCreateProgramObjectARB();

            GLenum glErr = glGetError();
            if(glErr != GL_NO_ERROR)
            {
                reportGLSLError( glErr, "GLSLLinkProgram::activate", "Error Creating GLSL Program Object", 0 );
            }

            uint32 hash = getCombinedHash();

            if ( GpuProgramManager::getSingleton().canGetCompiledShaderBuffer() &&
                 GpuProgramManager::getSingleton().isMicrocodeAvailableInCache(hash) &&
                 !mShaders[GPT_GEOMETRY_PROGRAM])
            {
                getMicrocodeFromCache(hash);
            }
            else
            {
                compileAndLink();

            }
            buildGLUniformReferences();
            extractAttributes();
        }
        if (mLinked)
        {
            GLenum glErr = glGetError();
            if(glErr != GL_NO_ERROR)
            {
                reportGLSLError( glErr, "GLSLLinkProgram::Activate",
                    "Error prior to using GLSL Program Object : ", mGLProgramHandle, false, false);
            }

            glUseProgramObjectARB( mGLProgramHandle );

            glErr = glGetError();
            if(glErr != GL_NO_ERROR)
            {
                reportGLSLError( glErr, "GLSLLinkProgram::Activate",
                    "Error using GLSL Program Object : ", mGLProgramHandle, false, false);
            }
        }
    }
    //-----------------------------------------------------------------------
    void GLSLLinkProgram::getMicrocodeFromCache(uint32 id)
    {
        GpuProgramManager::Microcode cacheMicrocode = 
            GpuProgramManager::getSingleton().getMicrocodeFromCache(id);
        
        GLenum binaryFormat = *((GLenum *)(cacheMicrocode->getPtr()));
        uint8 * programBuffer = cacheMicrocode->getPtr() + sizeof(GLenum);
        size_t sizeOfBuffer = cacheMicrocode->size() - sizeof(GLenum);
        glProgramBinary(mGLProgramHandle,
                        binaryFormat, 
                        programBuffer,
                        static_cast<GLsizei>(sizeOfBuffer)
                        );

        glGetProgramiv(mGLProgramHandle, GL_LINK_STATUS, &mLinked);
        if (!mLinked)
        {
            //
            // Something must have changed since the program binaries
            // were cached away.  Fallback to source shader loading path,
            // and then retrieve and cache new program binaries once again.
            //<commit_msg>GL: GLSLLinkProgram::activate - drop pre-use glGetError<commit_after>/*
-----------------------------------------------------------------------------
This source file is part of OGRE
    (Object-oriented Graphics Rendering Engine)
For the latest info, see http://www.ogre3d.org/

Copyright (c) 2000-2014 <NAME>

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.
-----------------------------------------------------------------------------
*/

#include "OgreGLSLExtSupport.h"
#include "OgreGLSLLinkProgram.h"
#include "OgreStringConverter.h"
#include "OgreGLSLProgram.h"
#include "OgreGLSLLinkProgramManager.h"
#include "OgreException.h"
#include "OgreGpuProgramManager.h"
#include "OgreGLSLProgramCommon.h"

namespace Ogre {
    namespace GLSL {

    static GLint getGLGeometryInputPrimitiveType(RenderOperation::OperationType operationType)
    {
        switch (operationType)
        {
        case RenderOperation::OT_POINT_LIST:
            return GL_POINTS;
        case RenderOperation::OT_LINE_LIST:
        case RenderOperation::OT_LINE_STRIP:
			return GL_LINES;
        case RenderOperation::OT_LINE_LIST_ADJ:
        case RenderOperation::OT_LINE_STRIP_ADJ:
			return GL_LINES_ADJACENCY_EXT;
        case RenderOperation::OT_TRIANGLE_LIST_ADJ:
        case RenderOperation::OT_TRIANGLE_STRIP_ADJ:
            return GL_TRIANGLES_ADJACENCY_EXT;
        default:
        case RenderOperation::OT_TRIANGLE_LIST:
        case RenderOperation::OT_TRIANGLE_STRIP:
        case RenderOperation::OT_TRIANGLE_FAN:
            return GL_TRIANGLES;
		}
    }
    //-----------------------------------------------------------------------
    static GLint getGLGeometryOutputPrimitiveType(RenderOperation::OperationType operationType)
    {
        switch (operationType)
        {
        case RenderOperation::OT_POINT_LIST:
            return GL_POINTS;
        case RenderOperation::OT_LINE_STRIP:
            return GL_LINE_STRIP;
        default:
        case RenderOperation::OT_TRIANGLE_STRIP:
            return GL_TRIANGLE_STRIP;
        }
    }

    //-----------------------------------------------------------------------
    GLSLLinkProgram::GLSLLinkProgram(const GLShaderList& shaders) : GLSLProgramCommon(shaders)
    {
        // Initialise uniform cache
        mUniformCache = new GLUniformCache();
    }

    //-----------------------------------------------------------------------
    GLSLLinkProgram::~GLSLLinkProgram(void)
    {
        glDeleteObjectARB(mGLProgramHandle);

        delete mUniformCache;
        mUniformCache = 0;
    }

    //-----------------------------------------------------------------------
    void GLSLLinkProgram::activate(void)
    {
        if (!mLinked)
        {           
            glGetError(); //Clean up the error. Otherwise will flood log.

            mGLProgramHandle = glCreateProgramObjectARB();

            GLenum glErr = glGetError();
            if(glErr != GL_NO_ERROR)
            {
                reportGLSLError( glErr, "GLSLLinkProgram::activate", "Error Creating GLSL Program Object", 0 );
            }

            uint32 hash = getCombinedHash();

            if ( GpuProgramManager::getSingleton().canGetCompiledShaderBuffer() &&
                 GpuProgramManager::getSingleton().isMicrocodeAvailableInCache(hash) &&
                 !mShaders[GPT_GEOMETRY_PROGRAM])
            {
                getMicrocodeFromCache(hash);
            }
            else
            {
                compileAndLink();

            }
            buildGLUniformReferences();
            extractAttributes();
        }
        if (mLinked)
        {
            glUseProgramObjectARB( mGLProgramHandle );

            GLenum glErr = glGetError();
            if(glErr != GL_NO_ERROR)
            {
                reportGLSLError( glErr, "GLSLLinkProgram::Activate",
                    "Error using GLSL Program Object : ", mGLProgramHandle, false, false);
            }
        }
    }
    //-----------------------------------------------------------------------
    void GLSLLinkProgram::getMicrocodeFromCache(uint32 id)
    {
        GpuProgramManager::Microcode cacheMicrocode = 
            GpuProgramManager::getSingleton().getMicrocodeFromCache(id);
        
        GLenum binaryFormat = *((GLenum *)(cacheMicrocode->getPtr()));
        uint8 * programBuffer = cacheMicrocode->getPtr() + sizeof(GLenum);
        size_t sizeOfBuffer = cacheMicrocode->size() - sizeof(GLenum);
        glProgramBinary(mGLProgramHandle,
                        binaryFormat, 
                        programBuffer,
                        static_cast<GLsizei>(sizeOfBuffer)
                        );

        glGetProgramiv(mGLProgramHandle, GL_LINK_STATUS, &mLinked);
        if (!mLinked)
        {
            //
            // Something must have changed since the program binaries
            // were cached away.  Fallback to source shader loading path,
            // and then retrieve and cache new program binaries once again.
            //
{'fraction_non_alphanumeric': 0.15114754098360655, 'fraction_numerical': 0.002622950819672131, 'mean_word_length': 1.855370933770185, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '7244687', 'n_tokens_mistral': 3156, 'n_tokens_neox': 3077, 'n_words': 804}