<commit_before>#include <cstdio>
#include <iostream>
#include <glad/glad.h>

#include "shader.h"

// src - a file that has been opened already
// dest - a std::string where the contents of src will be put into
static void readFile(std::string& dest, FILE* src)
{
    char* fileBuf;
    size_t fileSize;

    // get size of contents
    fseek(src, 0, SEEK_END);
    fileSize = ftell(src);

    // use size of contents
    dest.reserve(fileSize);
    fileBuf = new char[fileSize];

    // get contents
    rewind(src);
    fread(fileBuf, sizeof(char), fileSize, src);

    // assign contents and cleanup
    dest.assign(fileBuf);
    delete[] fileBuf;
}

Shader::Shader()
{
    anyErrors = false;
    typeFlags = 0;
    compiled = false;
    vertShaderHandle = 0;
    geomShaderHandle = 0;
    tessShaderHandle = 0;
    fragShaderHandle = 0;
}

Shader::Shader(std::string vert_filename, std::string frag_filename)
{
    FILE* inFile;

    inFile = fopen(vert_filename.c_str(), "rb");
    if (inFile == NULL) {
        std::cout << "Vertex shader file doesn't exist.\n";
        anyErrors = true;
        return;
    }
    readFile(vertSource, inFile);

    inFile = fopen(frag_filename.c_str(), "rb");
    if (inFile == NULL) {
        std::cout << "Fragment shader file doesn't exist.\n";
        anyErrors = true;
        return;
    }
    readFile(fragSource, inFile);
    fclose(inFile);

    typeFlags = VERT_SHADER | FRAG_SHADER;
    compiled = false;
    vertShaderHandle = 0;
    geomShaderHandle = 0;
    tessShaderHandle = 0;
    fragShaderHandle = 0;
}

int32_t Shader::addShader(std::string src_filename, shader_type type)
{
    return -1;
}

GLuint Shader::getShaderProgram()
{
    if (compiled == false) {
        std::cout << "Shader hasn't been compiled yet\n";
        anyErrors = true;
        return 0;
    }

    return shaderProgHandle;
}

static bool isShaderStatusGood(GLint shaderHandle, GLenum statusType, char errorMsg[1024])
{
    GLint status;
    glGetShaderiv(shaderHandle, statusType, &status);
    if (status == GL_FALSE) {
        GLint length;
        glGetShaderiv(shaderHandle, GL_INFO_LOG_LENGTH, &length);
        std::cout << "Shader compile error is " << length << " chars long.\n";
        glGetShaderInfoLog(shaderHandle, length, NULL, errorMsg);
        std::cout << "glCompileShader failed: " << errorMsg << "\n";
        return false;
    } else {
        return true;
    }
}

bool Shader::compile()
{
    char buffer[1024];

    // Vertex shader
    vertShaderHandle = glCreateShader(GL_VERTEX_SHADER);
    const GLchar* vs = vertSource.c_str();
    glShaderSource(vertShaderHandle, 1, &vs, NULL);
    glCompileShader(vertShaderHandle);

    // check vertex shader compile status
    if (!isShaderStatusGood(vertShaderHandle, GL_COMPILE_STATUS, buffer)) {
        anyErrors = true;
        std::cout << buffer << std::endl;
        return false;
    }

    // geometry shader
    // tesselation shader

    // fragment shader
    fragShaderHandle = glCreateShader(GL_FRAGMENT_SHADER);
    const GLchar* fs = fragSource.c_str();
    glShaderSource(fragShaderHandle, 1, &fs, NULL);
    glCompileShader(fragShaderHandle);

    // check vertex shader compile status
    if (!isShaderStatusGood(fragShaderHandle, GL_COMPILE_STATUS, buffer)) {
        anyErrors = true;
        std::cout << buffer << std::endl;
        return false;
    }

    // create and link full pipeline
    shaderProgHandle = glCreateProgram();
    glAttachShader(shaderProgHandle, vertShaderHandle);
    if (typeFlags & GEOM_SHADER) {
        glAttachShader(shaderProgHandle, geomShaderHandle);
    }
    if (typeFlags & TESS_SHADER) {
        glAttachShader(shaderProgHandle, tessShaderHandle);
    }
    glAttachShader(shaderProgHandle, fragShaderHandle);
    glLinkProgram(shaderProgHandle);
    // remember that a call to glUseProgram will still need to be done

    // check program link status
    /*if (!isShaderProgramStatusGood(shaderProgHandle, GL_LINK_STATUS, buffer)) {
        anyErrors = true;
        std::cout << buffer << std::endl;
        return false;
    }*/

    GLint status;
    glGetProgramiv(shaderProgHandle, GL_LINK_STATUS, &status);
    if (status == GL_FALSE) {
        GLint length;
        glGetProgramiv(shaderProgHandle, GL_INFO_LOG_LENGTH, &length);
        GLchar *info = new GLchar[length];
        glGetProgramInfoLog(shaderProgHandle, length, NULL, info);
        std::cout << "glLinkProgram failed: " << info << std::endl;
        delete [] info;
    }
    //glDetachShader(shaderProgHandle, vertShaderHandle);
    //glDetachShader(shaderProgHandle, fragShaderHandle);
    //glDeleteShader(vertShaderHandle);
    //glDeleteShader(fragShaderHandle);

    compiled = true;
    return true;
}<commit_msg>made error output a little nicer<commit_after>#include <cstdio>
#include <iostream>
#include <glad/glad.h>

#include "shader.h"

// src - a file that has been opened already
// dest - a std::string where the contents of src will be put into
static void readFile(std::string& dest, FILE* src)
{
    char* fileBuf;
    size_t fileSize;

    // get size of contents
    fseek(src, 0, SEEK_END);
    fileSize = ftell(src);
    
    // use size of contents
    dest.reserve(fileSize);
    fileBuf = new char[fileSize];

    // get contents
    rewind(src);
    fread(fileBuf, sizeof(char), fileSize, src);

    // assign contents and cleanup
    dest.assign(fileBuf);
    delete[] fileBuf;
}

Shader::Shader()
{
    anyErrors = false;
    typeFlags = 0;
    compiled = false;
    vertShaderHandle = 0;
    geomShaderHandle = 0;
    tessShaderHandle = 0;
    fragShaderHandle = 0;
}

Shader::Shader(std::string vert_filename, std::string frag_filename)
{
    FILE* inFile;

    inFile = fopen(vert_filename.c_str(), "rb");
    if (inFile == NULL) {
        std::cout << "Vertex shader file doesn't exist.\n";
        anyErrors = true;
        return;
    }
    readFile(vertSource, inFile);

    inFile = fopen(frag_filename.c_str(), "rb");
    if (inFile == NULL) {
        std::cout << "Fragment shader file doesn't exist.\n";
        anyErrors = true;
        return;
    }
    readFile(fragSource, inFile);
    fclose(inFile);

    typeFlags = VERT_SHADER | FRAG_SHADER;
    compiled = false;
    vertShaderHandle = 0;
    geomShaderHandle = 0;
    tessShaderHandle = 0;
    fragShaderHandle = 0;
}

int32_t Shader::addShader(std::string src_filename, shader_type type)
{
    return -1;
}

GLuint Shader::getShaderProgram()
{
    if (compiled == false) {
        std::cout << "Shader hasn't been compiled yet\n";
        anyErrors = true;
        return 0;
    }

    return shaderProgHandle;
}

static bool isShaderStatusGood(GLint shaderHandle, GLenum statusType, char errorMsg[1024])
{
    GLint status;
    glGetShaderiv(shaderHandle, statusType, &status);
    if (status == GL_FALSE) {
        GLint length;
        glGetShaderiv(shaderHandle, GL_INFO_LOG_LENGTH, &length);
        std::cout << "Shader compile error is " << length << " chars long.\n";
        glGetShaderInfoLog(shaderHandle, length, NULL, errorMsg);
        std::cout << "glCompileShader failed: \n" << errorMsg << "\n";
        return false;
    } else {
        return true;
    }
}

bool Shader::compile()
{
    char buffer[1024];

    // Vertex shader
    vertShaderHandle = glCreateShader(GL_VERTEX_SHADER);
    const GLchar* vs = vertSource.c_str();
    const GLint vsLen = vertSource.length();
    glShaderSource(vertShaderHandle, 1, &vs, &vsLen);
    glCompileShader(vertShaderHandle);

    // check vertex shader compile status
    if (!isShaderStatusGood(vertShaderHandle, GL_COMPILE_STATUS, buffer)) {
        anyErrors = true;
        //std::cout << buffer << std::endl;
        return false;
    }

    // geometry shader
    // tesselation shader

    // fragment shader
    fragShaderHandle = glCreateShader(GL_FRAGMENT_SHADER);
    const GLchar* fs = fragSource.c_str();
    const GLint fsLen = fragSource.length();
    glShaderSource(fragShaderHandle, 1, &fs, &fsLen);
    glCompileShader(fragShaderHandle);

    // check vertex shader compile status
    if (!isShaderStatusGood(fragShaderHandle, GL_COMPILE_STATUS, buffer)) {
        anyErrors = true;
        //std::cout << buffer << std::endl;
        return false;
    }

    // create and link full pipeline
    shaderProgHandle = glCreateProgram();
    glAttachShader(shaderProgHandle, vertShaderHandle);
    if (typeFlags & GEOM_SHADER) {
        glAttachShader(shaderProgHandle, geomShaderHandle);
    }
    if (typeFlags & TESS_SHADER) {
        glAttachShader(shaderProgHandle, tessShaderHandle);
    }
    glAttachShader(shaderProgHandle, fragShaderHandle);
    glLinkProgram(shaderProgHandle);
    // remember that a call to glUseProgram will still need to be done

    // check program link status
    /*if (!isShaderProgramStatusGood(shaderProgHandle, GL_LINK_STATUS, buffer)) {
        anyErrors = true;
        std::cout << buffer << std::endl;
        return false;
    }*/

    GLint status;
    glGetProgramiv(shaderProgHandle, GL_LINK_STATUS, &status);
    if (status == GL_FALSE) {
        GLint length;
        glGetProgramiv(shaderProgHandle, GL_INFO_LOG_LENGTH, &length);
        GLchar *info = new GLchar[length];
        glGetProgramInfoLog(shaderProgHandle, length, NULL, info);
        std::cout << "glLinkProgram failed: \n" << info << std::endl;
        delete [] info;
    }
    //glDetachShader(shaderProgHandle, vertShaderHandle);
    //glDetachShader(shaderProgHandle, fragShaderHandle);
    //glDeleteShader(vertShaderHandle);
    //glDeleteShader(fragShaderHandle);

    compiled = true;
    return true;
}
{'fraction_non_alphanumeric': 0.1139358721593857, 'fraction_numerical': 0.004980803154508664, 'mean_word_length': 3.049579831932773, 'pattern_counts': {'":': 0, '<': 81, '<?xml version=': 0, '>': 9, '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': '5355707', 'n_tokens_mistral': 3088, 'n_tokens_neox': 3010, 'n_words': 731}