Blender V5.3
gpu_shader_attribute_load.bsl.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
5#pragma once
6
8
14
16uint gpu_attr_load_index(uint vertex_index, int2 stride_and_offset)
17{
18 return vertex_index * uint(stride_and_offset.x) + uint(stride_and_offset.y);
19}
20
22{
23 /* TODO(fclem): Improve this. */
24 uint4 v_data = uint4(in_data) >> uint4(0, 10, 20, 30);
25 bool4 v_sign = greaterThan(v_data & uint4(0x3FF, 0x3FF, 0x3FF, 0x3),
26 uint4(0x1FF, 0x1FF, 0x1FF, 0x1));
27 uint4 v_data_u = floatBitsToUint(mix(uintBitsToFloat(v_data), uintBitsToFloat(~v_data), v_sign));
28 float4 mag = float4(v_data_u & uint4(0x1FF, 0x1FF, 0x1FF, 0x1)) /
29 float4(0x1FF, 0x1FF, 0x1FF, 0x1);
30 return mix(mag, -mag, v_sign);
31}
32
34{
35 return float4(unpackSnorm2x16(data0), unpackSnorm2x16(data1));
36}
37
39{
40 return (uint4(in_data) >> uint4(0, 8, 16, 24)) & uint4(0xFF);
41}
42
43/* TODO(fclem): Once the stride and offset are made obsolete, we can think of wrapping vec3 into
44 * structs of floats as they do not have the 16byte alignment restriction. */
45
46[[force_inline]] float3 gpu_attr_load_float3(const float (&data_buf)[],
47 int2 stride_and_offset,
48 uint i)
49{
50 uint index = gpu_attr_load_index(i, stride_and_offset);
51 float3 v;
52 v.x = data_buf[index + 0];
53 v.y = data_buf[index + 1];
54 v.z = data_buf[index + 2];
55 return v;
56}
57
58[[force_inline]] float2 gpu_attr_load_float2(const float (&data_buf)[],
59 int2 stride_and_offset,
60 uint i)
61{
62 uint index = gpu_attr_load_index(i, stride_and_offset);
63 float2 v;
64 v.x = data_buf[index + 0];
65 v.y = data_buf[index + 1];
66 return v;
67}
68
69[[force_inline]] uint3 gpu_attr_load_uint3(const uint (&data_buf)[],
70 int2 stride_and_offset,
71 uint i)
72{
73 uint index = gpu_attr_load_index(i, stride_and_offset);
74 uint3 v;
75 v.x = data_buf[index + 0];
76 v.y = data_buf[index + 1];
77 v.z = data_buf[index + 2];
78 return v;
79}
80
81[[force_inline]] uint2 gpu_attr_load_uint2(const uint (&data_buf)[],
82 int2 stride_and_offset,
83 uint i)
84{
85 uint index = gpu_attr_load_index(i, stride_and_offset);
86 uint2 v;
87 v.x = data_buf[index + 0];
88 v.y = data_buf[index + 1];
89 return v;
90}
91
92[[force_inline]] int3 gpu_attr_load_int3(const uint (&data_buf)[], int2 stride_and_offset, uint i)
93{
94 uint index = gpu_attr_load_index(i, stride_and_offset);
95 int3 v;
96 v.x = data_buf[index + 0];
97 v.y = data_buf[index + 1];
98 v.z = data_buf[index + 2];
99 return v;
100}
101
102[[force_inline]] int2 gpu_attr_load_int2(const uint (&data_buf)[], int2 stride_and_offset, uint i)
103{
104 uint index = gpu_attr_load_index(i, stride_and_offset);
105 int2 v;
106 v.x = data_buf[index + 0];
107 v.y = data_buf[index + 1];
108 return v;
109}
110
112 int2 stride_and_offset,
113 uint i)
114{
115 uint index = gpu_attr_load_index(i, stride_and_offset);
116 uint v = data_buf[index];
118}
119
120/* TODO(fclem): Once the stride and offset are made obsolete, we can think of wrapping short4 into
121 * structs of uint as they do not have the 16byte alignment restriction. */
122
123[[force_inline]] float4 gpu_attr_load_short4_snorm(const uint (&data_buf)[],
124 int2 stride_and_offset,
125 uint i)
126{
127 uint index = gpu_attr_load_index(i, stride_and_offset);
128 uint v0 = data_buf[index + 0];
129 uint v1 = data_buf[index + 1];
131}
132
133[[force_inline]] uint4 gpu_attr_load_uchar4(const uint (&data_buf)[], uint i)
134{
135 uint index = gpu_attr_load_index(i >> 2u, int2(1, 0));
136 return gpu_attr_decode_uchar4_to_uint4(data_buf[index]);
137}
138
139[[force_inline]] uint gpu_attr_load_uchar(const uint (&data_buf)[], uint i)
140{
141 uint index = gpu_attr_load_index(i >> 2u, int2(1, 0));
143 return v[i & 3u];
144}
145
146[[force_inline]] bool gpu_attr_load_bool(const uint (&data)[], uint i)
147{
148 uint v = gpu_attr_load_uchar(data, i);
149 return v != 0u;
150}
float4 gpu_attr_decode_1010102_snorm(uint in_data)
float4 gpu_attr_load_uint_1010102_snorm(const uint(&data_buf)[], int2 stride_and_offset, uint i)
uint3 gpu_attr_load_uint3(const uint(&data_buf)[], int2 stride_and_offset, uint i)
int2 gpu_attr_load_int2(const uint(&data_buf)[], int2 stride_and_offset, uint i)
int3 gpu_attr_load_int3(const uint(&data_buf)[], int2 stride_and_offset, uint i)
uint4 gpu_attr_decode_uchar4_to_uint4(uint in_data)
float4 gpu_attr_decode_short4_to_float4_snorm(uint data0, uint data1)
uint gpu_attr_load_uchar(const uint(&data_buf)[], uint i)
uint2 gpu_attr_load_uint2(const uint(&data_buf)[], int2 stride_and_offset, uint i)
float4 gpu_attr_load_short4_snorm(const uint(&data_buf)[], int2 stride_and_offset, uint i)
uint gpu_attr_load_index(uint vertex_index, int2 stride_and_offset)
float2 gpu_attr_load_float2(const float(&data_buf)[], int2 stride_and_offset, uint i)
float3 gpu_attr_load_float3(const float(&data_buf)[], int2 stride_and_offset, uint i)
bool gpu_attr_load_bool(const uint(&data)[], uint i)
uint4 gpu_attr_load_uchar4(const uint(&data_buf)[], uint i)
#define index(i)
#define force_inline
VecBase< bool, D > greaterThan(VecOp< T, D >, VecOp< T, D >) RET
float2 unpackSnorm2x16(uint) RET
VecBase< uint, D > floatBitsToUint(VecOp< float, D >) RET
VecBase< float, D > uintBitsToFloat(VecOp< uint, D >) RET
VecBase< bool, 4 > bool4
#define mix