How to know in advance whether a vertex program can be compiled or not

Problems building or running the engine, queries about how to use features etc.
assaber
Gnoblar
Posts: 7
Joined: Tue May 02, 2023 5:06 am
x 5

How to know in advance whether a vertex program can be compiled or not

Post by assaber »

Ogre Version: :?: 4.0.0
Operating System: :?: Windows10
Render System: :?: OpenGL 3+

guys, I'm trying to make a simple tool, and going to read the local file, load vertex program or fragment program, eventually pass them to a material by c++ code. Since I want the final app to be an editor, programs may be in an incomplete state (or even empty strings), I wonder if the program can compile successfully or if the material works before I use them by calling ‘setDatablockOrMaterialName’.

rpgplayerrobin
Orc Shaman
Posts: 719
Joined: Wed Mar 18, 2009 3:03 am
x 399

Re: How to know in advance whether a vertex program can be compiled or not

Post by rpgplayerrobin »

I don't know what Ogre version 4.0.0 is, but maybe it does not matter.

What have you tried so far? Have you tried to create a new program from code and input the code from your own editor there? Have you tried to compile that program then? In that case, what happens when it does not compile correctly? Do you get an error message? Can you catch the error message and handle that message yourself instead of the application exiting?

I think there are too many questions here that needs answers before anyone can help you.

assaber
Gnoblar
Posts: 7
Joined: Tue May 02, 2023 5:06 am
x 5

Re: How to know in advance whether a vertex program can be compiled or not

Post by assaber »

I'm sorry for the vagueness of my description. please give me a chance again.
'Ogre Version' I get it from OgrePrerequisites.h, OGRE_VERSION_MAJOR OGRE_VERSION_MINOR and OGRE_VERSION_PATCH are defined separately as: 4 0 0, It belongs to the ogre-next.
Back to my doubts, I know how to create a simple material with fragment program and vertex program, It looks like

Code: Select all

String customCasterMatVp = 
    "void customCasterVp(float4 position : POSITION,\n"
    "out float4 oPosition : POSITION,\n"
    "uniform float4x4 worldViewProj)\n"
    "{\n"
    "   oPosition = mul(worldViewProj, position);\n"
    "}\n";
String customCasterMatFp = 
    "void customCasterFp(\n"
    "out float4 oColor : COLOR)\n"
    "{\n"
    "   oColor = float4(1,1,0,1); // just a test\n"
    "}\n";

HighLevelGpuProgramPtr vp = HighLevelGpuProgramManager::getSingleton()
    .createProgram("CustomShadowCasterVp", 
        ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME, 
        "cg", GPT_VERTEX_PROGRAM);
vp->setSource(customCasterMatVp);
vp->setParameter("profiles", "vs_1_1 arbvp1");
vp->setParameter("entry_point", "customCasterVp");
vp->load();

HighLevelGpuProgramPtr fp = HighLevelGpuProgramManager::getSingleton()
    .createProgram("CustomShadowCasterFp", 
    ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME, 
    "cg", GPT_FRAGMENT_PROGRAM);
fp->setSource(customCasterMatFp);
fp->setParameter("profiles", "ps_1_1 arbfp1");
fp->setParameter("entry_point", "customCasterFp");
fp->load();

MaterialPtr mat = MaterialManager::getSingleton().create("CustomShadowCaster", 
    ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME);
Pass* p = mat->getTechnique(0)->getPass(0);
p->setVertexProgram("CustomShadowCasterVp");
p->getVertexProgramParameters()->setNamedAutoConstant(
    "worldViewProj", GpuProgramParameters::ACT_WORLDVIEWPROJ_MATRIX);
p->setFragmentProgram("CustomShadowCasterFp");

(copy from ogre-next's Tests)
But, I want to implement an editor, it can provide real-time feedback on the results of the program, if the program is wrong, the rendered item can also use the default material. But now...
Image
If I connect Simple Material's Output to Render Item's Material name (just call method OgreItem::setDatablockOrMaterialName, anything about material settings are done in the previous session), logs are as follows

Code: Select all

GLSL compile log: 2b3ed9b7-103b-4e14-87ba-c0f784667c35_vp
0(1) : error C0104: Unknown pre-processor directive #vers
OGRE EXCEPTION(3:RenderingAPIException): Vertex Program 2b3ed9b7-103b-4e14-87ba-c0f784667c35_vp failed to compile. See compile log above for details. in GLSLShader::compile at D:\code\Ogr...

I know that the program is incomplete, But I can't guarantee that everyone can hold back to perss 'ctrl + s' before entering a full line... like the error above, it will be discovered until call OgreRoot::renderOneFrame(specifically GLSLShader::compile). So, I'd like to ask if I can find out about this before that...

My Englist is poor, please forgive me if there is a grammatical problem QAQ

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

Re: How to know in advance whether a vertex program can be compiled or not

Post by dark_sylinc »

Hi!

The only way to know is to wrap it in a try/catch block:

Code: Select all

HighLevelGpuProgramPtr vp = HighLevelGpuProgramManager::getSingleton()
    .createProgram("CustomShadowCasterVp", 
        ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME, 
        "glsl", GPT_VERTEX_PROGRAM);
vp->setSource(customCasterMatVp);
try
{
    vp->load();
}
catch( Ogre::Exception &e )
{
    // Failed to compile
    printf( "%s", e.getFullDescription().c_str() ); // Print the error
}

Note that after the exception "CustomShadowCasterVp" will continue to exist until you destroy it; it will be in an unusable state because it failed to compile.

This doesn't cover every possible compiler error. When the object is rendered a PSO will be created (since at that time we know all the information such as linked vertex data, vertex shader, pixel shader) and it may fail to create the PSO.

You could wrap renderOneFrame into a try/catch block too; or try to render the object into a dumy offscreen Compositor Workspace and wrap the CompositorWorkspace::_update call into a try/catch block.

(copy from ogre-next's Tests)

I suspect you're using the really old Tests that are no longer being used. Someone suggested to remove them to avoid this sort of confussion.

Supported profiles are glsl (OpenGL), glslvk (Vulkan), hlsl (D3D11) and metal (Metal). Cg is no longer supported in OgreNext.

I suggest that you take a look at Hlms::compileShaderCode which creates shaders programmatically.

rpgplayerrobin
Orc Shaman
Posts: 719
Joined: Wed Mar 18, 2009 3:03 am
x 399

Re: How to know in advance whether a vertex program can be compiled or not

Post by rpgplayerrobin »

Note that after the exception "CustomShadowCasterVp" will continue to exist until you destroy it; it will be in an unusable state because it failed to compile.

I guess the right way to remove it is then to do something like this:

Code: Select all

HighLevelGpuProgramManager::getSingleton().unload(vp);
HighLevelGpuProgramManager::getSingleton().remove(vp);
vp.reset();

Supported profiles are glsl (OpenGL), glslvk (Vulkan), hlsl (D3D11) and metal (Metal). Cg is no longer supported in OgreNext.

I also saw that he used CG, I was thinking that CG could be an issue as well.
For my game I completely removed CG even if I can use it, since it introduces so many problems, some of which are detailed here:
viewtopic.php?t=94854

assaber
Gnoblar
Posts: 7
Joined: Tue May 02, 2023 5:06 am
x 5

Re: How to know in advance whether a vertex program can be compiled or not

Post by assaber »

dark_sylinc wrote: Mon May 20, 2024 6:17 pm

The only way to know is to wrap it in a try/catch block:

Ok, I know how to do to avoid exceptions causing the program to exit. I'm going to try to wrap it using try...catch in a critical place.
I haven't touched CG yet, and only know a little bit about vertex shaders and fragment shaders(T T), I want to Integrate them for later debugging and learning. Pbs and Unlit cards are packaged finished(Although my understanding may also be humorous).If I run into other problems, I'll come here and ask for help.
Very very thanks

assaber
Gnoblar
Posts: 7
Joined: Tue May 02, 2023 5:06 am
x 5

Re: How to know in advance whether a vertex program can be compiled or not

Post by assaber »

rpgplayerrobin wrote: Mon May 20, 2024 11:12 pm

Note that after the exception "CustomShadowCasterVp" will continue to exist until you destroy it; it will be in an unusable state because it failed to compile.

I guess the right way to remove it is then to do something like this:

Code: Select all

HighLevelGpuProgramManager::getSingleton().unload(vp);
HighLevelGpuProgramManager::getSingleton().remove(vp);
vp.reset();

Supported profiles are glsl (OpenGL), glslvk (Vulkan), hlsl (D3D11) and metal (Metal). Cg is no longer supported in OgreNext.

I also saw that he used CG, I was thinking that CG could be an issue as well.
For my game I completely removed CG even if I can use it, since it introduces so many problems, some of which are detailed here:
viewtopic.php?t=94854

Thanks, my bro. I'll notice that