OGRE-Next
4.0.0unstable
Object-Oriented Graphics Rendering Engine
|
Utility class to cache PSOs. More...
#include <OgrePsoCacheHelper.h>
Public Member Functions | |
PsoCacheHelper (RenderSystem *renderSystem) | |
~PsoCacheHelper () | |
void | clearState () |
HlmsPso * | getPso () |
Returns an HlmsPso you can set via renderSystem->_setPipelineStateObject based on all past calls to setRenderTarget + setVertexFormat + setMacroblock + etc. More... | |
HlmsPso * | getPso (uint32 renderableHash, bool renderableCacheAlreadySet=false) |
Returns an HlmsPso you can set via renderSystem->_setPipelineStateObject based on the input hash calculated via getRenderableHash. More... | |
uint32 | getRenderableHash () |
Returns a hash value you can cache into a Renderable (or whatever you're rendering). More... | |
void | setBlendblock (const HlmsBlendblock *blendblock) |
Calls to this function cannot be skipped unless you have a renderableHash. More... | |
void | setMacroblock (const HlmsMacroblock *macroblock) |
Calls to this function cannot be skipped unless you have a renderableHash. More... | |
void | setPixelShader (GpuProgramPtr &shader) |
Calls to this function can be skipped if it's valid to not have a shader set at this stage, or if you have a renderableHash. More... | |
void | setRenderTarget (const RenderPassDescriptor *renderPassDesc) |
You must call this function every frame, and every time the RenderTarget changes. More... | |
void | setVertexFormat (const VertexElement2VecVec &vertexElements, OperationType operationType, bool enablePrimitiveRestart) |
This function can be skipped if no vertex buffer is used (e.g. More... | |
void | setVertexShader (GpuProgramPtr &shader) |
Calls to this function can be skipped if it's valid to not have a shader set at this stage, or if you have a renderableHash. More... | |
Utility class to cache PSOs.
Useful for porting v1 libraries (eg. Gorilla) that render in "immediate mode" style and weren't build with PSOs in mind. Ideally a PSO would be created ahead of time and its pointer stored alongside the renderable you need to render.
void render() { psoCache.clearState(); psoCache.setRenderTarget( renderTarget ); for( int i=0; i<numMaterials; ++i ) { psoCache.setMacroblock( material[i].macroblock ); psoCache.setBlendblock( material[i].blendblock ); psoCache.setVertexShader( material[i].vertexShader ); psoCache.setPixelShader( material[i].pixelShader ); for( int j=0; j<numThingsToRenderPerMaterial; ++j ) { v1::RenderOperation renderOp = renderables[j].renderOp; Consider caching 'vertexElements' somewhere as convertToV2 involves allocations VertexElement2VecVec vertexElements = renderOp.vertexData-> vertexDeclaration->convertToV2(); psoCache.setVertexFormat( vertexElements, renderOp.operationType, enablePrimitiveRestart );
HlmsPso *pso = psoCache.getPso(); renderSystem->_setPipelineStateObject( pso ); } } }
Usage "MUCH better": PsoCacheHelper psoCache( renderSystem ); //Save this variable (i.e. per class)
Outside rendering, just ONCE at loading time or when the material changes (or the vertex layout changes): psoCache.clearState(); psoCache.setMacroblock( material[i].macroblock ); psoCache.setBlendblock( material[i].blendblock ); psoCache.setVertexShader( material[i].vertexShader ); psoCache.setPixelShader( material[i].pixelShader ); for( int j=0; j<numThingsToRenderPerMaterial; ++j ) { v1::RenderOperation renderOp = renderables[j].renderOp; Consider caching 'vertexElements' somewhere as convertToV2 involves allocations VertexElement2VecVec vertexElements = renderOp.vertexData-> vertexDeclaration->convertToV2(); psoCache.setVertexFormat( vertexElements, renderOp.operationType, enablePrimitiveRestart );
renderables[j].customSavedValue = psoCache.getRenderableHash(); }
During render: void render() { psoCache.clearState(); setRenderTarget still needs to be called. psoCache.setRenderTarget( renderTarget ); for( int i=0; i<numMaterials; ++i ) { for( int j=0; j<numThingsToRenderPerMaterial; ++j ) { HlmsPso *pso = psoCache.getPso( renderables[j].customSavedValue ); renderSystem->_setPipelineStateObject( pso ); } } }
Ogre::PsoCacheHelper::PsoCacheHelper | ( | RenderSystem * | renderSystem | ) |
Ogre::PsoCacheHelper::~PsoCacheHelper | ( | ) |
void Ogre::PsoCacheHelper::clearState | ( | ) |
HlmsPso* Ogre::PsoCacheHelper::getPso | ( | ) |
Returns an HlmsPso you can set via renderSystem->_setPipelineStateObject based on all past calls to setRenderTarget + setVertexFormat + setMacroblock + etc.
Use this version if for architecture reasons (i.e. legacy code, time constraints) you can't store the hash at loading time into the Renderable for later reuse.
HlmsPso* Ogre::PsoCacheHelper::getPso | ( | uint32 | renderableHash, |
bool | renderableCacheAlreadySet = false |
||
) |
Returns an HlmsPso you can set via renderSystem->_setPipelineStateObject based on the input hash calculated via getRenderableHash.
renderableHash | The hash obtained from getRenderableHash which you should've saved in the Renderable. |
renderableCacheAlreadySet | Internal parameter. Leave the default. Used by the other getPso overload to tell there is no need to perform a linear O(N) search in mRenderableCache because mCurrentState is up to date (this search only happens if the PSO hadn't been already cached). |
uint32 Ogre::PsoCacheHelper::getRenderableHash | ( | ) |
Returns a hash value you can cache into a Renderable (or whatever you're rendering).
This hash will contain VertexFormat, Macroblock, Blendblock information and shaders set, but nothing that is part of the pass (RenderTargets attached, RTT formats, stencil parameters, etc). You would later insert this value to getPso( renderableHash ); See examples in the class description.
void Ogre::PsoCacheHelper::setBlendblock | ( | const HlmsBlendblock * | blendblock | ) |
Calls to this function cannot be skipped unless you have a renderableHash.
void Ogre::PsoCacheHelper::setMacroblock | ( | const HlmsMacroblock * | macroblock | ) |
Calls to this function cannot be skipped unless you have a renderableHash.
void Ogre::PsoCacheHelper::setPixelShader | ( | GpuProgramPtr & | shader | ) |
Calls to this function can be skipped if it's valid to not have a shader set at this stage, or if you have a renderableHash.
void Ogre::PsoCacheHelper::setRenderTarget | ( | const RenderPassDescriptor * | renderPassDesc | ) |
You must call this function every frame, and every time the RenderTarget changes.
This function is a PASS state changing function, and its information is not part of getRenderableHash.
void Ogre::PsoCacheHelper::setVertexFormat | ( | const VertexElement2VecVec & | vertexElements, |
OperationType | operationType, | ||
bool | enablePrimitiveRestart | ||
) |
This function can be skipped if no vertex buffer is used (e.g.
you use gl_VertexID or other trickery) or if you have a renderableHash.
void Ogre::PsoCacheHelper::setVertexShader | ( | GpuProgramPtr & | shader | ) |
Calls to this function can be skipped if it's valid to not have a shader set at this stage, or if you have a renderableHash.