Blender  V3.3
Classes | Typedefs | Functions
multires_bake.c File Reference
#include <string.h>
#include "MEM_guardedalloc.h"
#include "DNA_mesh_types.h"
#include "DNA_meshdata_types.h"
#include "DNA_object_types.h"
#include "DNA_scene_types.h"
#include "BLI_listbase.h"
#include "BLI_math.h"
#include "BLI_threads.h"
#include "BKE_DerivedMesh.h"
#include "BKE_ccg.h"
#include "BKE_global.h"
#include "BKE_image.h"
#include "BKE_lib_id.h"
#include "BKE_material.h"
#include "BKE_mesh.h"
#include "BKE_mesh_tangent.h"
#include "BKE_modifier.h"
#include "BKE_multires.h"
#include "BKE_subsurf.h"
#include "DEG_depsgraph.h"
#include "RE_multires_bake.h"
#include "RE_pipeline.h"
#include "RE_texture.h"
#include "RE_texture_margin.h"
#include "IMB_imbuf.h"
#include "IMB_imbuf_types.h"

Go to the source code of this file.

Classes

struct  MultiresBakeResult
 
struct  MResolvePixelData
 
struct  MBakeRast
 
struct  MHeightBakeData
 
struct  MNormalBakeData
 
struct  BakeImBufuserData
 
struct  MultiresBakeQueue
 
struct  MultiresBakeThread
 

Typedefs

typedef void(* MPassKnownData) (DerivedMesh *lores_dm, DerivedMesh *hires_dm, void *thread_data, void *bake_data, ImBuf *ibuf, const int face_index, const int lvl, const float st[2], float tangmat[3][3], const int x, const int y)
 
typedef void *(* MInitBakeData) (MultiresBakeRender *bkr, ImBuf *ibuf)
 
typedef void(* MFreeBakeData) (void *bake_data)
 
typedef struct MultiresBakeResult MultiresBakeResult
 
typedef void(* MFlushPixel) (const MResolvePixelData *data, const int x, const int y)
 
typedef struct BakeImBufuserData BakeImBufuserData
 
typedef struct MultiresBakeQueue MultiresBakeQueue
 
typedef struct MultiresBakeThread MultiresBakeThread
 

Functions

static void multiresbake_get_normal (const MResolvePixelData *data, const int tri_num, const int vert_index, float r_normal[3])
 
static void init_bake_rast (MBakeRast *bake_rast, const ImBuf *ibuf, const MResolvePixelData *data, MFlushPixel flush_pixel, short *do_update)
 
static void flush_pixel (const MResolvePixelData *data, const int x, const int y)
 
static void set_rast_triangle (const MBakeRast *bake_rast, const int x, const int y)
 
static void rasterize_half (const MBakeRast *bake_rast, const float s0_s, const float t0_s, const float s1_s, const float t1_s, const float s0_l, const float t0_l, const float s1_l, const float t1_l, const int y0_in, const int y1_in, const int is_mid_right)
 
static void bake_rasterize (const MBakeRast *bake_rast, const float st0_in[2], const float st1_in[2], const float st2_in[2])
 
static int multiresbake_test_break (MultiresBakeRender *bkr)
 
static int multires_bake_queue_next_tri (MultiresBakeQueue *queue)
 
static voiddo_multires_bake_thread (void *data_v)
 
static void init_ccgdm_arrays (DerivedMesh *dm)
 
static void do_multires_bake (MultiresBakeRender *bkr, Image *ima, ImageTile *tile, ImBuf *ibuf, bool require_tangent, MPassKnownData passKnownData, MInitBakeData initBakeData, MFreeBakeData freeBakeData, MultiresBakeResult *result)
 
static void interp_bilinear_grid (CCGKey *key, CCGElem *grid, float crn_x, float crn_y, int mode, float res[3])
 
static void get_ccgdm_data (DerivedMesh *lodm, DerivedMesh *hidm, const int *index_mp_to_orig, const int lvl, const MLoopTri *lt, const float u, const float v, float co[3], float n[3])
 
static void interp_bilinear_mpoly (DerivedMesh *dm, MLoop *mloop, MPoly *mpoly, const float u, const float v, const int mode, float res[3])
 
static void interp_barycentric_mlooptri (DerivedMesh *dm, MLoop *mloop, const MLoopTri *lt, const float u, const float v, const int mode, float res[3])
 
static voidinit_heights_data (MultiresBakeRender *bkr, ImBuf *ibuf)
 
static void free_heights_data (void *bake_data)
 
static void apply_heights_callback (DerivedMesh *lores_dm, DerivedMesh *hires_dm, void *thread_data_v, void *bake_data, ImBuf *ibuf, const int tri_index, const int lvl, const float st[2], float UNUSED(tangmat[3][3]), const int x, const int y)
 
static voidinit_normal_data (MultiresBakeRender *bkr, ImBuf *UNUSED(ibuf))
 
static void free_normal_data (void *bake_data)
 
static void apply_tangmat_callback (DerivedMesh *lores_dm, DerivedMesh *hires_dm, void *UNUSED(thread_data), void *bake_data, ImBuf *ibuf, const int tri_index, const int lvl, const float st[2], float tangmat[3][3], const int x, const int y)
 
static void bake_ibuf_filter (ImBuf *ibuf, char *mask, const int margin, const char margin_type, DerivedMesh *dm, const float uv_offset[2])
 
static void bake_ibuf_normalize_displacement (ImBuf *ibuf, const float *displacement, const char *mask, float displacement_min, float displacement_max)
 
static void count_images (MultiresBakeRender *bkr)
 
static void bake_images (MultiresBakeRender *bkr, MultiresBakeResult *result)
 
static void finish_images (MultiresBakeRender *bkr, MultiresBakeResult *result)
 
void RE_multires_bake_images (MultiresBakeRender *bkr)
 

Typedef Documentation

◆ BakeImBufuserData

◆ MFlushPixel

typedef void(* MFlushPixel) (const MResolvePixelData *data, const int x, const int y)

Definition at line 84 of file multires_bake.c.

◆ MFreeBakeData

typedef void(* MFreeBakeData) (void *bake_data)

Definition at line 56 of file multires_bake.c.

◆ MInitBakeData

typedef void*(* MInitBakeData) (MultiresBakeRender *bkr, ImBuf *ibuf)

Definition at line 55 of file multires_bake.c.

◆ MPassKnownData

typedef void(* MPassKnownData) (DerivedMesh *lores_dm, DerivedMesh *hires_dm, void *thread_data, void *bake_data, ImBuf *ibuf, const int face_index, const int lvl, const float st[2], float tangmat[3][3], const int x, const int y)

Definition at line 43 of file multires_bake.c.

◆ MultiresBakeQueue

◆ MultiresBakeResult

◆ MultiresBakeThread

Function Documentation

◆ apply_heights_callback()

static void apply_heights_callback ( DerivedMesh lores_dm,
DerivedMesh hires_dm,
void thread_data_v,
void bake_data,
ImBuf ibuf,
const int  tri_index,
const int  lvl,
const float  st[2],
float   UNUSEDtangmat[3][3],
const int  x,
const int  y 
)
static

◆ apply_tangmat_callback()

static void apply_tangmat_callback ( DerivedMesh lores_dm,
DerivedMesh hires_dm,
void UNUSEDthread_data,
void bake_data,
ImBuf ibuf,
const int  tri_index,
const int  lvl,
const float  st[2],
float  tangmat[3][3],
const int  x,
const int  y 
)
static

MultiresBake callback for normals' baking.

General idea:

  • Find coord and normal of point with specified UV in hi-res mesh.
  • Multiply it by tangmat.
  • Vector in color space would be norm(vec) / 2 + (0.5, 0.5, 0.5).

Definition at line 932 of file multires_bake.c.

References add_v3_v3(), CD_MLOOPUV, clamp_v2(), get_ccgdm_data(), DerivedMesh::getLoopDataArray, DerivedMesh::getLoopTriArray, DerivedMesh::getPolyArray, MPoly::loopstart, mul_v3_m3v3(), normalize_v3_length(), NULL, MNormalBakeData::orig_index_mp_to_orig, MLoopTri::poly, ImBuf::rect, ImBuf::rect_float, resolve_quad_uv_v2(), resolve_tri_uv_v2(), rgb_float_to_uchar(), usdtokens::st(), MPoly::totloop, MLoopTri::tri, MLoopUV::uv, x, ImBuf::x, and y.

Referenced by bake_images().

◆ bake_ibuf_filter()

static void bake_ibuf_filter ( ImBuf ibuf,
char *  mask,
const int  margin,
const char  margin_type,
DerivedMesh dm,
const float  uv_offset[2] 
)
static

◆ bake_ibuf_normalize_displacement()

static void bake_ibuf_normalize_displacement ( ImBuf ibuf,
const float displacement,
const char *  mask,
float  displacement_min,
float  displacement_max 
)
static

◆ bake_images()

static void bake_images ( MultiresBakeRender bkr,
MultiresBakeResult result 
)
static

◆ bake_rasterize()

static void bake_rasterize ( const MBakeRast bake_rast,
const float  st0_in[2],
const float  st1_in[2],
const float  st2_in[2] 
)
static

Definition at line 278 of file multires_bake.c.

References ceilf, MBakeRast::h, rasterize_half(), SWAP, w(), and MBakeRast::w.

Referenced by do_multires_bake_thread().

◆ count_images()

static void count_images ( MultiresBakeRender bkr)
static

◆ do_multires_bake()

static void do_multires_bake ( MultiresBakeRender bkr,
Image ima,
ImageTile tile,
ImBuf ibuf,
bool  require_tangent,
MPassKnownData  passKnownData,
MInitBakeData  initBakeData,
MFreeBakeData  freeBakeData,
MultiresBakeResult result 
)
static

Definition at line 454 of file multires_bake.c.

References MResolvePixelData::bake_data, MultiresBakeThread::bake_rast, BKE_id_free(), BKE_image_get_tile_uv(), BKE_mesh_calc_loop_tangent_ex(), BKE_mesh_new_nomain(), BKE_mesh_poly_normals_ensure(), BKE_mesh_vertex_normals_ensure(), MultiresBakeThread::bkr, BLI_spin_end(), BLI_spin_init(), BLI_system_thread_count(), BLI_threadpool_end(), BLI_threadpool_init(), BLI_threadpool_insert(), CD_MLOOPUV, CD_NORMAL, CD_ORCO, CD_TANGENT, CustomData_get_layer_index(), MultiresBakeThread::data, DM_get_loop_data_layer(), do_multires_bake_thread(), MultiresBakeRender::do_update, float(), flush_pixel(), DerivedMesh::getEdgeArray, DerivedMesh::getLoopArray, DerivedMesh::getLoopDataArray, DerivedMesh::getLoopTriArray, DerivedMesh::getNumEdges, DerivedMesh::getNumLoops, DerivedMesh::getNumLoopTri, DerivedMesh::getNumPolys, DerivedMesh::getNumVerts, DerivedMesh::getPolyArray, DerivedMesh::getVertArray, DerivedMesh::getVertDataArray, MResolvePixelData::h, handles, MultiresBakeThread::height_max, MultiresBakeThread::height_min, MResolvePixelData::hires_dm, MultiresBakeRender::hires_dm, MResolvePixelData::ibuf, MultiresBakeThread::image, init_bake_rast(), init_ccgdm_arrays(), DerivedMesh::loopData, MResolvePixelData::lores_dm, MultiresBakeRender::lores_dm, MResolvePixelData::lvl, MultiresBakeRender::lvl, max_ff(), Mesh::medge, MEM_callocN, MEM_freeN, min_ff(), Mesh::mloop, MResolvePixelData::mloop, MResolvePixelData::mlooptri, MResolvePixelData::mloopuv, Mesh::mpoly, MResolvePixelData::mpoly, Mesh::mvert, MResolvePixelData::mvert, NULL, MResolvePixelData::pass_data, MResolvePixelData::precomputed_normals, MResolvePixelData::pvtangent, queue, MultiresBakeThread::queue, result, DerivedMesh::tangent_mask, MResolvePixelData::thread_data, blender::compositor::threads, MultiresBakeRender::threads, tile, Mesh::totedge, Mesh::totloop, Mesh::totpoly, Mesh::totvert, MResolvePixelData::uv_offset, MResolvePixelData::vert_normals, MResolvePixelData::w, ImBuf::x, and ImBuf::y.

Referenced by bake_images().

◆ do_multires_bake_thread()

static void* do_multires_bake_thread ( void data_v)
static

◆ finish_images()

static void finish_images ( MultiresBakeRender bkr,
MultiresBakeResult result 
)
static

◆ flush_pixel()

static void flush_pixel ( const MResolvePixelData data,
const int  x,
const int  y 
)
static

◆ free_heights_data()

static void free_heights_data ( void bake_data)
static

Definition at line 801 of file multires_bake.c.

References MEM_freeN, DerivedMesh::release, and MHeightBakeData::ssdm.

Referenced by bake_images().

◆ free_normal_data()

static void free_normal_data ( void bake_data)
static

Definition at line 917 of file multires_bake.c.

References MEM_freeN.

Referenced by bake_images().

◆ get_ccgdm_data()

static void get_ccgdm_data ( DerivedMesh lodm,
DerivedMesh hidm,
const int *  index_mp_to_orig,
const int  lvl,
const MLoopTri lt,
const float  u,
const float  v,
float  co[3],
float  n[3] 
)
static

◆ init_bake_rast()

static void init_bake_rast ( MBakeRast bake_rast,
const ImBuf ibuf,
const MResolvePixelData data,
MFlushPixel  flush_pixel,
short *  do_update 
)
static

◆ init_ccgdm_arrays()

static void init_ccgdm_arrays ( DerivedMesh dm)
static

◆ init_heights_data()

static void* init_heights_data ( MultiresBakeRender bkr,
ImBuf ibuf 
)
static

◆ init_normal_data()

static void* init_normal_data ( MultiresBakeRender bkr,
ImBuf UNUSEDibuf 
)
static

◆ interp_barycentric_mlooptri()

static void interp_barycentric_mlooptri ( DerivedMesh dm,
MLoop mloop,
const MLoopTri lt,
const float  u,
const float  v,
const int  mode,
float  res[3] 
)
static

◆ interp_bilinear_grid()

static void interp_bilinear_grid ( CCGKey key,
CCGElem grid,
float  crn_x,
float  crn_y,
int  mode,
float  res[3] 
)
static

◆ interp_bilinear_mpoly()

static void interp_bilinear_mpoly ( DerivedMesh dm,
MLoop mloop,
MPoly mpoly,
const float  u,
const float  v,
const int  mode,
float  res[3] 
)
static

◆ multires_bake_queue_next_tri()

static int multires_bake_queue_next_tri ( MultiresBakeQueue queue)
static

Definition at line 357 of file multires_bake.c.

References BLI_spin_lock(), BLI_spin_unlock(), and queue.

Referenced by do_multires_bake_thread().

◆ multiresbake_get_normal()

static void multiresbake_get_normal ( const MResolvePixelData data,
const int  tri_num,
const int  vert_index,
float  r_normal[3] 
)
static

Definition at line 109 of file multires_bake.c.

References BKE_mesh_calc_poly_normal(), copy_v3_v3(), data, MPoly::flag, MPoly::loopstart, and ME_SMOOTH.

Referenced by flush_pixel().

◆ multiresbake_test_break()

static int multiresbake_test_break ( MultiresBakeRender bkr)
static

Definition at line 324 of file multires_bake.c.

References G, and MultiresBakeRender::stop.

Referenced by do_multires_bake_thread().

◆ rasterize_half()

static void rasterize_half ( const MBakeRast bake_rast,
const float  s0_s,
const float  t0_s,
const float  s1_s,
const float  t1_s,
const float  s0_l,
const float  t0_l,
const float  s1_l,
const float  t1_l,
const int  y0_in,
const int  y1_in,
const int  is_mid_right 
)
static

Definition at line 228 of file multires_bake.c.

References ceilf, fabsf, MBakeRast::h, set_rast_triangle(), SWAP, w(), MBakeRast::w, x, y, and y1.

Referenced by bake_rasterize().

◆ RE_multires_bake_images()

void RE_multires_bake_images ( MultiresBakeRender bkr)

◆ set_rast_triangle()

static void set_rast_triangle ( const MBakeRast bake_rast,
const int  x,
const int  y 
)
static