Merge pull request #112481 from stuartcarnie/2d_canvas_vbos
Massively optimize canvas 2D rendering by using vertex buffers
This commit is contained in:
@ -772,7 +772,7 @@ void RenderForwardClustered::SceneState::grow_instance_buffer(RenderListType p_r
|
||||
if (instance_buffer[p_render_list].get_size(0u) < p_req_element_count * sizeof(SceneState::InstanceData)) {
|
||||
instance_buffer[p_render_list].uninit();
|
||||
uint32_t new_size = nearest_power_of_2_templated(MAX(uint64_t(INSTANCE_DATA_BUFFER_MIN_SIZE), p_req_element_count));
|
||||
instance_buffer[p_render_list].set_size(0u, new_size * sizeof(SceneState::InstanceData), true);
|
||||
instance_buffer[p_render_list].set_storage_size(0u, new_size * sizeof(SceneState::InstanceData));
|
||||
curr_gpu_ptr[p_render_list] = nullptr;
|
||||
}
|
||||
|
||||
@ -3281,7 +3281,7 @@ RID RenderForwardClustered::_setup_render_pass_uniform_set(RenderListType p_rend
|
||||
if (scene_state.instance_buffer[p_render_list].get_size(0u) == 0u) {
|
||||
// Any buffer will do since it's not used, so just create one.
|
||||
// We can't use scene_shader.default_vec4_xform_buffer because it's not dynamic.
|
||||
scene_state.instance_buffer[p_render_list].set_size(0u, INSTANCE_DATA_BUFFER_MIN_SIZE * sizeof(SceneState::InstanceData), true);
|
||||
scene_state.instance_buffer[p_render_list].set_storage_size(0u, INSTANCE_DATA_BUFFER_MIN_SIZE * sizeof(SceneState::InstanceData));
|
||||
scene_state.instance_buffer[p_render_list].prepare_for_upload();
|
||||
}
|
||||
RID instance_buffer = scene_state.instance_buffer[p_render_list]._get(0u);
|
||||
@ -3650,7 +3650,7 @@ RID RenderForwardClustered::_setup_sdfgi_render_pass_uniform_set(RID p_albedo_te
|
||||
if (scene_state.instance_buffer[RENDER_LIST_SECONDARY].get_size(0u) == 0u) {
|
||||
// Any buffer will do since it's not used, so just create one.
|
||||
// We can't use scene_shader.default_vec4_xform_buffer because it's not dynamic.
|
||||
scene_state.instance_buffer[RENDER_LIST_SECONDARY].set_size(0u, INSTANCE_DATA_BUFFER_MIN_SIZE * sizeof(SceneState::InstanceData), true);
|
||||
scene_state.instance_buffer[RENDER_LIST_SECONDARY].set_storage_size(0u, INSTANCE_DATA_BUFFER_MIN_SIZE * sizeof(SceneState::InstanceData));
|
||||
scene_state.instance_buffer[RENDER_LIST_SECONDARY].prepare_for_upload();
|
||||
}
|
||||
RID instance_buffer = scene_state.instance_buffer[RENDER_LIST_SECONDARY]._get(0u);
|
||||
|
||||
@ -467,7 +467,7 @@ RID RenderForwardMobile::_setup_render_pass_uniform_set(RenderListType p_render_
|
||||
if (scene_state.instance_buffer[p_render_list].get_size(0u) == 0u) {
|
||||
// Any buffer will do since it's not used, so just create one.
|
||||
// We can't use scene_shader.default_vec4_xform_buffer because it's not dynamic.
|
||||
scene_state.instance_buffer[p_render_list].set_size(0u, INSTANCE_DATA_BUFFER_MIN_SIZE * sizeof(SceneState::InstanceData), true);
|
||||
scene_state.instance_buffer[p_render_list].set_storage_size(0u, INSTANCE_DATA_BUFFER_MIN_SIZE * sizeof(SceneState::InstanceData));
|
||||
scene_state.instance_buffer[p_render_list].prepare_for_upload();
|
||||
}
|
||||
RID instance_buffer = scene_state.instance_buffer[p_render_list]._get(0u);
|
||||
@ -1910,7 +1910,7 @@ void RenderForwardMobile::SceneState::grow_instance_buffer(RenderListType p_rend
|
||||
if (instance_buffer[p_render_list].get_size(0u) < p_req_element_count * sizeof(SceneState::InstanceData)) {
|
||||
instance_buffer[p_render_list].uninit();
|
||||
uint32_t new_size = nearest_power_of_2_templated(MAX(uint64_t(INSTANCE_DATA_BUFFER_MIN_SIZE), p_req_element_count));
|
||||
instance_buffer[p_render_list].set_size(0u, new_size * sizeof(SceneState::InstanceData), true);
|
||||
instance_buffer[p_render_list].set_storage_size(0u, new_size * sizeof(SceneState::InstanceData));
|
||||
curr_gpu_ptr[p_render_list] = nullptr;
|
||||
}
|
||||
|
||||
@ -2204,7 +2204,7 @@ void RenderForwardMobile::_setup_environment(const RenderDataRD *p_render_data,
|
||||
|
||||
// May do this earlier in RenderSceneRenderRD::render_scene
|
||||
if (scene_state.uniform_buffers.get_size(0u) == 0u) {
|
||||
scene_state.uniform_buffers.set_size(0u, p_render_data->scene_data->get_uniform_buffer_size_bytes(), false);
|
||||
scene_state.uniform_buffers.set_uniform_size(0u, p_render_data->scene_data->get_uniform_buffer_size_bytes());
|
||||
}
|
||||
|
||||
float luminance_multiplier = p_render_data->render_buffers.is_valid() ? p_render_data->render_buffers->get_luminance_multiplier() : 1.0;
|
||||
|
||||
@ -35,6 +35,7 @@
|
||||
#include "core/math/math_defs.h"
|
||||
#include "core/math/math_funcs.h"
|
||||
#include "core/math/transform_interpolator.h"
|
||||
#include "core/templates/fixed_vector.h"
|
||||
#include "servers/rendering/renderer_rd/storage_rd/material_storage.h"
|
||||
#include "servers/rendering/renderer_rd/storage_rd/mesh_storage.h"
|
||||
#include "servers/rendering/renderer_rd/storage_rd/particles_storage.h"
|
||||
@ -914,6 +915,11 @@ void RendererCanvasRenderRD::canvas_render_items(RID p_to_render_target, Item *p
|
||||
}
|
||||
|
||||
texture_info_map.clear();
|
||||
|
||||
// Save the previous instance data pointer in case more items are rendered in the same frame.
|
||||
state.prev_instance_data = state.instance_data;
|
||||
state.prev_instance_data_index = state.instance_data_index;
|
||||
|
||||
state.instance_data = nullptr;
|
||||
if (state.instance_data_index > 0) {
|
||||
// If there was any remaining instance data, it must be flushed.
|
||||
@ -1728,8 +1734,8 @@ RendererCanvasRenderRD::RendererCanvasRenderRD() {
|
||||
default_samplers.default_repeat = RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED;
|
||||
}
|
||||
|
||||
// preallocate 5 slots for uniform set 3
|
||||
state.batch_texture_uniforms.resize(5);
|
||||
// preallocate slots for uniform set 3
|
||||
state.batch_texture_uniforms.resize(4);
|
||||
|
||||
{ //shader variants
|
||||
|
||||
@ -1750,10 +1756,7 @@ RendererCanvasRenderRD::RendererCanvasRenderRD() {
|
||||
variants.push_back(base_define + "#define USE_ATTRIBUTES\n#define USE_POINT_SIZE\n"); // SHADER_VARIANT_ATTRIBUTES_POINTS
|
||||
}
|
||||
|
||||
Vector<uint64_t> dynamic_buffers;
|
||||
dynamic_buffers.push_back(ShaderRD::DynamicBuffer::encode(BATCH_UNIFORM_SET, 4));
|
||||
|
||||
shader.canvas_shader.initialize(variants, global_defines, {}, dynamic_buffers);
|
||||
shader.canvas_shader.initialize(variants, global_defines, {}, {});
|
||||
|
||||
shader.default_version_data = memnew(CanvasShaderData);
|
||||
shader.default_version_data->version = shader.canvas_shader.version_create();
|
||||
@ -1786,7 +1789,7 @@ RendererCanvasRenderRD::RendererCanvasRenderRD() {
|
||||
actions.renames["NORMAL_MAP"] = "normal_map";
|
||||
actions.renames["NORMAL_MAP_DEPTH"] = "normal_map_depth";
|
||||
actions.renames["TEXTURE"] = "color_texture";
|
||||
actions.renames["TEXTURE_PIXEL_SIZE"] = "draw_data.color_texture_pixel_size";
|
||||
actions.renames["TEXTURE_PIXEL_SIZE"] = "read_draw_data_color_texture_pixel_size";
|
||||
actions.renames["NORMAL_TEXTURE"] = "normal_texture";
|
||||
actions.renames["SPECULAR_SHININESS_TEXTURE"] = "specular_texture";
|
||||
actions.renames["SPECULAR_SHININESS"] = "specular_shininess";
|
||||
@ -1840,7 +1843,7 @@ RendererCanvasRenderRD::RendererCanvasRenderRD() {
|
||||
actions.base_varying_index = 5;
|
||||
|
||||
actions.global_buffer_array_variable = "global_shader_uniforms.data";
|
||||
actions.instance_uniform_index_variable = "instances.data[instance_index].instance_uniforms_ofs";
|
||||
actions.instance_uniform_index_variable = "read_draw_data_instance_offset";
|
||||
|
||||
shader.compiler.initialize(actions);
|
||||
}
|
||||
@ -1954,6 +1957,64 @@ RendererCanvasRenderRD::RendererCanvasRenderRD() {
|
||||
shader.quad_index_array = RD::get_singleton()->index_array_create(shader.quad_index_buffer, 0, 6);
|
||||
}
|
||||
|
||||
{
|
||||
Vector<RD::VertexAttribute> vf;
|
||||
uint32_t offset = 0;
|
||||
RD::VertexAttribute vd;
|
||||
vd.format = RD::DATA_FORMAT_R32G32B32A32_SFLOAT;
|
||||
vd.stride = sizeof(InstanceData);
|
||||
vd.frequency = RD::VERTEX_FREQUENCY_INSTANCE;
|
||||
vd.location = 8;
|
||||
vd.binding = 0; // Explicitly assign binding 0 for instance data.
|
||||
vd.offset = offset;
|
||||
offset += sizeof(float) * 4;
|
||||
vf.push_back(vd); // attrib_A
|
||||
|
||||
vd.location = 9;
|
||||
vd.offset = offset;
|
||||
offset += sizeof(float) * 4;
|
||||
vf.push_back(vd); // attrib_B
|
||||
|
||||
vd.location = 10;
|
||||
vd.offset = offset;
|
||||
offset += sizeof(float) * 4;
|
||||
vf.push_back(vd); // attrib_C
|
||||
|
||||
vd.location = 11;
|
||||
vd.offset = offset;
|
||||
offset += sizeof(float) * 4;
|
||||
vf.push_back(vd); // attrib_D
|
||||
|
||||
vd.location = 12;
|
||||
vd.offset = offset;
|
||||
offset += sizeof(float) * 4;
|
||||
vf.push_back(vd); // attrib_E
|
||||
|
||||
uint32_t attrib_F_index = vf.size();
|
||||
vd.location = 13;
|
||||
vd.offset = offset;
|
||||
offset += sizeof(float) * 4;
|
||||
vf.push_back(vd); // attrib_F (RECT, NINEPATCH)
|
||||
|
||||
vd.format = RD::DATA_FORMAT_R32G32B32A32_UINT;
|
||||
vd.location = 14;
|
||||
vd.offset = offset;
|
||||
offset += sizeof(uint32_t) * 4;
|
||||
vf.push_back(vd); // attrib_G
|
||||
|
||||
vd.location = 15;
|
||||
vd.offset = offset;
|
||||
offset += sizeof(uint32_t) * 4;
|
||||
vf.push_back(vd); // attrib_H
|
||||
|
||||
// RECT, NINEPATCH
|
||||
shader.quad_vertex_format_id = RD::get_singleton()->vertex_format_create(vf);
|
||||
|
||||
// PRIMITIVE
|
||||
vf.write[attrib_F_index].format = RD::DATA_FORMAT_R32G32B32A32_UINT;
|
||||
shader.primitive_vertex_format_id = RD::get_singleton()->vertex_format_create(vf);
|
||||
}
|
||||
|
||||
{ //primitive
|
||||
primitive_arrays.index_array[0] = RD::get_singleton()->index_array_create(shader.quad_index_buffer, 0, 1);
|
||||
primitive_arrays.index_array[1] = RD::get_singleton()->index_array_create(shader.quad_index_buffer, 0, 2);
|
||||
@ -2064,7 +2125,7 @@ void fragment() {
|
||||
state.max_instances_per_buffer = uint32_t(GLOBAL_GET("rendering/2d/batching/item_buffer_size"));
|
||||
state.max_instance_buffer_size = state.max_instances_per_buffer * sizeof(InstanceData);
|
||||
state.canvas_instance_batches.reserve(200);
|
||||
state.instance_buffers.set_size(0, state.max_instance_buffer_size, true);
|
||||
state.instance_buffers.set_vertex_size(0, state.max_instance_buffer_size);
|
||||
}
|
||||
}
|
||||
|
||||
@ -2131,8 +2192,6 @@ void RendererCanvasRenderRD::_render_batch_items(RenderTarget p_to_render_target
|
||||
// First item always forms its own batch.
|
||||
bool batch_broken = false;
|
||||
Batch *current_batch = _new_batch(batch_broken);
|
||||
// Override the start position and index as we want to start from where we finished off last time.
|
||||
current_batch->start = state.instance_data_index;
|
||||
|
||||
for (int i = 0; i < p_item_count; i++) {
|
||||
Item *ci = items[i];
|
||||
@ -2384,6 +2443,19 @@ void RendererCanvasRenderRD::_record_item_commands(const Item *p_item, RenderTar
|
||||
_prepare_batch_texture_info(rect->texture, tex_state, tex_info);
|
||||
}
|
||||
|
||||
if (has_msdf != r_current_batch->use_msdf || rect->px_range != r_current_batch->msdf_pix_range || rect->outline != r_current_batch->msdf_outline) {
|
||||
r_current_batch = _new_batch(r_batch_broken);
|
||||
r_current_batch->use_msdf = has_msdf;
|
||||
r_current_batch->msdf_pix_range = rect->px_range;
|
||||
r_current_batch->msdf_outline = rect->outline;
|
||||
}
|
||||
|
||||
bool has_lcd = bool(rect->flags & CANVAS_RECT_LCD);
|
||||
if (has_lcd != r_current_batch->use_lcd) {
|
||||
r_current_batch = _new_batch(r_batch_broken);
|
||||
r_current_batch->use_lcd = has_lcd;
|
||||
}
|
||||
|
||||
if (r_current_batch->tex_info != tex_info) {
|
||||
r_current_batch = _new_batch(r_batch_broken);
|
||||
r_current_batch->tex_info = tex_info;
|
||||
@ -2437,16 +2509,6 @@ void RendererCanvasRenderRD::_record_item_commands(const Item *p_item, RenderTar
|
||||
src_rect = Rect2(0, 0, 1, 1);
|
||||
}
|
||||
|
||||
if (has_msdf) {
|
||||
instance_data->flags |= INSTANCE_FLAGS_USE_MSDF;
|
||||
instance_data->msdf[0] = rect->px_range; // Pixel range.
|
||||
instance_data->msdf[1] = rect->outline; // Outline size.
|
||||
instance_data->msdf[2] = 0.f; // Reserved.
|
||||
instance_data->msdf[3] = 0.f; // Reserved.
|
||||
} else if (rect->flags & CANVAS_RECT_LCD) {
|
||||
instance_data->flags |= INSTANCE_FLAGS_USE_LCD;
|
||||
}
|
||||
|
||||
instance_data->modulation[0] = modulated.r;
|
||||
instance_data->modulation[1] = modulated.g;
|
||||
instance_data->modulation[2] = modulated.b;
|
||||
@ -2500,8 +2562,8 @@ void RendererCanvasRenderRD::_record_item_commands(const Item *p_item, RenderTar
|
||||
} else {
|
||||
if (np->source != Rect2()) {
|
||||
src_rect = Rect2(np->source.position.x * tex_info->texpixel_size.width, np->source.position.y * tex_info->texpixel_size.height, np->source.size.x * tex_info->texpixel_size.width, np->source.size.y * tex_info->texpixel_size.height);
|
||||
instance_data->color_texture_pixel_size[0] = 1.0 / np->source.size.width;
|
||||
instance_data->color_texture_pixel_size[1] = 1.0 / np->source.size.height;
|
||||
instance_data->ninepatch_pixel_size[0] = 1.0 / np->source.size.width;
|
||||
instance_data->ninepatch_pixel_size[1] = 1.0 / np->source.size.height;
|
||||
} else {
|
||||
src_rect = Rect2(0, 0, 1, 1);
|
||||
}
|
||||
@ -2572,7 +2634,7 @@ void RendererCanvasRenderRD::_record_item_commands(const Item *p_item, RenderTar
|
||||
r_current_batch->render_primitive = _primitive_type_to_render_primitive(polygon->primitive);
|
||||
}
|
||||
|
||||
InstanceData *instance_data = new_instance_data(*r_current_batch, template_instance);
|
||||
InstanceData *instance_data = new_instance_data(*r_current_batch, template_instance, true);
|
||||
|
||||
Color color = base_color;
|
||||
if (use_linear_colors) {
|
||||
@ -2583,8 +2645,6 @@ void RendererCanvasRenderRD::_record_item_commands(const Item *p_item, RenderTar
|
||||
instance_data->modulation[1] = color.g;
|
||||
instance_data->modulation[2] = color.b;
|
||||
instance_data->modulation[3] = color.a;
|
||||
|
||||
_add_to_batch(r_batch_broken, r_current_batch);
|
||||
} break;
|
||||
|
||||
case Item::Command::TYPE_PRIMITIVE: {
|
||||
@ -2693,7 +2753,7 @@ void RendererCanvasRenderRD::_record_item_commands(const Item *p_item, RenderTar
|
||||
_prepare_batch_texture_info(m->texture, tex_state, tex_info);
|
||||
}
|
||||
r_current_batch->tex_info = tex_info;
|
||||
instance_data = new_instance_data(*r_current_batch, template_instance);
|
||||
instance_data = new_instance_data(*r_current_batch, template_instance, true);
|
||||
|
||||
r_current_batch->mesh_instance_count = 1;
|
||||
_update_transform_2d_to_mat2x3(base_transform * draw_transform * m->transform, instance_data->world);
|
||||
@ -2720,7 +2780,7 @@ void RendererCanvasRenderRD::_record_item_commands(const Item *p_item, RenderTar
|
||||
_prepare_batch_texture_info(mm->texture, tex_state, tex_info);
|
||||
}
|
||||
r_current_batch->tex_info = tex_info;
|
||||
instance_data = new_instance_data(*r_current_batch, template_instance);
|
||||
instance_data = new_instance_data(*r_current_batch, template_instance, true);
|
||||
|
||||
r_current_batch->flags |= 1; // multimesh, trails disabled
|
||||
|
||||
@ -2742,7 +2802,7 @@ void RendererCanvasRenderRD::_record_item_commands(const Item *p_item, RenderTar
|
||||
_prepare_batch_texture_info(pt->texture, tex_state, tex_info);
|
||||
}
|
||||
r_current_batch->tex_info = tex_info;
|
||||
instance_data = new_instance_data(*r_current_batch, template_instance);
|
||||
instance_data = new_instance_data(*r_current_batch, template_instance, true);
|
||||
|
||||
uint32_t divisor = 1;
|
||||
r_current_batch->mesh_instance_count = particles_storage->particles_get_amount(pt->particles, divisor);
|
||||
@ -2784,8 +2844,6 @@ void RendererCanvasRenderRD::_record_item_commands(const Item *p_item, RenderTar
|
||||
instance_data->modulation[1] = modulated.g;
|
||||
instance_data->modulation[2] = modulated.b;
|
||||
instance_data->modulation[3] = modulated.a;
|
||||
|
||||
_add_to_batch(r_batch_broken, r_current_batch);
|
||||
} break;
|
||||
|
||||
case Item::Command::TYPE_TRANSFORM: {
|
||||
@ -2941,7 +2999,7 @@ void RendererCanvasRenderRD::_render_batch(RD::DrawListID p_draw_list, CanvasSha
|
||||
{
|
||||
RendererRD::TextureStorage *ts = RendererRD::TextureStorage::get_singleton();
|
||||
|
||||
RIDSetKey key(p_batch->tex_info->state, p_batch->instance_buffer);
|
||||
RIDSetKey key(p_batch->tex_info->state);
|
||||
|
||||
const RID *uniform_set = rid_set_to_uniform_set.getptr(key);
|
||||
if (uniform_set == nullptr) {
|
||||
@ -2950,7 +3008,6 @@ void RendererCanvasRenderRD::_render_batch(RD::DrawListID p_draw_list, CanvasSha
|
||||
uniform_ptrw[1] = RD::Uniform(RD::UNIFORM_TYPE_TEXTURE, 1, p_batch->tex_info->normal);
|
||||
uniform_ptrw[2] = RD::Uniform(RD::UNIFORM_TYPE_TEXTURE, 2, p_batch->tex_info->specular);
|
||||
uniform_ptrw[3] = RD::Uniform(RD::UNIFORM_TYPE_SAMPLER, 3, p_batch->tex_info->sampler);
|
||||
uniform_ptrw[4] = RD::Uniform(RD::UNIFORM_TYPE_STORAGE_BUFFER_DYNAMIC, 4, p_batch->instance_buffer);
|
||||
|
||||
RID rid = RD::get_singleton()->uniform_set_create(state.batch_texture_uniforms, shader.default_version_rd_shader, BATCH_UNIFORM_SET);
|
||||
ERR_FAIL_COND_MSG(rid.is_null(), "Failed to create uniform set for batch.");
|
||||
@ -2978,10 +3035,6 @@ void RendererCanvasRenderRD::_render_batch(RD::DrawListID p_draw_list, CanvasSha
|
||||
RD::get_singleton()->draw_list_bind_uniform_set(p_draw_list, *uniform_set, BATCH_UNIFORM_SET);
|
||||
}
|
||||
}
|
||||
PushConstant push_constant;
|
||||
push_constant.base_instance_index = p_batch->start;
|
||||
push_constant.specular_shininess = p_batch->tex_info->specular_shininess;
|
||||
push_constant.batch_flags = p_batch->tex_info->flags | p_batch->flags;
|
||||
|
||||
RID pipeline;
|
||||
PipelineKey pipeline_key;
|
||||
@ -2989,18 +3042,26 @@ void RendererCanvasRenderRD::_render_batch(RD::DrawListID p_draw_list, CanvasSha
|
||||
pipeline_key.variant = p_batch->shader_variant;
|
||||
pipeline_key.render_primitive = p_batch->render_primitive;
|
||||
pipeline_key.shader_specialization.use_lighting = p_batch->use_lighting;
|
||||
pipeline_key.shader_specialization.use_msdf = p_batch->use_msdf;
|
||||
pipeline_key.shader_specialization.use_lcd = p_batch->use_lcd;
|
||||
pipeline_key.lcd_blend = p_batch->has_blend;
|
||||
|
||||
switch (p_batch->command_type) {
|
||||
case Item::Command::TYPE_RECT:
|
||||
case Item::Command::TYPE_NINEPATCH: {
|
||||
PushConstant push_constant = p_batch->push_constant();
|
||||
|
||||
pipeline_key.vertex_format_id = shader.quad_vertex_format_id;
|
||||
pipeline = _get_pipeline_specialization_or_ubershader(p_shader_data, pipeline_key, push_constant);
|
||||
RD::get_singleton()->draw_list_bind_render_pipeline(p_draw_list, pipeline);
|
||||
if (p_batch->has_blend) {
|
||||
RD::get_singleton()->draw_list_set_blend_constants(p_draw_list, p_batch->modulate);
|
||||
}
|
||||
|
||||
RD::get_singleton()->draw_list_set_push_constant(p_draw_list, &push_constant, sizeof(PushConstant));
|
||||
RD::get_singleton()->draw_list_set_push_constant(p_draw_list, &push_constant, sizeof(push_constant));
|
||||
FixedVector<RID, 1> vb = { p_batch->instance_buffer };
|
||||
FixedVector<uint64_t, 1> vo = { uint64_t(p_batch->start) * sizeof(InstanceData) };
|
||||
RD::get_singleton()->draw_list_bind_vertex_buffers_format(p_draw_list, shader.quad_vertex_format_id, 1, vb, vo);
|
||||
RD::get_singleton()->draw_list_bind_index_array(p_draw_list, shader.quad_index_array);
|
||||
RD::get_singleton()->draw_list_draw(p_draw_list, true, p_batch->instance_count);
|
||||
|
||||
@ -3013,6 +3074,7 @@ void RendererCanvasRenderRD::_render_batch(RD::DrawListID p_draw_list, CanvasSha
|
||||
|
||||
case Item::Command::TYPE_POLYGON: {
|
||||
ERR_FAIL_NULL(p_batch->command);
|
||||
PushConstantAttributes push_constant = p_batch->push_constant_attributes();
|
||||
|
||||
const Item::CommandPolygon *polygon = static_cast<const Item::CommandPolygon *>(p_batch->command);
|
||||
|
||||
@ -3023,7 +3085,7 @@ void RendererCanvasRenderRD::_render_batch(RD::DrawListID p_draw_list, CanvasSha
|
||||
pipeline = _get_pipeline_specialization_or_ubershader(p_shader_data, pipeline_key, push_constant);
|
||||
RD::get_singleton()->draw_list_bind_render_pipeline(p_draw_list, pipeline);
|
||||
|
||||
RD::get_singleton()->draw_list_set_push_constant(p_draw_list, &push_constant, sizeof(PushConstant));
|
||||
RD::get_singleton()->draw_list_set_push_constant(p_draw_list, &push_constant, sizeof(push_constant));
|
||||
RD::get_singleton()->draw_list_bind_vertex_array(p_draw_list, pb->vertex_array);
|
||||
if (pb->indices.is_valid()) {
|
||||
RD::get_singleton()->draw_list_bind_index_array(p_draw_list, pb->indices);
|
||||
@ -3042,10 +3104,15 @@ void RendererCanvasRenderRD::_render_batch(RD::DrawListID p_draw_list, CanvasSha
|
||||
|
||||
const Item::CommandPrimitive *primitive = static_cast<const Item::CommandPrimitive *>(p_batch->command);
|
||||
|
||||
PushConstant push_constant = p_batch->push_constant();
|
||||
pipeline_key.vertex_format_id = shader.primitive_vertex_format_id;
|
||||
pipeline = _get_pipeline_specialization_or_ubershader(p_shader_data, pipeline_key, push_constant);
|
||||
RD::get_singleton()->draw_list_bind_render_pipeline(p_draw_list, pipeline);
|
||||
|
||||
RD::get_singleton()->draw_list_set_push_constant(p_draw_list, &push_constant, sizeof(PushConstant));
|
||||
RD::get_singleton()->draw_list_set_push_constant(p_draw_list, &push_constant, sizeof(push_constant));
|
||||
FixedVector<RID, 1> vb = { p_batch->instance_buffer };
|
||||
FixedVector<uint64_t, 1> vo = { uint64_t(p_batch->start) * sizeof(InstanceData) };
|
||||
RD::get_singleton()->draw_list_bind_vertex_buffers_format(p_draw_list, shader.primitive_vertex_format_id, 1, vb, vo);
|
||||
RD::get_singleton()->draw_list_bind_index_array(p_draw_list, primitive_arrays.index_array[MIN(3u, primitive->point_count) - 1]);
|
||||
uint32_t instance_count = p_batch->instance_count;
|
||||
RD::get_singleton()->draw_list_draw(p_draw_list, true, instance_count);
|
||||
@ -3063,6 +3130,8 @@ void RendererCanvasRenderRD::_render_batch(RD::DrawListID p_draw_list, CanvasSha
|
||||
case Item::Command::TYPE_PARTICLES: {
|
||||
ERR_FAIL_NULL(p_batch->command);
|
||||
|
||||
PushConstantAttributes push_constant = p_batch->push_constant_attributes();
|
||||
|
||||
RendererRD::MeshStorage *mesh_storage = RendererRD::MeshStorage::get_singleton();
|
||||
RendererRD::ParticlesStorage *particles_storage = RendererRD::ParticlesStorage::get_singleton();
|
||||
|
||||
@ -3123,7 +3192,7 @@ void RendererCanvasRenderRD::_render_batch(RD::DrawListID p_draw_list, CanvasSha
|
||||
pipeline = _get_pipeline_specialization_or_ubershader(p_shader_data, pipeline_key, push_constant, mesh_instance, surface, j, &vertex_array);
|
||||
RD::get_singleton()->draw_list_bind_render_pipeline(p_draw_list, pipeline);
|
||||
|
||||
RD::get_singleton()->draw_list_set_push_constant(p_draw_list, &push_constant, sizeof(PushConstant));
|
||||
RD::get_singleton()->draw_list_set_push_constant(p_draw_list, &push_constant, sizeof(push_constant));
|
||||
|
||||
RID index_array = mesh_storage->mesh_surface_get_index_array(surface, 0);
|
||||
|
||||
@ -3149,19 +3218,36 @@ void RendererCanvasRenderRD::_render_batch(RD::DrawListID p_draw_list, CanvasSha
|
||||
}
|
||||
}
|
||||
|
||||
RendererCanvasRenderRD::InstanceData *RendererCanvasRenderRD::new_instance_data(Batch &p_current_batch, const InstanceData &template_instance) {
|
||||
DEV_ASSERT(state.instance_data != nullptr);
|
||||
RendererCanvasRenderRD::InstanceData *RendererCanvasRenderRD::new_instance_data(Batch &p_current_batch, const InstanceData &template_instance, bool p_use_push_data) {
|
||||
InstanceData *instance_data = nullptr;
|
||||
|
||||
if (unlikely(p_use_push_data)) {
|
||||
instance_data = &p_current_batch.push_data;
|
||||
// instance_count must be > 0 to indicate the batch has been used when calling _new_batch, so we set a flag.
|
||||
p_current_batch.instance_count = PUSH_DATA_INSTANCE_COUNT;
|
||||
} else {
|
||||
instance_data = &state.instance_data[state.instance_data_index];
|
||||
}
|
||||
|
||||
InstanceData *instance_data = &state.instance_data[state.instance_data_index];
|
||||
memcpy(instance_data, &template_instance, sizeof(InstanceData));
|
||||
instance_data->color_texture_pixel_size[0] = p_current_batch.tex_info->texpixel_size.width;
|
||||
instance_data->color_texture_pixel_size[1] = p_current_batch.tex_info->texpixel_size.height;
|
||||
return instance_data;
|
||||
}
|
||||
|
||||
RendererCanvasRenderRD::Batch *RendererCanvasRenderRD::_new_batch(bool &r_batch_broken) {
|
||||
if (state.canvas_instance_batches.is_empty()) {
|
||||
Batch new_batch;
|
||||
// First try to reuse previous instance buffer if possible.
|
||||
if (state.prev_instance_data && state.prev_instance_data_index < state.max_instances_per_buffer) {
|
||||
bool must_remap = state.instance_buffers.prepare_for_map(true);
|
||||
// must_remap will be false if we're preparing to map the buffer for the same frame and can reuse the existing UMA buffer.
|
||||
if (!must_remap) {
|
||||
state.instance_data = state.prev_instance_data;
|
||||
state.instance_data_index = state.prev_instance_data_index;
|
||||
new_batch.start = state.instance_data_index;
|
||||
}
|
||||
state.prev_instance_data = nullptr;
|
||||
state.prev_instance_data_index = 0;
|
||||
}
|
||||
// This will still be a valid point when multiple calls to _render_batch_items
|
||||
// are made in the same draw call.
|
||||
if (state.instance_data == nullptr) {
|
||||
@ -3182,13 +3268,17 @@ RendererCanvasRenderRD::Batch *RendererCanvasRenderRD::_new_batch(bool &r_batch_
|
||||
// Copy the properties of the current batch, we will manually update the things that changed.
|
||||
Batch new_batch = state.canvas_instance_batches[state.current_batch_index];
|
||||
new_batch.instance_count = 0;
|
||||
new_batch.start = state.canvas_instance_batches[state.current_batch_index].start + state.canvas_instance_batches[state.current_batch_index].instance_count;
|
||||
new_batch.start = state.instance_data_index;
|
||||
memset(&new_batch.push_data, 0, sizeof(new_batch.push_data));
|
||||
state.current_batch_index++;
|
||||
state.canvas_instance_batches.push_back(new_batch);
|
||||
return &state.canvas_instance_batches[state.current_batch_index];
|
||||
}
|
||||
|
||||
void RendererCanvasRenderRD::_add_to_batch(bool &r_batch_broken, Batch *&r_current_batch) {
|
||||
DEV_ASSERT(r_current_batch->command_type == Item::Command::TYPE_RECT ||
|
||||
r_current_batch->command_type == Item::Command::TYPE_NINEPATCH ||
|
||||
r_current_batch->command_type == Item::Command::TYPE_PRIMITIVE);
|
||||
r_current_batch->instance_count++;
|
||||
state.instance_data_index++;
|
||||
if (state.instance_data_index >= state.max_instances_per_buffer) {
|
||||
@ -3196,10 +3286,8 @@ void RendererCanvasRenderRD::_add_to_batch(bool &r_batch_broken, Batch *&r_curre
|
||||
state.instance_data = nullptr;
|
||||
_allocate_instance_buffer();
|
||||
state.instance_data_index = 0;
|
||||
state.instance_data_index = 0;
|
||||
r_batch_broken = false; // Force a new batch to be created
|
||||
r_current_batch = _new_batch(r_batch_broken);
|
||||
r_current_batch->start = 0;
|
||||
r_current_batch->instance_buffer = state.instance_buffers._get(0);
|
||||
}
|
||||
}
|
||||
|
||||
@ -67,8 +67,6 @@ class RendererCanvasRenderRD : public RendererCanvasRender {
|
||||
|
||||
INSTANCE_FLAGS_CLIP_RECT_UV = (1 << 4),
|
||||
INSTANCE_FLAGS_TRANSPOSE_RECT = (1 << 5),
|
||||
INSTANCE_FLAGS_USE_MSDF = (1 << 6),
|
||||
INSTANCE_FLAGS_USE_LCD = (1 << 7),
|
||||
|
||||
INSTANCE_FLAGS_NINEPACH_DRAW_CENTER = (1 << 8),
|
||||
INSTANCE_FLAGS_NINEPATCH_H_MODE_SHIFT = 9,
|
||||
@ -120,6 +118,8 @@ class RendererCanvasRenderRD : public RendererCanvasRender {
|
||||
|
||||
struct {
|
||||
uint32_t use_lighting : 1;
|
||||
uint32_t use_msdf : 1;
|
||||
uint32_t use_lcd : 1;
|
||||
};
|
||||
};
|
||||
};
|
||||
@ -186,6 +186,8 @@ class RendererCanvasRenderRD : public RendererCanvasRender {
|
||||
RID default_version_rd_shader;
|
||||
RID quad_index_buffer;
|
||||
RID quad_index_array;
|
||||
RD::VertexFormatID quad_vertex_format_id;
|
||||
RD::VertexFormatID primitive_vertex_format_id;
|
||||
ShaderCompiler compiler;
|
||||
uint32_t pipeline_compilations[RS::PIPELINE_SOURCE_MAX] = {};
|
||||
Mutex mutex;
|
||||
@ -352,16 +354,12 @@ class RendererCanvasRenderRD : public RendererCanvasRender {
|
||||
|
||||
struct InstanceData {
|
||||
float world[6];
|
||||
uint32_t flags;
|
||||
uint32_t instance_uniforms_ofs;
|
||||
float ninepatch_pixel_size[2];
|
||||
union {
|
||||
//rect
|
||||
struct {
|
||||
float modulation[4];
|
||||
union {
|
||||
float msdf[4];
|
||||
float ninepatch_margins[4];
|
||||
};
|
||||
float ninepatch_margins[4];
|
||||
float dst_rect[4];
|
||||
float src_rect[4];
|
||||
float pad[2];
|
||||
@ -373,15 +371,35 @@ class RendererCanvasRenderRD : public RendererCanvasRender {
|
||||
uint32_t colors[6]; // colors encoded as half
|
||||
};
|
||||
};
|
||||
float color_texture_pixel_size[2];
|
||||
uint32_t flags;
|
||||
uint32_t instance_uniforms_ofs;
|
||||
uint32_t lights[4];
|
||||
};
|
||||
|
||||
static_assert(sizeof(InstanceData) == 128, "2D instance data struct size must be 128 bytes");
|
||||
|
||||
struct PushConstant {
|
||||
uint32_t base_instance_index;
|
||||
ShaderSpecialization shader_specialization;
|
||||
uint32_t specular_shininess;
|
||||
uint32_t batch_flags;
|
||||
uint32_t pad0;
|
||||
|
||||
float msdf[2];
|
||||
float color_texture_pixel_size[2];
|
||||
};
|
||||
|
||||
struct PushConstantAttributes {
|
||||
PushConstant base;
|
||||
|
||||
float world[6];
|
||||
uint32_t flags;
|
||||
uint32_t instance_uniforms_ofs;
|
||||
float modulation[4];
|
||||
uint32_t lights[4];
|
||||
|
||||
operator PushConstant &() {
|
||||
return base;
|
||||
}
|
||||
};
|
||||
|
||||
// TextureState is used to determine when a new batch is required due to a change of texture state.
|
||||
@ -459,18 +477,16 @@ class RendererCanvasRenderRD : public RendererCanvasRender {
|
||||
/// A key used to uniquely identify a distinct BATCH_UNIFORM_SET
|
||||
struct RIDSetKey {
|
||||
TextureState state;
|
||||
RID instance_data;
|
||||
|
||||
RIDSetKey() {
|
||||
}
|
||||
|
||||
RIDSetKey(TextureState p_state, RID p_instance_data) :
|
||||
state(p_state),
|
||||
instance_data(p_instance_data) {
|
||||
RIDSetKey(TextureState p_state) :
|
||||
state(p_state) {
|
||||
}
|
||||
|
||||
_ALWAYS_INLINE_ bool operator==(const RIDSetKey &p_val) const {
|
||||
return state == p_val.state && instance_data == p_val.instance_data;
|
||||
return state == p_val.state;
|
||||
}
|
||||
|
||||
_ALWAYS_INLINE_ bool operator!=(const RIDSetKey &p_val) const {
|
||||
@ -478,9 +494,7 @@ class RendererCanvasRenderRD : public RendererCanvasRender {
|
||||
}
|
||||
|
||||
_ALWAYS_INLINE_ uint32_t hash() const {
|
||||
uint32_t h = state.hash();
|
||||
h = hash_murmur3_one_64(instance_data.get_id(), h);
|
||||
return hash_fmix32(h);
|
||||
return state.hash();
|
||||
}
|
||||
};
|
||||
|
||||
@ -495,6 +509,9 @@ class RendererCanvasRenderRD : public RendererCanvasRender {
|
||||
/// diffuse texture.
|
||||
HashMap<RID, TightLocalVector<RID>> canvas_texture_to_uniform_set;
|
||||
|
||||
static constexpr uint32_t PUSH_DATA_INSTANCE_COUNT = 0x8000'0000; // Use high bit to indicate instance data comes from push_data.
|
||||
static constexpr uint32_t INSTANCE_COUNT_MASK = 0x7fff'ffff;
|
||||
|
||||
struct Batch {
|
||||
/// First instance index into the instance buffer for this batch.
|
||||
uint32_t start = 0;
|
||||
@ -502,10 +519,14 @@ class RendererCanvasRenderRD : public RendererCanvasRender {
|
||||
uint32_t instance_count = 0;
|
||||
/// Resource ID of the instance buffer for this batch.
|
||||
RID instance_buffer; // UMA
|
||||
/// Push-constant payload for non-VAO draws.
|
||||
InstanceData push_data = {};
|
||||
|
||||
TextureInfo *tex_info;
|
||||
|
||||
Color modulate = Color(1.0, 1.0, 1.0, 1.0);
|
||||
float msdf_pix_range = 0.0;
|
||||
float msdf_outline = 0.0;
|
||||
|
||||
Item *clip = nullptr;
|
||||
|
||||
@ -517,6 +538,9 @@ class RendererCanvasRenderRD : public RendererCanvasRender {
|
||||
ShaderVariant shader_variant = SHADER_VARIANT_QUAD;
|
||||
RD::RenderPrimitive render_primitive = RD::RENDER_PRIMITIVE_TRIANGLES;
|
||||
bool use_lighting = false;
|
||||
bool use_msdf = false;
|
||||
bool use_lcd = false;
|
||||
bool has_blend = false;
|
||||
|
||||
// batch-specific data
|
||||
union {
|
||||
@ -525,8 +549,31 @@ class RendererCanvasRenderRD : public RendererCanvasRender {
|
||||
// TYPE_PARTICLES
|
||||
uint32_t mesh_instance_count;
|
||||
};
|
||||
bool has_blend = false;
|
||||
uint32_t flags = 0;
|
||||
|
||||
_FORCE_INLINE_ PushConstant push_constant() const {
|
||||
PushConstant pc;
|
||||
pc.specular_shininess = tex_info->specular_shininess;
|
||||
pc.batch_flags = tex_info->flags | flags;
|
||||
pc.pad0 = 0;
|
||||
|
||||
pc.msdf[0] = msdf_pix_range;
|
||||
pc.msdf[1] = msdf_outline;
|
||||
pc.color_texture_pixel_size[0] = tex_info->texpixel_size.x;
|
||||
pc.color_texture_pixel_size[1] = tex_info->texpixel_size.y;
|
||||
return pc;
|
||||
}
|
||||
|
||||
_FORCE_INLINE_ PushConstantAttributes push_constant_attributes() const {
|
||||
PushConstantAttributes pc;
|
||||
pc.base = push_constant();
|
||||
memcpy(pc.world, push_data.world, sizeof(pc.world));
|
||||
memcpy(pc.modulation, push_data.modulation, sizeof(pc.modulation));
|
||||
memcpy(pc.lights, push_data.lights, sizeof(pc.lights));
|
||||
pc.flags = push_data.flags;
|
||||
pc.instance_uniforms_ofs = push_data.instance_uniforms_ofs;
|
||||
return pc;
|
||||
}
|
||||
};
|
||||
|
||||
HashMap<TextureState, TextureInfo, HashMapHasherDefault, HashMapComparatorDefault<TextureState>, PagedAllocator<HashMapElement<TextureState, TextureInfo>>> texture_info_map;
|
||||
@ -564,6 +611,9 @@ class RendererCanvasRenderRD : public RendererCanvasRender {
|
||||
InstanceData *instance_data = nullptr;
|
||||
/// The index of the next instance to be added to <c>instance_data</c>.
|
||||
uint32_t instance_data_index = 0;
|
||||
/// Save the previous instance data to allow us to append .
|
||||
InstanceData *prev_instance_data = nullptr;
|
||||
uint32_t prev_instance_data_index = 0;
|
||||
|
||||
uint32_t max_instances_per_buffer = 16384;
|
||||
uint32_t max_instance_buffer_size = 16384 * sizeof(InstanceData);
|
||||
@ -626,7 +676,7 @@ class RendererCanvasRenderRD : public RendererCanvasRender {
|
||||
void _prepare_batch_texture_info(RID p_texture, TextureState &p_state, TextureInfo *p_info);
|
||||
|
||||
// non-UMA
|
||||
InstanceData *new_instance_data(Batch &p_current_batch, const InstanceData &template_instance);
|
||||
InstanceData *new_instance_data(Batch &p_current_batch, const InstanceData &template_instance, bool p_use_push_data = false);
|
||||
[[nodiscard]] Batch *_new_batch(bool &r_batch_broken);
|
||||
void _add_to_batch(bool &r_batch_broken, Batch *&r_current_batch);
|
||||
void _allocate_instance_buffer();
|
||||
|
||||
@ -24,12 +24,6 @@ layout(location = 11) in vec4 weight_attrib;
|
||||
|
||||
#include "canvas_uniforms_inc.glsl"
|
||||
|
||||
#ifndef USE_ATTRIBUTES
|
||||
layout(location = 4) out flat uint instance_index;
|
||||
#else
|
||||
#define instance_index params.base_instance_index
|
||||
#endif // USE_ATTRIBUTES
|
||||
|
||||
layout(location = 0) out vec2 uv_interp;
|
||||
layout(location = 1) out vec4 color_interp;
|
||||
layout(location = 2) out vec2 vertex_interp;
|
||||
@ -40,6 +34,79 @@ layout(location = 3) out vec2 pixel_size_interp;
|
||||
|
||||
#endif
|
||||
|
||||
#define read_draw_data_color_texture_pixel_size params.color_texture_pixel_size
|
||||
|
||||
#ifdef USE_ATTRIBUTES
|
||||
|
||||
#define read_draw_data_world_x params.world_x
|
||||
#define read_draw_data_world_y params.world_y
|
||||
#define read_draw_data_world_ofs params.world_ofs
|
||||
#define read_draw_data_modulation params.modulation
|
||||
#define read_draw_data_flags params.flags
|
||||
#define read_draw_data_instance_offset params.instance_uniforms_ofs
|
||||
#define read_draw_data_lights params.lights
|
||||
|
||||
#else // !USE_ATTRIBUTES
|
||||
|
||||
layout(location = 8) in vec4 attrib_A;
|
||||
layout(location = 9) in vec4 attrib_B;
|
||||
layout(location = 10) in vec4 attrib_C;
|
||||
layout(location = 11) in vec4 attrib_D;
|
||||
layout(location = 12) in vec4 attrib_E;
|
||||
#ifdef USE_PRIMITIVE
|
||||
layout(location = 13) in uvec4 attrib_F;
|
||||
#else // !USE_PRIMITIVE
|
||||
layout(location = 13) in vec4 attrib_F;
|
||||
#endif // USE_PRIMITIVE
|
||||
layout(location = 14) in uvec4 attrib_G;
|
||||
layout(location = 15) in uvec4 attrib_H;
|
||||
|
||||
// Varyings so the per-instance info can be used in the fragment shader
|
||||
layout(location = 5) out flat vec4 varying_A;
|
||||
layout(location = 6) out flat uvec4 varying_B;
|
||||
layout(location = 7) out flat uvec4 varying_C;
|
||||
|
||||
#ifdef USE_NINEPATCH
|
||||
layout(location = 8) out flat vec4 varying_D;
|
||||
layout(location = 9) out flat vec4 varying_E;
|
||||
#endif // USE_NINEPATCH
|
||||
|
||||
#define read_draw_data_world_x attrib_A.xy
|
||||
#define read_draw_data_world_y attrib_A.zw
|
||||
#define read_draw_data_world_ofs attrib_B.xy
|
||||
|
||||
#ifdef USE_PRIMITIVE
|
||||
|
||||
#define read_draw_data_point_a attrib_C.xy
|
||||
#define read_draw_data_point_b attrib_C.zw
|
||||
#define read_draw_data_point_c attrib_D.xy
|
||||
#define read_draw_data_uv_a attrib_D.zw
|
||||
#define read_draw_data_uv_b attrib_E.xy
|
||||
#define read_draw_data_uv_c attrib_E.zw
|
||||
|
||||
#define read_draw_data_color_a_rg attrib_F.x
|
||||
#define read_draw_data_color_a_ba attrib_F.y
|
||||
#define read_draw_data_color_b_rg attrib_F.z
|
||||
#define read_draw_data_color_b_ba attrib_F.w
|
||||
#define read_draw_data_color_c_rg attrib_G.x
|
||||
#define read_draw_data_color_c_ba attrib_G.y
|
||||
|
||||
#else // !USE_PRIMITIVE
|
||||
|
||||
#define read_draw_data_ninepatch_pixel_size (attrib_B.zw)
|
||||
#define read_draw_data_modulation attrib_C
|
||||
#define read_draw_data_ninepatch_margins attrib_D
|
||||
#define read_draw_data_dst_rect attrib_E
|
||||
#define read_draw_data_src_rect attrib_F
|
||||
|
||||
#endif // USE_PRIMITIVE
|
||||
|
||||
#define read_draw_data_flags attrib_G.z
|
||||
#define read_draw_data_instance_offset attrib_G.w
|
||||
#define read_draw_data_lights attrib_H
|
||||
|
||||
#endif // USE_ATTRIBUTES
|
||||
|
||||
#ifdef MATERIAL_UNIFORMS_USED
|
||||
/* clang-format off */
|
||||
layout(set = 1, binding = 0, std140) uniform MaterialUniforms {
|
||||
@ -57,6 +124,20 @@ vec3 srgb_to_linear(vec3 color) {
|
||||
#endif
|
||||
|
||||
void main() {
|
||||
#ifndef USE_ATTRIBUTES
|
||||
varying_A = vec4(read_draw_data_world_x, read_draw_data_world_y);
|
||||
#ifdef USE_PRIMITIVE
|
||||
varying_B = uvec4(read_draw_data_flags, read_draw_data_instance_offset, 0.0, 0.0);
|
||||
#else
|
||||
varying_B = uvec4(read_draw_data_flags, read_draw_data_instance_offset, packHalf2x16(read_draw_data_src_rect.xy), packHalf2x16(read_draw_data_src_rect.zw));
|
||||
#endif
|
||||
varying_C = read_draw_data_lights;
|
||||
#ifdef USE_NINEPATCH
|
||||
varying_D = read_draw_data_ninepatch_margins;
|
||||
varying_E = vec4(read_draw_data_dst_rect.z, read_draw_data_dst_rect.w, read_draw_data_ninepatch_pixel_size.x, read_draw_data_ninepatch_pixel_size.y);
|
||||
#endif // USE_NINEPATCH
|
||||
#endif // !USE_ATTRIBUTES
|
||||
|
||||
vec4 instance_custom = vec4(0.0);
|
||||
#if defined(CUSTOM0_USED)
|
||||
vec4 custom0 = vec4(0.0);
|
||||
@ -65,11 +146,6 @@ void main() {
|
||||
vec4 custom1 = vec4(0.0);
|
||||
#endif
|
||||
|
||||
#ifndef USE_ATTRIBUTES
|
||||
instance_index = gl_InstanceIndex + params.base_instance_index;
|
||||
#endif // USE_ATTRIBUTES
|
||||
const InstanceData draw_data = instances.data[instance_index];
|
||||
|
||||
#ifdef USE_PRIMITIVE
|
||||
|
||||
//weird bug,
|
||||
@ -79,18 +155,19 @@ void main() {
|
||||
vec4 color;
|
||||
|
||||
if (gl_VertexIndex == 0) {
|
||||
vertex = draw_data.points[0];
|
||||
uv = draw_data.uvs[0];
|
||||
color = vec4(unpackHalf2x16(draw_data.colors[0]), unpackHalf2x16(draw_data.colors[1]));
|
||||
vertex = read_draw_data_point_a;
|
||||
uv = read_draw_data_uv_a;
|
||||
color = vec4(unpackHalf2x16(read_draw_data_color_a_rg), unpackHalf2x16(read_draw_data_color_a_ba));
|
||||
} else if (gl_VertexIndex == 1) {
|
||||
vertex = draw_data.points[1];
|
||||
uv = draw_data.uvs[1];
|
||||
color = vec4(unpackHalf2x16(draw_data.colors[2]), unpackHalf2x16(draw_data.colors[3]));
|
||||
vertex = read_draw_data_point_b;
|
||||
uv = read_draw_data_uv_b;
|
||||
color = vec4(unpackHalf2x16(read_draw_data_color_b_rg), unpackHalf2x16(read_draw_data_color_b_ba));
|
||||
} else {
|
||||
vertex = draw_data.points[2];
|
||||
uv = draw_data.uvs[2];
|
||||
color = vec4(unpackHalf2x16(draw_data.colors[4]), unpackHalf2x16(draw_data.colors[5]));
|
||||
vertex = read_draw_data_point_c;
|
||||
uv = read_draw_data_uv_c;
|
||||
color = vec4(unpackHalf2x16(read_draw_data_color_c_rg), unpackHalf2x16(read_draw_data_color_c_ba));
|
||||
}
|
||||
|
||||
uvec4 bones = uvec4(0, 0, 0, 0);
|
||||
vec4 bone_weights = vec4(0.0);
|
||||
|
||||
@ -101,7 +178,7 @@ void main() {
|
||||
if (bool(canvas_data.flags & CANVAS_FLAGS_CONVERT_ATTRIBUTES_TO_LINEAR)) {
|
||||
color.rgb = srgb_to_linear(color.rgb);
|
||||
}
|
||||
color *= draw_data.modulation;
|
||||
color *= read_draw_data_modulation;
|
||||
vec2 uv = uv_attrib;
|
||||
|
||||
#if defined(CUSTOM0_USED)
|
||||
@ -119,14 +196,14 @@ void main() {
|
||||
vec2 vertex_base_arr[4] = vec2[](vec2(0.0, 0.0), vec2(0.0, 1.0), vec2(1.0, 1.0), vec2(1.0, 0.0));
|
||||
vec2 vertex_base = vertex_base_arr[gl_VertexIndex];
|
||||
|
||||
vec2 uv = draw_data.src_rect.xy + abs(draw_data.src_rect.zw) * ((draw_data.flags & INSTANCE_FLAGS_TRANSPOSE_RECT) != 0 ? vertex_base.yx : vertex_base.xy);
|
||||
vec4 color = draw_data.modulation;
|
||||
vec2 vertex = draw_data.dst_rect.xy + abs(draw_data.dst_rect.zw) * mix(vertex_base, vec2(1.0, 1.0) - vertex_base, lessThan(draw_data.src_rect.zw, vec2(0.0, 0.0)));
|
||||
vec2 uv = read_draw_data_src_rect.xy + abs(read_draw_data_src_rect.zw) * ((read_draw_data_flags & INSTANCE_FLAGS_TRANSPOSE_RECT) != 0 ? vertex_base.yx : vertex_base.xy);
|
||||
vec4 color = read_draw_data_modulation;
|
||||
vec2 vertex = read_draw_data_dst_rect.xy + abs(read_draw_data_dst_rect.zw) * mix(vertex_base, vec2(1.0, 1.0) - vertex_base, lessThan(read_draw_data_src_rect.zw, vec2(0.0, 0.0)));
|
||||
uvec4 bones = uvec4(0, 0, 0, 0);
|
||||
|
||||
#endif // USE_ATTRIBUTES
|
||||
|
||||
mat4 model_matrix = mat4(vec4(draw_data.world_x, 0.0, 0.0), vec4(draw_data.world_y, 0.0, 0.0), vec4(0.0, 0.0, 1.0, 0.0), vec4(draw_data.world_ofs, 0.0, 1.0));
|
||||
mat4 model_matrix = mat4(vec4(read_draw_data_world_x, 0.0, 0.0), vec4(read_draw_data_world_y, 0.0, 0.0), vec4(0.0, 0.0, 1.0, 0.0), vec4(read_draw_data_world_ofs, 0.0, 1.0));
|
||||
|
||||
#ifdef USE_ATTRIBUTES
|
||||
|
||||
@ -200,7 +277,7 @@ void main() {
|
||||
}
|
||||
|
||||
#ifdef USE_NINEPATCH
|
||||
pixel_size_interp = abs(draw_data.dst_rect.zw) * vertex_base;
|
||||
pixel_size_interp = abs(read_draw_data_dst_rect.zw) * vertex_base;
|
||||
#endif
|
||||
|
||||
#if !defined(SKIP_TRANSFORM_USED) && !defined(USE_WORLD_VERTEX_COORDS)
|
||||
@ -236,12 +313,6 @@ void main() {
|
||||
|
||||
#include "canvas_uniforms_inc.glsl"
|
||||
|
||||
#ifndef USE_ATTRIBUTES
|
||||
layout(location = 4) in flat uint instance_index;
|
||||
#else
|
||||
#define instance_index params.base_instance_index
|
||||
#endif // USE_ATTRIBUTES
|
||||
|
||||
layout(location = 0) in vec2 uv_interp;
|
||||
layout(location = 1) in vec4 color_interp;
|
||||
layout(location = 2) in vec2 vertex_interp;
|
||||
@ -252,6 +323,42 @@ layout(location = 3) in vec2 pixel_size_interp;
|
||||
|
||||
#endif
|
||||
|
||||
#define read_draw_data_color_texture_pixel_size params.color_texture_pixel_size
|
||||
|
||||
#ifdef USE_ATTRIBUTES
|
||||
|
||||
#define read_draw_data_world_x params.world_x
|
||||
#define read_draw_data_world_y params.world_y
|
||||
#define read_draw_data_flags params.flags
|
||||
#define read_draw_data_instance_offset params.instance_uniforms_ofs
|
||||
#define read_draw_data_lights params.lights
|
||||
|
||||
#else // !USE_ATTRIBUTES
|
||||
|
||||
// Can all be flat as they are the same for the whole batched instance
|
||||
layout(location = 5) in flat vec4 varying_A;
|
||||
|
||||
#define read_draw_data_world_x varying_A.xy
|
||||
#define read_draw_data_world_y varying_A.zw
|
||||
|
||||
layout(location = 6) in flat uvec4 varying_B;
|
||||
layout(location = 7) in flat uvec4 varying_C;
|
||||
#define read_draw_data_flags varying_B.x
|
||||
#define read_draw_data_instance_offset varying_B.y
|
||||
#define read_draw_data_src_rect (varying_B.zw)
|
||||
#define read_draw_data_lights varying_C
|
||||
|
||||
#ifdef USE_NINEPATCH
|
||||
layout(location = 8) in flat vec4 varying_D;
|
||||
layout(location = 9) in flat vec4 varying_E;
|
||||
#define read_draw_data_ninepatch_margins varying_D
|
||||
#define read_draw_data_dst_rect_z varying_E.x
|
||||
#define read_draw_data_dst_rect_w varying_E.y
|
||||
#define read_draw_data_ninepatch_pixel_size (varying_E.zw)
|
||||
#endif // USE_NINEPATCH
|
||||
|
||||
#endif // USE_ATTRIBUTES
|
||||
|
||||
layout(location = 0) out vec4 frag_color;
|
||||
|
||||
#ifdef MATERIAL_UNIFORMS_USED
|
||||
@ -312,7 +419,6 @@ vec4 light_compute(
|
||||
vec2 screen_uv,
|
||||
vec2 uv,
|
||||
vec4 color, bool is_directional) {
|
||||
const InstanceData draw_data = instances.data[instance_index];
|
||||
vec4 light = vec4(0.0);
|
||||
vec3 light_direction = vec3(0.0);
|
||||
|
||||
@ -333,8 +439,6 @@ vec4 light_compute(
|
||||
#ifdef USE_NINEPATCH
|
||||
|
||||
float map_ninepatch_axis(float pixel, float draw_size, float tex_pixel_size, float margin_begin, float margin_end, int np_repeat, inout int draw_center) {
|
||||
const InstanceData draw_data = instances.data[instance_index];
|
||||
|
||||
float tex_size = 1.0 / tex_pixel_size;
|
||||
|
||||
if (pixel < margin_begin) {
|
||||
@ -342,7 +446,7 @@ float map_ninepatch_axis(float pixel, float draw_size, float tex_pixel_size, flo
|
||||
} else if (pixel >= draw_size - margin_end) {
|
||||
return (tex_size - (draw_size - pixel)) * tex_pixel_size;
|
||||
} else {
|
||||
draw_center -= 1 - int(bitfieldExtract(draw_data.flags, INSTANCE_FLAGS_NINEPATCH_DRAW_CENTER_SHIFT, 1));
|
||||
draw_center -= 1 - int(bitfieldExtract(read_draw_data_flags, INSTANCE_FLAGS_NINEPATCH_DRAW_CENTER_SHIFT, 1));
|
||||
|
||||
// np_repeat is passed as uniform using NinePatchRect::AxisStretchMode enum.
|
||||
if (np_repeat == 0) { // Stretch.
|
||||
@ -473,12 +577,11 @@ void main() {
|
||||
vec2 uv = uv_interp;
|
||||
vec2 vertex = vertex_interp;
|
||||
|
||||
const InstanceData draw_data = instances.data[instance_index];
|
||||
|
||||
#if !defined(USE_ATTRIBUTES) && !defined(USE_PRIMITIVE)
|
||||
vec4 region_rect = draw_data.src_rect;
|
||||
vec4 src_rect = vec4(unpackHalf2x16(read_draw_data_src_rect.x), unpackHalf2x16(read_draw_data_src_rect.y));
|
||||
vec4 region_rect = src_rect;
|
||||
#else
|
||||
vec4 region_rect = vec4(0.0, 0.0, 1.0 / draw_data.color_texture_pixel_size);
|
||||
vec4 region_rect = vec4(0.0, 0.0, 1.0 / read_draw_data_color_texture_pixel_size);
|
||||
#endif
|
||||
|
||||
#if !defined(USE_ATTRIBUTES) && !defined(USE_PRIMITIVE)
|
||||
@ -487,29 +590,28 @@ void main() {
|
||||
|
||||
int draw_center = 2;
|
||||
uv = vec2(
|
||||
map_ninepatch_axis(pixel_size_interp.x, abs(draw_data.dst_rect.z), draw_data.color_texture_pixel_size.x, draw_data.ninepatch_margins.x, draw_data.ninepatch_margins.z, int(bitfieldExtract(draw_data.flags, INSTANCE_FLAGS_NINEPATCH_H_MODE_SHIFT, 2)), draw_center),
|
||||
map_ninepatch_axis(pixel_size_interp.y, abs(draw_data.dst_rect.w), draw_data.color_texture_pixel_size.y, draw_data.ninepatch_margins.y, draw_data.ninepatch_margins.w, int(bitfieldExtract(draw_data.flags, INSTANCE_FLAGS_NINEPATCH_V_MODE_SHIFT, 2)), draw_center));
|
||||
map_ninepatch_axis(pixel_size_interp.x, abs(read_draw_data_dst_rect_z), read_draw_data_ninepatch_pixel_size.x, read_draw_data_ninepatch_margins.x, read_draw_data_ninepatch_margins.z, int(bitfieldExtract(read_draw_data_flags, INSTANCE_FLAGS_NINEPATCH_H_MODE_SHIFT, 2)), draw_center),
|
||||
map_ninepatch_axis(pixel_size_interp.y, abs(read_draw_data_dst_rect_w), read_draw_data_ninepatch_pixel_size.y, read_draw_data_ninepatch_margins.y, read_draw_data_ninepatch_margins.w, int(bitfieldExtract(read_draw_data_flags, INSTANCE_FLAGS_NINEPATCH_V_MODE_SHIFT, 2)), draw_center));
|
||||
|
||||
if (draw_center == 0) {
|
||||
color.a = 0.0;
|
||||
}
|
||||
|
||||
uv = uv * draw_data.src_rect.zw + draw_data.src_rect.xy; //apply region if needed
|
||||
uv = uv * src_rect.zw + src_rect.xy; //apply region if needed
|
||||
|
||||
#endif
|
||||
if (bool(draw_data.flags & INSTANCE_FLAGS_CLIP_RECT_UV)) {
|
||||
vec2 half_texpixel = draw_data.color_texture_pixel_size * 0.5;
|
||||
uv = clamp(uv, draw_data.src_rect.xy + half_texpixel, draw_data.src_rect.xy + abs(draw_data.src_rect.zw) - half_texpixel);
|
||||
if (bool(read_draw_data_flags & INSTANCE_FLAGS_CLIP_RECT_UV)) {
|
||||
vec2 half_texpixel = read_draw_data_color_texture_pixel_size * 0.5;
|
||||
uv = clamp(uv, src_rect.xy + half_texpixel, src_rect.xy + abs(src_rect.zw) - half_texpixel);
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
#ifndef USE_PRIMITIVE
|
||||
if (bool(draw_data.flags & INSTANCE_FLAGS_USE_MSDF)) {
|
||||
float px_range = draw_data.ninepatch_margins.x;
|
||||
float outline_thickness = draw_data.ninepatch_margins.y;
|
||||
//float reserved1 = draw_data.ninepatch_margins.z;
|
||||
//float reserved2 = draw_data.ninepatch_margins.w;
|
||||
#if !defined(USE_ATTRIBUTES) && !defined(USE_PRIMITIVE)
|
||||
// only used by TYPE_RECT
|
||||
if (sc_use_msdf()) {
|
||||
float px_range = params.msdf.x;
|
||||
float outline_thickness = params.msdf.y;
|
||||
|
||||
vec4 msdf_sample = texture(sampler2D(color_texture, texture_sampler), uv);
|
||||
vec2 msdf_size = vec2(textureSize(sampler2D(color_texture, texture_sampler), 0));
|
||||
@ -526,7 +628,7 @@ void main() {
|
||||
float a = clamp((d - 0.5) * px_size + 0.5, 0.0, 1.0);
|
||||
color.a = a * color.a;
|
||||
}
|
||||
} else if (bool(draw_data.flags & INSTANCE_FLAGS_USE_LCD)) {
|
||||
} else if (sc_use_lcd()) {
|
||||
vec4 lcd_sample = texture(sampler2D(color_texture, texture_sampler), uv);
|
||||
if (lcd_sample.a == 1.0) {
|
||||
color.rgb = lcd_sample.rgb * color.a;
|
||||
@ -540,8 +642,8 @@ void main() {
|
||||
color *= texture(sampler2D(color_texture, texture_sampler), uv);
|
||||
}
|
||||
|
||||
uint light_count = draw_data.flags & 15u; //max 15 lights
|
||||
bool using_light = (light_count + canvas_data.directional_light_count) > 0;
|
||||
uint light_count = read_draw_data_flags & 15u; //max 15 lights
|
||||
bool using_light = ((light_count + canvas_data.directional_light_count) > 0) && sc_use_lighting();
|
||||
|
||||
vec3 normal;
|
||||
|
||||
@ -555,10 +657,10 @@ void main() {
|
||||
normal.xy = texture(sampler2D(normal_texture, texture_sampler), uv).xy * vec2(2.0, -2.0) - vec2(1.0, -1.0);
|
||||
|
||||
#if !defined(USE_ATTRIBUTES) && !defined(USE_PRIMITIVE)
|
||||
if (bool(draw_data.flags & INSTANCE_FLAGS_TRANSPOSE_RECT)) {
|
||||
if (bool(read_draw_data_flags & INSTANCE_FLAGS_TRANSPOSE_RECT)) {
|
||||
normal.xy = normal.yx;
|
||||
}
|
||||
normal.xy *= sign(draw_data.src_rect.zw);
|
||||
normal.xy *= sign(src_rect.zw);
|
||||
#endif
|
||||
normal.z = sqrt(max(0.0, 1.0 - dot(normal.xy, normal.xy)));
|
||||
normal_used = true;
|
||||
@ -609,7 +711,7 @@ void main() {
|
||||
|
||||
if (normal_used) {
|
||||
//convert by item transform
|
||||
normal.xy = mat2(normalize(draw_data.world_x), normalize(draw_data.world_y)) * normal.xy;
|
||||
normal.xy = mat2(normalize(read_draw_data_world_x), normalize(read_draw_data_world_y)) * normal.xy;
|
||||
//convert by canvas transform
|
||||
normal = normalize((canvas_data.canvas_normal_transform * vec4(normal, 0.0)).xyz);
|
||||
}
|
||||
@ -671,7 +773,7 @@ void main() {
|
||||
if (i >= light_count) {
|
||||
break;
|
||||
}
|
||||
uint light_base = bitfieldExtract(draw_data.lights[i >> 2], (int(i) & 0x3) * 8, 8);
|
||||
uint light_base = bitfieldExtract(read_draw_data_lights[i >> 2], (int(i) & 0x3) * 8, 8);
|
||||
|
||||
vec2 tex_uv = (vec4(vertex, 0.0, 1.0) * mat4(light_array.data[light_base].texture_matrix[0], light_array.data[light_base].texture_matrix[1], vec4(0.0, 0.0, 1.0, 0.0), vec4(0.0, 0.0, 0.0, 1.0))).xy; //multiply inverse given its transposed. Optimizer removes useless operations.
|
||||
vec2 tex_uv_atlas = tex_uv * light_array.data[light_base].atlas_rect.zw + light_array.data[light_base].atlas_rect.xy;
|
||||
@ -706,7 +808,7 @@ void main() {
|
||||
}
|
||||
#endif
|
||||
|
||||
if (bool(light_array.data[light_base].flags & LIGHT_FLAGS_HAS_SHADOW) && bool(draw_data.flags & (INSTANCE_FLAGS_SHADOW_MASKED << i))) {
|
||||
if (bool(light_array.data[light_base].flags & LIGHT_FLAGS_HAS_SHADOW) && bool(read_draw_data_flags & (INSTANCE_FLAGS_SHADOW_MASKED << i))) {
|
||||
vec2 shadow_pos = (vec4(shadow_vertex, 0.0, 1.0) * mat4(light_array.data[light_base].shadow_matrix[0], light_array.data[light_base].shadow_matrix[1], vec4(0.0, 0.0, 1.0, 0.0), vec4(0.0, 0.0, 0.0, 1.0))).xy; //multiply inverse given its transposed. Optimizer removes useless operations.
|
||||
|
||||
vec2 pos_norm = normalize(shadow_pos);
|
||||
|
||||
@ -8,8 +8,6 @@
|
||||
|
||||
#define INSTANCE_FLAGS_CLIP_RECT_UV (1 << 4)
|
||||
#define INSTANCE_FLAGS_TRANSPOSE_RECT (1 << 5)
|
||||
#define INSTANCE_FLAGS_USE_MSDF (1 << 6)
|
||||
#define INSTANCE_FLAGS_USE_LCD (1 << 7)
|
||||
|
||||
#define INSTANCE_FLAGS_NINEPATCH_DRAW_CENTER_SHIFT 8
|
||||
#define INSTANCE_FLAGS_NINEPATCH_H_MODE_SHIFT 9
|
||||
@ -22,8 +20,7 @@ struct InstanceData {
|
||||
vec2 world_x;
|
||||
vec2 world_y;
|
||||
vec2 world_ofs;
|
||||
uint flags;
|
||||
uint instance_uniforms_ofs;
|
||||
vec2 ninepatch_pixel_size;
|
||||
#ifdef USE_PRIMITIVE
|
||||
vec2 points[3];
|
||||
vec2 uvs[3];
|
||||
@ -36,7 +33,8 @@ struct InstanceData {
|
||||
vec2 pad;
|
||||
|
||||
#endif
|
||||
vec2 color_texture_pixel_size;
|
||||
uint flags;
|
||||
uint instance_uniforms_ofs;
|
||||
uvec4 lights;
|
||||
};
|
||||
|
||||
@ -51,10 +49,25 @@ struct InstanceData {
|
||||
#define BATCH_FLAGS_DEFAULT_SPECULAR_MAP_USED (1 << 10)
|
||||
|
||||
layout(push_constant, std430) uniform Params {
|
||||
uint base_instance_index; // base index to instance data
|
||||
uint sc_packed_0;
|
||||
uint specular_shininess;
|
||||
uint batch_flags;
|
||||
uint pad0;
|
||||
|
||||
vec2 msdf;
|
||||
vec2 color_texture_pixel_size;
|
||||
#ifdef USE_ATTRIBUTES
|
||||
// Particles and meshes
|
||||
vec2 world_x;
|
||||
vec2 world_y;
|
||||
|
||||
vec2 world_ofs;
|
||||
uint flags;
|
||||
uint instance_uniforms_ofs;
|
||||
|
||||
vec4 modulation;
|
||||
uvec4 lights;
|
||||
#endif
|
||||
}
|
||||
params;
|
||||
|
||||
@ -82,6 +95,14 @@ bool sc_use_lighting() {
|
||||
return ((sc_packed_0() >> 0) & 1U) != 0;
|
||||
}
|
||||
|
||||
bool sc_use_msdf() {
|
||||
return ((sc_packed_0() >> 1) & 1U) != 0;
|
||||
}
|
||||
|
||||
bool sc_use_lcd() {
|
||||
return ((sc_packed_0() >> 2) & 1U) != 0;
|
||||
}
|
||||
|
||||
// In vulkan, sets should always be ordered using the following logic:
|
||||
// Lower Sets: Sets that change format and layout less often
|
||||
// Higher sets: Sets that change format and layout very often
|
||||
@ -180,8 +201,3 @@ layout(set = 3, binding = 0) uniform texture2D color_texture;
|
||||
layout(set = 3, binding = 1) uniform texture2D normal_texture;
|
||||
layout(set = 3, binding = 2) uniform texture2D specular_texture;
|
||||
layout(set = 3, binding = 3) uniform sampler texture_sampler;
|
||||
|
||||
layout(set = 3, binding = 4, std430) restrict readonly buffer DrawData {
|
||||
InstanceData data[];
|
||||
}
|
||||
instances;
|
||||
|
||||
Reference in New Issue
Block a user