Skip to content

Commit

Permalink
Removed ontop property, added a material rendering priority system. F…
Browse files Browse the repository at this point in the history
…ixes #9935, closes #10135
  • Loading branch information
reduz committed Sep 1, 2017
1 parent 6e9e25b commit 8f30c52
Show file tree
Hide file tree
Showing 15 changed files with 119 additions and 40 deletions.
14 changes: 8 additions & 6 deletions drivers/gles3/rasterizer_scene_gles3.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1100,15 +1100,15 @@ bool RasterizerSceneGLES3::_setup_material(RasterizerStorageGLES3::Material *p_m
state.current_line_width = p_material->line_width;
}

if (state.current_depth_test != (!p_material->shader->spatial.ontop)) {
if (p_material->shader->spatial.ontop) {
if (state.current_depth_test != (!p_material->shader->spatial.no_depth_test)) {
if (p_material->shader->spatial.no_depth_test) {
glDisable(GL_DEPTH_TEST);

} else {
glEnable(GL_DEPTH_TEST);
}

state.current_depth_test = !p_material->shader->spatial.ontop;
state.current_depth_test = !p_material->shader->spatial.no_depth_test;
}

if (state.current_depth_draw != p_material->shader->spatial.depth_draw_mode) {
Expand Down Expand Up @@ -2195,7 +2195,7 @@ void RasterizerSceneGLES3::_add_geometry(RasterizerStorageGLES3::Geometry *p_geo
void RasterizerSceneGLES3::_add_geometry_with_material(RasterizerStorageGLES3::Geometry *p_geometry, InstanceBase *p_instance, RasterizerStorageGLES3::GeometryOwner *p_owner, RasterizerStorageGLES3::Material *p_material, bool p_shadow) {

bool has_base_alpha = (p_material->shader->spatial.uses_alpha && !p_material->shader->spatial.uses_alpha_scissor) || p_material->shader->spatial.uses_screen_texture;
bool has_blend_alpha = p_material->shader->spatial.blend_mode != RasterizerStorageGLES3::Shader::Spatial::BLEND_MODE_MIX || p_material->shader->spatial.ontop;
bool has_blend_alpha = p_material->shader->spatial.blend_mode != RasterizerStorageGLES3::Shader::Spatial::BLEND_MODE_MIX;
bool has_alpha = has_base_alpha || has_blend_alpha;
bool shadow = false;

Expand Down Expand Up @@ -2267,7 +2267,7 @@ void RasterizerSceneGLES3::_add_geometry_with_material(RasterizerStorageGLES3::G
}

e->sort_key |= uint64_t(e->material->index) << RenderList::SORT_KEY_MATERIAL_INDEX_SHIFT;
e->sort_key |= uint64_t(e->instance->depth_layer) << RenderList::SORT_KEY_DEPTH_LAYER_SHIFT;
e->sort_key |= uint64_t(e->instance->depth_layer) << RenderList::SORT_KEY_OPAQUE_DEPTH_LAYER_SHIFT;

if (!has_blend_alpha && has_alpha && p_material->shader->spatial.depth_draw_mode == RasterizerStorageGLES3::Shader::Spatial::DEPTH_DRAW_ALPHA_PREPASS) {

Expand All @@ -2283,6 +2283,8 @@ void RasterizerSceneGLES3::_add_geometry_with_material(RasterizerStorageGLES3::G
if (e->instance->gi_probe_instances.size()) {
e->sort_key |= SORT_KEY_GI_PROBES_FLAG;
}

e->sort_key |= uint64_t(p_material->render_priority + 128) << RenderList::SORT_KEY_PRIORITY_SHIFT;
}

/*
Expand Down Expand Up @@ -4282,7 +4284,7 @@ void RasterizerSceneGLES3::render_scene(const Transform &p_cam_transform, const
glEnable(GL_DEPTH_TEST);
glDisable(GL_SCISSOR_TEST);

render_list.sort_by_reverse_depth(true);
render_list.sort_by_reverse_depth_and_priority(true);

if (state.directional_light_count == 0) {
directional_light = NULL;
Expand Down
40 changes: 27 additions & 13 deletions drivers/gles3/rasterizer_scene_gles3.h
Original file line number Diff line number Diff line change
Expand Up @@ -645,17 +645,25 @@ class RasterizerSceneGLES3 : public RasterizerScene {
MAX_LIGHTS = 4096,
MAX_REFLECTIONS = 1024,

SORT_KEY_DEPTH_LAYER_SHIFT = 60,
SORT_KEY_PRIORITY_SHIFT = 56,
SORT_KEY_PRIORITY_MASK = 0xFF,
//depth layer for opaque (56-52)
SORT_KEY_OPAQUE_DEPTH_LAYER_SHIFT = 52,
SORT_KEY_OPAQUE_DEPTH_LAYER_MASK = 0xF,
//64 bits unsupported in MSVC
#define SORT_KEY_UNSHADED_FLAG (uint64_t(1) << 59)
#define SORT_KEY_NO_DIRECTIONAL_FLAG (uint64_t(1) << 58)
#define SORT_KEY_GI_PROBES_FLAG (uint64_t(1) << 57)
#define SORT_KEY_VERTEX_LIT_FLAG (uint64_t(1) << 56)
SORT_KEY_SHADING_SHIFT = 56,
#define SORT_KEY_UNSHADED_FLAG (uint64_t(1) << 51)
#define SORT_KEY_NO_DIRECTIONAL_FLAG (uint64_t(1) << 50)
#define SORT_KEY_GI_PROBES_FLAG (uint64_t(1) << 49)
#define SORT_KEY_VERTEX_LIT_FLAG (uint64_t(1) << 48)
SORT_KEY_SHADING_SHIFT = 48,
SORT_KEY_SHADING_MASK = 15,
SORT_KEY_MATERIAL_INDEX_SHIFT = 40,
SORT_KEY_GEOMETRY_INDEX_SHIFT = 20,
SORT_KEY_GEOMETRY_TYPE_SHIFT = 15,
//48-32 material index
SORT_KEY_MATERIAL_INDEX_SHIFT = 32,
//32-12 geometry index
SORT_KEY_GEOMETRY_INDEX_SHIFT = 12,
//bits 12-8 geometry type
SORT_KEY_GEOMETRY_TYPE_SHIFT = 8,
//bits 0-7 for flags
SORT_KEY_CULL_DISABLED_FLAG = 4,
SORT_KEY_SKELETON_FLAG = 2,
SORT_KEY_MIRROR_FLAG = 1
Expand Down Expand Up @@ -721,16 +729,22 @@ class RasterizerSceneGLES3 : public RasterizerScene {
}
}

struct SortByReverseDepth {
struct SortByReverseDepthAndPriority {

_FORCE_INLINE_ bool operator()(const Element *A, const Element *B) const {
return A->instance->depth > B->instance->depth;
uint32_t layer_A = uint32_t(A->sort_key >> SORT_KEY_PRIORITY_SHIFT);
uint32_t layer_B = uint32_t(B->sort_key >> SORT_KEY_PRIORITY_SHIFT);
if (layer_A == layer_B) {
return A->instance->depth > B->instance->depth;
} else {
return layer_A < layer_B;
}
}
};

void sort_by_reverse_depth(bool p_alpha) { //used for alpha
void sort_by_reverse_depth_and_priority(bool p_alpha) { //used for alpha

SortArray<Element *, SortByReverseDepth> sorter;
SortArray<Element *, SortByReverseDepthAndPriority> sorter;
if (p_alpha) {
sorter.sort(&elements[max_elements - alpha_element_count], alpha_element_count);
} else {
Expand Down
15 changes: 13 additions & 2 deletions drivers/gles3/rasterizer_storage_gles3.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1599,7 +1599,7 @@ void RasterizerStorageGLES3::_update_shader(Shader *p_shader) const {
p_shader->spatial.uses_alpha_scissor = false;
p_shader->spatial.uses_discard = false;
p_shader->spatial.unshaded = false;
p_shader->spatial.ontop = false;
p_shader->spatial.no_depth_test = false;
p_shader->spatial.uses_sss = false;
p_shader->spatial.uses_vertex_lighting = false;
p_shader->spatial.uses_screen_texture = false;
Expand All @@ -1621,7 +1621,7 @@ void RasterizerStorageGLES3::_update_shader(Shader *p_shader) const {
shaders.actions_scene.render_mode_values["cull_disabled"] = Pair<int *, int>(&p_shader->spatial.cull_mode, Shader::Spatial::CULL_MODE_DISABLED);

shaders.actions_scene.render_mode_flags["unshaded"] = &p_shader->spatial.unshaded;
shaders.actions_scene.render_mode_flags["ontop"] = &p_shader->spatial.ontop;
shaders.actions_scene.render_mode_flags["depth_test_disable"] = &p_shader->spatial.no_depth_test;

shaders.actions_scene.render_mode_flags["vertex_lighting"] = &p_shader->spatial.uses_vertex_lighting;

Expand Down Expand Up @@ -1948,6 +1948,17 @@ void RasterizerStorageGLES3::material_remove_instance_owner(RID p_material, Rast
}
}

void RasterizerStorageGLES3::material_set_render_priority(RID p_material, int priority) {

ERR_FAIL_COND(priority < VS::MATERIAL_RENDER_PRIORITY_MIN);
ERR_FAIL_COND(priority > VS::MATERIAL_RENDER_PRIORITY_MAX);

Material *material = material_owner.get(p_material);
ERR_FAIL_COND(!material);

material->render_priority = priority;
}

_FORCE_INLINE_ static void _fill_std140_variant_ubo_value(ShaderLanguage::DataType type, const Variant &value, uint8_t *data, bool p_linear_color) {
switch (type) {
case ShaderLanguage::TYPE_BOOL: {
Expand Down
6 changes: 5 additions & 1 deletion drivers/gles3/rasterizer_storage_gles3.h
Original file line number Diff line number Diff line change
Expand Up @@ -444,7 +444,7 @@ class RasterizerStorageGLES3 : public RasterizerStorage {
bool uses_alpha;
bool uses_alpha_scissor;
bool unshaded;
bool ontop;
bool no_depth_test;
bool uses_vertex;
bool uses_discard;
bool uses_sss;
Expand Down Expand Up @@ -502,6 +502,7 @@ class RasterizerStorageGLES3 : public RasterizerStorage {
SelfList<Material> dirty_list;
Vector<RID> textures;
float line_width;
int render_priority;

RID next_pass;

Expand All @@ -523,6 +524,7 @@ class RasterizerStorageGLES3 : public RasterizerStorage {
ubo_id = 0;
ubo_size = 0;
last_pass = 0;
render_priority = 0;
}
};

Expand Down Expand Up @@ -550,6 +552,8 @@ class RasterizerStorageGLES3 : public RasterizerStorage {
virtual void material_add_instance_owner(RID p_material, RasterizerScene::InstanceBase *p_instance);
virtual void material_remove_instance_owner(RID p_material, RasterizerScene::InstanceBase *p_instance);

virtual void material_set_render_priority(RID p_material, int priority);

void _update_material(Material *material);

void update_dirty_materials();
Expand Down
6 changes: 3 additions & 3 deletions editor/plugins/spatial_editor_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3505,7 +3505,7 @@ void SpatialEditor::_init_indicators() {

gizmo_hl = Ref<SpatialMaterial>(memnew(SpatialMaterial));
gizmo_hl->set_flag(SpatialMaterial::FLAG_UNSHADED, true);
gizmo_hl->set_flag(SpatialMaterial::FLAG_ONTOP, true);
gizmo_hl->set_on_top_of_alpha();
gizmo_hl->set_feature(SpatialMaterial::FEATURE_TRANSPARENT, true);
gizmo_hl->set_albedo(Color(1, 1, 1, gizmo_alph + 0.2f));
gizmo_hl->set_cull_mode(SpatialMaterial::CULL_DISABLED);
Expand All @@ -3518,7 +3518,7 @@ void SpatialEditor::_init_indicators() {

Ref<SpatialMaterial> mat = memnew(SpatialMaterial);
mat->set_flag(SpatialMaterial::FLAG_UNSHADED, true);
mat->set_flag(SpatialMaterial::FLAG_ONTOP, true);
mat->set_on_top_of_alpha();
mat->set_feature(SpatialMaterial::FEATURE_TRANSPARENT, true);
Color col;
col[i] = 1.0;
Expand Down Expand Up @@ -3613,7 +3613,7 @@ void SpatialEditor::_init_indicators() {

Ref<SpatialMaterial> plane_mat = memnew(SpatialMaterial);
plane_mat->set_flag(SpatialMaterial::FLAG_UNSHADED, true);
plane_mat->set_flag(SpatialMaterial::FLAG_ONTOP, true);
plane_mat->set_on_top_of_alpha();
plane_mat->set_feature(SpatialMaterial::FEATURE_TRANSPARENT, true);
plane_mat->set_cull_mode(SpatialMaterial::CULL_DISABLED);
Color col;
Expand Down
12 changes: 6 additions & 6 deletions editor/spatial_editor_gizmos.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -580,7 +580,7 @@ Ref<SpatialMaterial> EditorSpatialGizmo::create_material(const String &p_name, c
}

if (p_on_top && is_selected()) {
line_material->set_flag(SpatialMaterial::FLAG_ONTOP, true);
line_material->set_on_top_of_alpha();
}

line_material->set_albedo(color);
Expand Down Expand Up @@ -624,7 +624,7 @@ Ref<SpatialMaterial> EditorSpatialGizmo::create_icon_material(const String &p_na
icon->set_billboard_mode(SpatialMaterial::BILLBOARD_ENABLED);

if (p_on_top && is_selected()) {
icon->set_flag(SpatialMaterial::FLAG_ONTOP, true);
icon->set_on_top_of_alpha();
}

SpatialEditorGizmos::singleton->material_cache[name] = icon;
Expand Down Expand Up @@ -3411,7 +3411,7 @@ SpatialEditorGizmos::SpatialEditorGizmos() {

handle_material = Ref<SpatialMaterial>(memnew(SpatialMaterial));
handle_material->set_flag(SpatialMaterial::FLAG_UNSHADED, true);
handle_material->set_flag(SpatialMaterial::FLAG_ONTOP, true);
handle_material->set_on_top_of_alpha();
handle_material->set_albedo(Color(0.8, 0.8, 0.8));
handle_material_billboard = handle_material->duplicate();
handle_material_billboard->set_billboard_mode(SpatialMaterial::BILLBOARD_ENABLED);
Expand All @@ -3426,11 +3426,11 @@ SpatialEditorGizmos::SpatialEditorGizmos() {
handle2_material->set_feature(SpatialMaterial::FEATURE_TRANSPARENT, true);
handle2_material->set_flag(SpatialMaterial::FLAG_ALBEDO_FROM_VERTEX_COLOR, true);
handle2_material->set_flag(SpatialMaterial::FLAG_SRGB_VERTEX_COLOR, true);
handle2_material->set_flag(SpatialMaterial::FLAG_ONTOP, true);
handle2_material->set_on_top_of_alpha();
handle2_material_billboard = handle2_material->duplicate();
handle2_material_billboard->set_billboard_mode(SpatialMaterial::BILLBOARD_ENABLED);
handle2_material_billboard->set_billboard_mode(SpatialMaterial::BILLBOARD_ENABLED);
handle2_material_billboard->set_flag(SpatialMaterial::FLAG_ONTOP, true);
handle2_material_billboard->set_on_top_of_alpha();

EDITOR_DEF("editors/3d_gizmos/gizmo_colors/light", Color(1, 1, 0.2));
EDITOR_DEF("editors/3d_gizmos/gizmo_colors/stream_player_3d", Color(0.4, 0.8, 1));
Expand Down Expand Up @@ -3490,7 +3490,7 @@ SpatialEditorGizmos::SpatialEditorGizmos() {
skeleton_material = create_line_material(Color(0.6, 1.0, 0.3));
skeleton_material->set_cull_mode(SpatialMaterial::CULL_DISABLED);
skeleton_material->set_flag(SpatialMaterial::FLAG_UNSHADED, true);
skeleton_material->set_flag(SpatialMaterial::FLAG_ONTOP, true);
skeleton_material->set_on_top_of_alpha();
skeleton_material->set_depth_draw_mode(SpatialMaterial::DEPTH_DRAW_DISABLED);

//position 3D Shared mesh
Expand Down
4 changes: 2 additions & 2 deletions modules/gridmap/grid_map_editor_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1159,14 +1159,14 @@ GridMapEditor::GridMapEditor(EditorNode *p_editor) {

outer_mat.instance();
outer_mat->set_albedo(Color(0.7, 0.7, 1.0, 0.8));
outer_mat->set_flag(SpatialMaterial::FLAG_ONTOP, true);
outer_mat->set_on_top_of_alpha();
outer_mat->set_flag(SpatialMaterial::FLAG_UNSHADED, true);
outer_mat->set_line_width(3.0);
outer_mat->set_feature(SpatialMaterial::FEATURE_TRANSPARENT, true);

selection_floor_mat.instance();
selection_floor_mat->set_albedo(Color(0.80, 0.80, 1.0, 1));
selection_floor_mat->set_flag(SpatialMaterial::FLAG_ONTOP, true);
selection_floor_mat->set_on_top_of_alpha();
selection_floor_mat->set_flag(SpatialMaterial::FLAG_UNSHADED, true);
selection_floor_mat->set_line_width(3.0);
//selection_floor_mat->set_feature(SpatialMaterial::FEATURE_TRANSPARENT, true);
Expand Down
35 changes: 31 additions & 4 deletions scene/resources/material.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,19 @@ Ref<Material> Material::get_next_pass() const {
return next_pass;
}

void Material::set_render_priority(int p_priority) {

ERR_FAIL_COND(p_priority < RENDER_PRIORITY_MIN);
ERR_FAIL_COND(p_priority > RENDER_PRIORITY_MAX);
render_priority = p_priority;
VS::get_singleton()->material_set_render_priority(material, p_priority);
}

int Material::get_render_priority() const {

return render_priority;
}

RID Material::get_rid() const {

return material;
Expand All @@ -58,12 +71,20 @@ void Material::_bind_methods() {
ClassDB::bind_method(D_METHOD("set_next_pass", "next_pass"), &Material::set_next_pass);
ClassDB::bind_method(D_METHOD("get_next_pass"), &Material::get_next_pass);

ClassDB::bind_method(D_METHOD("set_render_priority", "priority"), &Material::set_render_priority);
ClassDB::bind_method(D_METHOD("get_render_priority"), &Material::get_render_priority);

ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "render_priority", PROPERTY_HINT_RANGE, itos(RENDER_PRIORITY_MIN) + "," + itos(RENDER_PRIORITY_MAX) + ",1"), "set_render_priority", "get_render_priority");
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "next_pass", PROPERTY_HINT_RESOURCE_TYPE, "Material"), "set_next_pass", "get_next_pass");

BIND_CONSTANT(RENDER_PRIORITY_MAX);
BIND_CONSTANT(RENDER_PRIORITY_MIN);
}

Material::Material() {

material = VisualServer::get_singleton()->material_create();
render_priority = 0;
}

Material::~Material() {
Expand Down Expand Up @@ -347,8 +368,8 @@ void SpatialMaterial::_update_shader() {
if (flags[FLAG_UNSHADED]) {
code += ",unshaded";
}
if (flags[FLAG_ONTOP]) {
code += ",ontop";
if (flags[FLAG_DISABLE_DEPTH_TEST]) {
code += ",depth_test_disable";
}
if (flags[FLAG_USE_VERTEX_LIGHTING]) {
code += ",vertex_lighting";
Expand Down Expand Up @@ -1459,6 +1480,12 @@ RID SpatialMaterial::get_material_rid_for_2d(bool p_shaded, bool p_transparent,
return materials_for_2d[version]->get_rid();
}

void SpatialMaterial::set_on_top_of_alpha() {
set_feature(FEATURE_TRANSPARENT, true);
set_render_priority(RENDER_PRIORITY_MAX);
set_flag(FLAG_DISABLE_DEPTH_TEST, true);
}

void SpatialMaterial::_bind_methods() {

ClassDB::bind_method(D_METHOD("set_albedo", "albedo"), &SpatialMaterial::set_albedo);
Expand Down Expand Up @@ -1606,7 +1633,7 @@ void SpatialMaterial::_bind_methods() {
ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "flags_transparent"), "set_feature", "get_feature", FEATURE_TRANSPARENT);
ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "flags_unshaded"), "set_flag", "get_flag", FLAG_UNSHADED);
ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "flags_vertex_lighting"), "set_flag", "get_flag", FLAG_USE_VERTEX_LIGHTING);
ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "flags_on_top"), "set_flag", "get_flag", FLAG_ONTOP);
ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "flags_no_depth_test"), "set_flag", "get_flag", FLAG_DISABLE_DEPTH_TEST);
ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "flags_use_point_size"), "set_flag", "get_flag", FLAG_USE_POINT_SIZE);
ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "flags_fixed_size"), "set_flag", "get_flag", FLAG_FIXED_SIZE);
ADD_GROUP("Vertex Color", "vertex_color");
Expand Down Expand Up @@ -1768,7 +1795,7 @@ void SpatialMaterial::_bind_methods() {

BIND_ENUM_CONSTANT(FLAG_UNSHADED);
BIND_ENUM_CONSTANT(FLAG_USE_VERTEX_LIGHTING);
BIND_ENUM_CONSTANT(FLAG_ONTOP);
BIND_ENUM_CONSTANT(FLAG_DISABLE_DEPTH_TEST);
BIND_ENUM_CONSTANT(FLAG_ALBEDO_FROM_VERTEX_COLOR);
BIND_ENUM_CONSTANT(FLAG_SRGB_VERTEX_COLOR);
BIND_ENUM_CONSTANT(FLAG_USE_POINT_SIZE);
Expand Down
Loading

0 comments on commit 8f30c52

Please sign in to comment.