Merge pull request #43194 from reduz/pixel-snap-refactor

Refactor pixel snapping.
This commit is contained in:
Juan Linietsky 2020-10-30 09:37:43 -03:00 committed by GitHub
commit 704d06d00f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
20 changed files with 124 additions and 29 deletions

View File

@ -60,7 +60,6 @@ private:
float _fps = 1; float _fps = 1;
int _target_fps = 0; int _target_fps = 0;
float _time_scale = 1.0; float _time_scale = 1.0;
bool _pixel_snap = false;
uint64_t _physics_frames = 0; uint64_t _physics_frames = 0;
float _physics_interpolation_fraction = 0.0f; float _physics_interpolation_fraction = 0.0f;
bool abort_on_gpu_errors = false; bool abort_on_gpu_errors = false;
@ -110,8 +109,6 @@ public:
bool has_singleton(const String &p_name) const; bool has_singleton(const String &p_name) const;
Object *get_singleton_object(const String &p_name) const; Object *get_singleton_object(const String &p_name) const;
_FORCE_INLINE_ bool get_use_pixel_snap() const { return _pixel_snap; }
#ifdef TOOLS_ENABLED #ifdef TOOLS_ENABLED
_FORCE_INLINE_ void set_editor_hint(bool p_enabled) { editor_hint = p_enabled; } _FORCE_INLINE_ void set_editor_hint(bool p_enabled) { editor_hint = p_enabled; }
_FORCE_INLINE_ bool is_editor_hint() const { return editor_hint; } _FORCE_INLINE_ bool is_editor_hint() const { return editor_hint; }

View File

@ -506,6 +506,11 @@ void EditorNode::_notification(int p_what) {
RS::get_singleton()->environment_set_volumetric_fog_directional_shadow_shrink_size(GLOBAL_GET("rendering/volumetric_fog/directional_shadow_shrink")); RS::get_singleton()->environment_set_volumetric_fog_directional_shadow_shrink_size(GLOBAL_GET("rendering/volumetric_fog/directional_shadow_shrink"));
RS::get_singleton()->environment_set_volumetric_fog_positional_shadow_shrink_size(GLOBAL_GET("rendering/volumetric_fog/positional_shadow_shrink")); RS::get_singleton()->environment_set_volumetric_fog_positional_shadow_shrink_size(GLOBAL_GET("rendering/volumetric_fog/positional_shadow_shrink"));
RS::get_singleton()->canvas_set_shadow_texture_size(GLOBAL_GET("rendering/quality/2d_shadow_atlas/size")); RS::get_singleton()->canvas_set_shadow_texture_size(GLOBAL_GET("rendering/quality/2d_shadow_atlas/size"));
bool snap_2d_transforms = GLOBAL_GET("rendering/quality/2d/snap_2d_transforms_to_pixel");
scene_root->set_snap_2d_transforms_to_pixel(snap_2d_transforms);
bool snap_2d_vertices = GLOBAL_GET("rendering/quality/2d/snap_2d_vertices_to_pixel");
scene_root->set_snap_2d_vertices_to_pixel(snap_2d_vertices);
} }
ResourceImporterTexture::get_singleton()->update_imports(); ResourceImporterTexture::get_singleton()->update_imports();
@ -517,6 +522,8 @@ void EditorNode::_notification(int p_what) {
OS::get_singleton()->set_low_processor_usage_mode_sleep_usec(int(EDITOR_GET("interface/editor/low_processor_mode_sleep_usec"))); OS::get_singleton()->set_low_processor_usage_mode_sleep_usec(int(EDITOR_GET("interface/editor/low_processor_mode_sleep_usec")));
get_tree()->get_root()->set_as_audio_listener(false); get_tree()->get_root()->set_as_audio_listener(false);
get_tree()->get_root()->set_as_audio_listener_2d(false); get_tree()->get_root()->set_as_audio_listener_2d(false);
get_tree()->get_root()->set_snap_2d_transforms_to_pixel(false);
get_tree()->get_root()->set_snap_2d_vertices_to_pixel(false);
get_tree()->set_auto_accept_quit(false); get_tree()->set_auto_accept_quit(false);
get_tree()->get_root()->connect("files_dropped", callable_mp(this, &EditorNode::_dropped_files)); get_tree()->get_root()->connect("files_dropped", callable_mp(this, &EditorNode::_dropped_files));

View File

@ -1273,7 +1273,6 @@ Error Main::setup(const char *execpath, int argc, char *argv[], bool p_second_ph
OS::get_singleton()->_allow_layered = false; OS::get_singleton()->_allow_layered = false;
} }
Engine::get_singleton()->_pixel_snap = GLOBAL_DEF("rendering/quality/2d/use_pixel_snap", false);
OS::get_singleton()->_keep_screen_on = GLOBAL_DEF("display/window/energy_saving/keep_screen_on", true); OS::get_singleton()->_keep_screen_on = GLOBAL_DEF("display/window/energy_saving/keep_screen_on", true);
if (rtm == -1) { if (rtm == -1) {
rtm = GLOBAL_DEF("rendering/threads/thread_model", OS::RENDER_THREAD_SAFE); rtm = GLOBAL_DEF("rendering/threads/thread_model", OS::RENDER_THREAD_SAFE);

View File

@ -31,6 +31,7 @@
#include "animated_sprite_2d.h" #include "animated_sprite_2d.h"
#include "core/os/os.h" #include "core/os/os.h"
#include "scene/main/viewport.h"
#include "scene/scene_string_names.h" #include "scene/scene_string_names.h"
#ifdef TOOLS_ENABLED #ifdef TOOLS_ENABLED
@ -443,7 +444,7 @@ void AnimatedSprite2D::_notification(int p_what) {
ofs -= s / 2; ofs -= s / 2;
} }
if (Engine::get_singleton()->get_use_pixel_snap()) { if (get_viewport() && get_viewport()->is_snap_2d_transforms_to_pixel_enabled()) {
ofs = ofs.floor(); ofs = ofs.floor();
} }
Rect2 dst_rect(ofs, s); Rect2 dst_rect(ofs, s);

View File

@ -99,7 +99,8 @@ void Sprite2D::_get_rects(Rect2 &r_src_rect, Rect2 &r_dst_rect, bool &r_filter_c
if (centered) { if (centered) {
dest_offset -= frame_size / 2; dest_offset -= frame_size / 2;
} }
if (Engine::get_singleton()->get_use_pixel_snap()) {
if (get_viewport() && get_viewport()->is_snap_2d_transforms_to_pixel_enabled()) {
dest_offset = dest_offset.floor(); dest_offset = dest_offset.floor();
} }
@ -366,7 +367,8 @@ Rect2 Sprite2D::get_rect() const {
if (centered) { if (centered) {
ofs -= Size2(s) / 2; ofs -= Size2(s) / 2;
} }
if (Engine::get_singleton()->get_use_pixel_snap()) {
if (get_viewport() && get_viewport()->is_snap_2d_transforms_to_pixel_enabled()) {
ofs = ofs.floor(); ofs = ofs.floor();
} }

View File

@ -1394,6 +1394,12 @@ SceneTree::SceneTree() {
const bool use_debanding = GLOBAL_DEF("rendering/quality/screen_filters/use_debanding", false); const bool use_debanding = GLOBAL_DEF("rendering/quality/screen_filters/use_debanding", false);
root->set_use_debanding(use_debanding); root->set_use_debanding(use_debanding);
bool snap_2d_transforms = GLOBAL_DEF("rendering/quality/2d/snap_2d_transforms_to_pixel", false);
root->set_snap_2d_transforms_to_pixel(snap_2d_transforms);
bool snap_2d_vertices = GLOBAL_DEF("rendering/quality/2d/snap_2d_vertices_to_pixel", false);
root->set_snap_2d_vertices_to_pixel(snap_2d_vertices);
{ //load default fallback environment { //load default fallback environment
//get possible extensions //get possible extensions
List<String> exts; List<String> exts;

View File

@ -3170,6 +3170,24 @@ bool Viewport::is_snap_controls_to_pixels_enabled() const {
return snap_controls_to_pixels; return snap_controls_to_pixels;
} }
void Viewport::set_snap_2d_transforms_to_pixel(bool p_enable) {
snap_2d_transforms_to_pixel = p_enable;
RS::get_singleton()->viewport_set_snap_2d_transforms_to_pixel(viewport, snap_2d_transforms_to_pixel);
}
bool Viewport::is_snap_2d_transforms_to_pixel_enabled() const {
return snap_2d_transforms_to_pixel;
}
void Viewport::set_snap_2d_vertices_to_pixel(bool p_enable) {
snap_2d_vertices_to_pixel = p_enable;
RS::get_singleton()->viewport_set_snap_2d_vertices_to_pixel(viewport, snap_2d_vertices_to_pixel);
}
bool Viewport::is_snap_2d_vertices_to_pixel_enabled() const {
return snap_2d_vertices_to_pixel;
}
bool Viewport::gui_is_dragging() const { bool Viewport::gui_is_dragging() const {
return gui.dragging; return gui.dragging;
} }
@ -3395,6 +3413,12 @@ void Viewport::_bind_methods() {
ClassDB::bind_method(D_METHOD("set_snap_controls_to_pixels", "enabled"), &Viewport::set_snap_controls_to_pixels); ClassDB::bind_method(D_METHOD("set_snap_controls_to_pixels", "enabled"), &Viewport::set_snap_controls_to_pixels);
ClassDB::bind_method(D_METHOD("is_snap_controls_to_pixels_enabled"), &Viewport::is_snap_controls_to_pixels_enabled); ClassDB::bind_method(D_METHOD("is_snap_controls_to_pixels_enabled"), &Viewport::is_snap_controls_to_pixels_enabled);
ClassDB::bind_method(D_METHOD("set_snap_2d_transforms_to_pixel", "enabled"), &Viewport::set_snap_2d_transforms_to_pixel);
ClassDB::bind_method(D_METHOD("is_snap_2d_transforms_to_pixel_enabled"), &Viewport::is_snap_2d_transforms_to_pixel_enabled);
ClassDB::bind_method(D_METHOD("set_snap_2d_vertices_to_pixel", "enabled"), &Viewport::set_snap_2d_vertices_to_pixel);
ClassDB::bind_method(D_METHOD("is_snap_2d_vertices_to_pixel_enabled"), &Viewport::is_snap_2d_vertices_to_pixel_enabled);
ClassDB::bind_method(D_METHOD("set_shadow_atlas_quadrant_subdiv", "quadrant", "subdiv"), &Viewport::set_shadow_atlas_quadrant_subdiv); ClassDB::bind_method(D_METHOD("set_shadow_atlas_quadrant_subdiv", "quadrant", "subdiv"), &Viewport::set_shadow_atlas_quadrant_subdiv);
ClassDB::bind_method(D_METHOD("get_shadow_atlas_quadrant_subdiv", "quadrant"), &Viewport::get_shadow_atlas_quadrant_subdiv); ClassDB::bind_method(D_METHOD("get_shadow_atlas_quadrant_subdiv", "quadrant"), &Viewport::get_shadow_atlas_quadrant_subdiv);
@ -3419,6 +3443,8 @@ void Viewport::_bind_methods() {
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "world_2d", PROPERTY_HINT_RESOURCE_TYPE, "World2D", 0), "set_world_2d", "get_world_2d"); ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "world_2d", PROPERTY_HINT_RESOURCE_TYPE, "World2D", 0), "set_world_2d", "get_world_2d");
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "transparent_bg"), "set_transparent_background", "has_transparent_background"); ADD_PROPERTY(PropertyInfo(Variant::BOOL, "transparent_bg"), "set_transparent_background", "has_transparent_background");
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "handle_input_locally"), "set_handle_input_locally", "is_handling_input_locally"); ADD_PROPERTY(PropertyInfo(Variant::BOOL, "handle_input_locally"), "set_handle_input_locally", "is_handling_input_locally");
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "snap_2d_transforms_to_pixel"), "set_snap_2d_transforms_to_pixel", "is_snap_2d_transforms_to_pixel_enabled");
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "snap_2d_vertices_to_pixel"), "set_snap_2d_vertices_to_pixel", "is_snap_2d_vertices_to_pixel_enabled");
ADD_GROUP("Rendering", ""); ADD_GROUP("Rendering", "");
ADD_PROPERTY(PropertyInfo(Variant::INT, "msaa", PROPERTY_HINT_ENUM, "Disabled,2x,4x,8x,16x,AndroidVR 2x,AndroidVR 4x"), "set_msaa", "get_msaa"); ADD_PROPERTY(PropertyInfo(Variant::INT, "msaa", PROPERTY_HINT_ENUM, "Disabled,2x,4x,8x,16x,AndroidVR 2x,AndroidVR 4x"), "set_msaa", "get_msaa");
ADD_PROPERTY(PropertyInfo(Variant::INT, "screen_space_aa", PROPERTY_HINT_ENUM, "Disabled,FXAA"), "set_screen_space_aa", "get_screen_space_aa"); ADD_PROPERTY(PropertyInfo(Variant::INT, "screen_space_aa", PROPERTY_HINT_ENUM, "Disabled,FXAA"), "set_screen_space_aa", "get_screen_space_aa");
@ -3577,6 +3603,9 @@ Viewport::Viewport() {
debug_draw = DEBUG_DRAW_DISABLED; debug_draw = DEBUG_DRAW_DISABLED;
snap_controls_to_pixels = true; snap_controls_to_pixels = true;
snap_2d_transforms_to_pixel = false;
snap_2d_vertices_to_pixel = false;
physics_last_mouse_state.alt = false; physics_last_mouse_state.alt = false;
physics_last_mouse_state.control = false; physics_last_mouse_state.control = false;
physics_last_mouse_state.shift = false; physics_last_mouse_state.shift = false;

View File

@ -225,6 +225,8 @@ private:
bool gen_mipmaps; bool gen_mipmaps;
bool snap_controls_to_pixels; bool snap_controls_to_pixels;
bool snap_2d_transforms_to_pixel;
bool snap_2d_vertices_to_pixel;
bool physics_object_picking; bool physics_object_picking;
List<Ref<InputEvent>> physics_picking_events; List<Ref<InputEvent>> physics_picking_events;
@ -556,6 +558,12 @@ public:
void set_snap_controls_to_pixels(bool p_enable); void set_snap_controls_to_pixels(bool p_enable);
bool is_snap_controls_to_pixels_enabled() const; bool is_snap_controls_to_pixels_enabled() const;
void set_snap_2d_transforms_to_pixel(bool p_enable);
bool is_snap_2d_transforms_to_pixel_enabled() const;
void set_snap_2d_vertices_to_pixel(bool p_enable);
bool is_snap_2d_vertices_to_pixel_enabled() const;
void set_input_as_handled(); void set_input_as_handled();
bool is_input_handled() const; bool is_input_handled() const;

View File

@ -1322,7 +1322,7 @@ public:
} }
}; };
virtual void canvas_render_items(RID p_to_render_target, Item *p_item_list, const Color &p_modulate, Light *p_light_list, const Transform2D &p_canvas_transform, RS::CanvasItemTextureFilter p_default_filter, RS::CanvasItemTextureRepeat p_default_repeat) = 0; virtual void canvas_render_items(RID p_to_render_target, Item *p_item_list, const Color &p_modulate, Light *p_light_list, const Transform2D &p_canvas_transform, RS::CanvasItemTextureFilter p_default_filter, RS::CanvasItemTextureRepeat p_default_repeat, bool p_snap_2d_vertices_to_pixel) = 0;
virtual void canvas_debug_viewport_shadows(Light *p_lights_with_shadow) = 0; virtual void canvas_debug_viewport_shadows(Light *p_lights_with_shadow) = 0;
struct LightOccluderInstance { struct LightOccluderInstance {

View File

@ -1194,7 +1194,7 @@ void RasterizerCanvasRD::_render_items(RID p_to_render_target, int p_item_count,
RD::get_singleton()->draw_list_end(); RD::get_singleton()->draw_list_end();
} }
void RasterizerCanvasRD::canvas_render_items(RID p_to_render_target, Item *p_item_list, const Color &p_modulate, Light *p_light_list, const Transform2D &p_canvas_transform, RenderingServer::CanvasItemTextureFilter p_default_filter, RenderingServer::CanvasItemTextureRepeat p_default_repeat) { void RasterizerCanvasRD::canvas_render_items(RID p_to_render_target, Item *p_item_list, const Color &p_modulate, Light *p_light_list, const Transform2D &p_canvas_transform, RenderingServer::CanvasItemTextureFilter p_default_filter, RenderingServer::CanvasItemTextureRepeat p_default_repeat, bool p_snap_2d_vertices_to_pixel) {
int item_count = 0; int item_count = 0;
//setup canvas state uniforms if needed //setup canvas state uniforms if needed
@ -1229,6 +1229,8 @@ void RasterizerCanvasRD::canvas_render_items(RID p_to_render_target, Item *p_ite
state_buffer.screen_pixel_size[1] = 1.0 / render_target_size.y; state_buffer.screen_pixel_size[1] = 1.0 / render_target_size.y;
state_buffer.time = state.time; state_buffer.time = state.time;
state_buffer.use_pixel_snap = p_snap_2d_vertices_to_pixel;
RD::get_singleton()->buffer_update(state.canvas_state_buffer, 0, sizeof(State::Buffer), &state_buffer, true); RD::get_singleton()->buffer_update(state.canvas_state_buffer, 0, sizeof(State::Buffer), &state_buffer, true);
} }

View File

@ -79,7 +79,6 @@ class RasterizerCanvasRD : public RasterizerCanvas {
FLAGS_NINEPACH_DRAW_CENTER = (1 << 12), FLAGS_NINEPACH_DRAW_CENTER = (1 << 12),
FLAGS_USING_PARTICLES = (1 << 13), FLAGS_USING_PARTICLES = (1 << 13),
FLAGS_USE_PIXEL_SNAP = (1 << 14),
FLAGS_USE_SKELETON = (1 << 15), FLAGS_USE_SKELETON = (1 << 15),
FLAGS_NINEPATCH_H_MODE_SHIFT = 16, FLAGS_NINEPATCH_H_MODE_SHIFT = 16,
@ -334,7 +333,7 @@ class RasterizerCanvasRD : public RasterizerCanvas {
float canvas_modulate[4]; float canvas_modulate[4];
float screen_pixel_size[2]; float screen_pixel_size[2];
float time; float time;
float pad; uint32_t use_pixel_snap;
//uint32_t light_count; //uint32_t light_count;
//uint32_t pad[3]; //uint32_t pad[3];
@ -422,7 +421,7 @@ public:
void occluder_polygon_set_shape_as_lines(RID p_occluder, const Vector<Vector2> &p_lines); void occluder_polygon_set_shape_as_lines(RID p_occluder, const Vector<Vector2> &p_lines);
void occluder_polygon_set_cull_mode(RID p_occluder, RS::CanvasOccluderPolygonCullMode p_mode); void occluder_polygon_set_cull_mode(RID p_occluder, RS::CanvasOccluderPolygonCullMode p_mode);
void canvas_render_items(RID p_to_render_target, Item *p_item_list, const Color &p_modulate, Light *p_light_list, const Transform2D &p_canvas_transform, RS::CanvasItemTextureFilter p_default_filter, RS::CanvasItemTextureRepeat p_default_repeat); void canvas_render_items(RID p_to_render_target, Item *p_item_list, const Color &p_modulate, Light *p_light_list, const Transform2D &p_canvas_transform, RS::CanvasItemTextureFilter p_default_filter, RS::CanvasItemTextureRepeat p_default_repeat, bool p_snap_2d_vertices_to_pixel);
void canvas_debug_viewport_shadows(Light *p_lights_with_shadow) {} void canvas_debug_viewport_shadows(Light *p_lights_with_shadow) {}

View File

@ -144,7 +144,7 @@ VERTEX_SHADER_CODE
color_interp = color; color_interp = color;
if (bool(draw_data.flags & FLAGS_USE_PIXEL_SNAP)) { if (canvas_data.use_pixel_snap) {
vertex = floor(vertex + 0.5); vertex = floor(vertex + 0.5);
// precision issue on some hardware creates artifacts within texture // precision issue on some hardware creates artifacts within texture
// offset uv by a small amount to avoid // offset uv by a small amount to avoid

View File

@ -15,7 +15,6 @@
#define FLAGS_USING_LIGHT_MASK (1 << 11) #define FLAGS_USING_LIGHT_MASK (1 << 11)
#define FLAGS_NINEPACH_DRAW_CENTER (1 << 12) #define FLAGS_NINEPACH_DRAW_CENTER (1 << 12)
#define FLAGS_USING_PARTICLES (1 << 13) #define FLAGS_USING_PARTICLES (1 << 13)
#define FLAGS_USE_PIXEL_SNAP (1 << 14)
#define FLAGS_NINEPATCH_H_MODE_SHIFT 16 #define FLAGS_NINEPATCH_H_MODE_SHIFT 16
#define FLAGS_NINEPATCH_V_MODE_SHIFT 18 #define FLAGS_NINEPATCH_V_MODE_SHIFT 18
@ -67,7 +66,8 @@ layout(set = 0, binding = 1, std140) uniform CanvasData {
vec4 canvas_modulation; vec4 canvas_modulation;
vec2 screen_pixel_size; vec2 screen_pixel_size;
float time; float time;
float time_pad; bool use_pixel_snap;
//uint light_count; //uint light_count;
} }
canvas_data; canvas_data;

View File

@ -37,7 +37,7 @@
static const int z_range = RS::CANVAS_ITEM_Z_MAX - RS::CANVAS_ITEM_Z_MIN + 1; static const int z_range = RS::CANVAS_ITEM_Z_MAX - RS::CANVAS_ITEM_Z_MIN + 1;
void RenderingServerCanvas::_render_canvas_item_tree(RID p_to_render_target, Canvas::ChildItem *p_child_items, int p_child_item_count, Item *p_canvas_item, const Transform2D &p_transform, const Rect2 &p_clip_rect, const Color &p_modulate, RasterizerCanvas::Light *p_lights, RenderingServer::CanvasItemTextureFilter p_default_filter, RenderingServer::CanvasItemTextureRepeat p_default_repeat) { void RenderingServerCanvas::_render_canvas_item_tree(RID p_to_render_target, Canvas::ChildItem *p_child_items, int p_child_item_count, Item *p_canvas_item, const Transform2D &p_transform, const Rect2 &p_clip_rect, const Color &p_modulate, RasterizerCanvas::Light *p_lights, RenderingServer::CanvasItemTextureFilter p_default_filter, RenderingServer::CanvasItemTextureRepeat p_default_repeat, bool p_snap_2d_vertices_to_pixel) {
RENDER_TIMESTAMP("Cull CanvasItem Tree"); RENDER_TIMESTAMP("Cull CanvasItem Tree");
memset(z_list, 0, z_range * sizeof(RasterizerCanvas::Item *)); memset(z_list, 0, z_range * sizeof(RasterizerCanvas::Item *));
@ -68,7 +68,7 @@ void RenderingServerCanvas::_render_canvas_item_tree(RID p_to_render_target, Can
RENDER_TIMESTAMP("Render Canvas Items"); RENDER_TIMESTAMP("Render Canvas Items");
RSG::canvas_render->canvas_render_items(p_to_render_target, list, p_modulate, p_lights, p_transform, p_default_filter, p_default_repeat); RSG::canvas_render->canvas_render_items(p_to_render_target, list, p_modulate, p_lights, p_transform, p_default_filter, p_default_repeat, p_snap_2d_vertices_to_pixel);
} }
void _collect_ysort_children(RenderingServerCanvas::Item *p_canvas_item, Transform2D p_transform, RenderingServerCanvas::Item *p_material_owner, RenderingServerCanvas::Item **r_items, int &r_index) { void _collect_ysort_children(RenderingServerCanvas::Item *p_canvas_item, Transform2D p_transform, RenderingServerCanvas::Item *p_material_owner, RenderingServerCanvas::Item **r_items, int &r_index) {
@ -113,7 +113,12 @@ void RenderingServerCanvas::_cull_canvas_item(Item *p_canvas_item, const Transfo
} }
Rect2 rect = ci->get_rect(); Rect2 rect = ci->get_rect();
Transform2D xform = p_transform * ci->xform; Transform2D xform = ci->xform;
if (snapping_2d_transforms_to_pixel) {
xform.elements[2].floor();
}
xform = p_transform * xform;
Rect2 global_rect = xform.xform(rect); Rect2 global_rect = xform.xform(rect);
global_rect.position += p_clip_rect.position; global_rect.position += p_clip_rect.position;
@ -314,9 +319,11 @@ void RenderingServerCanvas::_light_mask_canvas_items(int p_z, RasterizerCanvas::
} }
} }
void RenderingServerCanvas::render_canvas(RID p_render_target, Canvas *p_canvas, const Transform2D &p_transform, RasterizerCanvas::Light *p_lights, RasterizerCanvas::Light *p_masked_lights, const Rect2 &p_clip_rect, RenderingServer::CanvasItemTextureFilter p_default_filter, RenderingServer::CanvasItemTextureRepeat p_default_repeat) { void RenderingServerCanvas::render_canvas(RID p_render_target, Canvas *p_canvas, const Transform2D &p_transform, RasterizerCanvas::Light *p_lights, RasterizerCanvas::Light *p_masked_lights, const Rect2 &p_clip_rect, RenderingServer::CanvasItemTextureFilter p_default_filter, RenderingServer::CanvasItemTextureRepeat p_default_repeat, bool p_snap_2d_transforms_to_pixel, bool p_snap_2d_vertices_to_pixel) {
RENDER_TIMESTAMP(">Render Canvas"); RENDER_TIMESTAMP(">Render Canvas");
snapping_2d_transforms_to_pixel = p_snap_2d_transforms_to_pixel;
if (p_canvas->children_order_dirty) { if (p_canvas->children_order_dirty) {
p_canvas->child_items.sort(); p_canvas->child_items.sort();
p_canvas->children_order_dirty = false; p_canvas->children_order_dirty = false;
@ -334,26 +341,26 @@ void RenderingServerCanvas::render_canvas(RID p_render_target, Canvas *p_canvas,
} }
if (!has_mirror) { if (!has_mirror) {
_render_canvas_item_tree(p_render_target, ci, l, nullptr, p_transform, p_clip_rect, p_canvas->modulate, p_lights, p_default_filter, p_default_repeat); _render_canvas_item_tree(p_render_target, ci, l, nullptr, p_transform, p_clip_rect, p_canvas->modulate, p_lights, p_default_filter, p_default_repeat, p_snap_2d_vertices_to_pixel);
} else { } else {
//used for parallaxlayer mirroring //used for parallaxlayer mirroring
for (int i = 0; i < l; i++) { for (int i = 0; i < l; i++) {
const Canvas::ChildItem &ci2 = p_canvas->child_items[i]; const Canvas::ChildItem &ci2 = p_canvas->child_items[i];
_render_canvas_item_tree(p_render_target, nullptr, 0, ci2.item, p_transform, p_clip_rect, p_canvas->modulate, p_lights, p_default_filter, p_default_repeat); _render_canvas_item_tree(p_render_target, nullptr, 0, ci2.item, p_transform, p_clip_rect, p_canvas->modulate, p_lights, p_default_filter, p_default_repeat, p_snap_2d_vertices_to_pixel);
//mirroring (useful for scrolling backgrounds) //mirroring (useful for scrolling backgrounds)
if (ci2.mirror.x != 0) { if (ci2.mirror.x != 0) {
Transform2D xform2 = p_transform * Transform2D(0, Vector2(ci2.mirror.x, 0)); Transform2D xform2 = p_transform * Transform2D(0, Vector2(ci2.mirror.x, 0));
_render_canvas_item_tree(p_render_target, nullptr, 0, ci2.item, xform2, p_clip_rect, p_canvas->modulate, p_lights, p_default_filter, p_default_repeat); _render_canvas_item_tree(p_render_target, nullptr, 0, ci2.item, xform2, p_clip_rect, p_canvas->modulate, p_lights, p_default_filter, p_default_repeat, p_snap_2d_vertices_to_pixel);
} }
if (ci2.mirror.y != 0) { if (ci2.mirror.y != 0) {
Transform2D xform2 = p_transform * Transform2D(0, Vector2(0, ci2.mirror.y)); Transform2D xform2 = p_transform * Transform2D(0, Vector2(0, ci2.mirror.y));
_render_canvas_item_tree(p_render_target, nullptr, 0, ci2.item, xform2, p_clip_rect, p_canvas->modulate, p_lights, p_default_filter, p_default_repeat); _render_canvas_item_tree(p_render_target, nullptr, 0, ci2.item, xform2, p_clip_rect, p_canvas->modulate, p_lights, p_default_filter, p_default_repeat, p_snap_2d_vertices_to_pixel);
} }
if (ci2.mirror.y != 0 && ci2.mirror.x != 0) { if (ci2.mirror.y != 0 && ci2.mirror.x != 0) {
Transform2D xform2 = p_transform * Transform2D(0, ci2.mirror); Transform2D xform2 = p_transform * Transform2D(0, ci2.mirror);
_render_canvas_item_tree(p_render_target, nullptr, 0, ci2.item, xform2, p_clip_rect, p_canvas->modulate, p_lights, p_default_filter, p_default_repeat); _render_canvas_item_tree(p_render_target, nullptr, 0, ci2.item, xform2, p_clip_rect, p_canvas->modulate, p_lights, p_default_filter, p_default_repeat, p_snap_2d_vertices_to_pixel);
} }
} }
} }

View File

@ -152,9 +152,10 @@ public:
RID_PtrOwner<RasterizerCanvas::Light> canvas_light_owner; RID_PtrOwner<RasterizerCanvas::Light> canvas_light_owner;
bool disable_scale; bool disable_scale;
bool snapping_2d_transforms_to_pixel = false;
private: private:
void _render_canvas_item_tree(RID p_to_render_target, Canvas::ChildItem *p_child_items, int p_child_item_count, Item *p_canvas_item, const Transform2D &p_transform, const Rect2 &p_clip_rect, const Color &p_modulate, RasterizerCanvas::Light *p_lights, RS::CanvasItemTextureFilter p_default_filter, RS::CanvasItemTextureRepeat p_default_repeat); void _render_canvas_item_tree(RID p_to_render_target, Canvas::ChildItem *p_child_items, int p_child_item_count, Item *p_canvas_item, const Transform2D &p_transform, const Rect2 &p_clip_rect, const Color &p_modulate, RasterizerCanvas::Light *p_lights, RS::CanvasItemTextureFilter p_default_filter, RS::CanvasItemTextureRepeat p_default_repeat, bool p_snap_2d_vertices_to_pixel);
void _cull_canvas_item(Item *p_canvas_item, const Transform2D &p_transform, const Rect2 &p_clip_rect, const Color &p_modulate, int p_z, RasterizerCanvas::Item **z_list, RasterizerCanvas::Item **z_last_list, Item *p_canvas_clip, Item *p_material_owner); void _cull_canvas_item(Item *p_canvas_item, const Transform2D &p_transform, const Rect2 &p_clip_rect, const Color &p_modulate, int p_z, RasterizerCanvas::Item **z_list, RasterizerCanvas::Item **z_last_list, Item *p_canvas_clip, Item *p_material_owner);
void _light_mask_canvas_items(int p_z, RasterizerCanvas::Item *p_canvas_item, RasterizerCanvas::Light *p_masked_lights); void _light_mask_canvas_items(int p_z, RasterizerCanvas::Item *p_canvas_item, RasterizerCanvas::Light *p_masked_lights);
@ -162,7 +163,7 @@ private:
RasterizerCanvas::Item **z_last_list; RasterizerCanvas::Item **z_last_list;
public: public:
void render_canvas(RID p_render_target, Canvas *p_canvas, const Transform2D &p_transform, RasterizerCanvas::Light *p_lights, RasterizerCanvas::Light *p_masked_lights, const Rect2 &p_clip_rect, RS::CanvasItemTextureFilter p_default_filter, RS::CanvasItemTextureRepeat p_default_repeat); void render_canvas(RID p_render_target, Canvas *p_canvas, const Transform2D &p_transform, RasterizerCanvas::Light *p_lights, RasterizerCanvas::Light *p_masked_lights, const Rect2 &p_clip_rect, RS::CanvasItemTextureFilter p_default_filter, RS::CanvasItemTextureRepeat p_default_repeat, bool p_snap_2d_transforms_to_pixel, bool p_snap_2d_vertices_to_pixel);
RID canvas_create(); RID canvas_create();
void canvas_set_item_mirroring(RID p_canvas, RID p_item, const Point2 &p_mirroring); void canvas_set_item_mirroring(RID p_canvas, RID p_item, const Point2 &p_mirroring);

View File

@ -528,6 +528,8 @@ public:
BIND2(viewport_remove_canvas, RID, RID) BIND2(viewport_remove_canvas, RID, RID)
BIND3(viewport_set_canvas_transform, RID, RID, const Transform2D &) BIND3(viewport_set_canvas_transform, RID, RID, const Transform2D &)
BIND2(viewport_set_transparent_background, RID, bool) BIND2(viewport_set_transparent_background, RID, bool)
BIND2(viewport_set_snap_2d_transforms_to_pixel, RID, bool)
BIND2(viewport_set_snap_2d_vertices_to_pixel, RID, bool)
BIND2(viewport_set_default_canvas_item_texture_filter, RID, CanvasItemTextureFilter) BIND2(viewport_set_default_canvas_item_texture_filter, RID, CanvasItemTextureFilter)
BIND2(viewport_set_default_canvas_item_texture_repeat, RID, CanvasItemTextureRepeat) BIND2(viewport_set_default_canvas_item_texture_repeat, RID, CanvasItemTextureRepeat)

View File

@ -40,11 +40,21 @@ static Transform2D _canvas_get_transform(RenderingServerViewport::Viewport *p_vi
float scale = 1.0; float scale = 1.0;
if (p_viewport->canvas_map.has(p_canvas->parent)) { if (p_viewport->canvas_map.has(p_canvas->parent)) {
xf = xf * p_viewport->canvas_map[p_canvas->parent].transform; Transform2D c_xform = p_viewport->canvas_map[p_canvas->parent].transform;
if (p_viewport->snap_2d_transforms_to_pixel) {
c_xform.elements[2] = c_xform.elements[2].floor();
}
xf = xf * c_xform;
scale = p_canvas->parent_scale; scale = p_canvas->parent_scale;
} }
xf = xf * p_canvas_data->transform; Transform2D c_xform = p_canvas_data->transform;
if (p_viewport->snap_2d_transforms_to_pixel) {
c_xform.elements[2] = c_xform.elements[2].floor();
}
xf = xf * c_xform;
if (scale != 1.0 && !RSG::canvas->disable_scale) { if (scale != 1.0 && !RSG::canvas->disable_scale) {
Vector2 pivot = p_vp_size * 0.5; Vector2 pivot = p_vp_size * 0.5;
@ -255,7 +265,7 @@ void RenderingServerViewport::_draw_viewport(Viewport *p_viewport, XRInterface::
ptr = ptr->filter_next_ptr; ptr = ptr->filter_next_ptr;
} }
RSG::canvas->render_canvas(p_viewport->render_target, canvas, xform, canvas_lights, lights_with_mask, clip_rect, p_viewport->texture_filter, p_viewport->texture_repeat); RSG::canvas->render_canvas(p_viewport->render_target, canvas, xform, canvas_lights, lights_with_mask, clip_rect, p_viewport->texture_filter, p_viewport->texture_repeat, p_viewport->snap_2d_transforms_to_pixel, p_viewport->snap_2d_vertices_to_pixel);
i++; i++;
if (scenario_draw_canvas_bg && E->key().get_layer() >= scenario_canvas_max_layer) { if (scenario_draw_canvas_bg && E->key().get_layer() >= scenario_canvas_max_layer) {
@ -774,6 +784,18 @@ float RenderingServerViewport::viewport_get_measured_render_time_gpu(RID p_viewp
return double((viewport->time_gpu_end - viewport->time_gpu_begin) / 1000) / 1000.0; return double((viewport->time_gpu_end - viewport->time_gpu_begin) / 1000) / 1000.0;
} }
void RenderingServerViewport::viewport_set_snap_2d_transforms_to_pixel(RID p_viewport, bool p_enabled) {
Viewport *viewport = viewport_owner.getornull(p_viewport);
ERR_FAIL_COND(!viewport);
viewport->snap_2d_transforms_to_pixel = p_enabled;
}
void RenderingServerViewport::viewport_set_snap_2d_vertices_to_pixel(RID p_viewport, bool p_enabled) {
Viewport *viewport = viewport_owner.getornull(p_viewport);
ERR_FAIL_COND(!viewport);
viewport->snap_2d_vertices_to_pixel = p_enabled;
}
void RenderingServerViewport::viewport_set_default_canvas_item_texture_filter(RID p_viewport, RS::CanvasItemTextureFilter p_filter) { void RenderingServerViewport::viewport_set_default_canvas_item_texture_filter(RID p_viewport, RS::CanvasItemTextureFilter p_filter) {
ERR_FAIL_COND_MSG(p_filter == RS::CANVAS_ITEM_TEXTURE_FILTER_DEFAULT, "Viewport does not accept DEFAULT as texture filter (it's the topmost choice already).)"); ERR_FAIL_COND_MSG(p_filter == RS::CANVAS_ITEM_TEXTURE_FILTER_DEFAULT, "Viewport does not accept DEFAULT as texture filter (it's the topmost choice already).)");
Viewport *viewport = viewport_owner.getornull(p_viewport); Viewport *viewport = viewport_owner.getornull(p_viewport);

View File

@ -70,6 +70,9 @@ public:
bool disable_environment; bool disable_environment;
bool measure_render_time; bool measure_render_time;
bool snap_2d_transforms_to_pixel;
bool snap_2d_vertices_to_pixel;
uint64_t time_cpu_begin; uint64_t time_cpu_begin;
uint64_t time_cpu_end; uint64_t time_cpu_end;
@ -136,6 +139,9 @@ public:
screen_space_aa = RS::VIEWPORT_SCREEN_SPACE_AA_DISABLED; screen_space_aa = RS::VIEWPORT_SCREEN_SPACE_AA_DISABLED;
use_debanding = false; use_debanding = false;
snap_2d_transforms_to_pixel = false;
snap_2d_vertices_to_pixel = false;
for (int i = 0; i < RS::VIEWPORT_RENDER_INFO_MAX; i++) { for (int i = 0; i < RS::VIEWPORT_RENDER_INFO_MAX; i++) {
render_info[i] = 0; render_info[i] = 0;
} }
@ -220,6 +226,9 @@ public:
float viewport_get_measured_render_time_cpu(RID p_viewport) const; float viewport_get_measured_render_time_cpu(RID p_viewport) const;
float viewport_get_measured_render_time_gpu(RID p_viewport) const; float viewport_get_measured_render_time_gpu(RID p_viewport) const;
void viewport_set_snap_2d_transforms_to_pixel(RID p_viewport, bool p_enabled);
void viewport_set_snap_2d_vertices_to_pixel(RID p_viewport, bool p_enabled);
void viewport_set_default_canvas_item_texture_filter(RID p_viewport, RS::CanvasItemTextureFilter p_filter); void viewport_set_default_canvas_item_texture_filter(RID p_viewport, RS::CanvasItemTextureFilter p_filter);
void viewport_set_default_canvas_item_texture_repeat(RID p_viewport, RS::CanvasItemTextureRepeat p_repeat); void viewport_set_default_canvas_item_texture_repeat(RID p_viewport, RS::CanvasItemTextureRepeat p_repeat);

View File

@ -431,6 +431,8 @@ public:
FUNC2(viewport_remove_canvas, RID, RID) FUNC2(viewport_remove_canvas, RID, RID)
FUNC3(viewport_set_canvas_transform, RID, RID, const Transform2D &) FUNC3(viewport_set_canvas_transform, RID, RID, const Transform2D &)
FUNC2(viewport_set_transparent_background, RID, bool) FUNC2(viewport_set_transparent_background, RID, bool)
FUNC2(viewport_set_snap_2d_transforms_to_pixel, RID, bool)
FUNC2(viewport_set_snap_2d_vertices_to_pixel, RID, bool)
FUNC2(viewport_set_default_canvas_item_texture_filter, RID, CanvasItemTextureFilter) FUNC2(viewport_set_default_canvas_item_texture_filter, RID, CanvasItemTextureFilter)
FUNC2(viewport_set_default_canvas_item_texture_repeat, RID, CanvasItemTextureRepeat) FUNC2(viewport_set_default_canvas_item_texture_repeat, RID, CanvasItemTextureRepeat)

View File

@ -720,6 +720,8 @@ public:
virtual void viewport_remove_canvas(RID p_viewport, RID p_canvas) = 0; virtual void viewport_remove_canvas(RID p_viewport, RID p_canvas) = 0;
virtual void viewport_set_canvas_transform(RID p_viewport, RID p_canvas, const Transform2D &p_offset) = 0; virtual void viewport_set_canvas_transform(RID p_viewport, RID p_canvas, const Transform2D &p_offset) = 0;
virtual void viewport_set_transparent_background(RID p_viewport, bool p_enabled) = 0; virtual void viewport_set_transparent_background(RID p_viewport, bool p_enabled) = 0;
virtual void viewport_set_snap_2d_transforms_to_pixel(RID p_viewport, bool p_enabled) = 0;
virtual void viewport_set_snap_2d_vertices_to_pixel(RID p_viewport, bool p_enabled) = 0;
virtual void viewport_set_default_canvas_item_texture_filter(RID p_viewport, CanvasItemTextureFilter p_filter) = 0; virtual void viewport_set_default_canvas_item_texture_filter(RID p_viewport, CanvasItemTextureFilter p_filter) = 0;
virtual void viewport_set_default_canvas_item_texture_repeat(RID p_viewport, CanvasItemTextureRepeat p_repeat) = 0; virtual void viewport_set_default_canvas_item_texture_repeat(RID p_viewport, CanvasItemTextureRepeat p_repeat) = 0;