Source SDK 2013: Your First Shader: Difference between revisions
Jump to navigation
Jump to search
Deniz Sezen (talk | contribs) (Created page with "= Introduction = This page will teach you how to create a screen space pixel shader for use with Source SDK 2013. = Prerequisites = This tutorial assumes the following: #...") |
Deniz Sezen (talk | contribs) No edit summary |
||
Line 12: | Line 12: | ||
= Overview = | = Overview = | ||
== | == Definitions == | ||
Words you should understand the meaning of before you continue reading: | |||
* '''Material (or VMT):''' A text file that defines a two dimensional surface. See the [[Material]] page for more information. | |||
* '''Shader:''' A set of coded instructions which are sent to a GPU that dictate how an object should be drawn. See the [[Shader]] page for more information. | |||
== Architecture == | |||
Before we can begin, it is helpful to have a high-level understanding of the shader system in Source. The shader system consists of 4 major modules. The table below lists each module and describes (in a simplified fashion) its purpose: | Before we can begin, it is helpful to have a high-level understanding of the shader system in Source. The shader system consists of 4 major modules. The table below lists each module and describes (in a simplified fashion) its purpose: | ||
{| class="wikitable" | {| class="wikitable" | ||
Line 18: | Line 23: | ||
! Module name !! Purpose | ! Module name !! Purpose | ||
|- | |- | ||
| materialsystem || Responsible for loading vmt files. | | materialsystem || Responsible for loading vmt files and managing shader modules. | ||
|- | |- | ||
| shaderapidx9 || Low level graphics handling. Contains the implementation specific (DirectX) interface to the graphics adapter. | | shaderapidx9 || Low level graphics handling. Contains the implementation specific (DirectX) interface to the graphics adapter. This module also stores all of the shader instances. | ||
|- | |- | ||
| stdshader_dx9 || Contains the base set of shaders that ship with all Source games. The _dx9 identifier indicates that this module contains '''only''' DirectX 9 versions of the built shaders. Fallback shaders will be described later in this article. | | stdshader_dx9 || Contains the base set of shaders that ship with all Source games. The _dx9 identifier indicates that this module contains '''only''' DirectX 9 versions of the built shaders. Fallback shaders will be described later in this article. The stdshader modules expose a dictionary that maps symbolic shader names to shader objects. This will be used during shader lookups which we'll cover below. | ||
|- | |- | ||
| game_shader_dx9 || This module will contain all of your custom shaders. It is last to load in the game initialization process, thus allowing you to replace shaders defined in stdshader_dx9 (or below). | | game_shader_dx9 || This module will contain all of your custom shaders. It is last to load in the game initialization process, thus allowing you to replace shaders defined in stdshader_dx9 (or below). | ||
|} | |} | ||
The shader loading process is simple: | |||
# On game startup, all stdshader_dxX modules are loaded along with the game_shader_dx9 module. | |||
# Later on, something in the game triggers a call to CMaterialSystem::FindMaterial(). | |||
#* The materialsystem searches through its internal dictionary to see if it has the requested material already in memory. | |||
#** If the material is not found, it will be loaded in from the file system and a CMaterial instance will be created and returned. | |||
# The materialsystem will then call CMaterial::PrecacheVars() on the requested material. | |||
#* CMaterial::InitializeShader() is called with the VMT's KeyValues. | |||
#** Materialsystem loops over all loaded shader modules and checks their shader dictionaries for the shader. | |||
#*** If the shader is not found, the entire process is aborted. | |||
#*** The variables defined by the VMT are processed and loaded into the shader object. | |||
#**** '''NOTE:''' It is at this point in time that your SHADER_INIT is called. | |||
#*** Fallback shader processing occurs if required. | |||
#** Shader reference is stored inside CMaterial instance. | |||
#** A copy of the shader and VMT parameters is stored off inside the CMaterial instance. When the shader reference in the previous step is bound to the pipeline, these variables will be loaded in. | |||
== Code structure == |
Revision as of 22:37, 26 December 2014
Introduction
This page will teach you how to create a screen space pixel shader for use with Source SDK 2013.
Prerequisites
This tutorial assumes the following:
- You've got a Source SDK 2013 repository checked out and setup on your local machine.
- If you don't, visit the Source SDK 2013 page.
- You've successfully performed the steps to setup shader compiles.
- If not, visit the Source SDK 2013: Shader Authoring page.
- You understand C++.
- If you don't, you will have a lot of trouble following along.
Overview
Definitions
Words you should understand the meaning of before you continue reading:
- Material (or VMT): A text file that defines a two dimensional surface. See the Material page for more information.
- Shader: A set of coded instructions which are sent to a GPU that dictate how an object should be drawn. See the Shader page for more information.
Architecture
Before we can begin, it is helpful to have a high-level understanding of the shader system in Source. The shader system consists of 4 major modules. The table below lists each module and describes (in a simplified fashion) its purpose:
Module name | Purpose |
---|---|
materialsystem | Responsible for loading vmt files and managing shader modules. |
shaderapidx9 | Low level graphics handling. Contains the implementation specific (DirectX) interface to the graphics adapter. This module also stores all of the shader instances. |
stdshader_dx9 | Contains the base set of shaders that ship with all Source games. The _dx9 identifier indicates that this module contains only DirectX 9 versions of the built shaders. Fallback shaders will be described later in this article. The stdshader modules expose a dictionary that maps symbolic shader names to shader objects. This will be used during shader lookups which we'll cover below. |
game_shader_dx9 | This module will contain all of your custom shaders. It is last to load in the game initialization process, thus allowing you to replace shaders defined in stdshader_dx9 (or below). |
The shader loading process is simple:
- On game startup, all stdshader_dxX modules are loaded along with the game_shader_dx9 module.
- Later on, something in the game triggers a call to CMaterialSystem::FindMaterial().
- The materialsystem searches through its internal dictionary to see if it has the requested material already in memory.
- If the material is not found, it will be loaded in from the file system and a CMaterial instance will be created and returned.
- The materialsystem searches through its internal dictionary to see if it has the requested material already in memory.
- The materialsystem will then call CMaterial::PrecacheVars() on the requested material.
- CMaterial::InitializeShader() is called with the VMT's KeyValues.
- Materialsystem loops over all loaded shader modules and checks their shader dictionaries for the shader.
- If the shader is not found, the entire process is aborted.
- The variables defined by the VMT are processed and loaded into the shader object.
- NOTE: It is at this point in time that your SHADER_INIT is called.
- Fallback shader processing occurs if required.
- Shader reference is stored inside CMaterial instance.
- A copy of the shader and VMT parameters is stored off inside the CMaterial instance. When the shader reference in the previous step is bound to the pipeline, these variables will be loaded in.
- Materialsystem loops over all loaded shader modules and checks their shader dictionaries for the shader.
- CMaterial::InitializeShader() is called with the VMT's KeyValues.