Пример #1
0
        public void TestDefaultValuesGettingUpdatedAfterRecompile()
        {
            Init();

            var shaderClassName = "DefaultValuesBeingUpdatedTest";
            var variableName    = "floatVar";

            // First register the key as it would've been done by the generator
            var initialKey = ParameterKeys.NewValue(1f, $"{shaderClassName}.{variableName}");

            ParameterKeys.Merge(initialKey, null, initialKey.Name);

            GenerateAndCheck("1", 1f);

            Compiler.ResetCache(new HashSet <string>()
            {
                shaderClassName
            });

            GenerateAndCheck("2", 2f);

            void GenerateAndCheck(string stringValue, float value)
            {
                var variables = new List <(string name, TypeBase type, string value, object clrValue)>();

                variables.Add((name: variableName, type: ScalarType.Float, value: stringValue, clrValue: value));

                var assignments = new StringBuilder();

                foreach (var v in variables)
                {
                    assignments.AppendLine($"{v.type} {v.name} = {v.value};");
                }

                var mixinSource = new ShaderMixinSource()
                {
                    Name = shaderClassName
                };

                mixinSource.Mixins.Add(CreateShaderClassCode(shaderClassName, assignments.ToString()));
                var byteCodeTask = Compiler.Compile(mixinSource, MixinParameters.EffectParameters, MixinParameters);

                Assert.False(byteCodeTask.Result.CompilationLog.HasErrors);

                var byteCode = byteCodeTask.Result.Bytecode;

                using (var graphicsDevice = GraphicsDevice.New())
                {
                    // The effect constructor updates the effect reflection
                    var effect = new Effect(graphicsDevice, byteCode);

                    var members = byteCode.Reflection.ConstantBuffers[0].Members;
                    foreach (var v in variables)
                    {
                        // Fetch the default value via the key - the previous test already checked whether the default value is present in the value description
                        var effectValueDescription = members.FirstOrDefault(k => k.KeyInfo.KeyName == $"{shaderClassName}.{v.name}");
                        var defaultValue           = effectValueDescription.KeyInfo.Key.DefaultValueMetadata.GetDefaultValue();
                        Assert.NotNull(defaultValue);
                        Assert.Equal(v.clrValue, defaultValue);
                    }
                }
            }
        }