diff --git a/cpp_examples/rhi_examples/CMakeLists.txt b/cpp_examples/rhi_examples/CMakeLists.txt index e05ad11036bc2..647d5315fd448 100644 --- a/cpp_examples/rhi_examples/CMakeLists.txt +++ b/cpp_examples/rhi_examples/CMakeLists.txt @@ -2,7 +2,7 @@ macro(make_sample executable_name src_file) 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" "common_metal.mm") +target_sources(${executable_name} PRIVATE ${src_file} "common.h") target_include_directories(${executable_name} PRIVATE ${PROJECT_SOURCE_DIR} @@ -24,7 +24,7 @@ target_include_directories(${executable_name} SYSTEM PUBLIC ${PROJECT_SOURCE_DIR}/external/VulkanMemoryAllocator/include ) -target_link_libraries(${executable_name} taichi_core glfw "-framework QuartzCore") +target_link_libraries(${executable_name} taichi_core glfw) endmacro() make_sample(sample_1_window sample_1_window.cpp) diff --git a/cpp_examples/rhi_examples/common_metal.h b/cpp_examples/rhi_examples/common_metal.h deleted file mode 100644 index 2e8c141985f9c..0000000000000 --- a/cpp_examples/rhi_examples/common_metal.h +++ /dev/null @@ -1,55 +0,0 @@ -#pragma once - -#define GLFW_INCLUDE_NONE -#import -#define GLFW_EXPOSE_NATIVE_COCOA -#import -#include "glm/glm.hpp" - -#if defined(__APPLE__) && defined(__OBJC__) -#import -#import -#define DEFINE_METAL_ID_TYPE(x) typedef id x##_id; -#else -#define DEFINE_METAL_ID_TYPE(x) typedef struct x##_t *x##_id; -#endif - -DEFINE_METAL_ID_TYPE(MTLDevice); -DEFINE_METAL_ID_TYPE(MTLBuffer); -DEFINE_METAL_ID_TYPE(MTLTexture); -DEFINE_METAL_ID_TYPE(MTLSamplerState); -DEFINE_METAL_ID_TYPE(MTLLibrary); -DEFINE_METAL_ID_TYPE(MTLFunction); -DEFINE_METAL_ID_TYPE(MTLComputePipelineState); -DEFINE_METAL_ID_TYPE(MTLCommandQueue); -DEFINE_METAL_ID_TYPE(MTLCommandBuffer); -DEFINE_METAL_ID_TYPE(MTLBlitCommandEncoder); -DEFINE_METAL_ID_TYPE(MTLComputeCommandEncoder); -DEFINE_METAL_ID_TYPE(CAMetalDrawable); - -#undef DEFINE_METAL_ID_TYPE - -#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 render_loop( - StreamSemaphore image_available_semaphore) { - return {}; - } - - void run(); - - private: - GLFWwindow *glfw_window; - metal::MetalDevice *rhi_metal_device; - - std::unique_ptr surface; - -}; \ No newline at end of file diff --git a/cpp_examples/rhi_examples/common_metal.mm b/cpp_examples/rhi_examples/common_metal.mm deleted file mode 100644 index 240d933d8610d..0000000000000 --- a/cpp_examples/rhi_examples/common_metal.mm +++ /dev/null @@ -1,71 +0,0 @@ -#include "common_metal.h" - -#include -#import -#include -#include - -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()); - - rhi_metal_device = metal::MetalDevice::create(); - MTLDevice_id mtl_device = rhi_metal_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 = rhi_metal_device->create_surface(config); - - metal::MetalSurface* mtl_surf = dynamic_cast (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(); -} - -void App::run() { - while (!glfwWindowShouldClose(glfw_window)) { - auto image_available_semaphore = surface->acquire_next_image(); - - glfwPollEvents(); - - surface->present_image(render_loop(image_available_semaphore)); - } -} diff --git a/cpp_examples/rhi_examples/sample_1_window.cpp b/cpp_examples/rhi_examples/sample_1_window.cpp index 9d23b8e09720c..c1e5d0272a5dc 100644 --- a/cpp_examples/rhi_examples/sample_1_window.cpp +++ b/cpp_examples/rhi_examples/sample_1_window.cpp @@ -1,4 +1,4 @@ -#include "common_metal.h" +#include "common.h" class SampleApp : public App { public: