# GraphicsDevice

> Provides the main entry point for the AMD Module. Use this to interact with the FSR2, FSR3, and FSR4 features.

## Definition

* **Type:** Class
* **Namespace:** [UnityEngine.AMD](/engine/6000.7/script-reference/unityengine/amd.md)
* **Assembly:** UnityEngine.AMDModule

```csharp
public class GraphicsDevice
```

## Remarks

The `GraphicsDevice` includes an interface for creating and managing feature contexts, handling FSR2 command execution, and providing utility methods for quality mode resolution management.

`GraphicsDevice` is needed to implement FSR2, FSR3, and FSR4, outside of the built-in integration, for instance, FSR2 via the [HDRP Dynamic Resolution](https://docs.unity3d.com/Packages/com.unity.render-pipelines.high-definition@17.2/manual/Dynamic-Resolution.html). URP supports them via Upscaler Framework and HDRP supports FSR3 and FSR4 via the framework.

Before using `GraphicsDevice`, ensure the [AMDUnityPlugin](/engine/6000.7/script-reference/unityengine/amd/amdunityplugin.md) is loaded and the device is initialized via [GraphicsDevice.CreateGraphicsDevice](/engine/6000.7/script-reference/unityengine/amd/graphicsdevice/creategraphicsdevice.md).

Additional Resources: [AMDUnityPlugin](/engine/6000.7/script-reference/unityengine/amd/amdunityplugin.md), [FSR2Context](/engine/6000.7/script-reference/unityengine/amd/fsr2context.md), [FSR2TextureTable](/engine/6000.7/script-reference/unityengine/amd/fsr2texturetable.md), [FSR2CommandInitializationData](/engine/6000.7/script-reference/unityengine/amd/fsr2commandinitializationdata.md), [FSR2CommandExecutionData](/engine/6000.7/script-reference/unityengine/amd/fsr2commandexecutiondata.md), [FSRUpscalerCommandInitializationData](/engine/6000.7/script-reference/unityengine/amd/fsrupscalercommandinitializationdata.md)

## Examples

```csharp
using UnityEngine;
using UnityEngine.Rendering;
using UnityEngine.Rendering.HighDefinition;
using UnityEngine.AMD;

 // Example HDRP custom pass 
public class CustomFSRPass : CustomPass
{
    public static bool EnsureAMDPluginLoaded()
    {
        if (!AMDUnityPlugin.IsLoaded())
        {
            Debug.Log("AMDUnityPlugin is not loaded!");
            if (!AMDUnityPlugin.Load())
            {
                Debug.LogError("Unable to load AMDUnityPlugin");
                return false;
            }
        }
        Debug.Log("AMDUnityPlugin is successfully loaded!");
        return true;
    }

    void InitializeAMDDevice()
    {
        if (!EnsureAMDPluginLoaded())
            return;

        // AMDUnityPlugin initialization will handle device creation for us.
        // In case the device is not created, we call the static method GraphicsDevice.CreateGraphicsDevice().
        amdDevice = GraphicsDevice.device == null ? GraphicsDevice.CreateGraphicsDevice() : GraphicsDevice.device;

        Debug.LogFormat("AMD.GraphicsDevice initialized w/ version {0}", GraphicsDevice.version);
    }

    protected override void Setup(ScriptableRenderContext renderContext, CommandBuffer cmd)
    {
        if (amdDevice == null)
        {
            InitializeAMDDevice();
        }

        float scalingRatio = fsr2Context == null ? 1.0f : amdDevice.GetUpscaleRatioFromQualityMode(m_Quality);
        fsr2OutputColorBuffer = RTHandles.Alloc(
            new Vector2(scalingRatio, scalingRatio),
            dimension: TextureDimension.Tex2D,
            colorFormat: GraphicsFormat.R16G16B16A16_SFloat,
            name: "fsr2OutputColorBuffer",
            enableRandomWrite: true
        );

        // other pass setup code
    }

    protected override void Execute(CustomPassContext ctx)
    {
        bool initializeFsr2Context = fsr2Context == null || HasInputResolutionChanged(ctx) || HasOutputResolutionChanged(ctx);
        if (initializeFsr2Context)
        {
            if (fsr2Context != null)
            {
                amdDevice.DestroyFeature(ctx.cmd, fsr2Context);
                fsr2Context = null;
            }

            FSR2CommandInitializationData initData = new FSR2CommandInitializationData();
            // populate initData
            fsr2Context = amdDevice.CreateFeature(ctx.cmd, initData);
        }

        fsr2Context.executeData.enableSharpening = m_EnableSharpening ? 1 : 0;
        // populate rest of fsr2Context.executeData 

        FSR2TextureTable fsr2TextureTable = new FSR2TextureTable()
        {
            // populate texture table
        };

        amdDevice.ExecuteFSR2(ctx.cmd, fsr2Context, fsr2TextureTable);
    }

    protected override void Cleanup()
    {
        // pass cleanup code

        // No explicit clean up is necessary for AMD.GraphicsDevice, all handled internally
    }

    private GraphicsDevice amdDevice = null;
    private FSR2Context fsr2Context = null;
    private RTHandle fsr2OutputColorBuffer;
    // other member variables
}
```

## Static Properties

| Property                                                                             | Description                                                                                                                        |
| ------------------------------------------------------------------------------------ | ---------------------------------------------------------------------------------------------------------------------------------- |
| [device](/engine/6000.7/script-reference/unityengine/amd/graphicsdevice/device.md)   | Gets the device created by GraphicsDevice.CreateGraphicsDevice. If the device hasn't been created this property evaluates to null. |
| [version](/engine/6000.7/script-reference/unityengine/amd/graphicsdevice/version.md) | Gets the version that corresponds to the Unity host plugin that manages the AMD.AMDUnityPlugin official library.                   |

## Methods

| Method                                                                                                                                             | Description                                                                                                                                                     |
| -------------------------------------------------------------------------------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| [CreateDebugView](/engine/6000.7/script-reference/unityengine/amd/graphicsdevice/createdebugview.md)                                               | Allocates and returns a debug view. The view must be updated with UpdateDebugView() and deallocated with DeleteDebugView().                                     |
| [CreateFSRUpscale2](/engine/6000.7/script-reference/unityengine/amd/graphicsdevice/createfsrupscale2.md)                                           | Creates a FSR2 context.                                                                                                                                         |
| [CreateFSRUpscale3](/engine/6000.7/script-reference/unityengine/amd/graphicsdevice/createfsrupscale3.md)                                           | Creates a FSR3 context.                                                                                                                                         |
| [CreateFSRUpscale4](/engine/6000.7/script-reference/unityengine/amd/graphicsdevice/createfsrupscale4.md)                                           | Creates a FSR4 context.                                                                                                                                         |
| [DeleteDebugView](/engine/6000.7/script-reference/unityengine/amd/graphicsdevice/deletedebugview.md)                                               | Deletes a debug view.                                                                                                                                           |
| [DestroyFeature](/engine/6000.7/script-reference/unityengine/amd/graphicsdevice/destroyfeature.md)                                                 | Destroys a specific FSR2Context created with GraphicsDevice.CreateFSRUpscale2.                                                                                  |
| [ExecuteFSRUpscale2](/engine/6000.7/script-reference/unityengine/amd/graphicsdevice/executefsrupscale2.md)                                         | Records the execution of the FSR2 pass into a rendering command buffer. This call does not execute the command buffer, it only appends custom commands into it. |
| [ExecuteFSRUpscale3](/engine/6000.7/script-reference/unityengine/amd/graphicsdevice/executefsrupscale3.md)                                         | Records the execution of the FSR3 pass into a rendering command buffer. This call does not execute the command buffer, it only appends custom commands into it. |
| [ExecuteFSRUpscale4](/engine/6000.7/script-reference/unityengine/amd/graphicsdevice/executefsrupscale4.md)                                         | Records the execution of the FSR4 pass into a rendering command buffer. This call does not execute the command buffer, it only appends custom commands into it. |
| [GetRenderResolutionFromQualityMode](/engine/6000.7/script-reference/unityengine/amd/graphicsdevice/getrenderresolutionfromqualitymode.md)         | Queries the resolution configuration from a specified quality mode preset.                                                                                      |
| [GetRenderResolutionFromQualityModeFSR3](/engine/6000.7/script-reference/unityengine/amd/graphicsdevice/getrenderresolutionfromqualitymodefsr3.md) | Queries the resolution configuration from a specified quality mode preset.                                                                                      |
| [GetRenderResolutionFromQualityModeFSR4](/engine/6000.7/script-reference/unityengine/amd/graphicsdevice/getrenderresolutionfromqualitymodefsr4.md) | Queries the resolution configuration from a specified quality mode preset.                                                                                      |
| [GetUpscaleRatioFromQualityMode](/engine/6000.7/script-reference/unityengine/amd/graphicsdevice/getupscaleratiofromqualitymode.md)                 | Gets a precomputed upscaling ratio based on a preset quality setting.                                                                                           |
| [IsFeatureAvailable](/engine/6000.7/script-reference/unityengine/amd/graphicsdevice/isfeatureavailable.md)                                         | Checks if current platform supports given feature.                                                                                                              |
| [UpdateDebugView](/engine/6000.7/script-reference/unityengine/amd/graphicsdevice/updatedebugview.md)                                               | Updates a debug view.                                                                                                                                           |

## Static Methods

| Method                                                                                                         | Description                                                                  |
| -------------------------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------- |
| [CreateGraphicsDevice](/engine/6000.7/script-reference/unityengine/amd/graphicsdevice/creategraphicsdevice.md) | Creates the main API object. Call this method only once in your application. |
| [GetFeatureVersion](/engine/6000.7/script-reference/unityengine/amd/graphicsdevice/getfeatureversion.md)       | Gets the version of each feature.                                            |

## Classes

| Class                                                                                                                               | Description                                                                                                                  |
| ----------------------------------------------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------- |
| [GraphicsDevice.GraphicsDeviceDebugView](/engine/6000.7/script-reference/unityengine/amd/graphicsdevice/graphicsdevicedebugview.md) | Provides information to implement a debug view, including versions of the device and FidelityFX SDK, and available features. |

## Structs

| Struct                                                                                                                                    | Description                               |
| ----------------------------------------------------------------------------------------------------------------------------------------- | ----------------------------------------- |
| [GraphicsDevice.FSRUpscaleFeatureDebugInfo](/engine/6000.7/script-reference/unityengine/amd/graphicsdevice/fsrupscalefeaturedebuginfo.md) | Debug info for a single FSR feature slot. |
