Elffuss BraidHub

kikocis/ncnn public

master · 11.9 KiBRenderedRawPermalink
1# ncnn GLSL extension
2
3## rationale
4Different GPUs support different features, some support fp16 as buffer storage type, some support fp16 as operand variable, some old GPUs only support fp32
5
6When the GPU supports the `VK_KHR_16bit_storage` extension, in order to minimize the memory bandwidth consumption of the GPU, we will give priority to using fp16 as the storage type. Otherwise, we use `packHalf2x16` and `unpackHalf2x16` in GLSL 4.2 to compress 2 fp32 to uint, reducing read and write bandwidth.
7
8Similarly, when the gpu supports the `VK_KHR_shader_float16_int8` extension, in order to speed up the calculation efficiency, we will give priority to using fp16 as the operation operand, which usually doubles the speed. Otherwise, we use fp32.
9
10To ensure the widest compatibility, the following code for declaring descriptor binding and loading data will be written
11
12```c
13#if NCNN_fp16_storage // gpu supports 16bit storage
14layout (binding = 0) buffer blob { f16vec4 blob_data[]; };
15#elif NCNN_fp16_packed // gpu supports GLSL 4.2
16layout (binding = 0) buffer blob { uvec2 blob_data[]; };
17#else // gpu only supports fp32
18layout (binding = 0) buffer blob { vec4 blob_data[]; };
19#endif
20
21void main()
22{
23 const int i = int(gl_GlobalInvocationID.x);
24
25#if NCNN_fp16_storage && NCNN_fp16_arithmetic // gpu supports 16bit storage and shader float16
26 f16vec4 x = blob_data[i];
27#elif NCNN_fp16_storage // gpu supports 16bit storage but no shader float16
28 vec4 x = vec4(blob_data[i]);
29#elif NCNN_fp16_packed && NCNN_fp16_arithmetic // gpu supports GLSL 4.2 and shader float16
30 f16vec4 x = f16vec4(unpackFloat2x16(blob_data[i].x), unpackFloat2x16(blob_data[i].y));
31#elif NCNN_fp16_packed // gpu supports GLSL 4.2
32 vec4 x = vec4(unpackHalf2x16(blob_data[i].x), unpackHalf2x16(blob_data[i].y));
33#else // gpu only supports fp32
34 vec4 x = blob_data[i];
35#endif
36}
37```
38
39As you can see, just declaring the buffer type and reading a value consumes a lot of lines of code, which is a maintenance nightmare. Therefore, ncnn adds more flexible data types and auxiliary functions to reduce the size of the code and improve readability, and will automatically expand to the most efficient implementation according to the feature level supported by the GPU.
40
41The above code, by using the ncnn glsl extension, can be simplified to
42
43```c
44layout (binding = 0) buffer blob { sfpvec4 blob_data[]; };
45
46void main()
47{
48 const int i = int(gl_GlobalInvocationID.x);
49
50 afpvec4 x = buffer_ld4(blob_data, i);
51}
52```
53
54The ncnn glsl extension provides the necessary data types for storage, computation, shared memory, and load, store, conversion functions for buffers and images. We also provide some buffer and image copy functions to prevent loss of precision when using fp16 as the intermediate data type, and to avoid unnecessary `unpackHalf2x16` and `packHalf2x16` pair.
55
56# entrypoint for compiling GLSL
57
58The gpu.h header in the ncnn library exposes 3 APIs for compiling glsl code into spir-v binary, they support ncnn glsl extension, these 3 functions accept opt switch to control the expansion form of ncnn glsl extension. The first two accept raw glsl code strings, and the last one is used to create ncnn's built-in shader.
59
60```cpp
61namespace ncnn {
62
63// online spirv compilation
64NCNN_EXPORT int compile_spirv_module(const char* comp_string, const Option& opt, std::vector<uint32_t>& spirv);
65NCNN_EXPORT int compile_spirv_module(const char* comp_data, int comp_data_size, const Option& opt, std::vector<uint32_t>& spirv);
66NCNN_EXPORT int compile_spirv_module(int shader_type_index, const Option& opt, std::vector<uint32_t>& spirv);
67
68} // namespace ncnn
69```
70
71## compile ncnn extended GLSL code directly
72
73You can write shader code with ncnn glsl extension, compiled to spir-v using ncnn functions. The compiled product is a standard-compliant spir-v binary, which can be directly used to create a pipeline object in the vulkan api
74
75```cpp
76static const char my_glsl_data[] = R"(
77#version 450
78
79#if NCNN_fp16_storage
80#extension GL_EXT_shader_16bit_storage: require
81#endif
82#if NCNN_fp16_arithmetic
83#extension GL_EXT_shader_explicit_arithmetic_types_float16: require
84#endif
85
86layout (binding = 0) readonly buffer a_blob { sfpvec4 a_blob_data[]; };
87layout (binding = 1) writeonly buffer b_blob { sfpvec4 b_blob_data[]; };
88
89void main()
90{
91 const int i = int(gl_GlobalInvocationID.x);
92
93 afpvec4 v = buffer_ld4(a_blob_data, i);
94
95 v = v + 123;
96
97 buffer_st4(b_blob_data, i, v);
98}
99)";
100
101Option opt;
102 // you can control the extension behavior
103 // even if the gpu supports 16bit storage
104opt.use_fp16_storage = false;
105
106std::vector<uint32_t> spirv;
107ncnn::compile_spirv_module(my_glsl_data, sizeof(my_glsl_data) - 1, opt, spirv);
108
109// To create pipeline object later
110// ncnn::Pipeline pipeline(vkdev);
111// pipeline.set_local_size_xyz(64, 1, 1);
112// pipeline.create(spirv.data(), spirv.size() * 4, specializations);
113```
114
115## ncnn built-in shader
116
117The shader index inside ncnn is exposed in the `layer_shader_type.h` header and can be used if needed
118
119```cpp
120#include "layer_shader_type.h"
121
122int shader_type_index = LayerShaderType::convert_ycbcr;
123
124Option opt;
125
126std::vector<uint32_t> spirv;
127int retc = compile_spirv_module(shader_type_index, opt, spirv);
128```
129
130# data types
131
132## storage type
133
134declare buffer data layout in descriptor binding
135
136```c
137layout (binding = 0) buffer top_blob { sfpvec4 top_blob_data[]; };
138```
139
140|storage type|fp32|fp16p|fp16s|
141|---|---|---|---|
142|sfp|float|float|float16_t|
143|sfpvec2|vec2|uint|f16vec2|
144|sfpvec4|vec4|uvec2|f16vec4|
145|sfpvec8|mat2x4|uvec4|f16mat2x4|
146
147## arithmetic type
148
149declare local variable in glsl code
150
151```c
152void main()
153{
154 afpvec4 v = a * b;
155}
156```
157
158|arithmetic type|fp32|fp16a|
159|---|---|---|
160|afp|float|float16_t|
161|afpvec2|vec2|f16vec2|
162|afpvec4|vec4|f16vec4|
163|afpvec8|mat2x4|f16mat2x4|
164
165## local type
166
167declare variable in shared local memory
168
169```c
170shared lfp tmp_a[8][4][2];
171```
172
173|local type|fp32|fp16p / fp16s only|fp16s+fp16a|fp16s+fp16u|
174|---|---|---|---|---|
175|lfp|float|float|float|float16_t|
176|lfpvec4|vec4|uvec2|uint64_t|f16vec4|
177
178## image format and precision hint type
179
180declare image format in descriptor binding
181
182```c
183layout (binding = 0) uniform unfp sampler3D bottom_blob_3d;
184layout (binding = 1, imfmtc4) writeonly uniform unfp image3D top_blob_3d;
185```
186
187|format type|fp32|fp16p|fp16s|
188|---|---|---|---|
189|imfmt1|r32f|f32f|r16f|
190|imfmt4|rgba32f|rgba16f|rgba16f|
191
192|precision hint type|fp32|fp16p|fp16s|
193|---|---|---|---|
194|unfp|highp|mediump|mediump|
195
196# buffer functions
197
198- load typed value from src[offset]
199
200```c
201afp buffer_ld1(sfp src, int offset);
202afpvec2 buffer_ld2(sfpvec2 src, int offset);
203afpvec4 buffer_ld4(sfpvec4 src, int offset);
204afpvec8 buffer_ld8(sfpvec8 src, int offset);
205```
206
207- store typed value to dst[offset]
208
209```c
210void buffer_st1(sfp dst, int offset, afp v);
211void buffer_st2(sfpvec2 dst, int offset, afpvec2 v);
212void buffer_st4(sfpvec4 dst, int offset, afpvec4 v);
213void buffer_st8(sfpvec8 dst, int offset, afpvec8 v);
214```
215
216- copy typed value from src[src_offset] to dst[dst_offset]
217
218```c
219void buffer_cp1(sfp dst, int dst_offset, sfp src, int src_offset);
220void buffer_cp2(sfpvec2 dst, int dst_offset, sfpvec2 src, int src_offset);
221void buffer_cp4(sfpvec4 dst, int dst_offset, sfpvec4 src, int src_offset);
222void buffer_cp8(sfpvec4 dst, int dst_offset, sfpvec4 src, int src_offset);
223```
224
225- copy and pack value from src[src_offsets[0],src_offsets[1],...] to dst[dst_offset]
226
227```c
228void buffer_cp1to4(sfpvec4 dst, int dst_offset, sfp src, ivec4 src_offsets);
229void buffer_cp1to8(sfpvec8 dst, int dst_offset, sfp src, ivec4 src_offsets_0, ivec4 src_offsets_1);
230void buffer_cp4to8(sfpvec8 dst, int dst_offset, sfpvec4 src, ivec2 src_offsets);
231```
232
233- copy and unpack value from src[src_offset] to dst[dst_offsets[0],dst_offsets[1],...]
234
235```c
236void buffer_cp4to1(sfp dst, ivec4 dst_offsets, sfpvec4 src, int src_offset);
237void buffer_cp8to1(sfp dst, ivec4 dst_offsets_0, ivec4 dst_offsets_1, sfpvec8 src, int src_offset);
238void buffer_cp8to4(sfpvec4 dst, ivec2 dst_offsets, sfpvec8 src, int src_offset);
239```
240
241# image functions
242
243- load typed value from src at pos
244
245```c
246afp image1d_ld1(sampler1D src, float pos);
247afp image2d_ld1(sampler2D src, vec2 pos);
248afp image3d_ld1(sampler3D src, vec3 pos);
249afpvec4 image1d_ld4(sampler1D src, float pos);
250afpvec4 image2d_ld4(sampler2D src, vec2 pos);
251afpvec4 image3d_ld4(sampler3D src, vec3 pos);
252afpvec8 image1d_ld8(sampler1D src, float pos);
253afpvec8 image2d_ld8(sampler2D src, vec2 pos);
254afpvec8 image3d_ld8(sampler3D src, vec3 pos);
255```
256
257- store typed value to dst at pos
258
259```c
260void image1d_st1(image1D dst, int pos, afp v);
261void image2d_st1(image2D dst, ivec2 pos, afp v);
262void image3d_st1(image3D dst, ivec3 pos, afp v);
263void image1d_st4(image1D dst, int pos, afpvec4 v);
264void image2d_st4(image2D dst, ivec2 pos, afpvec4 v);
265void image3d_st4(image3D dst, ivec3 pos, afpvec4 v);
266void image1d_st8(image1D dst, int pos, afpvec8 v);
267void image2d_st8(image2D dst, ivec2 pos, afpvec8 v);
268void image3d_st8(image3D dst, ivec3 pos, afpvec8 v);
269```
270
271- copy typed value from src at src_pos to dst at dst_pos
272
273```c
274void image1d_cp1(image1D dst, int dst_pos, sampler1D src, float src_pos);
275void image2d_cp1(image2D dst, ivec2 dst_pos, sampler2D src, vec2 src_pos);
276void image3d_cp1(image3D dst, ivec3 dst_pos, sampler3D src, vec3 src_pos);
277void image1d_cp4(image1D dst, int dst_pos, sampler1D src, float src_pos);
278void image2d_cp4(image2D dst, ivec2 dst_pos, sampler2D src, vec2 src_pos);
279void image3d_cp4(image3D dst, ivec3 dst_pos, sampler3D src, vec3 src_pos);
280void image1d_cp8(image1D dst, int dst_pos, sampler1D src, float src_pos);
281void image2d_cp8(image2D dst, ivec2 dst_pos, sampler2D src, vec2 src_pos);
282void image3d_cp8(image3D dst, ivec3 dst_pos, sampler3D src, vec3 src_pos);
283```
284
285Note: Since image is an opaque data structure, no copy and pack/unpack functions are provided. To achieve this operation, you need to load first and then store.
286
287# local data conversion functions
288
289- storage buffer to local memory
290
291```c
292lfp sfp2lfp(sfp v);
293lfpvec4 sfp2lfpvec4(sfpvec4 v);
294```
295
296- local memory to local variable
297
298```c
299afp lfp2afp(lfp v);
300afpvec4 lfp2afpvec4(lfpvec4 v);
301```
302
303Note: The common usage of local memory is to read from global memory first, store it in local memory, and then read local variables from local memory for subsequent use. Therefore, only storage type to local type and local type to arithmetic type conversion functions are provided here.
304
305# misc functions
306
307- prefer specialization constant over push constant
308
309```c
310T psc(T x)
311```
312
313Declare the same variable in specialization constant AND push constant section, then `psc(x)` will become a compile-time constant when specialization constant given non-zero or be dynamic via push constant otherwise. This is often used for tensor shape specialization. We can usually resolve all shape information and make them be compile-time constants for more aggressive shader optimization.
314
315```c
316layout (constant_id = 0) const int size = 0;
317
318layout (push_constant) uniform parameter
319{
320 int size;
321} p;
322
323void main()
324{
325 const int s = psc(size);
326}
327```
328
329# platform macros
330
331judge if the current platform is moltenvk, for enabling some platform-specific workaround
332
333```c
334#if NCNN_moltenvk
335// enable workaround for moltenvk
336#endif
337```
338
339# option macros
340
341enable glsl extension only if user enable some options
342
343```c
344#if NCNN_fp16_storage
345#extension GL_EXT_shader_16bit_storage: require
346#endif
347#if NCNN_fp16_arithmetic
348#extension GL_EXT_shader_explicit_arithmetic_types_float16: require
349#endif
350```
351
352declare descriptor binding for image or buffer
353
354```c
355#if NCNN_image_shader
356layout (binding = 0) uniform unfp sampler3D bottom_blob_3d;
357#else
358layout (binding = 0) readonly buffer bottom_blob { sfpvec4 bottom_blob_data[]; };
359#endif
360```
361
362|macro|defined by option|
363|---|---|
364|NCNN_fp16_packed|opt.use_fp16_packed|
365|NCNN_fp16_storage|opt.use_fp16_storage|
366|NCNN_fp16_arithmetic|opt.use_fp16_arithmetic|
367|NCNN_int8_packed|opt.use_int8_packed|
368|NCNN_int8_storage|opt.use_int8_storage|
369|NCNN_int8_arithmetic|opt.use_int8_arithmetic|
370|NCNN_image_shader|opt.use_image_storage|
371|NCNN_shader_local_memory|opt.use_shader_local_memory|