public static void EnsureDirectory(System.IO.DirectoryInfo oDirInfo) { if (oDirInfo.Parent != null) EnsureDirectory(oDirInfo.Parent); if (!oDirInfo.Exists) { oDirInfo.Create(); } }
private static void createDir(System.IO.DirectoryInfo di) { if(!di.Parent.Exists) createDir(di.Parent); if(!di.Exists) { di.Create(); } }
/// <summary> /// Saves a texture to disk /// </summary> /// <param name="_Texture"></param> /// <param name="_FileName"></param> /// <param name="_FileType"></param> /// <param name="_Format"></param> private void SaveImage( ImageUtility.Bitmap _Texture, System.IO.FileInfo _FileName, ImageUtility.Bitmap.FILE_TYPE _FileType, ImageUtility.Bitmap.FORMAT_FLAGS _Format ) { using ( System.IO.FileStream S = _FileName.Create() ) _Texture.Save( S, _FileType, _Format, null ); }
/// <summary> /// Creates a new empty file with the specified pathname. /// </summary> /// <param name="path">The abstract pathname of the file</param> /// <returns>True if the file does not exist and was succesfully created</returns> public static bool CreateNewFile(System.IO.FileInfo path) { if (path.Exists) { return false; } else { System.IO.FileStream createdFile = path.Create(); createdFile.Close(); return true; } }
unsafe void ComputeBRDFIntegral( System.IO.FileInfo _TableFileName, uint _TableSize ) { // ComputeShader CS = new ComputeShader( m_Device, new System.IO.FileInfo( "Shaders/ComputeBRDFIntegral.hlsl" ), "CS", new ShaderMacro[0] ); ComputeShader CS = new ComputeShader( m_Device, new System.IO.FileInfo( "Shaders/ComputeBRDFIntegral.hlsl" ), "CS", new ShaderMacro[] { new ShaderMacro( "IMPORTANCE_SAMPLING", "1" ) } ); Texture2D TexTable = new Texture2D( m_Device, _TableSize, _TableSize, 1, 1, PIXEL_FORMAT.RG32_FLOAT, false, true, null ); TexTable.SetCSUAV( 0 ); CS.Use(); CS.Dispatch( _TableSize >> 4, _TableSize >> 4, 1 ); CS.Dispose(); string DDSFileName = System.IO.Path.GetFileNameWithoutExtension( _TableFileName.FullName ) + ".dds"; // DirectXTexManaged.TextureCreator.CreateDDS( DDSFileName, TexTable ); throw new Exception( "Deprecated!" ); Texture2D TexTableStaging = new Texture2D( m_Device, _TableSize, _TableSize, 1, 1, PIXEL_FORMAT.RG32_FLOAT, true, false, null ); TexTableStaging.CopyFrom( TexTable ); TexTable.Dispose(); // Write tables float2 Temp = new float2(); float F0_analytical; float ambient_analytical; float maxAbsoluteError_F0 = 0.0f; float maxRelativeError_F0 = 0.0f; float avgAbsoluteError_F0 = 0.0f; float avgRelativeError_F0 = 0.0f; float maxAbsoluteError_ambient = 0.0f; float maxRelativeError_ambient = 0.0f; float avgAbsoluteError_ambient = 0.0f; float avgRelativeError_ambient = 0.0f; float2[] Integral_SpecularReflectance = new float2[_TableSize]; PixelsBuffer Buff = TexTableStaging.Map( 0, 0 ); using ( System.IO.BinaryReader R = Buff.OpenStreamRead() ) using ( System.IO.FileStream S = _TableFileName.Create() ) using ( System.IO.BinaryWriter W = new System.IO.BinaryWriter( S ) ) for ( int Y=0; Y < _TableSize; Y++ ) { float2 SumSpecularlyReflected = float2.Zero; for ( int X=0; X < _TableSize; X++ ) { Temp.x = R.ReadSingle(); Temp.y = R.ReadSingle(); W.Write( Temp.x ); W.Write( Temp.y ); SumSpecularlyReflected.x += Temp.x; SumSpecularlyReflected.y += Temp.y; // Check analytical solution float NdotV = (float) X / (_TableSize-1); float roughness = (float) Y / (_TableSize-1); AnalyticalBRDFIntegral_Order3( NdotV, roughness, out F0_analytical, out ambient_analytical ); // AnalyticalBRDFIntegral_Order2( NdotV, roughness, out F0_analytical, out ambient_analytical ); float absoluteError_F0 = Math.Abs( F0_analytical - Temp.x ); float relativeError_F0 = F0_analytical > 1e-6f ? Temp.x / F0_analytical : 0.0f; maxAbsoluteError_F0 = Math.Max( maxAbsoluteError_F0, absoluteError_F0 ); maxRelativeError_F0 = Math.Max( maxRelativeError_F0, relativeError_F0 ); avgAbsoluteError_F0 += absoluteError_F0; avgRelativeError_F0 += relativeError_F0; float absoluteError_ambient = Math.Abs( ambient_analytical - Temp.y ); float relativeError_ambient = ambient_analytical > 1e-6f ? Temp.x / ambient_analytical : 0.0f; maxAbsoluteError_ambient = Math.Max( maxAbsoluteError_ambient, absoluteError_ambient ); maxRelativeError_ambient = Math.Max( maxRelativeError_ambient, relativeError_ambient ); avgAbsoluteError_ambient += absoluteError_ambient; avgRelativeError_ambient += relativeError_ambient; } // Normalize and store "not specularly reflected" light SumSpecularlyReflected = SumSpecularlyReflected / _TableSize; float sum_dielectric = 1.0f - (0.04f * SumSpecularlyReflected.x + SumSpecularlyReflected.y); float sum_metallic = 1.0f - (SumSpecularlyReflected.x + SumSpecularlyReflected.y); Integral_SpecularReflectance[Y] = SumSpecularlyReflected; } TexTableStaging.UnMap( 0, 0 ); avgAbsoluteError_F0 /= _TableSize*_TableSize; avgRelativeError_F0 /= _TableSize*_TableSize; avgAbsoluteError_ambient /= _TableSize*_TableSize; avgRelativeError_ambient /= _TableSize*_TableSize; string TotalSpecularReflectionTableFileName = System.IO.Path.GetFileNameWithoutExtension( _TableFileName.FullName ) + ".table"; using ( System.IO.FileStream S = new System.IO.FileInfo( TotalSpecularReflectionTableFileName ).Create() ) using ( System.IO.BinaryWriter W = new System.IO.BinaryWriter( S ) ) for ( int i=0; i < _TableSize; i++ ) { W.Write( Integral_SpecularReflectance[i].x ); W.Write( Integral_SpecularReflectance[i].y ); } }
void ComputeBRDFIntegral( System.IO.FileInfo _TableFileName0, System.IO.FileInfo _TableFileName1, int _TableSize ) { const int SAMPLES_COUNT_THETA = 128; const int SAMPLES_COUNT_PHI = 2*SAMPLES_COUNT_THETA; const double dTheta = 0.5 * Math.PI / SAMPLES_COUNT_THETA; const double dPhi = 2.0 * Math.PI / SAMPLES_COUNT_PHI; double[,] Table0 = new double[_TableSize,_TableSize]; double[,] Table1 = new double[_TableSize,_TableSize]; // float Theta = 0.5 * _UV.x * PI; // float3 ToLight = float3( sin( Theta ), 0, cos( Theta ) ); // float3 ToView = float3( -sin( Theta ), 0, cos( Theta ) ); // // float Albedo = 0.0; // const int THETA_COUNT = 64; // * $alwaysOne; // warning X4008: floating point division by zero // const float dTheta = HALFPI / THETA_COUNT; // const float dPhi = TWOPI / THETA_COUNT; // for ( int i=0; i < THETA_COUNT; i++ ) // { // Theta = HALFPI * (0.5 + i) / THETA_COUNT; // for ( int j=0; j < THETA_COUNT; j++ ) // { // float Phi = PI * j / THETA_COUNT; // // ToView = float3( sin( Theta ) * cos( Phi ), sin( Theta ) * sin( Phi ), cos( Theta ) ); // // float3 Half = normalize( ToLight + ToView ); // // // "True" and expensive evaluation of the Ward BRDF // float alpha = Roughness; // // float CosDelta = Half.z; // dot( Half, _wsNormal ); // float delta = acos( CosDelta ); // float CosThetaL = ToLight.z; // float SinThetaL = sqrt( 1.0 - CosThetaL*CosThetaL ); // float PhiL = atan2( ToLight.y, ToLight.x ); // float CosThetaV = ToView.z; // float SinThetaV = sqrt( 1.0 - CosThetaV*CosThetaV ); // float PhiV = atan2( ToView.y, ToView.x ); // // float BRDF = 1.0 / square(alpha) * exp( -square( tan( delta ) / alpha ) ) * 2.0 * (1.0 + CosThetaL*CosThetaV + SinThetaL*SinThetaV*cos( PhiV - PhiL )) / pow4( CosThetaL + CosThetaV ); // // Albedo += BRDF * cos( Theta ) * sin( Theta ) * dTheta * dPhi; // } // } // // Albedo *= INVPI; // Since we forgot that in the main loop float3 View = new float3(); float3 Light = new float3(); for ( int Y=0; Y < _TableSize; Y++ ) { float Roughness = Math.Max( 0.01f, (float) Y / (_TableSize-1) ); float r2 = Roughness*Roughness; for ( int X=0; X < _TableSize; X++ ) { // double CosThetaV = (double) (1+X) / _TableSize; // double SinThetaV = Math.Sqrt( 1.0 - CosThetaV*CosThetaV ); double ThetaV = 0.5 * Math.PI * X / _TableSize; double CosThetaV = Math.Cos( ThetaV ); double SinThetaV = Math.Sin( ThetaV ); View.x = (float) SinThetaV; View.y = (float) CosThetaV; View.z = 0.0f; double SumA = 0.0; double SumB = 0.0; for ( int Theta=0; Theta < SAMPLES_COUNT_THETA; Theta++ ) { double fTheta = 0.5 * Math.PI * (0.5 + Theta) / SAMPLES_COUNT_THETA; double CosThetaL = Math.Cos( fTheta ); double SinThetaL = Math.Sin( fTheta ); // Compute solid angle double SolidAngle = SinThetaL * dTheta * dPhi; double ProjectedSolidAngle = CosThetaL * SolidAngle; // (N.L) sin(Theta).dTheta.dPhi for ( int Phi=0; Phi < SAMPLES_COUNT_PHI; Phi++ ) { double fPhi = Math.PI * Phi / SAMPLES_COUNT_PHI; double CosPhiLight = Math.Cos( fPhi ); double SinPhiLight = Math.Sin( fPhi ); Light.x = (float) (SinPhiLight * SinThetaL); Light.y = (float) CosThetaL; Light.z = (float) (CosPhiLight * SinThetaL); // // Transform into "reflected-view-centered space" // Light = Light.x * OrthoX + Light.y * ReflectedView + Light.z * OrthoZ; // if ( Light.y < 0.0f ) // continue; // ProjectedSolidAngle = dPhi * dTheta * Light.y * Math.Sqrt( 1.0 - Light.y*Light.y ); float3 H_unorm = View + Light; float3 H = H_unorm.Normalized; // // Compute normal distribution function // float H_unorm_dot_N = H_unorm.y; // float H_unorm_dot_N2 = H_unorm_dot_N * H_unorm_dot_N; // float H_unorm_dot_N4 = H_unorm_dot_N2 * H_unorm_dot_N2; // // double BRDF = Math.Exp( -(H_unorm.x*H_unorm.x + H_unorm.z*H_unorm.z) / (r2 * H_unorm_dot_N2) ) * H_unorm.Dot( H_unorm ) / H_unorm_dot_N4; // Expensive Ward double PhiL = Math.Atan2( Light.z, Light.x ); double PhiV = Math.Atan2( View.z, View.x ); double tanDelta = Math.Tan( Math.Acos( H.y ) ); double BRDF = Math.Exp( -tanDelta*tanDelta / r2 ) * 2.0 * (1.0 + CosThetaL*CosThetaV + SinThetaL*SinThetaV*Math.Cos( PhiV - PhiL )) / Math.Pow( CosThetaL + CosThetaV, 4.0 ); // Try with Unreal's GGX & Smith G term to see if we get the same thing // double alpha = r2; // double alpha2 = alpha*alpha; // double D = alpha2 / (Math.PI * Math.Pow( HoN2*(alpha2 - 1.0) + 1.0, 2.0 )); // // double k = (Roughness + 1)*(Roughness + 1) / 8.0; // // HoN = H_norm.y; // double HoL = H_norm.Dot( Light ); // double HoV = H_norm.Dot( View ); // double Gl = HoL / (HoL * (1-k) + k); // double Gv = HoV / (HoV * (1-k) + k); // double G = Gl * Gv; // // double BRDF = D * G / (4.0 * Light.y * View.y); // //double BRDF = D * G * H_norm.Dot( View ) / Math.Max( 1e-6, HoN * View.y); // Expensive Ward with angles // double PhiL = Math.Atan2( Light.z, Light.x ); // double PhiV = 0.0;//Math.Atan2( View.z, View.x ); // double tanDelta = Math.Tan( Math.Acos( H_norm.y ) ); // double BRDF = Math.Exp( -tanDelta*tanDelta / r2 ) * 2.0 * (1.0 + CosThetaLight*CosThetaView + SinThetaLight*SinThetaView*Math.Cos( PhiV - PhiL )) / Math.Pow( CosThetaLight + CosThetaView, 4.0 ); // // SumA += BRDF * ProjectedSolidAngle; // SumB += BRDF * ProjectedSolidAngle; // Compute Fresnel terms double VoH = View.x * H.x + View.y * H.y; double Schlick = 1.0 - VoH; double Schlick5 = Schlick * Schlick; Schlick5 *= Schlick5 * Schlick; double FresnelA = 1.0 - Schlick5; double FresnelB = Schlick5; //FresnelA = FresnelB = 1.0; SumA += FresnelA * BRDF * ProjectedSolidAngle; SumB += FresnelB * BRDF * ProjectedSolidAngle; } } SumA /= Math.PI * r2; SumB /= Math.PI * r2; // // For few samples, the sum goes over 1 because we have poor solid angle sampling resolution... // SumA = Math.Min( 1.0, SumA ); // SumB = Math.Min( 1.0, SumB ); Table0[X,Y] = SumA; Table1[X,Y] = SumB; } } // Write table 0 using ( System.IO.FileStream S = _TableFileName0.Create() ) using ( System.IO.BinaryWriter W = new System.IO.BinaryWriter( S ) ) for ( int Y=0; Y < _TableSize; Y++ ) { for ( int X=0; X < _TableSize; X++ ) W.Write( Table0[X,Y] ); } // Write table 1 using ( System.IO.FileStream S = _TableFileName1.Create() ) using ( System.IO.BinaryWriter W = new System.IO.BinaryWriter( S ) ) for ( int Y=0; Y < _TableSize; Y++ ) { for ( int X=0; X < _TableSize; X++ ) W.Write( Table1[X,Y] ); } }
void ComputeBRDFIntegralImportanceSampling( System.IO.FileInfo _TableFileName0, System.IO.FileInfo _TableFileName1, int _TableSize ) { const int SAMPLES_COUNT = 1024; double[,] Table0 = new double[_TableSize,_TableSize]; double[,] Table1 = new double[_TableSize,_TableSize]; WMath.Hammersley QRNG = new WMath.Hammersley(); double[,] Sequence = QRNG.BuildSequence( SAMPLES_COUNT, 2 ); float3 View = new float3(); float3 Light = new float3(); float3 Half = new float3(); for ( int Y=0; Y < _TableSize; Y++ ) { double Roughness = Math.Max( 0.01f, (float) Y / (_TableSize-1) ); //Roughness = Math.Pow( 1.0 - Roughness, 4.0 ); double r2 = Roughness*Roughness; for ( int X=0; X < _TableSize; X++ ) { float CosThetaView = (float) (1+X) / _TableSize; float SinThetaView = (float) Math.Sqrt( 1.0f - CosThetaView*CosThetaView ); View.x = SinThetaView; View.y = CosThetaView; View.z = 0.0f; double SumA = 0.0; double SumB = 0.0; for ( int SampleIndex=0; SampleIndex < SAMPLES_COUNT; SampleIndex++ ) { double X0 = Sequence[SampleIndex,0]; double X1 = Sequence[SampleIndex,1]; double PhiH = 2.0 * Math.PI * X0; // WARD double ThetaH = Math.Atan( -r2 * Math.Log( 1.0 - X1 ) ); double CosThetaH = Math.Cos( ThetaH ); double SinThetaH = Math.Sin( ThetaH ); // // GGX // double a = r2; // double CosThetaH = Math.Sqrt( (1.0 - X1) / (1.0 + (a*a - 1.0) * X1 ) ); // double SinThetaH = Math.Sqrt( 1.0f - CosThetaH * CosThetaH ); double CosPhiH = Math.Cos( PhiH ); double SinPhiH = Math.Sin( PhiH ); Half.x = (float) (SinPhiH * SinThetaH); Half.y = (float) CosThetaH; Half.z = (float) (CosPhiH * SinThetaH); Light = 2.0f * View.Dot( Half ) * Half - View; // Light is on the other size of the Half vector... // Intuitively, we should have the same result if we sampled around the reflected view direction // float3 ReflectedView = 2.0f * View.Dot( float3.UnitY ) * float3.UnitY - View; // float3 OrthoY = ReflectedView.Cross( float3.UnitZ ).Normalized; // float3 OrthoX = float3.UnitZ; // Light = Half.x * OrthoX + Half.y * ReflectedView + Half.z * OrthoY; // // Half = (View + Light).Normalized; if ( Light.y <= 0 ) continue; double HoN = Half.y; double HoN2 = HoN*HoN; double HoV = Half.Dot( View ); // float HoV = Half.x * View.x + Half.y * View.y; // We know that Z=0 here... double HoL = Half.Dot( Light ); double NoL = Light.y; double NoV = View.y; // Apply sampling weight for correct distribution double SampleWeight = 2.0 / (1.0 + View.y / Light.y); double BRDF = SampleWeight; // Try with Unreal's GGX & Smith G term to see if we get the same thing // // // GGX NDF // // double alpha = r2; // // double alpha2 = alpha*alpha; // // double D = alpha2 / (Math.PI * Math.Pow( HoN2*(alpha2 - 1.0) + 1.0, 2.0 )); // // // Smith masking/shadowing // double k = (Roughness + 1)*(Roughness + 1) / 8.0; // double Gl = NoL / (NoL * (1-k) + k); // double Gv = NoV / (NoV * (1-k) + k); // double G = Gl * Gv; // // //double BRDF = G / (4.0 * View.y); // //double BRDF = G * HoV / (HoN * NoV); // double BRDF = NoL * GSmith( Roughness, NoV, NoL ) * 4.0f * HoV / HoN; // Compute Fresnel terms double Schlick = 1.0 - HoV; double Schlick5 = Schlick * Schlick; Schlick5 *= Schlick5 * Schlick; double FresnelA = 1.0f - Schlick5; double FresnelB = Schlick5; //FresnelA = FresnelB = 1.0; SumA += FresnelA * BRDF; SumB += FresnelB * BRDF; } // SumA *= 1.0 / (SAMPLES_COUNT * Math.PI * r2); // SumB *= 1.0 / (SAMPLES_COUNT * Math.PI * r2); SumA /= SAMPLES_COUNT; SumB /= SAMPLES_COUNT; // For few samples, the sum goes over 1 because we have poor solid angle sampling resolution... // SumA = Math.Min( 1.0, SumA ); // SumB = Math.Min( 1.0, SumB ); Table0[X,Y] = SumA; Table1[X,Y] = SumB; } } // Write table 0 using ( System.IO.FileStream S = _TableFileName0.Create() ) using ( System.IO.BinaryWriter W = new System.IO.BinaryWriter( S ) ) for ( int Y=0; Y < _TableSize; Y++ ) { for ( int X=0; X < _TableSize; X++ ) W.Write( Table0[X,Y] ); } // Write table 1 using ( System.IO.FileStream S = _TableFileName1.Create() ) using ( System.IO.BinaryWriter W = new System.IO.BinaryWriter( S ) ) for ( int Y=0; Y < _TableSize; Y++ ) { for ( int X=0; X < _TableSize; X++ ) W.Write( Table1[X,Y] ); } }
private bool FileCreate(System.IO.FileInfo cFileInfo) { reFileCreate: bool bCreateFile = false; if (!cFileInfo.Exists) { try { System.IO.FileStream fstr = cFileInfo.Create(); fstr.Close(); } catch (System.IO.DirectoryNotFoundException) { if (CreatePath(cFileInfo.DirectoryName)) { goto reFileCreate; } } catch (Exception ex) { Console.WriteLine(ex.ToString()); } } return bCreateFile; }
private void CreateDirectory(System.IO.DirectoryInfo dir) { if (!dir.Parent.Exists) this.CreateDirectory(dir.Parent); dir.Create(); }
public System.Threading.Tasks.Task<bool> Save(string mail, string password, System.IO.DirectoryInfo imgDir) { return System.Threading.Tasks.Task.Factory.StartNew(() => { var args = new SavingSettingEventArgs(mail, password, imgDir); OnSavingSetting(args); if (args.Cancel) return false; GPlusImageDownloader.Properties.Settings.Default.EmailAddress = mail; GPlusImageDownloader.Properties.Settings.Default.Password = password; GPlusImageDownloader.Properties.Settings.Default.ImageSaveDirectory = imgDir.FullName; GPlusImageDownloader.Properties.Settings.Default.Save(); EmailAddress = mail; Password = password; ImageSaveDirectory = imgDir; if (!imgDir.Exists) try { imgDir.Create(); IsErrorNotFoundImageSaveDirectory = false; } catch { IsErrorNotFoundImageSaveDirectory = true; } else IsErrorNotFoundImageSaveDirectory = false; OnSavedSetting(new EventArgs()); SerializeCookie(Cookies); return true; }); }
public void Save( System.IO.FileInfo _FileName, FORMAT_FLAGS _Parms, FormatEncoderOptions _Options ) { FILE_TYPE FileType = GetFileType( _FileName ); using ( System.IO.FileStream S = _FileName.Create() ) Save( S, FileType, _Parms, _Options ); }