Advertisement
Guest User

VertexWinder.cl

a guest
Feb 18th, 2013
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 8.13 KB | None | 0 0
  1. //Every vertex is assigned two workers. One worker winds a triangle by moving vertically, and the other winds the triangle by moving horizontally.
  2.  
  3. //take note that this horizontal/vertical worker abstraction is NOT related to the horiz/vertical worker abstraction used in QuadTree.cl
  4. //threadpool tasks assuming quadtree width is 4
  5. //     x
  6. //   0 1 2 3
  7. //  0\ \ \ \horizontal workers
  8. //  1\ \ \ \
  9. //  2\ \ \ \
  10. //z 3\ \ \ \
  11. //  4/ / / /vertical workers
  12. //  5/ / / /
  13. //  6/ / / /
  14. //  7/ / / /
  15. #define VERTS(x,y) (activeVerts[(x)*vertWidth+(y)])
  16. #define INDICIES(x,y) (indicies[(x)*vertWidth+(y)])
  17.  
  18. //enum name syntax:
  19. //{pos}_{dir}
  20. //TOPLEFT_RIGHT: position is in topright of quad, is winding triangle to the right
  21. //BOTTOMRIGHT_UP: position is in bottomright of quad, is winding triangle upwards.
  22. typedef enum{
  23.     TOPLEFT_RIGHT,
  24.     TOPLEFT_DOWN,
  25.     BOTTOMLEFT_UP,
  26.     BOTTOMLEFT_RIGHT,
  27.     BOTTOMRIGHT_LEFT,
  28.     BOTTOMRIGHT_UP,
  29.     TOPRIGHT_DOWN,
  30.     TOPRIGHT_LEFT
  31. } WINDINGTYPE;
  32.  
  33. void GetDirections(int2* directions, WINDINGTYPE workerType);
  34. void GetExtensionDirections(int2* directions, WINDINGTYPE workerType);
  35. void GetRenderDirections(int2* directions, WINDINGTYPE workerType);
  36. void GetExtensionRenderDirections(int2* directions, WINDINGTYPE workerType);
  37.  
  38. __kernel void VertexWinder(
  39.     __global char* activeVerts,
  40.     __global int3* indicies){
  41.    
  42.     int treeWidth = get_global_size(0);
  43.     int vertWidth =  treeWidth+1;
  44.     int x_id = get_global_id(0);
  45.     int y_id = get_global_id(1);
  46.     int indiceIdx = x_id+y_id*treeWidth;
  47.  
  48.     bool horizontalWorker = true;
  49.     if(y_id >= treeWidth){
  50.         horizontalWorker = false;
  51.         y_id -= treeWidth;
  52.     }
  53.  
  54.     //generate the worker origin point and the direction in which the worker winds the triangle
  55.     int x_pos, y_pos;
  56.     WINDINGTYPE windingType;
  57.    
  58.     bool isTop,isLeft;
  59.     if(x_id%2==0){
  60.         x_pos = x_id;
  61.         isLeft = true;
  62.     }
  63.     else{
  64.         x_pos = x_id+1;
  65.         isLeft = false;
  66.     }
  67.     if(y_id%2==0){
  68.         y_pos = y_id;
  69.         isTop = true;
  70.     }
  71.     else{
  72.         y_pos = y_id+1;
  73.         isTop = false;
  74.     }
  75.    
  76.     if(VERTS(x_pos, y_pos)==false){
  77.         return;
  78.     }
  79.    
  80.     //this mess of code figures out where and how this worker will  perform the winding
  81.     bool canExtend=false;
  82.     if(isTop)
  83.         if(isLeft)
  84.             if(horizontalWorker){
  85.                 windingType = TOPLEFT_RIGHT;
  86.             }
  87.             else{
  88.                 windingType = TOPLEFT_DOWN;
  89.                 canExtend = true;
  90.             }
  91.         else
  92.             if(horizontalWorker){
  93.                 windingType = TOPRIGHT_LEFT;
  94.                 canExtend = true;
  95.             }
  96.             else{
  97.                 windingType = TOPRIGHT_DOWN;
  98.             }
  99.     else
  100.         if(isLeft)
  101.             if(horizontalWorker){
  102.                 windingType = BOTTOMLEFT_RIGHT;
  103.                 canExtend = true;
  104.             }
  105.             else{
  106.                 windingType = BOTTOMLEFT_UP;
  107.             }
  108.         else
  109.             if(horizontalWorker){
  110.                 windingType = BOTTOMRIGHT_LEFT;
  111.             }
  112.             else{
  113.                 windingType = BOTTOMRIGHT_UP;
  114.                 canExtend = true;
  115.             }
  116.     int2 pos = (int2)(x_pos, y_pos);
  117.     int indexes[3];
  118.     int2 dirs[3];
  119.     GetDirections(dirs, windingType);
  120.     int step=1;
  121.     bool hasExtended = false;
  122.     int dirToCheck=0;
  123.     int2 checkPos = pos;
  124.     while(true){
  125.         if(hasExtended)
  126.             GetExtensionDirections(dirs, windingType);
  127.         else
  128.             GetDirections(dirs, windingType);
  129.        
  130.         for(int i=0; i<3; i++){
  131.             dirs[i] = dirs[i] * step;
  132.         }
  133.         int2 newPos = dirs[dirToCheck]+checkPos;
  134.         if(VERTS(newPos.x,newPos.y)==1){
  135.             if(dirToCheck == 2){
  136.                 break;
  137.             }
  138.            
  139.             dirToCheck++;
  140.             checkPos = newPos;
  141.         }
  142.         else{
  143.             if(canExtend){
  144.                 if(!hasExtended){
  145.                     hasExtended = true;
  146.                     dirToCheck=0;
  147.                     checkPos = pos;
  148.                     if(x_pos%(step*2) != 0 || y_pos%(step*2) != 0){
  149.                         return;
  150.                     }
  151.                 }
  152.                 else{
  153.                     step *= 2;
  154.                     hasExtended = false;
  155.                     dirToCheck=0;
  156.                     checkPos = pos;
  157.                 }
  158.             }
  159.             else{
  160.                 return;
  161.             }
  162.         }
  163.     }  
  164.    
  165.     if(hasExtended){
  166.         GetExtensionRenderDirections(dirs, windingType);
  167.     }
  168.     else{
  169.         GetRenderDirections(dirs, windingType);
  170.     }
  171.     for(int i=0; i<3; i++){
  172.         dirs[i] = dirs[i] * step;
  173.     }
  174.    
  175.    
  176.     int2 curPos = pos;
  177.     indexes[0] = curPos.x*(vertWidth)+curPos.y;
  178.     //int2 idxs[3];
  179.     //idxs[0] = curPos;
  180.     for(int i=1; i<3; i++){
  181.         curPos = dirs[i-1]+curPos;
  182.         //idxs[i] = curPos;
  183.         indexes[i] = curPos.x*(vertWidth)+curPos.y;
  184.     }
  185.     indicies[indiceIdx]= (int3)(indexes[0], indexes[1], indexes[2]);
  186.     }
  187.  
  188.     //this hardcoding is necessary until I can figure out a way to do it programatically
  189. void GetDirections(int2* directions, WINDINGTYPE workerType){
  190.     switch(workerType){
  191.         case TOPLEFT_RIGHT:
  192.             directions[0] = (int2)(1,0);
  193.             directions[1] = (int2)(0,1);
  194.             directions[2] = (int2)(-1,-1);
  195.             break;
  196.         case TOPLEFT_DOWN:
  197.             directions[0] = (int2)(0,1);
  198.             directions[1] = (int2)(1,0);
  199.             directions[2] = (int2)(-1,-1);
  200.             break;
  201.         case BOTTOMLEFT_UP:
  202.             directions[0] = (int2)(0,-1);
  203.             directions[1] = (int2)(1,0);
  204.             directions[2] = (int2)(-1,1);
  205.             break;
  206.         case BOTTOMLEFT_RIGHT:
  207.             directions[0] = (int2)(1,0);
  208.             directions[1] = (int2)(0,-1);
  209.             directions[2] = (int2)(-1,1);
  210.             break;
  211.         case BOTTOMRIGHT_LEFT:
  212.             directions[0] = (int2)(-1,0);
  213.             directions[1] = (int2)(0,-1);
  214.             directions[2] = (int2)(1,1);
  215.             break;
  216.         case BOTTOMRIGHT_UP:
  217.             directions[0] = (int2)(0,-1);
  218.             directions[1] = (int2)(-1,0);
  219.             directions[2] = (int2)(1,1);
  220.             break;
  221.         case TOPRIGHT_DOWN:
  222.             directions[0] = (int2)(0,1);
  223.             directions[1] = (int2)(-1,0);
  224.             directions[2] = (int2)(1,-1);
  225.             break;
  226.         case TOPRIGHT_LEFT:
  227.             directions[0] = (int2)(-1,0);
  228.             directions[1] = (int2)(0,1);
  229.             directions[2] = (int2)(1,-1);
  230.             break;
  231.     }
  232.     }
  233.  
  234. void GetExtensionDirections(int2* directions, WINDINGTYPE workerType){
  235.     switch(workerType){
  236.         case TOPLEFT_DOWN:
  237.             directions[0] = (int2)(0,2);
  238.             directions[1] = (int2)(1,-1);
  239.             directions[2] = (int2)(-1,-1);
  240.             break;
  241.         case BOTTOMLEFT_RIGHT:
  242.             directions[0] = (int2)(2,0);
  243.             directions[1] = (int2)(-1,-1);
  244.             directions[2] = (int2)(-1,1);
  245.             break;
  246.         case BOTTOMRIGHT_UP:
  247.             directions[0] = (int2)(0,-2);
  248.             directions[1] = (int2)(-1,1);
  249.             directions[2] = (int2)(1,-1);
  250.             break;
  251.         case TOPRIGHT_LEFT:
  252.             directions[0] = (int2)(-2,0);
  253.             directions[1] = (int2)(1,1);
  254.             directions[2] = (int2)(1,-1);
  255.             break;
  256.     }
  257.     }
  258.    
  259. //HARDCODING FOR THE HARDCODING GOD
  260. //We need these extra methods for getting how a triangle will be actually wound when rendered.
  261. //The above winding directions are only the way they are for the reduction-function to work.
  262. //These methods are defined such that the triangle indexes will be defined in a clockwise manner.
  263.  
  264. void GetRenderDirections(int2* directions, WINDINGTYPE workerType){
  265.     switch(workerType){
  266.         case TOPLEFT_RIGHT:
  267.             directions[0] = (int2)(1,0);
  268.             directions[1] = (int2)(0,1);
  269.             directions[2] = (int2)(-1,-1);
  270.             break;
  271.         case TOPLEFT_DOWN:
  272.             directions[0] = (int2)(1,1);
  273.             directions[1] = (int2)(-1,0);
  274.             directions[2] = (int2)(0,-1);
  275.             break;
  276.         case BOTTOMLEFT_UP:
  277.             directions[0] = (int2)(0,-1);
  278.             directions[1] = (int2)(1,0);
  279.             directions[2] = (int2)(-1,1);
  280.             break;
  281.         case BOTTOMLEFT_RIGHT:
  282.             directions[0] = (int2)(1,-1);
  283.             directions[1] = (int2)(0,1);
  284.             directions[2] = (int2)(-1,0);
  285.             break;
  286.         case BOTTOMRIGHT_LEFT:
  287.             directions[0] = (int2)(-1,0);
  288.             directions[1] = (int2)(0,-1);
  289.             directions[2] = (int2)(1,1);
  290.             break;
  291.         case BOTTOMRIGHT_UP:
  292.             directions[0] = (int2)(-1,-1);
  293.             directions[1] = (int2)(1,0);
  294.             directions[2] = (int2)(0,1);
  295.             break;
  296.         case TOPRIGHT_DOWN:
  297.             directions[0] = (int2)(0,1);
  298.             directions[1] = (int2)(-1,0);
  299.             directions[2] = (int2)(1,-1);
  300.             break;
  301.         case TOPRIGHT_LEFT:
  302.             directions[0] = (int2)(-1,1);
  303.             directions[1] = (int2)(0,-1);
  304.             directions[2] = (int2)(1,0);
  305.             break;
  306.     }
  307.     }
  308.  
  309. void GetExtensionRenderDirections(int2* directions, WINDINGTYPE workerType){
  310.     switch(workerType){
  311.         case TOPLEFT_DOWN:
  312.             directions[0] = (int2)(1,1);
  313.             directions[1] = (int2)(-1,1);
  314.             directions[2] = (int2)(0,-2);
  315.             break;
  316.         case BOTTOMLEFT_RIGHT:
  317.             directions[0] = (int2)(1,-1);
  318.             directions[1] = (int2)(1,1);
  319.             directions[2] = (int2)(-2,0);
  320.             break;
  321.         case BOTTOMRIGHT_UP:
  322.             directions[0] = (int2)(-1,-1);
  323.             directions[1] = (int2)(1,-1);
  324.             directions[2] = (int2)(0,2);
  325.             break;
  326.         case TOPRIGHT_LEFT:
  327.             directions[0] = (int2)(-1,1);
  328.             directions[1] = (int2)(-1,-1);
  329.             directions[2] = (int2)(2,0);
  330.             break;
  331.     }
  332.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement