Jump to content

Shader Families


Josh

3,180 views

 Share

I started to implement quads for tessellation, and at that point the shader system reached the point of being unmanageable. Rendering an object to a shadow map and to a color buffer are two different processes that require two different shaders. Turbo introduces an early Z-pass which can use another shader, and if variance shadow maps are not in use this can be a different shader from the shadow shader. Rendering with tessellation requires another set of shaders, with one different set for each primitive type (isolines, triangles, and quads). And then each one of these needs a masked and opaque option, if alpha discard is enabled.

All in all, there are currently 48 different shaders a material could use based on what is currently being drawn. This is unmanageable.

To handle this I am introducing the concept of a "shader family". This is a JSON file that lists all possible permutations of a shader. Instead of setting lots of different shaders in a material, you just set the shader family one:

shaderFamily: "PBR.json"

Or in code:

material->SetShaderFamily(LoadShaderFamily("PBR.json"));

The shader family file is a big JSON structure that contains all the different shader modules for each different rendering configuration: Here are the partial contents of my PBR.json file:

{
	"turboShaderFamily" :
	{
		"OPAQUE":
		{
			"default":
			{
				"base":
				{
					"vertex": "Shaders/PBR.vert.spv",
					"fragment": "Shaders/PBR.frag.spv"
				},
				"depthPass":
				{
					"vertex": "Shaders/Depthpass.vert.spv"
				},
				"shadow":
				{
					"vertex": "Shaders/Shadow.vert.spv"
				}
			},
			"isolines":
			{
				"base":
				{
					"vertex": "Shaders/PBR_Tess.vert.spv",
					"tessellationControl": "Shaders/Isolines.tesc.spv",
					"tessellationEvaluation": "Shaders/Isolines.tese.spv",
					"fragment": "Shaders/PBR_Tess.frag.spv"
				},
				"shadow":
				{
					"vertex": "Shaders/DepthPass_Tess.vert.spv",
					"tessellationControl": "Shaders/DepthPass_Isolines.tesc.spv",
					"tessellationEvaluation": "Shaders/DepthPass_Isolines.tese.spv"
				},
				"depthPass":
				{
					"vertex": "Shaders/DepthPass_Tess.vert.spv",
					"tessellationControl": "DepthPass_Isolines.tesc.spv",
					"tessellationEvaluation": "DepthPass_Isolines.tese.spv"
				}
			},			
			"triangles":
			{
				"base":
				{
					"vertex": "Shaders/PBR_Tess.vert.spv",
					"tessellationControl": "Shaders/Triangles.tesc.spv",
					"tessellationEvaluation": "Shaders/Triangles.tese.spv",
					"fragment": "Shaders/PBR_Tess.frag.spv"
				},
				"shadow":
				{
					"vertex": "Shaders/DepthPass_Tess.vert.spv",
					"tessellationControl": "Shaders/DepthPass_Triangles.tesc.spv",
					"tessellationEvaluation": "Shaders/DepthPass_Triangles.tese.spv"
				},
				"depthPass":
				{
					"vertex": "Shaders/DepthPass_Tess.vert.spv",
					"tessellationControl": "DepthPass_Triangles.tesc.spv",
					"tessellationEvaluation": "DepthPass_Triangles.tese.spv"
				}
			},
			"quads":
			{
				"base":
				{
					"vertex": "Shaders/PBR_Tess.vert.spv",
					"tessellationControl": "Shaders/Quads.tesc.spv",
					"tessellationEvaluation": "Shaders/Quads.tese.spv",
					"fragment": "Shaders/PBR_Tess.frag.spv"
				},
				"shadow":
				{
					"vertex": "Shaders/DepthPass_Tess.vert.spv",
					"tessellationControl": "Shaders/DepthPass_Quads.tesc.spv",
					"tessellationEvaluation": "Shaders/DepthPass_Quads.tese.spv"
				},
				"depthPass":
				{
					"vertex": "Shaders/DepthPass_Tess.vert.spv",
					"tessellationControl": "DepthPass_Quads.tesc.spv",
					"tessellationEvaluation": "DepthPass_Quads.tese.spv"
				}
			}
		}
	}
}

A shader family file can indicate a root to inherit values from. The Blinn-Phong shader family pulls settings from the PBR file and just switches some of the fragment shader values.

{
	"turboShaderFamily" :
	{
		"root": "PBR.json",
		"OPAQUE":
		{
			"default":
			{
				"base":
				{
					"fragment": "Shaders/Blinn-Phong.frag.spv"
				}
			},
			"isolines":
			{
				"base":
				{
					"fragment": "Shaders/Blinn-Phong_Tess.frag.spv"
				}
			},
			"triangles":
			{
				"base":
				{
					"fragment": "Shaders/Blinn-Phong_Tess.frag.spv"
				}
			},
			"quads":
			{
				"base":
				{
					"fragment": "Shaders/Blinn-Phong_Tess.frag.spv"
				}
			}
		}
	}
}

If you want to implement a custom shader, this is more work because you have to define all your changes for each possible shader variation. But once that is done, you have a new shader that will work with all of these different settings, which in the end is easier. I considered making a more complex inheritance / cascading schema but I think eliminating ambiguity is the most important goal in this and that should override any concern about the verbosity of these files. After all, I only plan on providing a couple of these files and you aren't going to need any more unless you are doing a lot of custom shaders. And if you are, this is the best solution for you anyways.

Consequently, the baseShader, depthShader, etc. values in the material file definition are going away. Leadwerks .mat files will always use the Blinn-Phong shader family, and there is no way to change this without creating a material file in the new JSON material format.

The shader class is no longer derived from the Asset class because it doesn't correspond to a single file. Instead, it is just a dumb container. A ShaderModule class derived from the Asset class has been added, and this does correspond with a single .spv file. But you, the user, won't really need to deal with any of this.

The result of this is that one material will work with tessellation enabled or disabled, quad, triangle, or line meshes, and animated meshes. I also added an optional parameter in the CreatePlane(), CreateBox(), and CreateQuadSphere() commands that will create these primitives out of quads instead of triangles. The main reason for supporting quad meshes is that the tessellation is cleaner when quads are used. (Note that Vulkan still displays quads in wireframe mode as if they are triangles. I think the renderer probably converts them to normal triangles after the tessellation stage.)

tris.png.924dbf1f8023ab6142f6c6ad5fcfc2f2.png

quads.png.a7632c4712016a13b721ea264422a538.png

I also was able to implement PN Quads, which is a quad version of the Bezier curve that PN Triangles add to tessellation.

original.thumb.png.1e8450693214116ddf623a1d82d4de9b.png

tess.thumb.png.37d043cbdf82795bfa7ace7bed1f48f6.png

PNQuads.thumb.png.45be40c442274d05a4ab0d00c353d721.png

Basically all the complexity is being packed into the shader family file so that these decisions only have to be made once instead of thousands of times for each different material.

  • Like 2
 Share

4 Comments


Recommended Comments

On 12/12/2019 at 7:11 PM, Lethal Raptor Games said:

How do these handle geometry shaders?

A geometry shader can be added in the definition.

  • Like 1
  • Thanks 1
Link to comment

Is this the correct naming?

{
	"root": "PBR.json",
	"float":
	{
		"OPAQUE":
		{
			"default":
			{
				"base":
				{
					"vertex": "Landscape.vert.spv",
					"geometry": "Landscape.geom.spv",
					"fragment": "Landscape.frag.spv"
				}

 

All stages compile and the vert & frag work, but the geometry shader seems to be skipped.  Changes to it don't take effect.

Link to comment
Guest
Add a comment...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...