Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Nvidia driver crash at draw call  (Read 7423 times)

0 Members and 5 Guests are viewing this topic.

sharethis

  • Jr. Member
  • **
  • Posts: 65
    • View Profile
Nvidia driver crash at draw call
« on: February 25, 2013, 12:21:39 pm »
I use SFML 2.0 RC for my application and I love it. Some days ago I changed my video card from ATI HD 7870 to NVIDIA GTX 660. From that point on my application stopped working properly.

It crashes at the draw call "glDrawElements" with the following message.
Quote
Unhandled exception at 0x5D9F74E3 (nvoglv32.dll) in Application.exe: 0xC0000005: Access violation reading location 0x00000000.

The first assumption was that there already was a bug in my application the ATI driver tolerated and the new NVIDIA driver does not. In the end of two whole days of debugging I decided to write a new minimal renderer using minimal shaders. But the problem still exists.

There are a few things I want to mention. I use OpenGL version 3.3 with my own shaders and a SFML::RenderWindow for drawing. There are neither compiler errors nor OpenGL errors at runtime. The application simply crashes at the draw call. Other OpenGL applications like third-party games are working on my system. I am using Windows 8 64 and Visual Studio 11 and built static SFML libraries myself. My video card driver is the lastest stable one but I also tried older ones with no success.
« Last Edit: February 25, 2013, 02:35:26 pm by sharethis »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
AW: Nvidia driver crash at draw call
« Reply #1 on: February 25, 2013, 12:54:47 pm »
Can you please provide a complete and minimal example that reproduces thd error?

Also do normal SFML applications work (without your specific OpenGL code)?
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Nvidia driver crash at draw call
« Reply #2 on: February 25, 2013, 12:56:23 pm »
Do you also draw SFML entities, or do you only use OpenGL?
Laurent Gomila - SFML developer

sharethis

  • Jr. Member
  • **
  • Posts: 65
    • View Profile
Re: Nvidia driver crash at draw call
« Reply #3 on: February 25, 2013, 01:28:22 pm »
Can you please provide a complete and minimal example that reproduces thd error?

Yes, I will do it later this day.

Also do normal SFML applications work (without your specific OpenGL code)?
Do you also draw SFML entities, or do you only use OpenGL?

I used SFML text drawing on top of the OpenGL scene. For debugging I disabled that but it still works.

auto wnd = Global->Get<RenderWindow>("window");
wnd->pushGLStates();
               
Text text(string, font, textsize);
wnd->draw(text);

wnd->popGLStates();

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Nvidia driver crash at draw call
« Reply #4 on: February 25, 2013, 01:47:52 pm »
Quote
For debugging I disabled that but it still works.
What do you mean? If you don't draw SFML stuff it still crashes?
Laurent Gomila - SFML developer

sharethis

  • Jr. Member
  • **
  • Posts: 65
    • View Profile
Re: Nvidia driver crash at draw call
« Reply #5 on: February 25, 2013, 02:35:05 pm »
What do you mean? If you don't draw SFML stuff it still crashes?

Only SFML drawing works, but only OpenGL drawing crashes and both combined crashes, too.

Yes, I will do it later this day.

Here is a minimal example. Window creation and Glew initialization are successful and there are no OpenGL errors.

main.cpp
#pragma once

#include <string>
#include <iostream>
#include <fstream>
using namespace std;
#include <GLEW/glew.h>
#include <SFML/OpenGL.hpp>
#include <SFML/Graphics/RenderWindow.hpp>
using namespace sf;
#include <GLM/glm.hpp>
#include <GLM/gtc/matrix_transform.hpp>
#include <GLM/gtc/type_ptr.hpp>
using namespace glm;

int main()
{
        // window
        RenderWindow window(VideoMode(1024, 768), "Window Title");
        window.resetGLStates();
        window.setVerticalSyncEnabled(true);
        cout << "Window creation " << (window.isOpen() ? "success" : "fail") << endl;

        // setup
        auto result = glewInit();
        cout << "Glew initialization " << (result == GLEW_OK ? "success" : "fail") << endl;

        glViewport(0, 0, 1024, 768);
        glClearColor(.4f,.6f,.9f,0.f);
        glEnable(GL_DEPTH_TEST);
        glDepthFunc(GL_LESS);
        glEnable(GL_CULL_FACE);

        // shader
        GLuint vertex = glCreateShader(GL_VERTEX_SHADER);
        string vertex_string((istreambuf_iterator<char>(ifstream("shaders/basic.vert"))), istreambuf_iterator<char>());
        const GLchar* vertex_chars = vertex_string.c_str();
        glShaderSource(vertex, 1, &vertex_chars, NULL);
        glCompileShader(vertex);

        GLuint fragment = glCreateShader(GL_FRAGMENT_SHADER);
        string fragment_string((istreambuf_iterator<char>(ifstream("shaders/basic.frag"))), istreambuf_iterator<char>());
        const GLchar* fragment_chars = fragment_string.c_str();
        glShaderSource(fragment, 1, &fragment_chars, NULL);
        glCompileShader(fragment);

        GLuint program = glCreateProgram();
        glAttachShader(program, vertex);
        glAttachShader(program, fragment);
        glLinkProgram(program);
        glUseProgram(program);

        mat4 Projection = perspective(45.f, 4.f / 3.f, 1.0f, 1000.f);
        glUniformMatrix4fv(glGetUniformLocation(program, "projection"), 1, GL_FALSE, value_ptr(Projection));

        mat4 View = lookAt(vec3(1.2f, 1.2f, 1.2f), vec3(0.0f, 0.0f, 0.0f), vec3(0.0f, 0.0f, 1.0f));
        glUniformMatrix4fv(glGetUniformLocation(program, "view"), 1, GL_FALSE, value_ptr(View));

        mat4 Model = translate(mat4(1), vec3(0)) * scale(mat4(1), vec3(1));
        glUniformMatrix4fv(glGetUniformLocation(program, "model"), 1, GL_FALSE, value_ptr(Model));

        // form
        const GLfloat VERTICES[]  = {-1.,-1.,1.,1.,-1.,1.,1.,1.,1.,-1.,1.,1.,-1.,1.,1.,1.,1.,1.,1.,1.,-1.,-1.,1.,-1.,1.,-1.,-1.,-1.,-1.,-1.,-1.,1.,-1.,1.,1.,-1.,-1.,-1.,-1.,1.,-1.,-1.,1.,-1.,1.,-1.,-1.,1.,-1.,-1.,-1.,-1.,-1.,1.,-1.,1.,1.,-1.,1.,-1.,1.,-1.,1.,1.,-1.,-1.,1.,1.,-1.,1.,1.,1.};
        const GLfloat NORMALS[]   = {0,0,1,0,0,1,0,0,1,0,0,1,0,1,0,0,1,0,0,1,0,0,1,0,0,0,-1,0,0,-1,0,0,-1,0,0,-1,0,-1,0,0,-1,0,0,-1,0,0,-1,0,-1,0,0,-1,0,0,-1,0,0,-1,0,0,1,0,0,1,0,0,1,0,0,1,0,0};
        const GLuint  ELEMENTS[]  = {0,1,2,2,3,0,4,5,6,6,7,4,8,9,10,10,11,8,12,13,14,14,15,12,16,17,18,18,19,16,20,21,22,22,23,20};

        GLuint Vertices, Normals, Elements;

        glGenBuffers(1, &Vertices);
        glBindBuffer(GL_ARRAY_BUFFER, Vertices);
        glBufferData(GL_ARRAY_BUFFER, 72 * sizeof(GLfloat), VERTICES, GL_STATIC_DRAW);
        glBindBuffer(GL_ARRAY_BUFFER, 0);

        glGenBuffers(1, &Normals);
        glBindBuffer(GL_ARRAY_BUFFER, Normals);
        glBufferData(GL_ARRAY_BUFFER, 72 * sizeof(GLfloat), NORMALS, GL_STATIC_DRAW);
        glBindBuffer(GL_ARRAY_BUFFER, 0);

        glGenBuffers(1, &Elements);
        glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, Elements);
        glBufferData(GL_ELEMENT_ARRAY_BUFFER, 36 * sizeof(GLuint), ELEMENTS, GL_STATIC_DRAW);
        glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);

        // draw
        while(window.isOpen())
        {
                glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

                glEnableVertexAttribArray(0);
                glBindBuffer(GL_ARRAY_BUFFER, Vertices);
                glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, (void*)0);

                glEnableVertexAttribArray(1);
                glBindBuffer(GL_ARRAY_BUFFER, Normals);
                glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, (void*)0);

                glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, Elements);

                glDrawElements(GL_TRIANGLES, 36, GL_UNSIGNED_INT, (void*)0);

                glDisableVertexAttribArray(0);
                glDisableVertexAttribArray(1);

                window.display();
        }
}

shaders/basic.vert
#version 330

layout(location = 0) in vec3 position;
layout(location = 1) in vec3 normal;

out vec3 fposition;
out vec3 fnormal;

uniform mat4 model;
uniform mat4 view;
uniform mat4 projection;

void main()
{
        mat4 mvp = projection * view * model;

        gl_Position = mvp * vec4(position, 1.0);

        fnormal = (view * transpose(inverse(model)) * vec4(normal, 0.0)).xyz;
}

shaders/basic.frag
#version 330 core

in vec3 fposition;
in vec3 fnormal;

out vec3 color;

void main()
{
        color = vec3(1.0, 1.0, 1.0);
}

This is the result.

Quote
Unhandled exception at 0x5E2174E3 (nvoglv32.dll) in Application.exe: 0xC0000005: Access violation reading location 0x00000000.

I think this issue is SFML related since third-party games perform correctly. I would like if you could help me.
« Last Edit: February 25, 2013, 02:49:02 pm by sharethis »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Nvidia driver crash at draw call
« Reply #6 on: February 25, 2013, 02:40:43 pm »
Can't you use your debugger to see where it crashes?

#include <string>
#include <iostream>
#include <fstream>
using namespace std;
#include <GLEW/glew.h>
#include <SFML/OpenGL.hpp>
#include <SFML/Graphics/RenderWindow.hpp>
using namespace sf;
#include <GLM/glm.hpp>
#include <GLM/gtc/matrix_transform.hpp>
#include <GLM/gtc/type_ptr.hpp>
using namespace glm;

:o
Never do that ('using namespace' before including a header).
Laurent Gomila - SFML developer

sharethis

  • Jr. Member
  • **
  • Posts: 65
    • View Profile
Re: Nvidia driver crash at draw call
« Reply #7 on: February 25, 2013, 02:44:17 pm »
It crashes at the draw call "glDrawElements" as mentioned in the thread title. I moved the "using namespace" declarations under the includes but that didn't solve the crash.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Nvidia driver crash at draw call
« Reply #8 on: February 25, 2013, 03:05:47 pm »
Internally, SFML binds buffers, and doesn't unbind it (to optimize performances), so you have to unbind everything that you don't use.

Quote
It crashes at the draw call "glDrawElements" as mentioned in the thread title
Sorry. Usually when people post the Windows error, it means that they didn't use their debugger to get more relevant informations ;)

Quote
I moved the "using namespace" declarations under the includes but that didn't solve the crash.
Of course not. It's just a good habit, which can help to avoid possible name conflicts, which can produce hard to understand compile errors.
Laurent Gomila - SFML developer

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Nvidia driver crash at draw call
« Reply #9 on: February 25, 2013, 03:26:14 pm »
An even better habit is to avoid using namespace at all, or only use it locally. But certainly not in header files. If using namespace were used everywhere, why would we have namespaces?

sharethis, you should also lookup the meaning of #pragma once, you use it wrongly.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

sharethis

  • Jr. Member
  • **
  • Posts: 65
    • View Profile
Re: Nvidia driver crash at draw call
« Reply #10 on: February 25, 2013, 03:26:48 pm »
I came up with a solution to fix the crash. :D

Even though I can't explain why, removing the "resetGLStates" function call solved the crash. Maybe it is because GLEW or something else isn't yet initialized that moment.

sf::RenderWindow window;
window.create(VideoMode(1024, 768), "Window Title");
window.resetGLStates(); // removing this line fixed the crash
window.setVerticalSyncEnabled(true);

By the way thanks to Nexus for your explanation about namespaces and pointing out the wrong use of "#pragma once". I do not know how it found its way in the source file.
« Last Edit: February 25, 2013, 03:30:32 pm by sharethis »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Nvidia driver crash at draw call
« Reply #11 on: February 25, 2013, 03:58:52 pm »
It fixes the crash because SFML doesn't set its internal OpenGL states. The error will pop up again when you'll draw text with SFML, so this "fix" works only on your minimal example.
Laurent Gomila - SFML developer

sharethis

  • Jr. Member
  • **
  • Posts: 65
    • View Profile
Re: Nvidia driver crash at draw call
« Reply #12 on: February 25, 2013, 06:38:53 pm »
It fixes the crash because SFML doesn't set its internal OpenGL states. The error will pop up again when you'll draw text with SFML, so this "fix" works only on your minimal example.

Thanks what I though, too. But text SFML drawing on top of the OpenGL scene works fine. Maybe it is because I pop the OpenGL states after text drawing.