Beispiel #1
0
        public void LoadGraph()
        {
            List <PropertyCollector.TextureInfo> lti;
            var assetCollection = new AssetCollection();

            ShaderGraphImporter.GetShaderText(kGraphName, out lti, assetCollection, out m_Graph);
            Assert.NotNull(m_Graph, $"Invalid graph data found for {kGraphName}");

            m_Graph.ValidateGraph();

            m_Collector = new PropertyCollector();
            m_Graph.CollectShaderProperties(m_Collector, GenerationMode.ForReals);

            // Open up the window
            if (!ShaderGraphImporterEditor.ShowGraphEditWindow(kGraphName))
            {
                Assert.Fail("ShaderGraphImporterEditor.ShowGraphEditWindow could not open " + kGraphName);
            }

            m_Window = EditorWindow.GetWindow <MaterialGraphEditWindow>();

            if (m_Window == null)
            {
                Assert.Fail("Could not open window");
            }

            // EditorWindow.GetWindow will return a new window if one is not found. A new window will have graphObject == null.
            if (m_Window.graphObject == null)
            {
                Assert.Fail("Existing Shader Graph window of " + kGraphName + " not found.");
            }

            m_GraphEditorView = m_Window.graphEditorView;
        }
Beispiel #2
0
        public void LoadGraph()
        {
            List <PropertyCollector.TextureInfo> lti;
            var assetCollection = new AssetCollection();

            ShaderGraphImporter.GetShaderText(kGraphName, out lti, assetCollection, out m_Graph);
            Assert.NotNull(m_Graph, $"Invalid graph data found for {kGraphName}");
        }
Beispiel #3
0
        public void LoadGraph()
        {
            List <PropertyCollector.TextureInfo> lti;
            var lsadp = new List <string>();

            ShaderGraphImporter.GetShaderText(kGraphName, out lti, lsadp, out m_Graph);
            Assert.NotNull(m_Graph, $"Invalid graph data found for {kGraphName}");
        }
Beispiel #4
0
        private static StringBuilder TryBuildFromShaderGraph(VFXShaderGraphParticleOutput context, VFXContextCompiledData contextData)
        {
            var stringBuilder = new StringBuilder();

            // Reconstruct the ShaderGraph.
            var path = AssetDatabase.GetAssetPath(context.GetOrRefreshShaderGraphObject());

            List <PropertyCollector.TextureInfo> configuredTextures;
            AssetCollection assetCollection = new AssetCollection();

            MinimalGraphData.GatherMinimalDependenciesFromFile(path, assetCollection);

            var textGraph = File.ReadAllText(path, Encoding.UTF8);
            var graph     = new GraphData
            {
                messageManager = new MessageManager(),
                assetGuid      = AssetDatabase.AssetPathToGUID(path)
            };

            MultiJson.Deserialize(graph, textGraph);
            graph.OnEnable();
            graph.ValidateGraph();

            // Check the validity of the shader graph (unsupported keywords or shader property usage).
            if (VFXLibrary.currentSRPBinder == null || !VFXLibrary.currentSRPBinder.IsGraphDataValid(graph))
            {
                return(null);
            }

            var target = graph.activeTargets.Where(o =>
            {
                if (o.SupportsVFX())
                {
                    //We are assuming the target has been implemented in the same package than srp binder.
                    var srpBinderAssembly = VFXLibrary.currentSRPBinder.GetType().Assembly;
                    var targetAssembly    = o.GetType().Assembly;
                    if (srpBinderAssembly == targetAssembly)
                    {
                        return(true);
                    }
                }
                return(false);
            }).FirstOrDefault();

            if (target == null || !target.TryConfigureContextData(context, contextData))
            {
                return(null);
            }

            // Use ShaderGraph to generate the VFX shader.
            var text = ShaderGraphImporter.GetShaderText(path, out configuredTextures, assetCollection, graph, GenerationMode.VFX, new[] { target });

            // Append the shader + strip the name header (VFX stamps one in later on).
            stringBuilder.Append(text);
            stringBuilder.Remove(0, text.IndexOf("{", StringComparison.Ordinal));

            return(stringBuilder);
        }
        public void LoadGraph()
        {
            List <PropertyCollector.TextureInfo> lti;
            var lsadp = new List <string>();

            ShaderGraphImporter.GetShaderText(kGraphName, out lti, lsadp, out m_Graph);
            Assert.NotNull(m_Graph, $"Invalid graph data found for {kGraphName}");
            m_CFNode = m_Graph.GetNodes <CustomFunctionNode>().FirstOrDefault();
            Assert.NotNull(m_CFNode, $"No CustomFunctionNode found in {kGraphName}.");
        }
Beispiel #6
0
    internal GraphData LoadGraph(string graphPath)
    {
        List <PropertyCollector.TextureInfo> lti;
        var assetCollection = new AssetCollection();

        ShaderGraphImporter.GetShaderText(graphPath, out lti, assetCollection, out var graph);
        Assert.NotNull(graph, $"Invalid graph data found for {graphPath}");
        graph.OnEnable();
        graph.ValidateGraph();
        return(graph);
    }
Beispiel #7
0
        public void LoadGraph()
        {
            List <PropertyCollector.TextureInfo> lti;
            var assetCollection = new AssetCollection();

            ShaderGraphImporter.GetShaderText(kGraphName, out lti, assetCollection, out m_Graph);
            Assert.NotNull(m_Graph, $"Invalid graph data found for {kGraphName}");

            m_Graph.ValidateGraph();

            m_Collector = new KeywordCollector();
            m_Graph.CollectShaderKeywords(m_Collector, GenerationMode.ForReals);
        }
Beispiel #8
0
        public void LoadGraph()
        {
            List <PropertyCollector.TextureInfo> lti;
            var lsadp = new List <string>();

            ShaderGraphImporter.GetShaderText(kGraphName, out lti, lsadp, out m_Graph);
            Assert.NotNull(m_Graph, $"Invalid graph data found for {kGraphName}");

            m_Graph.ValidateGraph();

            m_Collector = new PropertyCollector();
            m_Graph.CollectShaderProperties(m_Collector, GenerationMode.ForReals);
        }
Beispiel #9
0
        public void LoadGraph()
        {
            List <PropertyCollector.TextureInfo> lti;
            var assetCollection = new AssetCollection();

            ShaderGraphImporter.GetShaderText(kGraphName, out lti, assetCollection, out m_Graph);
            Assert.NotNull(m_Graph, $"Invalid graph data found for {kGraphName}");

            m_Graph.ValidateGraph();

            m_Collector = new PropertyCollector();
            m_Graph.CollectShaderProperties(m_Collector, GenerationMode.ForReals);

            // Open up the window
            if (!ShaderGraphImporterEditor.ShowGraphEditWindow(kGraphName))
            {
                Assert.Fail("ShaderGraphImporterEditor.ShowGraphEditWindow could not open " + kGraphName);
            }

            m_Window = EditorWindow.GetWindow <MaterialGraphEditWindow>();
            if (m_Window == null)
            {
                Assert.Fail("Could not open window");
            }

            // EditorWindow.GetWindow will return a new window if one is not found. A new window will have graphObject == null.
            if (m_Window.graphObject == null)
            {
                Assert.Fail("Existing Shader Graph window of " + kGraphName + " not found.");
            }

            m_GraphEditorView = m_Window.graphEditorView;

            // Create the blackboard test controller
            var blackboardViewModel = new BlackboardViewModel()
            {
                parentView = m_Window.graphEditorView.graphView, model = m_Window.graphObject.graph, title = m_Window.assetName
            };

            m_BlackboardTestController = new BlackboardTestController(m_Window, m_Graph, blackboardViewModel, m_Window.graphObject.graphDataStore);

            // Remove the normal blackboard
            m_GraphEditorView.blackboardController.blackboard.RemoveFromHierarchy();

            // And override reference to the blackboard controller to point at the test controller
            m_GraphEditorView.blackboardController = m_BlackboardTestController;
        }
        public void LoadGraph()
        {
            List <PropertyCollector.TextureInfo> lti;
            var lsadp = new List <string>();

            ShaderGraphImporter.GetShaderText(kGraphName, out lti, lsadp, out m_Graph);
            Assert.NotNull(m_Graph, $"Invalid graph data found for {kGraphName}");

            List <PreviewNode> previewNodes = m_Graph.GetNodes <PreviewNode>().ToList();

            Assert.AreNotEqual(0, previewNodes.Count, $"No PreviewNode found in {kGraphName}.");
            foreach (PreviewNode node in previewNodes)
            {
                GroupData group = m_Graph.groups.Where(x => x.guid == node.groupGuid).FirstOrDefault();
                Assert.NotNull(m_Graph, $"Invalid group data found for PreviewNode with guid {node.guid}");
                m_TestNodes.Add(group.title, node);
            }
        }
Beispiel #11
0
        public void SimpleThresholdTest()
        {
            const string kGraphName       = "Assets/CommonAssets/Graphs/CustomInterpolatorThreshold.shadergraph";
            const int    kExpectedPadding = 4;

            GraphData graph;
            List <PropertyCollector.TextureInfo> lti;
            var assetCollection = new AssetCollection();

            ShaderGraphImporter.GetShaderText(kGraphName, out lti, assetCollection, out graph);
            Assert.NotNull(graph, $"Invalid graph data found for {kGraphName}");

            graph.ValidateGraph();

            // setup the thresholds expected by this graph. We will add some buffer against the test target's padding in case the target for the test changes.
            int padding = kExpectedPadding - graph.activeTargets.First().padCustomInterpolatorLimit;
            int initialErrorThreshold   = ShaderGraphProjectSettings.instance.customInterpolatorErrorThreshold;
            int initialwarningThreshold = ShaderGraphProjectSettings.instance.customInterpolatorWarningThreshold;

            ShaderGraphProjectSettings.instance.customInterpolatorErrorThreshold   = 13 - padding;
            ShaderGraphProjectSettings.instance.customInterpolatorWarningThreshold = 12 - padding;

            graph.ValidateCustomBlockLimit();
            var msgs = graph.messageManager.GetNodeMessages();

            // this is not a granular test- but the expected error/warning messages for this graph given the thresholds and padding should be 1 of each.
            int nWarnings = msgs.Count(nodeKey => nodeKey.Value.Any(item => item.severity == Rendering.ShaderCompilerMessageSeverity.Warning));
            int nErrors   = msgs.Count(nodeKey => nodeKey.Value.Any(item => item.severity == Rendering.ShaderCompilerMessageSeverity.Error));

            // reset the thresholds.
            ShaderGraphProjectSettings.instance.customInterpolatorErrorThreshold   = initialErrorThreshold;
            ShaderGraphProjectSettings.instance.customInterpolatorWarningThreshold = initialwarningThreshold;

            // actual tests.
            Assert.IsTrue(nErrors == 1);
            Assert.IsTrue(nWarnings == 1);
        }
        public void Graph(TestInfo testInfo)
        {
            var file     = testInfo.info;
            var filePath = file.FullName;

            var textGraph = File.ReadAllText(filePath, Encoding.UTF8);
            var graph     = JsonUtility.FromJson <MaterialGraph>(textGraph);

            Assert.IsNotNull(graph.masterNode, "No master node in graph.");

            var rootPath             = Path.Combine(Path.Combine("" /*DefaultShaderIncludes.GetRepositoryPath()*/, "Testing"), "IntegrationTests");
            var shaderTemplatePath   = Path.Combine(rootPath, ".ShaderTemplates");
            var textTemplateFilePath = Path.Combine(shaderTemplatePath, string.Format("{0}.{1}", testInfo.name, "shader"));


            List <PropertyCollector.TextureInfo> configuredTextures = new List <PropertyCollector.TextureInfo>();

            if (!File.Exists(textTemplateFilePath))
            {
                Directory.CreateDirectory(Directory.GetParent(textTemplateFilePath).FullName);
                File.WriteAllText(textTemplateFilePath, ShaderGraphImporter.GetShaderText(filePath, out configuredTextures));
                configuredTextures.Clear();
            }

            // Generate the shader
            var shaderString = ShaderGraphImporter.GetShaderText(filePath, out configuredTextures);

            Directory.CreateDirectory(shaderTemplatePath);

            var textTemplate  = File.ReadAllText(textTemplateFilePath);
            var textsAreEqual = string.Compare(shaderString, textTemplate, CultureInfo.CurrentCulture, CompareOptions.IgnoreSymbols);

            if (0 != textsAreEqual)
            {
                var failedPath               = Path.Combine(rootPath, ".Failed");
                var misMatchLocationResult   = Path.Combine(failedPath, string.Format("{0}.{1}", testInfo.name, "shader"));
                var misMatchLocationTemplate = Path.Combine(failedPath, string.Format("{0}.template.{1}", testInfo.name, "shader"));
                Directory.CreateDirectory(Directory.GetParent(misMatchLocationResult).FullName);
                File.WriteAllText(misMatchLocationResult, shaderString);
                File.WriteAllText(misMatchLocationTemplate, textTemplate);

                Assert.Fail("Shader text from graph {0}, did not match .template file.", file);
            }

            m_Shader           = ShaderUtil.CreateShaderAsset(shaderString);
            m_Shader.hideFlags = HideFlags.HideAndDontSave;
            Assert.IsNotNull(m_Shader, "Shader Generation Failed");

            //Assert.IsFalse(AbstractMaterialNodeUI.ShaderHasError(m_Shader), "Shader has error");

            m_PreviewMaterial = new Material(m_Shader)
            {
                hideFlags = HideFlags.HideAndDontSave
            };

            foreach (var textureInfo in configuredTextures)
            {
                var texture = EditorUtility.InstanceIDToObject(textureInfo.textureId) as Texture;
                if (texture == null)
                {
                    continue;
                }
                m_PreviewMaterial.SetTexture(textureInfo.name, texture);
            }

            Assert.IsNotNull(m_PreviewMaterial, "preview material could not be created");

            const int res = 256;

            using (var generator = new MaterialGraphPreviewGenerator())
            {
                var renderTexture = new RenderTexture(res, res, 16, RenderTextureFormat.ARGB32, RenderTextureReadWrite.Default)
                {
                    hideFlags = HideFlags.HideAndDontSave
                };
                generator.DoRenderPreview(renderTexture, m_PreviewMaterial, null, PreviewMode.Preview3D, true, 10);

                Assert.IsNotNull(renderTexture, "Render failed");

                RenderTexture.active = renderTexture;
                m_Captured           = new Texture2D(renderTexture.width, renderTexture.height, TextureFormat.ARGB32, false);
                m_Captured.ReadPixels(new Rect(0, 0, renderTexture.width, renderTexture.height), 0, 0);
                RenderTexture.active = null; //can help avoid errors
                Object.DestroyImmediate(renderTexture, true);

                // find the reference image
                var dumpFileLocation = Path.Combine(shaderTemplatePath, string.Format("{0}.{1}", testInfo.name, "png"));
                if (!File.Exists(dumpFileLocation))
                {
                    Directory.CreateDirectory(Directory.GetParent(dumpFileLocation).FullName);
                    // no reference exists, create it
                    var generated = m_Captured.EncodeToPNG();
                    File.WriteAllBytes(dumpFileLocation, generated);
                    Assert.Fail("Image template file not found for {0}, creating it.", file);
                }

                var template = File.ReadAllBytes(dumpFileLocation);
                m_FromDisk = new Texture2D(2, 2);
                m_FromDisk.LoadImage(template, false);

                var rmse = CompareTextures(m_FromDisk, m_Captured);

                if (rmse > testInfo.threshold)
                {
                    var failedPath               = Path.Combine(rootPath, ".Failed");
                    var misMatchLocationResult   = Path.Combine(failedPath, string.Format("{0}.{1}", testInfo.name, "png"));
                    var misMatchLocationTemplate =
                        Path.Combine(failedPath, string.Format("{0}.template.{1}", testInfo.name, "png"));
                    var generated = m_Captured.EncodeToPNG();
                    Directory.CreateDirectory(Directory.GetParent(misMatchLocationResult).FullName);
                    File.WriteAllBytes(misMatchLocationResult, generated);
                    File.WriteAllBytes(misMatchLocationTemplate, template);

                    Assert.Fail("Shader image from graph {0}, did not match .template file.", file);
                }
            }
        }