• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2021 The Fuchsia Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef SRC_GRAPHICS_LIB_COMPUTE_RADIX_SORT_PLATFORMS_VK_SHADERS_BUFREF_H_
6 #define SRC_GRAPHICS_LIB_COMPUTE_RADIX_SORT_PLATFORMS_VK_SHADERS_BUFREF_H_
7 
8 //
9 // GLSL
10 //
11 
12 #ifdef VULKAN  // defined by GLSL/VK compiler
13 
14 #extension GL_EXT_shader_explicit_arithmetic_types : require
15 
16 //
17 // If the target does not support VkPhysicalDeviceFeatures.shaderInt64
18 // then:
19 //
20 //   #define RS_DISABLE_SHADER_INT64
21 //
22 // clang-format off
23 #ifdef RS_DISABLE_SHADER_INT64
24 #extension GL_EXT_buffer_reference_uvec2 : require
25 #else
26 #extension GL_EXT_buffer_reference2      : require
27 #endif
28 // clang-format on
29 
30 //
31 // Restrict shouldn't have any noticeable impact on these kernels and
32 // benchmarks appear to prove that true but it's correct to include
33 // the qualifier.
34 //
35 #define RS_RESTRICT restrict
36 
37 //
38 // If the device doesn't support .shaderInt64 then the buffer reference address
39 // is a uvec2.
40 //
41 #ifdef RS_DISABLE_SHADER_INT64
42 #define RS_DEVADDR u32vec2
43 #else
44 #define RS_DEVADDR uint64_t
45 #endif
46 
47 //
48 // Define a buffer reference.
49 //
50 #define RS_BUFREF_DEFINE(_layout, _name, _devaddr) RS_RESTRICT _layout _name = _layout(_devaddr)
51 
52 //
53 // Define a buffer reference at a UINT32 offset.
54 //
55 #ifdef RS_DISABLE_SHADER_INT64
56 #define RS_BUFREF_DEFINE_AT_OFFSET_UINT32(_layout, _name, _devaddr_u32vec2, _offset)               \
57   RS_RESTRICT _layout _name;                                                                       \
58   {                                                                                                \
59     u32vec2  devaddr;                                                                              \
60     uint32_t carry;                                                                                \
61                                                                                                    \
62     devaddr.x = uaddCarry(_devaddr_u32vec2.x, _offset, carry);                                     \
63     devaddr.y = _devaddr_u32vec2.y + carry;                                                        \
64                                                                                                    \
65     _name = _layout(devaddr);                                                                      \
66   }
67 #else
68 #define RS_BUFREF_DEFINE_AT_OFFSET_UINT32(_layout, _name, _devaddr, _offset)                       \
69   RS_RESTRICT _layout _name = _layout(_devaddr + _offset)
70 #endif
71 
72 //
73 // Define a buffer reference at a packed UINT64 offset.
74 //
75 #ifdef RS_DISABLE_SHADER_INT64
76 #define RS_BUFREF_DEFINE_AT_OFFSET_U32VEC2(_layout, _name, _devaddr_u32vec2, _offset_u32vec2)      \
77   RS_RESTRICT _layout _name;                                                                       \
78   {                                                                                                \
79     u32vec2  devaddr;                                                                              \
80     uint32_t carry;                                                                                \
81                                                                                                    \
82     devaddr.x = uaddCarry(_devaddr_u32vec2.x, _offset_u32vec2.x, carry);                           \
83     devaddr.y = _devaddr_u32vec2.y + _offset_u32vec2.y + carry;                                    \
84                                                                                                    \
85     _name = _layout(devaddr);                                                                      \
86   }
87 #else
88 #define RS_BUFREF_DEFINE_AT_OFFSET_U32VEC2(_layout, _name, _devaddr, _offset_u32vec2)              \
89   RS_RESTRICT _layout _name = _layout(_devaddr + pack64(_offset_u32vec2))
90 #endif
91 
92 //
93 // Increment the buffer reference by a UINT32 offset.
94 //
95 #ifdef RS_DISABLE_SHADER_INT64
96 #define RS_BUFREF_INC_UINT32(_layout, _name, _inc)                                                 \
97   {                                                                                                \
98     u32vec2  devaddr = u32vec2(_name);                                                             \
99     uint32_t carry;                                                                                \
100                                                                                                    \
101     devaddr.x = uaddCarry(devaddr.x, _inc, carry);                                                 \
102     devaddr.y = devaddr.y + carry;                                                                 \
103                                                                                                    \
104     _name = _layout(devaddr);                                                                      \
105   }
106 #else
107 #define RS_BUFREF_INC_UINT32(_layout, _name, _inc) _name = _layout(uint64_t(_name) + _inc)
108 #endif
109 
110 //
111 // Increment the buffer reference by a packed UINT64 offset.
112 //
113 #ifdef RS_DISABLE_SHADER_INT64
114 #define RS_BUFREF_INC_U32VEC2(_layout, _name, _inc_u32vec2)                                        \
115   {                                                                                                \
116     u32vec2  devaddr = u32vec2(_name);                                                             \
117     uint32_t carry;                                                                                \
118                                                                                                    \
119     devaddr.x = uaddCarry(devaddr.x, _inc_u32vec2.x, carry);                                       \
120     devaddr.y = devaddr.y + _inc_u32vec2.y + carry;                                                \
121                                                                                                    \
122     _name = _layout(devaddr);                                                                      \
123   }
124 #else
125 #define RS_BUFREF_INC_U32VEC2(_layout, _name, _inc_u32vec2)                                        \
126   _name = _layout(uint64_t(_name) + pack64(_inc_u32vec2))
127 #endif
128 
129 //
130 // Increment the buffer reference by the product of two UINT32 factors.
131 //
132 #define RS_BUFREF_INC_UINT32_UINT32(_layout, _name, _inc_a, _inc_b)                                \
133   {                                                                                                \
134     u32vec2 inc;                                                                                   \
135                                                                                                    \
136     umulExtended(_inc_a, _inc_b, inc.y, inc.x);                                                    \
137                                                                                                    \
138     RS_BUFREF_INC_U32VEC2(_layout, _name, inc);                                                    \
139   }
140 
141 //
142 //
143 //
144 
145 #endif
146 
147 //
148 //
149 //
150 
151 #endif  // SRC_GRAPHICS_LIB_COMPUTE_RADIX_SORT_PLATFORMS_VK_SHADERS_BUFREF_H_
152