Custom Texture interpreted as Texture2D when trying to sample from it

The Material Editor appears to interpret Custom Textures input into Custom nodes as "sampler2D"s (see the HLSL Code window), but when trying to sample from them using tex2D, it thinks it is a Texture2D in the error.

Casting has no effect, and using sister methods like tex2Dlod are equally futile.

[fredakilla’s workaround][2] no longer generates the Texture2D/SamplerState declarations in 4.4.3, so I’m stuck.

The most ideal way for me to get around all of this would be to have a for-loop node accessible from the material editor… but having CustomTextures sample-able from within Custom nodes would be fantastic.

Hi ,

I recently fixed this by removing the CustomTexture node completely and making the Custom node accept TextureObject and TextureObjectParameter. If you name your parameter in your Custom node “Tex” and connect a TextureObject, then there will be the Texture2D Tex parameter as well as an extra sampler parameter available inside the custom node called TexSampler.

Unfortunately it just missed the cutoff date to make it in time for 4.5, but it will be in 4.6. If you’re using code, you can find it in GitHub and it’s a relatively easy merge:

https://github.com/EpicGames/UnrealEngine/commit/548e6c187f8d7d7d5709b3150611ef009d9cb53b

Cheers

Thanks, !

I tried merging it (by hand :confused: ) into 4.4.3 and now when I go to apply my shader, the “Compiling Shaders (84)” box appears in the corner and just gets stuck there. The 3 dot animation still plays though…

When I exit out of my material window, the number jumps to 121 (Shaders Compiling (121), but it doesn’t move from there.

I’m going to try it tomorrow on the latest 4.5 release (since I see some of the files have changed length since 4.4.3 outside of your commit).

Thanks again

Hi , that’s unexpected (obviously!) - I can’t see why this change would cause other shaders to fail to compile. It’s also not really version specific so I’d expect it to work well in 4.4.

Can you have a look at the HTML window for your material to see that the Custom function and its call look correct? (You can paste the contents here if you like).

Thanks for taking a look; I’m almost certain I made a mistake transferring your code over. It’s just weird that it’s not throwing any errors (just getting stuck).

http://pastebin.com/zszAwm7X

The only thing I can think of is that something in your custom node expression is giving the HLSL compliler a very hard time allocating registers trying to unroll that loop, and the compiler is taking a very long time to complete. I guess in Task Manager you will be able to see a ShaderCompileWorker,exe process sitting there taking 100% of one CPU core? If you turn on the Command Line column view in Task Manager you can see the temporary folder name where you can find the exact shader it’s trying to compile.

Both the function definition:

MaterialFloat CustomExpression0(FMaterialPixelParameters Parameters,MaterialFloat3 from,MaterialFloat3 direction,MaterialFloat StepLength,MaterialFloat3 position,MaterialFloat rows,MaterialFloat columns,MaterialFloat width,MaterialFloat length,MaterialFloat height,MaterialFloat step,MaterialFloat MaximumRaySteps,Texture2D Tex, sampler TexSampler )
{
float totalValue = 0.0;int steps;for (steps=0; steps < MaximumRaySteps; steps++) {float3 p = (((from + (step * direction* StepLength))-position));if(abs(p.x)>width/2)return float2(0,0);if(abs(p.y)>length/2)return float2(0,0);if(abs(p.z)>height/2)return float2(0,0);int slice = ((p.z/height)*(rows*columns))+(rows*columns/2);float2 cornerpos = float2(((slice%columns)/columns), floor(slice/columns)/rows);float2 coord = cornerpos + float2(((p.x/width)+0.5)/columns,((p.y/length)+0.5)/rows);float4 value = tex2D(TexSampler, coord);totalValue += value.r;if (totalValue < 0.0) break;}return totalValue;
}

and the call:

    MaterialFloat Local9 = CustomExpression0(Parameters,View.ViewOrigin.xyz,Local5,2.00000000,Primitive.ActorWorldPosition,6.00000000,5.00000000,60.00000000,72.00000000,30.00000000,Local8,72.00000000,Material.Texture2D_0,Material.Texture2D_0Sampler );

look good to me… Could you try a simpler example?

Ah, shoot, I forgot to mention that it pulls this regardless of what I type into the custom node (even “1” does it).

I’m going to give the “merge” I did a thorough look-through tomorrow and I’ll let you know if I can fix it.

Hey , I just re-did the merge and it’s still getting stuck trying to compile.

After more testing, I’ve discovered it’s only on materials that try to use Tex (my thinking it happened on other materials is because the shaders compiling box stays stuck there until I restart the application, but it doesn’t get stuck when I try other materials first).

I wonder if something else big changed in the 4.4.3 → 4.5; I’ll see what I can do about merging this into 4.5 once that’s “released”.

Ack, I just noticed that the ShaderCompiler happens in a separate process (that doesn’t end with the UE4 Editor closing). I had about 12 of them built up from my testing. They’d all been running for over an hour by the point I ended them…

Hi ,

I’m using Texture2DSample with both the texture object and the sampler, instead of just tex2D and it seems to work fine for me, eg:

return Texture2DSample(Tex,TexSampler,UV);

16352-customnodetest.jpg

Hey , I changed the way I’m sampling the texture to match your format, and it works! However, I’m definitely noticing the compile times are directly proportional to how many times my loop runs.

I’m thinking this confirms your observation that it’s having a hard time unrolling the loop. I’m going to work on making this simpler so that I won’t hit this issue…

Thanks for your support!

Aha! The massive slow downs were caused by all the weird returns I’d put into my HLSL code. Getting rid of those has the shader compile and run smooth as butter.

Awesome, glad it’s working. Hope you’re doing something cool with the extra texture sampling!