<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Rouzbeh Abadi on Medium]]></title>
        <description><![CDATA[Stories by Rouzbeh Abadi on Medium]]></description>
        <link>https://medium.com/@JustRouzbeh?source=rss-9044966abfac------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*WRNWuuyQTIxZZtJ2.jpg</url>
            <title>Stories by Rouzbeh Abadi on Medium</title>
            <link>https://medium.com/@JustRouzbeh?source=rss-9044966abfac------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Thu, 19 Nov 2026 20:45:38 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@JustRouzbeh/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Swift Metrics in practice: the boring but important glue for observability in Swift]]></title>
            <link>https://medium.com/@JustRouzbeh/swift-metrics-in-practice-the-boring-but-important-glue-for-observability-in-swift-c4a058e6909a?source=rss-9044966abfac------2</link>
            <guid isPermaLink="false">https://medium.com/p/c4a058e6909a</guid>
            <category><![CDATA[ios-app-development]]></category>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[apple]]></category>
            <category><![CDATA[ios]]></category>
            <dc:creator><![CDATA[Rouzbeh Abadi]]></dc:creator>
            <pubDate>Sun, 01 Feb 2026 19:53:56 GMT</pubDate>
            <atom:updated>2026-02-01T19:53:56.138Z</atom:updated>
            <content:encoded><![CDATA[<p>If you’ve shipped any app or service that lives longer than a single request, you already know the feeling: at some point you stop debating architecture patterns and start asking a more urgent question, how do I <em>see</em> what this thing is doing in production. That’s where metrics become the boring but important part of building software. <a href="https://github.com/apple/swift-metrics">Apple’s <strong>swift-metrics</strong></a> is quietly great because it doesn’t try to be a full monitoring product. Instead, it gives the Swift ecosystem a shared <strong>Metrics API</strong>, so packages can emit metrics without locking your app into a specific vendor, protocol, or backend.</p><h3>What swift-metrics is, and why that distinction matters</h3><p>swift-metrics is not a Prometheus client, not a StatsD client, and not a hosted observability service. It’s a standard interface for <em>recording</em> metrics in Swift. The “backend” is something you choose at the app boundary, typically at startup, and it can be swapped without rewriting your libraries. That separation is the whole design. Your internal modules and third-party packages can record counters, timers, gauges, and distributions in a consistent way, while the application decides where those metrics go, which is exactly how it should be if you ever want to change tooling without refactoring your entire codebase.</p><p>This solves a real ecosystem problem: libraries should never decide your monitoring stack. If a library hard-codes Prometheus, it forces Prometheus on every consumer. If another library hard-codes StatsD, you end up with competing pipelines and inconsistent naming and tagging. swift-metrics makes it normal for libraries to just say “I have a metric” and let the app decide how to export and aggregate it.</p><h3>The one rule that keeps everything clean</h3><p>The core usage pattern is simple: <strong>libraries emit metrics, applications choose the backend</strong>. In practice, that means libraries import Metrics and record values, while the application calls <em>MetricsSystem.bootstrap(…)</em> once at startup to install a concrete implementation. If you remember only one thing, remember this: the bootstrap belongs in your executable entry point, not in your libraries. It keeps dependencies sane, it prevents accidental multiple pipelines, and it makes metrics behavior predictable across the whole process.</p><h3>Metric types that map to real production questions</h3><p>The API includes metric types that correspond to the questions you ask when something goes wrong. Counters answer “how many times did this happen.” Gauges and meters answer “what is the value right now” or “how is it changing.” Timers and recorders help you see latency as a distribution, which matters because averages lie and tail latency is what users feel.</p><p>A <strong>Counter</strong> is for things that only go up, like total requests, total signups, total errors. A <strong>Gauge</strong> is for values you set directly, like queue depth or current memory usage. A <strong>Meter</strong> is similar in spirit but supports atomic increment and decrement patterns, which can be convenient for active job counts or in-flight requests. A <strong>Timer</strong> measures durations, and a <strong>Recorder</strong> captures a stream of numeric observations so a backend can compute useful aggregates like min, max, mean, and quantiles depending on what it supports.</p><h3>Labels and dimensions are the real contract</h3><p>The most important practical part of metrics is naming and tagging. In swift-metrics, you typically create metrics with a <strong>label</strong> and optionally attach <strong>dimensions</strong>, which are key-value pairs you can use to slice data in dashboards. The label is the stable metric name, and dimensions are the controlled vocabulary that makes graphs useful.</p><p>A good rule of thumb is that labels should be stable nouns and units should be explicit in naming, while dimensions should remain low-cardinality. You want dimensions like<em> method=GET</em>, <em>status=500</em>, <em>route=/users</em>, <em>region=eu</em>, because those values come from small sets. You do not want user IDs, full URLs with query parameters, device IDs, or anything that explodes the number of unique time series. Cardinality problems are how you accidentally turn observability into a performance incident.</p><h3>A minimal example that feels like real life</h3><p>Below is a small, backend-agnostic example you can use in a server, CLI, or even in an iOS app if you’re instrumenting core flows. The code records a request counter and tracks request duration, tagging both with a few dimensions that are useful in dashboards.</p><pre>import Metrics<br>import Foundation</pre><pre>let requestsTotal = Counter(label: &quot;http_requests_total&quot;)<br>let requestDuration = Timer(label: &quot;http_request_duration_seconds&quot;)</pre><pre>func handleRequest(route: String, method: String, status: Int, work: () throws -&gt; Void) rethrows {<br>    let dims = [<br>        (&quot;route&quot;, route),<br>        (&quot;method&quot;, method),<br>        (&quot;status&quot;, &quot;\(status)&quot;)<br>    ]</pre><pre>    requestsTotal.increment(dimensions: dims)</pre><pre>    let start = DispatchTime.now().uptimeNanoseconds<br>    defer {<br>        let end = DispatchTime.now().uptimeNanoseconds<br>        let nanos = Double(end - start)<br>        requestDuration.recordNanoseconds(nanos, dimensions: dims)<br>    }</pre><pre>    try work()<br>}</pre><p>This is the nice part: none of this code cares whether you export to StatsD, Prometheus, OpenTelemetry, or something else. That decision happens once, where it belongs.</p><h3>Bootstrapping a backend, and how to think about the options</h3><p>Because swift-metrics is an API layer, you need a concrete implementation at runtime if you want metrics to leave your process. In most setups, you pick one of three directions.</p><p>If you want something straightforward and widely supported, a StatsD-style backend can be a fast way to get metrics into your pipeline with minimal effort. If you live in a Prometheus world, you’ll typically use a Prometheus-compatible backend and expose a /metrics endpoint in your server so Prometheus can scrape it. If you’re aiming for a future-proof telemetry pipeline, OpenTelemetry is attractive because it’s designed to unify metrics, logs, and traces across many vendors, and you can export using OTLP to a collector.</p><p>The bootstrap pattern is always the same conceptually: create your backend implementation and install it once at startup.</p><pre>import Metrics</pre><pre>@main<br>struct MyApp {<br>    static func main() async throws {<br>        // Pick one backend and bootstrap once here<br>        // MetricsSystem.bootstrap(MyMetricsBackend())</pre><pre>        // Start the rest of your app<br>    }<br>}</pre><p>Even if you change the backend later, your libraries and feature modules do not need to change. That’s the compounding benefit.</p><h3>Implementing a backend, or why this API stays stable</h3><p>Most people will never implement a backend, but understanding the shape of it helps you appreciate why swift-metrics is so flexible. Backends typically implement a factory that returns handlers for each metric type. Those handlers are what receive increments, recordings, and value updates. The API stays stable because it’s intentionally narrow; it does not try to encode the full surface area of every monitoring vendor. Instead, it encodes the smallest common contract that still maps well to real telemetry systems.</p><p>This is also why swift-metrics works nicely in library land: libraries don’t need to know anything about network transport, collectors, scraping endpoints, auth, or batching. They emit signals; the app owns export.</p><h3>Adding system and process metrics without writing OS-specific code</h3><p>Once you’re recording product metrics, you inevitably want the boring operational ones too: CPU time, RSS memory, file descriptors, process uptime, and other process-level signals that help you debug performance or resource leaks. The good news is you don’t have to reinvent that wheel. The Swift ecosystem includes packages like <strong>swift-system-metrics</strong> that can report process metrics through the same Metrics API, so they show up in the same dashboards and follow the same export pipeline. That’s another quietly great part of this design: it encourages a consistent flow of telemetry across both your code and the platform.</p><h3>Practical conventions that prevent metrics from becoming a mess</h3><p>Metrics rot is real. The only way to avoid it is to treat metric names and dimensions as part of your API. If you rename metrics every week, dashboards break and alerts become meaningless. If you let every engineer invent tags, you end up with inconsistent labels like userRoute vs route vs path. The simplest approach that scales is to define a small naming convention early, keep it boring, and enforce it in review.</p><p>Pick names that encode units, like _seconds for durations, and keep a consistent prefix for domains, like http_, db_, cache_, paywall_. For dimensions, define a canonical vocabulary. If you use route, always use route, not path sometimes. If you tag status, decide whether it’s status=200 or status_code=200 and stick with it.</p><p>Also, resist the urge to tag everything. Every new dimension can multiply your time series count. Keep tags purposeful. If a dimension doesn’t show up in a dashboard or alert, it’s probably not worth paying for.</p><h3>How I’d use swift-metrics in a real Swift backend today</h3><p>If you’re building a server, I’d start with a small set of foundational metrics: request count, request duration, error count, and one or two critical downstream latency metrics like DB query time. That gives you visibility into throughput, latency, and correctness. Then I’d add product-specific counters around conversion funnels or key flows, like onboarding completion or purchase success, because those are the metrics that actually connect engineering work to outcomes.</p><p>At that point you can make an informed choice about export. If you already run Prometheus, pick that. If you want a lightweight pipeline, StatsD can be enough. If you expect to evolve or you want to unify metrics with tracing later, I’d look seriously at OpenTelemetry and an OTLP collector. The beauty is you can start with one and migrate without rewriting your instrumentation.</p><p>If you want to dig deeper, here’s the repo I referenced</p><p><a href="https://github.com/apple/swift-metrics">https://github.com/apple/swift-metrics</a></p><p>I share more iOS and indie dev stuff here</p><p><a href="https://x.com/JustRouzbeh">https://x.com/JustRouzbeh</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c4a058e6909a" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Using Unix Sockets for Communication Between iOS Extensions and Main App]]></title>
            <link>https://medium.com/@JustRouzbeh/using-unix-sockets-for-communication-between-ios-extensions-and-main-app-27159bfc1144?source=rss-9044966abfac------2</link>
            <guid isPermaLink="false">https://medium.com/p/27159bfc1144</guid>
            <dc:creator><![CDATA[Rouzbeh Abadi]]></dc:creator>
            <pubDate>Fri, 11 Aug 2023 16:13:18 GMT</pubDate>
            <atom:updated>2026-01-08T20:08:58.263Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/690/1*GBNZwGs3VSxQ5k5o2vmcxg.png" /><figcaption>Image source: <a href="https://medium.com/swlh/getting-started-with-unix-domain-sockets-4472c0db4eb1">https://medium.com/swlh/getting-started-with-unix-domain-sockets-4472c0db4eb1</a></figcaption></figure><h3>Introduction</h3><p>In iOS development, communication between different parts of an application, such as an app extension and the main app, is often required. Various methods exist for this purpose, such as shared containers, named pipes, or even custom web servers. However, each of these methods comes with its trade-offs, and choosing the right one depends on the specific requirements of your project.</p><p>In this article, i’ll explore using Unix sockets for communication between an iOS extension and the main app. We’ll begin by discussing why you might choose this approach, its pros and cons, and then delve into the implementation details.</p><h3>Why Unix Sockets?</h3><h3>Pros:</h3><ol><li>Performance: Unix sockets provide low latency and high throughput, making them suitable for real-time communication.</li><li>Security: Using file system permissions, Unix sockets can be secured to limit access to specific users or groups.</li><li>Simplicity: Working with Unix sockets is relatively straightforward and doesn’t require complex setup.</li></ol><h3>Cons:</h3><ol><li>Platform Dependency: Unix sockets are specific to Unix-based systems, which limits their portability to other platforms.</li><li>Complexity in Error Handling: Handling various edge cases and error conditions might require more effort compared to other higher-level communication mechanisms.</li></ol><h3>Implementation</h3><p>The implementation will be divided into two parts: the Server (extension) and the Client (main app). Below, i’ll describe each function for both sides, and after each explanation, i’ll provide the corresponding code snippet.</p><h4>Server Side (Extension)</h4><p><strong>Obtain Container URL</strong></p><p>The first step in initializing the server side is to obtain the container URL. In iOS, the container URL provides a location where both the app and its extensions can read and write data. The app group identifier, specified in the app’s capabilities, is used to identify the shared container. This URL will form the basis for the socket path, and it’s essential to ensure that the app group identifier is correctly configured.</p><pre>guard let containerURL = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: appGroup) else {<br>    fatalError(&quot;Invalid app group identifier.&quot;)<br>}</pre><p><strong>Create Socket Path</strong></p><p>The socket path is a file system path that uniquely identifies the socket within the system. By appending the socket name to the container URL, you create a clear path to the socket file. This path is necessary for both the server and the client to establish a connection later on. If there are multiple sockets, each one must have a unique path.</p><pre>socketPath = containerURL.appendingPathComponent(socketName).path</pre><p><strong>Creating a Socket</strong></p><p>Creating a socket is a fundamental operation in socket programming. The Darwin.socket function is used to create a socket that adheres to the Unix domain (AF_UNIX) and is intended for stream communication (SOCK_STREAM). The function returns a file descriptor, which represents the socket internally within the system. This descriptor will be used in subsequent operations to reference the socket.</p><pre>socket = Darwin.socket(AF_UNIX, SOCK_STREAM, 0)</pre><p><strong>Binding the Socket</strong></p><p>Binding is the process of associating the socket with a specific address, in this case, the file system path. By calling Darwin.bind, the socket is tied to the path specified earlier, allowing clients to connect to it. If the socket file already exists from a previous connection, it must be removed first using unlink. The process ensures that there&#39;s no conflict with existing socket files, providing a clean slate for the connection.</p><pre>unlink(socketPath) // If socket file already exists<br>Darwin.bind(socket, withUnsafePointer(to: &amp;address, { $0.withMemoryRebound(to: sockaddr.self, capacity: 1) { $0 } }), socklen_t(MemoryLayout&lt;sockaddr_un&gt;.size))</pre><p><strong>Listening for Connections</strong></p><p>Listening is the act of waiting for incoming client connections. With Darwin.listen, the server socket is prepared to accept connection requests. The function takes two parameters: the socket file descriptor and the backlog, which defines the maximum number of pending connections that can be queued. In this case, a backlog of 1 means that only one connection request will be held in the queue at a time. Additional requests are rejected if the queue is full.</p><pre>Darwin.listen(socket, 1)</pre><p><strong>Accepting Connections</strong></p><p>Once the server is set up and listening for connections, it must have a way to accept incoming connection requests from clients. This is done using the Darwin.accept function, which waits for a connection request, accepts it, and returns a new socket descriptor that corresponds to the client connection. The server can handle this process asynchronously by utilizing a dispatch queue. This way, the server is free to continue with other tasks or accept additional connections.</p><pre>let clientSocket = Darwin.accept(socket, nil, nil)</pre><p><strong>Check Connection</strong></p><p>Before sending data to the client, the server should ensure that there is an established connection with the client. This might involve checking the validity of the client socket descriptor and ensuring it’s in the correct state for data transmission.</p><pre>guard clientSocket &gt; 0 else {<br>    print(&quot;No client connection&quot;)<br>    return<br>}</pre><p><strong>Prepare and Send Data</strong></p><p>The server can now send data to the connected client. This involves preparing the data, often converting it into a binary format, and then sending it through the socket using Darwin.send. The function requires the client socket descriptor, a pointer to the data, and the length of the data.</p><pre>let data: Data = // ... data to send<br>let result = data.withUnsafeBytes {<br>    Darwin.send(clientSocket, $0.baseAddress!, data.count, 0)<br>}</pre><h4>Client Side (Main App)</h4><p><strong>Initialization</strong></p><p>The initialization process on the client side mirrors the server side to a great extent. The client must also obtain the container URL using the same app group identifier, create the socket path, and proceed with creating the socket.</p><pre>// Obtaining container URL and creating socket path as done on the server side<br>let socket = Darwin.socket(AF_UNIX, SOCK_STREAM, 0)</pre><p><strong>Connecting to the Server</strong></p><p>The client attempts to connect to the server using the Darwin.connect function. This function requires the socket descriptor and the address to which the client wants to connect (the same path used by the server). Upon success, the client is connected to the server and ready to receive data.</p><pre>Darwin.connect(socket, withUnsafePointer(to: &amp;address, { $0.withMemoryRebound(to: sockaddr.self, capacity: 1) { $0 } }), socklen_t(MemoryLayout&lt;sockaddr_un&gt;.size))</pre><p><strong>Reading Data</strong></p><p>The client can read data from the server using the read function. This involves looping through the data stream, loading the data into a buffer, and then converting it into a Data object or other usable format. Proper buffering ensures smooth and efficient reading of the data.</p><pre>var buffer = [UInt8](repeating: 0, count: 1024)<br>let bytesRead = read(socket, &amp;buffer, buffer.count)<br>if bytesRead &gt; 0 {<br>    let data = Data(buffer)<br>    // Process the data as needed<br>}</pre><h4>Conclusion</h4><p>The process of setting up communication between an iOS extension and the main app using Unix sockets has been explained in detail. This method, although specific to Unix-based systems, offers advantages in performance, security, and simplicity. From initialization to handling connections, sending and receiving data, the procedures and code snippets provided cover the essential aspects required to establish this form of communication.</p><p>However, implementing Unix sockets in an actual project demands more than just following these basics. Here’s what you should consider next:</p><ol><li>Error Handling: Enhancing the error handling mechanisms, taking into account various edge cases, and ensuring that the sockets are closed properly can lead to a more resilient system.</li><li>Security Considerations: Depending on your application’s needs, you may need to implement additional security measures to restrict access to the sockets.</li><li>Performance Tuning: Testing and optimization for your specific use case are essential. This might include adjusting buffer sizes, handling multiple connections more efficiently, or optimizing the data transmission process.</li><li>Integration with Other Systems: Consider how this communication method will fit into your broader system architecture. Ensuring compatibility and smooth interaction with other components will be crucial.</li><li>Compliance with Platform Guidelines: Make sure to review and adhere to Apple’s guidelines and best practices for app development, especially if you’re planning to distribute the app through the App Store.</li><li>Further Reading and Learning: You may wish to explore additional resources, libraries, or tools that can simplify or enhance the use of Unix sockets within your application.</li><li>Customization: Tailor the implementation to fit your specific needs and requirements, whether it be a chat system, real-time data synchronization, or other forms of inter-process communication.</li></ol><p>To see the full code, please visit:</p><ul><li>Server (Extension): <a href="https://gist.github.com/rouzbeh-abadi/3cdda4959e2360f0f9e4dabd819dd170">Gist Link</a></li><li>Client (Main App): <a href="https://gist.github.com/rouzbeh-abadi/7e6650f48b6643605c012a04068df533">Gist Link</a></li></ul><p>This guide provides the fundamental understanding needed to adapt and extend this code to various use cases and requirements. By considering the above next steps, experimenting, and refining your approach, you can unlock the full potential of Unix sockets in your iOS development.</p><p>I share more iOS and indie dev stuff here</p><p><a href="https://x.com/JustRouzbeh">https://x.com/JustRouzbeh</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=27159bfc1144" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Real-time Image Histogram Generation with Swift and Metal: A Deep Dive]]></title>
            <link>https://medium.com/@JustRouzbeh/real-time-image-histogram-generation-with-swift-and-metal-a-deep-dive-bef69887a5a8?source=rss-9044966abfac------2</link>
            <guid isPermaLink="false">https://medium.com/p/bef69887a5a8</guid>
            <dc:creator><![CDATA[Rouzbeh Abadi]]></dc:creator>
            <pubDate>Fri, 14 Jul 2023 16:12:06 GMT</pubDate>
            <atom:updated>2026-01-08T20:09:35.809Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*vevmsUMxyF385y6MFT8eVg.jpeg" /></figure><p>The term “histogram” might take you back to the good old days of high school math, where it was introduced as a statistical graph representing a frequency distribution. In the context of image processing, a histogram is essentially the same. It serves as a graphical representation of the tonal distribution in a digital image. It plots the number of pixels for each tonal value, providing insights into the contrast, brightness, and intensity distribution of the image.</p><p>But why does this matter?</p><p>Well, image histograms play a pivotal role in image processing applications, allowing us to manipulate images to achieve desired effects. Whether it’s improving the contrast of a washed-out photo, or balancing the light in a poorly lit image, the histogram serves as our visual guide.</p><p>Now, let’s move on to our use case. We are dealing with video frames, meaning we’re working with a stream of images. This introduces an additional challenge: performance. To get a histogram in real-time, we need our application to be very efficient. Thus, we need to convert the pixel buffer image from the video to grayscale.</p><p>But why grayscale?</p><p>Converting to grayscale reduces the complexity. We take our 3 channel image (Red, Green, Blue) and transform it into a single channel image (Gray). This simplification means we don’t have to calculate the histogram separately for each color, but rather deal with a single merged histogram.</p><p>Given the real-time requirement, we don’t want to burden the CPU too much. Thankfully, Apple provides us with a high-performance, low-level API for GPU programming called Metal. This allows us to write code that runs directly on the GPU, enabling us to achieve our real-time goal.</p><p>Let’s take a look at a kernel function written in Metal, which converts an RGB image to grayscale:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c31348694303cf82f1050b751f01ac69/href">https://medium.com/media/c31348694303cf82f1050b751f01ac69/href</a></iframe><p>The kernel function grayscale that we use in Metal serves to convert an RGB image to grayscale. We define it to take in an input texture inTexture and an output texture outTexture. We also make use of the thread_position_in_grid qualifier to get the grid position of the thread, known as gid.</p><p>The function reads the color of the pixel at the grid position from the input texture. It then converts the color to grayscale using the dot product of the color and the BT.601 luma coefficients. This gives the grayscale value of the pixel. Finally, it writes the grayscale color to the output texture at the grid position.</p><p>We will now move on to explain the class MetalImageProcessor briefly. This class wraps up the process of converting an image to grayscale using the Metal API. It initializes a device, a command queue, and a pipeline state. It also includes a function named process(image:with:) that uses the command buffer and command encoder to convert the image to grayscale using the Metal pipeline.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/622c04e27c55cabe25ed976b6860e00c/href">https://medium.com/media/622c04e27c55cabe25ed976b6860e00c/href</a></iframe><p>Now, it’s time to build our HistogramProcessor class. This class is designed to work with our earlier created MetalImageProcessor to calculate the histogram of an image. The HistogramProcessor class will be responsible for transforming the image to grayscale, extracting pixel data, and finally calculating the histogram.</p><p>This class will include the main function, calculateHistogram(of image: CIImage) -&gt; [UInt]?, which calculates the histogram of a given image. To achieve this, the function needs to perform several steps which we&#39;ll explain in detail.</p><p>Let’s look at our HistogramProcessor class:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4569f2fb592a8105026c45b333ff9b92/href">https://medium.com/media/4569f2fb592a8105026c45b333ff9b92/href</a></iframe><p>Let’s dive into the function calculateHistogram(of:) now. This function is responsible for calculating the histogram of an image. Here’s how it works, step-by-step:</p><ol><li>First, it converts the given CIImage to grayscale using the process(image:with:) function of our MetalImageProcessor. If this process fails, it returns nil.</li><li>Next, it creates a CGImage from the grayscale CIImage. Again, if this fails, it returns nil.</li><li>It then calculates the total number of pixels in the image by multiplying the width and height of the image.</li><li>The function retrieves the pixel data from the CGImage through the dataProvider property. If there is no data, it returns nil.</li><li>It converts the pixel data into bytes. If this process fails, it returns nil.</li><li>With the pixel data in hand, it initializes an array named histogram of length 256, filling it with zeros. This array will be used to count the frequency of each pixel intensity.</li><li>Finally, the function iterates over all pixels in the image, reads the intensity value, and increments the corresponding element in the histogram array.</li></ol><p>This function ultimately returns the histogram — an array that represents the frequency of each pixel intensity in the image. If any step fails along the way, it returns nil.</p><p>You can use this function in real-time processing in your captureOutput(_:didOutput:from:) method. All you need to do is create an instance of the HistogramProcessor class and call the calculateHistogram(of:) function on each video frame.</p><p>This approach will allow you to visualize and manipulate the image data in real time, opening up endless possibilities for image processing and enhancements.</p><pre>import AVFoundation<br><br>class CaptureOutputDelegate: NSObject, AVCaptureVideoDataOutputSampleBufferDelegate {<br>    <br>    private var frameCounter = 0<br>    private let frameInterval = 30<br>    private let histogramProcessor = HistogramProcessor()<br>    private let processingQueue = DispatchQueue(label: &quot;com.example.HistogramProcessing&quot;, qos: .userInitiated)<br>    <br>    public func captureOutput(_ output: AVCaptureOutput, didOutput sampleBuffer: CMSampleBuffer, from connection: AVCaptureConnection) {<br>        <br>        // We only process one frame for every &#39;frameInterval&#39; frames.<br>        frameCounter += 1<br>        guard frameCounter % frameInterval == 0 else {<br>            return<br>        }<br>        <br>        // Convert the sampleBuffer to a CIImage<br>        guard let imageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer) else {<br>            return<br>        }<br>        <br>        let ciImage = CIImage(cvPixelBuffer: imageBuffer)<br>        <br>        // Calculate the histogram in the background<br>        processingQueue.async { [weak self] in<br>            guard let self = self else { return }<br>            if let histogram = self.histogramProcessor.calculateHistogram(of: ciImage) {<br>                // Do something with the histogram...<br>                // Make sure to dispatch back to main thread if you want to update the UI<br>                DispatchQueue.main.async {<br>                    // Update UI here...<br>                }<br>            }<br>        }<br>    }<br>}</pre><p>I share more iOS and indie dev stuff here</p><p><a href="https://x.com/JustRouzbeh">https://x.com/JustRouzbeh</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=bef69887a5a8" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Building custom shaders with Metal]]></title>
            <link>https://medium.com/@JustRouzbeh/building-custom-shaders-with-metal-854aae65458?source=rss-9044966abfac------2</link>
            <guid isPermaLink="false">https://medium.com/p/854aae65458</guid>
            <category><![CDATA[image-processing]]></category>
            <category><![CDATA[gpu-programming]]></category>
            <category><![CDATA[metal-shader]]></category>
            <category><![CDATA[swift-programming]]></category>
            <category><![CDATA[ios-development]]></category>
            <dc:creator><![CDATA[Rouzbeh Abadi]]></dc:creator>
            <pubDate>Mon, 15 May 2023 00:33:44 GMT</pubDate>
            <atom:updated>2026-01-08T20:08:26.505Z</atom:updated>
            <content:encoded><![CDATA[<p>In the world of computer graphics, shaders play an indispensable role in bringing visuals to life. They are powerful programs that run on the GPU and perform computations at each vertex and pixel of the graphics rendering pipeline. With Apple’s Metal framework, we can write custom shaders, which lets us tap directly into the GPU’s power for impressive performance. Here, we’re going to explore how to craft and utilize these custom shaders using Metal.</p><p><strong>Part 1: Understanding Metal shading language</strong></p><p>Metal Shading Language (MSL) is a single, unified language that allows you to write code that executes on the GPU, whether it’s part of a graphics or a compute pipeline. MSL’s syntax is heavily influenced by C++14, so if you’re familiar with C-based languages, you’ll find MSL quite intuitive. Shaders in Metal are typically categorized into vertex shaders, fragment shaders, and compute shaders. Each serves a distinct purpose in the rendering pipeline.</p><p><strong>Part 2: Setting Up a Metal Project</strong></p><p>Let’s dive in by setting up a basic Metal project in Xcode. First, create a new iOS project in Xcode, and make sure to import the Metal framework in your Swift files where you’ll be working with Metal.</p><pre>import Metal</pre><p>Next, initialize a device and create a command queue. This is a crucial part of setting up any Metal application.</p><pre>guard let device = MTLCreateSystemDefaultDevice(),<br>      let commandQueue = device.makeCommandQueue() else {<br>    fatalError(&quot;Unable to connect to GPU&quot;)<br>}</pre><p>This block of code is integral to setting up a Metal environment for your application. It starts by creating an object called device, which is essentially a representation of the GPU. This object is crucial for most Metal operations as it manages and controls the access to the GPU. The device object is created using a function called MTLCreateSystemDefaultDevice().</p><p>Once we have the device object, we then create a commandQueue. This queue is where the commands that will be executed by the GPU are held. It&#39;s created using a function called makeCommandQueue(), which is called on the device object.</p><p>The use of the guard let statement here is to handle cases where the system cannot access the GPU or create a command queue. If for some reason, MTLCreateSystemDefaultDevice() cannot access the GPU (for instance, if Metal is not supported on the device), it will return nil. Similarly, if makeCommandQueue() cannot create a command queue, it will return nil. If either of these situations occur, the fatalError() function will be called, and the program will be terminated. This is why it&#39;s extremely important to ensure that the devices your application targets support Metal.</p><p><strong>Part 3: Creating a Basic Shader</strong></p><p>We’re going to create a simple shader to transform an image into grayscale. This is a great example because it’s a common task in image processing, and it’s simple enough to understand even if you’re new to shaders.</p><p>First, we need to write our shader function in Metal Shading Language. Create a new ‘.metal’ file in your Xcode project, and let’s define our shader function there.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c31348694303cf82f1050b751f01ac69/href">https://medium.com/media/c31348694303cf82f1050b751f01ac69/href</a></iframe><p>This block of code is like a factory that takes in a colorful image and spits out a black and white, or grayscale, version of it. Here’s how it works:</p><ol><li>Input (inTexture): The inTexture parameter is our original, colorful image. You can think of it as a canvas we look at but never touch (hence, access::read). It&#39;s like an unopened box of crayons.</li><li>Output (outTexture): The outTexture parameter is our output image. This is the canvas where we&#39;ll do our drawing, or in this case, write our grayscale image (hence, access::write). It&#39;s like a coloring book waiting for our grayscale crayons.</li><li>Position (gid): The gid parameter represents the current pixel we&#39;re working on. It&#39;s like having a map telling us which part of the coloring book we&#39;re coloring at any given moment.</li></ol><p>Inside this factory, we have the following steps:</p><ol><li>Read Color: float4 color = inTexture.read(gid); This line reads the color of the current pixel from our box of crayons (input image).</li><li>Calculate Grayscale: float gray = dot(color.rgb, float3(0.299, 0.587, 0.114)); This line calculates the grayscale equivalent of the color. It&#39;s like taking a colorful crayon and figuring out what shade of gray matches it best.</li><li>Write Grayscale: outTexture.write(float4(gray, gray, gray, 1.0), gid); This line colors our grayscale image. We take our grayscale crayon and color in the right spot in our coloring book (output image).</li></ol><p>What’s cool about this is that it’s not limited to grayscale transformations. You could adjust the formula in the “Calculate Grayscale” step to emphasize certain colors. For example, you could make the red colors stand out more by increasing the 0.299 value. Or you could write a whole new formula to create different color effects, like a sepia filter. It’s like having an unlimited set of crayons and coloring books to create any image transformation you want.</p><p><strong>Diving Deeper — Image Derivatives and Saturation Adjustments</strong></p><p>As we’ve now mastered the basics of image transformations using custom shaders in Metal, let’s dive deeper into the realm of image processing. Two advanced operations that offer profound insights into the flexibility and power of Metal are calculating image derivatives and adjusting image saturation.</p><ul><li><strong>Calculating Image Derivatives</strong></li></ul><p>Image derivatives are a key concept in computer vision, used for edge detection and image segmentation. Using a custom Metal shader, we can calculate the derivatives for each pixel in an image, resulting in an output that highlights the edges. This operation involves convolving the input image with two 3x3 kernels, each responsible for calculating the horizontal and vertical derivatives. The magnitude of these derivatives is then computed and stored in an output texture.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*cJpFx5-dHm5QWtrwcPQInQ.png" /></figure><p>For a comprehensive explanation of this operation, check out this <a href="https://medium.com/@JustRouzbeh/calculating-image-derivatives-using-a-custom-metal-shader-in-swift-76fe5f71d294">Medium article</a> where I dissect the kernel function responsible for this image transformation.</p><ul><li><strong>Adjusting Image Saturation</strong></li></ul><p>Another fascinating image transformation that can be achieved using Metal is saturation adjustment. With a custom shader, we can either saturate or desaturate the colors in an image, providing a means to dynamically control its vibrancy. This operation works by converting each pixel’s RGB value to the HSV color space, modifying the saturation, and then converting it back to RGB.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*aaMWVpTPjXEMq6CirjPkSw.png" /></figure><p>For a more detailed walk-through of the process, head over to this <a href="https://medium.com/@JustRouzbeh/adjusting-saturation-with-a-custom-metal-shader-in-swift-4bb564ad6742">Medium article</a> that I explain the kernel function used to achieve this effect.</p><p><strong>Part 4: Using Your Shader</strong></p><p>In this part, you’re making the bridge between your Swift code and the GPU using Metal. Think of it as hiring a project manager (pipeline state) and a team of workers (command buffer and command encoder) to carry out the construction plans (shader function) you created earlier.</p><pre>// We start by asking the device to make a default library.<br>// Think of it as a bookshelf where your shader functions live.<br>guard let library = device.makeDefaultLibrary(),<br><br>// Then we look for the &#39;grayscale&#39; function on that bookshelf.<br>// It&#39;s like picking the right construction plan from the shelf.<br>      let function = library.makeFunction(name: &quot;grayscale&quot;),<br><br>// Using the &#39;grayscale&#39; function, we create a compute pipeline state.<br>// This is our project manager who knows how to execute the &#39;grayscale&#39; plan.<br>      let pipelineState = try? device.makeComputePipelineState(function: function) else {<br><br>// If anything goes wrong (like if we can&#39;t find the &#39;grayscale&#39; plan), we stop everything.<br>    fatalError(&quot;Failed to create pipeline state&quot;)<br>}</pre><p>This first part is all about setup. You’re making sure you have your shader function ready and a compute pipeline state that knows how to use it. If you’ve ever put together furniture, it’s like making sure you have the assembly instructions and all the right tools before you start.</p><p>The next part is about doing the actual work:</p><pre>// First, we create a command buffer. This is like a to-do list for the GPU.<br>let commandBuffer = commandQueue.makeCommandBuffer()<br><br>// Then, we create a command encoder. This is like a foreman who knows how to read the to-do list.<br>let commandEncoder = commandBuffer?.makeComputeCommandEncoder()<br><br>// We tell the foreman which construction plan (pipeline state) to use.<br>commandEncoder?.setComputePipelineState(pipelineState)<br><br>// And we hand over the input and output textures. These are like the raw materials and the place where the final product will go.<br>commandEncoder?.setTexture(inputTexture, index: 0)<br>commandEncoder?.setTexture(outputTexture, index: 1)<br><br>// Finally, we tell the foreman to start the job.<br>commandEncoder?.dispatchThreadgroups(threadgroups, threadsPerThreadgroup: threadsPerThreadgroup)<br><br>// And we make sure to clean up after the job is done.<br>commandEncoder?.endEncoding()<br>commandBuffer?.commit()</pre><p>In this part, you’re filling out the to-do list (command buffer) and handing it over to the foreman (command encoder). The foreman knows which construction plan to follow (compute pipeline state) and has the raw materials (input texture) and a place to put the final product (output texture). When you call commit, it&#39;s like you&#39;re saying, &quot;Start the job!&quot;</p><p><strong>Part 5: A Helper Class for Image Processing with Metal</strong></p><p>After exploring the nitty-gritty of writing and executing shaders with Metal, it’s time to discuss how we can abstract away some of the complexity and streamline the image processing workflow.</p><p>For this purpose, I’ve created a Swift helper class named MetalImageProcessing. It&#39;s a singleton class that encapsulates operations related to image processing using Metal. This class handles the setup of the Metal environment, including initializing the Metal device and command queue.</p><p>Moreover, it provides a high-level interface to apply a variety of filters (e.g., grayscale, derivatives, saturation) to a UIImage. The class is designed to handle the conversion between UIImage and Metal texture, the configuration of GPU threads, and the retrieval of output after processing, all while managing potential errors gracefully.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/dfa7a281e175b0a33fa4f4d1e78196db/href">https://medium.com/media/dfa7a281e175b0a33fa4f4d1e78196db/href</a></iframe><p>With MetalImageProcessing, you don&#39;t need to worry about the underlying Metal setup or shader execution. You can focus on the task at hand - image processing - and leave the GPU details to this helper class.</p><p>It’s worth noting that the MetalImageProcessing class is flexible and extensible. As your needs grow, you can easily add more filters by writing new shader functions and extending the Filter enum and the apply(filter:to:) method accordingly.</p><p>To see the complete implementation of the MetalImageProcessing class and learn how to integrate it into your projects, you can check out the <a href="https://github.com/rouzbeh-abadi/MetalCustomShaders/blob/main/MetalCustomShaders/Metal/Handlers/MetalImageProcessing.swift">MetalImageProcessing.swift</a> file on GitHub.</p><h3>Wrapping Up</h3><p>In this journey through the world of custom shaders with Metal, we’ve explored the power and flexibility of GPU programming for image processing. By tapping into Metal’s capabilities, we can harness the raw processing power of the GPU and unlock impressive performance gains in our applications.</p><p>We started by understanding Metal’s shading language and the different types of shaders that play a crucial role in the rendering pipeline. We then delved into setting up a Metal project, initializing the Metal device, and creating a command queue to interact with the GPU effectively.</p><p>Next, we created a basic shader to transform an image into grayscale, giving us a hands-on experience with writing Metal shaders. We saw how shaders are like factories, taking inputs, performing computations, and generating outputs, all executed on the GPU.</p><p>But we didn’t stop there! We explored more advanced image processing operations using Metal. We discovered how to calculate image derivatives to identify edges and perform image segmentation. Additionally, we learned how to adjust image saturation to control color vibrancy and create visually stunning effects.</p><p>To simplify the integration of custom shaders into your projects, we introduced the MetalImageProcessing class. This helper class encapsulates the complexities of Metal setup, texture conversion, thread configuration, and output retrieval. With just a few lines of code, you can apply various filters to your images, leaving the GPU details to the class.</p><p>To delve deeper into the code and gain a complete understanding of the implementation, you can explore the <a href="https://github.com/rouzbeh-abadi/MetalCustomShaders">MetalCustomShaders GitHub repository</a>. There, you’ll find the complete code, usage examples, and the MetalImageProcessing.swift file that contains the helper class.</p><p>Stay connected and follow me on <a href="https://twitter.com/JustRouzbeh">X</a> for more updates and insights into the fascinating world of coding.</p><p>With Metal’s power in your hands and the knowledge gained from this exploration, you’re well-equipped to take your image processing skills to new heights. Happy coding and happy shader crafting!</p><p>I share more iOS and indie dev stuff here</p><p><a href="https://x.com/JustRouzbeh">https://x.com/JustRouzbeh</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=854aae65458" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Calculating Image Derivatives Using a Custom Metal Shader in Swift]]></title>
            <link>https://medium.com/@JustRouzbeh/calculating-image-derivatives-using-a-custom-metal-shader-in-swift-76fe5f71d294?source=rss-9044966abfac------2</link>
            <guid isPermaLink="false">https://medium.com/p/76fe5f71d294</guid>
            <category><![CDATA[swift-programming]]></category>
            <category><![CDATA[ios-development]]></category>
            <category><![CDATA[image-processing]]></category>
            <category><![CDATA[gpu-programming]]></category>
            <dc:creator><![CDATA[Rouzbeh Abadi]]></dc:creator>
            <pubDate>Mon, 15 May 2023 00:01:47 GMT</pubDate>
            <atom:updated>2023-05-15T00:01:47.223Z</atom:updated>
            <content:encoded><![CDATA[<p>Hello, fellow Swift developers! Today, we delve into a fascinating journey, exploring the formidable capabilities of Apple’s Metal framework. Our mission is focused on image processing, specifically, we’ll be computing image derivatives — an operation fundamental to numerous computer vision tasks such as edge detection and image analysis.</p><p>For those new to this, Metal is a high-performance, low-level graphics and compute API that provides near-direct access to the GPU, enabling efficient graphics rendering and data-parallel computation.</p><h3>Complete Kernel Code</h3><p>Let’s start with the complete Metal shader code. We’ll dissect this code in the sections that follow:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4979e3f434b426f876befe406cb5f909/href">https://medium.com/media/4979e3f434b426f876befe406cb5f909/href</a></iframe><h3>Deep Dive into the Metal Kernel Function</h3><p>At the core of our Metal compute shader is a kernel function named derivatives. This function calculates the horizontal and vertical derivatives of an image using two distinct convolution kernels. The magnitude of these derivatives is then computed and stored in an output texture. Let&#39;s explore this further.</p><p>The derivatives function receives three primary arguments:</p><ol><li>inTexture - an input texture from which data will be read.</li><li>outTexture - an output texture where the result will be written.</li><li>gid - the grid position of the thread.</li></ol><p>The kernel function operates on each pixel of the image, excluding those near the edge to avoid boundary conditions. This is achieved by setting the radius of the convolution kernel and ensuring the kernel does not exceed the image boundaries.</p><p>Two predefined 3x3 convolution kernels — horizontal_kernel and vertical_kernel - are used to calculate the horizontal and vertical derivatives respectively. These derivatives are stored in result_horizontal and result_vertical.</p><p>To calculate the derivatives, each pixel in the neighborhood is convolved with the appropriate kernel. The RGB values of each pixel are multiplied with the corresponding kernel value and added to the result.</p><p>The RGB results are then converted to grayscale using the BT.601 standard. The magnitude of the gradient is calculated as the Euclidean distance between the horizontal and vertical grayscale values.</p><p>The final result, the magnitude, is written to the outTexture as a half-precision floating-point value. The gid parameter is used to specify the position at which the result is written.</p><h3>Integrating the Kernel into Your Swift Project</h3><p>With the kernel function defined, the next task is to set up the necessary structures and commands in your Swift code to interact with the Metal API. You’ll need to create a Metal device, command queue, command buffer, and command encoder. Once established, you can create a Metal compute pipeline state with the kernel function, and then dispatch threads to execute the function on the GPU.</p><h3>Seeing the Output</h3><p>Upon the kernel function’s execution, the output texture is populated with the gradient magnitude of the image. This texture can be converted to a UIImage (or CGImage) for display on your iOS device or Mac. The resultant image will showcase the edges in your original image, making it a basic edge detection mechanism.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*cJpFx5-dHm5QWtrwcPQInQ.png" /></figure><h3>Wrapping Up</h3><p>With Metal’s raw power, we’ve efficiently calculated the gradient magnitude of an image. This is a critical foundational step for more complex image analysis tasks. Remember, Metal’s true strength lies in its ability to carry out data parallel tasks on the GPU, making it a superb choice for operations such as image processing.</p><p>For the complete Metal shader and corresponding Swift code, please visit <a href="https://github.com/Rouzbeh/GradientMagnitudeMetal">this GitHub repository</a>. Don’t forget to follow me on <a href="https://twitter.com/JustRouzbeh">Twitter</a> for more updates and insights into the world of coding. Happy coding!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=76fe5f71d294" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Adjusting Saturation with a Custom Metal Shader in Swift]]></title>
            <link>https://medium.com/@JustRouzbeh/adjusting-saturation-with-a-custom-metal-shader-in-swift-4bb564ad6742?source=rss-9044966abfac------2</link>
            <guid isPermaLink="false">https://medium.com/p/4bb564ad6742</guid>
            <category><![CDATA[metal-shader]]></category>
            <category><![CDATA[swift-programming]]></category>
            <category><![CDATA[image-processing]]></category>
            <category><![CDATA[gpu-programming]]></category>
            <category><![CDATA[ios-development]]></category>
            <dc:creator><![CDATA[Rouzbeh Abadi]]></dc:creator>
            <pubDate>Sun, 14 May 2023 23:30:08 GMT</pubDate>
            <atom:updated>2023-05-14T23:30:08.448Z</atom:updated>
            <content:encoded><![CDATA[<p>Hello, Swift developers! We’re going to dive into the realm of image processing, focusing on how to create a custom Metal shader to adjust the saturation of an image. This can be particularly useful for developing image editing applications or adding image effect features to your existing app.</p><p>For those who might be new to Metal, it’s a high-performance, low-level API for processing graphics and data parallel tasks. It provides near-direct access to the GPU, enabling high-performance rendering and computational tasks.</p><h3>Setting the Stage</h3><p>Before we start adjusting image saturation, we need to create a structure in Swift that will serve as an input to our Metal kernel function. This structure will hold the saturation factor we wish to apply to our image.</p><p>Here’s what the structure looks like:</p><pre>/// Structure to encapsulate saturation factor for the Metal kernel function.<br>struct SaturationInput {<br>    var saturationFactor: Float ///&lt; Saturation factor to be applied to the image.<br>}</pre><p>The saturationFactor is a floating point value that will determine how much the saturation of the image should be adjusted. A value of 1 means the original saturation, values less than 1 will desaturate the image, and values greater than 1 will increase the saturation.</p><h3>Creating the Metal Kernel</h3><p>The kernel code is the centerpiece of the Metal shader, which does the heavy lifting of image processing. It is written in the Metal Shading Language, which is similar to C++14, and is executed directly on the GPU. You should place the kernel code in a ‘.metal’ file within your Xcode project. This file is typically found in the same directory as your Swift files.</p><p>Our kernel will consist of a series of helper functions and a main function called saturationAdjust which is responsible for reading the color of a pixel from the input texture, converting the color from the RGB color space to the HSV color space, adjusting the saturation of the color, and writing the adjusted color to the output texture.</p><p>You’ll find helper functions like rgb2hsv and hsv2rgb which are responsible for converting color models between RGB and HSV. The saturate_input struct in the kernel code corresponds to our SaturationInput struct in Swift.</p><h3>Adding an Input to the Kernel</h3><p>To use our SaturationInput structure with our Metal kernel, we first create an instance of it, then create a Metal buffer from it. The buffer can then be set as an input to the kernel function.</p><p>Here’s how you can do it:</p><pre>if let input = input {<br>    var metalInput = SaturationInput(saturationFactor: input.saturationFactor)<br>    let inputBuffer = device.makeBuffer(bytes: &amp;metalInput, length: MemoryLayout&lt;SaturationInput&gt;.stride, options: [])<br>    commandEncoder.setBuffer(inputBuffer, offset: 0, index: 0)<br>}</pre><p>In the above code, we’re creating a SaturationInput instance and passing the saturationFactor from our input. We then create a Metal buffer that contains the SaturationInput instance, and set it on the command encoder which will be used to encode commands for the GPU.</p><h3>Understanding the Kernel</h3><p>Now that we have our kernel in place and our inputs set up, let’s take a closer look at how the kernel code works. The saturationAdjust function operates in parallel on the GPU for each pixel of the image. It reads the color of a pixel from the input texture, then converts the color from the RGB color space to the HSV color space using the rgb2hsv function. The saturation component of the color (which is the second component of the float3 vector) is then adjusted by multiplying it with the saturation factor we passed in from our Swift code. We then ensure that the saturation remains within valid limits (between 0 and 1) by clamping it. The color is then converted back to the RGB color space using the hsv2rgb function. Finally, the adjusted color is written to the output texture at the grid position corresponding to the current pixel.</p><p>By manipulating each pixel’s color in parallel on the GPU, we can effectively adjust the saturation of the entire image in a highly efficient manner.</p><p>Let’s take a look at the kernel code and break it down:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8da393708257f9e622f119c1e38f5803/href">https://medium.com/media/8da393708257f9e622f119c1e38f5803/href</a></iframe><p>Now, let’s break down the Metal kernel code:</p><ol><li>#include &lt;metal_stdlib&gt; and using namespace metal;: The Metal standard library is included at the top of the file. This library provides a set of helper functions and types which are useful for common tasks in shader programming. The using namespace metal; directive makes it possible to use functions and types from the Metal standard library without having to prepend them with metal::.</li><li>struct saturate_input: This is a struct that encapsulates the saturation factor that we want to apply to the image. This struct will be passed from the Swift code to the Metal kernel. The saturationFactor field is a float that specifies the amount by which to adjust the saturation of the image.</li><li>rgb2hsv: This is a helper function that converts a color from the RGB color space to the HSV color space. It takes a float3 vector as input, where the components represent the red, green, and blue channels of a color, respectively. It returns a float3 vector, where the components represent the hue, saturation, and value of the color, respectively.</li><li>hsv2rgb: This is another helper function, but it converts colors from the HSV color space back to the RGB color space. It takes a float3 vector as input, where the components represent the hue, saturation, and value of a color, respectively. It returns a float3 vector, where the components represent the red, green, and blue channels of the color, respectively.</li><li>saturationAdjust: This is the main function of the shader. It is a kernel function, which means that it runs in parallel on the GPU for each pixel of the image. The function takes four parameters:</li></ol><ul><li>inputTexture: This is a 2D texture that represents the input image. We read the color of each pixel from this texture.</li><li>outputTexture: This is another 2D texture that represents the output image. We write the adjusted color to each pixel of this texture.</li><li>input: This is a reference to a saturate_input struct that holds the saturation factor.</li><li>gid: This is a 2D vector that represents the position of the current thread in the grid. Since the kernel function runs in parallel for each pixel of the image, we use gid to determine which pixel the current thread is responsible for.</li></ul><p>The saturationAdjust function works as follows: It first reads the color of a pixel from the input texture. It then converts the color from the RGB color space to the HSV color space using the rgb2hsv function. The saturation component of the color (the second component of the float3 vector) is then adjusted by multiplying it with the saturation factor. We then ensure that the saturation remains within valid limits (between 0 and 1) by clamping it. The color is then converted back to the RGB color space using the hsv2rgb function, and the adjusted color is written to the output texture.</p><h3>Putting it All Together</h3><p>With the Swift structure, kernel code, and input setup, you now have a custom Metal shader that can adjust the saturation of an image. Remember, the power of Metal lies in its ability to perform data parallel tasks on the GPU, making it an ideal choice for tasks such as image processing.</p><p>Now, to see the saturation adjustment in action, you can input your desired saturation factor and apply the saturationAdjust function to your image. The final image will have its saturation adjusted according to the provided saturation factor. Here&#39;s a visual representation of how it works:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*aaMWVpTPjXEMq6CirjPkSw.png" /></figure><p><strong>Conclusion</strong></p><p>Creating custom Metal shaders can be a powerful tool in your Swift development toolkit, especially when working with image processing tasks. We’ve walked through how to adjust an image’s saturation using Metal, demonstrating how to create a Swift structure to serve as an input, how to set up the Metal kernel, and how to implement and understand the kernel code.</p><p>By mastering these techniques, you’re opening up new possibilities not only for image manipulation, but also for any task that can benefit from the high-performance computation capabilities of the GPU using Metal. Whether you’re building a photo editing app or just looking to add image effect features to your existing project, custom Metal shaders can provide the performance and flexibility you need.</p><p>Remember, the complete Metal shader code for adjusting saturation can be found in the linked <a href="https://github.com/rouzbeh-abadi/MetalCustomShaders">GitHub repository</a>.</p><p>Now it’s your turn to experiment and see what you can create. Happy coding!</p><p>If you have any questions or want to share your own Metal shader projects, feel free to reach out to me on <a href="https://twitter.com/JustRouzbeh">Twitter</a>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=4bb564ad6742" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>