Exemplo n.º 1
0
        public void CreateDeviceObjects(GraphicsDevice gd, CommandList cl)
        {
            var factory = new DisposingResourceFactoryFacade(gd.ResourceFactory, _disposer);

            DeviceBuffer MakeBuffer(uint size, string name)
            {
                var buffer = factory.CreateBuffer(
                    new BufferDescription(size, BufferUsage.UniformBuffer | BufferUsage.Dynamic));

                buffer.Name = name;
                return(buffer);
            }

            ProjectionMatrixBuffer = MakeBuffer(64, "M_Projection");
            ModelViewMatrixBuffer  = MakeBuffer(64, "M_View");
            IdentityMatrixBuffer   = MakeBuffer(64, "M_Id");
            DepthLimitsBuffer      = MakeBuffer((uint)Unsafe.SizeOf <DepthCascadeLimits>(), "B_DepthLimits");
            CameraInfoBuffer       = MakeBuffer((uint)Unsafe.SizeOf <CameraInfo>(), "B_CameraInfo");

            cl.UpdateBuffer(IdentityMatrixBuffer, 0, Matrix4x4.Identity);

            var commonLayoutDescription = new ResourceLayoutDescription(
                ResourceLayoutHelper.Uniform("vdspv_1_0"),  // CameraInfo / common data buffer
                ResourceLayoutHelper.UniformV("vdspv_1_1"), // Perspective Matrix
                ResourceLayoutHelper.UniformV("vdspv_1_2"), // View Matrix
                ResourceLayoutHelper.Texture("vdspv_1_3")); // PaletteTexture

            CommonResourceLayout      = factory.CreateResourceLayout(commonLayoutDescription);
            CommonResourceLayout.Name = "RL_Common";

            _windowSized = new WindowSizedSceneContext(gd, MainSceneSampleCount);
            _disposer.Add(_windowSized, CommonResourceLayout);
        }
Exemplo n.º 2
0
        public void CreateDeviceObjects(GraphicsDevice gd, CommandList cl, SceneContext sc)
        {
            ResourceFactory factory = gd.ResourceFactory;

            _vertexBuffer      = factory.CreateBuffer(new BufferDescription(Vertices.SizeInBytes(), BufferUsage.VertexBuffer));
            _indexBuffer       = factory.CreateBuffer(new BufferDescription(Indices.SizeInBytes(), BufferUsage.IndexBuffer));
            _vertexBuffer.Name = "SpriteVertexBuffer";
            _indexBuffer.Name  = "SpriteIndexBuffer";
            cl.UpdateBuffer(_vertexBuffer, 0, Vertices);
            cl.UpdateBuffer(_indexBuffer, 0, Indices);

            if (_pipelines != null)
            {
                foreach (var pipeline in _pipelines)
                {
                    pipeline.Value.Dispose();
                }
            }

            var keys = new[]
            {
                new SpriteShaderKey(true, true),
                new SpriteShaderKey(true, false),
                new SpriteShaderKey(false, true),
                new SpriteShaderKey(false, false)
            };

            _perSpriteResourceLayout = factory.CreateResourceLayout(PerSpriteLayoutDescription);
            _pipelines = keys.ToDictionary(x => x, x => BuildPipeline(gd, sc, x));
            _disposeCollector.Add(_vertexBuffer, _indexBuffer, _perSpriteResourceLayout);
        }
Exemplo n.º 3
0
        public void CreateDeviceObjects(IRendererContext context)
        {
            var c  = (VeldridRendererContext)context;
            var cl = c.CommandList;
            var gd = c.GraphicsDevice;
            var sc = c.SceneContext;

            ResourceFactory factory = gd.ResourceFactory;

            _vb = factory.CreateBuffer(new BufferDescription(Vertices.SizeInBytes(), BufferUsage.VertexBuffer));
            _ib = factory.CreateBuffer(new BufferDescription(Indices.SizeInBytes(), BufferUsage.IndexBuffer));
            _miscUniformBuffer      = factory.CreateBuffer(new BufferDescription(MiscUniformData.SizeInBytes, BufferUsage.UniformBuffer));
            _vb.Name                = "TileMapVertexBuffer";
            _ib.Name                = "TileMapIndexBuffer";
            _miscUniformBuffer.Name = "TileMapMiscBuffer";
            cl.UpdateBuffer(_vb, 0, Vertices);
            cl.UpdateBuffer(_ib, 0, Indices);

            var shaderCache = Resolve <IShaderCache>();

            _shaders = shaderCache.GetShaderPair(gd.ResourceFactory,
                                                 VertexShaderName,
                                                 FragmentShaderName,
                                                 shaderCache.GetGlsl(VertexShaderName),
                                                 shaderCache.GetGlsl(FragmentShaderName));

            var shaderSet = new ShaderSetDescription(new[] { VertexLayout, InstanceLayout }, _shaders);

            _layout         = factory.CreateResourceLayout(PerSpriteLayoutDescription);
            _textureSampler = gd.ResourceFactory.CreateSampler(new SamplerDescription(
                                                                   SamplerAddressMode.Clamp,
                                                                   SamplerAddressMode.Clamp,
                                                                   SamplerAddressMode.Clamp,
                                                                   SamplerFilter.MinPoint_MagPoint_MipPoint,
                                                                   null, 1, 0, 0, 0, SamplerBorderColor.TransparentBlack
                                                                   ));

            var depthStencilMode = gd.IsDepthRangeZeroToOne
                    ? DepthStencilStateDescription.DepthOnlyLessEqual
                    : DepthStencilStateDescription.DepthOnlyGreaterEqual;

            var rasterizerMode = new RasterizerStateDescription(
                FaceCullMode.Front, PolygonFillMode.Solid, FrontFace.Clockwise,
                true, true);

            var pd = new GraphicsPipelineDescription(
                BlendStateDescription.SingleAlphaBlend,
                depthStencilMode,
                rasterizerMode,
                PrimitiveTopology.TriangleList,
                new ShaderSetDescription(new[] { VertexLayout, InstanceLayout },
                                         shaderSet.Shaders,
                                         ShaderHelper.GetSpecializations(gd)),
                new[] { _layout, sc.CommonResourceLayout },
                sc.MainSceneFramebuffer.OutputDescription);

            _pipeline      = factory.CreateGraphicsPipeline(ref pd);
            _pipeline.Name = "P_TileMapRenderer";
            _disposeCollector.Add(_vb, _ib, _layout, _textureSampler, _pipeline);
        }
Exemplo n.º 4
0
        public override void CreateDeviceObjects(GraphicsDevice gd, CommandList cl, SceneContext sc)
        {
            ResourceFactory factory = gd.ResourceFactory;

            _vb = factory.CreateBuffer(new BufferDescription(s_vertices.SizeInBytes(), BufferUsage.VertexBuffer));
            cl.UpdateBuffer(_vb, 0, s_vertices);

            _ib = factory.CreateBuffer(new BufferDescription(s_indices.SizeInBytes(), BufferUsage.IndexBuffer));
            cl.UpdateBuffer(_ib, 0, s_indices);

            ImageSharpCubemapTexture imageSharpCubemapTexture = new ImageSharpCubemapTexture(_right, _left, _top, _bottom, _back, _front, false);

            Texture     textureCube = imageSharpCubemapTexture.CreateDeviceTexture(gd, factory);
            TextureView textureView = factory.CreateTextureView(new TextureViewDescription(textureCube));

            VertexLayoutDescription[] vertexLayouts = new VertexLayoutDescription[]
            {
                new VertexLayoutDescription(
                    new VertexElementDescription("Position", VertexElementSemantic.TextureCoordinate, VertexElementFormat.Float3))
            };

            (Shader vs, Shader fs) = StaticResourceCache.GetShaders(gd, gd.ResourceFactory, "Skybox");

            _layout = factory.CreateResourceLayout(new ResourceLayoutDescription(
                                                       new ResourceLayoutElementDescription("Projection", ResourceKind.UniformBuffer, ShaderStages.Vertex),
                                                       new ResourceLayoutElementDescription("View", ResourceKind.UniformBuffer, ShaderStages.Vertex),
                                                       new ResourceLayoutElementDescription("CubeTexture", ResourceKind.TextureReadOnly, ShaderStages.Fragment),
                                                       new ResourceLayoutElementDescription("CubeSampler", ResourceKind.Sampler, ShaderStages.Fragment)));

            GraphicsPipelineDescription pd = new GraphicsPipelineDescription(
                BlendStateDescription.SingleAlphaBlend,
                gd.IsDepthRangeZeroToOne ? DepthStencilStateDescription.DepthOnlyGreaterEqual : DepthStencilStateDescription.DepthOnlyLessEqual,
                new RasterizerStateDescription(FaceCullMode.None, PolygonFillMode.Solid, FrontFace.Clockwise, true, true),
                PrimitiveTopology.TriangleList,
                new ShaderSetDescription(vertexLayouts, new[] { vs, fs }, ShaderHelper.GetSpecializations(gd)),
                new ResourceLayout[] { _layout },
                sc.MainSceneFramebuffer.OutputDescription);

            _pipeline           = factory.CreateGraphicsPipeline(ref pd);
            pd.Outputs          = sc.ReflectionFramebuffer.OutputDescription;
            _reflectionPipeline = factory.CreateGraphicsPipeline(ref pd);

            _resourceSet = factory.CreateResourceSet(new ResourceSetDescription(
                                                         _layout,
                                                         sc.ProjectionMatrixBuffer,
                                                         sc.ViewMatrixBuffer,
                                                         textureView,
                                                         gd.PointSampler));

            _disposeCollector.Add(_vb, _ib, textureCube, textureView, _layout, _pipeline, _reflectionPipeline, _resourceSet, vs, fs);
        }
Exemplo n.º 5
0
        public override void CreateDeviceObjects(GraphicsDevice gd, CommandList cl, SceneContext sc)
        {
            ResourceFactory factory = gd.ResourceFactory;

            _vb = factory.CreateBuffer(new BufferDescription(s_vertices.SizeInBytes(), BufferUsage.VertexBuffer));
            cl.UpdateBuffer(_vb, 0, s_vertices);

            _ib = factory.CreateBuffer(new BufferDescription(s_indices.SizeInBytes(), BufferUsage.IndexBuffer));
            cl.UpdateBuffer(_ib, 0, s_indices);

            ImageSharpCubemapTexture cubemap = PreloadTexture(sc);
            Texture textureCube = cubemap.CreateDeviceTexture(gd, factory);

            TextureLoaded?.Invoke(sc, cubemap);
            _pendingCubemap = null;

            VertexLayoutDescription[] vertexLayouts = new VertexLayoutDescription[]
            {
                new VertexLayoutDescription(
                    new VertexElementDescription("Position", VertexElementSemantic.TextureCoordinate, VertexElementFormat.Float3))
            };

            (Shader vs, Shader fs, SpecializationConstant[] specs) = StaticResourceCache.GetShaders(gd, gd.ResourceFactory, "Skybox");

            _layout = factory.CreateResourceLayout(new ResourceLayoutDescription(
                                                       new ResourceLayoutElementDescription("CameraInfo", ResourceKind.UniformBuffer, ShaderStages.Vertex),
                                                       new ResourceLayoutElementDescription("CubeTexture", ResourceKind.TextureReadOnly, ShaderStages.Fragment),
                                                       new ResourceLayoutElementDescription("CubeSampler", ResourceKind.Sampler, ShaderStages.Fragment)));

            GraphicsPipelineDescription pd = new GraphicsPipelineDescription(
                BlendStateDescription.SingleAlphaBlend,
                gd.IsDepthRangeZeroToOne ? DepthStencilStateDescription.DepthOnlyGreaterEqual : DepthStencilStateDescription.DepthOnlyLessEqual,
                new RasterizerStateDescription(FaceCullMode.None, PolygonFillMode.Solid, FrontFace.Clockwise, true, true),
                PrimitiveTopology.TriangleList,
                new ShaderSetDescription(vertexLayouts, new[] { vs, fs }, specs),
                new ResourceLayout[] { _layout },
                sc.MainSceneFramebuffer.OutputDescription);

            _pipeline = factory.CreateGraphicsPipeline(ref pd);

            _resourceSet = factory.CreateResourceSet(new ResourceSetDescription(
                                                         _layout,
                                                         sc.CameraInfoBuffer,
                                                         textureCube,
                                                         gd.PointSampler));

            _disposeCollector.Add(_vb, _ib, textureCube, _layout, _pipeline, _resourceSet, vs, fs);
        }
Exemplo n.º 6
0
        public override void CreateDeviceObjects(GraphicsDevice gd, CommandList cl, SceneContext sc, ResourceScope scope)
        {
            if ((scope & ResourceScope.Map) == 0)
            {
                return;
            }

            //TODO: use dispose factory
            var factory = gd.ResourceFactory;

            _vb = factory.CreateBuffer(new BufferDescription(s_vertices.SizeInBytes(), BufferUsage.VertexBuffer));
            cl.UpdateBuffer(_vb, 0, s_vertices);

            _ib = factory.CreateBuffer(new BufferDescription(s_indices.SizeInBytes(), BufferUsage.IndexBuffer));
            cl.UpdateBuffer(_ib, 0, s_indices);

            VertexLayoutDescription[] vertexLayouts = new VertexLayoutDescription[]
            {
                new VertexLayoutDescription(
                    new VertexElementDescription("Position", VertexElementSemantic.TextureCoordinate, VertexElementFormat.Float3),
                    new VertexElementDescription("Color", VertexElementSemantic.Color, VertexElementFormat.Float3))
            };

            (Shader vs, Shader fs) = sc.MapResourceCache.GetShaders(gd, gd.ResourceFactory, "CoordinateAxes");

            _layout = factory.CreateResourceLayout(new ResourceLayoutDescription(
                                                       new ResourceLayoutElementDescription("Projection", ResourceKind.UniformBuffer, ShaderStages.Vertex),
                                                       new ResourceLayoutElementDescription("View", ResourceKind.UniformBuffer, ShaderStages.Vertex)));

            GraphicsPipelineDescription pd = new GraphicsPipelineDescription(
                BlendStateDescription.SingleAlphaBlend,
                gd.IsDepthRangeZeroToOne ? DepthStencilStateDescription.DepthOnlyGreaterEqual : DepthStencilStateDescription.DepthOnlyLessEqual,
                new RasterizerStateDescription(FaceCullMode.None, PolygonFillMode.Solid, FrontFace.Clockwise, true, true),
                PrimitiveTopology.LineList,
                new ShaderSetDescription(vertexLayouts, new[] { vs, fs }),
                new ResourceLayout[] { _layout },
                sc.MainSceneFramebuffer.OutputDescription);

            _pipeline = factory.CreateGraphicsPipeline(ref pd);

            _resourceSet = factory.CreateResourceSet(new ResourceSetDescription(
                                                         _layout,
                                                         sc.ProjectionMatrixBuffer,
                                                         sc.ViewMatrixBuffer));

            _disposeCollector.Add(_vb, _ib, _layout, _pipeline, _resourceSet, vs, fs);
        }
Exemplo n.º 7
0
        public void CreateDeviceObjects(IRendererContext context)
        {
            var c = (VeldridRendererContext)context;

            ResourceFactory factory = c.GraphicsDevice.ResourceFactory;

            _vertexBuffer       = factory.CreateBuffer(new BufferDescription(Vertices.SizeInBytes(), BufferUsage.VertexBuffer));
            _indexBuffer        = factory.CreateBuffer(new BufferDescription(Indices.SizeInBytes(), BufferUsage.IndexBuffer));
            _uniformBuffer      = factory.CreateBuffer(new BufferDescription((uint)Unsafe.SizeOf <SkyboxUniformInfo>(), BufferUsage.UniformBuffer));
            _vertexBuffer.Name  = "SpriteVertexBuffer";
            _indexBuffer.Name   = "SpriteIndexBuffer";
            _uniformBuffer.Name = "SpriteUniformBuffer";
            c.CommandList.UpdateBuffer(_vertexBuffer, 0, Vertices);
            c.CommandList.UpdateBuffer(_indexBuffer, 0, Indices);

            _resourceLayout = factory.CreateResourceLayout(ResourceLayoutDescription);
            _disposeCollector.Add(_vertexBuffer, _indexBuffer, _uniformBuffer, _resourceLayout);
            _pipeline = BuildPipeline(c.GraphicsDevice, c.SceneContext);
        }
Exemplo n.º 8
0
        public void CreateDeviceObjects(IRendererContext context)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }
            var c       = (VeldridRendererContext)context;
            var cl      = c.CommandList;
            var gd      = c.GraphicsDevice;
            var factory = gd.ResourceFactory;

            _vertexBuffer      = factory.CreateBuffer(new BufferDescription(Vertices.SizeInBytes(), BufferUsage.VertexBuffer));
            _indexBuffer       = factory.CreateBuffer(new BufferDescription(Indices.SizeInBytes(), BufferUsage.IndexBuffer));
            _vertexBuffer.Name = "SpriteVertexBuffer";
            _indexBuffer.Name  = "SpriteIndexBuffer";
            cl.UpdateBuffer(_vertexBuffer, 0, Vertices);
            cl.UpdateBuffer(_indexBuffer, 0, Indices);

            _perSpriteResourceLayout = factory.CreateResourceLayout(PerSpriteLayoutDescription);
            _disposeCollector.Add(_vertexBuffer, _indexBuffer, _perSpriteResourceLayout);
        }
Exemplo n.º 9
0
        public unsafe override void CreateDeviceObjects(GraphicsDevice gd, CommandList cl, SceneContext sc)
        {
            ResourceFactory factory = gd.ResourceFactory;

            _vb = factory.CreateBuffer(new BufferDescription(VertexPosition.SizeInBytes * 4, BufferUsage.VertexBuffer));
            cl.UpdateBuffer(_vb, 0, new VertexPosition[]
            {
                new VertexPosition(new Vector3(-1000, 0, -1000)),
                new VertexPosition(new Vector3(+1000, 0, -1000)),
                new VertexPosition(new Vector3(+1000, 0, +1000)),
                new VertexPosition(new Vector3(-1000, 0, +1000)),
            });

            _ib = factory.CreateBuffer(new BufferDescription(6 * 2, BufferUsage.IndexBuffer));
            cl.UpdateBuffer(_ib, 0, new ushort[] { 0, 1, 2, 0, 2, 3 });

            const int gridSize    = 64;
            RgbaByte  borderColor = new RgbaByte(255, 255, 255, 150);

            RgbaByte[] pixels      = CreateGridTexturePixels(gridSize, 1, borderColor, new RgbaByte());
            Texture    gridTexture = factory.CreateTexture(new TextureDescription(gridSize, gridSize, 1, 1, 1, PixelFormat.R8_G8_B8_A8_UNorm, TextureUsage.Sampled));

            fixed(RgbaByte *pixelsPtr = pixels)
            {
                gd.UpdateTexture(gridTexture, (IntPtr)pixelsPtr, pixels.SizeInBytes(), 0, 0, 0, gridSize, gridSize, 1, 0, 0);
            }

            TextureView textureView = factory.CreateTextureView(new TextureViewDescription(gridTexture));

            VertexLayoutDescription[] vertexLayouts = new VertexLayoutDescription[]
            {
                new VertexLayoutDescription(
                    new VertexElementDescription("Position", VertexElementSemantic.Position, VertexElementFormat.Float3))
            };

            Shader gridVS = ShaderHelper.LoadShader(gd, factory, "Grid", ShaderStages.Vertex, "VS");
            Shader gridFS = ShaderHelper.LoadShader(gd, factory, "Grid", ShaderStages.Fragment, "FS");

            ResourceLayout layout = factory.CreateResourceLayout(new ResourceLayoutDescription(
                                                                     new ResourceLayoutElementDescription("Projection", ResourceKind.UniformBuffer, ShaderStages.Vertex),
                                                                     new ResourceLayoutElementDescription("View", ResourceKind.UniformBuffer, ShaderStages.Vertex),
                                                                     new ResourceLayoutElementDescription("GridTexture", ResourceKind.TextureReadOnly, ShaderStages.Fragment),
                                                                     new ResourceLayoutElementDescription("GridSampler", ResourceKind.Sampler, ShaderStages.Fragment)));

            GraphicsPipelineDescription pd = new GraphicsPipelineDescription(
                BlendStateDescription.SingleAlphaBlend,
                DepthStencilStateDescription.DepthOnlyLessEqual,
                new RasterizerStateDescription(FaceCullMode.None, PolygonFillMode.Solid, FrontFace.Clockwise, true, true),
                PrimitiveTopology.TriangleList,
                new ShaderSetDescription(vertexLayouts, new[] { gridVS, gridFS }),
                new ResourceLayout[] { layout },
                sc.MainSceneFramebuffer.OutputDescription);

            _pipeline = factory.CreateGraphicsPipeline(ref pd);

            _resourceSet = factory.CreateResourceSet(new ResourceSetDescription(
                                                         layout,
                                                         sc.ProjectionMatrixBuffer,
                                                         sc.ViewMatrixBuffer,
                                                         textureView,
                                                         gd.PointSampler));

            _disposeCollector.Add(_vb, _ib, gridTexture, textureView, gridVS, gridFS, layout, _pipeline, _resourceSet);
        }
Exemplo n.º 10
0
        public unsafe override void CreateDeviceObjects(GraphicsDevice gd, CommandList cl, SceneContext sc, ResourceScope scope)
        {
            if ((scope & ResourceScope.Map) == 0)
            {
                return;
            }

            ResourceFactory factory = gd.ResourceFactory;

            _vb = factory.CreateBuffer(new BufferDescription(s_vertices.SizeInBytes(), BufferUsage.VertexBuffer));
            cl.UpdateBuffer(_vb, 0, s_vertices);

            _ib = factory.CreateBuffer(new BufferDescription(s_indices.SizeInBytes(), BufferUsage.IndexBuffer));
            cl.UpdateBuffer(_ib, 0, s_indices);

            Texture     textureCube;
            TextureView textureView;

            fixed(Rgba32 *frontPin = &MemoryMarshal.GetReference(_front.GetPixelSpan()))
            {
                fixed(Rgba32 *backPin = &MemoryMarshal.GetReference(_back.GetPixelSpan()))
                {
                    fixed(Rgba32 *leftPin = &MemoryMarshal.GetReference(_left.GetPixelSpan()))
                    {
                        fixed(Rgba32 *rightPin = &MemoryMarshal.GetReference(_right.GetPixelSpan()))
                        {
                            fixed(Rgba32 *topPin = &MemoryMarshal.GetReference(_top.GetPixelSpan()))
                            {
                                fixed(Rgba32 *bottomPin = &MemoryMarshal.GetReference(_bottom.GetPixelSpan()))
                                {
                                    uint width  = (uint)_front.Width;
                                    uint height = (uint)_front.Height;

                                    textureCube = factory.CreateTexture(TextureDescription.Texture2D(
                                                                            width,
                                                                            height,
                                                                            1,
                                                                            1,
                                                                            PixelFormat.R8_G8_B8_A8_UNorm,
                                                                            TextureUsage.Sampled | TextureUsage.Cubemap));

                                    uint faceSize = (uint)(_front.Width * _front.Height * Unsafe.SizeOf <Rgba32>());

                                    gd.UpdateTexture(textureCube, (IntPtr)leftPin, faceSize, 0, 0, 0, width, height, 1, 0, 0);
                                    gd.UpdateTexture(textureCube, (IntPtr)rightPin, faceSize, 0, 0, 0, width, height, 1, 0, 1);
                                    gd.UpdateTexture(textureCube, (IntPtr)backPin, faceSize, 0, 0, 0, width, height, 1, 0, 2);
                                    gd.UpdateTexture(textureCube, (IntPtr)frontPin, faceSize, 0, 0, 0, width, height, 1, 0, 3);
                                    gd.UpdateTexture(textureCube, (IntPtr)topPin, faceSize, 0, 0, 0, width, height, 1, 0, 4);
                                    gd.UpdateTexture(textureCube, (IntPtr)bottomPin, faceSize, 0, 0, 0, width, height, 1, 0, 5);

                                    textureView = factory.CreateTextureView(new TextureViewDescription(textureCube));
                                }
                            }
                        }
                    }
                }
            }

            //Because GoldSource's coordinate system isn't the default OpenGL one, we have to pass texture coordinates seperately
            VertexLayoutDescription[] vertexLayouts = new VertexLayoutDescription[]
            {
                new VertexLayoutDescription(
                    new VertexElementDescription("Position", VertexElementSemantic.TextureCoordinate, VertexElementFormat.Float3),
                    new VertexElementDescription("TextureCoords", VertexElementSemantic.TextureCoordinate, VertexElementFormat.Float3))
            };

            (Shader vs, Shader fs) = sc.MapResourceCache.GetShaders(gd, gd.ResourceFactory, "Skybox2D");

            _layout = factory.CreateResourceLayout(new ResourceLayoutDescription(
                                                       new ResourceLayoutElementDescription("Projection", ResourceKind.UniformBuffer, ShaderStages.Vertex),
                                                       new ResourceLayoutElementDescription("View", ResourceKind.UniformBuffer, ShaderStages.Vertex),
                                                       new ResourceLayoutElementDescription("CubeTexture", ResourceKind.TextureReadOnly, ShaderStages.Fragment),
                                                       new ResourceLayoutElementDescription("CubeSampler", ResourceKind.Sampler, ShaderStages.Fragment)));

            GraphicsPipelineDescription pd = new GraphicsPipelineDescription(
                BlendStateDescription.SingleAlphaBlend,
                gd.IsDepthRangeZeroToOne ? DepthStencilStateDescription.DepthOnlyGreaterEqual : DepthStencilStateDescription.DepthOnlyLessEqual,
                new RasterizerStateDescription(FaceCullMode.None, PolygonFillMode.Solid, FrontFace.Clockwise, true, true),
                PrimitiveTopology.TriangleList,
                new ShaderSetDescription(vertexLayouts, new[] { vs, fs }),
                new ResourceLayout[] { _layout },
                sc.MainSceneFramebuffer.OutputDescription);

            _pipeline = factory.CreateGraphicsPipeline(ref pd);

            _resourceSet = factory.CreateResourceSet(new ResourceSetDescription(
                                                         _layout,
                                                         sc.ProjectionMatrixBuffer,
                                                         sc.ViewMatrixBuffer,
                                                         textureView,
                                                         gd.PointSampler));

            _disposeCollector.Add(_vb, _ib, textureCube, textureView, _layout, _pipeline, _resourceSet, vs, fs);
        }
Exemplo n.º 11
0
        public unsafe override void CreateDeviceObjects(GraphicsDevice gd, CommandList cl, SceneContext sc)
        {
            ResourceFactory factory = gd.ResourceFactory;

            _vb = factory.CreateBuffer(new BufferDescription(s_vertices.SizeInBytes(), BufferUsage.VertexBuffer));
            cl.UpdateBuffer(_vb, 0, s_vertices);

            _ib = factory.CreateBuffer(new BufferDescription(s_indices.SizeInBytes(), BufferUsage.IndexBuffer));
            cl.UpdateBuffer(_ib, 0, s_indices);

            Texture     textureCube;
            TextureView textureView;

            fixed(Rgba32 *frontPin = &_front.DangerousGetPinnableReferenceToPixelBuffer())
            fixed(Rgba32 * backPin   = &_back.DangerousGetPinnableReferenceToPixelBuffer())
            fixed(Rgba32 * leftPin   = &_left.DangerousGetPinnableReferenceToPixelBuffer())
            fixed(Rgba32 * rightPin  = &_right.DangerousGetPinnableReferenceToPixelBuffer())
            fixed(Rgba32 * topPin    = &_top.DangerousGetPinnableReferenceToPixelBuffer())
            fixed(Rgba32 * bottomPin = &_bottom.DangerousGetPinnableReferenceToPixelBuffer())
            {
                uint width  = (uint)_front.Width;
                uint height = (uint)_front.Height;

                textureCube = factory.CreateTexture(TextureDescription.Texture2D(
                                                        width,
                                                        height,
                                                        1,
                                                        1,
                                                        PixelFormat.R8_G8_B8_A8_UNorm,
                                                        TextureUsage.Sampled | TextureUsage.Cubemap));

                uint faceSize = (uint)(_front.Width * _front.Height * Unsafe.SizeOf <Rgba32>());

                gd.UpdateTexture(textureCube, (IntPtr)rightPin, faceSize, 0, 0, 0, width, height, 1, 0, 0);
                gd.UpdateTexture(textureCube, (IntPtr)leftPin, faceSize, 0, 0, 0, width, height, 1, 0, 1);
                gd.UpdateTexture(textureCube, (IntPtr)topPin, faceSize, 0, 0, 0, width, height, 1, 0, 2);
                gd.UpdateTexture(textureCube, (IntPtr)bottomPin, faceSize, 0, 0, 0, width, height, 1, 0, 3);
                gd.UpdateTexture(textureCube, (IntPtr)backPin, faceSize, 0, 0, 0, width, height, 1, 0, 4);
                gd.UpdateTexture(textureCube, (IntPtr)frontPin, faceSize, 0, 0, 0, width, height, 1, 0, 5);

                textureView = factory.CreateTextureView(new TextureViewDescription(textureCube));
            }

            VertexLayoutDescription[] vertexLayouts = new VertexLayoutDescription[]
            {
                new VertexLayoutDescription(
                    new VertexElementDescription("Position", VertexElementSemantic.Position, VertexElementFormat.Float3))
            };

            Shader vs = ShaderHelper.LoadShader(gd, factory, "Skybox", ShaderStages.Vertex, "VS");
            Shader fs = ShaderHelper.LoadShader(gd, factory, "Skybox", ShaderStages.Fragment, "FS");

            _layout = factory.CreateResourceLayout(new ResourceLayoutDescription(
                                                       new ResourceLayoutElementDescription("Projection", ResourceKind.UniformBuffer, ShaderStages.Vertex),
                                                       new ResourceLayoutElementDescription("View", ResourceKind.UniformBuffer, ShaderStages.Vertex),
                                                       new ResourceLayoutElementDescription("CubeTexture", ResourceKind.TextureReadOnly, ShaderStages.Fragment),
                                                       new ResourceLayoutElementDescription("CubeSampler", ResourceKind.Sampler, ShaderStages.Fragment)));

            GraphicsPipelineDescription pd = new GraphicsPipelineDescription(
                BlendStateDescription.SingleAlphaBlend,
                DepthStencilStateDescription.DepthOnlyLessEqual,
                new RasterizerStateDescription(FaceCullMode.None, PolygonFillMode.Solid, FrontFace.Clockwise, true, true),
                PrimitiveTopology.TriangleList,
                new ShaderSetDescription(vertexLayouts, new[] { vs, fs }),
                new ResourceLayout[] { _layout },
                sc.MainSceneFramebuffer.OutputDescription);

            _pipeline           = factory.CreateGraphicsPipeline(ref pd);
            pd.Outputs          = sc.ReflectionFramebuffer.OutputDescription;
            _reflectionPipeline = factory.CreateGraphicsPipeline(ref pd);

            _resourceSet = factory.CreateResourceSet(new ResourceSetDescription(
                                                         _layout,
                                                         sc.ProjectionMatrixBuffer,
                                                         sc.ViewMatrixBuffer,
                                                         textureView,
                                                         gd.PointSampler));

            _disposeCollector.Add(_vb, _ib, textureCube, textureView, _layout, _pipeline, _reflectionPipeline, _resourceSet, vs, fs);
        }
 T Track <T>(T disposable) where T : IDisposable
 {
     _collector.Add(disposable);
     return(disposable);
 }
Exemplo n.º 13
0
        public override void CreateDeviceObjects(GraphicsDevice gd, CommandList cl, SceneContext sc, ResourceScope scope)
        {
            if ((scope & ResourceScope.Map) == 0)
            {
                return;
            }

            var disposeFactory = new DisposeCollectorResourceFactory(gd.ResourceFactory, _disposeCollector);

            //Build a list of buffers, each buffer containing all of the faces that have the same texture
            //This reduces the number of buffers we have to create from a set for each face to a set for each texture and all of the faces referencing it
            //TODO: further split by visleaf when vis data is available
            var sortedFaces = BSPModel.SubModel.Faces.GroupBy(face => face.TextureInfo.MipTexture.Name);

            sortedFaces = sortedFaces.OrderByDescending(grouping => grouping.Count());

            var lightmapBuilders = new List <LightmapBuilder>
            {
                new LightmapBuilder(LightmapPoolSize, LightmapPoolSize)
            };

            var vertices = new List <BSPSurfaceData>();
            var indices  = new List <uint>();

            //A white texture is used so when rendering surfaces with no lightmaps, the surface is fullbright
            //Since surfaces like these are typically triggers it makes things a lot easier
            using (var defaultLightmapTexture = Image.LoadPixelData(new[] { Rgba32.White }, 1, 1))
            {
                foreach (var faces in sortedFaces)
                {
                    if (faces.Key != "sky")
                    {
                        var facesList = faces.ToList();

                        var mipTexture = facesList[0].TextureInfo.MipTexture;

                        var texture = sc.MapResourceCache.GetTexture2D(mipTexture.Name);

                        //If not found, fallback to have a valid texture
                        texture = texture ?? sc.MapResourceCache.GetPinkTexture(gd, gd.ResourceFactory);

                        var view = sc.MapResourceCache.GetTextureView(gd.ResourceFactory, texture);

                        var textureResourceSet = gd.ResourceFactory.CreateResourceSet(new ResourceSetDescription(sc.ModelRenderer.BrushRenderer.TextureLayout, view, sc.MainSampler));

                        BSPResourceUtils.BuildFacesBuffer(
                            BSPModel.BSPFile,
                            facesList,
                            mipTexture,
                            textureResourceSet,
                            defaultLightmapTexture,
                            lightmapBuilders,
                            vertices,
                            indices);
                    }
                }
            }

            Lightmaps = lightmapBuilders
                        .Select(builder => builder.Build(sc.ModelRenderer.BrushRenderer.LightmapLayout, sc.MapResourceCache, gd, gd.ResourceFactory))
                        .ToArray();

            Array.ForEach(Lightmaps, lightmap => _disposeCollector.Add(lightmap));

            var verticesArray = vertices.ToArray();
            var indicesArray  = indices.ToArray();

            VertexBuffer = disposeFactory.CreateBuffer(new BufferDescription(verticesArray.SizeInBytes(), BufferUsage.VertexBuffer));

            cl.UpdateBuffer(VertexBuffer, 0, verticesArray);

            IndexBuffer = disposeFactory.CreateBuffer(new BufferDescription(indicesArray.SizeInBytes(), BufferUsage.IndexBuffer));

            cl.UpdateBuffer(IndexBuffer, 0, indicesArray);
        }
Exemplo n.º 14
0
        public unsafe void CreateDeviceObjects(GraphicsDevice gd, CommandList cl, GraphicsSystem sc)
        {
            AssetDatabase   ad      = _as.Database;
            ResourceFactory factory = gd.ResourceFactory;

            _vb = factory.CreateBuffer(new BufferDescription(s_vertices.SizeInBytes(), BufferUsage.VertexBuffer));
            cl.UpdateBuffer(_vb, 0, s_vertices);

            _ib = factory.CreateBuffer(new BufferDescription(s_indices.SizeInBytes(), BufferUsage.IndexBuffer));
            cl.UpdateBuffer(_ib, 0, s_indices);

            Texture     textureCube;
            TextureView textureView;


            var front  = ad.LoadAsset <ImageSharpTexture>(EngineEmbeddedAssets.SkyboxFrontID);
            var back   = ad.LoadAsset <ImageSharpTexture>(EngineEmbeddedAssets.SkyboxBackID);
            var left   = ad.LoadAsset <ImageSharpTexture>(EngineEmbeddedAssets.SkyboxLeftID);
            var right  = ad.LoadAsset <ImageSharpTexture>(EngineEmbeddedAssets.SkyboxRightID);
            var top    = ad.LoadAsset <ImageSharpTexture>(EngineEmbeddedAssets.SkyboxTopID);
            var bottom = ad.LoadAsset <ImageSharpTexture>(EngineEmbeddedAssets.SkyboxBottomID);


            // var front = !_front.HasValue ? _front.Get(ad) : ad.LoadAsset<ImageSharpTexture>(EngineEmbeddedAssets.SkyboxFrontID);
            // var back = !_back.HasValue ? _back.Get(ad) : ad.LoadAsset<ImageSharpTexture>(EngineEmbeddedAssets.SkyboxBackID);
            // var left = !_left.HasValue ? _left.Get(ad) : ad.LoadAsset<ImageSharpTexture>(EngineEmbeddedAssets.SkyboxLeftID);
            // var right = !_right.HasValue ? _right.Get(ad) : ad.LoadAsset<ImageSharpTexture>(EngineEmbeddedAssets.SkyboxRightID);
            // var top = !_top.HasValue ? _top.Get(ad) : ad.LoadAsset<ImageSharpTexture>(EngineEmbeddedAssets.SkyboxTopID);
            // var bottom = !_bottom.HasValue ? _bottom.Get(ad) : ad.LoadAsset<ImageSharpTexture>(EngineEmbeddedAssets.SkyboxBottomID);


            // front.Images[0].DangerousGetPinnableReferenceToPixelBuffer()

            // using (var frontPin = front.Pixels.Pin())
            // using (var backPin = back.Pixels.Pin())
            // using (var leftPin = left.Pixels.Pin())
            // using (var rightPin = right.Pixels.Pin())
            // using (var topPin = top.Pixels.Pin())
            // using (var bottomPin = bottom.Pixels.Pin())

            fixed(Rgba32 *frontPin = &front.Images[0].DangerousGetPinnableReferenceToPixelBuffer())
            fixed(Rgba32 * backPin   = &back.Images[0].DangerousGetPinnableReferenceToPixelBuffer())
            fixed(Rgba32 * leftPin   = &left.Images[0].DangerousGetPinnableReferenceToPixelBuffer())
            fixed(Rgba32 * rightPin  = &right.Images[0].DangerousGetPinnableReferenceToPixelBuffer())
            fixed(Rgba32 * topPin    = &top.Images[0].DangerousGetPinnableReferenceToPixelBuffer())
            fixed(Rgba32 * bottomPin = &bottom.Images[0].DangerousGetPinnableReferenceToPixelBuffer())

            {
                uint width  = (uint)front.Width;
                uint height = (uint)front.Height;

                textureCube = factory.CreateTexture(TextureDescription.Texture2D(
                                                        width,
                                                        height,
                                                        1,
                                                        1,
                                                        PixelFormat.R8_G8_B8_A8_UNorm,
                                                        TextureUsage.Sampled | TextureUsage.Cubemap));

                uint faceSize = (uint)(front.Width * front.Height * Unsafe.SizeOf <Rgba32>());

                gd.UpdateTexture(textureCube, (IntPtr)rightPin, faceSize, 0, 0, 0, width, height, 1, 0, 0);
                gd.UpdateTexture(textureCube, (IntPtr)leftPin, faceSize, 0, 0, 0, width, height, 1, 0, 1);
                gd.UpdateTexture(textureCube, (IntPtr)topPin, faceSize, 0, 0, 0, width, height, 1, 0, 2);
                gd.UpdateTexture(textureCube, (IntPtr)bottomPin, faceSize, 0, 0, 0, width, height, 1, 0, 3);
                gd.UpdateTexture(textureCube, (IntPtr)backPin, faceSize, 0, 0, 0, width, height, 1, 0, 4);
                gd.UpdateTexture(textureCube, (IntPtr)frontPin, faceSize, 0, 0, 0, width, height, 1, 0, 5);

                textureView = factory.CreateTextureView(new TextureViewDescription(textureCube));
            }

            VertexLayoutDescription[] vertexLayouts = new VertexLayoutDescription[]
            {
                new VertexLayoutDescription(
                    new VertexElementDescription("Position", VertexElementSemantic.TextureCoordinate, VertexElementFormat.Float3))
            };

            (Shader vs, Shader fs) = StaticResourceCache.GetShaders(gd, gd.ResourceFactory, "Skybox");

            _layout = factory.CreateResourceLayout(new ResourceLayoutDescription(
                                                       new ResourceLayoutElementDescription("Projection", ResourceKind.UniformBuffer, ShaderStages.Vertex),
                                                       new ResourceLayoutElementDescription("View", ResourceKind.UniformBuffer, ShaderStages.Vertex),
                                                       new ResourceLayoutElementDescription("CubeTexture", ResourceKind.TextureReadOnly, ShaderStages.Fragment),
                                                       new ResourceLayoutElementDescription("CubeSampler", ResourceKind.Sampler, ShaderStages.Fragment)));

            GraphicsPipelineDescription pd = new GraphicsPipelineDescription(
                BlendStateDescription.SingleAlphaBlend,
                gd.IsDepthRangeZeroToOne ? DepthStencilStateDescription.DepthOnlyGreaterEqual : DepthStencilStateDescription.DepthOnlyLessEqual,
                new RasterizerStateDescription(FaceCullMode.None, PolygonFillMode.Solid, FrontFace.Clockwise, true, true),
                PrimitiveTopology.TriangleList,
                new ShaderSetDescription(vertexLayouts, new[] { vs, fs }),
                new ResourceLayout[] { _layout },
                sc.MainSceneFramebuffer.OutputDescription);

            _pipeline           = factory.CreateGraphicsPipeline(ref pd);
            pd.Outputs          = sc.ReflectionFramebuffer.OutputDescription;
            _reflectionPipeline = factory.CreateGraphicsPipeline(ref pd);

            _resourceSet = factory.CreateResourceSet(new ResourceSetDescription(
                                                         _layout,
                                                         sc.ProjectionMatrixBuffer,
                                                         sc.ViewMatrixBuffer,
                                                         textureView,
                                                         gd.PointSampler));

            _disposeCollector.Add(_vb, _ib, textureCube, textureView, _layout, _pipeline, _reflectionPipeline, _resourceSet, vs, fs);
        }