Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #pragma kernel GaussianElim
- #define IDX(x,y,NumX) (x + y * NumX)
- RWStructuredBuffer<float> AICMatrix;
- uint2 resolution;
- [numthreads(1024,1,1)]
- void GaussianElim(uint3 id : SV_DispatchThreadID)
- {
- uint x = id.x;
- for (uint pivot = 0; pivot < resolution.y; pivot++)
- {
- float pivotVal = AICMatrix[IDX(pivot, pivot, resolution.x)];
- DeviceMemoryBarrierWithGroupSync();
- if (x < resolution.x)
- {
- //If the pivot value is zero
- if (abs(pivotVal) < 0.0001f)
- {
- //Start by checking the row below
- uint notZeroPivot = pivot + 1;
- //Check all the remaining rows until the end of the matrix
- for (notZeroPivot; notZeroPivot < resolution.y; notZeroPivot++)
- {
- //If a not zero pivot is found, break the loop
- if (AICMatrix[IDX(pivot, notZeroPivot, resolution.x)] != 0)
- {
- break;
- }
- }
- //Swap the values in the pivot'th, and notZeroPivot'th Row
- float temp = AICMatrix[IDX(x, pivot, resolution.x)];
- AICMatrix[IDX(x, pivot, resolution.x)] = AICMatrix[IDX(x, notZeroPivot, resolution.x)];
- AICMatrix[IDX(x, notZeroPivot, resolution.x)] = temp;
- //Reassign the pivot value
- pivotVal = AICMatrix[IDX(pivot, pivot, resolution.x)];
- //Otherwise, the answers to the system are zero
- }
- //Scale by the pivotValue
- AICMatrix[IDX(x, pivot, resolution.x)] /= pivotVal;
- if (pivot < (resolution.y - 1))
- {
- for (uint row = pivot + 1; row < resolution.y; row++)
- {
- float elementToCancel = AICMatrix[IDX(pivot, row, resolution.x)];
- AICMatrix[IDX(x, row, resolution.x)] += AICMatrix[IDX(x, pivot, resolution.x)] * -elementToCancel;
- }
- }
- }
- }
- DeviceMemoryBarrierWithGroupSync();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement