{"id":15910473,"url":"https://github.com/computer-graphics-tools/shared-graphics-tools","last_synced_at":"2025-03-17T15:31:10.143Z","repository":{"id":45553572,"uuid":"494464563","full_name":"computer-graphics-tools/shared-graphics-tools","owner":"computer-graphics-tools","description":"A set of tools for shared graphics data on iOS","archived":false,"fork":false,"pushed_at":"2024-07-03T13:27:35.000Z","size":528,"stargazers_count":16,"open_issues_count":0,"forks_count":2,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-03-13T00:51:21.280Z","etag":null,"topics":["computer-graphics","metal","swift"],"latest_commit_sha":null,"homepage":"","language":"Swift","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/computer-graphics-tools.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2022-05-20T12:57:14.000Z","updated_at":"2025-01-02T02:53:18.000Z","dependencies_parsed_at":"2024-10-27T15:16:13.261Z","dependency_job_id":"515ff33d-8267-4e1f-873c-643a8079c2a8","html_url":"https://github.com/computer-graphics-tools/shared-graphics-tools","commit_stats":{"total_commits":15,"total_committers":2,"mean_commits":7.5,"dds":0.06666666666666665,"last_synced_commit":"5be8cf7a7521f79878a55a762d30e3210421984f"},"previous_names":["computer-graphics-tools/shared-graphics-tools"],"tags_count":9,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/computer-graphics-tools%2Fshared-graphics-tools","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/computer-graphics-tools%2Fshared-graphics-tools/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/computer-graphics-tools%2Fshared-graphics-tools/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/computer-graphics-tools%2Fshared-graphics-tools/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/computer-graphics-tools","download_url":"https://codeload.github.com/computer-graphics-tools/shared-graphics-tools/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243865657,"owners_count":20360480,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["computer-graphics","metal","swift"],"created_at":"2024-10-06T15:06:34.042Z","updated_at":"2025-03-17T15:31:09.760Z","avatar_url":"https://github.com/computer-graphics-tools.png","language":"Swift","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Shared Graphics Tools\n\n[![Platform Compatibility](https://img.shields.io/badge/Platforms-iOS%20|%20macOS%20|%20watchOS%20|%20tvOS-brightgreen)](https://swift.org/platforms/)\n[![Swift Version](https://img.shields.io/badge/Swift-5.9-orange)](https://swift.org)\n\n\u003cp align=\"left\"\u003e\n    \u003cimg src=\"Sources/SharedGraphicsTools/SharedGraphicsTools.docc/Resources/documentation-art/shared-graphics-tools@2x.png\", width=\"120\"\u003e\n\u003c/p\u003e\n\n## Description\n\nA set of tools and extensions that allow sharing page-aligned memory allocation between different graphics APIs and provide view interop for them. This library helps [ZERO10](https://zero10.ar) to reduce memory traffic in its computer vision pipelines.\n\n## Usage\n\n### Reinterpret Graphics Data\n\nThis library introduces a `GraphicsDataProvider` protocol. Every type that conforms to it allows to make a view of its contents as:\n\n- `vImage_Buffer`\n- `CVPixelBuffer`\n- `MLMultiArray`\n- `MTLBuffer`\n- `MTLTexture`\n\nBy default, these types are `vImage_Buffer`, `CGContext`, `CGImage`, `CVPixelBuffer`, `IOSurface`, `MTLTexture`, and `UIImage`.\n\nThat means it is possible to create an `IOSurface` and reinterpret its contents, such as `CVPixelBuffer` or `vImage_Buffer`.\n\n```swift\nlet vImageBuffer: vImage_Buffer = try ioSurface.vImageBufferView()\nlet pixelBuffer: CVPixelBuffer = try vImageBuffer.cvPixelBufferView(cvPixelFormat: .type_32BGRA)\nlet texture: MTLTexture = try vImageBuffer.mtlTextureView(\n    device: context.device,\n    pixelFormat: .bgra8Unorm,\n    usage: [.shaderRead, .shaderWrite]\n)\n```\n\nThis is quite handy as it allows for the reduction of boilerplate code as well as memory copy operations.\n\n### Page-aligned Memory Buffer\n\nIn some cases, it is impossible to make an `MTLBuffer` or `MTLTexture` view on some graphics data as it is [requires the allocation pointer of the data to be page-aligned](https://developer.apple.com/documentation/metal/mtldevice/1433382-makebuffer). This requirement is done because internally, before the creation of `MTLBuffer`, Metal marks certain memory pages as accessible to GPU, and the shared memory address beginning should align with the page address.\n\nTo overcome such situations, `MTLSharedGraphicsBuffer` is provided. Inside, this class encapsulates the allocation of page-aligned memory and initialises an `MTLBuffer`, `MTLTexture`, `vImage_Buffer`, `CVPixelBuffer` and `CGContext` in such a way so they look at the same memory.\n\nGiven that, you can do such tricks as combining `CoreGraphics` rendering with `Metal` without memory transfers:\n\n```swift\nlet context = try MTLContext()\n\nlet sharedBuffer = try MTLSharedGraphicsBuffer(\n    device: context.device,\n    width: 600,\n    height: 600,\n    pixelFormat: .bgra8Unorm\n)\n\nlet rect = CGRect(\n    x: 125,\n    y: 125,\n    width: 300,\n    height: 300\n)\nlet whiteColor = CGColor(\n    red: 1,\n    green: 1,\n    blue: 1,\n    alpha: 1\n)\n\n// Draw with CoreGraphics\nsharedBuffer.cgContext.setFillColor(whiteColor)\nsharedBuffer.cgContext.fill(rect)\n\n// Continue with Metal\ntry context.schedule { commandBuffer in\n    someFancyMetalFilter.encode(\n        source: sharedBuffer.texture,\n        destination: resultTexture,\n        in: commandBuffer\n    )\n}\n```\n\n## License\n\n`SharedGraphicsTools` is licensed under [MIT license](LICENSE).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcomputer-graphics-tools%2Fshared-graphics-tools","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcomputer-graphics-tools%2Fshared-graphics-tools","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcomputer-graphics-tools%2Fshared-graphics-tools/lists"}