Help: Information on Cycles Bake code

Hey everybody, I just built Blender for my first time and I’m looking for a first project. I think I’d like to work on adding support for baking to vertex colors for Cycles, since it was possible with Blender Render in 2.79, but is no longer possible in 2.8.

I’m having trouble finding information on Cycles bake code. I’ve looked through the information on the project page at developer.blender.org and can’t find anything on baking. I’ve been trying to figure out bake.h and bake.cpp, and I’m not sure what I’m looking at.

If I understand correctly, the code sets up the bake operation by figuring out which pixel (in the output image) belongs to each triangle of the object (it looks like Blender and Cycles are talking back and forth at this point- Blender sets the bake data and the properties of the Bake Manager in blender_session.cpp). Some information is saved about the surface of the object at that point on its surface (I think BakeData.data is the primitive and the uv’s, and BakeData.differentials is the tangent and bitangent of that point? I’m not formally educated in higher math, but eager to learn more) Then, it runs a rendering task for each pixel for the number of samples that Blender requests. This is saved to d_output, which is then copied into result, which looks is an array of floats that are each one channel of a pixel (so the data looks like r,g,b,a,r,g,b,a,r,g,b,a,r,g,b,a in the result).

What confuses me is that I don’t see anything use result. It isn’t a pointer or a reference, so it’s being passed by value, isn’t it? And I don’t see it being returned or referenced anywhere… so how does the result get out of the function? How does Blender get ahold of it?

What I want to do is bake to vertex colors, so I think it should be possible to bake only the pixels at the corners of each triangle, and use the result as a vertex color for the associated vertex-face-corner (loop is what it’s called in mesh Python API).

Am I far off the mark in my understanding of this piece of code? Any advice on finding more info? And most importantly, is this too hard for a first project?
Thanks!

For vertex color baking, the main work would be on the Blender side, in object_bake.c and object_bake_api.c.

You’re right about the approach to doing this. Rather than setting the face index + barycentric uv coordinates corresponding to a pixel, you’d set them for a vertex. So that means a face index + barycentric uv corresponding to the corner of a face where the vertex is.

Regarding the Cycles side, BakeManager::bake has a result parameter, that’s where the output gets written to. But I think vertex color baking may not require any code changes in Cycles at all.

1 Like

Thanks for the reply! I’ve been looking through the files you mentioned, and I think it will take me a few days to read them and still a few more to understand.

I was confused by result because I thought it was being passed-by-value, but it looks like Blender is giving it a NULL*, so I suppose Cycles is writing the pixels directly to the memory? I think I can figure it out, but it looks like I probably don’t have to in order to do what I want. It will take a while to learn, though.

Thanks again.