Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
RenderMesh.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 System.Collections.Generic;
5 
6 using SiliconStudio.Core;
7 using SiliconStudio.Paradox.Graphics;
8 
9 namespace SiliconStudio.Paradox.Effects
10 {
11  /// <summary>
12  /// An effect mesh.
13  /// </summary>
15  {
16  private VertexArrayObject vertexArrayObject;
17 
18  /// <summary>
19  /// The model instance associated to this effect mesh.
20  /// </summary>
21  /// <value>The model instance.</value>
22  public readonly RenderModel RenderModel;
23 
24  /// <summary>
25  /// The mesh associated with this instance.
26  /// </summary>
27  public readonly Mesh Mesh;
28 
29  /// <summary>
30  /// Initializes a new instance of the <see cref="RenderMesh" /> class.
31  /// </summary>
32  /// <param name="renderModel">The render model.</param>
33  /// <param name="mesh">The mesh data.</param>
34  /// <exception cref="System.ArgumentNullException">mesh</exception>
35  public RenderMesh(RenderModel renderModel, Mesh mesh)
36  {
37  if (renderModel == null) throw new ArgumentNullException("renderModel");
38  if (mesh == null) throw new ArgumentNullException("mesh");
39  RenderModel = renderModel;
40  Mesh = mesh;
41  Enabled = true;
42  }
43 
44  /// <summary>
45  /// Enable or disable this particular effect mesh.
46  /// </summary>
47  public bool Enabled { get; set; }
48 
49  /// <summary>
50  /// Draw this effect mesh.
51  /// </summary>
52  public void Draw(RenderContext context)
53  {
54  // Retrieve effect parameters
55  var currentPass = context.CurrentPass;
56  var currentRenderData = this.Mesh.Draw;
57 
58  //using (Profiler.Begin(ProfilingKeys.PrepareMesh))
59  {
60  // Order of application of parameters:
61  // - RenderPass.Parameters
62  // - ModelComponent.Parameters
63  // - RenderMesh.Parameters (originally copied from mesh parameters)
64  // The order is based on the granularity level of each element and how shared it can be. Material is heavily shared, a model contains many meshes. An effectMesh is unique.
65  // TODO: really copy mesh parameters into effectMesh instead of just referencing the meshDraw parameters.
66 
67  var modelComponent = this.RenderModel.ModelInstance;
68  var hasMaterialParams = this.Mesh.Material != null && this.Mesh.Material.Parameters != null;
69  var hasModelComponentParams = modelComponent != null && modelComponent.Parameters != null;
70  if (hasMaterialParams)
71  {
72  if (hasModelComponentParams)
73  this.Effect.Apply(currentPass.Parameters, this.Mesh.Material.Parameters, modelComponent.Parameters, this.Mesh.Parameters, true);
74  else
75  this.Effect.Apply(currentPass.Parameters, this.Mesh.Material.Parameters, this.Mesh.Parameters, true);
76  }
77  else if (hasModelComponentParams)
78  this.Effect.Apply(currentPass.Parameters, modelComponent.Parameters, this.Mesh.Parameters, true);
79  else
80  this.Effect.Apply(currentPass.Parameters, this.Mesh.Parameters, true);
81  }
82 
83  //using (Profiler.Begin(ProfilingKeys.RenderMesh))
84  {
85  if (currentRenderData != null)
86  {
87  var graphicsDevice = context.GraphicsDevice;
88 
89  graphicsDevice.SetVertexArrayObject(vertexArrayObject);
90 
91  if (currentRenderData.IndexBuffer == null)
92  {
93  graphicsDevice.Draw(currentRenderData.PrimitiveType, currentRenderData.DrawCount, currentRenderData.StartLocation);
94  }
95  else
96  {
97  graphicsDevice.DrawIndexed(currentRenderData.PrimitiveType, currentRenderData.DrawCount, currentRenderData.StartLocation);
98  }
99  }
100  }
101  }
102 
103  internal void Initialize(GraphicsDevice device)
104  {
105  Utilities.Dispose(ref vertexArrayObject);
106  vertexArrayObject = VertexArrayObject.New(device, Effect.InputSignature, Mesh.Draw.IndexBuffer, Mesh.Draw.VertexBuffers);
107  }
108 
109  public override void FillParameterCollections(IList<ParameterCollection> parameterCollections)
110  {
111  if (Mesh.Material != null)
112  {
113  parameterCollections.Add(Mesh.Material.Parameters);
114  }
115 
116  var modelInstance = RenderModel.ModelInstance;
117  if (modelInstance != null && modelInstance.Parameters != null)
118  {
119  parameterCollections.Add(modelInstance.Parameters);
120  }
121 
122  if (Mesh.Parameters != null)
123  {
124  parameterCollections.Add(Mesh.Parameters);
125  }
126  }
127  }
128 }
A dynamic effect instance updated by DynamicEffectCompiler.
ParameterCollection Parameters
Definition: Mesh.cs:47
override void FillParameterCollections(IList< ParameterCollection > parameterCollections)
Fills the parameter collections used by this instance.
Definition: RenderMesh.cs:109
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.
void Draw(RenderContext context)
Draw this effect mesh.
Definition: RenderMesh.cs:52
readonly RenderModel RenderModel
The model instance associated to this effect mesh.
Definition: RenderMesh.cs:22
RenderMesh(RenderModel renderModel, Mesh mesh)
Initializes a new instance of the RenderMesh class.
Definition: RenderMesh.cs:35
Thread-local storage context used during rendering.
Instantiation of a Model through a RenderPipeline.
Definition: RenderModel.cs:11
SiliconStudio.Paradox.Graphics.PrimitiveType PrimitiveType
readonly Mesh Mesh
The mesh associated with this instance.
Definition: RenderMesh.cs:27