Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
GraphicsPresenter.cs
Go to the documentation of this file.
1 // Copyright (c) 2014 Silicon Studio Corp. (http://siliconstudio.co.jp)
2 // This file is distributed under GPL v3. See LICENSE.md for details.
3 //
4 // Copyright (c) 2010-2013 SharpDX - Alexandre Mutel
5 //
6 // Permission is hereby granted, free of charge, to any person obtaining a copy
7 // of this software and associated documentation files (the "Software"), to deal
8 // in the Software without restriction, including without limitation the rights
9 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 // copies of the Software, and to permit persons to whom the Software is
11 // furnished to do so, subject to the following conditions:
12 //
13 // The above copyright notice and this permission notice shall be included in
14 // all copies or substantial portions of the Software.
15 //
16 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 // THE SOFTWARE.
23 
24 using System;
25 using SiliconStudio.Core;
26 using SiliconStudio.Core.ReferenceCounting;
27 
28 namespace SiliconStudio.Paradox.Graphics
29 {
30  /// <summary>
31  /// This class is a frontend to <see cref="SwapChain" /> and <see cref="SwapChain1" />.
32  /// </summary>
33  /// <remarks>
34  /// In order to create a new <see cref="GraphicsPresenter"/>, a <see cref="GraphicsDevice"/> should have been initialized first.
35  /// </remarks>
36  public abstract class GraphicsPresenter : ComponentBase
37  {
38  private DepthStencilBuffer depthStencilBuffer;
39 
40  /// <summary>
41  /// Initializes a new instance of the <see cref="GraphicsPresenter" /> class.
42  /// </summary>
43  /// <param name="device">The device.</param>
44  /// <param name="presentationParameters"> </param>
45  protected GraphicsPresenter(GraphicsDevice device, PresentationParameters presentationParameters)
46  {
47  GraphicsDevice = device.RootDevice;
48  Description = presentationParameters.Clone();
49 
50  DefaultViewport = new Viewport(0, 0, Description.BackBufferWidth, Description.BackBufferHeight);
51 
52  // Creates a default DepthStencilBuffer.
53  CreateDepthStencilBuffer();
54  }
55 
56  /// <summary>
57  /// Gets the graphics device.
58  /// </summary>
59  /// <value>The graphics device.</value>
60  public GraphicsDevice GraphicsDevice { get; private set; }
61 
62  /// <summary>
63  /// Gets the description of this presenter.
64  /// </summary>
65  public PresentationParameters Description { get; private set; }
66 
67  /// <summary>
68  /// Default viewport that covers the whole presenter surface.
69  /// </summary>
70  public Viewport DefaultViewport { get; protected set; }
71 
72  /// <summary>
73  /// Gets the default back buffer for this presenter.
74  /// </summary>
75  public abstract RenderTarget BackBuffer { get; }
76 
77  /// <summary>
78  /// Gets the default depth stencil buffer for this presenter.
79  /// </summary>
81  {
82  get
83  {
84  return depthStencilBuffer;
85  }
86 
87  protected set
88  {
89  depthStencilBuffer = value;
90  }
91  }
92 
93  /// <summary>
94  /// Gets the underlying native presenter (can be a <see cref="SharpDX.DXGI.SwapChain"/> or <see cref="SharpDX.DXGI.SwapChain1"/> or null, depending on the platform).
95  /// </summary>
96  /// <value>The native presenter.</value>
97  public abstract object NativePresenter { get; }
98 
99  /// <summary>
100  /// Gets or sets fullscreen mode for this presenter.
101  /// </summary>
102  /// <value><c>true</c> if this instance is full screen; otherwise, <c>false</c>.</value>
103  /// <remarks>This method is only valid on Windows Desktop and has no effect on Windows Metro.</remarks>
104  public abstract bool IsFullScreen { get; set; }
105 
106  /// <summary>
107  /// Gets or sets the <see cref="PresentInterval"/>. Default is to wait for one vertical blanking.
108  /// </summary>
109  /// <value>The present interval.</value>
111  {
112  get { return Description.PresentationInterval; }
113  set { Description.PresentationInterval = value; }
114  }
115 
116  /// <summary>
117  /// Presents the Backbuffer to the screen.
118  /// </summary>
119  public abstract void Present();
120 
121  /// <summary>
122  /// Resizes the current presenter, by resizing the back buffer and the depth stencil buffer.
123  /// </summary>
124  /// <param name="width"></param>
125  /// <param name="height"></param>
126  public void Resize(int width, int height, PixelFormat format)
127  {
128  Description.BackBufferWidth = width;
129  Description.BackBufferHeight = height;
130  Description.BackBufferFormat = format;
131 
132  DefaultViewport = new Viewport(0, 0, Description.BackBufferWidth, Description.BackBufferHeight);
133 
134  ResizeBackBuffer(width, height, format);
135  ResizeDepthStencilBuffer(width, height, format);
136  }
137 
138  protected abstract void ResizeBackBuffer(int width, int height, PixelFormat format);
139 
140  protected abstract void ResizeDepthStencilBuffer(int width, int height, PixelFormat format);
141 
143  {
144  if (DepthStencilBuffer != null)
145  {
146  depthStencilBuffer.Texture.ReleaseInternal();
147  depthStencilBuffer.RemoveKeepAliveBy(this);
148  }
149  }
150 
151  /// <summary>
152  /// Called when [destroyed].
153  /// </summary>
154  public virtual void OnDestroyed()
155  {
156  }
157 
158  /// <summary>
159  /// Called when [recreated].
160  /// </summary>
161  public virtual void OnRecreated()
162  {
163  }
164 
165  /// <summary>
166  /// Creates the depth stencil buffer.
167  /// </summary>
168  protected virtual void CreateDepthStencilBuffer()
169  {
170  // If no depth stencil buffer, just return
171  if (Description.DepthStencilFormat == PixelFormat.None)
172  return;
173 
174  // Creates the depth stencil buffer.
175  var depthTexture = Texture2D.New(GraphicsDevice, Description.BackBufferWidth, Description.BackBufferHeight, Description.DepthStencilFormat, TextureFlags.DepthStencil | TextureFlags.ShaderResource).KeepAliveBy(this);
176  DepthStencilBuffer = depthTexture.ToDepthStencilBuffer(false).KeepAliveBy(this);
177  }
178  }
179 }
void Resize(int width, int height, PixelFormat format)
Resizes the current presenter, by resizing the back buffer and the depth stencil buffer.
Base class for a framework component.
virtual void CreateDepthStencilBuffer()
Creates the depth stencil buffer.
Performs primitive-based rendering, creates resources, handles system-level variables, adjusts gamma ramp levels, and creates shaders. See The+GraphicsDevice+class to learn more about the class.
This class is a frontend to SwapChain and SwapChain1.
Defines the window dimensions of a render-target surface onto which a 3D volume projects.
Definition: Viewport.cs:14
GraphicsPresenter(GraphicsDevice device, PresentationParameters presentationParameters)
Initializes a new instance of the GraphicsPresenter class.
virtual void OnRecreated()
Called when [recreated].
PresentInterval
Defines flags that describe the relationship between the adapter refresh rate and the rate at which P...
virtual void OnDestroyed()
Called when [destroyed].
_In_ size_t _In_ size_t _In_ DXGI_FORMAT format
Definition: DirectXTexP.h:175
PixelFormat
Defines various types of pixel formats.
Definition: PixelFormat.cs:32
Describess how data will be displayed to the screen.