Skip to content

Commit

Permalink
Merge branch 'master' of github.com:taichi-dev/taichi into fix_for_lo…
Browse files Browse the repository at this point in the history
…op_break
  • Loading branch information
jim19930609 committed Sep 20, 2023
2 parents 9b7cb9d + aa0619f commit f484174
Show file tree
Hide file tree
Showing 79 changed files with 2,303 additions and 575 deletions.
2 changes: 0 additions & 2 deletions cmake/TaichiCore.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,6 @@ if(TI_WITH_PYTHON)
# This requires refactoring on the python/export_*.cpp as well as better
# error message on the Python side.
add_subdirectory(taichi/ui)
target_link_libraries(taichi_ui PUBLIC ${CORE_LIBRARY_NAME})

message("PYTHON_LIBRARIES: " ${PYTHON_LIBRARIES})
set(CORE_WITH_PYBIND_LIBRARY_NAME taichi_python)
Expand Down Expand Up @@ -383,7 +382,6 @@ if(TI_WITH_PYTHON)

if(TI_WITH_GGUI)
target_compile_definitions(${CORE_WITH_PYBIND_LIBRARY_NAME} PRIVATE -DTI_WITH_GGUI)
target_link_libraries(${CORE_WITH_PYBIND_LIBRARY_NAME} PRIVATE taichi_ui_vulkan)
endif()

target_link_libraries(${CORE_WITH_PYBIND_LIBRARY_NAME} PRIVATE taichi_ui)
Expand Down
23 changes: 19 additions & 4 deletions cpp_examples/rhi_examples/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
macro(make_sample executable_name src_file)
macro(make_sample executable_name src_file flags)
add_executable(${executable_name})
set_property(TARGET ${executable_name} PROPERTY CXX_STANDARD 17)
set_property(TARGET ${executable_name} PROPERTY C_STANDARD 17)
target_sources(${executable_name} PRIVATE ${src_file} "common.h")
if (${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
target_sources(${executable_name} PRIVATE ${src_file} "common_vulkan.h" "common_metal.mm")
else()
target_sources(${executable_name} PRIVATE ${src_file} "common_vulkan.h")
endif()
target_include_directories(${executable_name}
PRIVATE
${PROJECT_SOURCE_DIR}
Expand All @@ -25,7 +29,18 @@ target_include_directories(${executable_name} SYSTEM
${PROJECT_SOURCE_DIR}/external/VulkanMemoryAllocator/include
)
target_link_libraries(${executable_name} taichi_core glfw)
if (${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
target_link_libraries(${executable_name} "-framework QuartzCore")
endif()
target_compile_definitions(${executable_name} PRIVATE ${flags})
endmacro()

make_sample(sample_1_window sample_1_window.cpp)
make_sample(sample_2_triangle sample_2_triangle.cpp)
if (CMAKE_SYSTEM_NAME MATCHES "Darwin")
set(RHI_EXAMPLE_FLAGS "RHI_EXAMPLE_BACKEND_METAL")
else()
set(RHI_EXAMPLE_FLAGS "RHI_EXAMPLE_BACKEND_VULKAN")
endif()

make_sample(sample_1_window sample_1_window.cpp ${RHI_EXAMPLE_FLAGS})
make_sample(sample_2_triangle sample_2_triangle.cpp ${RHI_EXAMPLE_FLAGS})
make_sample(sample_3_textured_triangle sample_3_textured_triangle.cpp ${RHI_EXAMPLE_FLAGS})
30 changes: 30 additions & 0 deletions cpp_examples/rhi_examples/common_metal.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#pragma once

#define GLFW_INCLUDE_NONE
#import <GLFW/glfw3.h>
#define GLFW_EXPOSE_NATIVE_COCOA
#import <GLFW/glfw3native.h>
#include "glm/glm.hpp"

#include "taichi/rhi/metal/metal_api.h"
#include "taichi/rhi/metal/metal_device.h"

using namespace taichi::lang;

class App {
public:
explicit App(int width, int height, const std::string &title);
virtual ~App();

virtual std::vector<StreamSemaphore> render_loop(
StreamSemaphore image_available_semaphore) {
return {};
}

void run();

GLFWwindow *glfw_window;
metal::MetalDevice *device;

std::unique_ptr<Surface> surface;
};
77 changes: 77 additions & 0 deletions cpp_examples/rhi_examples/common_metal.mm
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
#include "common_metal.h"

#include <assert.h>
#include <stdio.h>
#include <stdlib.h>

using namespace taichi::lang;

static void glfw_error_callback(int error, const char *description) {
TI_WARN("GLFW Error {}: {}", error, description);
}

App::App(int width, int height, const std::string &title) {
TI_INFO("Creating App '{}' of {}x{}", title, width, height);

TI_ASSERT(metal::is_metal_api_available());

device = metal::MetalDevice::create();
MTLDevice_id mtl_device = device->mtl_device();

if (!mtl_device)
TI_ERROR("failed to init Metal Device");

if (glfwInit()) {
TI_INFO("Initialized GLFW");

glfwSetErrorCallback(glfw_error_callback);
glfwWindowHint(GLFW_VISIBLE, GLFW_TRUE);
glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API);
glfw_window =
glfwCreateWindow(width, height, "Sample Window", nullptr, nullptr);
if (!glfw_window) {
glfwTerminate();
TI_ERROR("failed to init GLFW Window");
}
TI_INFO("Initialized GLFW Window");
} else {
TI_ERROR("failed to init GLFW");
}

SurfaceConfig config;
config.width = width;
config.height = height;

surface = device->create_surface(config);

metal::MetalSurface *mtl_surf =
dynamic_cast<metal::MetalSurface *>(surface.get());

NSWindow *nswin = glfwGetCocoaWindow(glfw_window);
nswin.contentView.layer = mtl_surf->mtl_layer();
nswin.contentView.wantsLayer = YES;
}

App::~App() {
surface.reset();
glfwDestroyWindow(glfw_window);
glfwTerminate();
}

std::vector<uint32_t> frag_spv_bin =
#include "shaders/2_triangle.frag.spv.h"
;

std::vector<uint32_t> vert_spv_bin =
#include "shaders/2_triangle.vert.spv.h"
;

void App::run() {
while (!glfwWindowShouldClose(glfw_window)) {
auto image_available_semaphore = surface->acquire_next_image();

glfwPollEvents();

surface->present_image(render_loop(image_available_semaphore));
}
}
File renamed without changes.
8 changes: 7 additions & 1 deletion cpp_examples/rhi_examples/sample_1_window.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
#include "common.h"
#ifdef RHI_EXAMPLE_BACKEND_VULKAN
#include "common_vulkan.h"
#endif // RHI_EXAMPLE_BACKEND_VULKAN

#ifdef RHI_EXAMPLE_BACKEND_METAL
#include "common_metal.h"
#endif

class SampleApp : public App {
public:
Expand Down
8 changes: 7 additions & 1 deletion cpp_examples/rhi_examples/sample_2_triangle.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
#include "common.h"
#ifdef RHI_EXAMPLE_BACKEND_VULKAN
#include "common_vulkan.h"
#endif // RHI_EXAMPLE_BACKEND_VULKAN

#ifdef RHI_EXAMPLE_BACKEND_METAL
#include "common_metal.h"
#endif

std::vector<uint32_t> frag_spv =
#include "shaders/2_triangle.frag.spv.h"
Expand Down
Loading

0 comments on commit f484174

Please sign in to comment.