2
\$\begingroup\$

I stumbled upon a few videos about shaders and materials from The Cherno and my understanding is that materials are this sort of input that a shader takes to calculate an output instead of hard coding values into the shader so basically shaders become more flexible since you can control what their output is with a material.

I'm not sure if this is 100% accurate, but it makes sense so if this is true would you have multiple types of materials for different shaders? For example, shadows I believe require at least a shadow and depth shader so would I have a shadow material? Same for skinning/skeleton animations or are materials only for controlling how an object reacts to light?

\$\endgroup\$

2 Answers 2

4
\$\begingroup\$

A shader is simply a program that runs on the GPU. Shaders are most often used to determine how a mesh is rendered, but can be used for many other things as well.

Rendering meshes

The most common use case for shaders is to describe how the surface of a mesh is rendered. In this case, the shader will usually be paired with a material, which is a collection of settings for that shader that are not hard-coded into the shader. The settings may include what textures are displayed on the surface, how glossy or metallic the surface is, or anything else required by the shader. One game will often have many different materials that use the same shader. For example, two humans might use the same set of shaders for rendering their clothing and skin, but have different materials with different textures.

At runtime, the shader will receive scene information (such as the position and rotation of the mesh in 3D space, and the data for any lights that are in range of the mesh) as well as the settings from the material.

Typically, the shader will determine the color of each pixel on screen where the mesh is visible. The color is often determined by the texture(s) assigned to the material, and how light should interact with the surface at that pixel's point in 3D space. However, shaders can also incorporate special effects that are independent of scene lighting - for example, highlighting an object by flashing it red.

However, besides determining the color of each pixel, shaders can also do advanced things like manipulating the vertices of the mesh. For example, vegetation (such as trees or bushes) might use a shader that causes some of the vertices to sway back and forth, giving the illusion that the vegetation is swaying in the wind. In this case, the material would probably have settings specifying things like how often and how far the vertices sway.

Full-screen effects

Another common use for shaders is for applying post-processing effects over the entire screen. These are often called screen-space shaders. Rather than operating on a mesh, a screen-space shader takes a rendered frame as input (sometimes also using the depth buffer as a second input) and outputs a new version of the frame. Common screen-space effects include bloom, ambient occlusion, reflections, and depth-of-field.

While a screen-space shader still uses settings, I don't think the term 'material' is commonly used for screen-space shaders.

Compute shaders

While shaders started out as a way to render complex graphics, the specialized hardware of a GPU can be used for other types of computation as well. Modern GPUs often support compute shaders. This is a category of general-purpose GPU programs which operate on a set of input and produce an output that is (usually) not a rendered image.

Compute shaders may be used for any kind of data processing that benefits from the highly parallelized computational architecture of a GPU. One of the most famous recent uses for compute shaders is in "mining" digital currency such as Bitcoin. Compute shaders can be used in video games for animation, physics, AI, audio processing, and more, though the developer must take care that the compute shader doesn't use enough GPU time to interfere with rendering.

\$\endgroup\$
1
  • \$\begingroup\$ This answer is better. Up-vote this one. \$\endgroup\$
    – Casey
    Commented Feb 14 at 20:55
0
\$\begingroup\$

Sort of. The way I would generally explain it is as follows:

"Shader Program"

The representation of the actual code written in a shader language; HSLS or GLSL. In the case of DirectX this is a structure in C++ that encapsulates the byte code of the HLSL shader and any of the applicable shader stages (VS, PS, HS, DS, GS, CS).

"Shader"

A collection of Shader Programs and what raster, blend, depth, and stencil operations to apply to them.

"Material"

Encapsulates what Shader to use and what textures to use as input into a Shader Program.

My own engine only allows a one-to-one relationship between Shaders and Materials. That is, Materials can only reference one Shader. But, a Shader can reference multiple Shader Programs. You can write one Vertex Shader Program and reference it between several different Shader objects.

Example Shader (custom xml):

<shader name="asteroid">
    <shaderprogram src="Data/ShaderPrograms/entity_VS.cso"/>
    <shaderprogram src="Data/ShaderPrograms/entity_PS.cso"/>
    <raster>
        <fill>solid</fill>
        <cull>none</cull>
        <antialiasing>false</antialiasing>
    </raster>
    <blends>
        <blend enable = "true">
            <color src = "src_alpha" dest = "inv_src_alpha" op = "add" />
        </blend>
    </blends>
    <depth enable = "false" writable = "false" />
    <stencil enable = "false" readable = "false" writable = "false" />
</shader>

Seen here, the Shader references two shader programs, a compiled Vertex shader and a compiled Pixel shader.

Example Material (custom xml):

<material name="asteroid">
    <shader src="Data/Shaders/asteroid.shader" />
    <textures>
        <diffuse src="Data/Images/asteroid.png" />
    </textures>
</material>

The associated Material takes the previously mentioned Shader with a given texture as input.

During Runtime, this data is used to create a Material object that knows what shader stages and textures to apply during draw calls.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .