getting Access violation writing location when calling glDrawElements caused by shader

36 Views Asked by At

I am trying to write a program that compiles a shader with shaderc so I can load the binaries instead of compiling the shader each time i run the program. I get no errors when loading or compiling my shader but when I try to call glDrawElements I get the error Access violation writing location.

I tried using the same shader just compiled normally and that worked fine which is why I think it has something to do with my shader.

the code to compile my shader is:

std::unordered_map<unsigned int, std::vector<uint32_t>>& shaderData = OpenGLSPIRV;
shaderData.clear();

std::filesystem::path cacheDirectory = "assets/cache/shader/opengl";  
for (auto&& [stage, source] : shaderSources) 
{    std::filesystem::path shaderFilePath = FilePath;   
std::filesystem::path cachedPath = cacheDirectory / (shaderFilePath.filename().string() + Utils::GLShaderStageCachedOpenGLFileExtension(stage));
 std::ifstream in(cachedPath, std::ios::in | std::ios::binary);     
if (in.is_open()){  
    in.seekg(0, std::ios::end);     
    auto size = in.tellg();
    in.seekg(0, std::ios::beg); 
    auto& data = shaderData[stage];
    data.resize(size / sizeof(uint32_t));
    in.read((char*)data.data(), size);
 } else{
        shaderc::Compiler compiler;
    shaderc::CompileOptions options;
    options.SetTargetEnvironment(shaderc_target_env_opengl, shaderc_env_version_opengl_4_5);
    options.SetWarningsAsErrors();
    options.SetGenerateDebugInfo();
    options.SetAutoMapLocations(true);
        shaderc::SpvCompilationResult module = compiler.CompileGlslToSpv(source, Utils::GLShaderStageToShaderC(stage), FilePath.c_str(), options);
    if (module.GetCompilationStatus() != shaderc_compilation_status_success){
        AE_ASSERT(false, module.GetErrorMessage()); 
    }
        shaderData[stage] = std::vector<uint32_t>(module.cbegin(), module.cend());
    std::ofstream out(cachedPath, std::ios::out | std::ios::binary);
    if (out.is_open()){
        auto& data = shaderData[stage];
        out.write((char*)data.data(), data.size() * sizeof(uint32_t));
        out.flush();
        out.close();
    }                               

the code to load my shader is:

GLuint program = glCreateProgram();
        std::vector<GLuint> shaderIDs;

        for (auto&& [stage, spirv] : OpenGLSPIRV) {
            GLuint shaderID = glCreateShader(stage);
            glShaderBinary(1, &shaderID, GL_SHADER_BINARY_FORMAT_SPIR_V, spirv.data(), spirv.size() * sizeof(uint32_t));
            glSpecializeShader(shaderID, "main", 0, nullptr, nullptr);

            GLint isCompiled = 0;
            glGetShaderiv(shaderID, GL_COMPILE_STATUS, &isCompiled);
            if (isCompiled == GL_FALSE) {
                GLint maxInfoLength = 0;
                glGetShaderiv(shaderID, GL_INFO_LOG_LENGTH, &maxInfoLength);
                if (maxInfoLength > 0)
                {
                    std::vector<GLchar> infoLog(maxInfoLength);
                    glGetShaderInfoLog(shaderID, maxInfoLength, &maxInfoLength, &infoLog[0]);
                    AE_CORE_ERROR("Shader Compilation failed ({0}):\n{1} [-]", FilePath, &infoLog[0]);

                }

            }


            glAttachShader(program, shaderID);
            shaderIDs.emplace_back(shaderID);
        }
        glLinkProgram(program);

        GLint isLinked = 0;
        glGetProgramiv(program, GL_LINK_STATUS, &isLinked);
        if (isLinked == GL_FALSE) {
            GLint maxLength;
            glGetProgramiv(program, GL_INFO_LOG_LENGTH, &maxLength);

            std::vector<GLchar> infoLog(maxLength);
            glGetProgramInfoLog(program, maxLength, &maxLength, infoLog.data());
            AE_CORE_ERROR("Shader linking failed ({0}):\n{1}", FilePath, infoLog.data());

            glDeleteProgram(program);


            for (auto id : shaderIDs)
                glDeleteShader(id);
        }

        for (auto id : shaderIDs) {
            glDetachShader(program, id);
        }
        RendererID = program;

here is my shader:

Vertex:

layout(binding = 0, std140) uniform Camera
{
    mat4 u_ViewProjection;
} _19;

struct Transform
{
    mat4 u_Transform;
};

uniform Transform u_RendererUniforms;

layout(location = 0) in vec3 position;

void main()
{
    gl_Position = (_19.u_ViewProjection * u_RendererUniforms.u_Transform) * vec4(position, 1.0);
}

Fragment:

#version 450

struct Material
{
    vec4 u_Color;
    vec4 u_Color2;
};

uniform Material m_MaterialUniforms;

layout(location = 0) out vec4 color;

void main()
{
    color = m_MaterialUniforms.u_Color * m_MaterialUniforms.u_Color2;
}

How can I start to debug this.

0

There are 0 best solutions below