Welcome to mirror list, hosted at ThFree Co, Russian Federation.

git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLukas Stockner <lukas.stockner@freenet.de>2019-12-12 18:06:08 +0300
committerLukas Stockner <lukas.stockner@freenet.de>2019-12-12 20:40:37 +0300
commitc30d6571bb47734e0bcb2fced5cf11cb6d8b1169 (patch)
treef92f065147296e6c72e6cd85f26711157d81e09c /intern/cycles/render/nodes.cpp
parentd7a8a606889fed58775c88bfdc079bee3c9333e2 (diff)
Add support for tiled images and the UDIM naming scheme
This patch contains the work that I did during my week at the Code Quest - adding support for tiled images to Blender. With this patch, images now contain a list of tiles. By default, this just contains one tile, but if the source type is set to Tiled, the user can add additional tiles. When acquiring an ImBuf, the tile to be loaded is specified in the ImageUser. Therefore, code that is not yet aware of tiles will just access the default tile as usual. The filenames of the additional tiles are derived from the original filename according to the UDIM naming scheme - the filename contains an index that is calculated as (1001 + 10*<y coordinate of the tile> + <x coordinate of the tile>), where the x coordinate never goes above 9. Internally, the various tiles are stored in a cache just like sequences. When acquired for the first time, the code will try to load the corresponding file from disk. Alternatively, a new operator can be used to initialize the tile similar to the New Image operator. The following features are supported so far: - Automatic detection and loading of all tiles when opening the first tile (1001) - Saving all tiles - Adding and removing tiles - Filling tiles with generated images - Drawing all tiles in the Image Editor - Viewing a tiled grid even if no image is selected - Rendering tiled images in Eevee - Rendering tiled images in Cycles (in SVM mode) - Automatically skipping loading of unused tiles in Cycles - 2D texture painting (also across tiles) - 3D texture painting (also across tiles, only limitation: individual faces can not cross tile borders) - Assigning custom labels to individual tiles (drawn in the Image Editor instead of the ID) - Different resolutions between tiles There still are some missing features that will be added later (see T72390): - Workbench engine support - Packing/Unpacking support - Baking support - Cycles OSL support - many other Blender features that rely on images Thanks to Brecht for the review and to all who tested the intermediate versions! Differential Revision: https://developer.blender.org/D3509
Diffstat (limited to 'intern/cycles/render/nodes.cpp')
-rw-r--r--intern/cycles/render/nodes.cpp288
1 files changed, 199 insertions, 89 deletions
diff --git a/intern/cycles/render/nodes.cpp b/intern/cycles/render/nodes.cpp
index 5e12d79bc6b..b8847f92153 100644
--- a/intern/cycles/render/nodes.cpp
+++ b/intern/cycles/render/nodes.cpp
@@ -19,6 +19,7 @@
#include "render/image.h"
#include "render/integrator.h"
#include "render/light.h"
+#include "render/mesh.h"
#include "render/nodes.h"
#include "render/scene.h"
#include "render/svm.h"
@@ -204,6 +205,27 @@ void TextureMapping::compile(OSLCompiler &compiler)
/* Image Texture */
+ImageSlotTextureNode::~ImageSlotTextureNode()
+{
+ if (image_manager) {
+ foreach (int slot, slots) {
+ if (slot != -1) {
+ image_manager->remove_image(slot);
+ }
+ }
+ }
+}
+
+void ImageSlotTextureNode::add_image_user() const
+{
+ /* Increase image user count for new node. */
+ foreach (int slot, slots) {
+ if (slot != -1) {
+ image_manager->add_image_user(slot);
+ }
+ }
+}
+
NODE_DEFINE(ImageTextureNode)
{
NodeType *type = NodeType::add("image_texture", create, NodeType::SHADER);
@@ -253,30 +275,71 @@ NODE_DEFINE(ImageTextureNode)
ImageTextureNode::ImageTextureNode() : ImageSlotTextureNode(node_type)
{
- image_manager = NULL;
- slot = -1;
- is_float = -1;
+ is_float = false;
compress_as_srgb = false;
colorspace = u_colorspace_raw;
builtin_data = NULL;
animated = false;
+ tiles.push_back(1001);
}
-ImageTextureNode::~ImageTextureNode()
+ShaderNode *ImageTextureNode::clone() const
{
- if (image_manager) {
- image_manager->remove_image(
- filename.string(), builtin_data, interpolation, extension, alpha_type, colorspace);
- }
+ add_image_user();
+ return new ImageTextureNode(*this);
}
-ShaderNode *ImageTextureNode::clone() const
+void ImageTextureNode::cull_tiles(Scene *scene, ShaderGraph *graph)
{
- /* Increase image user count for new node. */
- if (slot != -1) {
- image_manager->add_image_user(slot);
+ if (!scene->params.background) {
+ /* During interactive renders, all tiles are loaded.
+ * While we could support updating this when UVs change, that could lead
+ * to annoying interruptions when loading images while editing UVs. */
+ return;
}
- return new ImageTextureNode(*this);
+
+ /* Only check UVs for tile culling if there are multiple tiles. */
+ if (tiles.size() < 2) {
+ return;
+ }
+
+ ShaderInput *vector_in = input("Vector");
+ ustring attribute;
+ if (vector_in->link) {
+ ShaderNode *node = vector_in->link->parent;
+ if (node->type == UVMapNode::node_type) {
+ UVMapNode *uvmap = (UVMapNode *)node;
+ attribute = uvmap->attribute;
+ }
+ else if (node->type == TextureCoordinateNode::node_type) {
+ if (vector_in->link != node->output("UV")) {
+ return;
+ }
+ }
+ else {
+ return;
+ }
+ }
+
+ unordered_set<int> used_tiles;
+ /* TODO(lukas): This is quite inefficient. A fairly simple improvement would
+ * be to have a cache in each mesh that is indexed by attribute.
+ * Additionally, building a graph-to-meshes list once could help. */
+ foreach (Mesh *mesh, scene->meshes) {
+ foreach (Shader *shader, mesh->used_shaders) {
+ if (shader->graph == graph) {
+ mesh->get_uv_tiles(attribute, used_tiles);
+ }
+ }
+ }
+
+ ccl::vector<int> new_tiles;
+ foreach (int tile, tiles) {
+ if (used_tiles.count(tile)) {
+ new_tiles.push_back(tile);
+ }
+ }
+ tiles.swap(new_tiles);
}
void ImageTextureNode::attributes(Shader *shader, AttributeRequestSet *attributes)
@@ -300,24 +363,61 @@ void ImageTextureNode::compile(SVMCompiler &compiler)
ShaderOutput *color_out = output("Color");
ShaderOutput *alpha_out = output("Alpha");
- image_manager = compiler.image_manager;
- if (is_float == -1) {
- ImageMetaData metadata;
- slot = image_manager->add_image(filename.string(),
- builtin_data,
- animated,
- 0,
- interpolation,
- extension,
- alpha_type,
- colorspace,
- metadata);
- is_float = metadata.is_float;
- compress_as_srgb = metadata.compress_as_srgb;
- known_colorspace = metadata.colorspace;
+ image_manager = compiler.scene->image_manager;
+ if (slots.empty()) {
+ cull_tiles(compiler.scene, compiler.current_graph);
}
+ if (slots.size() < tiles.size()) {
+ slots.clear();
+ slots.reserve(tiles.size());
+
+ bool have_metadata = false;
+ foreach (int tile, tiles) {
+ string tile_name = filename.string();
+ if (tiles.size() > 1) {
+ tile_name = string_printf(tile_name.c_str(), tile);
+ }
+
+ ImageMetaData metadata;
+ int slot = image_manager->add_image(tile_name,
+ builtin_data,
+ animated,
+ 0,
+ interpolation,
+ extension,
+ alpha_type,
+ colorspace,
+ metadata);
+ slots.push_back(slot);
+
+ /* We assume that all tiles have the same metadata. */
+ if (!have_metadata) {
+ is_float = metadata.is_float;
+ compress_as_srgb = metadata.compress_as_srgb;
+ known_colorspace = metadata.colorspace;
+ have_metadata = true;
+ }
+ }
+ }
+
+ bool has_image = false;
+ foreach (int slot, slots) {
+ if (slot != -1) {
+ has_image = true;
+ break;
+ }
+ }
+
+ if (has_image) {
+ /* If there only is one image (a very common case), we encode it as a negative value. */
+ int num_nodes;
+ if (slots.size() == 1) {
+ num_nodes = -slots[0];
+ }
+ else {
+ num_nodes = divide_up(slots.size(), 2);
+ }
- if (slot != -1) {
int vector_offset = tex_mapping.compile_begin(compiler, vector_in);
uint flags = 0;
@@ -336,7 +436,7 @@ void ImageTextureNode::compile(SVMCompiler &compiler)
if (projection != NODE_IMAGE_PROJ_BOX) {
compiler.add_node(NODE_TEX_IMAGE,
- slot,
+ num_nodes,
compiler.encode_uchar4(vector_offset,
compiler.stack_assign_if_linked(color_out),
compiler.stack_assign_if_linked(alpha_out),
@@ -345,7 +445,7 @@ void ImageTextureNode::compile(SVMCompiler &compiler)
}
else {
compiler.add_node(NODE_TEX_IMAGE_BOX,
- slot,
+ num_nodes,
compiler.encode_uchar4(vector_offset,
compiler.stack_assign_if_linked(color_out),
compiler.stack_assign_if_linked(alpha_out),
@@ -353,6 +453,23 @@ void ImageTextureNode::compile(SVMCompiler &compiler)
__float_as_int(projection_blend));
}
+ if (num_nodes > 0) {
+ for (int i = 0; i < num_nodes; i++) {
+ int4 node;
+ node.x = tiles[2 * i];
+ node.y = slots[2 * i];
+ if (2 * i + 1 < slots.size()) {
+ node.z = tiles[2 * i + 1];
+ node.w = slots[2 * i + 1];
+ }
+ else {
+ node.z = -1;
+ node.w = -1;
+ }
+ compiler.add_node(node.x, node.y, node.z, node.w);
+ }
+ }
+
tex_mapping.compile_end(compiler, vector_in, vector_offset);
}
else {
@@ -375,34 +492,37 @@ void ImageTextureNode::compile(OSLCompiler &compiler)
tex_mapping.compile(compiler);
- image_manager = compiler.image_manager;
- if (is_float == -1) {
+ image_manager = compiler.scene->image_manager;
+ if (slots.size() == 0) {
ImageMetaData metadata;
if (builtin_data == NULL) {
image_manager->get_image_metadata(filename.string(), NULL, colorspace, metadata);
+ slots.push_back(-1);
}
else {
- slot = image_manager->add_image(filename.string(),
- builtin_data,
- animated,
- 0,
- interpolation,
- extension,
- alpha_type,
- colorspace,
- metadata);
+ /* TODO(lukas): OSL UDIMs */
+ int slot = image_manager->add_image(filename.string(),
+ builtin_data,
+ animated,
+ 0,
+ interpolation,
+ extension,
+ alpha_type,
+ colorspace,
+ metadata);
+ slots.push_back(slot);
}
is_float = metadata.is_float;
compress_as_srgb = metadata.compress_as_srgb;
known_colorspace = metadata.colorspace;
}
- if (slot == -1) {
+ if (slots[0] == -1) {
compiler.parameter_texture(
"filename", filename, compress_as_srgb ? u_colorspace_raw : known_colorspace);
}
else {
- compiler.parameter_texture("filename", slot);
+ compiler.parameter_texture("filename", slots[0]);
}
const bool unassociate_alpha = !(ColorSpaceManager::colorspace_is_data(colorspace) ||
@@ -462,29 +582,16 @@ NODE_DEFINE(EnvironmentTextureNode)
EnvironmentTextureNode::EnvironmentTextureNode() : ImageSlotTextureNode(node_type)
{
- image_manager = NULL;
- slot = -1;
- is_float = -1;
+ is_float = false;
compress_as_srgb = false;
colorspace = u_colorspace_raw;
builtin_data = NULL;
animated = false;
}
-EnvironmentTextureNode::~EnvironmentTextureNode()
-{
- if (image_manager) {
- image_manager->remove_image(
- filename.string(), builtin_data, interpolation, EXTENSION_REPEAT, alpha_type, colorspace);
- }
-}
-
ShaderNode *EnvironmentTextureNode::clone() const
{
- /* Increase image user count for new node. */
- if (slot != -1) {
- image_manager->add_image_user(slot);
- }
+ add_image_user();
return new EnvironmentTextureNode(*this);
}
@@ -507,24 +614,25 @@ void EnvironmentTextureNode::compile(SVMCompiler &compiler)
ShaderOutput *color_out = output("Color");
ShaderOutput *alpha_out = output("Alpha");
- image_manager = compiler.image_manager;
- if (slot == -1) {
+ image_manager = compiler.scene->image_manager;
+ if (slots.empty()) {
ImageMetaData metadata;
- slot = image_manager->add_image(filename.string(),
- builtin_data,
- animated,
- 0,
- interpolation,
- EXTENSION_REPEAT,
- alpha_type,
- colorspace,
- metadata);
+ int slot = image_manager->add_image(filename.string(),
+ builtin_data,
+ animated,
+ 0,
+ interpolation,
+ EXTENSION_REPEAT,
+ alpha_type,
+ colorspace,
+ metadata);
+ slots.push_back(slot);
is_float = metadata.is_float;
compress_as_srgb = metadata.compress_as_srgb;
known_colorspace = metadata.colorspace;
}
- if (slot != -1) {
+ if (slots[0] != -1) {
int vector_offset = tex_mapping.compile_begin(compiler, vector_in);
uint flags = 0;
@@ -533,7 +641,7 @@ void EnvironmentTextureNode::compile(SVMCompiler &compiler)
}
compiler.add_node(NODE_TEX_ENVIRONMENT,
- slot,
+ slots[0],
compiler.encode_uchar4(vector_offset,
compiler.stack_assign_if_linked(color_out),
compiler.stack_assign_if_linked(alpha_out),
@@ -563,34 +671,36 @@ void EnvironmentTextureNode::compile(OSLCompiler &compiler)
/* See comments in ImageTextureNode::compile about support
* of builtin images.
*/
- image_manager = compiler.image_manager;
- if (is_float == -1) {
+ image_manager = compiler.scene->image_manager;
+ if (slots.empty()) {
ImageMetaData metadata;
if (builtin_data == NULL) {
image_manager->get_image_metadata(filename.string(), NULL, colorspace, metadata);
+ slots.push_back(-1);
}
else {
- slot = image_manager->add_image(filename.string(),
- builtin_data,
- animated,
- 0,
- interpolation,
- EXTENSION_REPEAT,
- alpha_type,
- colorspace,
- metadata);
+ int slot = image_manager->add_image(filename.string(),
+ builtin_data,
+ animated,
+ 0,
+ interpolation,
+ EXTENSION_REPEAT,
+ alpha_type,
+ colorspace,
+ metadata);
+ slots.push_back(slot);
}
is_float = metadata.is_float;
compress_as_srgb = metadata.compress_as_srgb;
known_colorspace = metadata.colorspace;
}
- if (slot == -1) {
+ if (slots[0] == -1) {
compiler.parameter_texture(
"filename", filename, compress_as_srgb ? u_colorspace_raw : known_colorspace);
}
else {
- compiler.parameter_texture("filename", slot);
+ compiler.parameter_texture("filename", slots[0]);
}
compiler.parameter(this, "projection");
@@ -1123,7 +1233,7 @@ void IESLightNode::get_slot()
void IESLightNode::compile(SVMCompiler &compiler)
{
- light_manager = compiler.light_manager;
+ light_manager = compiler.scene->light_manager;
get_slot();
ShaderInput *strength_in = input("Strength");
@@ -1145,7 +1255,7 @@ void IESLightNode::compile(SVMCompiler &compiler)
void IESLightNode::compile(OSLCompiler &compiler)
{
- light_manager = compiler.light_manager;
+ light_manager = compiler.scene->light_manager;
get_slot();
tex_mapping.compile(compiler);
@@ -1663,7 +1773,7 @@ void PointDensityTextureNode::compile(SVMCompiler &compiler)
const bool use_density = !density_out->links.empty();
const bool use_color = !color_out->links.empty();
- image_manager = compiler.image_manager;
+ image_manager = compiler.scene->image_manager;
if (use_density || use_color) {
add_image();
@@ -1704,7 +1814,7 @@ void PointDensityTextureNode::compile(OSLCompiler &compiler)
const bool use_density = !density_out->links.empty();
const bool use_color = !color_out->links.empty();
- image_manager = compiler.image_manager;
+ image_manager = compiler.scene->image_manager;
if (use_density || use_color) {
add_image();