Blender V5.3
gpu_shader_cxx_builtin.hh
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2024 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
10
11#pragma once
12
14
15/* Some compilers complain about lack of return values. Keep it short. */
16#define RET \
17 { \
18 return {}; \
19 }
20
21/* -------------------------------------------------------------------- */
24
26template<typename T, int D> VecBase<bool, D> lessThan(VecOp<T, D>, VecOp<T, D>) RET;
29template<typename T, int D> VecBase<bool, D> equal(VecOp<T, D>, VecOp<T, D>) RET;
30template<typename T, int D> VecBase<bool, D> notEqual(VecOp<T, D>, VecOp<T, D>) RET;
31template<int D> bool any(VecOp<bool, D>) RET;
32template<int D> bool all(VecOp<bool, D>) RET;
33/* `not` is a C++ keyword that aliases the `!` operator. Simply overload it. */
35
44int bitCount(int) RET;
45int bitCount(uint) RET;
52
57int findLSB(int) RET;
58int findLSB(uint) RET;
59int findMSB(int) RET;
60int findMSB(uint) RET;
61
62/* Math Functions. */
63
64/* NOTE: Declared inside a namespace and exposed behind macros to prevent
65 * errors on VS2019 due to `corecrt_math` conflicting functions. */
66namespace glsl {
67template<typename T> constexpr T abs(T) RET;
68/* TODO(fclem): These should be restricted to floats. */
69template<typename T> constexpr T ceil(T) RET;
70template<typename T> constexpr T exp(T) RET;
71template<typename T> constexpr T exp2(T) RET;
72template<typename T> constexpr T floor(T) RET;
73template<typename T> T fma(T, T, T) RET;
74float fma(float, float, float) RET;
75template<typename T> T frexp(T, T) RET;
76bool isinf(float) RET;
78bool isnan(float) RET;
80template<typename T> constexpr T log(T) RET;
81template<typename T> constexpr T log2(T) RET;
82template<typename T> T modf(T, T &) RET;
83template<typename T, typename U> constexpr T pow(T, U) RET;
84template<typename T> constexpr T round(T) RET;
85template<typename T> constexpr T sqrt(T) RET;
86template<typename T> constexpr T trunc(T) RET;
87template<typename T, typename U> T ldexp(T, U) RET;
88
89template<typename T> constexpr T acos(T) RET;
90template<typename T> T acosh(T) RET;
91template<typename T> constexpr T asin(T) RET;
92template<typename T> T asinh(T) RET;
93template<typename T> T atan(T, T) RET;
94template<typename T> T atan(T) RET;
95template<typename T> T atanh(T) RET;
96template<typename T> constexpr T cos(T) RET;
97template<typename T> T cosh(T) RET;
98template<typename T> constexpr T sin(T) RET;
99template<typename T> T sinh(T) RET;
100template<typename T> T tan(T) RET;
101template<typename T> T tanh(T) RET;
102} // namespace glsl
103
104#define abs glsl::abs
105#define ceil glsl::ceil
106#define exp glsl::exp
107#define exp2 glsl::exp2
108#define floor glsl::floor
109#define fma glsl::fma
110#define frexp glsl::frexp
111#define isinf glsl::isinf
112#define isnan glsl::isnan
113#define log glsl::log
114#define log2 glsl::log2
115#define modf glsl::modf
116#define pow glsl::pow
117#define round glsl::round
118#define sqrt glsl::sqrt
119#define trunc glsl::trunc
120#define ldexp glsl::ldexp
121#define acos glsl::acos
122#define acosh glsl::acosh
123#define asin glsl::asin
124#define asinh glsl::asinh
125#define atan glsl::atan
126#define atanh glsl::atanh
127#define cos glsl::cos
128#define cosh glsl::cosh
129#define sin glsl::sin
130#define sinh glsl::sinh
131#define tan glsl::tan
132#define tanh glsl::tanh
133
134template<typename T> constexpr T max(T, T) RET;
135template<typename T> constexpr T min(T, T) RET;
136template<typename T> constexpr T sign(T) RET;
137template<typename T, typename U> constexpr T clamp(T, U, U) RET;
138template<typename T> constexpr T clamp(T, float, float) RET;
139template<typename T, typename U> constexpr T max(T, U) RET;
140template<typename T, typename U> constexpr T min(T, U) RET;
141/* TODO(fclem): These should be restricted to floats. */
142template<typename T> T fract(T) RET;
143template<typename T> constexpr T inversesqrt(T) RET;
144constexpr float mod(float, float) RET;
145template<int D> VecBase<float, D> constexpr mod(VecOp<float, D>, float) RET;
147template<typename T> T smoothstep(T, T, T) RET;
148float step(float, float) RET;
151float smoothstep(float, float, float) RET;
152template<int D> VecBase<float, D> smoothstep(float, float, VecOp<float, D>) RET;
153
154template<typename T> constexpr T degrees(T) RET;
155template<typename T> constexpr T radians(T) RET;
156
157/* Declared explicitly to avoid type errors. */
158float mix(float, float, float) RET;
162
163#define select(A, B, C) mix(A, B, C)
164#define saturate(a) clamp(a, 0.0f, 1.0f)
165
167template<int D> float dot(VecOp<float, D>, VecOp<float, D>) RET;
168float distance(float, float) RET;
170template<int D> float length(VecOp<float, D>) RET;
172
177constexpr int floatBitsToInt(float) RET;
178constexpr uint floatBitsToUint(float) RET;
179constexpr float intBitsToFloat(int) RET;
180constexpr float uintBitsToFloat(uint) RET;
181
182/* Matrix compare operators. */
183#define EQ_OP(type, ...) \
184 inline bool operator==(type a, type b) \
185 { \
186 return __VA_ARGS__; \
187 }
188EQ_OP(float2x2, all(equal(a[0], b[0])) && all(equal(a[1], b[1])))
189EQ_OP(float2x3, all(equal(a[0], b[0])) && all(equal(a[1], b[1])))
190EQ_OP(float2x4, all(equal(a[0], b[0])) && all(equal(a[1], b[1])))
191EQ_OP(float3x2, all(equal(a[0], b[0])) && all(equal(a[1], b[1])) && all(equal(a[2], b[2])))
192EQ_OP(float3x3, all(equal(a[0], b[0])) && all(equal(a[1], b[1])) && all(equal(a[2], b[2])))
193EQ_OP(float3x4, all(equal(a[0], b[0])) && all(equal(a[1], b[1])) && all(equal(a[2], b[2])))
195 all(equal(a[0], b[0])) && all(equal(a[1], b[1])) && all(equal(a[2], b[2])) &&
196 all(equal(a[3], b[3])))
198 all(equal(a[0], b[0])) && all(equal(a[1], b[1])) && all(equal(a[2], b[2])) &&
199 all(equal(a[3], b[3])))
201 all(equal(a[0], b[0])) && all(equal(a[1], b[1])) && all(equal(a[2], b[2])) &&
202 all(equal(a[3], b[3])))
203#undef EQ_OP
204
205/* Matrices functions. */
206template<int C, int R> float determinant(MatBase<C, R>) RET;
207template<int C, int R> MatBase<C, R> inverse(MatBase<C, R>) RET;
208template<int C, int R> MatBase<R, C> transpose(MatBase<C, R>) RET;
209
210/* Derivative functions. */
211template<typename T> T gpu_dfdx(T) RET;
212template<typename T> T gpu_dfdy(T) RET;
213template<typename T> T gpu_fwidth(T) RET;
214
215/* Discards the output of the current fragment shader invocation and halts its execution. */
217
218/* Geometric functions. */
220template<typename T, int D> VecBase<T, D> reflect(VecOp<T, D>, VecOp<T, D>) RET;
221template<typename T, int D> VecBase<T, D> refract(VecOp<T, D>, VecOp<T, D>, float) RET;
222
223/* Atomic operations. */
224int atomicAdd(int &, int) RET;
225int atomicAnd(int &, int) RET;
226int atomicOr(int &, int) RET;
227int atomicXor(int &, int) RET;
228int atomicMin(int &, int) RET;
229int atomicMax(int &, int) RET;
230int atomicExchange(int &, int) RET;
231int atomicCompSwap(int &, int, int) RET;
232uint atomicAdd(uint &, uint) RET;
233uint atomicAnd(uint &, uint) RET;
234uint atomicOr(uint &, uint) RET;
235uint atomicXor(uint &, uint) RET;
236uint atomicMin(uint &, uint) RET;
237uint atomicMax(uint &, uint) RET;
238uint atomicExchange(uint &, uint) RET;
239uint atomicCompSwap(uint &, uint, uint) RET;
240
241/* Packing functions. */
252
253void barrier() {}
259
264
265struct rayQuery {};
267
268const uint gpu_RayFlagsNone = 0u;
269const uint gpu_RayFlagsOpaque = 0x00000001u;
270const uint gpu_RayFlagsNoOpaque = 0x00000002u;
271const uint gpu_RayFlagsTerminateOnFirstHit = 0x00000004u;
272const uint gpu_RayFlagsSkipClosestHitShader = 0x00000008u;
273const uint gpu_RayFlagsCullBackFacingTriangles = 0x00000010u;
275const uint gpu_RayFlagsCullOpaque = 0x00000040u;
276const uint gpu_RayFlagsCullNoOpaque = 0x00000080u;
277
281
284
285void rayQueryInitialize(rayQuery /*rayQuery*/,
286 accelerationStructure /*topLevel*/,
287 uint /*rayFlags*/,
288 uint /*cullMask*/,
289 float3 /*origin*/,
290 float /*tMin*/,
291 float3 /*direction*/,
292 float /*tMax*/) {};
293bool rayQueryProceed(rayQuery /*rayQuery*/) RET;
294void rayQueryTerminate(rayQuery /*rayQuery*/) {}
295void rayQueryGenerateIntersection(rayQuery /*rayQuery*/, float /*hitT*/) {}
297uint rayQueryGetIntersectionType(rayQuery /*rayQuery*/, bool /*committed*/) RET;
298float rayQueryGetRayTMin(rayQuery /*rayQuery*/) RET;
299/* Not available on Metal: no query-side ray-flags accessor. Gated off so use is a compile error.
300 */
301#if 0
302uint rayQueryGetRayFlags(rayQuery /*rayQuery*/) RET;
303#endif
306float rayQueryGetIntersectionT(rayQuery /*rayQuery*/, bool /*committed*/) RET;
307uint rayQueryGetIntersectionInstanceCustomIndex(rayQuery /*rayQuery*/, bool /*committed*/) RET;
308uint rayQueryGetIntersectionInstanceId(rayQuery /*rayQuery*/, bool /*committed*/) RET;
309/* Not available on Metal: no inline equivalent. Gated off so use is a compile error. */
310#if 0
311uint rayQueryGetIntersectionInstanceShaderBindingTableRecordOffset(rayQuery /*rayQuery*/,
312 bool /*committed*/) RET;
313#endif
314uint rayQueryGetIntersectionGeometryIndex(rayQuery /*rayQuery*/, bool /*committed*/) RET;
315uint rayQueryGetIntersectionPrimitiveIndex(rayQuery /*rayQuery*/, bool /*committed*/) RET;
316float2 rayQueryGetIntersectionBarycentrics(rayQuery /*rayQuery*/, bool /*committed*/) RET;
317bool rayQueryGetIntersectionFrontFace(rayQuery /*rayQuery*/, bool /*committed*/) RET;
323
325
326#undef RET
#define U
btScalar max
VecBase< int, D > findMSB(VecOp< int, D >) RET
float rayQueryGetIntersectionT(rayQuery, bool) RET
int atomicMin(int &, int) RET
const uint gpu_RayFlagsCullFrontFacingTriangles
VecBase< bool, D > notEqual(VecOp< T, D >, VecOp< T, D >) RET
const uint gpu_RayFlagsCullBackFacingTriangles
const uint gpu_RayFlagsOpaque
VecBase< int, D > bitfieldExtract(VecOp< int, D >, int, int) RET
T smoothstep(T, T, T) RET
void gpu_discard_fragment()
VecBase< bool, D > greaterThanEqual(VecOp< T, D >, VecOp< T, D >) RET
float4 unpackSnorm4x8(uint) RET
bool rayQueryProceed(rayQuery) RET
float4x3 rayQueryGetIntersectionObjectToWorld(rayQuery, bool) RET
VecBase< int, D > floatBitsToInt(VecOp< float, D >) RET
void memoryBarrier()
VecBase< bool, D > lessThanEqual(VecOp< T, D >, VecOp< T, D >) RET
float3 rayQueryGetIntersectionObjectRayOrigin(rayQuery, bool) RET
float2 rayQueryGetIntersectionBarycentrics(rayQuery, bool) RET
b[1] b &&[1] b &&[1] b &&[1] b &&[1] b[3] b &&[1] b[3] float determinant(MatBase< C, R >) RET
T fract(T) RET
const uint gpu_RayFlagsTerminateOnFirstHit
bool any(VecOp< bool, D >) RET
int atomicMax(int &, int) RET
VecBase< float, D > intBitsToFloat(VecOp< int, D >) RET
void memoryBarrierImage()
int atomicExchange(int &, int) RET
MatBase< C, R > inverse(MatBase< C, R >) RET
uint packUnorm2x16(float2) RET
VecBase< T, D > refract(VecOp< T, D >, VecOp< T, D >, float) RET
uint rayQueryGetIntersectionType(rayQuery, bool) RET
float3 rayQueryGetWorldRayDirection(rayQuery) RET
T gpu_dfdx(T) RET
int atomicCompSwap(int &, int, int) RET
VecBase< T, D > reflect(VecOp< T, D >, VecOp< T, D >) RET
float3 rayQueryGetIntersectionObjectRayDirection(rayQuery, bool) RET
VecBase< bool, D > greaterThan(VecOp< T, D >, VecOp< T, D >) RET
uint rayQueryGetIntersectionInstanceId(rayQuery, bool) RET
#define EQ_OP(type,...)
float4 unpackUnorm4x8(uint) RET
VecBase< float, D > normalize(VecOp< float, D >) RET
float dot(VecOp< float, D >, VecOp< float, D >) RET
uint packSnorm4x8(float4) RET
T gpu_fwidth(T) RET
constexpr T sign(T) RET
const uint gpu_RayFlagsNone
uint rayQueryGetIntersectionPrimitiveIndex(rayQuery, bool) RET
VecBase< T, D > faceforward(VecOp< T, D >, VecOp< T, D >, VecOp< T, D >) RET
const uint gpu_RayQueryCommittedIntersectionTriangle
constexpr T inversesqrt(T) RET
int atomicAnd(int &, int) RET
void rayQueryTerminate(rayQuery)
bool rayQueryGetIntersectionCandidateAABBOpaque(rayQuery) RET
float2 unpackSnorm2x16(uint) RET
uint rayQueryGetIntersectionGeometryIndex(rayQuery, bool) RET
const uint gpu_RayFlagsCullNoOpaque
VecBase< float, D > constexpr mod(VecOp< float, D >, VecOp< float, D >) RET
float rayQueryGetRayTMin(rayQuery) RET
uint packUnorm4x8(float4) RET
float distance(float, float) RET
int atomicXor(int &, int) RET
uint rayQueryGetIntersectionInstanceCustomIndex(rayQuery, bool) RET
VecBase< uint, D > floatBitsToUint(VecOp< float, D >) RET
VecBase< int, D > bitfieldInsert(VecOp< int, D >, VecOp< int, D >, int, int) RET
int atomicAdd(int &, int) RET
bool all(VecOp< bool, D >) RET
const uint gpu_RayFlagsNoOpaque
VecBase< float, D > step(VecOp< float, D >, VecOp< float, D >) RET
float2 unpackHalf2x16(uint) RET
MatBase< R, C > transpose(MatBase< C, R >) RET
float4x3 rayQueryGetIntersectionWorldToObject(rayQuery, bool) RET
VecBase< int, D > bitCount(VecOp< int, D >) RET
void memoryBarrierShared()
const uint gpu_RayQueryCandidateIntersectionTriangle
T gpu_dfdy(T) RET
VecBase< float, D > uintBitsToFloat(VecOp< uint, D >) RET
void rayQueryConfirmIntersection(rayQuery)
void memoryBarrierBuffer()
const uint gpu_RayFlagsSkipClosestHitShader
VecBase< float, 3 > cross(VecOp< float, 3 >, VecOp< float, 3 >) RET
VecBase< bool, D > lessThan(VecOp< T, D >, VecOp< T, D >) RET
#define RET
const uint gpu_RayQueryCommittedIntersectionGenerated
constexpr T radians(T) RET
VecBase< int, D > bitfieldReverse(VecOp< int, D >) RET
void groupMemoryBarrier()
bool rayQueryGetIntersectionFrontFace(rayQuery, bool) RET
uint packHalf2x16(float2) RET
VecBase< bool, D > equal(VecOp< T, D >, VecOp< T, D >) RET
float2 unpackUnorm2x16(uint) RET
const uint gpu_RayFlagsCullOpaque
const uint gpu_RayQueryCandidateIntersectionAABB
void barrier()
constexpr T clamp(T, U, U) RET
float length(VecOp< float, D >) RET
VecBase< int, D > findLSB(VecOp< int, D >) RET
void rayQueryGenerateIntersection(rayQuery, float)
float3 rayQueryGetWorldRayOrigin(rayQuery) RET
uint packSnorm2x16(float2) RET
void rayQueryInitialize(rayQuery, accelerationStructure, uint, uint, float3, float, float3, float)
const uint gpu_RayQueryCommittedIntersectionNone
int atomicOr(int &, int) RET
constexpr T degrees(T) RET
VecBase< bool, D > operator!(VecOp< bool, D >) RET
MatBase< 3, 4 > float3x4
MatBase< 2, 4 > float2x4
MatBase< 3, 2 > float3x2
MatBase< 2, 2 > float2x2
MatBase< 4, 4 > float4x4
MatBase< 2, 3 > float2x3
MatBase< 4, 3 > float4x3
MatBase< 4, 2 > float4x2
#define T
VecBase< bool, D > isinf(VecOp< float, D >) RET
T frexp(T, T) RET
T ldexp(T, U) RET
constexpr T floor(T) RET
constexpr T asin(T) RET
VecBase< bool, D > isnan(VecOp< float, D >) RET
constexpr T exp(T) RET
T modf(T, T &) RET
T cosh(T) RET
T sinh(T) RET
T asinh(T) RET
constexpr T cos(T) RET
constexpr T exp2(T) RET
constexpr T round(T) RET
constexpr T log2(T) RET
constexpr T pow(T, U) RET
constexpr T acos(T) RET
constexpr T sqrt(T) RET
constexpr T trunc(T) RET
T atan(T, T) RET
constexpr T ceil(T) RET
T acosh(T) RET
constexpr T log(T) RET
T fma(T, T, T) RET
T tan(T) RET
T tanh(T) RET
T atanh(T) RET
constexpr T sin(T) RET
constexpr T abs(T) RET
#define mix
#define min(a, b)
Definition sort.cc:38