Texture2D LoadCubeMap(System.IO.FileInfo _fileName) { if (!_fileName.Exists) { throw new Exception("File not found!"); } string Ext = _fileName.Extension.ToLower(); switch (Ext) { case ".dds": using (ImageUtility.ImagesMatrix I = new ImageUtility.ImagesMatrix()) { I.DDSLoadFile(_fileName); return(new Texture2D(m_Device, I, ImageUtility.COMPONENT_FORMAT.AUTO)); } case ".bimage": { BImage I = new BImage(_fileName); return(I.CreateTextureCube(m_Device)); } default: throw new Exception("Unsupported image extension!"); } }
void LoadHDRImage() { // m_HDRImage.Load( new System.IO.FileInfo( @".\Images\grace-new.hdr" ) ); m_HDRImage.Load(new System.IO.FileInfo(@".\Images\ennis_1024x512.hdr")); ImageUtility.ImagesMatrix badImages = new ImageUtility.ImagesMatrix(new ImageUtility.ImageFile[, ] { { m_HDRImage } }); ImageUtility.ImagesMatrix images = new ImageUtility.ImagesMatrix(); images.ConvertFrom(badImages, ImageUtility.PIXEL_FORMAT.RGBA32F, badImages.ColorProfile); m_Tex_HDREnvironment = new Texture2D(m_device, images, ImageUtility.COMPONENT_FORMAT.AUTO); // ImageUtility.ImageFile tempLDRImage = new ImageUtility.ImageFile(); // tempLDRImage.ToneMapFrom( m_HDRImage, ( float3 _HDR, ref float3 _LDR ) => { // _LDR = _HDR; // } ); // graphPanel.Bitmap = tempLDRImage.AsBitmap; // Integrate SH // EncodeSH(); // EncodeSH_20Orders(); // Test numerical integration // NumericalIntegration(); // NumericalIntegration_20Orders(); TestIntegral(); }
void EncodeCubeMapIntoSH(ImageUtility.ImagesMatrix _cubemap) { float3[] envSH = _cubemap.EncodeSHOrder2(); SphericalHarmonics.SHFunctions.FilterHanning(envSH, 2.8f); // Important otherwise banding occurs! m_CB_SH.m._SH0.Set(envSH[0], 0); m_CB_SH.m._SH1.Set(envSH[1], 0); m_CB_SH.m._SH2.Set(envSH[2], 0); m_CB_SH.m._SH3.Set(envSH[3], 0); m_CB_SH.m._SH4.Set(envSH[4], 0); m_CB_SH.m._SH5.Set(envSH[5], 0); m_CB_SH.m._SH6.Set(envSH[6], 0); m_CB_SH.m._SH7.Set(envSH[7], 0); m_CB_SH.m._SH8.Set(envSH[8], 0); m_CB_SH.UpdateData(); // // Encode as order-9 (100 coefficients), stored into a texture... FOR DEBUG PURPOSE ONLY!! // float3[] envSH2 = _cubemap.EncodeSH( 9 ); // // PixelsBuffer content = new PixelsBuffer( 100 * 16 ); // using ( BinaryWriter W = content.OpenStreamWrite() ) // for ( uint i=0; i < 100; i++ ) { // W.Write( envSH2[i].x ); // W.Write( envSH2[i].y ); // W.Write( envSH2[i].z ); // W.Write( 0.0f ); // } // // Texture2D texSH = new Texture2D( m_device, 100, 1, 1, 1, ImageUtility.PIXEL_FORMAT.RGBA32F, ImageUtility.COMPONENT_FORMAT.AUTO, false, false, new PixelsBuffer[] { content } ); // texSH.Set( 30 ); }
Texture2D LoadLTC(FileInfo _LTCFileName) { using (ImageUtility.ImagesMatrix M = new ImageUtility.ImagesMatrix()) { M.DDSLoadFile(_LTCFileName); Texture2D T = new Texture2D(m_device, M, ImageUtility.COMPONENT_FORMAT.AUTO); return(T); } }
/// <summary> /// Concatenates multiple tables into one single texture 2D array /// </summary> /// <param name="_tablesFileNames"></param> /// <param name="_targetFileName"></param> /// <param name="_foramt"></param> static void ExportTexture(FileInfo[] _tablesFileNames, FileInfo _targetFileName, ImageUtility.PIXEL_FORMAT _format) { // Load tables LTC[][,] tables = new LTC[_tablesFileNames.Length][, ]; for (int i = 0; i < _tablesFileNames.Length; i++) { int validResultsCount; LTC[,] table = FitterForm.LoadTable(_tablesFileNames[i], out validResultsCount); if (validResultsCount != table.Length) { throw new Exception("Not all table results are valid!"); } tables[i] = table; if (i != 0 && (table.GetLength(0) != tables[0].GetLength(0) || table.GetLength(1) != tables[0].GetLength(1))) { throw new Exception("Table dimensions mismatch!"); } } // Create the Texture2DArray uint W = (uint)tables[0].GetLength(0); uint H = (uint)tables[0].GetLength(1); ImageUtility.ImagesMatrix M = new ImageUtility.ImagesMatrix(); M.InitTexture2DArray(W, H, (uint)tables.Length, 1); M.AllocateImageFiles(_format, new ImageUtility.ColorProfile(ImageUtility.ColorProfile.STANDARD_PROFILE.LINEAR)); for (int i = 0; i < tables.Length; i++) { LTC[,] table = tables[i]; // ImageUtility.ImageFile I = new ImageUtility.ImageFile( W, H, _format, profile ); // M[(uint) i][0][0] = I; double largest = 0; ImageUtility.ImageFile I = M[(uint)i][0][0]; I.WritePixels((uint _X, uint _Y, ref float4 _color) => { LTC ltc = table[_X, _Y]; const double tol = 1e-6; // if ( Mathf.Abs( ltc.invM[2,2] - 1 ) > tol ) // throw new Exception( "Not one!" ); if (Mathf.Abs(ltc.invM[0, 1]) > tol || Mathf.Abs(ltc.invM[1, 0]) > tol || Mathf.Abs(ltc.invM[1, 2]) > tol || Mathf.Abs(ltc.invM[2, 1]) > tol) { throw new Exception("Not zero!"); } largest = Math.Max(largest, Math.Abs(ltc.invM[2, 2] - 1)); double factor = 1.0 / ltc.invM[2, 2]; _color.x = (float)(factor * ltc.invM[0, 0]); _color.y = (float)(factor * ltc.invM[0, 2]); _color.z = (float)(factor * ltc.invM[1, 1]); _color.w = (float)(factor * ltc.invM[2, 0]); }); } M.DDSSaveFile(_targetFileName, ImageUtility.COMPONENT_FORMAT.AUTO); }
Texture2D LoadFalseColors(FileInfo _fileName) { using (ImageUtility.ImageFile sourceImage = new ImageUtility.ImageFile(_fileName, ImageUtility.ImageFile.FILE_FORMAT.PNG)) { ImageUtility.ImageFile convertedImage = new ImageUtility.ImageFile(); convertedImage.ConvertFrom(sourceImage, ImageUtility.PIXEL_FORMAT.BGRA8); using (ImageUtility.ImagesMatrix image = new ImageUtility.ImagesMatrix(convertedImage, ImageUtility.ImagesMatrix.IMAGE_TYPE.sRGB)) return(new Texture2D(m_device, image, ImageUtility.COMPONENT_FORMAT.UNORM_sRGB)); } }
void ConvertCrossToCubeMap(FileInfo _crossMapFile, FileInfo _cubeMapFile) { using (ImageUtility.ImageFile crossMap = new ImageUtility.ImageFile(_crossMapFile)) { using (ImageUtility.ImagesMatrix cubeMap = new ImageUtility.ImagesMatrix()) { cubeMap.ConvertCrossToCubeMap(crossMap, true); cubeMap.DDSSaveFile(_cubeMapFile, ImageUtility.COMPONENT_FORMAT.AUTO); // Optional: BC6H Compress //ImageUtility.ImagesMatrix compressedCubeMap = m_device.DDSCompress( cubeMap, ImageUtility.ImagesMatrix.COMPRESSION_TYPE.BC6H, ImageUtility.COMPONENT_FORMAT.AUTO ); //compressedCubeMap.DDSSaveFile( _cubeMapFile, ImageUtility.COMPONENT_FORMAT.AUTO ); } } }
protected override void OnLoad(EventArgs e) { base.OnLoad(e); try { m_device.Init(panelOutput.Handle, false, true); } catch (Exception _e) { m_device = null; MessageBox.Show("Failed to initialize DX device!\n\n" + _e.Message, "Heat Wave Test", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } m_CB_Main = new ConstantBuffer <CB_Main>(m_device, 0); m_shader_HeatDiffusion0 = new Shader(m_device, new FileInfo("./Shaders/HeatDiffusion.hlsl"), VERTEX_FORMAT.Pt4, "VS", null, "PS"); m_shader_HeatDiffusion1 = new Shader(m_device, new FileInfo("./Shaders/HeatDiffusion2.hlsl"), VERTEX_FORMAT.Pt4, "VS", null, "PS"); m_shader_RenderHeatMap = new Shader(m_device, new FileInfo("./Shaders/RenderHeatMap.hlsl"), VERTEX_FORMAT.Pt4, "VS", null, "PS"); m_shader_DrawObstacles = new Shader(m_device, new FileInfo("./Shaders/DrawObstacles.hlsl"), VERTEX_FORMAT.Pt4, "VS", null, "PS"); m_tex_HeatMap_Staging = new Texture2D(m_device, (uint)GRAPH_SIZE, (uint)GRAPH_SIZE, 1, 1, ImageUtility.PIXEL_FORMAT.RGBA32F, ImageUtility.COMPONENT_FORMAT.AUTO, true, false, null); m_tex_HeatMap0 = new Texture2D(m_device, (uint)GRAPH_SIZE, (uint)GRAPH_SIZE, 1, 1, ImageUtility.PIXEL_FORMAT.RGBA32F, ImageUtility.COMPONENT_FORMAT.AUTO, false, false, null); m_tex_HeatMap1 = new Texture2D(m_device, (uint)GRAPH_SIZE, (uint)GRAPH_SIZE, 1, 1, ImageUtility.PIXEL_FORMAT.RGBA32F, ImageUtility.COMPONENT_FORMAT.AUTO, false, false, null); m_tex_Obstacles0 = new Texture2D(m_device, (uint)GRAPH_SIZE + 2, (uint)GRAPH_SIZE + 2, 1, 1, ImageUtility.PIXEL_FORMAT.RGBA8, ImageUtility.COMPONENT_FORMAT.UNORM, false, false, null); m_tex_Obstacles1 = new Texture2D(m_device, (uint)GRAPH_SIZE + 2, (uint)GRAPH_SIZE + 2, 1, 1, ImageUtility.PIXEL_FORMAT.RGBA8, ImageUtility.COMPONENT_FORMAT.UNORM, false, false, null); m_tex_Obstacles_Staging = new Texture2D(m_device, (uint)GRAPH_SIZE + 2, (uint)GRAPH_SIZE + 2, 1, 1, ImageUtility.PIXEL_FORMAT.RGBA8, ImageUtility.COMPONENT_FORMAT.UNORM, true, false, null); ClearObstacles(); buttonResetObstacles_Click(null, EventArgs.Empty); m_tex_Search = new Texture2D(m_device, (uint)GRAPH_SIZE, (uint)GRAPH_SIZE, 1, 1, ImageUtility.PIXEL_FORMAT.RGBA8, ImageUtility.COMPONENT_FORMAT.UNORM, false, false, null); m_tex_Search_Staging = new Texture2D(m_device, (uint)GRAPH_SIZE, (uint)GRAPH_SIZE, 1, 1, ImageUtility.PIXEL_FORMAT.RGBA8, ImageUtility.COMPONENT_FORMAT.UNORM, true, false, null); // Load false colors using (ImageUtility.ImageFile sourceImage = new ImageUtility.ImageFile(new FileInfo("../../Data/Images/Gradients/Magma.png"), ImageUtility.ImageFile.FILE_FORMAT.PNG)) { ImageUtility.ImageFile convertedImage = new ImageUtility.ImageFile(); convertedImage.ConvertFrom(sourceImage, ImageUtility.PIXEL_FORMAT.BGRA8); using (ImageUtility.ImagesMatrix image = new ImageUtility.ImagesMatrix(convertedImage, ImageUtility.ImagesMatrix.IMAGE_TYPE.sRGB)) m_tex_FalseColors0 = new Texture2D(m_device, image, ImageUtility.COMPONENT_FORMAT.UNORM_sRGB); } using (ImageUtility.ImageFile sourceImage = new ImageUtility.ImageFile(new FileInfo("../../Data/Images/Gradients/Viridis.png"), ImageUtility.ImageFile.FILE_FORMAT.PNG)) { ImageUtility.ImageFile convertedImage = new ImageUtility.ImageFile(); convertedImage.ConvertFrom(sourceImage, ImageUtility.PIXEL_FORMAT.BGRA8); using (ImageUtility.ImagesMatrix image = new ImageUtility.ImagesMatrix(convertedImage, ImageUtility.ImagesMatrix.IMAGE_TYPE.sRGB)) m_tex_FalseColors1 = new Texture2D(m_device, image, ImageUtility.COMPONENT_FORMAT.UNORM_sRGB); } // BuildGraph(); Application.Idle += Application_Idle; }
protected override void OnLoad(EventArgs e) { base.OnLoad(e); try { m_device.Init(panelOutput.Handle, false, true); } catch (Exception _e) { m_device = null; MessageBox.Show("Failed to initialize DX device!\n\n" + _e.Message, "MSBRDF Test", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } try { // m_shader_Render = new Shader( m_device, new System.IO.FileInfo( "Shaders/Render.hlsl" ), VERTEX_FORMAT.Pt4, "VS", null, "PS", null ); m_shader_Render = new Shader(m_device, new System.IO.FileInfo("Shaders/RenderComplete.hlsl"), VERTEX_FORMAT.Pt4, "VS", null, "PS", null); } catch (Exception _e) { MessageBox.Show("Shader failed to compile!\n\n" + _e.Message, "MSBRDF Test", MessageBoxButtons.OK, MessageBoxIcon.Error); } uint W = (uint)panelOutput.Width; uint H = (uint)panelOutput.Height; m_CB_Global = new ConstantBuffer <CB_Global>(m_device, 0); m_CB_Camera = new ConstantBuffer <CB_Camera>(m_device, 1); m_CB_Render = new ConstantBuffer <CB_Render>(m_device, 2); BuildNoiseTextures(); BuildMSBRDF(new DirectoryInfo(@".\Tables\")); // Load cube map using (ImageUtility.ImagesMatrix I = new ImageUtility.ImagesMatrix()) { I.DDSLoadFile(new FileInfo("garage4_hd.dds")); m_tex_CubeMap = new Texture2D(m_device, I, ImageUtility.COMPONENT_FORMAT.AUTO); } // Setup camera m_camera.CreatePerspectiveCamera((float)(60.0 * Math.PI / 180.0), (float)panelOutput.Width / panelOutput.Height, 0.01f, 100.0f); m_manipulator.Attach(panelOutput, m_camera); m_manipulator.InitializeCamera(new float3(0, 1.5f, 2.0f), new float3(-0.4f, 0, 0.4f), float3.UnitY); // Start game time m_Ticks2Seconds = 1.0 / System.Diagnostics.Stopwatch.Frequency; m_StopWatch.Start(); m_StartGameTime = GetGameTime(); }
void BuildFont() { string charSet = " !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~éèêëôöàçÔÖÂÊÉœûüù"; m_fontRectangles = new Rectangle[charSet.Length]; for (int charIndex = 0; charIndex < charSet.Length; charIndex++) { char C = charSet[charIndex]; int index = (int)C; m_char2Index[index] = charIndex; } // Load atlas & rectangles using (ImageUtility.ImageFile file = new ImageUtility.ImageFile(new FileInfo("Atlas.png"))) { ImageUtility.ImageFile file2 = new ImageUtility.ImageFile(file, ImageUtility.PIXEL_FORMAT.RGBA8); using (ImageUtility.ImagesMatrix M = new ImageUtility.ImagesMatrix(file2, ImageUtility.ImagesMatrix.IMAGE_TYPE.sRGB)) { m_tex_FontAtlas = new Texture2D(m_device, M, ImageUtility.COMPONENT_FORMAT.UNORM_sRGB); } } using (FileStream S = new FileInfo("Atlas.rect").OpenRead()) using (BinaryReader R = new BinaryReader(S)) { // Read both CPU and GPU versions float recW = 1.0f / m_tex_FontAtlas.Width; float recH = 1.0f / m_tex_FontAtlas.Height; using (PixelsBuffer content = new PixelsBuffer((uint)(16 * m_fontRectangles.Length))) { using (BinaryWriter W = content.OpenStreamWrite()) { for (int i = 0; i < m_fontRectangles.Length; i++) { m_fontRectangles[i].X = R.ReadInt32(); m_fontRectangles[i].Y = R.ReadInt32(); m_fontRectangles[i].Width = R.ReadInt32(); m_fontRectangles[i].Height = R.ReadInt32(); W.Write(recW * (float)m_fontRectangles[i].X); W.Write(recH * (float)m_fontRectangles[i].Y); W.Write(recW * (float)m_fontRectangles[i].Width); W.Write(recH * (float)m_fontRectangles[i].Height); } } m_tex_FontRectangle = new Texture2D(m_device, (uint)m_fontRectangles.Length, 1, 1, 1, ImageUtility.PIXEL_FORMAT.RGBA32F, ImageUtility.COMPONENT_FORMAT.AUTO, false, false, new PixelsBuffer[] { content }); } } }
void BuildNoiseTextures() { PixelsBuffer Content = new PixelsBuffer(NOISE_SIZE * NOISE_SIZE * NOISE_SIZE * 4); PixelsBuffer Content4D = new PixelsBuffer(NOISE_SIZE * NOISE_SIZE * NOISE_SIZE * 16); SimpleRNG.SetSeed(521288629, 362436069); float4 V = float4.Zero; using (BinaryWriter W = Content.OpenStreamWrite()) { using (BinaryWriter W2 = Content4D.OpenStreamWrite()) { for (int Z = 0; Z < NOISE_SIZE; Z++) { for (int Y = 0; Y < NOISE_SIZE; Y++) { for (int X = 0; X < NOISE_SIZE; X++) { V.Set((float)SimpleRNG.GetUniform(), (float)SimpleRNG.GetUniform(), (float)SimpleRNG.GetUniform(), (float)SimpleRNG.GetUniform()); W.Write(V.x); W2.Write(V.x); W2.Write(V.y); W2.Write(V.z); W2.Write(V.w); } } } } } m_tex_Noise = new Texture3D(m_device, NOISE_SIZE, NOISE_SIZE, NOISE_SIZE, 1, ImageUtility.PIXEL_FORMAT.R8, ImageUtility.COMPONENT_FORMAT.UNORM, false, false, new PixelsBuffer[] { Content }); m_tex_Noise4D = new Texture3D(m_device, NOISE_SIZE, NOISE_SIZE, NOISE_SIZE, 1, ImageUtility.PIXEL_FORMAT.RGBA8, ImageUtility.COMPONENT_FORMAT.UNORM, false, false, new PixelsBuffer[] { Content4D }); // Load blue noise using (ImageUtility.ImageFile I = new ImageUtility.ImageFile(new FileInfo("BlueNoise64x64_16bits.png"))) { ImageUtility.ImagesMatrix M = new ImageUtility.ImagesMatrix(new ImageUtility.ImageFile[, ] { { I } }); m_tex_BlueNoise = new Texture2D(m_device, M, ImageUtility.COMPONENT_FORMAT.UNORM); } }
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)); }
void BuildFont() { string charSet = " !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~éèêëôöàçÔÖÂÊÉœûüù"; m_fontRectangles = new Rectangle[charSet.Length]; for (int charIndex = 0; charIndex < charSet.Length; charIndex++) { char C = charSet[charIndex]; int index = (int)C; m_char2Index[index] = charIndex; } #if BUILD_FONTS using (Font F = new Font(this.Font.FontFamily, 36.0f)) { // Build small bitmap and write each character int width = 0; int maxheight = 0; using (Bitmap B = new Bitmap(70, 70, System.Drawing.Imaging.PixelFormat.Format32bppArgb)) { using (Graphics G = Graphics.FromImage(B)) { for (int i = 0; i < charSet.Length; i++) { string s = charSet.Substring(i, 1); // G.FillRectangle( Brushes.Black, 0, 0, B.Width, B.Height ); // G.DrawString( s, F, Brushes.White, new PointF( 0, 0 ) ); SizeF tempSize = G.MeasureString(s, F); m_fontRectangles[i] = new Rectangle(width, 0, (int)Mathf.Ceiling(tempSize.Width), (int)Mathf.Ceiling(tempSize.Height)); if (m_fontRectangles[i].Width > B.Width || m_fontRectangles[i].Height > B.Height) { throw new Exception("Fonts are too big, expand bitmap size or reduce font size!"); } width += m_fontRectangles[i].Width; maxheight = Math.Max(maxheight, m_fontRectangles[i].Height); } } } // Build the final bitmap using (Bitmap B = new Bitmap(width, maxheight, System.Drawing.Imaging.PixelFormat.Format32bppArgb)) { using (Graphics G = Graphics.FromImage(B)) { G.FillRectangle(Brushes.Black, 0, 0, B.Width, B.Height); for (int i = 0; i < charSet.Length; i++) { string s = charSet.Substring(i, 1); G.DrawString(s, F, Brushes.White, new PointF(m_fontRectangles[i].X, m_fontRectangles[i].Y)); } using (ImageUtility.ImageFile file = new ImageUtility.ImageFile(B, new ImageUtility.ColorProfile(ImageUtility.ColorProfile.STANDARD_PROFILE.sRGB))) { file.Save(new FileInfo("Atlas.png"), ImageUtility.ImageFile.FILE_FORMAT.PNG); } } } } // Write char sizes using (FileStream S = new FileInfo("Atlas.rect").Create()) using (BinaryWriter W = new BinaryWriter(S)) { for (int i = 0; i < m_fontRectangles.Length; i++) { W.Write(m_fontRectangles[i].X); W.Write(m_fontRectangles[i].Y); W.Write(m_fontRectangles[i].Width); W.Write(m_fontRectangles[i].Height); } } #endif // Load atlas & rectangles using (ImageUtility.ImageFile file = new ImageUtility.ImageFile(new FileInfo("Atlas.png"))) { ImageUtility.ImageFile file2 = new ImageUtility.ImageFile(file, ImageUtility.PIXEL_FORMAT.RGBA8); using (ImageUtility.ImagesMatrix M = new ImageUtility.ImagesMatrix(file2, ImageUtility.ImagesMatrix.IMAGE_TYPE.sRGB)) { m_tex_FontAtlas = new Texture2D(m_device, M, ImageUtility.COMPONENT_FORMAT.UNORM_sRGB); } } using (FileStream S = new FileInfo("Atlas.rect").OpenRead()) using (BinaryReader R = new BinaryReader(S)) { // Read both CPU and GPU versions float recW = 1.0f / m_tex_FontAtlas.Width; float recH = 1.0f / m_tex_FontAtlas.Height; using (PixelsBuffer content = new PixelsBuffer((uint)(16 * m_fontRectangles.Length))) { using (BinaryWriter W = content.OpenStreamWrite()) { for (int i = 0; i < m_fontRectangles.Length; i++) { m_fontRectangles[i].X = R.ReadInt32(); m_fontRectangles[i].Y = R.ReadInt32(); m_fontRectangles[i].Width = R.ReadInt32(); m_fontRectangles[i].Height = R.ReadInt32(); W.Write(recW * (float)m_fontRectangles[i].X); W.Write(recH * (float)m_fontRectangles[i].Y); W.Write(recW * (float)m_fontRectangles[i].Width); W.Write(recH * (float)m_fontRectangles[i].Height); } } m_tex_FontRectangle = new Texture2D(m_device, (uint)m_fontRectangles.Length, 1, 1, 1, ImageUtility.PIXEL_FORMAT.RGBA32F, ImageUtility.COMPONENT_FORMAT.AUTO, false, false, new PixelsBuffer[] { content }); } } }
protected override void OnLoad(EventArgs e) { base.OnLoad(e); try { m_device.Init(panelOutput.Handle, false, true); } catch (Exception _e) { m_device = null; MessageBox.Show("Failed to initialize DX device!\n\n" + _e.Message, "Heat Wave Test", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } try { ////////////////////////////////////////////////////////////////////////// // Load the graph we need to simulate m_graph = new ProtoParser.Graph(); // FileInfo file = new FileInfo( "../../../AI/Projects/Semantic Memory/Tests/Birds Database/Tools/ProtoParser/Concepts.graph" ); FileInfo file = new FileInfo("../../../AI/Projects/Semantic Memory/Tests/Birds Database/Tools/ProtoParser/TestGraph.graph"); if (!file.Exists) { file = new FileInfo("./Graphs/TestGraph.graph"); } using (FileStream S = file.OpenRead()) using (BinaryReader R = new BinaryReader(S)) m_graph.Read(R); ProtoParser.Neuron[] neurons = m_graph.Neurons; m_nodesCount = (uint)neurons.Length; /* * m_nodesCount = 2; * neurons = new ProtoParser.Neuron[2]; * neurons[0] = new ProtoParser.Neuron(); * neurons[1] = new ProtoParser.Neuron(); * neurons[0].LinkChild( neurons[1] ); * //*/ ////////////////////////////////////////////////////////////////////////// m_CB_Main = new ConstantBuffer <CB_Main>(m_device, 0); m_CB_Simulation = new ConstantBuffer <CB_Simulation>(m_device, 1); m_CB_Text = new ConstantBuffer <CB_Text>(m_device, 2); m_shader_ComputeForces = new ComputeShader(m_device, new FileInfo("./Shaders/SimulateGraph.hlsl"), "CS"); m_shader_Simulate = new ComputeShader(m_device, new FileInfo("./Shaders/SimulateGraph.hlsl"), "CS2"); #if RENDER_GRAPH_PROPER m_shader_RenderGraphNode = new Shader(m_device, new FileInfo("./Shaders/RenderGraph2.hlsl"), VERTEX_FORMAT.Pt4, "VS", null, "PS"); m_shader_RenderGraphLink = new Shader(m_device, new FileInfo("./Shaders/RenderGraph2.hlsl"), VERTEX_FORMAT.Pt4, "VS2", null, "PS2"); #else m_shader_RenderGraph = new Shader(m_device, new FileInfo("./Shaders/RenderGraph.hlsl"), VERTEX_FORMAT.Pt4, "VS", null, "PS"); #endif m_shader_RenderText = new Shader(m_device, new FileInfo("./Shaders/RenderText.hlsl"), VERTEX_FORMAT.Pt4, "VS", null, "PS"); // Build node info m_SB_Nodes = new StructuredBuffer <SB_NodeInfo>(m_device, m_nodesCount, true); m_neuron2ID = new Dictionary <ProtoParser.Neuron, uint>(neurons.Length); float maxMass = 0.0f; for (int neuronIndex = 0; neuronIndex < m_nodesCount; neuronIndex++) { ProtoParser.Neuron N = neurons[neuronIndex]; m_neuron2ID[N] = (uint)neuronIndex; uint linksCount = (uint)(N.ParentsCount + N.ChildrenCount + N.FeaturesCount); // m_SB_Nodes.m[neuronIndex].m_mass = (1 + 10.0f * linksCount) / (0.1f + N.Distance2Root); // Works with S=1e4 D=-1e3 // m_SB_Nodes.m[neuronIndex].m_mass = (1 + 1.0f * linksCount) / (0.01f + 0.0f * N.Distance2Root); // Works with S=1e4 D=-1e3 // m_SB_Nodes.m[neuronIndex].m_mass = (1 + 0.1f * linksCount) / (0.1f + N.Distance2Root); // Works with S=10 D=-10 m_SB_Nodes.m[neuronIndex].m_mass = 100.0f * (1 + 1.0f * linksCount); // Works with S=1e4 D=-1e3 m_SB_Nodes.m[neuronIndex].m_linkOffset = m_totalLinksCount; m_SB_Nodes.m[neuronIndex].m_linksCount = linksCount; m_SB_Nodes.m[neuronIndex].m_flags = 0U; maxMass = Mathf.Max(maxMass, m_SB_Nodes.m[neuronIndex].m_mass); m_totalLinksCount += linksCount; } m_SB_Nodes.m[0].m_mass = 1e4f; m_SB_Nodes.Write(); // Build node links m_SB_Links = new StructuredBuffer <uint>(m_device, m_totalLinksCount, true); m_SB_LinkSources = new StructuredBuffer <uint>(m_device, m_totalLinksCount, true); m_totalLinksCount = 0; for (int neuronIndex = 0; neuronIndex < m_nodesCount; neuronIndex++) { ProtoParser.Neuron N = neurons[neuronIndex]; foreach (ProtoParser.Neuron O in N.Parents) { m_SB_LinkSources.m[m_totalLinksCount] = (uint)neuronIndex; m_SB_Links.m[m_totalLinksCount++] = m_neuron2ID[O]; } foreach (ProtoParser.Neuron O in N.Children) { m_SB_LinkSources.m[m_totalLinksCount] = (uint)neuronIndex; m_SB_Links.m[m_totalLinksCount++] = m_neuron2ID[O]; } foreach (ProtoParser.Neuron O in N.Features) { m_SB_LinkSources.m[m_totalLinksCount] = (uint)neuronIndex; m_SB_Links.m[m_totalLinksCount++] = m_neuron2ID[O]; } } m_SB_Links.Write(); m_SB_LinkSources.Write(); // Setup initial CB m_CB_Main.m._nodesCount = m_nodesCount; m_CB_Main.m._resX = (uint)panelOutput.Width; m_CB_Main.m._resY = (uint)panelOutput.Height; m_CB_Main.m._maxMass = maxMass; m_CB_Main.m._cameraCenter = float2.Zero; m_CB_Main.m._cameraSize.Set(10.0f, 10.0f); m_CB_Main.m._hoveredNodeIndex = ~0U; m_CB_Main.UpdateData(); // Initialize sim buffers m_SB_Forces = new StructuredBuffer <float2>(m_device, m_nodesCount * m_nodesCount, false); m_SB_NodeSims[0] = new StructuredBuffer <SB_NodeSim>(m_device, m_nodesCount, true); m_SB_NodeSims[1] = new StructuredBuffer <SB_NodeSim>(m_device, m_nodesCount, true); buttonReset_Click(null, EventArgs.Empty); // m_shader_HeatDiffusion = new Shader( m_device, new FileInfo( "./Shaders/HeatDiffusion.hlsl" ), VERTEX_FORMAT.Pt4, "VS", null, "PS", null ); // m_tex_Search = new Texture2D( m_device, (uint) GRAPH_SIZE, (uint) GRAPH_SIZE, 1, 1, ImageUtility.PIXEL_FORMAT.RGBA8, ImageUtility.COMPONENT_FORMAT.UNORM, false, false, null ); // m_tex_Search_Staging = new Texture2D( m_device, (uint) GRAPH_SIZE, (uint) GRAPH_SIZE, 1, 1, ImageUtility.PIXEL_FORMAT.RGBA8, ImageUtility.COMPONENT_FORMAT.UNORM, true, false, null ); // Load false colors // using ( ImageUtility.ImageFile sourceImage = new ImageUtility.ImageFile( new FileInfo( "../../Images/Gradients/Viridis.png" ), ImageUtility.ImageFile.FILE_FORMAT.PNG ) ) { using (ImageUtility.ImageFile sourceImage = new ImageUtility.ImageFile(new FileInfo("../../Images/Gradients/Magma.png"), ImageUtility.ImageFile.FILE_FORMAT.PNG)) { ImageUtility.ImageFile convertedImage = new ImageUtility.ImageFile(); convertedImage.ConvertFrom(sourceImage, ImageUtility.PIXEL_FORMAT.BGRA8); using (ImageUtility.ImagesMatrix image = new ImageUtility.ImagesMatrix(convertedImage, ImageUtility.ImagesMatrix.IMAGE_TYPE.sRGB)) m_tex_FalseColors = new Texture2D(m_device, image, ImageUtility.COMPONENT_FORMAT.UNORM_sRGB); } // Prepare font atlas m_SB_Text = new StructuredBuffer <SB_Letter>(m_device, 1024U, true); BuildFont(); Application.Idle += Application_Idle; } catch (Exception _e) { m_device = null; MessageBox.Show("Failed to initialize shaders!\n\n" + _e.Message, "Heat Wave Test", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } }
protected override void OnLoad(EventArgs e) { base.OnLoad(e); try { m_device.Init(panelOutput.Handle, false, true); } catch (Exception _e) { m_device = null; MessageBox.Show("Failed to initialize DX device!\n\n" + _e.Message, "MSBRDF Test", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } try { // m_shader_Render = new Shader( m_device, new System.IO.FileInfo( "Shaders/Render.hlsl" ), VERTEX_FORMAT.Pt4, "VS", null, "PS", null ); // OBSOLETE MSBRDF CODE! For historical purpose only... m_shader_Finalize = new Shader(m_device, new System.IO.FileInfo("Shaders/RenderComplete.hlsl"), VERTEX_FORMAT.Pt4, "VS", null, "PS_Finalize", null); #if TEST_SH_ENVIRONMENT m_shader_Accumulate = new Shader(m_device, new System.IO.FileInfo("Shaders/RenderCompareSH.hlsl"), VERTEX_FORMAT.Pt4, "VS", null, "PS", null); // Use this to show a rendering with SH environment checkBoxUseRealTimeApprox.Visible = true; checkBoxUseRealTimeApprox.Checked = true; floatTrackbarControlRoughnessSphere.Value = 1; // Show full roughness floatTrackbarControlReflectanceSphere2.Value = 0; // Disturbing if diffuse is showing! groupBoxPlane.Visible = false; // No plane is available in this configuration #elif TEST_LTC_AREA_LIGHT m_shader_Accumulate = new Shader(m_device, new System.IO.FileInfo("Shaders/RenderCompareLTC.hlsl"), VERTEX_FORMAT.Pt4, "VS", null, "PS", null); // Use this to show a rendering with LTC area light checkBoxUseRealTimeApprox.Visible = true; // checkBoxUseRealTimeApprox.Checked = true; checkBoxUseRealTimeApprox.Checked = false; // floatTrackbarControlRoughnessSphere.Value = 1; // Show full roughness // floatTrackbarControlReflectanceSphere2.Value = 0; // Disturbing if diffuse is showing! checkBoxUseLTC.Visible = true; floatTrackbarControlRoughnessSphere.Value = 0.25f; floatTrackbarControlReflectanceSphere.Value = 0.04f; floatTrackbarControlRoughnessSphere2.Value = 0.80f; floatTrackbarControlReflectanceSphere2.Value = 0.5f; floatTrackbarControlRoughnessGround.Value = 0.85f; floatTrackbarControlReflectanceGround.Value = 0.35f; floatTrackbarControlLightElevation.Value = 0.5f; #else m_shader_Accumulate = new Shader(m_device, new System.IO.FileInfo("Shaders/RenderComplete.hlsl"), VERTEX_FORMAT.Pt4, "VS", null, "PS", null); // Use this for a full render #endif } catch (Exception _e) { MessageBox.Show("Shader failed to compile!\n\n" + _e.Message, "MSBRDF Test", MessageBoxButtons.OK, MessageBoxIcon.Error); } uint W = (uint)panelOutput.Width; uint H = (uint)panelOutput.Height; m_CB_Global = new ConstantBuffer <CB_Global>(m_device, 0); m_CB_Camera = new ConstantBuffer <CB_Camera>(m_device, 1); m_CB_Render = new ConstantBuffer <CB_Render>(m_device, 2); m_CB_SH = new ConstantBuffer <CB_SH>(m_device, 3); BuildNoiseTextures(); // Shuffle group indices for (uint groupIndex = 0; groupIndex < GROUPS_COUNT; groupIndex++) { m_groupShuffle[groupIndex] = groupIndex; } for (uint shuffleIndex = 100 * GROUPS_COUNT; shuffleIndex > 0; shuffleIndex--) { for (uint groupIndex = 0; groupIndex < GROUPS_COUNT; groupIndex++) { uint i0 = SimpleRNG.GetUint() % GROUPS_COUNT; uint i1 = SimpleRNG.GetUint() % GROUPS_COUNT; uint temp = m_groupShuffle[i0]; m_groupShuffle[i0] = m_groupShuffle[i1]; m_groupShuffle[i1] = temp; } } // Tables are "built" with Mathematica now // BuildMSBRDF( new DirectoryInfo( @".\Tables\" ) ); LoadMSBRDF(128, new FileInfo("./Tables/MSBRDF_GGX_G2_E128x128.float"), new FileInfo("./Tables/MSBRDF_GGX_G2_Eavg128.float"), out m_tex_MSBRDF_GGX_E, out m_tex_MSBRDF_GGX_Eavg); LoadMSBRDF(32, new FileInfo("./Tables/MSBRDF_OrenNayar_E32x32.float"), new FileInfo("./Tables/MSBRDF_OrenNayar_Eavg32.float"), out m_tex_MSBRDF_OrenNayar_E, out m_tex_MSBRDF_OrenNayar_Eavg); #if TEST_LTC_AREA_LIGHT // Area light m_tex_LTC = LoadLTC(new FileInfo(@".\Tables\LTC.dds")); m_tex_LTC_Unity = LoadUnityLTC(); #endif // Load cube map using (ImageUtility.ImagesMatrix I = new ImageUtility.ImagesMatrix()) { // I.DDSLoadFile( new FileInfo( "garage4_hd.dds" ) ); I.DDSLoadFile(new FileInfo("beach.dds")); EncodeCubeMapIntoSH(I); m_tex_CubeMap = new Texture2D(m_device, I, ImageUtility.COMPONENT_FORMAT.AUTO); } m_tex_Accumulator = new Texture2D(m_device, m_device.DefaultTarget.Width, m_device.DefaultTarget.Height, 1, 1, ImageUtility.PIXEL_FORMAT.RGBA32F, ImageUtility.COMPONENT_FORMAT.AUTO, false, true, null); // Setup camera m_camera.CreatePerspectiveCamera((float)(60.0 * Math.PI / 180.0), (float)panelOutput.Width / panelOutput.Height, 0.01f, 100.0f); m_manipulator.Attach(panelOutput, m_camera); // m_manipulator.InitializeCamera( new float3( 0, 1.5f, 2.0f ), new float3( -0.4f, 0, 0.4f ), float3.UnitY ); // Garage probe m_manipulator.InitializeCamera(new float3(1.46070266f, 1.10467184f, 1.36212754f), new float3(0, 1, 0), float3.UnitY); // Beach probe m_camera.CameraTransformChanged += Camera_CameraTransformChanged; Camera_CameraTransformChanged(null, EventArgs.Empty); // Start game time m_Ticks2Seconds = 1.0 / System.Diagnostics.Stopwatch.Frequency; m_StopWatch.Start(); m_StartGameTime = GetGameTime(); }
void LoadMSBRDF(uint[] _sizes, FileInfo[] _irradianceTablesNames, FileInfo[] _albedoTablesNames, out Texture2D _irradianceTexture, out Texture2D _albedoTexture) { uint BRDFSCount = (uint)_sizes.Length; if (_irradianceTablesNames.Length != BRDFSCount || _albedoTablesNames.Length != BRDFSCount) { throw new Exception("Irradiance and albedo textures count must match the amount of BRDFs computed from the size of the _sizes array!"); } // Determine max texture size uint textureSize = 0; foreach (uint size in _sizes) { textureSize = Math.Max(textureSize, size); } // Create placeholders ImageUtility.ImagesMatrix texE = new ImageUtility.ImagesMatrix(); texE.InitTexture2DArray(textureSize, textureSize, BRDFSCount, 1); texE.AllocateImageFiles(ImageUtility.PIXEL_FORMAT.R32F, new ImageUtility.ColorProfile(ImageUtility.ColorProfile.STANDARD_PROFILE.LINEAR)); ImageUtility.ImagesMatrix texEavg = new ImageUtility.ImagesMatrix(); texEavg.InitTexture2DArray(textureSize, BRDFSCount, 1, 1); texEavg.AllocateImageFiles(ImageUtility.PIXEL_FORMAT.R32F, new ImageUtility.ColorProfile(ImageUtility.ColorProfile.STANDARD_PROFILE.LINEAR)); float[][] albedoTables = new float[BRDFSCount][]; for (uint BRDFIndex = 0; BRDFIndex < BRDFSCount; BRDFIndex++) { uint size = _sizes[BRDFIndex]; // Read irradiance table float[,] irradianceTable = new float[size, size]; using (FileStream S = _irradianceTablesNames[BRDFIndex].OpenRead()) using (BinaryReader R = new BinaryReader(S)) { for (int Y = 0; Y < size; Y++) { for (int X = 0; X < size; X++) { irradianceTable[X, Y] = R.ReadSingle(); } } } // Write content if (size == textureSize) { // One-one correspondance texE[BRDFIndex][0][0].WritePixels((uint _X, uint _Y, ref float4 _color) => { _color.x = irradianceTable[_X, _Y]; }); } else { // Needs scaling texE[BRDFIndex][0][0].WritePixels((uint _X, uint _Y, ref float4 _color) => { _color.x = Mathf.BiLerp(irradianceTable, (float)_X / textureSize, (float)_Y / textureSize); }); } // Read albedo table float[] albedoTable = new float[size]; albedoTables[BRDFIndex] = albedoTable; using (FileStream S = _albedoTablesNames[BRDFIndex].OpenRead()) using (BinaryReader R = new BinaryReader(S)) { for (int Y = 0; Y < size; Y++) { albedoTable[Y] = R.ReadSingle(); } } } // Build the entire albedo tables texEavg[0][0][0].WritePixels((uint _X, uint _Y, ref float4 _color) => { _color.x = Mathf.Lerp(albedoTables[_Y], (float)_X / textureSize); }); // Create textures _irradianceTexture = new Texture2D(m_device, texE, ImageUtility.COMPONENT_FORMAT.AUTO); _albedoTexture = new Texture2D(m_device, texEavg, ImageUtility.COMPONENT_FORMAT.AUTO); }