using System.Diagnostics;
public SampleLocation[] SampleLocations;
public static void Main(string[] args) {
const int NOOP = int.MinValue;
const int PartitionSize = 32;
int partitionsX = Math.Max((textureWidth / PartitionSize),1);
int partitionsY = Math.Max((textureHeight / PartitionSize), 1);
SampleLocation[] sampleLocations = new SampleLocation[1] {
SampleBucket[] buckets = new SampleBucket[(partitionsX * partitionsY)];
for(int x=0; x < partitionsX; ++x)
for(int y = 0;y < partitionsY; ++y)
int idx = x + (y * partitionsX);
var bucketX = x * PartitionSize;
var bucketY = y * PartitionSize;
var bucketWidth = PartitionSize;
var bucketHeight = PartitionSize;
bucketWidth = Math.Min(bucketX + bucketWidth, (int)textureWidth) - bucketX;
bucketHeight = Math.Min(bucketY + bucketHeight, (int)textureHeight) - bucketY;
Debug.Assert(bucketWidth > 0);
Debug.Assert(bucketHeight > 0);
buckets[idx] = new SampleBucket()
SampleLocations = Enumerable.Repeat(new SampleLocation() { Index = NOOP}, bucketWidth * bucketHeight).ToArray()
foreach(var l in sampleLocations)
int bucketX = (int)(l.PixelX / PartitionSize);
int bucketY = (int)(l.PixelY / PartitionSize);
int bucketIdx = bucketX + bucketY * partitionsX;
Debug.Assert(bucketIdx < buckets.Length);
var bucket = buckets[bucketIdx];
var locations = bucket.SampleLocations;
var sampleX = l.PixelX - bucket.X;
var sampleY = l.PixelY - bucket.Y;
var sampleIdx = sampleX + sampleY * bucket.W;
Debug.Assert(sampleIdx < locations.Length);
locations[sampleIdx] = l;