clang 22.0.0git
hlsl_intrinsics.h
Go to the documentation of this file.
1//===----- hlsl_intrinsics.h - HLSL definitions for intrinsics ----------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#ifndef _HLSL_HLSL_INTRINSICS_H_
10#define _HLSL_HLSL_INTRINSICS_H_
11
13
14namespace hlsl {
15
16//===----------------------------------------------------------------------===//
17// asfloat builtins
18//===----------------------------------------------------------------------===//
19
20/// \fn float asfloat(T Val)
21/// \brief Interprets the bit pattern of x as float point number.
22/// \param Val The input value.
23
24template <typename T, int N>
25constexpr vector<float, N> asfloat(vector<T, N> V) {
27}
28
29template <typename T> constexpr float asfloat(T F) {
31}
32
33//===----------------------------------------------------------------------===//
34// asint builtins
35//===----------------------------------------------------------------------===//
36
37/// \fn int asint(T Val)
38/// \brief Interprets the bit pattern of x as an integer.
39/// \param Val The input value.
40
41template <typename T, int N> constexpr vector<int, N> asint(vector<T, N> V) {
43}
44
45template <typename T> constexpr int asint(T F) {
47}
48
49//===----------------------------------------------------------------------===//
50// asint16 builtins
51//===----------------------------------------------------------------------===//
52
53/// \fn int16_t asint16(T X)
54/// \brief Interprets the bit pattern of \a X as an 16-bit integer.
55/// \param X The input value.
56
57#ifdef __HLSL_ENABLE_16_BIT
58
59template <typename T, int N>
60_HLSL_16BIT_AVAILABILITY(shadermodel, 6.2)
61constexpr __detail::enable_if_t<__detail::is_same<int16_t, T>::value ||
62 __detail::is_same<uint16_t, T>::value ||
63 __detail::is_same<half, T>::value,
64 vector<int16_t, N>> asint16(vector<T, N> V) {
66}
67
68template <typename T>
69_HLSL_16BIT_AVAILABILITY(shadermodel, 6.2)
70constexpr __detail::enable_if_t<__detail::is_same<int16_t, T>::value ||
71 __detail::is_same<uint16_t, T>::value ||
72 __detail::is_same<half, T>::value,
73 int16_t> asint16(T F) {
75}
76#endif
77
78//===----------------------------------------------------------------------===//
79// asuint builtins
80//===----------------------------------------------------------------------===//
81
82/// \fn uint asuint(T Val)
83/// \brief Interprets the bit pattern of x as an unsigned integer.
84/// \param Val The input value.
85
86template <typename T, int N> constexpr vector<uint, N> asuint(vector<T, N> V) {
88}
89
90template <typename T> constexpr uint asuint(T F) {
92}
93
94//===----------------------------------------------------------------------===//
95// asuint splitdouble builtins
96//===----------------------------------------------------------------------===//
97
98/// \fn void asuint(double D, out uint lowbits, out int highbits)
99/// \brief Split and interprets the lowbits and highbits of double D into uints.
100/// \param D The input double.
101/// \param lowbits The output lowbits of D.
102/// \param highbits The output highbits of D.
103_HLSL_BUILTIN_ALIAS(__builtin_hlsl_elementwise_splitdouble)
104void asuint(double, out uint, out uint);
105_HLSL_BUILTIN_ALIAS(__builtin_hlsl_elementwise_splitdouble)
106void asuint(double2, out uint2, out uint2);
107_HLSL_BUILTIN_ALIAS(__builtin_hlsl_elementwise_splitdouble)
108void asuint(double3, out uint3, out uint3);
109_HLSL_BUILTIN_ALIAS(__builtin_hlsl_elementwise_splitdouble)
110void asuint(double4, out uint4, out uint4);
111
112//===----------------------------------------------------------------------===//
113// asuint16 builtins
114//===----------------------------------------------------------------------===//
115
116/// \fn uint16_t asuint16(T X)
117/// \brief Interprets the bit pattern of \a X as an 16-bit unsigned integer.
118/// \param X The input value.
119
120#ifdef __HLSL_ENABLE_16_BIT
121
122template <typename T, int N>
123_HLSL_16BIT_AVAILABILITY(shadermodel, 6.2)
124constexpr __detail::enable_if_t<__detail::is_same<int16_t, T>::value ||
125 __detail::is_same<uint16_t, T>::value ||
126 __detail::is_same<half, T>::value,
127 vector<uint16_t, N>> asuint16(vector<T, N> V) {
129}
130
131template <typename T>
132_HLSL_16BIT_AVAILABILITY(shadermodel, 6.2)
133constexpr __detail::enable_if_t<__detail::is_same<int16_t, T>::value ||
134 __detail::is_same<uint16_t, T>::value ||
135 __detail::is_same<half, T>::value,
136 uint16_t> asuint16(T F) {
138}
139#endif
140
141//===----------------------------------------------------------------------===//
142// distance builtins
143//===----------------------------------------------------------------------===//
144
145/// \fn K distance(T X, T Y)
146/// \brief Returns a distance scalar between \a X and \a Y.
147/// \param X The X input value.
148/// \param Y The Y input value.
149
150template <typename T>
151_HLSL_16BIT_AVAILABILITY(shadermodel, 6.2)
152const inline __detail::enable_if_t<__detail::is_arithmetic<T>::Value &&
153 __detail::is_same<half, T>::value,
154 T> distance(T X, T Y) {
155 return __detail::distance_impl(X, Y);
156}
157
158template <typename T>
159const inline __detail::enable_if_t<
161distance(T X, T Y) {
162 return __detail::distance_impl(X, Y);
163}
164
165template <int N>
166_HLSL_16BIT_AVAILABILITY(shadermodel, 6.2)
167const inline half distance(__detail::HLSL_FIXED_VECTOR<half, N> X,
168 __detail::HLSL_FIXED_VECTOR<half, N> Y) {
170}
171
172template <int N>
177
178//===----------------------------------------------------------------------===//
179// dot2add builtins
180//===----------------------------------------------------------------------===//
181
182/// \fn float dot2add(half2 A, half2 B, float C)
183/// \brief Dot product of 2 vector of type half and add a float scalar value.
184/// \param A The first input value to dot product.
185/// \param B The second input value to dot product.
186/// \param C The input value added to the dot product.
187
188_HLSL_AVAILABILITY(shadermodel, 6.4)
189const inline float dot2add(half2 A, half2 B, float C) {
190 return __detail::dot2add_impl(A, B, C);
191}
192
193//===----------------------------------------------------------------------===//
194// dst builtins
195//===----------------------------------------------------------------------===//
196
197/// \fn vector<T, 4> dst(vector<T, 4>, vector<T, 4>)
198/// \brief Calculates a distance vector.
199/// \param Src0 [in] Contains the squared distance
200/// \param Src1 [in] Contains the reciprocal distance
201///
202/// Return the computed distance vector
203
204_HLSL_16BIT_AVAILABILITY(shadermodel, 6.2)
205const inline half4 dst(half4 Src0, half4 Src1) {
206 return __detail::dst_impl(Src0, Src1);
207}
208
209const inline float4 dst(float4 Src0, float4 Src1) {
210 return __detail::dst_impl(Src0, Src1);
211}
212
213const inline double4 dst(double4 Src0, double4 Src1) {
214 return __detail::dst_impl(Src0, Src1);
215}
216
217//===----------------------------------------------------------------------===//
218// faceforward builtin
219//===----------------------------------------------------------------------===//
220
221/// \fn T faceforward(T N, T I, T Ng)
222/// \brief Flips the surface-normal (if needed) to face in a direction opposite
223/// to \a I. Returns the result in terms of \a N.
224/// \param N The resulting floating-point surface-normal vector.
225/// \param I A floating-point, incident vector that points from the view
226/// position to the shading position.
227/// \param Ng A floating-point surface-normal vector.
228///
229/// Return a floating-point, surface normal vector that is facing the view
230/// direction.
231
232template <typename T>
233_HLSL_16BIT_AVAILABILITY(shadermodel, 6.2)
234const inline __detail::enable_if_t<__detail::is_arithmetic<T>::Value &&
235 __detail::is_same<half, T>::value,
236 T> faceforward(T N, T I, T Ng) {
237 return __detail::faceforward_impl(N, I, Ng);
238}
239
240template <typename T>
241const inline __detail::enable_if_t<
243faceforward(T N, T I, T Ng) {
244 return __detail::faceforward_impl(N, I, Ng);
245}
246
247template <int L>
248_HLSL_16BIT_AVAILABILITY(shadermodel, 6.2)
249const inline __detail::HLSL_FIXED_VECTOR<half, L> faceforward(
250 __detail::HLSL_FIXED_VECTOR<half, L> N,
251 __detail::HLSL_FIXED_VECTOR<half, L> I,
252 __detail::HLSL_FIXED_VECTOR<half, L> Ng) {
253 return __detail::faceforward_impl(N, I, Ng);
254}
255
256template <int L>
263
264//===----------------------------------------------------------------------===//
265// fmod builtins
266//===----------------------------------------------------------------------===//
267
268/// \fn T fmod(T x, T y)
269/// \brief Returns the linear interpolation of x to y.
270/// \param x [in] The dividend.
271/// \param y [in] The divisor.
272///
273/// Return the floating-point remainder of the x parameter divided by the y
274/// parameter.
275
276template <typename T>
277_HLSL_16BIT_AVAILABILITY(shadermodel, 6.2)
278const inline __detail::enable_if_t<__detail::is_arithmetic<T>::Value &&
279 __detail::is_same<half, T>::value,
280 T> fmod(T X, T Y) {
281 return __detail::fmod_impl(X, Y);
282}
283
284template <typename T>
285const inline __detail::enable_if_t<
287fmod(T X, T Y) {
288 return __detail::fmod_impl(X, Y);
289}
290
291template <int N>
292_HLSL_16BIT_AVAILABILITY(shadermodel, 6.2)
293const inline __detail::HLSL_FIXED_VECTOR<half, N> fmod(
294 __detail::HLSL_FIXED_VECTOR<half, N> X,
295 __detail::HLSL_FIXED_VECTOR<half, N> Y) {
296 return __detail::fmod_vec_impl(X, Y);
297}
298
299template <int N>
305
306//===----------------------------------------------------------------------===//
307// ldexp builtins
308//===----------------------------------------------------------------------===//
309
310/// \fn T ldexp(T X, T Exp)
311/// \brief Returns the result of multiplying the specified value by two raised
312/// to the power of the specified exponent.
313/// \param X [in] The specified value.
314/// \param Exp [in] The specified exponent.
315///
316/// This function uses the following formula: X * 2^Exp
317
318template <typename T>
319_HLSL_16BIT_AVAILABILITY(shadermodel, 6.2)
320const inline __detail::enable_if_t<__detail::is_arithmetic<T>::Value &&
321 __detail::is_same<half, T>::value,
322 T> ldexp(T X, T Exp) {
323 return __detail::ldexp_impl(X, Exp);
324}
325
326template <typename T>
327const inline __detail::enable_if_t<
329ldexp(T X, T Exp) {
330 return __detail::ldexp_impl(X, Exp);
331}
332
333template <int N>
334_HLSL_16BIT_AVAILABILITY(shadermodel, 6.2)
335const inline __detail::HLSL_FIXED_VECTOR<half, N> ldexp(
336 __detail::HLSL_FIXED_VECTOR<half, N> X,
337 __detail::HLSL_FIXED_VECTOR<half, N> Exp) {
338 return __detail::ldexp_impl(X, Exp);
339}
340
341template <int N>
347
348//===----------------------------------------------------------------------===//
349// length builtins
350//===----------------------------------------------------------------------===//
351
352/// \fn T length(T x)
353/// \brief Returns the length of the specified floating-point vector.
354/// \param x [in] The vector of floats, or a scalar float.
355///
356/// Length is based on the following formula: sqrt(x[0]^2 + x[1]^2 + ...).
357
358template <typename T>
359_HLSL_16BIT_AVAILABILITY(shadermodel, 6.2)
360const inline __detail::enable_if_t<__detail::is_arithmetic<T>::Value &&
361 __detail::is_same<half, T>::value,
362 T> length(T X) {
363 return __detail::length_impl(X);
364}
365
366template <typename T>
367const inline __detail::enable_if_t<
370 return __detail::length_impl(X);
371}
372
373template <int N>
374_HLSL_16BIT_AVAILABILITY(shadermodel, 6.2)
375const inline half length(__detail::HLSL_FIXED_VECTOR<half, N> X) {
377}
378
379template <int N>
383
384//===----------------------------------------------------------------------===//
385// lit builtins
386//===----------------------------------------------------------------------===//
387
388/// \fn vector<T, 4> lit(T NDotL, T NDotH, T M)
389/// \brief Returns a lighting coefficient vector.
390/// \param NDotL The dot product of the normalized surface normal and the
391/// light vector.
392/// \param NDotH The dot product of the half-angle vector and the surface
393/// normal.
394/// \param M A specular exponent.
395///
396/// This function returns a lighting coefficient vector (ambient, diffuse,
397/// specular, 1).
398
399_HLSL_16BIT_AVAILABILITY(shadermodel, 6.2)
400const inline half4 lit(half NDotL, half NDotH, half M) {
401 return __detail::lit_impl(NDotL, NDotH, M);
402}
403
404const inline float4 lit(float NDotL, float NDotH, float M) {
405 return __detail::lit_impl(NDotL, NDotH, M);
406}
407
408//===----------------------------------------------------------------------===//
409// D3DCOLORtoUBYTE4 builtin
410//===----------------------------------------------------------------------===//
411
412/// \fn T D3DCOLORtoUBYTE4(T x)
413/// \brief Converts a floating-point, 4D vector set by a D3DCOLOR to a UBYTE4.
414/// \param x [in] The floating-point vector4 to convert.
415///
416/// The return value is the UBYTE4 representation of the \a x parameter.
417///
418/// This function swizzles and scales components of the \a x parameter. Use this
419/// function to compensate for the lack of UBYTE4 support in some hardware.
420
424
425//===----------------------------------------------------------------------===//
426// reflect builtin
427//===----------------------------------------------------------------------===//
428
429/// \fn T reflect(T I, T N)
430/// \brief Returns a reflection using an incident ray, \a I, and a surface
431/// normal, \a N.
432/// \param I The incident ray.
433/// \param N The surface normal.
434///
435/// The return value is a floating-point vector that represents the reflection
436/// of the incident ray, \a I, off a surface with the normal \a N.
437///
438/// This function calculates the reflection vector using the following formula:
439/// V = I - 2 * N * dot(I N) .
440///
441/// N must already be normalized in order to achieve the desired result.
442///
443/// The operands must all be a scalar or vector whose component type is
444/// floating-point.
445///
446/// Result type and the type of all operands must be the same type.
447
448template <typename T>
449_HLSL_16BIT_AVAILABILITY(shadermodel, 6.2)
450const inline __detail::enable_if_t<__detail::is_arithmetic<T>::Value &&
451 __detail::is_same<half, T>::value,
452 T> reflect(T I, T N) {
453 return __detail::reflect_impl(I, N);
454}
455
456template <typename T>
457const inline __detail::enable_if_t<
459reflect(T I, T N) {
460 return __detail::reflect_impl(I, N);
461}
462
463template <int L>
464_HLSL_16BIT_AVAILABILITY(shadermodel, 6.2)
465const inline __detail::HLSL_FIXED_VECTOR<half, L> reflect(
466 __detail::HLSL_FIXED_VECTOR<half, L> I,
467 __detail::HLSL_FIXED_VECTOR<half, L> N) {
468 return __detail::reflect_vec_impl(I, N);
469}
470
471template <int L>
477
478//===----------------------------------------------------------------------===//
479// refract builtin
480//===----------------------------------------------------------------------===//
481
482/// \fn T refract(T I, T N, T eta)
483/// \brief Returns a refraction using an entering ray, \a I, a surface
484/// normal, \a N and refraction index \a eta
485/// \param I The entering ray.
486/// \param N The surface normal.
487/// \param eta The refraction index.
488///
489/// The return value is a floating-point vector that represents the refraction
490/// using the refraction index, \a eta, for the direction of the entering ray,
491/// \a I, off a surface with the normal \a N.
492///
493/// This function calculates the refraction vector using the following formulas:
494/// k = 1.0 - eta * eta * (1.0 - dot(N, I) * dot(N, I))
495/// if k < 0.0 the result is 0.0
496/// otherwise, the result is eta * I - (eta * dot(N, I) + sqrt(k)) * N
497///
498/// I and N must already be normalized in order to achieve the desired result.
499///
500/// I and N must be a scalar or vector whose component type is
501/// floating-point.
502///
503/// eta must be a 16-bit or 32-bit floating-point scalar.
504///
505/// Result type, the type of I, and the type of N must all be the same type.
506
507template <typename T>
508_HLSL_16BIT_AVAILABILITY(shadermodel, 6.2)
509const inline __detail::enable_if_t<__detail::is_arithmetic<T>::Value &&
510 __detail::is_same<half, T>::value,
511 T> refract(T I, T N, T eta) {
512 return __detail::refract_impl(I, N, eta);
513}
514
515template <typename T>
516const inline __detail::enable_if_t<
518refract(T I, T N, T eta) {
519 return __detail::refract_impl(I, N, eta);
520}
521
522template <int L>
523_HLSL_16BIT_AVAILABILITY(shadermodel, 6.2)
524const inline __detail::HLSL_FIXED_VECTOR<half, L> refract(
525 __detail::HLSL_FIXED_VECTOR<half, L> I,
526 __detail::HLSL_FIXED_VECTOR<half, L> N, half eta) {
527 return __detail::refract_impl(I, N, eta);
528}
529
530template <int L>
536
537//===----------------------------------------------------------------------===//
538// smoothstep builtin
539//===----------------------------------------------------------------------===//
540
541/// \fn T smoothstep(T Min, T Max, T X)
542/// \brief Returns a smooth Hermite interpolation between 0 and 1, if \a X is in
543/// the range [\a Min, \a Max].
544/// \param Min The minimum range of the x parameter.
545/// \param Max The maximum range of the x parameter.
546/// \param X The specified value to be interpolated.
547///
548/// The return value is 0.0 if \a X ≤ \a Min and 1.0 if \a X â‰Ĩ \a Max. When \a
549/// Min < \a X < \a Max, the function performs smooth Hermite interpolation
550/// between 0 and 1.
551
552template <typename T>
553_HLSL_16BIT_AVAILABILITY(shadermodel, 6.2)
554const inline __detail::enable_if_t<__detail::is_arithmetic<T>::Value &&
555 __detail::is_same<half, T>::value,
556 T> smoothstep(T Min, T Max, T X) {
557 return __detail::smoothstep_impl(Min, Max, X);
558}
559
560template <typename T>
561const inline __detail::enable_if_t<
563smoothstep(T Min, T Max, T X) {
564 return __detail::smoothstep_impl(Min, Max, X);
565}
566
567template <int N>
568_HLSL_16BIT_AVAILABILITY(shadermodel, 6.2)
569const inline __detail::HLSL_FIXED_VECTOR<half, N> smoothstep(
570 __detail::HLSL_FIXED_VECTOR<half, N> Min,
571 __detail::HLSL_FIXED_VECTOR<half, N> Max,
572 __detail::HLSL_FIXED_VECTOR<half, N> X) {
573 return __detail::smoothstep_vec_impl(Min, Max, X);
574}
575
576template <int N>
583
584} // namespace hlsl
585#endif //_HLSL_HLSL_INTRINSICS_H_
#define V(N, I)
#define X(type, name)
Definition Value.h:97
#define _HLSL_BUILTIN_ALIAS(builtin)
#define _HLSL_AVAILABILITY(platform, version)
#define _HLSL_16BIT_AVAILABILITY(environment, version)
constexpr vector< T, N > smoothstep_vec_impl(vector< T, N > Min, vector< T, N > Max, vector< T, N > X)
constexpr T length_impl(T X)
constexpr vector< T, 4 > dst_impl(vector< T, 4 > Src0, vector< T, 4 > Src1)
constexpr T faceforward_impl(T N, T I, T Ng)
constexpr vector< T, L > reflect_vec_impl(vector< T, L > I, vector< T, L > N)
constexpr T distance_impl(T X, T Y)
constexpr T reflect_impl(T I, T N)
constexpr T ldexp_impl(T X, T Exp)
constexpr enable_if_t< is_same< float, T >::value||is_same< half, T >::value, T > distance_vec_impl(vector< T, N > X, vector< T, N > Y)
constexpr T fmod_impl(T X, T Y)
typename enable_if< B, T >::Type enable_if_t
Definition hlsl_detail.h:31
constexpr int4 d3d_color_to_ubyte4_impl(float4 V)
constexpr T smoothstep_impl(T Min, T Max, T X)
constexpr enable_if_t< is_same< float, T >::value||is_same< half, T >::value, T > length_vec_impl(vector< T, N > X)
constexpr T refract_impl(T I, T N, U Eta)
constexpr float dot2add_impl(half2 a, half2 b, float c)
vector< __detail::enable_if_t<(N > 1 &&N<=4), T >, N > HLSL_FIXED_VECTOR
Definition hlsl_detail.h:49
constexpr vector< T, N > fmod_vec_impl(vector< T, N > X, vector< T, N > Y)
constexpr enable_if_t< sizeof(U)==sizeof(T), vector< U, N > > bit_cast(vector< T, N > V)
Definition hlsl_detail.h:35
constexpr vector< T, 4 > lit_impl(T NDotL, T NDotH, T M)
unsigned int uint
An unsigned 32-bit integer.
vector< half, 4 > half4
constexpr int4 D3DCOLORtoUBYTE4(float4 V)
vector< half, 2 > half2
vector< uint, 2 > uint2
vector< float, 4 > float4
const __detail::enable_if_t< __detail::is_arithmetic< T >::Value &&__detail::is_same< half, T >::value, T > smoothstep(T Min, T Max, T X)
Returns a smooth Hermite interpolation between 0 and 1, if X is in the range [Min,...
const half4 lit(half NDotL, half NDotH, half M)
vector< int, 4 > int4
constexpr vector< uint, N > asuint(vector< T, N > V)
vector< double, 3 > double3
const __detail::enable_if_t< __detail::is_arithmetic< T >::Value &&__detail::is_same< half, T >::value, T > refract(T I, T N, T eta)
Returns a refraction using an entering ray, I, a surface normal, N and refraction index eta.
const __detail::enable_if_t< __detail::is_arithmetic< T >::Value &&__detail::is_same< half, T >::value, T > length(T X)
Returns the length of the specified floating-point vector.
const __detail::enable_if_t< __detail::is_arithmetic< T >::Value &&__detail::is_same< half, T >::value, T > ldexp(T X, T Exp)
Returns the result of multiplying the specified value by two raised to the power of the specified exp...
vector< uint, 3 > uint3
const __detail::enable_if_t< __detail::is_arithmetic< T >::Value &&__detail::is_same< half, T >::value, T > reflect(T I, T N)
Returns a reflection using an incident ray, I, and a surface normal, N.
const __detail::enable_if_t< __detail::is_arithmetic< T >::Value &&__detail::is_same< half, T >::value, T > distance(T X, T Y)
Returns a distance scalar between X and Y.
vector< double, 4 > double4
vector< double, 2 > double2
vector< uint, 4 > uint4
const __detail::enable_if_t< __detail::is_arithmetic< T >::Value &&__detail::is_same< half, T >::value, T > faceforward(T N, T I, T Ng)
Flips the surface-normal (if needed) to face in a direction opposite to I.
const half4 dst(half4 Src0, half4 Src1)
constexpr vector< int, N > asint(vector< T, N > V)
const __detail::enable_if_t< __detail::is_arithmetic< T >::Value &&__detail::is_same< half, T >::value, T > fmod(T X, T Y)
Returns the linear interpolation of x to y.
constexpr vector< float, N > asfloat(vector< T, N > V)
const float dot2add(half2 A, half2 B, float C)
Dot product of 2 vector of type half and add a float scalar value.
static const bool value
Definition hlsl_detail.h:17
#define ldexp(__x, __y)
Definition tgmath.h:868
#define fmod(__x, __y)
Definition tgmath.h:798