Cinema4D's break point in BakeTexture

On 04/07/2017 at 14:51, xxxxxxxx wrote:

User Information:
Cinema 4D Version:   R18 
Platform:   Windows  ;   
Language(s) :     C++  ;

---------
Hello.

I've got some problems when I try to use BakeTexture for a sub-shader in a ShaderData plugin.

Here is the scenario. I have a ShaderData plugin with a shaderlink parameter.  The shader set in that shaderlink parameter has to be baked.

So, in InitRender of the ShaderData, I use BakeTexture in the inner shader and I get a breakpoint here:

CINEMA 4D.exe!00007ff6c91528ce() Unknown
CINEMA 4D.exe!00007ff6c915283d() Unknown
c4dplugin.xdl64!00007ff89d020528() Unknown
c4dplugin.xdl64!00007ff89e92fe49() Unknown
c4dplugin.xdl64!00007ff89d050c43() Unknown
sla.cdl64!00007ff89019b52f() Unknown
sla.cdl64!00007ff890199e29() Unknown
<..... My method call that uses BakeTexture.......>

This problem with BakeTexture appears ONLY in the ShaderData's InitRender.
I use the same texture baking code in other places and everything works fine. 
I use the thread and document that are passed in InitRender.
Once I continue after the break point, the returned value of BakeTexture is BAKE_TEX_ERR_NONE.
The BakeTexture's result is correct.

How can I find the problem here ?

Thank you very much for your time.

On 05/07/2017 at 09:15, xxxxxxxx wrote:

Hi,

you gave me some headache here. Actually I was wondering, how do you use BakeTexture() to bake a shader? And even worse, where do you get the needed infos, when in InitRender()?
So I came up with my own solution, which does not show any problems. The sub-shader gets baken without any breakpoint or crash.
Maybe you can tell us, what exactly you are doing in InitRender()?

Here's roughly what I did:

- chn argument -> the BaseDocument.
- VolumeData (in irs argument) -> RayObject and TexData.
- TexData -> TextureTag and from this BaseObject
- Cloned the object (including tags, these tex and uvw tags will be used later on) and the sub-shader
- Created a new document, inserted cloned object
- Created new material
- Inserted the cloned shader and assigned it to the color channel (actually same channel the sub-shader is assigned to)
- Inserted new material into new document
- Set material of tex tag on cloned object to refer to new material

With all of this I was able to use InitBakeTexture() and BakeTexture() in InitRender to get a proper image of a noise shader without any breakpoints.

On 17/07/2017 at 03:22, xxxxxxxx wrote:

Hello and thank you very much for your support !

I don't use the VolumeData passed in InitRender's InitRenderStruct.
I create a dummy plane from which I retrieve the uvw tag that will be used later on InitBakeTexture:

	
	BaseContainer plane_data;
	plane_data.InsData(PRIM_PLANE_SUBW,GeData(1));
	plane_data.InsData(PRIM_PLANE_SUBH,GeData(1));
  
	BaseObject* plane = GeneratePrimitive(dummy_document, Oplane, plane_data, 1.0, false, baking_thread);
	ModelingCommandData command_data;
	command_data.doc = dummy_document;
	command_data.op = plane;
  
	if (!SendModelingCommand(MCOMMAND_CURRENTSTATETOOBJECT,command_data)) {
		BaseObject::Free(plane);
		return false;
	}
	BaseObject::Free(plane);
  
	BaseObject* polygon_plane = static_cast<BaseObject *>(command_data.result->GetIndex(0));
	bake_uvw_tag = (UVWTag * )polygon_plane->GetTag(Tuvw,0);
  
	bake_texture_tag = TextureTag::Alloc();
	bake_texture_tag->SetParameter(DescID(TEXTURETAG_PROJECTION),TEXTURETAG_PROJECTION_UVW,DESCFLAGS_SET_0);
	polygon_plane->InsertTag(bake_texture_tag);   
	dummy_document->InsertObject(polygon_plane,NULL,NULL);

The dummy_document in the above code is the cloned one. bake_uvw_tag and bake_texture_tag are used in InitBakeTexture.
So basically I skip the steps 2,3,4 in your post and I replace them with the code above. 
Does this cause any problem ?

Thank you.

On 18/07/2017 at 07:53, xxxxxxxx wrote:

Ok, by my approach I somewhat shot myself in the knee... sorry!

As I took the information from the VolumeData, I baked only if the VolumeData was existent. Obviously missing the case, where no VolumeData is available, which is the case when the shader preview gets rendered/prepared.

In this case there's a threading issue, as BakeTexture() internally wants to do something, it is not allowed from a worker thread. So there's unfortunately no solution in this case.

But lets look at it from a different perspective. Why do you need to bake? Couldn't you just sample the sub-shaders? If this could be an option, the BaseShader manual has a code snippet demonstrating how to do it properly.

On 19/07/2017 at 02:09, xxxxxxxx wrote:

Hello.

The thread that is used for baking is NULL (taken from InitRenderStruct).
I use texture baking because I want to produce an image based on that shader.
I could use sampling, but isn't it slower ?

Thank you for your time Andreas !

On 19/07/2017 at 09:18, xxxxxxxx wrote:

I haven't done any measurements and in the end it may probably depend on the scenario. But when baking the shader needs to be sampled as well.

On 20/07/2017 at 00:27, xxxxxxxx wrote:

Hello Andreas.

Allow me to ask one last thing before replacing texture baking with sampling.
Do they produce the same result if set correctly ? 
If I'm about to replace texture baking with sampling, I need them to produce the exact same image.

Thank you again !

On 20/07/2017 at 01:24, xxxxxxxx wrote:

Well, that depoends highly on the scenario, especially on the bake settings. With sampling you won't get any lighting information or effects.