public void DifferentSettingsGenerateDifferentFunction() { var graph = ScriptableObject.CreateInstance <VFXGraph>(); var updateContext = ScriptableObject.CreateInstance <VFXBasicUpdate>(); graph.AddChild(updateContext); var blockA = ScriptableObject.CreateInstance <VFXBlockSourceVariantTest>(); blockA.SetSettingValue("switchSourceCode", true); var blockB = ScriptableObject.CreateInstance <VFXBlockSourceVariantTest>(); blockB.SetSettingValue("switchSourceCode", false); updateContext.AddChild(blockA); updateContext.AddChild(blockB); var contextCompiledData = new VFXContextCompiledData() { gpuMapper = new VFXExpressionMapper(), uniformMapper = new VFXUniformMapper(new VFXExpressionMapper(), true) }; HashSet <string> dependencies = new HashSet <string>(); var stringBuilder = VFXCodeGenerator.Build(updateContext, VFXCompilationMode.Runtime, contextCompiledData, dependencies); var code = stringBuilder.ToString(); Assert.IsTrue(code.Contains(VFXBlockSourceVariantTest.sourceCodeVariant[0])); Assert.IsTrue(code.Contains(VFXBlockSourceVariantTest.sourceCodeVariant[1])); }
static void GenerateVFXAdditionalCommands(VFXContext context, VFXContextCompiledData contextData, out AdditionalCommandDescriptor loadAttributeDescriptor, out AdditionalCommandDescriptor blockFunctionDescriptor, out AdditionalCommandDescriptor blockCallFunctionDescriptor, out AdditionalCommandDescriptor interpolantsGenerationDescriptor, out AdditionalCommandDescriptor buildVFXFragInputsDescriptor, out AdditionalCommandDescriptor pixelPropertiesAssignDescriptor, out AdditionalCommandDescriptor defineSpaceDescriptor, out AdditionalCommandDescriptor parameterBufferDescriptor, out AdditionalCommandDescriptor additionalDefinesDescriptor, out AdditionalCommandDescriptor loadPositionAttributeDescriptor, out AdditionalCommandDescriptor loadCropFactorAttributesDescriptor, out AdditionalCommandDescriptor loadTexcoordAttributesDescriptor, out AdditionalCommandDescriptor vertexPropertiesGenerationDescriptor, out AdditionalCommandDescriptor vertexPropertiesAssignDescriptor) { // TODO: Clean all of this up. Currently just an adapter between VFX Code Gen + SG Code Gen and *everything* has been stuffed here. // Load Attributes loadAttributeDescriptor = new AdditionalCommandDescriptor("VFXLoadAttribute", VFXCodeGenerator.GenerateLoadAttribute(".", context).ToString()); // Graph Blocks VFXCodeGenerator.BuildContextBlocks(context, contextData, out var blockFunction, out var blockCallFunction); blockFunctionDescriptor = new AdditionalCommandDescriptor("VFXGeneratedBlockFunction", blockFunction); blockCallFunctionDescriptor = new AdditionalCommandDescriptor("VFXProcessBlocks", blockCallFunction); // Vertex Input VFXCodeGenerator.BuildVertexProperties(context, contextData, out var vertexPropertiesGeneration); vertexPropertiesGenerationDescriptor = new AdditionalCommandDescriptor("VFXVertexPropertiesGeneration", vertexPropertiesGeneration); VFXCodeGenerator.BuildVertexPropertiesAssign(context, contextData, out var vertexPropertiesAssign); vertexPropertiesAssignDescriptor = new AdditionalCommandDescriptor("VFXVertexPropertiesAssign", vertexPropertiesAssign); // Interpolator VFXCodeGenerator.BuildInterpolatorBlocks(context, contextData, out var interpolatorsGeneration); interpolantsGenerationDescriptor = new AdditionalCommandDescriptor("VFXInterpolantsGeneration", interpolatorsGeneration); // Frag Inputs - Only VFX will know if frag inputs come from interpolator or the CBuffer. VFXCodeGenerator.BuildFragInputsGeneration(context, contextData, out var buildFragInputsGeneration); buildVFXFragInputsDescriptor = new AdditionalCommandDescriptor("VFXSetFragInputs", buildFragInputsGeneration); VFXCodeGenerator.BuildPixelPropertiesAssign(context, contextData, out var pixelPropertiesAssign); pixelPropertiesAssignDescriptor = new AdditionalCommandDescriptor("VFXPixelPropertiesAssign", pixelPropertiesAssign); // Define coordinate space var defineSpaceDescriptorContent = string.Empty; if (context.GetData() is ISpaceable) { var spaceable = context.GetData() as ISpaceable; defineSpaceDescriptorContent = $"#define {(spaceable.space == VFXCoordinateSpace.World ? "VFX_WORLD_SPACE" : "VFX_LOCAL_SPACE")} 1"; } defineSpaceDescriptor = new AdditionalCommandDescriptor("VFXDefineSpace", defineSpaceDescriptorContent); // Parameter Cbuffer VFXCodeGenerator.BuildParameterBuffer(contextData, out var parameterBuffer); parameterBufferDescriptor = new AdditionalCommandDescriptor("VFXParameterBuffer", parameterBuffer); // Defines & Headers - Not all are necessary, however some important ones are mixed in like indirect draw, strips, flipbook, particle strip info... ShaderStringBuilder additionalDefines = new ShaderStringBuilder(); // TODO: Need to add defines for current/source usage (i.e. scale). var allCurrentAttributes = context.GetData().GetAttributes().Where(a => (context.GetData().IsCurrentAttributeUsed(a.attrib, context)) || (context.contextType == VFXContextType.Init && context.GetData().IsAttributeStored(a.attrib))); // In init, needs to declare all stored attributes for intialization var allSourceAttributes = context.GetData().GetAttributes().Where(a => (context.GetData().IsSourceAttributeUsed(a.attrib, context))); foreach (var attribute in allCurrentAttributes) { additionalDefines.AppendLine("#define VFX_USE_{0}_{1} 1", attribute.attrib.name.ToUpper(CultureInfo.InvariantCulture), "CURRENT"); } foreach (var attribute in allSourceAttributes) { additionalDefines.AppendLine("#define VFX_USE_{0}_{1} 1", attribute.attrib.name.ToUpper(CultureInfo.InvariantCulture), "SOURCE"); } foreach (var header in context.additionalDataHeaders) { additionalDefines.AppendLine(header); } foreach (var define in context.additionalDefines) { additionalDefines.AppendLine($"#define {define} 1"); } additionalDefinesDescriptor = new AdditionalCommandDescriptor("VFXDefines", additionalDefines.ToString()); // Load Position Attribute loadPositionAttributeDescriptor = new AdditionalCommandDescriptor("VFXLoadPositionAttribute", VFXCodeGenerator.GenerateLoadAttribute("position", context).ToString().ToString()); // Load Crop Factor Attribute var mainParameters = contextData.gpuMapper.CollectExpression(-1).ToArray(); var expressionToName = context.GetData().GetAttributes().ToDictionary(o => new VFXAttributeExpression(o.attrib) as VFXExpression, o => (new VFXAttributeExpression(o.attrib)).GetCodeString(null)); expressionToName = expressionToName.Union(contextData.uniformMapper.expressionToCode).ToDictionary(s => s.Key, s => s.Value); loadCropFactorAttributesDescriptor = new AdditionalCommandDescriptor("VFXLoadCropFactorParameter", VFXCodeGenerator.GenerateLoadParameter("cropFactor", mainParameters, expressionToName).ToString().ToString()); loadTexcoordAttributesDescriptor = new AdditionalCommandDescriptor("VFXLoadTexcoordParameter", VFXCodeGenerator.GenerateLoadParameter("texCoord", mainParameters, expressionToName).ToString().ToString()); }