godotengine / godot

Godot Engine – Multi-platform 2D and 3D game engine
https://godotengine.org
MIT License
90.21k stars 21.04k forks source link

"Detect 3D" is triggered for ShaderMaterial with `shader_type canvas_item;` #62087

Closed lyuma closed 1 year ago

lyuma commented 2 years ago

Godot version

4.0.dev 706dc74e

System information

Windows 10, Vulkan

Issue description

ShaderMaterial is treated as 3D for the purpose of "Detect 3D", even if the material is of type canvas_item and used in a 2D node.

Here you can see the problem. Let's take icon.png, a quality piece of artwork whose every pixel is perfectly preserved. While dragging into canvas_item material Now we drag it into a 2D canvas_item shader, as opposed to a 2D sprite's texture slot. There could be lots of reasons for this. For example, it might be an auxiliary texture used for outline drawing, or a palette, or something else.

Now see what happened. (The import inspector doesn't refresh, so you select a different file and back.) After dragging into material It got VRAM Compressed!

Godot also prints the following.

res://random_texture.png: Texture detected as used in 3D. Enabling mipmap generation and setting the texture compression mode to VRAM Compressed (S3TC/ETC/BPTC).

Steps to reproduce

  1. Create a new 2D scene.
  2. Add a new node of type ColorRect
  3. Create a new ShaderMaterial or convert to shader material.
  4. Modify the shader to have the following code.
    
    shader_type canvas_item;
    render_mode blend_mix;

uniform sampler2D tex;

void fragment() { COLOR = texture(tex, UV); }


5. Find a texture, such as `icon.png`. Check the Import tab and make sure that Compress Mode is set to Lossless, and Detect 3D Compress is set to VRAM Compressed.
6. Drag this texture into the `tex` slot in the material.
7. The import inspector will still show the old settings. This is a different bug.
8. Click a different file, then select icon.png again.
9. Now the import settings will show that it has been changed to VRAM Compressed, and Detect 3D was disabled.

### Minimal reproduction project

[detect3dcanvas.zip](https://github.com/godotengine/godot/files/8914364/detect3dcanvas.zip)

Editing to add repro project from #74335 in case it helps. To repro in this project, drag `perfect.png` into the material's Evil slot.
https://github.com/godotengine/godot/files/10887280/shader.texture.distorted.reproduction.project.zip
clayjohn commented 2 years ago

It looks like the logic for detect 3D is to call the callback anytime textures are updated and p_use_linear_color is true

https://github.com/godotengine/godot/blob/361a401fbb1d35206f594be86dd4f5a1010c763a/servers/rendering/renderer_rd/storage_rd/material_storage.cpp#L1161-L1163

However, p_use_linear_color is always true as it is set here:

https://github.com/godotengine/godot/blob/361a401fbb1d35206f594be86dd4f5a1010c763a/servers/rendering/renderer_rd/storage_rd/material_storage.cpp#L1272

Looks like maybe we used to handle srgb differently in 2D and then we lost something during a refactoring.

Related: https://github.com/godotengine/godot/issues/61697

clayjohn commented 2 years ago

I can't reproduce on Beta 2! So I am going to close this issue.

At any rate, I am changing the behaviour that I described in my earlier post so this problem should be gone soon for sure.

lyuma commented 1 year ago

Reopening due to reproduction project in #74335 Should we close the new issue and keep this one open?

Calinou commented 1 year ago

Should we close the new issue and keep this one open?

Yes :slightly_smiling_face:

RafaelVidaurre commented 1 year ago

Any news on this, or a workaround? Super nasty issues going on for me

Calinou commented 1 year ago

Any news on this, or a workaround? Super nasty issues going on for me

You can change the Import Defaults for Texture2D/ImageTexture in the Project Settings. Set Detect 3D to Disable so that 3D usage detection never kicks in.