Skip to content

Commit

Permalink
vk-shadertoy rework
Browse files Browse the repository at this point in the history
Signed-off-by: Joel Winarske <[email protected]>
  • Loading branch information
jwinarske committed May 13, 2024
1 parent 919b08b commit 9cbd62b
Show file tree
Hide file tree
Showing 29 changed files with 13,526 additions and 1,308 deletions.
6 changes: 3 additions & 3 deletions cmake/glsl_compile.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@ macro(append_glsl_to_target target sources version)

get_filename_component(FILE_NAME ${GLSL} NAME)

set(SPIRV "${CMAKE_CURRENT_BINARY_DIR}/shaders/${FILE_NAME}.spv")
set(SPIRV "${CMAKE_CURRENT_BINARY_DIR}/shaders//spv/${FILE_NAME}.spv")

if (NOT EXISTS "${GLSL}")
set(GLSL "${CMAKE_CURRENT_SOURCE_DIR}/${GLSL}")
endif ()

add_custom_command(
OUTPUT ${SPIRV}
COMMAND ${CMAKE_COMMAND} -E make_directory ${CMAKE_CURRENT_BINARY_DIR}/shaders
COMMAND ${CMAKE_COMMAND} -E make_directory ${CMAKE_CURRENT_BINARY_DIR}/shaders/spv
COMMAND ${GLSL_VALIDATOR} -V${version} ${GLSL} -o ${SPIRV}
DEPENDS ${GLSL}
)
Expand All @@ -44,7 +44,7 @@ macro(append_glsl_to_target target sources version)
add_dependencies(${target} Shaders)

add_custom_command(TARGET ${target} POST_BUILD
COMMAND ${CMAKE_COMMAND} -E make_directory "$<TARGET_FILE_DIR:${target}>/shaders/"
COMMAND ${CMAKE_COMMAND} -E make_directory "$<TARGET_FILE_DIR:${target}>/shaders"
COMMAND ${CMAKE_COMMAND} -E copy_directory "${CMAKE_CURRENT_BINARY_DIR}/shaders" "$<TARGET_FILE_DIR:${target}>/shaders"
)
endmacro()
8 changes: 3 additions & 5 deletions examples/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -102,12 +102,10 @@ add_sanitizers(agl-capture)
#
# Vulkan
#
if (FALSE)
find_package(Vulkan)
find_package(Vulkan)

if (VULKAN_FOUND)
add_subdirectory(vk-shadertoy)
endif ()
if (VULKAN_FOUND)
add_subdirectory(vk-shadertoy)
endif ()

#
Expand Down
11 changes: 8 additions & 3 deletions examples/vk-shadertoy/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,18 @@ add_executable(vk-shadertoy
app.cc
handlers.cc
main.cc
vk_backend.cc
vk_image.cc
vk_buffer.cc
shader_toy.cc
vulkan/render.cc
vulkan/utils.cc
vulkan/vk_error_print.cc
)

append_glsl_to_target(vk-shadertoy "${SHADER_SOURCES}" 100)

target_compile_definitions(vk-shadertoy PUBLIC -DVK_USE_PLATFORM_WAYLAND_KHR)

target_include_directories(vk-shadertoy PRIVATE .)

target_link_libraries(vk-shadertoy PRIVATE waypp wayland-gen cxxopts::cxxopts)

if (IPO_SUPPORT_RESULT)
Expand Down
57 changes: 24 additions & 33 deletions examples/vk-shadertoy/app.cc
Original file line number Diff line number Diff line change
Expand Up @@ -25,60 +25,51 @@

#include <thread>

static constexpr uint32_t kOffscreenBuffers = 4;

void App::draw_frame(void* /* data */, const uint32_t /* time */) {
// auto window = static_cast<Window *>(data);
void App::draw_frame(void * /* data */, const uint32_t /* time */) {
// auto window = static_cast<Window *>(data);
}

App::App(const Configuration& config)
: handlers_(std::make_unique<Handlers>()),
logging_(std::make_unique<Logging>()) {
spdlog::info("{}", kAppTitle);
App::App(const Configuration &config)
: handlers_(std::make_unique<Handlers>()),
logging_(std::make_unique<Logging>()) {
spdlog::info("{}", kAppTitle);

display_ = wl_display_connect(nullptr);
if (!display_) {
spdlog::critical("Unable to connect to Wayland socket.");
exit(EXIT_FAILURE);
}

std::thread t1([&] {
backend_ = std::make_unique<VulkanBackend>(kAppId, config.debug_enable);
});
display_ = wl_display_connect(nullptr);
if (!display_) {
spdlog::critical("Unable to connect to Wayland socket.");
exit(EXIT_FAILURE);
}

std::thread t2([&] {
shadertoy_ = std::make_unique<ShaderToy>();
wm_ = std::make_unique<XdgWindowManager>(display_, config.disable_cursor);
auto seat = wm_->get_seat();
if (seat.has_value()) {
seat.value()->register_observer(handlers_.get());
seat.value()->register_observer(handlers_.get());
}

spdlog::debug("XDG Window Manager Version: {}", wm_->get_version());

toplevel_ = wm_->create_top_level(
kAppTitle, kAppId, config.width, config.height, 0, 0, config.fullscreen,
config.maximized, config.fullscreen_ratio, config.tearing, draw_frame);
kAppTitle, kAppId, config.width, config.height, 0, 0, config.fullscreen,
config.maximized, config.fullscreen_ratio, config.tearing, draw_frame);
spdlog::debug("XDG Window Version: {}", toplevel_->get_version());
});

t1.join();
t2.join();

backend_->CreateSurface(wm_->get_display(), toplevel_->get_surface(),
config.width, config.height, kOffscreenBuffers);
shadertoy_->init(config.width, config.height, display_, toplevel_->get_surface(), 0, true, true, true);

/// paint padding
toplevel_->set_surface_damage(0, 0, config.width, config.height);
toplevel_->start_frame_callbacks();
/// paint padding
toplevel_->set_surface_damage(0, 0, config.width, config.height);
toplevel_->start_frame_callbacks();
}

App::~App() {
toplevel_->stop_frame_callbacks();
wl_display_flush(display_);
wl_display_flush(display_);
toplevel_->stop_frame_callbacks();
wl_display_flush(display_);
wl_display_flush(display_);
}

bool App::run() {
/// display_dispatch is blocking
return (toplevel_->is_valid() && wm_->display_dispatch() != -1);
/// display_dispatch is blocking
return (toplevel_->is_valid() && wm_->display_dispatch() != -1);
}
4 changes: 2 additions & 2 deletions examples/vk-shadertoy/app.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

#include "handlers.h"
#include "logging.h"
#include "vk_backend.h"
#include "shader_toy.h"
#include "window/xdg_toplevel.h"

class App {
Expand Down Expand Up @@ -53,8 +53,8 @@ class App {
std::unique_ptr<Logging> logging_;
std::unique_ptr<Handlers> handlers_;
std::unique_ptr<XdgWindowManager> wm_;
std::unique_ptr<VulkanBackend> backend_;
XdgTopLevel* toplevel_;
std::unique_ptr<ShaderToy> shadertoy_;

static void draw_frame(void* data, uint32_t time);
};
Loading

0 comments on commit 9cbd62b

Please sign in to comment.