Community
Arnold for 3ds Max
Rendering with Arnold in 3ds Max using the MaxtoA plug-in.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

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

16 REPLIES 16
SOLVED
Reply
Message 1 of 17
Anonymous
529 Views, 16 Replies

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?!!!

Tags (2)
Labels (2)
16 REPLIES 16
Message 2 of 17
schwungsau
in reply to: Anonymous

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.

Message 3 of 17
Anonymous
in reply to: Anonymous

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.

Message 4 of 17
madsd
in reply to: Anonymous

Try share the shader so we can profile it.

Message 5 of 17
Anonymous
in reply to: Anonymous

// 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;

}

}

}

}

}

Message 6 of 17
Anonymous
in reply to: Anonymous

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

Message 7 of 17
madsd
in reply to: Anonymous

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.

Message 8 of 17
madsd
in reply to: madsd

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

Message 9 of 17
Anonymous
in reply to: madsd

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?

Message 10 of 17
Anonymous
in reply to: madsd

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.

Message 11 of 17
madsd
in reply to: Anonymous

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.

Message 12 of 17
madsd
in reply to: Anonymous

Where is this clipping you see?

6345-qwe.png

Message 13 of 17
Anonymous
in reply to: madsd

The problem will appear when u increase the radius.

GridSizeClipping.png

Message 14 of 17
madsd
in reply to: Anonymous

Try 2x2x2 grid then, looks like its only happening if you go down to 1x1x1.

Message 15 of 17
Anonymous
in reply to: madsd

It depends on the radius. But 2x2x2 is enough in most cases.
I made a variable to change it in various scenarios.

Message 16 of 17
madsd
in reply to: Anonymous

You can also remove the volume and use u and v, reduce more calls.
Since you only probe u and v in position, you dont need an evaluation over z over anything, anywhere.

Message 17 of 17
Anonymous
in reply to: madsd

I removed z position evaluation and that made a huge difference.
I'll polish the code more further. I'm thinking about code it in python and also I have no idea how to do it. I will figure it out tomorrow.
Thank you again for your guidance.

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report