Skip to content

Implement RasterizedMeshTexture (RenderingDevice) #106331

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

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

beicause
Copy link
Contributor

@beicause beicause commented May 13, 2025

Ported from https://github.com/beicause/GodotMeshTextureRd (very different now).

Current MeshTexture is just a wrapper for canvas_item_add_mesh() and doesn't store texture data itself, which means it can't be used in shader uniform or 3D scene. And it can't do regional drawing.

This PR implements a more powerful rasterized mesh texture, which provides more flexible control than the MeshTexture. And should have better performance compared to ViewportTexture (#102028).

RasterizedMeshTexture reads one surface from mesh and supports shader material (both vertex shader and fragment shader) with the new shader type "rasterize_mesh".

This is only implemented in RenderingDevice (Forawrd+&Mobile renderer) for now, not Compatibility.

@beicause

This comment was marked as outdated.

@beicause beicause force-pushed the impl-rasterized-mesh-tex branch 2 times, most recently from 640ce03 to 86c5ddf Compare May 14, 2025 04:33
@Chaosus Chaosus added this to the 4.x milestone May 14, 2025
@beicause

This comment was marked as outdated.

@beicause beicause force-pushed the impl-rasterized-mesh-tex branch 7 times, most recently from 520549c to cab9b28 Compare May 16, 2025 02:11
@beicause

This comment was marked as outdated.

@beicause beicause marked this pull request as ready for review May 16, 2025 02:22
@beicause beicause requested review from a team as code owners May 16, 2025 02:22
@beicause beicause force-pushed the impl-rasterized-mesh-tex branch 3 times, most recently from 8991649 to cf91792 Compare May 16, 2025 13:24
@mk56-spn
Copy link

The latest commit seems to induce a crash when attempting to rotate the 3d preview for the mesh. this appear to be a regression from the version from before the force push 8991649 to cf91792 which did not in my testing suffer from this

@beicause
Copy link
Contributor Author

beicause commented May 17, 2025

The latest commit seems to induce a crash when attempting to rotate the 3d preview for the mesh. this appear to be a regression from the version from before the force push 8991649 to cf91792 which did not in my testing suffer from this

I couldn't reproduce the crash on Linux. However I tested on Windows and found it throws errors and can't run at all. (Fixed now, and it doesn't crash, either).
Could you provide more detailed information on how to reproduce it?

@mk56-spn
Copy link

mk56-spn commented May 17, 2025

The latest commit seems to induce a crash when attempting to rotate the 3d preview for the mesh. this appear to be a regression from the version from before the force push 8991649 to cf91792 which did not in my testing suffer from this

I couldn't reproduce the crash on Linux. However I tested on Windows and found it throws errors and can't run at all. (Fixed now, and it doesn't crash, either). Could you provide more detailed information on how to reproduce it?

2025-05-17.12-07-33.mp4

hey here are all the steps to repro this

and here is my specs in case. im also on linux

Godot v4.5.dev.mono (59a9f4c) - Pop!_OS 22.04 LTS on X11 - X11 display driver, Multi-window, 1 monitor - Vulkan (Forward+) - dedicated NVIDIA GeForce RTX 4060 Laptop GPU - 12th Gen Intel(R) Core(TM) i7-12650H (16 threads)

fwiw moving the main window also seems to cause a crash. weird

@beicause
Copy link
Contributor Author

Are you able to compile with dev_build=yes to get a backtrace? I can't reproduce Vulkan 1.4.305 - Forward+ - Using Device #0: AMD - AMD Radeon Graphics (RADV RENOIR)

@CarpenterBlue
Copy link

I have encountered crash when trying to copy paste the node between scenes.

godot.windows.editor.dev.x86_64_8BCCvefFns.mp4

@CarpenterBlue
Copy link

CarpenterBlue commented May 17, 2025

And another crash when I try to remove the material.

godot.windows.editor.dev.x86_64_2JHCZwxR2C.mp4

@beicause beicause force-pushed the impl-rasterized-mesh-tex branch 2 times, most recently from 5e82592 to dac9ee8 Compare May 18, 2025 07:11
@beicause
Copy link
Contributor Author

I have encountered crash when trying to copy paste the node between scenes.
And another crash when I try to remove the material.

Fixed now.

@beicause beicause force-pushed the impl-rasterized-mesh-tex branch from dac9ee8 to f628176 Compare May 18, 2025 07:34
@CarpenterBlue
Copy link

CarpenterBlue commented May 18, 2025

Perfect, I can confirm it's no longer crashing but when I add a shader, the texture stops showing up.
I also tried undo and redo and it still lived without an issue.

godot.windows.editor.x86_64_wCui41KOOc.mp4

@beicause
Copy link
Contributor Author

but when I add a shader, the texture stops showing up.

It's because the projection is actually zero matrix until modifying or reloading the scene. I opened a separate issue #106561

@beicause beicause force-pushed the impl-rasterized-mesh-tex branch from f628176 to 69b7359 Compare May 19, 2025 05:20
@CarpenterBlue

This comment was marked as outdated.

@beicause
Copy link
Contributor Author

You can also modify the ww component to globally scale.

@CarpenterBlue
Copy link

You can also modify the ww component to globally scale.

I did not realized that but that makes total sense.

@beicause beicause force-pushed the impl-rasterized-mesh-tex branch 3 times, most recently from 5a0cf31 to 25e8e17 Compare May 22, 2025 00:39
@beicause beicause marked this pull request as draft May 22, 2025 06:47
@beicause beicause force-pushed the impl-rasterized-mesh-tex branch 2 times, most recently from 82fa3ff to f821274 Compare May 22, 2025 11:33
@beicause
Copy link
Contributor Author

beicause commented May 24, 2025

Implemented drawable texture in my opinion that works with the mesh rasterizer. See also godotengine/godot-proposals#12443 (comment)

In addition:

  • Custom blend state via RasterizerBlendState (almost the same with RDPipelineColorBlendStateAttachment).
  • All possiable RD texture format that supported by Godot.
  • MSAA.

TODO:
Waiting for more feedback and unify this PR with #105701. As I focus on RD render and not familiar with compatibility renderer, not sure if these features are suitable for compatibility renderer.

In addition, this PR chooses to do nothing for the conversion between sRGB and linear( source_color hint also does nothing ). There is a difference in RGBAF/RGBAH texture colors in the 2D and 3D renderers. But RGBA8/RGBA8_SRGB textures displays the same in 2D and 3D. I think it's expected.

Test project:

rasterized-mesh-tex (2).zip

屏幕截图_20250524_100854

And use mesh rasterizer with https://github.com/Auburn/FastNoiseLite/blob/master/GLSL/FastNoiseLite.glsl:

shader_type mesh_rasterizer;

uniform vec2 size = vec2(256,256);
uniform int noise_seed = 0;

#include "include/FastNoiseLite.gdshaderinc"

void fragment() {
	fnl_state state = fnlCreateState(noise_seed);
	state.noise_type = FNL_NOISE_OPENSIMPLEX2;
	state.fractal_type = FNL_FRACTAL_FBM;
	state.frequency = 0.01f;
	state.octaves = 4;
	state.lacunarity = 2.0f;
	state.gain = 0.5f;

	FNLfloat val = fnlGetNoise2D(state, UV.x*size.x, UV.y*size.y)/ 2.0f + 0.5f;
	COLOR = vec4(val, 0.0, 0.0, 1.0);
}

屏幕截图_20250611_124944

@beicause beicause force-pushed the impl-rasterized-mesh-tex branch from a970b63 to abbef01 Compare July 5, 2025 12:22
@beicause
Copy link
Contributor Author

beicause commented Jul 5, 2025

Pushed a simplified implementation (just a single texture_drawable_draw_mesh to draw, no separate mesh_rasterizer rid), and it completely avoids processing mesh on CPU. However, this means the mesh matrix needs to be manually adjusted otherwise the default position may be not good.

Removed blend state and MSAA settings.

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

Successfully merging this pull request may close these issues.

Add support for rasterizing mesh to texture with custom shader
6 participants