Beispiel #1
0
 private static void ThrowIfFailed(int hr)
 {
     if (TerraFX.Interop.Windows.FAILED(hr))
     {
         ExceptionUtilities.ThrowExternalException("", hr);
     }
 }
Beispiel #2
0
        ValueList <GraphicsAdapter> GetAdapters(IDXGIFactory3 *dxgiFactory)
        {
            IDXGIAdapter1 *dxgiAdapter1 = null;

            var adapters = new ValueList <GraphicsAdapter>();
            var index    = 0u;

            do
            {
                var result = dxgiFactory->EnumAdapters1(index, &dxgiAdapter1);

                if (result.FAILED)
                {
                    if (result != DXGI_ERROR_NOT_FOUND)
                    {
                        ReleaseIfNotNull(dxgiAdapter1);
                        ExceptionUtilities.ThrowExternalException(nameof(IDXGIFactory1.EnumAdapters1), result);
                    }
                    index = 0;
                }
                else
                {
                    var adapter = new GraphicsAdapter(this, dxgiAdapter1);
                    adapters.Add(adapter);

                    index++;
                    dxgiAdapter1 = null;
                }
            }while (index != 0);

            return(adapters);
        }
Beispiel #3
0
 public static void ThrowIfFailed(int hr, [CallerMemberName] string name = null !)
 {
     if (Windows.FAILED(hr))
     {
         ExceptionUtilities.ThrowExternalException(name, hr);
     }
 }
 public static void ThrowExternalExceptionIfFailed(HRESULT value, [CallerArgumentExpression("value")] string?valueExpression = null)
 {
     if (value.FAILED)
     {
         AssertNotNull(valueExpression);
         ExceptionUtilities.ThrowExternalException(valueExpression, value);
     }
 }
 public static void ThrowIfFailed(int hr)
 {
     if (TerraFX.Interop.Windows.FAILED(hr))
     {
         // Set a breakpoint on this line to catch Win32 API errors.
         ExceptionUtilities.ThrowExternalException("External exception occured", hr);
     }
 }
 public static void ThrowIfFailed(string methodName, int hr)
 {
     if (TerraFX.Interop.Windows.FAILED(hr))
     {
         // Set a breakpoint on this line to catch Win32 API errors.
         ExceptionUtilities.ThrowExternalException(methodName, hr);
     }
 }
 public static void ThrowExternalExceptionStringInt32Test(
     [Values(null, "", "methodName")] string methodName,
     [Values(0, 1, unchecked ((int)(0x80000000)))] int errorCode
     )
 {
     Assert.That(() => ExceptionUtilities.ThrowExternalException(methodName, errorCode),
                 Throws.InstanceOf <ExternalException>()
                 .With.Property("ErrorCode").EqualTo(errorCode)
                 );
 }
Beispiel #8
0
    protected unsafe GraphicsShader CompileShader(GraphicsDevice graphicsDevice, GraphicsShaderKind kind, string shaderName, string entryPointName)
    {
        var assetName = $"{shaderName}{kind}.hlsl";

        fixed(char *assetPath = GetAssetFullPath("Shaders", shaderName, assetName))
        fixed(sbyte *entryPoint = entryPointName.GetUtf8Span())
        {
            var compileFlags = 0u;

            if (GraphicsService.EnableDebugMode)
            {
                // Enable better shader debugging with the graphics debugging tools.
                compileFlags |= D3DCOMPILE_DEBUG | D3DCOMPILE_SKIP_OPTIMIZATION;
            }
            else
            {
                compileFlags |= D3DCOMPILE_OPTIMIZATION_LEVEL3;
            }

            ID3DBlob *d3dShaderBlob      = null;
            ID3DBlob *d3dShaderErrorBlob = null;

            try
            {
                var result = D3DCompileFromFile((ushort *)assetPath, pDefines: null, D3D_COMPILE_STANDARD_FILE_INCLUDE, entryPoint, GetD3D12CompileTarget(kind).GetPointer(), compileFlags, Flags2: 0, &d3dShaderBlob, ppErrorMsgs: &d3dShaderErrorBlob);

                if (FAILED(result))
                {
                    // todo: var span = TerraFX.Utilities.InteropUtilities.MarshalUtf8ToReadOnlySpan((sbyte*)pError->GetBufferPointer(), (int)pError->GetBufferSize());
                    var errorMsg = System.Text.Encoding.UTF8.GetString((byte *)d3dShaderErrorBlob->GetBufferPointer(), (int)d3dShaderErrorBlob->GetBufferSize());
                    Console.WriteLine(errorMsg);
                    ExceptionUtilities.ThrowExternalException(nameof(D3DCompileFromFile), result);
                }

                var bytecode = new UnmanagedArray <byte>(d3dShaderBlob->GetBufferSize());
                new UnmanagedReadOnlySpan <byte>((byte *)d3dShaderBlob->GetBufferPointer(), bytecode.Length).CopyTo(bytecode);

                switch (kind)
                {
                case GraphicsShaderKind.Pixel:
                {
                    return(graphicsDevice.CreatePixelShader(bytecode, entryPointName));
                }

                case GraphicsShaderKind.Vertex:
                {
                    return(graphicsDevice.CreateVertexShader(bytecode, entryPointName));
                }

                default:
                {
                    ThrowForInvalidKind(kind);
                    return(null !);
                }
                }
            }
            finally
            {
                if (d3dShaderBlob != null)
                {
                    _ = d3dShaderBlob->Release();
                }

                if (d3dShaderErrorBlob != null)
                {
                    _ = d3dShaderErrorBlob->Release();
                }
            }
        }