Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[rhi] MetalSurface functions #8274

Merged
merged 23 commits into from
Jul 13, 2023
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
f9b6de0
Add metal version of common.h for rhi example 1
AntonioFerreras Jun 30, 2023
9ec3b44
Merge branch 'taichi-dev:master' into metal-rhi-work
AntonioFerreras Jul 5, 2023
b1c6c97
Surface things. Example update
AntonioFerreras Jul 7, 2023
a76dc57
MetalSurface funcs
AntonioFerreras Jul 10, 2023
4e6a36b
Merge branch 'taichi-dev:master' into metal-rhi-work
AntonioFerreras Jul 10, 2023
a89e235
Remove RHI examples changes
AntonioFerreras Jul 10, 2023
06529f2
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jul 10, 2023
0596a64
Add check for displaySyncEnabled exists before setting
AntonioFerreras Jul 11, 2023
ff2b052
Change
AntonioFerreras Jul 11, 2023
9ec4cd0
Fix undefined property on IOS
AntonioFerreras Jul 11, 2023
a2d3abc
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jul 11, 2023
cf8b8aa
Formatting and other fix
AntonioFerreras Jul 11, 2023
2bb23f0
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jul 11, 2023
45185ec
Make QuartzCore public link
AntonioFerreras Jul 11, 2023
27f1b37
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jul 11, 2023
90ed139
More ammends
AntonioFerreras Jul 11, 2023
a370dd4
More attempts at fixing
AntonioFerreras Jul 11, 2023
b7709a8
Test pre-commit ci
feisuzhu Jul 13, 2023
b63c00b
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jul 13, 2023
cb15797
Trigger rerun jobs
AntonioFerreras Jul 13, 2023
18fb6b4
Update metal_device.h
bobcao3 Jul 13, 2023
16bf37a
Update metal_device.h
bobcao3 Jul 13, 2023
905857d
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jul 13, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion taichi/rhi/metal/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@ target_include_directories(${METAL_RHI}
${PROJECT_SOURCE_DIR}/external/glad/include
${PROJECT_SOURCE_DIR}/external/glfw/include
)
target_link_libraries(${METAL_RHI} PRIVATE spirv-cross-msl spirv-cross-core)
target_link_libraries(${METAL_RHI} PRIVATE spirv-cross-msl spirv-cross-core "-framework QuartzCore")
160 changes: 93 additions & 67 deletions taichi/rhi/metal/metal_device.h
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
#pragma once
#include <memory>
#include "taichi/common/logging.h"
#include "taichi/rhi/device.h"
#include "taichi/rhi/metal/metal_api.h"
#include "taichi/rhi/impl_support.h"
#include "taichi/rhi/metal/metal_api.h"
#include <memory>

#if defined(__APPLE__) && defined(__OBJC__)
#import <CoreGraphics/CoreGraphics.h>
#import <Foundation/Foundation.h>
#import <Metal/Metal.h>
#import <MetalKit/MetalKit.h>
#import <CoreGraphics/CoreGraphics.h>
#import <QuartzCore/QuartzCore.h>
AntonioFerreras marked this conversation as resolved.
Show resolved Hide resolved
#define DEFINE_METAL_ID_TYPE(x) typedef id<x> x##_id;
#define DEFINE_OBJC_TYPE(x) @class x;
#else
#define DEFINE_METAL_ID_TYPE(x) typedef struct x##_t *x##_id;
#define DEFINE_OBJC_TYPE(x) typedef void x;
#endif

DEFINE_METAL_ID_TYPE(MTLDevice);
Expand All @@ -26,8 +29,11 @@ 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);
DEFINE_OBJC_TYPE(CAMetalLayer);

#undef DEFINE_METAL_ID_TYPE
#undef DEFINE_OBJC_TYPE

namespace taichi::lang {

Expand All @@ -41,7 +47,7 @@ class MetalStream;
class MetalDevice;

struct MetalMemory {
public:
public:
AntonioFerreras marked this conversation as resolved.
Show resolved Hide resolved
// `mtl_buffer` should be already retained.
explicit MetalMemory(MTLBuffer_id mtl_buffer, bool host_access);
~MetalMemory();
Expand All @@ -52,14 +58,14 @@ struct MetalMemory {
size_t size() const;
RhiResult mapped_ptr(void **mapped_ptr) const;

private:
private:
MTLBuffer_id mtl_buffer_;
bool can_map_{false};
bool dont_destroy_{false};
};

struct MetalImage {
public:
public:
// `mtl_texture` should be already retained.
explicit MetalImage(MTLTexture_id mtl_texture);
~MetalImage();
Expand All @@ -68,20 +74,20 @@ struct MetalImage {

MTLTexture_id mtl_texture() const;

private:
private:
MTLTexture_id mtl_texture_;
bool dont_destroy_{false};
};

struct MetalSampler {
public:
public:
// `mtl_texture` should be already retained.
explicit MetalSampler(MTLSamplerState_id mtl_sampler_state);
~MetalSampler();

MTLSamplerState_id mtl_sampler_state() const;

private:
private:
MTLSamplerState_id mtl_sampler_state_;
};

Expand All @@ -91,19 +97,17 @@ struct MetalWorkgroupSize {
uint32_t z{0};
};
class MetalPipeline final : public Pipeline {
public:
public:
// `mtl_library`, `mtl_function`, `mtl_compute_pipeline_state` should be
// already retained.
explicit MetalPipeline(const MetalDevice &device,
MTLLibrary_id mtl_library,
explicit MetalPipeline(const MetalDevice &device, MTLLibrary_id mtl_library,
AntonioFerreras marked this conversation as resolved.
Show resolved Hide resolved
MTLFunction_id mtl_function,
MTLComputePipelineState_id mtl_compute_pipeline_state,
MetalWorkgroupSize workgroup_size);
~MetalPipeline() final;

static MetalPipeline *create(const MetalDevice &device,
const uint32_t *spv_data,
size_t spv_size,
const uint32_t *spv_data, size_t spv_size,
const std::string &name);
void destroy();

Expand All @@ -114,7 +118,7 @@ class MetalPipeline final : public Pipeline {
return workgroup_size_;
}

private:
private:
const MetalDevice *device_;
MTLLibrary_id mtl_library_;
MTLFunction_id mtl_function_;
Expand Down Expand Up @@ -145,37 +149,34 @@ struct MetalShaderResource {
};
};
class MetalShaderResourceSet final : public ShaderResourceSet {
public:
public:
explicit MetalShaderResourceSet(const MetalDevice &device);
~MetalShaderResourceSet() final;

ShaderResourceSet &rw_buffer(uint32_t binding,
DevicePtr ptr,
ShaderResourceSet &rw_buffer(uint32_t binding, DevicePtr ptr,
size_t size) final;
ShaderResourceSet &rw_buffer(uint32_t binding, DeviceAllocation alloc) final;

ShaderResourceSet &buffer(uint32_t binding, DevicePtr ptr, size_t size) final;
ShaderResourceSet &buffer(uint32_t binding, DeviceAllocation alloc) final;

ShaderResourceSet &image(uint32_t binding,
DeviceAllocation alloc,
ShaderResourceSet &image(uint32_t binding, DeviceAllocation alloc,
ImageSamplerConfig sampler_config) override;

ShaderResourceSet &rw_image(uint32_t binding,
DeviceAllocation alloc,
ShaderResourceSet &rw_image(uint32_t binding, DeviceAllocation alloc,
AntonioFerreras marked this conversation as resolved.
Show resolved Hide resolved
int lod) override;

inline const std::vector<MetalShaderResource> &resources() const {
return resources_;
}

private:
private:
const MetalDevice *device_;
std::vector<MetalShaderResource> resources_;
std::vector<MetalShaderResource> resources_; // TODO: need raster resources
};

class MetalCommandList final : public CommandList {
public:
public:
explicit MetalCommandList(const MetalDevice &device,
MTLCommandQueue_id cmd_queue);
~MetalCommandList() final;
Expand All @@ -192,13 +193,12 @@ class MetalCommandList final : public CommandList {
void buffer_fill(DevicePtr ptr, size_t size, uint32_t data) noexcept final;
RhiResult dispatch(uint32_t x, uint32_t y = 1, uint32_t z = 1) noexcept final;

void image_transition(DeviceAllocation img,
ImageLayout old_layout,
void image_transition(DeviceAllocation img, ImageLayout old_layout,
AntonioFerreras marked this conversation as resolved.
Show resolved Hide resolved
ImageLayout new_layout) final;

MTLCommandBuffer_id finalize();

private:
private:
friend class MetalStream;

const MetalDevice *device_;
Expand All @@ -210,7 +210,7 @@ class MetalCommandList final : public CommandList {
};

class MetalStream final : public Stream {
public:
public:
// `mtl_command_queue` should be already retained.
explicit MetalStream(const MetalDevice &device,
MTLCommandQueue_id mtl_command_queue);
Expand All @@ -219,47 +219,78 @@ class MetalStream final : public Stream {
static MetalStream *create(const MetalDevice &device);
void destroy();

MTLCommandQueue_id mtl_command_queue() const {
return mtl_command_queue_;
}
MTLCommandQueue_id mtl_command_queue() const { return mtl_command_queue_; }

RhiResult new_command_list(CommandList **out_cmdlist) noexcept final;
StreamSemaphore submit(
CommandList *cmdlist,
const std::vector<StreamSemaphore> &wait_semaphores = {}) final;
StreamSemaphore submit_synced(
CommandList *cmdlist,
const std::vector<StreamSemaphore> &wait_semaphores = {}) final;
StreamSemaphore
submit(CommandList *cmdlist,
const std::vector<StreamSemaphore> &wait_semaphores = {}) final;
StreamSemaphore
submit_synced(CommandList *cmdlist,
AntonioFerreras marked this conversation as resolved.
Show resolved Hide resolved
const std::vector<StreamSemaphore> &wait_semaphores = {}) final;

void command_sync() override;

private:
private:
const MetalDevice *device_;
MTLCommandQueue_id mtl_command_queue_;
std::vector<MTLCommandBuffer_id> pending_cmdbufs_;
bool is_destroyed_{false};
};

class MetalSurface final : public Surface {
public:
MetalSurface(MetalDevice *device, const SurfaceConfig &config);
~MetalSurface() override;

CAMetalLayer *mtl_layer() { return layer_; }

StreamSemaphore acquire_next_image() override;
DeviceAllocation get_target_image() override;

void present_image(
const std::vector<StreamSemaphore> &wait_semaphores = {}) override;
std::pair<uint32_t, uint32_t> get_size() override;
int get_image_count() override;
BufferFormat image_format() override;
void resize(uint32_t width, uint32_t height) override;

DeviceAllocation get_depth_data(DeviceAllocation &depth_alloc) override {
TI_NOT_IMPLEMENTED;
}
DeviceAllocation get_image_data() override { TI_NOT_IMPLEMENTED; }

private:
void destroy_swap_chain();

SurfaceConfig config_;

BufferFormat image_format_{BufferFormat::unknown};

uint32_t width_{0};
uint32_t height_{0};

MTLTexture_id current_swap_chain_texture_;
std::unordered_map<MTLTexture_id, DeviceAllocation> swapchain_images_;
CAMetalDrawable_id current_drawable_;

MetalDevice *device_{nullptr};
CAMetalLayer *layer_;
};

class MetalDevice final : public GraphicsDevice {
public:
public:
// `mtl_device` should be already retained.
explicit MetalDevice(MTLDevice_id mtl_device);
~MetalDevice() override;

Arch arch() const override {
return Arch::metal;
}
MTLDevice_id mtl_device() const {
return mtl_device_;
}
Arch arch() const override { return Arch::metal; }
MTLDevice_id mtl_device() const { return mtl_device_; }

static MetalDevice *create();
void destroy();

std::unique_ptr<Surface> create_surface(
const SurfaceConfig &config) override {
TI_NOT_IMPLEMENTED;
}
std::unique_ptr<Surface> create_surface(const SurfaceConfig &config) override;

RhiResult allocate_memory(const AllocParams &params,
DeviceAllocation *out_devalloc) override;
Expand All @@ -282,34 +313,29 @@ class MetalDevice final : public GraphicsDevice {
void unmap(DeviceAllocation ptr) override;

RhiResult create_pipeline(Pipeline **out_pipeline,
const PipelineSourceDesc &src,
std::string name,
const PipelineSourceDesc &src, std::string name,
PipelineCache *cache) noexcept final;
ShaderResourceSet *create_resource_set() override;

std::unique_ptr<Pipeline> create_raster_pipeline(
const std::vector<PipelineSourceDesc> &src,
const RasterParams &raster_params,
const std::vector<VertexInputBinding> &vertex_inputs,
const std::vector<VertexInputAttribute> &vertex_attrs,
std::string name = "Pipeline") override {
TI_NOT_IMPLEMENTED;
}
RasterResources *create_raster_resources() override {
std::unique_ptr<Pipeline>
create_raster_pipeline(const std::vector<PipelineSourceDesc> &src,
const RasterParams &raster_params,
const std::vector<VertexInputBinding> &vertex_inputs,
const std::vector<VertexInputAttribute> &vertex_attrs,
std::string name = "Pipeline") override {
TI_NOT_IMPLEMENTED;
}
RasterResources *create_raster_resources() override { TI_NOT_IMPLEMENTED; }

Stream *get_compute_stream() override;
Stream *get_graphics_stream() override;
void wait_idle() override;

void memcpy_internal(DevicePtr dst, DevicePtr src, uint64_t size) override;

const MetalSampler &get_default_sampler() const {
return *default_sampler_;
}
const MetalSampler &get_default_sampler() const { return *default_sampler_; }

private:
private:
MTLDevice_id mtl_device_;
rhi_impl::SyncedPtrStableObjectList<MetalMemory> memory_allocs_;
rhi_impl::SyncedPtrStableObjectList<MetalImage> image_allocs_;
Expand All @@ -320,5 +346,5 @@ class MetalDevice final : public GraphicsDevice {
bool is_destroyed_{false};
};

} // namespace metal
} // namespace taichi::lang
} // namespace metal
} // namespace taichi::lang
Loading
Loading