Advanced GLSL shader runs fine on AMD but crashes on nVidia

I am working to an OpenGL graphic engine which can render transparent meshes with accuracy. The full project is available here; it is a Visual Studio 2017 solution (you may need to install the Windows 10 SDK) but also includes a makefile to build under Linux. It uses linked lists of fragments on the GPU which are then sorted and blended back-to-front.

The problem is that everything works fine on my notebook with discrete AMD graphics, but crashes on my other nVidia PC (GT 740, very cheap). It crashes both on Windows and Linux and both with the official driver and the open source stack. The code that cause this is inside the sorting shader (please see the TODO comment):

int listBegin = listsHeads[getPixelBufferIndex()];

if(listBegin != -1)
{
    // Sort linked list using bubble sort
    bool swapped;
    do
    {
        // Start at list head
        swapped = false;
        int previousNode = listBegin;
        int currentNode = listsNodes[listBegin].nextNode;

        // Loop until list end
        while(currentNode != -1)
        {
            // Furthest first
            float previousDepth = listsNodes[previousNode].depth;
            float currentDepth = listsNodes[currentNode].depth;

            // TODO fix crash on nVidia
            if(previousDepth < currentDepth)
            {
                swapped = true;
                FragmentNode temp;
                temp.color = listsNodes[currentNode].color;
                temp.meshId = listsNodes[currentNode].meshId;

                listsNodes[currentNode].color = listsNodes[previousNode].color;
                listsNodes[currentNode].depth = previousDepth;
                listsNodes[currentNode].meshId = listsNodes[previousNode].meshId;

                listsNodes[previousNode].color = temp.color;
                listsNodes[previousNode].depth = currentDepth;
                listsNodes[previousNode].meshId = temp.meshId;
            }
            previousNode = currentNode;
            currentNode = listsNodes[currentNode].nextNode;                 
        }
    }
    while(swapped);
}

If I remove the inner if the program no longer crashes, but of course the result is incorrect since the lists are not sorted. I am starting to think that there is a problem with my particular graphic card model, because crashing both with the binary driver and the open source stack on the same card seems to suggests that. But maybe there is actually a bug in the shader that I couldn’t find.

Any idea?

Hi TheBrokenGod,

Could you post the call stack trace where shows the last few calls in your program and the first entry point of crashing dll (Windows)?

I replaced bubble sort with selection sort and now the program runs fine; however I couldn’t find any bugs in my previous code (there may be one, though). Here is the exception:

Unhandled exception at 0x00000000641FEE68 (nvoglv64.dll) in OmegaEngine.exe: Fatal program exit requested. occurred

and the stack trace from VS:

nvoglv64.dll!00000000641fee68()	
nvoglv64.dll!0000000063e2326d()	
nvoglv64.dll!0000000063e6f216()	
nvoglv64.dll!0000000063e6f26e()	
nvoglv64.dll!0000000063f6d9eb()	
nvoglv64.dll!0000000063f6d832()	
nvoglv64.dll!0000000063f70b81()	
nvoglv64.dll!0000000063f1e02a()	
nvoglv64.dll!0000000063e5810c()	
nvoglv64.dll!0000000063e302e0()	
nvoglv64.dll!0000000063e30475()	
nvoglv64.dll!0000000063e30274()	
nvoglv64.dll!0000000063e2fd66()	
nvoglv64.dll!0000000063e19d9a()	
opengl32.dll!00007ff9785ba697()	
gdi32full.dll!00007ff9b6d5ea7e() 
OmegaEngine.exe!omega::Engine::render() Line 285	C++
OmegaEngine.exe!omega::Engine::mainLoop() Line 224	C++
OmegaEngine.exe!main(int argc, char * * argv) Line 101	C++
[External Code]