Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -836,6 +836,7 @@ void RenderForwardClustered::_fill_instance_data(RenderListType p_render_list, i

uint32_t repeats = 0;
GeometryInstanceSurfaceDataCache *prev_surface = nullptr;
GeometryInstanceSurfaceDataCache::SortKey prev_sort_key = {};
for (uint32_t i = 0; i < element_total; i++) {
GeometryInstanceSurfaceDataCache *surface = rl->elements[i + p_offset];
GeometryInstanceForwardClustered *inst = surface->owner;
Expand Down Expand Up @@ -895,7 +896,12 @@ void RenderForwardClustered::_fill_instance_data(RenderListType p_render_list, i

const bool cant_repeat = instance_data.flags & INSTANCE_DATA_FLAG_MULTIMESH || inst->mesh_instance.is_valid();

if (prev_surface != nullptr && !cant_repeat && prev_surface->sort.sort_key1 == surface->sort.sort_key1 && prev_surface->sort.sort_key2 == surface->sort.sort_key2 && prev_surface->uses_lightmap_specular == surface->uses_lightmap_specular && inst->mirror == prev_surface->owner->mirror && repeats < RenderElementInfo::MAX_REPEATS) {
// Strip out values that do not matter for equivalency.
GeometryInstanceSurfaceDataCache::SortKey sort_key = surface->sort;
sort_key.depth_layer = 0;
sort_key.priority = 0;

if (prev_surface != nullptr && !cant_repeat && prev_sort_key.sort_key1 == sort_key.sort_key1 && prev_sort_key.sort_key2 == sort_key.sort_key2 && prev_surface->uses_lightmap_specular == surface->uses_lightmap_specular && inst->mirror == prev_surface->owner->mirror && repeats < RenderElementInfo::MAX_REPEATS) {
//this element is the same as the previous one, count repeats to draw it using instancing
repeats++;
} else {
Expand All @@ -913,13 +919,14 @@ void RenderForwardClustered::_fill_instance_data(RenderListType p_render_list, i
RenderElementInfo &element_info = rl->element_info[p_offset + i];

// Manually bit shift and OR the "uses_lightmap_specular" value to avoid redundant memory stores generated by the compiler.
element_info.value = uint32_t(surface->sort.sort_key1 & 0xFFF) | (uint32_t(surface->uses_lightmap_specular) << 12);
element_info.value = uint32_t((surface->sort.sort_key1 >> 4) & 0xFFF) | (uint32_t(surface->uses_lightmap_specular) << 12);

if (cant_repeat) {
prev_surface = nullptr;
} else {
prev_surface = surface;
}
prev_sort_key = sort_key;
}

if (repeats > 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -521,12 +521,14 @@ class RenderForwardClustered : public RendererSceneRenderRD {
FLAG_USES_STENCIL = 262144,
};

union {
union SortKey {
struct {
uint64_t sort_key1;
uint64_t sort_key2;
};
struct {
uint64_t depth_layer : 4;

// Needs to be grouped together to be used in RenderElementInfo, as the value is masked directly.
uint64_t lod_index : 8;
uint64_t uses_softshadow : 1;
Expand All @@ -538,7 +540,6 @@ class RenderForwardClustered : public RendererSceneRenderRD {
// Sorted based on optimal order for respecting priority and reducing the amount of rebinding of shaders, materials,
// and geometry. This current order was found to be the most optimal in large projects. If you wish to measure
// differences, refer to RenderingDeviceGraph and the methods available to print statistics for draw lists.
uint64_t depth_layer : 4;
uint64_t surface_index : 8;
uint64_t geometry_id : 32;
uint64_t material_id_hi : 8;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2204,7 +2204,7 @@ void RenderForwardMobile::_fill_instance_data(RenderListType p_render_list, uint
RenderElementInfo &element_info = rl->element_info[p_offset + i];

// Sets lod_index, uses_lightmap, and uses_lightmap_specular at once.
element_info.value = uint32_t(surface->sort.sort_key1 & 0x3FF);
element_info.value = uint32_t((surface->sort.sort_key1 >> 4) & 0x3FF);
}

if (p_update_buffer && element_total > 0u) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -493,6 +493,8 @@ class RenderForwardMobile : public RendererSceneRenderRD {
uint64_t sort_key2;
};
struct {
uint64_t depth_layer : 4;

// Needs to be grouped together to be used in RenderElementInfo, as the value is masked directly.
uint64_t lod_index : 8;
uint64_t uses_lightmap : 1;
Expand All @@ -502,7 +504,6 @@ class RenderForwardMobile : public RendererSceneRenderRD {
// Sorted based on optimal order for respecting priority and reducing the amount of rebinding of shaders, materials,
// and geometry. This current order was found to be the most optimal in large projects. If you wish to measure
// differences, refer to RenderingDeviceGraph and the methods available to print statistics for draw lists.
uint64_t depth_layer : 4;
uint64_t surface_index : 8;
uint64_t geometry_id : 32;
uint64_t material_id_hi : 8;
Expand Down
Loading