How can I store global variables one by one in an array?
トマトフライ
I saw your github and contacted you. Is this the right place to post my question? If I'm wrong, please delete it. I would like to ask the following questions. I am trying to create a function in an OSL script that will evaluate the Coordinate of one of the "global variables P" and the Coordinate of the other. I have read and checked the OSL specifics, but I could not find any description of how to extract an array from a global variable. If possible, could you please tell me how to call the Coordinate one by one from the "global variable P"? |
|
Larry Gritz
I'm not sure what you mean by the "coordinate of P" or what you mean by "an array from a global variable." Can you explain in more detail, or maybe show a few lines of code illustrating what you want to do?
The "global variable" P is just the position of the single point that is being shaded by that invocation of the shader. Do you mean getting at the individual x, y, and z values for that point using an array notation? You can just say P[0] for the x coordinate (this is equivalent to saying P.x), P[1] for the y coordinate (same as P.y), and P[2] for the z coordinate (same as P.z).
|
|
トマトフライ
I replied to the email that was sent to me, but I'm not sure if it was for sending only.
I'm not sure if you received it, so I'll send it to this thread as well.
I'm sorry for the confusing question.
I will explain based on the reply you gave me.
First of all, in OSL, if you simply call "P", you will be calling a set of shading points P, I think.
This means that you don't know how many shading points there are in the scene, and you can't read the numerical values of the coordinates of each shading point.
What I want to do is to take one arbitrary coordinate from the set of shading points (like a normal point type) and store that coordinate in an array. Then I want to use the length of P as the conditional expression in the for statement, and in that I want to take an element from the array and compare it to the other P to create a map.
Since what I want to do is declare a variable, so to speak, I can't show any code in OSL that would change this.
If I were to express it in C instead
int P_length = sizeof(P.x) / sizeof(P.x[0]);
float P_xval =P.x[103];
and so on.
Or, if I can access the coordinates of every single point in the set of P, I may be able to omit this kind of code.
Maybe it exists as a member of the structure P.
Is it possible to program like this in OSL?
And do such variables exist?
That's what I want to ask.
And one more thing, I am sorry to repeat this question.
It seems that the global variables contain elements like members in c language.
Is there a site or a way to view all of them? I would appreciate it if you could help me.
Thank you. |
|
Larry Gritz
The programming model (how you think of the shader operating) is that you are shading one point at a time. That's all that is exposed. Whether the system is actually shading one or multiple points at the same time (and if they are synchronized in any meaningful way) depends on the implementation and which "back end" you may be using. The ordinary CPU based back end that almost all OSL renderers use REALLY IS just shading one point at a time (though multiple threads may be shading different points simultaneously, which are not necessarily the same material or shader). The "CPU batched shading" that Intel has ~95% complete shades up to 8 or 16 points of the same material simultaneously (depending on the kind of CPU you have). The Cuda-based GPU back end may shade LOTS of points, but even in that case, it's not ALL the points that will be shaded, and groups of points in parallel don't necessarily know about and are not necessarily synchronized with other groups of points in the same overall kernel launch. So in short, OSL just doesn't present this kind of interface, and most renderers currently in operation do not work underneath in a way that would be well matched to accessing all points simultaneously at the same point of execution of the shader.
|
|
トマトフライ
Thank you very much for your detailed answer.
I now understand what I misunderstood about shading points.
I apologize for taking time out of your busy schedule. |
|
Larry Gritz
Can you tell us more about what you were TRYING to do? Not just the "what", but I really mean the "why." What task were you trying to accomplish, for which you hypothesized that the solution was needing to know all the P values of all shaded points at once?
Maybe there's another way to solve your real problem.
|
|
トマトフライ
I know this is a silly thing to talk to a developer about.
I would appreciate it if you could share your wisdom.
I am trying to recreate a Raytraced Hard Shadow in Cycles in Blender.
https://github.com/unity3d-jp/RaytracedHardShadow
In the original OSL, this can be easily implemented using trace().
However, Cycles does not accept "shade" parameter input.
Therefore, the result of trace() is very dirty.
So I was looking for a way to calculate shadows without relying on the "Shade" parameter.
By letting the trace rays penetrate self geometry, I was able to determine the shadows falling from other objects.
Used inner product to determine P not facing the light.
However, I was unable to achieve a way to determine the shadow cast by the self geometry on self, which is why I asked the question.
If we have an array containing P, we can determine if light is being blocked by the difference in the inner product of the vector toward the light source.
With this in mind, I asked you the question, but I regret that I could not realize it.
Would what I have in mind be realized by other means?
I hope that will be the case.
Thank you. |
|
Larry Gritz
Hi, sorry for letting this sit for a long time without a reply, but I'm still not sure I understand what you're trying to do.
If you are tracing rays in some order, when tracing one particular ray, you don't yet know where all the other rays (that you haven't trace yet) are going to hit. So you don't know when you're shading what all the other P's will necessarily be, if that's what you're trying to find out. Are you asking for the vertices of the mesh of the geometry that you hit? Are you just trying to find out which side of the surface a light is? The variable Ng should be the oriented surface normal, and you can compare that to the direction of the light to determine whether the light (or any other point) is in front of or behind the surface. Also dPdu and dPdv are the tangents of the surface at the shading point, and those can also be used to figure out the orientations.
|
|