-1

What is the sampler object in MSL from the language perspective?

sampler s(coord::pixel, address::clamp_to_zero, filter::linear);

I don't know C++ good enough to parse what is going on here. If I understand correctly sampler is a struct-type. But I only worked with C-structs and the syntax above is completely alien to me. Is this some kind of well-known pattern in C++? Some newer C++ feature? How is it called?

I only know pure C struct initialization syntax like so:

MY_TYPE a = { .stuff = 0.456, .flag = true, .value = 123 };
simd
  • 1,779
  • 3
  • 17
  • 23
  • 3
    It is a [constructor](https://en.cppreference.com/w/cpp/language/constructor) for a sampler class and it is as basic a language structure as you can get in C++. So before you start using C++ you really should learn a bit more about it. – Pepijn Kramer Aug 27 '23 at 11:17
  • 2
    Good sources to learn cpp from are : A [recent C++ book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) or have a go at https://www.learncpp.com/ (that's pretty decent, and pretty up-to-date). For C++ reference material use : [cppreference](https://en.cppreference.com/w/). And after you learned the C++ basics from those sources, look at the [C++ coreguidelines](https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines) regularely to keep up-to-date with the latest guidelines. – Pepijn Kramer Aug 27 '23 at 11:18
  • _"Some newer C++ feature?"_ No? – πάντα ῥεῖ Aug 27 '23 at 11:24
  • @PepijnKramer I probably should, thank you! Metal Shading Language is based on C++, but it's very specialized and doesn't use much OOP or other complex features, so I was able to program on it with just pure C knowledge so far :) – simd Aug 27 '23 at 11:26
  • No idea what metal uses from C++ but C++ isn't only about OOP ;) It has some very nice other features as well, like templates, lambda functions and more. Compile time function evaluation (constexpr). I would say it is worth learning regardless :) – Pepijn Kramer Aug 27 '23 at 11:32
  • @PepijnKramer yeah, MSL actually uses constexpr extensively. In fact samplers usually declared with constexpr. I'll take a tour in C++ basics from your links to never embarrass myself like this :) – simd Aug 27 '23 at 11:37
  • Don't worry too much, happy learning – Pepijn Kramer Aug 27 '23 at 11:50

1 Answers1

0
sampler s(coord::pixel, address::clamp_to_zero, filter::linear);

sampler Is a type indicating that you're declaring a sampler object.

coord::pixel This likely refers to the coordinate mode used when sampling the texture. It might indicate that pixel coordinates (integer coordinates) are being used for sampling.

address::clamp_to_zero This refers to the addressing mode for the sampler. "Clamp to zero" could be a method of handling texture coordinates that fall outside the valid range by setting them to zero.

filter::linear This specifies the filtering method used when sampling the texture. "Linear" filtering typically involves blending neighboring texels to compute the sampled value.

is not part of the standard C++. It is specific to graphics like HLSL and MSL, where it's used to define sampler objects. This syntax is not a well-known pattern in general C++ , and it's not a newer C++ feature either. It's a construct designed for a particular domain of programming, which is graphics and shader development.

Puzyrin
  • 51
  • 3