Example #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;
        }
Example #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()));
        }
Example #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));
            }
        }
Example #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(),
     });
Example #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)) };
        }
Example #6
0
        private ModelData.MeshPart Process(ModelData.Mesh mesh, Mesh assimpMesh)
        {
            var meshPart = new ModelData.MeshPart
            {
                PrimitiveTopology = PrimitiveTopology.TriangleList,
                VertexBufferRange = new ModelData.BufferRange {
                    Slot = mesh.VertexBuffers.Count
                },
                IndexBufferRange = new ModelData.BufferRange {
                    Slot = mesh.IndexBuffers.Count
                }
            };

            var vertexBuffer = new ModelData.VertexBuffer
            {
                Layout = new List <VertexElement>()
            };

            mesh.VertexBuffers.Add(vertexBuffer);

            var indexBuffer = new ModelData.IndexBuffer();

            mesh.IndexBuffers.Add(indexBuffer);

            var layout = vertexBuffer.Layout;

            int vertexBufferElementSize = 0;

            // Add position
            layout.Add(VertexElement.PositionTransformed(Format.R32G32B32_Float, 0));
            vertexBufferElementSize += SharpDX.Utilities.SizeOf <Vector3>();

            // Add normals
            if (assimpMesh.HasNormals)
            {
                layout.Add(VertexElement.Normal(0, Format.R32G32B32_Float, vertexBufferElementSize));
                vertexBufferElementSize += SharpDX.Utilities.SizeOf <Vector3>();
            }

            // Add colors
            if (assimpMesh.VertexColorChannelCount > 0)
            {
                for (int localIndex = 0, i = 0; i < assimpMesh.VertexColorChannelCount; i++)
                {
                    if (assimpMesh.HasVertexColors(i))
                    {
                        layout.Add(VertexElement.Normal(localIndex, Format.R32G32B32A32_Float, vertexBufferElementSize));
                        vertexBufferElementSize += SharpDX.Utilities.SizeOf <Color4>();
                        localIndex++;
                    }
                }
            }

            // Add textures
            if (assimpMesh.TextureCoordsChannelCount > 0)
            {
                for (int localIndex = 0, i = 0; i < assimpMesh.TextureCoordsChannelCount; i++)
                {
                    if (assimpMesh.HasTextureCoords(i))
                    {
                        var uvCount = assimpMesh.GetUVComponentCount(i);

                        if (uvCount == 2)
                        {
                            layout.Add(VertexElement.TextureCoordinate(localIndex, Format.R32G32_Float, vertexBufferElementSize));
                            vertexBufferElementSize += SharpDX.Utilities.SizeOf <Vector2>();
                        }
                        else if (uvCount == 3)
                        {
                            layout.Add(VertexElement.TextureCoordinate(localIndex, Format.R32G32B32_Float, vertexBufferElementSize));
                            vertexBufferElementSize += SharpDX.Utilities.SizeOf <Vector3>();
                        }
                        else
                        {
                            throw new InvalidOperationException("Unexpected uv count");
                        }

                        localIndex++;
                    }
                }
            }

            if (options.ModelOperations.HasFlag(ModelOperation.CalculateBarycentricCoordinates))
            {
                layout.Add(new VertexElement("BARYCENTRIC", 0, Format.R32G32B32_Float, vertexBufferElementSize));
                vertexBufferElementSize += SharpDX.Utilities.SizeOf <Vector3>();
            }
            else
            // Add tangent / bitangent
            if (assimpMesh.HasTangentBasis)
            {
                if (!options.ExcludeElements.Contains("Tangent"))
                {
                    layout.Add(VertexElement.Tangent(Format.R32G32B32A32_Float, vertexBufferElementSize));
                    vertexBufferElementSize += SharpDX.Utilities.SizeOf <Vector4>();
                }

                if (!options.ExcludeElements.Contains("BiTangent"))
                {
                    layout.Add(VertexElement.BiTangent(Format.R32G32B32_Float, vertexBufferElementSize));
                    vertexBufferElementSize += SharpDX.Utilities.SizeOf <Vector3>();
                }
            }

            if (options.ModelOperations.HasFlag(ModelOperation.CalculateBarycentricCoordinates))
            {
                WriteBarycentricVertices(assimpMesh, meshPart, vertexBuffer, vertexBufferElementSize);
            }
            else
            {
                WriteVertices(assimpMesh, meshPart, vertexBuffer, vertexBufferElementSize);
            }
            WriteIndices(assimpMesh, meshPart, indexBuffer);
            return(meshPart);
        }