Code Monkey home page Code Monkey logo

d-metal-binding's Introduction

D-Metal-Binding

Bindings for the D language to Apple's Metal Graphics API

Those bindings were created by hand and includes comments from apple.developer documentations. If you wish to contrirbute, try to stick to what was done by keeping comments for easier developer understanding on what is happening. If possible, the binding must be user friendly as a programming language to learn Metal.

The Binding has a wrapper over it to make development easier. For example, most arrays are accessed on Objective-C using objectIndexedAtSubscript. However, this API is not natural for D. So, beyond the bindings, this also includes opIndex and opIndexAssign, making the API more familiar to Objective-C.

The binding is a little bit different from Objective-C too by giving g enum short names. For example instead of using MTLPixelFormatR8Unorm, this enum is accessed by using MTLPixelFormat.R8Unorm. This will make your code shorter and easier to read. Enums that has a number after the prefix starts with an underline. For example: MTLTextureType._2D.

Another detail on this API, using NSArray as the generic type for everything is counter intuitive, so, for making the API more self descriptive, this binding defines 2 concepts:

  1. NSArray_(T): This is an alias that resolves to NSArray. It does not create a type, and it is solely for documentation on function purposes. It is the type you use for interfacing with Objective-C.

  2. NSArrayD(T): This is a structt that wraps a NSArray with alias arr this. It is strongly typed for keeping your code less error prone. It also wraps opIndex and opApply for being able to use the foreach and index operators.

This binding was even further extended to make Objective-C interfacing easier. Whenever needing to interface to Objective-C via NSDictionary, NSArray, NSString, NSNumber or other kind of objects that D can easily simulate, you can just append .ns to your type. The following style is used to create a macro dictionary from D side:

MTLCompileOptions opts = MTLCompileOptions.alloc.initialize;
opts.preprocessorMacros = ["ARGS_TIER2": 0].ns;

This way, there's no need to kep dealing all the time with Objective-C API.

d-metal-binding's People

Contributors

ichordev avatar lunathefoxgirl avatar mrcsnm avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

ichordev inochi2d

d-metal-binding's Issues

A lot of the MTL API is missing alloc/init/release declarations.

A large part of the API has no way to release references to the classes created, this in my testing can result in some memory leaks.
A lower class than Object should probably be added, which provides just an interface to retain, release, autorelease and retainCount

The hello_triangle example doesn't produce an executable

Although it does compile and produces a static binary, I'm not really sure how to use the hello_triangle example, or what the main() function I need to write to use it would look like. I've written the equivalent in Objective-C, but that uses NSApplication, NSWindow etc which aren't included in this binding. Can this be documented or added to the examples?

Thank you!

_objc_msgSend_stret is undefined

When compiling the following small example, linking to SDL2 and d-metal-binding, a linker error is thrown that _objc_msgSend_stret is undefined.

import std.stdio;
import std.exception;
import bindbc.sdl;
import metal;
import objc.runtime;
import objc.meta;

void main() {

 // Init SDL2
 auto load = loadSDL();
 enforce(load != SDLSupport.noLibrary, "SDL2 not found!");
 SDL_Init(SDL_INIT_VIDEO);

 SDL_Window* window = SDL_CreateWindow("Metal Test", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 640, 480, SDL_WINDOW_METAL | SDL_WINDOW_ALLOW_HIGHDPI);
 SDL_MetalView view = SDL_Metal_CreateView(window);
 CAMetalLayer layer = cast(CAMetalLayer)SDL_Metal_GetLayer(view);
 writeln(layer.drawableSize());
 

 writeln("Edit source/app.d to start your project.");
}

Relevant output from dub

ld: Undefined symbols:
  _objc_msgSend_stret, referenced from:
      CAMetalLayer::drawableSize() in libmetal.a[12](objc.metal_gen.o)
clang: error: linker command failed with exit code 1 (use -v to see invocation)

Type 'Expression' not implemented: 'class'

Compiling on macOS ARM64 with LDC 1.32.0 yields this error:
d-metal-binding/source/objc/runtime.d(10,12): Error: Internal compiler error: Type 'Expression' not implemented: 'class'
Possibly a compiler bug?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.