Cpu to shader parameter passing


Hi Guys,

I am trying to pass in a vec3 to the shaders from c++

I can see SRenderObjData has a couple of arrays which might do the job.

m_fTempVars and m_Constants look possible, even m_pShaderParams looks possible, but when I look into the actual shaders, I cannot see where any of them end up.

I can see the obvious ones like

float4 PS_ScreenSize : PB_ScreenSize;

And all the things like that I would expect to see, but nothing that maps to the data in SRenderObjData.

Anyone point me in the right direction ?



asked 7 years ago146 views
5 Answers
Accepted Answer


The way to go here is to declare the parameter by name and enumeration, fill it, and then match it in the shader – here is a pre-existing example for sending a camera's front vector to a shader:

  1. The enum declaration (ShaderComponents.h):
  1. The mapping to the shader (ShaderComponents.cpp):
SParamDB(PARAM(PB_CameraFront, ECGP_PB_CameraFront), 0),
  1. The data structure mapped to the buffer (ShaderComponents.h):
Vec3 pCameraFront; // ECGP_PB_CameraFront
  1. The setting of the data into the buffer (D3DHWShader.cpp):
        case ECGP_PB_CameraFront:
sCameraFront(sData, r);
  1. And finally, the shader declaration / matching (in Common.cfx) to be used by the shaders:
float4 CameraFrontVector: PB_CameraFront
answered 7 years ago

I'm looking into an answer on this one for you. I'll get back to you soon.

answered 7 years ago

Hi Binky,

Any progress?

The whole shader system seems to be designed to be obfuscated.

I have written a tool to parse the source code and handle all the conditional flags so I can look at what actually gets passed to the shader compiler, but it's not helped me at all.

For example I can see global constants all over the place. ( I hate global shader constants, but that's another issue). When I look for those values in the C++ code, no sign of them.

float4 g_PS_NearestScaled : PF_NearestScaled < psregister = PS_PROJ_RATIO_NEAREST_SCALED; >;

If you do a find in files of the source code

  Find all "g_PS_NearestScaled", Match case, Subfolders, Find Results 1, Entire Solution, ""
Matching lines: 0 Matching files: 0 Total files searched: 11083


  Find all "PF_NearestScaled ", Match case, Subfolders, Find Results 1, Entire Solution, ""
Matching lines: 0 Matching files: 0 Total files searched: 11083


I can see it is mapped to registers ..


But that's all.

This is frustrating as hell, I just need to do some really simple changes to the shaders, but if I cannot pass in a vec3, I am screwed.

Time is running short and there is talk of me flying out to LA to work on a UE4 project, so I really want to get this working ASAP.

Anything you can do to speed things up would be worth beer.

I am going to be at the Lumberyard event in London on Monday, but I would like to be in the position to show something to the guys , rather than turn up and batter them with technical questions.



answered 7 years ago

Ah yes, these can be tricky to find. g_PS_NearestScaled is getting used inside of Engine/Shaders/HWScripts/CryFx/ShadowMaskGen.cfx (line 361)

float2 ProjRatio = (fSceneDepth<fNearestScaledLinear)?g_PS_NearestScaled.xy:g_PS_ProjRatio.xy;
answered 7 years ago

Thanks Gene,

That sorted me out. Now I can actually get on with some real work.



answered 7 years ago

This post is closed: Adding new answers, comments, and votes is disabled.