Debugging DirectX 11 shaders with Visual Studio

1649 篇文章 11 订阅
1623 篇文章 22 订阅

In Visual Studio 2012, Microsoft has indroduced Graphics Debugger, you can use it to capture a frame from platforms like Unity Editor, Windows Standalone or Windows Store Apps.

It’s recommended to use Visual Studio 2013, as it contains several fixes for Graphics Debugger.

Note: Unity Editor contains multiple child windows inside, this may cause Graphics Debugger to capture frame from incorrect window, to ensure that correct window will be captured, check ‘Maximize on Play’ tab, and hit Play button before capturing, but even then there’s no guarantee that the correct window will be captured, that’s why it’s not recommended to use Unity Editor for frame capturing.

Note: Frames can only be captured if Unity is running under DirectX 11, you can select DirectX 11 from Player Settings (PC, Mac & LinuxStandalone) -> Other Settings.

Steps to capture frame from Unity Editor or Windows Standalone:

  • Launch Visual Studio 2013
  • Go to File->New->Project->Visual C++->Empty Project
  • Go to Project->Properties->Configuration Properties->Debugging
  • In Command field, replace $(TargetPath) with path to Unity Editor or Windows Standalone, for ex., C:\MyApp\MyApp.exe
  • (Optional) In Command Arguments specify -force-d3d11, this will force Windows Standalone or Unity Editor to run under DirectX 11
  • Project Properties
  • You’re ready to run Graphics Debugger, go to Debug->Graphics->Start Diagnostics
  • If everything is configured correctly you should see “Use ‘Print Screen’ key to capture a frame” message in top left corner of the application. See screenshot below.
  • Screenshot from Windows Standalone application running with Graphics Debugger

Steps to debug DirectX 11 shader:

  • To debug a shader you have to compile with debug symbols, to do that, you have insert #pragma enable_d3d11_debug_symbols
  • Let’s try to create a basic example:
    • Create a new Unity project
    • Create a new shader in Assets window
    • Insert #pragma enable_d3d11_debug_symbols
    • Create a new material and select your custom shader
    • Assign a material to some object, for ex., a cube
    • Build Windows Standalone application
    • Capture a frame with steps mentioned above

Your shader should look like this:

Shader "Custom/NewShader" {
    Properties {
        _MainTex ("Base (RGB)", 2D) = "white" {}
    }
    SubShader {
        Tags { "RenderType"="Opaque" }
        LOD 200
        
        CGPROGRAM
        #pragma surface surf Lambert
        #pragma enable_d3d11_debug_symbols
        sampler2D _MainTex;

        struct Input {
            float2 uv_MainTex;
        };

        void surf (Input IN, inout SurfaceOutput o) {
            half4 c = tex2D (_MainTex, IN.uv_MainTex);
            o.Albedo = c.rgb;
            o.Alpha = c.a;
        }
        ENDCG
    } 
    FallBack "Diffuse"
}

If everything done correctly, you should see the following captured frame, right click and select Pixel History and select the pixel of an object which has your custom shader assigned.

Captured frameCaptured frame

Click the play button next to the Vertex Shader (circled in red in screenshot above), you should be able to debug vertex shader.

Debugging vertex shaderDebugging vertex shader

Windows Store Apps

For Windows Store Apps, it’s a bit easier, because you don’t need to create a dummy Visual Studio project, the project is already created by Unity.

Steps for capturing the frame and shader debugging are the same like for Unity Editor or Windows Standalone.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值