Ejemplo n.º 1
0
 public unsafe void GetOutputBufferPrecision()
 {
     Assert.AreEqual(D2D1PixelShader.GetOutputBufferPrecision <ShaderWithMultipleInputs>(), D2D1BufferPrecision.Unknown);
     Assert.AreEqual(D2D1PixelShader.GetOutputBufferPrecision <OnlyBufferPrecisionShader>(), D2D1BufferPrecision.UInt16Normalized);
     Assert.AreEqual(D2D1PixelShader.GetOutputBufferPrecision <OnlyChannelDepthShader>(), D2D1BufferPrecision.Unknown);
     Assert.AreEqual(D2D1PixelShader.GetOutputBufferPrecision <CustomBufferOutputShader>(), D2D1BufferPrecision.UInt8NormalizedSrgb);
 }
Ejemplo n.º 2
0
 public unsafe void GetOutputBufferChannelDepth()
 {
     Assert.AreEqual(D2D1PixelShader.GetOutputBufferChannelDepth <ShaderWithMultipleInputs>(), D2D1ChannelDepth.Default);
     Assert.AreEqual(D2D1PixelShader.GetOutputBufferChannelDepth <OnlyBufferPrecisionShader>(), D2D1ChannelDepth.Default);
     Assert.AreEqual(D2D1PixelShader.GetOutputBufferChannelDepth <OnlyChannelDepthShader>(), D2D1ChannelDepth.Four);
     Assert.AreEqual(D2D1PixelShader.GetOutputBufferChannelDepth <CustomBufferOutputShader>(), D2D1ChannelDepth.One);
 }
Ejemplo n.º 3
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);
    }
Ejemplo n.º 4
0
    public unsafe void GetBytecode_FromEmbeddedBytecode_WithCompileOptions()
    {
        // Bytecode with no parameters
        ReadOnlyMemory <byte> bytecode = D2D1PixelShader.LoadBytecode <ShaderWithEmbeddedBytecodeAndCompileOptions>();

        Assert.IsTrue(MemoryMarshal.TryGetMemoryManager(bytecode, out MemoryManager <byte>?manager));
        Assert.AreEqual("PinnedBufferMemoryManager", manager !.GetType().Name);

        // Matching shader profile
        bytecode = D2D1PixelShader.LoadBytecode <ShaderWithEmbeddedBytecodeAndCompileOptions>(D2D1ShaderProfile.PixelShader40Level91);

        Assert.IsTrue(MemoryMarshal.TryGetMemoryManager(bytecode, out manager));
        Assert.AreEqual("PinnedBufferMemoryManager", manager !.GetType().Name);

        // Matching compile options
        bytecode = D2D1PixelShader.LoadBytecode <ShaderWithEmbeddedBytecodeAndCompileOptions>(D2D1CompileOptions.IeeeStrictness | D2D1CompileOptions.OptimizationLevel2);

        Assert.IsTrue(MemoryMarshal.TryGetMemoryManager(bytecode, out manager));
        Assert.AreEqual("PinnedBufferMemoryManager", manager !.GetType().Name);

        // Matching shader profile and compile options
        bytecode = D2D1PixelShader.LoadBytecode <ShaderWithEmbeddedBytecodeAndCompileOptions>(D2D1ShaderProfile.PixelShader40Level91, D2D1CompileOptions.IeeeStrictness | D2D1CompileOptions.OptimizationLevel2);

        Assert.IsTrue(MemoryMarshal.TryGetMemoryManager(bytecode, out manager));
        Assert.AreEqual("PinnedBufferMemoryManager", manager !.GetType().Name);
    }
Ejemplo n.º 5
0
 public unsafe void GetInputCount()
 {
     Assert.AreEqual(D2D1PixelShader.GetInputCount <InvertEffect>(), 1);
     Assert.AreEqual(D2D1PixelShader.GetInputCount <PixelateEffect.Shader>(), 1);
     Assert.AreEqual(D2D1PixelShader.GetInputCount <ZonePlateEffect>(), 0);
     Assert.AreEqual(D2D1PixelShader.GetInputCount <ShaderWithMultipleInputs>(), 7);
 }
Ejemplo n.º 6
0
 public unsafe void GetInputType()
 {
     Assert.AreEqual(D2D1PixelShader.GetInputType <ShaderWithMultipleInputs>(0), D2D1PixelShaderInputType.Simple);
     Assert.AreEqual(D2D1PixelShader.GetInputType <ShaderWithMultipleInputs>(1), D2D1PixelShaderInputType.Complex);
     Assert.AreEqual(D2D1PixelShader.GetInputType <ShaderWithMultipleInputs>(2), D2D1PixelShaderInputType.Simple);
     Assert.AreEqual(D2D1PixelShader.GetInputType <ShaderWithMultipleInputs>(3), D2D1PixelShaderInputType.Complex);
     Assert.AreEqual(D2D1PixelShader.GetInputType <ShaderWithMultipleInputs>(4), D2D1PixelShaderInputType.Complex);
     Assert.AreEqual(D2D1PixelShader.GetInputType <ShaderWithMultipleInputs>(5), D2D1PixelShaderInputType.Complex);
 }
Ejemplo n.º 7
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);
    }
Ejemplo n.º 8
0
    public unsafe void ShaderWithAssemblyLevelAttributesAndOverriddenOptions()
    {
        ReadOnlyMemory <byte> bytecode = D2D1PixelShader.LoadBytecode <ShaderWithOverriddenOptions>();

        Assert.IsTrue(MemoryMarshal.TryGetMemoryManager(bytecode, out MemoryManager <byte>?manager));
        Assert.AreEqual("PinnedBufferMemoryManager", manager !.GetType().Name);

        D2D1PixelShader.LoadBytecode <ShaderWithOverriddenOptions>(
            shaderProfile: D2D1ShaderProfile.PixelShader41,
            options: D2D1CompileOptions.Debug | D2D1CompileOptions.AvoidFlowControl | D2D1CompileOptions.PartialPrecision);

        Assert.IsTrue(MemoryMarshal.TryGetMemoryManager(bytecode, out manager));
        Assert.AreEqual("PinnedBufferMemoryManager", manager !.GetType().Name);
    }
Ejemplo n.º 9
0
    public unsafe void ShaderWithAssemblyLevelAttributesAndOverriddenProfile()
    {
        ReadOnlyMemory <byte> bytecode = D2D1PixelShader.LoadBytecode <ShaderWithOverriddenProfile>();

        Assert.IsTrue(MemoryMarshal.TryGetMemoryManager(bytecode, out MemoryManager <byte>?manager));
        Assert.AreEqual("PinnedBufferMemoryManager", manager !.GetType().Name);

        D2D1PixelShader.LoadBytecode <ShaderWithOverriddenProfile>(
            shaderProfile: D2D1ShaderProfile.PixelShader50,
            options: D2D1CompileOptions.IeeeStrictness | D2D1CompileOptions.OptimizationLevel2 | D2D1CompileOptions.PartialPrecision);

        Assert.IsTrue(MemoryMarshal.TryGetMemoryManager(bytecode, out manager));
        Assert.AreEqual("PinnedBufferMemoryManager", manager !.GetType().Name);
    }
Ejemplo n.º 10
0
    public unsafe void ShaderWithAssemblyLevelAttributes()
    {
        ReadOnlyMemory <byte> bytecode = D2D1PixelShader.LoadBytecode <ShaderWithNoCompileAttributes>();

        // Verify the shader was precompiled
        Assert.IsTrue(MemoryMarshal.TryGetMemoryManager(bytecode, out MemoryManager <byte>?manager));
        Assert.AreEqual("PinnedBufferMemoryManager", manager !.GetType().Name);

        D2D1PixelShader.LoadBytecode <ShaderWithNoCompileAttributes>(
            shaderProfile: D2D1ShaderProfile.PixelShader41,
            options: D2D1CompileOptions.IeeeStrictness | D2D1CompileOptions.OptimizationLevel2 | D2D1CompileOptions.PartialPrecision);

        // Verify the expected options were used
        Assert.IsTrue(MemoryMarshal.TryGetMemoryManager(bytecode, out manager));
        Assert.AreEqual("PinnedBufferMemoryManager", manager !.GetType().Name);
    }
Ejemplo n.º 11
0
 public unsafe void GetBytecode_FromEmbeddedBytecode_WithTargetProfileAndPackMatrixColumnMajor()
 {
     _ = D2D1PixelShader.LoadBytecode <ShaderWithEmbeddedBytecode>(D2D1ShaderProfile.PixelShader40Level91, D2D1CompileOptions.Default | D2D1CompileOptions.PackMatrixColumnMajor);
 }
Ejemplo n.º 12
0
 public unsafe void GetBytecode_FromEmbeddedBytecode_WithPackMatrixColumnMajor()
 {
     _ = D2D1PixelShader.LoadBytecode <ShaderWithEmbeddedBytecode>(D2D1CompileOptions.Default | D2D1CompileOptions.PackMatrixColumnMajor);
 }
Ejemplo n.º 13
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;
                }
            }
        }