0

I found this example of implementing Phong lightning in hlsl. It is first snippet where I see that strange syntax where you declare variables in hlsl like here:

float3 materialEmissive : EMISSIVE;
float3 materialAmbient : AMBIENT;

In usual instead of EMISSIVE or AMBIENT I used to declare position in register like:

float3 materialEmissive : register(c0);
float3 materialAmbient : register(c1);

Why would I declare variables like in example from link? I checked DirectX documentation, but didn't find whether EMMISIVE or AMBIENT are some key words in hlsl.

bartosz.baczek
  • 1,567
  • 1
  • 17
  • 32

1 Answers1

2

In this case, EMISSIVE and AMBIENT are so called semantics. They describe what that variable should contain (and not where it is stored).

The shader compiler can access these semantics to create a handle for the variable. E.g. the Effect Framework and older versions of DirectX let you specify global variables by their semantic name. This decouples the actual shader implementation (i.e. the variable name) from its interface to the outside world (i.e. the semantics).

Nico Schertler
  • 32,049
  • 4
  • 39
  • 70
  • The OP's example above isn't an example of [DirectX Standard Annotations and Semantics Reference](https://msdn.microsoft.com/en-us/library/windows/desktop/bb173004.aspx) which is used by the legacy Effects framework. It might have some meaning in a custom tool, but I'm fairly sure it's meaningless otherwise and the HLSL compiler just tolerates it as a custom semantic name. – Chuck Walbourn Sep 14 '15 at 20:01
  • Note that semantics like ``EMISSIVE`` and ``WORLDVIEWPROJ`` do have some meaning for the legacy Direct3D 9 fixed-function pipeline, but they aren't used for Direct3D 10+ shaders or for the Direct3D 9 programmable pipeline when you provide both a VS and a PS. – Chuck Walbourn Sep 14 '15 at 20:08
  • I don't see your point. They are definitely semantics (which can be used by [`GetVariableBySemantic`](https://msdn.microsoft.com/de-de/library/windows/desktop/ff476842(v=vs.85).aspx)). I didn't say that they trigger some automatic behavior. – Nico Schertler Sep 15 '15 at 06:12
  • Right, there's both 'semantics' and there's 'annotations', both of which are related to the legacy [Effects](https://github.com/Microsoft/FX11) system and not to HLSL or used by Direct3D itself. – Chuck Walbourn Sep 15 '15 at 07:35