Advertisement
Guest User

Gaussian Elimination Compute Shadere

a guest
Jun 16th, 2024
377
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.13 KB | Source Code | 0 0
  1. #pragma kernel GaussianElim
  2.  
  3. #define IDX(x,y,NumX) (x + y * NumX)
  4.  
  5. RWStructuredBuffer<float> AICMatrix;
  6. uint2 resolution;
  7.  
  8. [numthreads(1024,1,1)]
  9. void GaussianElim(uint3 id : SV_DispatchThreadID)
  10. {
  11.     uint x = id.x;
  12.  
  13.     for (uint pivot = 0; pivot < resolution.y; pivot++)
  14.     {
  15.         float pivotVal = AICMatrix[IDX(pivot, pivot, resolution.x)];
  16.         DeviceMemoryBarrierWithGroupSync();
  17.         if (x < resolution.x)
  18.         {
  19.            
  20.             //If the pivot value is zero
  21.             if (abs(pivotVal) < 0.0001f)
  22.             {
  23.                 //Start by checking the row below
  24.                 uint notZeroPivot = pivot + 1;
  25.                 //Check all the remaining rows until the end of the matrix
  26.                 for (notZeroPivot; notZeroPivot < resolution.y; notZeroPivot++)
  27.                 {
  28.                     //If a not zero pivot is found, break the loop
  29.                     if (AICMatrix[IDX(pivot, notZeroPivot, resolution.x)] != 0)
  30.                     {
  31.                         break;
  32.                     }
  33.                 }
  34.                 //Swap the values in the pivot'th, and notZeroPivot'th Row
  35.                 float temp = AICMatrix[IDX(x, pivot, resolution.x)];
  36.                 AICMatrix[IDX(x, pivot, resolution.x)] = AICMatrix[IDX(x, notZeroPivot, resolution.x)];
  37.                 AICMatrix[IDX(x, notZeroPivot, resolution.x)] = temp;
  38.                
  39.                 //Reassign the pivot value
  40.                 pivotVal = AICMatrix[IDX(pivot, pivot, resolution.x)];
  41.                 //Otherwise, the answers to the system are zero
  42.             }
  43.             //Scale by the pivotValue
  44.             AICMatrix[IDX(x, pivot, resolution.x)] /= pivotVal;
  45.             if (pivot < (resolution.y - 1))
  46.             {
  47.                 for (uint row = pivot + 1; row < resolution.y; row++)
  48.                 {
  49.                     float elementToCancel = AICMatrix[IDX(pivot, row, resolution.x)];
  50.                     AICMatrix[IDX(x, row, resolution.x)] += AICMatrix[IDX(x, pivot, resolution.x)] * -elementToCancel;
  51.                 }
  52.             }
  53.         }
  54.     }
  55.     DeviceMemoryBarrierWithGroupSync();
  56. }
Tags: shaders
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement