private void SetUpBuffers(Vector2 outputVPSizePixels)
        {
            bool bufferChange = false;

            if (preGlowTargetBuffer == null || preGlowTargetBuffer.Width != (uint)outputVPSizePixels.X || preGlowTargetBuffer.Height != (uint)outputVPSizePixels.Y)
            {
                if (preGlowTargetBufferRTV != null)
                {
                    preGlowTargetBufferRTV.Dispose();
                }
                if (preGlowTargetBufferSRV != null)
                {
                    preGlowTargetBufferSRV.Dispose();
                }
                if (preGlowTargetBuffer != null)
                {
                    preGlowTargetBuffer.Dispose();
                }

                preGlowTargetBuffer = textureBuilder
                                      .WithWidth((uint)outputVPSizePixels.X)
                                      .WithHeight((uint)outputVPSizePixels.Y)
                                      .WithPermittedBindings(GPUBindings.ReadableShaderResource | GPUBindings.RenderTarget)
                                      .WithUsage(ResourceUsage.Write);
                preGlowTargetBufferRTV = preGlowTargetBuffer.CreateRenderTargetView(0U);
                preGlowTargetBufferSRV = preGlowTargetBuffer.CreateView();

                bufferChange = true;
            }

            Vector2 outputSizeOverTwo = outputVPSizePixels * 0.5f;

            if (glowSrcBuffer == null || glowSrcBuffer.Width != (uint)outputSizeOverTwo.X || glowSrcBuffer.Height != (uint)outputSizeOverTwo.Y)
            {
                if (glowSrcBufferRTV != null)
                {
                    glowSrcBufferRTV.Dispose();
                }
                if (glowSrcBufferSRV != null)
                {
                    glowSrcBufferSRV.Dispose();
                }
                if (glowSrcBuffer != null)
                {
                    glowSrcBuffer.Dispose();
                }

                glowSrcBuffer = textureBuilder
                                .WithWidth((uint)outputSizeOverTwo.X)
                                .WithHeight((uint)outputSizeOverTwo.Y)
                                .WithPermittedBindings(GPUBindings.ReadableShaderResource | GPUBindings.RenderTarget)
                                .WithUsage(ResourceUsage.Write);
                glowSrcBufferRTV = glowSrcBuffer.CreateRenderTargetView(0U);
                glowSrcBufferSRV = glowSrcBuffer.CreateView();

                bufferChange = true;
            }

            if (glowDstBuffer == null || glowDstBuffer.Width != (uint)outputSizeOverTwo.X || glowDstBuffer.Height != (uint)outputSizeOverTwo.Y)
            {
                if (glowDstBufferRTV != null)
                {
                    glowDstBufferRTV.Dispose();
                }
                if (glowDstBufferSRV != null)
                {
                    glowDstBufferSRV.Dispose();
                }
                if (glowDstBuffer != null)
                {
                    glowDstBuffer.Dispose();
                }

                glowDstBuffer = textureBuilder
                                .WithWidth((uint)outputSizeOverTwo.X)
                                .WithHeight((uint)outputSizeOverTwo.Y)
                                .WithPermittedBindings(GPUBindings.ReadableShaderResource | GPUBindings.RenderTarget)
                                .WithUsage(ResourceUsage.Write);
                glowDstBufferRTV = glowDstBuffer.CreateRenderTargetView(0U);
                glowDstBufferSRV = glowDstBuffer.CreateView();

                bufferChange = true;
            }

            if (glowDSBuffer == null || glowDSBuffer.Width != (uint)outputSizeOverTwo.X || glowDSBuffer.Height != (uint)outputSizeOverTwo.Y)
            {
                if (glowDSBufferDSV != null)
                {
                    glowDSBufferDSV.Dispose();
                }
                if (glowDSBuffer != null)
                {
                    glowDSBuffer.Dispose();
                }

                glowDSBuffer = dsBufferBuilder
                               .WithWidth((uint)outputSizeOverTwo.X)
                               .WithHeight((uint)outputSizeOverTwo.Y);
                glowDSBufferDSV = glowDSBuffer.CreateDepthStencilView(0U);

                bufferChange = true;
            }

            if (bufferChange)
            {
                SetUpShaderResPackages();
            }
        }
Ejemplo n.º 2
0
        private void CheckGeometryPassParameters(Texture2D <TexelFormat.RGBA32Float>[] currentGBuffer, Vector2 outputSizePixels)
        {
            if (preBloomBuffer == null || preBloomBufferRTV.ResourceOrViewDisposed || preBloomBufferSRV.ResourceOrViewDisposed ||
                preBloomBuffer.Width != (uint)outputSizePixels.X || preBloomBuffer.Height != (uint)outputSizePixels.Y)
            {
                if (preBloomBufferRTV != null && !preBloomBufferRTV.IsDisposed)
                {
                    preBloomBufferRTV.Dispose();
                }
                if (preBloomBufferSRV != null && !preBloomBufferSRV.IsDisposed)
                {
                    preBloomBufferSRV.Dispose();
                }
                if (preBloomBuffer != null && !preBloomBuffer.IsDisposed)
                {
                    preBloomBuffer.Dispose();
                }
                if (dsThrowawayBufferDSV != null && !dsThrowawayBufferDSV.IsDisposed)
                {
                    dsThrowawayBufferDSV.Dispose();
                }
                if (dsThrowawayBuffer != null && !dsThrowawayBuffer.IsDisposed)
                {
                    dsThrowawayBuffer.Dispose();
                }
                preBloomBuffer       = preBloomBufferBuilder.WithWidth((uint)outputSizePixels.X).WithHeight((uint)outputSizePixels.Y);
                dsThrowawayBuffer    = dsBufferBuilder.WithWidth((uint)outputSizePixels.X).WithHeight((uint)outputSizePixels.Y);
                preBloomBufferSRV    = preBloomBuffer.CreateView();
                preBloomBufferRTV    = preBloomBuffer.CreateRenderTargetView(0U);
                dsThrowawayBufferDSV = dsThrowawayBuffer.CreateDepthStencilView(0U);
                RecalculateLightTileOffsets();
            }
            if (nonDepthOfFieldBackBuffer == null || nonDepthOfFieldBackBufferRTV.ResourceOrViewDisposed || nonDepthOfFieldBackBufferSRV.ResourceOrViewDisposed ||
                nonDepthOfFieldBackBuffer.Width != (uint)outputSizePixels.X || nonDepthOfFieldBackBuffer.Height != (uint)outputSizePixels.Y)
            {
                if (nonDepthOfFieldBackBuffer != null && !nonDepthOfFieldBackBuffer.IsDisposed)
                {
                    nonDepthOfFieldBackBuffer.Dispose();
                }
                if (nonDepthOfFieldBackBufferRTV != null && !nonDepthOfFieldBackBufferRTV.IsDisposed)
                {
                    nonDepthOfFieldBackBufferRTV.Dispose();
                }
                if (nonDepthOfFieldBackBufferSRV != null && !nonDepthOfFieldBackBufferSRV.IsDisposed)
                {
                    nonDepthOfFieldBackBufferSRV.Dispose();
                }
                nonDepthOfFieldBackBuffer    = preBloomBufferBuilder.WithWidth((uint)outputSizePixels.X).WithHeight((uint)outputSizePixels.Y);
                nonDepthOfFieldBackBufferRTV = nonDepthOfFieldBackBuffer.CreateRenderTargetView(0U);
                nonDepthOfFieldBackBufferSRV = nonDepthOfFieldBackBuffer.CreateView();
            }
            outputSizePixels /= 2f;
            if (reducedBloomBuffer == null || reducedBloomBufferRTV.ResourceOrViewDisposed || reducedBloomBufferSRV.ResourceOrViewDisposed ||
                reducedBloomBuffer.Width != (uint)outputSizePixels.X || reducedBloomBuffer.Height != (uint)outputSizePixels.Y)
            {
                if (reducedBloomBufferRTV != null && !reducedBloomBufferRTV.IsDisposed)
                {
                    reducedBloomBufferRTV.Dispose();
                }
                if (reducedBloomBufferSRV != null && !reducedBloomBufferSRV.IsDisposed)
                {
                    reducedBloomBufferSRV.Dispose();
                }
                if (reducedBloomBuffer != null && !reducedBloomBuffer.IsDisposed)
                {
                    reducedBloomBuffer.Dispose();
                }
                reducedBloomBuffer    = preBloomBufferBuilder.WithWidth((uint)outputSizePixels.X).WithHeight((uint)outputSizePixels.Y);
                reducedBloomBufferSRV = reducedBloomBuffer.CreateView();
                reducedBloomBufferRTV = reducedBloomBuffer.CreateRenderTargetView(0U);

                if (bloomTargetBufferSRV != null)
                {
                    bloomTargetBufferSRV.Dispose();
                }
                if (bloomTargetBuffer != null)
                {
                    bloomTargetBuffer.Dispose();
                }
                bloomTargetBuffer    = reducedBloomBuffer.Clone();
                bloomTargetBufferSRV = bloomTargetBuffer.CreateView();
                bloomTargetBufferRTV = bloomTargetBuffer.CreateRenderTargetView(0U);

                if (bloomResizeCopyDSDSV != null)
                {
                    bloomResizeCopyDSDSV.Dispose();
                }
                if (bloomResizeCopyDSBuffer != null)
                {
                    bloomResizeCopyDSBuffer.Dispose();
                }
                bloomResizeCopyDSBuffer = dsBufferBuilder.WithWidth((uint)outputSizePixels.X).WithHeight((uint)outputSizePixels.Y);
                bloomResizeCopyDSDSV    = bloomResizeCopyDSBuffer.CreateDepthStencilView(0U);
            }

            if (depthOfFieldBackBuffer == null || depthOfFieldBackBufferRTV.ResourceOrViewDisposed || depthOfFieldBackBufferSRV.ResourceOrViewDisposed ||
                depthOfFieldBackBuffer.Width != (uint)outputSizePixels.X || depthOfFieldBackBuffer.Height != (uint)outputSizePixels.Y)
            {
                if (depthOfFieldBackBuffer != null && !depthOfFieldBackBuffer.IsDisposed)
                {
                    depthOfFieldBackBuffer.Dispose();
                }
                if (depthOfFieldBackBufferRTV != null && !depthOfFieldBackBufferRTV.IsDisposed)
                {
                    depthOfFieldBackBufferRTV.Dispose();
                }
                if (depthOfFieldBackBufferSRV != null && !depthOfFieldBackBufferSRV.IsDisposed)
                {
                    depthOfFieldBackBufferSRV.Dispose();
                }
                depthOfFieldBackBuffer    = preBloomBufferBuilder.WithWidth((uint)outputSizePixels.X).WithHeight((uint)outputSizePixels.Y);
                depthOfFieldBackBufferRTV = depthOfFieldBackBuffer.CreateRenderTargetView(0U);
                depthOfFieldBackBufferSRV = depthOfFieldBackBuffer.CreateView();
            }
            if (reducedNonDepthOfFieldBackBuffer == null || reducedNonDepthOfFieldBackBufferRTV.ResourceOrViewDisposed || reducedNonDepthOfFieldBackBufferSRV.ResourceOrViewDisposed ||
                reducedNonDepthOfFieldBackBuffer.Width != (uint)outputSizePixels.X || reducedNonDepthOfFieldBackBuffer.Height != (uint)outputSizePixels.Y)
            {
                if (reducedNonDepthOfFieldBackBuffer != null && !reducedNonDepthOfFieldBackBuffer.IsDisposed)
                {
                    reducedNonDepthOfFieldBackBuffer.Dispose();
                }
                if (reducedNonDepthOfFieldBackBufferRTV != null && !reducedNonDepthOfFieldBackBufferRTV.IsDisposed)
                {
                    reducedNonDepthOfFieldBackBufferRTV.Dispose();
                }
                if (reducedNonDepthOfFieldBackBufferSRV != null && !reducedNonDepthOfFieldBackBufferSRV.IsDisposed)
                {
                    reducedNonDepthOfFieldBackBufferSRV.Dispose();
                }
                reducedNonDepthOfFieldBackBuffer    = preBloomBufferBuilder.WithWidth((uint)outputSizePixels.X).WithHeight((uint)outputSizePixels.Y);
                reducedNonDepthOfFieldBackBufferRTV = reducedNonDepthOfFieldBackBuffer.CreateRenderTargetView(0U);
                reducedNonDepthOfFieldBackBufferSRV = reducedNonDepthOfFieldBackBuffer.CreateView();
            }

            if (outliningShader != null && outliningShaderResPackage.GetValue((ResourceViewBinding)outliningShader.GetBindingByIdentifier("GeomDepthBuffer")) != GeometryPass.PrimaryDSBufferSRV)
            {
                outliningShaderResPackage.SetValue((ResourceViewBinding)outliningShader.GetBindingByIdentifier("GeomDepthBuffer"), GeometryPass.PrimaryDSBufferSRV);
                outliningShaderUnbindResPackage.SetValue((ResourceViewBinding)outliningShader.GetBindingByIdentifier("GeomDepthBuffer"), null);
            }

            if (dofShader != null && dofShaderResPackage.GetValue((ResourceViewBinding)dofShader.GetBindingByIdentifier("SceneDepth")) != GeometryPass.PrimaryDSBufferSRV)
            {
                dofShaderResPackage.SetValue((ResourceViewBinding)dofShader.GetBindingByIdentifier("SceneDepth"), GeometryPass.PrimaryDSBufferSRV);
                dofShaderUnbindResPackage.SetValue((ResourceViewBinding)dofShader.GetBindingByIdentifier("SceneDepth"), null);
            }

            bool bufferChange = false;

            for (int i = 0; i < DLGeometryPass.NUM_GBUFFER_TEXTURES; ++i)
            {
                if (previousGBuffer[i] != currentGBuffer[i])
                {
                    bufferChange = true;
                    break;
                }
            }
            if (!bufferChange)
            {
                return;
            }
            Array.Copy(currentGBuffer, previousGBuffer, DLGeometryPass.NUM_GBUFFER_TEXTURES);
            for (int i = 0; i < DLGeometryPass.NUM_GBUFFER_TEXTURES; ++i)
            {
                if (gBufferSRVArray[i] != null)
                {
                    gBufferSRVArray[i].Dispose();
                }
                gBufferSRVArray[i] = currentGBuffer[i].CreateView();
            }
            if (lightPlaneVertices != null)
            {
                lightPlaneVertices.Dispose();
            }
            if (lightPlaneInputLayout != InputLayoutHandle.NULL)
            {
                InteropUtils.CallNative(NativeMethods.ResourceFactory_ReleaseResource, (ResourceHandle)lightPlaneInputLayout).ThrowOnFailure();
            }

            lightPlaneVertices = lightPlaneBuilder.WithInitialData(new[] {
                new LightPlaneVertex(new Vector3(-1f, -1f, 0f), new Vector2(0f, 1f)),
                new LightPlaneVertex(new Vector3(-1f, 1f, 0f), new Vector2(0f, 0f)),
                new LightPlaneVertex(new Vector3(1f, 1f, 0f), new Vector2(1f, 0f)),

                new LightPlaneVertex(new Vector3(1f, 1f, 0f), new Vector2(1f, 0f)),
                new LightPlaneVertex(new Vector3(1f, -1f, 0f), new Vector2(1f, 1f)),
                new LightPlaneVertex(new Vector3(-1f, -1f, 0f), new Vector2(0f, 1f)),
            });

            InputElementDesc[] inputElements =
            {
                new InputElementDesc(
                    "POSITION",
                    0U,
                    BaseResource.GetFormatForType(typeof(Vector3)),
                    0U,
                    true
                    ),
                new InputElementDesc(
                    "TEXCOORD",
                    0U,
                    BaseResource.GetFormatForType(typeof(Vector2)),
                    0U,
                    true
                    )
            };

            InputLayoutHandle outInputLayoutPtr;

            InteropUtils.CallNative(NativeMethods.ShaderManager_CreateInputLayout,
                                    RenderingModule.Device,
                                    dlLightVS.Handle,
                                    inputElements,
                                    (uint)inputElements.Length,
                                    (IntPtr)(&outInputLayoutPtr)
                                    ).ThrowOnFailure();

            lightPlaneInputLayout = outInputLayoutPtr;

            SetVSResources();
            SetFSResources();
        }
Ejemplo n.º 3
0
        protected internal unsafe override void Execute(ParallelizationProvider pp)
        {
            // See if we need to resize the depth buffer
            Vector2 viewportDimensions = output.SizePixels;

            if (viewportDimensions.X < 1f || viewportDimensions.Y < 1f)
            {
                return;
            }
            uint viewportX = (uint)viewportDimensions.X;
            uint viewportY = (uint)viewportDimensions.Y;

            if (shadowBuffer == null || shadowBufferDSV == null || shadowBufferDSV.ResourceOrViewDisposed ||
                shadowBufferSRV == null || shadowBufferSRV.ResourceOrViewDisposed ||
                shadowBuffer.Width != viewportX || shadowBuffer.Height != viewportY)
            {
                if (shadowBufferDSV != null && !shadowBufferDSV.IsDisposed)
                {
                    shadowBufferDSV.Dispose();
                }
                if (shadowBuffer != null && !shadowBuffer.IsDisposed)
                {
                    shadowBuffer.Dispose();
                }
                shadowBuffer    = shadowBufferBuilder.WithWidth(viewportX).WithHeight(viewportY);
                shadowBufferDSV = shadowBuffer.CreateDepthStencilView <TexelFormat.DepthStencil>(0U);
                shadowBufferSRV = shadowBuffer.CreateView <TexelFormat.R24UnormX8Typeless>(0U, 1U);
            }

            // Clear the depth buffer
            QueueRenderCommand(RenderCommand.ClearDepthStencil(shadowBufferDSV));

            List <GeometryCache> activeCaches = GeometryCache.ActiveCaches;

            foreach (GeometryCache c in activeCaches)
            {
                // Set view/proj matrix
                Matrix vpMat    = (*((Matrix *)lightCam.GetRecalculatedViewMatrix()) * *((Matrix *)Output.GetRecalculatedProjectionMatrix(lightCam))).Transpose;
                byte * vpMapPtr = (byte *)&vpMat;
                shadowVS.ViewProjMatBinding.SetValue(vpMapPtr);

                // Set state for current cache
                cpuInstanceBufferCurIndex = 0;
                List <SceneLayer> allEnabledLayers = Scene.EnabledLayers;
                uint maxLayer = 0U;
                for (int i = 0; i < allEnabledLayers.Count; ++i)
                {
                    if (allEnabledLayers[i].Index > maxLayer)
                    {
                        maxLayer = allEnabledLayers[i].Index;
                    }
                }
                if (allEnabledLayers.Count > 0 && currentSceneLayers.Length <= maxLayer)
                {
                    currentSceneLayers = new SceneLayer[maxLayer + 1U];
                }
                Array.Clear(currentSceneLayers, 0, currentSceneLayers.Length);
                foreach (SceneLayer layer in allEnabledLayers)
                {
                    currentSceneLayers[layer.Index] = layer;
                }
                currentCache = c;
                ++frameNum;
                Thread.MemoryBarrier();
                currentInstanceData = currentCache.GetModelInstanceData();

                // Set up each thread
                pp.InvokeOnAll(setUpCacheForLocalThreadAct, true);                 // membar here

                // Iterate all model instances (ordered by material)
                pp.Execute((int)currentInstanceData.Length, (int)(currentInstanceData.Length / (pp.NumThreads << 3)) + 1, renderCacheIterateMatAct);

                // Set instance buffer and write to it
                if (gpuInstanceBuffer == null || gpuInstanceBuffer.Length < cpuInstanceBuffer.Length)
                {
                    if (gpuInstanceBuffer != null)
                    {
                        gpuInstanceBuffer.Dispose();
                    }
                    gpuInstanceBuffer = gpuInstanceBufferBuilder.WithLength((uint)cpuInstanceBuffer.Length).Create();
                }
                gpuInstanceBuffer.DiscardWrite(cpuInstanceBuffer);                 // Happens immediately (required)

                // Set instance buffer and flush all commands, first on immediate context, then on each deferred
                SetInstanceBufferAndFlushCommands();
                pp.InvokeOnAll(setInstanceBufferAndFlushCommandsAct, false);
            }
        }