Blender V5.3
gpu_shader_utildefines.bsl.hh
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
5#pragma once
6
8
9#define FLT_11_MAX uintBitsToFloat(0x477E0000)
10#define FLT_10_MAX uintBitsToFloat(0x477C0000)
11#define FLT_11_11_10_MAX float3(FLT_11_MAX, FLT_11_MAX, FLT_10_MAX)
12
13#define UNPACK2(a) (a)[0], (a)[1]
14#define UNPACK3(a) (a)[0], (a)[1], (a)[2]
15#define UNPACK4(a) (a)[0], (a)[1], (a)[2], (a)[3]
16
17/* clang-format off */
18#define in_range_inclusive(val, min_v, max_v) (all(greaterThanEqual(val, min_v)) && all(lessThanEqual(val, max_v)))
19#define in_range_exclusive(val, min_v, max_v) (all(greaterThan(val, min_v)) && all(lessThan(val, max_v)))
20#define in_texture_range(texel, tex) (all(greaterThanEqual(texel, int2(0))) && all(lessThan(texel, textureSize(tex, 0).xy)))
21#define in_image_range(texel, tex) (all(greaterThanEqual(texel, int2(0))) && all(lessThan(texel, imageSize(tex).xy)))
22
23#define weighted_sum(val0, val1, val2, val3, weights) ((val0 * weights[0] + val1 * weights[1] + val2 * weights[2] + val3 * weights[3]) * safe_rcp(weights[0] + weights[1] + weights[2] + weights[3]))
24/* clang-format on */
25
26bool flag_test(uint flag, uint val)
27{
28 return (flag & val) != 0u;
29}
30bool flag_test(int flag, uint val)
31{
32 return flag_test(uint(flag), val);
33}
34bool flag_test(int flag, int val)
35{
36 return (flag & val) != 0;
37}
38
39void set_flag_from_test(uint &value, bool test, uint flag)
40{
41 if (test) {
42 value |= flag;
43 }
44 else {
45 value &= ~flag;
46 }
47}
48void set_flag_from_test(int &value, bool test, int flag)
49{
50 if (test) {
51 value |= flag;
52 }
53 else {
54 value &= ~flag;
55 }
56}
57
62bool bitmask64_test(uint2 bitmask, uint bit_index)
63{
64 uint bitmask32 = (bit_index >= 32u) ? bitmask.y : bitmask.x;
65 return flag_test(bitmask32, 1u << (bit_index & 0x1Fu));
66}
67
72{
73 a = (a & 0xFFFFu) << uint2(0u, 16u);
74 return a.x | a.y;
75}
77{
78 return (uint2(a) >> uint2(0u, 16u)) & uint2(0xFFFFu);
79}
80
85{
86 a = (a & 0xFFu) << uint4(0u, 8u, 16u, 24u);
87 return a.x | a.y | a.z | a.w;
88}
90{
91 return (uint4(a) >> uint4(0u, 8u, 16u, 24u)) & uint4(0xFFu);
92}
93
98int floatBitsToOrderedInt(float value)
99{
100 /* Floats can be sorted using their bits interpreted as integers for positive values.
101 * Negative values do not follow int's two's complement ordering which is reversed.
102 * So we have to XOR all bits except the sign bits in order to reverse the ordering.
103 * Note that this is highly hardware dependent, but there seems to be no case of GPU where the
104 * ints ares not two's complement. */
105 int int_value = floatBitsToInt(value);
106 return (int_value < 0) ? (int_value ^ 0x7FFFFFFF) : int_value;
107}
108float orderedIntBitsToFloat(int int_value)
109{
110 return intBitsToFloat((int_value < 0) ? (int_value ^ 0x7FFFFFFF) : int_value);
111}
VecBase< int, D > floatBitsToInt(VecOp< float, D >) RET
VecBase< float, D > intBitsToFloat(VecOp< int, D >) RET
uint2 unpackUvec2x16(uint a)
uint4 unpackUvec4x8(uint a)
void set_flag_from_test(uint &value, bool test, uint flag)
bool flag_test(uint flag, uint val)
uint packUvec2x16(uint2 a)
uint packUvec4x8(uint4 a)
int floatBitsToOrderedInt(float value)
float orderedIntBitsToFloat(int int_value)
bool bitmask64_test(uint2 bitmask, uint bit_index)
uint x
Definition types_uint2.h:13
uint y
Definition types_uint2.h:13
uint x
Definition types_uint4.h:13
uint y
Definition types_uint4.h:13
uint z
Definition types_uint4.h:13
uint w
Definition types_uint4.h:13