cpt-max / MonoGame-Shader-Samples

Compute, Tessellation and Geometry Shader Samples for MonoGame
46 stars 4 forks source link

MonoGame Shader Samples Overview

Each sample is in a separate branch of this repository, so don't use this branch here, pick a sample from below first.
The samples are based on a custom MonoGame fork, that adds tesselation, geometry and compute shaders.
You don't need to build the fork in order to run the samples, as they use prebuilt NuGet packages. As long as .Net 8 is installed, they should just launch.

Equivalent Android Samples
Compute Shader Guide for MonoGame
NuGet Packages, Platform Support and Build Requirements
Pull Request for main MonoGame Repo
MonoGame Forum Post
Download Prebuilt Executables (win-x64, linux-x64, requires .Net 5+)

Simple Tessellation & Geometry Shader

This sample uses a very simple hull and domain shader to tessellate a single input triangle into many sub triangles. Each sub triangle is then passed into a geometry shader to generate even more triangles along it's edges, which creates a wireframe-like effect.

Edge-rounding Tessellation Shader

This sample uses a hull and domain shader to round off the edges of a mesh. The mesh is created out of quad patches. The rounding radius and tesselltation factor can be changed dynamically.

Particle Compute Shader

This sample uses a compute shader to update particles on the GPU. The particle buffer is used directly by the vertex shader that draws the particles. Since no data needs to be downloaded to the CPU, this method is very fast.

Collision Test Compute Shader

This sample uses a compute shader to do brute-force collision checks between circles. The buffer containing the collision results is then downloaded to the CPU, in order to color the circles according to how many collisions they are involved in.

Pixel-Sort Compute Shader

This sample uses a compute shader to sort pixels in a texture horizontally by hue. For each pair of pixels a compute thread is launched, that swaps the pixels (if neccessary) like a bubble sort.

Edit Mesh Compute Shader

This sample uses a compute shader to modify a vertex and an index buffer directly on the GPU.

Texture3D Compute Shader

This sample uses a compute shader to update a 3D texture on the GPU.
The texture is initialized with a bunch of randomly colored pixels. The pixel's color represents a velocity, so pixels move through the volume.

Object Culling with Indirect Draw

This sample uses a compute shader to determine the visibility of objects directly on the GPU. A structured buffer is filled up with all the visible objects, which are then draw using indirect draw. This has the advantage that no data has to be downloaded from the GPU to the CPU.
There is a converted version of this sample using an append buffer, which slightly simplifies the code.

Particles with Indirect Draw

This sample uses a compute shader to spawn, destroy and update particles.
Since the spawn and destroy logic is done on the GPU, the CPU doesn't know how many particles to draw.
Using indirect draw makes it possible to draw and update the correct number of particles, without the need to download that data to the CPU.