Debian Codegraph @

src/gui/render/renderMetal.mm

raw
1 /**
2 * Furnace Tracker - multi-system chiptune tracker
3 * Copyright (C) 2021-2025 tildearrow and contributors
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #include "renderMetal.h"
21 #include "backends/imgui_impl_metal.h"
22
23 #include <Metal/Metal.h>
24 #include <QuartzCore/QuartzCore.h>
25
26 struct FurnaceGUIRenderMetalPrivate {
27 CAMetalLayer* context;
28 id<MTLCommandQueue> cmdQueue;
29 id<MTLCommandBuffer> cmdBuf;
30 id<MTLRenderCommandEncoder> renderEncoder;
31 id<CAMetalDrawable> drawable;
32 MTLRenderPassDescriptor* renderPass;
33
34 FurnaceGUIRenderMetalPrivate():
35 context(NULL),
36 cmdQueue(NULL),
37 cmdBuf(NULL),
38 renderEncoder(NULL),
39 drawable(NULL),
40 renderPass(NULL) {}
41 };
42
43 class FurnaceMetalTexture: public FurnaceGUITexture {
44 public:
45 id<MTLTexture> tex;
46 int width, height;
47 FurnaceGUITextureFormat format;
48 unsigned char* lockedData;
49 FurnaceMetalTexture():
50 tex(NULL),
51 width(0),
52 height(0),
53 format(GUI_TEXFORMAT_UNKNOWN),
54 lockedData(NULL) {}
55 };
56
57 ImTextureID FurnaceGUIRenderMetal::getTextureID(FurnaceGUITexture* which) {
58 FurnaceMetalTexture* t=(FurnaceMetalTexture*)which;
59 return (ImTextureID)t->tex;
60 }
61
62 FurnaceGUITextureFormat FurnaceGUIRenderMetal::getTextureFormat(FurnaceGUITexture* which) {
63 FurnaceMetalTexture* t=(FurnaceMetalTexture*)which;
64 return t->format;
65 }
66
67 bool FurnaceGUIRenderMetal::lockTexture(FurnaceGUITexture* which, void** data, int* pitch) {
68 FurnaceMetalTexture* t=(FurnaceMetalTexture*)which;
69 if (t->lockedData!=NULL) return false;
70 t->lockedData=new unsigned char[t->width*t->height*4];
71
72 *data=t->lockedData;
73 *pitch=t->width*4;
74 return true;
75 }
76
77 bool FurnaceGUIRenderMetal::unlockTexture(FurnaceGUITexture* which) {
78 FurnaceMetalTexture* t=(FurnaceMetalTexture*)which;
79 if (t->lockedData==NULL) return false;
80
81 [t->tex replaceRegion:MTLRegionMake2D(0,0,(NSUInteger)t->width,(NSUInteger)t->height) mipmapLevel:0 withBytes:t->lockedData bytesPerRow:(NSUInteger)t->width*4];
82 delete[] t->lockedData;
83 t->lockedData=NULL;
84
85 return true;
86 }
87
88 bool FurnaceGUIRenderMetal::updateTexture(FurnaceGUITexture* which, void* data, int pitch) {
89 FurnaceMetalTexture* t=(FurnaceMetalTexture*)which;
90 [t->tex replaceRegion:MTLRegionMake2D(0,0,(NSUInteger)t->width,(NSUInteger)t->height) mipmapLevel:0 withBytes:data bytesPerRow:(NSUInteger)pitch];
91 return true;
92 }
93
94 FurnaceGUITexture* FurnaceGUIRenderMetal::createTexture(bool dynamic, int width, int height, bool interpolate, FurnaceGUITextureFormat format) {
95 if (format!=GUI_TEXFORMAT_ABGR32) {
96 logE("unsupported texture format!");
97 return NULL;
98 }
99 MTLTextureDescriptor* texDesc=[MTLTextureDescriptor texture2DDescriptorWithPixelFormat:MTLPixelFormatRGBA8Unorm width:(NSUInteger)width height:(NSUInteger)height mipmapped:NO];
100 texDesc.usage=MTLTextureUsageShaderRead;
101 texDesc.storageMode=MTLStorageModeManaged;
102
103 id<MTLTexture> texture=[priv->context.device newTextureWithDescriptor:texDesc];
104
105 if (texture==NULL) return NULL;
106 FurnaceMetalTexture* ret=new FurnaceMetalTexture;
107 ret->tex=texture;
108 ret->width=width;
109 ret->height=height;
110
111 [texDesc release];
112
113 return ret;
114 }
115
116 bool FurnaceGUIRenderMetal::destroyTexture(FurnaceGUITexture* which) {
117 FurnaceMetalTexture* t=(FurnaceMetalTexture*)which;
118 [t->tex release];
119 t->tex=NULL;
120 delete t;
121 return true;
122 }
123
124 void FurnaceGUIRenderMetal::setTextureBlendMode(FurnaceGUITexture* which, FurnaceGUIBlendMode mode) {
125 }
126
127 void FurnaceGUIRenderMetal::setBlendMode(FurnaceGUIBlendMode mode) {
128 }
129
130 // you should only call this once!!!
131 void FurnaceGUIRenderMetal::clear(ImVec4 color) {
132 int outW, outH;
133 getOutputSize(outW,outH);
134 priv->context.drawableSize=CGSizeMake(outW,outH);
135
136 if (priv->drawable) {
137 [priv->drawable release];
138 }
139 if (priv->cmdBuf) {
140 [priv->cmdBuf release];
141 }
142
143 priv->drawable=[priv->context nextDrawable];
144
145 priv->cmdBuf=[priv->cmdQueue commandBuffer];
146 priv->renderPass.colorAttachments[0].clearColor=MTLClearColorMake(color.x,color.y,color.z,color.w);
147 priv->renderPass.colorAttachments[0].texture=priv->drawable.texture;
148 priv->renderPass.colorAttachments[0].loadAction=MTLLoadActionClear;
149 priv->renderPass.colorAttachments[0].storeAction=MTLStoreActionStore;
150 priv->renderEncoder=[priv->cmdBuf renderCommandEncoderWithDescriptor:priv->renderPass];
151 }
152
153 void FurnaceGUIRenderMetal::newFrame() {
154 ImGui_ImplMetal_NewFrame(priv->renderPass);
155 }
156
157 bool FurnaceGUIRenderMetal::canVSync() {
158 return swapIntervalSet;
159 }
160
161 void FurnaceGUIRenderMetal::renderGUI() {
162 ImGui_ImplMetal_RenderDrawData(ImGui::GetDrawData(),priv->cmdBuf,priv->renderEncoder);
163 }
164
165 void FurnaceGUIRenderMetal::wipe(float alpha) {
166 // you know what? cheat.
167 // I don't feel like learning yet another API just to please Apple.
168 // screw it.
169 SDL_SetRenderDrawBlendMode(sdlRend,SDL_BLENDMODE_BLEND);
170 SDL_SetRenderDrawColor(sdlRend,0,0,0,255*alpha);
171 SDL_RenderFillRect(sdlRend,NULL);
172 }
173
174 void FurnaceGUIRenderMetal::present() {
175 [priv->renderEncoder endEncoding];
176
177 [priv->cmdBuf presentDrawable:priv->drawable];
178 [priv->cmdBuf commit];
179
180 [priv->renderEncoder release];
181 }
182
183 bool FurnaceGUIRenderMetal::getOutputSize(int& w, int& h) {
184 return SDL_GetRendererOutputSize(sdlRend,&w,&h)==0;
185 }
186
187 int FurnaceGUIRenderMetal::getWindowFlags() {
188 return 0;
189 }
190
191 int FurnaceGUIRenderMetal::getMaxTextureWidth() {
192 return bigTextures?16384:8192;
193 }
194
195 int FurnaceGUIRenderMetal::getMaxTextureHeight() {
196 return bigTextures?16384:8192;
197 }
198
199 unsigned int FurnaceGUIRenderMetal::getTextureFormats() {
200 return GUI_TEXFORMAT_ABGR32;
201 }
202
203 const char* FurnaceGUIRenderMetal::getBackendName() {
204 return "Metal";
205 }
206
207 const char* FurnaceGUIRenderMetal::getVendorName() {
208 return vendorName.c_str();
209 }
210
211 const char* FurnaceGUIRenderMetal::getDeviceName() {
212 return deviceName.c_str();
213 }
214
215 const char* FurnaceGUIRenderMetal::getAPIVersion() {
216 return apiVersion.c_str();
217 }
218
219 void FurnaceGUIRenderMetal::setSwapInterval(int swapInterval) {
220 if (SDL_RenderSetVSync(sdlRend,(swapInterval>=0)?1:0)!=0) {
221 swapIntervalSet=false;
222 logW("tried to enable VSync but couldn't!");
223 } else {
224 swapIntervalSet=true;
225 }
226 }
227
228 void FurnaceGUIRenderMetal::preInit(const DivConfig& conf) {
229 SDL_SetHint(SDL_HINT_RENDER_DRIVER,"metal");
230 priv=new FurnaceGUIRenderMetalPrivate;
231 }
232
233 static const char* metalFamilyNames[]={
234 "Apple1",
235 "Apple2",
236 "Apple3",
237 "Apple4",
238 "Apple5",
239 "Apple6",
240 "Apple7",
241 "Apple8",
242 "Apple9",
243 "Mac1",
244 "Mac2",
245 "Common1",
246 "Common2",
247 "Common3",
248 "Metal3",
249 NULL
250 };
251
252 static const NSInteger metalFamilies[]={
253 1001, 1002, 1003, 1004, 1005, 1006, 1007, 1008, 1009, // Apple
254 2001, 2002, // Mac
255 3001, 3002, 3003, // Common
256 5001, // Metal3
257 0
258 };
259
260 bool FurnaceGUIRenderMetal::init(SDL_Window* win, int swapInterval) {
261 SDL_SetHint(SDL_HINT_RENDER_DRIVER,"metal");
262
263 sdlRend=SDL_CreateRenderer(win,-1,SDL_RENDERER_ACCELERATED|SDL_RENDERER_PRESENTVSYNC|SDL_RENDERER_TARGETTEXTURE);
264
265 if (sdlRend==NULL) return false;
266
267 if (SDL_RenderSetVSync(sdlRend,(swapInterval>=0)?1:0)!=0) {
268 swapIntervalSet=false;
269 logW("tried to enable VSync but couldn't!");
270 } else {
271 swapIntervalSet=true;
272 }
273
274 logI("retrieving context...");
275
276 priv->context=(__bridge CAMetalLayer*)SDL_RenderGetMetalLayer(sdlRend);
277
278 if (priv->context==NULL) {
279 logE("Metal layer is NULL!");
280 return false;
281 }
282
283 vendorName="N/A";
284 deviceName=[priv->context.device.name UTF8String];
285 apiVersion="";
286
287 bool comma=false;
288 for (int i=0; metalFamilies[i]; i++) {
289 const char* familyName=metalFamilyNames[i];
290 MTLGPUFamily family=(MTLGPUFamily)metalFamilies[i];
291
292 if ([priv->context.device supportsFamily:family]) {
293 if (comma) {
294 apiVersion+=", ";
295 }
296 apiVersion+=familyName;
297 comma=true;
298 }
299 }
300
301 priv->context.pixelFormat=MTLPixelFormatBGRA8Unorm;
302
303 priv->cmdQueue=[priv->context.device newCommandQueue];
304 priv->renderPass=[MTLRenderPassDescriptor new];
305 return true;
306 }
307
308 void FurnaceGUIRenderMetal::initGUI(SDL_Window* win) {
309 ImGui_ImplMetal_Init(priv->context.device);
310 ImGui_ImplSDL2_InitForMetal(win);
311 }
312
313 void FurnaceGUIRenderMetal::quitGUI() {
314 ImGui_ImplMetal_Shutdown();
315 }
316
317 bool FurnaceGUIRenderMetal::quit() {
318 if (sdlRend==NULL) return false;
319 [priv->renderPass release];
320 [priv->cmdQueue release];
321 SDL_DestroyRenderer(sdlRend);
322 sdlRend=NULL;
323 return true;
324 }