[Interest] Qt3D: Correct way of using QBuffer and a compute shader
Konstantin Tokarev
annulen at yandex.ru
Tue Jan 16 19:47:41 CET 2018
16.01.2018, 20:29, "Esch Lorenz TU Ilmenau" <lorenz.esch at tu-ilmenau.de>:
> Dear Qt Community,
>
> I am writing my first application using a compute shader based on Qt3D in C++. My question: What is the correct way of updating the data needed by the compute shader during run-time? This is what I tried so far:
>
> I created a QBuffer object (in form of a QPointer) and fill it with data. The pointer is kept as a member in a class. I set the buffer via .data() to a QVariant and set it as data to the corresponding QParameter. The following is a code snippet showing the basic idea:
>
> QByteArray interpolationBufferData = buildInterpolationMatrixBuffer(matInterpolationMatrix); //Builds the data array from a large Eigen Matrix
>
> m_pInterpolationMatBuffer = new Qt3DRender::QBuffer(Qt3DRender::QBuffer::ShaderStorageBuffer);
>
> m_pInterpolationMatBuffer->setData(interpolationBufferData);
>
> m_pParameter ->setValue(QVariant::fromValue(m_pInterpolationMatBuffer.data()), QStringLiteral("InterpolationMat"));
>
> m_pInterpolationMatBuffer->setData(interpolationBufferData);
>
> Everything works fine until here. The visualization is doing what it is supposed to do and the compute shader works fine. After a new input matrix arrives I create a new QByteArray and try to load it to the QBuffer object via updateData (I also tried setData). Once I try to update the data in the QBuffer the application crashes.
>
> QByteArray interpolationBufferData = buildInterpolationMatrixBuffer(matInterpolationMatrix); //Builds the data array from an incoming Eigen Matrix
>
> m_pInterpolationMatBuffer->updateData(0, interpolationBufferData);
>
> Am I missing something? Is there a better way to update data used by the compute shader? Should I pay attention to the usage type (StreamDraw, StaticDraw, etc.)? Also, is the complete QByteArray internally copied to the GPU when setting only the pointer to the QVariant?
Yes. While integrated GPUs can use same physical RAM as CPU, data may need to be copied in the special range of physical addresses. And even if it isn't, OpenGL implementation may safely assume that you never attempt to modify buffer data behind it's back.
> Maybe I should create a new buffer every time I receive new data? Questions over questions 😊
>
> Thanks,
>
> Lorenz
>
> ,
>
> _______________________________________________
> Interest mailing list
> Interest at qt-project.org
> http://lists.qt-project.org/mailman/listinfo/interest
--Â
Regards,
Konstantin
More information about the Interest
mailing list