private void Generate() { try { panelParameters.Enabled = false; ////////////////////////////////////////////////////////////////////////// // 1] Apply bilateral filtering to the input texture as a pre-process ApplyBilateralFiltering( m_textureSourceHeightMap, m_textureTarget0, floatTrackbarControlBilateralRadius.Value, floatTrackbarControlBilateralTolerance.Value, checkBoxWrap.Checked, BILATERAL_PROGRESS ); ////////////////////////////////////////////////////////////////////////// // 2] Compute directional occlusion m_textureTarget1.RemoveFromLastAssignedSlots(); // Prepare computation parameters m_textureTarget0.SetCS( 0 ); m_textureTarget1.SetCSUAV( 0 ); m_SB_Rays.SetInput( 1 ); m_TextureSourceNormal.SetCS( 2 ); 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_GenerateAOMap.Use() ) throw new Exception( "Can't generate self-shadowed bump map as compute shader failed to compile!" ); uint h = Math.Max( 1, MAX_LINES*1024 / W ); uint CallsCount = (uint) 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_GenerateAOMap.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 ); // ImageUtility.ColorProfile profile = m_profilesRGB; // AO maps are sRGB! (although strange, that's certainly to have more range in dark values) ImageUtility.ColorProfile profile = m_profileLinear; float3 whitePoint_xyY = new float3( profile.Chromas.White, 0 ); float3 whitePoint_XYZ = new float3(); ImageUtility.Bitmap tempBitmap = new ImageUtility.Bitmap( W, H ); Renderer.PixelsBuffer Pixels = m_textureTarget_CPU.Map( 0, 0 ); using ( System.IO.BinaryReader R = Pixels.OpenStreamRead() ) for ( uint Y=0; Y < H; Y++ ) { R.BaseStream.Position = Y * Pixels.RowPitch; for ( uint X=0; X < W; X++ ) { whitePoint_xyY.z = R.ReadSingle(); // Linear value ImageUtility.ColorProfile.xyY2XYZ( whitePoint_xyY, ref whitePoint_XYZ ); tempBitmap[X,Y] = new float4( whitePoint_XYZ, 1 ); } } Pixels.Dispose(); m_textureTarget_CPU.UnMap( 0, 0 ); // Convert to RGB ImageUtility.ImageFile temmpImageRGBA32F = new ImageUtility.ImageFile(); tempBitmap.ToImageFile( temmpImageRGBA32F, profile ); if ( m_imageResult == null ) m_imageResult = new ImageUtility.ImageFile(); m_imageResult.ToneMapFrom( temmpImageRGBA32F, ( float3 _HDR, ref float3 _LDR ) => { _LDR = _HDR; // Return as-is.. } ); // Assign result viewportPanelResult.Bitmap = m_imageResult.AsBitmap; } catch ( Exception _e ) { MessageBox( "An error occurred during generation!\r\n\r\nDetails: ", _e ); } finally { panelParameters.Enabled = true; } }
private void buttonTestBilateral_Click( object sender, EventArgs e ) { try { panelParameters.Enabled = false; ////////////////////////////////////////////////////////////////////////// // 1] Apply bilateral filtering to the input texture as a pre-process ApplyBilateralFiltering( m_textureSourceHeightMap, m_textureTarget0, floatTrackbarControlBilateralRadius.Value, floatTrackbarControlBilateralTolerance.Value, checkBoxWrap.Checked, 100 ); progressBar.Value = progressBar.Maximum; ////////////////////////////////////////////////////////////////////////// // 2] Copy target to staging for CPU readback and update the resulting bitmap m_textureTarget_CPU.CopyFrom( m_textureTarget0 ); ImageUtility.Bitmap tempBitmap = new ImageUtility.Bitmap( W, H ); Renderer.PixelsBuffer Pixels = m_textureTarget_CPU.Map( 0, 0 ); using ( System.IO.BinaryReader R = Pixels.OpenStreamRead() ) for ( uint Y=0; Y < H; Y++ ) { R.BaseStream.Position = Y * Pixels.RowPitch; for ( uint X=0; X < W; X++ ) { float AO = R.ReadSingle(); tempBitmap[X,Y] = new float4( AO, AO, AO, 1 ); } } Pixels.Dispose(); m_textureTarget_CPU.UnMap( 0, 0 ); // Convert to RGB // ImageUtility.ColorProfile Profile = m_ProfilesRGB; // AO maps are sRGB! (although strange, that's certainly to have more range in dark values) ImageUtility.ColorProfile Profile = m_profilesRGB; // AO maps are sRGB! (although strange, that's certainly to have more range in dark values) if ( m_imageResult != null ) m_imageResult.Dispose(); m_imageResult = new ImageUtility.ImageFile(); tempBitmap.ToImageFile( m_imageResult, Profile ); // Assign result viewportPanelResult.Bitmap = m_imageResult.AsCustomBitmap( ( ref float4 _color ) => {} ); } catch ( Exception _e ) { MessageBox( "An error occurred during generation!\r\n\r\nDetails: ", _e ); } finally { panelParameters.Enabled = true; } }