Пример #1
0
        public unsafe override void CreateDeviceObjects(GraphicsDevice gd, CommandList cl, SceneContext sc)
        {
            ResourceFactory factory = gd.ResourceFactory;

            _vb = factory.CreateVertexBuffer(new BufferDescription(s_vertices.SizeInBytes()));
            cl.UpdateBuffer(_vb, 0, s_vertices);

            _ib = factory.CreateIndexBuffer(new IndexBufferDescription(s_indices.SizeInBytes(), IndexFormat.UInt16));
            cl.UpdateBuffer(_ib, 0, s_indices);

            _viewMatrixBuffer = factory.CreateUniformBuffer(new BufferDescription((ulong)Unsafe.SizeOf <Matrix4x4>()));

            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(new TextureDescription(
                                                        width,
                                                        height,
                                                        1,
                                                        1,
                                                        1,
                                                        PixelFormat.R8_G8_B8_A8_UNorm,
                                                        TextureUsage.Sampled | TextureUsage.Cubemap));

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

                cl.UpdateTextureCube(textureCube, (IntPtr)leftPin, faceSize, CubeFace.NegativeX, 0, 0, width, height, 0, 0);
                cl.UpdateTextureCube(textureCube, (IntPtr)rightPin, faceSize, CubeFace.PositiveX, 0, 0, width, height, 0, 0);
                cl.UpdateTextureCube(textureCube, (IntPtr)bottomPin, faceSize, CubeFace.NegativeY, 0, 0, width, height, 0, 0);
                cl.UpdateTextureCube(textureCube, (IntPtr)topPin, faceSize, CubeFace.PositiveY, 0, 0, width, height, 0, 0);
                cl.UpdateTextureCube(textureCube, (IntPtr)backPin, faceSize, CubeFace.NegativeZ, 0, 0, width, height, 0, 0);
                cl.UpdateTextureCube(textureCube, (IntPtr)frontPin, faceSize, CubeFace.PositiveZ, 0, 0, width, height, 0, 0);

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

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

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

            ShaderStageDescription[] shaderStages = new ShaderStageDescription[]
            {
                new ShaderStageDescription(ShaderStages.Vertex, vs, "VS"),
                new ShaderStageDescription(ShaderStages.Fragment, fs, "VS"),
            };

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

            PipelineDescription pd = new PipelineDescription(
                BlendStateDescription.SingleAlphaBlend,
                DepthStencilStateDescription.LessEqual,
                new RasterizerStateDescription(FaceCullMode.None, TriangleFillMode.Solid, FrontFace.Clockwise, false, true),
                PrimitiveTopology.TriangleList,
                new ShaderSetDescription(vertexLayouts, shaderStages),
                new ResourceLayout[] { _layout },
                gd.SwapchainFramebuffer.OutputDescription);

            _pipeline = factory.CreatePipeline(ref pd);

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

            _disposeCollector.Add(_vb, _ib, _viewMatrixBuffer, textureCube, textureView, _layout, _pipeline, _resourceSet, vs, fs);
        }
Пример #2
0
        public unsafe override void CreateDeviceObjects(GraphicsDevice gd, CommandList cl, SceneContext sc)
        {
            ResourceFactory factory = gd.ResourceFactory;

            _vb = factory.CreateVertexBuffer(new BufferDescription(VertexPosition.SizeInBytes * 4));
            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.CreateIndexBuffer(new IndexBufferDescription(6 * 2, IndexFormat.UInt16));
            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)
            {
                cl.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(factory, "Grid", ShaderStages.Vertex);
            Shader gridFS = ShaderHelper.LoadShader(factory, "Grid", ShaderStages.Fragment);

            ShaderStageDescription[] shaderStages = new ShaderStageDescription[]
            {
                new ShaderStageDescription(ShaderStages.Vertex, gridVS, "VS"),
                new ShaderStageDescription(ShaderStages.Fragment, gridFS, "FS"),
            };

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

            PipelineDescription pd = new PipelineDescription(
                BlendStateDescription.SingleAlphaBlend,
                DepthStencilStateDescription.LessEqual,
                new RasterizerStateDescription(FaceCullMode.None, TriangleFillMode.Solid, FrontFace.Clockwise, true, true),
                PrimitiveTopology.TriangleList,
                new ShaderSetDescription(vertexLayouts, shaderStages),
                new ResourceLayout[] { layout },
                gd.SwapchainFramebuffer.OutputDescription);

            _pipeline = factory.CreatePipeline(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);
        }