All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
kanzi::Shader Class Reference

Shader GPU resource. More...

#include <kanzi/graphics_3d/shader.hpp>

Inheritance diagram for kanzi::Shader:
kanzi::GPUResource kanzi::Resource kanzi::Object

Classes

struct  BinaryProgramData
 
struct  BinaryShadersCombinedData
 
struct  BinaryShadersSeparateData
 
class  DeployContext
 
struct  ShaderData
 
struct  ShaderSourceCodeData
 

Public Types

enum  FixedUniform {
  FixedUniformProjectionCameraWorldMatrix, FixedUniformWorldMatrix, FixedUniformProjectionMatrix, FixedUniformCameraMatrix,
  FixedUniformCameraPosition, FixedUniformViewport, FixedUniformNormalMatrix, FixedUniformCameraNormalMatrix,
  FixedUniformCameraWorldMatrix, FixedUniformNearFarPlane, FixedUniformTime, FixedUniformMorphWeights,
  FixedUniformMatrixPalette, FixedUniformDirectionalLightColor, FixedUniformPointLightColor, FixedUniformSpotLightColor,
  FixedUniformPointLightPosition, FixedUniformSpotLightPosition, FixedUniformDirectionalLightDirection, FixedUniformSpotLightDirection,
  FixedUniformPointLightRadius, FixedUniformPointLightAttenuation, FixedUniformSpotLightAttenuation, FixedUniformSpotLightCutoffAngle,
  FixedUniformSpotLightExponent, FixedUniformCount, FixedUniformInvalid
}
 Fixed uniform locations of the shader. More...
 
typedef vector< const
KzuPropertyType * > 
PropertyTypeContainer
 
typedef vector< int > UniformLocationContainer
 

Public Member Functions

 Shader (Domain *domain, string_view name="")
 
virtual ~Shader () KZ_OVERRIDE
 
virtual bool isRenderable () const
 Returns true if the shader is deployed and valid. More...
 
void addPropertyType (const KzuPropertyType *propertyType)
 Adds property type to shader. More...
 
bool hasPropertyType (const KzuPropertyType *propertyType) const
 Tell if shader has a property type. More...
 
void removePropertyType (const KzuPropertyType *propertyType)
 Removes property type from shader. More...
 
const KzuPropertyTypegetPropertyTypeByName (kzString propertyTypeName) const
 Gets property type from shader by name. Returns 0 if not found. More...
 
PropertyTypeContainer::const_iterator beginPropertyTypes () const
 
PropertyTypeContainer::const_iterator endPropertyTypes () const
 
unsigned int getProgramHandle () const
 Get shader program handle. More...
 
const vector< string > & getAttributes () const
 
int getAttributeIndex (const string &attributeName) const
 Get location of attribute in Shader from attribute name. More...
 
void bind () const
 Binds the shader for rendering. More...
 
bool hasSamplersWithoutProperties () const
 Return true if shader has samplers that have no corresponding texture property. More...
 
void getTextureUniformLocation (unsigned int textureUnit, int *out_textureWidthUniformLocation, int *out_textureHeightUniformLocation)
 Returns texture size uniform locations from shader. More...
 
void clearTextureUniformLocations ()
 Clear texture uniform location array. More...
 
int getUniformLocation (unsigned int propertyIndex)
 Get uniform location in Shader with property index. More...
 
void initializeFromMemory (const char *const vertexShaderSourceSource, const char *const fragmentShaderSourceCode, unsigned int attributeCount, const char *const *attributes)
 Prepare Shader by providing vertex and fragment shader sources. More...
 
void initializeFromMemory (unsigned int shaderBinaryFormat, unsigned int dataSize, void *data, unsigned int attributeCount, const char *const *attributes)
 Prepare Shader by providing precompiled binary shader data. More...
 
void initializeFromMemory (unsigned int shaderBinaryFormat, unsigned int vertexDataSize, void *vertexData, unsigned int fragmentDataSize, void *fragmentData, unsigned int attributeCount, const char *const *attributes)
 Prepare Shader by providing precompiled binary shader data. More...
 
void setStrictWarningLogged ()
 
bool getStrictWarningLogged () const
 
unsigned int getLightCount (Light::Type lightType) const
 
virtual void loadFromKZB (const KzuResourceLoaderThreadContext *threadContext, KzcInputStream *inputStream, const KzuBinaryFileInfo *file) KZ_OVERRIDE
 Function for loading the resource from .KZB. More...
 
virtual kzUint getCPUMemoryUsage () const KZ_OVERRIDE
 Function for getting the memory usage of a resource. More...
 
virtual void unloadOverride () KZ_OVERRIDE
 Unload function for resource. More...
 
- Public Member Functions inherited from kanzi::GPUResource
virtual ~GPUResource () KZ_OVERRIDE
 
void deploy ()
 Make graphics API object(s) in the resource usable by preparing GPU objects and data in them. More...
 
void undeploy ()
 Free the GPU objects. After this, the resource is not usable for graphics API until deploy() is called again. More...
 
void invalidate ()
 Notify resource that graphics context was lost and all GPU resources are invalid. More...
 
unsigned int getGPUMemoryUsage () const
 
RenderergetRenderer () const
 
bool getDeployOnLoad () const
 Returns true if GPUResource is automatically deployed when loaded. More...
 
void setDeployOnLoad (bool value)
 Sets GPUResource automatic deployment on loade on or orr. More...
 
bool isDeployed () const
 
void deployed ()
 
void undeployed ()
 
virtual void finishLoadingOverride (const KzuResourceLoaderThreadContext *threadContext) KZ_OVERRIDE
 Function for finishing the loading of the resource. More...
 
- Public Member Functions inherited from kanzi::Resource
KzcMemoryManagergetMemoryManager () const
 
 Resource (Domain *domain, string_view name)
 
virtual ~Resource ()
 
void setKZB (const KzuBinaryDirectory *binaryDirectory, kzString path)
 
void setLoaded ()
 
kzString getName () const
 Accessor. More...
 
void load ()
 
void unload ()
 
bool isLoaded () const
 
void kzuResourceLoad_private (const KzuResourceLoaderThreadContext *threadContext)
 Private function for kzuResourceLoad() with specified thread context. More...
 
void kzuResourceFinishLoading_private (const KzuResourceLoaderThreadContext *threadContext)
 Private function for finishing the loading of the resource. More...
 
KzuResourceLoadingStrategy getLoadingStrategy () const
 
void setLoadingStrategy (KzuResourceLoadingStrategy loadingStrategy)
 
bool getKeepAlive () const
 Tells if the resource has keep alive flag set. More...
 
void setKeepAlive (bool keepAlive)
 Sets the keep alive flag. More...
 
- Public Member Functions inherited from kanzi::Object
 Object (Domain *domain)
 
virtual ~Object ()
 
DomaingetDomain () const
 Returns the domain the object belongs to. More...
 
KzuPropertyManager * getPropertyManager () const
 Returns the property manager of the object. More...
 
KzuTaskSchedulergetTaskScheduler () const
 Returns the task scheduler of the object. More...
 
KzuMessageDispatchergetMessageDispatcher () const
 Returns the message dispatcher of the object. More...
 
ResourceManagergetResourceManager () const
 Returns the resource manager of the object. More...
 
virtual const MetaclassgetDynamicMetaclass () const
 Returns the metaclass of the dynamic type of the object. More...
 
bool isTypeOf (const Metaclass *objectType) const
 Determines if the type of this object is the given type or derived from it. More...
 
template<typename DataType >
void setProperty (const PropertyType< DataType > &propertyType, typename PropertyType< DataType >::DataType value)
 Sets the local value of a property. More...
 
void setProperty (const PropertyType< ResourceID > &propertyType, ResourceSharedPtr value)
 Sets the local value of a resource id property with a resource pointer. More...
 
template<typename DataType >
DataType getProperty (const PropertyType< DataType > &propertyType) const
 Returns the current value of a property. More...
 
template<typename DataType >
bool getProperty (const PropertyType< DataType > &propertyType, typename PropertyType< DataType >::DataType &value) const
 Evaluates the property value in the same way as the overload above but does not default to the value in property metadata if there are no inputs to the property value. More...
 
template<typename DataType >
void setAbstractProperty (AbstractPropertyType abstractPropertyType, typename PropertyType< DataType >::DataType value)
 
void setAbstractProperty (AbstractPropertyType abstractPropertyType, ResourceSharedPtr value)
 
template<typename DataType >
DataType getAbstractProperty (AbstractPropertyType abstractPropertyType) const
 
template<typename DataType >
bool getAbstractProperty (AbstractPropertyType abstractPropertyType, typename PropertyType< DataType >::DataType &value) const
 
bool hasValue (AbstractPropertyType propertyType) const
 Evaluates whether there are any inputs into the property value. More...
 
bool hasLocalValue (AbstractPropertyType propertyType) const
 Evaluates whether there is a local value set for the property. More...
 
void removeLocalValue (AbstractPropertyType propertyType)
 Removes the local value associated with the property. More...
 
void copyLocalValues (const Object &other)
 Copies all local values from another object. More...
 
virtual void onPropertyChanged (AbstractPropertyType propertyType, KzuPropertyNotificationReason reason)
 Virtual function to handle property change notifications. More...
 
AppliedStyleEntryapplyObjectStyle (kanzi::StyleSharedPtr style)
 Applies a style to an object. More...
 
void unapplyObjectStyle (AppliedStyleEntry *appliedStyleEntry)
 
void applyObjectStyles ()
 Apply all styles for an object node. More...
 
void unapplyObjectStyles ()
 Unapplies and removes all applied styles. More...
 

Static Public Member Functions

static ShaderSharedPtr create (Domain *domain, string_view name="")
 
- Static Public Member Functions inherited from kanzi::Object
static const MetaclassgetStaticMetaclass ()
 Returns the metaclass of Object class. More...
 
static
PropertyTypeEditorInfoSharedPtr 
makeEditorInfo ()
 Default implementation that returns empty editor info. More...
 

Protected Types

enum  ProgramType {
  ProgramTypeNotDeployed, ProgramTypeShaderSources, ProgramTypeBinaryShadersSeparate, ProgramTypeBinaryShadesCombined,
  ProgramTypeBinaryProgram
}
 Describes state of program after tryDeploy() More...
 
typedef array< int,
VertexAttribute::SemanticCount
FixedAttributeArray
 
- Protected Types inherited from kanzi::Object
typedef vector
< AppliedStyleEntry * > 
AppliedStyleContainer
 Applied style container. More...
 

Protected Member Functions

virtual void deployOverride () KZ_OVERRIDE
 Make graphics API object(s) in the resource usable by preparing GPU objects and data in them. Called by deploy() More...
 
virtual void invalidateOverride () KZ_OVERRIDE
 Notify resource that graphics context was lost and all GPU resources are invalid. Called by invalidate() More...
 
virtual void undeployOverride () KZ_OVERRIDE
 Free the GPU objects. After this, the resource is not usable for graphics API until deploy() is called again. Called by undeploy() More...
 
virtual void freeCommonData () KZ_OVERRIDE
 Notify resource that graphics context was lost and all GPU resources are invalid. More...
 
void setAttributes (unsigned int attributeCount, const char *const *attributes)
 
ProgramType tryDeploy (const GLProgramHandle &programHandle)
 Tries to deploy the program. More...
 
void findDefaultSamplers ()
 Locate samplers in program which are missing property that provides value for them. More...
 
void locateFixedUniforms ()
 Locate uniforms in program which Kanzi engine provides values directly. More...
 
void locateTextureUniforms ()
 Locate texture related uniforms in program. More...
 
void locatePropertyUniforms ()
 Locate uniforms in program for properties. More...
 
- Protected Member Functions inherited from kanzi::GPUResource
 GPUResource (Domain *domain, string_view name="")
 
virtual unsigned int getGPUMemoryUsageOverride () const
 
- Protected Member Functions inherited from kanzi::Resource
KzuResourceLoadState getLoadState ()
 Access. More...
 
- Protected Member Functions inherited from kanzi::Object
void initialize ()
 
void onCopy (const Object &other)
 

Static Protected Member Functions

static void locateFixedAttributes (vector< string > const &attributes, FixedAttributeArray &outFixedAttributes)
 
static const char * toString (Shader::ProgramType type)
 Returns human readable string for ProgramType. More...
 
static unsigned int getShaderBinaryFormat (string_view format)
 Translate string to GL shader binary format. More...
 
static unsigned int getProgramBinaryFormat (string_view format)
 Translate string to GL program binary format. More...
 

Additional Inherited Members

- Protected Attributes inherited from kanzi::Object
AppliedStyleContainer m_appliedStyles
 Listing of applied styles applied to this object. More...
 

Detailed Description

Shader GPU resource.

Member Typedef Documentation

Member Enumeration Documentation

Fixed uniform locations of the shader.

Enumerator
FixedUniformProjectionCameraWorldMatrix 

Projection * camera * world matrix.

FixedUniformWorldMatrix 

World matrix.

FixedUniformProjectionMatrix 

Projection matrix.

FixedUniformCameraMatrix 

Camera matrix.

FixedUniformCameraPosition 

Camera position.

FixedUniformViewport 

Viewport area.

FixedUniformNormalMatrix 

Normal matrix.

FixedUniformCameraNormalMatrix 

Camera* normal matrix (screenspace normal).

FixedUniformCameraWorldMatrix 

Camera * world matrix.

FixedUniformNearFarPlane 

Vec2 containing near and far plane.

FixedUniformTime 

Debug timer.

FixedUniformMorphWeights 

Morph weights.

FixedUniformMatrixPalette 

Bone matrices.

FixedUniformDirectionalLightColor 
FixedUniformPointLightColor 
FixedUniformSpotLightColor 
FixedUniformPointLightPosition 
FixedUniformSpotLightPosition 
FixedUniformDirectionalLightDirection 
FixedUniformSpotLightDirection 
FixedUniformPointLightRadius 
FixedUniformPointLightAttenuation 
FixedUniformSpotLightAttenuation 
FixedUniformSpotLightCutoffAngle 
FixedUniformSpotLightExponent 
FixedUniformCount 

Number of fixed uniforms.

FixedUniformInvalid 

Describes state of program after tryDeploy()

Enumerator
ProgramTypeNotDeployed 

Program has not been deployed.

ProgramTypeShaderSources 

Program has been deployed from shader sources.

ProgramTypeBinaryShadersSeparate 

Program has been deployed from separate binary shaders.

ProgramTypeBinaryShadesCombined 

Program has been deployed from combined binary shaders.

ProgramTypeBinaryProgram 

Program has been deployed from program binary.

Constructor & Destructor Documentation

kanzi::Shader::Shader ( Domain domain,
string_view  name = "" 
)
explicit
virtual kanzi::Shader::~Shader ( )
virtual

Member Function Documentation

static ShaderSharedPtr kanzi::Shader::create ( Domain domain,
string_view  name = "" 
)
static
virtual bool kanzi::Shader::isRenderable ( ) const
virtual

Returns true if the shader is deployed and valid.

void kanzi::Shader::addPropertyType ( const KzuPropertyType propertyType)

Adds property type to shader.

bool kanzi::Shader::hasPropertyType ( const KzuPropertyType propertyType) const

Tell if shader has a property type.

Parameters
propertyTypeProperty type to check.
Returns
True if yes, false if no.
void kanzi::Shader::removePropertyType ( const KzuPropertyType propertyType)

Removes property type from shader.

const KzuPropertyType* kanzi::Shader::getPropertyTypeByName ( kzString  propertyTypeName) const

Gets property type from shader by name. Returns 0 if not found.

PropertyTypeContainer::const_iterator kanzi::Shader::beginPropertyTypes ( ) const
inline
PropertyTypeContainer::const_iterator kanzi::Shader::endPropertyTypes ( ) const
inline
unsigned int kanzi::Shader::getProgramHandle ( ) const

Get shader program handle.

Parameters
shaderShader to query.
Returns
Program handle.
const vector<string>& kanzi::Shader::getAttributes ( ) const
int kanzi::Shader::getAttributeIndex ( const string &  attributeName) const

Get location of attribute in Shader from attribute name.

Parameters
attributeNameName of attribute.
Returns
Location of attribute, or -1 if attribute was not found in Shader.
void kanzi::Shader::bind ( ) const

Binds the shader for rendering.

bool kanzi::Shader::hasSamplersWithoutProperties ( ) const

Return true if shader has samplers that have no corresponding texture property.

void kanzi::Shader::getTextureUniformLocation ( unsigned int  textureUnit,
int *  out_textureWidthUniformLocation,
int *  out_textureHeightUniformLocation 
)

Returns texture size uniform locations from shader.

void kanzi::Shader::clearTextureUniformLocations ( )

Clear texture uniform location array.

int kanzi::Shader::getUniformLocation ( unsigned int  propertyIndex)

Get uniform location in Shader with property index.

Parameters
propertyIndexIndex of property in Shader
Returns
Uniform location in Shader, or -1 if uniform was not found in Shader.
void kanzi::Shader::initializeFromMemory ( const char *const  vertexShaderSourceSource,
const char *const  fragmentShaderSourceCode,
unsigned int  attributeCount,
const char *const *  attributes 
)

Prepare Shader by providing vertex and fragment shader sources.

After Shader has been prepared with initializeFromMemory, the Shader can be deployed.

Parameters
vertexShaderSource code string for vertex shader.
fragmentShaderSource code string for fragment shader.
attributeCountNumber of attributes.
vertexAttributesArray of attribute names. Array length must equal to attributeCount.
void kanzi::Shader::initializeFromMemory ( unsigned int  shaderBinaryFormat,
unsigned int  dataSize,
void *  data,
unsigned int  attributeCount,
const char *const *  attributes 
)

Prepare Shader by providing precompiled binary shader data.

This version takes in a single data blob which specifies has been precompiled from both vertex and fragment shaders.

After Shader has been prepared with initializeFromMemory, the Shader can be deployed.

Note
Supported shader binary formats are vendor specific. Each vendor uses their own shader binary formats. Use Renderer::isGlShaderBinaryFormatSupported() to verify that shaderBinaryFormat is supported before using initializeFromMemory().
Parameters
shaderBinaryFormatGL shader binary format
dataSizeSize of shader binary data in bytes
dataPointer to shader binary data
attributeCountNumber of attributes.
vertexAttributesArray of attribute names. Array length must equal to attributeCount.
void kanzi::Shader::initializeFromMemory ( unsigned int  shaderBinaryFormat,
unsigned int  vertexDataSize,
void *  vertexData,
unsigned int  fragmentDataSize,
void *  fragmentData,
unsigned int  attributeCount,
const char *const *  attributes 
)

Prepare Shader by providing precompiled binary shader data.

This version takes in a two data blobs, one for vertex shader and another one for fragment shader.

After Shader has been prepared with initializeFromMemory, the Shader can be deployed.

Note
Supported shader binary formats are vendor specific. Each vendor uses their own shader binary formats. Use Renderer::isGlShaderBinaryFormatSupported() to verify that shaderBinaryFormat is supported before using initializeFromMemory().
Parameters
shaderBinaryFormatGL shader binary format
dataSizeSize of shader binary data in bytes
dataPointer to shader binary data
attributeCountNumber of attributes.
vertexAttributesArray of attribute names. Array length must equal to attributeCount.
void kanzi::Shader::setStrictWarningLogged ( )
inline
bool kanzi::Shader::getStrictWarningLogged ( ) const
inline
unsigned int kanzi::Shader::getLightCount ( Light::Type  lightType) const
inline
virtual void kanzi::Shader::loadFromKZB ( const KzuResourceLoaderThreadContext threadContext,
KzcInputStream inputStream,
const KzuBinaryFileInfo file 
)
virtual

Function for loading the resource from .KZB.

This function is called when loading the data from .KZB file. Implementations should call super class loadFromKZB before reading anything else from inputStream. This function is called from arbitrary loading threads. The parameter threadContext contains thread safe memory manager and a thread specific Property manager which can be locked with the threadLock in the threadContext. This function may not access resource manager or the UIDomain due to race conditions.

Reimplemented from kanzi::GPUResource.

virtual kzUint kanzi::Shader::getCPUMemoryUsage ( ) const
virtual

Function for getting the memory usage of a resource.

Reimplemented from kanzi::GPUResource.

virtual void kanzi::Shader::unloadOverride ( )
virtual

Unload function for resource.

Releases all memory taken by the resource except the resource loader.

Reimplemented from kanzi::GPUResource.

virtual void kanzi::Shader::deployOverride ( )
protectedvirtual

Make graphics API object(s) in the resource usable by preparing GPU objects and data in them. Called by deploy()

Reimplemented from kanzi::GPUResource.

virtual void kanzi::Shader::invalidateOverride ( )
protectedvirtual

Notify resource that graphics context was lost and all GPU resources are invalid. Called by invalidate()

Reimplemented from kanzi::GPUResource.

virtual void kanzi::Shader::undeployOverride ( )
protectedvirtual

Free the GPU objects. After this, the resource is not usable for graphics API until deploy() is called again. Called by undeploy()

Reimplemented from kanzi::GPUResource.

virtual void kanzi::Shader::freeCommonData ( )
protectedvirtual

Notify resource that graphics context was lost and all GPU resources are invalid.

Reimplemented from kanzi::GPUResource.

void kanzi::Shader::setAttributes ( unsigned int  attributeCount,
const char *const *  attributes 
)
protected
static void kanzi::Shader::locateFixedAttributes ( vector< string > const &  attributes,
FixedAttributeArray outFixedAttributes 
)
staticprotected
static const char* kanzi::Shader::toString ( Shader::ProgramType  type)
staticprotected

Returns human readable string for ProgramType.

ProgramType kanzi::Shader::tryDeploy ( const GLProgramHandle programHandle)
protected

Tries to deploy the program.

Returns
Status of program after attempted deployment
void kanzi::Shader::findDefaultSamplers ( )
protected

Locate samplers in program which are missing property that provides value for them.

void kanzi::Shader::locateFixedUniforms ( )
protected

Locate uniforms in program which Kanzi engine provides values directly.

void kanzi::Shader::locateTextureUniforms ( )
protected

Locate texture related uniforms in program.

void kanzi::Shader::locatePropertyUniforms ( )
protected

Locate uniforms in program for properties.

static unsigned int kanzi::Shader::getShaderBinaryFormat ( string_view  format)
staticprotected

Translate string to GL shader binary format.

static unsigned int kanzi::Shader::getProgramBinaryFormat ( string_view  format)
staticprotected

Translate string to GL program binary format.


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