Crash with a scene with a shader

Hi there!

I’m working on integrating the Cycles renderer with my own application, which is built using Qt and OpenGL.
I’ve got some reasonable progress on it, but I’m having crashes when I try to add materials/shaders to my geometry. The scene renders correctly with just geometry, but when I create a shader with a shader graph, I now have random crashes. The crashes seem to happen in the OpenGL part of my application, so I’m guessing that the OpenGL state is being disturbed by Cycles in some way, but since I’m using a CPU device, I don’t know if that is actually the cause…

Has anybody had any issues with this and/or can shed some light into this issue?

The shader I’m using is pretty simple:

void CyclesRender::set_shader_pbr(ccl::Mesh* mesh, const std::string& albedo_name)
{
    // Create shader graph
    ShaderGraph* shader_graph = new ShaderGraph();
    auto         output_node = shader_graph->output();

    auto bsdf_node = new PrincipledBsdfNode();
    shader_graph->add(bsdf_node);

    if (albedo_name != "")
    {
        ImageTextureNode* itn = new ImageTextureNode();
        itn->set_filename(ustring(albedo_name));

        shader_graph->connect(itn->output("Color"), bsdf_node->input("Base Color"));

        shader_graph->add(itn);
    }

    // Create connections
    shader_graph->connect(bsdf_node->output("BSDF"), output_node->input("Surface"));

    // Create shader
    Shader* shader = new Shader();
    shader->set_graph(shader_graph);
    _scene->shaders.push_back(shader);

    // Set shader to material
    array<Node*> used_shaders = mesh->get_used_shaders();
    used_shaders.push_back_slow(shader);
    mesh->set_used_shaders(used_shaders);
 }

Thanks in advance!
Diogo

For anybody that runs into this problem in the future:

This happened because I create the shader like:

Shader* shader = new Shader();

The correct way of creating the shader is to use:

scene->create_node();

Which initializes some other structures and sets the ownership of the object.

The standalone application code also uses new Shader, instead of create_node, that’s why I couldn’t figure it out.

This also applies to other objects like Mesh, Object, etc…

Short version: use the scene as a factory for all Node-based objects.

Best regards,
Diogo

1 Like