public Texture2D Image2Texture(System.IO.FileInfo _fileName, ImageUtility.COMPONENT_FORMAT _componentFormat) { ImageUtility.ImagesMatrix images = null; if (_fileName.Extension.ToLower() == ".dds") { images = new ImageUtility.ImagesMatrix(); images.DDSLoadFile(_fileName); } else { ImageUtility.ImageFile image = new ImageUtility.ImageFile(_fileName); if (image.PixelFormat != ImageUtility.PIXEL_FORMAT.BGRA8) { ImageUtility.ImageFile badImage = image; image = new ImageUtility.ImageFile(); image.ConvertFrom(badImage, ImageUtility.PIXEL_FORMAT.BGRA8); badImage.Dispose(); } images = new ImageUtility.ImagesMatrix(new ImageUtility.ImageFile[1, 1] { { image } }); } return(new Texture2D(m_device, images, _componentFormat)); }
private void Generate() { try { tabControlGenerators.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); ////////////////////////////////////////////////////////////////////////// // 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!"); } uint h = Math.Max(1, MAX_LINES * 1024 / W); uint callsCount = (uint)Math.Ceiling((float)H / h); for (uint i = 0; i < callsCount; i++) { m_CB_Input.m.Y0 = 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_imageResult != null) { m_imageResult.Dispose(); } m_imageResult = null; m_imageResult = new ImageUtility.ImageFile(W, H, ImageUtility.PIXEL_FORMAT.RGBA8, m_linearProfile); float4[] scanline = new float4[W]; PixelsBuffer pixels = m_textureTarget_CPU.MapRead(0, 0); using (System.IO.BinaryReader R = pixels.OpenStreamRead()) for (uint Y = 0; Y < H; Y++) { R.BaseStream.Position = Y * pixels.RowPitch; for (int X = 0; X < W; X++) { scanline[X].Set(R.ReadSingle(), R.ReadSingle(), R.ReadSingle(), R.ReadSingle()); } m_imageResult.WriteScanline(Y, scanline); } m_textureTarget_CPU.UnMap(pixels); // Assign result viewportPanelResult.Image = m_imageResult; } 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_imageSourceHeightMap != null) { m_imageSourceHeightMap.Dispose(); } m_imageSourceHeightMap = 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_textureSourceHeightMap != null) { m_textureSourceHeightMap.Dispose(); } m_textureSourceHeightMap = null; // Load the source image m_SourceFileName = _FileName; m_imageSourceHeightMap = new ImageUtility.ImageFile(_FileName); outputPanelInputHeightMap.Image = m_imageSourceHeightMap; W = m_imageSourceHeightMap.Width; H = m_imageSourceHeightMap.Height; // Build the source texture assuming the image is in linear space float4[] scanline = new float4[W]; PixelsBuffer SourceHeightMap = new PixelsBuffer(W * H * 4); // using ( System.IO.BinaryWriter Wr = SourceHeightMap.OpenStreamWrite() ) // for ( uint Y=0; Y < H; Y++ ) { // m_imageSourceHeightMap.ReadScanline( Y, scanline ); // for ( int X=0; X < W; X++ ) // Wr.Write( scanline[X].x ); // } using (System.IO.BinaryWriter Wr = SourceHeightMap.OpenStreamWrite()) { m_imageSourceHeightMap.ReadPixels((uint X, uint Y, ref float4 _color) => { Wr.Write(_color.x); }); } m_textureSourceHeightMap = new Texture2D(m_device, W, H, 1, 1, ImageUtility.PIXEL_FORMAT.R32F, ImageUtility.COMPONENT_FORMAT.AUTO, false, false, new PixelsBuffer[] { SourceHeightMap }); // Build the target UAV & staging texture for readback m_textureTarget0 = new Texture2D(m_device, W, H, 1, 1, ImageUtility.PIXEL_FORMAT.R32F, ImageUtility.COMPONENT_FORMAT.AUTO, false, true, null); m_textureTarget1 = new Texture2D(m_device, W, H, 1, 1, ImageUtility.PIXEL_FORMAT.RGBA32F, ImageUtility.COMPONENT_FORMAT.AUTO, false, true, null); m_textureTarget_CPU = new Texture2D(m_device, W, H, 1, 1, ImageUtility.PIXEL_FORMAT.RGBA32F, ImageUtility.COMPONENT_FORMAT.AUTO, true, false, null); tabControlGenerators.Enabled = true; buttonGenerate.Focus(); } catch (Exception _e) { MessageBox("An error occurred while opening the image:\n\n", _e); } }