2222#include " metal_context.h"
2323#include < unordered_map>
2424
25+ #include " metal_texture.h"
26+
2527class MetalDriver final : public ImGuiDriver {
2628public:
29+ MetalDriver () {
30+ ImGui_ImplMetal_Init (MetalContext::Instance ()->GetDevice ());
31+ }
32+
2733 void reset () override
2834 {
2935 ImGuiDriver::reset ();
3036 ImGui_ImplMetal_Shutdown ();
3137 }
3238
3339 void newFrame () override {
40+ MetalContext *context = MetalContext::Instance ();
41+ drawable = context->GetLayer ()->nextDrawable ();
42+
43+ MTL::RenderPassDescriptor *descriptor = MTL::RenderPassDescriptor::alloc ()->init ();
44+
45+ descriptor->setDefaultRasterSampleCount (1 );
46+
47+ auto color = descriptor->colorAttachments ()->object (0 );
48+ color->setClearColor (MTL::ClearColor (0 .f , 0 .f , 0 .f , 1 .f ));
49+ color->setTexture (drawable->texture ());
50+ color->setLoadAction (MTL::LoadActionClear);
51+ color->setStoreAction (MTL::StoreActionStore);
52+
53+ commandEncoder = context->commandBuffer ->renderCommandEncoder (descriptor);
54+
55+ ImGui_ImplMetal_NewFrame (descriptor);
3456
57+ // descriptor->release();
3558 }
3659
3760 void renderDrawData (ImDrawData *drawData, bool gui_open) override {
61+ MetalContext *context = MetalContext::Instance ();
62+ MTL::CommandBuffer *buffer = context->commandBuffer ;
63+
64+ ImGui_ImplMetal_RenderDrawData (drawData, buffer, commandEncoder);
65+
66+ commandEncoder->endEncoding ();
67+ buffer->presentDrawable (drawable);
68+ buffer->commit ();
69+
70+ buffer->release ();
71+ commandEncoder->release ();
72+ commandEncoder = nullptr ;
73+
74+ context->commandBuffer = context->GetQueue ()->commandBuffer ();
75+
3876 if (gui_open)
3977 frameRendered = true ;
4078 }
@@ -49,36 +87,38 @@ class MetalDriver final : public ImGuiDriver {
4987 auto it = textures.find (name);
5088 if (it != textures.end ())
5189 return &it->second .texture ;
52- else
53- return ImTextureID{};
90+
91+ return ImTextureID{};
5492 }
5593
5694 ImTextureID updateTexture (const std::string &name, const u8 *data, int width, int height, bool nearestSampling) override {
57- Texture& texture = textures[name];
58- texture.texture ->setPurgeableState (MTL::PurgeableStateEmpty);
95+ Texture texture (std::make_unique<MetalTexture>());
96+ texture.texture ->tex_type = TextureType::_8888;
97+ texture.texture ->UploadToGPU (width, height, data, false );
5998
60- MTL::TextureDescriptor *desc = MTL::TextureDescriptor::alloc ()->init ();
61- desc->setWidth (width);
62- desc->setHeight (height);
99+ ImTextureID textureID = texture.texture ->texture ;
63100
64- MTL::Region region = MTL::Region { 0 , 0 , static_cast <NS::UInteger>(width), static_cast <NS::UInteger>(height) };
65- texture.texture = MetalContext::Instance ()->GetDevice ()->newTexture (desc);
66- texture.texture ->replaceRegion (region, 0 , data, width * 4 );
101+ textures[name] = std::move (texture);
67102
68- return texture. texture ;
103+ return textureID ;
69104 }
70105
71106 void deleteTexture (const std::string &name) override {
72107 auto it = textures.find (name);
73- it->second .texture ->setPurgeableState (MTL::PurgeableStateEmpty);
108+ it->second .texture ->texture -> setPurgeableState (MTL::PurgeableStateEmpty);
74109 textures.erase (name);
75110 }
76111
77112private:
78113 struct Texture {
79- MTL::Texture *texture;
114+ Texture () = default ;
115+ Texture (std::unique_ptr<MetalTexture>&& texture) : texture(std::move(texture)) {}
116+
117+ std::unique_ptr<MetalTexture> texture;
80118 };
81119
82120 bool frameRendered = false ;
121+ MTL::RenderCommandEncoder *commandEncoder;
122+ CA::MetalDrawable *drawable;
83123 std::unordered_map<std::string, Texture> textures;
84- };
124+ };
0 commit comments