custom shader use in 2.1

A place for users of OGRE to discuss ideas and experiences of utilitising OGRE in their games / demos / applications.
Post Reply
Posts: 11
Joined: Sun Sep 09, 2018 12:59 am

custom shader use in 2.1

Post by nqdev » Sat Oct 05, 2019 11:58 pm

I have a wireframe and am trying to render it with solid surfaces by using custom shaders. I'm having a hard time integrating a basic shader into Ogre. Here's what I've done.

1. I have created and registered Ogre::HlmsUnlit and Ogre::HlmsPbs. Have a working colored wireframe.
2. I have this path in resources2.cfg.

Code: Select all

3. The file, VertexShader_vs.glsl is placed into this directory.

Code: Select all

and has the following contents.

Code: Select all

#version 450 core

layout (location = 0) in vec3 aPosition;

out vec4 vertexColor;

void main()
    gl_Position = vec4(aPosition, 1.0);
    vertexColor = vec4(0.5, 0.0, 0.0, 1.0);
I'm also working with a basic fragment shader, following a few OpenGL tutorials. To render the wireframe, an HlmsUnlit is created, then create an HlmsMacroblock and an HlmsUnlitDatablock, and then setDatablock to the Item. However, I'm receiving this error when running the application after having swapped out the VertexShader_vs.glsl that comes with Ogre.

Code: Select all

terminate called after throwing an instance of 'Ogre::ItemIdentityException'
  what():  OGRE EXCEPTION(5:ItemIdentityException): Parameter called worldMatBuf does not exist.  in GpuProgramParameters::_findNamedConstantDefinition at /home/dev/tools/ogre-v2-1/OgreMain/src/OgreGpuProgramParams.cpp (line 2218)
Not sure how much of what's needed of the current shaders as I did copy over what was in Samples/Media/Hlms/* and it appears to still be looking for something. I was hoping to strip things down and only include exactly what I am using.

Can anyone clarify how much of this is correct?
0 x

User avatar
OGRE Team Member
OGRE Team Member
Posts: 4177
Joined: Sat Jul 21, 2007 4:55 pm
Location: Buenos Aires, Argentina
x 280

Re: custom shader use in 2.1

Post by dark_sylinc » Mon Oct 07, 2019 2:23 am


When writing your own custom shader implementations you can do two things:
  1. Start one from scratch
  2. Modify an existing one (whether at script level and/or C++ level)
You attempted to modify an existing one, thus the code you're based on expects a few things from the shader code, thus if you remove it without altering the C++, it's going to crash.

The particular piece of code that is complaining is because HlmsUnlit::createShaderCacheEntry will set the constant 'worldMatBuf' to 0; which is the value of the shader binding slot from where we send world matrices.

The call to setNamedConstant will throw if there is no such parameter found.
Thus you either:
  1. Add back "uniform samplerBuffer worldMatBuf;" to VertexShader_vs.glsl so that it can be found, even if you don't use it
  2. Overload HlmsUnlit::createShaderCacheEntry with your custom code, and avoid the setNamedConstant call.
Most of the non-beginner-friendly code you'll find from Hlms implementations actually stems from that key piece of code: In order to efficiently draw lots of objects on screen, we copy all world matrices into a buffer and bind the correct offsets during runtime; and that requires a lot of memory management.

If we take away that, then an empty implementation that derives from 'Hlms' and overloads the virtual functions required to build could be enough to compile the kind of shader you seem to be writing.

i.e. the shader you posted from the tutorial will copy the vertex position "as is" to the GPU, without taking camera movement, rotation or object position/orientation/scale.
For that to work, you need to apply a world_view_projection matrix.

0 x

Post Reply