godot_heightmap_plugin icon indicating copy to clipboard operation
godot_heightmap_plugin copied to clipboard

Default shader doesn't work on all hardware

Open darrylryan opened this issue 8 years ago • 17 comments

The terrain is black on my laptop... after playing with the shader and stripping it back to the basic albedo splatmap it works, so I think there must be something not quite right in the shader.

darrylryan avatar Apr 07 '18 15:04 darrylryan

The current shader is unoptimized, it samples way to many textures, so I expect it to run on mostly high-end hardware for now. I plan to reduce the amount of texture fetches in later updates. For the amount of textures, this would help: https://github.com/godotengine/godot/issues/9008

Did you see any error? Were you able to identify exactly which part makes it fail?

I wonder if that can be flagged as an engine bug since the shader works on my 2-years-old laptop with an nVidia 940M. Which one is yours?

Zylann avatar Apr 07 '18 15:04 Zylann

Yes I think it could be considered an engine bug since there are no errors with the shaders, it just fails to work. It's a Radeon HD7670M I'm using.

darrylryan avatar Apr 08 '18 10:04 darrylryan

Can you identify at which point the shader fails when you modify it? I can't reproduce this on my computer

Zylann avatar Apr 08 '18 14:04 Zylann

I'm probably having the same issue on a old computer. No errors are shown in editor but when running the scene these errors silently appear in the Debugger Errors window:

C Error: SceneShaderGLES3: Program LINK FAILED:
error: Too many fragment shader texture samplers

Looks like it's because of the extra roughness textures. If I reduce textures by commenting any of these lines, it works:

ALBEDO = tint.rgb * (w0 * col0.rgb + w1 * col1.rgb + w2 * col2.rgb + w3 * col3.rgb) / w_sum;
ROUGHNESS = (w0 * roughness0 + w1 * roughness1 + w2 * roughness2 + w3 * roughness3) / w_sum;
NORMAL = (INV_CAMERA_MATRIX * (WORLD_MATRIX * vec4(normal, 0.0))).xyz;

BTW this only worked with depth_blending disabled.

argentite avatar Apr 11 '18 10:04 argentite

Yeah, that's what I thought... some old graphics cards may not support that many textures. I plan to reduce them: I already removed the separate hole map to be in the color map, and I should merge roughness with albedo and bump with normal map. That means extra texture baking to be done, though.

I think depth blending does make a difference because it adds one more texture to fetch (for bump).

Zylann avatar Apr 11 '18 11:04 Zylann

Can you try again? I submitted a change that makes detail textures packed, so now there should be 11 texture fetches in the fragment shader, instead of 19.

Zylann avatar Apr 15 '18 19:04 Zylann

Unfortunately it still does not work with same error. The maximum number of textures should be defined by gl.MAX_TEXTURE_IMAGE_UNITS according to this StackOverflow answer which on my PC is 16 as reported by glxinfo (Is this limit same in GLES too?).

Godot dumped the whole generated 1735 line GLSL code in console pastebin. Your fragment code starts at around line 1189. (I hardcoded depth_blending to false at line 1211.)

So I think the problem is because Godot uses more texture samplers internally (described at line 21). I think perhaps an easier solution would be to provide a different simple shader without roughness, depth_blending etc. as most WebGL (mobile?) platforms seem to support only 16 (stats from here).

argentite avatar Apr 16 '18 10:04 argentite

@anubhabg Wow, that graphics card you have sounds quite limitating :( Also note that now, there is only two textures per slot in the terrain, so removing roughness, bump or depth blending won't change anything because those are actually part of the ALBEDO and NORMAL textures (it's a single texture fetch for both).

Ooh hang on... in the pastebin you gave me, the shader still has separate roughness Oo Those shouldn't be here, they are not used anymore. Also, I don't see packed samplers. Are you sure you got latest?

According to that WebGLStats graph, maximum of 16 textures is very widespread (on mobiles mostly)... which means some things in the Godot workflow are quite inefficient, such as CurveTextures or GradientTextures (1 curve = 1 texture, 1 gradient = 1 texture), or the fact default SpatialMaterials only accept maps as individual textures. So using ShaderMaterials with packed textures is even more relevant. Good thing I was worrying about it :O

Zylann avatar Apr 16 '18 12:04 Zylann

Yep, sorry for the slow reply, but I also have the same situation - commenting out the extra textures makes it work.

I think we really need those texture arrays in the core... to complement this terrain we could do with tree billboards and grass for a foliage system. That would be much easier to do if all the different trees could each have their own texture in the array, and each texture could contain 8 or so different view angles of the tree.

darrylryan avatar Apr 16 '18 15:04 darrylryan

@darrylryan I'm not working on trees in this plugin, but for sure there are many cool uses for texture arrays ;) For now the only option I can think of to make it work on such hardware is to either have a generated shader, or let people modify the shader at their own risk.

Zylann avatar Apr 16 '18 16:04 Zylann

It's working now out of the box with depth blending! I guess yesterday the shader being open in Godot wasn't updated by git. Performance is poor though (at least of the demo) but that should be expected from really old hardware like this. Sadly it appears a lot of Android devices have MAX_TEXTURE_IMAGE_UNITS = 8 from here.

argentite avatar Apr 17 '18 06:04 argentite

@anubhabg the cost and amount of textures can still be reduced by removing features, but for now you have to edit the shader yourself (by default all features are running). I'd be curious to know if that's graphic performance or CPU. The plugin is full-GDScript but I haven't experienced issues on that side on my PC.

Zylann avatar Apr 17 '18 09:04 Zylann

@Zylann I might have a go at making some basic tree imposters for this terrain system... the two things need to work together because the trees and grass need to be placed on the terrain, so need to know how to read the height and ideally load the grass only for active chunks of terrain.

Do you plan to support paging/streaming large terrains with this?

darrylryan avatar Apr 17 '18 09:04 darrylryan

@darrylryan you can get height with get_interpolated_height_at(). For trees I was thinking of something a bit wider, like any mesh (rocks too). Grass is too numerous to use the same technique. For streaming, I have notes but it's a massive change to do in the internal logic, I need to prototype stuff. The thing is, the API limitation of Godot makes it quite "prehistoric" compared to what games like FC5 do xD but those things are different issues to be discussed in other threads.

Zylann avatar Apr 17 '18 10:04 Zylann

I managed to spare 2 more texture fetches in the fragment shader. It's not much but maybe that will improve a little. There is a way to spare another 2 but I'll need more time to figure out how to do it properly (the amount of varyings may increase too, which is also a limit according to WebGL stats :| )

Zylann avatar Apr 22 '18 13:04 Zylann

Some news with commit 19ac52868d751de2b45c6b4cf12d37a64032d563:

  • There is now a lite version of the shader, at the moment it has to be toggled from within hterrain.gd, near the beginning of the script. I'll have to work on easier shader swapping in the future
  • Bump and roughness have been swapped, so the lite shader can perform depth blending without too much plugin modifications
  • Due to the above, the textures you already assigned won't work anymore and will need to be re-packed and re-assigned (use an image editor, or the channel packer plugin I made. Or just leave them white if you don't need depth blending)
  • The demo is broken too but I will update it for a future release of the plugin.

Zylann avatar Jul 09 '18 20:07 Zylann

Current master now has easy dropdown to select a less-demanding shader, named Classic4Lite (see also previous comment)

Zylann avatar Jul 21 '18 22:07 Zylann