Skip to content

Commit

Permalink
Reduce shader permutations in the compatibility backend
Browse files Browse the repository at this point in the history
  • Loading branch information
clayjohn committed Nov 16, 2024
1 parent 6c05ec3 commit 4532204
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 26 deletions.
40 changes: 27 additions & 13 deletions drivers/gles3/rasterizer_canvas_gles3.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -705,10 +705,9 @@ void RasterizerCanvasGLES3::_render_items(RID p_to_render_target, int p_item_cou
}

GLES3::CanvasMaterialData *material_data = state.canvas_instance_batches[i].material_data;
CanvasShaderGLES3::ShaderVariant variant = state.canvas_instance_batches[i].shader_variant;
uint64_t specialization = 0;
specialization |= uint64_t(state.canvas_instance_batches[i].lights_disabled);
specialization |= uint64_t(!GLES3::Config::get_singleton()->float_texture_supported) << 1;
CanvasShaderGLES3::ShaderVariant variant = CanvasShaderGLES3::MODE_DEFAULT;
uint64_t specialization = state.canvas_instance_batches[i].specialization;
specialization |= GLES3::Config::get_singleton()->float_texture_supported ? 0 : CanvasShaderGLES3::USE_RGBA_SHADOWS;
RID shader_version = data.canvas_shader_default_version;

if (material_data) {
Expand Down Expand Up @@ -868,9 +867,9 @@ void RasterizerCanvasGLES3::_record_item_commands(const Item *p_item, RID p_rend

bool lights_disabled = light_count == 0 && !state.using_directional_lights;

if (lights_disabled != state.canvas_instance_batches[state.current_batch_index].lights_disabled) {
if (lights_disabled != bool(state.canvas_instance_batches[state.current_batch_index].specialization & CanvasShaderGLES3::DISABLE_LIGHTING)) {
_new_batch(r_batch_broken);
state.canvas_instance_batches[state.current_batch_index].lights_disabled = lights_disabled;
state.canvas_instance_batches[state.current_batch_index].specialization |= CanvasShaderGLES3::DISABLE_LIGHTING;
}

const Item::Command *c = p_item->commands;
Expand Down Expand Up @@ -936,7 +935,10 @@ void RasterizerCanvasGLES3::_record_item_commands(const Item *p_item, RID p_rend
state.canvas_instance_batches[state.current_batch_index].tex = rect->texture;
state.canvas_instance_batches[state.current_batch_index].command_type = Item::Command::TYPE_RECT;
state.canvas_instance_batches[state.current_batch_index].command = c;
state.canvas_instance_batches[state.current_batch_index].shader_variant = CanvasShaderGLES3::MODE_QUAD;
state.canvas_instance_batches[state.current_batch_index].specialization &= ~CanvasShaderGLES3::USE_NINEPATCH;
state.canvas_instance_batches[state.current_batch_index].specialization &= ~CanvasShaderGLES3::USE_PRIMITIVE;
state.canvas_instance_batches[state.current_batch_index].specialization &= ~CanvasShaderGLES3::USE_ATTRIBUTES;
state.canvas_instance_batches[state.current_batch_index].specialization &= ~CanvasShaderGLES3::USE_INSTANCING;
}

_prepare_canvas_texture(rect->texture, state.canvas_instance_batches[state.current_batch_index].filter, state.canvas_instance_batches[state.current_batch_index].repeat, r_index, texpixel_size);
Expand Down Expand Up @@ -1026,7 +1028,10 @@ void RasterizerCanvasGLES3::_record_item_commands(const Item *p_item, RID p_rend
state.canvas_instance_batches[state.current_batch_index].tex = np->texture;
state.canvas_instance_batches[state.current_batch_index].command_type = Item::Command::TYPE_NINEPATCH;
state.canvas_instance_batches[state.current_batch_index].command = c;
state.canvas_instance_batches[state.current_batch_index].shader_variant = CanvasShaderGLES3::MODE_NINEPATCH;
state.canvas_instance_batches[state.current_batch_index].specialization |= CanvasShaderGLES3::USE_NINEPATCH;
state.canvas_instance_batches[state.current_batch_index].specialization &= ~CanvasShaderGLES3::USE_PRIMITIVE;
state.canvas_instance_batches[state.current_batch_index].specialization &= ~CanvasShaderGLES3::USE_ATTRIBUTES;
state.canvas_instance_batches[state.current_batch_index].specialization &= ~CanvasShaderGLES3::USE_INSTANCING;
}

_prepare_canvas_texture(np->texture, state.canvas_instance_batches[state.current_batch_index].filter, state.canvas_instance_batches[state.current_batch_index].repeat, r_index, texpixel_size);
Expand Down Expand Up @@ -1092,7 +1097,10 @@ void RasterizerCanvasGLES3::_record_item_commands(const Item *p_item, RID p_rend
state.canvas_instance_batches[state.current_batch_index].tex = polygon->texture;
state.canvas_instance_batches[state.current_batch_index].command_type = Item::Command::TYPE_POLYGON;
state.canvas_instance_batches[state.current_batch_index].command = c;
state.canvas_instance_batches[state.current_batch_index].shader_variant = CanvasShaderGLES3::MODE_ATTRIBUTES;
state.canvas_instance_batches[state.current_batch_index].specialization &= ~CanvasShaderGLES3::USE_NINEPATCH;
state.canvas_instance_batches[state.current_batch_index].specialization &= ~CanvasShaderGLES3::USE_PRIMITIVE;
state.canvas_instance_batches[state.current_batch_index].specialization |= CanvasShaderGLES3::USE_ATTRIBUTES;
state.canvas_instance_batches[state.current_batch_index].specialization &= ~CanvasShaderGLES3::USE_INSTANCING;

_prepare_canvas_texture(polygon->texture, state.canvas_instance_batches[state.current_batch_index].filter, state.canvas_instance_batches[state.current_batch_index].repeat, r_index, texpixel_size);

Expand All @@ -1119,7 +1127,10 @@ void RasterizerCanvasGLES3::_record_item_commands(const Item *p_item, RID p_rend
state.canvas_instance_batches[state.current_batch_index].primitive_points = primitive->point_count;
state.canvas_instance_batches[state.current_batch_index].command_type = Item::Command::TYPE_PRIMITIVE;
state.canvas_instance_batches[state.current_batch_index].command = c;
state.canvas_instance_batches[state.current_batch_index].shader_variant = CanvasShaderGLES3::MODE_PRIMITIVE;
state.canvas_instance_batches[state.current_batch_index].specialization &= ~CanvasShaderGLES3::USE_NINEPATCH;
state.canvas_instance_batches[state.current_batch_index].specialization |= CanvasShaderGLES3::USE_PRIMITIVE;
state.canvas_instance_batches[state.current_batch_index].specialization &= ~CanvasShaderGLES3::USE_ATTRIBUTES;
state.canvas_instance_batches[state.current_batch_index].specialization &= ~CanvasShaderGLES3::USE_INSTANCING;
}

_prepare_canvas_texture(state.canvas_instance_batches[state.current_batch_index].tex, state.canvas_instance_batches[state.current_batch_index].filter, state.canvas_instance_batches[state.current_batch_index].repeat, r_index, texpixel_size);
Expand Down Expand Up @@ -1164,7 +1175,10 @@ void RasterizerCanvasGLES3::_record_item_commands(const Item *p_item, RID p_rend
_new_batch(r_batch_broken);

Color modulate(1, 1, 1, 1);
state.canvas_instance_batches[state.current_batch_index].shader_variant = CanvasShaderGLES3::MODE_ATTRIBUTES;
state.canvas_instance_batches[state.current_batch_index].specialization &= ~CanvasShaderGLES3::USE_NINEPATCH;
state.canvas_instance_batches[state.current_batch_index].specialization &= ~CanvasShaderGLES3::USE_PRIMITIVE;
state.canvas_instance_batches[state.current_batch_index].specialization |= CanvasShaderGLES3::USE_ATTRIBUTES;
state.canvas_instance_batches[state.current_batch_index].specialization &= ~CanvasShaderGLES3::USE_INSTANCING;
if (c->type == Item::Command::TYPE_MESH) {
const Item::CommandMesh *m = static_cast<const Item::CommandMesh *>(c);
state.canvas_instance_batches[state.current_batch_index].tex = m->texture;
Expand All @@ -1174,7 +1188,7 @@ void RasterizerCanvasGLES3::_record_item_commands(const Item *p_item, RID p_rend
} else if (c->type == Item::Command::TYPE_MULTIMESH) {
const Item::CommandMultiMesh *mm = static_cast<const Item::CommandMultiMesh *>(c);
state.canvas_instance_batches[state.current_batch_index].tex = mm->texture;
state.canvas_instance_batches[state.current_batch_index].shader_variant = CanvasShaderGLES3::MODE_INSTANCED;
state.canvas_instance_batches[state.current_batch_index].specialization |= CanvasShaderGLES3::USE_INSTANCING;

if (GLES3::MeshStorage::get_singleton()->multimesh_uses_colors(mm->multimesh)) {
state.instance_data_array[r_index].flags |= FLAGS_INSTANCING_HAS_COLORS;
Expand All @@ -1189,7 +1203,7 @@ void RasterizerCanvasGLES3::_record_item_commands(const Item *p_item, RID p_rend
const Item::CommandParticles *pt = static_cast<const Item::CommandParticles *>(c);
RID particles = pt->particles;
state.canvas_instance_batches[state.current_batch_index].tex = pt->texture;
state.canvas_instance_batches[state.current_batch_index].shader_variant = CanvasShaderGLES3::MODE_INSTANCED;
state.canvas_instance_batches[state.current_batch_index].specialization |= CanvasShaderGLES3::USE_INSTANCING;
state.instance_data_array[r_index].flags |= FLAGS_INSTANCING_HAS_COLORS;
state.instance_data_array[r_index].flags |= FLAGS_INSTANCING_HAS_CUSTOM_DATA;

Expand Down
5 changes: 3 additions & 2 deletions drivers/gles3/rasterizer_canvas_gles3.h
Original file line number Diff line number Diff line change
Expand Up @@ -273,14 +273,15 @@ class RasterizerCanvasGLES3 : public RendererCanvasRender {

RID material;
GLES3::CanvasMaterialData *material_data = nullptr;
CanvasShaderGLES3::ShaderVariant shader_variant = CanvasShaderGLES3::MODE_QUAD;
uint64_t vertex_input_mask = RS::ARRAY_FORMAT_VERTEX | RS::ARRAY_FORMAT_COLOR | RS::ARRAY_FORMAT_TEX_UV;
uint64_t specialization = 0;
//CanvasShaderGLES3::ShaderVariant shader_variant = CanvasShaderGLES3::MODE_QUAD;

const Item::Command *command = nullptr;
Item::Command::Type command_type = Item::Command::TYPE_ANIMATION_SLICE; // Can default to any type that doesn't form a batch.
uint32_t primitive_points = 0;

bool lights_disabled = false;
//bool lights_disabled = false;
};

// DataBuffer contains our per-frame data. I.e. the resources that are updated each frame.
Expand Down
11 changes: 5 additions & 6 deletions drivers/gles3/shaders/canvas.glsl
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
/* clang-format off */
#[modes]

mode_quad =
mode_ninepatch = #define USE_NINEPATCH
mode_primitive = #define USE_PRIMITIVE
mode_attributes = #define USE_ATTRIBUTES
mode_instanced = #define USE_ATTRIBUTES \n#define USE_INSTANCING
mode_default =

#[specializations]

DISABLE_LIGHTING = true
USE_RGBA_SHADOWS = false
SINGLE_INSTANCE = false
USE_NINEPATCH = false
USE_PRIMITIVE = false
USE_ATTRIBUTES = false
USE_INSTANCING = false

#[vertex]

Expand Down
8 changes: 3 additions & 5 deletions drivers/gles3/shaders/sky.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,15 @@
#[modes]

mode_background =
mode_half_res = #define USE_HALF_RES_PASS
mode_quarter_res = #define USE_QUARTER_RES_PASS
mode_cubemap = #define USE_CUBEMAP_PASS
mode_cubemap_half_res = #define USE_CUBEMAP_PASS \n#define USE_HALF_RES_PASS
mode_cubemap_quarter_res = #define USE_CUBEMAP_PASS \n#define USE_QUARTER_RES_PASS

#[specializations]

USE_MULTIVIEW = false
USE_INVERTED_Y = true
APPLY_TONEMAPPING = true
USE_QUARTER_RES_PASS = false
USE_HALF_RES_PASS = false

#[vertex]

Expand Down Expand Up @@ -56,7 +54,7 @@ uniform sampler2D quarter_res; //texunit:-3

layout(std140) uniform GlobalShaderUniformData { //ubo:1
vec4 global_shader_uniforms[MAX_GLOBAL_SHADER_UNIFORMS];
};
}

struct DirectionalLightData {
vec4 direction_energy;
Expand Down

0 comments on commit 4532204

Please sign in to comment.