Skip to content

Commit

Permalink
[javascript] Avoid all usages of glfw/vulkan/volk when TI_EMSCRIPTENE…
Browse files Browse the repository at this point in the history
…D (JS 5/n) (#4108)

* Avoid all usage of glfw/vulkan/volk when TI_EMSCRIPTENED

* fix
  • Loading branch information
AmesingFlank authored Jan 25, 2022
1 parent 9dc8ad3 commit 31da0f9
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 14 deletions.
6 changes: 4 additions & 2 deletions taichi/backends/vulkan/vulkan_device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1919,6 +1919,7 @@ VkPresentModeKHR choose_swap_present_mode(

VulkanSurface::VulkanSurface(VulkanDevice *device, const SurfaceConfig &config)
: config_(config), device_(device) {
#if !defined(TI_EMSCRIPTENED)
#ifdef ANDROID
window_ = (ANativeWindow *)config.window_handle;
#else
Expand Down Expand Up @@ -1964,6 +1965,7 @@ VulkanSurface::VulkanSurface(VulkanDevice *device, const SurfaceConfig &config)
swapchain_images_.push_back(device->create_image(params));
swapchain_images_.push_back(device->create_image(params));
}
#endif
}

void VulkanSurface::create_swap_chain() {
Expand Down Expand Up @@ -2019,7 +2021,7 @@ void VulkanSurface::create_swap_chain() {
#ifdef ANDROID
width = ANativeWindow_getWidth(window_);
height = ANativeWindow_getWidth(window_);
#else
#elif !defined(TI_EMSCRIPTENED)
glfwGetFramebufferSize(window_, &width, &height);
#endif

Expand Down Expand Up @@ -2129,7 +2131,7 @@ std::pair<uint32_t, uint32_t> VulkanSurface::get_size() {
#ifdef ANDROID
width = ANativeWindow_getWidth(window_);
height = ANativeWindow_getWidth(window_);
#else
#elif !defined(TI_EMSCRIPTENED)
glfwGetFramebufferSize(window_, &width, &height);
#endif
return std::make_pair(width, height);
Expand Down
4 changes: 2 additions & 2 deletions taichi/backends/vulkan/vulkan_device.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

#ifdef ANDROID
#include <android/native_window_jni.h>
#else
#elif !defined(TI_EMSCRIPTENED)
#include <GLFW/glfw3.h>
#endif

Expand Down Expand Up @@ -387,7 +387,7 @@ class VulkanSurface : public Surface {
VkSemaphore image_available_;
#ifdef ANDROID
ANativeWindow *window_;
#else
#elif !defined(TI_EMSCRIPTENED)
GLFWwindow *window_;
#endif
BufferFormat image_format_;
Expand Down
6 changes: 3 additions & 3 deletions taichi/backends/vulkan/vulkan_loader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ bool VulkanLoader::init() {
if (initialized) {
return;
}
#ifdef __APPLE__
#if defined(__APPLE__) || defined(TI_EMSCRIPTENED)
initialized = true;
#else
VkResult result = volkInitialize();
Expand All @@ -29,14 +29,14 @@ bool VulkanLoader::init() {

void VulkanLoader::load_instance(VkInstance instance) {
vulkan_instance_ = instance;
#ifdef __APPLE__
#if defined(__APPLE__) || defined(TI_EMSCRIPTENED)
#else
volkLoadInstance(instance);
#endif
}
void VulkanLoader::load_device(VkDevice device) {
vulkan_device_ = device;
#ifdef __APPLE__
#if defined(__APPLE__) || defined(TI_EMSCRIPTENED)
#else
volkLoadDevice(device);
#endif
Expand Down
14 changes: 8 additions & 6 deletions taichi/backends/vulkan/vulkan_program.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
#include "taichi/backends/vulkan/vulkan_program.h"
#include "taichi/backends/vulkan/aot_module_builder_impl.h"

#ifdef ANDROID
#else
#if !defined(ANDROID) && !defined(TI_EMSCRIPTENED)
#include "GLFW/glfw3.h"
#endif

Expand All @@ -22,16 +21,17 @@ std::vector<std::string> get_required_instance_extensions() {

return extensions;
#else
std::vector<std::string> extensions;

#ifndef TI_EMSCRIPTENED
uint32_t glfw_ext_count = 0;
const char **glfw_extensions;
glfw_extensions = glfwGetRequiredInstanceExtensions(&glfw_ext_count);

std::vector<std::string> extensions;

for (int i = 0; i < glfw_ext_count; ++i) {
extensions.push_back(glfw_extensions[i]);
}

#endif
// VulkanDeviceCreator will check that these are supported
extensions.push_back(VK_KHR_GET_PHYSICAL_DEVICE_PROPERTIES_2_EXTENSION_NAME);
#if TI_WITH_CUDA
Expand Down Expand Up @@ -85,6 +85,7 @@ void VulkanProgramImpl::materialize_runtime(MemoryPool *memory_pool,
*result_buffer_ptr = (uint64 *)memory_pool->allocate(
sizeof(uint64) * taichi_result_buffer_entries, 8);

#ifndef TI_EMSCRIPTENED
// Android is meant to be embedded in other application only so the creation of
// the device and other states is left to the caller/host.
// The following code is only used when Taichi is running on its own.
Expand All @@ -105,11 +106,12 @@ void VulkanProgramImpl::materialize_runtime(MemoryPool *memory_pool,
TI_WARN("GLFW reports no Vulkan support");
}
}
#endif
#endif

VulkanDeviceCreator::Params evd_params;
evd_params.api_version = VulkanEnvSettings::kApiVersion();
#ifndef ANDROID
#if !defined(ANDROID) && !defined(TI_EMSCRIPTENED)
if (glfw_window) {
// then we should be able to create a device with graphics abilities
evd_params.additional_instance_extensions =
Expand Down
13 changes: 12 additions & 1 deletion taichi/gui/gui.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
(defined(TI_PLATFORM_UNIX) && !defined(TI_PLATFORM_OSX))
#if defined(TI_PLATFORM_ANDROID)
#define TI_GUI_ANDROID
#else
#elif !defined(TI_EMSCRIPTENED)
#define TI_GUI_X11
#endif
#endif
Expand Down Expand Up @@ -448,6 +448,17 @@ using GUIBase = GUIBaseAndroid;

#endif

#if defined(TI_EMSCRIPTENED)

class GUIBaseJavascript {
public:
// @TODO
};

using GUIBase = GUIBaseJavascript;

#endif

#if defined(TI_GUI_X11)

class CXImage;
Expand Down

0 comments on commit 31da0f9

Please sign in to comment.