furnace/src/gui/render/renderMetal.mm

244 lines
7 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:
// - wipe
// - textures
// - maybe fix VSync
#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;
2024-04-14 20:14:42 -04:00
FurnaceGUIRenderMetalPrivate():
context(NULL),
cmdQueue(NULL),
cmdBuf(NULL),
renderEncoder(NULL),
drawable(NULL),
renderPass(NULL) {}
2023-07-03 19:13:45 -04:00
};
class FurnaceMetalTexture: public FurnaceGUITexture {
public:
2024-04-11 15:19:46 -04:00
id<MTLTexture> tex;
2024-04-15 05:37:41 -04:00
int width, height;
unsigned char* lockedData;
FurnaceMetalTexture():
2024-04-15 05:37:41 -04:00
tex(NULL),
width(0),
height(0),
lockedData(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
FurnaceMetalTexture* t=(FurnaceMetalTexture*)which;
2024-04-15 05:37:41 -04:00
if (t->lockedData!=NULL) return false;
t->lockedData=new unsigned char[t->width*t->height*4];
*data=t->lockedData;
*pitch=t->width*4;
return true;
}
bool FurnaceGUIRenderMetal::unlockTexture(FurnaceGUITexture* which) {
2024-04-12 00:57:39 -04:00
FurnaceMetalTexture* t=(FurnaceMetalTexture*)which;
2024-04-15 05:37:41 -04:00
if (t->lockedData==NULL) return false;
[t->tex replaceRegion:MTLRegionMake2D(0,0,(NSUInteger)t->width,(NSUInteger)t->height) mipmapLevel:0 withBytes:t->lockedData bytesPerRow:(NSUInteger)t->width*4];
delete[] t->lockedData;
t->lockedData=NULL;
return true;
}
bool FurnaceGUIRenderMetal::updateTexture(FurnaceGUITexture* which, void* data, int pitch) {
2024-04-15 05:37:41 -04:00
FurnaceMetalTexture* t=(FurnaceMetalTexture*)which;
[t->tex replaceRegion:MTLRegionMake2D(0,0,(NSUInteger)t->width,(NSUInteger)t->height) mipmapLevel:0 withBytes:data bytesPerRow:(NSUInteger)pitch];
return true;
}
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;
2024-04-15 05:37:41 -04:00
ret->width=width;
ret->height=height;
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);
2024-04-14 20:14:42 -04:00
if (priv->drawable) {
[priv->drawable release];
}
if (priv->cmdBuf) {
[priv->cmdBuf release];
}
2024-04-12 00:57:39 -04:00
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() {
2024-04-14 19:46:34 -04:00
return ImGui_ImplMetal_NewFrame(priv->renderPass);
}
2024-04-14 20:39:43 -04:00
bool FurnaceGUIRenderMetal::canVSync() {
return swapIntervalSet;
}
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];
2024-04-14 20:30:54 -04:00
[priv->renderEncoder release];
}
bool FurnaceGUIRenderMetal::getOutputSize(int& w, int& h) {
return SDL_GetRendererOutputSize(sdlRend,&w,&h)==0;
}
int FurnaceGUIRenderMetal::getWindowFlags() {
return 0;
}
2024-04-14 20:39:43 -04:00
void FurnaceGUIRenderMetal::setSwapInterval(int swapInterval) {
if (SDL_RenderSetVSync(sdlRend,(swapInterval>=0)?1:0)!=0) {
swapIntervalSet=false;
logW("tried to enable VSync but couldn't!");
} else {
swapIntervalSet=true;
}
}
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
2024-04-14 20:39:43 -04:00
if (SDL_RenderSetVSync(sdlRend,(swapInterval>=0)?1:0)!=0) {
swapIntervalSet=false;
logW("tried to enable VSync but couldn't!");
} else {
swapIntervalSet=true;
}
2024-04-14 18:48:01 -04:00
logI("retrieving context...");
2023-07-03 19:13:45 -04:00
priv->context=(__bridge CAMetalLayer*)SDL_RenderGetMetalLayer(sdlRend);
2024-04-14 19:11:46 -04:00
if (priv->context==NULL) {
logE("Metal layer is NULL!");
return false;
}
2023-07-03 19:13:45 -04:00
priv->context.pixelFormat=MTLPixelFormatBGRA8Unorm;
2024-04-14 19:11:46 -04:00
priv->cmdQueue=[priv->context.device newCommandQueue];
priv->renderPass=[MTLRenderPassDescriptor new];
2023-07-03 19:13:45 -04:00
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);
}
void FurnaceGUIRenderMetal::quitGUI() {
2024-04-12 01:55:57 -04:00
ImGui_ImplMetal_Shutdown();
}
bool FurnaceGUIRenderMetal::quit() {
if (sdlRend==NULL) return false;
2024-04-14 19:11:46 -04:00
[priv->renderPass release];
[priv->cmdQueue release];
SDL_DestroyRenderer(sdlRend);
sdlRend=NULL;
return true;
}