Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
PrimitiveQuad.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 using System;
4 using SiliconStudio.Core;
5 using SiliconStudio.Core.Mathematics;
6 using SiliconStudio.Paradox.Effects.Modules;
7 
8 namespace SiliconStudio.Paradox.Graphics
9 {
10  /// <summary>
11  /// Primitive quad use to draw an effect on a quad (fullscreen by default). This is directly accessible from the <see cref="GraphicsDevice.DrawQuad"/> method.
12  /// </summary>
14  {
15  private readonly Effect simpleEffect;
16  private readonly SharedData sharedData;
17  private const int QuadCount = 3;
18 
19  /// <summary>
20  /// Initializes a new instance of the <see cref="PrimitiveQuad" /> class with a <see cref="SimpleEffect"/>.
21  /// </summary>
22  /// <param name="graphicsDevice">The graphics device.</param>
23  public PrimitiveQuad(GraphicsDevice graphicsDevice) : this(graphicsDevice, new SimpleEffect(graphicsDevice))
24  {
25  }
26 
27  /// <summary>
28  /// Initializes a new instance of the <see cref="PrimitiveQuad" /> class with a particular effect.
29  /// </summary>
30  /// <param name="graphicsDevice">The graphics device.</param>
31  /// <param name="effect">The effect.</param>
32  public PrimitiveQuad(GraphicsDevice graphicsDevice, Effect effect)
33  {
34  GraphicsDevice = graphicsDevice;
35  simpleEffect = effect;
36  simpleEffect.Parameters.Set(SpriteBaseKeys.MatrixTransform, Matrix.Identity);
37  sharedData = GraphicsDevice.GetOrCreateSharedData(GraphicsDeviceSharedDataType.PerDevice, "PrimitiveQuad::VertexBuffer", () => new SharedData(GraphicsDevice, simpleEffect.InputSignature));
38  }
39 
40  /// <summary>
41  /// Gets the graphics device.
42  /// </summary>
43  /// <value>The graphics device.</value>
44  public GraphicsDevice GraphicsDevice { get; private set; }
45 
46  /// <summary>
47  /// Draws a quad. The effect must have been applied before calling this method with pixel shader having the signature float2:TEXCOORD.
48  /// </summary>
49  public void Draw()
50  {
51  GraphicsDevice.SetVertexArrayObject(sharedData.VertexBuffer);
52  GraphicsDevice.Draw(PrimitiveType.TriangleList, QuadCount);
53  GraphicsDevice.SetVertexArrayObject(null);
54  }
55 
56  /// <summary>
57  /// Draws a quad with a texture. This Draw method is using the current effect bound to this instance.
58  /// </summary>
59  /// <param name="texture">The texture.</param>
60  public void Draw(Texture texture)
61  {
62  Draw(texture, null, Color.White);
63  }
64 
65  /// <summary>
66  /// Draws a quad with a texture. This Draw method is using a simple pixel shader that is sampling the texture.
67  /// </summary>
68  /// <param name="texture">The texture to draw.</param>
69  /// <param name="samplerState">State of the sampler. If null, default sampler is <see cref="SamplerStateFactory.LinearClamp" />.</param>
70  /// <param name="color">The color.</param>
71  /// <exception cref="System.ArgumentException">Expecting a Texture2D;texture</exception>
72  public void Draw(Texture texture, SamplerState samplerState, Color4 color)
73  {
74  var texture2D = texture as Texture2D;
75  if (texture2D == null) throw new ArgumentException("Expecting a Texture2D", "texture");
76 
77  // Make sure that we are using our vertex shader
78  simpleEffect.Parameters.Set(SpriteEffectKeys.Color, color);
79  simpleEffect.Parameters.Set(TexturingKeys.Texture0, texture as Texture2D);
80  simpleEffect.Parameters.Set(TexturingKeys.Sampler, samplerState ?? GraphicsDevice.SamplerStates.LinearClamp);
81  simpleEffect.Apply();
82  Draw();
83 
84  // TODO ADD QUICK UNBIND FOR SRV
85  //GraphicsDevice.Context.PixelShader.SetShaderResource(0, null);
86  }
87 
88  /// <summary>
89  /// Internal structure used to store VertexBuffer and VertexInputLayout.
90  /// </summary>
91  private class SharedData : ComponentBase
92  {
93  /// <summary>
94  /// The vertex buffer
95  /// </summary>
96  public readonly VertexArrayObject VertexBuffer;
97 
98  private static readonly VertexPositionTexture[] QuadsVertices = new []
99  {
100  new VertexPositionTexture(new Vector3(-1, 1, 0), new Vector2(0, 0)),
101  new VertexPositionTexture(new Vector3( 3, 1, 0), new Vector2(2, 0)),
102  new VertexPositionTexture(new Vector3(-1,-3, 0), new Vector2(0, 2)),
103  };
104 
105  public SharedData(GraphicsDevice device, EffectInputSignature defaultSignature)
106  {
107  var vertexBuffer = Buffer.Vertex.New(device, QuadsVertices).DisposeBy(this);
108 
109  // Register reload
110  vertexBuffer.Reload = (graphicsResource) => ((Buffer)graphicsResource).Recreate(QuadsVertices);
111 
112  VertexBuffer = VertexArrayObject.New(device, defaultSignature, new VertexBufferBinding(vertexBuffer, VertexPositionTexture.Layout, QuadsVertices.Length, VertexPositionTexture.Size)).DisposeBy(this);
113  }
114  }
115  }
116 }
SiliconStudio.Paradox.Games.Mathematics.Vector2 Vector2
PrimitiveQuad(GraphicsDevice graphicsDevice)
Initializes a new instance of the PrimitiveQuad class with a SimpleEffect.
Primitive quad use to draw an effect on a quad (fullscreen by default). This is directly accessible f...
Base class for a framework component.
Represents a color in the form of rgba.
Definition: Color4.cs:42
SiliconStudio.Paradox.Graphics.Buffer Buffer
Definition: BasicEffect.cs:15
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.
Describes a custom vertex format structure that contains position and color information.
PrimitiveQuad(GraphicsDevice graphicsDevice, Effect effect)
Initializes a new instance of the PrimitiveQuad class with a particular effect.
SiliconStudio.Core.Mathematics.Color Color
Definition: ColorPicker.cs:14
Represents a 32-bit color (4 bytes) in the form of RGBA (in byte order: R, G, B, A).
Definition: Color.cs:16
A Texture 2D frontend to SharpDX.Direct3D11.Texture2D.
Definition: Texture2D.cs:37
void Draw()
Draws a quad. The effect must have been applied before calling this method with pixel shader having t...
void Draw(Texture texture, SamplerState samplerState, Color4 color)
Draws a quad with a texture. This Draw method is using a simple pixel shader that is sampling the tex...
void Draw(Texture texture)
Draws a quad with a texture. This Draw method is using the current effect bound to this instance...
SiliconStudio.Core.Mathematics.Vector3 Vector3
static readonly Color White
White color.
Base class for texture resources.
Definition: Texture.cs:38