Transcript PPT
GPU Broad Phase Collision Detection GPU Graphics Gary J. Katz University of Pennsylvania CIS 665 Adapted from articles taken from GPU Gems III Basic Collision Detection Broad Phase Reviews the whole simulation Looks at coarse view of objects (usually bounding boxes) Determines if objects may intersect Fast Narrow Phase Reviews objects that may intersect (determined by broad phase) Looks at detailed view of objects Determines if objects actually intersect Slow Current Broad Phase Methods Brute Force: Sort and Sweep: n objects need n(n-1)/2 collision tests Complexity = O(n2) Average Complexity = O(n log n) Worst-Case Complexity O(n2) Spatial Subdivision: Average Complexity = O(n log n) Worst-Case Complexity O(n2) Sort and Sweep Bounding volume is projected onto x, y, z axis O1 Determine collision interval for each object [bi, ei] O2 Two objects who’s collision intervals do not overlap can not collide O3 Sorting Axis B1 B3 E1 B2 E3 E2 Sort and Sweep Sorted List: B1 B3 E1 B2 E3 E2 O1 O2 Active Objects: 1 3 2 O3 Objects to compare against: 1: 2: 3 3: 1 B1 B3E1 B2 E3 E2 Add an object i to the active objects list when Bi is reached and remove when Ei is reached Check for intersection between object i and all other object in the active objects list at the time Bi is reached Spatial Subdivision 6 5 2 1 8 7 3 4 Example O1 1 2 3 O4 O2 O3 Images from pg 699, 700 GPU Gems III 5 6 7 8 4 Parallel Spatial Subdivision Complications: 1. 2. Single object can be involved in multiple collision tests Need to prevent multiple threads updating the state of an object at the same time Ways to solve this? Guaranteed Individual Collision Tests Prove: No two cells updated in parallel may contain the same object that is being updated 1. 2. Constraints Each cell is as large as the bounding volume of the largest object Each cell processed in parallel must be separated by each other cell by at least one intervening cell 4 In 2d this takes _____ number of passes 8 In 3d this takes _____ number of passes Example of Parallel Spatial Subdivision O1 1 2 1 O4 2 O2 O3 3 4 O1 1 3 2 4 1 O4 O2 O3 3 4 3 4 2 Avoiding Extra Collision Testing 1. 2. Associate each object a set of control bits to test where its centroid resides Scale the bounding sphere of each object by sqrt(2) to ensure the grid cell is at least 1.5 times larger than the largest object 1 2 1 2 Case 2 Case 1 3 4 3 4 Implementing in CUDA Store list of object IDs, cell IDs in device memory Build the list of cell IDs from object’s bounding boxes Sorting list from previous step Build an index table to traverse the sorted list Schedule pairs of objects for narrow phase collision detection Initialization Cell ID Array OBJ 1 Cell ID 1 OBJ 1 Cell ID 2 OBJ 1 Cell ID 3 OBJ 1 Cell ID 4 OBJ 2 Cell ID 1 OBJ 2 Cell ID 2 OBJ 2 Cell ID 3 OBJ 2 Cell ID 4 . . . Object ID Array OBJ 1 ID, Control Bits OBJ 1 ID, Control Bits OBJ 1 ID, Control Bits OBJ 1 ID, Control Bits OBJ 2 ID, Control Bits OBJ 2 ID, Control Bits OBJ 2 ID, Control Bits OBJ 2 ID, Control Bits . . . Construct the Cell ID Array Host Cells (H – Cells) Contain the centroid of the object H-Cell Hash = (pos.x / CELLSIZE) << XSHIFT) | (pos.y / CELLSIZE) << YSHIFT) | (pos.z / CELLSIZE) << ZSHIFT) Phantom Cells (P-Cells) Overlap with bounding volume but do not contain the centroid P-Cells – Test the cells surrounding the H cell There can be as many as 2d-1 P cells 3d-1 P P P P H P P P P Sorting the Cell ID Array What we want: Starting with a partial sort Sorted by Cell ID H cells of an ID occur before P cells of an ID H cells are before P cells, but array is not sorted by Cell ID Solution: Radix Sort Radix Sort ensures identical cell IDs remain in the same order as before sorting. Sorting Cell Array Cell ID Array 010 0 011 1 111 2 101 3 021 4 020 0 110 2 100 3 011 4 011 0 021 0 Sorted Cell ID Array 021 n 000 2 011 n 101 3 011 n 001 2 020 0 101 2 100 2 021 n 010 0 021 4 110 2 000 2 111 n 010 2 021 n 111 2 001 2 022 n 011 1 021 0 111 n 101 2 011 0 022 n 111 n 011 2 011 2 100 2 102 n 010 2 011 4 100 3 103 3 ... ... Legend Invalid Cell 011 1 Home Cell 100 2 Phantom Cell 103 Cell ID 3 Object ID Spatial Subdivision 6 5 2 1 8 7 3 4 Example O1 1. Assign to each cell the list of bounding volumes whose objects intersect with the cell 1 2 O4 O2 2. Perform Collision test only if both objects are in the cell and one has a centroid in the cell Images from pg 699, 700 GPU Gems III 3 O3 5 6 7 8 4 Create the Collision Cell List Scan sorted cell ID array for changes of cell ID 1. 2. Mark by end of the list of occupants of one cell and beginning of another Count number of objects each collision cell contains and convert them into offsets using scan Create entries for each collision cell in new array 1. 2. 3. Start Number of H occupants Number of P occupants Create Collision Cell List Cell Index & Size Array Sorted Cell ID Array 000 2 011 n 101 3 001 2 020 0 101 2 010 0 021 4 110 2 010 2 021 n 111 2 011 1 021 0 111 n 011 0 022 n 111 n 011 2 100 2 102 n 011 4 100 3 103 3 ... 2 11 ID H P 4 1 4 10 2 1 ... ID = Cell index in sorted Cell ID Array H = Number of Home Cell IDs P = Number of Phantom Cell IDs Traverse Collision Cell List Cell Index & Size Array 2 11 T0 1 4 10 2 1 16 1 1 19 1 1 ... X p q T1 T2 T3 T4 ... Tn 4 Perform Collision Test Per Cell 0 1 0 2 1 ... … Number of Collisions / Thread Array Credits Based upon GPU Gems article Chapter 32 Chapter Author: Scott Le Grand This presentation was put together without the approval of the author and should only be used for educational purposes Backup Spatial Subdivision Partition space into uniform grid Grid cell is at least as large as largest object Each cell contains list of each object whose centroid is in the cell Implementation: 1. 6 5 2. 3. 2 1 3 4. 8 7 Create list of object IDs along with hashing of cell IDs in which they reside Sort list by cell ID Traverse swaths of identical cell IDs Perform collision tests on all objects that share same cell ID 4 Collision tests are performed between objects who are in same cell or adjacent cells Example O1 1 2 3 O4 O2 O3 Images from pg 699, 700 GPU Gems III 5 6 7 8 4