Enabling QOpenGLContext::supportsThreadedOpenGL() WebAssembly Kit in Qt 6.6.3
-
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 accessesFirst 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.cppvoid 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
-
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.