示例#1
0
        /// <summary>
        /// Returns a ShaderModelConstantRegister object with the information contained in
        /// the given regular expression match.
        /// </summary>
        private static ShaderModelConstantRegister CreateRegister(TargetFramework targetFramework, Match match)
        {
            ShaderModelConstantRegister register = null;

            // Figure out the .NET type that corresponds to the register type.
            var registerTypeInHlsl = match.Groups["registerType"].Value;
            var registerType       = GetRegisterType(targetFramework, registerTypeInHlsl);

            if (registerType != null)
            {
                // See if the user prefers to specify a different type in a comment.
                if (match.Groups["type"].Success)
                {
                    OverrideTypeIfAllowed(targetFramework, match.Groups["type"].Value, ref registerType);
                }

                // Capitalize the first letter of the variable name.  Leave the rest alone.
                var registerName = match.Groups["registerName"].Value;
                registerName = char.ToUpperInvariant(registerName[0]) + registerName.Substring(1);

                // Get the register number and the optional summary comment.
                var registerNumber = int.Parse(match.Groups["registerNumber"].Value);
                if (typeof(Brush).IsAssignableFrom(registerType) && (registerNumber == 0))
                {
                    return(null); // ignore the implicit input sampler
                }

                var summary = match.Groups["summary"].Value;

                // Get the standard min, max, and default value for the register type.
                GetStandardValues(registerType, out object minValue, out object maxValue, out object defaultValue);

                // Allow the user to override the defaults with values from their comments.
                ConvertValue(match.Groups["minValue"].Value, registerType, ref minValue);
                ConvertValue(match.Groups["maxValue"].Value, registerType, ref maxValue);
                ConvertValue(match.Groups["defaultValue"].Value, registerType, ref defaultValue);

                // And if the user specified an initializer for the register value in HLSL,
                // that value overrides any other default value.
                if (match.Groups["initializer"].Success)
                {
                    ParseInitializerValue(match.Groups["initializerValue"].Value, registerType, ref defaultValue);
                }

                // Create a structure to hold the register information.
                register = new ShaderModelConstantRegister(
                    registerName,
                    registerType,
                    registerNumber,
                    summary,
                    minValue,
                    maxValue,
                    defaultValue);
            }

            return(register);
        }
示例#2
0
        public TexturePicker(ShaderModelConstantRegister register)
        {
            this.InitializeComponent();
            this.register = register;
            //// attempt to get the already loaded value
            Images.TryGetValue(this.register.RegisterNumber, out ImageBrush result);
            this.Value = result;
            if (this.Value == null)
            {
                this.LoadTextureFromSettings();
            }

            this.SetupTextures();
        }
示例#3
0
        public TexturePicker(ShaderModelConstantRegister register)
        {
            InitializeComponent();
            _register = register;
            ImageBrush result;

            // attempt to get the already loaded value
            _images.TryGetValue(_register.RegisterNumber, out result);
            Value = result;
            if (Value == null)
            {
                LoadTextureFromSettings();
            }

            SetupTextures();
        }
示例#4
0
        /// <summary>
        /// Returns a shader model constructed from the information found in the
        /// given pixel shader file.
        /// </summary>
        public static ShaderModel ParseShader(string shaderFileName, string shaderText)
        {
            // Remove all double-slash comments (but not triple-slash comments).
            // This helps us avoid parsing register declarations that are commented out.
            shaderText = CommentRegex.Replace(shaderText, String.Empty);

            // Find all header comments.
            Match headerMatch = HeaderCommentsRegex.Match(shaderText);

            // Determine the class name, namespace, description, and target platform.
            string defaultClassName = Path.GetFileNameWithoutExtension(shaderFileName);

            defaultClassName = Char.ToUpperInvariant(defaultClassName[0]) + defaultClassName.Substring(1) + "Effect";
            string          className           = GetValidId(headerMatch.Groups["class"].Value, defaultClassName, false);
            string          namespaceName       = GetValidId(headerMatch.Groups["namespace"].Value, Settings.Default.GeneratedNamespace, true);
            string          description         = headerMatch.Groups["description"].Success ? headerMatch.Groups["description"].Value : null;
            string          targetFrameworkName = headerMatch.Groups["target"].Success ? headerMatch.Groups["target"].Value : Settings.Default.TargetFramework;
            TargetFramework targetFramework     = targetFrameworkName == "Silverlight" ? TargetFramework.Silverlight : TargetFramework.WPF;

            // Find all register declarations.
            MatchCollection matches = RegisterConstantDeclarationRegex.Matches(shaderText);

            // Create a list of shader model constant registers.
            List <ShaderModelConstantRegister> registers = new List <ShaderModelConstantRegister>();

            foreach (Match match in matches)
            {
                ShaderModelConstantRegister register = CreateRegister(targetFramework, match);
                if (register != null)
                {
                    registers.Add(register);
                }
            }

            // Return a new shader model.
            return(new ShaderModel
            {
                ShaderFileName = shaderFileName,
                GeneratedClassName = className,
                GeneratedNamespace = namespaceName,
                Description = description,
                TargetFramework = targetFramework,
                Registers = registers
            });
        }
    private static CodeMemberField CreateShaderRegisterDependencyProperty(ShaderModel shaderModel, ShaderModelConstantRegister register)
    {
        if (typeof(Brush).IsAssignableFrom(register.RegisterType))
        {
            return(new CodeMemberField
            {
                Type = new CodeTypeReference("DependencyProperty"),
                Name = String.Format("{0}Property", register.RegisterName),
                Attributes = MemberAttributes.Public | MemberAttributes.Static,
                InitExpression = new CodeMethodInvokeExpression
                {
                    Method = new CodeMethodReferenceExpression
                    {
                        TargetObject = new CodeTypeReferenceExpression("ShaderEffect"),
                        MethodName = "RegisterPixelShaderSamplerProperty"
                    },
                    Parameters =
                    {
                        new CodePrimitiveExpression(register.RegisterName),
                        new CodeTypeOfExpression(shaderModel.GeneratedClassName),
                        new CodePrimitiveExpression(register.RegisterNumber)
                    }
                }
            });
        }

        return(new CodeMemberField
        {
            Type = new CodeTypeReference("DependencyProperty"),
            Name = String.Format("{0}Property", register.RegisterName),
            Attributes = MemberAttributes.Public | MemberAttributes.Static,
            InitExpression = new CodeMethodInvokeExpression
            {
                Method = new CodeMethodReferenceExpression
                {
                    TargetObject = new CodeTypeReferenceExpression("DependencyProperty"),
                    MethodName = "Register"
                },
                Parameters =
                {
                    new CodePrimitiveExpression(register.RegisterName),
                    new CodeTypeOfExpression(CreateCodeTypeReference(register.RegisterType)),
                    new CodeTypeOfExpression(shaderModel.GeneratedClassName),
                    new CodeObjectCreateExpression
                    {
                        // Silverlight doesn't have UIPropertyMetadata.
                        CreateType = new CodeTypeReference(shaderModel.TargetFramework == TargetFramework.WPF ? "UIPropertyMetadata" : "PropertyMetadata"),
                        Parameters =
                        {
                            CreateDefaultValue(register.DefaultValue),
                            new CodeMethodInvokeExpression
                            {
                                Method = new CodeMethodReferenceExpression(null,"PixelShaderConstantCallback"),
                                Parameters =
                                {
                                    new CodePrimitiveExpression(register.RegisterNumber)
                                }
                            }
                        }
                    }
                }
            }
        });
    }
示例#6
0
        private static CodeMemberField CreateShaderRegisterDependencyProperty(ShaderModel shaderModel, ShaderModelConstantRegister register)
        {
            if (typeof(Brush).IsAssignableFrom(register.RegisterType))
            {
                return(new CodeMemberField
                {
                    Comments =
                    {
                        new CodeCommentStatement("<summary>The ShaderEffect.RegisterPixelShaderSamplerProperty() method must be used with this field as argument. Note the last parameter of this method: it is an integer and it corresponds to the S0 pixel shader register.</summary>"),
                    },
                    Type = new CodeTypeReference("DependencyProperty"),
                    Name = $"{register.RegisterName}Property",
                    //// ReSharper disable once BitwiseOperatorOnEnumWithoutFlags
                    Attributes = MemberAttributes.Public | MemberAttributes.Static,
                    InitExpression = new CodeMethodInvokeExpression
                    {
                        Method = new CodeMethodReferenceExpression
                        {
                            TargetObject = new CodeTypeReferenceExpression("ShaderEffect"),
                            MethodName = "RegisterPixelShaderSamplerProperty"
                        },
                        Parameters =
                        {
                            new CodePrimitiveExpression(register.RegisterName),
                            new CodeTypeOfExpression(shaderModel.GeneratedClassName),
                            new CodePrimitiveExpression(register.RegisterNumber)
                        }
                    },
                });
            }

            return(new CodeMemberField
            {
                Type = new CodeTypeReference("DependencyProperty"),
                Name = $"{register.RegisterName}Property",
                //// ReSharper disable once BitwiseOperatorOnEnumWithoutFlags
                Attributes = MemberAttributes.Public | MemberAttributes.Static,
                InitExpression = new CodeMethodInvokeExpression
                {
                    Method = new CodeMethodReferenceExpression
                    {
                        TargetObject = new CodeTypeReferenceExpression("DependencyProperty"),
                        MethodName = "Register"
                    },
                    Parameters =
                    {
                        new CodePrimitiveExpression(register.RegisterName),
                        new CodeTypeOfExpression(CreateCodeTypeReference(register.RegisterType)),
                        new CodeTypeOfExpression(shaderModel.GeneratedClassName),
                        new CodeObjectCreateExpression
                        {
                            // Silverlight doesn't have UIPropertyMetadata.
                            CreateType = new CodeTypeReference(shaderModel.TargetFramework == TargetFramework.WPF ? "UIPropertyMetadata" : "PropertyMetadata"),
                            Parameters =
                            {
                                CreateDefaultValue(register.RegisterType, register.DefaultValue),
                                new CodeMethodInvokeExpression
                                {
                                    Method = new CodeMethodReferenceExpression(null,"PixelShaderConstantCallback"),
                                    Parameters =
                                    {
                                        new CodePrimitiveExpression(register.RegisterNumber)
                                    }
                                }
                            }
                        }
                    }
                }
            });
        }
        private void GenerateShaderInputControl(ShaderModelConstantRegister register)
        {
            string toolTipText = String.IsNullOrEmpty(register.Description) ? null : register.Description;

            TextBlock textBlock = new TextBlock
            {
                Foreground = Brushes.White,
                Margin     = new Thickness(5),
                Inlines    =
                {
                    new Run {
                        Foreground = (Brush)Application.Current.FindResource("HighlightBrush"), Text = register.RegisterName
                    },
                    new Run {
                        Text = String.Format(" : {0}", register.RegisterType.Name)
                    },
                },
                ToolTip = toolTipText
            };

            inputControlPanel.Children.Add(textBlock);

            Control control = null;

            if (register.RegisterType == typeof(Brush))
            {
                control = new TexturePicker(register);
            }
            else if (register.RegisterType == typeof(double) || register.RegisterType == typeof(float))
            {
                double minValue = Convert.ToDouble(register.MinValue);
                double maxValue = Convert.ToDouble(register.MaxValue);
                //double defaultValue = Double.Parse(register.DefaultValue.ToString(), NumberStyles.Any, null);
                double defaultValue = Convert.ToDouble(register.DefaultValue);
                control = new AdjustableSlider
                {
                    Minimum = Math.Min(minValue, defaultValue),
                    Maximum = Math.Max(maxValue, defaultValue),
                    Value   = defaultValue
                };
            }
            else if (register.RegisterType == typeof(Point) || register.RegisterType == typeof(Vector) || register.RegisterType == typeof(Size))
            {
                Point minValue     = (Point)RegisterValueConverter.ConvertToUsualType(register.MinValue);
                Point maxValue     = (Point)RegisterValueConverter.ConvertToUsualType(register.MaxValue);
                Point defaultValue = (Point)RegisterValueConverter.ConvertToUsualType(register.DefaultValue);
                control = new AdjustableSliderPair
                {
                    Minimum = new Point(Math.Min(minValue.X, defaultValue.X), Math.Min(minValue.Y, defaultValue.Y)),
                    Maximum = new Point(Math.Max(maxValue.X, defaultValue.X), Math.Max(maxValue.Y, defaultValue.Y)),
                    Value   = defaultValue
                };
            }
            else if (register.RegisterType == typeof(Point3D) || register.RegisterType == typeof(Vector3D))
            {
                Point3D minValue     = (Point3D)RegisterValueConverter.ConvertToUsualType(register.MinValue);
                Point3D maxValue     = (Point3D)RegisterValueConverter.ConvertToUsualType(register.MaxValue);
                Point3D defaultValue = (Point3D)RegisterValueConverter.ConvertToUsualType(register.DefaultValue);
                control = new AdjustableSliderTriplet
                {
                    Minimum = new Point3D(Math.Min(minValue.X, defaultValue.X), Math.Min(minValue.Y, defaultValue.Y), Math.Min(minValue.Z, defaultValue.Z)),
                    Maximum = new Point3D(Math.Max(maxValue.X, defaultValue.X), Math.Max(maxValue.Y, defaultValue.Y), Math.Max(maxValue.Z, defaultValue.Z)),
                    Value   = defaultValue
                };
            }
            else if (register.RegisterType == typeof(Color))
            {
                Color defaultValue = (Color)register.DefaultValue;
                //control = new Telerik.Windows.Controls.RadColorEditor
                //{

                //  HorizontalAlignment = HorizontalAlignment.Left,
                //  SelectedColor = defaultValue
                //};

                control = new AdjustableColor
                {
                    HorizontalAlignment = HorizontalAlignment.Left,
                    Value = defaultValue
                };
                //  ((control) as AdjustableColor).;
                //control = new TextBox
                //{
                //  Background = Brushes.LightYellow,
                //  Width = 150,
                //  HorizontalAlignment = HorizontalAlignment.Left,
                //  Text = defaultValue.ToString()
                //};
            }
            else if (register.RegisterType == typeof(Point4D))
            {
                Point4D minValue     = (Point4D)register.MinValue;
                Point4D maxValue     = (Point4D)register.MaxValue;
                Point4D defaultValue = (Point4D)register.DefaultValue;
                control = new AdjustableSliderQuadruplet
                {
                    Minimum = new Point4D(Math.Min(minValue.X, defaultValue.X), Math.Min(minValue.Y, defaultValue.Y), Math.Min(minValue.Z, defaultValue.Z), Math.Min(minValue.W, defaultValue.W)),
                    Maximum = new Point4D(Math.Max(maxValue.X, defaultValue.X), Math.Max(maxValue.Y, defaultValue.Y), Math.Max(maxValue.Z, defaultValue.Z), Math.Max(maxValue.W, defaultValue.W)),
                    Value   = defaultValue
                };
            }

            if (control != null)
            {
                control.Margin  = new Thickness(15, 2, 25, 5);
                control.ToolTip = toolTipText;
                this.inputControlPanel.Children.Add(control);
                register.AffiliatedControl = control;
            }
        }