Ejemplo n.º 1
0
    /// <summary>
    /// Compiles a D2D1 pixel shader with <c>D2D_FULL_SHADER</c>.
    /// </summary>
    /// <param name="source">The HLSL source code to compile.</param>
    /// <param name="shaderProfile">The shader profile to use to compile the shader.</param>
    /// <returns>The bytecode for the compiled shader.</returns>
    private static ComPtr <ID3DBlob> CompileD2DFullShader(ReadOnlySpan <char> source, D2D1ShaderProfile shaderProfile)
    {
        // Encode the HLSL source to ASCII
        int maxLength = Encoding.ASCII.GetMaxByteCount(source.Length);

        byte[] buffer = ArrayPool <byte> .Shared.Rent(maxLength);

        int writtenBytes = Encoding.ASCII.GetBytes(source, buffer);

        using ComPtr <ID3DBlob> d3DBlobBytecode = default;
        using ComPtr <ID3DBlob> d3DBlobErrors   = default;

        int hResult;

        fixed(byte *bufferPtr = buffer)
        {
            // Prepare the macros for full shader compilation:
            //
            // -D D2D_FULL_SHADER
            // -D D2D_ENTRY=Execute
            D3D_SHADER_MACRO *macros = stackalloc D3D_SHADER_MACRO[]
            {
                new()
                {
                    Name       = (sbyte *)Unsafe.AsPointer(ref MemoryMarshal.GetReference(ASCII.D2D_FULL_SHADER)),
                    Definition = (sbyte *)Unsafe.AsPointer(ref MemoryMarshal.GetReference(ASCII.NullTerminator))
                },
                new()
                {
                    Name       = (sbyte *)Unsafe.AsPointer(ref MemoryMarshal.GetReference(ASCII.D2D_ENTRY)),
                    Definition = (sbyte *)Unsafe.AsPointer(ref MemoryMarshal.GetReference(ASCII.Execute))
                },
                new()
            };

            // Compile the shader with -ps_5_0 -O3 -We -Zpr
            hResult = DirectX.D3DCompile(
                pSrcData: bufferPtr,
                SrcDataSize: (nuint)writtenBytes,
                pSourceName: null,
                pDefines: macros,
                pInclude: D3DIncludeForD2D1EffectHelpers,
                pEntrypoint: (sbyte *)Unsafe.AsPointer(ref MemoryMarshal.GetReference(ASCII.Execute)),
                pTarget: (sbyte *)Unsafe.AsPointer(ref MemoryMarshal.GetReference(ASCII.GetPixelShaderProfile(shaderProfile))),
                Flags1: D3DCOMPILE.D3DCOMPILE_OPTIMIZATION_LEVEL3 | D3DCOMPILE.D3DCOMPILE_WARNINGS_ARE_ERRORS | D3DCOMPILE.D3DCOMPILE_PACK_MATRIX_ROW_MAJOR,
                Flags2: 0,
                ppCode: d3DBlobBytecode.GetAddressOf(),
                ppErrorMsgs: d3DBlobErrors.GetAddressOf());
        }

        ArrayPool <byte> .Shared.Return(buffer);

        // Throw if an error was retrieved, then also double check the HRESULT
        if (d3DBlobErrors.Get() is not null)
        {
            ThrowHslsCompilationException(d3DBlobErrors.Get());
        }

        hResult.Assert();

        return(d3DBlobBytecode.Move());
    }