Exemplo n.º 1
0
        void CreateDeviceObjects()
        {
            // set up a commandlist
            commandList = context.CommandList;

            // compile de shader
            imShader = new EffectInstance(effectSystem.LoadEffect("ImGuiShader").WaitForResult());
            imShader.UpdateEffect(device);

            var layout = new VertexDeclaration(
                VertexElement.Position <Vector2>(),
                VertexElement.TextureCoordinate <Vector2>(),
                VertexElement.Color(PixelFormat.R8G8B8A8_UNorm)
                );

            imVertLayout = layout;

            // de pipeline desc
            var pipeline = new PipelineStateDescription()
            {
                BlendState = BlendStates.NonPremultiplied,

                RasterizerState = new RasterizerStateDescription()
                {
                    CullMode  = CullMode.None,
                    DepthBias = 0,
                    FillMode  = FillMode.Solid,
                    MultisampleAntiAliasLine = false,
                    ScissorTestEnable        = true,
                    SlopeScaleDepthBias      = 0,
                },

                PrimitiveType     = PrimitiveType.TriangleList,
                InputElements     = imVertLayout.CreateInputElements(),
                DepthStencilState = DepthStencilStates.Default,

                EffectBytecode = imShader.Effect.Bytecode,
                RootSignature  = imShader.RootSignature,

                Output = new RenderOutputDescription(PixelFormat.R8G8B8A8_UNorm)
            };

            // finally set up the pipeline
            var pipelineState = PipelineState.New(device, ref pipeline);

            imPipeline = pipelineState;

            var is32Bits           = false;
            var indexBuffer        = Xenko.Graphics.Buffer.Index.New(device, INITIAL_INDEX_BUFFER_SIZE * sizeof(ushort), GraphicsResourceUsage.Dynamic);
            var indexBufferBinding = new IndexBufferBinding(indexBuffer, is32Bits, 0);

            indexBinding = indexBufferBinding;

            var vertexBuffer        = Xenko.Graphics.Buffer.Vertex.New(device, INITIAL_VERTEX_BUFFER_SIZE * imVertLayout.CalculateSize(), GraphicsResourceUsage.Dynamic);
            var vertexBufferBinding = new VertexBufferBinding(vertexBuffer, layout, 0);

            vertexBinding = vertexBufferBinding;
        }
Exemplo n.º 2
0
        static VertexDeclaration GetVertexDeclaration(SimpleMesh mesh)
        {
            var elements = new List <VertexElement>();

            elements.Add(VertexElement.Position <Vector3>());
            if (mesh.HasVertexNormals)
            {
                elements.Add(VertexElement.Normal <Vector3>());
            }
            if (mesh.HasVertexUVs)
            {
                elements.Add(VertexElement.TextureCoordinate <Vector2>());
            }
            if (mesh.HasVertexColors)
            {
                elements.Add(VertexElement.Color <Color>());
            }
            return(new VertexDeclaration(elements.ToArray()));
        }
Exemplo n.º 3
0
        private static VertexElement[] CreateElement(VertexShaderFlags flag)
        {
            switch (flag)
            {
            case VertexShaderFlags.Position:
                return(new[] { VertexElement.Position(Format.R32G32B32_Float) });

            case VertexShaderFlags.Normal:
                return(new[] { VertexElement.Normal(Format.R32G32B32_Float) });

            case VertexShaderFlags.TextureUV:
                return(new[] { VertexElement.TextureCoordinate(Format.R32G32_Float) });

            case VertexShaderFlags.TextureUVW:
                return(new[] { VertexElement.TextureCoordinate(Format.R32G32B32_Float) });

            case VertexShaderFlags.Color:
                return(new[] { VertexElement.Color(Format.R32G32B32A32_Float) });

            case VertexShaderFlags.Tangent:
                return(new[] { VertexElement.Tangent(Format.R32G32B32A32_Float) });

            case VertexShaderFlags.Barycentric:
                return(new[] { new VertexElement("BARYCENTRIC", Format.R32G32B32_Float) });

            case VertexShaderFlags.InstanceWorld:
                return(new[]
                {
                    new VertexElement("INSTANCEWORLD", 0, Format.R32G32B32A32_Float),
                    new VertexElement("INSTANCEWORLD", 1, Format.R32G32B32A32_Float),
                    new VertexElement("INSTANCEWORLD", 2, Format.R32G32B32A32_Float),
                    new VertexElement("INSTANCEWORLD", 3, Format.R32G32B32A32_Float),
                });

            default:
                throw new ArgumentOutOfRangeException(string.Format("[{0}]: VertexShaderFlag not valid", flag));
            }
        }
Exemplo n.º 4
0
 public static (VertexElement, int) ConvertVertexElement(KeyValuePair <string, SharpGLTF.Schema2.Accessor> accessor, int offset)
 {
     return((accessor.Key, accessor.Value.Format.ByteSize) switch
     {
         ("POSITION", 12) => (VertexElement.Position <Vector3>(0, offset), Vector3.SizeInBytes),
         ("NORMAL", 12) => (VertexElement.Normal <Vector3>(0, offset), Vector3.SizeInBytes),
         ("TANGENT", 12) => (VertexElement.Tangent <Vector3>(0, offset), Vector3.SizeInBytes),
         ("COLOR", 16) => (VertexElement.Color <Vector4>(0, offset), Vector4.SizeInBytes),
         ("TEXCOORD_0", 8) => (VertexElement.TextureCoordinate <Vector2>(0, offset), Vector2.SizeInBytes),
         ("TEXCOORD_1", 8) => (VertexElement.TextureCoordinate <Vector2>(1, offset), Vector2.SizeInBytes),
         ("TEXCOORD_2", 8) => (VertexElement.TextureCoordinate <Vector2>(2, offset), Vector2.SizeInBytes),
         ("TEXCOORD_3", 8) => (VertexElement.TextureCoordinate <Vector2>(3, offset), Vector2.SizeInBytes),
         ("TEXCOORD_4", 8) => (VertexElement.TextureCoordinate <Vector2>(4, offset), Vector2.SizeInBytes),
         ("TEXCOORD_5", 8) => (VertexElement.TextureCoordinate <Vector2>(5, offset), Vector2.SizeInBytes),
         ("TEXCOORD_6", 8) => (VertexElement.TextureCoordinate <Vector2>(6, offset), Vector2.SizeInBytes),
         ("TEXCOORD_7", 8) => (VertexElement.TextureCoordinate <Vector2>(7, offset), Vector2.SizeInBytes),
         ("TEXCOORD_8", 8) => (VertexElement.TextureCoordinate <Vector2>(8, offset), Vector2.SizeInBytes),
         ("TEXCOORD_9", 8) => (VertexElement.TextureCoordinate <Vector2>(9, offset), Vector2.SizeInBytes),
         ("JOINTS_0", 8) => (new VertexElement(VertexElementUsage.BlendIndices, 0, PixelFormat.R16G16B16A16_UInt, offset), 8),
         ("JOINTS_0", 4) => (new VertexElement(VertexElementUsage.BlendIndices, 0, PixelFormat.R8G8B8A8_UInt, offset), 4),
         ("WEIGHTS_0", 16) => (new VertexElement(VertexElementUsage.BlendWeight, 0, PixelFormat.R32G32B32A32_Float, offset), Vector4.SizeInBytes),
         _ => throw new NotImplementedException(),
     });
Exemplo n.º 5
0
        protected override async Task LoadContent()
        {
            await base.LoadContent();

            var vertices = new Vertex[4];

            vertices[0] = new Vertex {
                Position = new Vector3(-1, -1, 0.5f), TexCoords = new Vector2(0, 0)
            };
            vertices[1] = new Vertex {
                Position = new Vector3(-1, 1, 0.5f), TexCoords = new Vector2(3, 0)
            };
            vertices[2] = new Vertex {
                Position = new Vector3(1, 1, 0.5f), TexCoords = new Vector2(3, 3)
            };
            vertices[3] = new Vertex {
                Position = new Vector3(1, -1, 0.5f), TexCoords = new Vector2(0, 3)
            };

            var indices = new short[] { 0, 1, 2, 0, 2, 3 };

            var vertexBuffer = Buffer.Vertex.New(GraphicsDevice, vertices, GraphicsResourceUsage.Default);
            var indexBuffer  = Buffer.Index.New(GraphicsDevice, indices, GraphicsResourceUsage.Default);
            var meshDraw     = new MeshDraw
            {
                DrawCount     = 4,
                PrimitiveType = PrimitiveType.TriangleList,
                VertexBuffers = new[]
                {
                    new VertexBufferBinding(vertexBuffer,
                                            new VertexDeclaration(VertexElement.Position <Vector3>(),
                                                                  VertexElement.TextureCoordinate <Vector2>()),
                                            4)
                },
                IndexBuffer = new IndexBufferBinding(indexBuffer, false, indices.Length),
            };

            var mesh = new Mesh
            {
                Draw = meshDraw,
            };

            simpleEffect             = new Effect(GraphicsDevice, SpriteEffect.Bytecode);
            parameterCollection      = new ParameterCollection();
            parameterCollectionGroup = new EffectParameterCollectionGroup(GraphicsDevice, simpleEffect, new[] { parameterCollection });
            parameterCollection.Set(TexturingKeys.Texture0, UVTexture);

            vao = VertexArrayObject.New(GraphicsDevice, mesh.Draw.IndexBuffer, mesh.Draw.VertexBuffers);

            myDraws    = new DrawOptions[3];
            myDraws[0] = new DrawOptions {
                Sampler = GraphicsDevice.SamplerStates.LinearClamp, Transform = Matrix.Multiply(Matrix.Scaling(0.4f), Matrix.Translation(-0.5f, 0.5f, 0f))
            };
            myDraws[1] = new DrawOptions {
                Sampler = GraphicsDevice.SamplerStates.LinearWrap, Transform = Matrix.Multiply(Matrix.Scaling(0.4f), Matrix.Translation(0.5f, 0.5f, 0f))
            };
            myDraws[2] = new DrawOptions {
                Sampler = SamplerState.New(GraphicsDevice, new SamplerStateDescription(TextureFilter.Linear, TextureAddressMode.Mirror)), Transform = Matrix.Multiply(Matrix.Scaling(0.4f), Matrix.Translation(0.5f, -0.5f, 0f))
            };
            //var borderDescription = new SamplerStateDescription(TextureFilter.Linear, TextureAddressMode.Border) { BorderColor = Color.Purple };
            //var border = SamplerState.New(GraphicsDevice, borderDescription);
            //myDraws[3] = new DrawOptions { Sampler = border, Transform = Matrix.Multiply(Matrix.Scale(0.3f), Matrix.Translation(-0.5f, -0.5f, 0f)) };
        }
        private void SetupMeshResources(EffectOld effect, EffectMesh effectMesh)
        {
            // Generates a quad for post effect rendering (should be utility function)
            var vertices = new[]
            {
                -1.0f, 1.0f,
                1.0f, 1.0f,
                -1.0f, -1.0f,
                1.0f, -1.0f,
            };

            // Use the quad for this effectMesh
            effectMesh.MeshData.Draw = new MeshDraw
            {
                DrawCount     = 4,
                PrimitiveType = PrimitiveType.TriangleStrip,
                VertexBuffers = new[]
                {
                    new VertexBufferBinding(Buffer.Vertex.New(GraphicsDevice, vertices), new VertexDeclaration(VertexElement.Position <Vector2>()), 4)
                }
            };
        }
Exemplo n.º 7
0
        void SetupMeshResources(EffectOld effect, EffectMesh effectMesh)
        {
            // Generates a quad for post effect rendering (should be utility function)
            var vertices = new[]
            {
                -1.0f, 1.0f,
                1.0f, 1.0f,
                -1.0f, -1.0f,
                1.0f, -1.0f,
            };

            // Use the quad for this effectMesh
            effectMesh.MeshData.Draw = new MeshDraw
            {
                DrawCount     = 4,
                PrimitiveType = PrimitiveType.TriangleStrip,
                VertexBuffers = new[]
                {
                    new VertexBufferBinding(Buffer.Vertex.New(GraphicsDevice, vertices), new VertexDeclaration(VertexElement.Position <Vector2>()), 4)
                }
            };

            // TODO: unbind render targets
            var previousRender = effectMesh.Render;

            effectMesh.Render += (threadContext) =>
            {
                // Setup render target
                var renderTarget = effectMesh.Parameters.Get(RenderTargetKeys.RenderTarget);
                var desc         = renderTarget.Description;
                threadContext.GraphicsDevice.SetViewport(new Viewport(0, 0, desc.Width, desc.Height));
                threadContext.GraphicsDevice.SetRenderTarget(renderTarget);

                // Draw
                previousRender.Invoke(threadContext);

                // Unbind RenderTargets
                threadContext.GraphicsDevice.UnsetRenderTargets();
            };
        }
Exemplo n.º 8
0
        public override void Load()
        {
            base.Load();

            slideShowEffect = this.EffectSystemOld.BuildEffect("SlideShow")
                              .Using(new StateShaderPlugin()
            {
                RenderPassPlugin = this, UseDepthStencilState = true
            })
                              .Using(new BasicShaderPlugin(new ShaderClassSource("PostEffectTransition"))
            {
                RenderPassPlugin = this
            })
                              .InstantiatePermutation();

            if (OfflineCompilation)
            {
                return;
            }

            RenderPass.StartPass += (context) =>
            {
                if (RenderPass.Enabled)
                {
                    // Setup the Viewport
                    context.GraphicsDevice.SetViewport(MainTargetPlugin.Viewport);

                    // Setup the depth stencil and main render target.
                    context.GraphicsDevice.SetRenderTarget(RenderTarget);
                }
            };

            RenderPass.EndPass += (context) =>
            {
                if (RenderPass.Enabled)
                {
                    context.GraphicsDevice.UnsetRenderTargets();
                }
            };

            // Generates a quad for post effect rendering (should be utility function)
            var vertices = new[]
            {
                -1.0f, 1.0f,
                1.0f, 1.0f,
                -1.0f, -1.0f,
                1.0f, -1.0f,
            };

            // Use the quad for this effectMesh
            var quadData = new Mesh();

            quadData.Draw = new MeshDraw
            {
                DrawCount     = 4,
                PrimitiveType = PrimitiveType.TriangleStrip,
                VertexBuffers = new[]
                {
                    new VertexBufferBinding(Buffer.Vertex.New(GraphicsDevice, vertices), new VertexDeclaration(VertexElement.Position <Vector2>()), 4)
                }
            };
            var textureMesh = new EffectMesh(slideShowEffect, quadData).KeepAliveBy(this);

            textureMesh.Parameters.Set(EffectPlugin.DepthStencilStateKey, GraphicsDevice.DepthStencilStates.None);

            textureMesh.Parameters.AddSources(this.Parameters);
            RenderSystem.GlobalMeshes.AddMesh(textureMesh);
        }
Exemplo n.º 9
0
        public override void Load()
        {
            base.Load();

            skyboxEffect = this.EffectSystemOld.BuildEffect("Skybox")
                           .Using(new StateShaderPlugin()
            {
                RenderPassPlugin = this, UseDepthStencilState = true
            })
                           .Using(new BasicShaderPlugin(
                                      new ShaderMixinSource()
            {
                Mixins = new List <ShaderClassSource>()
                {
                    new ShaderClassSource("SkyBox")
                },
                Compositions = new Dictionary <string, ShaderSource>()
                {
                    { "color", SkyBoxColor }
                }
            })
            {
                RenderPassPlugin = this
            })
                           .InstantiatePermutation();

            if (OfflineCompilation)
            {
                return;
            }

            Parameters.AddSources(MainPlugin.ViewParameters);

            var zBackgroundValue = MainTargetPlugin.ClearDepth;
            // Generates a quad for post effect rendering (should be utility function)
            var vertices = new[]
            {
                -1.0f, 1.0f, zBackgroundValue, 1.0f,
                1.0f, 1.0f, zBackgroundValue, 1.0f,
                -1.0f, -1.0f, zBackgroundValue, 1.0f,
                1.0f, -1.0f, zBackgroundValue, 1.0f,
            };

            Parameters.RegisterParameter(EffectPlugin.DepthStencilStateKey);
            Parameters.Set(TexturingKeys.Sampler, GraphicsDevice.SamplerStates.LinearWrap);

            // Use the quad for this effectMesh
            var quadData = new Mesh();

            quadData.Draw = new MeshDraw
            {
                DrawCount     = 4,
                PrimitiveType = PrimitiveType.TriangleStrip,
                VertexBuffers = new[]
                {
                    new VertexBufferBinding(Buffer.Vertex.New(GraphicsDevice, vertices), new VertexDeclaration(VertexElement.Position <Vector4>()), 4)
                }
            };

            RenderPass.StartPass += (context) =>
            {
                // Setup the Viewport
                context.GraphicsDevice.SetViewport(MainTargetPlugin.Viewport);

                // Setup the depth stencil and main render target.
                context.GraphicsDevice.SetRenderTarget(MainTargetPlugin.DepthStencil, MainTargetPlugin.RenderTarget);
            };

            RenderPass.EndPass += (context) => context.GraphicsDevice.UnsetRenderTargets();

            var skyboxMesh = new EffectMesh(skyboxEffect, quadData).KeepAliveBy(this);

            // If the main target plugin is not clearing anything, we assume that this is the job of the skybox plugin
            if (!MainTargetPlugin.EnableClearTarget && !MainTargetPlugin.EnableClearDepth)
            {
                var description = new DepthStencilStateDescription().Default();
                description.DepthBufferFunction = CompareFunction.Always;
                var alwaysWrite = DepthStencilState.New(GraphicsDevice, description);
                skyboxMesh.Parameters.Set(EffectPlugin.DepthStencilStateKey, alwaysWrite);
            }
            else
            {
                skyboxMesh.Parameters.Set(EffectPlugin.DepthStencilStateKey, MainTargetPlugin.DepthStencilState);
            }

            skyboxMesh.Parameters.AddSources(this.Parameters);
            RenderSystem.GlobalMeshes.AddMesh(skyboxMesh);
        }