LuisaRender icon indicating copy to clipboard operation
LuisaRender copied to clipboard

Would you like to provide integration to QT6 (QWindow)? Thanks!

Open zhangfq-chemistry opened this issue 1 year ago • 14 comments

zhangfq-chemistry avatar Dec 21 '24 10:12 zhangfq-chemistry

Hi, @zhangfq-chemistry

Sorry for the late reply (I was too busy and had sorrily forgotten this issue).

We already have an ImGUI display here. If you just want to interactively view the rendering, it might be useful.

If you prefer integration with an existing Qt app, it can be done by implementing a custom widget and presenting LuisaCompute images to its window surface, as demonstrated here.

Another choice is to combine the existing ImGUI display with a Qt widget, but this might require some modifications to the LuisaCompute code as it's using GLFW for window management by default.

Mike-Leo-Smith avatar Jan 13 '25 05:01 Mike-Leo-Smith

Thanks a lot! Is there one way to subclass directly from QWindow instead of QWidget ? The QWidget encapsulates a lots and runs very slowly. In addition, I cannot find exe of test_swapchain_qt.cpp after compiling.

zhangfq-chemistry avatar Jan 16 '25 10:01 zhangfq-chemistry

Thanks a lot! Is there one way to subclass directly from QWindow instead of QWidget ? The QWidget encapsulates a lots and runs very slowly. In addition, I cannot find exe of test_swapchain_qt.cpp after compiling.

I guess it would work with QWindow but I need to check it for sure.

Did you see other test_*.exe? It might be because LuisaRender disables tests, or CMake failed to find the Qt package.

Mike-Leo-Smith avatar Jan 17 '25 03:01 Mike-Leo-Smith

There are a lot of test_*.exe including test_swapchain_static and test_swapchain_static. And I cannot find test_swapchain_qt. Would you like to add it to tests? Thanks!

zhangfq-chemistry avatar Jan 17 '25 03:01 zhangfq-chemistry

There are a lot of test_*.exe including test_swapchain_static and test_swapchain_static. And I cannot find test_swapchain_qt. Would you like to add it to tests? Thanks!

test_swapchain_qt is turned on by default in the tests. If it's not compiled, then CMake might have failed to find the Qt libraries. Would you please post the CMake configuration log?

Mike-Leo-Smith avatar Jan 17 '25 05:01 Mike-Leo-Smith

home/zhangfq/engine/LuisaRender/src/compute/src/gui/../ext/imgui/imgui_internal.h:112:2: error: Please '#define IMGUI_DEFINE_MATH_OPERATORS' BEFORE including imgui.h! #error Please '#define IMGUI_DEFINE_MATH_OPERATORS' BEFORE including imgui.h! ^ 1 error generated.

zhangfq-chemistry avatar Feb 11 '25 05:02 zhangfq-chemistry

I have just added this macro definition in LuisaCompute. Please

git pull && git submodule update --recursive

and compile the project again.

Mike-Leo-Smith avatar Feb 11 '25 07:02 Mike-Leo-Smith

imgui ok! How to build tests/test_swapchain_qt.cpp? I cannot find any information about qt with cmake-gui.

zhangfq-chemistry avatar Mar 30 '25 03:03 zhangfq-chemistry

Hi, @zhangfq-chemistry

Qt is automatically detected by CMake's find_package and test_swapchain_qt will be built if Qt is found: https://github.com/LuisaGroup/LuisaCompute/blob/adbd67effe5b3ede6c97efa40b65ad92f6194dc6/src/tests/CMakeLists.txt#L182-L196

Did you see any configuration warnings related to Qt?

Mike-Leo-Smith avatar Mar 30 '25 16:03 Mike-Leo-Smith

OK now! I want to use LuisaRender to replace QVulkanWindow/QVulkanWindowRenderer. Would you like to provide some examples including instancing(a lot of atoms/spheres, bonds/cylinders, lines), mouse-picking? In addition, the class Canvas : public QWidget, in which QWidget is inefficient than QWindow. I tried but failed. Please build one for me. Thanks a lot!

zhangfq-chemistry avatar Mar 31 '25 10:03 zhangfq-chemistry

Hi, you can use the following code to create a QWindow, bind a swap chain to it, and embed it into another widget:

QWindow canvas;
auto swapchain = device.create_swapchain(
    stream,
    SwapchainOption{
        .display = 0u,
        .window = static_cast<uint64_t>(canvas.winId()),
        .size = make_uint2(width, height),
        .wants_hdr = false,
        .wants_vsync = false,
        .back_buffer_count = 2,
    });

auto canvas_widget = QWidget::createWindowContainer(&canvas);
// Now you can embed canvas_widget into other Qt widgets

Would you like to provide some examples including instancing(a lot of atoms/spheres, bonds/cylinders, lines), mouse-picking?

Currently, we support rendering triangle meshes, curves, and procedural geometry with AABB. You'll be able to find out how to render them in the linked examples.

We currently don't have a built-in tool for mouse picking. You might need to render an object ID map and download the chosen pixel to find which object is picked.

Feel free to ask me anything if you still have questions.

Mike-Leo-Smith avatar Apr 01 '25 18:04 Mike-Leo-Smith

I have updated the Qt integration example to show how to directly interact with a QWindow: https://github.com/LuisaGroup/LuisaCompute/blob/next/src/tests/test_swapchain_qt.cpp

Mike-Leo-Smith avatar Apr 02 '25 07:04 Mike-Leo-Smith

Dear Sir, please help me to build following codes:

int main(int argc, char *argv[]) {

Context context{argv[0]};

if (argc <= 1) {
    LUISA_INFO("Usage: {} <backend>. <backend>: cuda, dx, cpu, metal", argv[0]);
    exit(1);
}
Device device = context.create_device(argv[1]);
static constexpr auto width = 1280u;
static constexpr auto height = 720u;
static constexpr auto resolution = make_uint2(width, height);

auto draw = device.compile<2>([](ImageFloat image, Float time) noexcept {
    auto p = dispatch_id().xy();
    auto uv = make_float2(p) / make_float2(resolution) * 2.0f - 1.0f;
    auto color = def(make_float4());
    Constant<float> scales{pi, luisa::exp(1.f), luisa::sqrt(2.f)};
    for (auto i = 0u; i < 3u; i++) {
        color[i] = cos(time * scales[i] + uv.y * 11.f +
                       sin(-time * scales[2u - i] + uv.x * 7.f) * 4.f) *
                       .5f +
                   .5f;
    }
    color[3] = 1.0f;
    image.write(p, color);
});

Stream stream = device.create_stream(StreamTag::GRAPHICS);
auto image = device.create_image<float>(PixelStorage::BYTE4, resolution);

QApplication app{argc, argv};
QMainWindow window;
window.setFixedSize(width, height);
window.setWindowTitle("Display");
window.setAutoFillBackground(true);


QWidget overlay{&window};
overlay.setFixedSize(window.contentsRect().size() / 2);
overlay.move(window.contentsRect().center() - overlay.rect().center());
overlay.setAutoFillBackground(true);

QPushButton button{"Quit", &overlay};
button.move(overlay.contentsRect().center() - button.rect().center());
QObject::connect(&button, &QPushButton::clicked, [&] {
    window.setVisible(false);
});


QWindow _canvas;
auto swapchain = device.create_swapchain(
    stream,
    SwapchainOption{
        .display = 0u,
        .window = static_cast<uint64_t>(_canvas.winId()),
        .size = make_uint2(width, height),
        .wants_hdr = false,
        .wants_vsync = false,
        .back_buffer_count = 2,
    });

auto Canvas = QWidget::createWindowContainer(&_canvas);

window.show();

Clock clk;
Framerate framerate;
while (window.isVisible()) {
    QApplication::processEvents();
    auto time = static_cast<float>(clk.toc() * 1e-3);
    stream << draw(image, time).dispatch(resolution)
           << swapchain.present(image);
    framerate.record();
    auto title = luisa::format("Display - {:.2f} fps", framerate.report());
    window.setWindowTitle(title.c_str());
}

stream << synchronize();
QApplication::quit();

}

zhangfq-chemistry avatar Apr 05 '25 04:04 zhangfq-chemistry

I see two problems in your code:

  1. After auto Canvas = QWidget::createWindowContainer(&_canvas); you need to add it to a parent widget. Or, you can specify its parent upon creation:

    auto Canvas = QWidget::createWindowContainer(&_canvas, &window);
    
  2. You need to tell overlay to use a native window, otherwise it might not be drawn above _canvas:

    QWidget overlay{&window};
    overlay.setAttribute(Qt::WA_NativeWindow);// Note: this is required for overlay to be displayed above the canvas
    

Mike-Leo-Smith avatar Apr 05 '25 06:04 Mike-Leo-Smith