CUGL 4.0
Cornell University Game Library
Loading...
Searching...
No Matches
Public Member Functions | Static Public Member Functions | List of all members
cugl::graphics::ShaderSource Class Reference

#include <CUShaderSource.h>

Public Member Functions

 ShaderSource ()
 
 ~ShaderSource ()
 
void dispose ()
 
bool init (ShaderStage stage)
 
bool initOpenGL (const std::string source, ShaderStage stage)
 
bool initOpenGLFromFile (const std::string file, ShaderStage stage)
 
bool initVulkan (const std::vector< std::byte > &bytes, ShaderStage stage)
 
bool initVulkan (std::vector< std::byte > &&bytes, ShaderStage stage)
 
bool initVulkan (std::string source, ShaderStage stage)
 
bool initVulkanFromFile (const std::string file, ShaderStage stage)
 
bool initFromSource (const std::shared_ptr< JsonValue > &source)
 
ShaderStage getStage () const
 
void setStage (ShaderStage stage)
 
const std::string getOpenGL () const
 
void setOpenGL (const std::string source)
 
bool loadOpenGL (const std::string file)
 
const std::vector< std::byte > & getVulkan () const
 
void setVulkan (const std::string source)
 
void setVulkan (const std::vector< std::byte > &bytes)
 
void setVulkan (std::vector< std::byte > &&bytes)
 
bool loadVulkan (const std::string file)
 

Static Public Member Functions

static std::shared_ptr< ShaderSourcealloc (ShaderStage stage)
 
static std::shared_ptr< ShaderSourceallocOpenGL (const std::string source, ShaderStage stage)
 
static std::shared_ptr< ShaderSourceallocOpenGLFromFile (const std::string file, ShaderStage stage)
 
static std::shared_ptr< ShaderSourceallocVulkan (const std::vector< std::byte > &bytes, ShaderStage stage)
 
static std::shared_ptr< ShaderSourceallocVulkan (std::vector< std::byte > &&bytes, ShaderStage stage)
 
static std::shared_ptr< ShaderSourceallocVulkanFromFile (const std::string file, ShaderStage stage)
 
static std::shared_ptr< ShaderSourceallocFromSource (const std::shared_ptr< JsonValue > &source)
 

Detailed Description

This class represents source code for a GLSL shader.

This class provides us with a flexible way to support both OpenGL and Vulkan shaders. In particular, it stores the source code for both – OpenGL shaders as a string and Vulkan shaders as byte code. Note that these shaders have to be loaded separately, as Vulkan GLSL is not the same as OpenGL GLSL.

Note that CUGL OpenGL shaders should not start with a #version directive. That information will be appended by this class, as the correct version depends on the platform (OpenGL or OpenGLES).

CUGL assumes that Vulkan shaders have already been coverted to bytecode via a SpirV compiler offline. CUGL does not include a SpirV compiler, as that can add up to 1 GB to the application footprint.

ShaderModules do not check source for validity. That is done at compilation time for either GraphicsShader or ComputeShader.

Note that while this class can theortically represent any stage in a graphics pipeline, CUGL only support the shader stages VERTEX, FRAGMENT, and COMPUTE.

Constructor & Destructor Documentation

◆ ShaderSource()

cugl::graphics::ShaderSource::ShaderSource ( )
inline

Creates a empty shader module with no source code

The stage is undefined. You must initialize this shader module to use it.

◆ ~ShaderSource()

cugl::graphics::ShaderSource::~ShaderSource ( )
inline

Deletes this shader module, disposing all resources.

Member Function Documentation

◆ alloc()

static std::shared_ptr< ShaderSource > cugl::graphics::ShaderSource::alloc ( ShaderStage  stage)
inlinestatic

Returns a newly allocated shader module with the given stage

The shader has no source. It expects that source to be loaded separately.

Parameters
stageThe shader stage
Returns
a newly allocated shader module with the given stage

◆ allocFromSource()

static std::shared_ptr< ShaderSource > cugl::graphics::ShaderSource::allocFromSource ( const std::shared_ptr< JsonValue > &  source)
inlinestatic

Returns a newly allocated shader module with the given JSON specificaton.

This initializer is designed to allow a shader module to load both an OpenGL shader and a Vulkan shader. It is the responsibility of the developer to ensure these shaders are compatible.

This initializer is typically passed to the object via an AssetManager. This JSON format supports the following attribute values:

 "stage":    One of "vertex", "fragment", or "compute"
 "opengl":   An OpenGL shader object
 "vulkan":   A Vulkan shader object

Shader objects have exactly one of the two attributes

 "file":     The path to the shader code
 "source":   The shader source embedded in the JSON object

The shader source will be a string for both OpenGL and Vulkan shaders. In the case of Vulkan, this string will be a Base 64 encoding of the byte code

https://en.wikipedia.org/wiki/Base64

Parameters
sourceThe JSON object specifying the shader source
Returns
a newly allocated shader module with the given JSON specificaton.

◆ allocOpenGL()

static std::shared_ptr< ShaderSource > cugl::graphics::ShaderSource::allocOpenGL ( const std::string  source,
ShaderStage  stage 
)
inlinestatic

Returns a newly allocated OpenGL shader module with the given source

The string should be the source of a GLSL shader, but it should not start with a #version directive. That information will be appened to the string as it depends on the platform (OpenGL or OpenGLES).

The Vulkan shader will be blank until it is set.

Parameters
sourceThe shader source
stageThe shader stage
Returns
a newly allocated OpenGL shader module with the given source

◆ allocOpenGLFromFile()

static std::shared_ptr< ShaderSource > cugl::graphics::ShaderSource::allocOpenGLFromFile ( const std::string  file,
ShaderStage  stage 
)
inlinestatic

Returns a newly allocated OpenGL shader module from the given file.

The file should contain the source of a GLSL shader, but it should not start with a #version directive. That information will be appened to the string as it depends on the platform (OpenGL or OpenGLES).

The Vulkan shader will be blank until it is set.

Parameters
fileThe shader source
stageThe shader stage
Returns
a newly allocated OpenGL shader module from the given file.

◆ allocVulkan() [1/2]

static std::shared_ptr< ShaderSource > cugl::graphics::ShaderSource::allocVulkan ( const std::vector< std::byte > &  bytes,
ShaderStage  stage 
)
inlinestatic

Returns a newly allocated Vulkan shader module with the given bytecode

The Vulkan shader will be blank until it is set.

Parameters
bytesThe shader bytecode
stageThe shader stage
Returns
a newly allocated Vulkan shader module with the given bytecode

◆ allocVulkan() [2/2]

static std::shared_ptr< ShaderSource > cugl::graphics::ShaderSource::allocVulkan ( std::vector< std::byte > &&  bytes,
ShaderStage  stage 
)
inlinestatic

Returns a newly allocated Vulkan shader module with the given bytecode

This method will take ownership of the given bytecode. The Vulkan shader will be blank until it is set.

Parameters
bytesThe shader bytecode
stageThe shader stage
Returns
a newly allocated Vulkan shader module with the given bytecode

◆ allocVulkanFromFile()

static std::shared_ptr< ShaderSource > cugl::graphics::ShaderSource::allocVulkanFromFile ( const std::string  file,
ShaderStage  stage 
)
inlinestatic

Returns a newly allocated Vulkan shader module from the given file.

The file should contain the bytecode of a Vulkan shader. The OpenGL shader will be blank until it is set.

Parameters
fileThe shader source
stageThe shader stage
Returns
a newly allocated Vulkan shader module from the given file.

◆ dispose()

void cugl::graphics::ShaderSource::dispose ( )
inline

Disposes the shader module, freeing all resources.

You must reinitialize the shader module to use it.

◆ getOpenGL()

const std::string cugl::graphics::ShaderSource::getOpenGL ( ) const
inline

Returns the OpenGL source for this shader module

The returned string will include a #version directive chosen for the current platform (OpenGL or OpenGLES).

Returns
the OpenGL source for this shader module

◆ getStage()

ShaderStage cugl::graphics::ShaderSource::getStage ( ) const
inline

Returns the shader stage for this module.

CUGL only supports the stages VERTEX, FRAGMENT, and COMPUTE.

Returns
the shader stage for this module.

◆ getVulkan()

const std::vector< std::byte > & cugl::graphics::ShaderSource::getVulkan ( ) const
inline

Returns the Vulkan bytecode for this shader module

Returns
the Vulkan bytecode for this shader module

◆ init()

bool cugl::graphics::ShaderSource::init ( ShaderStage  stage)

Initializes a shader module with the given stage

The shader has no source. It expects that source to be loaded separately.

Parameters
stageThe shader stage
Returns
true if initialization was successful

◆ initFromSource()

bool cugl::graphics::ShaderSource::initFromSource ( const std::shared_ptr< JsonValue > &  source)

Initializes a shader module with the given JSON specificaton.

This initializer is designed to allow a shader module to load both an OpenGL shader and a Vulkan shader. It is the responsibility of the developer to ensure these shaders are compatible.

This initializer is typically passed to the object via an AssetManager. This JSON format supports the following attribute values:

 "stage":    One of "vertex", "fragment", or "compute"
 "opengl":   An OpenGL shader object
 "vulkan":   A Vulkan shader object

Shader objects have exactly one of the two attributes

 "file":     The path to the shader code
 "source":   The shader source embedded in the JSON object

The shader source will be a string for both OpenGL and Vulkan shaders. In the case of Vulkan, this string will be a Base 64 encoding of the byte code

https://en.wikipedia.org/wiki/Base64

Parameters
sourceThe JSON object specifying the shader
Returns
true if initialization was successful.

◆ initOpenGL()

bool cugl::graphics::ShaderSource::initOpenGL ( const std::string  source,
ShaderStage  stage 
)

Initializes an OpenGL shader module with the given source

The string should be the source of a GLSL shader, but it should not start with a #version directive. That information will be appened to the string as it depends on the platform (OpenGL or OpenGLES).

The Vulkan shader will be blank until it is set.

Parameters
sourceThe shader source
stageThe shader stage
Returns
true if initialization was successful

◆ initOpenGLFromFile()

bool cugl::graphics::ShaderSource::initOpenGLFromFile ( const std::string  file,
ShaderStage  stage 
)

Initializes an OpenGL shader module from the given file.

The file should contain the source of a GLSL shader, but it should not start with a #version directive. That information will be appened to the string as it depends on the platform (OpenGL or OpenGLES).

The Vulkan shader will be blank until it is set.

Parameters
fileThe file with the shader source
stageThe shader stage
Returns
true if initialization was successful

◆ initVulkan() [1/3]

bool cugl::graphics::ShaderSource::initVulkan ( const std::vector< std::byte > &  bytes,
ShaderStage  stage 
)

Initializes a Vulkan shader module with the given bytecode

The OpenGL shader will be blank until it is set.

Parameters
bytesThe shader bytecode
stageThe shader stage
Returns
true if initialization was successful

◆ initVulkan() [2/3]

bool cugl::graphics::ShaderSource::initVulkan ( std::string  source,
ShaderStage  stage 
)

Initializes a Vulkan shader module with the given source

The string for the Vulkan shader is assumed to be a Base 64 encoding of the byte code

https://en.wikipedia.org/wiki/Base64

The OpenGL shader will be blank until it is set.

Parameters
sourceThe shader source in Base 64
stageThe shader stage
Returns
true if initialization was successful

◆ initVulkan() [3/3]

bool cugl::graphics::ShaderSource::initVulkan ( std::vector< std::byte > &&  bytes,
ShaderStage  stage 
)

Initializes a Vulkan shader module with the given bytecode

This method will take ownership of the given bytecode. The OpenGL shader will be blank until it is set.

Parameters
bytesThe shader bytecode
stageThe shader stage
Returns
true if initialization was successful

◆ initVulkanFromFile()

bool cugl::graphics::ShaderSource::initVulkanFromFile ( const std::string  file,
ShaderStage  stage 
)

Initializes a Vulkan shader module from the given file.

The file should contain the bytecode of a Vulkan shader in binary format. The OpenGL shader will be blank until it is set.

Parameters
fileThe file with the shader source
stageThe shader stage
Returns
true if initialization was successful

◆ loadOpenGL()

bool cugl::graphics::ShaderSource::loadOpenGL ( const std::string  file)

Loads an OpenGL shader from the given file.

The file should contain the source of a GLSL shader, but it should not start with a #version directive. That information will be appened to the string as it depends on the platform (OpenGL or OpenGLES).

Parameters
fileThe file with the shader source
Returns
true if the file could be read

◆ loadVulkan()

bool cugl::graphics::ShaderSource::loadVulkan ( const std::string  file)

Loads a Vulkan shader from the given file.

The file should contain the bytecode of a Vulkan shader in binary format.

Parameters
fileThe file with the shader source
Returns
true if the file could be read

◆ setOpenGL()

void cugl::graphics::ShaderSource::setOpenGL ( const std::string  source)

Sets the OpenGL source for this shader module

The string should be the source of a GLSL shader, but it should not start with a #version directive. That information will be appened to the string as it depends on the platform (OpenGL or OpenGLES).

Parameters
sourceThe shader source

◆ setStage()

void cugl::graphics::ShaderSource::setStage ( ShaderStage  stage)
inline

Sets the shader stage for this module.

CUGL only supports the stages VERTEX, FRAGMENT, and COMPUTE.

Parameters
stageThe shader stage for this module.

◆ setVulkan() [1/3]

void cugl::graphics::ShaderSource::setVulkan ( const std::string  source)

Sets the Vulkan bytecode for this shader module

The string for the Vulkan shader is assumed to be a Base 64 encoding of the byte code

https://en.wikipedia.org/wiki/Base64

Parameters
sourceThe shader source in Base 64

◆ setVulkan() [2/3]

void cugl::graphics::ShaderSource::setVulkan ( const std::vector< std::byte > &  bytes)

Sets the Vulkan bytecode for this shader module

Parameters
bytesThe shader bytecode

◆ setVulkan() [3/3]

void cugl::graphics::ShaderSource::setVulkan ( std::vector< std::byte > &&  bytes)

Sets the Vulkan bytecode for this shader module

This method will take ownership of the given bytecode.

Parameters
bytesThe shader bytecode

The documentation for this class was generated from the following file: