PCG GPU HLSL - How to split input points into two Output Pins

Hello,

I was playing around with the Custom HLSL node, and there is something I can’t get my head around. I’m trying to take points that each have a boolean attribute and split them accordingly into two separate output pins - True into OutputPin1 and False into OutputPin2.

Thanks in advance,

Best regards,

Adam

[Attachment Removed]

Steps to Reproduce[Attachment Removed]

Hi,

A custom HLSL node cannot dynamically route points to different output pins based on a condition, but you can achieve something similar by using a custom HLSL node (with a single output), followed by an Attribute Filter node which performs the split based on the boolean attribute.

Below are some screenshots of a simple PCG graph which modifies the color of points in a 3D grid depending on whether they lie inside or outside a sphere (which is calculated in the custom HLSL node as the distance from each point to the grid’s center). The HLSL node outputs the entire set of points that were passed into its input, but adds an attribute to them that the filter node uses to split them into two point sets. The result depends on what gets piped into the final Debug node (although this example does not use a boolean value, it should be possible to adapt the condition to mimic a boolean with a float):

[Image Removed]

The code in the first HLSL node (Point Generator):

float3 Position = CreateGrid3D(ElementIndex, NumPoints, -500, 500);
float3 Scale = float3(.2, .2, .2);
 
Out_SetScale(Out_DataIndex, ElementIndex, Scale);
Out_SetPosition(Out_DataIndex, ElementIndex, Position);

Code in the second HLSL node (Point Processor):

float3 Location = Location_GetFloat3(In_DataIndex, 0, 'Location');
float3 PointPosition = In_GetPosition(In_DataIndex, ElementIndex);
float Dist = distance(Location, PointPosition);
float Density = 1;
 
if (Dist > 500) 
{
    Density = 0;
} 
 
Out_SetDensity(Out_DataIndex, ElementIndex, Density);

Please let me know if this helps or if you have more questions.

Thanks,

Sam

[Attachment Removed]

First of all, thank you for your answer, and sorry for the late response.

I’ve already considered the implementation you suggested. Still, I’m trying to avoid the GPU readback that occurs when I use the FilterAttributeElements node, since that node is locked to the CPU. What I was trying to achieve is to load the data on the GPU once and then do all of the processing there. So, is there any way I could do this on the GPU only?

I was also wondering how I can make the second output work in general. Is there any example of how it can be initialized and used? Even if it can’t solve my initial question, I’m still kinda wondering how you can make it work, since every time I’ve tried something with it, I get errors.

[Image Removed]

Thanks,

Adam

[Attachment Removed]

Hi,

A PCG Custom HLSL node cannot directly compute new point sets and output them to two different pins as it cannot perform real branching. Branching can only be done at the graph level (not inside the HLSL node), but if you want to perform all graph computations (or as many as possible) on the GPU side, you can add a second custom HLSL node and duplicate the input set into both nodes, as shown in the screenshots below (the HLSL code for the relevant nodes is also shown).

[Image Removed]

From there you should be able to process each processed point set further by adding another HLSL node which removes some of the points, similarly to when using a FilterAttributeElements node:

[Image Removed]

Alternatively, you can add two HLSL nodes to the output of the first one and split the points based on some condition (using RemovePoint in each node):

[Image Removed]

To get rid of the error when creating a node with two (or more) outputs, just add some code that targets the second output, as shown here:

[Image Removed]

I hope that helps, but let me know if you have further questions.

Thanks,

Sam

[Attachment Removed]

Helps a lot, thank you!

Cheers,

Adam

[Attachment Removed]

Glad it helped. I will close this case for now, but please reopen it if you have more questions.

Thanks,

Sam

[Attachment Removed]