Example #1
0
        private static List <MgVertexInputAttributeDescription> ExtractAttributeDescriptions(MgOptimizedStorageContainer container, List <GltfMeshAccessor> accessors)
        {
            const MgBufferUsageFlagBits VERT_MASK = MgBufferUsageFlagBits.VERTEX_BUFFER_BIT;
            var inputAttributes = new List <MgVertexInputAttributeDescription>();

            for (var i = 0; i < accessors.Count; i += 1)
            {
                var srcAttribute = accessors[i];
                var dstAttribute = container.Map.Allocations[i];

                if ((dstAttribute.Usage & VERT_MASK) == VERT_MASK)
                {
                    var attribute = new MgVertexInputAttributeDescription
                    {
                        Location = srcAttribute.LocationIndex,
                        Binding  = dstAttribute.BlockIndex,
                        Format   = srcAttribute.Format,
                        Offset   = (uint)dstAttribute.Offset,
                    };

                    inputAttributes.Add(attribute);
                }
            }

            return(inputAttributes);
        }
Example #2
0
        private static void IncludePerInstanceAttributes(List <MgVertexInputAttributeDescription> attributes)
        {
            {
                var attribute = new MgVertexInputAttributeDescription
                {
                    Binding  = 1U,
                    Format   = MgFormat.R32G32B32_SFLOAT,
                    Location = 11,
                    Offset   = (uint)Marshal.OffsetOf <PerInstance>("Translation"),
                };

                attributes.Add(attribute);
            }

            {
                var attribute = new MgVertexInputAttributeDescription
                {
                    Binding  = 1U,
                    Format   = MgFormat.R32G32B32_SFLOAT,
                    Location = 12,
                    Offset   = (uint)Marshal.OffsetOf(typeof(PerInstance), "Scale"),
                };

                attributes.Add(attribute);
            }

            {
                var attribute = new MgVertexInputAttributeDescription
                {
                    Binding  = 1U,
                    Format   = MgFormat.R32G32B32A32_SFLOAT,
                    Location = 13,
                    Offset   = (uint)Marshal.OffsetOf(typeof(PerInstance), "Rotation"),
                };

                attributes.Add(attribute);
            }

            {
                var attribute = new MgVertexInputAttributeDescription
                {
                    Binding  = 1U,
                    Format   = MgFormat.R32_SINT,
                    Location = 14,
                    Offset   = (uint)Marshal.OffsetOf(typeof(PerInstance), "CameraIndex"),
                };

                attributes.Add(attribute);
            }

            {
                var attribute = new MgVertexInputAttributeDescription
                {
                    Binding  = 1U,
                    Format   = MgFormat.R32_SINT,
                    Location = 15,
                    Offset   = (uint)Marshal.OffsetOf(typeof(PerInstance), "MaterialIndex"),
                };

                attributes.Add(attribute);
            }
        }
Example #3
0
        void InitPipeline()
        {
            MgOptimizedMesh mesh = null;

            // LOOK LIKE IT IS WILL BE EASIER TO GET THIS INFO FROM GLTF MODEL FILE
            var attributes   = new List <MgVertexInputAttributeDescription>();
            var firstBinding = 0U;

            foreach (var attr in mesh.Allocations)
            {
                var elem = new MgVertexInputAttributeDescription
                {
                    Location = attr.Index,         // FIXED
                    Binding  = firstBinding + attr.BlockIndex,
                    Format   = MgFormat.UNDEFINED, // NEED
                    Offset   = (uint)attr.ByteOffset,
                };
                attributes.Add(elem);
            }

            var bindings = new List <MgVertexInputBindingDescription>();

            for (var i = 0U; i < mesh.Instances.Length; i += 1)
            {
                var current = mesh.Instances[i];

                var stride = 0U;
                foreach (var attrIndex in current.PackingOrder)
                {
                    var attr = mesh.Allocations[attrIndex];
                    // BUFFERVIEW IS A BETTER MATCH
                    if ((attr.Usage & MgBufferUsageFlagBits.VERTEX_BUFFER_BIT) == MgBufferUsageFlagBits.VERTEX_BUFFER_BIT)
                    {
                        // ELEMENT COUNT
                        stride += 0U;
                    }
                }

                var elem = new MgVertexInputBindingDescription
                {
                    Binding   = firstBinding + i,
                    InputRate = MgVertexInputRate.VERTEX,
                    Stride    = stride,
                };
                bindings.Add(elem);
            }

            IMgDevice device = null;

            IMgPipeline[] pPipelines;
            var           pCreateInfos = new[] {
                new MgGraphicsPipelineCreateInfo {
                    InputAssemblyState = new MgPipelineInputAssemblyStateCreateInfo
                    {
                        Topology = MgPrimitiveTopology.TRIANGLE_LIST,
                    },
                    VertexInputState = new MgPipelineVertexInputStateCreateInfo
                    {
                        VertexBindingDescriptions   = bindings.ToArray(),
                        VertexAttributeDescriptions = attributes.ToArray(),
                    }
                }
            };

            device.CreateGraphicsPipelines(null, pCreateInfos, null, out pPipelines);
        }
Example #4
0
        private MgPipelineVertexInputStateCreateInfo GenerateVertexInputState(PerVertexDefinition definition)
        {
            PerVertexStrideInfo[] strides = new PerVertexStrideInfo[]
            {
                GetPositionStride(definition.Position),    // 0
                GetNormalStride(definition.Normal),        // 1
                GetTangentStride(definition.Tangent),      // 2
                GetTexCoordsStride(definition.TexCoords0), // 3
                GetTexCoordsStride(definition.TexCoords1), // 4
                GetColorStride(definition.Color0),         // 5
                GetJointsStride(definition.Joints0),       // 6
                GetWeightsStride(definition.Weights0),     // 7
                GetColorStride(definition.Color1),         // 8
                GetJointsStride(definition.Joints1),       // 9
                GetWeightsStride(definition.Weights1),     // 10
            };

            var attributes      = new List <MgVertexInputAttributeDescription>();
            var perVertexStride = 0U;
            var location        = 0U;

            foreach (var stride in strides)
            {
                if (stride.Range > 0)
                {
                    var attribute = new MgVertexInputAttributeDescription
                    {
                        Binding  = 0U,
                        Format   = stride.Format,
                        Location = location,
                        Offset   = perVertexStride,
                    };

                    location += 1;

                    attributes.Add(attribute);
                }
                perVertexStride += stride.Range;
            }

            var perInstanceStride = (uint)Marshal.SizeOf(typeof(PerInstance));

            IncludePerInstanceAttributes(attributes);

            return(new MgPipelineVertexInputStateCreateInfo
            {
                VertexBindingDescriptions = new[]
                {
                    new MgVertexInputBindingDescription
                    {
                        Binding = 0U,
                        InputRate = MgVertexInputRate.VERTEX,
                        Stride = perVertexStride,
                    },
                    new MgVertexInputBindingDescription
                    {
                        Binding = 1U,
                        InputRate = MgVertexInputRate.INSTANCE,
                        Stride = perInstanceStride,
                    },
                },
                VertexAttributeDescriptions = attributes.ToArray(),
            });
        }