Wasabi
Public Member Functions | Static Public Member Functions | Protected Member Functions | Protected Attributes | Friends | List of all members
WShader Class Reference

#include <WEffect.hpp>

Inheritance diagram for WShader:
WFileAsset WBase WBackfaceDepthRenderStageObjectPS WForwardRenderStageAnimatedObjectVS WForwardRenderStageObjectPS WForwardRenderStageObjectVS WForwardRenderStageTerrainPS WForwardRenderStageTerrainVS WGBufferAnimatedVS WGBufferPS WGBufferVS WSpritePS WSpriteVS

Public Member Functions

virtual std::string GetTypeName () const override
 
virtual void SetID (uint32_t newID) override
 
virtual void SetName (std::string newName) override
 
 WShader (class Wasabi *const app, uint32_t ID=0)
 
virtual void Load (bool bSaveData=false)
 
virtual bool Valid () const override
 
virtual WError SaveToStream (WFile *file, std::ostream &outputStream) override
 
virtual WError LoadFromStream (WFile *file, std::istream &inputStream, std::vector< void * > &args, std::string nameSuffix) override
 
- Public Member Functions inherited from WFileAsset
 WFileAsset (class Wasabi *const app, uint32_t ID=0)
 
virtual WError LoadFromStream (class WFile *file, std::istream &inputStream, vector< void * > &args, std::string nameSuffix)=0
 
- Public Member Functions inherited from WBase
 WBase (class Wasabi *const app, uint32_t ID=0)
 
uint32_t GetID () const
 
std::string GetName () const
 
class WasabiGetAppPtr () const
 
void AddReference ()
 
void RemoveReference ()
 

Static Public Member Functions

static std::string _GetTypeName ()
 
static std::vector< void * > LoadArgs (bool bSaveData=false)
 

Protected Member Functions

void LoadCodeSPIRV (const char *const code, int len, bool bSaveData=false)
 
void LoadCodeGLSL (std::string code, bool bSaveData=false)
 
void LoadCodeSPIRVFromFile (std::string filename, bool bSaveData=false)
 
void LoadCodeGLSLFromFile (std::string filename, bool bSaveData=false)
 

Protected Attributes

W_SHADER_DESC m_desc
 
VkShaderModule m_module
 
- Protected Attributes inherited from WBase
class Wasabim_app
 
uint32_t m_ID
 
std::string m_name
 

Friends

class WEffect
 
class WMaterial
 

Detailed Description

Encapsulation of a shader object. A shader is a small program bound to the graphics (or compute) Vulkan pipelines that does its work on the GPU.

To create a WShader, one must define a child class which implements WShader::Load(). The load function needs to fill in the m_desc and m_module protected variables. m_module is automatically filled by a call to one of the LoadCode* functions (which needs to be called during the Load() call). The m_desc structure should be initialized to describe the shader and the resources that can/need to be bound to it.

Examples:

Creating a simple vertex shader type that is compatible with the default vertex layout of Wasabi (see WDefaultVertex).

class SimpleVS : public WShader {
public:
SimpleVS(class Wasabi* const app) : WShader(app) {}
// We need to implement Load(), which loads the shader.
virtual void Load(bool bSaveData = false) {
m_desc.type = W_VERTEX_SHADER; // Creating a vertex shader
// We have 1 UBO, bound to "location = 0" (in the shader code below)
// projection matrix
// world matrix
// view matrix
}),
};
// We have 1 per-vertex input layout
// This layout corresponds to the layout of vertices in WDefaultVertex
// which WGeometry uses by default.
})};
"#version 450\n"
""
"#extension GL_ARB_separate_shader_objects : enable\n"
"#extension GL_ARB_shading_language_420pack : enable\n"
""
"layout(location = 0) in vec3 inPos;\n"
"layout(location = 1) in vec3 inTang;\n"
"layout(location = 2) in vec3 inNorm;\n"
"layout(location = 3) in vec2 inUV;\n"
""
"layout(binding = 0) uniform UBO {\n"
" mat4 projectionMatrix;\n"
" mat4 modelMatrix;\n"
" mat4 viewMatrix;\n"
"} ubo;\n"
""
"layout(location = 0) out vec2 outUV;\n"
""
"void main() {\n"
" outUV = inUV;\n"
" gl_Position = ubo.projectionMatrix * \n"
" ubo.viewMatrix * \n"
" ubo.modelMatrix * \n"
" vec4(outWorldPos, 1.0);\n"
"}\n"
, bSaveData);
}
};

Simple pixel (or fragment) shader, compatible with the above vertex shader:

class SimplePS : public WShader {
public:
SimplePS(class Wasabi* const app) : WShader(app) {}
virtual void Load(bool bSaveData = false) {
m_desc.type = W_FRAGMENT_SHADER; // Fragment (or pixel) shader
m_desc.bound_resources = {}; // no bound resources
"#version 450\n"
""
"#extension GL_ARB_separate_shader_objects : enable\n"
"#extension GL_ARB_shading_language_420pack : enable\n"
"" // The UV coordinates are passed in from the vertex shader
"layout(location = 0) in vec2 inUV;\n"
"" // We need to output a color using this variable in main()
"layout(location = 0) out vec4 outFragColor;\n"
""
"void main() {\n"
" outFragColor = vec4(inUV.x, inUV.y, 0, 1);\n"
"}\n"
, bSaveData);
}
};

Using the above shaders to create an effect:

WShader* vs = new SimpleVS(this);
vs->Load();
WShader* ps = new SimplePS(this);
ps->Load();
WEffect* FX = new WEffect(this);
FX->BindShader(vs);
FX->BindShader(ps);
FX->BuildPipeline(Renderer->GetDefaultRenderTarget());

Member Function Documentation

◆ _GetTypeName()

static std::string WShader::_GetTypeName ( )
static

Returns "Shader" string.

Returns
Returns "Shader" string

◆ GetTypeName()

virtual std::string WShader::GetTypeName ( ) const
overridevirtual

This function must be implemented by a child class. This is used for debugging, in which a class should return its name.

Returns
The name of the class

Implements WBase.

◆ Load()

virtual void WShader::Load ( bool  bSaveData = false)
inlinevirtual

Loads the shader into this object. This function must be implemented by a child class. The implemented function must fill in m_module and m_desc protected members to fully define the shader. See WShader for example usage.

Parameters
bSaveDataWhether or not to save the code data to be able to write it to a file later

Reimplemented in WForwardRenderStageTerrainPS, WForwardRenderStageTerrainVS, WForwardRenderStageObjectPS, WSpritePS, WForwardRenderStageAnimatedObjectVS, WGBufferPS, WSpriteVS, WForwardRenderStageObjectVS, WGBufferAnimatedVS, WBackfaceDepthRenderStageObjectPS, and WGBufferVS.

◆ LoadCodeGLSL()

void WShader::LoadCodeGLSL ( std::string  code,
bool  bSaveData = false 
)
protected

Loads GLSL shader code from a string. Loaded code will be compiled into m_module.

Parameters
codeString containing GLSL code

◆ LoadCodeGLSLFromFile()

void WShader::LoadCodeGLSLFromFile ( std::string  filename,
bool  bSaveData = false 
)
protected

Loads GLSL shader code from file. Loaded code will be compiled into m_module.

Parameters
filenameName of the file to load the code from

◆ LoadCodeSPIRV()

void WShader::LoadCodeSPIRV ( const char *const  code,
int  len,
bool  bSaveData = false 
)
protected

Loads SPIR-V formatted shader code from memory. Loaded code will be compiled into m_module.

Parameters
codeAddress of the SPIR-V code in memory
lenLength of the code, in bytes

◆ LoadCodeSPIRVFromFile()

void WShader::LoadCodeSPIRVFromFile ( std::string  filename,
bool  bSaveData = false 
)
protected

Loads SPIR-V formatted shader code from a file. Loaded code will be compiled into m_module.

Parameters
filenameName of the file to load the code from

◆ SetID()

virtual void WShader::SetID ( uint32_t  newID)
overridevirtual

Sets the ID of this object and notifies its manager.

Parameters
newIDNew ID

Implements WBase.

◆ SetName()

virtual void WShader::SetName ( std::string  newName)
overridevirtual

Sets the name of this object.

Parameters
nameNew name for the object

Implements WBase.

◆ Valid()

virtual bool WShader::Valid ( ) const
overridevirtual

Checks the validity of the shader. A shader is valid if m_module is set.

Returns
true if the shader is valid, false otherwise

Implements WBase.

Member Data Documentation

◆ m_desc

W_SHADER_DESC WShader::m_desc
protected

Shader description

◆ m_module

VkShaderModule WShader::m_module
protected

Compiled shader code object


The documentation for this class was generated from the following file:
WShader::LoadCodeGLSL
void LoadCodeGLSL(std::string code, bool bSaveData=false)
WEffect::BindShader
WError BindShader(WShader *shader)
W_BOUND_RESOURCE
struct W_BOUND_RESOURCE W_BOUND_RESOURCE
W_TYPE_VEC_3
Definition: WEffect.hpp:50
WEffect
Definition: WEffect.hpp:539
WShader::Load
virtual void Load(bool bSaveData=false)
Definition: WEffect.hpp:485
W_SHADER_DESC::bound_resources
std::vector< W_BOUND_RESOURCE > bound_resources
Definition: WEffect.hpp:265
W_TYPE_UBO
Definition: WEffect.hpp:63
W_TYPE_MAT4X4
Definition: WEffect.hpp:54
W_VERTEX_SHADER
Definition: WEffect.hpp:23
W_INPUT_LAYOUT
struct W_INPUT_LAYOUT W_INPUT_LAYOUT
WBase::RemoveReference
void RemoveReference()
W_FRAGMENT_SHADER
Definition: WEffect.hpp:25
W_TYPE_VEC_2
Definition: WEffect.hpp:48
W_SHADER_DESC::type
W_SHADER_TYPE type
Definition: WEffect.hpp:263
WShader::m_desc
W_SHADER_DESC m_desc
Definition: WEffect.hpp:431
W_SHADER_DESC::input_layouts
std::vector< W_INPUT_LAYOUT > input_layouts
Definition: WEffect.hpp:268
WEffect::BuildPipeline
WError BuildPipeline(class WRenderTarget *rt)
W_SHADER_VARIABLE_INFO
struct W_SHADER_VARIABLE_INFO W_SHADER_VARIABLE_INFO
Wasabi
Definition: WCore.hpp:44
WShader
Definition: WEffect.hpp:421