Пример #1
0
        public void Dispose_ResourceSet()
        {
            ResourceLayout layout = RF.CreateResourceLayout(new ResourceLayoutDescription(
                                                                new ResourceLayoutElementDescription("InfoBuffer", ResourceKind.UniformBuffer, ShaderStages.Vertex),
                                                                new ResourceLayoutElementDescription("Ortho", ResourceKind.UniformBuffer, ShaderStages.Vertex)));

            DeviceBuffer ub0 = RF.CreateBuffer(new BufferDescription(256, BufferUsage.UniformBuffer));
            DeviceBuffer ub1 = RF.CreateBuffer(new BufferDescription(256, BufferUsage.UniformBuffer));

            ResourceSet rs = RF.CreateResourceSet(new ResourceSetDescription(layout, ub0, ub1));

            rs.Dispose();
            Assert.True(rs.IsDisposed);
            Assert.False(ub0.IsDisposed);
            Assert.False(ub1.IsDisposed);
            Assert.False(layout.IsDisposed);
            layout.Dispose();
            Assert.True(layout.IsDisposed);
            Assert.False(ub0.IsDisposed);
            Assert.False(ub1.IsDisposed);
            ub0.Dispose();
            Assert.True(ub0.IsDisposed);
            ub1.Dispose();
            Assert.True(ub1.IsDisposed);
        }
Пример #2
0
        public Building(RgbaFloat color) : base("Building")
        {
            Resources.OnInitialize = (factory, device) => {
                _model.Buffer.Initialize(factory, device);
                _color = new UniformColor(color);
                _color.Buffer.Initialize(factory, device);

                ResourceLayout = factory.CreateResourceLayout(new ResourceLayoutDescription(
                                                                  new ResourceLayoutElementDescription[]
                {
                    UniformModelTransformation.ResourceLayout,
                    UniformViewProjection.ResourceLayout,
                    _color.LayoutDescription
                }
                                                                  ));

                ResourceSet = factory.CreateResourceSet(new ResourceSetDescription(
                                                            ResourceLayout,
                                                            _model.Buffer.DeviceBuffer,
                                                            CameraViewProjection.DeviceBuffer,
                                                            _color.Buffer.DeviceBuffer
                                                            ));
            };
            Resources.OnDispose = () => {
                _color.Buffer.Dispose();
                ResourceLayout.Dispose();
                ResourceSet.Dispose();
            };
        }
Пример #3
0
 public void Dispose()
 {
     ResourceSet.Dispose();
     ResourceLayout.Dispose();
     Sampler.Dispose();
     Texture.Dispose();
 }
Пример #4
0
 public void Dispose()
 {
     _pipeline.Dispose();
     _indexBuffer.Dispose();
     _projectionMatrixBuffer.Dispose();
     _groupResourceLayout.Dispose();
     _cameraResourceLayout.Dispose();
 }
Пример #5
0
 public void Dispose()
 {
     _rl.Dispose();
     _pipeline.Dispose();
     _sampleRegionUB.Dispose();
     _sampleRegionSet.Dispose();
     _sampleRegionLayout.Dispose();
 }
 public void Dispose()
 {
     _projectionResourceSet?.Dispose();
     _projectionBuffer?.Dispose();
     _pipeline?.Dispose();
     _transformsLayout?.Dispose();
     _vertex?.Dispose();
     _fragment?.Dispose();
 }
 public void Dispose()
 {
     commandList.Dispose();
     indexBuffer.Dispose();
     vertexBuffer.Dispose();
     worldMatrixBuffer.Dispose();
     pipeline.Dispose();
     viewResourceLayout.Dispose();
     graphicsResourceLayout.Dispose();
 }
Пример #8
0
        protected override void Dispose(bool disposeManagedResources)
        {
            base.Dispose(disposeManagedResources);

            ResourceSet.Dispose();
            ResourceSet = null;
            TextureLayout.Dispose();
            TextureLayout = null;
            ProjectionBuffer.Dispose();
            ProjectionBuffer = null;
            WorldBuffer.Dispose();
            WorldBuffer = null;
        }
Пример #9
0
 private static void DisposeResources()
 {
     _pipeline.Dispose();
     foreach (var s in _shaders)
     {
         s.Dispose();
     }
     _commandList.Dispose();
     _vertexBuffer.Dispose();
     _indexBuffer.Dispose();
     _layout.Dispose();
     _graphicsDevice.Dispose();
 }
Пример #10
0
        public void Dispose()
        {
            if (_disposed)
            {
                return;
            }

            foreach (var pipeline in _pipelines.Values)
            {
                pipeline.Dispose();
            }

            foreach (var t in _textureResourceSets)
            {
                t?.Dispose();
            }
            foreach (var t in _textureViews)
            {
                t?.Dispose();
            }
            _textureLayout.Dispose();

            _wvpSet.Dispose();
            _wvpBuffer.Dispose();
            _wvpLayout.Dispose();

            foreach (var s in _samplerResourceSets)
            {
                if (s.IsValueCreated)
                {
                    s.Value.Dispose();
                }
            }

            _linearClamp?.Dispose();
            _pointClamp?.Dispose();
            _anisotropicClamp?.Dispose();

            _samplerLayout.Dispose();

            _vertexShader.Dispose();
            _fragmentShader.Dispose();
            _commandList.Dispose();
            _vertexBuffer.Dispose();
            _indexBuffer.Dispose();

            _disposed = true;
        }
Пример #11
0
        public void DisposeResources()
        {
            Pipeline.Dispose();
            foreach (Shader shader in Shaders)
            {
                shader.Dispose();
            }

            CommandList.Dispose();
            VertexBuffer.Dispose();
            IndexBuffer.Dispose();
            GraphicsDevice.Dispose();
            ProjectionBuffer.Dispose();
            WorldBuffer.Dispose();
            ResourceSet.Dispose();
            ResourceLayout.Dispose();
        }
Пример #12
0
        public void Dispose_Pipeline()
        {
            Shader[]             shaders   = TestShaders.LoadVertexFragment(RF, "UIntVertexAttribs");
            ShaderSetDescription shaderSet = new ShaderSetDescription(
                new VertexLayoutDescription[]
            {
                new VertexLayoutDescription(
                    new VertexElementDescription("Position", VertexElementSemantic.TextureCoordinate, VertexElementFormat.Float2),
                    new VertexElementDescription("Color_UInt", VertexElementSemantic.TextureCoordinate, VertexElementFormat.UInt4))
            },
                shaders);

            ResourceLayout layout = RF.CreateResourceLayout(new ResourceLayoutDescription(
                                                                new ResourceLayoutElementDescription("InfoBuffer", ResourceKind.UniformBuffer, ShaderStages.Vertex),
                                                                new ResourceLayoutElementDescription("Ortho", ResourceKind.UniformBuffer, ShaderStages.Vertex)));

            GraphicsPipelineDescription gpd = new GraphicsPipelineDescription(
                BlendStateDescription.SingleOverrideBlend,
                DepthStencilStateDescription.Disabled,
                RasterizerStateDescription.Default,
                PrimitiveTopology.PointList,
                shaderSet,
                layout,
                new OutputDescription(null, new OutputAttachmentDescription(PixelFormat.R32_G32_B32_A32_Float)));
            Pipeline pipeline = RF.CreateGraphicsPipeline(ref gpd);

            pipeline.Dispose();
            Assert.True(pipeline.IsDisposed);
            Assert.False(shaders[0].IsDisposed);
            Assert.False(shaders[1].IsDisposed);
            Assert.False(layout.IsDisposed);
            layout.Dispose();
            Assert.True(layout.IsDisposed);
            Assert.False(shaders[0].IsDisposed);
            Assert.False(shaders[1].IsDisposed);
            shaders[0].Dispose();
            Assert.True(shaders[0].IsDisposed);
            shaders[1].Dispose();
            Assert.True(shaders[1].IsDisposed);
        }
Пример #13
0
 public static void Destory()
 {
     SamplersSet.Dispose();
     SamplersLayout.Dispose();
     _linearSampler.Dispose();
 }
Пример #14
0
        public void CreateDynamicResources(string fragmentCode)
        {
            // shaders
            string newFragmentShader;

            if (backend == GraphicsBackend.OpenGL)
            {
                newFragmentShader = fragmentHeaderCode + BuildShaderResourceCode() + fragmentCode;
            }
            else
            {
                newFragmentShader = fragmentHeaderCode + BuildShaderResourceCode() + fragmentHeaderNonGLCode + fragmentCode;
            }
            if (!forceRecreate && currentFragmentShader != null && currentFragmentShader.Equals(newFragmentShader))
            {
                return;
            }
            forceRecreate = false;

            // shaders
            if (!isInitialized)
            {
                vertexShaderDesc = CreateShaderDescription(VertexCode, ShaderStages.Vertex);
            }
            currentFragmentShader = newFragmentShader;
            Shader[] newShaders;
            try
            {
                ShaderDescription fragmentShaderDesc = CreateShaderDescription(currentFragmentShader, ShaderStages.Fragment);
                newShaders = factory.CreateFromSpirv(vertexShaderDesc, fragmentShaderDesc);
                DisposeShaders();
                shaders = newShaders;
                uiController.SetError(null);
            }
            catch (Exception e)
            {
                uiController.SetError(e.Message);
                return;
            }

            // resources
            runtimeDataBuffer?.Dispose();
            runtimeDataBuffer = factory.CreateBuffer(new BufferDescription(
                                                         YALCTRuntimeData.Size,
                                                         BufferUsage.UniformBuffer | BufferUsage.Dynamic));
            runtimeDataBuffer.Name = "YALCT Runtime Data";

            List <ResourceLayoutElementDescription> layoutDescriptions = new List <ResourceLayoutElementDescription>
            {
                new ResourceLayoutElementDescription("RuntimeData", ResourceKind.UniformBuffer, ShaderStages.Fragment),
                new ResourceLayoutElementDescription("Sampler", ResourceKind.Sampler, ShaderStages.Fragment),
            };
            List <BindableResource> bindableResources = new List <BindableResource> {
                runtimeDataBuffer,
                graphicsDevice.PointSampler
            };

            for (int i = 0; i < textureViews.Count; i++)
            {
                TextureView view = textureViews[i];
                layoutDescriptions.Add(new ResourceLayoutElementDescription($"InputTex{i}", ResourceKind.TextureReadOnly, ShaderStages.Fragment));
                bindableResources.Add(view);
            }
            resourceLayout?.Dispose();
            resourceLayout      = factory.CreateResourceLayout(new ResourceLayoutDescription(layoutDescriptions.ToArray()));
            resourceLayout.Name = "YALCT Resource Layout";

            ResourceSetDescription resourceSetDescription = new ResourceSetDescription(resourceLayout, bindableResources.ToArray());

            resourceSet?.Dispose();
            resourceSet      = factory.CreateResourceSet(resourceSetDescription);
            resourceSet.Name = "YALCT Resource Set";

            // pipeline
            pipeline?.Dispose();
            pipeline = factory.CreateGraphicsPipeline(
                new GraphicsPipelineDescription(
                    BlendStateDescription.SingleOverrideBlend,
                    new DepthStencilStateDescription(
                        depthTestEnabled: false,
                        depthWriteEnabled: false,
                        comparisonKind: ComparisonKind.Always),
                    new RasterizerStateDescription(
                        cullMode: FaceCullMode.Back,
                        fillMode: PolygonFillMode.Solid,
                        frontFace: FrontFace.Clockwise,
                        depthClipEnabled: false,
                        scissorTestEnabled: false),
                    PrimitiveTopology.TriangleList,
                    new ShaderSetDescription(
                        vertexLayouts: new VertexLayoutDescription[] {
                new VertexLayoutDescription(
                    new VertexElementDescription("Position", VertexElementSemantic.TextureCoordinate, VertexElementFormat.Float3)
                    )
            },
                        shaders: shaders),
                    new ResourceLayout[] { resourceLayout },
                    swapchain.Framebuffer.OutputDescription)
                );
            pipeline.Name = "YALCT Fullscreen Pipeline";

            isInitialized = true;
        }
Пример #15
0
        public void UpdateConfigs(RenderConfig config)
        {
            Logger.AddLog("VeldridRender.UpdateConfigs");
            CheckThread();

            try
            {
                try { rubber?.Dispose(); } catch { }
                try { _textureView?.Dispose(); } catch { }
                try { textureLayout?.Dispose(); } catch { }
                try { _textureSet?.Dispose(); } catch { }

                try { _vertBuffer?.Dispose(); } catch { }
                try { _fragLightBuffer?.Dispose(); } catch { }
                try { _fragMaterialBuffer?.Dispose(); } catch { }

                try { uniformsLayout?.Dispose(); } catch { }
                try { _uniformsResourceSet?.Dispose(); } catch { }

                try { _offscreenReadOut?.Dispose(); } catch { }
                try { _offscreenFB?.Dispose(); } catch { }
                try { _offscreenColor?.Dispose(); } catch { }

                try { _pipeline?.Dispose(); } catch { }

                #region Textures
                ProcessImage tmpTexture = Helpers.ImageProcessing.PngToRgba8(new ProcessImage {
                    Load = Helpers.GetAssetByteArray("App.rubber4.png"), Type = ProcessImageType.PNG
                });
                rubber = resourceFactory.CreateTexture(TextureDescription.Texture2D((uint)tmpTexture.Width, (uint)tmpTexture.Height, 1, 1, PixelFormat.R8_G8_B8_A8_UNorm, TextureUsage.Sampled));

                graphicsDevice.UpdateTexture(rubber, tmpTexture.Load, 0, 0, 0, (uint)tmpTexture.Width, (uint)tmpTexture.Height, 1, 0, 0);

                _textureView = resourceFactory.CreateTextureView(rubber);
                ResourceLayoutElementDescription[] textureLayoutDescriptions =
                {
                    new ResourceLayoutElementDescription("Tex",  ResourceKind.TextureReadOnly, ShaderStages.Fragment),
                    new ResourceLayoutElementDescription("Samp", ResourceKind.Sampler,         ShaderStages.Fragment)
                };
                textureLayout = resourceFactory.CreateResourceLayout(new ResourceLayoutDescription(textureLayoutDescriptions));
                _textureSet   = resourceFactory.CreateResourceSet(new ResourceSetDescription(textureLayout, _textureView, graphicsDevice.LinearSampler));
                #endregion

                #region Uniforms
                _vertBuffer         = resourceFactory.CreateBuffer(new BufferDescription(64, BufferUsage.UniformBuffer | BufferUsage.Dynamic));
                _fragLightBuffer    = resourceFactory.CreateBuffer(new BufferDescription(64, BufferUsage.UniformBuffer | BufferUsage.Dynamic));
                _fragMaterialBuffer = resourceFactory.CreateBuffer(new BufferDescription(64, BufferUsage.UniformBuffer | BufferUsage.Dynamic));

                ResourceLayoutElementDescription[] resourceLayoutElementDescriptions =
                {
                    new ResourceLayoutElementDescription("ModelViewProjection", ResourceKind.UniformBuffer, ShaderStages.Vertex),
                    new ResourceLayoutElementDescription("LightInfo",           ResourceKind.UniformBuffer, ShaderStages.Fragment),
                    new ResourceLayoutElementDescription("MaterialInfo",        ResourceKind.UniformBuffer, ShaderStages.Fragment)
                };
                uniformsLayout       = resourceFactory.CreateResourceLayout(new ResourceLayoutDescription(resourceLayoutElementDescriptions));
                _uniformsResourceSet = resourceFactory.CreateResourceSet(new ResourceSetDescription(uniformsLayout, _vertBuffer, _fragLightBuffer, _fragMaterialBuffer));
                #endregion

                var VertBufferDescription = new VertexLayoutDescription(
                    new VertexElementDescription("Position", VertexElementSemantic.Position, VertexElementFormat.Float3),
                    new VertexElementDescription("Normal", VertexElementSemantic.Normal, VertexElementFormat.Float3),
                    new VertexElementDescription("Texture", VertexElementSemantic.TextureCoordinate, VertexElementFormat.Float2));


                //Pipeline
                _offscreenReadOut = resourceFactory.CreateTexture(TextureDescription.Texture2D((uint)config.Width, (uint)config.Height, 1, 1, PixelFormat.R32_G32_B32_A32_Float, TextureUsage.Staging));

                _offscreenColor = resourceFactory.CreateTexture(TextureDescription.Texture2D((uint)config.Width, (uint)config.Height, 1, 1, PixelFormat.R32_G32_B32_A32_Float, TextureUsage.RenderTarget));

                _offscreenFB = resourceFactory.CreateFramebuffer(new FramebufferDescription(null, _offscreenColor));

                GraphicsPipelineDescription pipelineDescription = new GraphicsPipelineDescription()
                {
                    BlendState      = BlendStateDescription.SingleDisabled,
                    RasterizerState = new RasterizerStateDescription()
                    {
                        CullMode           = FaceCullMode.None,
                        FillMode           = PolygonFillMode.Solid,
                        FrontFace          = FrontFace.CounterClockwise,
                        DepthClipEnabled   = true,
                        ScissorTestEnabled = true
                    },
                    PrimitiveTopology = PrimitiveTopology.TriangleList,
                    ResourceLayouts   = new ResourceLayout[] { uniformsLayout, textureLayout },
                    Outputs           = _offscreenFB.OutputDescription,
                    ShaderSet         = new ShaderSetDescription()
                    {
                        Shaders       = Shaders,
                        VertexLayouts = new VertexLayoutDescription[] { VertBufferDescription }
                    },
                    ResourceBindingModel = ResourceBindingModel.Improved
                };
                _pipeline = resourceFactory.CreateGraphicsPipeline(ref pipelineDescription);


                FragLightUniforms.Lightdirection = new Vector4(Vector3.Normalize(config.LightDirection), 0);
                FragLightUniforms.Lightambient   = new Vector4(config.LightAmbient, 0);
                FragLightUniforms.Lightdiffuse   = new Vector4(config.LightDiffuse, 0);
                FragLightUniforms.Lightspecular  = new Vector4(config.LightSpecular, 0);

                FragMaterialUniforms.Materialdiffuse   = new Vector4(config.MaterialDiffuse, 0);
                FragMaterialUniforms.Materialspecular  = new Vector4(config.MaterialSpecular, 0);
                FragMaterialUniforms.Materialshininess = new Vector4(config.MaterialShininess, 0, 0, 0);

                FragMaterialUniforms.ViewDir = new Vector4(Vector3.Normalize(config.CameraPosition), 0);

                Vector3   camPos    = new Vector3(config.CameraPosition.X, config.CameraPosition.Y, config.CameraPosition.Z);
                Vector3   camLookAt = new Vector3(config.CameraLookAt.X, config.CameraLookAt.Y, config.CameraLookAt.Z);
                Matrix4x4 model     = Matrix4x4.Identity * Matrix4x4.CreateTranslation(config.ModelTranslation.X, config.ModelTranslation.Y, config.ModelTranslation.Z);
                model *= Matrix4x4.CreateFromAxisAngle(new Vector3(1.0f, 0, 0), config.ModelRotation.X)
                         * Matrix4x4.CreateFromAxisAngle(new Vector3(0, 1.0f, 0), config.ModelRotation.Y)
                         * Matrix4x4.CreateFromAxisAngle(new Vector3(0, 0, 1.0f), config.ModelRotation.Z);
                Matrix4x4 projection = Matrix4x4.CreatePerspectiveFieldOfView(DegreesToRadians(config.CameraFOV), config.Width / (float)config.Height, config.CameraNear, config.CameraFar);
                Matrix4x4 view       = Matrix4x4.CreateLookAt(camPos, camLookAt - camPos, -Vector3.UnitX);
                Matrix4x4 MVP        = model * view * projection;

                graphicsDevice.UpdateBuffer(_vertBuffer, 0, MVP);
                graphicsDevice.UpdateBuffer(_fragLightBuffer, 0, FragLightUniforms);
                graphicsDevice.UpdateBuffer(_fragMaterialBuffer, 0, FragMaterialUniforms);

                graphicsDevice.WaitForIdle();
            }
            catch (Exception ex)
            {
                Logger.AddLog(ex);
            }
        }