private void GPUTimer_Tick(object sender, EventArgs e) { return; (int sx, int sy) = MainForm.form.GetDisplaySize(); Color3[,] pixelMap = new Color3[sx, sy]; for (int px = 0; px < sx; px++) { for (int py = 0; py < sy; py++) { pixelMap[px, py].red = (float)px / (float)sx; pixelMap[px, py].blue = (float)py / (float)sy; } } // MainForm.form.DrawPixels(pixelMap); }
public static void InitDevicePointers() { // GPU kernal inputs // Random rnd = new Random(); voxelMap = new Color3[32, 32, 32]; for (int x = 0; x < 32; x++) { for (int y = 0; y < 32; y++) { for (int z = 0; z < 32; z++) { if (rnd.NextDouble() < -0.03) { voxelMap[x, y, z].red = Math.Abs((float)x - 16) / 16; voxelMap[x, y, z].green = Math.Abs((float)y - 16) / 16; voxelMap[x, y, z].blue = Math.Abs((float)z - 16) / 16; } else { voxelMap[x, y, z].red = 0f; voxelMap[x, y, z].blue = 0f; voxelMap[x, y, z].green = 0f; } if (x == 0 || x == 31 || y == 0 || y == 31 || z == 0 || z == 31) { voxelMap[x, y, z].red = Math.Abs((float)x - 16) / 16; voxelMap[x, y, z].green = Math.Abs((float)y - 16) / 16; voxelMap[x, y, z].blue = Math.Abs((float)z - 16) / 16; } } } } List <Color3> voxels = new List <Color3>(); chunkData = new ChunkData[33, 33, 33]; // init chunks for (int x = 0; x <= 32; x++) { for (int y = 0; y <= 32; y++) { for (int z = 0; z <= 32; z++) { chunkData[x, y, z].empty = 1; chunkData[x, y, z].valid = 1; chunkData[x, y, z].generated = 1; chunkData[x, y, z].size = 8; chunkData[x, y, z].chunkX = x - 16; chunkData[x, y, z].chunkY = y - 16; chunkData[x, y, z].chunkZ = z - 16; chunkData[x, y, z].voxelDataIndex = voxels.Count; for (int vx = 0; vx < 8; vx++) { for (int vy = 0; vy < 8; vy++) { for (int vz = 0; vz < 8; vz++) { Color3 newVoxel = new Color3(); if (rnd.NextDouble() < 0.02) { newVoxel.red = (float)rnd.NextDouble() * 0.7f + 0.3f; newVoxel.green = (float)rnd.NextDouble() * 0.7f + 0.3f; newVoxel.blue = (float)rnd.NextDouble() * 0.7f + 0.3f; chunkData[x, y, z].empty = 0; } else { // } voxels.Add(newVoxel); } } } chunkHashTable.Insert(x, y, z, chunkData[x, y, z]); } } } voxelData = voxels.ToArray(); pixelMap = new Color3[viewport.ClientSize.Width, viewport.ClientSize.Height]; imageBytes = new byte[pixelMap.GetLength(0) * pixelMap.GetLength(1) * 4]; camera[0].x = 0f; camera[0].y = 0f; camera[0].z = 0f; camera[1].x = 0f; camera[1].y = 0f; camera[1].z = 0f; units = new FSMUnit[64]; dev_units = gpu.Allocate(units); dev_voxelMap = gpu.Allocate(voxelMap); dev_pixelMap = gpu.Allocate(pixelMap); dev_camera = gpu.Allocate(camera); dev_imageBytes = gpu.Allocate(imageBytes); dev_chunkData = gpu.Allocate(chunkData); dev_voxelData = gpu.Allocate(voxelData); Stopwatch sw = new Stopwatch(); sw.Start(); gpu.CopyToDevice(voxelMap, dev_voxelMap); gpu.CopyToDevice(pixelMap, dev_pixelMap); gpu.CopyToDevice(camera, dev_camera); gpu.CopyToDevice(imageBytes, dev_imageBytes); gpu.CopyToDevice(chunkData, dev_chunkData); gpu.CopyToDevice(voxelData, dev_voxelData); GCHandle pinned = GCHandle.Alloc(imageBytes, GCHandleType.Pinned); imageBytesAddress = pinned.AddrOfPinnedObject(); double t1 = sw.ElapsedMilliseconds; }