furnace/src/gui/render/renderMetal.mm

183 lines
5.5 KiB
Plaintext
Raw Normal View History

/**
* Furnace Tracker - multi-system chiptune tracker
* Copyright (C) 2021-2023 tildearrow and contributors
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
// TODO: everything
#include "renderMetal.h"
#include "backends/imgui_impl_metal.h"
2023-07-03 19:13:45 -04:00
#include <Metal/Metal.h>
#include <QuartzCore/QuartzCore.h>
2023-07-03 19:13:45 -04:00
struct FurnaceGUIRenderMetalPrivate {
CAMetalLayer* context;
id<MTLCommandQueue> cmdQueue;
2024-04-12 00:57:39 -04:00
id<MTLCommandBuffer> cmdBuf;
id<MTLRenderCommandEncoder> renderEncoder;
id<CAMetalDrawable> drawable;
2023-07-03 19:13:45 -04:00
MTLRenderPassDescriptor* renderPass;
};
class FurnaceMetalTexture: public FurnaceGUITexture {
public:
2024-04-11 15:19:46 -04:00
id<MTLTexture> tex;
FurnaceMetalTexture():
tex(NULL) {}
};
ImTextureID FurnaceGUIRenderMetal::getTextureID(FurnaceGUITexture* which) {
2024-04-12 00:57:39 -04:00
FurnaceMetalTexture* t=(FurnaceMetalTexture*)which;
return t->tex;
}
bool FurnaceGUIRenderMetal::lockTexture(FurnaceGUITexture* which, void** data, int* pitch) {
2024-04-12 00:57:39 -04:00
return false;
/*
FurnaceMetalTexture* t=(FurnaceMetalTexture*)which;
return SDL_LockTexture(t->tex,NULL,data,pitch)==0;*/
}
bool FurnaceGUIRenderMetal::unlockTexture(FurnaceGUITexture* which) {
2024-04-12 00:57:39 -04:00
return false;
/*
FurnaceMetalTexture* t=(FurnaceMetalTexture*)which;
SDL_UnlockTexture(t->tex);
2024-04-12 00:57:39 -04:00
return true;*/
}
bool FurnaceGUIRenderMetal::updateTexture(FurnaceGUITexture* which, void* data, int pitch) {
2024-04-12 00:57:39 -04:00
return false;
/*
FurnaceSDLTexture* t=(FurnaceSDLTexture*)which;
2024-04-12 00:57:39 -04:00
return SDL_UpdateTexture(t->tex,NULL,data,pitch)==0;*/
}
2024-04-11 23:35:47 -04:00
FurnaceGUITexture* FurnaceGUIRenderMetal::createTexture(bool dynamic, int width, int height, bool interpolate) {
2024-04-12 00:57:39 -04:00
MTLTextureDescriptor* texDesc=[MTLTextureDescriptor texture2DDescriptorWithPixelFormat:MTLPixelFormatRGBA8Unorm width:(NSUInteger)width height:(NSUInteger)height mipmapped:NO];
texDesc.usage=MTLTextureUsageShaderRead;
texDesc.storageMode=MTLStorageModeManaged;
id<MTLTexture> texture=[priv->context.device newTextureWithDescriptor:texDesc];
2024-04-12 00:57:39 -04:00
if (texture==NULL) return NULL;
FurnaceMetalTexture* ret=new FurnaceMetalTexture;
ret->tex=texture;
return ret;
}
bool FurnaceGUIRenderMetal::destroyTexture(FurnaceGUITexture* which) {
2024-04-12 00:57:39 -04:00
FurnaceMetalTexture* t=(FurnaceMetalTexture*)which;
delete t;
return true;
}
void FurnaceGUIRenderMetal::setTextureBlendMode(FurnaceGUITexture* which, FurnaceGUIBlendMode mode) {
}
void FurnaceGUIRenderMetal::setBlendMode(FurnaceGUIBlendMode mode) {
}
2024-04-12 00:57:39 -04:00
// you should only call this once!!!
void FurnaceGUIRenderMetal::clear(ImVec4 color) {
2024-04-12 00:57:39 -04:00
int outW, outH;
getOutputSize(outW,outH);
priv->context.drawableSize=CGSizeMake(outW,outH);
priv->drawable=[priv->context nextDrawable];
priv->cmdBuf=[priv->cmdQueue commandBuffer];
priv->renderPass.colorAttachments[0].clearColor=MTLClearColorMake(color.x,color.y,color.z,color.w);
2024-04-12 01:10:44 -04:00
priv->renderPass.colorAttachments[0].texture=priv->drawable.texture;
2024-04-12 00:57:39 -04:00
priv->renderPass.colorAttachments[0].loadAction=MTLLoadActionClear;
priv->renderPass.colorAttachments[0].storeAction=MTLStoreActionStore;
2024-04-12 01:34:55 -04:00
priv->renderEncoder=[priv->cmdBuf renderCommandEncoderWithDescriptor:priv->renderPass];
}
bool FurnaceGUIRenderMetal::newFrame() {
2023-07-03 19:13:45 -04:00
ImGui_ImplMetal_NewFrame(priv->renderPass);
}
void FurnaceGUIRenderMetal::createFontsTexture() {
2024-04-12 01:34:55 -04:00
ImGui_ImplMetal_CreateFontsTexture(priv->context.device);
}
void FurnaceGUIRenderMetal::destroyFontsTexture() {
2024-04-12 00:57:39 -04:00
ImGui_ImplMetal_DestroyFontsTexture();
}
void FurnaceGUIRenderMetal::renderGUI() {
2024-04-12 00:57:39 -04:00
ImGui_ImplMetal_RenderDrawData(ImGui::GetDrawData(),priv->cmdBuf,priv->renderEncoder);
}
void FurnaceGUIRenderMetal::wipe(float alpha) {
2024-04-12 00:57:39 -04:00
// TODO
}
void FurnaceGUIRenderMetal::present() {
2024-04-12 00:57:39 -04:00
[priv->renderEncoder endEncoding];
[priv->cmdBuf presentDrawable:priv->drawable];
[priv->cmdBuf commit];
}
bool FurnaceGUIRenderMetal::getOutputSize(int& w, int& h) {
return SDL_GetRendererOutputSize(sdlRend,&w,&h)==0;
}
int FurnaceGUIRenderMetal::getWindowFlags() {
return 0;
}
void FurnaceGUIRenderMetal::preInit() {
SDL_SetHint(SDL_HINT_RENDER_DRIVER,"metal");
2023-07-03 19:13:45 -04:00
priv=new FurnaceGUIRenderMetalPrivate;
}
2024-04-11 23:35:47 -04:00
bool FurnaceGUIRenderMetal::init(SDL_Window* win, int swapInterval) {
SDL_SetHint(SDL_HINT_RENDER_DRIVER,"metal");
sdlRend=SDL_CreateRenderer(win,-1,SDL_RENDERER_ACCELERATED|SDL_RENDERER_PRESENTVSYNC|SDL_RENDERER_TARGETTEXTURE);
2023-07-03 19:13:45 -04:00
2024-04-12 01:55:57 -04:00
if (sdlRend==NULL) return false;
2023-07-03 19:13:45 -04:00
priv->context=(__bridge CAMetalLayer*)SDL_RenderGetMetalLayer(sdlRend);
priv->context.pixelFormat=MTLPixelFormatBGRA8Unorm;
return true;
}
void FurnaceGUIRenderMetal::initGUI(SDL_Window* win) {
2024-04-12 01:55:57 -04:00
ImGui_ImplMetal_Init(priv->context.device);
2023-07-03 19:13:45 -04:00
ImGui_ImplSDL2_InitForMetal(win);
priv->cmdQueue=[priv->context.device newCommandQueue];
priv->renderPass=[MTLRenderPassDescriptor new];
}
void FurnaceGUIRenderMetal::quitGUI() {
2024-04-12 01:55:57 -04:00
ImGui_ImplMetal_Shutdown();
2023-07-03 19:13:45 -04:00
[priv->renderPass release];
[priv->cmdQueue release];
}
bool FurnaceGUIRenderMetal::quit() {
if (sdlRend==NULL) return false;
SDL_DestroyRenderer(sdlRend);
sdlRend=NULL;
return true;
}