Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
ParameterConstantBuffer.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.Paradox.Graphics;
5 using SiliconStudio.Core;
6 using SiliconStudio.Paradox.Shaders;
7 
8 namespace SiliconStudio.Paradox.Graphics.Internals
9 {
10  internal class ParameterConstantBuffer : ComponentBase
11  {
12  ConstantBufferData[] constantBufferDatas;
13  public SiliconStudio.Paradox.Graphics.Buffer Buffer { get; private set; }
14  DataPointer[] dataStreams;
15  internal ShaderConstantBufferDescription ConstantBufferDesc;
16  private bool forceDataChanged = false;
17 
18 #if SILICONSTUDIO_PARADOX_GRAPHICS_API_OPENGL
19  private static readonly bool UsingMap = false;
20 #else
21  private static readonly bool UsingMap = true;
22 #endif
23 
24  public ParameterConstantBuffer(GraphicsDevice device, string constantBufferName, ShaderConstantBufferDescription constantBufferDesc)
25  {
26  ConstantBufferDesc = constantBufferDesc;
27  constantBufferDatas = new ConstantBufferData[GraphicsDevice.ThreadCount];
28  dataStreams = new DataPointer[GraphicsDevice.ThreadCount];
29 
30  for (uint i = 0; i < GraphicsDevice.ThreadCount; ++i)
31  {
32  constantBufferDatas[i] = new ConstantBufferData(constantBufferDesc);
33  dataStreams[i] = new DataPointer(constantBufferDatas[i].Data, constantBufferDesc.Size);
34  }
35 
36  Buffer = SiliconStudio.Paradox.Graphics.Buffer.New(device, constantBufferDatas[0].Desc.Size, BufferFlags.ConstantBuffer, UsingMap ? GraphicsResourceUsage.Dynamic : GraphicsResourceUsage.Default);
37 
38  // We want to clear flags
39  // TODO: Should be later replaced with either an internal field on GraphicsResourceBase, or a reset counter somewhere?
40  Buffer.Reload = Reload;
41  }
42 
43  private void Reload(GraphicsResourceBase graphicsResourceBase)
44  {
45  forceDataChanged = true;
46 
47  // Force recreation
48  graphicsResourceBase.OnRecreate();
49  }
50 
51  public void Update(GraphicsDevice graphicsDevice, ShaderParameterUpdater parameterUpdater)
52  {
53  var threadIndex = graphicsDevice.ThreadIndex;
54  bool dataChanged = constantBufferDatas[threadIndex].Update(parameterUpdater);
55 
56  // Check if update is really needed
57  if (forceDataChanged)
58  forceDataChanged = false;
59  else if (!dataChanged)
60  return;
61 
62  // Upload data to constant buffer
63  Buffer.SetData(graphicsDevice, dataStreams[threadIndex]);
64  }
65  }
66 }
SiliconStudio.Shaders.Ast.Hlsl.ConstantBuffer ConstantBuffer
Base class for a framework component.
All-in-One Buffer class linked SharpDX.Direct3D11.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.