From 6e0d5d58d228cad8d665f95eb25ebcd2b8cfe556 Mon Sep 17 00:00:00 2001 From: Patrick Cleavelin Date: Wed, 20 Mar 2024 19:05:38 -0500 Subject: [PATCH] 'support' dpi scaling, sorta --- shaders/text_atlas.metal | 1 + src/gfx.h | 13 ++++++++++++- src/main.c | 4 ++-- 3 files changed, 15 insertions(+), 3 deletions(-) diff --git a/shaders/text_atlas.metal b/shaders/text_atlas.metal index 404b82e..e5d99ea 100644 --- a/shaders/text_atlas.metal +++ b/shaders/text_atlas.metal @@ -44,6 +44,7 @@ vs_main( float2 scaled_size = ((vertices[vertex_id].position + 1.0) / 2.0) * (glyph.size/2.0); float2 scaled_size_2 = ((vertices[vertex_id].position + 1.0) / 2.0) * (glyph.size); float2 glyph_pos = scaled_size + glyph.target_position + float2(0, glyph.y_offset/2.0+32); + float4 device_position = to_device_position(glyph_pos, params.screen_size); float2 atlas_position = (scaled_size_2 + glyph.atlas_position) / 1024.0; diff --git a/src/gfx.h b/src/gfx.h index 8b78cc8..d2c216a 100644 --- a/src/gfx.h +++ b/src/gfx.h @@ -92,7 +92,9 @@ static void _metal_gfx_send_events(_metal_gfx_context *cx); _gfx_context.frame_width = _gfx_context.backend.window.contentView.frame.size.width; _gfx_context.frame_height = _gfx_context.backend.window.contentView.frame.size.height; - [_gfx_context.backend.metal_layer setDrawableSize:CGSizeMake(_gfx_context.frame_width, _gfx_context.frame_height)]; + + CGFloat scale = _gfx_context.backend.metal_layer.contentsScale; + [_gfx_context.backend.metal_layer setDrawableSize:CGSizeMake(_gfx_context.frame_width * scale, _gfx_context.frame_height * scale)]; _metal_gfx_present(&_gfx_context.backend); } @@ -161,6 +163,9 @@ static _metal_gfx_context _metal_gfx_init_context(uint32_t width, uint32_t heigh metal_layer.needsDisplayOnBoundsChange = YES; metal_layer.presentsWithTransaction = YES; metal_layer.autoresizingMask = kCALayerWidthSizable|kCALayerHeightSizable; + + // TODO: set this to the display dpi scale + metal_layer.contentsScale = 2.0; view.wantsLayer = YES; [view.layer addSublayer:metal_layer]; view.layerContentsRedrawPolicy = NSViewLayerContentsRedrawOnSetNeedsDisplay; @@ -191,6 +196,12 @@ static _metal_gfx_context _metal_gfx_init_context(uint32_t width, uint32_t heigh [pipeline_descriptor setVertexFunction:vertex_func]; [pipeline_descriptor setFragmentFunction:fragment_func]; pipeline_descriptor.colorAttachments[0].pixelFormat = MTLPixelFormatRGBA8Unorm; + pipeline_descriptor.colorAttachments[0].alphaBlendOperation = MTLBlendOperationAdd; + pipeline_descriptor.colorAttachments[0].sourceAlphaBlendFactor = MTLBlendFactorSourceAlpha; + pipeline_descriptor.colorAttachments[0].destinationAlphaBlendFactor = MTLBlendFactorOneMinusSourceAlpha; + pipeline_descriptor.colorAttachments[0].sourceRGBBlendFactor = MTLBlendFactorSourceAlpha; + pipeline_descriptor.colorAttachments[0].destinationRGBBlendFactor = MTLBlendFactorOneMinusSourceAlpha; + pipeline_descriptor.colorAttachments[0].blendingEnabled = true; NSError *pipeline_error = NULL; array(_MTLRenderPipelineState) pipelines = newArray(_MTLRenderPipelineState, 2); diff --git a/src/main.c b/src/main.c index d5a384e..8a6ef4f 100644 --- a/src/main.c +++ b/src/main.c @@ -126,7 +126,7 @@ void ed_init(_gfx_frame_func frame_func) { // manually add glyph for SPACE pushArray(GpuGlyph, &state.glyph_cache, ((GpuGlyph){ .atlas_position = { 0 }, - .size = { rasterized_font_height/4, rasterized_font_height }, + .size = { rasterized_font_height/4, 1 }, .position = { 0 }, .y_offset = -rasterized_font_height, })); @@ -178,7 +178,7 @@ void ed_frame() { ui_compute_layout(&state.ui_cx, 0); state.gpu_glyphs.size = 0; - for (size_t i = 0; i < state.ui_cx.frame_elements.size; ++i) { + for (size_t i = 1; i < state.ui_cx.frame_elements.size; ++i) { string text = state.ui_cx.frame_elements.data[i].key; ui_element_frame_data *elm = &state.ui_cx.frame_elements.data[i];