Skip to content

Commit

Permalink
ImGui still mad but progress
Browse files Browse the repository at this point in the history
  • Loading branch information
IsaacMarovitz committed Dec 31, 2024
1 parent 0c78a87 commit 2d77eb8
Show file tree
Hide file tree
Showing 9 changed files with 93 additions and 32 deletions.
2 changes: 1 addition & 1 deletion core/rend/metal/metal_buffer.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright 2024 flyinghead
Copyright 2024 flyinghead
This file is part of Flycast.
Expand Down
2 changes: 1 addition & 1 deletion core/rend/metal/metal_buffer.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright 2024 flyinghead
Copyright 2024 flyinghead
This file is part of Flycast.
Expand Down
4 changes: 4 additions & 0 deletions core/rend/metal/metal_context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ bool MetalContext::init() {
return false;
}

layer->setDevice(device);
queue = device->newCommandQueue();
commandBuffer = queue->commandBuffer();

NOTICE_LOG(RENDERER, "Created Metal view.");

imguiDriver = std::unique_ptr<ImGuiDriver>(new MetalDriver());
Expand Down
4 changes: 4 additions & 0 deletions core/rend/metal/metal_context.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ class MetalContext : public GraphicsContext
void term() override;

MTL::Device* GetDevice() const { return device; }
CA::MetalLayer* GetLayer() const { return layer; }
MTL::CommandQueue* GetQueue() const { return queue; }
MTL::CommandBuffer *commandBuffer = nullptr;
void resize() override;
void Present();

Expand All @@ -54,6 +57,7 @@ class MetalContext : public GraphicsContext
static MetalContext* Instance() { return contextInstance; }
private:
MTL::Device* device = MTL::CreateSystemDefaultDevice();
MTL::CommandQueue *queue = nullptr;
CA::MetalLayer* layer;
static MetalContext* contextInstance;
};
68 changes: 54 additions & 14 deletions core/rend/metal/metal_driver.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,57 @@
#include "metal_context.h"
#include <unordered_map>

#include "metal_texture.h"

class MetalDriver final : public ImGuiDriver {
public:
MetalDriver() {
ImGui_ImplMetal_Init(MetalContext::Instance()->GetDevice());
}

void reset() override
{
ImGuiDriver::reset();
ImGui_ImplMetal_Shutdown();
}

void newFrame() override {
MetalContext *context = MetalContext::Instance();
drawable = context->GetLayer()->nextDrawable();

MTL::RenderPassDescriptor *descriptor = MTL::RenderPassDescriptor::alloc()->init();

descriptor->setDefaultRasterSampleCount(1);

auto color = descriptor->colorAttachments()->object(0);
color->setClearColor(MTL::ClearColor(0.f, 0.f, 0.f, 1.f));
color->setTexture(drawable->texture());
color->setLoadAction(MTL::LoadActionClear);
color->setStoreAction(MTL::StoreActionStore);

commandEncoder = context->commandBuffer->renderCommandEncoder(descriptor);

ImGui_ImplMetal_NewFrame(descriptor);

// descriptor->release();
}

void renderDrawData(ImDrawData *drawData, bool gui_open) override {
MetalContext *context = MetalContext::Instance();
MTL::CommandBuffer *buffer = context->commandBuffer;

ImGui_ImplMetal_RenderDrawData(drawData, buffer, commandEncoder);

commandEncoder->endEncoding();
buffer->presentDrawable(drawable);
buffer->commit();

buffer->release();
commandEncoder->release();
commandEncoder = nullptr;

context->commandBuffer = context->GetQueue()->commandBuffer();

if (gui_open)
frameRendered = true;
}
Expand All @@ -49,36 +87,38 @@ class MetalDriver final : public ImGuiDriver {
auto it = textures.find(name);
if (it != textures.end())
return &it->second.texture;
else
return ImTextureID{};

return ImTextureID{};
}

ImTextureID updateTexture(const std::string &name, const u8 *data, int width, int height, bool nearestSampling) override {
Texture& texture = textures[name];
texture.texture->setPurgeableState(MTL::PurgeableStateEmpty);
Texture texture(std::make_unique<MetalTexture>());
texture.texture->tex_type = TextureType::_8888;
texture.texture->UploadToGPU(width, height, data, false);

MTL::TextureDescriptor *desc = MTL::TextureDescriptor::alloc()->init();
desc->setWidth(width);
desc->setHeight(height);
ImTextureID textureID = texture.texture->texture;

MTL::Region region = MTL::Region { 0, 0, static_cast<NS::UInteger>(width), static_cast<NS::UInteger>(height) };
texture.texture = MetalContext::Instance()->GetDevice()->newTexture(desc);
texture.texture->replaceRegion(region, 0, data, width * 4);
textures[name] = std::move(texture);

return texture.texture;
return textureID;
}

void deleteTexture(const std::string &name) override {
auto it = textures.find(name);
it->second.texture->setPurgeableState(MTL::PurgeableStateEmpty);
it->second.texture->texture->setPurgeableState(MTL::PurgeableStateEmpty);
textures.erase(name);
}

private:
struct Texture {
MTL::Texture *texture;
Texture() = default;
Texture(std::unique_ptr<MetalTexture>&& texture) : texture(std::move(texture)) {}

std::unique_ptr<MetalTexture> texture;
};

bool frameRendered = false;
MTL::RenderCommandEncoder *commandEncoder;
CA::MetalDrawable *drawable;
std::unordered_map<std::string, Texture> textures;
};
};
31 changes: 23 additions & 8 deletions core/rend/metal/metal_renderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,6 @@ bool MetalRenderer::Init()
shaders = MetalShaders();
samplers = MetalSamplers();

queue = MetalContext::Instance()->GetDevice()->newCommandQueue();
commandBuffer = queue->commandBuffer();

frameRendered = false;

return true;
Expand Down Expand Up @@ -75,11 +72,18 @@ bool MetalRenderer::Render() {
}

Draw(fogTexture.get(), paletteTexture.get());
if (config::EmulateFramebuffer || pvrrc.isRTT)
// delay ending the render pass in case of multi render
EndRenderPass();
// if (config::EmulateFramebuffer || pvrrc.isRTT)
// // delay ending the render pass in case of multi render
// EndRenderPass();

return !pvrrc.isRTT;
return true;
}

void MetalRenderer::EndRenderPass() {
if (!renderPassStarted)
return;

frameRendered = true;
}

void MetalRenderer::RenderFramebuffer(const FramebufferInfo &info) {
Expand Down Expand Up @@ -350,8 +354,9 @@ bool MetalRenderer::Draw(const MetalTexture *fogTexture, const MetalTexture *pal

currentScissor = MTL::ScissorRect {};

MTL::CommandBuffer *buffer = MetalContext::Instance()->commandBuffer;
MTL::RenderPassDescriptor *descriptor = MTL::RenderPassDescriptor::alloc()->init();
MTL::RenderCommandEncoder *encoder = commandBuffer->renderCommandEncoder(descriptor);
MTL::RenderCommandEncoder *encoder = buffer->renderCommandEncoder(descriptor);

// Upload vertex and index buffers
VertexShaderUniforms vtxUniforms;
Expand Down Expand Up @@ -385,6 +390,16 @@ bool MetalRenderer::Draw(const MetalTexture *fogTexture, const MetalTexture *pal
}
}

encoder->endEncoding();
buffer->presentDrawable(MetalContext::Instance()->GetLayer()->nextDrawable());
buffer->commit();

buffer->release();
encoder->release();

DEBUG_LOG(RENDERER, "Render command buffer released");

MetalContext::Instance()->commandBuffer = MetalContext::Instance()->GetQueue()->commandBuffer();
return !pvrrc.isRTT;
}

Expand Down
7 changes: 1 addition & 6 deletions core/rend/metal/metal_renderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,7 @@ class MetalRenderer final : public Renderer
void DrawList(MTL::RenderCommandEncoder *encoder, u32 listType, bool sortTriangles, const std::vector<PolyParam>& polys, u32 first, u32 last);
void DrawModVols(MTL::RenderCommandEncoder *encoder, int first, int count);
void UploadMainBuffer(const VertexShaderUniforms& vertexUniforms, const FragmentShaderUniforms& fragmentUniforms);

virtual void EndRenderPass() {
renderPassStarted = false;
}
void EndRenderPass();

protected:
TileClipping SetTileClip(MTL::RenderCommandEncoder *encoder, u32 val, MTL::ScissorRect& clipRect);
Expand Down Expand Up @@ -121,8 +118,6 @@ class MetalRenderer final : public Renderer
u64 lightsOffset = 0;
} offsets;

MTL::CommandQueue *queue = nullptr;
MTL::CommandBuffer *commandBuffer = nullptr;
bool renderPassStarted = false;

MTL::ScissorRect baseScissor {};
Expand Down
5 changes: 4 additions & 1 deletion core/rend/metal/metal_texture.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright 2024 flyinghead
Copyright 2024 flyinghead
This file is part of Flycast.
Expand Down Expand Up @@ -63,6 +63,8 @@ void MetalTexture::UploadToGPU(int width, int height, const u8 *temp_tex_buffer,
desc->setHeight(height);
desc->setPixelFormat(format);
desc->setMipmapLevelCount(mipmapLevels);
desc->setStorageMode(MTL::StorageModeShared);
desc->setUsage(MTL::TextureUsageUnknown);

auto device = MetalContext::Instance()->GetDevice();

Expand All @@ -77,6 +79,7 @@ bool MetalTexture::Delete()
{
texture->setPurgeableState(MTL::PurgeableStateEmpty);
texture->release();
texture = nullptr;

return true;
}
Expand Down
2 changes: 1 addition & 1 deletion core/rend/metal/metal_texture.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright 2024 flyinghead
Copyright 2024 flyinghead
This file is part of Flycast.
Expand Down

0 comments on commit 2d77eb8

Please sign in to comment.