Пример #1
0
    public unsafe void GetInputDescriptions_Custom()
    {
        ReadOnlyMemory <D2D1InputDescription> inputDescriptions = D2D1PixelShader.GetInputDescriptions <ShaderWithInputDescriptions>();

        Assert.AreEqual(inputDescriptions.Length, 5);

        ReadOnlySpan <D2D1InputDescription> span = inputDescriptions.Span;

        Assert.AreEqual(span[0].Index, 0);
        Assert.AreEqual(span[0].Filter, D2D1Filter.MinPointMagLinearMipPoint);
        Assert.AreEqual(span[0].LevelOfDetailCount, 0);

        Assert.AreEqual(span[1].Index, 1);
        Assert.AreEqual(span[1].Filter, D2D1Filter.Anisotropic);
        Assert.AreEqual(span[1].LevelOfDetailCount, 0);

        Assert.AreEqual(span[2].Index, 2);
        Assert.AreEqual(span[2].Filter, D2D1Filter.MinLinearMagPointMinLinear);
        Assert.AreEqual(span[2].LevelOfDetailCount, 4);

        Assert.AreEqual(span[3].Index, 5);
        Assert.AreEqual(span[3].Filter, D2D1Filter.MinMagPointMipLinear);
        Assert.AreEqual(span[3].LevelOfDetailCount, 0);

        Assert.AreEqual(span[4].Index, 6);
        Assert.AreEqual(span[4].Filter, D2D1Filter.MinPointMagMipLinear);
        Assert.AreEqual(span[4].LevelOfDetailCount, 3);
    }
Пример #2
0
    public unsafe void GetInputDescriptions_Empty()
    {
        Assert.AreEqual(D2D1PixelShader.GetInputDescriptions <CheckerboardClipEffect>().Length, 0);
        Assert.AreEqual(D2D1PixelShader.GetInputDescriptions <InvertEffect>().Length, 0);
        Assert.AreEqual(D2D1PixelShader.GetInputDescriptions <InvertWithThresholdEffect>().Length, 0);
        Assert.AreEqual(D2D1PixelShader.GetInputDescriptions <PixelateEffect.Shader>().Length, 0);
        Assert.AreEqual(D2D1PixelShader.GetInputDescriptions <ZonePlateEffect>().Length, 0);

        Assert.AreEqual(D2D1PixelShader.GetInputDescriptions <ShaderWithMultipleInputs>().Length, 0);
        Assert.AreEqual(D2D1PixelShader.GetInputDescriptions <EmptyShader>().Length, 0);
        Assert.AreEqual(D2D1PixelShader.GetInputDescriptions <OnlyBufferPrecisionShader>().Length, 0);
        Assert.AreEqual(D2D1PixelShader.GetInputDescriptions <OnlyChannelDepthShader>().Length, 0);
        Assert.AreEqual(D2D1PixelShader.GetInputDescriptions <CustomBufferOutputShader>().Length, 0);
    }
Пример #3
0
        /// <summary>
        /// Initializes the <see cref="For{T}"/> shared state.
        /// </summary>
        /// <param name="d2D1DrawTransformMapperFactory">The factory of <see cref="ID2D1TransformMapper{T}"/> instances to use for each created effect.</param>
        /// <exception cref="InvalidOperationException">Thrown if initialization is attempted with a mismatched transform factory.</exception>
        public static void Initialize(Func <ID2D1TransformMapper <T> >?d2D1DrawTransformMapperFactory)
        {
            // This conceptually acts as a static constructor, and this type is
            // internal, so in this very specific case locking on the type is fine.
            lock (typeof(For <T>))
            {
                if (isInitialized)
                {
                    // If the factory is already initialized, ensure the draw transform mapper is the same
                    if (For <T> .d2D1DrawTransformMapperFactory != d2D1DrawTransformMapperFactory)
                    {
                        ThrowHelper.ThrowInvalidOperationException(
                            "Cannot initialize an ID2D1Effect factory for the same shader type with two different transform mappings. " +
                            "Make sure to only ever register a pixel shader effect with either no transform, or the same transform type.");
                    }
                }
                else
                {
                    // Load all shader properties
                    Guid shaderId = typeof(T).GUID;
                    ReadOnlyMemory <byte> bytecodeInfo = D2D1PixelShader.LoadBytecode <T>();
                    int   bytecodeSize = bytecodeInfo.Length;
                    byte *bytecode     = (byte *)RuntimeHelpers.AllocateTypeAssociatedMemory(typeof(For <T>), bytecodeSize);
                    D2D1BufferPrecision bufferPrecision = D2D1PixelShader.GetOutputBufferPrecision <T>();
                    D2D1ChannelDepth    channelDepth    = D2D1PixelShader.GetOutputBufferChannelDepth <T>();
                    D2D1PixelOptions    pixelOptions    = D2D1PixelShader.GetPixelOptions <T>();

                    // Prepare the inputs info
                    int inputCount = D2D1PixelShader.GetInputCount <T>();
                    D2D1PixelShaderInputType *inputTypes = (D2D1PixelShaderInputType *)RuntimeHelpers.AllocateTypeAssociatedMemory(typeof(For <T>), sizeof(D2D1PixelShaderInputType) * inputCount);

                    for (int i = 0; i < inputCount; i++)
                    {
                        inputTypes[i] = D2D1PixelShader.GetInputType <T>(i);
                    }

                    // Prepare the input descriptions
                    ReadOnlyMemory <D2D1InputDescription> inputDescriptionsInfo = D2D1PixelShader.GetInputDescriptions <T>();
                    int inputDescriptionCount = inputDescriptionsInfo.Length;
                    D2D1InputDescription *inputDescriptions = (D2D1InputDescription *)RuntimeHelpers.AllocateTypeAssociatedMemory(typeof(For <T>), sizeof(D2D1InputDescription) * inputDescriptionCount);

                    inputDescriptionsInfo.Span.CopyTo(new Span <D2D1InputDescription>(inputDescriptions, inputDescriptionCount));

                    // Copy the bytecode to the target buffer
                    bytecodeInfo.Span.CopyTo(new Span <byte>(bytecode, bytecodeSize));

                    // Set the shared state and mark the type as initialized
                    For <T> .shaderId                       = shaderId;
                    For <T> .inputCount                     = inputCount;
                    For <T> .inputTypes                     = inputTypes;
                    For <T> .inputDescriptionCount          = inputDescriptionCount;
                    For <T> .inputDescriptions              = inputDescriptions;
                    For <T> .pixelOptions                   = pixelOptions;
                    For <T> .bytecode                       = bytecode;
                    For <T> .bytecodeSize                   = bytecodeSize;
                    For <T> .bufferPrecision                = bufferPrecision;
                    For <T> .channelDepth                   = channelDepth;
                    For <T> .d2D1DrawTransformMapperFactory = d2D1DrawTransformMapperFactory;

                    isInitialized = true;
                }
            }
        }