Ejemplo n.º 1
0
        private void doTestThree()
        {
            // What do you think will be output here?
            CppObject cpp = new CppObject();

            cpp = new CppObject(5);
        }
Ejemplo n.º 2
0
        unsafe IntPtr IStateSubObjectDescriptionMarshal.__MarshalAlloc(Dictionary <StateSubObject, IntPtr> subObjectLookup)
        {
            __Native *native = (__Native *)Marshal.AllocHGlobal(sizeof(__Native));

            native->RootSignature = CppObject.ToCallbackPtr <ID3D12RootSignature>(RootSignature);
            return((IntPtr)native);
        }
Ejemplo n.º 3
0
        private static void PlatformPlaySong(Song song)
        {
            // Cleanup the last song first.
            if (State != MediaState.Stopped)
            {
                _session.Stop();
                _session.ClearTopologies();
                _session.Close();
                _volumeController.Dispose();
                _clock.Dispose();
            }

            // Set the new song.
            _session.SetTopology(SessionSetTopologyFlags.Immediate, song.Topology);

            // Get the volume interface.
            _volumeController = CppObject.FromPointer <AudioStreamVolume>(MediaPlayer.GetVolumeObj(_session));
            SetChannelVolumes();

            // Get the clock.
            _clock = _session.Clock.QueryInterface <PresentationClock>();

            //create the callback if it hasn't been created yet
            if (_callback == null)
            {
                _callback = new Callback();
                _session.BeginGetEvent(_callback, null);
            }

            // Start playing.
            var varStart = new Variant();

            _session.Start(null, varStart);
        }
Ejemplo n.º 4
0
        private static Result RawD3D11CreateDeviceNoDeviceAndContext(
            IDXGIAdapter adapter,
            DriverType driverType,
            DeviceCreationFlags flags,
            FeatureLevel[] featureLevels,
            out FeatureLevel featureLevel)
        {
            unsafe
            {
                var adapterPtr = CppObject.ToCallbackPtr <IDXGIAdapter>(adapter);
                fixed(void *featureLevelPtr = &featureLevel)
                {
                    Result result = D3D11CreateDevice_(
                        (void *)adapterPtr,
                        (int)driverType,
                        null,
                        (int)flags,
                        featureLevels != null && featureLevels.Length > 0 ? Unsafe.AsPointer(ref featureLevels[0]) : null,
                        featureLevels?.Length ?? 0,
                        SdkVersion,
                        null,
                        featureLevelPtr,
                        null);

                    GC.KeepAlive(adapter);
                    return(result);
                }
            }
        }
Ejemplo n.º 5
0
        public void GetCallbackPtrForClassWithMultipleInheritenceShouldReturnPointer()
        {
            using var callback = new Callback2Impl();

            Assert.NotEqual(IntPtr.Zero, CppObject.ToCallbackPtr <ICallback>(callback));
            Assert.NotEqual(IntPtr.Zero, CppObject.ToCallbackPtr <ICallback2>(callback));
        }
Ejemplo n.º 6
0
 public void GetCallbackPtrReturnsPointerToShadow()
 {
     using (var callback = new CallbackImpl())
     {
         Assert.NotEqual(IntPtr.Zero, CppObject.ToCallbackPtr <ICallback>(callback));
     }
 }
Ejemplo n.º 7
0
        void InitializeSharedBackBuffer(IntPtr resourcePtr)
        {
            // convert native pointer to DXGI shared resource
            Resource resource = CppObject.FromPointer <Resource>(resourcePtr).QueryInterface <Resource>();

            // convert shared resource to D3D11 Texture
            D3D11.Texture2D sharedBackbuffer = device.OpenSharedResource <D3D11.Texture2D>(resource.SharedHandle);

            // release reference
            resource.Dispose();

            // use D3D11 Texture as render target
            D3D11.RenderTargetViewDescription desc = new D3D11.RenderTargetViewDescription();
            desc.Format             = Format.B8G8R8A8_UNorm;
            desc.Dimension          = D3D11.RenderTargetViewDimension.Texture2D;
            desc.Texture2D.MipSlice = 0;

            renderTargetView = new D3D11.RenderTargetView(device, sharedBackbuffer, desc);
            deviceContext.OutputMerger.SetRenderTargets(renderTargetView);

            // release reference
            sharedBackbuffer.Dispose();

            // setup viewport
            Size size = Utils.WpfSizeToPixels(ImageGrid);

            deviceContext.Rasterizer.SetViewport(new Viewport(0, 0, (int)size.Width, (int)size.Height, 0.0f, 1.0f));
        }
Ejemplo n.º 8
0
        /// <summary>
        /// Opens a handle for a shared fence by using resourceHandle.
        /// </summary>
        /// <param name="resourceHandle">The handle that was returned by a call to ID3D11Fence::CreateSharedHandle or ID3D12Device::CreateSharedHandle</param>
        /// <returns>Fence</returns>
        public Fence OpenSharedFence(IntPtr resourceHandle)
        {
            IntPtr temp;

            OpenSharedFence(resourceHandle, Utilities.GetGuidFromType(typeof(Fence)), out temp);
            return(CppObject.FromPointer <Fence>(temp));
        }
Ejemplo n.º 9
0
        private void PlatformPlay()
        {
            // Cleanup the last song first.
            if (State != MediaState.Stopped)
            {
                _session.Stop();
                _volumeController.Dispose();
                _clock.Dispose();
            }

            // Set the new song.
            _session.SetTopology(0, _currentVideo.Topology);

            _volumeController              = CppObject.FromPointer <SimpleAudioVolume>(GetVolumeObj(_session));
            _volumeController.Mute         = IsMuted;
            _volumeController.MasterVolume = _volume;

            // Get the clock.
            _clock = _session.Clock.QueryInterface <PresentationClock>();

            //create the callback if it hasn't been created yet
            if (_callback == null)
            {
                _callback = new Callback();
                _session.BeginGetEvent(_callback, null);
            }

            // Start playing.
            var varStart = new Variant();

            _session.Start(null, varStart);
        }
Ejemplo n.º 10
0
 /// <summary>
 /// Initializes a new instance of the <see cref="ObjectReference"/> class.
 /// </summary>
 /// <param name="creationTime">The creation time.</param>
 /// <param name="cppObject">The com object to track.</param>
 /// <param name="stackTrace">The stack trace.</param>
 public ObjectReference(DateTime creationTime, CppObject cppObject, string stackTrace)
 {
     CreationTime = creationTime;
     // Creates a long weak reference to the CppObject
     Object     = new WeakReference(cppObject, true);
     StackTrace = stackTrace;
 }
Ejemplo n.º 11
0
        private static void PlaySong(Song song)
        {
#if WINDOWS_MEDIA_ENGINE
            _mediaEngineEx.Source = song.FilePath;
            _mediaEngineEx.Load();
            _mediaEngineEx.Play();
#elif WINDOWS_MEDIA_SESSION
            // Cleanup the last song first.
            if (State != MediaState.Stopped)
            {
                _session.Stop();
                _volumeController.Dispose();
                _clock.Dispose();
            }

            // Set the new song.
            _session.SetTopology(0, song.GetTopology());

            // Get the volume interface.
            IntPtr volumeObj;


            try
            {
                MediaFactory.GetService(_session, MRPolicyVolumeService, SimpleAudioVolumeGuid, out volumeObj);
            }
            catch (Exception e)
            {
                MediaFactory.GetService(_session, MRPolicyVolumeService, SimpleAudioVolumeGuid, out volumeObj);
            }


            _volumeController              = CppObject.FromPointer <SimpleAudioVolume>(volumeObj);
            _volumeController.Mute         = _isMuted;
            _volumeController.MasterVolume = _volume;

            // Get the clock.
            _clock = _session.Clock.QueryInterface <PresentationClock>();

            // Start playing.
            var varStart = new Variant();
            _session.Start(null, varStart);
#elif WINDOWS_PHONE
            Deployment.Current.Dispatcher.BeginInvoke(() =>
            {
                _mediaElement.Source = new Uri(song.FilePath, UriKind.Relative);
                _mediaElement.Play();

                // Ensure only one subscribe
                _mediaElement.MediaEnded -= OnSongFinishedPlaying;
                _mediaElement.MediaEnded += OnSongFinishedPlaying;
            });
#else
            song.SetEventHandler(OnSongFinishedPlaying);
            song.Volume = _isMuted ? 0.0f : _volume;
            song.Play();
#endif
            State = MediaState.Playing;
        }
        public unsafe Result SetDevice(IUnknown device)
        {
            IntPtr devicePtr = CppObject.ToCallbackPtr <SharpGen.Runtime.IUnknown>(device);
            Result result    = LocalInterop.CalliStdCallint(_nativePointer, devicePtr.ToPointer(), (*(void ***)_nativePointer)[3]);

            GC.KeepAlive(device);
            return(result);
        }
Ejemplo n.º 13
0
 internal unsafe MarkerNotification(RawNotification *rawNotification)
     : base(rawNotification)
 {
     CueIndex  = rawNotification->Data.Marker.CueIndex;
     SoundBank = CppObject.FromPointer <SoundBank>(rawNotification->Data.Marker.SoundBankPointer);
     Cue       = CppObject.FromPointer <Cue>(rawNotification->Data.Marker.CuePointer);
     Marker    = rawNotification->Data.Marker.Marker;
 }
 internal unsafe void __MarshalTo(ref __Native @ref)
 {
     @ref.RootSignature = CppObject.ToCallbackPtr <ID3D12RootSignature>(RootSignature);
     ComputeShader?.__MarshalTo(ref @ref.CS);
     @ref.NodeMask = NodeMask;
     CachedPSO.__MarshalTo(ref @ref.CachedPSO);
     @ref.Flags = Flags;
 }
Ejemplo n.º 15
0
        private void doTestOne()
        {
            // Basic test; create some objects, modify them, then exit
            CppObject    cpp = new CppObject();
            CSharpObject cs  = new CSharpObject();

            cpp.increment(4);
            cs.increment(5);
        }
Ejemplo n.º 16
0
        internal static unsafe Result D3D12CreateDeviceNoDevice(IUnknown adapter, FeatureLevel minFeatureLevel)
        {
            var adapterPtr = CppObject.ToCallbackPtr <IUnknown>(adapter);
            var riid       = typeof(ID3D12Device).GUID;
            var result     = D3D12CreateDevice_((void *)adapterPtr, (int)minFeatureLevel, &riid, null);

            GC.KeepAlive(adapter);
            return(result);
        }
Ejemplo n.º 17
0
        private static void OnTopologyReady()
        {
            IntPtr volumeObjectPtr;

            MediaFactory.GetService(_session, MediaServiceKeys.StreamVolume, AudioStreamVolumeGuid, out volumeObjectPtr);
            _volumeController = CppObject.FromPointer <AudioStreamVolume>(volumeObjectPtr);

            SetChannelVolumes();
        }
Ejemplo n.º 18
0
        /// <summary>
        /// Update D3DImage for WPF
        /// </summary>
        /// <param name="surface">Pointer to Surface from FFMpeg</param>
        /// <param name="surfaceIndex">Index to subsufaace from the current update</param>
        private void UpdateImage(IntPtr surface, uint surfaceIndex)
        {
            if (Shutdown == false)
            {
                if (surface != IntPtr.Zero)
                {
                    //Access ffmpeg d3d device
                    using (var ffmpegSrc = CppObject.FromPointer <Texture2D>(surface)) {
                        if (FFmpegD3DDevice == null)
                        {
                            FFmpegD3DDevice  = ffmpegSrc.Device;
                            FFmpegD3DContext = FFmpegD3DDevice.ImmediateContext;
                        }

                        //Need to Create a Transfer Texture on the ffmpeg device to allow shared access from wpf d3ddevice
                        //Only Create Once
                        if (TransferTexture == null)
                        {
                            var tempDesc = ffmpegSrc.Description;
                            tempDesc.ArraySize   = 1;
                            tempDesc.BindFlags  |= BindFlags.ShaderResource;
                            tempDesc.OptionFlags = ResourceOptionFlags.Shared;
                            tempDesc.Usage       = ResourceUsage.Default;

                            TransferTexture = new Texture2D(FFmpegD3DDevice, tempDesc);
                        }

                        ////Copy the ffmpegTexture to Transfer Texture, Also select the correct sub texture index to a single texture for transfer
                        FFmpegD3DContext.CopySubresourceRegion(ffmpegSrc, Convert.ToInt32(surfaceIndex), null, TransferTexture, 0);
                    }

                    //Hack to create D3D Device so we dont miss the first frame
                    if (D3DDevice == null)
                    {
                        RequestD3DRender();
                    }

                    if (D3DDevice != null && TransferTexture != null && DisplayTexture == null)
                    {
                        //Convert transfer texture thru to DXGI resource Object
                        using (var resource = TransferTexture.QueryInterface <SharpDX.DXGI.Resource>()) {
                            //Get shared handle from DXGI resource
                            using (var sharedResource = D3DDevice.OpenSharedResource <SharpDX.DXGI.Resource>(resource.SharedHandle)) {
                                //Convert back to Texture2D Object
                                using (var sharedTexture = sharedResource.QueryInterface <SharpDX.Direct3D11.Texture2D>()) {
                                    DisplayTexture = sharedResource.QueryInterface <SharpDX.Direct3D11.Texture2D>();
                                }
                            }
                        }
                    }

                    //Request a render evey time we have a new frame from FFMpeg
                    RequestD3DRender();
                }
            }
        }
Ejemplo n.º 19
0
        public void CanRoundTripCallThroughNativeVtblToManagedObject()
        {
            using var callback = new CallbackImpl();

            var callbackPtr    = CppObject.ToCallbackPtr <ICallback>(callback);
            var methodPtr      = Marshal.ReadIntPtr(Marshal.ReadIntPtr(callbackPtr));
            var delegateObject = Marshal.GetDelegateForFunctionPointer <CallbackShadow.CallbackVbtl.IncrementDelegate>(methodPtr);

            Assert.Equal(3, delegateObject(callbackPtr, 2));
        }
Ejemplo n.º 20
0
 internal unsafe VariableNotification(RawNotification *rawNotification)
     : base(rawNotification)
 {
     CueIndex      = rawNotification->Data.Variable.CueIndex;
     SoundBank     = CppObject.FromPointer <SoundBank>(rawNotification->Data.Variable.SoundBankPointer);
     Cue           = CppObject.FromPointer <Cue>(rawNotification->Data.Variable.CuePointer);
     VariableIndex = rawNotification->Data.Variable.VariableIndex;
     VariableValue = rawNotification->Data.Variable.VariableValue;
     IsLocal       = rawNotification->Data.Variable.Local;
 }
Ejemplo n.º 21
0
        /// <summary>
        /// Try to create new instance of <see cref="IDXGIFactory1"/>.
        /// </summary>
        /// <returns>Return an instance of <see cref="IDXGIFactory1"/> or null if failed.</returns>
        public static T CreateDXGIFactory1 <T>() where T : IDXGIFactory1
        {
            var result = CreateDXGIFactory1(typeof(T).GUID, out var nativePtr);

            if (result.Success)
            {
                return(CppObject.FromPointer <T>(nativePtr));
            }

            return(default);
Ejemplo n.º 22
0
 internal unsafe WaveNotification(RawNotification *rawNotification)
     : base(rawNotification)
 {
     WaveBank  = CppObject.FromPointer <WaveBank>(rawNotification->Data.Wave.WaveBankPointer);
     WaveIndex = rawNotification->Data.Wave.WaveIndex;
     CueIndex  = rawNotification->Data.Wave.CueIndex;
     SoundBank = CppObject.FromPointer <SoundBank>(rawNotification->Data.Wave.SoundBankPointer);
     Cue       = CppObject.FromPointer <Cue>(rawNotification->Data.Wave.CuePointer);
     Wave      = CppObject.FromPointer <Wave>(rawNotification->Data.Wave.WavePointer);
 }
        public void ResizeBackbuffer(IntPtr newBackbuffer)
        {
            // Unbind the old RTV and DSV from the pipeline by binding null
            // Then Dispose them
            immediateContext.OutputMerger.SetRenderTargets(null, renderTargetView: null);
            depthStencilView?.Dispose();
            depthBuffer?.Dispose();
            backbufferRTV?.Dispose();

            // Query to get to the DX11 version of the backbuffer
            // (The backbuffer is shared between DX9 and DX11)
            var surface      = CppObject.FromPointer <Surface>(newBackbuffer);
            var resourceDXGI = surface.QueryInterface <SharpDX.DXGI.Resource>();
            var sharedHandle = resourceDXGI.SharedHandle;

            resourceDXGI.Dispose();

            var resourceDX11 = device.OpenSharedResource <SharpDX.Direct3D11.Resource>(sharedHandle);
            var texture2D    = resourceDX11.QueryInterface <Texture2D>();

            resourceDX11.Dispose();

            // Create a new RTV
            var rtvDesc = new RenderTargetViewDescription {
                Format    = Format.B8G8R8A8_UNorm,
                Dimension = RenderTargetViewDimension.Texture2D,
                Texture2D = { MipSlice = 0 }
            };

            backbufferRTV = new RenderTargetView(device, texture2D, rtvDesc);


            // Create the new Depth buffer and bind it
            var texture2DDesc = texture2D.Description;

            immediateContext.Rasterizer.SetViewport(new Viewport(0, 0, texture2DDesc.Width, texture2DDesc.Height, 0.0f, 1.0f));
            depthBuffer = new Texture2D(device, new Texture2DDescription {
                Format            = Format.D32_Float,
                ArraySize         = 1,
                MipLevels         = 1,
                Width             = texture2DDesc.Width,
                Height            = texture2DDesc.Height,
                SampleDescription = new SampleDescription(1, 0),
                Usage             = ResourceUsage.Default,
                BindFlags         = BindFlags.DepthStencil,
                CpuAccessFlags    = CpuAccessFlags.None,
                OptionFlags       = ResourceOptionFlags.None
            });
            depthStencilView = new DepthStencilView(this.device, this.depthBuffer);
            immediateContext.OutputMerger.SetRenderTargets(depthStencilView, backbufferRTV);

            // Set up the viewport and projection matrix
            immediateContext.Rasterizer.SetViewport(0.0f, 0.0f, texture2DDesc.Width, texture2DDesc.Height);
            Camera.UpdateProjectionMatrix(texture2DDesc.Width, texture2DDesc.Height, 100000.0f, 1.0f);
        }
Ejemplo n.º 24
0
        private static IDisposable SetupTests(bool supportExceptions, out CallbackInterfaceNative nativeView, out ManagedImplementation target)
        {
            target     = supportExceptions ? new ExceptionEnabledManagedImplementation() : new ManagedImplementation();
            nativeView = new CallbackInterfaceNative(CppObject.ToCallbackPtr <CallbackInterface>(target));

            return(new CompositeDisposable
            {
                target,
                nativeView
            });
        }
Ejemplo n.º 25
0
        /// <summary>
        ///     指定された型のインターフェースを取得する。
        /// </summary>
        /// <typeparam name="T">希望する型。</typeparam>
        /// <param name="unknownObject">取得元のオブジェクト。</param>
        /// <returns>
        ///     取得されたオブジェクト。クエリに失敗した場合は null 。
        /// </returns>
        public static T QueryInterface <T>(this IUnknown unknownObject) where T : ComObject
        {
            if (null == unknownObject)
            {
                return(null);
            }

            var guid    = SharpDX.Utilities.GetGuidFromType(typeof(T));
            var resuult = unknownObject.QueryInterface(ref guid, out IntPtr comObject);

            return((resuult.Failure) ? null : CppObject.FromPointer <T>(comObject));
        }
Ejemplo n.º 26
0
        private void doTestTwo()
        {
            // Do the same as above, but show off alternative constructors
            CppObject    cpp = new CppObject(10);
            CSharpObject cs  = new CSharpObject(10000);

            cpp.increment(-5);
            cs.increment(-5000);

            // Also show off explicitly running destructor
            cpp.Dispose();
        }
Ejemplo n.º 27
0
        public PresentHook(CppObject swapChain)
        {
            _presentHook = new FunctionHook <Present>(
                VirtualTableAddress.GetVtableAddress(swapChain.NativePointer, (int)VirtualTableIndices.DxgiSwapChain.Present),
                new Present(OnPresent),
                this);

            // This is not the SwapChain we're looking for...
            swapChain.Dispose();

            _presentHook.Activate();
        }
Ejemplo n.º 28
0
            private static unsafe int LoadSource(IntPtr thisObject, void *param0, void *param1)
            {
                try
                {
                    IntPtr             filenameRef = (IntPtr)param0;
                    IDxcIncludeHandler @this       = (IDxcIncludeHandler)ToShadow <Vortice.Dxc.IDxcIncludeHandlerShadow>(thisObject).Callback;
                    string             fileName    = Marshal.PtrToStringUni(filenameRef);
                    Result             result      = @this.LoadSource(fileName, out IDxcBlob includeSource);

                    ref IntPtr includeSourceOut = ref Unsafe.AsRef <IntPtr>(param1);
                    includeSourceOut = CppObject.ToCallbackPtr <IDxcBlob>(includeSource);
                    return(result.Code);
                }
Ejemplo n.º 29
0
        private void OnTopologyReady()
        {
            if (_session.IsDisposed)
            {
                return;
            }

            // Get the volume interface.
            MediaFactory.GetService(_session, MediaServiceKeys.StreamVolume, AudioStreamVolumeGuid, out IntPtr volumeObjectPtr);
            _volumeController = CppObject.FromPointer <AudioStreamVolume>(volumeObjectPtr);

            SetChannelVolumes();
        }
Ejemplo n.º 30
0
        /// <summary>
        /// Try to create new instance of <see cref="IDXGIFactory1"/>.
        /// </summary>
        /// <param name="factory">The <see cref="IDXGIFactory1"/> being created.</param>
        /// <returns>Return the <see cref="Result"/>.</returns>
        public static Result CreateDXGIFactory1 <T>(out T factory) where T : IDXGIFactory1
        {
            var result = CreateDXGIFactory1(typeof(T).GUID, out var nativePtr);

            if (result.Success)
            {
                factory = CppObject.FromPointer <T>(nativePtr);
                return(result);
            }

            factory = null;
            return(result);
        }