![]() |
CUGL 4.0
Cornell University Game Library
|
#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< ShaderSource > | alloc (ShaderStage stage) |
| static std::shared_ptr< ShaderSource > | allocOpenGL (const std::string source, ShaderStage stage) |
| static std::shared_ptr< ShaderSource > | allocOpenGLFromFile (const std::string file, ShaderStage stage) |
| static std::shared_ptr< ShaderSource > | allocVulkan (const std::vector< std::byte > &bytes, ShaderStage stage) |
| static std::shared_ptr< ShaderSource > | allocVulkan (std::vector< std::byte > &&bytes, ShaderStage stage) |
| static std::shared_ptr< ShaderSource > | allocVulkanFromFile (const std::string file, ShaderStage stage) |
| static std::shared_ptr< ShaderSource > | allocFromSource (const std::shared_ptr< JsonValue > &source) |
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.
|
inline |
Creates a empty shader module with no source code
The stage is undefined. You must initialize this shader module to use it.
|
inline |
Deletes this shader module, disposing all resources.
|
inlinestatic |
Returns a newly allocated shader module with the given stage
The shader has no source. It expects that source to be loaded separately.
| stage | The shader stage |
|
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
| source | The JSON object specifying the shader source |
|
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.
| source | The shader source |
| stage | The shader 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.
| file | The shader source |
| stage | The shader stage |
|
inlinestatic |
Returns a newly allocated Vulkan shader module with the given bytecode
The Vulkan shader will be blank until it is set.
| bytes | The shader bytecode |
| stage | The shader 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.
| bytes | The shader bytecode |
| stage | The shader 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.
| file | The shader source |
| stage | The shader stage |
|
inline |
Disposes the shader module, freeing all resources.
You must reinitialize the shader module to use it.
|
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).
|
inline |
Returns the shader stage for this module.
CUGL only supports the stages VERTEX, FRAGMENT, and COMPUTE.
|
inline |
Returns the Vulkan bytecode for this shader module
| 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.
| stage | The shader stage |
| 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
| source | The JSON object specifying the shader |
| 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.
| source | The shader source |
| stage | The shader stage |
| 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.
| file | The file with the shader source |
| stage | The shader stage |
| 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.
| bytes | The shader bytecode |
| stage | The shader stage |
| 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.
| source | The shader source in Base 64 |
| stage | The shader stage |
| 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.
| bytes | The shader bytecode |
| stage | The shader stage |
| 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.
| file | The file with the shader source |
| stage | The shader stage |
| 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).
| file | The file with the shader source |
| 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.
| file | The file with the shader source |
| 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).
| source | The shader source |
|
inline |
Sets the shader stage for this module.
CUGL only supports the stages VERTEX, FRAGMENT, and COMPUTE.
| stage | The shader stage for this module. |
| 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
| source | The shader source in Base 64 |
| void cugl::graphics::ShaderSource::setVulkan | ( | const std::vector< std::byte > & | bytes | ) |
Sets the Vulkan bytecode for this shader module
| bytes | The shader bytecode |
| 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.
| bytes | The shader bytecode |