Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace glfw with SDL in anari_viewer #282

Draft
wants to merge 4 commits into
base: next_release
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions examples/python/anari_tutorial.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ def anari_status(device, source, sourceType, severity, code, message):
anariCommitParameters(device, mesh)

material = anariNewMaterial(device, 'matte')
anariSetParameter(device, material, 'color', ANARI_STRING, 'color')
anariCommitParameters(device, material)

surface = anariNewSurface(device)
Expand Down
2 changes: 1 addition & 1 deletion examples/viewer/SceneSelector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ static void buildUISceneHandle(
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////

SceneSelector::SceneSelector(const char *name) : Window(name, true)
SceneSelector::SceneSelector(Application *app, const char *name) : Window(app, name, true)
{
m_categories = anari::scenes::getAvailableSceneCategories();
m_scenes.resize(m_categories.size());
Expand Down
2 changes: 1 addition & 1 deletion examples/viewer/SceneSelector.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ using SceneSelectionCallback = std::function<void(const char *, const char *)>;

struct SceneSelector : public Window
{
SceneSelector(const char *name = "Scene");
SceneSelector(Application *app, const char *name = "Scene");
~SceneSelector();

void buildUI() override;
Expand Down
6 changes: 3 additions & 3 deletions examples/viewer/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,12 +107,12 @@ struct Application : public anari_viewer::Application
if (g_useDefaultLayout)
ImGui::LoadIniSettingsFromMemory(getDefaultUILayout());

auto *viewport = new anari_viewer::windows::Viewport(g_device, "Viewport");
auto *viewport = new anari_viewer::windows::Viewport(this, g_device, "Viewport");
viewport->setManipulator(&m_state.manipulator);

auto *leditor = new anari_viewer::windows::LightsEditor(g_device);
auto *leditor = new anari_viewer::windows::LightsEditor(this, g_device);

auto *sselector = new anari_viewer::windows::SceneSelector();
auto *sselector = new anari_viewer::windows::SceneSelector(this);
sselector->setCallback([=](const char *category, const char *scene) {
try {
auto s = anari::scenes::createScene(g_device, category, scene);
Expand Down
107 changes: 57 additions & 50 deletions src/anari_viewer/Application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@
// SPDX-License-Identifier: Apache-2.0

#include "Application.h"
// glad
#include "glad/glad.h"
// glfw
#include <GLFW/glfw3.h>
#include "windows/Window.h"
// sdl
#include <SDL3/SDL.h>
#include <SDL3/SDL_opengl.h>
// imgui
#define IMGUI_DISABLE_INCLUDE_IMCONFIG_H
#include "imgui_impl_glfw.h"
#include "imgui_impl_opengl2.h"
#include "imgui_impl_sdl3.h"
#include "imgui_impl_sdlrenderer3.h"
// std
#include <chrono>
#include <cstdio>
Expand All @@ -18,14 +18,10 @@

namespace anari_viewer {

static void glfw_error_callback(int error, const char *description)
{
fprintf(stderr, "Glfw Error %d: %s\n", error, description);
}

struct AppImpl
{
GLFWwindow *window{nullptr};
SDL_Window *window{nullptr};
SDL_Renderer *sdl_renderer{nullptr};
int width{0};
int height{0};
bool windowResized{true};
Expand All @@ -44,7 +40,6 @@ struct AppImpl
Application::Application()
{
m_impl = std::make_shared<AppImpl>();
glfwSetErrorCallback(glfw_error_callback);
}

void Application::uiFrameStart()
Expand All @@ -57,6 +52,11 @@ void Application::uiFrameEnd()
// no-op
}

SDL_Renderer* Application::sdlRenderer()
{
return m_impl->sdl_renderer;
}

void Application::run(int width, int height, const char *name)
{
m_impl->width = width;
Expand Down Expand Up @@ -87,19 +87,28 @@ void Application::mainLoop()
{
auto window = m_impl->window;

while (!glfwWindowShouldClose(window)) {
bool open = true;
while (open) {
m_impl->frameStartTime = m_impl->frameEndTime;
m_impl->frameEndTime = std::chrono::steady_clock::now();
glfwPollEvents();

ImGui_ImplOpenGL2_NewFrame();
ImGui_ImplGlfw_NewFrame();
SDL_Event event;
while (SDL_PollEvent(&event))
{
ImGui_ImplSDL3_ProcessEvent(&event);
if (event.type == SDL_EVENT_QUIT)
open = false;
if (event.type == SDL_EVENT_WINDOW_CLOSE_REQUESTED && event.window.windowID == SDL_GetWindowID(window))
open = false;
}

ImGui_ImplSDLRenderer3_NewFrame();
ImGui_ImplSDL3_NewFrame();

ImGui::NewFrame();

ImGuiIO &io = ImGui::GetIO();
if (io.KeysDown[GLFW_KEY_Q] && io.KeysDown[GLFW_KEY_LEFT_CONTROL])
glfwSetWindowShouldClose(window, 1);
// if (io.KeysDown[GLFW_KEY_Q] && io.KeysDown[GLFW_KEY_LEFT_CONTROL])
// open = false;

uiFrameStart();

Expand Down Expand Up @@ -128,12 +137,15 @@ void Application::mainLoop()
ImGui::End();

ImGui::Render();
m_impl->width = io.DisplaySize.x;
m_impl->height = io.DisplaySize.y;

glClearColor(0.1f, 0.1f, 0.1f, 1.f);
glClear(GL_COLOR_BUFFER_BIT);
ImGui_ImplOpenGL2_RenderDrawData(ImGui::GetDrawData());
auto sdl_renderer = m_impl->sdl_renderer;
SDL_SetRenderDrawColorFloat(sdl_renderer, 0.1f, 0.1f, 0.1f, 1.f);
SDL_RenderClear(sdl_renderer);
ImGui_ImplSDLRenderer3_RenderDrawData(ImGui::GetDrawData(), sdl_renderer);
SDL_RenderPresent(sdl_renderer);

glfwSwapBuffers(window);
m_impl->windowResized = false;

uiFrameEnd();
Expand All @@ -142,38 +154,33 @@ void Application::mainLoop()

void AppImpl::init()
{
if (!glfwInit())
throw std::runtime_error("failed to initialize GLFW");
if (!SDL_Init(SDL_INIT_VIDEO | SDL_INIT_GAMEPAD))
throw std::runtime_error("failed to initialize SDL");

glfwWindowHint(GLFW_SRGB_CAPABLE, GLFW_TRUE);

window = glfwCreateWindow(width, height, name.c_str(), nullptr, nullptr);
if (window == nullptr)
throw std::runtime_error("failed to create GLFW window");
Uint32 window_flags = SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE | SDL_WINDOW_HIDDEN;
window = SDL_CreateWindow(name.c_str(), width, height, window_flags);

glfwSetWindowUserPointer(window, this);
if (window == nullptr)
throw std::runtime_error("failed to create SDL window");

glfwMakeContextCurrent(window);
glfwSwapInterval(1);
sdl_renderer = SDL_CreateRenderer(window, nullptr);

if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) {
glfwTerminate();
throw std::runtime_error("Failed to load GL");
SDL_SetWindowPosition(window, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED);
if (sdl_renderer == nullptr)
{
SDL_DestroyWindow(window);
SDL_Quit();
throw std::runtime_error("Failed to create SDL renderer");
}

glfwSetFramebufferSizeCallback(
window, [](GLFWwindow *w, int newWidth, int newHeight) {
auto *app = (AppImpl *)glfwGetWindowUserPointer(w);
app->width = newWidth;
app->height = newHeight;
app->windowResized = true;
});
SDL_ShowWindow(window);

ImGui::CreateContext();
ImGui::StyleColorsDark();

ImGui_ImplGlfw_InitForOpenGL(window, true);
ImGui_ImplOpenGL2_Init();
ImGui_ImplSDL3_InitForSDLRenderer(window, sdl_renderer);
ImGui_ImplSDLRenderer3_Init(sdl_renderer);

ImGuiIO &io = ImGui::GetIO();
io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard;
Expand Down Expand Up @@ -204,13 +211,13 @@ void AppImpl::cleanup()
{
windows.clear();

ImGui_ImplOpenGL2_Shutdown();
ImGui_ImplGlfw_Shutdown();

ImGui_ImplSDLRenderer3_Shutdown();
ImGui_ImplSDL3_Shutdown();
ImGui::DestroyContext();

glfwDestroyWindow(window);
glfwTerminate();
SDL_DestroyRenderer(sdl_renderer);
SDL_DestroyWindow(window);
SDL_Quit();

window = nullptr;
}
Expand Down
8 changes: 7 additions & 1 deletion src/anari_viewer/Application.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

#pragma once

#include "windows/Window.h"
#include <SDL3/SDL.h>
// std
#include <memory>
#include <string_view>
Expand All @@ -12,6 +12,10 @@
namespace anari_viewer {

struct AppImpl;

namespace windows {
struct Window;
}
using WindowArray = std::vector<std::unique_ptr<windows::Window>>;

class Application
Expand All @@ -29,6 +33,8 @@ class Application
// Allow teardown of objects before application destruction
virtual void teardown() = 0;

SDL_Renderer* sdlRenderer();

// Start the application run loop
void run(int width, int height, const char *name);

Expand Down
2 changes: 1 addition & 1 deletion src/anari_viewer/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ project_include_directories(INTERFACE ${CMAKE_CURRENT_LIST_DIR}/..)
project_link_libraries(INTERFACE
anari::anari
anari_viewer_glad
anari_viewer_imgui_glfw
anari_viewer_imgui_sdl
anari_viewer_nfd
anari_viewer_stb_image
)
Expand Down
32 changes: 16 additions & 16 deletions src/anari_viewer/external/imgui/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,39 +1,39 @@
## Copyright 2023-2024 The Khronos Group
## SPDX-License-Identifier: Apache-2.0

project(anari_viewer_imgui_glfw LANGUAGES CXX)
project(anari_viewer_imgui_sdl LANGUAGES CXX)

anari_sdk_fetch_project(
NAME ${PROJECT_NAME}
URL https://github.com/ocornut/imgui/archive/refs/tags/v1.91.0-docking.zip
MD5 8098badecdd791acb0ac6056da4161a6
URL https://github.com/ocornut/imgui/archive/refs/tags/v1.91.7-docking.zip
MD5 2cfbf7b7790076d6debe5060ec1fb47f
)

include(CMakeFindDependencyMacro)

set(OpenGL_GL_PREFERENCE "LEGACY")
find_dependency(OpenGL 4 REQUIRED)
find_dependency(glfw3 REQUIRED)
find_dependency(SDL3 REQUIRED)

project_add_library(INTERFACE)

project_sources(
INTERFACE
${anari_viewer_imgui_glfw_LOCATION}/imgui.cpp
${anari_viewer_imgui_glfw_LOCATION}/imgui_draw.cpp
${anari_viewer_imgui_glfw_LOCATION}/imgui_demo.cpp
${anari_viewer_imgui_glfw_LOCATION}/imgui_tables.cpp
${anari_viewer_imgui_glfw_LOCATION}/imgui_widgets.cpp
${anari_viewer_imgui_glfw_LOCATION}/backends/imgui_impl_glfw.cpp
${anari_viewer_imgui_glfw_LOCATION}/backends/imgui_impl_opengl2.cpp
${anari_viewer_imgui_glfw_LOCATION}/misc/cpp/imgui_stdlib.cpp
${anari_viewer_imgui_sdl_LOCATION}/imgui.cpp
${anari_viewer_imgui_sdl_LOCATION}/imgui_draw.cpp
${anari_viewer_imgui_sdl_LOCATION}/imgui_demo.cpp
${anari_viewer_imgui_sdl_LOCATION}/imgui_tables.cpp
${anari_viewer_imgui_sdl_LOCATION}/imgui_widgets.cpp
${anari_viewer_imgui_sdl_LOCATION}/backends/imgui_impl_sdl3.cpp
${anari_viewer_imgui_sdl_LOCATION}/backends/imgui_impl_sdlrenderer3.cpp
${anari_viewer_imgui_sdl_LOCATION}/misc/cpp/imgui_stdlib.cpp
)

project_link_libraries(INTERFACE glfw OpenGL::GL)
project_link_libraries(INTERFACE SDL3::SDL3 OpenGL::GL)

project_include_directories(
INTERFACE
${anari_viewer_imgui_glfw_LOCATION}
${anari_viewer_imgui_glfw_LOCATION}/backends
${anari_viewer_imgui_glfw_LOCATION}/misc/cpp
${anari_viewer_imgui_sdl_LOCATION}
${anari_viewer_imgui_sdl_LOCATION}/backends
${anari_viewer_imgui_sdl_LOCATION}/misc/cpp
)
8 changes: 4 additions & 4 deletions src/anari_viewer/windows/LightsEditor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,17 @@ static const char *lightToType(Light::LightType type)
}
}

LightsEditor::LightsEditor(std::vector<anari::Device> devices, const char *name)
: Window(name, true), m_devices(devices)
LightsEditor::LightsEditor(Application *app, std::vector<anari::Device> devices, const char *name)
: Window(app, name, true), m_devices(devices)
{
for (auto d : m_devices)
anari::retain(d, d);
addNewLight(Light::DIRECTIONAL);
m_worlds.resize(m_devices.size(), nullptr);
}

LightsEditor::LightsEditor(anari::Device device, const char *name)
: LightsEditor(std::vector<anari::Device>{device}, name)
LightsEditor::LightsEditor(Application *app, anari::Device device, const char *name)
: LightsEditor(app, std::vector<anari::Device>{device}, name)
{}

LightsEditor::~LightsEditor()
Expand Down
4 changes: 2 additions & 2 deletions src/anari_viewer/windows/LightsEditor.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ struct Light

struct LightsEditor : public Window
{
LightsEditor(
LightsEditor(Application *app,
std::vector<anari::Device> devices, const char *name = "Lights Editor");
LightsEditor(anari::Device device, const char *name = "Lights Editor");
LightsEditor(Application *app, anari::Device device, const char *name = "Lights Editor");
~LightsEditor();

void buildUI() override;
Expand Down
Loading