HLSL

2. 키워드와 연산자

Awesome Red Tomato 2021. 12. 24. 14:14

1. 키워드

다음은 HLSL이 정의하는 모든 키워드의 목록이다.

asm bool compile const decl do
double else extern false float for
half if in inline inout int
matrix out pass pixelshader return sampler
shared static string struct technique texture
true tyepdef uniform vector vertexshader void
volatile while        

 

그리고 예약되어 있지만 실제로 쓰이지 않는 키워드들이다.

auto break case catch char class
const_cast continue default delete dynamic_cast enum
explicit friend goto long mutable namespace
new operator private protected public register
reinterpret_cast short signed sizeof static_cast switch
template this throw try typename union
unsigned using virtual      

 

 

2. 연산자

몇몇을 제외하고 C++과 동일하다.

[ ] . < > <= >= != == !
&& || ?: + += - -+ * *=
/ /= % %= ++ -- = () ,

 

첫째로, 나머지 연산자 %는 정수뿐 아니라 부동소수점 형식에도 작동한다. 다만 연산자 좌변과 우변의 부호가 같아야 한다.

둘째로, HLSL 연산자 중에는 성분별로 작동하는 것이 많다. 예를 들면 

float4 u = {1.0f, 0.0f, -3.0f, 1.0f};
float4 v = {-4.0f, 2.0f, 1.0f, 0.0f};

float4 sum = u + v; // sum = (-3.0f, 2.0f, -2.0f, 1.0f)

위와 같이 연산이 성분별로 수행된다.

 

 

3. 변수들의 승격(promotion) 규칙

이항 연산에서 좌변과 우변의 차원 수가 다르면 차원이 낮은 쪽이 높은 쪽으로 승격된다.

float x;
float3 y;

// x + y 는 float3으로 x가 승격된다.

승격은 HLSL 언어에 정의된 캐스팅 규칙에 따라 일어나는데, x는 승격될 때 (x, x, x)로 캐스팅 된다. 고로 캐스팅이 정의되어있지 않으면(float2->float3 등) 승격이 불가능하다.

 

또, 이항 연산에서 좌변과 우변의 형식이 다르면 해상도(정밀도)가 낮은 쪽이 높은 쪽으로 승격된다.

int x;
half y;

// x + y에서 x는 half로  승격된다.