<commit_before>﻿/***********************************************************************
    created:    Fri, 4th July 2014
    author:     <NAME>
*************************************************************************/
/***************************************************************************
 *   Copyright (C) 2004 - 2014 <NAME> & The CEGUI Development Team
 *
 *   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 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 "CEGUI/RendererModules/Ogre/ShaderWrapper.h"
#include "CEGUI/RendererModules/Ogre/Texture.h"
#include "CEGUI/ShaderParameterBindings.h"
#include "CEGUI/Exceptions.h"

#include <glm/gtc/type_ptr.hpp>
#include "OgreRenderSystem.h"

// Start of CEGUI namespace section
namespace CEGUI
{

//----------------------------------------------------------------------------//
OgreShaderWrapper::OgreShaderWrapper(OgreRenderer& owner, 
    Ogre::RenderSystem& rs, 
    Ogre::HighLevelGpuProgramPtr vs, Ogre::HighLevelGpuProgramPtr ps)
    : d_vertexShader(vs),
    d_pixelShader(ps),
    d_owner(owner),
    d_renderSystem(rs),
    d_previousMatrix(Ogre::Matrix4::ZERO)
{
    d_vertexParameters = d_vertexShader->createParameters();
    d_pixelParameters = d_pixelShader->createParameters();

    Ogre::GpuConstantDefinitionMap::const_iterator target = 
        d_vertexShader->getConstantDefinitions().map.find("modelViewPerspMatrix");

    d_physicalIndex = target->second.physicalIndex;
}

//----------------------------------------------------------------------------//
OgreShaderWrapper::~OgreShaderWrapper()
{
    d_pixelParameters.setNull();
    d_vertexParameters.setNull();
    d_vertexShader.setNull();
    d_pixelShader.setNull();
}

//----------------------------------------------------------------------------//
void OgreShaderWrapper::prepareForRendering(const ShaderParameterBindings* shaderParameterBindings)
{

    Ogre::GpuProgram* vs = d_vertexShader->_getBindingDelegate();
    d_renderSystem.bindGpuProgram(vs);

    Ogre::GpuProgram* ps = d_pixelShader->_getBindingDelegate();
    d_renderSystem.bindGpuProgram(ps);


    const ShaderParameterBindings::ShaderParameterBindingsMap& 
        shader_parameter_bindings = shaderParameterBindings->
            getShaderParameterBindings();

    ShaderParameterBindings::ShaderParameterBindingsMap::const_iterator iter = 
        shader_parameter_bindings.begin();
    ShaderParameterBindings::ShaderParameterBindingsMap::const_iterator end = 
        shader_parameter_bindings.end();

    while(iter != end)
    {
        const CEGUI::ShaderParameter* parameter = iter->second;
        const ShaderParamType parameterType = parameter->getType();


        if (iter->first == "texture0")
        {
            // Texture always goes to the pixel shader

            if (parameterType != SPT_TEXTURE)
            {
                CEGUI_THROW(RendererException("Invalid parameter type"));
            }

            const CEGUI::ShaderParameterTexture* parameterTexture = 
                static_cast<const CEGUI::ShaderParameterTexture*>(parameter);

            const CEGUI::OgreTexture* texture = static_cast<const 
                CEGUI::OgreTexture*>(parameterTexture->d_parameterValue);


            d_renderSystem._setTexture(0, true, texture->getOgreTexture());

        } 
        else if (iter->first == "modelViewPerspMatrix")
        {
            // Transform matrix goes to the vertex shader

            if (parameterType != SPT_MATRIX_4X4)
            {

                CEGUI_THROW(RendererException("Invalid parameter type"));
            }

            const CEGUI::ShaderParameterMatrix* mat = static_cast<const 
                CEGUI::ShaderParameterMatrix*>(parameter);

            if (d_previousMatrix != *mat->d_parameterValue)
            {
                d_previousMatrix = *mat->d_parameterValue;

                d_vertexParameters->_writeRawConstants(d_physicalIndex, 
                    &d_previousMatrix[0][0], 4);

                d_vertexParameters->_writeRawConstants(d_physicalIndex+4, 
                    &d_previousMatrix[1][0], 4);

                d_vertexParameters->_writeRawConstants(d_physicalIndex+8, 
                    &d_previousMatrix[2][0], 4);

                d_vertexParameters->_writeRawConstants(d_physicalIndex+12, 
                    &d_previousMatrix[3][0], 4);
            }
        } 
        else
        {
            std::string errorMessage = std::string("Unknown variable name: \"")+
                iter->first + "\"";
            CEGUI_THROW(RendererException(errorMessage));
        }

        ++iter; 
    }


    // Pass the finalized parameters to Ogre
    d_renderSystem.bindGpuProgramParameters(Ogre::GPT_VERTEX_PROGRAM, 
        d_vertexParameters, Ogre::GPV_ALL);
    d_renderSystem.bindGpuProgramParameters(Ogre::GPT_FRAGMENT_PROGRAM, 
        d_pixelParameters, Ogre::GPV_ALL);
}

//----------------------------------------------------------------------------//
}

<commit_msg>Fixed hardware index getting, added checking/exception for invalid index<commit_after>﻿/***********************************************************************
    created:    Fri, 4th July 2014
    author:     <NAME>
*************************************************************************/
/***************************************************************************
 *   Copyright (C) 2004 - 2014 <NAME> & The CEGUI Development Team
 *
 *   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 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 "CEGUI/RendererModules/Ogre/ShaderWrapper.h"
#include "CEGUI/RendererModules/Ogre/Texture.h"
#include "CEGUI/ShaderParameterBindings.h"
#include "CEGUI/Exceptions.h"

#include <glm/gtc/type_ptr.hpp>
#include "OgreRenderSystem.h"

//! Used to abort setting parameters when they don't make any sense
//! Even this value would cause errors but the value we are checking for is VERY
//! large
#define LAST_SANE_HW_INDEX          100000


// Start of CEGUI namespace section
namespace CEGUI
{

//----------------------------------------------------------------------------//
OgreShaderWrapper::OgreShaderWrapper(OgreRenderer& owner, 
    Ogre::RenderSystem& rs, 
    Ogre::HighLevelGpuProgramPtr vs, Ogre::HighLevelGpuProgramPtr ps)
    : d_vertexShader(vs),
    d_pixelShader(ps),
    d_owner(owner),
    d_renderSystem(rs),
    d_previousMatrix(Ogre::Matrix4::ZERO)
{
    d_vertexParameters = d_vertexShader->createParameters();
    d_pixelParameters = d_pixelShader->createParameters();

    const Ogre::GpuConstantDefinitionMap& map = 
        d_vertexShader->getConstantDefinitions().map;

    Ogre::GpuConstantDefinitionMap::const_iterator target = 
        map.find("worldViewProjMatrix");

    // This will only be true when the shaders/parameter names are invalid
    if (target == map.end())
    {
        // This should map to almost SIZE_T_MAX
        d_physicalIndex = -1;

        CEGUI_THROW(RendererException("Ogre renderer couldn't find an index for"
            " the shader data."));

        // Don't want to fall through, ever
        return;
    }


    d_physicalIndex = target->second.physicalIndex;
}

//----------------------------------------------------------------------------//
OgreShaderWrapper::~OgreShaderWrapper()
{
    d_pixelParameters.setNull();
    d_vertexParameters.setNull();
    d_vertexShader.setNull();
    d_pixelShader.setNull();
}

//----------------------------------------------------------------------------//
void OgreShaderWrapper::prepareForRendering(const ShaderParameterBindings* shaderParameterBindings)
{

    Ogre::GpuProgram* vs = d_vertexShader->_getBindingDelegate();
    d_renderSystem.bindGpuProgram(vs);

    Ogre::GpuProgram* ps = d_pixelShader->_getBindingDelegate();
    d_renderSystem.bindGpuProgram(ps);


    const ShaderParameterBindings::ShaderParameterBindingsMap& 
        shader_parameter_bindings = shaderParameterBindings->
            getShaderParameterBindings();

    ShaderParameterBindings::ShaderParameterBindingsMap::const_iterator iter = 
        shader_parameter_bindings.begin();
    ShaderParameterBindings::ShaderParameterBindingsMap::const_iterator end = 
        shader_parameter_bindings.end();

    while(iter != end)
    {
        const CEGUI::ShaderParameter* parameter = iter->second;
        const ShaderParamType parameterType = parameter->getType();


        if (iter->first == "texture0")
        {
            // Texture always goes to the pixel shader

            if (parameterType != SPT_TEXTURE)
            {
                CEGUI_THROW(RendererException("Invalid parameter type"));
            }

            const CEGUI::ShaderParameterTexture* parameterTexture = 
                static_cast<const CEGUI::ShaderParameterTexture*>(parameter);

            const CEGUI::OgreTexture* texture = static_cast<const 
                CEGUI::OgreTexture*>(parameterTexture->d_parameterValue);


            d_renderSystem._setTexture(0, true, texture->getOgreTexture());

        } 
        else if (iter->first == "modelViewPerspMatrix")
        {
            // Transform matrix goes to the vertex shader

            if (parameterType != SPT_MATRIX_4X4)
            {

                CEGUI_THROW(RendererException("Invalid parameter type"));
            }

            const CEGUI::ShaderParameterMatrix* mat = static_cast<const 
                CEGUI::ShaderParameterMatrix*>(parameter);

            // The other check is to make sure that even invalid operations 
            // don't assert the program
            // TODO: throw when invalid physical index?
            if (d_previousMatrix != *mat->d_parameterValue && d_physicalIndex <
                LAST_SANE_HW_INDEX)
            {
                d_previousMatrix = *mat->d_parameterValue;

                d_vertexParameters->_writeRawConstants(d_physicalIndex, 
                    &d_previousMatrix[0][0], 4);

                d_vertexParameters->_writeRawConstants(d_physicalIndex+4, 
                    &d_previousMatrix[1][0], 4);

                d_vertexParameters->_writeRawConstants(d_physicalIndex+8, 
                    &d_previousMatrix[2][0], 4);

                d_vertexParameters->_writeRawConstants(d_physicalIndex+12, 
                    &d_previousMatrix[3][0], 4);
            }
        } 
        else
        {
            std::string errorMessage = std::string("Unknown variable name: \"")+
                iter->first + "\"";
            CEGUI_THROW(RendererException(errorMessage));
        }

        ++iter; 
    }


    // Pass the finalized parameters to Ogre
    d_renderSystem.bindGpuProgramParameters(Ogre::GPT_VERTEX_PROGRAM, 
        d_vertexParameters, Ogre::GPV_ALL);
    d_renderSystem.bindGpuProgramParameters(Ogre::GPT_FRAGMENT_PROGRAM, 
        d_pixelParameters, Ogre::GPV_ALL);
}

//----------------------------------------------------------------------------//
}


{'fraction_non_alphanumeric': 0.17514947110225357, 'fraction_numerical': 0.005748888548214012, 'mean_word_length': 2.5991724137931036, 'pattern_counts': {'":': 0, '<': 16, '<?xml version=': 0, '>': 55, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4325333', 'n_tokens_mistral': 3466, 'n_tokens_neox': 3207, 'n_words': 792}