private void Generate() { try { tabControlGenerators.Enabled = false; ////////////////////////////////////////////////////////////////////////// // 1] Apply bilateral filtering to the input texture as a pre-process ApplyBilateralFiltering(m_TextureSource, m_TextureTarget0, floatTrackbarControlBilateralRadius.Value, floatTrackbarControlBilateralTolerance.Value, checkBoxWrap.Checked); ////////////////////////////////////////////////////////////////////////// // 2] Compute directional occlusion m_TextureTarget1.RemoveFromLastAssignedSlots(); // Prepare computation parameters m_TextureTarget0.SetCS(0); m_TextureTarget1.SetCSUAV(0); m_SB_Rays.SetInput(1); m_CB_Input.m.RaysCount = (UInt32)Math.Min(MAX_THREADS, integerTrackbarControlRaysCount.Value); m_CB_Input.m.MaxStepsCount = (UInt32)integerTrackbarControlMaxStepsCount.Value; m_CB_Input.m.Tile = (uint)(checkBoxWrap.Checked ? 1 : 0); m_CB_Input.m.TexelSize_mm = TextureSize_mm / Math.Max(W, H); m_CB_Input.m.Displacement_mm = TextureHeight_mm; // Start if (!m_CS_GenerateSSBumpMap.Use()) { throw new Exception("Can't generate self-shadowed bump map as compute shader failed to compile!"); } int h = Math.Max(1, MAX_LINES * 1024 / W); int CallsCount = (int)Math.Ceiling((float)H / h); for (int i = 0; i < CallsCount; i++) { m_CB_Input.m.Y0 = (UInt32)(i * h); m_CB_Input.UpdateData(); m_CS_GenerateSSBumpMap.Dispatch(W, h, 1); m_Device.Present(true); progressBar.Value = (int)(0.01f * (BILATERAL_PROGRESS + (100 - BILATERAL_PROGRESS) * (i + 1) / (CallsCount)) * progressBar.Maximum); // for ( int a=0; a < 10; a++ ) Application.DoEvents(); } m_TextureTarget1.RemoveFromLastAssignedSlotUAV(); // So we can use it as input for next stage progressBar.Value = progressBar.Maximum; // Compute in a single shot (this is madness!) // m_CB_Input.m.y = 0; // m_CB_Input.UpdateData(); // m_CS_GenerateSSBumpMap.Dispatch( W, H, 1 ); ////////////////////////////////////////////////////////////////////////// // 3] Copy target to staging for CPU readback and update the resulting bitmap m_TextureTarget_CPU.CopyFrom(m_TextureTarget1); if (m_BitmapResult != null) { m_BitmapResult.Dispose(); } m_BitmapResult = null; m_BitmapResult = new ImageUtility.Bitmap(W, H, m_LinearProfile); m_BitmapResult.HasAlpha = true; RendererManaged.PixelsBuffer Pixels = m_TextureTarget_CPU.Map(0, 0); using (System.IO.BinaryReader R = Pixels.OpenStreamRead()) for (int Y = 0; Y < H; Y++) { R.BaseStream.Position = Y * Pixels.RowPitch; for (int X = 0; X < W; X++) { ImageUtility.float4 Color = new ImageUtility.float4(R.ReadSingle(), R.ReadSingle(), R.ReadSingle(), R.ReadSingle()); Color = m_LinearProfile.RGB2XYZ(Color); m_BitmapResult.ContentXYZ[X, Y] = Color; } } Pixels.Dispose(); m_TextureTarget_CPU.UnMap(0, 0); // Assign result viewportPanelResult.Image = m_BitmapResult; } catch (Exception _e) { MessageBox("An error occurred during generation!\r\n\r\nDetails: ", _e); } finally { tabControlGenerators.Enabled = true; } }
private void LoadHeightMap(System.IO.FileInfo _FileName) { try { tabControlGenerators.Enabled = false; // Dispose of existing resources if (m_BitmapSource != null) { m_BitmapSource.Dispose(); } m_BitmapSource = null; if (m_TextureTarget_CPU != null) { m_TextureTarget_CPU.Dispose(); } m_TextureTarget_CPU = null; if (m_TextureTarget0 != null) { m_TextureTarget0.Dispose(); } m_TextureTarget0 = null; if (m_TextureTarget1 != null) { m_TextureTarget1.Dispose(); } m_TextureTarget1 = null; if (m_TextureSource != null) { m_TextureSource.Dispose(); } m_TextureSource = null; // Load the source image assuming it's in linear space m_SourceFileName = _FileName; m_BitmapSource = new ImageUtility.Bitmap(_FileName, m_LinearProfile); outputPanelInputHeightMap.Image = m_BitmapSource; W = m_BitmapSource.Width; H = m_BitmapSource.Height; // Build the source texture RendererManaged.PixelsBuffer SourceHeightMap = new RendererManaged.PixelsBuffer(W * H * 4); using (System.IO.BinaryWriter Wr = SourceHeightMap.OpenStreamWrite()) for (int Y = 0; Y < H; Y++) { for (int X = 0; X < W; X++) { Wr.Write(m_BitmapSource.ContentXYZ[X, Y].y); } } m_TextureSource = new RendererManaged.Texture2D(m_Device, W, H, 1, 1, RendererManaged.PIXEL_FORMAT.R32_FLOAT, false, false, new RendererManaged.PixelsBuffer[] { SourceHeightMap }); // Build the target UAV & staging texture for readback m_TextureTarget0 = new RendererManaged.Texture2D(m_Device, W, H, 1, 1, RendererManaged.PIXEL_FORMAT.R32_FLOAT, false, true, null); m_TextureTarget1 = new RendererManaged.Texture2D(m_Device, W, H, 1, 1, RendererManaged.PIXEL_FORMAT.RGBA32_FLOAT, false, true, null); m_TextureTarget_CPU = new RendererManaged.Texture2D(m_Device, W, H, 1, 1, RendererManaged.PIXEL_FORMAT.RGBA32_FLOAT, true, false, null); tabControlGenerators.Enabled = true; buttonGenerate.Focus(); } catch (Exception _e) { MessageBox("An error occurred while opening the image:\n\n", _e); } }