question

Amin Yavarimoghadam avatar image
Amin Yavarimoghadam asked ·

Why my costume OSL shader cause render time increase unusually ?!!!

Hi
I wrote an osl-shader and this is a simple 2d scratch generator.
When I assign it even to an aiUtility shader, the render time is so unusual. What may cause this?!!!

shaderosl
10 |600 characters needed characters left characters exceeded

Up to 5 attachments (including images) can be used with a maximum of 2.0 MiB each and 9.8 MiB total.

Mads Drøschler avatar image
Mads Drøschler answered ·
shader GMScratch

(



point UVW = point(u, v, 0),

float Scale = 1.0,

float Radius = 0.5 [[ float min=0.0, float max=2.0 ]],

float Width = 0.005 [[ float min=0.01, float max=1.0 ]],

float Hardness = 0.5 [[ float min=0.00, float max=1.0 ]],

float Rotate = 0.0,

float RotateJitter = 0.0,

float Distort = 0.0,

float DistortScale = 1.0,



output color OutColor = 0,



)

{

point pnt = UVW / Scale;

// Standard method for randomization:

// Go through a 3x3x3 grid that we offset

for (float x = -1; x <= 1; x++)

{

for (float y = -1; y <= 1; y++)

{

for(float z = -1; z <= 1; z++)

{

// Point that sources all our randomization

// We run these through cell noises to get

// random values

// MAXX-44914 fix: Adds the 0.001 offset and floor()

point rndpoint = floor(pnt) + point(x, y, z) ; //+ 0.001;

// Compute a center

point dotcenter = rndpoint + noise("cell", rndpoint, 1);



vector PointDiff = pnt - dotcenter;

float RotRand = noise("snoise", dotcenter );

vector distort = noise("snoise", pnt / DistortScale ) * Distort;

PointDiff += distort;

PointDiff = rotate (PointDiff, ((Rotate*2)+(RotateJitter*RotRand)), point(0, 0, 0), point(0, 0, 1));

PointDiff /= vector (1.0, Width*0.16+0.016, 1.0);

float f = 0.0;

f = 1 - smoothstep(Radius*clamp(Hardness, 0.25, 0.997), Radius/clamp(Hardness, 0.25, 0.997), length(PointDiff));

if (f > 0.0 )



{

OutColor += f ;



if (!1)

return;

}

}

}

}

}


This is 5-10 times faster on my machine, reduce the grid size, commented out the offset as well. but doesnt matter.

10 comments Share
10 |600 characters needed characters left characters exceeded

Up to 5 attachments (including images) can be used with a maximum of 2.0 MiB each and 9.8 MiB total.

Also, I would look into generating an external hash float instead of using the internal perlin. But that is just me.

0 Likes 0 · ·

Since I'm new to osl shader programming and I just started a few days ago, I'm just wondering why there is no rand() function? And I'm searching for a go0d method to do the same.

0 Likes 0 · ·
Mads Drøschler avatar image Mads Drøschler Amin Yavarimoghadam ·
float fract ( float x ){
return x - floor(x);
}

vector fract ( vector x ){
return x - floor(x);
}

float random (vector st) {
    return fract(sin(dot(st, vector(12.8,78.2,0)))* 48.5);
}



You can also extend to vec2 and vec3 and vec4 if you want.

Note you want to have fairly small decimals to work with to respect the sliding occuring with float precision drift.

0 Likes 0 · ·

Thank you for checking the shader. If I decrease the grid size it causes clipping of the shape of scratch. But it had a huge impact on render time. Thank you again.
I will check for a solution, but since we are here is there any other way to prevent that clipping effect?

0 Likes 0 · ·
Mads Drøschler avatar image Mads Drøschler Amin Yavarimoghadam ·

Where is this clipping you see?

0 Likes 0 · ·
qwe.png (223.4 KiB)

The problem will appear when u increase the radius.

GridSizeClipping.png

0 Likes 0 · ·
gridsizeclipping.png (268.3 KiB)
Show more comments
Amin Yavarimoghadam avatar image
Amin Yavarimoghadam answered ·

// Based on Candy.osl, by Zap Andersson

// Modified: 2020-03-18


shader GMScratch

(


point UVW = point(u, v, 0),

float Scale = 1.0,

float Radius = 0.5 [[ float min=0.0, float max=2.0 ]],

float Width = 0.005 [[ float min=0.01, float max=1.0 ]],

float Hardness = 0.5 [[ float min=0.00, float max=1.0 ]],

float Rotate = 0.0,

float RotateJitter = 0.0,

float Distort = 0.0,

float DistortScale = 1.0,


output color OutColor = 0,


)

{

point pnt = UVW / Scale;

// Standard method for randomization:

// Go through a 3x3x3 grid that we offset

for (float x = -3; x <= 3; x++)

{

for (float y = -3; y <= 3; y++)

{

for(float z = -3; z <= 3; z++)

{

// Point that sources all our randomization

// We run these through cell noises to get

// random values

// MAXX-44914 fix: Adds the 0.001 offset and floor()

point rndpoint = floor(pnt) + point(x, y, z) + 0.001;

// Compute a center

point dotcenter = rndpoint + noise("cell", rndpoint, 1);


vector PointDiff = pnt - dotcenter;

float RotRand = noise("snoise", dotcenter );

vector distort = noise("snoise", pnt / DistortScale ) * Distort;

PointDiff += distort;

PointDiff = rotate (PointDiff, ((Rotate*2)+(RotateJitter*RotRand)), point(0, 0, 0), point(0, 0, 1));

PointDiff /= vector (1.0, Width*0.16+0.016, 1.0);

float f = 0.0;

f = 1 - smoothstep(Radius*clamp(Hardness, 0.25, 0.997), Radius/clamp(Hardness, 0.25, 0.997), length(PointDiff));

if (f > 0.0 )


{

OutColor += f ;


if (!1)

return;

}

}

}

}

}

1 comment Share
10 |600 characters needed characters left characters exceeded

Up to 5 attachments (including images) can be used with a maximum of 2.0 MiB each and 9.8 MiB total.

This is the osl shader text. I couldn't upload .osl file.

0 Likes 0 · ·
Amin Yavarimoghadam avatar image
Amin Yavarimoghadam answered ·

Ty for your response @Heribert Raab

I'm new to osl shader programming and it is possible that I'm doing something wrong. But it happens even if I'm using osl shaders wrote by pro programmers. I think it's because of osl shader nature.
Is there any kind of rasterization method to output an osl shader in a specific resolution map inside Maya shading network?!!!
I think it will resolve any heavy post-process of osl shaders.

1 comment Share
10 |600 characters needed characters left characters exceeded

Up to 5 attachments (including images) can be used with a maximum of 2.0 MiB each and 9.8 MiB total.

Try share the shader so we can profile it.

0 Likes 0 · ·
Heribert Raab avatar image
Heribert Raab answered ·

depends on your OSL shader. OSL is slower a pre compiled C++ shader. also depends on your code, how optimized it is. a wrong line can drop rendertime drastically.
like for all renderers, the more procedural noises etc... more slower the rendering.

Share
10 |600 characters needed characters left characters exceeded

Up to 5 attachments (including images) can be used with a maximum of 2.0 MiB each and 9.8 MiB total.

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 5 attachments (including images) can be used with a maximum of 2.0 MiB each and 9.8 MiB total.

Welcome to the Arnold Answers community.

This is the place for Arnold renderer users everywhere to ask and answer rendering questions, and share knowledge about using Arnold, Arnold plugins, workflows and developing tools with Arnold.

If you are a new user to Arnold Answers, please first check out our FAQ and User Guide for more information.

When posting questions, please be sure to select the appropriate Space for your Arnold plugin and include the plugin version you are using.

Please include images, scene and log files whenever possible as this helps the community answer your questions.

Instructions for generating full verbosity log files are available for MtoA, MaxtoA, C4DtoA, HtoA, KtoA, and Kick.

If you are looking for Arnold Documentation and Support please visit the Arnold Support site.

To try Arnold please visit the Arnold Trial page.

Bottom No panel present for this section.