2023-05-31 16:19:06 +02:00
|
|
|
/* SPDX-FileCopyrightText: 2001-2002 NaN Holding BV. All rights reserved.
|
|
|
|
|
*
|
|
|
|
|
* SPDX-License-Identifier: GPL-2.0-or-later */
|
2022-01-17 18:00:35 +01:00
|
|
|
|
|
|
|
|
/** \file
|
|
|
|
|
* \ingroup render
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include "BLI_assert.h"
|
|
|
|
|
#include "BLI_math_geom.h"
|
|
|
|
|
#include "BLI_math_vector.hh"
|
2023-01-04 00:14:55 +01:00
|
|
|
#include "BLI_math_vector_types.hh"
|
2022-01-17 18:00:35 +01:00
|
|
|
#include "BLI_vector.hh"
|
|
|
|
|
|
|
|
|
|
#include "BKE_DerivedMesh.h"
|
2022-05-15 20:27:28 +02:00
|
|
|
#include "BKE_customdata.h"
|
2023-03-12 22:29:15 +01:00
|
|
|
#include "BKE_mesh.hh"
|
2023-08-02 22:14:18 +02:00
|
|
|
#include "BKE_mesh_mapping.hh"
|
2022-01-17 18:00:35 +01:00
|
|
|
|
|
|
|
|
#include "DNA_mesh_types.h"
|
|
|
|
|
#include "DNA_meshdata_types.h"
|
|
|
|
|
|
|
|
|
|
#include "IMB_imbuf.h"
|
|
|
|
|
#include "IMB_imbuf_types.h"
|
|
|
|
|
|
|
|
|
|
#include "MEM_guardedalloc.h"
|
|
|
|
|
|
|
|
|
|
#include "zbuf.h" // for rasterizer
|
|
|
|
|
|
|
|
|
|
#include "RE_texture_margin.h"
|
|
|
|
|
|
|
|
|
|
#include <algorithm>
|
2022-01-27 10:53:34 -06:00
|
|
|
#include <cmath>
|
2022-01-17 18:00:35 +01:00
|
|
|
#include <valarray>
|
|
|
|
|
|
|
|
|
|
namespace blender::render::texturemargin {
|
|
|
|
|
|
2022-02-02 13:15:43 +11:00
|
|
|
/**
|
2023-07-24 22:06:55 +02:00
|
|
|
* The map class contains both a pixel map which maps out face indices for all UV-polygons and
|
2022-01-17 18:00:35 +01:00
|
|
|
* adjacency tables.
|
|
|
|
|
*/
|
|
|
|
|
class TextureMarginMap {
|
2022-01-31 13:56:26 +01:00
|
|
|
static const int directions[8][2];
|
|
|
|
|
static const int distances[8];
|
2022-01-17 18:00:35 +01:00
|
|
|
|
2022-02-02 13:15:43 +11:00
|
|
|
/** Maps UV-edges to their corresponding UV-edge. */
|
2022-01-17 18:00:35 +01:00
|
|
|
Vector<int> loop_adjacency_map_;
|
2023-07-24 22:06:55 +02:00
|
|
|
/** Maps UV-edges to their corresponding face. */
|
|
|
|
|
Array<int> loop_to_face_map_;
|
2022-01-17 18:00:35 +01:00
|
|
|
|
|
|
|
|
int w_, h_;
|
2022-04-22 20:44:49 +02:00
|
|
|
float uv_offset_[2];
|
2022-01-17 18:00:35 +01:00
|
|
|
Vector<uint32_t> pixel_data_;
|
|
|
|
|
ZSpan zspan_;
|
|
|
|
|
uint32_t value_to_store_;
|
2023-07-05 14:20:28 +02:00
|
|
|
bool write_mask_;
|
2022-01-17 18:00:35 +01:00
|
|
|
char *mask_;
|
|
|
|
|
|
2023-07-24 22:06:55 +02:00
|
|
|
OffsetIndices<int> faces_;
|
Mesh: Replace MLoop struct with generic attributes
Implements #102359.
Split the `MLoop` struct into two separate integer arrays called
`corner_verts` and `corner_edges`, referring to the vertex each corner
is attached to and the next edge around the face at each corner. These
arrays can be sliced to give access to the edges or vertices in a face.
Then they are often referred to as "poly_verts" or "poly_edges".
The main benefits are halving the necessary memory bandwidth when only
one array is used and simplifications from using regular integer indices
instead of a special-purpose struct.
The commit also starts a renaming from "loop" to "corner" in mesh code.
Like the other mesh struct of array refactors, forward compatibility is
kept by writing files with the older format. This will be done until 4.0
to ease the transition process.
Looking at a small portion of the patch should give a good impression
for the rest of the changes. I tried to make the changes as small as
possible so it's easy to tell the correctness from the diff. Though I
found Blender developers have been very inventive over the last decade
when finding different ways to loop over the corners in a face.
For performance, nearly every piece of code that deals with `Mesh` is
slightly impacted. Any algorithm that is memory bottle-necked should
see an improvement. For example, here is a comparison of interpolating
a vertex float attribute to face corners (Ryzen 3700x):
**Before** (Average: 3.7 ms, Min: 3.4 ms)
```
threading::parallel_for(loops.index_range(), 4096, [&](IndexRange range) {
for (const int64_t i : range) {
dst[i] = src[loops[i].v];
}
});
```
**After** (Average: 2.9 ms, Min: 2.6 ms)
```
array_utils::gather(src, corner_verts, dst);
```
That's an improvement of 28% to the average timings, and it's also a
simplification, since an index-based routine can be used instead.
For more examples using the new arrays, see the design task.
Pull Request: https://projects.blender.org/blender/blender/pulls/104424
2023-03-20 15:55:13 +01:00
|
|
|
Span<int> corner_edges_;
|
2023-03-19 11:33:04 -04:00
|
|
|
Span<float2> mloopuv_;
|
2022-01-17 18:00:35 +01:00
|
|
|
int totedge_;
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
TextureMarginMap(size_t w,
|
|
|
|
|
size_t h,
|
2022-04-22 20:44:49 +02:00
|
|
|
const float uv_offset[2],
|
2023-03-19 11:33:04 -04:00
|
|
|
const int totedge,
|
2023-07-24 22:06:55 +02:00
|
|
|
const OffsetIndices<int> faces,
|
Mesh: Replace MLoop struct with generic attributes
Implements #102359.
Split the `MLoop` struct into two separate integer arrays called
`corner_verts` and `corner_edges`, referring to the vertex each corner
is attached to and the next edge around the face at each corner. These
arrays can be sliced to give access to the edges or vertices in a face.
Then they are often referred to as "poly_verts" or "poly_edges".
The main benefits are halving the necessary memory bandwidth when only
one array is used and simplifications from using regular integer indices
instead of a special-purpose struct.
The commit also starts a renaming from "loop" to "corner" in mesh code.
Like the other mesh struct of array refactors, forward compatibility is
kept by writing files with the older format. This will be done until 4.0
to ease the transition process.
Looking at a small portion of the patch should give a good impression
for the rest of the changes. I tried to make the changes as small as
possible so it's easy to tell the correctness from the diff. Though I
found Blender developers have been very inventive over the last decade
when finding different ways to loop over the corners in a face.
For performance, nearly every piece of code that deals with `Mesh` is
slightly impacted. Any algorithm that is memory bottle-necked should
see an improvement. For example, here is a comparison of interpolating
a vertex float attribute to face corners (Ryzen 3700x):
**Before** (Average: 3.7 ms, Min: 3.4 ms)
```
threading::parallel_for(loops.index_range(), 4096, [&](IndexRange range) {
for (const int64_t i : range) {
dst[i] = src[loops[i].v];
}
});
```
**After** (Average: 2.9 ms, Min: 2.6 ms)
```
array_utils::gather(src, corner_verts, dst);
```
That's an improvement of 28% to the average timings, and it's also a
simplification, since an index-based routine can be used instead.
For more examples using the new arrays, see the design task.
Pull Request: https://projects.blender.org/blender/blender/pulls/104424
2023-03-20 15:55:13 +01:00
|
|
|
const Span<int> corner_edges,
|
2023-03-19 11:33:04 -04:00
|
|
|
const Span<float2> mloopuv)
|
Mesh: Replace MLoop struct with generic attributes
Implements #102359.
Split the `MLoop` struct into two separate integer arrays called
`corner_verts` and `corner_edges`, referring to the vertex each corner
is attached to and the next edge around the face at each corner. These
arrays can be sliced to give access to the edges or vertices in a face.
Then they are often referred to as "poly_verts" or "poly_edges".
The main benefits are halving the necessary memory bandwidth when only
one array is used and simplifications from using regular integer indices
instead of a special-purpose struct.
The commit also starts a renaming from "loop" to "corner" in mesh code.
Like the other mesh struct of array refactors, forward compatibility is
kept by writing files with the older format. This will be done until 4.0
to ease the transition process.
Looking at a small portion of the patch should give a good impression
for the rest of the changes. I tried to make the changes as small as
possible so it's easy to tell the correctness from the diff. Though I
found Blender developers have been very inventive over the last decade
when finding different ways to loop over the corners in a face.
For performance, nearly every piece of code that deals with `Mesh` is
slightly impacted. Any algorithm that is memory bottle-necked should
see an improvement. For example, here is a comparison of interpolating
a vertex float attribute to face corners (Ryzen 3700x):
**Before** (Average: 3.7 ms, Min: 3.4 ms)
```
threading::parallel_for(loops.index_range(), 4096, [&](IndexRange range) {
for (const int64_t i : range) {
dst[i] = src[loops[i].v];
}
});
```
**After** (Average: 2.9 ms, Min: 2.6 ms)
```
array_utils::gather(src, corner_verts, dst);
```
That's an improvement of 28% to the average timings, and it's also a
simplification, since an index-based routine can be used instead.
For more examples using the new arrays, see the design task.
Pull Request: https://projects.blender.org/blender/blender/pulls/104424
2023-03-20 15:55:13 +01:00
|
|
|
: w_(w),
|
|
|
|
|
h_(h),
|
2023-07-24 22:06:55 +02:00
|
|
|
faces_(faces),
|
Mesh: Replace MLoop struct with generic attributes
Implements #102359.
Split the `MLoop` struct into two separate integer arrays called
`corner_verts` and `corner_edges`, referring to the vertex each corner
is attached to and the next edge around the face at each corner. These
arrays can be sliced to give access to the edges or vertices in a face.
Then they are often referred to as "poly_verts" or "poly_edges".
The main benefits are halving the necessary memory bandwidth when only
one array is used and simplifications from using regular integer indices
instead of a special-purpose struct.
The commit also starts a renaming from "loop" to "corner" in mesh code.
Like the other mesh struct of array refactors, forward compatibility is
kept by writing files with the older format. This will be done until 4.0
to ease the transition process.
Looking at a small portion of the patch should give a good impression
for the rest of the changes. I tried to make the changes as small as
possible so it's easy to tell the correctness from the diff. Though I
found Blender developers have been very inventive over the last decade
when finding different ways to loop over the corners in a face.
For performance, nearly every piece of code that deals with `Mesh` is
slightly impacted. Any algorithm that is memory bottle-necked should
see an improvement. For example, here is a comparison of interpolating
a vertex float attribute to face corners (Ryzen 3700x):
**Before** (Average: 3.7 ms, Min: 3.4 ms)
```
threading::parallel_for(loops.index_range(), 4096, [&](IndexRange range) {
for (const int64_t i : range) {
dst[i] = src[loops[i].v];
}
});
```
**After** (Average: 2.9 ms, Min: 2.6 ms)
```
array_utils::gather(src, corner_verts, dst);
```
That's an improvement of 28% to the average timings, and it's also a
simplification, since an index-based routine can be used instead.
For more examples using the new arrays, see the design task.
Pull Request: https://projects.blender.org/blender/blender/pulls/104424
2023-03-20 15:55:13 +01:00
|
|
|
corner_edges_(corner_edges),
|
|
|
|
|
mloopuv_(mloopuv),
|
|
|
|
|
totedge_(totedge)
|
2022-01-17 18:00:35 +01:00
|
|
|
{
|
2022-04-22 20:44:49 +02:00
|
|
|
copy_v2_v2(uv_offset_, uv_offset);
|
|
|
|
|
|
2022-01-17 18:00:35 +01:00
|
|
|
pixel_data_.resize(w_ * h_, 0xFFFFFFFF);
|
|
|
|
|
|
|
|
|
|
zbuf_alloc_span(&zspan_, w_, h_);
|
|
|
|
|
|
|
|
|
|
build_tables();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
~TextureMarginMap()
|
|
|
|
|
{
|
|
|
|
|
zbuf_free_span(&zspan_);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
inline void set_pixel(int x, int y, uint32_t value)
|
|
|
|
|
{
|
|
|
|
|
BLI_assert(x < w_);
|
|
|
|
|
BLI_assert(x >= 0);
|
|
|
|
|
pixel_data_[y * w_ + x] = value;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
inline uint32_t get_pixel(int x, int y) const
|
|
|
|
|
{
|
|
|
|
|
if (x < 0 || y < 0 || x >= w_ || y >= h_) {
|
|
|
|
|
return 0xFFFFFFFF;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return pixel_data_[y * w_ + x];
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-05 14:20:28 +02:00
|
|
|
void rasterize_tri(float *v1, float *v2, float *v3, uint32_t value, char *mask, bool writemask)
|
2022-01-17 18:00:35 +01:00
|
|
|
{
|
|
|
|
|
/* NOTE: This is not thread safe, because the value to be written by the rasterizer is
|
2022-02-04 14:52:52 -06:00
|
|
|
* a class member. If this is ever made multi-threaded each thread needs to get its own. */
|
2022-01-17 18:00:35 +01:00
|
|
|
value_to_store_ = value;
|
|
|
|
|
mask_ = mask;
|
2023-07-05 14:20:28 +02:00
|
|
|
write_mask_ = writemask;
|
2022-01-17 18:00:35 +01:00
|
|
|
zspan_scanconvert(
|
|
|
|
|
&zspan_, this, &(v1[0]), &(v2[0]), &(v3[0]), TextureMarginMap::zscan_store_pixel);
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-27 10:53:34 -06:00
|
|
|
static void zscan_store_pixel(
|
|
|
|
|
void *map, int x, int y, [[maybe_unused]] float u, [[maybe_unused]] float v)
|
2022-01-17 18:00:35 +01:00
|
|
|
{
|
2022-02-02 13:15:43 +11:00
|
|
|
/* NOTE: Not thread safe, see comment above. */
|
2022-01-17 18:00:35 +01:00
|
|
|
TextureMarginMap *m = static_cast<TextureMarginMap *>(map);
|
|
|
|
|
if (m->mask_) {
|
2023-07-05 14:20:28 +02:00
|
|
|
if (m->write_mask_) {
|
2023-07-05 19:42:52 +02:00
|
|
|
/* if there is a mask and write_mask_ is true, write to the mask */
|
2023-07-05 14:20:28 +02:00
|
|
|
m->mask_[y * m->w_ + x] = 1;
|
|
|
|
|
m->set_pixel(x, y, m->value_to_store_);
|
2023-07-05 19:42:52 +02:00
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
/* if there is a mask and write_mask_ is false, read the mask
|
|
|
|
|
* to decide if the map needs to be written
|
|
|
|
|
*/
|
|
|
|
|
if (m->mask_[y * m->w_ + x] != 0) {
|
2023-07-05 14:20:28 +02:00
|
|
|
m->set_pixel(x, y, m->value_to_store_);
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-07-05 19:42:52 +02:00
|
|
|
}
|
|
|
|
|
else {
|
2023-07-05 14:20:28 +02:00
|
|
|
m->set_pixel(x, y, m->value_to_store_);
|
2022-01-17 18:00:35 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-24 22:06:55 +02:00
|
|
|
/* The map contains 2 kinds of pixels: DijkstraPixels and face indices. The top bit determines
|
2022-01-31 13:56:26 +01:00
|
|
|
* what kind it is. With the top bit set, it is a 'dijkstra' pixel. The bottom 4 bits encode the
|
|
|
|
|
* direction of the shortest path and the remaining 27 bits are used to store the distance. If
|
2023-10-19 18:53:16 +11:00
|
|
|
* the top bit is not set, the rest of the bits is used to store the face index.
|
2022-01-17 18:00:35 +01:00
|
|
|
*/
|
2022-01-31 13:56:26 +01:00
|
|
|
#define PackDijkstraPixel(dist, dir) (0x80000000 + ((dist) << 4) + (dir))
|
|
|
|
|
#define DijkstraPixelGetDistance(dp) (((dp) ^ 0x80000000) >> 4)
|
|
|
|
|
#define DijkstraPixelGetDirection(dp) ((dp)&0xF)
|
2022-01-17 18:00:35 +01:00
|
|
|
#define IsDijkstraPixel(dp) ((dp)&0x80000000)
|
|
|
|
|
#define DijkstraPixelIsUnset(dp) ((dp) == 0xFFFFFFFF)
|
|
|
|
|
|
2022-02-02 13:15:43 +11:00
|
|
|
/**
|
|
|
|
|
* Use dijkstra's algorithm to 'grow' a border around the polygons marked in the map.
|
2023-07-24 22:06:55 +02:00
|
|
|
* For each pixel mark which direction is the shortest way to a face.
|
2022-01-17 18:00:35 +01:00
|
|
|
*/
|
|
|
|
|
void grow_dijkstra(int margin)
|
|
|
|
|
{
|
|
|
|
|
class DijkstraActivePixel {
|
|
|
|
|
public:
|
2023-03-29 16:50:54 +02:00
|
|
|
DijkstraActivePixel(int dist, int _x, int _y) : distance(dist), x(_x), y(_y) {}
|
2022-01-17 18:00:35 +01:00
|
|
|
int distance;
|
|
|
|
|
int x, y;
|
|
|
|
|
};
|
|
|
|
|
auto cmp_dijkstrapixel_fun = [](DijkstraActivePixel const &a1, DijkstraActivePixel const &a2) {
|
|
|
|
|
return a1.distance > a2.distance;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
Vector<DijkstraActivePixel> active_pixels;
|
|
|
|
|
for (int y = 0; y < h_; y++) {
|
|
|
|
|
for (int x = 0; x < w_; x++) {
|
|
|
|
|
if (DijkstraPixelIsUnset(get_pixel(x, y))) {
|
2022-01-31 13:56:26 +01:00
|
|
|
for (int i = 0; i < 8; i++) {
|
2022-01-17 18:00:35 +01:00
|
|
|
int xx = x - directions[i][0];
|
|
|
|
|
int yy = y - directions[i][1];
|
|
|
|
|
|
|
|
|
|
if (xx >= 0 && xx < w_ && yy >= 0 && yy < w_ && !IsDijkstraPixel(get_pixel(xx, yy))) {
|
2022-01-31 13:56:26 +01:00
|
|
|
set_pixel(x, y, PackDijkstraPixel(distances[i], i));
|
|
|
|
|
active_pixels.append(DijkstraActivePixel(distances[i], x, y));
|
2022-01-17 18:00:35 +01:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-02 13:15:43 +11:00
|
|
|
/* Not strictly needed because at this point it already is a heap. */
|
|
|
|
|
#if 0
|
|
|
|
|
std::make_heap(active_pixels.begin(), active_pixels.end(), cmp_dijkstrapixel_fun);
|
|
|
|
|
#endif
|
2022-01-17 18:00:35 +01:00
|
|
|
|
|
|
|
|
while (active_pixels.size()) {
|
|
|
|
|
std::pop_heap(active_pixels.begin(), active_pixels.end(), cmp_dijkstrapixel_fun);
|
|
|
|
|
DijkstraActivePixel p = active_pixels.pop_last();
|
|
|
|
|
|
|
|
|
|
int dist = p.distance;
|
|
|
|
|
|
2022-01-31 13:56:26 +01:00
|
|
|
if (dist < 2 * (margin + 1)) {
|
|
|
|
|
for (int i = 0; i < 8; i++) {
|
2022-01-17 18:00:35 +01:00
|
|
|
int x = p.x + directions[i][0];
|
|
|
|
|
int y = p.y + directions[i][1];
|
|
|
|
|
if (x >= 0 && x < w_ && y >= 0 && y < h_) {
|
|
|
|
|
uint32_t dp = get_pixel(x, y);
|
2022-01-31 13:56:26 +01:00
|
|
|
if (IsDijkstraPixel(dp) && (DijkstraPixelGetDistance(dp) > dist + distances[i])) {
|
|
|
|
|
BLI_assert(DijkstraPixelGetDirection(dp) != i);
|
|
|
|
|
set_pixel(x, y, PackDijkstraPixel(dist + distances[i], i));
|
|
|
|
|
active_pixels.append(DijkstraActivePixel(dist + distances[i], x, y));
|
2022-01-17 18:00:35 +01:00
|
|
|
std::push_heap(active_pixels.begin(), active_pixels.end(), cmp_dijkstrapixel_fun);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-02 13:15:43 +11:00
|
|
|
/**
|
|
|
|
|
* Walk over the map and for margin pixels follow the direction stored in the bottom 3
|
2023-07-24 22:06:55 +02:00
|
|
|
* bits back to the face.
|
|
|
|
|
* Then look up the pixel from the next face.
|
2022-01-17 18:00:35 +01:00
|
|
|
*/
|
|
|
|
|
void lookup_pixels(ImBuf *ibuf, char *mask, int maxPolygonSteps)
|
|
|
|
|
{
|
|
|
|
|
for (int y = 0; y < h_; y++) {
|
|
|
|
|
for (int x = 0; x < w_; x++) {
|
|
|
|
|
uint32_t dp = get_pixel(x, y);
|
|
|
|
|
if (IsDijkstraPixel(dp) && !DijkstraPixelIsUnset(dp)) {
|
|
|
|
|
int dist = DijkstraPixelGetDistance(dp);
|
|
|
|
|
int direction = DijkstraPixelGetDirection(dp);
|
|
|
|
|
|
|
|
|
|
int xx = x;
|
|
|
|
|
int yy = y;
|
|
|
|
|
|
2023-07-24 22:06:55 +02:00
|
|
|
/* Follow the dijkstra directions to find the face this margin pixels belongs to. */
|
2022-01-17 18:00:35 +01:00
|
|
|
while (dist > 0) {
|
|
|
|
|
xx -= directions[direction][0];
|
|
|
|
|
yy -= directions[direction][1];
|
|
|
|
|
dp = get_pixel(xx, yy);
|
2022-01-31 13:56:26 +01:00
|
|
|
dist -= distances[direction];
|
2022-01-17 18:00:35 +01:00
|
|
|
BLI_assert(!dist || (dist == DijkstraPixelGetDistance(dp)));
|
|
|
|
|
direction = DijkstraPixelGetDirection(dp);
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-24 22:06:55 +02:00
|
|
|
uint32_t face = get_pixel(xx, yy);
|
2022-01-17 18:00:35 +01:00
|
|
|
|
2023-07-24 22:06:55 +02:00
|
|
|
BLI_assert(!IsDijkstraPixel(face));
|
2022-01-17 18:00:35 +01:00
|
|
|
|
|
|
|
|
float destX, destY;
|
|
|
|
|
|
|
|
|
|
int other_poly;
|
|
|
|
|
bool found_pixel_in_polygon = false;
|
2023-07-24 22:06:55 +02:00
|
|
|
if (lookup_pixel_polygon_neighbourhood(x, y, &face, &destX, &destY, &other_poly)) {
|
2022-01-17 18:00:35 +01:00
|
|
|
|
|
|
|
|
for (int i = 0; i < maxPolygonSteps; i++) {
|
|
|
|
|
/* Force to pixel grid. */
|
2022-09-25 18:33:28 +10:00
|
|
|
int nx = int(round(destX));
|
|
|
|
|
int ny = int(round(destY));
|
2022-01-17 18:00:35 +01:00
|
|
|
uint32_t polygon_from_map = get_pixel(nx, ny);
|
|
|
|
|
if (other_poly == polygon_from_map) {
|
|
|
|
|
found_pixel_in_polygon = true;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-31 13:56:26 +01:00
|
|
|
float dist_to_edge;
|
2023-07-24 22:06:55 +02:00
|
|
|
/* Look up again, but starting from the face we were expected to land in. */
|
2022-01-31 13:56:26 +01:00
|
|
|
if (!lookup_pixel(nx, ny, other_poly, &destX, &destY, &other_poly, &dist_to_edge)) {
|
|
|
|
|
found_pixel_in_polygon = false;
|
|
|
|
|
break;
|
|
|
|
|
}
|
2022-01-17 18:00:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (found_pixel_in_polygon) {
|
|
|
|
|
bilinear_interpolation(ibuf, ibuf, destX, destY, x, y);
|
|
|
|
|
/* Add our new pixels to the assigned pixel map. */
|
|
|
|
|
mask[y * w_ + x] = 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else if (DijkstraPixelIsUnset(dp) || !IsDijkstraPixel(dp)) {
|
|
|
|
|
/* These are not margin pixels, make sure the extend filter which is run after this step
|
|
|
|
|
* leaves them alone.
|
|
|
|
|
*/
|
|
|
|
|
mask[y * w_ + x] = 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private:
|
Mesh: Move UV layers to generic attributes
Currently the `MLoopUV` struct stores UV coordinates and flags related
to editing UV maps in the UV editor. This patch changes the coordinates
to use the generic 2D vector type, and moves the flags into three
separate boolean attributes. This follows the design in T95965, with
the ultimate intention of simplifying code and improving performance.
Importantly, the change allows exporters and renderers to use UVs
"touched" by geometry nodes, which only creates generic attributes.
It also allows geometry nodes to create "proper" UV maps from scratch,
though only with the Store Named Attribute node for now.
The new design considers any 2D vector attribute on the corner domain
to be a UV map. In the future, they might be distinguished from regular
2D vectors with attribute metadata, which may be helpful because they
are often interpolated differently.
Most of the code changes deal with passing around UV BMesh custom data
offsets and tracking the boolean "sublayers". The boolean layers are
use the following prefixes for attribute names: vert selection: `.vs.`,
edge selection: `.es.`, pinning: `.pn.`. Currently these are short to
avoid using up the maximum length of attribute names. To accommodate
for these 4 extra characters, the name length limit is enlarged to 68
bytes, while the maximum user settable name length is still 64 bytes.
Unfortunately Python/RNA API access to the UV flag data becomes slower.
Accessing the boolean layers directly is be better for performance in
general.
Like the other mesh SoA refactors, backward and forward compatibility
aren't affected, and won't be changed until 4.0. We pay for that by
making mesh reading and writing more expensive with conversions.
Resolves T85962
Differential Revision: https://developer.blender.org/D14365
2023-01-10 00:47:04 -05:00
|
|
|
float2 uv_to_xy(const float2 &mloopuv) const
|
2022-01-17 18:00:35 +01:00
|
|
|
{
|
|
|
|
|
float2 ret;
|
Mesh: Move UV layers to generic attributes
Currently the `MLoopUV` struct stores UV coordinates and flags related
to editing UV maps in the UV editor. This patch changes the coordinates
to use the generic 2D vector type, and moves the flags into three
separate boolean attributes. This follows the design in T95965, with
the ultimate intention of simplifying code and improving performance.
Importantly, the change allows exporters and renderers to use UVs
"touched" by geometry nodes, which only creates generic attributes.
It also allows geometry nodes to create "proper" UV maps from scratch,
though only with the Store Named Attribute node for now.
The new design considers any 2D vector attribute on the corner domain
to be a UV map. In the future, they might be distinguished from regular
2D vectors with attribute metadata, which may be helpful because they
are often interpolated differently.
Most of the code changes deal with passing around UV BMesh custom data
offsets and tracking the boolean "sublayers". The boolean layers are
use the following prefixes for attribute names: vert selection: `.vs.`,
edge selection: `.es.`, pinning: `.pn.`. Currently these are short to
avoid using up the maximum length of attribute names. To accommodate
for these 4 extra characters, the name length limit is enlarged to 68
bytes, while the maximum user settable name length is still 64 bytes.
Unfortunately Python/RNA API access to the UV flag data becomes slower.
Accessing the boolean layers directly is be better for performance in
general.
Like the other mesh SoA refactors, backward and forward compatibility
aren't affected, and won't be changed until 4.0. We pay for that by
making mesh reading and writing more expensive with conversions.
Resolves T85962
Differential Revision: https://developer.blender.org/D14365
2023-01-10 00:47:04 -05:00
|
|
|
ret.x = (((mloopuv[0] - uv_offset_[0]) * w_) - (0.5f + 0.001f));
|
|
|
|
|
ret.y = (((mloopuv[1] - uv_offset_[1]) * h_) - (0.5f + 0.001f));
|
2022-01-17 18:00:35 +01:00
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void build_tables()
|
|
|
|
|
{
|
2023-07-24 22:06:55 +02:00
|
|
|
loop_to_face_map_ = blender::bke::mesh::build_loop_to_face_map(faces_);
|
2022-01-17 18:00:35 +01:00
|
|
|
|
Mesh: Replace MLoop struct with generic attributes
Implements #102359.
Split the `MLoop` struct into two separate integer arrays called
`corner_verts` and `corner_edges`, referring to the vertex each corner
is attached to and the next edge around the face at each corner. These
arrays can be sliced to give access to the edges or vertices in a face.
Then they are often referred to as "poly_verts" or "poly_edges".
The main benefits are halving the necessary memory bandwidth when only
one array is used and simplifications from using regular integer indices
instead of a special-purpose struct.
The commit also starts a renaming from "loop" to "corner" in mesh code.
Like the other mesh struct of array refactors, forward compatibility is
kept by writing files with the older format. This will be done until 4.0
to ease the transition process.
Looking at a small portion of the patch should give a good impression
for the rest of the changes. I tried to make the changes as small as
possible so it's easy to tell the correctness from the diff. Though I
found Blender developers have been very inventive over the last decade
when finding different ways to loop over the corners in a face.
For performance, nearly every piece of code that deals with `Mesh` is
slightly impacted. Any algorithm that is memory bottle-necked should
see an improvement. For example, here is a comparison of interpolating
a vertex float attribute to face corners (Ryzen 3700x):
**Before** (Average: 3.7 ms, Min: 3.4 ms)
```
threading::parallel_for(loops.index_range(), 4096, [&](IndexRange range) {
for (const int64_t i : range) {
dst[i] = src[loops[i].v];
}
});
```
**After** (Average: 2.9 ms, Min: 2.6 ms)
```
array_utils::gather(src, corner_verts, dst);
```
That's an improvement of 28% to the average timings, and it's also a
simplification, since an index-based routine can be used instead.
For more examples using the new arrays, see the design task.
Pull Request: https://projects.blender.org/blender/blender/pulls/104424
2023-03-20 15:55:13 +01:00
|
|
|
loop_adjacency_map_.resize(corner_edges_.size(), -1);
|
2022-01-17 18:00:35 +01:00
|
|
|
|
|
|
|
|
Vector<int> tmpmap;
|
|
|
|
|
tmpmap.resize(totedge_, -1);
|
|
|
|
|
|
Mesh: Replace MLoop struct with generic attributes
Implements #102359.
Split the `MLoop` struct into two separate integer arrays called
`corner_verts` and `corner_edges`, referring to the vertex each corner
is attached to and the next edge around the face at each corner. These
arrays can be sliced to give access to the edges or vertices in a face.
Then they are often referred to as "poly_verts" or "poly_edges".
The main benefits are halving the necessary memory bandwidth when only
one array is used and simplifications from using regular integer indices
instead of a special-purpose struct.
The commit also starts a renaming from "loop" to "corner" in mesh code.
Like the other mesh struct of array refactors, forward compatibility is
kept by writing files with the older format. This will be done until 4.0
to ease the transition process.
Looking at a small portion of the patch should give a good impression
for the rest of the changes. I tried to make the changes as small as
possible so it's easy to tell the correctness from the diff. Though I
found Blender developers have been very inventive over the last decade
when finding different ways to loop over the corners in a face.
For performance, nearly every piece of code that deals with `Mesh` is
slightly impacted. Any algorithm that is memory bottle-necked should
see an improvement. For example, here is a comparison of interpolating
a vertex float attribute to face corners (Ryzen 3700x):
**Before** (Average: 3.7 ms, Min: 3.4 ms)
```
threading::parallel_for(loops.index_range(), 4096, [&](IndexRange range) {
for (const int64_t i : range) {
dst[i] = src[loops[i].v];
}
});
```
**After** (Average: 2.9 ms, Min: 2.6 ms)
```
array_utils::gather(src, corner_verts, dst);
```
That's an improvement of 28% to the average timings, and it's also a
simplification, since an index-based routine can be used instead.
For more examples using the new arrays, see the design task.
Pull Request: https://projects.blender.org/blender/blender/pulls/104424
2023-03-20 15:55:13 +01:00
|
|
|
for (const int64_t i : corner_edges_.index_range()) {
|
|
|
|
|
int edge = corner_edges_[i];
|
2022-01-17 18:00:35 +01:00
|
|
|
if (tmpmap[edge] == -1) {
|
|
|
|
|
loop_adjacency_map_[i] = -1;
|
|
|
|
|
tmpmap[edge] = i;
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
BLI_assert(tmpmap[edge] >= 0);
|
|
|
|
|
loop_adjacency_map_[i] = tmpmap[edge];
|
|
|
|
|
loop_adjacency_map_[tmpmap[edge]] = i;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-02 13:15:43 +11:00
|
|
|
/**
|
|
|
|
|
* Call lookup_pixel for the start_poly. If that fails, try the adjacent polygons as well.
|
2023-07-24 22:06:55 +02:00
|
|
|
* Because the Dijkstra is not very exact in determining which face is the closest, the
|
|
|
|
|
* face we need can be the one next to the one the Dijkstra map provides. To prevent missing
|
2022-02-02 13:15:43 +11:00
|
|
|
* pixels also check the neighboring polygons.
|
|
|
|
|
*/
|
2022-01-31 13:56:26 +01:00
|
|
|
bool lookup_pixel_polygon_neighbourhood(
|
|
|
|
|
float x, float y, uint32_t *r_start_poly, float *r_destx, float *r_desty, int *r_other_poly)
|
|
|
|
|
{
|
|
|
|
|
float found_dist;
|
|
|
|
|
if (lookup_pixel(x, y, *r_start_poly, r_destx, r_desty, r_other_poly, &found_dist)) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-24 22:06:55 +02:00
|
|
|
int loopstart = faces_[*r_start_poly].start();
|
|
|
|
|
int totloop = faces_[*r_start_poly].size();
|
2022-01-31 13:56:26 +01:00
|
|
|
|
|
|
|
|
float destx, desty;
|
|
|
|
|
int foundpoly;
|
|
|
|
|
|
2022-03-25 12:04:19 +11:00
|
|
|
float mindist = -1.0f;
|
2022-01-31 13:56:26 +01:00
|
|
|
|
2022-02-02 13:15:43 +11:00
|
|
|
/* Loop over all adjacent polygons and determine which edge is closest.
|
|
|
|
|
* This could be optimized by only inspecting neighbors which are on the edge of an island.
|
2022-01-31 13:56:26 +01:00
|
|
|
* But it seems fast enough for now and that would add a lot of complexity. */
|
|
|
|
|
for (int i = 0; i < totloop; i++) {
|
|
|
|
|
int otherloop = loop_adjacency_map_[i + loopstart];
|
2022-01-31 20:56:54 +01:00
|
|
|
|
|
|
|
|
if (otherloop < 0) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-24 22:06:55 +02:00
|
|
|
uint32_t face = loop_to_face_map_[otherloop];
|
2022-01-31 13:56:26 +01:00
|
|
|
|
2023-07-24 22:06:55 +02:00
|
|
|
if (lookup_pixel(x, y, face, &destx, &desty, &foundpoly, &found_dist)) {
|
2022-09-23 15:20:24 +10:00
|
|
|
if (mindist < 0.0f || found_dist < mindist) {
|
2022-01-31 13:56:26 +01:00
|
|
|
mindist = found_dist;
|
|
|
|
|
*r_other_poly = foundpoly;
|
|
|
|
|
*r_destx = destx;
|
|
|
|
|
*r_desty = desty;
|
2023-07-24 22:06:55 +02:00
|
|
|
*r_start_poly = face;
|
2022-01-31 13:56:26 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-25 12:04:19 +11:00
|
|
|
return mindist >= 0.0f;
|
2022-01-31 13:56:26 +01:00
|
|
|
}
|
|
|
|
|
|
2022-02-02 13:15:43 +11:00
|
|
|
/**
|
2023-07-24 22:06:55 +02:00
|
|
|
* Find which edge of the src_poly is closest to x,y. Look up its adjacent UV-edge and face.
|
|
|
|
|
* Then return the location of the equivalent pixel in the other face.
|
2022-01-17 18:00:35 +01:00
|
|
|
* Returns true if a new pixel location was found, false if it wasn't, which can happen if the
|
2023-07-24 22:06:55 +02:00
|
|
|
* margin pixel is on a corner, or the UV-edge doesn't have an adjacent face.
|
2022-02-02 13:15:43 +11:00
|
|
|
*/
|
2022-01-31 13:56:26 +01:00
|
|
|
bool lookup_pixel(float x,
|
|
|
|
|
float y,
|
|
|
|
|
int src_poly,
|
|
|
|
|
float *r_destx,
|
|
|
|
|
float *r_desty,
|
|
|
|
|
int *r_other_poly,
|
|
|
|
|
float *r_dist_to_edge)
|
2022-01-17 18:00:35 +01:00
|
|
|
{
|
|
|
|
|
float2 point(x, y);
|
|
|
|
|
|
|
|
|
|
*r_destx = *r_desty = 0;
|
|
|
|
|
|
|
|
|
|
int found_edge = -1;
|
|
|
|
|
float found_dist = -1;
|
|
|
|
|
float found_t = 0;
|
|
|
|
|
|
|
|
|
|
/* Find the closest edge on which the point x,y can be projected.
|
|
|
|
|
*/
|
2023-07-24 22:06:55 +02:00
|
|
|
for (size_t i = 0; i < faces_[src_poly].size(); i++) {
|
|
|
|
|
int l1 = faces_[src_poly].start() + i;
|
2022-01-17 18:00:35 +01:00
|
|
|
int l2 = l1 + 1;
|
2023-07-24 22:06:55 +02:00
|
|
|
if (l2 >= faces_[src_poly].start() + faces_[src_poly].size()) {
|
|
|
|
|
l2 = faces_[src_poly].start();
|
2022-01-17 18:00:35 +01:00
|
|
|
}
|
|
|
|
|
/* edge points */
|
|
|
|
|
float2 edgepoint1 = uv_to_xy(mloopuv_[l1]);
|
|
|
|
|
float2 edgepoint2 = uv_to_xy(mloopuv_[l2]);
|
|
|
|
|
/* Vector AB is the vector from the first edge point to the second edge point.
|
|
|
|
|
* Vector AP is the vector from the first edge point to our point under investigation. */
|
|
|
|
|
float2 ab = edgepoint2 - edgepoint1;
|
|
|
|
|
float2 ap = point - edgepoint1;
|
|
|
|
|
|
|
|
|
|
/* Project ap onto ab. */
|
|
|
|
|
float dotv = math::dot(ab, ap);
|
|
|
|
|
|
|
|
|
|
float ablensq = math::length_squared(ab);
|
|
|
|
|
|
|
|
|
|
float t = dotv / ablensq;
|
|
|
|
|
|
|
|
|
|
if (t >= 0.0 && t <= 1.0) {
|
|
|
|
|
|
|
|
|
|
/* Find the point on the edge closest to P */
|
|
|
|
|
float2 reflect_point = edgepoint1 + (t * ab);
|
|
|
|
|
/* This is the vector to P, so 90 degrees out from the edge. */
|
|
|
|
|
float2 reflect_vec = reflect_point - point;
|
|
|
|
|
|
|
|
|
|
float reflectLen = sqrt(reflect_vec[0] * reflect_vec[0] + reflect_vec[1] * reflect_vec[1]);
|
|
|
|
|
float cross = ab[0] * reflect_vec[1] - ab[1] * reflect_vec[0];
|
|
|
|
|
/* Only if P is on the outside of the edge, which means the cross product is positive,
|
|
|
|
|
* we consider this edge.
|
|
|
|
|
*/
|
|
|
|
|
bool valid = (cross > 0.0);
|
|
|
|
|
|
|
|
|
|
if (valid && (found_dist < 0 || reflectLen < found_dist)) {
|
|
|
|
|
/* Stother_ab the info of the closest edge so far. */
|
|
|
|
|
found_dist = reflectLen;
|
|
|
|
|
found_t = t;
|
2023-07-24 22:06:55 +02:00
|
|
|
found_edge = i + faces_[src_poly].start();
|
2022-01-17 18:00:35 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (found_edge < 0) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-31 13:56:26 +01:00
|
|
|
*r_dist_to_edge = found_dist;
|
|
|
|
|
|
2023-07-24 22:06:55 +02:00
|
|
|
/* Get the 'other' edge. I.E. the UV edge from the neighbor face. */
|
2022-01-17 18:00:35 +01:00
|
|
|
int other_edge = loop_adjacency_map_[found_edge];
|
|
|
|
|
|
|
|
|
|
if (other_edge < 0) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-24 22:06:55 +02:00
|
|
|
int dst_poly = loop_to_face_map_[other_edge];
|
2022-01-17 18:00:35 +01:00
|
|
|
|
|
|
|
|
if (r_other_poly) {
|
|
|
|
|
*r_other_poly = dst_poly;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int other_edge2 = other_edge + 1;
|
2023-07-24 22:06:55 +02:00
|
|
|
if (other_edge2 >= faces_[dst_poly].start() + faces_[dst_poly].size()) {
|
|
|
|
|
other_edge2 = faces_[dst_poly].start();
|
2022-01-17 18:00:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
float2 other_edgepoint1 = uv_to_xy(mloopuv_[other_edge]);
|
|
|
|
|
float2 other_edgepoint2 = uv_to_xy(mloopuv_[other_edge2]);
|
|
|
|
|
|
2022-02-04 14:52:52 -06:00
|
|
|
/* Calculate the vector from the order edges last point to its first point. */
|
2022-01-17 18:00:35 +01:00
|
|
|
float2 other_ab = other_edgepoint1 - other_edgepoint2;
|
|
|
|
|
float2 other_reflect_point = other_edgepoint2 + (found_t * other_ab);
|
|
|
|
|
float2 perpendicular_other_ab;
|
|
|
|
|
perpendicular_other_ab.x = other_ab.y;
|
|
|
|
|
perpendicular_other_ab.y = -other_ab.x;
|
|
|
|
|
|
|
|
|
|
/* The new point is dound_dist distance from other_reflect_point at a 90 degree angle to
|
|
|
|
|
* other_ab */
|
|
|
|
|
float2 new_point = other_reflect_point + (found_dist / math::length(perpendicular_other_ab)) *
|
|
|
|
|
perpendicular_other_ab;
|
|
|
|
|
|
|
|
|
|
*r_destx = new_point.x;
|
|
|
|
|
*r_desty = new_point.y;
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}; // class TextureMarginMap
|
|
|
|
|
|
2022-01-31 13:56:26 +01:00
|
|
|
const int TextureMarginMap::directions[8][2] = {
|
|
|
|
|
{-1, 0}, {-1, -1}, {0, -1}, {1, -1}, {1, 0}, {1, 1}, {0, 1}, {-1, 1}};
|
|
|
|
|
const int TextureMarginMap::distances[8] = {2, 3, 2, 3, 2, 3, 2, 3};
|
2022-01-17 18:00:35 +01:00
|
|
|
|
|
|
|
|
static void generate_margin(ImBuf *ibuf,
|
|
|
|
|
char *mask,
|
|
|
|
|
const int margin,
|
2023-03-19 11:33:04 -04:00
|
|
|
const Span<float3> vert_positions,
|
|
|
|
|
const int edges_num,
|
2023-07-24 22:06:55 +02:00
|
|
|
const OffsetIndices<int> faces,
|
Mesh: Replace MLoop struct with generic attributes
Implements #102359.
Split the `MLoop` struct into two separate integer arrays called
`corner_verts` and `corner_edges`, referring to the vertex each corner
is attached to and the next edge around the face at each corner. These
arrays can be sliced to give access to the edges or vertices in a face.
Then they are often referred to as "poly_verts" or "poly_edges".
The main benefits are halving the necessary memory bandwidth when only
one array is used and simplifications from using regular integer indices
instead of a special-purpose struct.
The commit also starts a renaming from "loop" to "corner" in mesh code.
Like the other mesh struct of array refactors, forward compatibility is
kept by writing files with the older format. This will be done until 4.0
to ease the transition process.
Looking at a small portion of the patch should give a good impression
for the rest of the changes. I tried to make the changes as small as
possible so it's easy to tell the correctness from the diff. Though I
found Blender developers have been very inventive over the last decade
when finding different ways to loop over the corners in a face.
For performance, nearly every piece of code that deals with `Mesh` is
slightly impacted. Any algorithm that is memory bottle-necked should
see an improvement. For example, here is a comparison of interpolating
a vertex float attribute to face corners (Ryzen 3700x):
**Before** (Average: 3.7 ms, Min: 3.4 ms)
```
threading::parallel_for(loops.index_range(), 4096, [&](IndexRange range) {
for (const int64_t i : range) {
dst[i] = src[loops[i].v];
}
});
```
**After** (Average: 2.9 ms, Min: 2.6 ms)
```
array_utils::gather(src, corner_verts, dst);
```
That's an improvement of 28% to the average timings, and it's also a
simplification, since an index-based routine can be used instead.
For more examples using the new arrays, see the design task.
Pull Request: https://projects.blender.org/blender/blender/pulls/104424
2023-03-20 15:55:13 +01:00
|
|
|
const Span<int> corner_edges,
|
2023-04-19 12:40:50 -07:00
|
|
|
const Span<int> corner_verts,
|
2023-03-19 11:33:04 -04:00
|
|
|
const Span<float2> mloopuv,
|
2022-04-22 20:44:49 +02:00
|
|
|
const float uv_offset[2])
|
2022-01-17 18:00:35 +01:00
|
|
|
{
|
2023-07-24 22:06:55 +02:00
|
|
|
Array<MLoopTri> looptris(poly_to_tri_count(faces.size(), corner_edges.size()));
|
|
|
|
|
bke::mesh::looptris_calc(vert_positions, faces, corner_verts, looptris);
|
2022-01-17 18:00:35 +01:00
|
|
|
|
2023-07-24 22:06:55 +02:00
|
|
|
Array<int> looptri_faces(looptris.size());
|
|
|
|
|
bke::mesh::looptris_calc_face_indices(faces, looptri_faces);
|
2023-05-04 15:39:10 +02:00
|
|
|
|
2023-07-24 22:06:55 +02:00
|
|
|
TextureMarginMap map(ibuf->x, ibuf->y, uv_offset, edges_num, faces, corner_edges, mloopuv);
|
2022-01-17 18:00:35 +01:00
|
|
|
|
|
|
|
|
bool draw_new_mask = false;
|
2022-01-18 14:27:29 +11:00
|
|
|
/* Now the map contains 3 sorts of values: 0xFFFFFFFF for empty pixels, `0x80000000 + polyindex`
|
2023-07-24 22:06:55 +02:00
|
|
|
* for margin pixels, just `polyindex` for face pixels. */
|
2022-01-17 18:00:35 +01:00
|
|
|
if (mask) {
|
|
|
|
|
mask = (char *)MEM_dupallocN(mask);
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
mask = (char *)MEM_callocN(sizeof(char) * ibuf->x * ibuf->y, __func__);
|
|
|
|
|
draw_new_mask = true;
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-19 11:33:04 -04:00
|
|
|
for (const int i : looptris.index_range()) {
|
|
|
|
|
const MLoopTri *lt = &looptris[i];
|
2022-01-17 18:00:35 +01:00
|
|
|
float vec[3][2];
|
|
|
|
|
|
|
|
|
|
for (int a = 0; a < 3; a++) {
|
Mesh: Move UV layers to generic attributes
Currently the `MLoopUV` struct stores UV coordinates and flags related
to editing UV maps in the UV editor. This patch changes the coordinates
to use the generic 2D vector type, and moves the flags into three
separate boolean attributes. This follows the design in T95965, with
the ultimate intention of simplifying code and improving performance.
Importantly, the change allows exporters and renderers to use UVs
"touched" by geometry nodes, which only creates generic attributes.
It also allows geometry nodes to create "proper" UV maps from scratch,
though only with the Store Named Attribute node for now.
The new design considers any 2D vector attribute on the corner domain
to be a UV map. In the future, they might be distinguished from regular
2D vectors with attribute metadata, which may be helpful because they
are often interpolated differently.
Most of the code changes deal with passing around UV BMesh custom data
offsets and tracking the boolean "sublayers". The boolean layers are
use the following prefixes for attribute names: vert selection: `.vs.`,
edge selection: `.es.`, pinning: `.pn.`. Currently these are short to
avoid using up the maximum length of attribute names. To accommodate
for these 4 extra characters, the name length limit is enlarged to 68
bytes, while the maximum user settable name length is still 64 bytes.
Unfortunately Python/RNA API access to the UV flag data becomes slower.
Accessing the boolean layers directly is be better for performance in
general.
Like the other mesh SoA refactors, backward and forward compatibility
aren't affected, and won't be changed until 4.0. We pay for that by
making mesh reading and writing more expensive with conversions.
Resolves T85962
Differential Revision: https://developer.blender.org/D14365
2023-01-10 00:47:04 -05:00
|
|
|
const float *uv = mloopuv[lt->tri[a]];
|
2022-01-17 18:00:35 +01:00
|
|
|
|
2023-02-09 11:30:25 +11:00
|
|
|
/* NOTE(@ideasman42): workaround for pixel aligned UVs which are common and can screw up
|
2022-08-09 14:18:18 +10:00
|
|
|
* our intersection tests where a pixel gets in between 2 faces or the middle of a quad,
|
2022-01-17 18:00:35 +01:00
|
|
|
* camera aligned quads also have this problem but they are less common.
|
2023-02-12 14:37:16 +11:00
|
|
|
* Add a small offset to the UVs, fixes bug #18685. */
|
2022-09-25 18:33:28 +10:00
|
|
|
vec[a][0] = (uv[0] - uv_offset[0]) * float(ibuf->x) - (0.5f + 0.001f);
|
|
|
|
|
vec[a][1] = (uv[1] - uv_offset[1]) * float(ibuf->y) - (0.5f + 0.002f);
|
2022-01-17 18:00:35 +01:00
|
|
|
}
|
|
|
|
|
|
2022-02-02 13:15:43 +11:00
|
|
|
/* NOTE: we need the top bit for the dijkstra distance map. */
|
2023-07-24 22:06:55 +02:00
|
|
|
BLI_assert(looptri_faces[i] < 0x80000000);
|
2022-02-02 13:15:43 +11:00
|
|
|
|
2023-07-24 22:06:55 +02:00
|
|
|
map.rasterize_tri(vec[0], vec[1], vec[2], looptri_faces[i], mask, draw_new_mask);
|
2022-01-17 18:00:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
char *tmpmask = (char *)MEM_dupallocN(mask);
|
|
|
|
|
/* Extend (with averaging) by 2 pixels. Those will be overwritten, but it
|
2023-02-27 21:44:59 +11:00
|
|
|
* helps linear interpolations on the edges of polygons. */
|
2022-01-17 18:00:35 +01:00
|
|
|
IMB_filter_extend(ibuf, tmpmask, 2);
|
|
|
|
|
MEM_freeN(tmpmask);
|
|
|
|
|
|
|
|
|
|
map.grow_dijkstra(margin);
|
|
|
|
|
|
|
|
|
|
/* Looking further than 3 polygons away leads to so much cumulative rounding
|
2022-01-18 14:27:29 +11:00
|
|
|
* that it isn't worth it. So hard-code it to 3. */
|
2022-01-17 18:00:35 +01:00
|
|
|
map.lookup_pixels(ibuf, mask, 3);
|
|
|
|
|
|
|
|
|
|
/* Use the extend filter to fill in the missing pixels at the corners, not strictly correct, but
|
|
|
|
|
* the visual difference seems very minimal. This also catches pixels we missed because of very
|
|
|
|
|
* narrow polygons.
|
|
|
|
|
*/
|
|
|
|
|
IMB_filter_extend(ibuf, mask, margin);
|
|
|
|
|
|
|
|
|
|
MEM_freeN(mask);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // namespace blender::render::texturemargin
|
|
|
|
|
|
2022-04-22 20:44:49 +02:00
|
|
|
void RE_generate_texturemargin_adjacentfaces(ImBuf *ibuf,
|
|
|
|
|
char *mask,
|
|
|
|
|
const int margin,
|
2023-03-19 11:33:04 -04:00
|
|
|
const Mesh *mesh,
|
2022-04-22 20:44:49 +02:00
|
|
|
char const *uv_layer,
|
|
|
|
|
const float uv_offset[2])
|
2022-01-17 18:00:35 +01:00
|
|
|
{
|
2023-03-19 11:33:04 -04:00
|
|
|
const blender::float2 *mloopuv;
|
|
|
|
|
if ((uv_layer == nullptr) || (uv_layer[0] == '\0')) {
|
|
|
|
|
mloopuv = static_cast<const blender::float2 *>(
|
2023-07-25 21:15:52 +02:00
|
|
|
CustomData_get_layer(&mesh->loop_data, CD_PROP_FLOAT2));
|
2023-03-19 11:33:04 -04:00
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
mloopuv = static_cast<const blender::float2 *>(
|
2023-07-25 21:15:52 +02:00
|
|
|
CustomData_get_layer_named(&mesh->loop_data, CD_PROP_FLOAT2, uv_layer));
|
2023-03-19 11:33:04 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
blender::render::texturemargin::generate_margin(ibuf,
|
|
|
|
|
mask,
|
|
|
|
|
margin,
|
|
|
|
|
mesh->vert_positions(),
|
|
|
|
|
mesh->totedge,
|
2023-07-24 22:06:55 +02:00
|
|
|
mesh->faces(),
|
Mesh: Replace MLoop struct with generic attributes
Implements #102359.
Split the `MLoop` struct into two separate integer arrays called
`corner_verts` and `corner_edges`, referring to the vertex each corner
is attached to and the next edge around the face at each corner. These
arrays can be sliced to give access to the edges or vertices in a face.
Then they are often referred to as "poly_verts" or "poly_edges".
The main benefits are halving the necessary memory bandwidth when only
one array is used and simplifications from using regular integer indices
instead of a special-purpose struct.
The commit also starts a renaming from "loop" to "corner" in mesh code.
Like the other mesh struct of array refactors, forward compatibility is
kept by writing files with the older format. This will be done until 4.0
to ease the transition process.
Looking at a small portion of the patch should give a good impression
for the rest of the changes. I tried to make the changes as small as
possible so it's easy to tell the correctness from the diff. Though I
found Blender developers have been very inventive over the last decade
when finding different ways to loop over the corners in a face.
For performance, nearly every piece of code that deals with `Mesh` is
slightly impacted. Any algorithm that is memory bottle-necked should
see an improvement. For example, here is a comparison of interpolating
a vertex float attribute to face corners (Ryzen 3700x):
**Before** (Average: 3.7 ms, Min: 3.4 ms)
```
threading::parallel_for(loops.index_range(), 4096, [&](IndexRange range) {
for (const int64_t i : range) {
dst[i] = src[loops[i].v];
}
});
```
**After** (Average: 2.9 ms, Min: 2.6 ms)
```
array_utils::gather(src, corner_verts, dst);
```
That's an improvement of 28% to the average timings, and it's also a
simplification, since an index-based routine can be used instead.
For more examples using the new arrays, see the design task.
Pull Request: https://projects.blender.org/blender/blender/pulls/104424
2023-03-20 15:55:13 +01:00
|
|
|
mesh->corner_edges(),
|
2023-04-19 12:40:50 -07:00
|
|
|
mesh->corner_verts(),
|
2023-03-19 11:33:04 -04:00
|
|
|
{mloopuv, mesh->totloop},
|
|
|
|
|
uv_offset);
|
2022-01-17 18:00:35 +01:00
|
|
|
}
|
|
|
|
|
|
2022-04-22 20:44:49 +02:00
|
|
|
void RE_generate_texturemargin_adjacentfaces_dm(
|
|
|
|
|
ImBuf *ibuf, char *mask, const int margin, DerivedMesh *dm, const float uv_offset[2])
|
2022-01-17 18:00:35 +01:00
|
|
|
{
|
2023-03-19 11:33:04 -04:00
|
|
|
const blender::float2 *mloopuv = static_cast<const blender::float2 *>(
|
|
|
|
|
dm->getLoopDataArray(dm, CD_PROP_FLOAT2));
|
|
|
|
|
|
2022-04-22 20:44:49 +02:00
|
|
|
blender::render::texturemargin::generate_margin(
|
2023-03-19 11:33:04 -04:00
|
|
|
ibuf,
|
|
|
|
|
mask,
|
|
|
|
|
margin,
|
|
|
|
|
{reinterpret_cast<const blender::float3 *>(dm->getVertArray(dm)), dm->getNumVerts(dm)},
|
|
|
|
|
dm->getNumEdges(dm),
|
Mesh: Replace MPoly struct with offset indices
Implements #95967.
Currently the `MPoly` struct is 12 bytes, and stores the index of a
face's first corner and the number of corners/verts/edges. Polygons
and corners are always created in order by Blender, meaning each
face's corners will be after the previous face's corners. We can take
advantage of this fact and eliminate the redundancy in mesh face
storage by only storing a single integer corner offset for each face.
The size of the face is then encoded by the offset of the next face.
The size of a single integer is 4 bytes, so this reduces memory
usage by 3 times.
The same method is used for `CurvesGeometry`, so Blender already has
an abstraction to simplify using these offsets called `OffsetIndices`.
This class is used to easily retrieve a range of corner indices for
each face. This also gives the opportunity for sharing some logic with
curves.
Another benefit of the change is that the offsets and sizes stored in
`MPoly` can no longer disagree with each other. Storing faces in the
order of their corners can simplify some code too.
Face/polygon variables now use the `IndexRange` type, which comes with
quite a few utilities that can simplify code.
Some:
- The offset integer array has to be one longer than the face count to
avoid a branch for every face, which means the data is no longer part
of the mesh's `CustomData`.
- We lose the ability to "reference" an original mesh's offset array
until more reusable CoW from #104478 is committed. That will be added
in a separate commit.
- Since they aren't part of `CustomData`, poly offsets often have to be
copied manually.
- To simplify using `OffsetIndices` in many places, some functions and
structs in headers were moved to only compile in C++.
- All meshes created by Blender use the same order for faces and face
corners, but just in case, meshes with mismatched order are fixed by
versioning code.
- `MeshPolygon.totloop` is no longer editable in RNA. This API break is
necessary here unfortunately. It should be worth it in 3.6, since
that's the best way to allow loading meshes from 4.0, which is
important for an LTS version.
Pull Request: https://projects.blender.org/blender/blender/pulls/105938
2023-04-04 20:39:28 +02:00
|
|
|
blender::Span(dm->getPolyArray(dm), dm->getNumPolys(dm) + 1),
|
Mesh: Replace MLoop struct with generic attributes
Implements #102359.
Split the `MLoop` struct into two separate integer arrays called
`corner_verts` and `corner_edges`, referring to the vertex each corner
is attached to and the next edge around the face at each corner. These
arrays can be sliced to give access to the edges or vertices in a face.
Then they are often referred to as "poly_verts" or "poly_edges".
The main benefits are halving the necessary memory bandwidth when only
one array is used and simplifications from using regular integer indices
instead of a special-purpose struct.
The commit also starts a renaming from "loop" to "corner" in mesh code.
Like the other mesh struct of array refactors, forward compatibility is
kept by writing files with the older format. This will be done until 4.0
to ease the transition process.
Looking at a small portion of the patch should give a good impression
for the rest of the changes. I tried to make the changes as small as
possible so it's easy to tell the correctness from the diff. Though I
found Blender developers have been very inventive over the last decade
when finding different ways to loop over the corners in a face.
For performance, nearly every piece of code that deals with `Mesh` is
slightly impacted. Any algorithm that is memory bottle-necked should
see an improvement. For example, here is a comparison of interpolating
a vertex float attribute to face corners (Ryzen 3700x):
**Before** (Average: 3.7 ms, Min: 3.4 ms)
```
threading::parallel_for(loops.index_range(), 4096, [&](IndexRange range) {
for (const int64_t i : range) {
dst[i] = src[loops[i].v];
}
});
```
**After** (Average: 2.9 ms, Min: 2.6 ms)
```
array_utils::gather(src, corner_verts, dst);
```
That's an improvement of 28% to the average timings, and it's also a
simplification, since an index-based routine can be used instead.
For more examples using the new arrays, see the design task.
Pull Request: https://projects.blender.org/blender/blender/pulls/104424
2023-03-20 15:55:13 +01:00
|
|
|
{dm->getCornerEdgeArray(dm), dm->getNumLoops(dm)},
|
2023-04-19 12:40:50 -07:00
|
|
|
{dm->getCornerVertArray(dm), dm->getNumLoops(dm)},
|
2023-03-19 11:33:04 -04:00
|
|
|
{mloopuv, dm->getNumLoops(dm)},
|
|
|
|
|
uv_offset);
|
2022-01-17 18:00:35 +01:00
|
|
|
}
|