예제 #1
0
        // CActivity 実装

        public override void OnManagedCreateResources()
        {
            if (!base.bNotActivated)
            {
                try                                     // #xxxxx 2012.12.31 yyagi: to prepare flush, first of all, I create q queue to the GPU.
                {
                    IDirect3DQuery9 = new SharpDX.Direct3D9.Query(CDTXMania.app.Device, QueryType.Occlusion);
                }
                catch (Exception e)
                {
                    Trace.TraceError(e.Message);
                }
                base.OnManagedCreateResources();
            }
        }
예제 #2
0
        private void CreateResources(Device device, int width, int height, Format format)
        {
            if (_resourcesInitialised) return;
            _resourcesInitialised = true;
            
            // Create offscreen surface to use as copy of render target data
            _renderTargetCopy = ToDispose(Surface.CreateOffscreenPlain(device, width, height, format, Pool.SystemMemory));
            
            // Create our resolved surface (resizing if necessary and to resolve any multi-sampling)
            _resolvedTarget = ToDispose(Surface.CreateRenderTarget(device, width, height, format, MultisampleType.None, 0, false));

            _query = ToDispose(new Query(device, QueryType.Event));
        }
예제 #3
0
 public void UnloadContent()
 {
     if (dxQuery != null && !dxQuery.IsDisposed)
     {
         dxQuery.Dispose();
         dxQuery = null;
     }
 }
예제 #4
0
 public void LoadContent(Device device)
 {
     System.Diagnostics.Debug.Assert(dxQuery == null);
     dxQuery = new Query(device, QueryType.Occlusion);
 }
        private void SetupSurfaces(Device device)
        {
            try
            {
                this.surface = Surface.CreateOffscreenPlain(device, this.width, this.height, (Format)this.format, Pool.SystemMemory);
                var lockedRect = this.surface.LockRectangle(LockFlags.ReadOnly);
                this.pitch = lockedRect.Pitch;
                this.surface.UnlockRectangle();
                this.renderTarget = Surface.CreateRenderTarget(device, this.width, this.height, this.format, MultisampleType.None, 0, false);
                this.query = new Query(device, QueryType.Event);

                killThread = false;
                this.copyThread = new Thread(this.HandleCaptureRequestThread);
                this.copyThread.Start();

                this.retrieveThread = new Thread(this.RetrieveImageDataThread);
                this.retrieveThread.Start();

                this.surfacesSetup = true;
            }
            catch (Exception ex)
            {
                this.DebugMessage(ex.ToString());
                ClearData();
            }
        }
        private void ClearData()
        {
            this.DebugMessage("ClearData called");

            if (this.copyThread != null)
            {
                this.killThread = true;
                this.copyEvent.Set();

                if (!this.copyThread.Join(500))
                {
                    this.copyThread.Abort();
                }

                this.copyEvent.Reset();
                this.copyThread = null;
            }

            if (this.retrieveThread != null)
            {
                this.killThread = true;
                this.copyReadySignal.Set();

                if (this.retrieveThread.Join(500))
                {
                    this.retrieveThread.Abort();
                }

                this.copyReadySignal.Reset();
                this.retrieveThread = null;
            }

            // currentDevice = null;
            if (this.Request != null)
            {
                this.Request.Dispose();
                this.Request = null;
            }

            this.width = 0;
            this.height = 0;
            this.pitch = 0;
            if (this.surfaceLocked)
            {
                lock (this.surfaceLock)
                {
                    this.surface.UnlockRectangle();
                    this.surfaceLocked = false;
                }
            }

            if (this.surface != null)
            {
                this.surface.Dispose();
                this.surface = null;
            }
            if (this.renderTarget != null)
            {
                this.renderTarget.Dispose();
                this.renderTarget = null;
            }
            if (this.query != null)
            {
                this.query.Dispose();
                this.query = null;
                this.queryIssued = false;
            }
            this.hooksStarted = false;
            this.surfacesSetup = false;
        }
예제 #7
0
 private void SetupSurfaces(Device device)
 {
     try
     {
         this.surface = Surface.CreateOffscreenPlain(device, width, height, (Format)format, Pool.SystemMemory);
         var lockedRect = this.surface.LockRectangle(LockFlags.ReadOnly);
         this.pitch = lockedRect.Pitch;
         this.surface.UnlockRectangle();
         this.renderTarget = Surface.CreateRenderTarget(device, width, height, format, MultisampleType.None, 0, false);
         this.query = new Query(device, QueryType.Event);
         surfacesSetup = true;
     }
     catch (Exception ex)
     {
         DebugMessage(ex.ToString());
     }
 }
예제 #8
0
        private void ClearData()
        {
            DebugMessage("ClearData called");

            // currentDevice = null;
            this.Request = null;
            width = 0;
            height = 0;
            pitch = 0;
            surfacesSetup = false;
            this.hooksStarted = false;
            if (surfaceLocked)
            {
                lock (surfaceLock)
                {
                    surface.UnlockRectangle();
                    surfaceLocked = false;
                }
            }

            if (surface != null)
            {
                surface.Dispose();
                surface = null;
            }
            if (renderTarget != null)
            {
                renderTarget.Dispose();
                renderTarget = null;
            }
            if (query != null)
            {
                query.Dispose();
                query = null;
                queryIssued = false;
            }
        }
        private void SetupSurfaces(Device device)
        {
            try
            {
                for (var i = 0; i < BUFFERS; i++)
                {
                    _surfaces[i] = Surface.CreateOffscreenPlain(device, _copyData.width, _copyData.height, (Format)_copyData.format, Pool.SystemMemory);
                    var lockedRect = _surfaces[i].LockRectangle(LockFlags.ReadOnly);
                    _copyData.pitch = lockedRect.Pitch;
                    _surfaces[i].UnlockRectangle();

                    _copySurfaces[i] = Surface.CreateRenderTarget(device, _copyData.width, _copyData.height, (Format)_copyData.format, MultisampleType.None, 0, false);
                    _queries[i] = new Query(device, QueryType.Event);

                    _surfaceLocks[i] = new object();
                }

                _copyDataMem = MemoryMappedFile.CreateOrOpen("CaptureHookSharedMemData", Marshal.SizeOf(typeof(CopyData)));
                _copyDataMemAccess = _copyDataMem.CreateViewAccessor(0, Marshal.SizeOf(typeof(CopyData)));
                _copyDataMemAccess.SafeMemoryMappedViewHandle.AcquirePointer(ref _copyDataMemPtr);

                _copyData.textureId = Guid.NewGuid();

                for (var i = 0; i < 2; i++)
                {
                    var locked = false;
                    try
                    {
                        try
                        {
                            locked = _sharedMemMutexes[i].WaitOne(0);
                        }
                        catch (AbandonedMutexException)
                        {
                            locked = true;
                        }
                        if (!locked)
                        {
                            DebugMessage("shared mem mutex still locked");
                        }

                        _sharedTextures[i] = MemoryMappedFile.CreateOrOpen(
                            _copyData.textureId.ToString() + i,
                            _copyData.pitch * _copyData.height,
                            MemoryMappedFileAccess.ReadWrite);
                        _sharedTexturesAccess[i] = _sharedTextures[i].CreateViewAccessor(0, _copyData.pitch * _copyData.height, MemoryMappedFileAccess.ReadWrite);
                        _sharedTexturesAccess[i].SafeMemoryMappedViewHandle.AcquirePointer(ref _sharedTexturesPtr[i]);
                    }
                    finally
                    {
                        if (locked)
                        {
                            _sharedMemMutexes[i].ReleaseMutex();
                        }
                    }
                }

                _killThread = false;
                _surfacesSetup = true;

                Marshal.StructureToPtr(_copyData, new IntPtr(_copyDataMemPtr), false);

                _retrieveThread = new Thread(RetrieveImageDataThread);
                _retrieveThread.IsBackground = true;
                _retrieveThread.Start();
                _hookReadyWaitHandle.Set();
            }
            catch (Exception ex)
            {
                DebugMessage(ex.ToString());
                ClearData();
            }
        }
        private void SetupSurfaces(Device device)
        {
            try
            {
                _surface = Surface.CreateOffscreenPlain(device, _width, _height, _format, Pool.SystemMemory);
                var lockedRect = _surface.LockRectangle(LockFlags.ReadOnly);
                _pitch = lockedRect.Pitch;
                _surface.UnlockRectangle();
                _renderTarget = Surface.CreateRenderTarget(device, _width, _height, _format, MultisampleType.None, 0, false);
                _query = new Query(device, QueryType.Event);

                _killThread = false;
                _copyThread = new Thread(HandleCaptureRequestThread);
                _copyThread.IsBackground = true;
                _copyThread.Start();

                _retrieveThread = new Thread(RetrieveImageDataThread);
                _retrieveThread.IsBackground = true;
                _retrieveThread.Start();

                _surfacesSetup = true;
            }
            catch (Exception ex)
            {
                DebugMessage(ex.ToString());
                ClearData();
            }
        }
        private void ClearData()
        {
            DebugMessage("ClearData called");

            if (_copyThread != null)
            {
                _killThread = true;
                _copyEvent.Set();

                if (!_copyThread.Join(500))
                {
                    _copyThread.Abort();
                }

                _copyEvent.Reset();
                _copyThread = null;
            }

            if (_retrieveThread != null)
            {
                _killThread = true;
                _copyReadySignal.Set();

                if (_retrieveThread.Join(500))
                {
                    _retrieveThread.Abort();
                }

                _copyReadySignal.Reset();
                _retrieveThread = null;
            }

            // currentDevice = null;
            if (Request != null)
            {
                Request.Dispose();
                Request = null;
            }

            _width = 0;
            _height = 0;
            _pitch = 0;
            if (_surfaceLocked)
            {
                lock (_surfaceLock)
                {
                    _surface.UnlockRectangle();
                    _surfaceLocked = false;
                }
            }

            if (_surface != null)
            {
                _surface.Dispose();
                _surface = null;
            }
            if (_renderTarget != null)
            {
                _renderTarget.Dispose();
                _renderTarget = null;
            }
            if (_query != null)
            {
                _query.Dispose();
                _query = null;
                _queryIssued = false;
            }
            _hooksStarted = false;
            _surfacesSetup = false;
        }
        // Testing function call - creates DX9 device & present test:
        public bool TestDX(Direct3D d3dh, ref MyAdapterInfo[] infos)
        {
#if !XB1
            bool isAnyGraphicsSupported = false;
            MyLog.Default.WriteLine("MyGraphicTest.TestDX() - START");
            MyLog.Default.IncreaseIndent();

            LogInfoFromWMI();

            bool isAnyGoodGCinWMI = IsAnyGoodGCinList(m_WMIGraphicsCards);
            MyLog.Default.WriteLine("Good graphics in WMI detected: " + isAnyGoodGCinWMI);

            //Check debug runtime
            MyLog.Default.WriteLine("Debug runtime enabled: " + IsDebugRuntimeEnabled);

            PresentParameters newPresentParameters;

            try
            {
                MyLog.Default.WriteLine("Adapter count: " + d3dh.AdapterCount);
                for (int i = 0; i < d3dh.AdapterCount; i++)
                {
                    var info = d3dh.GetAdapterIdentifier(i);
                    MyLog.Default.WriteLine(String.Format("Found adapter: {0} ({1})", info.Description, info.DeviceName));
                }
                MyLog.Default.WriteLine("Adapter count: " + d3dh.AdapterCount);

                // DX:
                newPresentParameters = new PresentParameters();
                newPresentParameters.InitDefaults();
                newPresentParameters.Windowed = true;
                newPresentParameters.AutoDepthStencilFormat = Format.D24S8;
                newPresentParameters.EnableAutoDepthStencil = true;
                newPresentParameters.SwapEffect = SwapEffect.Discard;
                newPresentParameters.PresentFlags = PresentFlags.DiscardDepthStencil;

                m_DXGraphicsCards.Clear();

                // Write adapter information to the LOG file:
                MyLog.Default.WriteLine("Adapters count: " + d3dh.AdapterCount);
                MyLog.Default.WriteLine("Adapter array count: " + d3dh.Adapters.Count);

                for (int adapter = 0; adapter < d3dh.AdapterCount; adapter++)
                {
                    bool adapterSupported = false;

                    var adapterIdentifier = d3dh.GetAdapterIdentifier(adapter);
                    MyLog.Default.WriteLine("Adapter " + adapterIdentifier.Description + ": " + adapterIdentifier.DeviceName);

                    Device d3d = null;
                    Form testForm = null;

                    try
                    {
                        //Create window, because other this fails on some ATIs..
                        testForm = new Form();
                        testForm.ClientSize = new System.Drawing.Size(64, 64);
                        testForm.StartPosition = FormStartPosition.CenterScreen;
                        testForm.FormBorderStyle = FormBorderStyle.None;
                        testForm.BackColor = System.Drawing.Color.Black;
                        testForm.Show();

                        newPresentParameters.DeviceWindowHandle = testForm.Handle;
                        d3d = new Device(d3dh, adapter, DeviceType.Hardware, testForm.Handle, CreateFlags.HardwareVertexProcessing | CreateFlags.FpuPreserve, newPresentParameters);

                        if (d3d == null)
                        {
                            throw new Exception("Cannot create Direct3D Device");
                        }
                        else
                            MyLog.Default.WriteLine("d3d handle ok ");
                    }
                    catch (Exception e)
                    {
                        if (testForm != null)
                            testForm.Close();

                        MyLog.Default.WriteLine("Direct3D Device create fail");
                        MyLog.Default.WriteLine(e.ToString());

                        Write(newPresentParameters, MyLog.Default.WriteLine);
                        continue;
                    }

                    adapterSupported |= !TestCapabilities(d3d, d3dh, adapter);

                    infos[adapter].MaxTextureSize = d3d.Capabilities.MaxTextureWidth;

                    bool Rgba1010102Supported = d3dh.CheckDeviceFormat(adapter, DeviceType.Hardware, Format.X8R8G8B8, Usage.RenderTarget, ResourceType.Surface, Format.A2R10G10B10);
                    MyLog.Default.WriteLine("Rgba1010102Supported: " + Rgba1010102Supported);

                    bool MipmapNonPow2Supported = !d3d.Capabilities.TextureCaps.HasFlag(TextureCaps.Pow2) &&
                        !d3d.Capabilities.TextureCaps.HasFlag(TextureCaps.NonPow2Conditional) &&
                        d3d.Capabilities.TextureCaps.HasFlag(TextureCaps.MipMap);
                    MyLog.Default.WriteLine("MipmapNonPow2Supported: " + MipmapNonPow2Supported);

                    infos[adapter].HDRSupported = Rgba1010102Supported && MipmapNonPow2Supported;
                    MyLog.Default.WriteLine("HDRSupported: " + infos[adapter].HDRSupported);

                    bool QueriesSupported = false;
                    try
                    {
                        MyLog.Default.WriteLine("Create query");
                        Query query = new Query(d3d, QueryType.Event);
                        MyLog.Default.WriteLine("Dispose query");
                        query.Dispose();
                        QueriesSupported = true;
                    }
                    catch
                    {
                        QueriesSupported = false;
                    }

                    //Test sufficient video memory (512MB)
                    bool Has512AvailableVRAM = TestAvailable512VRAM(d3d);

                    //We require queries
                    adapterSupported &= QueriesSupported;

                    infos[adapter].IsDx9Supported = adapterSupported;
                    infos[adapter].Has512MBRam = Has512AvailableVRAM;

                    isAnyGraphicsSupported |= adapterSupported;

                    MyLog.Default.WriteLine("Queries supported: " + QueriesSupported.ToString());

                    m_DXGraphicsCards.Add(adapterIdentifier.VendorId);

                    if (d3d != null)
                    {
                        d3d.Dispose();
                        d3d = null;
                    }

                    if (testForm != null)
                        testForm.Close();
                }
            }
            catch (Exception ex)
            {
                MyLog.Default.WriteLine("Exception throwed by DX test. Source: " + ex.Source);
                MyLog.Default.WriteLine("Message: " + ex.Message);
                MyLog.Default.WriteLine("Inner exception: " + ex.InnerException);
                MyLog.Default.WriteLine("Exception details" + ex.ToString());
            }

            bool isAnyGoodGCinDX = IsAnyGoodGCinList(m_DXGraphicsCards);
            MyLog.Default.WriteLine("Good graphics in DX detected: " + isAnyGoodGCinDX);

            IsBetterGCAvailable = isAnyGoodGCinWMI && !isAnyGoodGCinDX;
            MyLog.Default.WriteLine("Is better graphics available: " + IsBetterGCAvailable);

            MyLog.Default.DecreaseIndent();
            MyLog.Default.WriteLine("MyGraphicTest.TestDX() - END");

            return isAnyGraphicsSupported;
#else // XB1
            System.Diagnostics.Debug.Assert(false, "XB1 TOOD?");
            return false;
#endif // XB1
        }
        public void UnloadContent()
        {
            if (dxQuery != null && !dxQuery.IsDisposed)
            {
                dxQuery.Dispose();
            }

            dxQuery = null;
            m_started = false;
            m_inDraw = false;
        }
예제 #14
0
        private void DoD3DHooks(Device device)
        {
            bool bSuccess = true;

            for (int i = 0; i < NUM_BUFFERS; i++)
            {
                try
                {
                    textures[i] = Surface.CreateOffscreenPlain(device, copyData.width, copyData.height, (Format)copyData.format, Pool.SystemMemory);
                    if (i == (NUM_BUFFERS - 1))
                    {
                        var lr = textures[i].LockRectangle(LockFlags.ReadOnly);
                        copyData.pitch = lr.Pitch;
                        textures[i].UnlockRectangle();
                    }
                }
                catch (Exception ex)
                {
                    DebugMessage(ex.ToString());
                    bSuccess = false;
                    break;
                }
            }

            if (bSuccess)
            {
                for (var i = 0; i < NUM_BUFFERS; i++)
                {
                    try
                    {
                        copyD3D9Textures[i] = Surface.CreateRenderTarget(device, copyData.width, copyData.height, (Format)copyData.format, MultisampleType.None, 0, false);
                        queries[i] = new Query(device, QueryType.Event);
                    }
                    catch (Exception ex)
                    {
                        DebugMessage(ex.ToString());
                        bSuccess = false;
                    }
                }
            }

            if (bSuccess)
            {
                try
                {
                    bKillThread = false;
                    hCopyThread = new Thread(CopyD3D9CPUTextureThread);
                    hCopyThread.Start();
                }
                catch (Exception ex)
                {
                    DebugMessage(ex.ToString());
                }
            }

            if (bSuccess)
            {
                try
                {
                    sharedMem = MemoryMappedFile.CreateNew("CaptureHookSharedMem", copyData.pitch * copyData.height + (Marshal.SizeOf(typeof(CopyData))), MemoryMappedFileAccess.ReadWrite);
                }
                catch (Exception ex)
                {
                    DebugMessage(ex.ToString());
                    bSuccess = false;
                }
            }

            if (bSuccess)
            {
                bHasTextures = true;
                DebugMessage("Hooked Direct3D Surfaces.");
            }
            else
            {
                DebugMessage("Unknwown error during hooking Direct3D Surfaces.");
                ClearD3D9Data();
            }
        }