예제 #1
0
        public override void Execute(ScriptableRenderer renderer, ref ScriptableRenderContext context,
                                     ref CullResults cullResults,
                                     ref RenderingData renderingData)
        {
            // Restore Render target for additional editor rendering.
            // Note: Scene view camera always perform depth prepass
            CommandBuffer cmd = CommandBufferPool.Get(k_CopyDepthToCameraTag);

            CoreUtils.SetRenderTarget(cmd, BuiltinRenderTextureType.CameraTarget);
            cmd.SetGlobalTexture("_CameraDepthAttachment", source.Identifier());
            cmd.EnableShaderKeyword(LightweightKeywordStrings.DepthNoMsaa);
            cmd.DisableShaderKeyword(LightweightKeywordStrings.DepthMsaa2);
            cmd.DisableShaderKeyword(LightweightKeywordStrings.DepthMsaa4);
            cmd.Blit(source.Identifier(), BuiltinRenderTextureType.CameraTarget, renderer.GetMaterial(MaterialHandles.DepthCopy));
            context.ExecuteCommandBuffer(cmd);
            CommandBufferPool.Release(cmd);
        }
예제 #2
0
    public override void Render(ScriptableRenderContext context, Camera[] cameras)
    {
        base.Render(context, cameras);

        foreach (var camera in cameras)
        {
            // Culling
            ScriptableCullingParameters cullingParams;
            if (!CullResults.GetCullingParameters(camera, out cullingParams))
            {
                continue;
            }

            CullResults cull = CullResults.Cull(ref cullingParams, context);

            // Setup camera for rendering (sets render target, view/projection matrices and other
            // per-camera built-in shader variables).
            context.SetupCameraProperties(camera);

            // clear depth buffer
            var cmd = new CommandBuffer();
            cmd.ClearRenderTarget(true, false, Color.black);
            context.ExecuteCommandBuffer(cmd);
            cmd.Release();

            // Draw opaque objects using BasicPass shader pass
            var settings = new DrawRendererSettings(camera, new ShaderPassName("BasicPass"));
            settings.sorting.flags = SortFlags.CommonOpaque;

            var filterSettings = new FilterRenderersSettings(true)
            {
                renderQueueRange = RenderQueueRange.opaque
            };
            context.DrawRenderers(cull.visibleRenderers, ref settings, filterSettings);

            // Draw skybox
            context.DrawSkybox(camera);

            // Draw transparent objects using BasicPass shader pass
            settings.sorting.flags          = SortFlags.CommonTransparent;
            filterSettings.renderQueueRange = RenderQueueRange.transparent;
            context.DrawRenderers(cull.visibleRenderers, ref settings, filterSettings);

            context.Submit();
        }
    }
        private void Render(PipelineCamera pipelineCam, RenderTargetIdentifier dest, ref ScriptableRenderContext context, Camera cam)
        {
            CameraRenderingPath path = pipelineCam.renderingPath;

            pipelineCam.cam = cam;
            pipelineCam.EnableThis();
            if (!CullResults.GetCullingParameters(cam, out data.cullParams))
            {
                return;
            }
            context.SetupCameraProperties(cam);
            //Set Global Data
            data.defaultDrawSettings = new DrawRendererSettings(cam, new ShaderPassName(""));
            data.context             = context;
            data.cullResults         = CullResults.Cull(ref data.cullParams, context);

            PipelineFunctions.InitRenderTarget(ref pipelineCam.targets, cam, data.buffer);
            data.resources = resources;
            PipelineFunctions.GetViewProjectMatrix(cam, out data.vp, out data.inverseVP);
            for (int i = 0; i < data.frustumPlanes.Length; ++i)
            {
                Plane p = data.cullParams.GetCullingPlane(i);
                //GPU Driven RP's frustum plane is inverse from SRP's frustum plane
                data.frustumPlanes[i] = new Vector4(-p.normal.x, -p.normal.y, -p.normal.z, -p.distance);
            }
            DrawEvent evt;

            if (allDrawEvents.TryGetValue(path, out evt))
            {
                //Pre Calculate Events
                foreach (var i in evt.preRenderEvents)
                {
                    i.PreRenderFrame(pipelineCam, ref data);
                }
                //Run job system together
                JobHandle.ScheduleBatchedJobs();
                //Start Prepare Render Targets
                //Frame Update Events
                foreach (var i in evt.drawEvents)
                {
                    i.FrameUpdate(pipelineCam, ref data);
                }
            }
            data.buffer.Blit(pipelineCam.targets.renderTargetIdentifier, dest);
            data.ExecuteCommandBuffer();
        }
예제 #4
0
        void RenderGBuffer(CullResults cull, Camera camera, ScriptableRenderContext renderContext)
        {
            if (m_Owner.renderingSettings.ShouldUseForwardRenderingOnly())
            {
                return;
            }

            using (new Utilities.ProfilingSample("GBuffer Pass", renderContext))
            {
                bool debugLighting = globalDebugSettings.lightingDebugSettings.lightingDebugMode != LightingDebugMode.None;

                // setup GBuffer for rendering
                Utilities.SetRenderTarget(renderContext, m_gbufferManager.GetGBuffers(), m_CameraDepthStencilBufferRT);
                // render opaque objects into GBuffer
                RenderOpaqueRenderList(cull, camera, renderContext, debugLighting ? "GBufferDebugLighting" : "GBuffer", Utilities.kRendererConfigurationBakedLighting);
            }
        }
예제 #5
0
        void RenderDepthPrepass(CullResults cull, Camera camera, ScriptableRenderContext renderContext)
        {
            // If we are forward only we will do a depth prepass
            // TODO: Depth prepass should be enabled based on light loop settings. LightLoop define if they need a depth prepass + forward only...
            if (!debugParameters.useDepthPrepass)
            {
                return;
            }

            using (new Utilities.ProfilingSample("Depth Prepass", renderContext))
            {
                // TODO: Must do opaque then alpha masked for performance!
                // TODO: front to back for opaque and by materal for opaque tested when we split in two
                Utilities.SetRenderTarget(renderContext, m_CameraDepthStencilBufferRT);
                RenderOpaqueRenderList(cull, camera, renderContext, "DepthOnly");
            }
        }
예제 #6
0
        public void Setup(ref ScriptableRenderContext context, ref CullResults cullResults, ref RenderingData renderingData)
        {
            Clear();

            SetupPerObjectLightIndices(ref cullResults, ref renderingData.lightData);
            RenderTextureDescriptor baseDescriptor = CreateRTDesc(ref renderingData.cameraData);

            bool requiresCameraDepth  = renderingData.cameraData.requiresDepthTexture;
            bool requiresDepthPrepass = renderingData.shadowData.requiresScreenSpaceShadowResolve ||
                                        renderingData.cameraData.isSceneViewCamera || (requiresCameraDepth && !CanCopyDepth(ref renderingData.cameraData));

            // For now VR requires a depth prepass until we figure out how to properly resolve texture2DMS in stereo
            requiresDepthPrepass |= renderingData.cameraData.isStereoEnabled;

            CommandBuffer cmd = CommandBufferPool.Get("Setup Rendering");

            if (requiresDepthPrepass)
            {
                EnqueuePass(cmd, RenderPassHandles.DepthPrepass, baseDescriptor, null, RenderTargetHandles.DepthTexture);
            }

            if (renderingData.shadowData.renderDirectionalShadows)
            {
                EnqueuePass(cmd, RenderPassHandles.DirectionalShadows, baseDescriptor);
                if (renderingData.shadowData.requiresScreenSpaceShadowResolve)
                {
                    EnqueuePass(cmd, RenderPassHandles.ScreenSpaceShadowResolve, baseDescriptor, new[] { RenderTargetHandles.ScreenSpaceShadowmap });
                }
            }

            if (renderingData.shadowData.renderLocalShadows)
            {
                EnqueuePass(cmd, RenderPassHandles.LocalShadows, baseDescriptor);
            }

            bool requiresDepthAttachment = requiresCameraDepth && !requiresDepthPrepass;
            bool requiresColorAttachment = RequiresColorAttachment(ref renderingData.cameraData, baseDescriptor) || requiresDepthAttachment;

            int[] colorHandles = (requiresColorAttachment) ? new[] { RenderTargetHandles.Color } : null;
            int   depthHandle  = (requiresColorAttachment) ? RenderTargetHandles.DepthAttachment : -1;

            EnqueuePass(cmd, RenderPassHandles.ForwardLit, baseDescriptor, colorHandles, depthHandle, renderingData.cameraData.msaaSamples);

            context.ExecuteCommandBuffer(cmd);
            CommandBufferPool.Release(cmd);
        }
    public override void Render(ScriptableRenderContext context, Camera[] cameras)
    {
        foreach (var camera in cameras)
        {
            cmd.Clear();
            cmd.GetTemporaryRT(_ScreenSpaceShadowMapId, shadowResolution, shadowResolution, 32, FilterMode.Bilinear, RenderTextureFormat.ARGB32);
            cmd.GetTemporaryRT(_CameraDepthId, camera.pixelWidth, camera.pixelHeight, 32, FilterMode.Bilinear, RenderTextureFormat.ARGB32);
            cmd.GetTemporaryRT(_ShadowColorId, camera.pixelWidth, camera.pixelHeight, 32, FilterMode.Bilinear, RenderTextureFormat.ARGB32);
            cmd.GetTemporaryRT(_OpaqueId, camera.pixelWidth, camera.pixelHeight, 32, FilterMode.Bilinear, RenderTextureFormat.ARGB32);
            context.ExecuteCommandBuffer(cmd);

            CullResults cull;
            ScriptableCullingParameters cullingParams;
            if (!CullResults.GetCullingParameters(camera, out cullingParams))
            {
                continue;
            }
            cullingParams.shadowDistance = Mathf.Min(1000, camera.farClipPlane);

            CullResults.Cull(camera, context, out cull);

            shadow_pass(ref cull, ref context);

            context.SetupCameraProperties(camera);

            depth_pass(camera, ref cull, ref context);

            collect_shadow(camera, ref cull, ref context);

            opaque_pass(camera, ref cull, ref context);

            transparent_pass(camera, ref cull, ref context);

            blit_pass(camera, ref cull, ref context);

            cmd.Clear();
            cmd.ReleaseTemporaryRT(_ScreenSpaceShadowMapId);
            cmd.ReleaseTemporaryRT(_CameraDepthId);
            cmd.ReleaseTemporaryRT(_ShadowColorId);
            cmd.ReleaseTemporaryRT(_OpaqueId);
            context.ExecuteCommandBuffer(cmd);

            context.Submit();
        }
    }
예제 #8
0
        public override void Render(ScriptableRenderContext context, Camera[] cameras)
        {
            base.Render(context, cameras);

            foreach (var camera in cameras)
            {
                if (!CullResults.Cull(camera, context, out m_CullResults))
                {
                    continue;
                }

                context.SetupCameraProperties(camera);

                {
                    var cmd = CommandBufferPool.Get("Clear");
                    cmd.ClearRenderTarget(true, true, Color.black);
                    context.ExecuteCommandBuffer(cmd);
                    CommandBufferPool.Release(cmd);
                }

                // SRP doesn't allow us to 100% control drawing of renderers, but we can configure a lot of things
                // via the DrawRendererSettings and FilterRenderersSettings.
                // Here we specify the camera we want to use, and that we want to use the "Forward" shader pass. In the
                // shader source this filters passes based on the "LightMode" tag.
                var drawSettings = new DrawRendererSettings(camera, new ShaderPassName("Forward"));
                // The `true` passed to the constructor here will initalize values in the struct to sensible defaults.
                var filterSettings = new FilterRenderersSettings(true);

                // We want to use typical opaque sorting.
                drawSettings.sorting.flags = SortFlags.CommonOpaque;
                // We specify the render queue range to only include opaque objects, thus filtering out e.g. transparent
                // objects.
                filterSettings.renderQueueRange = RenderQueueRange.opaque;
                // We can now draw the renderers using the settings we just set up.
                context.DrawRenderers(m_CullResults.visibleRenderers, ref drawSettings, filterSettings);

                // Draw transparent renderers by setting the sorting flags and render queue range similarly to how we
                // did for the opaque renderers.
                drawSettings.sorting.flags      = SortFlags.CommonTransparent;
                filterSettings.renderQueueRange = RenderQueueRange.transparent;
                context.DrawRenderers(m_CullResults.visibleRenderers, ref drawSettings, filterSettings);

                context.Submit();
            }
        }
예제 #9
0
    public void SRP04Rendering(ScriptableRenderContext context, IEnumerable <Camera> cameras)
    {
        foreach (Camera camera in cameras)
        {
            // Culling
            ScriptableCullingParameters cullingParams;
            if (!CullResults.GetCullingParameters(camera, out cullingParams))
            {
                continue;
            }
            CullResults cull = CullResults.Cull(ref cullingParams, context);

            // Camera settings
            context.SetupCameraProperties(camera);

            // RenderPass
            using (RenderPass rp = new RenderPass(context, camera.pixelWidth, camera.pixelHeight, 1, new[] { m_Albedo, m_Emission, m_Output }, m_Depth))
            {
                using (new RenderPass.SubPass(rp, new[] { m_Albedo, m_Emission }, null))
                {
                    var settings = new DrawRendererSettings(camera, new ShaderPassName("BasicPass"))
                    {
                        sorting = { flags = SortFlags.CommonOpaque }
                    };
                    var fs = new FilterRenderersSettings(true);
                    fs.renderQueueRange = RenderQueueRange.opaque;
                    context.DrawRenderers(cull.visibleRenderers, ref settings, fs);
                }

                using (new RenderPass.SubPass(rp, new[] { m_Output }, new[] { m_Albedo, m_Emission }, false))
                {
                    context.DrawSkybox(camera);
                    var settings = new DrawRendererSettings(camera, new ShaderPassName("AddPass"))
                    {
                        sorting = { flags = SortFlags.CommonOpaque }
                    };
                    var fs = new FilterRenderersSettings(true);
                    fs.renderQueueRange = RenderQueueRange.opaque;
                    context.DrawRenderers(cull.visibleRenderers, ref settings, fs);
                }
            }

            context.Submit();
        }
    }
    private void transparent_pass(Camera camera, ref CullResults cull, ref ScriptableRenderContext context)
    {
        var settings = new DrawRendererSettings(camera, new ShaderPassName("sonil_light_base"))
        {
            flags = DrawRendererFlags.EnableDynamicBatching | DrawRendererFlags.EnableInstancing,
            rendererConfiguration = RendererConfiguration.PerObjectLightIndices8,
        };

        settings.sorting.flags = SortFlags.CommonTransparent;

        var filter = new FilterRenderersSettings(true)
        {
            renderQueueRange = RenderQueueRange.transparent,
            layerMask        = ~0,
        };

        context.DrawRenderers(cull.visibleRenderers, ref settings, filter);
    }
예제 #11
0
        private void FillLightIndices(ref CullResults cullResults, int visibleLightsCount)
        {
            //int visibleRenderersCount = cullResults.GetVisibleRenderersCount();
            // TODO: commenting cullResults.GetVisislbeRenderersCount() to avoid compiler errors as it is not in main SRP trunk yet
            // For now setting a small amount enough for the test scenes.
            int visibleRenderersCount = 1024;

            if (visibleRenderersCount > m_LightIndicesCount)
            {
                m_LightIndicesCount = visibleRenderersCount * visibleLightsCount;
                if (m_LightIndexListBuffer != null)
                {
                    m_LightIndexListBuffer.Release();
                }
                m_LightIndexListBuffer = new ComputeBuffer(m_LightIndicesCount, sizeof(uint));
            }
            cullResults.FillLightIndices(m_LightIndexListBuffer);
        }
        public bool Execute(ref CullResults cullResults, ref LightData lightData, ref ScriptableRenderContext context)
        {
            Clear();

            bool directionalShadowmapRendered = false;

            if (IsDirectionalShadowsEnabled)
            {
                directionalShadowmapRendered = RenderDirectionalCascadeShadowmap(ref cullResults, ref lightData, ref context);
            }

            if (IsLocalShadowsEnabled)
            {
                RenderLocalShadowmapAtlas(ref cullResults, ref lightData, ref context);
            }

            return(directionalShadowmapRendered && m_ShadowSettings.screenSpace);
        }
예제 #13
0
        public void RenderObjectsWithError(ScriptableRenderContext context, ref CullResults cullResults, Camera camera, FilterRenderersSettings filterSettings, SortFlags sortFlags)
        {
            Material errorMaterial = GetMaterial(MaterialHandles.Error);

            if (errorMaterial != null)
            {
                DrawRendererSettings errorSettings = new DrawRendererSettings(camera, m_LegacyShaderPassNames[0]);
                for (int i = 1; i < m_LegacyShaderPassNames.Count; ++i)
                {
                    errorSettings.SetShaderPassName(i, m_LegacyShaderPassNames[i]);
                }

                errorSettings.sorting.flags         = sortFlags;
                errorSettings.rendererConfiguration = RendererConfiguration.None;
                errorSettings.SetOverrideMaterial(errorMaterial, 0);
                context.DrawRenderers(cullResults.visibleRenderers, ref errorSettings, filterSettings);
            }
        }
예제 #14
0
        public override void Render(ScriptableRenderContext renderContext, Camera[] cameras)
        {
            base.Render(renderContext, cameras);

            foreach (var camera in cameras)
            {
                ScriptableCullingParameters cullingParams;
                if (!CullResults.GetCullingParameters(camera, out cullingParams))
                {
                    continue;
                }

                cullingParams.shadowDistance = Mathf.Min(2000, camera.farClipPlane);

                CullResults cull = CullResults.Cull(ref cullingParams, renderContext);

                renderContext.SetupCameraProperties(camera);

                var cmd = new CommandBuffer();
                cmd.ClearRenderTarget(true, false, m_ClearColor);
                renderContext.ExecuteCommandBuffer(cmd);

                cmd.Release();

                var settings = new DrawRendererSettings(camera, new ShaderPassName("basic"));
                settings.sorting.flags = SortFlags.CommonOpaque;

                var filterSettings = new FilterRenderersSettings(true)
                {
                    renderQueueRange = RenderQueueRange.opaque
                };
                renderContext.DrawRenderers(cull.visibleRenderers, ref settings, filterSettings);

                var shadowSettings = new DrawShadowsSettings(cull, 0);
                //renderContext.DrawShadows(ref shadowSettings);

                renderContext.DrawSkybox(camera);

                settings.sorting.flags          = SortFlags.CommonTransparent;
                filterSettings.renderQueueRange = RenderQueueRange.transparent;
                renderContext.DrawRenderers(cull.visibleRenderers, ref settings, filterSettings);
            }
            renderContext.Submit();
        }
예제 #15
0
        public override void Execute(ScriptableRenderer renderer, ref ScriptableRenderContext context,
                                     ref CullResults cullResults,
                                     ref RenderingData renderingData)
        {
            CommandBuffer          cmd              = CommandBufferPool.Get(k_DepthCopyTag);
            RenderTargetIdentifier depthSurface     = source.Identifier();
            RenderTargetIdentifier copyDepthSurface = destination.Identifier();
            Material depthCopyMaterial              = renderer.GetMaterial(MaterialHandles.DepthCopy);

            RenderTextureDescriptor descriptor = ScriptableRenderer.CreateRTDesc(ref renderingData.cameraData);

            descriptor.colorFormat     = RenderTextureFormat.Depth;
            descriptor.depthBufferBits = 32; //TODO: fix this ;
            descriptor.msaaSamples     = 1;
            descriptor.bindMS          = false;
            cmd.GetTemporaryRT(destination.id, descriptor, FilterMode.Point);

            cmd.SetGlobalTexture("_CameraDepthAttachment", source.Identifier());

            if (renderingData.cameraData.msaaSamples > 1)
            {
                cmd.DisableShaderKeyword(LightweightKeywordStrings.DepthNoMsaa);
                if (renderingData.cameraData.msaaSamples == 4)
                {
                    cmd.DisableShaderKeyword(LightweightKeywordStrings.DepthMsaa2);
                    cmd.EnableShaderKeyword(LightweightKeywordStrings.DepthMsaa4);
                }
                else
                {
                    cmd.EnableShaderKeyword(LightweightKeywordStrings.DepthMsaa2);
                    cmd.DisableShaderKeyword(LightweightKeywordStrings.DepthMsaa4);
                }
                cmd.Blit(depthSurface, copyDepthSurface, depthCopyMaterial);
            }
            else
            {
                cmd.EnableShaderKeyword(LightweightKeywordStrings.DepthNoMsaa);
                cmd.DisableShaderKeyword(LightweightKeywordStrings.DepthMsaa2);
                cmd.DisableShaderKeyword(LightweightKeywordStrings.DepthMsaa4);
                LightweightPipeline.CopyTexture(cmd, depthSurface, copyDepthSurface, depthCopyMaterial);
            }
            context.ExecuteCommandBuffer(cmd);
            CommandBufferPool.Release(cmd);
        }
예제 #16
0
        void RenderDebugViewMaterial(CullResults cull, HDCamera hdCamera, ScriptableRenderContext renderContext)
        {
            using (new Utilities.ProfilingSample("DebugView Material Mode Pass", renderContext))
            // Render Opaque forward
            {
                Utilities.SetRenderTarget(renderContext, m_CameraColorBufferRT, m_CameraDepthStencilBufferRT, Utilities.kClearAll, Color.black);

                Shader.SetGlobalInt("_DebugViewMaterial", (int)debugParameters.debugViewMaterial);

                RenderOpaqueRenderList(cull, hdCamera.camera, renderContext, "DebugViewMaterial", Utilities.kRendererConfigurationBakedLighting);
            }

            // Render GBuffer opaque
            if (!debugParameters.ShouldUseForwardRenderingOnly())
            {
                Utilities.SetupMaterialHDCamera(hdCamera, m_DebugViewMaterialGBuffer);
                m_DebugViewMaterialGBuffer.SetFloat("_DebugViewMaterial", (float)debugParameters.debugViewMaterial);

                // m_gbufferManager.BindBuffers(m_DebugViewMaterialGBuffer);
                // TODO: Bind depth textures
                var cmd = new CommandBuffer {
                    name = "GBuffer Debug Pass"
                };
                cmd.Blit(null, m_CameraColorBufferRT, m_DebugViewMaterialGBuffer, 0);
                renderContext.ExecuteCommandBuffer(cmd);
                cmd.Dispose();
            }

            // Render forward transparent
            {
                RenderTransparentRenderList(cull, hdCamera.camera, renderContext, "DebugViewMaterial", Utilities.kRendererConfigurationBakedLighting);
            }

            // Last blit
            {
                var cmd = new CommandBuffer {
                    name = "Blit DebugView Material Debug"
                };
                cmd.Blit(m_CameraColorBufferRT, BuiltinRenderTextureType.CameraTarget);
                renderContext.ExecuteCommandBuffer(cmd);
                cmd.Dispose();
            }
        }
        void RenderGBuffer(CullResults cull, Camera camera, RenderLoop renderLoop)
        {
            if (debugParameters.useForwardRenderingOnly)
            {
                return;
            }

            // setup GBuffer for rendering
            var cmd = new CommandBuffer {
                name = "GBuffer Pass"
            };

            cmd.SetRenderTarget(m_gbufferManager.GetGBuffers(cmd), new RenderTargetIdentifier(s_CameraDepthBuffer));
            renderLoop.ExecuteCommandBuffer(cmd);
            cmd.Dispose();

            // render opaque objects into GBuffer
            RenderOpaqueRenderList(cull, camera, renderLoop, "GBuffer");
        }
예제 #18
0
    public override void Render(ScriptableRenderContext renderContext, Camera[] cameras)
    {
        base.Render(renderContext, cameras);
        if (_cb == null)
        {
            _cb = new CommandBuffer();
        }
        foreach (var camera in cameras)
        {
            renderContext.SetupCameraProperties(camera);
            _cb.name = "Setup";
            //显式将当前渲染目标设置为相机Backbuffer。
            _cb.SetRenderTarget(BuiltinRenderTextureType.CameraTarget);
            //设置渲染目标的颜色为相机背景色
            _cb.ClearRenderTarget(true, true, camera.backgroundColor);
            renderContext.ExecuteCommandBuffer(_cb);
            _cb.Clear();
            //绘制天空盒子,注意需要在ClearRenderTarget之后进行,不然颜色会被覆盖。
            renderContext.DrawSkybox(camera);

            //设置裁剪
            var culled = new CullResults();
            CullResults.Cull(camera, renderContext, out culled);

            //设置Renderer Settings
            //在构造的时候就需要传入Lightmode参数
            //使用Shader中指定光照模式为Always的pass !!!
            var ds = new DrawRendererSettings(camera, new ShaderPassName("Always"));
            ds.sorting.flags = SortFlags.None;

            //设置过滤
            var fs = new FilterRenderersSettings(true);
            //只绘制不透明物体
            fs.renderQueueRange = RenderQueueRange.opaque;
            //绘制所有层
            fs.layerMask = ~0;

            //绘制物体
            renderContext.DrawRenderers(culled.visibleRenderers, ref ds, fs);

            renderContext.Submit();
        }
    }
    // Main entry point for our scriptable render loop

    public static void Render(ScriptableRenderContext context, IEnumerable <Camera> cameras)
    {
        foreach (var camera in cameras)
        {
            // Culling
            ScriptableCullingParameters cullingParams;
            if (!CullResults.GetCullingParameters(camera, out cullingParams))
            {
                continue;
            }
            CullResults cull = new CullResults();
            CullResults.Cull(ref cullingParams, context, ref cull);

            // Setup camera for rendering (sets render target, view/projection matrices and other
            // per-camera built-in shader variables).
            context.SetupCameraProperties(camera);

            // clear depth buffer
            var cmd = CommandBufferPool.Get();
            cmd.ClearRenderTarget(true, false, Color.black);
            context.ExecuteCommandBuffer(cmd);
            CommandBufferPool.Release(cmd);

            // Setup global lighting shader variables
            SetupLightShaderVariables(cull.visibleLights, context);

            // Draw opaque objects using BasicPass shader pass
            var settings = new DrawRendererSettings(cull, camera, new ShaderPassName("BasicPass"));
            settings.sorting.flags = SortFlags.CommonOpaque;
            settings.inputFilter.SetQueuesOpaque();
            context.DrawRenderers(ref settings);

            // Draw skybox
            context.DrawSkybox(camera);

            // Draw transparent objects using BasicPass shader pass
            settings.sorting.flags = SortFlags.CommonTransparent;
            settings.inputFilter.SetQueuesTransparent();
            context.DrawRenderers(ref settings);

            context.Submit();
        }
    }
예제 #20
0
        public static bool ExtractDirectionalLightMatrix(ref CullResults cullResults, ref ShadowData shadowData,
                                                         int shadowLightIndex, int cascadeIndex, int shadowResolution, float shadowNearPlane, float shadowFarPlane,
                                                         out Vector4 cascadeSplitDistance, out ShadowSliceData shadowSliceData, out Matrix4x4 viewMatrix, out Matrix4x4 projMatrix)
        {
            ShadowSplitData splitData;
            Vector3         splitRotio = Vector3.one;

            if (shadowNearPlane > 1f)
            {
                float r = Mathf.Pow(shadowFarPlane / shadowNearPlane, 1f / shadowData.directionalLightCascadeCount);
                splitRotio = new Vector3(shadowNearPlane * r, shadowNearPlane * r * r, shadowNearPlane * r * r * r) / shadowFarPlane;
            }
            else
            {
                if (shadowData.directionalLightCascadeCount == 1)
                {
                    splitRotio = new Vector3(1f, 0f, 0f);
                }
                else if (shadowData.directionalLightCascadeCount == 2)
                {
                    splitRotio = new Vector3(0.25f, 1f, 0f);
                }
                else
                {
                    splitRotio = new Vector3(0.067f, 0.2f, 0.467f);
                }
            }
            bool success = cullResults.ComputeDirectionalShadowMatricesAndCullingPrimitives(shadowLightIndex, cascadeIndex,
                                                                                            shadowData.directionalLightCascadeCount, splitRotio, shadowResolution, shadowNearPlane, out viewMatrix, out projMatrix, out splitData);

            cascadeSplitDistance            = splitData.cullingSphere;
            shadowSliceData.offsetX         = (cascadeIndex % 2) * shadowResolution;
            shadowSliceData.offsetY         = (cascadeIndex / 2) * shadowResolution;
            shadowSliceData.resolution      = shadowResolution;
            shadowSliceData.shadowTransform = GetShadowTransform(projMatrix, viewMatrix);

            if (shadowData.directionalLightCascadeCount > 1)
            {
                ApplySliceTransform(ref shadowSliceData, shadowData.directionalShadowAltasRes, shadowData.directionalShadowAltasRes);
            }

            return(success);
        }
예제 #21
0
    public void ExecuteRenderLoop(Camera camera, CullResults cullResults, ScriptableRenderContext scriptableRenderContext)
    {
        // Bind the albedo surface to the current camera target, so the final pass will render the scene to the screen backbuffer
        // The second argument specifies whether the existing contents of the surface need to be loaded as the initial values;
        // in our case we do not need that because we'll be clearing the attachment anyway. This saves a lot of memory
        // bandwidth on tiled GPUs.
        // The third argument specifies whether the rendering results need to be written out to memory at the end of
        // the renderpass. We need this as we'll be generating the final image there.
        // We could do this in the constructor already, but the camera target may change on the fly, esp. in the editor
        m_Albedo.BindSurface(BuiltinRenderTextureType.CameraTarget, false, true);

        // All other attachments are transient surfaces that are not stored anywhere. If the renderer allows,
        // those surfaces do not even have a memory allocated for the pixel values, saving RAM usage.

        // Start the renderpass using the given scriptable rendercontext, resolution, samplecount, array of attachments that will be used within the renderpass and the depth surface
        using (RenderPass rp = new RenderPass(scriptableRenderContext, camera.pixelWidth, camera.pixelHeight, 1, new[] { m_Albedo, m_SpecRough, m_Normal, m_Emission }, m_Depth))
        {
            // Start the first subpass, GBuffer creation: render to albedo, specRough, normal and emission, no need to read any input attachments
            using (new RenderPass.SubPass(rp, new[] { m_Albedo, m_SpecRough, m_Normal, m_Emission }, null))
            {
                // Render the deferred G-Buffer
                RenderGBuffer(cullResults, camera, scriptableRenderContext);
            }
            // Second subpass, lighting: Render to the emission buffer, read from albedo, specRough, normal and depth.
            // The last parameter indicates whether the depth buffer can be bound as read-only.
            // Note that some renderers (notably iOS Metal) won't allow reading from the depth buffer while it's bound as Z-buffer,
            // so those renderers should write the Z into an additional FP32 render target manually in the pixel shader and read from it instead
            using (new RenderPass.SubPass(rp, new[] { m_Emission }, new[] { m_Albedo, m_SpecRough, m_Normal, m_Depth }, true))
            {
                PushGlobalShadowParams(scriptableRenderContext);

                RenderLighting(camera, cullResults, scriptableRenderContext);

                scriptableRenderContext.DrawSkybox(camera);
            }
            // Third subpass, tonemapping: Render to albedo (which is bound to the camera target), read from emission.
            using (new RenderPass.SubPass(rp, new[] { m_Albedo }, new[] { m_Emission }, true))
            {
                // present frame buffer.
                FinalPass(scriptableRenderContext);
            }
        }
    }
    /// <summary>
    /// 実際の描画処理
    /// </summary>
    public override void Render(ScriptableRenderContext context, Camera[] cameras)
    {
        base.Render(context, cameras);
        if (cmd == null)
        {
            cmd = new CommandBuffer();
        }
        int idx = 0;

        foreach (var camera in cameras)
        {
            // Cullingします
            if (!CullResults.GetCullingParameters(camera, out cullingParams))
            {
                continue;
            }
            CullResults.Cull(ref cullingParams, context, ref cull);

            // カメラに関するShaderパラメーターをセットアップします
            context.SetupCameraProperties(camera);

            // 画面をクリアします
            cmd.Clear();
            cmd.ClearRenderTarget(true, true, Color.black, 1.0f);
            context.ExecuteCommandBuffer(cmd);

            // Directional Lightの値を設定します
            SetUpDirectionalLightParam(cull.visibleLights);

            // キャラクターを ZPrepassで描画します
            DrawCharacter(context, camera, zPrepass, SortFlags.CommonOpaque);
            // BGをBasicPassで描画します
            DrawBg(context, camera);
            // キャラクターをBasicPassで描画します
            DrawCharacter(context, camera, basicPass, SortFlags.OptimizeStateChanges);
            // 最後に影を描画します
            DrawShadow(context, camera);

            // 描画内容をコミットします
            context.Submit();
            ++idx;
        }
    }
예제 #23
0
        public void Execute(ref ScriptableRenderContext context, ref CullResults cullResults, ref RenderingData renderingData)
        {
            // TODO: The reason we have to separate passes into two queues is because shadows require different camera
            // context. We need to take a look at approaches to effectively share shadow between cameras, then we
            // can move this out
            for (int i = 0; i < m_ActiveShadowQueue.Count; ++i)
            {
                m_ActiveShadowQueue[i].Execute(ref context, ref cullResults, ref renderingData);
            }

            // SetupCameraProperties does the following:
            // Setup Camera RenderTarget and Viewport
            // VR Camera Setup and SINGLE_PASS_STEREO props
            // Setup camera view, proj and their inv matrices.
            // Setup properties: _WorldSpaceCameraPos, _ProjectionParams, _ScreenParams, _ZBufferParams, unity_OrthoParams
            // Setup camera world clip planes props
            // setup HDR keyword
            // Setup global time properties (_Time, _SinTime, _CosTime)
            context.SetupCameraProperties(renderingData.cameraData.camera, renderingData.cameraData.isStereoEnabled);

            for (int i = 0; i < m_ActiveRenderPassQueue.Count; ++i)
            {
                m_ActiveRenderPassQueue[i].Execute(ref context, ref cullResults, ref renderingData);
            }

#if UNITY_EDITOR
            if (renderingData.cameraData.isSceneViewCamera)
            {
                // Restore Render target for additional editor rendering.
                // Note: Scene view camera always perform depth prepass
                CommandBuffer cmd = CommandBufferPool.Get("Copy Depth to Camera");
                CoreUtils.SetRenderTarget(cmd, BuiltinRenderTextureType.CameraTarget);
                cmd.EnableShaderKeyword(LightweightKeywordStrings.DepthNoMsaa);
                cmd.DisableShaderKeyword(LightweightKeywordStrings.DepthMsaa2);
                cmd.DisableShaderKeyword(LightweightKeywordStrings.DepthMsaa4);
                cmd.Blit(GetSurface(RenderTargetHandles.DepthTexture), BuiltinRenderTextureType.CameraTarget, GetMaterial(MaterialHandles.DepthCopy));
                context.ExecuteCommandBuffer(cmd);
                CommandBufferPool.Release(cmd);
            }
#endif

            DisposePasses(ref context);
        }
예제 #24
0
        void RenderMainLightCascadeShadowmap(ref ScriptableRenderContext context, ref CullResults cullResults, ref LightData lightData, ref ShadowData shadowData)
        {
            int shadowLightIndex = lightData.mainLightIndex;

            if (shadowLightIndex == -1)
            {
                return;
            }

            VisibleLight shadowLight = lightData.visibleLights[shadowLightIndex];

            CommandBuffer cmd = CommandBufferPool.Get(k_RenderMainLightShadowmapTag);

            using (new ProfilingSample(cmd, k_RenderMainLightShadowmapTag))
            {
                var settings = new DrawShadowsSettings(cullResults, shadowLightIndex);

                m_MainLightShadowmapTexture = RenderTexture.GetTemporary(shadowData.mainLightShadowmapWidth,
                                                                         shadowData.mainLightShadowmapHeight, k_ShadowmapBufferBits, m_ShadowmapFormat);
                m_MainLightShadowmapTexture.filterMode = FilterMode.Bilinear;
                m_MainLightShadowmapTexture.wrapMode   = TextureWrapMode.Clamp;
                SetRenderTarget(cmd, m_MainLightShadowmapTexture, RenderBufferLoadAction.DontCare,
                                RenderBufferStoreAction.Store, ClearFlag.Depth, Color.black, TextureDimension.Tex2D);

                for (int cascadeIndex = 0; cascadeIndex < m_ShadowCasterCascadesCount; ++cascadeIndex)
                {
                    settings.splitData.cullingSphere = m_CascadeSplitDistances[cascadeIndex];
                    Vector4 shadowBias = ShadowUtils.GetShadowBias(ref shadowLight, shadowLightIndex, ref shadowData, m_CascadeSlices[cascadeIndex].projectionMatrix, m_CascadeSlices[cascadeIndex].resolution);
                    ShadowUtils.SetupShadowCasterConstantBuffer(cmd, ref shadowLight, shadowBias);
                    ShadowUtils.RenderShadowSlice(cmd, ref context, ref m_CascadeSlices[cascadeIndex],
                                                  ref settings, m_CascadeSlices[cascadeIndex].projectionMatrix, m_CascadeSlices[cascadeIndex].viewMatrix);
                }

                SetupMainLightShadowReceiverConstants(cmd, ref shadowData, shadowLight);
            }

            CoreUtils.SetKeyword(cmd, ShaderKeywordStrings.MainLightShadows, true);
            CoreUtils.SetKeyword(cmd, ShaderKeywordStrings.MainLightShadowCascades, shadowData.mainLightShadowCascadesCount > 1);
            CoreUtils.SetKeyword(cmd, ShaderKeywordStrings.SoftShadows, shadowLight.light.shadows == LightShadows.Soft && shadowData.supportsSoftShadows);
            context.ExecuteCommandBuffer(cmd);
            CommandBufferPool.Release(cmd);
        }
예제 #25
0
        public static void RenderSingleCamera(ScriptableRenderContext context, Camera camera,
                                              ref CullResults cullResults, IRendererSetup setup, ScriptableRenderer renderer)
        {
            CommandBuffer cmd = commandBufferPool.Get("RenderSingleCam");

            CameraData cameraData;

            InitializeCameraData(camera, out cameraData);

            ScriptableCullingParameters cullingParam;

            if (!CullResults.GetCullingParameters(camera, false, out cullingParam))
            {
                commandBufferPool.Release(cmd);
                return;
            }

            cullingParam.shadowDistance = Mathf.Min(ShadowDistance, camera.farClipPlane);
            context.ExecuteCommandBuffer(cmd);
            cmd.Clear();

#if UNITY_EDITOR
            //SceneView Camera
#endif
            CullResults.Cull(ref cullingParam, context, ref cullResults);
            RenderingData renderingData;
            InitializeRenderingData(ref cameraData, ref cullResults, out renderingData);

            IRendererSetup setupToUse = setup;
            if (setupToUse == null)
            {
                setupToUse = defaultRenderSetup;
            }

            renderer.Clear();
            setupToUse.Setup(renderer, ref renderingData);
            renderer.Execute(context, ref renderingData);

            context.ExecuteCommandBuffer(cmd);
            commandBufferPool.Release(cmd);
            context.Submit();
        }
        void RenderForward(CullResults cullResults, Camera camera, RenderLoop renderLoop)
        {
            // Bind material data
            m_LitRenderLoop.Bind();

            var cmd = new CommandBuffer {
                name = "Forward Pass"
            };

            cmd.SetRenderTarget(new RenderTargetIdentifier(s_CameraColorBuffer), new RenderTargetIdentifier(s_CameraDepthBuffer));
            renderLoop.ExecuteCommandBuffer(cmd);
            cmd.Dispose();

            if (debugParameters.useForwardRenderingOnly)
            {
                RenderOpaqueRenderList(cullResults, camera, renderLoop, "Forward");
            }

            RenderTransparentRenderList(cullResults, camera, renderLoop, "Forward");
        }
예제 #27
0
        void RenderTransparents(ref ScriptableRenderContext context, ref CullResults cullResults, ref CameraData cameraData, RendererConfiguration rendererConfiguration, bool dynamicBatching)
        {
            CommandBuffer cmd = CommandBufferPool.Get(k_RenderTransparentsTag);

            using (new ProfilingSample(cmd, k_RenderTransparentsTag))
            {
                Camera camera = cameraData.camera;
                SetRenderTarget(cmd, RenderBufferLoadAction.Load, RenderBufferStoreAction.Store, ClearFlag.None, Color.black);
                context.ExecuteCommandBuffer(cmd);
                cmd.Clear();

                var drawSettings = CreateDrawRendererSettings(camera, SortFlags.CommonTransparent, rendererConfiguration, dynamicBatching);
                context.DrawRenderers(cullResults.visibleRenderers, ref drawSettings, renderer.transparentFilterSettings);

                // Render objects that did not match any shader pass with error shader
                RenderObjectsWithError(ref context, ref cullResults, camera, renderer.transparentFilterSettings, SortFlags.None);
            }
            context.ExecuteCommandBuffer(cmd);
            CommandBufferPool.Release(cmd);
        }
예제 #28
0
        public override void Execute(ScriptableRenderer renderer, ref ScriptableRenderContext context,
                                     ref CullResults cullResults,
                                     ref RenderingData renderingData)
        {
            CommandBuffer cmd = CommandBufferPool.Get("Draw Skybox (Set RT's)");

            if (renderingData.cameraData.isStereoEnabled && XRGraphicsConfig.eyeTextureDesc.dimension == TextureDimension.Tex2DArray)
            {
                cmd.SetRenderTarget(colorAttachmentHandle.Identifier(), depthAttachmentHandle.Identifier(), 0, CubemapFace.Unknown, -1);
            }
            else
            {
                cmd.SetRenderTarget(colorAttachmentHandle.Identifier(), depthAttachmentHandle.Identifier());
            }

            context.ExecuteCommandBuffer(cmd);
            CommandBufferPool.Release(cmd);

            context.DrawSkybox(renderingData.cameraData.camera);
        }
        public override void Execute(ScriptableRenderer renderer, ref ScriptableRenderContext context,
                                     ref CullResults cullResults, ref RenderingData renderingData)
        {
            Camera camera = renderingData.cameraData.camera;

            m_CameraTarget.BindSurface(BuiltinRenderTextureType.CameraTarget, false, true);
            context.SetupCameraProperties(camera, false);

            using (RenderPass rp = new RenderPass(context, camera.pixelWidth, camera.pixelHeight, 1, new[] { m_GBufferDiffuse, m_GBufferSpecularAndRoughness, m_GBufferNormal, m_CameraTarget }, m_Depth))
            {
                using (new RenderPass.SubPass(rp, new[] { m_GBufferDiffuse, m_GBufferSpecularAndRoughness, m_GBufferNormal, m_CameraTarget }, null))
                {
                    RenderGBuffer(context, cullResults, camera);
                }

                using (new RenderPass.SubPass(rp, new[] { m_CameraTarget }, new[] { m_GBufferDiffuse, m_GBufferSpecularAndRoughness, m_GBufferNormal, m_Depth }, true))
                {
                    RenderLights(context, cullResults, renderingData.lightData);
                }
            }
        }
        public override void Execute(ref ScriptableRenderContext context, ref CullResults cullResults, ref RenderingData renderingData)
        {
            CommandBuffer cmd = CommandBufferPool.Get(k_RenderTransparentsTag);

            using (new ProfilingSample(cmd, k_RenderTransparentsTag))
            {
                SetRenderTarget(cmd, RenderBufferLoadAction.Load, RenderBufferStoreAction.Store, clearFlag, CoreUtils.ConvertSRGBToActiveColorSpace(clearColor));
                context.ExecuteCommandBuffer(cmd);
                cmd.Clear();

                Camera camera       = renderingData.cameraData.camera;
                var    drawSettings = CreateDrawRendererSettings(camera, SortFlags.CommonTransparent, rendererConfiguration, dynamicBatching);
                context.DrawRenderers(cullResults.visibleRenderers, ref drawSettings, renderer.transparentFilterSettings);

                // Render objects that did not match any shader pass with error shader
                RenderObjectsWithError(ref context, ref cullResults, camera, renderer.transparentFilterSettings, SortFlags.None);
            }

            context.ExecuteCommandBuffer(cmd);
            CommandBufferPool.Release(cmd);
        }