godotengine / godot

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

Assigning Viewport to Uniform in gdshader not working after Export #73868

Open davibu opened 1 year ago

davibu commented 1 year ago

Godot version

4.0.1

System information

Pop!_OS 22.04 LTS

Issue description

The SubViewport texture is not loaded correctly when applying to a uniform for gdshader use. It works in the editor and on web-export, but doesn't work after exporting for Windows or Linux. (other export platforms not tested)

Steps to reproduce

  1. Create a SubViewport
  2. Create a TextureRect
  3. Apply a placeholder texture and set a minimum size
  4. Set material of TextureRect to a shader
  5. Write shader that expects Sampler2D and outputs the texture
  6. Attach Script to TextureRect
  7. Get Subviewport node in script and set_shader parameter to subviewport texture

Minimal reproduction project

General Scene structure Screenshot from 2023-02-24 12-56-39

SubViewport Inspector Screenshot from 2023-02-24 12-58-28

TextureRect Inspector Screenshot from 2023-02-24 12-59-45

TextureRect Inspector Material Screenshot from 2023-02-24 13-00-53

TextureRect Script

extends TextureRect

# Called when the node enters the scene tree for the first time.
func _ready():
    var mat:ShaderMaterial = material
    assert(mat!=null,"Couldn't get shader material")
    var viewport_node:SubViewport = get_node("../SubViewport")
    assert(viewport_node!=null,"Couldn't aquire SubViewport")
    var viewport_texture:ViewportTexture = viewport_node.get_texture()
    material.set_shader_parameter("tex_frg",viewport_texture)

# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta):
    pass

Shader Code

shader_type canvas_item;

uniform sampler2D tex_frg;

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

Play Scene in editor Screenshot from 2023-02-24 13-01-57

After Export on Linux Screenshot from 2023-02-24 13-03-10

Calinou commented 1 year ago

@davibu Please upload a minimal reproduction project to make this easier to troubleshoot. (It's faster than having us cobble together the project back from the parts you uploaded.)

Can you reproduce this after switching the rendering method to Compatibility in the top-right corner? The web export always uses the Compatibility rendering method.

davibu commented 1 year ago

@Calinou Exporting via Compatibility works. (Tested with Godot 4.0.1) Forward+ export still produces the same issue.

The minimal reproduction project: DeferredViewport.zip

Pyredrid commented 1 year ago

I ran into the same issue. It seems to be some issue with the SubViewport's Update Mode since when render_target_update_mode is set to UpdateMode.UPDATE_ALWAYS then it works regardless of renderer in exported builds.

Eorlanas commented 1 year ago

b

This doesn't seem to be the case for me, I have it set to that in the editor and they don't work

ShaunJansa commented 9 months ago

Has a solution been found? I am running Godot 4.1.1 and have this exact issue...

Eorlanas commented 9 months ago

Has a solution been found? I am running Godot 4.1.1 and have this exact issue...

Not to my knowledge, but I haven't looked into it since

ShaunJansa commented 9 months ago

Has a solution been found? I am running Godot 4.1.1 and have this exact issue...

Not to my knowledge, but I haven't looked into it since

Ok thanks.

gongpha commented 3 months ago

I can't reproduce this on Godot 4.2.2-stable Forward+ (Windows, tested with both Release and Debug)

image