1212
1313#define VEC4_T ${texel_type(DTYPE)}
1414
15- #define TILE_SIZE ${TILE_SIZE}
15+ #define TILE_SIZE_X ${TILE_SIZE_X}
16+ #define TILE_SIZE_Y ${TILE_SIZE_Y}
17+ #define LOCAL_WG_SIZE 64
1618
1719#define op(X, A, B) ${OPERATOR}
1820
@@ -41,19 +43,19 @@ layout(push_constant) uniform restrict Block {
4143
4244layout (local_size_x_id = 0 , local_size_y_id = 1 , local_size_z_id = 2 ) in ;
4345
44- // shared memory to hold calculated positions, this would reduce register usage thus improving performance .
45- // 64 is the number of threads in the local wg
46- $num_shared = 64 * TILE_SIZE * TILE_SIZE
47- shared ivec2 pos_shared[${num_shared} ];
46+ // For performance improvement, reduce register usage by caching positions in shared memory .
47+ // Offset index by 1 every 16 points to avoid bank access conflict.
48+ #define offset_pos_index(index) (index + ((index) >> 4 ))
49+ shared ivec3 pos_shared[offset_pos_index(LOCAL_WG_SIZE * TILE_SIZE_X * TILE_SIZE_Y) ];
4850
4951/*
5052 * Computes a 2D pointwise convolution of an NxN output tile. Calculating an
5153 * output tile for pointwise convolution is more efficient because the kernel
5254 * size is only 1x1, making it easier to re-use loaded texels from t_kernel.
5355 */
5456void main() {
55- const ivec2 out_limits_scaled = (out_limits.xy + TILE_SIZE - 1 ) / TILE_SIZE ;
56- const uint shared_mem_stride = gl_WorkGroupSize.x * gl_WorkGroupSize.y * gl_WorkGroupSize.z ;
57+ const ivec2 out_limits_scaled = (out_limits.xy + ivec2 (TILE_SIZE_X - 1 , TILE_SIZE_Y - 1 )) / ivec2 (TILE_SIZE_X, TILE_SIZE_Y) ;
58+ const uint shared_mem_stride = LOCAL_WG_SIZE ;
5759
5860 const uint div_by_x = gl_GlobalInvocationID.x / out_limits_scaled.x;
5961 const ivec3 gpos = ivec3 (
@@ -67,33 +69,32 @@ void main() {
6769 // +--------+--------+
6870 // | pos[2] | pos[3] |
6971 // +--------+--------+
70- ivec2 pos[TILE_SIZE * TILE_SIZE];
71- for (int y = 0 , i = 0 ; y < TILE_SIZE; ++ y) {
72- for (int x = 0 ; x < TILE_SIZE; ++ x) {
73- pos[i] = ivec2 (
74- gpos.x * TILE_SIZE + x, gpos.y * TILE_SIZE + y);
75- pos_shared[(shared_mem_stride * i) + gl_LocalInvocationIndex] = pos[i];
72+ ivec2 pos[TILE_SIZE_X * TILE_SIZE_Y];
73+ for (int y = 0 , i = 0 ; y < TILE_SIZE_Y; ++ y) {
74+ for (int x = 0 ; x < TILE_SIZE_X; ++ x) {
75+ pos[i] = ivec2 (gpos.x * TILE_SIZE_X + x, gpos.y * TILE_SIZE_Y + y);
76+ pos_shared[offset_pos_index((shared_mem_stride * i) + gl_LocalInvocationIndex)] = ivec3 (pos[i], gpos.z);
7677 i++ ;
7778 }
7879 }
7980
8081 // If the top left position is out of bounds, then this invocation will have
8182 // no work to do.
82- if (any ( greaterThanEqual ( ivec3 (pos[ 0 ], gpos.z), out_limits.xyz)) ) {
83+ if (gpos.z >= out_limits.z ) {
8384 return ;
8485 }
8586
8687 // Compute the index of the input texture that needs to be loaded for each
8788 // output position. Note that negative indices can be produced indicating that
8889 // the top-left element is in a region added by padding.
89- ivec2 ipos[TILE_SIZE * TILE_SIZE ];
90- for (int i = 0 ; i < TILE_SIZE * TILE_SIZE ; ++ i) {
90+ ivec2 ipos[TILE_SIZE_X * TILE_SIZE_Y ];
91+ for (int i = 0 ; i < TILE_SIZE_X * TILE_SIZE_Y ; ++ i) {
9192 ipos[i] = pos[i] * stride - padding;
9293 }
9394
94- vec4 sum[TILE_SIZE * TILE_SIZE ];
95+ vec4 sum[TILE_SIZE_X * TILE_SIZE_Y ];
9596 sum[0 ] = texelFetch(t_bias, ivec2 (gpos.z, 0 ), 0 );
96- for (int i = 1 ; i < TILE_SIZE * TILE_SIZE ; ++ i) {
97+ for (int i = 1 ; i < TILE_SIZE_X * TILE_SIZE_Y ; ++ i) {
9798 sum[i] = sum[0 ];
9899 }
99100
@@ -109,7 +110,7 @@ void main() {
109110 const vec4 ktex_3 = texelFetchOffset(t_kernel, ivec2 (z, gpos.z), 0 , ivec2 (3 , 0 ));
110111
111112#pragma unroll
112- for (int i = 0 ; i < TILE_SIZE * TILE_SIZE ; ++ i) {
113+ for (int i = 0 ; i < TILE_SIZE_X * TILE_SIZE_Y ; ++ i) {
113114 const vec4 in_tex = texelFetch(t_in, ivec3 (ipos[i], z4), 0 );
114115 // For 2x2 tile size algorithm works as follows.
115116 // To explain the calculations below, the contents of one in_tex and the
@@ -151,10 +152,11 @@ void main() {
151152 }
152153 }
153154
154- for (int i = 0 ; i < TILE_SIZE * TILE_SIZE; ++ i) {
155- const ivec2 pos = pos_shared[(shared_mem_stride * i) + gl_LocalInvocationIndex];
156- if (all (lessThan (ivec3 (pos, gpos.z), out_limits.xyz))) {
157- imageStore(t_out, ivec3 (pos, gpos.z), op(sum[i], out_min, out_max));
155+ for (int i = 0 ; i < TILE_SIZE_X * TILE_SIZE_Y; ++ i) {
156+ const uint index = (shared_mem_stride * i) + gl_LocalInvocationIndex;
157+ const ivec3 pos = pos_shared[offset_pos_index(index)];
158+ if (all (lessThan (pos, out_limits.xyz))) {
159+ imageStore(t_out, pos, op(sum[i], out_min, out_max));
158160 }
159161 }
160162}
0 commit comments