Skip to content

Commit

Permalink
Get metal backend working better on SDL3
Browse files Browse the repository at this point in the history
  • Loading branch information
Hugh Sanderson committed Sep 15, 2024
1 parent 8ffbd8b commit fb19106
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 30 deletions.
2 changes: 1 addition & 1 deletion project/ToolkitBuild.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
<set name="NME_DYNAMIC_ANGLE" value="1" if="NME_LOCAL_TOOLKIT" unless="NME_NO_ANGLE" />
<set name="NATIVE_TOOLKIT_SDL_ANGLE" value="1" if="NME_ANGLE" />
<set name="NATIVE_TOOLKIT_STATIC_ANGLE" value="1" if="NME_ANGLE" />
<set name="OBJC_ARC" value="1" if="NME_SDL3" />

<set name="NME_LOCAL_TOOLKIT" value="1" if="android" />

Expand Down Expand Up @@ -274,7 +275,6 @@
<compilerflag value="-DNME_SIMD_D" if="NME_SIMD_D" />
<compilerflag value="${NME_SIMD_D}" if="NME_SIMD_D" />
<compilerflag value="-I${ANGLE_DIR}/include" if="NME_ANGLE" />
<mflag value="-fobjc-arc" if="NME_LOCAL_TOOLKIT" />


<cache value="1" unless="winrpi" />
Expand Down
41 changes: 22 additions & 19 deletions project/src/metal/MetalContext.mm
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,6 @@
{


#ifdef NME_SDL3
void *GetMetalLayerFromRenderer(SDL_Renderer *renderer)
{
const CAMetalLayer *swapchain = (__bridge CAMetalLayer *)SDL_GetRenderMetalLayer(renderer);
return (void *)swapchain;
}
#elif defined(NME_SDL2)
void *GetMetalLayerFromRenderer(SDL_Renderer *renderer)
{
const CAMetalLayer *swapchain = (__bridge CAMetalLayer *)SDL_RenderGetMetalLayer(renderer);
return (void *)swapchain;
}
#endif

static const double one_on_256 = 1.0/256.0;

Expand Down Expand Up @@ -498,14 +485,14 @@ void Dirty(const Rect &inRect)
shader += shaderTail;


// printf("SHADER: %x whole=%d\n%s\n\n",progId, wholeTexture, shader.c_str() );

__autoreleasing NSError *error = nil;

NSString* librarySrc = [NSString stringWithCString:shader.c_str() encoding:[NSString defaultCStringEncoding]];

id<MTLLibrary> lib = [device newLibraryWithSource:librarySrc options:nil error:&error];
if(!lib) {
printf("Shader compile failed: %x whole=%d\n%s\n\n",progId, wholeTexture, shader.c_str() );
[NSException raise:@"Failed to compile shaders" format:@"%@", [error localizedDescription]];
}

Expand Down Expand Up @@ -677,21 +664,29 @@ void bindUniforms(id<MTLRenderCommandEncoder> renderEncoder)

};


static size_t totalBuffer = 0;
struct MetalBuffer
{
id<MTLBuffer> buffer;
int size;

MetalBuffer(id<MTLDevice> device, const void *pointer, NSUInteger length)
{
size = (int)length;
totalBuffer += length;
buffer = [device newBufferWithBytes:pointer
length:(NSUInteger)length
options:MTLResourceCPUCacheModeWriteCombined ];
}

~MetalBuffer()
{
#ifndef OBJC_ARC
[buffer release];
#endif

buffer = nil;
totalBuffer -= size;
}
};

Expand Down Expand Up @@ -740,7 +735,7 @@ void bindUniforms(id<MTLRenderCommandEncoder> renderEncoder)

public:

MetalContext(void *inCAMetalLayer) :
MetalContext(CAMetalLayer *inCAMetalLayer) :
swapchain(0),
_device(nil),
queue(nil),
Expand All @@ -749,7 +744,7 @@ void bindUniforms(id<MTLRenderCommandEncoder> renderEncoder)
surface(nullptr)
{
frame = 0;
swapchain = (__bridge CAMetalLayer *)inCAMetalLayer;
swapchain = inCAMetalLayer;
_device = swapchain.device;
queue = [_device newCommandQueue];
width = height = 0;
Expand Down Expand Up @@ -977,9 +972,17 @@ void EndDirectRender() { }
};


HardwareRenderer *HardwareRenderer::CreateMetal(void *inLayer)
HardwareRenderer *HardwareRenderer::CreateMetal(void *inRenderer)
{
HardwareRenderer *ctx = new MetalContext(inLayer);
SDL_Renderer *renderer = (SDL_Renderer *)renderer;

#ifdef NME_SDL3
CAMetalLayer *metalLayer = (__bridge CAMetalLayer *)SDL_GetRenderMetalLayer(renderer);
#elif defined(NME_SDL2)
CAMetalLayer *metalLayer = (__bridge CAMetalLayer *)SDL_RenderGetMetalLayer(renderer);
#endif

HardwareRenderer *ctx = new MetalContext( metalLayer );

return ctx;
}
Expand Down
9 changes: 5 additions & 4 deletions project/src/sdl2/SDL2Stage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,9 @@ int InitSDL()
SDL_SetHint(SDL_HINT_RENDER_DRIVER, "metal");
else
SDL_SetHint(SDL_HINT_RENDER_DRIVER, "opengl");
#ifdef NME_SDL3
SDL_SetHint(SDL_HINT_RENDER_METAL_PREFER_LOW_POWER_DEVICE, "0");
#endif
#endif

#ifdef NME_DYNAMIC_ANGLE
Expand Down Expand Up @@ -419,16 +422,14 @@ class SDLStage : public Stage
}
else
{
void *swapchain = GetMetalLayerFromRenderer(mSDLRenderer);
mHardwareRenderer = HardwareRenderer::CreateMetal(swapchain);
mHardwareRenderer = HardwareRenderer::CreateMetal(mSDLRenderer);
}
#elif defined(NME_OGL)
mHardwareRenderer = HardwareRenderer::CreateOpenGL(0, 0, sgIsOGL2);

#elif defined(NME_METAL)

void *swapchain = GetMetalLayerFromRenderer(mSDLRenderer);
mHardwareRenderer = HardwareRenderer::CreateMetal(swapchain);
mHardwareRenderer = HardwareRenderer::CreateMetal(mSDLRenderer);

#else
#error "No valid HardwareRenderer"
Expand Down
6 changes: 0 additions & 6 deletions samples/BunnyMark/Source/Main.hx
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,6 @@ class Main extends Sprite
{
super();

trace("--");
for(ext in nme.gl.GL.getSupportedExtensions())
if (ext.indexOf("draw_buffer")>=0 || ext.indexOf("frame")>=0)
trace(" " + ext);
trace("--");

bunnies = new Array();
minX = 0;
maxX = 800;
Expand Down

0 comments on commit fb19106

Please sign in to comment.