/// <summary> /// Updates resources associated with a holographic camera's swap chain. /// The app does not access the swap chain directly, but it does create /// resource views for the back buffer. /// </summary> public void CreateResourcesForBackBuffer( DeviceResources deviceResources, ref HolographicCameraRenderingParameters cameraParameters ) { var device = deviceResources.D3DDevice; // Get the WinRT object representing the holographic camera's back buffer. IDirect3DSurface surface = cameraParameters.Direct3D11BackBuffer; // Get a DXGI interface for the holographic camera's back buffer. // Holographic cameras do not provide the DXGI swap chain, which is owned // by the system. The Direct3D back buffer resource is provided using WinRT // interop APIs. InteropStatics.IDirect3DDxgiInterfaceAccess surfaceDxgiInterfaceAccess = surface as InteropStatics.IDirect3DDxgiInterfaceAccess; IntPtr pResource = surfaceDxgiInterfaceAccess.GetInterface(InteropStatics.ID3D11Resource); // Determine if the back buffer has changed. If so, ensure that the render target view // is for the current back buffer. if ((null == d3dBackBuffer) || (d3dBackBuffer.NativePointer != pResource)) { // Clean up references to previous resources. this.RemoveAndDispose(ref d3dBackBuffer); this.RemoveAndDispose(ref d3dRenderTargetView); // This can change every frame as the system moves to the next buffer in the // swap chain. This mode of operation will occur when certain rendering modes // are activated. d3dBackBuffer = this.ToDispose(new SharpDX.Direct3D11.Texture2D(pResource)); // Create a render target view of the back buffer. // Creating this resource is inexpensive, and is better than keeping track of // the back buffers in order to pre-allocate render target views for each one. d3dRenderTargetView = this.ToDispose(new RenderTargetView(device, d3dBackBuffer)); // Get the DXGI format for the back buffer. // This information can be accessed by the app using CameraResources::GetBackBufferDXGIFormat(). Texture2DDescription backBufferDesc = BackBufferTexture2D.Description; dxgiFormat = backBufferDesc.Format; // Check for render target size changes. Size currentSize = holographicCamera.RenderTargetSize; if (d3dRenderTargetSize != currentSize) { // Set render target size. d3dRenderTargetSize = HolographicCamera.RenderTargetSize; // A new depth stencil view is also needed. this.RemoveAndDispose(ref d3dDepthStencilView); } } // We took a reference to a COM interface when we got the native pointer; here, we // release that reference. Marshal.Release(pResource); // Refresh depth stencil resources, if needed. if (null == DepthStencilView) { // Create a depth stencil view for use with 3D rendering if needed. var depthStencilDesc = new Texture2DDescription { Format = SharpDX.DXGI.Format.D16_UNorm, Width = (int)RenderTargetSize.Width, Height = (int)RenderTargetSize.Height, ArraySize = IsRenderingStereoscopic ? 2 : 1, // Create two textures when rendering in stereo. MipLevels = 1, // Use a single mipmap level. BindFlags = BindFlags.DepthStencil, SampleDescription = new SharpDX.DXGI.SampleDescription(1, 0) }; using (var depthStencil = new Texture2D(device, depthStencilDesc)) { var depthStencilViewDesc = new DepthStencilViewDescription(); depthStencilViewDesc.Dimension = IsRenderingStereoscopic ? DepthStencilViewDimension.Texture2DArray : DepthStencilViewDimension.Texture2D; depthStencilViewDesc.Texture2DArray.ArraySize = IsRenderingStereoscopic ? 2 : 0; d3dDepthStencilView = this.ToDispose(new DepthStencilView(device, depthStencil, depthStencilViewDesc)); } } // Create the constant buffer, if needed. if (null == viewProjectionConstantBuffer) { // Create a constant buffer to store view and projection matrices for the camera. ViewProjectionConstantBuffer viewProjectionConstantBufferData = new ViewProjectionConstantBuffer(); viewProjectionConstantBuffer = this.ToDispose(SharpDX.Direct3D11.Buffer.Create( device, BindFlags.ConstantBuffer, ref viewProjectionConstantBufferData)); } }
/// <summary> /// Updates the constant buffer for the display with view and projection /// matrices for the current frame. /// </summary> public void UpdateViewProjectionBuffer( DeviceResources deviceResources, HolographicCameraPose cameraPose, SpatialCoordinateSystem coordinateSystem ) { // The system changes the viewport on a per-frame basis for system optimizations. d3dViewport.X = (float)cameraPose.Viewport.Left; d3dViewport.Y = (float)cameraPose.Viewport.Top; d3dViewport.Width = (float)cameraPose.Viewport.Width; d3dViewport.Height = (float)cameraPose.Viewport.Height; d3dViewport.MinDepth = 0; d3dViewport.MaxDepth = 1; // The projection transform for each frame is provided by the HolographicCameraPose. HolographicStereoTransform cameraProjectionTransform = cameraPose.ProjectionTransform; // Get a container object with the view and projection matrices for the given // pose in the given coordinate system. HolographicStereoTransform?viewTransformContainer = cameraPose.TryGetViewTransform(coordinateSystem); // If TryGetViewTransform returns null, that means the pose and coordinate system // cannot be understood relative to one another; content cannot be rendered in this // coordinate system for the duration of the current frame. // This usually means that positional tracking is not active for the current frame, in // which case it is possible to use a SpatialLocatorAttachedFrameOfReference to render // content that is not world-locked instead. ViewProjectionConstantBuffer viewProjectionConstantBufferData = new ViewProjectionConstantBuffer(); bool viewTransformAcquired = viewTransformContainer.HasValue; if (viewTransformAcquired) { // Otherwise, the set of view transforms can be retrieved. HolographicStereoTransform viewCoordinateSystemTransform = viewTransformContainer.Value; // Update the view matrices. Holographic cameras (such as Microsoft HoloLens) are // constantly moving relative to the world. The view matrices need to be updated // every frame. viewProjectionConstantBufferData.viewProjectionLeft = Matrix4x4.Transpose( viewCoordinateSystemTransform.Left * cameraProjectionTransform.Left ); viewProjectionConstantBufferData.viewProjectionRight = Matrix4x4.Transpose( viewCoordinateSystemTransform.Right * cameraProjectionTransform.Right ); } // Use the D3D device context to update Direct3D device-based resources. var context = deviceResources.D3DDeviceContext; // Loading is asynchronous. Resources must be created before they can be updated. if (context == null || viewProjectionConstantBuffer == null || !viewTransformAcquired) { framePending = false; } else { // Update the view and projection matrices. context.UpdateSubresource(ref viewProjectionConstantBufferData, viewProjectionConstantBuffer); framePending = true; } }