Beispiel #1
0
        public void SimpleTimeCheckFromRef()
        {
            Stopwatch stopwatch = new Stopwatch();

            stopwatch.Start();

            double timedScopeTime = 0;

            using (TimedScope.FromRef(ref timedScopeTime))
            {
                Thread.Sleep(k_MillisecondsTimeout);
            }

            stopwatch.Stop();
            double stopWatchTime = stopwatch.Elapsed.TotalMilliseconds;

            Assert.AreEqual(stopWatchTime, timedScopeTime, k_TimeComparisonDelta);
        }
        public void OnProcessComputeShader(ComputeShader shader, string kernelName, IList <ShaderCompilerData> inputData)
        {
            if (HDRenderPipeline.currentAsset == null)
            {
                return;
            }

            if (HDRenderPipelineGlobalSettings.Ensure(canCreateNewAsset: false) == null)
            {
                return;
            }

            // Discard any compute shader use for raytracing if none of the RP asset required it
            ComputeShader unused;

            if (!ShaderBuildPreprocessor.playerNeedRaytracing && ShaderBuildPreprocessor.computeShaderCache.TryGetValue(shader.GetInstanceID(), out unused))
            {
                return;
            }

            using (new ExportComputeShaderStrip("Temp/compute-shader-strip.json", shader, kernelName, inputData, this))
            {
                var inputShaderVariantCount = inputData.Count;
                var hdPipelineAssets        = ShaderBuildPreprocessor.hdrpAssets;

                if (hdPipelineAssets.Count == 0)
                {
                    return;
                }

                uint preStrippingCount = (uint)inputData.Count;

                double stripTimeMs = 0;
                using (TimedScope.FromRef(ref stripTimeMs))
                {
                    for (int i = 0; i < inputShaderVariantCount;)
                    {
                        ShaderCompilerData input = inputData[i];

                        bool removeInput = true;
                        foreach (var hdAsset in hdPipelineAssets)
                        {
                            if (!StripShader(hdAsset, shader, kernelName, input))
                            {
                                removeInput = false;
                                break;
                            }
                        }

                        if (removeInput)
                        {
                            inputData[i] = inputData[--inputShaderVariantCount];
                        }
                        else
                        {
                            ++i;
                        }
                    }

                    if (inputData is List <ShaderCompilerData> inputDataList)
                    {
                        inputDataList.RemoveRange(inputShaderVariantCount,
                                                  inputDataList.Count - inputShaderVariantCount);
                    }
                    else
                    {
                        for (int i = inputData.Count - 1; i >= inputShaderVariantCount; --i)
                        {
                            inputData.RemoveAt(i);
                        }
                    }
                }

                LogShaderVariants(shader, kernelName, preStrippingCount, (uint)inputData.Count, stripTimeMs);
            }
        }
        public void OnProcessShader(Shader shader, ShaderSnippetData snippet, IList <ShaderCompilerData> inputData)
        {
            if (HDRenderPipeline.currentAsset == null)
            {
                return;
            }

            if (HDRenderPipelineGlobalSettings.Ensure(canCreateNewAsset: false) == null)
            {
                return;
            }

            double stripTimeMs = 0;

            using (new ExportShaderStrip("Temp/shader-strip.json", shader, snippet, inputData, this))
            {
                // TODO: Grab correct configuration/quality asset.
                var hdPipelineAssets = ShaderBuildPreprocessor.hdrpAssets;

                if (hdPipelineAssets.Count == 0)
                {
                    return;
                }

                uint preStrippingCount = (uint)inputData.Count;
                using (TimedScope.FromRef(ref stripTimeMs))
                {
                    // Test if striping is enabled in any of the found HDRP assets.
                    if (hdPipelineAssets.Count == 0 || !hdPipelineAssets.Any(a => a.allowShaderVariantStripping))
                    {
                        return;
                    }

                    var inputShaderVariantCount = inputData.Count;
                    for (int i = 0; i < inputShaderVariantCount;)
                    {
                        ShaderCompilerData input = inputData[i];

                        // Remove the input by default, until we find a HDRP Asset in the list that needs it.
                        bool removeInput = true;

                        foreach (var hdAsset in hdPipelineAssets)
                        {
                            var strippedByPreprocessor = false;

                            // Call list of strippers
                            // Note that all strippers cumulate each other, so be aware of any conflict here
                            foreach (BaseShaderPreprocessor shaderPreprocessor in shaderProcessorsList)
                            {
                                if (shaderPreprocessor.ShadersStripper(hdAsset, shader, snippet, input))
                                {
                                    strippedByPreprocessor = true;
                                    break;
                                }
                            }

                            if (!strippedByPreprocessor)
                            {
                                removeInput = false;
                                break;
                            }
                        }

                        if (removeInput)
                        {
                            inputData[i] = inputData[--inputShaderVariantCount];
                        }
                        else
                        {
                            ++i;
                        }
                    }

                    if (inputData is List <ShaderCompilerData> inputDataList)
                    {
                        inputDataList.RemoveRange(inputShaderVariantCount,
                                                  inputDataList.Count - inputShaderVariantCount);
                    }
                    else
                    {
                        for (int i = inputData.Count - 1; i >= inputShaderVariantCount; --i)
                        {
                            inputData.RemoveAt(i);
                        }
                    }
                }

                LogShaderVariants(shader, snippet, preStrippingCount, (uint)inputData.Count, stripTimeMs);
            }

            shaderPreprocessed?.Invoke(shader, snippet, inputData.Count, stripTimeMs);
        }
Beispiel #4
0
        public void OnProcessShader(Shader shader, ShaderSnippetData snippetData, IList <ShaderCompilerData> compilerDataList)
        {
#if PROFILE_BUILD
            Profiler.BeginSample(k_ProcessShaderTag);
#endif

            // We only want to perform shader variant stripping if the built-in render pipeline
            // is the active render pipeline (i.e., there is no SRP asset in place).
            RenderPipelineAsset rpAsset = GraphicsSettings.currentRenderPipeline;
            if (rpAsset != null || compilerDataList == null || compilerDataList.Count == 0)
            {
                return;
            }

            double stripTimeMs      = 0.0;
            int    prevVariantCount = compilerDataList.Count;
            using (TimedScope.FromRef(ref stripTimeMs))
            {
                var inputShaderVariantCount = compilerDataList.Count;
                for (int i = 0; i < inputShaderVariantCount;)
                {
                    bool removeInput = true;
                    foreach (var supportedFeatures in ShaderBuildPreprocessor.supportedFeaturesList)
                    {
                        if (!StripUnused(supportedFeatures, shader, snippetData, compilerDataList[i]))
                        {
                            removeInput = false;
                            break;
                        }
                    }

                    // Remove at swap back
                    if (removeInput)
                    {
                        compilerDataList[i] = compilerDataList[--inputShaderVariantCount];
                    }
                    else
                    {
                        ++i;
                    }
                }

                if (compilerDataList is List <ShaderCompilerData> inputDataList)
                {
                    inputDataList.RemoveRange(inputShaderVariantCount, inputDataList.Count - inputShaderVariantCount);
                }
                else
                {
                    for (int i = compilerDataList.Count - 1; i >= inputShaderVariantCount; --i)
                    {
                        compilerDataList.RemoveAt(i);
                    }
                }
            }

            m_TotalVariantsInputCount  += prevVariantCount;
            m_TotalVariantsOutputCount += compilerDataList.Count;
            LogShaderVariants(shader, snippetData, prevVariantCount, compilerDataList.Count);

#if PROFILE_BUILD
            Profiler.EndSample();
#endif
            shaderPreprocessed?.Invoke(shader, snippetData, prevVariantCount, stripTimeMs);
        }