Exemplo n.º 1
0
        public void TestCreation()
        {
            // Define variables and constants
            VertexBufferBuilder <int>  defaultBuilder           = BufferFactory.NewVertexBuffer <int>().WithLength(1);
            VertexBufferBuilder <int>  builderWithFrequentWrite = defaultBuilder.WithUsage(ResourceUsage.DiscardWrite);
            VertexBufferBuilder <int>  builderWith300Verts      = builderWithFrequentWrite.WithLength(300);
            VertexBufferBuilder <int>  builderWithInitData      = defaultBuilder.WithInitialData(Enumerable.Range(0, 10).ToArray());
            VertexBufferBuilder <long> builderWith200LongVert   = builderWithFrequentWrite.WithVertexType <long>().WithLength(200);

            // Set up context


            // Execute
            VertexBuffer <int>  withFreqWriteBuffer = builderWithFrequentWrite;
            VertexBuffer <int>  with300VertBuffer   = builderWith300Verts;
            VertexBuffer <int>  withInitDataBuffer  = builderWithInitData;
            VertexBuffer <long> with200LongBuffer   = builderWith200LongVert;

            // Assert outcome
            Assert.AreEqual(ResourceUsage.DiscardWrite, withFreqWriteBuffer.Usage);
            Assert.AreEqual(300U, with300VertBuffer.Length);
            Assert.AreEqual(10U, withInitDataBuffer.Length);
            Assert.AreEqual(200U * sizeof(long), with200LongBuffer.Size.InBytes);

            try {
                builderWithInitData.WithUsage(ResourceUsage.StagingRead);
                Assert.Fail();
            }
            catch (ArgumentException) { }

            withFreqWriteBuffer.Dispose();
            with300VertBuffer.Dispose();
            withInitDataBuffer.Dispose();
            with200LongBuffer.Dispose();
        }
        private void SetUpGlowPlane()
        {
            if (glowPlaneVertices != null)
            {
                glowPlaneVertices.Dispose();
            }
            if (glowPlaneInputLayout != InputLayoutHandle.NULL)
            {
                InteropUtils.CallNative(NativeMethods.ResourceFactory_ReleaseResource, (ResourceHandle)glowPlaneInputLayout).ThrowOnFailure();
            }
            if (glowVS != null)
            {
                glowPlaneVertices = glowPlaneBuilder.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,
                                        glowVS.Handle,
                                        inputElements,
                                        (uint)inputElements.Length,
                                        (IntPtr)(&outInputLayoutPtr)
                                        ).ThrowOnFailure();

                glowPlaneInputLayout = outInputLayoutPtr;

                glowVS.GetBindingByIdentifier("POSITION").Bind(glowPlaneVertices);
            }
        }
Exemplo 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();
        }