25 using System.Globalization;
26 using System.Text.RegularExpressions;
27 using SiliconStudio.Core;
28 using SiliconStudio.Core.Mathematics;
29 using SiliconStudio.Core.Serialization;
30 using SiliconStudio.Core.Serialization.Serializers;
32 namespace SiliconStudio.
Paradox.Graphics
47 internal string semanticName;
49 internal int semanticIndex;
53 internal int alignedByteOffset;
55 internal int hashCode;
58 internal static readonly Regex MatchSemanticIndex =
new Regex(
@"(.*)(\d+)$");
65 public const int AppendAligned = -1;
78 if (semanticName == null)
79 throw new ArgumentNullException(
"semanticName");
82 semanticName = semanticName.ToUpperInvariant();
84 var match = MatchSemanticIndex.Match(semanticName);
88 this.semanticName = match.Groups[1].Value;
89 semanticIndex = int.Parse(match.Groups[2].Value);
93 this.semanticName = semanticName;
97 alignedByteOffset = AppendAligned;
100 hashCode = ComputeHashCode();
113 if (semanticName == null)
114 throw new ArgumentNullException(
"semanticName");
117 semanticName = semanticName.ToUpperInvariant();
119 var match = MatchSemanticIndex.Match(semanticName);
121 throw new ArgumentException(
"Semantic name cannot a semantic index when using constructor with explicit semantic index. Use implicit semantic index constructor.");
124 this.semanticName = semanticName;
125 this.semanticIndex = semanticIndex;
127 this.alignedByteOffset = alignedByteOffset;
130 hashCode = ComputeHashCode();
136 public string SemanticName
147 public string SemanticAsText
151 if (semanticIndex == 0)
153 return string.Format(
"{0}{1}", semanticName, semanticIndex.ToString(CultureInfo.InvariantCulture));
160 public int SemanticIndex
164 return semanticIndex;
182 public int AlignedByteOffset
186 return alignedByteOffset;
193 return hashCode == other.hashCode && semanticName.Equals(other.semanticName) && semanticIndex == other.semanticIndex &&
format == other.format && alignedByteOffset == other.alignedByteOffset;
198 if (ReferenceEquals(null, obj))
return false;
207 internal int ComputeHashCode()
211 int localHashCode = semanticName.GetHashCode();
212 localHashCode = (localHashCode * 397) ^ semanticIndex;
213 localHashCode = (localHashCode * 397) ^
format.GetHashCode();
214 localHashCode = (localHashCode * 397) ^ alignedByteOffset;
215 return localHashCode;
221 return left.Equals(right);
226 return !left.Equals(right);
231 return string.Format(
"{0}{1},{2},{3}", semanticName, semanticIndex == 0 ? string.Empty : string.Empty + semanticIndex,
format, alignedByteOffset);
243 return Color(semanticIndex, ConvertTypeToFormat<T>(), offsetInBytes);
254 return Color(0, format, offsetInBytes);
266 return new VertexElement(
"COLOR", semanticIndex, format, offsetInBytes);
276 public static VertexElement Normal<T>(
int semanticIndex = 0,
int offsetInBytes = AppendAligned) where T :
struct
278 return Normal(semanticIndex, ConvertTypeToFormat<T>(), offsetInBytes);
289 return Normal(0, format, offsetInBytes);
301 return new VertexElement(
"NORMAL", semanticIndex, format, offsetInBytes);
311 public static VertexElement Position<T>(
int semanticIndex = 0,
int offsetInBytes = AppendAligned) where T :
struct
313 return Position(semanticIndex, ConvertTypeToFormat<T>(), offsetInBytes);
324 return Position(0, format, offsetInBytes);
336 return new VertexElement(
"POSITION", semanticIndex, format, offsetInBytes);
346 public static VertexElement PositionTransformed<T>(
int semanticIndex = 0,
int offsetInBytes = AppendAligned) where T :
struct
348 return PositionTransformed(semanticIndex, ConvertTypeToFormat<T>(), offsetInBytes);
359 return PositionTransformed(0, format, offsetInBytes);
371 return new VertexElement(
"SV_POSITION", semanticIndex, format, offsetInBytes);
381 public static VertexElement TextureCoordinate<T>(
int semanticIndex = 0,
int offsetInBytes = AppendAligned) where T :
struct
383 return TextureCoordinate(semanticIndex, ConvertTypeToFormat<T>(), offsetInBytes);
406 return new VertexElement(
"TEXCOORD", semanticIndex, format, offsetInBytes);
416 public static VertexElement Tangent<T>(
int semanticIndex = 0,
int offsetInBytes = AppendAligned) where T :
struct
418 return Tangent(semanticIndex, ConvertTypeToFormat<T>(), offsetInBytes);
429 return Tangent(0, format, offsetInBytes);
441 return new VertexElement(
"TANGENT", semanticIndex, format, offsetInBytes);
451 public static VertexElement BiTangent<T>(
int semanticIndex = 0,
int offsetInBytes = AppendAligned) where T :
struct
453 return BiTangent(semanticIndex, ConvertTypeToFormat<T>(), offsetInBytes);
464 return BiTangent(0, format, offsetInBytes);
476 return new VertexElement(
"BITANGENT", semanticIndex, format, offsetInBytes);
479 private static PixelFormat ConvertTypeToFormat<T>() where T :
struct
481 return ConvertTypeToFormat(typeof(T));
490 private static PixelFormat ConvertTypeToFormat(Type typeT)
493 return PixelFormat.R32G32B32A32_Float;
495 return PixelFormat.R32G32B32_Float;
497 return PixelFormat.R32G32_Float;
498 if (typeof(
float) == typeT)
499 return PixelFormat.R32_Float;
501 if (typeof(
Color) == typeT)
502 return PixelFormat.R8G8B8A8_UNorm;
504 return PixelFormat.B8G8R8A8_UNorm;
506 if (typeof(
Half4) == typeT)
507 return PixelFormat.R16G16B16A16_Float;
508 if (typeof(
Half2) == typeT)
509 return PixelFormat.R16G16_Float;
510 if (typeof(
Half) == typeT)
511 return PixelFormat.R16_Float;
513 if (typeof(
Int4) == typeT)
514 return PixelFormat.R32G32B32A32_UInt;
515 if (typeof(
Int3) == typeT)
516 return PixelFormat.R32G32B32_UInt;
517 if (typeof(
int) == typeT)
518 return PixelFormat.R32_UInt;
519 if (typeof(uint) == typeT)
520 return PixelFormat.R32_UInt;
528 throw new NotSupportedException(
string.Format(
"Type [{0}] is not supported. You must specify an explicit DXGI.Format", typeT.Name));
537 obj.semanticName = stream.ReadString();
538 obj.semanticIndex = stream.ReadInt32();
540 obj.alignedByteOffset = stream.ReadInt32();
541 obj.ComputeHashCode();
545 stream.Write(obj.semanticName);
546 stream.Write(obj.semanticIndex);
548 stream.Write(obj.alignedByteOffset);
override bool Equals(object obj)
static VertexElement Color(PixelFormat format, int offsetInBytes=AppendAligned)
Declares a VertexElement with the semantic "COLOR".
static VertexElement TextureCoordinate(int semanticIndex, PixelFormat format, int offsetInBytes=AppendAligned)
Declares a VertexElement with the semantic "TEXCOORD".
Represents a two dimensional mathematical vector.
override string ToString()
VertexElement(string semanticName, PixelFormat format)
Initializes a new instance of the VertexElement struct.
VertexElement(string semanticName, int semanticIndex, PixelFormat format, int alignedByteOffset=AppendAligned)
Initializes a new instance of the VertexElement struct.
static VertexElement Tangent(PixelFormat format, int offsetInBytes=AppendAligned)
Declares a VertexElement with the semantic "TANGENT".
static VertexElement TextureCoordinate(PixelFormat format, int offsetInBytes=AppendAligned)
Declares a VertexElement with the semantic "TEXCOORD".
static VertexElement Normal(int semanticIndex, PixelFormat format, int offsetInBytes=AppendAligned)
Declares a VertexElement with the semantic "NORMAL".
Represents a color in the form of rgb.
Represents a three dimensional mathematical vector.
Represents a 32-bit color (4 bytes) in the form of BGRA (in byte order: B, G, B, A).
TextureCoordinate
The texture coordinate.
Represents a three dimensional mathematical vector.
static VertexElement Position(int semanticIndex, PixelFormat format, int offsetInBytes=AppendAligned)
Declares a VertexElement with the semantic "POSITION".
Represents a color in the form of rgba.
static VertexElement BiTangent(int semanticIndex, PixelFormat format, int offsetInBytes=AppendAligned)
Declares a VertexElement with the semantic "BITANGENT".
static VertexElement PositionTransformed(int semanticIndex, PixelFormat format, int offsetInBytes=AppendAligned)
Declares a VertexElement with the semantic "SV_POSITION".
Base class for implementation of SerializationStream.
static VertexElement BiTangent(PixelFormat format, int offsetInBytes=AppendAligned)
Declares a VertexElement with the semantic "BITANGENT".
A half precision (16 bit) floating point value.
static VertexElement Normal(PixelFormat format, int offsetInBytes=AppendAligned)
Declares a VertexElement with the semantic "NORMAL".
Represents a four dimensional mathematical vector.
static VertexElement Tangent(int semanticIndex, PixelFormat format, int offsetInBytes=AppendAligned)
Declares a VertexElement with the semantic "TANGENT".
SiliconStudio.Core.Mathematics.Color Color
Represents a four dimensional mathematical vector.
Represents a 32-bit color (4 bytes) in the form of RGBA (in byte order: R, G, B, A).
The normal style (One line per item, structured by space).
static VertexElement Position(PixelFormat format, int offsetInBytes=AppendAligned)
Declares a VertexElement with the semantic "POSITION".
override int GetHashCode()
static VertexElement Color(int semanticIndex, PixelFormat format, int offsetInBytes=AppendAligned)
Declares a VertexElement with the semantic "COLOR".
Describes how to serialize and deserialize an object without knowing its type. Used as a common base ...
ArchiveMode
Enumerates the different mode of serialization (either serialization or deserialization).
_In_ size_t _In_ size_t _In_ DXGI_FORMAT format
Defines a two component vector, using half precision floating point coordinates.
static VertexElement PositionTransformed(PixelFormat format, int offsetInBytes=AppendAligned)
Declares a VertexElement with the semantic "SV_POSITION".
Defines a four component vector, using half precision floating point coordinates. ...
PixelFormat
Defines various types of pixel formats.
A description of a single element for the input-assembler stage. This structure is related to Direct3...
bool Equals(VertexElement other)