Compute Shader

It transfers some computation from CPU to GPU by HLSL code. Hence the name has “compute” and “shader”.

References:

Unity Documentation: https://docs.unity3d.com/Manual/class-ComputeShader.html

Compute Shader are programs that run on the graphic card, outside of the normal rendering pipeline. It works on:

  • Android: OpenGL ES 3.1, iOS: Metal

Compute Shader Assets

  • .compute file extension, DirectX 11 style in HLSL, #pragma indicates compute shader kernel
#pragma kernel FillWithRed

RWTexture2D<float4> res;

[numthreads(1,1,1)]
void FillWithRed(uint3 dtid : SV_DispatchThreadID)
{	
	res[dtid.xy] = float4(1,0,0,1); 
}

numthreads, what does it mean?

The amount of threads that share data and are executed in one go (warp). Every group is made of numthreads, so dispatching fewer groups is faster. The threads that are dispatched should be a multiple of the GPU’s architecture’s minimum threads for optima performance. 32 for NVIDIA, 64 for AMD. Fewer threads mean wasted resources.

ComputerBuffer is structured buffer in DX11. Render Texture having “random access” flag is unordered access view in DX11 (UAV)

Texture and samplers aren’t separate objects in Unity, so:

  • use sampler at the beginning
  • use pre-defined sampler, such as SamplerStateMyLinearClampSampler creates a sampler that has linear filter mode and clamp wrap mode.

DirextX 11 supports many actions that are not supported on other platforms. Be aware of this and ensure shaders' behavior offers less support:

  • out-of-bounds memory access is bad.
  • initialize resources
  • bind all resources that are declared in compute shader

Sampler State

SampelrState decides how to sample a texture (point filter, linear filter, …). Multiple textures can reuse one sampler, and devices have a limitation on the number of sampler states.

Unity recognizes certain hard-coded naming pattern (all case insensitive):

  • filtering mode: “Point”, “Linear”, “Trilinear”
  • wrap mode: “Clamp”, “Repeat”, “Mirror”, “MirrorOnce”
  • “Compare” set up sampler for depth comparison
  • “AnisoX”, (x = 2, 4, 8, 16) for anisotropic filtering

Reflection

Pixel-projected Reflections

URL: http://advances.realtimerendering.com/s2017/index.html

  • Clear the intermediate buffer

    • A pixel-data grid, single value per pixel
    • projection fill the value, reflection read the value
    • problem: multiple pixels can be written in the same place in the buffer, because of GPU scheduling and independent pixel processing
    • solution: encode a screen-space offset instead of color
      • we know reflected pixel coordinate (current pixel position), and the reflecting pixel coordinate (where we write the data)
      • find and store the smallest offset : InterlockedMin operation
      • for different offset direction: 4 coordinate system is enough, and Y is the most important one, reflection pass will restore the encoded offset
      • most to least significant bits:
        • 12 bits ‘Y’ integer, unsigned
        • 3 bits ‘Y’ fraction, signed, flipped
        • 12 bits ‘X’ integer, signed,
        • 3 bits ‘X’ fraction, signed, not flipped
        • 2 bits coordinate system index
  • Projection Pass

    • for every pixel, find the rectangle on the grid that reflects it.
    • write corresponding pixel-data to the intermediate buffer.
  • Reflection Pass

    • read pixel-data from the “intermediate buffer”
    • write the color to the final reflection buffer
    • Fill the holes: find the neighboring pixel-data with the smallest offset; use it if there’s no data in intermediate buffer.
    • Filtering the distortion caused by stretching and squeezing: use the encoded fraction
    • Color bleeding caused by filtered color sampling: combine filtered and no-filtered samples

Similar implementation Screen Space Planar Reflections in Ghost Recon Wildlands: http://remi-genin.fr/blog/screen-space-plane-indexed-reflection-in-ghost-recon-wildlands/#projection-jump

The idea is to retrieve the color from the original pixel position at the projected pixel position.

Misc

Zero-G Cuckooland Ghost In The Machine: https://zero-g.co.uk/products/ghost-in-the-machine

This is a collection of audio sampling “textures” for music creation. Many games used it, like this, and this, and this.