<commit_before>//------------------------------------------------------------------------------
//  glShaderFactory.cc
//------------------------------------------------------------------------------
#include "Pre.h"
#include "glShaderFactory.h"
#include "Render/Core/shader.h"
#include "Render/gl/glTypes.h"
#include "Render/gl/gl_impl.h"
#include "Core/String/StringBuilder.h"
#include "Core/Memory/Memory.h"

namespace Oryol {
namespace Render {

using namespace Core;
using namespace IO;
using namespace Resource;
    
//------------------------------------------------------------------------------
glShaderFactory::glShaderFactory() :
isValid(false) {
    // empty
}

//------------------------------------------------------------------------------
glShaderFactory::~glShaderFactory() {
    o_assert(!this->isValid);
}

//------------------------------------------------------------------------------
void
glShaderFactory::Setup() {
    o_assert(!this->isValid);
    this->isValid = true;
}

//------------------------------------------------------------------------------
void
glShaderFactory::Discard() {
    o_assert(this->isValid);
    this->isValid = false;
}

//------------------------------------------------------------------------------
bool
glShaderFactory::IsValid() const {
    return this->isValid;
}

//------------------------------------------------------------------------------
void
glShaderFactory::SetupResource(shader& shd) {
    o_assert(this->isValid);
    o_assert(shd.GetState() == State::Setup);
    ORYOL_GL_CHECK_ERROR();
    
    Log::Info("glShaderFactory: compiling shader '%s'\n", shd.GetSetup().GetLocator().Location().AsCStr());
    
    // create a shader object
    const ShaderSetup& setup = shd.GetSetup();
    GLenum glShaderType = glTypes::AsGLShaderType(setup.GetType());
    const GLuint glShader = glCreateShader(glShaderType);
    o_assert(0 != glShader);
    ORYOL_GL_CHECK_ERROR();
    
    // estimate length of complete source string
    int32 srcLength = 0;
    for (const auto& define : setup.GetDefines()) {
        srcLength += 10 + define.Key().Length() + define.Value().Length();
    }
    srcLength += setup.GetSource().Length();
    
    // setup the shader source
    StringBuilder strBuilder;
    strBuilder.Reserve(2 * srcLength + 1024);
    #if ORYOL_OPENGLES2
        strBuilder.Append("#define ORYOL_OPENGLES2 (1)\n");
        if (GL_FRAGMENT_SHADER == glShaderType) {
            strBuilder.Append("precision mediump float;\n");
            strBuilder.Append("#define FS_INPUT(type,name) varying type name\n");
            strBuilder.Append("#define FragmentColor gl_FragColor\n");
        }
        if (GL_VERTEX_SHADER == glShaderType) {
            strBuilder.Append("#define VS_INPUT(type,name) attribute type name\n");
            strBuilder.Append("#define VS_OUTPUT(type,name) varying type name\n");
        }
    #else
        strBuilder.Append("#version 150\n");
        strBuilder.Append("#define ORYOL_OPENGL (1)\n");
        strBuilder.Append("#define lowp\n");
        strBuilder.Append("#define mediump\n");
        strBuilder.Append("#define highp\n");
        if (GL_VERTEX_SHADER == glShaderType) {
            strBuilder.Append("#define VS_INPUT(type,name) in type name\n");
            strBuilder.Append("#define VS_OUTPUT(type,name) out type name\n");
        }
        if (GL_FRAGMENT_SHADER == glShaderType) {
            strBuilder.Append("#define FS_INPUT(type,name) in type name\n");
            strBuilder.Append("out vec4 FragmentColor;\n");
        }
    #endif
    
    #if ORYOL_WINDOWS
        strBuilder.Append("#define ORYOL_WINDOWS (1)\n");
    #elif ORYOL_OSX
        strBuilder.Append("#define ORYOL_OSX (1)\n");<commit_msg>TEXTURE2D shader code macro to hide difference between GLSL versions<commit_after>//------------------------------------------------------------------------------
//  glShaderFactory.cc
//------------------------------------------------------------------------------
#include "Pre.h"
#include "glShaderFactory.h"
#include "Render/Core/shader.h"
#include "Render/gl/glTypes.h"
#include "Render/gl/gl_impl.h"
#include "Core/String/StringBuilder.h"
#include "Core/Memory/Memory.h"

namespace Oryol {
namespace Render {

using namespace Core;
using namespace IO;
using namespace Resource;
    
//------------------------------------------------------------------------------
glShaderFactory::glShaderFactory() :
isValid(false) {
    // empty
}

//------------------------------------------------------------------------------
glShaderFactory::~glShaderFactory() {
    o_assert(!this->isValid);
}

//------------------------------------------------------------------------------
void
glShaderFactory::Setup() {
    o_assert(!this->isValid);
    this->isValid = true;
}

//------------------------------------------------------------------------------
void
glShaderFactory::Discard() {
    o_assert(this->isValid);
    this->isValid = false;
}

//------------------------------------------------------------------------------
bool
glShaderFactory::IsValid() const {
    return this->isValid;
}

//------------------------------------------------------------------------------
void
glShaderFactory::SetupResource(shader& shd) {
    o_assert(this->isValid);
    o_assert(shd.GetState() == State::Setup);
    ORYOL_GL_CHECK_ERROR();
    
    Log::Info("glShaderFactory: compiling shader '%s'\n", shd.GetSetup().GetLocator().Location().AsCStr());
    
    // create a shader object
    const ShaderSetup& setup = shd.GetSetup();
    GLenum glShaderType = glTypes::AsGLShaderType(setup.GetType());
    const GLuint glShader = glCreateShader(glShaderType);
    o_assert(0 != glShader);
    ORYOL_GL_CHECK_ERROR();
    
    // estimate length of complete source string
    int32 srcLength = 0;
    for (const auto& define : setup.GetDefines()) {
        srcLength += 10 + define.Key().Length() + define.Value().Length();
    }
    srcLength += setup.GetSource().Length();
    
    // setup the shader source
    StringBuilder strBuilder;
    strBuilder.Reserve(2 * srcLength + 1024);
    #if ORYOL_OPENGLES2
        strBuilder.Append("#define ORYOL_OPENGLES2 (1)\n");
        if (GL_VERTEX_SHADER == glShaderType) {
            strBuilder.Append("#define VS_INPUT(type,name) attribute type name\n");
            strBuilder.Append("#define VS_OUTPUT(type,name) varying type name\n");
        }
        if (GL_FRAGMENT_SHADER == glShaderType) {
            strBuilder.Append("precision mediump float;\n");
            strBuilder.Append("#define FS_INPUT(type,name) varying type name\n");
            strBuilder.Append("#define TEXTURE2D(x,y) texture2D(x,y)\n");            
            strBuilder.Append("#define FragmentColor gl_FragColor\n");
        }
    #else
        strBuilder.Append("#version 150\n");
        strBuilder.Append("#define ORYOL_OPENGL (1)\n");
        strBuilder.Append("#define lowp\n");
        strBuilder.Append("#define mediump\n");
        strBuilder.Append("#define highp\n");
        if (GL_VERTEX_SHADER == glShaderType) {
            strBuilder.Append("#define VS_INPUT(type,name) in type name\n");
            strBuilder.Append("#define VS_OUTPUT(type,name) out type name\n");
        }
        if (GL_FRAGMENT_SHADER == glShaderType) {
            strBuilder.Append("#define FS_INPUT(type,name) in type name\n");
            strBuilder.Append("#define TEXTURE2D(x,y) texture(x,y)\n");
            strBuilder.Append("out vec4 FragmentColor;\n");
        }
    #endif
    
    #if ORYOL_WINDOWS
        strBuilder.Append("#define ORYOL_WINDOWS (1)\n");
    #elif ORYOL_OSX
        strBuilder.Append("#define ORYOL_OSX (1)\n");
{'fraction_non_alphanumeric': 0.2825491984882054, 'fraction_numerical': 0.005995047569399192, 'mean_word_length': 1.748567335243553, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 17, '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': '3507579', 'n_tokens_mistral': 2092, 'n_tokens_neox': 2071, 'n_words': 388}