Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for WebAssembly
  4. Enabling QOpenGLContext::supportsThreadedOpenGL() WebAssembly Kit in Qt 6.6.3
QtWS25 Last Chance

Enabling QOpenGLContext::supportsThreadedOpenGL() WebAssembly Kit in Qt 6.6.3

Scheduled Pinned Locked Moved Unsolved Qt for WebAssembly
qopenglmultithreadingqopenglbuffer
3 Posts 3 Posters 376 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    MasterWork
    wrote on last edited by
    #1

    Okay, so I am currently writing, or at least trying to write a clothing simulator in Qt for WebAssembly.
    I was able to compile my project with the Qt 6.6.3 WebAssembly (multi-threaded) Kit and wrote some Code to import Meshes to my Project and Display them in the QOpenGLWidget.

    Then i wanted to approach the physics simulation part. I am working with MVC pattern and my PhysicsEngine class processes the mesh and then updates my Model. The Model handles the meshes and updates them. The Model is also the Publisher and then sends the updated Model-Data to all Observers including the PhysicsEngine.

    Now as soon, as my QtConcurrent-Thread calls the Models method to update the meshes i get an error:
    worker.js onmessage() captured an uncaught exception: RuntimeError: operation does not support unaligned accesses

    First i thought this is due to me not doing correct data handling, but i found out when calling
    QOpenGLContext::supportsThreadedOpenGL()
    i get false.

    So as soon as my thread, which calls the model, which calls the mesh and then proceeds to execute the code on my QOpenGLBuffer m_arrayBuffer i get the unaligned acces error.

    So in short my Question is:
    Is there a way to enable threaded OpenGl with the WebAssembly Kit? Or should i try to handle the update of data otherwise?

    This is the code
    PhysicsEngine.cpp

    
    void PhysicsEngine::startSimulation()
    {
        m_isRunning = true;
        QFuture<void> future = QtConcurrent::run([this]() {
            while (m_isRunning) {
                // Simulate physics here
                simulationLoop();
                // Emit signal for each simulation step completed
                emit simulationStepComplete();
                // Sleep or add appropriate delay here
                QThread::msleep(1000);
            }
        });
    }
    
    void PhysicsEngine::simulationStepComplete()
    {
        {
            QMutexLocker locker(&m_mutex);
            //m_meshVectorNew = m_meshVector;
            if(m_meshVectorNew.size() > 0){
                std::cout << "Update Model!" << std::endl;
                m_model->updateMeshes(m_meshVectorNew);
            }
        }
    }
    

    Model.cpp

    //Use the Meshes update Function to update all meshes
    void Model::updateMeshes(QVector<Mesh> meshVector)
    {
        for(int i = 0; i < meshVector.size(); i++)
        {
            std::cout << "Updated Mesh " << i << std::endl;
            m_meshVector[i].updateVertices(meshVector[i].getVertices());
        }
        notify();
    }
    

    Mesh.cpp

    void Mesh::updateVertices(QVector<VertexData> vertices)
    {
        for(int i = 0; i < vertices.size() ; i++)
        {
            VertexData vertexNew;
            QVector3D position (vertices[i].position.x(),vertices[i].position.y(),vertices[i].position.z());
            QVector3D normal (vertices[i].normal.x(),vertices[i].normal.y(),vertices[i].normal.z());
            QVector2D texCoords (vertices[i].texCoords.x(),vertices[i].texCoords.y());
            vertexNew.position = position;
            vertexNew.normal = normal;
            vertexNew.texCoords = texCoords;
            m_vertices[i] = vertexNew;
        }
        updateVBOQt();
    }
    
    void Mesh::updateVBOQt()
    {
        initializeOpenGLFunctions();
    
        if (!m_arrayBuffer.bind()) {
            std::cout << "Failed to bind buffer" << std::endl;
            return;
        }
        // Allocate new memory for changed VertexData
        m_arrayBuffer.allocate(m_vertices.size() * sizeof(VertexData));
    
        // Write the changes
        m_arrayBuffer.write(0, m_vertices.constData(), m_vertices.size() * sizeof(VertexData));
    
        // Unbind the buffer
        m_arrayBuffer.release();
    
    }
    

    And the complete error

    Clothing-Simulator.worker.js:64 worker.js onmessage() captured an uncaught exception: RuntimeError: operation does not support unaligned accesses
    threadPrintErr @ Clothing-Simulator.worker.js:64
    Clothing-Simulator.worker.js:64 RuntimeError: operation does not support unaligned accesses
        at QRecursiveMutex::tryLock(QDeadlineTimer) (http://localhost:30000/Clothing-Simulator.wasm:wasm-function[6031]:0x1a1439)
        at QOpenGLSharedResource::QOpenGLSharedResource(QOpenGLContextGroup*) (http://localhost:30000/Clothing-Simulator.wasm:wasm-function[13573]:0x478d60)
        at QOpenGLFunctionsPrivateEx* QOpenGLMultiGroupSharedResource::value<QOpenGLFunctionsPrivateEx>(QOpenGLContext*) (http://localhost:30000/Clothing-Simulator.wasm:wasm-function[13661]:0x47df69)
        at QOpenGLFunctions::initializeOpenGLFunctions() (http://localhost:30000/Clothing-Simulator.wasm:wasm-function[13670]:0x480961)
        at Mesh::updateVBOQt() (http://localhost:30000/Clothing-Simulator.wasm:wasm-function[1039]:0x320e7)
        at Mesh::updateVertices(QList<VertexData>) (http://localhost:30000/Clothing-Simulator.wasm:wasm-function[1043]:0x326d6)
        at Model::updateMeshes(QList<Mesh>) (http://localhost:30000/Clothing-Simulator.wasm:wasm-function[552]:0x1cf94)
        at PhysicsEngine::simulationStepComplete() (http://localhost:30000/Clothing-Simulator.wasm:wasm-function[917]:0x2dc23)
        at PhysicsEngine::startSimulation()::$_0::operator()() const (http://localhost:30000/Clothing-Simulator.wasm:wasm-function[961]:0x2eef6)
        at decltype(std::declval<PhysicsEngine::startSimulation()::$_0&>()()) std::__2::__invoke[abi:v15007]<PhysicsEngine::startSimulation()::$_0&>(PhysicsEngine::startSimulation()::$_0&) (http://localhost:30000/Clothing-Simulator.wasm:wasm-function[960]:0x2ee81)
    threadPrintErr @ Clothing-Simulator.worker.js:64
    Clothing-Simulator.js:3578 Pthread 0x00e037b0 sent an error! http://localhost:30000/Clothing-Simulator.worker.js:198: Uncaught RuntimeError: operation does not support unaligned accesses
    
    1 Reply Last reply
    1
    • lorn.potterL Offline
      lorn.potterL Offline
      lorn.potter
      wrote on last edited by lorn.potter
      #2

      WebGL does not work in web workers (threads), so no, unless things change there, it will never work. You cannot block on the main thread.

      Freelance Software Engineer, Platform Maintainer QtWebAssembly, Maintainer QtSensors
      Author, Hands-On Mobile and Embedded Development with Qt 5 http://bit.ly/HandsOnMobileEmbedded

      1 Reply Last reply
      0
      • S Offline
        S Offline
        SamiV123
        wrote on last edited by
        #3

        With normal caveats regarding "threads" (web workers in web parlance) you could just run your data simulation in a thread and then use the main thread to update your VBO and render.

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved