Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question ComputeShader how to lock nearby datas?

Discussion in 'Shaders' started by flyer19, Jun 25, 2020.

  1. flyer19

    flyer19

    Joined:
    Aug 26, 2016
    Posts:
    121
    I has one 512x512 pixels texture, and set one array name of "hittestBufferInt" with RWStructuredBuffer<int> match that.Want to Random one pixel at (x,y),then
    set hittestBufferInt[x+512*y] =1,
    and set hittestBufferInt[m,512*n]=0 (the pixels which near this pixel within one pixel, m from (x-1,x+1) ,n from(y-1,y+1);

    and want to render one cube offset 1 pixel matching pixels.

    I found can not lock correct the datas, where wrong :
    Code (CSharp):
    1. void SetLayerData(int layer,int x,int y,int HadecoType,int minx,int maxx,int miny,int maxy,int gapPixel)
    2. {
    3.                    
    4. //the same time all get zero at start ,so all will 1, so need to remove cur point nearby get 1 points
    5.      
    6.    int size=SIZE;
    7.   int index=x+y*size;
    8.     hittestBufferInt[index]=0;
    9.     InterlockedAdd(hittestBufferInt[index],1);
    10.     GroupMemoryBarrierWithGroupSync();
    11.    //
    12.  
    13.      int had=0;          
    14.      for(int i=minx;i<=maxx;i++)
    15.       {
    16.          for(int j=miny;j<=maxy;j++)
    17.          {
    18.             float  index2=i+j*size;
    19.    
    20.            
    21.                if(index!=index2)
    22.                 {
    23.                   if(hittestBufferInt[index2]>0)
    24.                   {
    25.                     had=1;
    26.                     break;
    27.                   }
    28.                 }
    29.          }
    30.       }
    31.    
    32.        GroupMemoryBarrierWithGroupSync();
    33.       if(hittestBufferInt[index]>0&&had==1)
    34.       {
    35.           for(int i=minx;i<=maxx;i++)
    36.           {
    37.              for(int j=miny;j<=maxy;j++)
    38.              {
    39.                 float  index2=i+j*size;
    40.        
    41.                
    42.                    if(index!=index2)
    43.                     {
    44.                       EcosysmData nearby2=outputBuffer[index2];
    45.                       hittestBufferInt[index2]=0;
    46.                       InterlockedAdd(hittestBufferInt[index2],0);
    47.                    
    48.                        nearby2.had=0;
    49.                         outputBuffer[index2]=nearby2;
    50.                       GroupMemoryBarrierWithGroupSync();
    51.                    
    52.  
    53.                     }
    54.                     else
    55.                     {
    56.                       EcosysmData nearby=outputBuffer[index];
    57.                       nearby.realPos=Randompos(x,y,gapPixel,layer);
    58.                       nearby.had=1;
    59.                       outputBuffer[index]=nearby;
    60.                       //GroupMemoryBarrierWithGroupSync();
    61.                         hittestBufferInt[index]=0;
    62.                       InterlockedAdd(hittestBufferInt[index],1);
    63.                       GroupMemoryBarrierWithGroupSync();
    64.                     }
    65.              }
    66.           }
    67.       }
    68.                      
    69.                                  
    70.                                                            
    71.                                      
    72.                          
    73.              
    74.        
    75. }
    lock.jpg
     
    Last edited: Jun 25, 2020
  2. flyer19

    flyer19

    Joined:
    Aug 26, 2016
    Posts:
    121
    any help
     
  3. flyer19

    flyer19

    Joined:
    Aug 26, 2016
    Posts:
    121
    any one used this method to creat open world veggitation like DAWN HORIZEON?