V.4 Shaders. Create a ProgramState?

Hi,
I’m trying to port my old GL shader program to V.4. The doc says:

Create a backend::ProgramState object with ProgramState(const std::string& vertexShader, const std::string& fragmentShader)

Well, I’m stuck here. I don’t seem to have this constructor. I tried to have a look around cocos to spy how it’s done, but I’ve had no luck in finding examples of loading shaders from files.

What do I do? I must be missing something obvious!

Edit: Should I subclass Program?
Also:

Metal uses MSL as the shader development language. To support the OpenGL ES shader running on the Metal framework, V4 uses glsl-optimizer to convert the OpenGL ES shader to a Metal MSL shader.

Does this mean that my GL shaders will automatically be adapted to Metal without me having to rewrite them?

Hi,
you can take a look at this https://github.com/cocos2d/cocos2d-x/blob/v4/tests/cpp-tests/Classes/ShaderTest/ShaderTest.cpp#L477

And yes, that’s right.

Thanks a lot!
I’ll check it out. Although I tried with cocos2d::backend::Device previously, getting a strange error “No member named ‘Device’ in namespace ‘cocos2d::backend’”, even though everything seems to be in place (backend.h includes Device.h so I should have it). Compiling for iOS/Xcode11.5.
I’ll get back to you when I get it right.

Edit: of course. Wrong include. Grrr.

Super! Now my shaders load, hooray!
Except they fail to convert to Metal ( Can not get metal shader:).
I’ll mark your answer as a solution as soon as I’m sure I don’t want to ask any more questions :).

@kalika06
There are a few factors that i’ve came across which result in that error.

  • initializing uniforms (eg uniform int u_var1 = 0;), which is not allowed
  • using uniforms within a global function (outside on main()), which is also not allowed.
  • gl_position in your vertex shader has been used in an operation that involves half4. (refer to my post here)

i can only think of these for now… i remember back then i could let it print out the error but somehow don’t remember the steps already. :sweat_smile: