vop structs set element equivalent

   1899   3   0
User Avatar
Member
85 posts
Joined: Aug. 2010
Offline
Hi there lovely community

I am using structs in vops for our uber shader. They are great but I have run across a basic problem

Does anyone have an idea how to modify a single element in a custom struct by index either in vops without unpacking and repacking the whole thing?

I want something similar to the set element or set vector component vop

or alternatively a vex equivalent to the syntax @P.x

any ideas?

many thanks
Sam Swift-Glasman
Art Director
Five AI
User Avatar
Staff
1449 posts
Joined: July 2005
Offline
I think you are best off using the Struct VOP to modify the members.

In the attached file, see ‘flexible_modify’ Struct VOP. It does however make a new copy of the struct and then modifies it.

You may be tempted to make it more efficient by createing a custom HDA, ‘efficient_modify’ VOP. It modifies the struct in-place, but has an unintended consequences. If you look at ‘access_wrong_value’, it would seem that Z should be 1, and yet it is 2 because of such in-place edit.

So, in short, you should probably use Struct VOP as in ‘flexible_modify’.

Attachments:
modify_struct.hip (82.5 KB)

User Avatar
Member
85 posts
Joined: Aug. 2010
Offline
hi rafal

thanks for your examples - very informatve

two points from my end:

1) - @Struct.member syntax is great to know -

does the unexpected behavior in the efficient_modify method happen outside of the local function scope? - ie outside the current vop - if so this is not prohibitive in this case as it is my last operation - i will try to test tomo

2) in my case I am actually trying to dynamically replace a different elemnt in my struct by index. ( all elements are of uniform type vector 4 so type is not an issue). Any vop method would require a prohibitive amount of switches as i could not find a single inut to many outputs switch

Otherwise in vex is there a way of getting and setting the value by index value? I realise the @Struct.element syntax would not work unless i could maybe using hscript `expressions` ? ie @mystruct.`$myindex` and the form @mystruct.1 was valid?

is that supported?
Sam Swift-Glasman
Art Director
Five AI
User Avatar
Staff
1449 posts
Joined: July 2005
Offline
1) since the structs are passed as reference to functions, the unintended behaviour will happen outside of the local function scope.

2) structs are by nature static (ie, member list is not expandable) and they use member names (rather than indices) for referencing data. Perhaps arrays are better suited for dynamic problems.

However, if you use consistent naming convention then there are tricks you can play in VOPs, as illustrated in the new attachment: use VOP parameter to determine variable name used in the generated code. Since the variable is determined at compile-time, you can't wire in any index whose value is calculated at runtime, though. This may be enough, and in theory allows arbitrary number of members in a struct, since you can always type in an arbitrary “index” in the parameter, and the VOP will work.

For runtime indexing you will need a proper if-else statement in the code. The advantage is you can determine the modified member at runtime. The downside is that the number of members is predetermined and limited to the number handled in the code, so any expansion will require updating the VOP definition (code). However, most structs have a pre-fixed members anyway (the exception being ad-hoc packed structs), so that downside is not a big one.

Finally, you can make the non-safe struct modification into a safe one by naming the HDA output struct differently, assigning to it the value of an input struct, and then operating in the code on the output struct. The gained safety will outweigh the efficiency benefits in the long run. So, unlike the previous attachment, this one uses this safety assignment.

Attachments:
modify_struct-2.hip (87.4 KB)

  • Quick Links