Ejemplo n.º 1
0
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            try {
                // Initialize the device
                m_device = new Device();
                m_device.Init(graphPanel.Handle, false, true);

                // Create the render shaders
                try {
                    Shader.WarningAsError = false;

                    m_shader_RenderSphere = new Shader(m_device, new System.IO.FileInfo(@"./Shaders/RenderSphere.hlsl"), VERTEX_FORMAT.Pt4, "VS", null, "PS");
                    m_shader_RenderScene  = new Shader(m_device, new System.IO.FileInfo(@"./Shaders/RenderScene.hlsl"), VERTEX_FORMAT.Pt4, "VS", null, "PS");
                    m_shader_RenderLDR    = new Shader(m_device, new System.IO.FileInfo(@"./Shaders/RenderLDR.hlsl"), VERTEX_FORMAT.Pt4, "VS", null, "PS");
                } catch (Exception _e) {
                    throw new Exception("Failed to compile shader! " + _e.Message);
                }

                // Create CB
                m_CB_Render = new ConstantBuffer <CB_Main>(m_device, 0);

                // Create textures
                LoadHDRImage();

                m_Tex_HDRBuffer = new Texture2D(m_device, (uint)graphPanel.Width, (uint)graphPanel.Height, 2, 1, ImageUtility.PIXEL_FORMAT.RGBA32F, ImageUtility.COMPONENT_FORMAT.AUTO, false, false, null);

                {                       // Build noise texture
                    SimpleRNG.SetSeed(1U);
                    PixelsBuffer content = new PixelsBuffer(256 * 256 * 16);
                    using (System.IO.BinaryWriter W = content.OpenStreamWrite())
                        for (int i = 0; i < 256 * 256; i++)
                        {
                            W.Write((float)SimpleRNG.GetUniform());
                            W.Write((float)SimpleRNG.GetUniform());
                            W.Write((float)SimpleRNG.GetUniform());
                            W.Write((float)SimpleRNG.GetUniform());
                        }
                    m_Tex_Noise = new Texture2D(m_device, 256, 256, 1, 1, ImageUtility.PIXEL_FORMAT.RGBA32F, ImageUtility.COMPONENT_FORMAT.AUTO, false, false, new PixelsBuffer[] { content });
                }

                // Build SH coeffs
                const int ORDERS = 20;
                {
                    const int TABLE_SIZE = 64;

                    // Load A coeffs into a texture array
                    float[,,]       A = new float[TABLE_SIZE, TABLE_SIZE, ORDERS];
//					using ( System.IO.FileStream S = new System.IO.FileInfo( @"ConeTable_cosAO_order20.float" ).OpenRead() )
                    using (System.IO.FileStream S = new System.IO.FileInfo(@"ConeTable_cosTheta_order20.float").OpenRead())
                        using (System.IO.BinaryReader R = new System.IO.BinaryReader(S)) {
                            for (int thetaIndex = 0; thetaIndex < TABLE_SIZE; thetaIndex++)
                            {
                                for (int AOIndex = 0; AOIndex < TABLE_SIZE; AOIndex++)
                                {
                                    for (int order = 0; order < ORDERS; order++)
                                    {
                                        A[thetaIndex, AOIndex, order] = R.ReadSingle();
                                    }
                                }
                            }
                        }

                    PixelsBuffer[] coeffSlices = new PixelsBuffer[5];                           // 5 slices of 4 coeffs each to get our 20 orders
                    for (int sliceIndex = 0; sliceIndex < coeffSlices.Length; sliceIndex++)
                    {
                        PixelsBuffer coeffSlice = new PixelsBuffer(TABLE_SIZE * TABLE_SIZE * 16);
                        coeffSlices[sliceIndex] = coeffSlice;

                        using (System.IO.BinaryWriter W = coeffSlice.OpenStreamWrite()) {
                            for (int thetaIndex = 0; thetaIndex < TABLE_SIZE; thetaIndex++)
                            {
                                for (int AOIndex = 0; AOIndex < TABLE_SIZE; AOIndex++)
                                {
                                    W.Write(A[thetaIndex, AOIndex, 4 * sliceIndex + 0]);
                                    W.Write(A[thetaIndex, AOIndex, 4 * sliceIndex + 1]);
                                    W.Write(A[thetaIndex, AOIndex, 4 * sliceIndex + 2]);
                                    W.Write(A[thetaIndex, AOIndex, 4 * sliceIndex + 3]);
                                }
                            }
                        }
                    }

                    m_Tex_ACoeffs = new Texture2D(m_device, 64, 64, coeffSlices.Length, 1, ImageUtility.PIXEL_FORMAT.RGBA32F, ImageUtility.COMPONENT_FORMAT.AUTO, false, false, coeffSlices);
                }

                {
                    // Load environment coeffs into a constant buffer
                    float3[] coeffs = new float3[ORDERS * ORDERS];
                    using (System.IO.FileStream S = new System.IO.FileInfo(@"Ennis_order20.float3").OpenRead())
                        using (System.IO.BinaryReader R = new System.IO.BinaryReader(S))
                            for (int coeffIndex = 0; coeffIndex < ORDERS * ORDERS; coeffIndex++)
                            {
                                coeffs[coeffIndex].Set(R.ReadSingle(), R.ReadSingle(), R.ReadSingle());
                            }

                    // Write into a raw byte[]
                    byte[] rawContent = new byte[400 * 4 * 4];
                    using (System.IO.MemoryStream MS = new System.IO.MemoryStream(rawContent))
                        using (System.IO.BinaryWriter W = new System.IO.BinaryWriter(MS)) {
                            for (int coeffIndex = 0; coeffIndex < ORDERS * ORDERS; coeffIndex++)
                            {
                                W.Write(coeffs[coeffIndex].x);
                                W.Write(coeffs[coeffIndex].y);
                                W.Write(coeffs[coeffIndex].z);
                                W.Write(0.0f);
                            }
                        }
                    m_CB_Coeffs = new RawConstantBuffer(m_device, 1, rawContent.Length);
                    m_CB_Coeffs.UpdateData(rawContent);
                }

                // Create camera + manipulator
                m_camera.CreatePerspectiveCamera(0.5f * (float)Math.PI, (float)graphPanel.Width / graphPanel.Height, 0.01f, 100.0f);
                m_camera.CameraTransformChanged += m_camera_CameraTransformChanged;
                m_cameraManipulator.Attach(graphPanel, m_camera);
                m_cameraManipulator.InitializeCamera(-2.0f * float3.UnitZ, float3.Zero, float3.UnitY);
                m_camera_CameraTransformChanged(null, EventArgs.Empty);

                // Start rendering
                Application.Idle += Application_Idle;
            } catch (Exception _e) {
                MessageBox.Show("Failed to initialize D3D renderer!\r\nReason: " + _e.Message);
            }
        }
Ejemplo n.º 2
0
        protected override void OnLoad( EventArgs e )
        {
            base.OnLoad( e );

            try {
                // Initialize the device
                m_device = new Device();
                m_device.Init( graphPanel.Handle, false, true );

                // Create the render shaders
                try {
                    Shader.WarningAsError = false;

                    m_shader_RenderSphere = new Shader( m_device, new System.IO.FileInfo( @"./Shaders/RenderSphere.hlsl" ), VERTEX_FORMAT.Pt4, "VS", null, "PS", null );
                    m_shader_RenderScene = new Shader( m_device, new System.IO.FileInfo( @"./Shaders/RenderScene.hlsl" ), VERTEX_FORMAT.Pt4, "VS", null, "PS", null );
                    m_shader_RenderLDR = new Shader( m_device, new System.IO.FileInfo( @"./Shaders/RenderLDR.hlsl" ), VERTEX_FORMAT.Pt4, "VS", null, "PS", null );
                } catch ( Exception _e ) {
                    throw new Exception( "Failed to compile shader! " + _e.Message );
                }

                // Create CB
                m_CB_Render = new ConstantBuffer< CB_Main >( m_device, 0 );

                // Create textures
                LoadHDRImage();

                m_Tex_HDRBuffer = new Texture2D( m_device, (uint) graphPanel.Width, (uint) graphPanel.Height, 2, 1, PIXEL_FORMAT.RGBA32_FLOAT, false, false, null );

                {	// Build noise texture
                    SimpleRNG.SetSeed( 1U );
                    PixelsBuffer	content = new PixelsBuffer( 256*256*16 );
                    using ( System.IO.BinaryWriter W = content.OpenStreamWrite() )
                        for ( int i=0; i < 256*256; i++ ) {
                            W.Write( (float) SimpleRNG.GetUniform() );
                            W.Write( (float) SimpleRNG.GetUniform() );
                            W.Write( (float) SimpleRNG.GetUniform() );
                            W.Write( (float) SimpleRNG.GetUniform() );
                        }
                    m_Tex_Noise = new Texture2D( m_device, 256, 256, 1, 1, PIXEL_FORMAT.RGBA32_FLOAT, false, false, new PixelsBuffer[] { content } );
                }

                // Build SH coeffs
                const int	ORDERS = 20;
                {
                    const int	TABLE_SIZE = 64;

                    // Load A coeffs into a texture array
                    float[,,]	A = new float[TABLE_SIZE,TABLE_SIZE,ORDERS];
            //					using ( System.IO.FileStream S = new System.IO.FileInfo( @"ConeTable_cosAO_order20.float" ).OpenRead() )
                    using ( System.IO.FileStream S = new System.IO.FileInfo( @"ConeTable_cosTheta_order20.float" ).OpenRead() )
                        using ( System.IO.BinaryReader R = new System.IO.BinaryReader( S ) ) {
                            for ( int thetaIndex=0; thetaIndex < TABLE_SIZE; thetaIndex++ )
                                for ( int AOIndex=0; AOIndex < TABLE_SIZE; AOIndex++ ) {
                                    for ( int order=0; order < ORDERS; order++ )
                                        A[thetaIndex,AOIndex,order] = R.ReadSingle();
                                }
                        }

                    PixelsBuffer[]	coeffSlices = new PixelsBuffer[5];	// 5 slices of 4 coeffs each to get our 20 orders
                    for ( int sliceIndex=0; sliceIndex < coeffSlices.Length; sliceIndex++ ) {
                        PixelsBuffer	coeffSlice = new PixelsBuffer( TABLE_SIZE*TABLE_SIZE*16 );
                        coeffSlices[sliceIndex] = coeffSlice;

                        using ( System.IO.BinaryWriter W = coeffSlice.OpenStreamWrite() ) {
                            for ( int thetaIndex=0; thetaIndex < TABLE_SIZE; thetaIndex++ )
                                for ( int AOIndex=0; AOIndex < TABLE_SIZE; AOIndex++ ) {
                                    W.Write( A[thetaIndex,AOIndex,4*sliceIndex+0] );
                                    W.Write( A[thetaIndex,AOIndex,4*sliceIndex+1] );
                                    W.Write( A[thetaIndex,AOIndex,4*sliceIndex+2] );
                                    W.Write( A[thetaIndex,AOIndex,4*sliceIndex+3] );
                                }
                        }
                    }

                    m_Tex_ACoeffs = new Texture2D( m_device, 64, 64, coeffSlices.Length, 1, PIXEL_FORMAT.RGBA32_FLOAT, false, false, coeffSlices );
                }

                {
                    // Load environment coeffs into a constant buffer
                    float3[]	coeffs = new float3[ORDERS*ORDERS];
                    using ( System.IO.FileStream S = new System.IO.FileInfo( @"Ennis_order20.float3" ).OpenRead() )
                        using ( System.IO.BinaryReader R = new System.IO.BinaryReader( S ) )
                            for ( int coeffIndex=0; coeffIndex < ORDERS*ORDERS; coeffIndex++ )
                                coeffs[coeffIndex].Set( R.ReadSingle(), R.ReadSingle(), R.ReadSingle() );

                    // Write into a raw byte[]
                    byte[]	rawContent = new byte[400 * 4 * 4];
                    using ( System.IO.MemoryStream	MS = new System.IO.MemoryStream( rawContent ) )
                        using ( System.IO.BinaryWriter W = new System.IO.BinaryWriter( MS ) ) {
                            for ( int coeffIndex=0; coeffIndex < ORDERS*ORDERS; coeffIndex++ ) {
                                W.Write( coeffs[coeffIndex].x );
                                W.Write( coeffs[coeffIndex].y );
                                W.Write( coeffs[coeffIndex].z );
                                W.Write( 0.0f );
                            }
                        }
             					m_CB_Coeffs = new RawConstantBuffer( m_device, 1, rawContent.Length );
                    m_CB_Coeffs.UpdateData( rawContent );
                }

                // Create camera + manipulator
                m_camera.CreatePerspectiveCamera( 0.5f * (float) Math.PI, (float) graphPanel.Width / graphPanel.Height, 0.01f, 100.0f );
                m_camera.CameraTransformChanged += m_camera_CameraTransformChanged;
                m_cameraManipulator.Attach( graphPanel, m_camera );
                m_cameraManipulator.InitializeCamera( -2.0f * float3.UnitZ, float3.Zero, float3.UnitY );
                m_camera_CameraTransformChanged( null, EventArgs.Empty );

                // Start rendering
                Application.Idle += Application_Idle;

            } catch ( Exception _e ) {
                MessageBox.Show( "Failed to initialize D3D renderer!\r\nReason: " + _e.Message );
            }
        }