ChangeBufferStrideShaderSource.comp 1.8 KB

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