6

I am watching the introduction video's from Apple about Metal and MetalKit.

The sample code for the shaders has these double brackets like [[buffer(0)]] arguments. Why are there two brackets? Does it mean anything or is it just to indicate that there is keyword "buffer" following? There is no such construct in standard c++, is there?

vertex Vertex vertex_func(constant Vertex *vertices [[buffer(0)]],
                              constant Uniforms &uniforms [[buffer(1)]],
                              uint vid [[vertex_id]])

Also what would be a good 1 or 2 week fun project as an introduction into GP-GPU? Something manageable for a novice with good math skills but no artistic skills.

Eric Aya
  • 69,473
  • 35
  • 181
  • 253
user965972
  • 2,489
  • 2
  • 23
  • 39
  • 3
    Yes, this is a standard syntax. http://en.cppreference.com/w/cpp/language/attributes – jtbandes Dec 04 '16 at 20:29
  • 1
    Also, shameless plug for a playground that I wrote which might be helpful if you are just learning how to use Metal: https://github.com/jtbandes/Metalbrot.playground – jtbandes Dec 04 '16 at 20:30
  • Oh, so it's an _attribute_ that just says where to find the vertices variable. Wake up Brain. I was too hung up on the double [. Still, any idea why they choose two [ instead of just one? – user965972 Dec 04 '16 at 20:55
  • Probably to avoid ambiguities with regular array subscripting syntax. `int x[length];` vs. `int x[[some_attr]];`. – jtbandes Dec 04 '16 at 20:56
  • Probably. Still in a function header I would expect more doStuff([int] x) or doStuff(x: [Int]) and not doStuff(int x[length]). What would that even mean? Then again, I am also new to c++. If you put your answer in a reply, I'll upvote it. – user965972 Dec 04 '16 at 20:59

2 Answers2

7

These are called attributes, and their syntax and behavior are defined in section 7.6.1 of the C++ standard, Attribute syntax and semantics. They look like this in the grammar:

attribute-specifier:
    [ [ attribute-list ] ]
    alignment-specifier

The Metal shading language defines numerous attributes that allow you to associate various semantics with variables, struct/class members, and function arguments (including argument table bindings as in your question).

The Metal Shading Language Specification defers to the C++ standard on this, so the C++ standard really is the reference to consult:

The Metal programming language is based on the C++14 Specification (a.k.a., the ISO/IEC JTC1/SC22/ WG21 N4431 Language Specification) with specific extensions and restrictions. Please refer to the C++14 Specification for a detailed description of the language grammar.

warrenm
  • 31,094
  • 6
  • 92
  • 116
  • Was also going to add that the `[[buffer(0)]]` argument mentioned is created by the render command encoder. It sets up an argument table of resources to send to the shaders: https://developer.apple.com/library/prerelease/content/documentation/Miscellaneous/Conceptual/MetalProgrammingGuide/Render-Ctx/Render-Ctx.html#//apple_ref/doc/uid/TP40014221-CH7-SW10 So it can set up buffers, textures, and samplerStates to be referred to by subscript as this is doing with the `buffer[0]` and `buffer[1]`. – Janie Larson Dec 09 '16 at 15:30
  • The parameter attribute identifies the resource "slot" of the corresponding buffer object as set on the render command encoder at the time of the current draw call or compute command. – warrenm Dec 09 '16 at 17:00
3

With this [[ x ]] you declare att that are getting passed between shaders ans CPU

I quote:

The [[ … ]] syntax is used to declare attributes such as resource locations, shader inputs, and built-in variables that are passed back and forth between shaders and CPU

ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97