ChangeBufferStrideShaderSource.comp
1 #version 450 core 2 3 #extension GL_EXT_shader_8bit_storage : require 4 5 layout (local_size_x = 64, local_size_y = 1, local_size_z = 1) in; 6 7 layout (std140, set = 0, binding = 0) uniform stride_arguments 8 { 9 ivec4 stride_arguments_data; 10 }; 11 12 layout (std430, set = 1, binding = 1) buffer in_s 13 { 14 uint8_t[] in_data; 15 }; 16 17 layout (std430, set = 1, binding = 2) buffer out_s 18 { 19 uint8_t[] out_data; 20 }; 21 22 void main() 23 { 24 // Determine what slice of the stride copies this invocation will perform. 25 26 int sourceStride = stride_arguments_data.x; 27 int targetStride = stride_arguments_data.y; 28 int bufferSize = stride_arguments_data.z; 29 int sourceOffset = stride_arguments_data.w; 30 31 int strideRemainder = targetStride - sourceStride; 32 int invocations = int(gl_WorkGroupSize.x * gl_NumWorkGroups.x); 33 34 int copiesRequired = bufferSize / sourceStride; 35 36 // Find the copies that this invocation should perform. 37 38 // - Copies that all invocations perform. 39 int allInvocationCopies = copiesRequired / invocations; 40 41 // - Extra remainder copy that this invocation performs. 42 int index = int(gl_GlobalInvocationID.x); 43 int extra = (index < (copiesRequired % invocations)) ? 1 : 0; 44 45 int copyCount = allInvocationCopies + extra; 46 47 // Finally, get the starting offset. Make sure to count extra copies. 48 49 int startCopy = allInvocationCopies * index + min(copiesRequired % invocations, index); 50 51 int srcOffset = sourceOffset + startCopy * sourceStride; 52 int dstOffset = startCopy * targetStride; 53 54 // Perform the copies for this region 55 for (int i=0; i<copyCount; i++) { 56 for (int j=0; j<sourceStride; j++) { 57 out_data[dstOffset++] = in_data[srcOffset++]; 58 } 59 60 for (int j=0; j<strideRemainder; j++) { 61 out_data[dstOffset++] = uint8_t(0); 62 } 63 } 64 }