Godot: VIEWPORT_SIZE doesn鈥檛 seem to give the viewport size in pixels

Created on 2 Jul 2018  路  13Comments  路  Source: godotengine/godot

Godot version: 3.0.2

OS/device including version: Ubuntu 17.10

Issue description: The documentation says the VIEWPORT_SIZE uniform contains the size of the viewport in pixels, but its actual value seems to be an aspect ratio. For example, resizing the window in the vertical direction should not affect VIEWPORT_SIZE.x, but it does.

Steps to reproduce: Create any shader material that uses VIEWPORT_SIZE

Minimal reproduction project: viewport.zip

bug rendering

Most helpful comment

This issue was about fixing the behavior of VIEWPORT_SIZE where it is defined, not whether it's defined everywhere.

VIEWPORT_SIZE is defined for vertex, fragment and light spatial shaders, as described in the docs: https://docs.godotengine.org/en/stable/tutorials/shading/shading_reference/spatial_shader.html?highlight=VIEWPORT_SIZE
If you're trying to use it in a canvas_item shader, it's not defined.

All 13 comments

The viewport size is dependent on camera aspect. For example if you set "Keep aspect" to "Keep Width" the problem is gone. Maybe "with respect of camera aspect" line should be added to docs.

I confirm this is an issue still in 3.2 alpha 3. Supposedly GLES2 works just GLES3 does not. I could not confirm that "keep width" on camera fixes this issue @Chaosus It seems to provide values that are very small between 0.05 and 0.0 which are meaningless.

I have been investigating this and seems the issue is that the actual sizes are never passed but rather some kind of mipmap effect for reflection purposes only
https://github.com/godotengine/godot/blob/d482a1d8e8b4e68f2f386ba618cd091c79c20424/drivers/gles3/rasterizer_scene_gles3.cpp#L3560

Compare that with GLES2 which uses state.viewport_sizet which gives the actual dimensions https://github.com/godotengine/godot/blob/d482a1d8e8b4e68f2f386ba618cd091c79c20424/drivers/gles2/rasterizer_scene_gles2.cpp#L2225

In GLES3 viewport size seems to be transferred as part of the state.ubo_data:

https://github.com/godotengine/godot/blob/7d710a745e3eecbde482349fff78f0a4e14f6371/drivers/gles3/rasterizer_scene_gles3.cpp#L2693

apart from the possible banditing for reflection.

It is actually currently initialized to half the viewport extents, which is (I think, haven't double checked this) half the width and height in world units at the near clipping plane. I was looking to see whether I should change this to be the full width and height, but looks like the width and height in pixels is desired rather than anything derived from the camera matrix.

What exactly is this used for currently? I'm presuming it is there for custom shaders. I can't see it used in the glsl files aside from the screen space reflection code. If so would it be safe for me to change it to viewport width and height in pixels (as described in the docs)?

@lawnjelly, I think it is only there for VIEWPORT_SIZE which should be width and height in pixels.

Unless I'm missing something super obvious, VIEWPORT_SIZE not working seems to still be a problem in 3.2.2.
Screenshot 2020-10-12 225933

@grahamoverby I don't know if is available in fragment shader. You might have to set it in vertex and use it in fragment.

If this is the case this should be opened as an issue, either the code or the docs might be wrong, as the docs say VIEWPORT_SIZE should be available in fragment shaders.

EDIT: Sorry this is incorrect, as Akien points out below this is only for spatial shaders, hence the confusion.

I don't know if this is indeed the case but there is an issue already opened that talks about certain built in variables not useable in functions https://github.com/godotengine/godot/issues/14904

This issue was about fixing the behavior of VIEWPORT_SIZE where it is defined, not whether it's defined everywhere.

VIEWPORT_SIZE is defined for vertex, fragment and light spatial shaders, as described in the docs: https://docs.godotengine.org/en/stable/tutorials/shading/shading_reference/spatial_shader.html?highlight=VIEWPORT_SIZE
If you're trying to use it in a canvas_item shader, it's not defined.

Ah good catch! :smile:

Thank you @akien-mga , and sorry about that, my mistake. Read the docs end to end and still missed that!

Was this page helpful?
0 / 5 - 0 ratings