Пример #1
0
 public static void Init(PictureBox handle)
 {
     try
     {
         presentParams = new PresentParameters();
         presentParams.Windowed = true;
         presentParams.EnableAutoDepthStencil = true;
         presentParams.AutoDepthStencilFormat = DepthFormat.D24S8;
         presentParams.SwapEffect = SwapEffect.Discard;
         device = new Device(0, DeviceType.Hardware, handle, CreateFlags.SoftwareVertexProcessing, presentParams);
         if (device == null)
             return;
         SetupEnvironment();                
         CreateCoordLines();
         CamDistance = 4;
         init = true;
         cam = new Vector3(0, 0, 0);
         dir = new Vector3(1, 1, 1);
         camdist = 3f;
         DebugLog.PrintLn(presentParams.ToString());
         DebugLog.PrintLn(device.DeviceCaps.ToString());
         DebugLog.PrintLn("DirectX Init succeeded...");
     }
     catch (DirectXException ex)
     {
         string s = "DirectX Init error:"
                         + "\n\n" + ex.ToString()
                         + "\n\n" + presentParams.ToString();
         if (device != null)
             s += "\n\n" + device.DeviceCaps.ToString();
         DebugLog.PrintLn(s);
         error = true;
     }
 }
Пример #2
0
 public static void Init(PictureBox handle)
 {
     try
     {
         presentParams          = new PresentParameters();
         presentParams.Windowed = true;
         presentParams.EnableAutoDepthStencil = true;
         presentParams.AutoDepthStencilFormat = DepthFormat.D24S8;
         presentParams.SwapEffect             = SwapEffect.Discard;
         device = new Device(0, DeviceType.Hardware, handle, CreateFlags.SoftwareVertexProcessing, presentParams);
         if (device == null)
         {
             return;
         }
         SetupEnvironment();
         CreateCoordLines();
         CamDistance = 4;
         init        = true;
         cam         = new Vector3(0, 0, 0);
         dir         = new Vector3(1, 1, 1);
         camdist     = 3f;
         DebugLog.PrintLn(presentParams.ToString());
         DebugLog.PrintLn(device.DeviceCaps.ToString());
         DebugLog.PrintLn("DirectX Init succeeded...");
     }
     catch (DirectXException ex)
     {
         string s = "DirectX Init error:"
                    + "\n\n" + ex.ToString()
                    + "\n\n" + presentParams.ToString();
         if (device != null)
         {
             s += "\n\n" + device.DeviceCaps.ToString();
         }
         DebugLog.PrintLn(s);
         error = true;
     }
 }
        /// <summary>
        /// Specifies the custom attribute by converting this to a string and passing to GetCustomAttribute()
        /// </summary>
        // public enum CustomAttribute { D3DDEVICE, D3DZBUFFER, D3DBACKBUFFER }
        public void CreateD3DResources()
        {
            // access device via driver
            Device device = driver.Device;

            if (isSwapChain && device == null) {
                throw new Exception("Secondary window has not been given the device from the primary!");
            }

            DeviceType devType = DeviceType.Hardware;

            presentParams = new PresentParameters();
            presentParams.Windowed = !isFullScreen;
            presentParams.SwapEffect = SwapEffect.Discard;
            // triple buffer if VSync is on
            presentParams.BackBufferCount = isVSync ? 2 : 1;
            presentParams.EnableAutoDepthStencil = isDepthBuffered;
            presentParams.DeviceWindow = windowHandle;
            presentParams.BackBufferWidth = width;
            presentParams.BackBufferHeight = height;
            presentParams.FullScreenRefreshRateInHz = isFullScreen ? displayFrequency : 0;

            if (isVSync) {
                presentParams.PresentationInterval = PresentInterval.One;
            } else {
                // NB not using vsync in windowed mode in D3D9 can cause jerking at low
                // frame rates no matter what buffering modes are used (odd - perhaps a
                // timer issue in D3D9 since GL doesn't suffer from this)
                // low is < 200fps in this context
                if (!isFullScreen)
                    log.Debug("Disabling VSync in windowed mode can cause timing issues at lower " +
                              "frame rates, turn VSync on if you observe this problem.");
                presentParams.PresentationInterval = PresentInterval.Immediate;
            }

            presentParams.BackBufferFormat = Format.R5G6B5;
            if (colorDepth > 16)
                presentParams.BackBufferFormat = Format.X8R8G8B8;

            if (colorDepth > 16) {
                // Try to create a 32-bit depth, 8-bit stencil
                if (!D3D.Manager.CheckDeviceFormat(driver.AdapterNumber, devType,
                                                   presentParams.BackBufferFormat,
                                                   Usage.DepthStencil,
                                                   ResourceType.Surface, DepthFormat.D24S8)) {
                    // Bugger, no 8-bit hardware stencil, just try 32-bit zbuffer
                    if (!D3D.Manager.CheckDeviceFormat(driver.AdapterNumber, devType,
                                                      presentParams.BackBufferFormat,
                                                      Usage.DepthStencil,
                                                      ResourceType.Surface, DepthFormat.D32)) {
                        // Jeez, what a naff card. Fall back on 16-bit depth buffering
                        presentParams.AutoDepthStencilFormat = DepthFormat.D16;
                    } else
                        presentParams.AutoDepthStencilFormat = DepthFormat.D32;
                } else {
                    // Woohoo!
                    if (D3D.Manager.CheckDepthStencilMatch(driver.AdapterNumber, devType,
                                                           presentParams.BackBufferFormat,
                                                           presentParams.BackBufferFormat,
                                                           DepthFormat.D24S8)) {
                        presentParams.AutoDepthStencilFormat = DepthFormat.D24S8;
                    } else
                        presentParams.AutoDepthStencilFormat = DepthFormat.D24X8;
                }
            } else {
                // 16-bit depth, software stencil
                presentParams.AutoDepthStencilFormat = DepthFormat.D16;
            }

            presentParams.MultiSample = fsaaType;
            presentParams.MultiSampleQuality = fsaaQuality;

            if (isSwapChain) {
                swapChain = new SwapChain(device, presentParams);
                if (swapChain == null) {
               		    // Try a second time, may fail the first time due to back buffer count,
                    // which will be corrected by the runtime
                    swapChain = new SwapChain(device, presentParams);
                }
                // Store references to buffers for convenience
                renderSurface = swapChain.GetBackBuffer(0, BackBufferType.Mono);
             			// Additional swap chains need their own depth buffer
                // to support resizing them
                if (isDepthBuffered) {
                    renderZBuffer =
                        device.CreateDepthStencilSurface(width, height,
                                                         presentParams.AutoDepthStencilFormat,
                                                         presentParams.MultiSample,
                                                         presentParams.MultiSampleQuality,
                                                         false);
                } else {
                    renderZBuffer = null;
                }
                // Ogre releases the mpRenderSurface here (but not the mpRenderZBuffer)
                // release immediately so we don't hog them
                // mpRenderSurface->Release();
                // We'll need the depth buffer for rendering the swap chain
                // //mpRenderZBuffer->Release();
            } else {
                if (device == null) {
            #if !USE_D3D_EVENTS
                    // Turn off default event handlers, since Managed DirectX seems confused.
                    Device.IsUsingEventHandlers = false;
            #endif

                    // We haven't created the device yet, this must be the first time

                    // Do we want to preserve the FPU mode? Might be useful for scientific apps
                    CreateFlags extraFlags = 0;
                    if (multiThreaded) {
                        extraFlags |= CreateFlags.MultiThreaded;
                    }
                    // TODO: query and preserve the fpu mode

                    // Set default settings (use the one Ogre discovered as a default)
                    int adapterToUse = driver.AdapterNumber;
                    if (useNVPerfHUD) {
                        // Look for 'NVIDIA NVPerfHUD' adapter
                        // If it is present, override default settings
                        foreach (AdapterInformation identifier in D3D.Manager.Adapters) {
                            log.Info("Device found: " + identifier.Information.Description);
                            if (identifier.Information.Description.Contains("PerfHUD")) {
                                log.Info("Attempting to use PerfHUD");
                                adapterToUse = identifier.Adapter;
                                devType = DeviceType.Reference;
                                break;
                            }
                        }
                    }

                    try
                    {
                        device = new D3D.Device(adapterToUse, devType, windowHandle,
                                                CreateFlags.HardwareVertexProcessing | extraFlags,
                                                presentParams);
                    }
                    catch (Exception) {
                        log.Info("First device creation failed");
                        try
                        {
                            // Try a second time, may fail the first time due to back buffer count,
                            // which will be corrected down to 1 by the runtime
                            device = new D3D.Device(adapterToUse, devType, windowHandle,
                                                    CreateFlags.HardwareVertexProcessing | extraFlags,
                                                    presentParams);
                        }
                        catch (Exception)
                        {
                            try
                            {
                                // Looks like we can't use HardwareVertexProcessing, so try Mixed
                                device = new D3D.Device(adapterToUse, devType, windowHandle,
                                                        CreateFlags.MixedVertexProcessing | extraFlags,
                                                        presentParams);
                            }
                            catch (Exception)
                            {
                                // Ok, one last try. Try software.  If this fails, just throw up.
                                device = new D3D.Device(adapterToUse, devType, windowHandle,
                                                        CreateFlags.SoftwareVertexProcessing | extraFlags,
                                                        presentParams);
                            }
                        }
                    }

                    // TODO: For a full screen app, I'm supposed to do this to prevent alt-tab
                    //       from messing things up.
                    //device.DeviceResizing += new
                    //    System.ComponentModel.CancelEventHandler(this.CancelResize);

                }
                log.InfoFormat("Device constructed with presentation parameters: {0}", presentParams.ToString());

                // update device in driver
                driver.Device = device;
                // Store references to buffers for convenience
                renderSurface = device.GetRenderTarget(0);
                renderZBuffer = device.DepthStencilSurface;
                // Ogre releases these here
                // release immediately so we don't hog them
                // mpRenderSurface->Release();
                // mpRenderZBuffer->Release();
            }
        }
Пример #4
0
        /// <summary>
        /// Specifies the custom attribute by converting this to a string and passing to GetCustomAttribute()
        /// </summary>
        // public enum CustomAttribute { D3DDEVICE, D3DZBUFFER, D3DBACKBUFFER }

        public void CreateD3DResources()
        {
            // access device via driver
            Device device = driver.Device;

            if (isSwapChain && device == null)
            {
                throw new Exception("Secondary window has not been given the device from the primary!");
            }

            DeviceType devType = DeviceType.Hardware;

            presentParams            = new PresentParameters();
            presentParams.Windowed   = !isFullScreen;
            presentParams.SwapEffect = SwapEffect.Discard;
            // triple buffer if VSync is on
            presentParams.BackBufferCount           = isVSync ? 2 : 1;
            presentParams.EnableAutoDepthStencil    = isDepthBuffered;
            presentParams.DeviceWindow              = windowHandle;
            presentParams.BackBufferWidth           = width;
            presentParams.BackBufferHeight          = height;
            presentParams.FullScreenRefreshRateInHz = isFullScreen ? displayFrequency : 0;

            if (isVSync)
            {
                presentParams.PresentationInterval = PresentInterval.One;
            }
            else
            {
                // NB not using vsync in windowed mode in D3D9 can cause jerking at low
                // frame rates no matter what buffering modes are used (odd - perhaps a
                // timer issue in D3D9 since GL doesn't suffer from this)
                // low is < 200fps in this context
                if (!isFullScreen)
                {
                    log.Debug("Disabling VSync in windowed mode can cause timing issues at lower " +
                              "frame rates, turn VSync on if you observe this problem.");
                }
                presentParams.PresentationInterval = PresentInterval.Immediate;
            }

            presentParams.BackBufferFormat = Format.R5G6B5;
            if (colorDepth > 16)
            {
                presentParams.BackBufferFormat = Format.X8R8G8B8;
            }

            if (colorDepth > 16)
            {
                // Try to create a 32-bit depth, 8-bit stencil
                if (!D3D.Manager.CheckDeviceFormat(driver.AdapterNumber, devType,
                                                   presentParams.BackBufferFormat,
                                                   Usage.DepthStencil,
                                                   ResourceType.Surface, DepthFormat.D24S8))
                {
                    // Bugger, no 8-bit hardware stencil, just try 32-bit zbuffer
                    if (!D3D.Manager.CheckDeviceFormat(driver.AdapterNumber, devType,
                                                       presentParams.BackBufferFormat,
                                                       Usage.DepthStencil,
                                                       ResourceType.Surface, DepthFormat.D32))
                    {
                        // Jeez, what a naff card. Fall back on 16-bit depth buffering
                        presentParams.AutoDepthStencilFormat = DepthFormat.D16;
                    }
                    else
                    {
                        presentParams.AutoDepthStencilFormat = DepthFormat.D32;
                    }
                }
                else
                {
                    // Woohoo!
                    if (D3D.Manager.CheckDepthStencilMatch(driver.AdapterNumber, devType,
                                                           presentParams.BackBufferFormat,
                                                           presentParams.BackBufferFormat,
                                                           DepthFormat.D24S8))
                    {
                        presentParams.AutoDepthStencilFormat = DepthFormat.D24S8;
                    }
                    else
                    {
                        presentParams.AutoDepthStencilFormat = DepthFormat.D24X8;
                    }
                }
            }
            else
            {
                // 16-bit depth, software stencil
                presentParams.AutoDepthStencilFormat = DepthFormat.D16;
            }

            presentParams.MultiSample        = fsaaType;
            presentParams.MultiSampleQuality = fsaaQuality;

            if (isSwapChain)
            {
                swapChain = new SwapChain(device, presentParams);
                if (swapChain == null)
                {
                    // Try a second time, may fail the first time due to back buffer count,
                    // which will be corrected by the runtime
                    swapChain = new SwapChain(device, presentParams);
                }
                // Store references to buffers for convenience
                renderSurface = swapChain.GetBackBuffer(0, BackBufferType.Mono);
                // Additional swap chains need their own depth buffer
                // to support resizing them
                if (isDepthBuffered)
                {
                    renderZBuffer =
                        device.CreateDepthStencilSurface(width, height,
                                                         presentParams.AutoDepthStencilFormat,
                                                         presentParams.MultiSample,
                                                         presentParams.MultiSampleQuality,
                                                         false);
                }
                else
                {
                    renderZBuffer = null;
                }
                // Ogre releases the mpRenderSurface here (but not the mpRenderZBuffer)
                // release immediately so we don't hog them
                // mpRenderSurface->Release();
                // We'll need the depth buffer for rendering the swap chain
                // //mpRenderZBuffer->Release();
            }
            else
            {
                if (device == null)
                {
#if !USE_D3D_EVENTS
                    // Turn off default event handlers, since Managed DirectX seems confused.
                    Device.IsUsingEventHandlers = false;
#endif

                    // We haven't created the device yet, this must be the first time

                    // Do we want to preserve the FPU mode? Might be useful for scientific apps
                    CreateFlags extraFlags = 0;
                    if (multiThreaded)
                    {
                        extraFlags |= CreateFlags.MultiThreaded;
                    }
                    // TODO: query and preserve the fpu mode

                    // Set default settings (use the one Ogre discovered as a default)
                    int adapterToUse = driver.AdapterNumber;
                    if (useNVPerfHUD)
                    {
                        // Look for 'NVIDIA NVPerfHUD' adapter
                        // If it is present, override default settings
                        foreach (AdapterInformation identifier in D3D.Manager.Adapters)
                        {
                            log.Info("Device found: " + identifier.Information.Description);
                            if (identifier.Information.Description.Contains("PerfHUD"))
                            {
                                log.Info("Attempting to use PerfHUD");
                                adapterToUse = identifier.Adapter;
                                devType      = DeviceType.Reference;
                                break;
                            }
                        }
                    }

                    try
                    {
                        device = new D3D.Device(adapterToUse, devType, windowHandle,
                                                CreateFlags.HardwareVertexProcessing | extraFlags,
                                                presentParams);
                    }
                    catch (Exception) {
                        log.Info("First device creation failed");
                        try
                        {
                            // Try a second time, may fail the first time due to back buffer count,
                            // which will be corrected down to 1 by the runtime
                            device = new D3D.Device(adapterToUse, devType, windowHandle,
                                                    CreateFlags.HardwareVertexProcessing | extraFlags,
                                                    presentParams);
                        }
                        catch (Exception)
                        {
                            try
                            {
                                // Looks like we can't use HardwareVertexProcessing, so try Mixed
                                device = new D3D.Device(adapterToUse, devType, windowHandle,
                                                        CreateFlags.MixedVertexProcessing | extraFlags,
                                                        presentParams);
                            }
                            catch (Exception)
                            {
                                // Ok, one last try. Try software.  If this fails, just throw up.
                                device = new D3D.Device(adapterToUse, devType, windowHandle,
                                                        CreateFlags.SoftwareVertexProcessing | extraFlags,
                                                        presentParams);
                            }
                        }
                    }

                    // TODO: For a full screen app, I'm supposed to do this to prevent alt-tab
                    //       from messing things up.
                    //device.DeviceResizing += new
                    //    System.ComponentModel.CancelEventHandler(this.CancelResize);
                }
                log.InfoFormat("Device constructed with presentation parameters: {0}", presentParams.ToString());

                // update device in driver
                driver.Device = device;
                // Store references to buffers for convenience
                renderSurface = device.GetRenderTarget(0);
                renderZBuffer = device.DepthStencilSurface;
                // Ogre releases these here
                // release immediately so we don't hog them
                // mpRenderSurface->Release();
                // mpRenderZBuffer->Release();
            }
        }