Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
ShaderSourceComparer.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 using System.Linq;
6 using SiliconStudio.Core.Extensions;
7 using SiliconStudio.Paradox.Effects;
8 
9 namespace SiliconStudio.Paradox.Shaders.Compiler
10 {
11  public class ShaderSourceComparer : EqualityComparer<ShaderSource>
12  {
13  private CompositionComparer compositionComparer;
14 
16  {
17  compositionComparer = new CompositionComparer(this);
18  }
19 
20  public override bool Equals(ShaderSource x, ShaderSource y)
21  {
22  if (x == null && y == null)
23  return true;
24 
25  if (x == null || y == null)
26  return false;
27 
28  if (x.GetType() != y.GetType())
29  return false;
30 
31  if (x is ShaderClassSource)
32  {
33  var x1 = (ShaderClassSource)x;
34  var y1 = (ShaderClassSource)y;
35  return x1.ClassName == y1.ClassName
36  && ArrayExtensions.ArraysEqual(x1.GenericArguments, y1.GenericArguments);
37  }
38  if (x is ShaderMixinSource)
39  {
40  var x1 = (ShaderMixinSource)x;
41  var y1 = (ShaderMixinSource)y;
42  return ArrayExtensions.ArraysEqual(x1.Mixins, y1.Mixins, this)
43  && ArrayExtensions.ArraysEqual(x1.Compositions.OrderBy(item => item.Key).ToArray(), y1.Compositions.OrderBy(item => item.Key).ToArray(), compositionComparer);
44  }
45  if (x is ShaderArraySource)
46  {
47  var x1 = (ShaderArraySource)x;
48  var y1 = (ShaderArraySource)y;
49  return ArrayExtensions.ArraysEqual(x1.Values, y1.Values, this);
50  }
51 
52  throw new InvalidOperationException("Invalid ShaderSource comparison.");
53  }
54 
55  public override int GetHashCode(ShaderSource obj)
56  {
57  if (obj == null)
58  return 0;
59 
60  unchecked
61  {
62  if (obj is ShaderClassSource)
63  {
64  var obj1 = (ShaderClassSource)obj;
65  return obj1.ClassName.GetHashCode()
66  ^ ArrayExtensions.ComputeHash(obj1.GenericArguments);
67  }
68  if (obj is ShaderMixinSource)
69  {
70  var obj1 = (ShaderMixinSource)obj;
71  return ArrayExtensions.ComputeHash(obj1.Mixins, this)
72  ^ ArrayExtensions.ComputeHash(obj1.Compositions.OrderBy(item => item.Key).ToArray(), compositionComparer);
73  }
74  if (obj is ShaderArraySource)
75  {
76  var obj1 = (ShaderArraySource)obj;
77  return ArrayExtensions.ComputeHash(obj1.Values, this);
78  }
79 
80  throw new InvalidOperationException("Invalid ShaderSource comparison.");
81  }
82  }
83 
84  private class CompositionComparer : EqualityComparer<KeyValuePair<string, ShaderSource>>
85  {
86  private ShaderSourceComparer shaderSourceComparer;
87 
88  public CompositionComparer(ShaderSourceComparer shaderSourceComparer)
89  {
90  this.shaderSourceComparer = shaderSourceComparer;
91  }
92 
93  public override bool Equals(KeyValuePair<string, ShaderSource> x, KeyValuePair<string, ShaderSource> y)
94  {
95  return x.Key == y.Key && shaderSourceComparer.Equals(x.Value, y.Value);
96  }
97 
98  public override int GetHashCode(KeyValuePair<string, ShaderSource> obj)
99  {
100  return obj.Key.GetHashCode() ^ shaderSourceComparer.GetHashCode(obj.Value);
101  }
102  }
103  }
104 }
A mixin performing a combination of ShaderClassSource and other mixins.
_In_ size_t _In_ DXGI_FORMAT _In_ size_t _In_ float size_t y
Definition: DirectXTexP.h:191
override bool Equals(ShaderSource x, ShaderSource y)
An array of ShaderSource used only in shader mixin compositions.