public override void CalcToneMap() { //toneBeginEvt.Record(mainStream); DeviceBuffer2D glOutputBuffer = DeviceBuffer2D.GLMapBufferObject(glOutputBufferID, (uint)(4 * xRes)); updateOutputKernel.LaunchAsync(mainStream, glOutputBuffer); glOutputBuffer.GLUnmapBufferObject(glOutputBufferID); //toneEndEvt.Record(mainStream); }
public override void ResetOutput() { mainStream.Synchronize(); context.Synchronize(); //resetBeginEvt.Record(); resetIteratorsKernel.Launch(); DeviceBuffer2D glOutputBuffer = DeviceBuffer2D.GLMapBufferObject(glOutputBufferID, (uint)(4 * xRes)); resetOutputKernel.Launch(glOutputBuffer); glOutputBuffer.GLUnmapBufferObject(glOutputBufferID); //resetEndEvt.Record(); context.Synchronize(); }
public override void Allocate(int xRes, int yRes) { context.Synchronize(); Deallocate(); this.xRes = xRes; this.yRes = yRes; xRasterBlockCount = (xRes + RasterBlockSize - 1) / RasterBlockSize; yRasterBlockCount = (yRes + RasterBlockSize - 1) / RasterBlockSize; module.WriteConstant("xRes", xRes); module.WriteConstant("yRes", yRes); module.WriteConstant("iterBlockCount", IterBlockCount); accumBuffer = DeviceBuffer2D.Alloc(SubPixelByteSize, xRes * 4, yRes); module.WriteConstant("accumBuffer", accumBuffer); GL.GenBuffers(1, out glOutputBufferID); GL.BindBuffer(BufferTarget.PixelUnpackBuffer, glOutputBufferID); GL.BufferData(BufferTarget.PixelUnpackBuffer, (IntPtr)(4 * xRes * yRes), IntPtr.Zero, BufferUsageHint.StreamCopy); GL.BindBuffer(BufferTarget.PixelUnpackBuffer, 0); context.GLRegisterBufferObject(glOutputBufferID); glOutputTexID = GL.GenTexture(); GL.BindTexture(TextureTarget.Texture2D, glOutputTexID); GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Four, xRes, yRes, 0, PixelFormat.Rgba, PixelType.UnsignedByte, IntPtr.Zero); GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.GenerateMipmap, 1); GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.LinearMipmapLinear); GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Linear); GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapS, (int)TextureWrapMode.Clamp); GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapT, (int)TextureWrapMode.Clamp); GL.BindTexture(TextureTarget.Texture2D, 0); resetOutputKernel.SetBlockShape(RasterBlockSize, RasterBlockSize, 1); resetOutputKernel.SetGridDim(xRasterBlockCount, yRasterBlockCount); resetOutputKernel.SetSharedSize(0); updateOutputKernel.SetBlockShape(RasterBlockSize, RasterBlockSize, 1); updateOutputKernel.SetGridDim(xRasterBlockCount, yRasterBlockCount); updateOutputKernel.SetSharedSize(0); }
public CudaFractalEngine() { device = Device.Devices[0]; context = device.CreateContext(); iterBlockCount = Util.Clamp(device.MultiprocessorCount * 2, 2, 64); //System.Reflection.Assembly loadedAssembly = typeof(CudaFractalEngine).Assembly; //System.IO.Stream stream = loadedAssembly.GetManifestResourceStream(typeof(CudaFractalEngine), "kernels.ptx"); System.IO.MemoryStream stream = new System.IO.MemoryStream(CudaResources.kernels_ptx); module = context.LoadModule(stream); initIteratorsKernel = module.GetKernel("init_iterators_kernel"); resetIteratorsKernel = module.GetKernel("reset_iterators_kernel"); iterateKernel = module.GetKernel("iterate_kernel"); updateStatsKernel = module.GetKernel("update_stats_kernel"); resetOutputKernel = module.GetKernel("reset_output_kernel"); updateOutputKernel = module.GetKernel("update_output_kernel"); glOutputBufferID = 0; mainStream = new Cuda.Stream(); iterPosStateBuffer = DeviceBuffer2D.Alloc(8, IterBlockSize, IterBlockCount); module.WriteConstant("iterPosStateBuffer", iterPosStateBuffer); iterColorStateBuffer = DeviceBuffer2D.Alloc(16, IterBlockSize, IterBlockCount); module.WriteConstant("iterColorStateBuffer", iterColorStateBuffer); entropyXBuffer = DeviceBuffer2D.Alloc(16, IterBlockSize, IterBlockCount); module.WriteConstant("entropyXBuffer", entropyXBuffer); entropyCBuffer = DeviceBuffer2D.Alloc(4, IterBlockSize, IterBlockCount); module.WriteConstant("entropyCBuffer", entropyCBuffer); entropySeedBuffer = DeviceBuffer2D.Alloc(4, IterBlockSize, IterBlockCount); module.WriteConstant("entropySeedBuffer", entropySeedBuffer); HostBuffer2D <uint> hostEntropySeedBuffer = HostBuffer2D <uint> .Alloc(IterBlockSize, IterBlockCount); uint rnd; for (int y = 0; y < IterBlockCount; y++) { for (int x = 0; x < IterBlockSize; x++) { rnd = (uint)rand.Next(65536); hostEntropySeedBuffer[y, x] = rnd; } } CudaMem.Copy(hostEntropySeedBuffer, entropySeedBuffer); hostEntropySeedBuffer.Free(); dotCountBuffer = DeviceBuffer2D.Alloc(8, IterBlockSize, IterBlockCount); module.WriteConstant("dotCountBuffer", dotCountBuffer); peakDensityBuffer = DeviceBuffer2D.Alloc(4, IterBlockSize, IterBlockCount); module.WriteConstant("peakDensityBuffer", peakDensityBuffer); totalIterCountMem = DevicePtr.AllocRaw(8); module.WriteConstant("totalIterCountMem", totalIterCountMem); totalDotCountMem = DevicePtr.AllocRaw(8); module.WriteConstant("totalDotCountMem", totalDotCountMem); densityMem = DevicePtr.AllocRaw(4); module.WriteConstant("densityMem", densityMem); peakDensityMem = DevicePtr.AllocRaw(4); module.WriteConstant("peakDensityMem", peakDensityMem); scaleConstantMem = DevicePtr.AllocRaw(4); module.WriteConstant("scaleConstantMem", scaleConstantMem); paletteImage = CudaArray.Null; paletteTex = module.GetTexRef("paletteTex"); resetBeginEvt = new Event(); resetEndEvt = new Event(); cycleIterEvt = new Event(); cycleStatEvt = new Event(); cycleEndEvt = new Event(); toneBeginEvt = new Event(); toneEndEvt = new Event(); initIteratorsKernel.SetBlockShape(IterBlockSize, 1, 1); initIteratorsKernel.SetGridDim(IterBlockCount, 1); initIteratorsKernel.SetSharedSize(0); resetIteratorsKernel.SetBlockShape(IterBlockSize, 1, 1); resetIteratorsKernel.SetGridDim(IterBlockCount, 1); resetIteratorsKernel.SetSharedSize(0); iterateKernel.SetBlockShape(IterBlockSize, 1, 1); iterateKernel.SetGridDim(IterBlockCount, 1); iterateKernel.SetSharedSize(0); updateStatsKernel.SetBlockShape(1, 1, 1); updateStatsKernel.SetGridDim(1, 1); updateStatsKernel.SetSharedSize(0); initIteratorsKernel.Launch(); context.Synchronize(); }