Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
ShaderExtensions.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 #if PARADOX_EFFECT_COMPILER
4 using System;
5 using System.Collections.Generic;
6 using System.Linq;
7 
8 using SiliconStudio.Paradox.Shaders.Parser.Ast;
9 using SiliconStudio.Shaders.Ast;
10 using SiliconStudio.Shaders.Ast.Hlsl;
11 using SiliconStudio.Shaders.Convertor;
12 
13 namespace SiliconStudio.Paradox.Shaders.Parser
14 {
15  public static class ShaderExtensions
16  {
17  // Used as key tag on TypeBase to link it to its actual ShaderClassType (if specified by user)
18  internal static string associatedClass = "AssociatedClass";
19  // Used as key tag on ShaderRootClassType to define its composition types
20  internal static string associatedCompositions = "AssociatedCompositions";
21  // Used as key tag on TypeBase to link it to its actual ShaderClassType (if specified by user)
22  public readonly static string AssociatedMacrosTag = "AssociatedMacros";
23 
24  public static void ReplaceAnnotation(this IAttributes node, string name, params object[] values)
25  {
26  foreach (var annotation in node.Attributes.OfType<AttributeDeclaration>())
27  {
28  if (annotation.Name == name && annotation.Parameters.Count >= 1)
29  {
30  annotation.Parameters = values.Select(x => new Literal(x)).ToList();
31  return;
32  }
33  }
34  node.Attributes.Add(new AttributeDeclaration { Name = new Identifier(name), Parameters = values.Select(x => new Literal(x)).ToList() });
35  }
36 
37  public static ShaderRootClassType GetMainShaderClass(this Shader shader)
38  {
39  var defaultShader = shader.Declarations.OfType<ShaderRootClassType>().FirstOrDefault(x => x.Name == "Shader");
40  if (defaultShader == null)
41  {
42  defaultShader = new ShaderRootClassType("Shader");
43  shader.Declarations.Add(defaultShader);
44  }
45  return defaultShader;
46  }
47 
48  public static ShaderRootClassType StartMix()
49  {
50  // TODO: Rename during Shader mixing
51  return new ShaderRootClassType("Mix");
52  }
53 
54  public static ShaderRootClassType Mix(this Shader shader, TypeBase mixinClass)
55  {
56  // Find the shader class which will drive compilation and add this new mixinClass in the list.
57  var mainShaderClass = shader.GetMainShaderClass();
58  mainShaderClass.Mix(mixinClass);
59 
60  return mainShaderClass;
61  }
62 
63  public static ShaderRootClassType Mix(this ShaderRootClassType target, TypeBase mixinClass)
64  {
65  var typeName = new TypeName(mixinClass.Name);
66  if (mixinClass is ShaderClassType)
67  typeName.SetTag(associatedClass, mixinClass);
68  target.BaseClasses.Add(typeName);
69 
70  return target;
71  }
72 
73  public static ShaderRootClassType Compose(this ShaderRootClassType sourceClass, string variableName, params ShaderClassType[] variableTypes)
74  {
75  var currentVariableTypes = (Dictionary<string, ShaderClassType[]>)sourceClass.GetTag(associatedCompositions);
76  if (currentVariableTypes == null)
77  {
78  currentVariableTypes = new Dictionary<string, ShaderClassType[]>();
79  sourceClass.SetTag(associatedCompositions, currentVariableTypes);
80  }
81 
82  currentVariableTypes[variableName] = variableTypes;
83 
84  return sourceClass;
85  }
86 
87  private class NameEqualityComparer : IEqualityComparer<IDeclaration>
88  {
89  public bool Equals(IDeclaration x, IDeclaration y)
90  {
91  return x.Name == y.Name;
92  }
93 
94  public int GetHashCode(IDeclaration obj)
95  {
96  return obj.Name.GetHashCode();
97  }
98  }
99 
100  /// <summary>
101  /// Gets the input or output structure.
102  /// </summary>
103  /// <param name="type">The pipeline stage.</param>
104  /// <param name="input">If set to <c>true</c> input, if set to <c>false</c> output.</param>
105  /// <returns></returns>
106  public static StructType GetStream(this Shader shader, PipelineStage type, bool input)
107  {
108  if (type == PipelineStage.Hull || type == PipelineStage.Domain)
109  throw new NotImplementedException();
110 
111  foreach (var typeDecl in shader.Declarations.OfType<StructType>())
112  {
113  var stream = typeDecl.Attributes.OfType<AttributeDeclaration>().FirstOrDefault(x => x.Name == "Stream");
114  if (stream != null)
115  {
116  if ((string)stream.Parameters[0].Value == (input ? "Input" : "Output") && (string)stream.Parameters[1].Value == type.ToString())
117  return typeDecl;
118  }
119  }
120 
121  return null;
122  }
123 
124  public static MethodDefinition GetEntryPoint(this Shader shader, ShaderStage type)
125  {
126  return shader.Declarations.OfType<MethodDefinition>().FirstOrDefault(f => f.Attributes.OfType<AttributeDeclaration>().Any(a => a.Name == "EntryPoint" && (string)a.Parameters[0].Value == type.ToString()));
127  }
128  }
129 }
130 #endif
function a
_In_ size_t _In_ DXGI_FORMAT _In_ size_t _In_ float size_t y
Definition: DirectXTexP.h:191
The compose mixin used to specifiy a composition.
ShaderStage
Enum to specify shader stage.
Definition: ShaderStage.cs:12
PipelineStage
Enum to specify pipeline stage.