public void TestRTVCreation()
        {
            Texture2D <TexelFormat.RenderTarget> testResource = TextureFactory.NewTexture2D <TexelFormat.RenderTarget>()
                                                                .WithDynamicDetail(false)
                                                                .WithMipAllocation(false)
                                                                .WithMipGenerationTarget(false)
                                                                .WithPermittedBindings(GPUBindings.RenderTarget)
                                                                .WithUsage(ResourceUsage.Write)
                                                                .WithWidth(800U)
                                                                .WithHeight(600U)
                                                                .WithMultisampling(true);
            RenderTargetView testRTV = testResource.CreateRenderTargetView(0U);

            Assert.IsFalse(testRTV.ResourceOrViewDisposed);
            Assert.AreEqual(testResource, testRTV.Resource);
            Assert.AreEqual(0U, testRTV.MipIndex);
            testResource.Dispose();
            Assert.IsTrue(testRTV.ResourceOrViewDisposed);
            testRTV.Dispose();
            Assert.IsTrue(testRTV.IsDisposed);

            Texture2DArray <TexelFormat.RenderTarget> testResourceArr = TextureFactory.NewTexture2D <TexelFormat.RenderTarget>()
                                                                        .WithDynamicDetail(false)
                                                                        .WithMipAllocation(false)
                                                                        .WithMipGenerationTarget(false)
                                                                        .WithPermittedBindings(GPUBindings.RenderTarget)
                                                                        .WithUsage(ResourceUsage.Write)
                                                                        .WithWidth(800U)
                                                                        .WithHeight(600U)
                                                                        .WithMultisampling(true)
                                                                        .CreateArray(10U);

            testRTV = testResourceArr[3].CreateRenderTargetView(0U);
            Assert.IsFalse(testRTV.ResourceOrViewDisposed);
            Assert.AreEqual(testResourceArr[3], testRTV.Resource);
            Assert.AreEqual(0U, testRTV.MipIndex);
            testResourceArr.Dispose();
            Assert.IsTrue(testRTV.ResourceOrViewDisposed);
            testRTV.Dispose();
            Assert.IsTrue(testRTV.IsDisposed);
        }
        public void TestClearRenderTarget()
        {
            Texture2D <TexelFormat.RenderTarget> renderTarget = TextureFactory.NewTexture2D <TexelFormat.RenderTarget>()
                                                                .WithWidth(800U)
                                                                .WithHeight(600U)
                                                                .WithDynamicDetail(false)
                                                                .WithMipAllocation(false)
                                                                .WithMipGenerationTarget(false)
                                                                .WithMultisampling(false)
                                                                .WithPermittedBindings(GPUBindings.RenderTarget)
                                                                .WithUsage(ResourceUsage.Write);

            RenderTargetView rtv = renderTarget.CreateRenderTargetView(0U);

            RenderCommand testCommand = RenderCommand.ClearRenderTarget(rtv);

            Assert.AreEqual(RenderCommandInstruction.ClearRenderTarget, testCommand.Instruction);
            Assert.AreEqual((RenderCommandArgument)(IntPtr)(ResourceViewHandle)rtv.ResourceViewHandle, testCommand.Arg1);

#if !DEVELOPMENT && !RELEASE
            try {
                RenderCommand.ClearRenderTarget(null as RenderTargetView);
                Assert.Fail();
            }
            catch (AssuranceFailedException) { }
#endif

            rtv.Dispose();
            renderTarget.Dispose();

#if !DEVELOPMENT && !RELEASE
            try {
                RenderCommand.ClearRenderTarget(rtv);
                Assert.Fail();
            }
            catch (AssuranceFailedException) { }
#endif
        }
Esempio n. 3
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();
        }
        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();
            }
        }