Repository: godotengine/godot (language: cpp)

Issue:
`CanvasItem.clip_children` has no effect
### Tested versions

- Reproducible in: 4.4.beta [a013481b0911e59cc3f3dea7ebb732450c3e1460]
- Not reproducible in: 4.4.beta1 [d33da79d3f8fe84be2521d25b9ba8e440cf25a88]

### System information

Godot v4.4.beta (a013481b0) - Windows 11 (build 26100) - Multi-window, 3 monitors - Vulkan (Forward+) - integrated AMD Radeon(TM) Graphics (Advanced Micro Devices, Inc.; 32.0.11029.1008) - AMD Ryzen 9 7940HS w/ Radeon 780M Graphics (16 threads)

### Issue description

The `CanvasItem` property `clip_children`, which is meant to allow the parent to act as a mask for any of its `CanvasItem` children, seemingly has no effect in current `master` (a013481b0911e59cc3f3dea7ebb732450c3e1460).

Here's what it looked like in 4.4-beta1:

![Image](https://github.com/user-attachments/assets/c0156e4c-641d-4153-82d8-e9b3d70a215a)

Here's what it looks like now:

![Image](https://github.com/user-attachments/assets/fc242ecb-21ee-45fd-ad08-f60051753fb9)

### Steps to reproduce

- Open the MRP in latest `master`.
- Observe the issue.

### Minimal reproduction project (MRP)

[clip-children-bug-4.4.zip](https://github.com/user-attachments/files/18585252/clip-children-bug-4.4.zip)


Discussion / hints:
Bisected #101998 as the cause.

CC @stuartcarnie.

Fix patch:
diff --git a/drivers/gles3/rasterizer_canvas_gles3.cpp b/drivers/gles3/rasterizer_canvas_gles3.cpp
index 8cbbda249b00..0ca013abd1a0 100644
--- a/drivers/gles3/rasterizer_canvas_gles3.cpp
+++ b/drivers/gles3/rasterizer_canvas_gles3.cpp
@@ -568,8 +568,6 @@ void RasterizerCanvasGLES3::canvas_render_items(RID p_to_render_target, Item *p_
 
 	// Clear out state used in 2D pass
 	reset_canvas();
-	state.current_batch_index = 0;
-	state.canvas_instance_batches.clear();
 	state.current_data_buffer_index = (state.current_data_buffer_index + 1) % state.canvas_instance_data_buffers.size();
 	state.current_instance_buffer_index = 0;
 }
@@ -807,6 +805,8 @@ void RasterizerCanvasGLES3::_render_items(RID p_to_render_target, int p_item_cou
 	}
 
 	glDisable(GL_SCISSOR_TEST);
+	state.current_batch_index = 0;
+	state.canvas_instance_batches.clear();
 	state.last_item_index += index;
 }
 
@@ -1567,7 +1567,9 @@ void RasterizerCanvasGLES3::_add_to_batch(uint32_t &r_index, bool &r_batch_broke
 
 void RasterizerCanvasGLES3::_new_batch(bool &r_batch_broken) {
 	if (state.canvas_instance_batches.size() == 0) {
-		state.canvas_instance_batches.push_back(Batch());
+		Batch new_batch;
+		new_batch.instance_buffer_index = state.current_instance_buffer_index;
+		state.canvas_instance_batches.push_back(new_batch);
 		return;
 	}
 
diff --git a/servers/rendering/renderer_rd/renderer_canvas_render_rd.cpp b/servers/rendering/renderer_rd/renderer_canvas_render_rd.cpp
index 6388bc864110..172d6e547118 100644
--- a/servers/rendering/renderer_rd/renderer_canvas_render_rd.cpp
+++ b/servers/rendering/renderer_rd/renderer_canvas_render_rd.cpp
@@ -914,8 +914,6 @@ void RendererCanvasRenderRD::canvas_render_items(RID p_to_render_target, Item *p
 	}
 
 	texture_info_map.clear();
-	state.current_batch_index = 0;
-	state.canvas_instance_batches.clear();
 	state.current_data_buffer_index = (state.current_data_buffer_index + 1) % BATCH_DATA_BUFFER_COUNT;
 	state.current_instance_buffer_index = 0;
 }
@@ -2289,6 +2287,8 @@ void RendererCanvasRenderRD::_render_batch_items(RenderTarget p_to_render_target
 
 	RD::get_singleton()->draw_list_end();
 
+	state.current_batch_index = 0;
+	state.canvas_instance_batches.clear();
 	state.last_instance_index += instance_index;
 }
 
@@ -3200,7 +3200,9 @@ void RendererCanvasRenderRD::_render_batch(RD::DrawListID p_draw_list, CanvasSha
 
 RendererCanvasRenderRD::Batch *RendererCanvasRenderRD::_new_batch(bool &r_batch_broken) {
 	if (state.canvas_instance_batches.size() == 0) {
-		state.canvas_instance_batches.push_back(Batch());
+		Batch new_batch;
+		new_batch.instance_buffer_index = state.current_instance_buffer_index;
+		state.canvas_instance_batches.push_back(new_batch);
 		return state.canvas_instance_batches.ptr();
 	}
 


Test patch: