Exemple #1
0
        private static InputLayout CreateLayout(Device device, byte[] shaderBytecode, MaterialVertexInput[] vertexInputs)
        {
            int count   = vertexInputs.Sum(mvi => mvi.Elements.Length);
            int element = 0;

            InputElement[]  elements       = new InputElement[count];
            SemanticIndices indicesTracker = new SemanticIndices();

            for (int vbSlot = 0; vbSlot < vertexInputs.Length; vbSlot++)
            {
                MaterialVertexInput bufferInput = vertexInputs[vbSlot];
                int numElements   = bufferInput.Elements.Length;
                int currentOffset = 0;
                for (int i = 0; i < numElements; i++)
                {
                    var genericElement = bufferInput.Elements[i];
                    elements[element] = new InputElement(
                        GetSemanticName(genericElement.SemanticType),
                        indicesTracker.GetAndIncrement(genericElement.SemanticType),
                        ConvertGenericFormat(genericElement.ElementFormat),
                        currentOffset,
                        vbSlot,
                        D3DFormats.ConvertInputClass(genericElement.StorageClassifier),
                        genericElement.InstanceStepRate);
                    currentOffset += genericElement.SizeInBytes;
                    element       += 1;
                }
            }

            return(new InputLayout(device, shaderBytecode, elements));
        }
            public static int GetAndIncrement(ref SemanticIndices si, VertexElementSemantic type)
            {
                switch (type)
                {
                case VertexElementSemantic.Position:
                    return(si._position++);

                case VertexElementSemantic.TextureCoordinate:
                    return(si._texCoord++);

                case VertexElementSemantic.Normal:
                    return(si._normal++);

                case VertexElementSemantic.Color:
                    return(si._color++);

                default:
                    throw Illegal.Value <VertexElementSemantic>();
                }
            }
        private InputLayout CreateNewInputLayout(VertexLayoutDescription[] vertexLayouts, byte[] vsBytecode)
        {
            int totalCount = 0;

            for (int i = 0; i < vertexLayouts.Length; i++)
            {
                totalCount += vertexLayouts[i].Elements.Length;
            }

            int element = 0; // Total element index across slots.

            InputElement[]  elements = new InputElement[totalCount];
            SemanticIndices si       = new SemanticIndices();

            for (int slot = 0; slot < vertexLayouts.Length; slot++)
            {
                VertexElementDescription[] elementDescs = vertexLayouts[slot].Elements;
                uint stepRate      = vertexLayouts[slot].InstanceStepRate;
                int  currentOffset = 0;
                for (int i = 0; i < elementDescs.Length; i++)
                {
                    VertexElementDescription desc = elementDescs[i];
                    elements[element] = new InputElement(
                        GetSemanticString(desc.Semantic),
                        SemanticIndices.GetAndIncrement(ref si, desc.Semantic),
                        D3D11Formats.ToDxgiFormat(desc.Format),
                        currentOffset,
                        slot,
                        stepRate == 0 ? InputClassification.PerVertexData : InputClassification.PerInstanceData,
                        (int)stepRate);

                    currentOffset += (int)FormatHelpers.GetSizeInBytes(desc.Format);
                    element       += 1;
                }
            }

            return(new InputLayout(_device, vsBytecode, elements));
        }