Blender V5.3
bake.cc File Reference

The API itself is simple. Blender sends a populated array of BakePixels to the renderer, and gets back an array of floats with the result. More...

#include <climits>
#include <cstring>
#include "BLI_string_ref.hh"
#include "MEM_guardedalloc.h"
#include "BLI_index_range.hh"
#include "BLI_math_geom_c.hh"
#include "BLI_math_matrix_c.hh"
#include "BLI_math_vector_c.hh"
#include "BLI_task.hh"
#include "BKE_attribute.hh"
#include "BKE_bvhutils.hh"
#include "BKE_customdata.hh"
#include "BKE_image.hh"
#include "BKE_lib_id.hh"
#include "BKE_mesh.hh"
#include "BKE_mesh_runtime.hh"
#include "BKE_mesh_tangent.hh"
#include "BKE_node.hh"
#include "IMB_imbuf.hh"
#include "IMB_imbuf_types.hh"
#include "RE_bake.h"
#include "RE_texture_margin.h"
#include "zbuf.h"

Go to the source code of this file.

Classes

struct  blender::BakeDataZSpan
struct  blender::TriTessFace

Namespaces

namespace  blender
 BMesh Iterators.

Functions

static void blender::store_bake_pixel (void *handle, int x, int y, float u, float v)
void blender::RE_bake_mask_fill (const BakePixel pixel_array[], const size_t pixels_num, char *mask)
void blender::RE_bake_margin (ImBuf *ibuf, char *mask, const int margin, const char margin_type, const Mesh *mesh, const StringRef uv_layer, const float uv_offset[2])
static void blender::calc_point_from_barycentric_cage (TriTessFace *triangles_low, TriTessFace *triangles_cage, const float mat_low[4][4], const float mat_cage[4][4], int primitive_id, float u, float v, float r_co[3], float r_dir[3])
static void blender::calc_point_from_barycentric_extrusion (TriTessFace *triangles, const float mat[4][4], const float imat[4][4], int primitive_id, float u, float v, float cage_extrusion, float r_co[3], float r_dir[3], const bool is_cage)
static void blender::barycentric_differentials_from_position (const float co[3], const float v1[3], const float v2[3], const float v3[3], const float dxco[3], const float dyco[3], const float facenor[3], const bool differentials, float *u, float *v, float *dx_u, float *dx_v, float *dy_u, float *dy_v)
static bool blender::cast_ray_highpoly (const bke::bvh::Tree **treeData, TriTessFace *triangle_low, TriTessFace *triangles[], BakePixel *pixel_array_low, BakePixel *pixel_array, const float mat_low[4][4], BakeHighPolyData *highpoly, const int highpoly_num, MutableSpan< std::optional< bke::bvh::RayHit > > hits, const float co[3], const float dir[3], const int pixel_id, const float max_ray_distance)
static TriTessFace * blender::mesh_calc_tri_tessface (Mesh *mesh, bool tangent, Mesh *mesh_eval)
bool blender::RE_bake_pixels_populate_from_objects (Mesh *me_low, BakePixel pixel_array_from[], BakePixel pixel_array_to[], BakeHighPolyData highpoly[], const int highpoly_num, const size_t pixels_num, const bool is_custom_cage, const float cage_extrusion, const float max_ray_distance, const float mat_low[4][4], const float mat_cage[4][4], Mesh *me_cage)
static void blender::bake_differentials (BakeDataZSpan *bd, const float *uv1, const float *uv2, const float *uv3)
static void blender::bake_pixels_add_denoise_margin (BakePixel pixel_array[], const BakeTargets *targets, int margin)
void blender::RE_bake_pixels_populate (Mesh *mesh, BakePixel pixel_array[], const size_t pixels_num, const BakeTargets *targets, const StringRef uv_layer, int margin)
static void blender::normal_compress (float out[3], const float in[3], const eBakeNormalSwizzle normal_swizzle[3])
void blender::RE_bake_normal_world_to_tangent (const BakePixel pixel_array[], const size_t pixels_num, const int depth, float result[], Mesh *mesh, const eBakeNormalSwizzle normal_swizzle[3], const float mat[4][4])
void blender::RE_bake_normal_world_to_object (const BakePixel pixel_array[], const size_t pixels_num, const int depth, float result[], Object *ob, const eBakeNormalSwizzle normal_swizzle[3])
void blender::RE_bake_normal_world_to_world (const BakePixel pixel_array[], const size_t pixels_num, const int depth, float result[], const eBakeNormalSwizzle normal_swizzle[3])
void blender::RE_bake_ibuf_clear (Image *image, const bool is_tangent)
int blender::RE_pass_depth (const eScenePassType pass_type)

Detailed Description

The API itself is simple. Blender sends a populated array of BakePixels to the renderer, and gets back an array of floats with the result.

Development Notes for External Engines

The Bake API is fully implemented with Python rna functions. The operator expects/call a function:

def bake(scene, object, pass_type, object_id, pixel_array, pixels_num, depth, result)

  • scene: current scene (Python object)
  • object: object to render (Python object)
  • pass_type: pass to render (string, e.g., "COMBINED", "AO", "NORMAL", ...)
  • object_id: index of object to bake (to use with the pixel_array)
  • pixel_array: list of primitive ids and barycentric coordinates to bake(Python object, see bake_pixel).
  • pixels_num: size of pixel_array, number of pixels to bake int.
  • depth: depth of pixels to return (int, assuming always 4 now).
  • result: array to be populated by the engine (float array, #PyLong_AsVoidPtr).
Note
Normals are expected to be in World Space and in the +X, +Y, +Z orientation.

BakePixel data structure

pixel_array is a Python object storing BakePixel elements:

struct BakePixel {
int primitive_id, object_id;
float uv[2];
float du_dx, du_dy;
float dv_dx, dv_dy;
};

In python you have access to:

  • primitive_id, object_id, uv, du_dx, du_dy, next.
  • next() is a function that returns the next #BakePixel in the array.
Note
Pixels that should not be baked have primitive_id == -1.

For a complete implementation example look at the Cycles Bake commit.

Definition in file bake.cc.