void Initialize() { ReleaseD3D(); HwndSource hwnd = new HwndSource(0, 0, 0, 0, 0, "test", IntPtr.Zero); pp = new PresentParameters(); pp.SwapEffect = SwapEffect.Discard; pp.DeviceWindowHandle = hwnd.Handle; pp.Windowed = true; pp.BackBufferWidth = (int)ActualWidth; pp.BackBufferHeight = (int)ActualHeight; pp.BackBufferFormat = Format.X8R8G8B8; //pp.Multisample = MultisampleType.EightSamples; device = new DeviceEx(Direct3D, 0, DeviceType.Hardware, hwnd.Handle, CreateFlags.HardwareVertexProcessing, pp); System.Windows.Media.CompositionTarget.Rendering += new EventHandler(CompositionTarget_Rendering); d3dimage.IsFrontBufferAvailableChanged += new DependencyPropertyChangedEventHandler(d3dimage_IsFrontBufferAvailableChanged); d3dimage.Lock(); d3dimage.SetBackBuffer(D3DResourceType.IDirect3DSurface9, device.GetBackBuffer(0, 0).ComPointer); d3dimage.Unlock(); }
private void ResizeRendering() { if (_texture != null) { _texture.Dispose(); } if (_renderBuffer != null) { _renderBuffer.Dispose(); } if (_backBuffer != null) { _backBuffer.Dispose(); } if (_device != null) { _device.Dispose(); } double NewWidth = ActualWidth; double NewHeight = ActualHeight; PresentParameters pp = new PresentParameters(); pp.DeviceWindowHandle = _hwnd; pp.Windowed = true; pp.SwapEffect = SwapEffect.Flip; pp.PresentationInterval = PresentInterval.Default; pp.BackBufferWidth = (int)NewWidth; pp.BackBufferHeight = (int)NewHeight; pp.BackBufferFormat = Format.X8R8G8B8; pp.BackBufferCount = 1; _device = new DeviceEx(_d3dex, 0, DeviceType.Hardware, _hwnd, CreateFlags.HardwareVertexProcessing | CreateFlags.PureDevice | CreateFlags.FpuPreserve | CreateFlags.Multithreaded, pp); _texture = new Texture(_device, (int)ActualWidth, (int)ActualHeight, 1, Usage.None, Format.X8R8G8B8, Pool.Default); _renderBuffer = _texture.GetSurfaceLevel(0); d3dimg.Lock(); _backBuffer = _device.GetBackBuffer(0, 0); d3dimg.SetBackBuffer(D3DResourceType.IDirect3DSurface9, _backBuffer.ComPointer); d3dimg.Unlock(); }
/// <summary> /// Change the size of the rendering. /// </summary> private void ResizeRendering() { if (_gl.IsTexture(_glRenderBufferName) == 1) { _gl.DeleteTextures(1, new uint[] { _glRenderBufferName }); } if (_gl.IsFramebufferEXT(_framebuffer)) { _gl.DeleteFramebuffersEXT(1, new uint[] { _framebuffer }); } uint[] names = new uint[1]; _gl.GenTextures(1, names); _glRenderBufferName = names[0]; _gl.GenFramebuffersEXT(1, names); _framebuffer = names[0]; if (_texture != null) { _gl.DXUnregisterObjectNV(_dxDeviceGLHandle, _glRenderBufferHandle); _texture.Dispose(); } if (_renderBuffer != null) { _renderBuffer.Dispose(); } if (_backBuffer != null) { _backBuffer.Dispose(); } if (_device != null) { _device.Dispose(); } if (_dxDeviceGLHandle != IntPtr.Zero) { _gl.DXCloseDeviceNV(_dxDeviceGLHandle); } PresentParameters pp = new PresentParameters(); pp.DeviceWindowHandle = _hwnd; pp.Windowed = true; pp.SwapEffect = SwapEffect.Flip; pp.PresentationInterval = PresentInterval.Default; pp.BackBufferWidth = (int)ActualWidth; pp.BackBufferHeight = (int)ActualHeight; pp.BackBufferFormat = Format.X8R8G8B8; pp.BackBufferCount = 1; _device = new DeviceEx(_d3dex, 0, DeviceType.Hardware, _hwnd, CreateFlags.HardwareVertexProcessing | CreateFlags.PureDevice | CreateFlags.FpuPreserve | CreateFlags.Multithreaded, pp); _gl.MakeCurrent(); _dxDeviceGLHandle = _gl.DXOpenDeviceNV(_device.ComPointer); _texture = new Texture(_device, (int)ActualWidth, (int)ActualHeight, 1, Usage.None, Format.X8R8G8B8, Pool.Default); _renderBuffer = _texture.GetSurfaceLevel(0); _glRenderBufferHandle = _gl.DXRegisterObjectNV(_dxDeviceGLHandle, _texture.ComPointer, _glRenderBufferName, OpenGL.GL_TEXTURE_2D, OpenGL.WGL_ACCESS_READ_WRITE_NV); _gl.DXLockObjectsNV(_dxDeviceGLHandle, new IntPtr[] { _glRenderBufferHandle }); _gl.BindFramebufferEXT(OpenGL.GL_FRAMEBUFFER_EXT, _framebuffer); _gl.FramebufferTexture2DEXT(OpenGL.GL_FRAMEBUFFER_EXT, OpenGL.GL_COLOR_ATTACHMENT0_EXT, OpenGL.GL_TEXTURE_2D, _glRenderBufferName, 0); uint status = _gl.CheckFramebufferStatusEXT(OpenGL.GL_FRAMEBUFFER_EXT); switch (status) { case OpenGL.GL_FRAMEBUFFER_COMPLETE_EXT: break; case OpenGL.GL_FRAMEBUFFER_UNSUPPORTED_EXT: throw new InvalidOperationException("Not supported framebuffer-format!"); default: throw new InvalidOperationException(status.ToString()); } _gl.SetDimensions((int)ActualWidth, (int)ActualHeight); _gl.Viewport(0, 0, (int)ActualWidth, (int)ActualHeight); _gl.MatrixMode(OpenGL.GL_PROJECTION); _gl.LoadIdentity(); _gl.Ortho(0, ActualWidth, ActualHeight, 0, 0, 1); _gl.MatrixMode(OpenGL.GL_MODELVIEW); _gl.LoadIdentity(); // Pixel-position correction, look there: // http://msdn.microsoft.com/en-us/library/windows/desktop/dd374282(v=vs.85).aspx _gl.Translate(0.375, 0.375, 0); _gl.DXUnlockObjectsNV(_dxDeviceGLHandle, new IntPtr[] { _glRenderBufferHandle }); _image.Lock(); _backBuffer = _device.GetBackBuffer(0, 0); _image.SetBackBuffer(D3DResourceType.IDirect3DSurface9, _backBuffer.ComPointer); _image.Unlock(); }
void Initialize() { ReleaseD3D(); HwndSource hwnd = new HwndSource(0, 0, 0, 0, 0, "test", IntPtr.Zero); pp = new PresentParameters(); pp.SwapEffect = SwapEffect.Discard; pp.DeviceWindowHandle = hwnd.Handle; pp.Windowed = true; pp.BackBufferWidth = (int)ActualWidth; pp.BackBufferHeight = (int)ActualHeight; pp.BackBufferFormat = Format.X8R8G8B8; //pp.Multisample = MultisampleType.EightSamples; device = new DeviceEx(Direct3D, 0, DeviceType.Hardware, hwnd.Handle, CreateFlags.HardwareVertexProcessing, pp); System.Windows.Media.CompositionTarget.Rendering += new EventHandler(CompositionTarget_Rendering); d3dimage.IsFrontBufferAvailableChanged += new DependencyPropertyChangedEventHandler(d3dimage_IsFrontBufferAvailableChanged); d3dimage.Lock(); d3dimage.SetBackBuffer(D3DResourceType.IDirect3DSurface9, device.GetBackBuffer(0, 0).ComPointer); d3dimage.Unlock(); }
/// <summary> /// Change the size of the rendering. /// </summary> private void ResizeRendering() { if (_gl.IsTexture(_glRenderBufferName) == 1) _gl.DeleteTextures(1, new uint[] { _glRenderBufferName }); if (_gl.IsFramebufferEXT(_framebuffer)) _gl.DeleteFramebuffersEXT(1, new uint[] { _framebuffer }); uint[] names = new uint[1]; _gl.GenTextures(1, names); _glRenderBufferName = names[0]; _gl.GenFramebuffersEXT(1, names); _framebuffer = names[0]; if (_texture != null) { _gl.DXUnregisterObjectNV(_dxDeviceGLHandle, _glRenderBufferHandle); _texture.Dispose(); } if (_renderBuffer != null) _renderBuffer.Dispose(); if (_backBuffer != null) _backBuffer.Dispose(); if (_device != null) _device.Dispose(); if (_dxDeviceGLHandle != IntPtr.Zero) _gl.DXCloseDeviceNV(_dxDeviceGLHandle); PresentParameters pp = new PresentParameters(); pp.DeviceWindowHandle = _hwnd; pp.Windowed = true; pp.SwapEffect = SwapEffect.Flip; pp.PresentationInterval = PresentInterval.Default; pp.BackBufferWidth = (int)ActualWidth; pp.BackBufferHeight = (int)ActualHeight; pp.BackBufferFormat = Format.X8R8G8B8; pp.BackBufferCount = 1; _device = new DeviceEx(_d3dex, 0, DeviceType.Hardware, _hwnd, CreateFlags.HardwareVertexProcessing | CreateFlags.PureDevice | CreateFlags.FpuPreserve | CreateFlags.Multithreaded, pp); _gl.MakeCurrent(); _dxDeviceGLHandle = _gl.DXOpenDeviceNV(_device.ComPointer); _texture = new Texture(_device, (int)ActualWidth, (int)ActualHeight, 1, Usage.None, Format.X8R8G8B8, Pool.Default); _renderBuffer = _texture.GetSurfaceLevel(0); _glRenderBufferHandle = _gl.DXRegisterObjectNV(_dxDeviceGLHandle, _texture.ComPointer, _glRenderBufferName, OpenGL.GL_TEXTURE_2D, OpenGL.WGL_ACCESS_READ_WRITE_NV); _gl.DXLockObjectsNV(_dxDeviceGLHandle, new IntPtr[] { _glRenderBufferHandle }); _gl.BindFramebufferEXT(OpenGL.GL_FRAMEBUFFER_EXT, _framebuffer); _gl.FramebufferTexture2DEXT(OpenGL.GL_FRAMEBUFFER_EXT, OpenGL.GL_COLOR_ATTACHMENT0_EXT, OpenGL.GL_TEXTURE_2D, _glRenderBufferName, 0); uint status = _gl.CheckFramebufferStatusEXT(OpenGL.GL_FRAMEBUFFER_EXT); switch (status) { case OpenGL.GL_FRAMEBUFFER_COMPLETE_EXT: break; case OpenGL.GL_FRAMEBUFFER_UNSUPPORTED_EXT: throw new InvalidOperationException("Not supported framebuffer-format!"); default: throw new InvalidOperationException(status.ToString()); } _gl.SetDimensions((int)ActualWidth, (int)ActualHeight); _gl.Viewport(0, 0, (int)ActualWidth, (int)ActualHeight); _gl.MatrixMode(OpenGL.GL_PROJECTION); _gl.LoadIdentity(); _gl.Ortho(0, ActualWidth, ActualHeight, 0, 0, 1); _gl.MatrixMode(OpenGL.GL_MODELVIEW); _gl.LoadIdentity(); // Pixel-position correction, look there: // http://msdn.microsoft.com/en-us/library/windows/desktop/dd374282(v=vs.85).aspx _gl.Translate(0.375, 0.375, 0); _gl.DXUnlockObjectsNV(_dxDeviceGLHandle, new IntPtr[] { _glRenderBufferHandle }); _image.Lock(); _backBuffer = _device.GetBackBuffer(0, 0); _image.SetBackBuffer(D3DResourceType.IDirect3DSurface9, _backBuffer.ComPointer); _image.Unlock(); }
private bool InitializeD3D() { HwndSource hwnd = new HwndSource(0, 0, 0, 0, 0, "null", IntPtr.Zero); // Create the D3D object. d3d = new Direct3DEx(); PresentParameters pp = new PresentParameters(); pp.BackBufferWidth = 512; pp.BackBufferHeight = 512; pp.BackBufferFormat = Format.Unknown; pp.BackBufferCount = 0; pp.Multisample = MultisampleType.None; pp.MultisampleQuality = 0; pp.SwapEffect = SwapEffect.Discard; pp.DeviceWindowHandle = (IntPtr)0; pp.Windowed = true; pp.EnableAutoDepthStencil = false; pp.AutoDepthStencilFormat = Format.Unknown; pp.PresentationInterval = PresentInterval.Default; bDeviceFound = false; CUdevice[] cudaDevices = null; for (g_iAdapter = 0; g_iAdapter < d3d.AdapterCount; g_iAdapter++) { device = new DeviceEx(d3d, d3d.Adapters[g_iAdapter].Adapter, DeviceType.Hardware, hwnd.Handle, CreateFlags.HardwareVertexProcessing | CreateFlags.Multithreaded, pp); try { cudaDevices = CudaContext.GetDirectXDevices(device.ComPointer, CUd3dXDeviceList.All, CudaContext.DirectXVersion.D3D9); bDeviceFound = cudaDevices.Length > 0; infoLog.AppendText("> Display Device #" + d3d.Adapters[g_iAdapter].Adapter + ": \"" + d3d.Adapters[g_iAdapter].Details.Description + "\" supports Direct3D9 and CUDA.\n"); break; } catch (CudaException) { //No Cuda device found for this Direct3D9 device infoLog.AppendText("> Display Device #" + d3d.Adapters[g_iAdapter].Adapter + ": \"" + d3d.Adapters[g_iAdapter].Details.Description + "\" supports Direct3D9 but not CUDA.\n"); } } // we check to make sure we have found a cuda-compatible D3D device to work on if (!bDeviceFound) { infoLog.AppendText("No CUDA-compatible Direct3D9 device available"); if (device != null) { device.Dispose(); } return(false); } ctx = new CudaContext(cudaDevices[0], device.ComPointer, CUCtxFlags.BlockingSync, CudaContext.DirectXVersion.D3D9); deviceName.Text = "Device name: " + ctx.GetDeviceName(); // Set projection matrix SlimDX.Matrix matProj = SlimDX.Matrix.OrthoOffCenterLH(0, 1, 1, 0, 0, 1); device.SetTransform(TransformState.Projection, matProj); // Turn off D3D lighting, since we are providing our own vertex colors device.SetRenderState(RenderState.Lighting, false); //Load kernels CUmodule module = ctx.LoadModulePTX("kernel.ptx"); addForces_k = new CudaKernel("addForces_k", module, ctx); advectVelocity_k = new CudaKernel("advectVelocity_k", module, ctx); diffuseProject_k = new CudaKernel("diffuseProject_k", module, ctx); updateVelocity_k = new CudaKernel("updateVelocity_k", module, ctx); advectParticles_k = new CudaKernel("advectParticles_k", module, ctx); d3dimage.Lock(); Surface surf = device.GetBackBuffer(0, 0); d3dimage.SetBackBuffer(D3DResourceType.IDirect3DSurface9, surf.ComPointer); d3dimage.Unlock(); surf.Dispose(); //Setup the "real" frame rate counter. //The cuda counter only measures cuda runtime, not the overhead to actually //show the result via DirectX and WPF. realLastTick = Environment.TickCount; return(true); }
private bool InitializeD3D() { HwndSource hwnd = new HwndSource(0, 0, 0, 0, 0, "null", IntPtr.Zero); // Create the D3D object. d3d = new Direct3DEx(); PresentParameters pp = new PresentParameters(); pp.BackBufferWidth = 512; pp.BackBufferHeight = 512; pp.BackBufferFormat = Format.Unknown; pp.BackBufferCount = 0; pp.Multisample = MultisampleType.None; pp.MultisampleQuality = 0; pp.SwapEffect = SwapEffect.Discard; pp.DeviceWindowHandle = (IntPtr)0; pp.Windowed = true; pp.EnableAutoDepthStencil = false; pp.AutoDepthStencilFormat = Format.Unknown; pp.PresentationInterval = PresentInterval.Default; bDeviceFound = false; CUdevice[] cudaDevices = null; for (g_iAdapter = 0; g_iAdapter < d3d.AdapterCount; g_iAdapter++) { device = new DeviceEx(d3d, d3d.Adapters[g_iAdapter].Adapter, DeviceType.Hardware, hwnd.Handle, CreateFlags.HardwareVertexProcessing | CreateFlags.Multithreaded, pp); try { cudaDevices = CudaContext.GetDirectXDevices(device.ComPointer, CUd3dXDeviceList.All, CudaContext.DirectXVersion.D3D9); bDeviceFound = cudaDevices.Length > 0; infoLog.AppendText("> Display Device #" + d3d.Adapters[g_iAdapter].Adapter + ": \"" + d3d.Adapters[g_iAdapter].Details.Description + "\" supports Direct3D9 and CUDA.\n"); break; } catch (CudaException) { //No Cuda device found for this Direct3D9 device infoLog.AppendText("> Display Device #" + d3d.Adapters[g_iAdapter].Adapter + ": \"" + d3d.Adapters[g_iAdapter].Details.Description + "\" supports Direct3D9 but not CUDA.\n"); } } // we check to make sure we have found a cuda-compatible D3D device to work on if (!bDeviceFound) { infoLog.AppendText("No CUDA-compatible Direct3D9 device available"); if (device != null) device.Dispose(); return false; } ctx = new CudaContext(cudaDevices[0], device.ComPointer, CUCtxFlags.BlockingSync, CudaContext.DirectXVersion.D3D9); deviceName.Text = "Device name: " + ctx.GetDeviceName(); // Set projection matrix SlimDX.Matrix matProj = SlimDX.Matrix.OrthoOffCenterLH(0, 1, 1, 0, 0, 1); device.SetTransform(TransformState.Projection, matProj); // Turn off D3D lighting, since we are providing our own vertex colors device.SetRenderState(RenderState.Lighting, false); //Load kernels CUmodule module = ctx.LoadModulePTX("kernel.ptx"); addForces_k = new CudaKernel("addForces_k", module, ctx); advectVelocity_k = new CudaKernel("advectVelocity_k", module, ctx); diffuseProject_k = new CudaKernel("diffuseProject_k", module, ctx); updateVelocity_k = new CudaKernel("updateVelocity_k", module, ctx); advectParticles_k = new CudaKernel("advectParticles_k", module, ctx); d3dimage.Lock(); Surface surf = device.GetBackBuffer(0, 0); d3dimage.SetBackBuffer(D3DResourceType.IDirect3DSurface9, surf.ComPointer); d3dimage.Unlock(); surf.Dispose(); //Setup the "real" frame rate counter. //The cuda counter only measures cuda runtime, not the overhead to actually //show the result via DirectX and WPF. realLastTick = Environment.TickCount; return true; }