1 of 58

Tightening Up the Graphics: Tools and Techniques for Debugging and Optimization

Elizabeth Baumel

2 of 58

Overview

  • Computer graphics AND YOU
  • Common problems
  • What those problems might be and what to check for
  • Time to break out the TOOLS
  • Debugging with Renderdoc
  • Performance and Profiling with GPU PerfStudio
  • Things that make your frame slow

3 of 58

Who this talk is geared for

  • Done games, but not graphics
  • New to graphics
  • Maybe you’ve done some graphics, but not for games
  • One time a game you were playing had a hilarious glitch and you want to know what happened
  • Want to write a corrupter…?

4 of 58

Computer Graphics for Games

  • You have 16-33 milliseconds per frame. (less for VR)
  • You will have to make tradeoffs
  • CPU and GPU work together
  • The GPU is powerful, but not all-powerful.
  • GPUs act differently than CPUs and can be ~mysterious~.

5 of 58

Graphics (D3D11) Pipeline

Input Assembler

Vertex Shader

Hull Shader

Tessellator

Domain Shader

Geometry Shader

Stream Out

Rasterizer

Pixel Shader

Output Merger

Memory Resources

Textures

Buffers

Render Targets

Constants

DepthStencil

...

CPU Side

6 of 58

Graphics (D3D11) Pipeline

Input Assembler

Vertex Shader

Hull Shader

Tessellator

Domain Shader

Geometry Shader

Stream Out

Rasterizer

Pixel Shader

Output Merger

Memory Resources

Textures

Buffers

Render Targets

Constants

DepthStencil

...

CPU Side

7 of 58

Common Graphics Problems

  • Blank screen
    • Or: things not drawing that should be
  • Flickering
  • Corruptions
  • Shader bugs
  • S l o w n e s s

8 of 58

Blank Screen Quick Checks

  • Is your math right?
  • Where’s your camera?
  • Is your geometry getting culled?
  • Are you clearing?
  • Are you actually drawing anything?

¯\_(ツ)_/¯

9 of 58

Flickering

  • Timing is wrong somewhere
  • Double-buffering correct?
  • Are multiple resources touching the same memory?
  • Dependencies?

pretend this is flashing

10 of 58

Corruptions

  • What’s getting corrupted?
  • Geometry?
  • Textures?
  • Render targets?
  • Depth buffer?
  • Constants?
  • Something else?

11 of 58

Geometry Corruptions

  • Triangle horrors
  • Mesh holes
  • Messed up orientations
  • Missing geometry
  • No geometry at all

12 of 58

Texture Corruptions

  • Rainbow missingno
  • Bizarre stretching
  • Wrong color
  • Some legit data + artifacts

13 of 58

Depth and Render Target Horrors

  • Smearing - Make sure to clear!!
  • Frame leftovers
  • Wrong order of objects
  • Translucent stuff wrong

14 of 58

Constant Buffer Corruptions

  • Might not be your resources
  • Shader constants can get wrecked too
  • Could also be a synchronization issue
  • Don’t trash what you’re not done with!

15 of 58

Shader color “printf” debugging

  • Quick, no tools involved
  • Good for sanity checks
  • Best for PS
  • Can pass thru other things too

16 of 58

Use the D3D debug layer

  • Catches a lot of issues that cause these horrors
    • Parameter & consistency validation
    • Shader linkage & resource binding validation
  • Helps you debug crashes
  • Lets you dig around directly in your code
  • No equivalent in OpenGL :(

17 of 58

Down the Rabbit Hole

  • Where in the pipeline does the badness start?
  • Which resources are involved in the badness?
  • How are you messing up the resources?
  • How do you fix it?

18 of 58

Frame Capture and Analysis Tools

  • AMD GPU PerfStudio, Intel GPA, Renderdoc, etc.
  • API calls, frame events
  • Resource viewer
  • Shader Debugger (sometimes separate)

19 of 58

Renderdoc

  • Free, open source
  • DirectX 11, OpenGL
  • Similar to other tools (PIX, Razor, etc.)
  • Simple to use

20 of 58

Renderdoc

Timeline

API Calls

Specify exe to debug here

Events

21 of 58

Renderdoc

Timeline

API Calls

Pipeline State

Events

22 of 58

Timeline

API Calls

Mesh Output

Events

(draw calls)

23 of 58

24 of 58

25 of 58

26 of 58

Render Passes

Current pass’s clears and draw calls

Corresponding API calls

27 of 58

28 of 58

29 of 58

30 of 58

31 of 58

32 of 58

33 of 58

34 of 58

35 of 58

36 of 58

Shader Debugging

  • Kind of similar to CPU debugging
  • Step through shader assembly
    • Need to compile with debug info for shader source
  • Not all shader debuggers have edit and continue

37 of 58

38 of 58

39 of 58

Sometimes it’s not your fault (but usually it is)

  • Other people’s bugs can appear in:
    • Engine
    • Compiler
    • API
    • Driver
    • Hardware!

40 of 58

Profiling

  • Your game has slow parts - I guarantee it!
  • Where are they?
  • Who’s taking longer?
    • CPU or GPU?
  • Only one way to find out.

41 of 58

Profiling

  • Profiling from the CPU side code
    • Wrap draw calls in timing blocks
    • Do a lot of testing
    • DX11 timestamps, Unreal stats, RAD telemetry
  • Graphics profiling tools
    • GPU PerfStudio
    • RTTV, PIX

42 of 58

GPU PerfStudio

  • Frame Profiler
  • Frame Debugger, Shader Analyzer
  • DX12!!
  • Tiny bit more complicated than RenderDoc

43 of 58

44 of 58

45 of 58

GPU PerfStudio

  • Lots of counters!
  • See cool and useful stats on your frame
  • Quantitatively see stuff like
    • Overdraw
    • Shader invocations
    • Primitives culled
    • Data read

46 of 58

Draw Calls

Times PS was invoked

# prims rendered

# prims sent to rasterizer

# samples that passed depth stencil

# verts/prims read by IA

Times VS was invoked

47 of 58

48 of 58

Performance Issues

  • Overdraw
  • Inefficient data layouts
  • Content issues
  • Too many tiny triangles
  • Bad shaders
  • Anti-aliasing
  • Too much context switching
  • Blood curses
  • And many more!

49 of 58

Overdraw

  • Slow because you’re drawing things you don’t need to
  • Cull as much as you can
    • Depth pre-pass, occlusion volumes, etc.
  • Use Hi-Z!
  • Opaque stuff first
  • Be careful with depth writes + alpha/discard
    • You’ll bypass early Z
    • Using Late Z -> many more pixels shaded

Hi-Z

Early Z

Pixel Shader

Late Z

50 of 58

Textures

  • PLEASE USE MIP-MAPPING
  • The right format for the right job
    • Don’t use highest precision if not needed
    • Consider using block compressed formats
  • Filter wisely
    • Trilinear filtering twice as expensive as bilinear
    • Anisotropic filtering N times as expensive, depending on sample count

51 of 58

Shaders

  • How long/complex are your shaders?
  • Some instructions are slower than others
  • Don’t use more registers than you need
  • The compiler will sometimes betray you
    • Look at the asm
  • I/O to GPU adds up fast (lol bandwidth)

52 of 58

Vertex Shaders

  • Minimize your vertex attributes!
  • Optimize your meshes
  • Complicated shaders (skinning, etc.)
  • Use an LOD system (test a lot)

struct VS_INPUT

{

float4 Position : POSITION;

float2 Texcoord : TEXCOORD0;

float3 Normal : NORMAL;

float3 Tangent : TANGENT;

float3 Bitangent: BINORMAL;

};

53 of 58

Tessellation

  • Disable it when you don’t need it
  • Minimize vertex output attributes
  • Do your own culling
    • HW culling happens later in the pipeline, at the Rasterizer
  • Don’t overtessellate!!
    • Subpixel triangles are BAD
  • Undertessellation will give you artifacts

54 of 58

Geometry Shaders

  • Probably better off doing it on the VS in most cases
  • VS instancing
  • If you must use GS, minimize output attributes
  • Why GS is slow: http://www.joshbarczak.com/blog/?p=667

55 of 58

Pixel Shaders

  • Often a bottleneck, but not always (sometimes tess or CS)
  • See if you can early-out with discard
  • Limit your trig and other expensive instructions
    • cos, rcp, integer mul and div, etc.
  • Minimize attributes sent from earlier stages
  • Texture fetches add up
    • minimize them and cluster reads/writes

56 of 58

Compute Shaders

  • Useful for general purpose computing, stuff that’s too slow in other stages
  • Powerful, but can be tricky - They are not a cure-all!
  • Big pile of threads that share memory
  • Output to unordered access view (UAV)
  • Can do atomic operations

57 of 58

Wrap Up

  • Graphics is cool and messy and involves lots of detective work
  • Try many things, testing is the way to enlightenment
  • Use the tools!
  • Make things easy for the GPU and you will be rewarded

58 of 58

Questions?

@Icetigris