Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

texelFetch behavior changed at Godot 4.0 Alpha 12 #64669

Closed
ChristianKaltenecker opened this issue Aug 20, 2022 · 5 comments
Closed

texelFetch behavior changed at Godot 4.0 Alpha 12 #64669

ChristianKaltenecker opened this issue Aug 20, 2022 · 5 comments

Comments

@ChristianKaltenecker
Copy link

ChristianKaltenecker commented Aug 20, 2022

Godot version

4.0 Alpha 12

System information

ArchLinux; Nvidia GeForce RTX 3070 Ti; Vulkan

Issue description

Given the following script to deliver the position of an item:

var position := Icon.global_position.floor()
var image := Image.new()
image.create(1,1, false, Image.FORMAT_RGBAF)

# Store the position in the red and green channel
image.set_pixel(0, 0, Color(position.x, position.y, 0, 0))

var texture := ImageTexture.new()
texture.create_from_image(image)
material.set_shader_param("position", texture)

I am using the following shader:

// Get the position data
vec4 texel = texelFetch(position, ivec2(0, 0), 0);
// The following line works, however:
//texel = vec4(482.0, 258.0, 0.0, 0.0);

// Distance from this pixel to the light source then normalize
float dist = distance(texel.xy, frag_position);
dist = min(dist / 100.0, 1.0);

COLOR = vec4(dist, dist, 1.0, 0.5);

However, beginning with Godot 4.0 Alpha 12, the behavior of texelFetch seems to have changed and only gets the coordinates (0, 0) as x and y coordinates.
This bug is still included in the current Alpha 14.
In Godot 4.0 Alpha 11, it worked as it should.
Is this a bug or have I missed something?

Thanks in advance for your help!

Steps to reproduce

  1. Open the project and execute the Main scene Main.tscn
  2. The bug should be easily visible if the blue area is in the left upper corner and not at the position of the icon
  3. Compare it with Godot 4.0 Alpha 11

Minimal reproduction project

TestProject.zip

@Calinou Calinou added this to the 4.0 milestone Aug 20, 2022
@clayjohn
Copy link
Member

I don't think anything has changed in the code that would change how texelFetch behaves. Have you verified that the image is still created correctly?

@ChristianKaltenecker
Copy link
Author

ChristianKaltenecker commented Aug 22, 2022

I have verified via the debugger that the position is encoded correctly into the image.
The problem occurred in another codebase (in a game I develop), but was easily reproducible via the minimal reproduction project. Most interestingly, the behavior changed from Alpha 11 to Alpha 12.

The image in both Alpha versions is still rendered correctly. Therefore, I have set the alpha of the shader of the minimal reproduction project to 0.5.

I also searched in the change log and issue list for something interesting regarding this issue, but failed to find something that could be related to this issue...

EDIT: Some more clarification.

@ChristianKaltenecker
Copy link
Author

As further information:
Using the minimal reproduction project above, I have now also reproduced it on Windows 11 (Nvidia RTX 3070 Ti) with Godot 4.0 Alpha 11 and Alpha 12 by starting the project.

@clayjohn
Copy link
Member

I ran your test project through NVidia NSight to debug it and the shader appears to be working correctly. The issue is with the texture. Between Alpha 11 and Alpha 12 create_from_image() was changed to a function that returns the created image.

e.g. texture.create_from_image() became texture = ImageTexture.create_from_image(). In your code you end up just throwing your texture away and passing an invalid texture to the shader.

Changing your texture creation code to the following fixes your issue:

var texture := ImageTexture.create_from_image(image)
material.set_shader_param("position", texture)

@ChristianKaltenecker
Copy link
Author

Thank you very much for your help! I completely missed that change.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Archived in project
Development

No branches or pull requests

3 participants