diff options
author | Patrick Mours <pmours@nvidia.com> | 2022-09-09 12:55:35 +0300 |
---|---|---|
committer | Patrick Mours <pmours@nvidia.com> | 2022-09-09 16:35:44 +0300 |
commit | ef7c9e793ec5331ac694eec9336565bd2254c406 (patch) | |
tree | ca30908bae0bac8c58d18b964af7dc388f9de463 /intern/cycles/scene | |
parent | 291c313f80b4cccc8fcce3035584caeaa654844f (diff) |
Cycles: Remove separate OSL attribute map and instead always use SVM attribute map
The SVM attribute map is always generated and uses a simple
linear search to lookup by an opaque ID, so can reuse that for OSL
as well and simply use the attribute name hash as ID instead of
generating a unique value separately. This works for both object
and geometry attributes since the SVM attribute map already
stores both. Simplifies code somewhat and reduces memory
usage slightly.
This patch was split from D15902.
Differential Revision: https://developer.blender.org/D15918
Diffstat (limited to 'intern/cycles/scene')
-rw-r--r-- | intern/cycles/scene/geometry.cpp | 152 | ||||
-rw-r--r-- | intern/cycles/scene/geometry.h | 4 | ||||
-rw-r--r-- | intern/cycles/scene/osl.cpp | 12 | ||||
-rw-r--r-- | intern/cycles/scene/osl.h | 3 | ||||
-rw-r--r-- | intern/cycles/scene/shader.cpp | 8 | ||||
-rw-r--r-- | intern/cycles/scene/shader.h | 6 |
6 files changed, 73 insertions, 112 deletions
diff --git a/intern/cycles/scene/geometry.cpp b/intern/cycles/scene/geometry.cpp index ae8dcaa43b6..d1a3df851c1 100644 --- a/intern/cycles/scene/geometry.cpp +++ b/intern/cycles/scene/geometry.cpp @@ -302,111 +302,32 @@ GeometryManager::~GeometryManager() { } -void GeometryManager::update_osl_attributes(Device *device, - Scene *scene, - vector<AttributeRequestSet> &geom_attributes) +void GeometryManager::update_osl_globals(Device *device, Scene *scene) { #ifdef WITH_OSL - /* for OSL, a hash map is used to lookup the attribute by name. */ OSLGlobals *og = (OSLGlobals *)device->get_cpu_osl_memory(); og->object_name_map.clear(); - og->attribute_map.clear(); og->object_names.clear(); - og->attribute_map.resize(scene->objects.size() * ATTR_PRIM_TYPES); - for (size_t i = 0; i < scene->objects.size(); i++) { /* set object name to object index map */ Object *object = scene->objects[i]; og->object_name_map[object->name] = i; og->object_names.push_back(object->name); - - /* set object attributes */ - foreach (ParamValue &attr, object->attributes) { - OSLGlobals::Attribute osl_attr; - - osl_attr.type = attr.type(); - osl_attr.desc.element = ATTR_ELEMENT_OBJECT; - osl_attr.value = attr; - osl_attr.desc.offset = 0; - osl_attr.desc.flags = 0; - - og->attribute_map[i * ATTR_PRIM_TYPES + ATTR_PRIM_GEOMETRY][attr.name()] = osl_attr; - og->attribute_map[i * ATTR_PRIM_TYPES + ATTR_PRIM_SUBD][attr.name()] = osl_attr; - } - - /* find geometry attributes */ - size_t j = object->geometry->index; - assert(j < scene->geometry.size() && scene->geometry[j] == object->geometry); - - AttributeRequestSet &attributes = geom_attributes[j]; - - /* set mesh attributes */ - foreach (AttributeRequest &req, attributes.requests) { - OSLGlobals::Attribute osl_attr; - - if (req.desc.element != ATTR_ELEMENT_NONE) { - osl_attr.desc = req.desc; - - if (req.type == TypeDesc::TypeFloat) - osl_attr.type = TypeDesc::TypeFloat; - else if (req.type == TypeDesc::TypeMatrix) - osl_attr.type = TypeDesc::TypeMatrix; - else if (req.type == TypeFloat2) - osl_attr.type = TypeFloat2; - else if (req.type == TypeRGBA) - osl_attr.type = TypeRGBA; - else - osl_attr.type = TypeDesc::TypeColor; - - if (req.std != ATTR_STD_NONE) { - /* if standard attribute, add lookup by geom: name convention */ - ustring stdname(string("geom:") + string(Attribute::standard_name(req.std))); - og->attribute_map[i * ATTR_PRIM_TYPES + ATTR_PRIM_GEOMETRY][stdname] = osl_attr; - } - else if (req.name != ustring()) { - /* add lookup by geometry attribute name */ - og->attribute_map[i * ATTR_PRIM_TYPES + ATTR_PRIM_GEOMETRY][req.name] = osl_attr; - } - } - - if (req.subd_desc.element != ATTR_ELEMENT_NONE) { - osl_attr.desc = req.subd_desc; - - if (req.subd_type == TypeDesc::TypeFloat) - osl_attr.type = TypeDesc::TypeFloat; - else if (req.subd_type == TypeDesc::TypeMatrix) - osl_attr.type = TypeDesc::TypeMatrix; - else if (req.subd_type == TypeFloat2) - osl_attr.type = TypeFloat2; - else if (req.subd_type == TypeRGBA) - osl_attr.type = TypeRGBA; - else - osl_attr.type = TypeDesc::TypeColor; - - if (req.std != ATTR_STD_NONE) { - /* if standard attribute, add lookup by geom: name convention */ - ustring stdname(string("geom:") + string(Attribute::standard_name(req.std))); - og->attribute_map[i * ATTR_PRIM_TYPES + ATTR_PRIM_SUBD][stdname] = osl_attr; - } - else if (req.name != ustring()) { - /* add lookup by geometry attribute name */ - og->attribute_map[i * ATTR_PRIM_TYPES + ATTR_PRIM_SUBD][req.name] = osl_attr; - } - } - } } #else (void)device; (void)scene; - (void)geom_attributes; #endif } /* Generate a normal attribute map entry from an attribute descriptor. */ -static void emit_attribute_map_entry( - AttributeMap *attr_map, int index, uint id, TypeDesc type, const AttributeDescriptor &desc) +static void emit_attribute_map_entry(AttributeMap *attr_map, + size_t index, + uint64_t id, + TypeDesc type, + const AttributeDescriptor &desc) { attr_map[index].id = id; attr_map[index].element = desc.element; @@ -431,7 +352,7 @@ static void emit_attribute_map_entry( /* Generate an attribute map end marker, optionally including a link to another map. * Links are used to connect object attribute maps to mesh attribute maps. */ static void emit_attribute_map_terminator(AttributeMap *attr_map, - int index, + size_t index, bool chain, uint chain_link) { @@ -446,15 +367,8 @@ static void emit_attribute_map_terminator(AttributeMap *attr_map, /* Generate all necessary attribute map entries from the attribute request. */ static void emit_attribute_mapping( - AttributeMap *attr_map, int index, Scene *scene, AttributeRequest &req, Geometry *geom) + AttributeMap *attr_map, size_t index, uint64_t id, AttributeRequest &req, Geometry *geom) { - uint id; - - if (req.std == ATTR_STD_NONE) - id = scene->shader_manager->get_attribute_id(req.name); - else - id = scene->shader_manager->get_attribute_id(req.std); - emit_attribute_map_entry(attr_map, index, id, req.type, req.desc); if (geom->is_mesh()) { @@ -475,12 +389,26 @@ void GeometryManager::update_svm_attributes(Device *, * attribute, based on a unique shader attribute id. */ /* compute array stride */ - int attr_map_size = 0; + size_t attr_map_size = 0; for (size_t i = 0; i < scene->geometry.size(); i++) { Geometry *geom = scene->geometry[i]; geom->attr_map_offset = attr_map_size; - attr_map_size += (geom_attributes[i].size() + 1) * ATTR_PRIM_TYPES; + +#ifdef WITH_OSL + size_t attr_count = 0; + foreach (AttributeRequest &req, geom_attributes[i].requests) { + if (req.std != ATTR_STD_NONE && + scene->shader_manager->get_attribute_id(req.std) != (uint64_t)req.std) + attr_count += 2; + else + attr_count += 1; + } +#else + const size_t attr_count = geom_attributes[i].size(); +#endif + + attr_map_size += (attr_count + 1) * ATTR_PRIM_TYPES; } for (size_t i = 0; i < scene->objects.size(); i++) { @@ -512,11 +440,26 @@ void GeometryManager::update_svm_attributes(Device *, AttributeRequestSet &attributes = geom_attributes[i]; /* set geometry attributes */ - int index = geom->attr_map_offset; + size_t index = geom->attr_map_offset; foreach (AttributeRequest &req, attributes.requests) { - emit_attribute_mapping(attr_map, index, scene, req, geom); + uint64_t id; + if (req.std == ATTR_STD_NONE) + id = scene->shader_manager->get_attribute_id(req.name); + else + id = scene->shader_manager->get_attribute_id(req.std); + + emit_attribute_mapping(attr_map, index, id, req, geom); index += ATTR_PRIM_TYPES; + +#ifdef WITH_OSL + /* Some standard attributes are explicitly referenced via their standard ID, so add those + * again in case they were added under a different attribute ID. */ + if (req.std != ATTR_STD_NONE && id != (uint64_t)req.std) { + emit_attribute_mapping(attr_map, index, (uint64_t)req.std, req, geom); + index += ATTR_PRIM_TYPES; + } +#endif } emit_attribute_map_terminator(attr_map, index, false, 0); @@ -528,10 +471,16 @@ void GeometryManager::update_svm_attributes(Device *, /* set object attributes */ if (attributes.size() > 0) { - int index = object->attr_map_offset; + size_t index = object->attr_map_offset; foreach (AttributeRequest &req, attributes.requests) { - emit_attribute_mapping(attr_map, index, scene, req, object->geometry); + uint64_t id; + if (req.std == ATTR_STD_NONE) + id = scene->shader_manager->get_attribute_id(req.name); + else + id = scene->shader_manager->get_attribute_id(req.std); + + emit_attribute_mapping(attr_map, index, id, req, object->geometry); index += ATTR_PRIM_TYPES; } @@ -982,7 +931,7 @@ void GeometryManager::device_update_attributes(Device *device, /* create attribute lookup maps */ if (scene->shader_manager->use_osl()) - update_osl_attributes(device, scene, geom_attributes); + update_osl_globals(device, scene); update_svm_attributes(device, dscene, scene, geom_attributes, object_attributes); @@ -2188,7 +2137,6 @@ void GeometryManager::device_free(Device *device, DeviceScene *dscene, bool forc if (og) { og->object_name_map.clear(); - og->attribute_map.clear(); og->object_names.clear(); } #else diff --git a/intern/cycles/scene/geometry.h b/intern/cycles/scene/geometry.h index 6210a64509a..8a1bdc33a6f 100644 --- a/intern/cycles/scene/geometry.h +++ b/intern/cycles/scene/geometry.h @@ -219,9 +219,7 @@ class GeometryManager { void create_volume_mesh(const Scene *scene, Volume *volume, Progress &progress); /* Attributes */ - void update_osl_attributes(Device *device, - Scene *scene, - vector<AttributeRequestSet> &geom_attributes); + void update_osl_globals(Device *device, Scene *scene); void update_svm_attributes(Device *device, DeviceScene *dscene, Scene *scene, diff --git a/intern/cycles/scene/osl.cpp b/intern/cycles/scene/osl.cpp index f5ee0c0f1d3..7c8d9bcd3e2 100644 --- a/intern/cycles/scene/osl.cpp +++ b/intern/cycles/scene/osl.cpp @@ -78,6 +78,18 @@ void OSLShaderManager::reset(Scene * /*scene*/) shading_system_init(); } +uint64_t OSLShaderManager::get_attribute_id(ustring name) +{ + return name.hash(); +} + +uint64_t OSLShaderManager::get_attribute_id(AttributeStandard std) +{ + /* if standard attribute, use geom: name convention */ + ustring stdname(string("geom:") + string(Attribute::standard_name(std))); + return stdname.hash(); +} + void OSLShaderManager::device_update_specific(Device *device, DeviceScene *dscene, Scene *scene, diff --git a/intern/cycles/scene/osl.h b/intern/cycles/scene/osl.h index bf27069b1b1..76c6bd96ce1 100644 --- a/intern/cycles/scene/osl.h +++ b/intern/cycles/scene/osl.h @@ -66,6 +66,9 @@ class OSLShaderManager : public ShaderManager { return true; } + uint64_t get_attribute_id(ustring name) override; + uint64_t get_attribute_id(AttributeStandard std) override; + void device_update_specific(Device *device, DeviceScene *dscene, Scene *scene, diff --git a/intern/cycles/scene/shader.cpp b/intern/cycles/scene/shader.cpp index bd647ab55e7..96a8f40bbad 100644 --- a/intern/cycles/scene/shader.cpp +++ b/intern/cycles/scene/shader.cpp @@ -414,7 +414,7 @@ ShaderManager *ShaderManager::create(int shadingsystem) return manager; } -uint ShaderManager::get_attribute_id(ustring name) +uint64_t ShaderManager::get_attribute_id(ustring name) { thread_scoped_spin_lock lock(attribute_lock_); @@ -424,14 +424,14 @@ uint ShaderManager::get_attribute_id(ustring name) if (it != unique_attribute_id.end()) return it->second; - uint id = (uint)ATTR_STD_NUM + unique_attribute_id.size(); + uint64_t id = ATTR_STD_NUM + unique_attribute_id.size(); unique_attribute_id[name] = id; return id; } -uint ShaderManager::get_attribute_id(AttributeStandard std) +uint64_t ShaderManager::get_attribute_id(AttributeStandard std) { - return (uint)std; + return (uint64_t)std; } int ShaderManager::get_shader_id(Shader *shader, bool smooth) diff --git a/intern/cycles/scene/shader.h b/intern/cycles/scene/shader.h index 274bb9b4fa1..2670776aca4 100644 --- a/intern/cycles/scene/shader.h +++ b/intern/cycles/scene/shader.h @@ -192,8 +192,8 @@ class ShaderManager { void device_free_common(Device *device, DeviceScene *dscene, Scene *scene); /* get globally unique id for a type of attribute */ - uint get_attribute_id(ustring name); - uint get_attribute_id(AttributeStandard std); + virtual uint64_t get_attribute_id(ustring name); + virtual uint64_t get_attribute_id(AttributeStandard std); /* get shader id for mesh faces */ int get_shader_id(Shader *shader, bool smooth = false); @@ -223,7 +223,7 @@ class ShaderManager { uint32_t update_flags; - typedef unordered_map<ustring, uint, ustringHash> AttributeIDMap; + typedef unordered_map<ustring, uint64_t, ustringHash> AttributeIDMap; AttributeIDMap unique_attribute_id; static thread_mutex lookup_table_mutex; |