1#pragma once
2#include "taichi/ui/common/app_config.h"
3#include <memory>
4#include "taichi/rhi/vulkan/vulkan_device_creator.h"
5#include "taichi/rhi/vulkan/vulkan_loader.h"
6#include "taichi/rhi/vulkan/vulkan_device.h"
7#include "taichi/ui/backends/vulkan/swap_chain.h"
8#ifdef ANDROID
9#include <android/native_window.h>
10#endif
11
12namespace taichi::lang {
13class Program;
14} // namespace taichi::lang
15
16namespace taichi::ui {
17
18#ifdef ANDROID
19using TaichiWindow = ANativeWindow;
20#else
21using TaichiWindow = GLFWwindow;
22#endif
23
24namespace vulkan {
25
26class TI_DLL_EXPORT AppContext {
27 public:
28 void init(lang::Program *prog, TaichiWindow *window, const AppConfig &config);
29 ~AppContext();
30
31 TaichiWindow *taichi_window() const;
32 lang::Program *prog() const;
33
34 taichi::lang::vulkan::VulkanDevice &device();
35 const taichi::lang::vulkan::VulkanDevice &device() const;
36 bool requires_export_sharing() const;
37
38 AppConfig config;
39
40 struct RasterPipelineConfig {
41 std::string frag_path;
42 std::string vert_path;
43 taichi::lang::TopologyType prim_topology{
44 taichi::lang::TopologyType::Triangles};
45 bool depth{false};
46 taichi::lang::PolygonMode polygon_mode{taichi::lang::PolygonMode::Fill};
47 bool blend{true};
48 bool vbo_instanced{false};
49 };
50
51 // Get a raster pipeline with the given fragment shader and vertex shader &
52 // options.
53 // - This function will cache the pipeline for future use.
54 // - This function will use the default GGUI vertex input format
55 taichi::lang::Pipeline *get_raster_pipeline(
56 const RasterPipelineConfig &config);
57
58 // Get a raster pipeline with the given fragment shader and vertex shader &
59 // options.
60 // - This function will cache the pipeline for future use
61 // - This function will use the provided vertex input format
62 taichi::lang::Pipeline *get_customized_raster_pipeline(
63 const RasterPipelineConfig &config,
64 const std::vector<taichi::lang::VertexInputBinding> &vertex_inputs,
65 const std::vector<taichi::lang::VertexInputAttribute> &vertex_attribs);
66
67 // Get a compute pipeline with the given compute shader
68 // - This function will cache the pipeline for future use
69 taichi::lang::Pipeline *get_compute_pipeline(const std::string &shader_path);
70
71 private:
72 std::unique_ptr<taichi::lang::vulkan::VulkanDeviceCreator>
73 embedded_vulkan_device_{nullptr};
74
75 // not owned
76 taichi::lang::vulkan::VulkanDevice *vulkan_device_{nullptr};
77
78 std::unordered_map<std::string, taichi::lang::UPipeline> pipelines_;
79
80 TaichiWindow *taichi_window_{nullptr};
81
82 lang::Program *prog_{nullptr};
83};
84
85} // namespace vulkan
86
87} // namespace taichi::ui
88