-
-
Notifications
You must be signed in to change notification settings - Fork 21.4k
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
Add ImageTexture
unit tests
#88044
Add ImageTexture
unit tests
#88044
Conversation
You can use the viewport texture for this and access the images pixels. For reference you find a usage of this in colorpicker: godot/scene/gui/color_picker.cpp Lines 1599 to 1602 in d335281
|
27cddae
to
f507a0d
Compare
Will this work in headless mode without a GPU? CI doesn't have access to any GPU. |
I didn't think about this - I'm not yet familiar with the rendering server. After looking in detail at the PR, I'm no longer sure, if my previously mentioned method can work, since no nodes are added to the scene tree. |
Can confirm it if the style errors are fixed and the CI can run |
f507a0d
to
4d8a8fb
Compare
fa0b259
to
62f7e05
Compare
ce6a92e
to
2b95df2
Compare
ImageTexture
unit tests
tests/scene/test_image_texture.h
Outdated
TEST_CASE("[SceneTree][ImageTexture] draw") { | ||
Ref<Image> image = Image::load_from_file(TestUtils::get_data_path("images/icon.png")); | ||
Ref<ImageTexture> image_texture = ImageTexture::create_from_image(image); | ||
RID canvas = RenderingServer::get_singleton()->canvas_create(); | ||
RID canvas_item = RenderingServer::get_singleton()->canvas_item_create(); | ||
RenderingServer::get_singleton()->canvas_item_set_parent(canvas_item, canvas); | ||
image_texture->draw(canvas_item, Point2(0.0, 0.0)); | ||
// TODO: figure out how to check to see if it was drawn. can we sample a pixel on the canvas? | ||
// I suppose not segfaulting/crashing is a positive result that might make this worth | ||
// keeping. | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note this wouldn't draw anything, the canvas would also need to be attached to a viewport.
But if tests run in headless mode (as Calinou mentioned in #88044 (comment)) then this test case seems useless to me. 🤔 Again, I'm not that familiar with the tests / not sure what's desired here hence I won't be approving/dismissing it. cc @akien-mga
(the other test cases look fine to me though)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The tests typically run as headless, but they should also work if ran with an actual rendering server and display server.
So it's worth making sure that it does something useful in non-headless mode, and if it can't work in headless, it can be made optional with some if (DisplayServer::get_singleton()->get_name() == "headless")
checks.
Now I didn't check, but does headless drawing to a texture in memory actually work? If it does, that's also worth testing.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the feedback.
I'm not sure how to execute tests headless vs non-headless. When you add the "--test" flag, the editor binary doesn't actually start up, it just executes the tests and exits like the console binary. Looking at the startup code, it looks like if the "--test" argument is present, the main enters "TEST_MAIN_PARAM_OVERRIDE" and returns/exits from that before opening a windows context or running the actual proper engine main.
Things I've tried:
DisplayServer::get_singleton()->get_name()
returns "mock"DisplayServer::get_singleton()->window_can_draw()
returns false- Adding proper viewport initialization code (see below), errors out on "RenderDummy" calls to access a null pointer viewport texture.
- Accessing "RSG::viewport->get_total_objects_drawn()" and similar metrics functions, they all always return 0.
Unless anyone has any other ideas, I'm on board to just delete the draw test case.
TEST_CASE("[SceneTree][ImageTexture] draw") {
print_line(DisplayServer::get_singleton()->get_name()); // prints "mock"
RID scenario = RS::get_singleton()->scenario_create();
RID viewport = RS::get_singleton()->viewport_create();
RS::get_singleton()->viewport_set_update_mode(viewport, RS::VIEWPORT_UPDATE_DISABLED);
RS::get_singleton()->viewport_set_scenario(viewport, scenario);
RS::get_singleton()->viewport_set_size(viewport, 128, 128);
RS::get_singleton()->viewport_set_transparent_background(viewport, true);
RS::get_singleton()->viewport_set_active(viewport, true);
Ref<Image> image = Image::load_from_file(TestUtils::get_data_path("images/icon.png"));
Ref<ImageTexture> image_texture = ImageTexture::create_from_image(image);
RID canvas = RenderingServer::get_singleton()->canvas_create();
RID canvas_item = RenderingServer::get_singleton()->canvas_item_create();
RenderingServer::get_singleton()->canvas_item_set_parent(canvas_item, canvas);
image_texture->draw(canvas_item, Point2(0.0, 0.0));
print_line("get_total_objects_drawn: ", RSG::viewport->get_total_objects_drawn()); // prints 0
print_line("get_total_primitives_drawn: ", RSG::viewport->get_total_primitives_drawn()); // prints 0
print_line("get_total_draw_calls_used: ", RSG::viewport->get_total_draw_calls_used()); // prints 0
RS::get_singleton()->viewport_attach_canvas(viewport, canvas);
RID viewport_texture = RS::get_singleton()->viewport_get_texture(viewport);
Ref<Image> img = RS::get_singleton()->texture_2d_get(viewport_texture); // ERROR: Parameter "t" is null.
// at: RendererDummy::TextureStorage::texture_2d_get (C:\repos\godot\servers\rendering\dummy\storage\texture_storage.h:106)
if (img.is_valid() && !img->is_empty()) {
// never reached
} else {
print_line("viewport texture is null/empty"); // executed every time
}
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unless anyone has any other ideas, I'm on board to just delete the draw test case.
Yeah, makes sense to just remove it. If desired (and decided what's a proper way to test it) it could be always added separately in another PR.
(Sorry for the late response; I've seen your comment before but I think I've missed this specific sentence. 🤔)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
All good, my comment was a bit of a wall of text. I went ahead and deleted the test case. I'll re-request a review.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I went ahead and deleted the test case. I'll re-request a review.
I've already approved the PR after the removal. So that was a pointless re-request. 🙃
dd9a207
to
0b6f0b9
Compare
0b6f0b9
to
6dbbc24
Compare
Thanks! |
Added ImageTexture unit tests.
I wasn't entirely sure on how to go about testing the draw methods and some feedback around that area would be appreciated. Thanks.
Link back to #43440