示例#1
0
        public int InitializeDevice(IntPtr dwUserID, ref VMR9AllocationInfo lpAllocInfo, ref int lpNumBuffers)
        {
            int width = 1;
            int height = 1;
            float fTU = 1.0f;
            float fTV = 1.0f;

            if (vmrSurfaceAllocatorNotify == null)
            {
                return E_FAIL;
            }

            int hr = 0;

            try
            {
                IntPtr unmanagedDevice = device.GetObjectByValue(DxMagicNumber);
                IntPtr hMonitor = Manager.GetAdapterMonitor(adapterInfo.Adapter);

                hr = vmrSurfaceAllocatorNotify.SetD3DDevice(unmanagedDevice, hMonitor);
                DsError.ThrowExceptionForHR(hr);

                if (device.DeviceCaps.TextureCaps.SupportsPower2)
                {
                    while (width < lpAllocInfo.dwWidth)
                        width = width << 1;
                    while (height < lpAllocInfo.dwHeight)
                        height = height << 1;

                    fTU = (float)(lpAllocInfo.dwWidth) / (float)(width);
                    fTV = (float)(lpAllocInfo.dwHeight) / (float)(height);
                    scene.SetSrcRect(fTU, fTV);

                    lpAllocInfo.dwWidth = width;
                    lpAllocInfo.dwHeight = height;
                }

                // NOTE:
                // we need to make sure that we create textures because
                // surfaces can not be textured onto a primitive.
                lpAllocInfo.dwFlags |= VMR9SurfaceAllocationFlags.TextureSurface;

                DeleteSurfaces();

                unmanagedSurfaces = new IntPtr[lpNumBuffers];

                hr = vmrSurfaceAllocatorNotify.AllocateSurfaceHelper(ref lpAllocInfo, ref lpNumBuffers, unmanagedSurfaces);

                // If we couldn't create a texture surface and
                // the format is not an alpha format,
                // then we probably cannot create a texture.
                // So what we need to do is create a private texture
                // and copy the decoded images onto it.
                if (hr < 0)
                {
                    DeleteSurfaces();

                    FourCC fcc = new FourCC("0000");

                    // is surface YUV ?
                    if (lpAllocInfo.Format > fcc.ToInt32())
                    {
                        // create the private texture
                        privateTexture = new Texture(
                            device,
                            lpAllocInfo.dwWidth,
                            lpAllocInfo.dwHeight,
                            1,
                            Usage.RenderTarget,
                            adapterInfo.CurrentDisplayMode.Format,
                            Pool.Default
                            );

                        privateSurface = privateTexture.GetSurfaceLevel(0);
                    }

                    lpAllocInfo.dwFlags &= ~VMR9SurfaceAllocationFlags.TextureSurface;
                    lpAllocInfo.dwFlags |= VMR9SurfaceAllocationFlags.OffscreenSurface;

                    unmanagedSurfaces = new IntPtr[lpNumBuffers];

                    hr = vmrSurfaceAllocatorNotify.AllocateSurfaceHelper(ref lpAllocInfo, ref lpNumBuffers, unmanagedSurfaces);
                    if (hr < 0)
                        return hr;
                }
                else
                {
                    surfaces = new Hashtable(unmanagedSurfaces.Length);
                    textures = new Hashtable(unmanagedSurfaces.Length);

                    for (int i = 0; i < lpNumBuffers; i++)
                    {
                        Surface surf = new Surface(unmanagedSurfaces[i]);
                        Texture text = (Texture) surf.GetContainer(new Guid("85C31227-3DE5-4f00-9B3A-F11AC38C18B5"));
                        surfaces.Add(unmanagedSurfaces[i], surf);
                        textures.Add(unmanagedSurfaces[i], text);
                    }
                }

                return scene.Init(device);
            }
            catch(DirectXException e)
            {
                return e.ErrorCode;
            }
            catch
            {
                return E_FAIL;
            }
        }
示例#2
0
        public int InitializeDevice(IntPtr dwUserID, ref VMR9AllocationInfo lpAllocInfo, ref int lpNumBuffers)
        {
            int   width  = 1;
            int   height = 1;
            float fTU    = 1.0f;
            float fTV    = 1.0f;

            if (vmrSurfaceAllocatorNotify == null)
            {
                return(E_FAIL);
            }

            int hr = 0;

            try
            {
                IntPtr unmanagedDevice = device.GetObjectByValue(DxMagicNumber);
                IntPtr hMonitor        = Manager.GetAdapterMonitor(adapterInfo.Adapter);

                hr = vmrSurfaceAllocatorNotify.SetD3DDevice(unmanagedDevice, hMonitor);
                DsError.ThrowExceptionForHR(hr);

                if (device.DeviceCaps.TextureCaps.SupportsPower2)
                {
                    while (width < lpAllocInfo.dwWidth)
                    {
                        width = width << 1;
                    }
                    while (height < lpAllocInfo.dwHeight)
                    {
                        height = height << 1;
                    }

                    fTU = (float)(lpAllocInfo.dwWidth) / (float)(width);
                    fTV = (float)(lpAllocInfo.dwHeight) / (float)(height);
                    scene.SetSrcRect(fTU, fTV);

                    lpAllocInfo.dwWidth  = width;
                    lpAllocInfo.dwHeight = height;
                }

                // NOTE:
                // we need to make sure that we create textures because
                // surfaces can not be textured onto a primitive.
                lpAllocInfo.dwFlags |= VMR9SurfaceAllocationFlags.TextureSurface;

                DeleteSurfaces();

                unmanagedSurfaces = new IntPtr[lpNumBuffers];

                hr = vmrSurfaceAllocatorNotify.AllocateSurfaceHelper(ref lpAllocInfo, ref lpNumBuffers, unmanagedSurfaces);

                // If we couldn't create a texture surface and
                // the format is not an alpha format,
                // then we probably cannot create a texture.
                // So what we need to do is create a private texture
                // and copy the decoded images onto it.
                if (hr < 0)
                {
                    DeleteSurfaces();

                    FourCC fcc = new FourCC("0000");

                    // is surface YUV ?
                    if (lpAllocInfo.Format > fcc.ToInt32())
                    {
                        // create the private texture
                        privateTexture = new Texture(
                            device,
                            lpAllocInfo.dwWidth,
                            lpAllocInfo.dwHeight,
                            1,
                            Usage.RenderTarget,
                            adapterInfo.CurrentDisplayMode.Format,
                            Pool.Default
                            );

                        privateSurface = privateTexture.GetSurfaceLevel(0);
                    }

                    lpAllocInfo.dwFlags &= ~VMR9SurfaceAllocationFlags.TextureSurface;
                    lpAllocInfo.dwFlags |= VMR9SurfaceAllocationFlags.OffscreenSurface;

                    unmanagedSurfaces = new IntPtr[lpNumBuffers];

                    hr = vmrSurfaceAllocatorNotify.AllocateSurfaceHelper(ref lpAllocInfo, ref lpNumBuffers, unmanagedSurfaces);
                    if (hr < 0)
                    {
                        return(hr);
                    }
                }
                else
                {
                    surfaces = new Hashtable(unmanagedSurfaces.Length);
                    textures = new Hashtable(unmanagedSurfaces.Length);

                    for (int i = 0; i < lpNumBuffers; i++)
                    {
                        Surface surf = new Surface(unmanagedSurfaces[i]);
                        Texture text = (Texture)surf.GetContainer(new Guid("85C31227-3DE5-4f00-9B3A-F11AC38C18B5"));
                        surfaces.Add(unmanagedSurfaces[i], surf);
                        textures.Add(unmanagedSurfaces[i], text);
                    }
                }

                return(scene.Init(device));
            }
            catch (DirectXException e)
            {
                return(e.ErrorCode);
            }
            catch
            {
                return(E_FAIL);
            }
        }