summaryrefslogtreecommitdiff
path: root/servers/rendering/rasterizer_rd/rasterizer_canvas_rd.cpp
diff options
context:
space:
mode:
authorRĂ©mi Verschelde <rverschelde@gmail.com>2020-10-24 22:13:57 +0200
committerGitHub <noreply@github.com>2020-10-24 22:13:57 +0200
commitd5bda5964978b6af7a956262cecf80de1797500c (patch)
treef5b2f2ec923bf3eb1593531bc3d91c3dc1a1c0a1 /servers/rendering/rasterizer_rd/rasterizer_canvas_rd.cpp
parentb67ccf1a6f326c5d4d5fa1cc7cd15eff3573f8f7 (diff)
parent84d734da0e4dc4f0076c61a7178d8ad7b9f3b616 (diff)
Merge pull request #43052 from reduz/refactor-2d-lighting
Refactored 2D shader and lighting system
Diffstat (limited to 'servers/rendering/rasterizer_rd/rasterizer_canvas_rd.cpp')
-rw-r--r--servers/rendering/rasterizer_rd/rasterizer_canvas_rd.cpp839
1 files changed, 332 insertions, 507 deletions
diff --git a/servers/rendering/rasterizer_rd/rasterizer_canvas_rd.cpp b/servers/rendering/rasterizer_rd/rasterizer_canvas_rd.cpp
index aad2be45c6..28674b118c 100644
--- a/servers/rendering/rasterizer_rd/rasterizer_canvas_rd.cpp
+++ b/servers/rendering/rasterizer_rd/rasterizer_canvas_rd.cpp
@@ -92,152 +92,6 @@ void RasterizerCanvasRD::_update_transform_to_mat4(const Transform &p_transform,
p_mat4[15] = 1;
}
-void RasterizerCanvasRD::_update_specular_shininess(const Color &p_transform, uint32_t *r_ss) {
- *r_ss = uint32_t(CLAMP(p_transform.a * 255.0, 0, 255)) << 24;
- *r_ss |= uint32_t(CLAMP(p_transform.b * 255.0, 0, 255)) << 16;
- *r_ss |= uint32_t(CLAMP(p_transform.g * 255.0, 0, 255)) << 8;
- *r_ss |= uint32_t(CLAMP(p_transform.r * 255.0, 0, 255));
-}
-
-RID RasterizerCanvasRD::_create_texture_binding(RID p_texture, RID p_normalmap, RID p_specular, RenderingServer::CanvasItemTextureFilter p_filter, RenderingServer::CanvasItemTextureRepeat p_repeat, RID p_multimesh) {
- Vector<RD::Uniform> uniform_set;
-
- { // COLOR TEXTURE
- RD::Uniform u;
- u.type = RD::UNIFORM_TYPE_TEXTURE;
- u.binding = 1;
- RID texture = storage->texture_get_rd_texture(p_texture);
- if (!texture.is_valid()) {
- //use default white texture
- texture = storage->texture_rd_get_default(RasterizerStorageRD::DEFAULT_RD_TEXTURE_WHITE);
- }
- u.ids.push_back(texture);
- uniform_set.push_back(u);
- }
-
- { // NORMAL TEXTURE
- RD::Uniform u;
- u.type = RD::UNIFORM_TYPE_TEXTURE;
- u.binding = 2;
- RID texture = storage->texture_get_rd_texture(p_normalmap);
- if (!texture.is_valid()) {
- //use default normal texture
- texture = storage->texture_rd_get_default(RasterizerStorageRD::DEFAULT_RD_TEXTURE_NORMAL);
- }
- u.ids.push_back(texture);
- uniform_set.push_back(u);
- }
-
- { // SPECULAR TEXTURE
- RD::Uniform u;
- u.type = RD::UNIFORM_TYPE_TEXTURE;
- u.binding = 3;
- RID texture = storage->texture_get_rd_texture(p_specular);
- if (!texture.is_valid()) {
- //use default white texture
- texture = storage->texture_rd_get_default(RasterizerStorageRD::DEFAULT_RD_TEXTURE_WHITE);
- }
- u.ids.push_back(texture);
- uniform_set.push_back(u);
- }
-
- { // SAMPLER
- RD::Uniform u;
- u.type = RD::UNIFORM_TYPE_SAMPLER;
- u.binding = 4;
- RID sampler = storage->sampler_rd_get_default(p_filter, p_repeat);
- ERR_FAIL_COND_V(sampler.is_null(), RID());
- u.ids.push_back(sampler);
- uniform_set.push_back(u);
- }
-
- { // MULTIMESH TEXTURE BUFFER
- RD::Uniform u;
- u.type = RD::UNIFORM_TYPE_TEXTURE_BUFFER;
- u.binding = 5;
- u.ids.push_back(storage->texture_rd_get_default(RasterizerStorageRD::DEFAULT_RD_TEXTURE_MULTIMESH_BUFFER));
- uniform_set.push_back(u);
- }
-
- return RD::get_singleton()->uniform_set_create(uniform_set, shader.default_version_rd_shader, 0);
-}
-
-RasterizerCanvas::TextureBindingID RasterizerCanvasRD::request_texture_binding(RID p_texture, RID p_normalmap, RID p_specular, RenderingServer::CanvasItemTextureFilter p_filter, RenderingServer::CanvasItemTextureRepeat p_repeat, RID p_multimesh) {
- if (p_filter == RS::CANVAS_ITEM_TEXTURE_FILTER_DEFAULT) {
- p_filter = default_samplers.default_filter;
- }
-
- if (p_repeat == RS::CANVAS_ITEM_TEXTURE_REPEAT_DEFAULT) {
- p_repeat = default_samplers.default_repeat;
- }
-
- TextureBindingKey key;
- key.texture = p_texture;
- key.normalmap = p_normalmap;
- key.specular = p_specular;
- key.multimesh = p_multimesh;
- key.texture_filter = p_filter;
- key.texture_repeat = p_repeat;
-
- TextureBinding *binding;
- TextureBindingID id;
- {
- TextureBindingID *idptr = bindings.texture_key_bindings.getptr(key);
-
- if (!idptr) {
- id = bindings.id_generator++;
- bindings.texture_key_bindings[key] = id;
- binding = memnew(TextureBinding);
- binding->key = key;
- binding->id = id;
-
- bindings.texture_bindings[id] = binding;
-
- } else {
- id = *idptr;
- binding = bindings.texture_bindings[id];
- }
- }
-
- binding->reference_count++;
-
- if (binding->to_dispose.in_list()) {
- //was queued for disposal previously, but ended up reused.
- bindings.to_dispose_list.remove(&binding->to_dispose);
- }
-
- if (binding->uniform_set.is_null() || !RD::get_singleton()->uniform_set_is_valid(binding->uniform_set)) {
- binding->uniform_set = _create_texture_binding(p_texture, p_normalmap, p_specular, p_filter, p_repeat, p_multimesh);
- }
-
- return id;
-}
-
-void RasterizerCanvasRD::free_texture_binding(TextureBindingID p_binding) {
- TextureBinding **binding_ptr = bindings.texture_bindings.getptr(p_binding);
- ERR_FAIL_COND(!binding_ptr);
- TextureBinding *binding = *binding_ptr;
- ERR_FAIL_COND(binding->reference_count == 0);
- binding->reference_count--;
- if (binding->reference_count == 0) {
- bindings.to_dispose_list.add(&binding->to_dispose);
- }
-}
-
-void RasterizerCanvasRD::_dispose_bindings() {
- while (bindings.to_dispose_list.first()) {
- TextureBinding *binding = bindings.to_dispose_list.first()->self();
- if (binding->uniform_set.is_valid() && RD::get_singleton()->uniform_set_is_valid(binding->uniform_set)) {
- RD::get_singleton()->free(binding->uniform_set);
- }
-
- bindings.texture_key_bindings.erase(binding->key);
- bindings.texture_bindings.erase(binding->id);
- bindings.to_dispose_list.remove(&binding->to_dispose);
- memdelete(binding);
- }
-}
-
RasterizerCanvas::PolygonID RasterizerCanvasRD::request_polygon(const Vector<int> &p_indices, const Vector<Point2> &p_points, const Vector<Color> &p_colors, const Vector<Point2> &p_uvs, const Vector<int> &p_bones, const Vector<float> &p_weights) {
// Care must be taken to generate array formats
// in ways where they could be reused, so we will
@@ -457,36 +311,72 @@ void RasterizerCanvasRD::free_polygon(PolygonID p_polygon) {
polygon_buffers.polygons.erase(p_polygon);
}
-Size2i RasterizerCanvasRD::_bind_texture_binding(TextureBindingID p_binding, RD::DrawListID p_draw_list, uint32_t &flags) {
- TextureBinding **texture_binding_ptr = bindings.texture_bindings.getptr(p_binding);
- ERR_FAIL_COND_V(!texture_binding_ptr, Size2i());
- TextureBinding *texture_binding = *texture_binding_ptr;
+////////////////////
+
+void RasterizerCanvasRD::_bind_canvas_texture(RD::DrawListID p_draw_list, RID p_texture, RS::CanvasItemTextureFilter p_base_filter, RS::CanvasItemTextureRepeat p_base_repeat, RID &r_last_texture, PushConstant &push_constant, Size2 &r_texpixel_size) {
+ if (p_texture == RID()) {
+ p_texture = default_canvas_texture;
+ }
- if (texture_binding->key.normalmap.is_valid()) {
- flags |= FLAGS_DEFAULT_NORMAL_MAP_USED;
+ if (r_last_texture == p_texture) {
+ return; //nothing to do, its the same
}
- if (texture_binding->key.specular.is_valid()) {
- flags |= FLAGS_DEFAULT_SPECULAR_MAP_USED;
+
+ RID uniform_set;
+ Color specular_shininess;
+ Size2i size;
+ bool use_normal;
+ bool use_specular;
+
+ bool success = storage->canvas_texture_get_unifom_set(p_texture, p_base_filter, p_base_repeat, shader.default_version_rd_shader, CANVAS_TEXTURE_UNIFORM_SET, uniform_set, size, specular_shininess, use_normal, use_specular);
+ //something odd happened
+ if (!success) {
+ _bind_canvas_texture(p_draw_list, default_canvas_texture, p_base_filter, p_base_repeat, r_last_texture, push_constant, r_texpixel_size);
+ return;
}
- if (!RD::get_singleton()->uniform_set_is_valid(texture_binding->uniform_set)) {
- //texture may have changed (erased or replaced, see if we can fix)
- texture_binding->uniform_set = _create_texture_binding(texture_binding->key.texture, texture_binding->key.normalmap, texture_binding->key.specular, texture_binding->key.texture_filter, texture_binding->key.texture_repeat, texture_binding->key.multimesh);
- ERR_FAIL_COND_V(!texture_binding->uniform_set.is_valid(), Size2i(1, 1));
+ RD::get_singleton()->draw_list_bind_uniform_set(p_draw_list, uniform_set, CANVAS_TEXTURE_UNIFORM_SET);
+
+ if (specular_shininess.a < 0.999) {
+ push_constant.flags |= FLAGS_DEFAULT_SPECULAR_MAP_USED;
+ } else {
+ push_constant.flags &= ~FLAGS_DEFAULT_SPECULAR_MAP_USED;
}
- RD::get_singleton()->draw_list_bind_uniform_set(p_draw_list, texture_binding->uniform_set, 0);
- if (texture_binding->key.texture.is_valid()) {
- return storage->texture_2d_get_size(texture_binding->key.texture);
+ if (use_normal) {
+ push_constant.flags |= FLAGS_DEFAULT_NORMAL_MAP_USED;
} else {
- return Size2i(1, 1);
+ push_constant.flags &= ~FLAGS_DEFAULT_NORMAL_MAP_USED;
}
+
+ push_constant.specular_shininess = uint32_t(CLAMP(specular_shininess.a * 255.0, 0, 255)) << 24;
+ push_constant.specular_shininess |= uint32_t(CLAMP(specular_shininess.b * 255.0, 0, 255)) << 16;
+ push_constant.specular_shininess |= uint32_t(CLAMP(specular_shininess.g * 255.0, 0, 255)) << 8;
+ push_constant.specular_shininess |= uint32_t(CLAMP(specular_shininess.r * 255.0, 0, 255));
+
+ r_texpixel_size.x = 1.0 / float(size.x);
+ r_texpixel_size.y = 1.0 / float(size.y);
+
+ push_constant.color_texture_pixel_size[0] = r_texpixel_size.x;
+ push_constant.color_texture_pixel_size[1] = r_texpixel_size.y;
+
+ r_last_texture = p_texture;
}
-////////////////////
void RasterizerCanvasRD::_render_item(RD::DrawListID p_draw_list, const Item *p_item, RD::FramebufferFormatID p_framebuffer_format, const Transform2D &p_canvas_transform_inverse, Item *&current_clip, Light *p_lights, PipelineVariants *p_pipeline_variants) {
//create an empty push constant
+ RS::CanvasItemTextureFilter current_filter = default_filter;
+ RS::CanvasItemTextureRepeat current_repeat = default_repeat;
+
+ if (p_item->texture_filter != RS::CANVAS_ITEM_TEXTURE_FILTER_DEFAULT) {
+ current_filter = p_item->texture_filter;
+ }
+
+ if (p_item->texture_repeat != RS::CANVAS_ITEM_TEXTURE_REPEAT_DEFAULT) {
+ current_repeat = p_item->texture_repeat;
+ }
+
PushConstant push_constant;
Transform2D base_transform = p_canvas_transform_inverse * p_item->final_transform;
_update_transform_2d_to_mat2x3(base_transform, push_constant.world);
@@ -513,16 +403,6 @@ void RasterizerCanvasRD::_render_item(RD::DrawListID p_draw_list, const Item *p_
uint32_t base_flags = 0;
- bool light_uniform_set_dirty = false;
-
- if (!p_item->custom_data) {
- p_item->custom_data = memnew(ItemStateData);
- light_uniform_set_dirty = true;
- }
-
- ItemStateData *state_data = (ItemStateData *)p_item->custom_data;
-
- Light *light_cache[DEFAULT_MAX_LIGHTS_PER_ITEM];
uint16_t light_count = 0;
PipelineLightMode light_mode;
@@ -534,160 +414,34 @@ void RasterizerCanvasRD::_render_item(RD::DrawListID p_draw_list, const Item *p_
uint32_t light_index = light->render_index_cache;
push_constant.lights[light_count >> 2] |= light_index << ((light_count & 3) * 8);
- if (!light_uniform_set_dirty && (state_data->light_cache[light_count].light != light || state_data->light_cache[light_count].light_version != light->version)) {
- light_uniform_set_dirty = true;
- }
-
- light_cache[light_count] = light;
-
light_count++;
+
if (light->mode == RS::CANVAS_LIGHT_MODE_MASK) {
base_flags |= FLAGS_USING_LIGHT_MASK;
}
- if (light_count == state.max_lights_per_item) {
+
+ if (light_count == MAX_LIGHTS_PER_ITEM) {
break;
}
}
light = light->next_ptr;
}
- if (light_count != state_data->light_cache_count) {
- light_uniform_set_dirty = true;
- }
base_flags |= light_count << FLAGS_LIGHT_COUNT_SHIFT;
}
- {
- RID &canvas_item_state = light_count ? state_data->state_uniform_set_with_light : state_data->state_uniform_set;
-
- bool invalid_uniform = canvas_item_state.is_valid() && !RD::get_singleton()->uniform_set_is_valid(canvas_item_state);
-
- if (canvas_item_state.is_null() || invalid_uniform || (light_count > 0 && light_uniform_set_dirty)) {
- //re create canvas state
- Vector<RD::Uniform> uniforms;
-
- if (state_data->state_uniform_set_with_light.is_valid() && !invalid_uniform) {
- RD::get_singleton()->free(canvas_item_state);
- }
-
- {
- RD::Uniform u;
- u.type = RD::UNIFORM_TYPE_UNIFORM_BUFFER;
- u.binding = 0;
- u.ids.push_back(state.canvas_state_buffer);
- uniforms.push_back(u);
- }
-
- if (false && p_item->skeleton.is_valid()) {
- //bind skeleton stuff
- } else {
- //bind default
-
- {
- RD::Uniform u;
- u.type = RD::UNIFORM_TYPE_TEXTURE_BUFFER;
- u.binding = 1;
- u.ids.push_back(shader.default_skeleton_texture_buffer);
- uniforms.push_back(u);
- }
-
- {
- RD::Uniform u;
- u.type = RD::UNIFORM_TYPE_UNIFORM_BUFFER;
- u.binding = 2;
- u.ids.push_back(shader.default_skeleton_uniform_buffer);
- uniforms.push_back(u);
- }
- }
-
- {
- RD::Uniform u;
- u.type = RD::UNIFORM_TYPE_STORAGE_BUFFER;
- u.binding = 7;
- u.ids.push_back(storage->global_variables_get_storage_buffer());
- uniforms.push_back(u);
- }
-
- //validate and update lighs if they are being used
-
- if (light_count > 0) {
- //recreate uniform set
-
- {
- RD::Uniform u;
- u.type = RD::UNIFORM_TYPE_UNIFORM_BUFFER;
- u.binding = 3;
- u.ids.push_back(state.lights_uniform_buffer);
- uniforms.push_back(u);
- }
-
- {
- RD::Uniform u_lights;
- u_lights.type = RD::UNIFORM_TYPE_TEXTURE;
- u_lights.binding = 4;
-
- RD::Uniform u_shadows;
- u_shadows.type = RD::UNIFORM_TYPE_TEXTURE;
- u_shadows.binding = 5;
-
- //lights
- for (uint32_t i = 0; i < state.max_lights_per_item; i++) {
- if (i < light_count) {
- CanvasLight *cl = canvas_light_owner.getornull(light_cache[i]->light_internal);
- ERR_CONTINUE(!cl);
-
- RID rd_texture;
-
- if (cl->texture.is_valid()) {
- rd_texture = storage->texture_get_rd_texture(cl->texture);
- }
- if (rd_texture.is_valid()) {
- u_lights.ids.push_back(rd_texture);
- } else {
- u_lights.ids.push_back(storage->texture_rd_get_default(RasterizerStorageRD::DEFAULT_RD_TEXTURE_WHITE));
- }
- if (cl->shadow.texture.is_valid()) {
- u_shadows.ids.push_back(cl->shadow.texture);
- } else {
- u_shadows.ids.push_back(storage->texture_rd_get_default(RasterizerStorageRD::DEFAULT_RD_TEXTURE_BLACK));
- }
- } else {
- u_lights.ids.push_back(storage->texture_rd_get_default(RasterizerStorageRD::DEFAULT_RD_TEXTURE_WHITE));
- u_shadows.ids.push_back(storage->texture_rd_get_default(RasterizerStorageRD::DEFAULT_RD_TEXTURE_BLACK));
- }
- }
-
- uniforms.push_back(u_lights);
- uniforms.push_back(u_shadows);
- }
-
- {
- RD::Uniform u;
- u.type = RD::UNIFORM_TYPE_SAMPLER;
- u.binding = 6;
- u.ids.push_back(state.shadow_sampler);
- uniforms.push_back(u);
- }
-
- canvas_item_state = RD::get_singleton()->uniform_set_create(uniforms, shader.default_version_rd_shader_light, 2);
- } else {
- canvas_item_state = RD::get_singleton()->uniform_set_create(uniforms, shader.default_version_rd_shader, 2);
- }
- }
-
- RD::get_singleton()->draw_list_bind_uniform_set(p_draw_list, canvas_item_state, 2);
- }
-
light_mode = light_count > 0 ? PIPELINE_LIGHT_MODE_ENABLED : PIPELINE_LIGHT_MODE_DISABLED;
PipelineVariants *pipeline_variants = p_pipeline_variants;
bool reclip = false;
+ RID last_texture;
+ Size2 texpixel_size;
+
const Item::Command *c = p_item->commands;
while (c) {
- push_constant.flags = base_flags; //reset on each command for sanity
- push_constant.specular_shininess = 0xFFFFFFFF;
+ push_constant.flags = base_flags | (push_constant.flags & (FLAGS_DEFAULT_NORMAL_MAP_USED | FLAGS_DEFAULT_SPECULAR_MAP_USED)); //reset on each command for sanity, keep canvastexture binding config
switch (c->type) {
case Item::Command::TYPE_RECT: {
@@ -701,26 +455,12 @@ void RasterizerCanvasRD::_render_item(RD::DrawListID p_draw_list, const Item *p_
//bind textures
- Size2 texpixel_size;
- {
- texpixel_size = _bind_texture_binding(rect->texture_binding.binding_id, p_draw_list, push_constant.flags);
- texpixel_size.x = 1.0 / texpixel_size.x;
- texpixel_size.y = 1.0 / texpixel_size.y;
- }
-
- if (rect->specular_shininess.a < 0.999) {
- push_constant.flags |= FLAGS_DEFAULT_SPECULAR_MAP_USED;
- }
-
- _update_specular_shininess(rect->specular_shininess, &push_constant.specular_shininess);
+ _bind_canvas_texture(p_draw_list, rect->texture, current_filter, current_repeat, last_texture, push_constant, texpixel_size);
Rect2 src_rect;
Rect2 dst_rect;
- if (texpixel_size != Vector2()) {
- push_constant.color_texture_pixel_size[0] = texpixel_size.x;
- push_constant.color_texture_pixel_size[1] = texpixel_size.y;
-
+ if (rect->texture != RID()) {
src_rect = (rect->flags & CANVAS_RECT_REGION) ? Rect2(rect->source.position * texpixel_size, rect->source.size * texpixel_size) : Rect2(0, 0, 1, 1);
dst_rect = Rect2(rect->rect.position, rect->rect.size);
@@ -762,7 +502,6 @@ void RasterizerCanvasRD::_render_item(RD::DrawListID p_draw_list, const Item *p_
}
src_rect = Rect2(0, 0, 1, 1);
- texpixel_size = Vector2(1, 1);
}
push_constant.modulation[0] = rect->modulate.r * base_color.r;
@@ -780,9 +519,6 @@ void RasterizerCanvasRD::_render_item(RD::DrawListID p_draw_list, const Item *p_
push_constant.dst_rect[2] = dst_rect.size.width;
push_constant.dst_rect[3] = dst_rect.size.height;
- push_constant.color_texture_pixel_size[0] = texpixel_size.x;
- push_constant.color_texture_pixel_size[1] = texpixel_size.y;
-
RD::get_singleton()->draw_list_set_push_constant(p_draw_list, &push_constant, sizeof(PushConstant));
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);
@@ -800,30 +536,21 @@ void RasterizerCanvasRD::_render_item(RD::DrawListID p_draw_list, const Item *p_
//bind textures
- Size2 texpixel_size;
- {
- texpixel_size = _bind_texture_binding(np->texture_binding.binding_id, p_draw_list, push_constant.flags);
- texpixel_size.x = 1.0 / texpixel_size.x;
- texpixel_size.y = 1.0 / texpixel_size.y;
- }
-
- if (np->specular_shininess.a < 0.999) {
- push_constant.flags |= FLAGS_DEFAULT_SPECULAR_MAP_USED;
- }
-
- _update_specular_shininess(np->specular_shininess, &push_constant.specular_shininess);
+ _bind_canvas_texture(p_draw_list, np->texture, current_filter, current_repeat, last_texture, push_constant, texpixel_size);
Rect2 src_rect;
Rect2 dst_rect(np->rect.position.x, np->rect.position.y, np->rect.size.x, np->rect.size.y);
- if (texpixel_size == Size2()) {
+ if (np->texture == RID()) {
texpixel_size = Size2(1, 1);
src_rect = Rect2(0, 0, 1, 1);
} else {
if (np->source != Rect2()) {
src_rect = Rect2(np->source.position.x * texpixel_size.width, np->source.position.y * texpixel_size.height, np->source.size.x * texpixel_size.width, np->source.size.y * texpixel_size.height);
- texpixel_size = Size2(1.0 / np->source.size.width, 1.0 / np->source.size.height);
+ push_constant.color_texture_pixel_size[0] = 1.0 / np->source.size.width;
+ push_constant.color_texture_pixel_size[1] = 1.0 / np->source.size.height;
+
} else {
src_rect = Rect2(0, 0, 1, 1);
}
@@ -844,9 +571,6 @@ void RasterizerCanvasRD::_render_item(RD::DrawListID p_draw_list, const Item *p_
push_constant.dst_rect[2] = dst_rect.size.width;
push_constant.dst_rect[3] = dst_rect.size.height;
- push_constant.color_texture_pixel_size[0] = texpixel_size.x;
- push_constant.color_texture_pixel_size[1] = texpixel_size.y;
-
push_constant.flags |= int(np->axis_x) << FLAGS_NINEPATCH_H_MODE_SHIFT;
push_constant.flags |= int(np->axis_y) << FLAGS_NINEPATCH_V_MODE_SHIFT;
@@ -863,6 +587,10 @@ void RasterizerCanvasRD::_render_item(RD::DrawListID p_draw_list, const Item *p_
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);
+ //restore if overrided
+ push_constant.color_texture_pixel_size[0] = texpixel_size.x;
+ push_constant.color_texture_pixel_size[1] = texpixel_size.y;
+
} break;
case Item::Command::TYPE_POLYGON: {
const Item::CommandPolygon *polygon = static_cast<const Item::CommandPolygon *>(c);
@@ -884,18 +612,7 @@ void RasterizerCanvasRD::_render_item(RD::DrawListID p_draw_list, const Item *p_
//bind textures
- Size2 texpixel_size;
- {
- texpixel_size = _bind_texture_binding(polygon->texture_binding.binding_id, p_draw_list, push_constant.flags);
- texpixel_size.x = 1.0 / texpixel_size.x;
- texpixel_size.y = 1.0 / texpixel_size.y;
- }
-
- if (polygon->specular_shininess.a < 0.999) {
- push_constant.flags |= FLAGS_DEFAULT_SPECULAR_MAP_USED;
- }
-
- _update_specular_shininess(polygon->specular_shininess, &push_constant.specular_shininess);
+ _bind_canvas_texture(p_draw_list, polygon->texture, current_filter, current_repeat, last_texture, push_constant, texpixel_size);
push_constant.modulation[0] = base_color.r;
push_constant.modulation[1] = base_color.g;
@@ -908,9 +625,6 @@ void RasterizerCanvasRD::_render_item(RD::DrawListID p_draw_list, const Item *p_
push_constant.ninepatch_margins[j] = 0;
}
- push_constant.color_texture_pixel_size[0] = texpixel_size.x;
- push_constant.color_texture_pixel_size[1] = texpixel_size.y;
-
RD::get_singleton()->draw_list_set_push_constant(p_draw_list, &push_constant, sizeof(PushConstant));
RD::get_singleton()->draw_list_bind_vertex_array(p_draw_list, pb->vertex_array);
if (pb->indices.is_valid()) {
@@ -932,15 +646,7 @@ void RasterizerCanvasRD::_render_item(RD::DrawListID p_draw_list, const Item *p_
//bind textures
- {
- _bind_texture_binding(primitive->texture_binding.binding_id, p_draw_list, push_constant.flags);
- }
-
- if (primitive->specular_shininess.a < 0.999) {
- push_constant.flags |= FLAGS_DEFAULT_SPECULAR_MAP_USED;
- }
-
- _update_specular_shininess(primitive->specular_shininess, &push_constant.specular_shininess);
+ _bind_canvas_texture(p_draw_list, RID(), current_filter, current_repeat, last_texture, push_constant, texpixel_size);
RD::get_singleton()->draw_list_bind_index_array(p_draw_list, primitive_arrays.index_array[MIN(3, primitive->point_count) - 1]);
@@ -1295,7 +1001,108 @@ void RasterizerCanvasRD::_render_item(RD::DrawListID p_draw_list, const Item *p_
}
}
-void RasterizerCanvasRD::_render_items(RID p_to_render_target, int p_item_count, const Transform2D &p_canvas_transform_inverse, Light *p_lights, RID p_screen_uniform_set) {
+RID RasterizerCanvasRD::_create_base_uniform_set(RID p_to_render_target, bool p_backbuffer) {
+ //re create canvas state
+ Vector<RD::Uniform> uniforms;
+
+ {
+ RD::Uniform u;
+ u.type = RD::UNIFORM_TYPE_UNIFORM_BUFFER;
+ u.binding = 1;
+ u.ids.push_back(state.canvas_state_buffer);
+ uniforms.push_back(u);
+ }
+
+ {
+ RD::Uniform u;
+ u.type = RD::UNIFORM_TYPE_UNIFORM_BUFFER;
+ u.binding = 2;
+ u.ids.push_back(state.lights_uniform_buffer);
+ uniforms.push_back(u);
+ }
+
+ {
+ RD::Uniform u;
+ u.type = RD::UNIFORM_TYPE_TEXTURE;
+ u.binding = 3;
+ u.ids.push_back(storage->decal_atlas_get_texture());
+ uniforms.push_back(u);
+ }
+
+ {
+ RD::Uniform u;
+ u.type = RD::UNIFORM_TYPE_TEXTURE;
+ u.binding = 4;
+ u.ids.push_back(state.shadow_texture);
+ uniforms.push_back(u);
+ }
+
+ {
+ RD::Uniform u;
+ u.type = RD::UNIFORM_TYPE_SAMPLER;
+ u.binding = 5;
+ u.ids.push_back(state.shadow_sampler);
+ uniforms.push_back(u);
+ }
+
+ {
+ RD::Uniform u;
+ u.type = RD::UNIFORM_TYPE_TEXTURE;
+ u.binding = 6;
+ RID screen;
+ if (p_backbuffer) {
+ screen = storage->render_target_get_rd_texture(p_to_render_target);
+ } else {
+ screen = storage->render_target_get_rd_backbuffer(p_to_render_target);
+ if (screen.is_null()) { //unallocated backbuffer
+ screen = storage->texture_rd_get_default(RasterizerStorageRD::DEFAULT_RD_TEXTURE_WHITE);
+ }
+ }
+ u.ids.push_back(screen);
+ uniforms.push_back(u);
+ }
+
+ {
+ //needs samplers for the material (uses custom textures) create them
+ RD::Uniform u;
+ u.type = RD::UNIFORM_TYPE_SAMPLER;
+ u.binding = 7;
+ u.ids.resize(12);
+ RID *ids_ptr = u.ids.ptrw();
+ ids_ptr[0] = storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_NEAREST, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED);
+ ids_ptr[1] = storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED);
+ ids_ptr[2] = storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_NEAREST_WITH_MIPMAPS, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED);
+ ids_ptr[3] = storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR_WITH_MIPMAPS, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED);
+ ids_ptr[4] = storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_NEAREST_WITH_MIPMAPS_ANISOTROPIC, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED);
+ ids_ptr[5] = storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR_WITH_MIPMAPS_ANISOTROPIC, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED);
+ ids_ptr[6] = storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_NEAREST, RS::CANVAS_ITEM_TEXTURE_REPEAT_ENABLED);
+ ids_ptr[7] = storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR, RS::CANVAS_ITEM_TEXTURE_REPEAT_ENABLED);
+ ids_ptr[8] = storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_NEAREST_WITH_MIPMAPS, RS::CANVAS_ITEM_TEXTURE_REPEAT_ENABLED);
+ ids_ptr[9] = storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR_WITH_MIPMAPS, RS::CANVAS_ITEM_TEXTURE_REPEAT_ENABLED);
+ ids_ptr[10] = storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_NEAREST_WITH_MIPMAPS_ANISOTROPIC, RS::CANVAS_ITEM_TEXTURE_REPEAT_ENABLED);
+ ids_ptr[11] = storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR_WITH_MIPMAPS_ANISOTROPIC, RS::CANVAS_ITEM_TEXTURE_REPEAT_ENABLED);
+ uniforms.push_back(u);
+ }
+
+ {
+ RD::Uniform u;
+ u.type = RD::UNIFORM_TYPE_STORAGE_BUFFER;
+ u.binding = 8;
+ u.ids.push_back(storage->global_variables_get_storage_buffer());
+ uniforms.push_back(u);
+ }
+
+ RID uniform_set = RD::get_singleton()->uniform_set_create(uniforms, shader.default_version_rd_shader, BASE_UNIFORM_SET);
+ if (p_backbuffer) {
+ storage->render_target_set_backbuffer_uniform_set(p_to_render_target, uniform_set);
+ } else {
+ storage->render_target_set_framebuffer_uniform_set(p_to_render_target, uniform_set);
+ }
+
+ return uniform_set;
+}
+
+void RasterizerCanvasRD::_render_items(RID p_to_render_target, int p_item_count, const Transform2D &p_canvas_transform_inverse, Light *p_lights) {
Item *current_clip = nullptr;
Transform2D canvas_transform_inverse = p_canvas_transform_inverse;
@@ -1313,13 +1120,19 @@ void RasterizerCanvasRD::_render_items(RID p_to_render_target, int p_item_count,
#warning TODO obtain from framebuffer format eventually when this is implemented
#endif
+ RID fb_uniform_set = storage->render_target_get_framebuffer_uniform_set(p_to_render_target);
+
+ if (fb_uniform_set.is_null() || !RD::get_singleton()->uniform_set_is_valid(fb_uniform_set)) {
+ fb_uniform_set = _create_base_uniform_set(p_to_render_target, false);
+ }
+
RD::FramebufferFormatID fb_format = RD::get_singleton()->framebuffer_get_format(framebuffer);
RD::DrawListID draw_list = RD::get_singleton()->draw_list_begin(framebuffer, clear ? RD::INITIAL_ACTION_CLEAR : RD::INITIAL_ACTION_KEEP, RD::FINAL_ACTION_READ, RD::INITIAL_ACTION_KEEP, RD::FINAL_ACTION_DISCARD, clear_colors);
- if (p_screen_uniform_set.is_valid()) {
- RD::get_singleton()->draw_list_bind_uniform_set(draw_list, p_screen_uniform_set, 3);
- }
+ RD::get_singleton()->draw_list_bind_uniform_set(draw_list, fb_uniform_set, BASE_UNIFORM_SET);
+ RD::get_singleton()->draw_list_bind_uniform_set(draw_list, state.default_transforms_uniform_set, TRANSFORMS_UNIFORM_SET);
+
RID prev_material;
PipelineVariants *pipeline_variants = &shader.pipeline_variants;
@@ -1349,7 +1162,7 @@ void RasterizerCanvasRD::_render_items(RID p_to_render_target, int p_item_count,
if (material_data->shader_data->version.is_valid() && material_data->shader_data->valid) {
pipeline_variants = &material_data->shader_data->pipeline_variants;
if (material_data->uniform_set.is_valid()) {
- RD::get_singleton()->draw_list_bind_uniform_set(draw_list, material_data->uniform_set, 1);
+ RD::get_singleton()->draw_list_bind_uniform_set(draw_list, material_data->uniform_set, MATERIAL_UNIFORM_SET);
}
} else {
pipeline_variants = &shader.pipeline_variants;
@@ -1367,7 +1180,7 @@ void RasterizerCanvasRD::_render_items(RID p_to_render_target, int p_item_count,
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) {
+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) {
int item_count = 0;
//setup canvas state uniforms if needed
@@ -1435,24 +1248,42 @@ void RasterizerCanvasRD::canvas_render_items(RID p_to_render_target, Item *p_ite
state.light_uniforms[index].height = l->height * (p_canvas_transform.elements[0].length() + p_canvas_transform.elements[1].length()) * 0.5; //approximate height conversion to the canvas size, since all calculations are done in canvas coords to avoid precision loss
for (int i = 0; i < 4; i++) {
- state.light_uniforms[index].shadow_color[i] = l->shadow_color[i];
+ state.light_uniforms[index].shadow_color[i] = uint8_t(CLAMP(int32_t(l->shadow_color[i] * 255.0), 0, 255));
state.light_uniforms[index].color[i] = l->color[i];
}
state.light_uniforms[index].color[3] = l->energy; //use alpha for energy, so base color can go separate
- if (clight->shadow.texture.is_valid()) {
- state.light_uniforms[index].shadow_pixel_size = (1.0 / clight->shadow.size) * (1.0 + l->shadow_smooth);
+ if (state.shadow_fb.is_valid()) {
+ state.light_uniforms[index].shadow_pixel_size = (1.0 / state.shadow_texture_size) * (1.0 + l->shadow_smooth);
+ state.light_uniforms[index].shadow_z_far_inv = 1.0 / clight->shadow.z_far;
+ state.light_uniforms[index].shadow_y_ofs = clight->shadow.y_offset;
} else {
state.light_uniforms[index].shadow_pixel_size = 1.0;
+ state.light_uniforms[index].shadow_z_far_inv = 1.0;
+ state.light_uniforms[index].shadow_y_ofs = 0;
}
state.light_uniforms[index].flags |= l->mode << LIGHT_FLAGS_BLEND_SHIFT;
state.light_uniforms[index].flags |= l->shadow_filter << LIGHT_FLAGS_FILTER_SHIFT;
- if (clight->shadow.texture.is_valid()) {
+ if (clight->shadow.enabled) {
state.light_uniforms[index].flags |= LIGHT_FLAGS_HAS_SHADOW;
}
+ if (clight->texture.is_valid()) {
+ Rect2 atlas_rect = storage->decal_atlas_get_texture_rect(clight->texture);
+ state.light_uniforms[index].atlas_rect[0] = atlas_rect.position.x;
+ state.light_uniforms[index].atlas_rect[1] = atlas_rect.position.y;
+ state.light_uniforms[index].atlas_rect[2] = atlas_rect.size.width;
+ state.light_uniforms[index].atlas_rect[3] = atlas_rect.size.height;
+
+ } else {
+ state.light_uniforms[index].atlas_rect[0] = 0;
+ state.light_uniforms[index].atlas_rect[1] = 0;
+ state.light_uniforms[index].atlas_rect[2] = 0;
+ state.light_uniforms[index].atlas_rect[3] = 0;
+ }
+
l->render_index_cache = index;
index++;
@@ -1464,12 +1295,16 @@ void RasterizerCanvasRD::canvas_render_items(RID p_to_render_target, Item *p_ite
}
}
+ { //default filter/repeat
+ default_filter = p_default_filter;
+ default_repeat = p_default_repeat;
+ }
+
//fill the list until rendering is possible.
bool material_screen_texture_found = false;
Item *ci = p_item_list;
Rect2 back_buffer_rect;
bool backbuffer_copy = false;
- RID screen_uniform_set;
while (ci) {
if (ci->copy_back_buffer) {
@@ -1490,10 +1325,6 @@ void RasterizerCanvasRD::canvas_render_items(RID p_to_render_target, Item *p_ite
backbuffer_copy = true;
back_buffer_rect = Rect2();
}
- if (screen_uniform_set.is_null()) {
- RID backbuffer_shader = shader.canvas_shader.version_get_shader(md->shader_data->version, 0); //any version is fine
- screen_uniform_set = storage->render_target_get_back_buffer_uniform_set(p_to_render_target, backbuffer_shader);
- }
}
if (md->last_frame != RasterizerRD::singleton->get_frame_number()) {
@@ -1509,7 +1340,7 @@ void RasterizerCanvasRD::canvas_render_items(RID p_to_render_target, Item *p_ite
if (backbuffer_copy) {
//render anything pending, including clearing if no items
- _render_items(p_to_render_target, item_count, canvas_transform_inverse, p_light_list, screen_uniform_set);
+ _render_items(p_to_render_target, item_count, canvas_transform_inverse, p_light_list);
item_count = 0;
storage->render_target_copy_to_back_buffer(p_to_render_target, back_buffer_rect);
@@ -1521,7 +1352,7 @@ void RasterizerCanvasRD::canvas_render_items(RID p_to_render_target, Item *p_ite
items[item_count++] = ci;
if (!ci->next || item_count == MAX_RENDER_ITEMS - 1) {
- _render_items(p_to_render_target, item_count, canvas_transform_inverse, p_light_list, screen_uniform_set);
+ _render_items(p_to_render_target, item_count, canvas_transform_inverse, p_light_list);
//then reset
item_count = 0;
}
@@ -1532,7 +1363,6 @@ void RasterizerCanvasRD::canvas_render_items(RID p_to_render_target, Item *p_ite
RID RasterizerCanvasRD::light_create() {
CanvasLight canvas_light;
- canvas_light.shadow.size = 0;
return canvas_light_owner.make_rid(canvas_light);
}
@@ -1542,71 +1372,71 @@ void RasterizerCanvasRD::light_set_texture(RID p_rid, RID p_texture) {
if (cl->texture == p_texture) {
return;
}
-
+ if (cl->texture.is_valid()) {
+ storage->texture_remove_from_decal_atlas(cl->texture);
+ }
cl->texture = p_texture;
+
+ if (cl->texture.is_valid()) {
+ storage->texture_add_to_decal_atlas(cl->texture);
+ }
}
-void RasterizerCanvasRD::light_set_use_shadow(RID p_rid, bool p_enable, int p_resolution) {
+void RasterizerCanvasRD::light_set_use_shadow(RID p_rid, bool p_enable) {
CanvasLight *cl = canvas_light_owner.getornull(p_rid);
ERR_FAIL_COND(!cl);
- ERR_FAIL_COND(p_resolution < 64);
- if (cl->shadow.texture.is_valid() == p_enable && p_resolution == cl->shadow.size) {
- return;
- }
- if (cl->shadow.texture.is_valid()) {
- RD::get_singleton()->free(cl->shadow.fb);
- RD::get_singleton()->free(cl->shadow.depth);
- RD::get_singleton()->free(cl->shadow.texture);
- cl->shadow.fb = RID();
- cl->shadow.texture = RID();
- cl->shadow.depth = RID();
- }
+ cl->shadow.enabled = p_enable;
+}
+
+void RasterizerCanvasRD::light_update_shadow(RID p_rid, int p_shadow_index, const Transform2D &p_light_xform, int p_light_mask, float p_near, float p_far, LightOccluderInstance *p_occluders) {
+ CanvasLight *cl = canvas_light_owner.getornull(p_rid);
+ ERR_FAIL_COND(!cl->shadow.enabled);
+
+ if (state.shadow_fb == RID()) {
+ //ah, we lack the shadow texture..
+ RD::get_singleton()->free(state.shadow_texture); //erase placeholder
- if (p_enable) {
Vector<RID> fb_textures;
{ //texture
RD::TextureFormat tf;
tf.type = RD::TEXTURE_TYPE_2D;
- tf.width = p_resolution;
- tf.height = 1;
+ tf.width = state.shadow_texture_size;
+ tf.height = state.max_lights_per_render * 2;
tf.usage_bits = RD::TEXTURE_USAGE_COLOR_ATTACHMENT_BIT | RD::TEXTURE_USAGE_SAMPLING_BIT;
tf.format = RD::DATA_FORMAT_R32_SFLOAT;
- cl->shadow.texture = RD::get_singleton()->texture_create(tf, RD::TextureView());
- fb_textures.push_back(cl->shadow.texture);
+ state.shadow_texture = RD::get_singleton()->texture_create(tf, RD::TextureView());
+ fb_textures.push_back(state.shadow_texture);
}
{
RD::TextureFormat tf;
tf.type = RD::TEXTURE_TYPE_2D;
- tf.width = p_resolution;
- tf.height = 1;
+ tf.width = state.shadow_texture_size;
+ tf.height = state.max_lights_per_render * 2;
tf.usage_bits = RD::TEXTURE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT;
- tf.format = RD::get_singleton()->texture_is_format_supported_for_usage(RD::DATA_FORMAT_X8_D24_UNORM_PACK32, RD::TEXTURE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT) ? RD::DATA_FORMAT_X8_D24_UNORM_PACK32 : RD::DATA_FORMAT_D32_SFLOAT;
+ tf.format = RD::DATA_FORMAT_D32_SFLOAT;
//chunks to write
- cl->shadow.depth = RD::get_singleton()->texture_create(tf, RD::TextureView());
- fb_textures.push_back(cl->shadow.depth);
+ state.shadow_depth_texture = RD::get_singleton()->texture_create(tf, RD::TextureView());
+ fb_textures.push_back(state.shadow_depth_texture);
}
- cl->shadow.fb = RD::get_singleton()->framebuffer_create(fb_textures);
+ state.shadow_fb = RD::get_singleton()->framebuffer_create(fb_textures);
}
- cl->shadow.size = p_resolution;
-}
-
-void RasterizerCanvasRD::light_update_shadow(RID p_rid, const Transform2D &p_light_xform, int p_light_mask, float p_near, float p_far, LightOccluderInstance *p_occluders) {
- CanvasLight *cl = canvas_light_owner.getornull(p_rid);
- ERR_FAIL_COND(cl->shadow.texture.is_null());
+ cl->shadow.z_far = p_far;
+ cl->shadow.y_offset = float(p_shadow_index * 2 + 1) / float(state.max_lights_per_render * 2);
+ Vector<Color> cc;
+ cc.push_back(Color(p_far, p_far, p_far, 1.0));
for (int i = 0; i < 4; i++) {
//make sure it remains orthogonal, makes easy to read angle later
//light.basis.scale(Vector3(to_light.elements[0].length(),to_light.elements[1].length(),1));
- Vector<Color> cc;
- cc.push_back(Color(p_far, p_far, p_far, 1.0));
- RD::DrawListID draw_list = RD::get_singleton()->draw_list_begin(cl->shadow.fb, RD::INITIAL_ACTION_CLEAR, RD::FINAL_ACTION_READ, RD::INITIAL_ACTION_CLEAR, RD::FINAL_ACTION_DISCARD, cc, 1.0, 0, Rect2i((cl->shadow.size / 4) * i, 0, (cl->shadow.size / 4), 1));
+ Rect2i rect((state.shadow_texture_size / 4) * i, p_shadow_index * 2, (state.shadow_texture_size / 4), 2);
+ RD::DrawListID draw_list = RD::get_singleton()->draw_list_begin(state.shadow_fb, RD::INITIAL_ACTION_CLEAR, RD::FINAL_ACTION_READ, RD::INITIAL_ACTION_CLEAR, RD::FINAL_ACTION_DISCARD, cc, 1.0, 0, rect);
CameraMatrix projection;
{
@@ -1635,8 +1465,8 @@ void RasterizerCanvasRD::light_update_shadow(RID p_rid, const Transform2D &p_lig
static const Vector2 directions[4] = { Vector2(1, 0), Vector2(0, 1), Vector2(-1, 0), Vector2(0, -1) };
push_constant.direction[0] = directions[i].x;
push_constant.direction[1] = directions[i].y;
- push_constant.pad[0] = 0;
- push_constant.pad[1] = 0;
+ push_constant.z_far = p_far;
+ push_constant.pad = 0;
/*if (i == 0)
*p_xform_cache = projection;*/
@@ -1774,7 +1604,6 @@ void RasterizerCanvasRD::ShaderData::set_code(const String &p_code) {
ubo_size = 0;
uniforms.clear();
uses_screen_texture = false;
- uses_material_samplers = false;
if (code == String()) {
return; //just invalid, but no error
@@ -1812,10 +1641,6 @@ void RasterizerCanvasRD::ShaderData::set_code(const String &p_code) {
version = canvas_singleton->shader.canvas_shader.version_create();
}
- if (gen_code.texture_uniforms.size() || uses_screen_texture) { //requires the samplers
- gen_code.defines.push_back("\n#define USE_MATERIAL_SAMPLERS\n");
- uses_material_samplers = true;
- }
#if 0
print_line("**compiling shader:");
print_line("**defines:\n");
@@ -2022,7 +1847,6 @@ Variant RasterizerCanvasRD::ShaderData::get_default_parameter(const StringName &
RasterizerCanvasRD::ShaderData::ShaderData() {
valid = false;
uses_screen_texture = false;
- uses_material_samplers = false;
}
RasterizerCanvasRD::ShaderData::~ShaderData() {
@@ -2085,7 +1909,7 @@ void RasterizerCanvasRD::MaterialData::update_parameters(const Map<StringName, V
update_textures(p_parameters, shader_data->default_texture_params, shader_data->texture_uniforms, texture_cache.ptrw(), false);
}
- if (shader_data->ubo_size == 0 && !shader_data->uses_material_samplers) {
+ if (shader_data->ubo_size == 0) {
// This material does not require an uniform set, so don't create it.
return;
}
@@ -2098,32 +1922,10 @@ void RasterizerCanvasRD::MaterialData::update_parameters(const Map<StringName, V
Vector<RD::Uniform> uniforms;
{
- if (shader_data->uses_material_samplers) {
- //needs samplers for the material (uses custom textures) create them
- RD::Uniform u;
- u.type = RD::UNIFORM_TYPE_SAMPLER;
- u.binding = 0;
- u.ids.resize(12);
- RID *ids_ptr = u.ids.ptrw();
- ids_ptr[0] = canvas_singleton->storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_NEAREST, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED);
- ids_ptr[1] = canvas_singleton->storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED);
- ids_ptr[2] = canvas_singleton->storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_NEAREST_WITH_MIPMAPS, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED);
- ids_ptr[3] = canvas_singleton->storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR_WITH_MIPMAPS, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED);
- ids_ptr[4] = canvas_singleton->storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_NEAREST_WITH_MIPMAPS_ANISOTROPIC, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED);
- ids_ptr[5] = canvas_singleton->storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR_WITH_MIPMAPS_ANISOTROPIC, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED);
- ids_ptr[6] = canvas_singleton->storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_NEAREST, RS::CANVAS_ITEM_TEXTURE_REPEAT_ENABLED);
- ids_ptr[7] = canvas_singleton->storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR, RS::CANVAS_ITEM_TEXTURE_REPEAT_ENABLED);
- ids_ptr[8] = canvas_singleton->storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_NEAREST_WITH_MIPMAPS, RS::CANVAS_ITEM_TEXTURE_REPEAT_ENABLED);
- ids_ptr[9] = canvas_singleton->storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR_WITH_MIPMAPS, RS::CANVAS_ITEM_TEXTURE_REPEAT_ENABLED);
- ids_ptr[10] = canvas_singleton->storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_NEAREST_WITH_MIPMAPS_ANISOTROPIC, RS::CANVAS_ITEM_TEXTURE_REPEAT_ENABLED);
- ids_ptr[11] = canvas_singleton->storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR_WITH_MIPMAPS_ANISOTROPIC, RS::CANVAS_ITEM_TEXTURE_REPEAT_ENABLED);
- uniforms.push_back(u);
- }
-
if (shader_data->ubo_size) {
RD::Uniform u;
u.type = RD::UNIFORM_TYPE_UNIFORM_BUFFER;
- u.binding = 1;
+ u.binding = 0;
u.ids.push_back(uniform_buffer);
uniforms.push_back(u);
}
@@ -2132,13 +1934,13 @@ void RasterizerCanvasRD::MaterialData::update_parameters(const Map<StringName, V
for (uint32_t i = 0; i < tex_uniform_count; i++) {
RD::Uniform u;
u.type = RD::UNIFORM_TYPE_TEXTURE;
- u.binding = 2 + i;
+ u.binding = 1 + i;
u.ids.push_back(textures[i]);
uniforms.push_back(u);
}
}
- uniform_set = RD::get_singleton()->uniform_set_create(uniforms, canvas_singleton->shader.canvas_shader.version_get_shader(shader_data->version, 0), 1);
+ uniform_set = RD::get_singleton()->uniform_set_create(uniforms, canvas_singleton->shader.canvas_shader.version_get_shader(shader_data->version, 0), MATERIAL_UNIFORM_SET);
}
RasterizerCanvasRD::MaterialData::~MaterialData() {
@@ -2164,7 +1966,6 @@ void RasterizerCanvasRD::set_time(double p_time) {
}
void RasterizerCanvasRD::update() {
- _dispose_bindings();
}
RasterizerCanvasRD::RasterizerCanvasRD(RasterizerStorageRD *p_storage) {
@@ -2178,22 +1979,7 @@ RasterizerCanvasRD::RasterizerCanvasRD(RasterizerStorageRD *p_storage) {
{ //shader variants
- uint32_t textures_per_stage = RD::get_singleton()->limit_get(RD::LIMIT_MAX_TEXTURES_PER_SHADER_STAGE);
-
String global_defines;
- if (textures_per_stage <= 16) {
- //ARM pretty much, and very old Intel GPUs under Linux
- state.max_lights_per_item = 4; //sad
- global_defines += "#define MAX_LIGHT_TEXTURES 4\n";
- } else if (textures_per_stage <= 32) {
- //Apple (Metal)
- state.max_lights_per_item = 8; //sad
- global_defines += "#define MAX_LIGHT_TEXTURES 8\n";
- } else {
- //Anything else (16 lights per item)
- state.max_lights_per_item = DEFAULT_MAX_LIGHTS_PER_ITEM;
- global_defines += "#define MAX_LIGHT_TEXTURES " + itos(DEFAULT_MAX_LIGHTS_PER_ITEM) + "\n";
- }
uint32_t uniform_max_size = RD::get_singleton()->limit_get(RD::LIMIT_MAX_UNIFORM_BUFFER_SIZE);
if (uniform_max_size < 65536) {
@@ -2226,7 +2012,6 @@ RasterizerCanvasRD::RasterizerCanvasRD(RasterizerStorageRD *p_storage) {
shader.default_version = shader.canvas_shader.version_create();
shader.default_version_rd_shader = shader.canvas_shader.version_get_shader(shader.default_version, SHADER_VARIANT_QUAD);
- shader.default_version_rd_shader_light = shader.canvas_shader.version_get_shader(shader.default_version, SHADER_VARIANT_QUAD_LIGHT);
for (int i = 0; i < PIPELINE_LIGHT_MODE_MAX; i++) {
for (int j = 0; j < PIPELINE_VARIANT_MAX; j++) {
@@ -2327,8 +2112,8 @@ RasterizerCanvasRD::RasterizerCanvasRD(RasterizerStorageRD *p_storage) {
actions.custom_samplers["SPECULAR_SHININESS_TEXTURE"] = "texture_sampler";
actions.custom_samplers["SCREEN_TEXTURE"] = "material_samplers[3]"; //mipmap and filter for screen texture
actions.sampler_array_name = "material_samplers";
- actions.base_texture_binding_index = 2;
- actions.texture_layout_set = 1;
+ actions.base_texture_binding_index = 1;
+ actions.texture_layout_set = MATERIAL_UNIFORM_SET;
actions.base_uniform_string = "material.";
actions.default_filter = ShaderLanguage::FILTER_LINEAR;
actions.default_repeat = ShaderLanguage::REPEAT_DISABLE;
@@ -2354,7 +2139,7 @@ RasterizerCanvasRD::RasterizerCanvasRD(RasterizerStorageRD *p_storage) {
attachments.push_back(af_color);
RD::AttachmentFormat af_depth;
- af_depth.format = RD::get_singleton()->texture_is_format_supported_for_usage(RD::DATA_FORMAT_D32_SFLOAT, RD::TEXTURE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT) ? RD::DATA_FORMAT_D32_SFLOAT : RD::DATA_FORMAT_X8_D24_UNORM_PACK32;
+ af_depth.format = RD::DATA_FORMAT_D32_SFLOAT;
af_depth.usage_flags = RD::TEXTURE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT;
attachments.push_back(af_depth);
@@ -2386,21 +2171,17 @@ RasterizerCanvasRD::RasterizerCanvasRD(RasterizerStorageRD *p_storage) {
}
{ //bindings
- bindings.id_generator = 0;
- //generate for 0
- bindings.default_empty = request_texture_binding(RID(), RID(), RID(), RS::CANVAS_ITEM_TEXTURE_FILTER_DEFAULT, RS::CANVAS_ITEM_TEXTURE_REPEAT_DEFAULT, RID());
-
- { //state allocate
- state.canvas_state_buffer = RD::get_singleton()->uniform_buffer_create(sizeof(State::Buffer));
- state.lights_uniform_buffer = RD::get_singleton()->uniform_buffer_create(sizeof(LightUniform) * state.max_lights_per_render);
-
- RD::SamplerState shadow_sampler_state;
- shadow_sampler_state.mag_filter = RD::SAMPLER_FILTER_LINEAR;
- shadow_sampler_state.min_filter = RD::SAMPLER_FILTER_LINEAR;
- shadow_sampler_state.repeat_u = RD::SAMPLER_REPEAT_MODE_REPEAT; //shadow wrap around
- shadow_sampler_state.compare_op = RD::COMPARE_OP_GREATER;
- state.shadow_sampler = RD::get_singleton()->sampler_create(shadow_sampler_state);
- }
+
+ state.canvas_state_buffer = RD::get_singleton()->uniform_buffer_create(sizeof(State::Buffer));
+ state.lights_uniform_buffer = RD::get_singleton()->uniform_buffer_create(sizeof(LightUniform) * state.max_lights_per_render);
+
+ RD::SamplerState shadow_sampler_state;
+ shadow_sampler_state.mag_filter = RD::SAMPLER_FILTER_LINEAR;
+ shadow_sampler_state.min_filter = RD::SAMPLER_FILTER_LINEAR;
+ shadow_sampler_state.repeat_u = RD::SAMPLER_REPEAT_MODE_REPEAT; //shadow wrap around
+ shadow_sampler_state.compare_op = RD::COMPARE_OP_GREATER;
+ shadow_sampler_state.enable_compare = true;
+ state.shadow_sampler = RD::get_singleton()->sampler_create(shadow_sampler_state);
}
{
@@ -2443,6 +2224,35 @@ RasterizerCanvasRD::RasterizerCanvasRD(RasterizerStorageRD *p_storage) {
shader.default_skeleton_texture_buffer = RD::get_singleton()->texture_buffer_create(32, RD::DATA_FORMAT_R32G32B32A32_SFLOAT);
}
+ {
+ //default shadow texture to keep uniform set happy
+ RD::TextureFormat tf;
+ tf.type = RD::TEXTURE_TYPE_2D;
+ tf.width = 4;
+ tf.height = 4;
+ tf.usage_bits = RD::TEXTURE_USAGE_SAMPLING_BIT;
+ tf.format = RD::DATA_FORMAT_R32_SFLOAT;
+
+ state.shadow_texture = RD::get_singleton()->texture_create(tf, RD::TextureView());
+ }
+
+ {
+ Vector<RD::Uniform> uniforms;
+
+ {
+ RD::Uniform u;
+ u.type = RD::UNIFORM_TYPE_STORAGE_BUFFER;
+ u.binding = 0;
+ u.ids.push_back(storage->get_default_rd_storage_buffer());
+ uniforms.push_back(u);
+ }
+
+ state.default_transforms_uniform_set = RD::get_singleton()->uniform_set_create(uniforms, shader.default_version_rd_shader, TRANSFORMS_UNIFORM_SET);
+ }
+
+ default_canvas_texture = storage->canvas_texture_create();
+
+ state.shadow_texture_size = GLOBAL_GET("rendering/quality/2d_shadow_atlas/size");
//create functions for shader and material
storage->shader_set_data_request_function(RasterizerStorageRD::SHADER_TYPE_2D, _create_shader_funcs);
@@ -2457,7 +2267,7 @@ bool RasterizerCanvasRD::free(RID p_rid) {
if (canvas_light_owner.owns(p_rid)) {
CanvasLight *cl = canvas_light_owner.getornull(p_rid);
ERR_FAIL_COND_V(!cl, false);
- light_set_use_shadow(p_rid, false, 64);
+ light_set_use_shadow(p_rid, false);
canvas_light_owner.free(p_rid);
} else if (occluder_polygon_owner.owns(p_rid)) {
occluder_polygon_set_shape_as_lines(p_rid, Vector<Vector2>());
@@ -2469,6 +2279,31 @@ bool RasterizerCanvasRD::free(RID p_rid) {
return true;
}
+void RasterizerCanvasRD::set_shadow_texture_size(int p_size) {
+ p_size = nearest_power_of_2_templated(p_size);
+ if (p_size == state.shadow_texture_size) {
+ return;
+ }
+ state.shadow_texture_size = p_size;
+ if (state.shadow_fb.is_valid()) {
+ RD::get_singleton()->free(state.shadow_texture);
+ RD::get_singleton()->free(state.shadow_depth_texture);
+ state.shadow_fb = RID();
+
+ {
+ //create a default shadow texture to keep uniform set happy (and that it gets erased when a new one is created)
+ RD::TextureFormat tf;
+ tf.type = RD::TEXTURE_TYPE_2D;
+ tf.width = 4;
+ tf.height = 4;
+ tf.usage_bits = RD::TEXTURE_USAGE_SAMPLING_BIT;
+ tf.format = RD::DATA_FORMAT_R32_SFLOAT;
+
+ state.shadow_texture = RD::get_singleton()->texture_create(tf, RD::TextureView());
+ }
+ }
+}
+
RasterizerCanvasRD::~RasterizerCanvasRD() {
//canvas state
@@ -2490,24 +2325,6 @@ RasterizerCanvasRD::~RasterizerCanvasRD() {
RD::get_singleton()->free(state.shadow_sampler);
}
//bindings
- {
- free_texture_binding(bindings.default_empty);
-
- //dispose pending
- _dispose_bindings();
- //anything remains?
- if (bindings.texture_bindings.size()) {
- ERR_PRINT("Some texture bindings were not properly freed (leaked CanvasItems?)");
- const TextureBindingID *key = nullptr;
- while ((key = bindings.texture_bindings.next(key))) {
- TextureBinding *tb = bindings.texture_bindings[*key];
- tb->reference_count = 1;
- free_texture_binding(*key);
- }
- //dispose pending
- _dispose_bindings();
- }
- }
//shaders
@@ -2520,5 +2337,13 @@ RasterizerCanvasRD::~RasterizerCanvasRD() {
//primitives are erase by dependency
}
+ if (state.shadow_fb.is_valid()) {
+ RD::get_singleton()->free(state.shadow_depth_texture);
+ }
+ RD::get_singleton()->free(state.shadow_texture);
+
+ storage->free(default_canvas_texture);
//pipelines don't need freeing, they are all gone after shaders are gone
+
+ RD::get_singleton()->free(state.default_transforms_uniform_set);
}