Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
ConstantBufferData.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.Runtime.CompilerServices;
5 using System.Runtime.InteropServices;
6 using SiliconStudio.Paradox.Effects;
7 using SiliconStudio.Core.Mathematics;
8 using SiliconStudio.Paradox.Shaders;
9 
10 namespace SiliconStudio.Paradox.Graphics.Internals
11 {
12  internal class ConstantBufferData
13  {
14  public IntPtr Data { get; private set; }
15  public ShaderConstantBufferDescription Desc { get; private set; }
16 
17  private BoundConstantBufferParam[] constantBufferParams;
18 
19  public ConstantBufferData(ShaderConstantBufferDescription description)
20  {
21  Desc = description;
22  Data = Marshal.AllocHGlobal(Desc.Size);
23  constantBufferParams = new BoundConstantBufferParam[Desc.Members.Length];
24  }
25 
26  public bool Update(ShaderParameterUpdater parameterUpdater)
27  {
28  bool dataChanged = false;
29 
30  //fixed (BoundConstantBufferParam* paramReferences = &this.constantBufferParams[0])
31  {
32  for (int i = 0; i < this.constantBufferParams.Length; ++i)
33  {
34  dataChanged |= UpdateValue(parameterUpdater, ref Desc.Members[i], i);
35  }
36  }
37  return dataChanged;
38  }
39 
40  private unsafe bool UpdateValue(ShaderParameterUpdater parameterUpdater, ref EffectParameterValueData shaderVariable, int i)
41  {
42  if (shaderVariable.Param.KeyIndex == -1)
43  {
44  throw new InvalidOperationException();
45  }
46 
47  BoundConstantBufferParam paramReference = constantBufferParams[i];
48 
49  var internalValue = parameterUpdater.GetInternalValue(shaderVariable.Param.KeyIndex);
50 
51  // TODO: Comparing Counter+DataPointer is not enough (if realloc on same address)
52  if (internalValue.Counter == paramReference.DirtyCount
53  && internalValue == paramReference.DataPointer)
54  return false;
55 
56  constantBufferParams[i] = new BoundConstantBufferParam
57  {
58  DataPointer = internalValue,
59  DirtyCount = internalValue.Counter
60  };
61 
62  var destination = (byte*)(Data + shaderVariable.Offset);
63 
64  int sourceOffset = 0;
65 
66  float* variableData = (float*)destination; // + shaderVariable.Offset);
67 
68  Matrix tempMatrix;
69 
70  switch (shaderVariable.Param.Class)
71  {
72  case EffectParameterClass.Struct:
73  internalValue.ReadFrom((IntPtr)variableData, sourceOffset, shaderVariable.Size);
74  break;
75  case EffectParameterClass.Scalar:
76  for (int elt = 0; elt < shaderVariable.Count; ++elt)
77  {
78  internalValue.ReadFrom((IntPtr)variableData, sourceOffset, sizeof(float));
79  //*variableData = *source++;
80  sourceOffset += 4;
81  variableData += 4; // 4 floats
82  }
83  break;
84  case EffectParameterClass.Vector:
85  case EffectParameterClass.Color:
86  for (int elt = 0; elt < shaderVariable.Count; ++elt)
87  {
88  //Framework.Utilities.CopyMemory((IntPtr)variableData, (IntPtr)source, (int)(shaderVariable.ColumnCount * sizeof(float)));
89  internalValue.ReadFrom((IntPtr)variableData, sourceOffset, (int)(shaderVariable.ColumnCount * sizeof(float)));
90  sourceOffset += (int)shaderVariable.ColumnCount * 4;
91  variableData += 4;
92  }
93  break;
94  case EffectParameterClass.MatrixColumns:
95  for (int elt = 0; elt < shaderVariable.Count; ++elt)
96  {
97  //fixed (Matrix* p = &tempMatrix)
98  {
99  internalValue.ReadFrom((IntPtr)(byte*)&tempMatrix, sourceOffset, (int)(shaderVariable.ColumnCount * shaderVariable.RowCount * sizeof(float)));
100  ((Matrix*)variableData)->CopyMatrixFrom((float*)&tempMatrix, unchecked((int)shaderVariable.ColumnCount), unchecked((int)shaderVariable.RowCount));
101  sourceOffset += (int)(shaderVariable.ColumnCount * shaderVariable.RowCount) * 4;
102  variableData += 4 * shaderVariable.RowCount;
103  }
104  }
105  break;
106  case EffectParameterClass.MatrixRows:
107  for (int elt = 0; elt < shaderVariable.Count; ++elt)
108  {
109  //fixed (Matrix* p = &tempMatrix)
110  {
111  internalValue.ReadFrom((IntPtr)(byte*)&tempMatrix, sourceOffset, (int)(shaderVariable.ColumnCount * shaderVariable.RowCount * sizeof(float)));
112  ((Matrix*)variableData)->TransposeMatrixFrom((float*)&tempMatrix, unchecked((int)shaderVariable.ColumnCount), unchecked((int)shaderVariable.RowCount));
113  //source += shaderVariable.ColumnCount * shaderVariable.RowCount;
114  sourceOffset += (int)(shaderVariable.ColumnCount * shaderVariable.RowCount) * 4;
115  variableData += 4 * shaderVariable.RowCount;
116  }
117  }
118  break;
119  }
120 
121  return true;
122  }
123 
124  private struct BoundConstantBufferParam
125  {
126  public int DirtyCount;
128  }
129  }
130 }
Describes a shader parameter for a valuetype (usually stored in constant buffers).
Represents a 4x4 mathematical matrix.
Definition: Matrix.cs:47